STM32F103xB HAL User Manual
stm32f1xx_hal_crc.c
Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    stm32f1xx_hal_crc.c
00004   * @author  MCD Application Team
00005   * @brief   CRC HAL module driver.
00006   *          This file provides firmware functions to manage the following
00007   *          functionalities of the Cyclic Redundancy Check (CRC) peripheral:
00008   *           + Initialization and de-initialization functions
00009   *           + Peripheral Control functions
00010   *           + Peripheral State functions
00011   *
00012   @verbatim
00013  ===============================================================================
00014                      ##### How to use this driver #####
00015  ===============================================================================
00016     [..]
00017          (+) Enable CRC AHB clock using __HAL_RCC_CRC_CLK_ENABLE();
00018          (+) Initialize CRC calculator
00019              (++) specify generating polynomial (peripheral default or non-default one)
00020              (++) specify initialization value (peripheral default or non-default one)
00021              (++) specify input data format
00022              (++) specify input or output data inversion mode if any
00023          (+) Use HAL_CRC_Accumulate() function to compute the CRC value of the
00024              input data buffer starting with the previously computed CRC as
00025              initialization value
00026          (+) Use HAL_CRC_Calculate() function to compute the CRC value of the
00027              input data buffer starting with the defined initialization value
00028              (default or non-default) to initiate CRC calculation
00029 
00030   @endverbatim
00031   ******************************************************************************
00032   * @attention
00033   *
00034   * <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
00035   * All rights reserved.</center></h2>
00036   *
00037   * This software component is licensed by ST under BSD 3-Clause license,
00038   * the "License"; You may not use this file except in compliance with the
00039   * License. You may obtain a copy of the License at:
00040   *                        opensource.org/licenses/BSD-3-Clause
00041   *
00042   ******************************************************************************
00043   */
00044 
00045 /* Includes ------------------------------------------------------------------*/
00046 #include "stm32f1xx_hal.h"
00047 
00048 /** @addtogroup STM32F1xx_HAL_Driver
00049   * @{
00050   */
00051 
00052 /** @defgroup CRC CRC
00053   * @brief CRC HAL module driver.
00054   * @{
00055   */
00056 
00057 #ifdef HAL_CRC_MODULE_ENABLED
00058 
00059 /* Private typedef -----------------------------------------------------------*/
00060 /* Private define ------------------------------------------------------------*/
00061 /* Private macro -------------------------------------------------------------*/
00062 /* Private variables ---------------------------------------------------------*/
00063 /* Private function prototypes -----------------------------------------------*/
00064 
00065 /* Exported functions --------------------------------------------------------*/
00066 
00067 /** @defgroup CRC_Exported_Functions CRC Exported Functions
00068   * @{
00069   */
00070 
00071 /** @defgroup CRC_Exported_Functions_Group1 Initialization and de-initialization functions
00072   *  @brief    Initialization and Configuration functions.
00073   *
00074 @verbatim
00075  ===============================================================================
00076             ##### Initialization and de-initialization functions #####
00077  ===============================================================================
00078     [..]  This section provides functions allowing to:
00079       (+) Initialize the CRC according to the specified parameters
00080           in the CRC_InitTypeDef and create the associated handle
00081       (+) DeInitialize the CRC peripheral
00082       (+) Initialize the CRC MSP (MCU Specific Package)
00083       (+) DeInitialize the CRC MSP
00084 
00085 @endverbatim
00086   * @{
00087   */
00088 
00089 /**
00090   * @brief  Initialize the CRC according to the specified
00091   *         parameters in the CRC_InitTypeDef and create the associated handle.
00092   * @param  hcrc CRC handle
00093   * @retval HAL status
00094   */
00095 HAL_StatusTypeDef HAL_CRC_Init(CRC_HandleTypeDef *hcrc)
00096 {
00097   /* Check the CRC handle allocation */
00098   if (hcrc == NULL)
00099   {
00100     return HAL_ERROR;
00101   }
00102 
00103   /* Check the parameters */
00104   assert_param(IS_CRC_ALL_INSTANCE(hcrc->Instance));
00105 
00106   if (hcrc->State == HAL_CRC_STATE_RESET)
00107   {
00108     /* Allocate lock resource and initialize it */
00109     hcrc->Lock = HAL_UNLOCKED;
00110     /* Init the low level hardware */
00111     HAL_CRC_MspInit(hcrc);
00112   }
00113 
00114   /* Change CRC peripheral state */
00115   hcrc->State = HAL_CRC_STATE_READY;
00116 
00117   /* Return function status */
00118   return HAL_OK;
00119 }
00120 
00121 /**
00122   * @brief  DeInitialize the CRC peripheral.
00123   * @param  hcrc CRC handle
00124   * @retval HAL status
00125   */
00126 HAL_StatusTypeDef HAL_CRC_DeInit(CRC_HandleTypeDef *hcrc)
00127 {
00128   /* Check the CRC handle allocation */
00129   if (hcrc == NULL)
00130   {
00131     return HAL_ERROR;
00132   }
00133 
00134   /* Check the parameters */
00135   assert_param(IS_CRC_ALL_INSTANCE(hcrc->Instance));
00136 
00137   /* Check the CRC peripheral state */
00138   if (hcrc->State == HAL_CRC_STATE_BUSY)
00139   {
00140     return HAL_BUSY;
00141   }
00142 
00143   /* Change CRC peripheral state */
00144   hcrc->State = HAL_CRC_STATE_BUSY;
00145 
00146   /* Reset CRC calculation unit */
00147   __HAL_CRC_DR_RESET(hcrc);
00148 
00149   /* Reset IDR register content */
00150   CLEAR_BIT(hcrc->Instance->IDR, CRC_IDR_IDR);
00151 
00152   /* DeInit the low level hardware */
00153   HAL_CRC_MspDeInit(hcrc);
00154 
00155   /* Change CRC peripheral state */
00156   hcrc->State = HAL_CRC_STATE_RESET;
00157 
00158   /* Process unlocked */
00159   __HAL_UNLOCK(hcrc);
00160 
00161   /* Return function status */
00162   return HAL_OK;
00163 }
00164 
00165 /**
00166   * @brief  Initializes the CRC MSP.
00167   * @param  hcrc CRC handle
00168   * @retval None
00169   */
00170 __weak void HAL_CRC_MspInit(CRC_HandleTypeDef *hcrc)
00171 {
00172   /* Prevent unused argument(s) compilation warning */
00173   UNUSED(hcrc);
00174 
00175   /* NOTE : This function should not be modified, when the callback is needed,
00176             the HAL_CRC_MspInit can be implemented in the user file
00177    */
00178 }
00179 
00180 /**
00181   * @brief  DeInitialize the CRC MSP.
00182   * @param  hcrc CRC handle
00183   * @retval None
00184   */
00185 __weak void HAL_CRC_MspDeInit(CRC_HandleTypeDef *hcrc)
00186 {
00187   /* Prevent unused argument(s) compilation warning */
00188   UNUSED(hcrc);
00189 
00190   /* NOTE : This function should not be modified, when the callback is needed,
00191             the HAL_CRC_MspDeInit can be implemented in the user file
00192    */
00193 }
00194 
00195 /**
00196   * @}
00197   */
00198 
00199 /** @defgroup CRC_Exported_Functions_Group2 Peripheral Control functions
00200   *  @brief    management functions.
00201   *
00202 @verbatim
00203  ===============================================================================
00204                       ##### Peripheral Control functions #####
00205  ===============================================================================
00206     [..]  This section provides functions allowing to:
00207       (+) compute the 32-bit CRC value of a 32-bit data buffer
00208           using combination of the previous CRC value and the new one.
00209 
00210        [..]  or
00211 
00212       (+) compute the 32-bit CRC value of a 32-bit data buffer
00213           independently of the previous CRC value.
00214 
00215 @endverbatim
00216   * @{
00217   */
00218 
00219 /**
00220   * @brief  Compute the 32-bit CRC value of a 32-bit data buffer
00221   *         starting with the previously computed CRC as initialization value.
00222   * @param  hcrc CRC handle
00223   * @param  pBuffer pointer to the input data buffer.
00224   * @param  BufferLength input data buffer length (number of uint32_t words).
00225   * @retval uint32_t CRC (returned value LSBs for CRC shorter than 32 bits)
00226   */
00227 uint32_t HAL_CRC_Accumulate(CRC_HandleTypeDef *hcrc, uint32_t pBuffer[], uint32_t BufferLength)
00228 {
00229   uint32_t index;      /* CRC input data buffer index */
00230   uint32_t temp = 0U;  /* CRC output (read from hcrc->Instance->DR register) */
00231 
00232   /* Change CRC peripheral state */
00233   hcrc->State = HAL_CRC_STATE_BUSY;
00234 
00235   /* Enter Data to the CRC calculator */
00236   for (index = 0U; index < BufferLength; index++)
00237   {
00238     hcrc->Instance->DR = pBuffer[index];
00239   }
00240   temp = hcrc->Instance->DR;
00241 
00242   /* Change CRC peripheral state */
00243   hcrc->State = HAL_CRC_STATE_READY;
00244 
00245   /* Return the CRC computed value */
00246   return temp;
00247 }
00248 
00249 /**
00250   * @brief  Compute the 32-bit CRC value of a 32-bit data buffer
00251   *         starting with hcrc->Instance->INIT as initialization value.
00252   * @param  hcrc CRC handle
00253   * @param  pBuffer pointer to the input data buffer.
00254   * @param  BufferLength input data buffer length (number of uint32_t words).
00255   * @retval uint32_t CRC (returned value LSBs for CRC shorter than 32 bits)
00256   */
00257 uint32_t HAL_CRC_Calculate(CRC_HandleTypeDef *hcrc, uint32_t pBuffer[], uint32_t BufferLength)
00258 {
00259   uint32_t index;      /* CRC input data buffer index */
00260   uint32_t temp = 0U;  /* CRC output (read from hcrc->Instance->DR register) */
00261 
00262   /* Change CRC peripheral state */
00263   hcrc->State = HAL_CRC_STATE_BUSY;
00264 
00265   /* Reset CRC Calculation Unit (hcrc->Instance->INIT is
00266   *  written in hcrc->Instance->DR) */
00267   __HAL_CRC_DR_RESET(hcrc);
00268 
00269   /* Enter 32-bit input data to the CRC calculator */
00270   for (index = 0U; index < BufferLength; index++)
00271   {
00272     hcrc->Instance->DR = pBuffer[index];
00273   }
00274   temp = hcrc->Instance->DR;
00275 
00276   /* Change CRC peripheral state */
00277   hcrc->State = HAL_CRC_STATE_READY;
00278 
00279   /* Return the CRC computed value */
00280   return temp;
00281 }
00282 
00283 /**
00284   * @}
00285   */
00286 
00287 /** @defgroup CRC_Exported_Functions_Group3 Peripheral State functions
00288   *  @brief    Peripheral State functions.
00289   *
00290 @verbatim
00291  ===============================================================================
00292                       ##### Peripheral State functions #####
00293  ===============================================================================
00294     [..]
00295     This subsection permits to get in run-time the status of the peripheral.
00296 
00297 @endverbatim
00298   * @{
00299   */
00300 
00301 /**
00302   * @brief  Return the CRC handle state.
00303   * @param  hcrc CRC handle
00304   * @retval HAL state
00305   */
00306 HAL_CRC_StateTypeDef HAL_CRC_GetState(CRC_HandleTypeDef *hcrc)
00307 {
00308   /* Return CRC handle state */
00309   return hcrc->State;
00310 }
00311 
00312 /**
00313   * @}
00314   */
00315 
00316 /**
00317   * @}
00318   */
00319 
00320 
00321 #endif /* HAL_CRC_MODULE_ENABLED */
00322 /**
00323   * @}
00324   */
00325 
00326 /**
00327   * @}
00328   */
00329 
00330 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/