STM32L4xx_HAL_Driver  1.14.0
stm32l4xx_hal_crc.c
Go to the documentation of this file.
1 
45 /* Includes ------------------------------------------------------------------*/
46 #include "stm32l4xx_hal.h"
47 
57 #ifdef HAL_CRC_MODULE_ENABLED
58 
59 /* Private typedef -----------------------------------------------------------*/
60 /* Private define ------------------------------------------------------------*/
61 /* Private macro -------------------------------------------------------------*/
62 /* Private variables ---------------------------------------------------------*/
63 /* Private function prototypes -----------------------------------------------*/
67 static uint32_t CRC_Handle_8(CRC_HandleTypeDef *hcrc, uint8_t pBuffer[], uint32_t BufferLength);
68 static uint32_t CRC_Handle_16(CRC_HandleTypeDef *hcrc, uint16_t pBuffer[], uint32_t BufferLength);
73 /* Exported functions --------------------------------------------------------*/
74 
103 HAL_StatusTypeDef HAL_CRC_Init(CRC_HandleTypeDef *hcrc)
104 {
105  /* Check the CRC handle allocation */
106  if (hcrc == NULL)
107  {
108  return HAL_ERROR;
109  }
110 
111  /* Check the parameters */
112  assert_param(IS_CRC_ALL_INSTANCE(hcrc->Instance));
113 
114  if (hcrc->State == HAL_CRC_STATE_RESET)
115  {
116  /* Allocate lock resource and initialize it */
117  hcrc->Lock = HAL_UNLOCKED;
118  /* Init the low level hardware */
119  HAL_CRC_MspInit(hcrc);
120  }
121 
122  hcrc->State = HAL_CRC_STATE_BUSY;
123 
124  /* check whether or not non-default generating polynomial has been
125  * picked up by user */
126  assert_param(IS_DEFAULT_POLYNOMIAL(hcrc->Init.DefaultPolynomialUse));
127  if (hcrc->Init.DefaultPolynomialUse == DEFAULT_POLYNOMIAL_ENABLE)
128  {
129  /* initialize peripheral with default generating polynomial */
130  WRITE_REG(hcrc->Instance->POL, DEFAULT_CRC32_POLY);
131  MODIFY_REG(hcrc->Instance->CR, CRC_CR_POLYSIZE, CRC_POLYLENGTH_32B);
132  }
133  else
134  {
135  /* initialize CRC peripheral with generating polynomial defined by user */
137  {
138  return HAL_ERROR;
139  }
140  }
141 
142  /* check whether or not non-default CRC initial value has been
143  * picked up by user */
144  assert_param(IS_DEFAULT_INIT_VALUE(hcrc->Init.DefaultInitValueUse));
145  if (hcrc->Init.DefaultInitValueUse == DEFAULT_INIT_VALUE_ENABLE)
146  {
147  WRITE_REG(hcrc->Instance->INIT, DEFAULT_CRC_INITVALUE);
148  }
149  else
150  {
151  WRITE_REG(hcrc->Instance->INIT, hcrc->Init.InitValue);
152  }
153 
154 
155  /* set input data inversion mode */
156  assert_param(IS_CRC_INPUTDATA_INVERSION_MODE(hcrc->Init.InputDataInversionMode));
157  MODIFY_REG(hcrc->Instance->CR, CRC_CR_REV_IN, hcrc->Init.InputDataInversionMode);
158 
159  /* set output data inversion mode */
160  assert_param(IS_CRC_OUTPUTDATA_INVERSION_MODE(hcrc->Init.OutputDataInversionMode));
161  MODIFY_REG(hcrc->Instance->CR, CRC_CR_REV_OUT, hcrc->Init.OutputDataInversionMode);
162 
163  /* makes sure the input data format (bytes, halfwords or words stream)
164  * is properly specified by user */
165  assert_param(IS_CRC_INPUTDATA_FORMAT(hcrc->InputDataFormat));
166 
167  /* Change CRC peripheral state */
168  hcrc->State = HAL_CRC_STATE_READY;
169 
170  /* Return function status */
171  return HAL_OK;
172 }
173 
179 HAL_StatusTypeDef HAL_CRC_DeInit(CRC_HandleTypeDef *hcrc)
180 {
181  /* Check the CRC handle allocation */
182  if (hcrc == NULL)
183  {
184  return HAL_ERROR;
185  }
186 
187  /* Check the parameters */
188  assert_param(IS_CRC_ALL_INSTANCE(hcrc->Instance));
189 
190  /* Check the CRC peripheral state */
191  if (hcrc->State == HAL_CRC_STATE_BUSY)
192  {
193  return HAL_BUSY;
194  }
195 
196  /* Change CRC peripheral state */
197  hcrc->State = HAL_CRC_STATE_BUSY;
198 
199  /* Reset CRC calculation unit */
200  __HAL_CRC_DR_RESET(hcrc);
201 
202  /* Reset IDR register content */
203  CLEAR_BIT(hcrc->Instance->IDR, CRC_IDR_IDR);
204 
205  /* DeInit the low level hardware */
206  HAL_CRC_MspDeInit(hcrc);
207 
208  /* Change CRC peripheral state */
209  hcrc->State = HAL_CRC_STATE_RESET;
210 
211  /* Process unlocked */
212  __HAL_UNLOCK(hcrc);
213 
214  /* Return function status */
215  return HAL_OK;
216 }
217 
224 {
225  /* Prevent unused argument(s) compilation warning */
226  UNUSED(hcrc);
227 
228  /* NOTE : This function should not be modified, when the callback is needed,
229  the HAL_CRC_MspInit can be implemented in the user file
230  */
231 }
232 
239 {
240  /* Prevent unused argument(s) compilation warning */
241  UNUSED(hcrc);
242 
243  /* NOTE : This function should not be modified, when the callback is needed,
244  the HAL_CRC_MspDeInit can be implemented in the user file
245  */
246 }
247 
287 uint32_t HAL_CRC_Accumulate(CRC_HandleTypeDef *hcrc, uint32_t pBuffer[], uint32_t BufferLength)
288 {
289  uint32_t index; /* CRC input data buffer index */
290  uint32_t temp = 0U; /* CRC output (read from hcrc->Instance->DR register) */
291 
292  /* Change CRC peripheral state */
293  hcrc->State = HAL_CRC_STATE_BUSY;
294 
295  switch (hcrc->InputDataFormat)
296  {
297  case CRC_INPUTDATA_FORMAT_WORDS:
298  /* Enter Data to the CRC calculator */
299  for (index = 0U; index < BufferLength; index++)
300  {
301  hcrc->Instance->DR = pBuffer[index];
302  }
303  temp = hcrc->Instance->DR;
304  break;
305 
306  case CRC_INPUTDATA_FORMAT_BYTES:
307  temp = CRC_Handle_8(hcrc, (uint8_t *)pBuffer, BufferLength);
308  break;
309 
310  case CRC_INPUTDATA_FORMAT_HALFWORDS:
311  temp = CRC_Handle_16(hcrc, (uint16_t *)(void *)pBuffer, BufferLength); /* Derogation MisraC2012 R.11.5 */
312  break;
313  default:
314  break;
315  }
316 
317  /* Change CRC peripheral state */
318  hcrc->State = HAL_CRC_STATE_READY;
319 
320  /* Return the CRC computed value */
321  return temp;
322 }
323 
339 uint32_t HAL_CRC_Calculate(CRC_HandleTypeDef *hcrc, uint32_t pBuffer[], uint32_t BufferLength)
340 {
341  uint32_t index; /* CRC input data buffer index */
342  uint32_t temp = 0U; /* CRC output (read from hcrc->Instance->DR register) */
343 
344  /* Change CRC peripheral state */
345  hcrc->State = HAL_CRC_STATE_BUSY;
346 
347  /* Reset CRC Calculation Unit (hcrc->Instance->INIT is
348  * written in hcrc->Instance->DR) */
349  __HAL_CRC_DR_RESET(hcrc);
350 
351  switch (hcrc->InputDataFormat)
352  {
353  case CRC_INPUTDATA_FORMAT_WORDS:
354  /* Enter 32-bit input data to the CRC calculator */
355  for (index = 0U; index < BufferLength; index++)
356  {
357  hcrc->Instance->DR = pBuffer[index];
358  }
359  temp = hcrc->Instance->DR;
360  break;
361 
362  case CRC_INPUTDATA_FORMAT_BYTES:
363  /* Specific 8-bit input data handling */
364  temp = CRC_Handle_8(hcrc, (uint8_t *)pBuffer, BufferLength);
365  break;
366 
367  case CRC_INPUTDATA_FORMAT_HALFWORDS:
368  /* Specific 16-bit input data handling */
369  temp = CRC_Handle_16(hcrc, (uint16_t *)(void *)pBuffer, BufferLength); /* Derogation MisraC2012 R.11.5 */
370  break;
371 
372  default:
373  break;
374  }
375 
376  /* Change CRC peripheral state */
377  hcrc->State = HAL_CRC_STATE_READY;
378 
379  /* Return the CRC computed value */
380  return temp;
381 }
382 
407 {
408  /* Return CRC handle state */
409  return hcrc->State;
410 }
411 
432 static uint32_t CRC_Handle_8(CRC_HandleTypeDef *hcrc, uint8_t pBuffer[], uint32_t BufferLength)
433 {
434  uint32_t i; /* input data buffer index */
435  uint16_t data;
436  __IO uint16_t *pReg;
437 
438  /* Processing time optimization: 4 bytes are entered in a row with a single word write,
439  * last bytes must be carefully fed to the CRC calculator to ensure a correct type
440  * handling by the peripheral */
441  for (i = 0U; i < (BufferLength / 4U); i++)
442  {
443  hcrc->Instance->DR = ((uint32_t)pBuffer[4U * i] << 24U) | \
444  ((uint32_t)pBuffer[(4U * i) + 1U] << 16U) | \
445  ((uint32_t)pBuffer[(4U * i) + 2U] << 8U) | \
446  (uint32_t)pBuffer[(4U * i) + 3U];
447  }
448  /* last bytes specific handling */
449  if ((BufferLength % 4U) != 0U)
450  {
451  if ((BufferLength % 4U) == 1U)
452  {
453  *(__IO uint8_t *)(__IO void *)(&hcrc->Instance->DR) = pBuffer[4U * i]; /* Derogation MisraC2012 R.11.5 */
454  }
455  if ((BufferLength % 4U) == 2U)
456  {
457  data = ((uint16_t)(pBuffer[4U * i]) << 8U) | (uint16_t)pBuffer[(4U * i) + 1U];
458  pReg = (__IO uint16_t *)(__IO void *)(&hcrc->Instance->DR); /* Derogation MisraC2012 R.11.5 */
459  *pReg = data;
460  }
461  if ((BufferLength % 4U) == 3U)
462  {
463  data = ((uint16_t)(pBuffer[4U * i]) << 8U) | (uint16_t)pBuffer[(4U * i) + 1U];
464  pReg = (__IO uint16_t *)(__IO void *)(&hcrc->Instance->DR); /* Derogation MisraC2012 R.11.5 */
465  *pReg = data;
466 
467  *(__IO uint8_t *)(__IO void *)(&hcrc->Instance->DR) = pBuffer[(4U * i) + 2U]; /* Derogation MisraC2012 R.11.5 */
468  }
469  }
470 
471  /* Return the CRC computed value */
472  return hcrc->Instance->DR;
473 }
474 
483 static uint32_t CRC_Handle_16(CRC_HandleTypeDef *hcrc, uint16_t pBuffer[], uint32_t BufferLength)
484 {
485  uint32_t i; /* input data buffer index */
486  __IO uint16_t *pReg;
487 
488  /* Processing time optimization: 2 HalfWords are entered in a row with a single word write,
489  * in case of odd length, last HalfWord must be carefully fed to the CRC calculator to ensure
490  * a correct type handling by the peripheral */
491  for (i = 0U; i < (BufferLength / 2U); i++)
492  {
493  hcrc->Instance->DR = ((uint32_t)pBuffer[2U * i] << 16U) | (uint32_t)pBuffer[(2U * i) + 1U];
494  }
495  if ((BufferLength % 2U) != 0U)
496  {
497  pReg = (__IO uint16_t *)(__IO void *)(&hcrc->Instance->DR); /* Derogation MisraC2012 R.11.5 */
498  *pReg = pBuffer[2U * i];
499  }
500 
501  /* Return the CRC computed value */
502  return hcrc->Instance->DR;
503 }
504 
509 #endif /* HAL_CRC_MODULE_ENABLED */
510 
518 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
uint32_t OutputDataInversionMode
uint32_t GeneratingPolynomial
This file contains all the functions prototypes for the HAL module driver.
static uint32_t CRC_Handle_8(CRC_HandleTypeDef *hcrc, uint8_t pBuffer[], uint32_t BufferLength)
Enter 8-bit input data to the CRC calculator. Specific data handling to optimize processing time...
__IO HAL_CRC_StateTypeDef State
__HAL_UNLOCK(hrtc)
void HAL_CRC_MspInit(CRC_HandleTypeDef *hcrc)
Initializes the CRC MSP.
uint32_t HAL_CRC_Accumulate(CRC_HandleTypeDef *hcrc, uint32_t pBuffer[], uint32_t BufferLength)
Compute the 7, 8, 16 or 32-bit CRC value of an 8, 16 or 32-bit data buffer starting with the previous...
CLEAR_BIT(hrtc->Instance->CR, RTC_CR_WUTE)
static uint32_t CRC_Handle_16(CRC_HandleTypeDef *hcrc, uint16_t pBuffer[], uint32_t BufferLength)
Enter 16-bit input data to the CRC calculator. Specific data handling to optimize processing time...
return HAL_OK
uint8_t DefaultPolynomialUse
HAL_StatusTypeDef HAL_CRCEx_Polynomial_Set(CRC_HandleTypeDef *hcrc, uint32_t Pol, uint32_t PolyLength)
Initialize the CRC polynomial if different from default one.
CRC_InitTypeDef Init
CRC_TypeDef * Instance
uint32_t InputDataInversionMode
uint32_t HAL_CRC_Calculate(CRC_HandleTypeDef *hcrc, uint32_t pBuffer[], uint32_t BufferLength)
Compute the 7, 8, 16 or 32-bit CRC value of an 8, 16 or 32-bit data buffer starting with hcrc->Instan...
CRC Handle Structure definition.
HAL_StatusTypeDef HAL_CRC_DeInit(CRC_HandleTypeDef *hcrc)
DeInitialize the CRC peripheral.
HAL_LockTypeDef Lock
MODIFY_REG(hrtc->Instance->CR, RTC_CR_WUCKSEL,(uint32_t) WakeUpClock)
HAL_CRC_StateTypeDef
CRC HAL State Structure definition.
uint8_t DefaultInitValueUse
assert_param(IS_RTC_WAKEUP_CLOCK(WakeUpClock))
void HAL_CRC_MspDeInit(CRC_HandleTypeDef *hcrc)
DeInitialize the CRC MSP.
HAL_StatusTypeDef HAL_CRC_Init(CRC_HandleTypeDef *hcrc)
Initialize the CRC according to the specified parameters in the CRC_InitTypeDef and create the associ...
HAL_CRC_StateTypeDef HAL_CRC_GetState(CRC_HandleTypeDef *hcrc)
Return the CRC handle state.