S32 SDK
crc_driver.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016, Freescale Semiconductor, Inc.
3  * Copyright 2016-2017 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  */
44 #include "device_registers.h"
45 #if defined(FEATURE_CRC_DRIVER_S32K1xx)
46 #include "crc_hw_access.h"
47 #elif defined(FEATURE_CRC_DRIVER_MPC57xx)
48 #include "crc_c55_hw_access.h"
49 #endif
50 
51 /*******************************************************************************
52  * Variables
53  ******************************************************************************/
55 static CRC_Type * const s_crcBase[] = CRC_BASE_PTRS;
56 
57 /*******************************************************************************
58  * Code
59  ******************************************************************************/
60 /*FUNCTION**********************************************************************
61  *
62  * Function Name : CRC_DRV_Init
63  * Description : This function initializes CRC driver based on user configuration input.
64  * The user must make sure that the clock is enabled.
65  *
66  * Implements : CRC_DRV_Init_Activity
67  *END**************************************************************************/
68 status_t CRC_DRV_Init(uint32_t instance,
69  const crc_user_config_t * userConfigPtr)
70 {
71  DEV_ASSERT(instance < CRC_INSTANCE_COUNT);
72  DEV_ASSERT(userConfigPtr != NULL);
73  CRC_Type * base = s_crcBase[instance];
74  status_t retStatus = STATUS_SUCCESS;
75 
76  /* Set the default configuration */
77  CRC_Init(base);
78  /* Set the CRC configuration */
79  retStatus = CRC_DRV_Configure(instance, userConfigPtr);
80 
81  return retStatus;
82 }
83 
84 /*FUNCTION**********************************************************************
85  *
86  * Function Name : CRC_DRV_Deinit
87  * Description : This function sets the default configuration.
88  *
89  * Implements : CRC_DRV_Deinit_Activity
90  *END**************************************************************************/
91 status_t CRC_DRV_Deinit(uint32_t instance)
92 {
93  DEV_ASSERT(instance < CRC_INSTANCE_COUNT);
94  CRC_Type * base = s_crcBase[instance];
95 
96  /* Set the default configuration */
97  CRC_Init(base);
98 
99  return STATUS_SUCCESS;
100 }
101 
102 /*FUNCTION**********************************************************************
103  *
104  * Function Name : CRC_DRV_GetCrc32
105  * Description : This function appends 32-bit data to the current CRC calculation
106  * and returns new result. If the newSeed is true, seed set and result are calculated
107  * from the seed new value (new CRC calculation).
108  *
109  * Implements : CRC_DRV_GetCrc32_Activity
110  *END**************************************************************************/
111 uint32_t CRC_DRV_GetCrc32(uint32_t instance,
112  uint32_t data,
113  bool newSeed,
114  uint32_t seed)
115 {
116  DEV_ASSERT(instance < CRC_INSTANCE_COUNT);
117  CRC_Type * base = s_crcBase[instance];
118 
119  /* If newSeed is true then write a seed to initial checksum */
120  if (newSeed)
121  {
122  /* Write a seed - initial checksum */
123  CRC_SetSeedReg(base, seed);
124  }
125 
126  /* Write 32-bit data */
127  CRC_SetDataReg(base, data);
128 
129  /* Result of the CRC calculation */
130  return CRC_GetCrcResult(base);
131 }
132 
133 /*FUNCTION**********************************************************************
134  *
135  * Function Name : CRC_DRV_GetCrc16
136  * Description : This function appends 16-bit data to the current CRC calculation
137  * and returns new result. If the newSeed is true, seed set and result are calculated
138  * from the seed new value (new CRC calculation).
139  *
140  * Implements : CRC_DRV_GetCrc16_Activity
141  *END**************************************************************************/
142 uint32_t CRC_DRV_GetCrc16(uint32_t instance,
143  uint16_t data,
144  bool newSeed,
145  uint32_t seed)
146 {
147  DEV_ASSERT(instance < CRC_INSTANCE_COUNT);
148  CRC_Type * base = s_crcBase[instance];
149 
150  /* If newSeed is true then write a seed to initial checksum */
151  if (newSeed)
152  {
153  /* Write a seed - initial checksum */
154  CRC_SetSeedReg(base, seed);
155  }
156  /* Write 16-bit data */
157  CRC_SetDataLReg(base, data);
158 
159  /* Result of the CRC calculation */
160  return CRC_GetCrcResult(base);
161 }
162 
163 /*FUNCTION**********************************************************************
164  *
165  * Function Name : CRC_DRV_GetCrc8
166  * Description : This function appends 8-bit data to the current CRC calculation
167  * and returns new result. If the newSeed is true, seed set and result are calculated
168  * from the seed new value (new CRC calculation).
169  *
170  * Implements : CRC_DRV_GetCrc8_Activity
171  *END**************************************************************************/
172 uint32_t CRC_DRV_GetCrc8(uint32_t instance,
173  uint8_t data,
174  bool newSeed,
175  uint32_t seed)
176 {
177  DEV_ASSERT(instance < CRC_INSTANCE_COUNT);
178  CRC_Type * base = s_crcBase[instance];
179 
180  /* If newSeed is true then write a seed to initial checksum */
181  if (newSeed)
182  {
183  /* Write a seed - initial checksum */
184  CRC_SetSeedReg(base, seed);
185  }
186  /* Write 8-bit data */
187  CRC_SetDataLLReg(base, data);
188 
189  /* Result of the CRC calculation */
190  return CRC_GetCrcResult(base);
191 }
192 
193 /*FUNCTION**********************************************************************
194  *
195  * Function Name : CRC_DRV_WriteData
196  * Description : This function appends a block of bytes to the current CRC calculation.
197  *
198  * Implements : CRC_DRV_WriteData_Activity
199  *END**************************************************************************/
200 void CRC_DRV_WriteData(uint32_t instance,
201  const uint8_t * data,
202  uint32_t dataSize)
203 {
204  DEV_ASSERT(instance < CRC_INSTANCE_COUNT);
205  DEV_ASSERT(data != NULL);
206  uint32_t i;
207  CRC_Type * base = s_crcBase[instance];
208 
209  /* 8-bit writes till end of data buffer */
210  for (i = 0U; i < dataSize; i++)
211  {
212  CRC_SetDataLLReg(base, data[i]);
213  }
214 }
215 
216 /*FUNCTION**********************************************************************
217  *
218  * Function Name : CRC_DRV_GetCrcResult
219  * Description : This function returns the current result of the CRC calculation.
220  *
221  * Implements : CRC_DRV_GetCrcResult_Activity
222  *END**************************************************************************/
223 uint32_t CRC_DRV_GetCrcResult(uint32_t instance)
224 {
225  DEV_ASSERT(instance < CRC_INSTANCE_COUNT);
226  const CRC_Type * base = s_crcBase[instance];
227 
228  /* Result of the CRC calculation */
229  return CRC_GetCrcResult(base);
230 }
231 
232 /*FUNCTION**********************************************************************
233  *
234  * Function Name : CRC_DRV_Configure
235  * Description : This function configures the CRC module from a user configuration structure.
236  *
237  * Implements : CRC_DRV_Configure_Activity
238  *END**************************************************************************/
239 status_t CRC_DRV_Configure(uint32_t instance,
240  const crc_user_config_t * userConfigPtr)
241 {
242  DEV_ASSERT(instance < CRC_INSTANCE_COUNT);
243  DEV_ASSERT(userConfigPtr != NULL);
244  CRC_Type * base = s_crcBase[instance];
245 
246 #if defined(FEATURE_CRC_DRIVER_S32K1xx)
247  /* Set CRC mode */
248  CRC_SetProtocolWidth(base, userConfigPtr->crcWidth);
249 #endif /* FEATURE_CRC_DRIVER_S32K1xx */
250  /* Set transposes options */
251  CRC_SetReadTranspose(base, userConfigPtr->readTranspose);
252  /* Set CRC polynomial */
253  CRC_SetPolyReg(base, userConfigPtr->polynomial);
254  /* Set writes transposes */
255  CRC_SetWriteTranspose(base, userConfigPtr->writeTranspose);
256  /* Sets complement or inversion checksum */
257  CRC_SetFXorMode(base, userConfigPtr->complementChecksum);
258  /* Write a seed - initial checksum */
259  CRC_SetSeedReg(base, userConfigPtr->seed);
260 
261  return STATUS_SUCCESS;
262 }
263 
264 /*FUNCTION**********************************************************************
265  *
266  * Function Name : CRC_DRV_GetConfig
267  * Description : This function Get configures of the CRC module currently
268  *
269  * Implements : CRC_DRV_GetConfig_Activity
270  *END**************************************************************************/
271 status_t CRC_DRV_GetConfig(uint32_t instance,
272  crc_user_config_t * const userConfigPtr)
273 {
274  DEV_ASSERT(instance < CRC_INSTANCE_COUNT);
275  DEV_ASSERT(userConfigPtr != NULL);
276  const CRC_Type * const base = s_crcBase[instance];
277 
278 #if defined(FEATURE_CRC_DRIVER_S32K1xx)
279  /* Gets CRC mode */
280  userConfigPtr->crcWidth = CRC_GetProtocolWidth(base);
281 #endif /* FEATURE_CRC_DRIVER_S32K1xx */
282  /* Gets transposes and complement options */
283  userConfigPtr->readTranspose = CRC_GetReadTranspose(base);
284  /* Get a polynomial */
285  userConfigPtr->polynomial = CRC_GetPolyReg(base);
286  /* Gets transposes options */
287  userConfigPtr->writeTranspose = CRC_GetWriteTranspose(base);
288  /* Gets complement or inversion checksum */
289  userConfigPtr->complementChecksum = CRC_GetFXorMode(base);
290  /* Get a seed - initial checksum */
291  userConfigPtr->seed = CRC_GetDataReg(base);
292 
293  return STATUS_SUCCESS;
294 }
295 
296 /*FUNCTION**********************************************************************
297  *
298  * Function Name : CRC_DRV_GetDefaultConfig
299  * Description : This function Get default configures the CRC module for user configuration structure
300  *
301  * Implements : CRC_DRV_GetDefaultConfig_Activity
302  *END**************************************************************************/
304 {
305  DEV_ASSERT(userConfigPtr != NULL);
306 
307 #if defined(FEATURE_CRC_DRIVER_S32K1xx)
308  /* Gets CRC mode default is 16 bit */
309  userConfigPtr->crcWidth = CRC_DEFAULT_WIDTH;
310 #endif /* FEATURE_CRC_DRIVER_S32K1xx */
311  /* Gets default a polynomial default is reset value */
312  userConfigPtr->polynomial = CRC_DEFAULT_POLYNOMIAL;
313  /* Gets default read transposes none */
314  userConfigPtr->readTranspose = CRC_DEFAULT_READ_TRANSPOSE;
315  /* Gets default write transpose none */
317  /* Gets default no complement or inversion checksum */
318  userConfigPtr->complementChecksum = false;
319  /* Gets default a seed - initial checksum */
320  userConfigPtr->seed = CRC_DEFAULT_SEED;
321 
322  return STATUS_SUCCESS;
323 }
324 
325 /*******************************************************************************
326  * EOF
327  ******************************************************************************/
uint32_t CRC_DRV_GetCrc32(uint32_t instance, uint32_t data, bool newSeed, uint32_t seed)
Appends 32-bit data to the current CRC calculation and returns new result.
Definition: crc_driver.c:111
#define CRC_DEFAULT_READ_TRANSPOSE
#define CRC_BASE_PTRS
Definition: S32K142.h:1949
status_t CRC_DRV_Init(uint32_t instance, const crc_user_config_t *userConfigPtr)
Initializes the CRC module.
Definition: crc_driver.c:68
#define CRC_DEFAULT_WRITE_TRANSPOSE
Definition: crc_driver.h:42
status_t CRC_DRV_Configure(uint32_t instance, const crc_user_config_t *userConfigPtr)
Configures the CRC module from a user configuration structure.
Definition: crc_driver.c:239
CRC configuration structure. Implements : crc_user_config_t_Class.
Definition: crc_driver.h:87
status_t CRC_DRV_GetConfig(uint32_t instance, crc_user_config_t *const userConfigPtr)
Get configures of the CRC module currently.
Definition: crc_driver.c:271
#define DEV_ASSERT(x)
Definition: devassert.h:77
#define CRC_DEFAULT_POLYNOMIAL
uint32_t CRC_DRV_GetCrc8(uint32_t instance, uint8_t data, bool newSeed, uint32_t seed)
Appends 8-bit data to the current CRC calculation and returns new result.
Definition: crc_driver.c:172
uint32_t CRC_DRV_GetCrc16(uint32_t instance, uint16_t data, bool newSeed, uint32_t seed)
Appends 16-bit data to the current CRC calculation and returns new result.
Definition: crc_driver.c:142
status_t
Status return codes. Common error codes will be a unified enumeration (C enum) that will contain all ...
Definition: status.h:44
#define CRC_DEFAULT_SEED
Definition: crc_driver.h:44
bool complementChecksum
Definition: crc_driver.h:99
status_t CRC_DRV_Deinit(uint32_t instance)
Sets the default configuration.
Definition: crc_driver.c:91
status_t CRC_DRV_GetDefaultConfig(crc_user_config_t *const userConfigPtr)
Get default configures the CRC module for configuration structure.
Definition: crc_driver.c:303
crc_transpose_t writeTranspose
Definition: crc_driver.h:98
uint32_t CRC_DRV_GetCrcResult(uint32_t instance)
Returns the current result of the CRC calculation.
Definition: crc_driver.c:223
static CRC_Type *const s_crcBase[]
Table of base addresses for CRC instances.
Definition: crc_driver.c:55
#define CRC_INSTANCE_COUNT
Definition: S32K142.h:1938
void CRC_DRV_WriteData(uint32_t instance, const uint8_t *data, uint32_t dataSize)
Appends a block of bytes to the current CRC calculation.
Definition: crc_driver.c:200
#define CRC_DEFAULT_WIDTH