Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:23:06

0001 /**
0002   ******************************************************************************
0003   * @file    stm32h7xx_hal_crc_ex.c
0004   * @author  MCD Application Team
0005   * @brief   Extended CRC HAL module driver.
0006   *          This file provides firmware functions to manage the extended
0007   *          functionalities of the CRC peripheral.
0008   *
0009   ******************************************************************************
0010   * @attention
0011   *
0012   * Copyright (c) 2017 STMicroelectronics.
0013   * All rights reserved.
0014   *
0015   * This software is licensed under terms that can be found in the LICENSE file
0016   * in the root directory of this software component.
0017   * If no LICENSE file comes with this software, it is provided AS-IS.
0018   *
0019   ******************************************************************************
0020   @verbatim
0021 ================================================================================
0022             ##### How to use this driver #####
0023 ================================================================================
0024     [..]
0025          (+) Set user-defined generating polynomial through HAL_CRCEx_Polynomial_Set()
0026          (+) Configure Input or Output data inversion
0027 
0028   @endverbatim
0029   ******************************************************************************
0030   */
0031 
0032 /* Includes ------------------------------------------------------------------*/
0033 #include "stm32h7xx_hal.h"
0034 
0035 /** @addtogroup STM32H7xx_HAL_Driver
0036   * @{
0037   */
0038 
0039 /** @defgroup CRCEx CRCEx
0040   * @ingroup RTEMSBSPsARMSTM32H7
0041   * @brief CRC Extended HAL module driver
0042   * @{
0043   */
0044 
0045 #ifdef HAL_CRC_MODULE_ENABLED
0046 
0047 /* Private typedef -----------------------------------------------------------*/
0048 /* Private define ------------------------------------------------------------*/
0049 /* Private macro -------------------------------------------------------------*/
0050 /* Private variables ---------------------------------------------------------*/
0051 /* Private function prototypes -----------------------------------------------*/
0052 /* Exported functions --------------------------------------------------------*/
0053 
0054 /** @defgroup CRCEx_Exported_Functions CRC Extended Exported Functions
0055   * @ingroup RTEMSBSPsARMSTM32H7
0056   * @{
0057   */
0058 
0059 /** @defgroup CRCEx_Exported_Functions_Group1 Extended Initialization/de-initialization functions
0060   * @ingroup RTEMSBSPsARMSTM32H7
0061   * @brief    Extended Initialization and Configuration functions.
0062   *
0063 @verbatim
0064  ===============================================================================
0065             ##### Extended configuration functions #####
0066  ===============================================================================
0067     [..]  This section provides functions allowing to:
0068       (+) Configure the generating polynomial
0069       (+) Configure the input data inversion
0070       (+) Configure the output data inversion
0071 
0072 @endverbatim
0073   * @{
0074   */
0075 
0076 
0077 /**
0078   * @brief  Initialize the CRC polynomial if different from default one.
0079   * @param  hcrc CRC handle
0080   * @param  Pol CRC generating polynomial (7, 8, 16 or 32-bit long).
0081   *         This parameter is written in normal representation, e.g.
0082   *         @arg for a polynomial of degree 7, X^7 + X^6 + X^5 + X^2 + 1 is written 0x65
0083   *         @arg for a polynomial of degree 16, X^16 + X^12 + X^5 + 1 is written 0x1021
0084   * @param  PolyLength CRC polynomial length.
0085   *         This parameter can be one of the following values:
0086   *          @arg @ref CRC_POLYLENGTH_7B  7-bit long CRC (generating polynomial of degree 7)
0087   *          @arg @ref CRC_POLYLENGTH_8B  8-bit long CRC (generating polynomial of degree 8)
0088   *          @arg @ref CRC_POLYLENGTH_16B 16-bit long CRC (generating polynomial of degree 16)
0089   *          @arg @ref CRC_POLYLENGTH_32B 32-bit long CRC (generating polynomial of degree 32)
0090   * @retval HAL status
0091   */
0092 HAL_StatusTypeDef HAL_CRCEx_Polynomial_Set(CRC_HandleTypeDef *hcrc, uint32_t Pol, uint32_t PolyLength)
0093 {
0094   HAL_StatusTypeDef status = HAL_OK;
0095   uint32_t msb = 31U; /* polynomial degree is 32 at most, so msb is initialized to max value */
0096 
0097   /* Check the parameters */
0098   assert_param(IS_CRC_POL_LENGTH(PolyLength));
0099 
0100   /* Ensure that the generating polynomial is odd */
0101   if ((Pol & (uint32_t)(0x1U)) ==  0U)
0102   {
0103     status =  HAL_ERROR;
0104   }
0105   else
0106   {
0107     /* check polynomial definition vs polynomial size:
0108      * polynomial length must be aligned with polynomial
0109      * definition. HAL_ERROR is reported if Pol degree is
0110      * larger than that indicated by PolyLength.
0111      * Look for MSB position: msb will contain the degree of
0112      *  the second to the largest polynomial member. E.g., for
0113      *  X^7 + X^6 + X^5 + X^2 + 1, msb = 6. */
0114     while ((msb-- > 0U) && ((Pol & ((uint32_t)(0x1U) << (msb & 0x1FU))) == 0U))
0115     {
0116     }
0117 
0118     switch (PolyLength)
0119     {
0120 
0121       case CRC_POLYLENGTH_7B:
0122         if (msb >= HAL_CRC_LENGTH_7B)
0123         {
0124           status =   HAL_ERROR;
0125         }
0126         break;
0127       case CRC_POLYLENGTH_8B:
0128         if (msb >= HAL_CRC_LENGTH_8B)
0129         {
0130           status =   HAL_ERROR;
0131         }
0132         break;
0133       case CRC_POLYLENGTH_16B:
0134         if (msb >= HAL_CRC_LENGTH_16B)
0135         {
0136           status =   HAL_ERROR;
0137         }
0138         break;
0139 
0140       case CRC_POLYLENGTH_32B:
0141         /* no polynomial definition vs. polynomial length issue possible */
0142         break;
0143       default:
0144         status =  HAL_ERROR;
0145         break;
0146     }
0147   }
0148   if (status == HAL_OK)
0149   {
0150     /* set generating polynomial */
0151     WRITE_REG(hcrc->Instance->POL, Pol);
0152 
0153     /* set generating polynomial size */
0154     MODIFY_REG(hcrc->Instance->CR, CRC_CR_POLYSIZE, PolyLength);
0155   }
0156   /* Return function status */
0157   return status;
0158 }
0159 
0160 /**
0161   * @brief  Set the Reverse Input data mode.
0162   * @param  hcrc CRC handle
0163   * @param  InputReverseMode Input Data inversion mode.
0164   *         This parameter can be one of the following values:
0165   *          @arg @ref CRC_INPUTDATA_INVERSION_NONE     no change in bit order (default value)
0166   *          @arg @ref CRC_INPUTDATA_INVERSION_BYTE     Byte-wise bit reversal
0167   *          @arg @ref CRC_INPUTDATA_INVERSION_HALFWORD HalfWord-wise bit reversal
0168   *          @arg @ref CRC_INPUTDATA_INVERSION_WORD     Word-wise bit reversal
0169   * @retval HAL status
0170   */
0171 HAL_StatusTypeDef HAL_CRCEx_Input_Data_Reverse(CRC_HandleTypeDef *hcrc, uint32_t InputReverseMode)
0172 {
0173   /* Check the parameters */
0174   assert_param(IS_CRC_INPUTDATA_INVERSION_MODE(InputReverseMode));
0175 
0176   /* Change CRC peripheral state */
0177   hcrc->State = HAL_CRC_STATE_BUSY;
0178 
0179   /* set input data inversion mode */
0180   MODIFY_REG(hcrc->Instance->CR, CRC_CR_REV_IN, InputReverseMode);
0181   /* Change CRC peripheral state */
0182   hcrc->State = HAL_CRC_STATE_READY;
0183 
0184   /* Return function status */
0185   return HAL_OK;
0186 }
0187 
0188 /**
0189   * @brief  Set the Reverse Output data mode.
0190   * @param  hcrc CRC handle
0191   * @param  OutputReverseMode Output Data inversion mode.
0192   *         This parameter can be one of the following values:
0193   *          @arg @ref CRC_OUTPUTDATA_INVERSION_DISABLE no CRC inversion (default value)
0194   *          @arg @ref CRC_OUTPUTDATA_INVERSION_ENABLE  bit-level inversion (e.g. for a 8-bit CRC: 0xB5 becomes 0xAD)
0195   * @retval HAL status
0196   */
0197 HAL_StatusTypeDef HAL_CRCEx_Output_Data_Reverse(CRC_HandleTypeDef *hcrc, uint32_t OutputReverseMode)
0198 {
0199   /* Check the parameters */
0200   assert_param(IS_CRC_OUTPUTDATA_INVERSION_MODE(OutputReverseMode));
0201 
0202   /* Change CRC peripheral state */
0203   hcrc->State = HAL_CRC_STATE_BUSY;
0204 
0205   /* set output data inversion mode */
0206   MODIFY_REG(hcrc->Instance->CR, CRC_CR_REV_OUT, OutputReverseMode);
0207 
0208   /* Change CRC peripheral state */
0209   hcrc->State = HAL_CRC_STATE_READY;
0210 
0211   /* Return function status */
0212   return HAL_OK;
0213 }
0214 
0215 
0216 
0217 
0218 /**
0219   * @}
0220   */
0221 
0222 
0223 /**
0224   * @}
0225   */
0226 
0227 
0228 #endif /* HAL_CRC_MODULE_ENABLED */
0229 /**
0230   * @}
0231   */
0232 
0233 /**
0234   * @}
0235   */