STM32F439xx HAL User Manual
stm32f4xx_hal_crc.c
Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    stm32f4xx_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       The CRC HAL driver can be used as follows:
00018 
00019       (#) Enable CRC AHB clock using __HAL_RCC_CRC_CLK_ENABLE();
00020 
00021       (#) Use HAL_CRC_Accumulate() function to compute the CRC value of 
00022           a 32-bit data buffer using combination of the previous CRC value
00023           and the new one.
00024 
00025       (#) Use HAL_CRC_Calculate() function to compute the CRC Value of 
00026           a new 32-bit data buffer. This function resets the CRC computation  
00027           unit before starting the computation to avoid getting wrong CRC values.
00028 
00029   @endverbatim
00030   ******************************************************************************
00031   * @attention
00032   *
00033   * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
00034   *
00035   * Redistribution and use in source and binary forms, with or without modification,
00036   * are permitted provided that the following conditions are met:
00037   *   1. Redistributions of source code must retain the above copyright notice,
00038   *      this list of conditions and the following disclaimer.
00039   *   2. Redistributions in binary form must reproduce the above copyright notice,
00040   *      this list of conditions and the following disclaimer in the documentation
00041   *      and/or other materials provided with the distribution.
00042   *   3. Neither the name of STMicroelectronics nor the names of its contributors
00043   *      may be used to endorse or promote products derived from this software
00044   *      without specific prior written permission.
00045   *
00046   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00047   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00048   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00049   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00050   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00051   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00052   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00053   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00054   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00055   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00056   *
00057   ******************************************************************************
00058   */
00059 
00060 /* Includes ------------------------------------------------------------------*/
00061 #include "stm32f4xx_hal.h"
00062 
00063 /** @addtogroup STM32F4xx_HAL_Driver
00064   * @{
00065   */
00066 
00067 /** @addtogroup CRC 
00068   * @{
00069   */
00070 
00071 #ifdef HAL_CRC_MODULE_ENABLED
00072 
00073 /* Private typedef -----------------------------------------------------------*/
00074 /* Private define ------------------------------------------------------------*/
00075 /* Private macro -------------------------------------------------------------*/
00076 /* Private variables ---------------------------------------------------------*/
00077 /* Private function prototypes -----------------------------------------------*/
00078 /* Private functions ---------------------------------------------------------*/
00079 /* Exported functions --------------------------------------------------------*/
00080 
00081 /** @addtogroup CRC_Exported_Functions
00082   * @{
00083   */
00084 
00085 /** @addtogroup CRC_Exported_Functions_Group1
00086  *  @brief   Initialization and de-initialization functions 
00087  *
00088 @verbatim     
00089   ==============================================================================
00090             ##### Initialization and de-initialization functions #####
00091   ==============================================================================
00092     [..]  This section provides functions allowing to:
00093       (+) Initialize the CRC according to the specified parameters 
00094           in the CRC_InitTypeDef and create the associated handle
00095       (+) DeInitialize the CRC peripheral
00096       (+) Initialize the CRC MSP
00097       (+) DeInitialize CRC MSP 
00098  
00099 @endverbatim
00100   * @{
00101   */
00102 
00103 /**
00104   * @brief  Initializes the CRC according to the specified
00105   *         parameters in the CRC_InitTypeDef and creates the associated handle.
00106   * @param  hcrc pointer to a CRC_HandleTypeDef structure that contains
00107   *         the configuration information for CRC
00108   * @retval HAL status
00109   */
00110 HAL_StatusTypeDef HAL_CRC_Init(CRC_HandleTypeDef *hcrc)
00111 {
00112   /* Check the CRC handle allocation */
00113   if(hcrc == NULL)
00114   {
00115     return HAL_ERROR;
00116   }
00117 
00118   /* Check the parameters */
00119   assert_param(IS_CRC_ALL_INSTANCE(hcrc->Instance));
00120 
00121   if(hcrc->State == HAL_CRC_STATE_RESET)
00122   {
00123     /* Allocate lock resource and initialize it */
00124     hcrc->Lock = HAL_UNLOCKED;
00125     /* Init the low level hardware */
00126     HAL_CRC_MspInit(hcrc);
00127   }
00128   
00129   /* Change CRC peripheral state */
00130   hcrc->State = HAL_CRC_STATE_BUSY;
00131    
00132   /* Change CRC peripheral state */
00133   hcrc->State = HAL_CRC_STATE_READY;
00134   
00135   /* Return function status */
00136   return HAL_OK;
00137 }
00138 
00139 /**
00140   * @brief  DeInitializes the CRC peripheral.
00141   * @param  hcrc pointer to a CRC_HandleTypeDef structure that contains
00142   *         the configuration information for CRC
00143   * @retval HAL status
00144   */
00145 HAL_StatusTypeDef HAL_CRC_DeInit(CRC_HandleTypeDef *hcrc)
00146 {
00147   /* Check the CRC handle allocation */
00148   if(hcrc == NULL)
00149   {
00150     return HAL_ERROR;
00151   }
00152 
00153   /* Check the parameters */
00154   assert_param(IS_CRC_ALL_INSTANCE(hcrc->Instance));
00155 
00156   /* Change CRC peripheral state */
00157   hcrc->State = HAL_CRC_STATE_BUSY;
00158 
00159   /* DeInit the low level hardware */
00160   HAL_CRC_MspDeInit(hcrc);
00161 
00162   /* Change CRC peripheral state */
00163   hcrc->State = HAL_CRC_STATE_RESET;
00164 
00165   /* Release Lock */
00166   __HAL_UNLOCK(hcrc);
00167 
00168   /* Return function status */
00169   return HAL_OK;
00170 }
00171 
00172 /**
00173   * @brief  Initializes the CRC MSP.
00174   * @param  hcrc pointer to a CRC_HandleTypeDef structure that contains
00175   *         the configuration information for CRC
00176   * @retval None
00177   */
00178 __weak void HAL_CRC_MspInit(CRC_HandleTypeDef *hcrc)
00179 {
00180   /* Prevent unused argument(s) compilation warning */
00181   UNUSED(hcrc);
00182   /* NOTE : This function Should not be modified, when the callback is needed,
00183             the HAL_CRC_MspInit could be implemented in the user file
00184    */
00185 }
00186 
00187 /**
00188   * @brief  DeInitializes the CRC MSP.
00189   * @param  hcrc pointer to a CRC_HandleTypeDef structure that contains
00190   *         the configuration information for CRC
00191   * @retval None
00192   */
00193 __weak void HAL_CRC_MspDeInit(CRC_HandleTypeDef *hcrc)
00194 {
00195   /* Prevent unused argument(s) compilation warning */
00196   UNUSED(hcrc);
00197   /* NOTE : This function Should not be modified, when the callback is needed,
00198             the HAL_CRC_MspDeInit could be implemented in the user file
00199    */
00200 }
00201 
00202 /**
00203   * @}
00204   */ 
00205 
00206 /** @addtogroup CRC_Exported_Functions_Group2
00207  *  @brief   Peripheral Control functions 
00208  *
00209 @verbatim  
00210   ==============================================================================
00211                       ##### Peripheral Control functions #####
00212   ==============================================================================  
00213     [..]  This section provides functions allowing to:
00214       (+) Compute the 32-bit CRC value of 32-bit data buffer,
00215           using combination of the previous CRC value and the new one.
00216       (+) Compute the 32-bit CRC value of 32-bit data buffer,
00217           independently of the previous CRC value.
00218 
00219 @endverbatim
00220   * @{
00221   */
00222 
00223 /**
00224   * @brief  Computes the 32-bit CRC of 32-bit data buffer using combination
00225   *         of the previous CRC value and the new one.
00226   * @param  hcrc pointer to a CRC_HandleTypeDef structure that contains
00227   *         the configuration information for CRC
00228   * @param  pBuffer pointer to the buffer containing the data to be computed
00229   * @param  BufferLength length of the buffer to be computed
00230   * @retval 32-bit CRC
00231   */
00232 uint32_t HAL_CRC_Accumulate(CRC_HandleTypeDef *hcrc, uint32_t pBuffer[], uint32_t BufferLength)
00233 {
00234   uint32_t index = 0U;
00235 
00236   /* Process Locked */
00237   __HAL_LOCK(hcrc);
00238 
00239   /* Change CRC peripheral state */
00240   hcrc->State = HAL_CRC_STATE_BUSY;
00241 
00242   /* Enter Data to the CRC calculator */
00243   for(index = 0U; index < BufferLength; index++)
00244   {
00245     hcrc->Instance->DR = pBuffer[index];
00246   }
00247 
00248   /* Change CRC peripheral state */
00249   hcrc->State = HAL_CRC_STATE_READY;
00250 
00251   /* Process Unlocked */
00252   __HAL_UNLOCK(hcrc);
00253 
00254   /* Return the CRC computed value */
00255   return hcrc->Instance->DR;
00256 }
00257 
00258 /**
00259   * @brief  Computes the 32-bit CRC of 32-bit data buffer independently
00260   *         of the previous CRC value.
00261   * @param  hcrc pointer to a CRC_HandleTypeDef structure that contains
00262   *         the configuration information for CRC
00263   * @param  pBuffer Pointer to the buffer containing the data to be computed
00264   * @param  BufferLength Length of the buffer to be computed
00265   * @retval 32-bit CRC
00266   */
00267 uint32_t HAL_CRC_Calculate(CRC_HandleTypeDef *hcrc, uint32_t pBuffer[], uint32_t BufferLength)
00268 {
00269   uint32_t index = 0U;
00270 
00271   /* Process Locked */
00272   __HAL_LOCK(hcrc); 
00273 
00274   /* Change CRC peripheral state */
00275   hcrc->State = HAL_CRC_STATE_BUSY;
00276 
00277   /* Reset CRC Calculation Unit */
00278   __HAL_CRC_DR_RESET(hcrc);
00279 
00280   /* Enter Data to the CRC calculator */
00281   for(index = 0U; index < BufferLength; index++)
00282   {
00283     hcrc->Instance->DR = pBuffer[index];
00284   }
00285 
00286   /* Change CRC peripheral state */
00287   hcrc->State = HAL_CRC_STATE_READY;
00288 
00289   /* Process Unlocked */
00290   __HAL_UNLOCK(hcrc);
00291 
00292   /* Return the CRC computed value */
00293   return hcrc->Instance->DR;
00294 }
00295 
00296 /**
00297   * @}
00298   */ 
00299 
00300   
00301 /** @addtogroup CRC_Exported_Functions_Group3
00302  *  @brief   Peripheral State functions 
00303  *
00304 @verbatim   
00305   ==============================================================================
00306                       ##### Peripheral State functions #####
00307   ==============================================================================  
00308     [..]
00309     This subsection permits to get in run-time the status of the peripheral 
00310     and the data flow.
00311 
00312 @endverbatim
00313   * @{
00314   */
00315 
00316 /**
00317   * @brief  Returns the CRC state.
00318   * @param  hcrc pointer to a CRC_HandleTypeDef structure that contains
00319   *         the configuration information for CRC
00320   * @retval HAL state
00321   */
00322 HAL_CRC_StateTypeDef HAL_CRC_GetState(CRC_HandleTypeDef *hcrc)
00323 {
00324   return hcrc->State;
00325 }
00326 
00327 /**
00328   * @}
00329   */
00330 
00331 /**
00332   * @}
00333   */
00334 
00335 #endif /* HAL_CRC_MODULE_ENABLED */
00336 /**
00337   * @}
00338   */
00339 
00340 /**
00341   * @}
00342   */
00343 
00344 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/