STM32F769IDiscovery  1.00
uDANTE Audio Networking with STM32F7 DISCO board
stm32f7xx_hal_crc.c
Go to the documentation of this file.
1 
67 /* Includes ------------------------------------------------------------------*/
68 #include "stm32f7xx_hal.h"
69 
79 #ifdef HAL_CRC_MODULE_ENABLED
80 
81 /* Private typedef -----------------------------------------------------------*/
82 /* Private define ------------------------------------------------------------*/
83 /* Private macro -------------------------------------------------------------*/
84 /* Private variables ---------------------------------------------------------*/
85 /* Private function prototypes -----------------------------------------------*/
86 static uint32_t CRC_Handle_8(CRC_HandleTypeDef *hcrc, uint8_t pBuffer[], uint32_t BufferLength);
87 static uint32_t CRC_Handle_16(CRC_HandleTypeDef *hcrc, uint16_t pBuffer[], uint32_t BufferLength);
88 /* Exported functions --------------------------------------------------------*/
89 
119 {
120  /* Check the CRC handle allocation */
121  if(hcrc == NULL)
122  {
123  return HAL_ERROR;
124  }
125 
126  /* Check the parameters */
128 
129  if(hcrc->State == HAL_CRC_STATE_RESET)
130  {
131  /* Allocate lock resource and initialize it */
132  hcrc->Lock = HAL_UNLOCKED;
133  /* Init the low level hardware */
134  HAL_CRC_MspInit(hcrc);
135  }
136 
137  /* Change CRC peripheral state */
138  hcrc->State = HAL_CRC_STATE_BUSY;
139 
140  /* check whether or not non-default generating polynomial has been
141  * picked up by user */
144  {
145  /* initialize IP with default generating polynomial */
148  }
149  else
150  {
151  /* initialize CRC IP with generating polynomial defined by user */
153  {
154  return HAL_ERROR;
155  }
156  }
157 
158  /* check whether or not non-default CRC initial value has been
159  * picked up by user */
162  {
164  }
165  else
166  {
167  WRITE_REG(hcrc->Instance->INIT, hcrc->Init.InitValue);
168  }
169 
170 
171  /* set input data inversion mode */
174 
175  /* set output data inversion mode */
178 
179  /* makes sure the input data format (bytes, halfwords or words stream)
180  * is properly specified by user */
182 
183  /* Change CRC peripheral state */
184  hcrc->State = HAL_CRC_STATE_READY;
185 
186  /* Return function status */
187  return HAL_OK;
188 }
189 
196 {
197  /* Check the CRC handle allocation */
198  if(hcrc == NULL)
199  {
200  return HAL_ERROR;
201  }
202 
203  /* Check the parameters */
205 
206  /* Check the CRC peripheral state */
207  if(hcrc->State == HAL_CRC_STATE_BUSY)
208  {
209  return HAL_BUSY;
210  }
211 
212  /* Change CRC peripheral state */
213  hcrc->State = HAL_CRC_STATE_BUSY;
214 
215  /* Reset CRC calculation unit */
216  __HAL_CRC_DR_RESET(hcrc);
217 
218  /* DeInit the low level hardware */
219  HAL_CRC_MspDeInit(hcrc);
220 
221  /* Change CRC peripheral state */
222  hcrc->State = HAL_CRC_STATE_RESET;
223 
224  /* Process unlocked */
225  __HAL_UNLOCK(hcrc);
226 
227  /* Return function status */
228  return HAL_OK;
229 }
230 
236 __weak void HAL_CRC_MspInit(CRC_HandleTypeDef *hcrc)
237 {
238  /* Prevent unused argument(s) compilation warning */
239  UNUSED(hcrc);
240 
241  /* NOTE : This function should not be modified, when the callback is needed,
242  the HAL_CRC_MspInit can be implemented in the user file
243  */
244 }
245 
251 __weak void HAL_CRC_MspDeInit(CRC_HandleTypeDef *hcrc)
252 {
253  /* Prevent unused argument(s) compilation warning */
254  UNUSED(hcrc);
255 
256  /* NOTE : This function should not be modified, when the callback is needed,
257  the HAL_CRC_MspDeInit can be implemented in the user file
258  */
259 }
260 
300 uint32_t HAL_CRC_Accumulate(CRC_HandleTypeDef *hcrc, uint32_t pBuffer[], uint32_t BufferLength)
301 {
302  uint32_t index = 0; /* CRC input data buffer index */
303  uint32_t temp = 0; /* CRC output (read from hcrc->Instance->DR register) */
304 
305  /* Process locked */
306  __HAL_LOCK(hcrc);
307 
308  /* Change CRC peripheral state */
309  hcrc->State = HAL_CRC_STATE_BUSY;
310 
311  switch (hcrc->InputDataFormat)
312  {
314  /* Enter Data to the CRC calculator */
315  for(index = 0; index < BufferLength; index++)
316  {
317  hcrc->Instance->DR = pBuffer[index];
318  }
319  temp = hcrc->Instance->DR;
320  break;
321 
323  temp = CRC_Handle_8(hcrc, (uint8_t*)pBuffer, BufferLength);
324  break;
325 
327  temp = CRC_Handle_16(hcrc, (uint16_t*)pBuffer, BufferLength);
328  break;
329  default:
330  break;
331  }
332 
333  /* Change CRC peripheral state */
334  hcrc->State = HAL_CRC_STATE_READY;
335 
336  /* Process unlocked */
337  __HAL_UNLOCK(hcrc);
338 
339  /* Return the CRC computed value */
340  return temp;
341 }
342 
358 uint32_t HAL_CRC_Calculate(CRC_HandleTypeDef *hcrc, uint32_t pBuffer[], uint32_t BufferLength)
359 {
360  uint32_t index = 0; /* CRC input data buffer index */
361  uint32_t temp = 0; /* CRC output (read from hcrc->Instance->DR register) */
362 
363  /* Process locked */
364  __HAL_LOCK(hcrc);
365 
366  /* Change CRC peripheral state */
367  hcrc->State = HAL_CRC_STATE_BUSY;
368 
369  /* Reset CRC Calculation Unit (hcrc->Instance->INIT is
370  * written in hcrc->Instance->DR) */
371  __HAL_CRC_DR_RESET(hcrc);
372 
373  switch (hcrc->InputDataFormat)
374  {
376  /* Enter 32-bit input data to the CRC calculator */
377  for(index = 0; index < BufferLength; index++)
378  {
379  hcrc->Instance->DR = pBuffer[index];
380  }
381  temp = hcrc->Instance->DR;
382  break;
383 
385  /* Specific 8-bit input data handling */
386  temp = CRC_Handle_8(hcrc, (uint8_t*)pBuffer, BufferLength);
387  break;
388 
390  /* Specific 16-bit input data handling */
391  temp = CRC_Handle_16(hcrc, (uint16_t*)pBuffer, BufferLength);
392  break;
393  default:
394  break;
395  }
396 
397  /* Change CRC peripheral state */
398  hcrc->State = HAL_CRC_STATE_READY;
399 
400  /* Process unlocked */
401  __HAL_UNLOCK(hcrc);
402 
403  /* Return the CRC computed value */
404  return temp;
405 }
406 
415 static uint32_t CRC_Handle_8(CRC_HandleTypeDef *hcrc, uint8_t pBuffer[], uint32_t BufferLength)
416 {
417  uint32_t i = 0; /* input data buffer index */
418 
419  /* Processing time optimization: 4 bytes are entered in a row with a single word write,
420  * last bytes must be carefully fed to the CRC calculator to ensure a correct type
421  * handling by the IP */
422  for(i = 0; i < (BufferLength/4); i++)
423  {
424  hcrc->Instance->DR = (uint32_t)(((uint32_t)(pBuffer[4*i])<<24) | ((uint32_t)(pBuffer[4*i+1])<<16) | ((uint32_t)(pBuffer[4*i+2])<<8) | (uint32_t)(pBuffer[4*i+3]));
425  }
426  /* last bytes specific handling */
427  if((BufferLength%4) != 0)
428  {
429  if(BufferLength%4 == 1)
430  {
431  *(__IO uint8_t*) (&hcrc->Instance->DR) = pBuffer[4*i];
432  }
433  if(BufferLength%4 == 2)
434  {
435  *(__IO uint16_t*)(&hcrc->Instance->DR) = (uint16_t)((uint16_t)((uint16_t)(pBuffer[4*i])<<8) | (uint16_t)(pBuffer[4*i+1]));
436  }
437  if(BufferLength%4 == 3)
438  {
439  *(__IO uint16_t*)(&hcrc->Instance->DR) = (uint16_t)((uint16_t)((uint16_t)(pBuffer[4*i])<<8) | (uint16_t)(pBuffer[4*i+1]));
440  *(__IO uint8_t*) (&hcrc->Instance->DR) = pBuffer[4*i+2];
441  }
442  }
443 
444  /* Return the CRC computed value */
445  return hcrc->Instance->DR;
446 }
447 
456 static uint32_t CRC_Handle_16(CRC_HandleTypeDef *hcrc, uint16_t pBuffer[], uint32_t BufferLength)
457 {
458  uint32_t i = 0; /* input data buffer index */
459 
460  /* Processing time optimization: 2 HalfWords are entered in a row with a single word write,
461  * in case of odd length, last HalfWord must be carefully fed to the CRC calculator to ensure
462  * a correct type handling by the IP */
463  for(i = 0; i < (BufferLength/2); i++)
464  {
465  hcrc->Instance->DR = (((uint32_t)(pBuffer[2*i])<<16) | (uint32_t)(pBuffer[2*i+1]));
466  }
467  if((BufferLength%2) != 0)
468  {
469  *(__IO uint16_t*) (&hcrc->Instance->DR) = (uint16_t)pBuffer[2*i];
470  }
471 
472  /* Return the CRC computed value */
473  return hcrc->Instance->DR;
474 }
475 
501 {
502  return hcrc->State;
503 }
504 
513 #endif /* HAL_CRC_MODULE_ENABLED */
514 
522 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
__IO uint32_t INIT
Definition: stm32f745xx.h:328
#define MODIFY_REG(REG, CLEARMASK, SETMASK)
Definition: stm32f7xx.h:190
#define assert_param(expr)
Include module&#39;s header file.
#define CRC_INPUTDATA_FORMAT_HALFWORDS
CRC_InitTypeDef Init
#define DEFAULT_CRC32_POLY
#define __HAL_UNLOCK(__HANDLE__)
#define CRC_CR_POLYSIZE
Definition: stm32f745xx.h:3070
#define IS_CRC_INPUTDATA_FORMAT(__FORMAT__)
void HAL_CRC_MspInit(CRC_HandleTypeDef *hcrc)
#define IS_DEFAULT_INIT_VALUE(__VALUE__)
#define WRITE_REG(REG, VAL)
Definition: stm32f7xx.h:186
__IO uint32_t POL
Definition: stm32f745xx.h:329
#define DEFAULT_INIT_VALUE_ENABLE
HAL_LockTypeDef Lock
__IO uint32_t DR
Definition: stm32f745xx.h:322
#define IS_DEFAULT_POLYNOMIAL(__DEFAULT__)
CRC_TypeDef * Instance
#define DEFAULT_CRC_INITVALUE
#define __HAL_LOCK(__HANDLE__)
#define IS_CRC_OUTPUTDATA_INVERSION_MODE(__MODE__)
#define NULL
Definition: usbd_def.h:53
#define CRC_CR_REV_IN
Definition: stm32f745xx.h:3073
HAL_CRC_StateTypeDef
#define __IO
Definition: core_cm0.h:213
void HAL_CRC_MspDeInit(CRC_HandleTypeDef *hcrc)
This file contains all the functions prototypes for the HAL module driver.
uint32_t OutputDataInversionMode
__IO HAL_CRC_StateTypeDef State
uint32_t InputDataInversionMode
uint8_t DefaultPolynomialUse
__IO uint32_t CR
Definition: stm32f745xx.h:326
#define UNUSED(x)
uint32_t HAL_CRC_Accumulate(CRC_HandleTypeDef *hcrc, uint32_t pBuffer[], uint32_t BufferLength)
#define IS_CRC_ALL_INSTANCE(__INSTANCE__)
Definition: stm32f745xx.h:8790
#define __HAL_CRC_DR_RESET(__HANDLE__)
Reset CRC Data Register.
#define CRC_POLYLENGTH_32B
#define CRC_INPUTDATA_FORMAT_BYTES
HAL_CRC_StateTypeDef HAL_CRC_GetState(CRC_HandleTypeDef *hcrc)
#define DEFAULT_POLYNOMIAL_ENABLE
uint32_t GeneratingPolynomial
uint8_t DefaultInitValueUse
HAL_StatusTypeDef HAL_CRCEx_Polynomial_Set(CRC_HandleTypeDef *hcrc, uint32_t Pol, uint32_t PolyLength)
#define CRC_CR_REV_OUT
Definition: stm32f745xx.h:3076
#define IS_CRC_INPUTDATA_INVERSION_MODE(__MODE__)
HAL_StatusTypeDef
HAL Status structures definition.
HAL_StatusTypeDef HAL_CRC_DeInit(CRC_HandleTypeDef *hcrc)
#define CRC_INPUTDATA_FORMAT_WORDS
uint32_t HAL_CRC_Calculate(CRC_HandleTypeDef *hcrc, uint32_t pBuffer[], uint32_t BufferLength)
HAL_StatusTypeDef HAL_CRC_Init(CRC_HandleTypeDef *hcrc)