S32 SDK
crc_hal.c
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  */
26 #include "crc_hal.h"
27 
28 /*******************************************************************************
29  * Definitions
30  ******************************************************************************/
31 /* Default polynomial 0x1021U */
32 #define CRC_DEFAULT_POLYNOMIAL (0x1021U)
33 /* Initial checksum */
34 #define CRC_INITIAL_SEED (0U)
35 
36 /*******************************************************************************
37  * Code
38  ******************************************************************************/
39 /*FUNCTION**********************************************************************
40  *
41  * Function Name : CRC_HAL_Init
42  * Description : This function initializes the module to default configuration
43  * (Initial checksum: 0U,
44  * Default polynomial: 0x1021U,
45  * Type of read transpose: CRC_TRANSPOSE_NONE,
46  * Type of write transpose: CRC_TRANSPOSE_NONE,
47  * No complement of checksum read,
48  * 32-bit CRC).
49  *
50  * Implements : CRC_HAL_Init_Activity
51  *END**************************************************************************/
52 void CRC_HAL_Init(CRC_Type * const base)
53 {
54  /* Set CRC mode to 32-bit */
56 
57  /* Set read/write transpose and complement checksum to none */
60  CRC_HAL_SetFXorMode(base, false);
61 
62  /* Write polynomial to 0x1021U */
64 
65  /* Write seed to zero */
66  CRC_HAL_SetSeedOrDataMode(base, true);
68  CRC_HAL_SetSeedOrDataMode(base, false);
69 }
70 
71 /*FUNCTION**********************************************************************
72  *
73  * Function Name : CRC_HAL_GetCrc32
74  * Description : This function appends 32-bit data to the current CRC calculation
75  * and returns new result. If the newSeed is true, seed set and result are calculated
76  * from the seed new value (new CRC calculation).
77  *
78  * Implements : CRC_HAL_GetCrc32_Activity
79  *END**************************************************************************/
80 uint32_t CRC_HAL_GetCrc32(CRC_Type * const base,
81  uint32_t data,
82  bool newSeed,
83  uint32_t seed)
84 {
85  if (newSeed)
86  {
87  CRC_HAL_SetSeedOrDataMode(base, true);
88  CRC_HAL_SetDataReg(base, seed);
89  CRC_HAL_SetSeedOrDataMode(base, false);
90  }
91 
92  CRC_HAL_SetDataReg(base, data);
93 
94  return CRC_HAL_GetCrcResult(base);
95 }
96 
97 /*FUNCTION**********************************************************************
98  *
99  * Function Name : CRC_HAL_GetCrc16
100  * Description : This function appends 16-bit data to the current CRC calculation
101  * and returns new result. If the newSeed is true, seed set and result are calculated
102  * from the seed new value (new CRC calculation).
103  *
104  * Implements : CRC_HAL_GetCrc16_Activity
105  *END**************************************************************************/
106 uint32_t CRC_HAL_GetCrc16(CRC_Type * const base,
107  uint16_t data,
108  bool newSeed,
109  uint32_t seed)
110 {
111  if (newSeed)
112  {
113  CRC_HAL_SetSeedOrDataMode(base, true);
114  CRC_HAL_SetDataReg(base, seed);
115  CRC_HAL_SetSeedOrDataMode(base, false);
116  }
117 
118  CRC_HAL_SetDataLReg(base, data);
119 
120  return CRC_HAL_GetCrcResult(base);
121 }
122 
123 /*FUNCTION**********************************************************************
124  *
125  * Function Name : CRC_HAL_GetCrc8
126  * Description : This function appends 8-bit data to the current CRC calculation
127  * and returns new result. If the newSeed is true, seed set and result are calculated
128  * from the seed new value (new CRC calculation).
129  *
130  * Implements : CRC_HAL_GetCrc8_Activity
131  *END**************************************************************************/
132 uint32_t CRC_HAL_GetCrc8(CRC_Type * const base,
133  uint8_t data,
134  bool newSeed,
135  uint32_t seed)
136 {
137  if (newSeed)
138  {
139  CRC_HAL_SetSeedOrDataMode(base, true);
140  CRC_HAL_SetDataReg(base, seed);
141  CRC_HAL_SetSeedOrDataMode(base, false);
142  }
143 
144  CRC_HAL_SetDataLLReg(base, data);
145 
146  return CRC_HAL_GetCrcResult(base);
147 }
148 
149 /*FUNCTION**********************************************************************
150  *
151  * Function Name : CRC_HAL_GetCrcResult
152  * Description : This function returns the current result of the CRC calculation.
153  *
154  * Implements : CRC_HAL_GetCrcResult_Activity
155  *END**************************************************************************/
156 uint32_t CRC_HAL_GetCrcResult(const CRC_Type * const base)
157 {
159  crc_transpose_t transpose;
160  uint32_t result;
161 
162  if (width == CRC_BITS_16)
163  {
164  transpose = CRC_HAL_GetReadTranspose(base);
165  if ((transpose == CRC_TRANSPOSE_BITS_AND_BYTES) || (transpose == CRC_TRANSPOSE_BYTES))
166  {
167  /* Returns upper 16 bits of CRC because of transposition in 16-bit mode */
168  result = (uint32_t)CRC_HAL_GetDataHReg(base);
169  }
170  else
171  {
172  result = (uint32_t)CRC_HAL_GetDataLReg(base);
173  }
174  }
175  else
176  {
177  result = CRC_HAL_GetDataReg(base);
178  }
179 
180  return result;
181 }
182 
183 /*******************************************************************************
184  * EOF
185  ******************************************************************************/
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
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 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
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
static crc_bit_width_t CRC_HAL_GetProtocolWidth(const CRC_Type *const base)
Gets the CRC protocol width.
Definition: crc_hal.h:488
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
static uint32_t CRC_HAL_GetDataReg(const CRC_Type *const base)
Gets the current CRC result.
Definition: crc_hal.h:166
#define CRC_INITIAL_SEED
Definition: crc_hal.c:34
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
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
void CRC_HAL_Init(CRC_Type *const base)
Initializes the CRC module.
Definition: crc_hal.c:52
static void CRC_HAL_SetPolyReg(CRC_Type *const base, uint32_t value)
Sets the polynomial register.
Definition: crc_hal.h:327
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
static void CRC_HAL_SetProtocolWidth(CRC_Type *const base, crc_bit_width_t width)
Sets the CRC protocol width.
Definition: crc_hal.h:511
#define CRC_DEFAULT_POLYNOMIAL
Definition: crc_hal.c:32
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
crc_bit_width_t
CRC bit width Implements : crc_bit_width_t_Class.
Definition: crc_hal.h:54
static void CRC_HAL_SetFXorMode(CRC_Type *const base, bool enable)
Sets complement read of CRC data register.
Definition: crc_hal.h:467
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