STM32L486xx 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   @verbatim
00010 ================================================================================
00011             ##### How to use this driver #####
00012 ================================================================================
00013     [..]
00014          (+) Set user-defined generating polynomial thru HAL_CRCEx_Polynomial_Set()
00015          (+) Configure Input or Output data inversion
00016 
00017   @endverbatim
00018   ******************************************************************************
00019   * @attention
00020   *
00021   * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
00022   *
00023   * Redistribution and use in source and binary forms, with or without modification,
00024   * are permitted provided that the following conditions are met:
00025   *   1. Redistributions of source code must retain the above copyright notice,
00026   *      this list of conditions and the following disclaimer.
00027   *   2. Redistributions in binary form must reproduce the above copyright notice,
00028   *      this list of conditions and the following disclaimer in the documentation
00029   *      and/or other materials provided with the distribution.
00030   *   3. Neither the name of STMicroelectronics nor the names of its contributors
00031   *      may be used to endorse or promote products derived from this software
00032   *      without specific prior written permission.
00033   *
00034   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00035   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00036   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00037   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00038   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00039   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00040   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00041   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00042   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00043   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00044   *
00045   ******************************************************************************
00046   */
00047 
00048 /* Includes ------------------------------------------------------------------*/
00049 #include "stm32l4xx_hal.h"
00050 
00051 /** @addtogroup STM32L4xx_HAL_Driver
00052   * @{
00053   */
00054 
00055 /** @defgroup CRCEx CRCEx
00056   * @brief CRC Extended HAL module driver
00057   * @{
00058   */
00059 
00060 #ifdef HAL_CRC_MODULE_ENABLED
00061 
00062 /* Private typedef -----------------------------------------------------------*/
00063 /* Private define ------------------------------------------------------------*/
00064 /* Private macro -------------------------------------------------------------*/
00065 /* Private variables ---------------------------------------------------------*/
00066 /* Private function prototypes -----------------------------------------------*/
00067 /* Exported functions --------------------------------------------------------*/
00068 
00069 /** @defgroup CRCEx_Exported_Functions CRC Extended Exported Functions
00070   * @{
00071   */
00072 
00073 /** @defgroup CRCEx_Exported_Functions_Group1 Extended Initialization/de-initialization functions
00074   * @brief    Extended Initialization and Configuration functions.
00075   *
00076 @verbatim
00077  ===============================================================================
00078             ##### Extended configuration functions #####
00079  ===============================================================================
00080     [..]  This section provides functions allowing to:
00081       (+) Configure the generating polynomial
00082       (+) Configure the input data inversion
00083       (+) Configure the output data inversion
00084 
00085 @endverbatim
00086   * @{
00087   */
00088 
00089 
00090 /**
00091   * @brief  Initialize the CRC polynomial if different from default one.
00092   * @param  hcrc CRC handle
00093   * @param  Pol CRC generating polynomial (7, 8, 16 or 32-bit long).
00094   *         This parameter is written in normal representation, e.g.
00095   *         @arg for a polynomial of degree 7, X^7 + X^6 + X^5 + X^2 + 1 is written 0x65
00096   *         @arg for a polynomial of degree 16, X^16 + X^12 + X^5 + 1 is written 0x1021
00097   * @param  PolyLength CRC polynomial length.
00098   *         This parameter can be one of the following values:
00099   *          @arg @ref CRC_POLYLENGTH_7B  7-bit long CRC (generating polynomial of degree 7)
00100   *          @arg @ref CRC_POLYLENGTH_8B  8-bit long CRC (generating polynomial of degree 8)
00101   *          @arg @ref CRC_POLYLENGTH_16B 16-bit long CRC (generating polynomial of degree 16)
00102   *          @arg @ref CRC_POLYLENGTH_32B 32-bit long CRC (generating polynomial of degree 32)
00103   * @retval HAL status
00104   */
00105 HAL_StatusTypeDef HAL_CRCEx_Polynomial_Set(CRC_HandleTypeDef *hcrc, uint32_t Pol, uint32_t PolyLength)
00106 {
00107   HAL_StatusTypeDef status = HAL_OK;
00108   uint32_t msb = 31U; /* polynomial degree is 32 at most, so msb is initialized to max value */
00109 
00110   /* Check the parameters */
00111   assert_param(IS_CRC_POL_LENGTH(PolyLength));
00112 
00113   /* check polynomial definition vs polynomial size:
00114    * polynomial length must be aligned with polynomial
00115    * definition. HAL_ERROR is reported if Pol degree is
00116    * larger than that indicated by PolyLength.
00117    * Look for MSB position: msb will contain the degree of
00118    *  the second to the largest polynomial member. E.g., for
00119    *  X^7 + X^6 + X^5 + X^2 + 1, msb = 6. */
00120   while ((msb-- > 0U) && ((Pol & ((uint32_t)(0x1U) << msb)) == 0U))
00121   {
00122   }
00123 
00124   switch (PolyLength)
00125   {
00126     case CRC_POLYLENGTH_7B:
00127       if (msb >= HAL_CRC_LENGTH_7B)
00128       {
00129         status =   HAL_ERROR;
00130       }
00131       break;
00132     case CRC_POLYLENGTH_8B:
00133       if (msb >= HAL_CRC_LENGTH_8B)
00134       {
00135         status =   HAL_ERROR;
00136       }
00137       break;
00138     case CRC_POLYLENGTH_16B:
00139       if (msb >= HAL_CRC_LENGTH_16B)
00140       {
00141         status =   HAL_ERROR;
00142       }
00143       break;
00144 
00145     case CRC_POLYLENGTH_32B:
00146       /* no polynomial definition vs. polynomial length issue possible */
00147       break;
00148     default:
00149       status =  HAL_ERROR;
00150       break;
00151   }
00152   if (status == HAL_OK)
00153   {
00154     /* set generating polynomial */
00155     WRITE_REG(hcrc->Instance->POL, Pol);
00156 
00157     /* set generating polynomial size */
00158     MODIFY_REG(hcrc->Instance->CR, CRC_CR_POLYSIZE, PolyLength);
00159   }
00160   /* Return function status */
00161   return status;
00162 }
00163 
00164 /**
00165   * @brief  Set the Reverse Input data mode.
00166   * @param  hcrc CRC handle
00167   * @param  InputReverseMode Input Data inversion mode.
00168   *         This parameter can be one of the following values:
00169   *          @arg @ref CRC_INPUTDATA_INVERSION_NONE     no change in bit order (default value)
00170   *          @arg @ref CRC_INPUTDATA_INVERSION_BYTE     Byte-wise bit reversal
00171   *          @arg @ref CRC_INPUTDATA_INVERSION_HALFWORD HalfWord-wise bit reversal
00172   *          @arg @ref CRC_INPUTDATA_INVERSION_WORD     Word-wise bit reversal
00173   * @retval HAL status
00174   */
00175 HAL_StatusTypeDef HAL_CRCEx_Input_Data_Reverse(CRC_HandleTypeDef *hcrc, uint32_t InputReverseMode)
00176 {
00177   /* Check the parameters */
00178   assert_param(IS_CRC_INPUTDATA_INVERSION_MODE(InputReverseMode));
00179 
00180   /* Change CRC peripheral state */
00181   hcrc->State = HAL_CRC_STATE_BUSY;
00182 
00183   /* set input data inversion mode */
00184   MODIFY_REG(hcrc->Instance->CR, CRC_CR_REV_IN, InputReverseMode);
00185   /* Change CRC peripheral state */
00186   hcrc->State = HAL_CRC_STATE_READY;
00187 
00188   /* Return function status */
00189   return HAL_OK;
00190 }
00191 
00192 /**
00193   * @brief  Set the Reverse Output data mode.
00194   * @param  hcrc CRC handle
00195   * @param  OutputReverseMode Output Data inversion mode.
00196   *         This parameter can be one of the following values:
00197   *          @arg @ref CRC_OUTPUTDATA_INVERSION_DISABLE no CRC inversion (default value)
00198   *          @arg @ref CRC_OUTPUTDATA_INVERSION_ENABLE  bit-level inversion (e.g. for a 8-bit CRC: 0xB5 becomes 0xAD)
00199   * @retval HAL status
00200   */
00201 HAL_StatusTypeDef HAL_CRCEx_Output_Data_Reverse(CRC_HandleTypeDef *hcrc, uint32_t OutputReverseMode)
00202 {
00203   /* Check the parameters */
00204   assert_param(IS_CRC_OUTPUTDATA_INVERSION_MODE(OutputReverseMode));
00205 
00206   /* Change CRC peripheral state */
00207   hcrc->State = HAL_CRC_STATE_BUSY;
00208 
00209   /* set output data inversion mode */
00210   MODIFY_REG(hcrc->Instance->CR, CRC_CR_REV_OUT, OutputReverseMode);
00211 
00212   /* Change CRC peripheral state */
00213   hcrc->State = HAL_CRC_STATE_READY;
00214 
00215   /* Return function status */
00216   return HAL_OK;
00217 }
00218 
00219 
00220 
00221 
00222 /**
00223   * @}
00224   */
00225 
00226 
00227 /**
00228   * @}
00229   */
00230 
00231 
00232 #endif /* HAL_CRC_MODULE_ENABLED */
00233 /**
00234   * @}
00235   */
00236 
00237 /**
00238   * @}
00239   */
00240 
00241 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/