STM32L443xx HAL User Manual
stm32l4xx_hal_crc_ex.c
Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    stm32l4xx_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) 2017 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 "stm32l4xx_hal.h"
00034 
00035 /** @addtogroup STM32L4xx_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   /* check polynomial definition vs polynomial size:
00098    * polynomial length must be aligned with polynomial
00099    * definition. HAL_ERROR is reported if Pol degree is
00100    * larger than that indicated by PolyLength.
00101    * Look for MSB position: msb will contain the degree of
00102    *  the second to the largest polynomial member. E.g., for
00103    *  X^7 + X^6 + X^5 + X^2 + 1, msb = 6. */
00104   while ((msb-- > 0U) && ((Pol & ((uint32_t)(0x1U) << (msb & 0x1FU))) == 0U))
00105   {
00106   }
00107 
00108   switch (PolyLength)
00109   {
00110     case CRC_POLYLENGTH_7B:
00111       if (msb >= HAL_CRC_LENGTH_7B)
00112       {
00113         status =   HAL_ERROR;
00114       }
00115       break;
00116     case CRC_POLYLENGTH_8B:
00117       if (msb >= HAL_CRC_LENGTH_8B)
00118       {
00119         status =   HAL_ERROR;
00120       }
00121       break;
00122     case CRC_POLYLENGTH_16B:
00123       if (msb >= HAL_CRC_LENGTH_16B)
00124       {
00125         status =   HAL_ERROR;
00126       }
00127       break;
00128 
00129     case CRC_POLYLENGTH_32B:
00130       /* no polynomial definition vs. polynomial length issue possible */
00131       break;
00132     default:
00133       status =  HAL_ERROR;
00134       break;
00135   }
00136   if (status == HAL_OK)
00137   {
00138     /* set generating polynomial */
00139     WRITE_REG(hcrc->Instance->POL, Pol);
00140 
00141     /* set generating polynomial size */
00142     MODIFY_REG(hcrc->Instance->CR, CRC_CR_POLYSIZE, PolyLength);
00143   }
00144   /* Return function status */
00145   return status;
00146 }
00147 
00148 /**
00149   * @brief  Set the Reverse Input data mode.
00150   * @param  hcrc CRC handle
00151   * @param  InputReverseMode Input Data inversion mode.
00152   *         This parameter can be one of the following values:
00153   *          @arg @ref CRC_INPUTDATA_INVERSION_NONE     no change in bit order (default value)
00154   *          @arg @ref CRC_INPUTDATA_INVERSION_BYTE     Byte-wise bit reversal
00155   *          @arg @ref CRC_INPUTDATA_INVERSION_HALFWORD HalfWord-wise bit reversal
00156   *          @arg @ref CRC_INPUTDATA_INVERSION_WORD     Word-wise bit reversal
00157   * @retval HAL status
00158   */
00159 HAL_StatusTypeDef HAL_CRCEx_Input_Data_Reverse(CRC_HandleTypeDef *hcrc, uint32_t InputReverseMode)
00160 {
00161   /* Check the parameters */
00162   assert_param(IS_CRC_INPUTDATA_INVERSION_MODE(InputReverseMode));
00163 
00164   /* Change CRC peripheral state */
00165   hcrc->State = HAL_CRC_STATE_BUSY;
00166 
00167   /* set input data inversion mode */
00168   MODIFY_REG(hcrc->Instance->CR, CRC_CR_REV_IN, InputReverseMode);
00169   /* Change CRC peripheral state */
00170   hcrc->State = HAL_CRC_STATE_READY;
00171 
00172   /* Return function status */
00173   return HAL_OK;
00174 }
00175 
00176 /**
00177   * @brief  Set the Reverse Output data mode.
00178   * @param  hcrc CRC handle
00179   * @param  OutputReverseMode Output Data inversion mode.
00180   *         This parameter can be one of the following values:
00181   *          @arg @ref CRC_OUTPUTDATA_INVERSION_DISABLE no CRC inversion (default value)
00182   *          @arg @ref CRC_OUTPUTDATA_INVERSION_ENABLE  bit-level inversion (e.g. for a 8-bit CRC: 0xB5 becomes 0xAD)
00183   * @retval HAL status
00184   */
00185 HAL_StatusTypeDef HAL_CRCEx_Output_Data_Reverse(CRC_HandleTypeDef *hcrc, uint32_t OutputReverseMode)
00186 {
00187   /* Check the parameters */
00188   assert_param(IS_CRC_OUTPUTDATA_INVERSION_MODE(OutputReverseMode));
00189 
00190   /* Change CRC peripheral state */
00191   hcrc->State = HAL_CRC_STATE_BUSY;
00192 
00193   /* set output data inversion mode */
00194   MODIFY_REG(hcrc->Instance->CR, CRC_CR_REV_OUT, OutputReverseMode);
00195 
00196   /* Change CRC peripheral state */
00197   hcrc->State = HAL_CRC_STATE_READY;
00198 
00199   /* Return function status */
00200   return HAL_OK;
00201 }
00202 
00203 
00204 
00205 
00206 /**
00207   * @}
00208   */
00209 
00210 
00211 /**
00212   * @}
00213   */
00214 
00215 
00216 #endif /* HAL_CRC_MODULE_ENABLED */
00217 /**
00218   * @}
00219   */
00220 
00221 /**
00222   * @}
00223   */