STM32G474xx HAL用户手册
stm32g4xx_hal_crc_ex.c
查看此文件的文档。
00001 /**
00002   ******************************************************************************
00003   * @file    stm32g4xx_hal_crc_ex.c
00004   * @author  MCD Application Team
00005   * @brief   Extended CRC HAL module driver.
00006   *          This file provides firmware functions to manage the extended
00007   *          functionalities of the CRC peripheral.
00008   *
00009   ******************************************************************************
00010   * @attention
00011   *
00012   * Copyright (c) 2019 STMicroelectronics.
00013   * All rights reserved.
00014   *
00015   * This software is licensed under terms that can be found in the LICENSE file
00016   * in the root directory of this software component.
00017   * If no LICENSE file comes with this software, it is provided AS-IS.
00018   *
00019   ******************************************************************************
00020   @verbatim
00021 ================================================================================
00022             ##### How to use this driver #####
00023 ================================================================================
00024     [..]
00025          (+) Set user-defined generating polynomial through HAL_CRCEx_Polynomial_Set()
00026          (+) Configure Input or Output data inversion
00027 
00028   @endverbatim
00029   ******************************************************************************
00030   */
00031 
00032 /* Includes ------------------------------------------------------------------*/
00033 #include stm32g4xx_hal.h"
00034 
00035 /** @addtogroup STM32G4xx_HAL_Driver
00036   * @{
00037   */
00038 
00039 /** @defgroup CRCEx CRCEx
00040   * @brief CRC Extended HAL module driver
00041   * @{
00042   */
00043 
00044 #ifdef HAL_CRC_MODULE_ENABLED
00045 
00046 /* Private typedef -----------------------------------------------------------*/
00047 /* Private define ------------------------------------------------------------*/
00048 /* Private macro -------------------------------------------------------------*/
00049 /* Private variables ---------------------------------------------------------*/
00050 /* Private function prototypes -----------------------------------------------*/
00051 /* Exported functions --------------------------------------------------------*/
00052 
00053 /** @defgroup CRCEx_Exported_Functions CRC Extended Exported Functions
00054   * @{
00055   */
00056 
00057 /** @defgroup CRCEx_Exported_Functions_Group1 Extended Initialization/de-initialization functions
00058   * @brief    Extended Initialization and Configuration functions.
00059   *
00060 @verbatim
00061  ===============================================================================
00062             ##### Extended configuration functions #####
00063  ===============================================================================
00064     [..]  This section provides functions allowing to:
00065       (+) Configure the generating polynomial
00066       (+) Configure the input data inversion
00067       (+) Configure the output data inversion
00068 
00069 @endverbatim
00070   * @{
00071   */
00072 
00073 
00074 /**
00075   * @brief  Initialize the CRC polynomial if different from default one.
00076   * @param  hcrc CRC handle
00077   * @param  Pol CRC generating polynomial (7, 8, 16 or 32-bit long).
00078   *         This parameter is written in normal representation, e.g.
00079   *         @arg for a polynomial of degree 7, X^7 + X^6 + X^5 + X^2 + 1 is written 0x65
00080   *         @arg for a polynomial of degree 16, X^16 + X^12 + X^5 + 1 is written 0x1021
00081   * @param  PolyLength CRC polynomial length.
00082   *         This parameter can be one of the following values:
00083   *          @arg @ref CRC_POLYLENGTH_7B  7-bit long CRC (generating polynomial of degree 7)
00084   *          @arg @ref CRC_POLYLENGTH_8B  8-bit long CRC (generating polynomial of degree 8)
00085   *          @arg @ref CRC_POLYLENGTH_16B 16-bit long CRC (generating polynomial of degree 16)
00086   *          @arg @ref CRC_POLYLENGTH_32B 32-bit long CRC (generating polynomial of degree 32)
00087   * @retval HAL status
00088   */
00089 HAL_StatusTypeDef HAL_CRCEx_Polynomial_Set(CRC_HandleTypeDef *hcrc, uint32_t Pol, uint32_t PolyLength)
00090 {
00091   HAL_StatusTypeDef status = HAL_OK;
00092   uint32_t msb = 31U; /* polynomial degree is 32 at most, so msb is initialized to max value */
00093 
00094   /* Check the parameters */
00095   assert_param(IS_CRC_POL_LENGTH(PolyLength));
00096 
00097   /* Ensure that the generating polynomial is odd */
00098   if ((Pol & (uint32_t)(0x1U)) ==  0U)
00099   {
00100     status =  HAL_ERROR;
00101   }
00102   else
00103   {
00104     /* check polynomial definition vs polynomial size:
00105      * polynomial length must be aligned with polynomial
00106      * definition. HAL_ERROR is reported if Pol degree is
00107      * larger than that indicated by PolyLength.
00108      * Look for MSB position: msb will contain the degree of
00109      *  the second to the largest polynomial member. E.g., for
00110      *  X^7 + X^6 + X^5 + X^2 + 1, msb = 6. */
00111     while ((msb-- > 0U) && ((Pol & ((uint32_t)(0x1U) << (msb & 0x1FU))) == 0U))
00112     {
00113     }
00114 
00115     switch (PolyLength)
00116     {
00117 
00118       case CRC_POLYLENGTH_7B:
00119         if (msb >= HAL_CRC_LENGTH_7B)
00120         {
00121           status =   HAL_ERROR;
00122         }
00123         break;
00124       case CRC_POLYLENGTH_8B:
00125         if (msb >= HAL_CRC_LENGTH_8B)
00126         {
00127           status =   HAL_ERROR;
00128         }
00129         break;
00130       case CRC_POLYLENGTH_16B:
00131         if (msb >= HAL_CRC_LENGTH_16B)
00132         {
00133           status =   HAL_ERROR;
00134         }
00135         break;
00136 
00137       case CRC_POLYLENGTH_32B:
00138         /* no polynomial definition vs. polynomial length issue possible */
00139         break;
00140       default:
00141         status =  HAL_ERROR;
00142         break;
00143     }
00144   }
00145   if (status == HAL_OK)
00146   {
00147     /* set generating polynomial */
00148     WRITE_REG(hcrc->Instance->POL, Pol);
00149 
00150     /* set generating polynomial size */
00151     MODIFY_REG(hcrc->Instance->CR, CRC_CR_POLYSIZE, PolyLength);
00152   }
00153   /* Return function status */
00154   return status;
00155 }
00156 
00157 /**
00158   * @brief  Set the Reverse Input data mode.
00159   * @param  hcrc CRC handle
00160   * @param  InputReverseMode Input Data inversion mode.
00161   *         This parameter can be one of the following values:
00162   *          @arg @ref CRC_INPUTDATA_INVERSION_NONE     no change in bit order (default value)
00163   *          @arg @ref CRC_INPUTDATA_INVERSION_BYTE     Byte-wise bit reversal
00164   *          @arg @ref CRC_INPUTDATA_INVERSION_HALFWORD HalfWord-wise bit reversal
00165   *          @arg @ref CRC_INPUTDATA_INVERSION_WORD     Word-wise bit reversal
00166   * @retval HAL status
00167   */
00168 HAL_StatusTypeDef HAL_CRCEx_Input_Data_Reverse(CRC_HandleTypeDef *hcrc, uint32_t InputReverseMode)
00169 {
00170   /* Check the parameters */
00171   assert_param(IS_CRC_INPUTDATA_INVERSION_MODE(InputReverseMode));
00172 
00173   /* Change CRC peripheral state */
00174   hcrc->State = HAL_CRC_STATE_BUSY;
00175 
00176   /* set input data inversion mode */
00177   MODIFY_REG(hcrc->Instance->CR, CRC_CR_REV_IN, InputReverseMode);
00178   /* Change CRC peripheral state */
00179   hcrc->State = HAL_CRC_STATE_READY;
00180 
00181   /* Return function status */
00182   return HAL_OK;
00183 }
00184 
00185 /**
00186   * @brief  Set the Reverse Output data mode.
00187   * @param  hcrc CRC handle
00188   * @param  OutputReverseMode Output Data inversion mode.
00189   *         This parameter can be one of the following values:
00190   *          @arg @ref CRC_OUTPUTDATA_INVERSION_DISABLE no CRC inversion (default value)
00191   *          @arg @ref CRC_OUTPUTDATA_INVERSION_ENABLE  bit-level inversion (e.g. for a 8-bit CRC: 0xB5 becomes 0xAD)
00192   * @retval HAL status
00193   */
00194 HAL_StatusTypeDef HAL_CRCEx_Output_Data_Reverse(CRC_HandleTypeDef *hcrc, uint32_t OutputReverseMode)
00195 {
00196   /* Check the parameters */
00197   assert_param(IS_CRC_OUTPUTDATA_INVERSION_MODE(OutputReverseMode));
00198 
00199   /* Change CRC peripheral state */
00200   hcrc->State = HAL_CRC_STATE_BUSY;
00200 
00201   /* set output data inversion mode */
00202   MODIFY_REG(hcrc->Instance->CR, CRC_CR_REV_OUT, OutputReverseMode);
00203 
00204   /* Change CRC peripheral state */
00205   hcrc->State = HAL_CRC_STATE_READY;
00206 
00207   /* Return function status */
00208   return HAL_OK;
00209 }
00210 
00211 
00212 /**
00213   * @}
00214   */
00215 
00216 
00217 
00218 /**
00219   * @}
00220   */
00221 
00222 
00223 #endif /* HAL_CRC_MODULE_ENABLED */
00224 /**
00225