2 ******************************************************************************
3 * @file stm32l0xx_hal_crc_ex.c
4 * @author MCD Application Team
6 * @date 06-February-2015
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
14 ==============================================================================
15 ##### CRC specific features #####
16 ==============================================================================
18 (#) Polynomial configuration.
19 (#) Input data reverse mode.
20 (#) Output data reverse mode.
23 ******************************************************************************
26 * <h2><center>© COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
28 * Redistribution and use in source and binary forms, with or without modification,
29 * are permitted provided that the following conditions are met:
30 * 1. Redistributions of source code must retain the above copyright notice,
31 * this list of conditions and the following disclaimer.
32 * 2. Redistributions in binary form must reproduce the above copyright notice,
33 * this list of conditions and the following disclaimer in the documentation
34 * and/or other materials provided with the distribution.
35 * 3. Neither the name of STMicroelectronics nor the names of its contributors
36 * may be used to endorse or promote products derived from this software
37 * without specific prior written permission.
39 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
40 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
42 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
43 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
44 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
45 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
46 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
47 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
48 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
50 ******************************************************************************
53 /* Includes ------------------------------------------------------------------*/
54 #include "stm32l0xx_hal.h"
56 /** @addtogroup STM32L0xx_HAL_Driver
61 * @brief CRC Extended HAL module driver
65 #ifdef HAL_CRC_MODULE_ENABLED
67 /* Private typedef -----------------------------------------------------------*/
68 /* Private define ------------------------------------------------------------*/
69 /* Private macro -------------------------------------------------------------*/
70 /* Private variables ---------------------------------------------------------*/
71 /* Private function prototypes -----------------------------------------------*/
72 /* Private functions ---------------------------------------------------------*/
74 /** @addtogroup CRCEx_Exported_Functions
78 /** @addtogroup CRCEx_Exported_Functions_Group1
79 * @brief Extended CRC features functions
82 ===============================================================================
83 ##### CRC Extended features functions #####
84 ===============================================================================
86 This subsection provides function allowing to:
87 (+) Set CRC polynomial if different from default one.
95 * @brief Initializes the CRC polynomial if different from default one.
96 * @param hcrc: CRC handle
97 * @param Pol: CRC generating polynomial (7, 8, 16 or 32-bit long)
98 * This parameter is written in normal representation, e.g.
99 * for a polynomial of degree 7, X^7 + X^6 + X^5 + X^2 + 1 is written 0x65
100 * for a polynomial of degree 16, X^16 + X^12 + X^5 + 1 is written 0x1021
101 * @param PolyLength: CRC polynomial length
102 * This parameter can be one of the following values:
103 * @arg CRC_POLYLENGTH_7B: 7-bit long CRC (generating polynomial of degree 7)
104 * @arg CRC_POLYLENGTH_8B: 8-bit long CRC (generating polynomial of degree 8)
105 * @arg CRC_POLYLENGTH_16B: 16-bit long CRC (generating polynomial of degree 16)
106 * @arg CRC_POLYLENGTH_32B: 32-bit long CRC (generating polynomial of degree 32)
109 HAL_StatusTypeDef HAL_CRCEx_Polynomial_Set(CRC_HandleTypeDef *hcrc, uint32_t Pol, uint32_t PolyLength)
111 uint32_t msb = 31; /* polynomial degree is 32 at most, so msb is initialized to max value */
113 /* Check the parameters */
114 assert_param(IS_CRC_POL_LENGTH(PolyLength));
116 /* check polynomial definition vs polynomial size:
117 * polynomial length must be aligned with polynomial
118 * definition. HAL_ERROR is reported if Pol degree is
119 * larger than that indicated by PolyLength.
120 * Look for MSB position: msb will contain the degree of
121 * the second to the largest polynomial member. E.g., for
122 * X^7 + X^6 + X^5 + X^2 + 1, msb = 6. */
123 while (((Pol & ((uint32_t)(0x1) << msb)) == 0) && (msb-- > 0));
127 case CRC_POLYLENGTH_7B:
128 if (msb >= HAL_CRC_LENGTH_7B) return HAL_ERROR;
130 case CRC_POLYLENGTH_8B:
131 if (msb >= HAL_CRC_LENGTH_8B) return HAL_ERROR;
133 case CRC_POLYLENGTH_16B:
134 if (msb >= HAL_CRC_LENGTH_16B) return HAL_ERROR;
136 case CRC_POLYLENGTH_32B:
137 /* no polynomial definition vs. polynomial length issue possible */
143 /* set generating polynomial */
144 WRITE_REG(hcrc->Instance->POL, Pol);
146 /* set generating polynomial size */
147 MODIFY_REG(hcrc->Instance->CR, CRC_CR_POLYSIZE, PolyLength);
149 /* Return function status */
154 * @brief Set the Reverse Input data mode.
155 * @param hcrc: CRC handle
156 * @param InputReverseMode: Input Data inversion mode
157 * This parameter can be one of the following values:
158 * @arg CRC_INPUTDATA_INVERSION_NONE: no change in bit order (default value)
159 * @arg CRC_INPUTDATA_INVERSION_BYTE: Byte-wise bit reversal
160 * @arg CRC_INPUTDATA_INVERSION_HALFWORD: HalfWord-wise bit reversal
161 * @arg CRC_INPUTDATA_INVERSION_WORD: Word-wise bit reversal
164 HAL_StatusTypeDef HAL_CRCEx_Input_Data_Reverse(CRC_HandleTypeDef *hcrc, uint32_t InputReverseMode)
166 /* Check the parameters */
167 assert_param(IS_CRC_INPUTDATA_INVERSION_MODE(InputReverseMode));
169 /* Change CRC peripheral state */
170 hcrc->State = HAL_CRC_STATE_BUSY;
172 /* set input data inversion mode */
173 MODIFY_REG(hcrc->Instance->CR, CRC_CR_REV_IN, InputReverseMode);
174 /* Change CRC peripheral state */
175 hcrc->State = HAL_CRC_STATE_READY;
177 /* Return function status */
182 * @brief Set the Reverse Output data mode.
183 * @param hcrc: CRC handle
184 * @param OutputReverseMode: Output Data inversion mode
185 * This parameter can be one of the following values:
186 * @arg CRC_OUTPUTDATA_INVERSION_DISABLE: no CRC inversion (default value)
187 * @arg CRC_OUTPUTDATA_INVERSION_ENABLE: bit-level inversion (e.g for a 8-bit CRC: 0xB5 becomes 0xAD)
190 HAL_StatusTypeDef HAL_CRCEx_Output_Data_Reverse(CRC_HandleTypeDef *hcrc, uint32_t OutputReverseMode)
192 /* Check the parameters */
193 assert_param(IS_CRC_OUTPUTDATA_INVERSION_MODE(OutputReverseMode));
195 /* Change CRC peripheral state */
196 hcrc->State = HAL_CRC_STATE_BUSY;
198 /* set output data inversion mode */
199 MODIFY_REG(hcrc->Instance->CR, CRC_CR_REV_OUT, OutputReverseMode);
201 /* Change CRC peripheral state */
202 hcrc->State = HAL_CRC_STATE_READY;
204 /* Return function status */
221 #endif /* HAL_CRC_MODULE_ENABLED */
230 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/