S32 SDK
crc_hal.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016, Freescale Semiconductor, Inc.
3  * Copyright 2016 NXP
4  * All rights reserved.
5  *
6  * THIS SOFTWARE IS PROVIDED BY NXP "AS IS" AND ANY EXPRESSED OR
7  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
8  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
9  * IN NO EVENT SHALL NXP OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
10  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
11  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
12  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
13  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
14  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
15  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
16  * THE POSSIBILITY OF SUCH DAMAGE.
17  */
18 
19 #ifndef CRC_HAL_H
20 #define CRC_HAL_H
21 
24 #include <stddef.h>
25 #include <stdbool.h>
26 #include "device_registers.h"
27 
35 /*******************************************************************************
36  * Definitions
37  ******************************************************************************/
42 typedef enum
43 {
49 
54 typedef enum
55 {
56  CRC_BITS_16 = 0U,
59 
60 /*******************************************************************************
61  * API
62  ******************************************************************************/
68 #if defined(__cplusplus)
69 extern "C" {
70 #endif
71 
85 void CRC_HAL_Init(CRC_Type * const base);
86 
102 uint32_t CRC_HAL_GetCrc32(CRC_Type * const base,
103  uint32_t data,
104  bool newSeed,
105  uint32_t seed);
106 
122 uint32_t CRC_HAL_GetCrc16(CRC_Type * const base,
123  uint16_t data,
124  bool newSeed,
125  uint32_t seed);
126 
142 uint32_t CRC_HAL_GetCrc8(CRC_Type * const base,
143  uint8_t data,
144  bool newSeed,
145  uint32_t seed);
146 
155 uint32_t CRC_HAL_GetCrcResult(const CRC_Type * const base);
156 
166 static inline uint32_t CRC_HAL_GetDataReg(const CRC_Type * const base)
167 {
168  return base->DATAu.DATA;
169 }
170 
180 static inline void CRC_HAL_SetDataReg(CRC_Type * const base,
181  uint32_t value)
182 {
183  base->DATAu.DATA = value;
184 }
185 
195 static inline uint16_t CRC_HAL_GetDataHReg(const CRC_Type * const base)
196 {
197  return base->DATAu.DATA_16.H;
198 }
199 
209 static inline void CRC_HAL_SetDataHReg(CRC_Type * const base,
210  uint16_t value)
211 {
212  base->DATAu.DATA_16.H = value;
213 }
214 
224 static inline uint16_t CRC_HAL_GetDataLReg(const CRC_Type * const base)
225 {
226  return base->DATAu.DATA_16.L;
227 }
228 
238 static inline void CRC_HAL_SetDataLReg(CRC_Type * const base,
239  uint16_t value)
240 {
241  base->DATAu.DATA_16.L = value;
242 }
243 
253 static inline void CRC_HAL_SetDataHUReg(CRC_Type * const base,
254  uint8_t value)
255 {
256  base->DATAu.DATA_8.HU = value;
257 }
258 
268 static inline void CRC_HAL_SetDataHLReg(CRC_Type * const base,
269  uint8_t value)
270 {
271  base->DATAu.DATA_8.HL = value;
272 }
273 
283 static inline void CRC_HAL_SetDataLUReg(CRC_Type * const base,
284  uint8_t value)
285 {
286  base->DATAu.DATA_8.LU = value;
287 }
288 
298 static inline void CRC_HAL_SetDataLLReg(CRC_Type * const base,
299  uint8_t value)
300 {
301  base->DATAu.DATA_8.LL = value;
302 }
303 
313 static inline uint32_t CRC_HAL_GetPolyReg(const CRC_Type * const base)
314 {
315  return base->GPOLY;
316 }
317 
327 static inline void CRC_HAL_SetPolyReg(CRC_Type * const base,
328  uint32_t value)
329 {
330  base->GPOLY = value;
331 }
332 
343 static inline uint16_t CRC_HAL_GetPolyHReg(const CRC_Type * const base)
344 {
345  return (uint16_t)((base->GPOLY & CRC_GPOLY_HIGH_MASK) >> CRC_GPOLY_HIGH_SHIFT);
346 }
347 
358 static inline void CRC_HAL_SetPolyHReg(CRC_Type * const base,
359  uint16_t value)
360 {
361  uint32_t gpolyTemp = base->GPOLY;
362 
363  gpolyTemp &= ~(CRC_GPOLY_HIGH_MASK);
364  gpolyTemp |= CRC_GPOLY_HIGH(value);
365  base->GPOLY = gpolyTemp;
366 }
367 
377 static inline uint16_t CRC_HAL_GetPolyLReg(const CRC_Type * const base)
378 {
379  return (uint16_t)((base->GPOLY & CRC_GPOLY_LOW_MASK) >> CRC_GPOLY_LOW_SHIFT);
380 }
381 
391 static inline void CRC_HAL_SetPolyLReg(CRC_Type * const base,
392  uint16_t value)
393 {
394  uint32_t gpolyTemp = base->GPOLY;
395 
396  gpolyTemp &= ~(CRC_GPOLY_LOW_MASK);
397  gpolyTemp |= CRC_GPOLY_LOW(value);
398  base->GPOLY = gpolyTemp;
399 }
400 
412 static inline bool CRC_HAL_GetSeedOrDataMode(const CRC_Type * const base)
413 {
414  return ((base->CTRL & CRC_CTRL_WAS_MASK) >> CRC_CTRL_WAS_SHIFT) != 0U;
415 }
416 
428 static inline void CRC_HAL_SetSeedOrDataMode(CRC_Type * const base,
429  bool enable)
430 {
431  uint32_t ctrlTemp = base->CTRL;
432 
433  ctrlTemp &= ~(CRC_CTRL_WAS_MASK);
434  ctrlTemp |= CRC_CTRL_WAS(enable ? 1UL : 0UL);
435  base->CTRL = ctrlTemp;
436 }
437 
451 static inline bool CRC_HAL_GetFXorMode(const CRC_Type * const base)
452 {
453  return ((base->CTRL & CRC_CTRL_FXOR_MASK) >> CRC_CTRL_FXOR_SHIFT) != 0U;
454 }
455 
467 static inline void CRC_HAL_SetFXorMode(CRC_Type * const base,
468  bool enable)
469 {
470  uint32_t ctrlTemp = base->CTRL;
471 
472  ctrlTemp &= ~(CRC_CTRL_FXOR_MASK);
473  ctrlTemp |= CRC_CTRL_FXOR(enable ? 1UL : 0UL);
474  base->CTRL = ctrlTemp;
475 }
476 
488 static inline crc_bit_width_t CRC_HAL_GetProtocolWidth(const CRC_Type * const base)
489 {
490  crc_bit_width_t retVal = CRC_BITS_16;
491 
492  if (((base->CTRL & CRC_CTRL_TCRC_MASK) >> CRC_CTRL_TCRC_SHIFT) != 0U)
493  {
494  retVal = CRC_BITS_32;
495  }
496 
497  return retVal;
498 }
499 
511 static inline void CRC_HAL_SetProtocolWidth(CRC_Type * const base,
512  crc_bit_width_t width)
513 {
514  uint32_t ctrlTemp = base->CTRL;
515 
516  ctrlTemp &= ~(CRC_CTRL_TCRC_MASK);
517  ctrlTemp |= CRC_CTRL_TCRC(width);
518  base->CTRL = ctrlTemp;
519 }
520 
530 static inline crc_transpose_t CRC_HAL_GetWriteTranspose(const CRC_Type * const base)
531 {
532  crc_transpose_t type;
533  uint32_t temp = (base->CTRL & CRC_CTRL_TOT_MASK) >> CRC_CTRL_TOT_SHIFT;
534 
535  switch (temp)
536  {
537  case 1U:
538  type = CRC_TRANSPOSE_BITS;
539  break;
540  case 2U:
542  break;
543  case 3U:
544  type = CRC_TRANSPOSE_BYTES;
545  break;
546  default:
547  type = CRC_TRANSPOSE_NONE;
548  break;
549  }
550 
551  return type;
552 }
553 
563 static inline void CRC_HAL_SetWriteTranspose(CRC_Type * const base,
564  crc_transpose_t transp)
565 {
566  uint32_t ctrlTemp = base->CTRL;
567 
568  ctrlTemp &= ~(CRC_CTRL_TOT_MASK);
569  ctrlTemp |= CRC_CTRL_TOT(transp);
570  base->CTRL = ctrlTemp;
571 }
572 
582 static inline crc_transpose_t CRC_HAL_GetReadTranspose(const CRC_Type * const base)
583 {
584  crc_transpose_t type;
585  uint32_t temp = (base->CTRL & CRC_CTRL_TOTR_MASK) >> CRC_CTRL_TOTR_SHIFT;
586 
587  switch (temp)
588  {
589  case 1U:
590  type = CRC_TRANSPOSE_BITS;
591  break;
592  case 2U:
594  break;
595  case 3U:
596  type = CRC_TRANSPOSE_BYTES;
597  break;
598  default:
599  type = CRC_TRANSPOSE_NONE;
600  break;
601  }
602 
603  return type;
604 }
605 
615 static inline void CRC_HAL_SetReadTranspose(CRC_Type * const base,
616  crc_transpose_t transp)
617 {
618  uint32_t ctrlTemp = base->CTRL;
619 
620  ctrlTemp &= ~(CRC_CTRL_TOTR_MASK);
621  ctrlTemp |= CRC_CTRL_TOTR(transp);
622  base->CTRL = ctrlTemp;
623 }
624 
627 #if defined(__cplusplus)
628 }
629 #endif
630 
633 #endif /* CRC_HAL_H */
634 /*******************************************************************************
635  * EOF
636  ******************************************************************************/
#define CRC_CTRL_TOT_SHIFT
Definition: S32K144.h:2120
union CRC_Type::@1 DATAu
static void CRC_HAL_SetWriteTranspose(CRC_Type *const base, crc_transpose_t transp)
Sets the CRC transpose type for writes.
Definition: crc_hal.h:563
#define CRC_CTRL_TOT(x)
Definition: S32K144.h:2122
#define CRC_CTRL_TCRC_SHIFT
Definition: S32K144.h:2104
static crc_transpose_t CRC_HAL_GetWriteTranspose(const CRC_Type *const base)
Gets the CRC transpose type for writes.
Definition: crc_hal.h:530
static bool CRC_HAL_GetFXorMode(const CRC_Type *const base)
Gets complement read of CRC data register.
Definition: crc_hal.h:451
static void CRC_HAL_SetDataLReg(CRC_Type *const base, uint16_t value)
Sets the lower 16 bits of CRC data register.
Definition: crc_hal.h:238
static uint32_t CRC_HAL_GetPolyReg(const CRC_Type *const base)
Gets the polynomial register value.
Definition: crc_hal.h:313
static void CRC_HAL_SetDataLLReg(CRC_Type *const base, uint8_t value)
Sets the Low Lower Byte - LL.
Definition: crc_hal.h:298
static void CRC_HAL_SetReadTranspose(CRC_Type *const base, crc_transpose_t transp)
Sets the CRC transpose type for reads.
Definition: crc_hal.h:615
static uint16_t CRC_HAL_GetDataHReg(const CRC_Type *const base)
Gets the upper 16 bits of the current CRC result.
Definition: crc_hal.h:195
#define CRC_GPOLY_LOW(x)
Definition: S32K144.h:2097
crc_transpose_t
CRC type of transpose of read write data Implements : crc_transpose_t_Class.
Definition: crc_hal.h:42
static void CRC_HAL_SetSeedOrDataMode(CRC_Type *const base, bool enable)
Sets the CRC_DATA register mode.
Definition: crc_hal.h:428
#define CRC_CTRL_TCRC_MASK
Definition: S32K144.h:2103
#define CRC_CTRL_WAS(x)
Definition: S32K144.h:2110
static crc_bit_width_t CRC_HAL_GetProtocolWidth(const CRC_Type *const base)
Gets the CRC protocol width.
Definition: crc_hal.h:488
static uint16_t CRC_HAL_GetPolyHReg(const CRC_Type *const base)
Gets the upper 16 bits of polynomial register.
Definition: crc_hal.h:343
static void CRC_HAL_SetPolyHReg(CRC_Type *const base, uint16_t value)
Sets the upper 16 bits of polynomial register.
Definition: crc_hal.h:358
uint32_t CRC_HAL_GetCrc8(CRC_Type *const base, uint8_t data, bool newSeed, uint32_t seed)
Appends 8-bit data to the current CRC calculation and returns new result.
Definition: crc_hal.c:132
#define CRC_GPOLY_LOW_SHIFT
Definition: S32K144.h:2095
#define CRC_GPOLY_LOW_MASK
Definition: S32K144.h:2094
static uint32_t CRC_HAL_GetDataReg(const CRC_Type *const base)
Gets the current CRC result.
Definition: crc_hal.h:166
#define CRC_GPOLY_HIGH_SHIFT
Definition: S32K144.h:2099
uint32_t CRC_HAL_GetCrc16(CRC_Type *const base, uint16_t data, bool newSeed, uint32_t seed)
Appends 16-bit data to the current CRC calculation and returns new result.
Definition: crc_hal.c:106
#define CRC_CTRL_TOTR(x)
Definition: S32K144.h:2118
#define CRC_CTRL_WAS_SHIFT
Definition: S32K144.h:2108
#define CRC_CTRL_TOTR_SHIFT
Definition: S32K144.h:2116
static uint16_t CRC_HAL_GetPolyLReg(const CRC_Type *const base)
Gets the lower 16 bits of polynomial register.
Definition: crc_hal.h:377
#define CRC_CTRL_FXOR_SHIFT
Definition: S32K144.h:2112
__IO uint32_t CTRL
Definition: S32K144.h:2020
static crc_transpose_t CRC_HAL_GetReadTranspose(const CRC_Type *const base)
Gets the CRC transpose type for reads.
Definition: crc_hal.h:582
uint32_t CRC_HAL_GetCrcResult(const CRC_Type *const base)
Returns the current result of the CRC calculation.
Definition: crc_hal.c:156
static void CRC_HAL_SetDataLUReg(CRC_Type *const base, uint8_t value)
Sets the Low Upper Byte - LU.
Definition: crc_hal.h:283
void CRC_HAL_Init(CRC_Type *const base)
Initializes the CRC module.
Definition: crc_hal.c:52
static bool CRC_HAL_GetSeedOrDataMode(const CRC_Type *const base)
Gets the CRC_DATA register mode.
Definition: crc_hal.h:412
static void CRC_HAL_SetPolyReg(CRC_Type *const base, uint32_t value)
Sets the polynomial register.
Definition: crc_hal.h:327
#define CRC_CTRL_TCRC(x)
Definition: S32K144.h:2106
#define CRC_CTRL_TOT_MASK
Definition: S32K144.h:2119
struct CRC_Type::@1::@3 DATA_8
#define CRC_CTRL_FXOR(x)
Definition: S32K144.h:2114
#define CRC_CTRL_WAS_MASK
Definition: S32K144.h:2107
__IO uint32_t DATA
Definition: S32K144.h:2007
static void CRC_HAL_SetDataReg(CRC_Type *const base, uint32_t value)
Sets the 32 bits of CRC data register.
Definition: crc_hal.h:180
#define CRC_GPOLY_HIGH(x)
Definition: S32K144.h:2101
static void CRC_HAL_SetProtocolWidth(CRC_Type *const base, crc_bit_width_t width)
Sets the CRC protocol width.
Definition: crc_hal.h:511
static void CRC_HAL_SetDataHReg(CRC_Type *const base, uint16_t value)
Sets the upper 16 bits of CRC data register.
Definition: crc_hal.h:209
static void CRC_HAL_SetDataHUReg(CRC_Type *const base, uint8_t value)
Sets the High Upper Byte - HU.
Definition: crc_hal.h:253
__IO uint32_t GPOLY
Definition: S32K144.h:2019
static uint16_t CRC_HAL_GetDataLReg(const CRC_Type *const base)
Gets the lower 16 bits of the current CRC result.
Definition: crc_hal.h:224
#define CRC_CTRL_TOTR_MASK
Definition: S32K144.h:2115
crc_bit_width_t
CRC bit width Implements : crc_bit_width_t_Class.
Definition: crc_hal.h:54
static void CRC_HAL_SetPolyLReg(CRC_Type *const base, uint16_t value)
Sets the lower 16 bits of polynomial register.
Definition: crc_hal.h:391
static void CRC_HAL_SetDataHLReg(CRC_Type *const base, uint8_t value)
Sets the High Lower Byte - HL.
Definition: crc_hal.h:268
static void CRC_HAL_SetFXorMode(CRC_Type *const base, bool enable)
Sets complement read of CRC data register.
Definition: crc_hal.h:467
#define CRC_GPOLY_HIGH_MASK
Definition: S32K144.h:2098
struct CRC_Type::@1::@2 DATA_16
#define CRC_CTRL_FXOR_MASK
Definition: S32K144.h:2111
uint32_t CRC_HAL_GetCrc32(CRC_Type *const base, uint32_t data, bool newSeed, uint32_t seed)
Appends 32-bit data to the current CRC calculation and returns new result.
Definition: crc_hal.c:80