![]() |
|
|||
File indexing completed on 2025-05-11 08:23:06
0001 /** 0002 ****************************************************************************** 0003 * @file stm32h7xx_hal_crc.c 0004 * @author MCD Application Team 0005 * @brief CRC HAL module driver. 0006 * This file provides firmware functions to manage the following 0007 * functionalities of the Cyclic Redundancy Check (CRC) peripheral: 0008 * + Initialization and de-initialization functions 0009 * + Peripheral Control functions 0010 * + Peripheral State functions 0011 * 0012 ****************************************************************************** 0013 * @attention 0014 * 0015 * Copyright (c) 2017 STMicroelectronics. 0016 * All rights reserved. 0017 * 0018 * This software is licensed under terms that can be found in the LICENSE file 0019 * in the root directory of this software component. 0020 * If no LICENSE file comes with this software, it is provided AS-IS. 0021 * 0022 ****************************************************************************** 0023 @verbatim 0024 =============================================================================== 0025 ##### How to use this driver ##### 0026 =============================================================================== 0027 [..] 0028 (+) Enable CRC AHB clock using __HAL_RCC_CRC_CLK_ENABLE(); 0029 (+) Initialize CRC calculator 0030 (++) specify generating polynomial (peripheral default or non-default one) 0031 (++) specify initialization value (peripheral default or non-default one) 0032 (++) specify input data format 0033 (++) specify input or output data inversion mode if any 0034 (+) Use HAL_CRC_Accumulate() function to compute the CRC value of the 0035 input data buffer starting with the previously computed CRC as 0036 initialization value 0037 (+) Use HAL_CRC_Calculate() function to compute the CRC value of the 0038 input data buffer starting with the defined initialization value 0039 (default or non-default) to initiate CRC calculation 0040 0041 @endverbatim 0042 ****************************************************************************** 0043 */ 0044 0045 /* Includes ------------------------------------------------------------------*/ 0046 #include "stm32h7xx_hal.h" 0047 0048 /** @addtogroup STM32H7xx_HAL_Driver 0049 * @{ 0050 */ 0051 0052 /** @defgroup CRC CRC 0053 * @ingroup RTEMSBSPsARMSTM32H7 0054 * @brief CRC HAL module driver. 0055 * @{ 0056 */ 0057 0058 #ifdef HAL_CRC_MODULE_ENABLED 0059 0060 /* Private typedef -----------------------------------------------------------*/ 0061 /* Private define ------------------------------------------------------------*/ 0062 /* Private macro -------------------------------------------------------------*/ 0063 /* Private variables ---------------------------------------------------------*/ 0064 /* Private function prototypes -----------------------------------------------*/ 0065 /** @defgroup CRC_Private_Functions CRC Private Functions 0066 * @ingroup RTEMSBSPsARMSTM32H7 0067 * @{ 0068 */ 0069 static uint32_t CRC_Handle_8(CRC_HandleTypeDef *hcrc, uint8_t pBuffer[], uint32_t BufferLength); 0070 static uint32_t CRC_Handle_16(CRC_HandleTypeDef *hcrc, uint16_t pBuffer[], uint32_t BufferLength); 0071 /** 0072 * @} 0073 */ 0074 0075 /* Exported functions --------------------------------------------------------*/ 0076 0077 /** @defgroup CRC_Exported_Functions CRC Exported Functions 0078 * @ingroup RTEMSBSPsARMSTM32H7 0079 * @{ 0080 */ 0081 0082 /** @defgroup CRC_Exported_Functions_Group1 Initialization and de-initialization functions 0083 * @ingroup RTEMSBSPsARMSTM32H7 0084 * @brief Initialization and Configuration functions. 0085 * 0086 @verbatim 0087 =============================================================================== 0088 ##### Initialization and de-initialization functions ##### 0089 =============================================================================== 0090 [..] This section provides functions allowing to: 0091 (+) Initialize the CRC according to the specified parameters 0092 in the CRC_InitTypeDef and create the associated handle 0093 (+) DeInitialize the CRC peripheral 0094 (+) Initialize the CRC MSP (MCU Specific Package) 0095 (+) DeInitialize the CRC MSP 0096 0097 @endverbatim 0098 * @{ 0099 */ 0100 0101 /** 0102 * @brief Initialize the CRC according to the specified 0103 * parameters in the CRC_InitTypeDef and create the associated handle. 0104 * @param hcrc CRC handle 0105 * @retval HAL status 0106 */ 0107 HAL_StatusTypeDef HAL_CRC_Init(CRC_HandleTypeDef *hcrc) 0108 { 0109 /* Check the CRC handle allocation */ 0110 if (hcrc == NULL) 0111 { 0112 return HAL_ERROR; 0113 } 0114 0115 /* Check the parameters */ 0116 assert_param(IS_CRC_ALL_INSTANCE(hcrc->Instance)); 0117 0118 if (hcrc->State == HAL_CRC_STATE_RESET) 0119 { 0120 /* Allocate lock resource and initialize it */ 0121 hcrc->Lock = HAL_UNLOCKED; 0122 /* Init the low level hardware */ 0123 HAL_CRC_MspInit(hcrc); 0124 } 0125 0126 hcrc->State = HAL_CRC_STATE_BUSY; 0127 0128 /* check whether or not non-default generating polynomial has been 0129 * picked up by user */ 0130 assert_param(IS_DEFAULT_POLYNOMIAL(hcrc->Init.DefaultPolynomialUse)); 0131 if (hcrc->Init.DefaultPolynomialUse == DEFAULT_POLYNOMIAL_ENABLE) 0132 { 0133 /* initialize peripheral with default generating polynomial */ 0134 WRITE_REG(hcrc->Instance->POL, DEFAULT_CRC32_POLY); 0135 MODIFY_REG(hcrc->Instance->CR, CRC_CR_POLYSIZE, CRC_POLYLENGTH_32B); 0136 } 0137 else 0138 { 0139 /* initialize CRC peripheral with generating polynomial defined by user */ 0140 if (HAL_CRCEx_Polynomial_Set(hcrc, hcrc->Init.GeneratingPolynomial, hcrc->Init.CRCLength) != HAL_OK) 0141 { 0142 return HAL_ERROR; 0143 } 0144 } 0145 0146 /* check whether or not non-default CRC initial value has been 0147 * picked up by user */ 0148 assert_param(IS_DEFAULT_INIT_VALUE(hcrc->Init.DefaultInitValueUse)); 0149 if (hcrc->Init.DefaultInitValueUse == DEFAULT_INIT_VALUE_ENABLE) 0150 { 0151 WRITE_REG(hcrc->Instance->INIT, DEFAULT_CRC_INITVALUE); 0152 } 0153 else 0154 { 0155 WRITE_REG(hcrc->Instance->INIT, hcrc->Init.InitValue); 0156 } 0157 0158 0159 /* set input data inversion mode */ 0160 assert_param(IS_CRC_INPUTDATA_INVERSION_MODE(hcrc->Init.InputDataInversionMode)); 0161 MODIFY_REG(hcrc->Instance->CR, CRC_CR_REV_IN, hcrc->Init.InputDataInversionMode); 0162 0163 /* set output data inversion mode */ 0164 assert_param(IS_CRC_OUTPUTDATA_INVERSION_MODE(hcrc->Init.OutputDataInversionMode)); 0165 MODIFY_REG(hcrc->Instance->CR, CRC_CR_REV_OUT, hcrc->Init.OutputDataInversionMode); 0166 0167 /* makes sure the input data format (bytes, halfwords or words stream) 0168 * is properly specified by user */ 0169 assert_param(IS_CRC_INPUTDATA_FORMAT(hcrc->InputDataFormat)); 0170 0171 /* Change CRC peripheral state */ 0172 hcrc->State = HAL_CRC_STATE_READY; 0173 0174 /* Return function status */ 0175 return HAL_OK; 0176 } 0177 0178 /** 0179 * @brief DeInitialize the CRC peripheral. 0180 * @param hcrc CRC handle 0181 * @retval HAL status 0182 */ 0183 HAL_StatusTypeDef HAL_CRC_DeInit(CRC_HandleTypeDef *hcrc) 0184 { 0185 /* Check the CRC handle allocation */ 0186 if (hcrc == NULL) 0187 { 0188 return HAL_ERROR; 0189 } 0190 0191 /* Check the parameters */ 0192 assert_param(IS_CRC_ALL_INSTANCE(hcrc->Instance)); 0193 0194 /* Check the CRC peripheral state */ 0195 if (hcrc->State == HAL_CRC_STATE_BUSY) 0196 { 0197 return HAL_BUSY; 0198 } 0199 0200 /* Change CRC peripheral state */ 0201 hcrc->State = HAL_CRC_STATE_BUSY; 0202 0203 /* Reset CRC calculation unit */ 0204 __HAL_CRC_DR_RESET(hcrc); 0205 0206 /* Reset IDR register content */ 0207 CLEAR_REG(hcrc->Instance->IDR); 0208 0209 /* DeInit the low level hardware */ 0210 HAL_CRC_MspDeInit(hcrc); 0211 0212 /* Change CRC peripheral state */ 0213 hcrc->State = HAL_CRC_STATE_RESET; 0214 0215 /* Process unlocked */ 0216 __HAL_UNLOCK(hcrc); 0217 0218 /* Return function status */ 0219 return HAL_OK; 0220 } 0221 0222 /** 0223 * @brief Initializes the CRC MSP. 0224 * @param hcrc CRC handle 0225 * @retval None 0226 */ 0227 __weak void HAL_CRC_MspInit(CRC_HandleTypeDef *hcrc) 0228 { 0229 /* Prevent unused argument(s) compilation warning */ 0230 UNUSED(hcrc); 0231 0232 /* NOTE : This function should not be modified, when the callback is needed, 0233 the HAL_CRC_MspInit can be implemented in the user file 0234 */ 0235 } 0236 0237 /** 0238 * @brief DeInitialize the CRC MSP. 0239 * @param hcrc CRC handle 0240 * @retval None 0241 */ 0242 __weak void HAL_CRC_MspDeInit(CRC_HandleTypeDef *hcrc) 0243 { 0244 /* Prevent unused argument(s) compilation warning */ 0245 UNUSED(hcrc); 0246 0247 /* NOTE : This function should not be modified, when the callback is needed, 0248 the HAL_CRC_MspDeInit can be implemented in the user file 0249 */ 0250 } 0251 0252 /** 0253 * @} 0254 */ 0255 0256 /** @defgroup CRC_Exported_Functions_Group2 Peripheral Control functions 0257 * @ingroup RTEMSBSPsARMSTM32H7 0258 * @brief management functions. 0259 * 0260 @verbatim 0261 =============================================================================== 0262 ##### Peripheral Control functions ##### 0263 =============================================================================== 0264 [..] This section provides functions allowing to: 0265 (+) compute the 7, 8, 16 or 32-bit CRC value of an 8, 16 or 32-bit data buffer 0266 using combination of the previous CRC value and the new one. 0267 0268 [..] or 0269 0270 (+) compute the 7, 8, 16 or 32-bit CRC value of an 8, 16 or 32-bit data buffer 0271 independently of the previous CRC value. 0272 0273 @endverbatim 0274 * @{ 0275 */ 0276 0277 /** 0278 * @brief Compute the 7, 8, 16 or 32-bit CRC value of an 8, 16 or 32-bit data buffer 0279 * starting with the previously computed CRC as initialization value. 0280 * @param hcrc CRC handle 0281 * @param pBuffer pointer to the input data buffer, exact input data format is 0282 * provided by hcrc->InputDataFormat. 0283 * @param BufferLength input data buffer length (number of bytes if pBuffer 0284 * type is * uint8_t, number of half-words if pBuffer type is * uint16_t, 0285 * number of words if pBuffer type is * uint32_t). 0286 * @note By default, the API expects a uint32_t pointer as input buffer parameter. 0287 * Input buffer pointers with other types simply need to be cast in uint32_t 0288 * and the API will internally adjust its input data processing based on the 0289 * handle field hcrc->InputDataFormat. 0290 * @retval uint32_t CRC (returned value LSBs for CRC shorter than 32 bits) 0291 */ 0292 uint32_t HAL_CRC_Accumulate(CRC_HandleTypeDef *hcrc, uint32_t pBuffer[], uint32_t BufferLength) 0293 { 0294 uint32_t index; /* CRC input data buffer index */ 0295 uint32_t temp = 0U; /* CRC output (read from hcrc->Instance->DR register) */ 0296 0297 /* Change CRC peripheral state */ 0298 hcrc->State = HAL_CRC_STATE_BUSY; 0299 0300 switch (hcrc->InputDataFormat) 0301 { 0302 case CRC_INPUTDATA_FORMAT_WORDS: 0303 /* Enter Data to the CRC calculator */ 0304 for (index = 0U; index < BufferLength; index++) 0305 { 0306 hcrc->Instance->DR = pBuffer[index]; 0307 } 0308 temp = hcrc->Instance->DR; 0309 break; 0310 0311 case CRC_INPUTDATA_FORMAT_BYTES: 0312 temp = CRC_Handle_8(hcrc, (uint8_t *)pBuffer, BufferLength); 0313 break; 0314 0315 case CRC_INPUTDATA_FORMAT_HALFWORDS: 0316 temp = CRC_Handle_16(hcrc, (uint16_t *)(void *)pBuffer, BufferLength); /* Derogation MisraC2012 R.11.5 */ 0317 break; 0318 default: 0319 break; 0320 } 0321 0322 /* Change CRC peripheral state */ 0323 hcrc->State = HAL_CRC_STATE_READY; 0324 0325 /* Return the CRC computed value */ 0326 return temp; 0327 } 0328 0329 /** 0330 * @brief Compute the 7, 8, 16 or 32-bit CRC value of an 8, 16 or 32-bit data buffer 0331 * starting with hcrc->Instance->INIT as initialization value. 0332 * @param hcrc CRC handle 0333 * @param pBuffer pointer to the input data buffer, exact input data format is 0334 * provided by hcrc->InputDataFormat. 0335 * @param BufferLength input data buffer length (number of bytes if pBuffer 0336 * type is * uint8_t, number of half-words if pBuffer type is * uint16_t, 0337 * number of words if pBuffer type is * uint32_t). 0338 * @note By default, the API expects a uint32_t pointer as input buffer parameter. 0339 * Input buffer pointers with other types simply need to be cast in uint32_t 0340 * and the API will internally adjust its input data processing based on the 0341 * handle field hcrc->InputDataFormat. 0342 * @retval uint32_t CRC (returned value LSBs for CRC shorter than 32 bits) 0343 */ 0344 uint32_t HAL_CRC_Calculate(CRC_HandleTypeDef *hcrc, uint32_t pBuffer[], uint32_t BufferLength) 0345 { 0346 uint32_t index; /* CRC input data buffer index */ 0347 uint32_t temp = 0U; /* CRC output (read from hcrc->Instance->DR register) */ 0348 0349 /* Change CRC peripheral state */ 0350 hcrc->State = HAL_CRC_STATE_BUSY; 0351 0352 /* Reset CRC Calculation Unit (hcrc->Instance->INIT is 0353 * written in hcrc->Instance->DR) */ 0354 __HAL_CRC_DR_RESET(hcrc); 0355 0356 switch (hcrc->InputDataFormat) 0357 { 0358 case CRC_INPUTDATA_FORMAT_WORDS: 0359 /* Enter 32-bit input data to the CRC calculator */ 0360 for (index = 0U; index < BufferLength; index++) 0361 { 0362 hcrc->Instance->DR = pBuffer[index]; 0363 } 0364 temp = hcrc->Instance->DR; 0365 break; 0366 0367 case CRC_INPUTDATA_FORMAT_BYTES: 0368 /* Specific 8-bit input data handling */ 0369 temp = CRC_Handle_8(hcrc, (uint8_t *)pBuffer, BufferLength); 0370 break; 0371 0372 case CRC_INPUTDATA_FORMAT_HALFWORDS: 0373 /* Specific 16-bit input data handling */ 0374 temp = CRC_Handle_16(hcrc, (uint16_t *)(void *)pBuffer, BufferLength); /* Derogation MisraC2012 R.11.5 */ 0375 break; 0376 0377 default: 0378 break; 0379 } 0380 0381 /* Change CRC peripheral state */ 0382 hcrc->State = HAL_CRC_STATE_READY; 0383 0384 /* Return the CRC computed value */ 0385 return temp; 0386 } 0387 0388 /** 0389 * @} 0390 */ 0391 0392 /** @defgroup CRC_Exported_Functions_Group3 Peripheral State functions 0393 * @ingroup RTEMSBSPsARMSTM32H7 0394 * @brief Peripheral State functions. 0395 * 0396 @verbatim 0397 =============================================================================== 0398 ##### Peripheral State functions ##### 0399 =============================================================================== 0400 [..] 0401 This subsection permits to get in run-time the status of the peripheral. 0402 0403 @endverbatim 0404 * @{ 0405 */ 0406 0407 /** 0408 * @brief Return the CRC handle state. 0409 * @param hcrc CRC handle 0410 * @retval HAL state 0411 */ 0412 HAL_CRC_StateTypeDef HAL_CRC_GetState(const CRC_HandleTypeDef *hcrc) 0413 { 0414 /* Return CRC handle state */ 0415 return hcrc->State; 0416 } 0417 0418 /** 0419 * @} 0420 */ 0421 0422 /** 0423 * @} 0424 */ 0425 0426 /** @addtogroup CRC_Private_Functions 0427 * @{ 0428 */ 0429 0430 /** 0431 * @brief Enter 8-bit input data to the CRC calculator. 0432 * Specific data handling to optimize processing time. 0433 * @param hcrc CRC handle 0434 * @param pBuffer pointer to the input data buffer 0435 * @param BufferLength input data buffer length 0436 * @retval uint32_t CRC (returned value LSBs for CRC shorter than 32 bits) 0437 */ 0438 static uint32_t CRC_Handle_8(CRC_HandleTypeDef *hcrc, uint8_t pBuffer[], uint32_t BufferLength) 0439 { 0440 uint32_t i; /* input data buffer index */ 0441 uint16_t data; 0442 __IO uint16_t *pReg; 0443 0444 /* Processing time optimization: 4 bytes are entered in a row with a single word write, 0445 * last bytes must be carefully fed to the CRC calculator to ensure a correct type 0446 * handling by the peripheral */ 0447 for (i = 0U; i < (BufferLength / 4U); i++) 0448 { 0449 hcrc->Instance->DR = ((uint32_t)pBuffer[4U * i] << 24U) | \ 0450 ((uint32_t)pBuffer[(4U * i) + 1U] << 16U) | \ 0451 ((uint32_t)pBuffer[(4U * i) + 2U] << 8U) | \ 0452 (uint32_t)pBuffer[(4U * i) + 3U]; 0453 } 0454 /* last bytes specific handling */ 0455 if ((BufferLength % 4U) != 0U) 0456 { 0457 if ((BufferLength % 4U) == 1U) 0458 { 0459 *(__IO uint8_t *)(__IO void *)(&hcrc->Instance->DR) = pBuffer[4U * i]; /* Derogation MisraC2012 R.11.5 */ 0460 } 0461 if ((BufferLength % 4U) == 2U) 0462 { 0463 data = ((uint16_t)(pBuffer[4U * i]) << 8U) | (uint16_t)pBuffer[(4U * i) + 1U]; 0464 pReg = (__IO uint16_t *)(__IO void *)(&hcrc->Instance->DR); /* Derogation MisraC2012 R.11.5 */ 0465 *pReg = data; 0466 } 0467 if ((BufferLength % 4U) == 3U) 0468 { 0469 data = ((uint16_t)(pBuffer[4U * i]) << 8U) | (uint16_t)pBuffer[(4U * i) + 1U]; 0470 pReg = (__IO uint16_t *)(__IO void *)(&hcrc->Instance->DR); /* Derogation MisraC2012 R.11.5 */ 0471 *pReg = data; 0472 0473 *(__IO uint8_t *)(__IO void *)(&hcrc->Instance->DR) = pBuffer[(4U * i) + 2U]; /* Derogation MisraC2012 R.11.5 */ 0474 } 0475 } 0476 0477 /* Return the CRC computed value */ 0478 return hcrc->Instance->DR; 0479 } 0480 0481 /** 0482 * @brief Enter 16-bit input data to the CRC calculator. 0483 * Specific data handling to optimize processing time. 0484 * @param hcrc CRC handle 0485 * @param pBuffer pointer to the input data buffer 0486 * @param BufferLength input data buffer length 0487 * @retval uint32_t CRC (returned value LSBs for CRC shorter than 32 bits) 0488 */ 0489 static uint32_t CRC_Handle_16(CRC_HandleTypeDef *hcrc, uint16_t pBuffer[], uint32_t BufferLength) 0490 { 0491 uint32_t i; /* input data buffer index */ 0492 __IO uint16_t *pReg; 0493 0494 /* Processing time optimization: 2 HalfWords are entered in a row with a single word write, 0495 * in case of odd length, last HalfWord must be carefully fed to the CRC calculator to ensure 0496 * a correct type handling by the peripheral */ 0497 for (i = 0U; i < (BufferLength / 2U); i++) 0498 { 0499 hcrc->Instance->DR = ((uint32_t)pBuffer[2U * i] << 16U) | (uint32_t)pBuffer[(2U * i) + 1U]; 0500 } 0501 if ((BufferLength % 2U) != 0U) 0502 { 0503 pReg = (__IO uint16_t *)(__IO void *)(&hcrc->Instance->DR); /* Derogation MisraC2012 R.11.5 */ 0504 *pReg = pBuffer[2U * i]; 0505 } 0506 0507 /* Return the CRC computed value */ 0508 return hcrc->Instance->DR; 0509 } 0510 0511 /** 0512 * @} 0513 */ 0514 0515 #endif /* HAL_CRC_MODULE_ENABLED */ 0516 /** 0517 * @} 0518 */ 0519 0520 /** 0521 * @} 0522 */
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |
![]() ![]() |