2 ******************************************************************************
3 * @file stm32f0xx_hal_crc_ex.c
4 * @author MCD Application Team
6 * @date 11-December-2014
7 * @brief Extended CRC HAL module driver.
8 * This file provides firmware functions to manage the following
9 * functionalities of the CRC peripheral:
10 * + Extended initialization functions
13 ================================================================================
14 ##### Product specific features #####
15 ================================================================================
17 ##### How to use this driver #####
18 ================================================================================
20 (+) Extended initialization
21 (+) Set or not user-defined generating
22 polynomial other than default one
25 ******************************************************************************
28 * <h2><center>© COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
30 * Redistribution and use in source and binary forms, with or without modification,
31 * are permitted provided that the following conditions are met:
32 * 1. Redistributions of source code must retain the above copyright notice,
33 * this list of conditions and the following disclaimer.
34 * 2. Redistributions in binary form must reproduce the above copyright notice,
35 * this list of conditions and the following disclaimer in the documentation
36 * and/or other materials provided with the distribution.
37 * 3. Neither the name of STMicroelectronics nor the names of its contributors
38 * may be used to endorse or promote products derived from this software
39 * without specific prior written permission.
41 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
42 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
44 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
47 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
48 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
49 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
50 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
52 ******************************************************************************
55 /* Includes ------------------------------------------------------------------*/
56 #include "stm32f0xx_hal.h"
58 /** @addtogroup STM32F0xx_HAL_Driver
62 /** @defgroup CRCEx CRCEx Extended HAL Module Driver
63 * @brief CRC Extended HAL module driver
67 #ifdef HAL_CRC_MODULE_ENABLED
69 /* Private typedef -----------------------------------------------------------*/
70 /* Private define ------------------------------------------------------------*/
71 /* Private macro -------------------------------------------------------------*/
72 /* Private variables ---------------------------------------------------------*/
73 /* Private function prototypes -----------------------------------------------*/
74 /* Private functions ---------------------------------------------------------*/
76 /** @defgroup CRCEx_Exported_Functions CRCEx Exported Functions
80 /** @defgroup CRCEx_Exported_Functions_Group1 Extended Initialization/de-initialization functions
81 * @brief Extended Initialization and Configuration functions.
84 ===============================================================================
85 ##### Initialization and Configuration functions #####
86 ===============================================================================
87 [..] This section provides functions allowing to:
88 (+) Initialize the CRC generating polynomial: if programmable polynomial
89 feature is applicable to device, set default or non-default generating
90 polynomial according to hcrc->Init.DefaultPolynomialUse parameter.
91 If feature is non-applicable to device in use, HAL_CRCEx_Init straight
93 (+) Set the generating polynomial
101 * @brief Extended initialization to set generating polynomial
102 * @param hcrc: CRC handle
105 HAL_StatusTypeDef HAL_CRCEx_Init(CRC_HandleTypeDef *hcrc)
107 #if defined(STM32F071xB) || defined(STM32F072xB) || defined(STM32F078xx) || defined(STM32F091xC) || defined (STM32F098xx)
108 /* check whether or not non-default generating polynomial has been
109 * picked up by user */
110 assert_param(IS_DEFAULT_POLYNOMIAL(hcrc->Init.DefaultPolynomialUse));
111 if (hcrc->Init.DefaultPolynomialUse == DEFAULT_POLYNOMIAL_ENABLE)
113 /* initialize IP with default generating polynomial */
114 WRITE_REG(hcrc->Instance->POL, DEFAULT_CRC32_POLY);
115 MODIFY_REG(hcrc->Instance->CR, CRC_CR_POLYSIZE, CRC_POLYLENGTH_32B);
119 /* initialize CRC IP with generating polynomial defined by user */
120 if (HAL_CRCEx_Polynomial_Set(hcrc, hcrc->Init.GeneratingPolynomial, hcrc->Init.CRCLength) != HAL_OK)
125 #endif /* defined(STM32F071xB) || defined(STM32F072xB) || defined(STM32F078xx) || defined(STM32F091xC) || defined (STM32F098xx) */
131 * @brief Set the Reverse Input data mode.
132 * @param hcrc: CRC handle
133 * @param InputReverseMode: Input Data inversion mode
134 * This parameter can be one of the following values:
135 * @arg CRC_INPUTDATA_NOINVERSION: no change in bit order (default value)
136 * @arg CRC_INPUTDATA_INVERSION_BYTE: Byte-wise bit reversal
137 * @arg CRC_INPUTDATA_INVERSION_HALFWORD: HalfWord-wise bit reversal
138 * @arg CRC_INPUTDATA_INVERSION_WORD: Word-wise bit reversal
141 HAL_StatusTypeDef HAL_CRCEx_Input_Data_Reverse(CRC_HandleTypeDef *hcrc, uint32_t InputReverseMode)
143 /* Check the parameters */
144 assert_param(IS_CRC_INPUTDATA_INVERSION_MODE(InputReverseMode));
146 /* Change CRC peripheral state */
147 hcrc->State = HAL_CRC_STATE_BUSY;
149 /* set input data inversion mode */
150 MODIFY_REG(hcrc->Instance->CR, CRC_CR_REV_IN, InputReverseMode);
151 /* Change CRC peripheral state */
152 hcrc->State = HAL_CRC_STATE_READY;
154 /* Return function status */
159 * @brief Set the Reverse Output data mode.
160 * @param hcrc: CRC handle
161 * @param OutputReverseMode: Output Data inversion mode
162 * This parameter can be one of the following values:
163 * @arg CRC_OUTPUTDATA_INVERSION_DISABLED: no CRC inversion (default value)
164 * @arg CRC_OUTPUTDATA_INVERSION_ENABLED: bit-level inversion (e.g for a 8-bit CRC: 0xB5 becomes 0xAD)
167 HAL_StatusTypeDef HAL_CRCEx_Output_Data_Reverse(CRC_HandleTypeDef *hcrc, uint32_t OutputReverseMode)
169 /* Check the parameters */
170 assert_param(IS_CRC_OUTPUTDATA_INVERSION_MODE(OutputReverseMode));
172 /* Change CRC peripheral state */
173 hcrc->State = HAL_CRC_STATE_BUSY;
175 /* set output data inversion mode */
176 MODIFY_REG(hcrc->Instance->CR, CRC_CR_REV_OUT, OutputReverseMode);
178 /* Change CRC peripheral state */
179 hcrc->State = HAL_CRC_STATE_READY;
181 /* Return function status */
185 #if defined (STM32F071xB) || defined (STM32F072xB) || defined (STM32F078xx) || defined (STM32F091xC) || defined (STM32F098xx)
187 * @brief Initializes the CRC polynomial if different from default one.
188 * @param hcrc: CRC handle
189 * @param Pol: CRC generating polynomial (7, 8, 16 or 32-bit long)
190 * This parameter is written in normal representation, e.g.
191 * for a polynomial of degree 7, X^7 + X^6 + X^5 + X^2 + 1 is written 0x65
192 * for a polynomial of degree 16, X^16 + X^12 + X^5 + 1 is written 0x1021
193 * @param PolyLength: CRC polynomial length
194 * This parameter can be one of the following values:
195 * @arg CRC_POLYLENGTH_7B: 7-bit long CRC (generating polynomial of degree 7)
196 * @arg CRC_POLYLENGTH_8B: 8-bit long CRC (generating polynomial of degree 8)
197 * @arg CRC_POLYLENGTH_16B: 16-bit long CRC (generating polynomial of degree 16)
198 * @arg CRC_POLYLENGTH_32B: 32-bit long CRC (generating polynomial of degree 32)
201 HAL_StatusTypeDef HAL_CRCEx_Polynomial_Set(CRC_HandleTypeDef *hcrc, uint32_t Pol, uint32_t PolyLength)
203 uint32_t msb = 31; /* polynomial degree is 32 at most, so msb is initialized to max value */
205 /* Check the parameters */
206 assert_param(IS_CRC_POL_LENGTH(PolyLength));
208 /* check polynomial definition vs polynomial size:
209 * polynomial length must be aligned with polynomial
210 * definition. HAL_ERROR is reported if Pol degree is
211 * larger than that indicated by PolyLength.
212 * Look for MSB position: msb will contain the degree of
213 * the second to the largest polynomial member. E.g., for
214 * X^7 + X^6 + X^5 + X^2 + 1, msb = 6. */
215 while (((Pol & (1U << msb)) == 0) && (msb-- > 0))
220 case CRC_POLYLENGTH_7B:
221 if (msb >= HAL_CRC_LENGTH_7B)
226 case CRC_POLYLENGTH_8B:
227 if (msb >= HAL_CRC_LENGTH_8B)
232 case CRC_POLYLENGTH_16B:
233 if (msb >= HAL_CRC_LENGTH_16B)
238 case CRC_POLYLENGTH_32B:
239 /* no polynomial definition vs. polynomial length issue possible */
245 /* set generating polynomial */
246 WRITE_REG(hcrc->Instance->POL, Pol);
248 /* set generating polynomial size */
249 MODIFY_REG(hcrc->Instance->CR, CRC_CR_POLYSIZE, PolyLength);
251 /* Return function status */
254 #endif /* #if defined (STM32F071xB) || defined (STM32F072xB) || defined (STM32F078xx) || defined (STM32F091xC) || defined (STM32F098xx) */
266 #endif /* HAL_CRC_MODULE_ENABLED */
275 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/