2 ******************************************************************************
3 * @file stm32f3xx_hal_crc_ex.c
4 * @author MCD Application Team
7 * @brief Extended CRC HAL module driver.
9 * This file provides firmware functions to manage the following
10 * functionalities of the CRC peripheral:
11 * + Initialization/de-initialization functions
12 * + I/O operation functions
13 * + Peripheral Control functions
14 * + Peripheral State functions
17 ================================================================================
18 ##### <Product specific features/integration> #####
19 ================================================================================
21 [..] < This section can contain:
22 (#) Description of the product specific implementation; all features
23 that is specific to this IP: separate clock for RTC/LCD/IWDG/ADC,
24 power domain (backup domain for the RTC)...
25 (#) IP main features, only when needed and not mandatory for all IPs,
26 ex. for xWDG, GPIO, COMP...
29 [..] < You can add as much sections as needed.>
31 [..] < You can add as much sections as needed.>
34 ##### How to use this driver #####
35 ================================================================================
37 (+) Enable CRC AHB clock using __CRC_CLK_ENABLE();
38 (+) Initialize CRC calculator
39 - specify generating polynomial (IP default or non-default one)
40 - specify initialization value (IP default or non-default one)
41 - specify input data format
42 - specify input or output data inversion mode if any
43 (+) Use HAL_CRC_Accumulate() function to compute the CRC value of the
44 input data buffer starting with the previously computed CRC as
46 (+) Use HAL_CRC_Calculate() function to compute the CRC value of the
47 input data buffer starting with the defined initialization value
48 (default or non-default) to initiate CRC calculation
51 ******************************************************************************
54 * <h2><center>© COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
56 * Redistribution and use in source and binary forms, with or without modification,
57 * are permitted provided that the following conditions are met:
58 * 1. Redistributions of source code must retain the above copyright notice,
59 * this list of conditions and the following disclaimer.
60 * 2. Redistributions in binary form must reproduce the above copyright notice,
61 * this list of conditions and the following disclaimer in the documentation
62 * and/or other materials provided with the distribution.
63 * 3. Neither the name of STMicroelectronics nor the names of its contributors
64 * may be used to endorse or promote products derived from this software
65 * without specific prior written permission.
67 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
68 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
69 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
70 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
71 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
72 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
73 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
74 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
75 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
76 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
78 ******************************************************************************
81 /* Includes ------------------------------------------------------------------*/
82 #include "stm32f3xx_hal.h"
84 /** @addtogroup STM32F3xx_HAL_Driver
88 /** @defgroup CRCEx CRC Extended HAL module driver
89 * @brief CRC Extended HAL module driver
93 #ifdef HAL_CRC_MODULE_ENABLED
95 /* Private typedef -----------------------------------------------------------*/
96 /* Private define ------------------------------------------------------------*/
97 /* Private macro -------------------------------------------------------------*/
98 /* Private variables ---------------------------------------------------------*/
99 /* Private function prototypes -----------------------------------------------*/
100 /* Exported functions --------------------------------------------------------*/
102 /** @defgroup CRCEx_Exported_Functions CRC Extended Exported Functions
106 /** @defgroup CRCEx_Exported_Functions_Group1 Extended Initialization and de-initialization functions
107 * @brief Extended Initialization and Configuration functions.
110 ===============================================================================
111 ##### Initialization/de-initialization functions #####
112 ===============================================================================
113 [..] This section provides functions allowing to:
114 (+) Initialize the CRC according to the specified parameters
115 in the CRC_InitTypeDef and create the associated handle
116 (+) DeInitialize the CRC peripheral
117 (+) Initialize the CRC MSP
118 (+) DeInitialize CRC MSP
126 * @brief Initializes the CRC polynomial if different from default one.
127 * @param hcrc: CRC handle
128 * @param Pol: CRC generating polynomial (7, 8, 16 or 32-bit long)
129 * This parameter is written in normal representation, e.g.
130 * for a polynomial of degree 7, X^7 + X^6 + X^5 + X^2 + 1 is written 0x65
131 * for a polynomial of degree 16, X^16 + X^12 + X^5 + 1 is written 0x1021
132 * @param PolyLength: CRC polynomial length
133 * This parameter can be one of the following values:
134 * @arg CRC_POLYLENGTH_7B: 7-bit long CRC (generating polynomial of degree 7)
135 * @arg CRC_POLYLENGTH_8B: 8-bit long CRC (generating polynomial of degree 8)
136 * @arg CRC_POLYLENGTH_16B: 16-bit long CRC (generating polynomial of degree 16)
137 * @arg CRC_POLYLENGTH_32B: 32-bit long CRC (generating polynomial of degree 32)
140 HAL_StatusTypeDef HAL_CRCEx_Polynomial_Set(CRC_HandleTypeDef *hcrc, uint32_t Pol, uint32_t PolyLength)
142 uint32_t msb = 31; /* polynomial degree is 32 at most, so msb is initialized to max value */
144 /* Check the parameters */
145 assert_param(IS_CRC_POL_LENGTH(PolyLength));
147 /* check polynomial definition vs polynomial size:
148 * polynomial length must be aligned with polynomial
149 * definition. HAL_ERROR is reported if Pol degree is
150 * larger than that indicated by PolyLength.
151 * Look for MSB position: msb will contain the degree of
152 * the second to the largest polynomial member. E.g., for
153 * X^7 + X^6 + X^5 + X^2 + 1, msb = 6. */
154 while (((Pol & (0x1 << msb)) == 0) && (msb-- > 0));
158 case CRC_POLYLENGTH_7B:
159 if (msb >= HAL_CRC_LENGTH_7B) return HAL_ERROR;
161 case CRC_POLYLENGTH_8B:
162 if (msb >= HAL_CRC_LENGTH_8B) return HAL_ERROR;
164 case CRC_POLYLENGTH_16B:
165 if (msb >= HAL_CRC_LENGTH_16B) return HAL_ERROR;
167 case CRC_POLYLENGTH_32B:
168 /* no polynomial definition vs. polynomial length issue possible */
172 /* set generating polynomial */
173 WRITE_REG(hcrc->Instance->POL, Pol);
175 /* set generating polynomial size */
176 MODIFY_REG(hcrc->Instance->CR, CRC_CR_POLYSIZE, PolyLength);
178 /* Return function status */
183 * @brief Set the Reverse Input data mode.
184 * @param hcrc: CRC handle
185 * @param InputReverseMode: Input Data inversion mode
186 * This parameter can be one of the following values:
187 * @arg CRC_INPUTDATA_NOINVERSION: no change in bit order (default value)
188 * @arg CRC_INPUTDATA_INVERSION_BYTE: Byte-wise bit reversal
189 * @arg CRC_INPUTDATA_INVERSION_HALFWORD: HalfWord-wise bit reversal
190 * @arg CRC_INPUTDATA_INVERSION_WORD: Word-wise bit reversal
193 HAL_StatusTypeDef HAL_CRCEx_Input_Data_Reverse(CRC_HandleTypeDef *hcrc, uint32_t InputReverseMode)
195 /* Check the parameters */
196 assert_param(IS_CRC_INPUTDATA_INVERSION_MODE(InputReverseMode));
198 /* Change CRC peripheral state */
199 hcrc->State = HAL_CRC_STATE_BUSY;
201 /* set input data inversion mode */
202 MODIFY_REG(hcrc->Instance->CR, CRC_CR_REV_IN, InputReverseMode);
203 /* Change CRC peripheral state */
204 hcrc->State = HAL_CRC_STATE_READY;
206 /* Return function status */
211 * @brief Set the Reverse Output data mode.
212 * @param hcrc: CRC handle
213 * @param OutputReverseMode: Output Data inversion mode
214 * This parameter can be one of the following values:
215 * @arg CRC_OUTPUTDATA_INVERSION_DISABLED: no CRC inversion (default value)
216 * @arg CRC_OUTPUTDATA_INVERSION_ENABLED: bit-level inversion (e.g for a 8-bit CRC: 0xB5 becomes 0xAD)
219 HAL_StatusTypeDef HAL_CRCEx_Output_Data_Reverse(CRC_HandleTypeDef *hcrc, uint32_t OutputReverseMode)
221 /* Check the parameters */
222 assert_param(IS_CRC_OUTPUTDATA_INVERSION_MODE(OutputReverseMode));
224 /* Change CRC peripheral state */
225 hcrc->State = HAL_CRC_STATE_BUSY;
227 /* set output data inversion mode */
228 MODIFY_REG(hcrc->Instance->CR, CRC_CR_REV_OUT, OutputReverseMode);
230 /* Change CRC peripheral state */
231 hcrc->State = HAL_CRC_STATE_READY;
233 /* Return function status */
250 #endif /* HAL_CRC_MODULE_ENABLED */
259 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/