1 /**
2 ******************************************************************************
3 * @file stm32l4xx_hal_crc_ex.c
4 * @author MCD Application Team
5 * @brief Extended CRC HAL module driver.
6 * This file provides firmware functions to manage the extended
7 * functionalities of the CRC peripheral.
8 *
9 ******************************************************************************
10 * @attention
11 *
12 * Copyright (c) 2017 STMicroelectronics.
13 * All rights reserved.
14 *
15 * This software is licensed under terms that can be found in the LICENSE file
16 * in the root directory of this software component.
17 * If no LICENSE file comes with this software, it is provided AS-IS.
18 *
19 ******************************************************************************
20 @verbatim
21 ================================================================================
22 ##### How to use this driver #####
23 ================================================================================
24 [..]
25 (+) Set user-defined generating polynomial through HAL_CRCEx_Polynomial_Set()
26 (+) Configure Input or Output data inversion
27
28 @endverbatim
29 ******************************************************************************
30 */
31
32 /* Includes ------------------------------------------------------------------*/
33 #include "stm32l4xx_hal.h"
34
35 /** @addtogroup STM32L4xx_HAL_Driver
36 * @{
37 */
38
39 /** @defgroup CRCEx CRCEx
40 * @brief CRC Extended HAL module driver
41 * @{
42 */
43
44 #ifdef HAL_CRC_MODULE_ENABLED
45
46 /* Private typedef -----------------------------------------------------------*/
47 /* Private define ------------------------------------------------------------*/
48 /* Private macro -------------------------------------------------------------*/
49 /* Private variables ---------------------------------------------------------*/
50 /* Private function prototypes -----------------------------------------------*/
51 /* Exported functions --------------------------------------------------------*/
52
53 /** @defgroup CRCEx_Exported_Functions CRC Extended Exported Functions
54 * @{
55 */
56
57 /** @defgroup CRCEx_Exported_Functions_Group1 Extended Initialization/de-initialization functions
58 * @brief Extended Initialization and Configuration functions.
59 *
60 @verbatim
61 ===============================================================================
62 ##### Extended configuration functions #####
63 ===============================================================================
64 [..] This section provides functions allowing to:
65 (+) Configure the generating polynomial
66 (+) Configure the input data inversion
67 (+) Configure the output data inversion
68
69 @endverbatim
70 * @{
71 */
72
73
74 /**
75 * @brief Initialize the CRC polynomial if different from default one.
76 * @param hcrc CRC handle
77 * @param Pol CRC generating polynomial (7, 8, 16 or 32-bit long).
78 * This parameter is written in normal representation, e.g.
79 * @arg for a polynomial of degree 7, X^7 + X^6 + X^5 + X^2 + 1 is written 0x65
80 * @arg for a polynomial of degree 16, X^16 + X^12 + X^5 + 1 is written 0x1021
81 * @param PolyLength CRC polynomial length.
82 * This parameter can be one of the following values:
83 * @arg @ref CRC_POLYLENGTH_7B 7-bit long CRC (generating polynomial of degree 7)
84 * @arg @ref CRC_POLYLENGTH_8B 8-bit long CRC (generating polynomial of degree 8)
85 * @arg @ref CRC_POLYLENGTH_16B 16-bit long CRC (generating polynomial of degree 16)
86 * @arg @ref CRC_POLYLENGTH_32B 32-bit long CRC (generating polynomial of degree 32)
87 * @retval HAL status
88 */
HAL_CRCEx_Polynomial_Set(CRC_HandleTypeDef * hcrc,uint32_t Pol,uint32_t PolyLength)89 HAL_StatusTypeDef HAL_CRCEx_Polynomial_Set(CRC_HandleTypeDef *hcrc, uint32_t Pol, uint32_t PolyLength)
90 {
91 HAL_StatusTypeDef status = HAL_OK;
92 uint32_t msb = 31U; /* polynomial degree is 32 at most, so msb is initialized to max value */
93
94 /* Check the parameters */
95 assert_param(IS_CRC_POL_LENGTH(PolyLength));
96
97 /* check polynomial definition vs polynomial size:
98 * polynomial length must be aligned with polynomial
99 * definition. HAL_ERROR is reported if Pol degree is
100 * larger than that indicated by PolyLength.
101 * Look for MSB position: msb will contain the degree of
102 * the second to the largest polynomial member. E.g., for
103 * X^7 + X^6 + X^5 + X^2 + 1, msb = 6. */
104 while ((msb-- > 0U) && ((Pol & ((uint32_t)(0x1U) << (msb & 0x1FU))) == 0U))
105 {
106 }
107
108 switch (PolyLength)
109 {
110 case CRC_POLYLENGTH_7B:
111 if (msb >= HAL_CRC_LENGTH_7B)
112 {
113 status = HAL_ERROR;
114 }
115 break;
116 case CRC_POLYLENGTH_8B:
117 if (msb >= HAL_CRC_LENGTH_8B)
118 {
119 status = HAL_ERROR;
120 }
121 break;
122 case CRC_POLYLENGTH_16B:
123 if (msb >= HAL_CRC_LENGTH_16B)
124 {
125 status = HAL_ERROR;
126 }
127 break;
128
129 case CRC_POLYLENGTH_32B:
130 /* no polynomial definition vs. polynomial length issue possible */
131 break;
132 default:
133 status = HAL_ERROR;
134 break;
135 }
136 if (status == HAL_OK)
137 {
138 /* set generating polynomial */
139 WRITE_REG(hcrc->Instance->POL, Pol);
140
141 /* set generating polynomial size */
142 MODIFY_REG(hcrc->Instance->CR, CRC_CR_POLYSIZE, PolyLength);
143 }
144 /* Return function status */
145 return status;
146 }
147
148 /**
149 * @brief Set the Reverse Input data mode.
150 * @param hcrc CRC handle
151 * @param InputReverseMode Input Data inversion mode.
152 * This parameter can be one of the following values:
153 * @arg @ref CRC_INPUTDATA_INVERSION_NONE no change in bit order (default value)
154 * @arg @ref CRC_INPUTDATA_INVERSION_BYTE Byte-wise bit reversal
155 * @arg @ref CRC_INPUTDATA_INVERSION_HALFWORD HalfWord-wise bit reversal
156 * @arg @ref CRC_INPUTDATA_INVERSION_WORD Word-wise bit reversal
157 * @retval HAL status
158 */
HAL_CRCEx_Input_Data_Reverse(CRC_HandleTypeDef * hcrc,uint32_t InputReverseMode)159 HAL_StatusTypeDef HAL_CRCEx_Input_Data_Reverse(CRC_HandleTypeDef *hcrc, uint32_t InputReverseMode)
160 {
161 /* Check the parameters */
162 assert_param(IS_CRC_INPUTDATA_INVERSION_MODE(InputReverseMode));
163
164 /* Change CRC peripheral state */
165 hcrc->State = HAL_CRC_STATE_BUSY;
166
167 /* set input data inversion mode */
168 MODIFY_REG(hcrc->Instance->CR, CRC_CR_REV_IN, InputReverseMode);
169 /* Change CRC peripheral state */
170 hcrc->State = HAL_CRC_STATE_READY;
171
172 /* Return function status */
173 return HAL_OK;
174 }
175
176 /**
177 * @brief Set the Reverse Output data mode.
178 * @param hcrc CRC handle
179 * @param OutputReverseMode Output Data inversion mode.
180 * This parameter can be one of the following values:
181 * @arg @ref CRC_OUTPUTDATA_INVERSION_DISABLE no CRC inversion (default value)
182 * @arg @ref CRC_OUTPUTDATA_INVERSION_ENABLE bit-level inversion (e.g. for a 8-bit CRC: 0xB5 becomes 0xAD)
183 * @retval HAL status
184 */
HAL_CRCEx_Output_Data_Reverse(CRC_HandleTypeDef * hcrc,uint32_t OutputReverseMode)185 HAL_StatusTypeDef HAL_CRCEx_Output_Data_Reverse(CRC_HandleTypeDef *hcrc, uint32_t OutputReverseMode)
186 {
187 /* Check the parameters */
188 assert_param(IS_CRC_OUTPUTDATA_INVERSION_MODE(OutputReverseMode));
189
190 /* Change CRC peripheral state */
191 hcrc->State = HAL_CRC_STATE_BUSY;
192
193 /* set output data inversion mode */
194 MODIFY_REG(hcrc->Instance->CR, CRC_CR_REV_OUT, OutputReverseMode);
195
196 /* Change CRC peripheral state */
197 hcrc->State = HAL_CRC_STATE_READY;
198
199 /* Return function status */
200 return HAL_OK;
201 }
202
203
204
205
206 /**
207 * @}
208 */
209
210
211 /**
212 * @}
213 */
214
215
216 #endif /* HAL_CRC_MODULE_ENABLED */
217 /**
218 * @}
219 */
220
221 /**
222 * @}
223 */
224