提交 | 用户 | age
|
bfc108
|
1 |
/**
|
Q |
2 |
******************************************************************************
|
|
3 |
* @file stm32f0xx_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 following
|
|
7 |
* functionalities of the CRC peripheral:
|
|
8 |
* + Extended initialization functions
|
|
9 |
*
|
|
10 |
@verbatim
|
|
11 |
================================================================================
|
|
12 |
##### How to use this driver #####
|
|
13 |
================================================================================
|
|
14 |
[..]
|
|
15 |
(+) Extended initialization
|
|
16 |
(+) Set or not user-defined generating
|
|
17 |
polynomial other than default one
|
|
18 |
|
|
19 |
@endverbatim
|
|
20 |
******************************************************************************
|
|
21 |
* @attention
|
|
22 |
*
|
|
23 |
* <h2><center>© COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
|
|
24 |
*
|
|
25 |
* Redistribution and use in source and binary forms, with or without modification,
|
|
26 |
* are permitted provided that the following conditions are met:
|
|
27 |
* 1. Redistributions of source code must retain the above copyright notice,
|
|
28 |
* this list of conditions and the following disclaimer.
|
|
29 |
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
30 |
* this list of conditions and the following disclaimer in the documentation
|
|
31 |
* and/or other materials provided with the distribution.
|
|
32 |
* 3. Neither the name of STMicroelectronics nor the names of its contributors
|
|
33 |
* may be used to endorse or promote products derived from this software
|
|
34 |
* without specific prior written permission.
|
|
35 |
*
|
|
36 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
37 |
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
38 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
39 |
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
40 |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
41 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
42 |
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
43 |
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
44 |
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
45 |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
46 |
*
|
|
47 |
******************************************************************************
|
|
48 |
*/
|
|
49 |
|
|
50 |
/* Includes ------------------------------------------------------------------*/
|
|
51 |
#include "stm32f0xx_hal.h"
|
|
52 |
|
|
53 |
/** @addtogroup STM32F0xx_HAL_Driver
|
|
54 |
* @{
|
|
55 |
*/
|
|
56 |
|
|
57 |
/** @defgroup CRCEx CRCEx
|
|
58 |
* @brief CRC Extended HAL module driver
|
|
59 |
* @{
|
|
60 |
*/
|
|
61 |
|
|
62 |
#ifdef HAL_CRC_MODULE_ENABLED
|
|
63 |
|
|
64 |
/* Private typedef -----------------------------------------------------------*/
|
|
65 |
/* Private define ------------------------------------------------------------*/
|
|
66 |
/* Private macro -------------------------------------------------------------*/
|
|
67 |
/* Private variables ---------------------------------------------------------*/
|
|
68 |
/* Private function prototypes -----------------------------------------------*/
|
|
69 |
/* Private functions ---------------------------------------------------------*/
|
|
70 |
|
|
71 |
/** @defgroup CRCEx_Exported_Functions CRCEx Exported Functions
|
|
72 |
* @{
|
|
73 |
*/
|
|
74 |
|
|
75 |
/** @defgroup CRCEx_Exported_Functions_Group1 Extended Initialization/de-initialization functions
|
|
76 |
* @brief Extended Initialization and Configuration functions.
|
|
77 |
*
|
|
78 |
@verbatim
|
|
79 |
===============================================================================
|
|
80 |
##### Initialization and Configuration functions #####
|
|
81 |
===============================================================================
|
|
82 |
[..] This section provides functions allowing to:
|
|
83 |
(+) Initialize the CRC generating polynomial: if programmable polynomial
|
|
84 |
feature is applicable to device, set default or non-default generating
|
|
85 |
polynomial according to hcrc->Init.DefaultPolynomialUse parameter.
|
|
86 |
If feature is non-applicable to device in use, HAL_CRCEx_Init straight
|
|
87 |
away reports HAL_OK.
|
|
88 |
(+) Set the generating polynomial
|
|
89 |
|
|
90 |
@endverbatim
|
|
91 |
* @{
|
|
92 |
*/
|
|
93 |
|
|
94 |
|
|
95 |
/**
|
|
96 |
* @brief Extended initialization to set generating polynomial
|
|
97 |
* @param hcrc CRC handle
|
|
98 |
* @retval HAL status
|
|
99 |
*/
|
|
100 |
HAL_StatusTypeDef HAL_CRCEx_Init(CRC_HandleTypeDef *hcrc)
|
|
101 |
{
|
|
102 |
#if defined(STM32F071xB) || defined(STM32F072xB) || defined(STM32F078xx) || defined(STM32F091xC) || defined (STM32F098xx)
|
|
103 |
/* check whether or not non-default generating polynomial has been
|
|
104 |
* picked up by user */
|
|
105 |
assert_param(IS_DEFAULT_POLYNOMIAL(hcrc->Init.DefaultPolynomialUse));
|
|
106 |
if (hcrc->Init.DefaultPolynomialUse == DEFAULT_POLYNOMIAL_ENABLE)
|
|
107 |
{
|
|
108 |
/* initialize IP with default generating polynomial */
|
|
109 |
WRITE_REG(hcrc->Instance->POL, DEFAULT_CRC32_POLY);
|
|
110 |
MODIFY_REG(hcrc->Instance->CR, CRC_CR_POLYSIZE, CRC_POLYLENGTH_32B);
|
|
111 |
}
|
|
112 |
else
|
|
113 |
{
|
|
114 |
/* initialize CRC IP with generating polynomial defined by user */
|
|
115 |
if (HAL_CRCEx_Polynomial_Set(hcrc, hcrc->Init.GeneratingPolynomial, hcrc->Init.CRCLength) != HAL_OK)
|
|
116 |
{
|
|
117 |
return HAL_ERROR;
|
|
118 |
}
|
|
119 |
}
|
|
120 |
#endif /* defined(STM32F071xB) || defined(STM32F072xB) || defined(STM32F078xx) || defined(STM32F091xC) || defined (STM32F098xx) */
|
|
121 |
|
|
122 |
return HAL_OK;
|
|
123 |
}
|
|
124 |
|
|
125 |
/**
|
|
126 |
* @brief Set the Reverse Input data mode.
|
|
127 |
* @param hcrc CRC handle
|
|
128 |
* @param InputReverseMode Input Data inversion mode
|
|
129 |
* This parameter can be one of the following values:
|
|
130 |
* @arg CRC_INPUTDATA_NOINVERSION: no change in bit order (default value)
|
|
131 |
* @arg CRC_INPUTDATA_INVERSION_BYTE: Byte-wise bit reversal
|
|
132 |
* @arg CRC_INPUTDATA_INVERSION_HALFWORD: HalfWord-wise bit reversal
|
|
133 |
* @arg CRC_INPUTDATA_INVERSION_WORD: Word-wise bit reversal
|
|
134 |
* @retval HAL status
|
|
135 |
*/
|
|
136 |
HAL_StatusTypeDef HAL_CRCEx_Input_Data_Reverse(CRC_HandleTypeDef *hcrc, uint32_t InputReverseMode)
|
|
137 |
{
|
|
138 |
/* Check the parameters */
|
|
139 |
assert_param(IS_CRC_INPUTDATA_INVERSION_MODE(InputReverseMode));
|
|
140 |
|
|
141 |
/* Change CRC peripheral state */
|
|
142 |
hcrc->State = HAL_CRC_STATE_BUSY;
|
|
143 |
|
|
144 |
/* set input data inversion mode */
|
|
145 |
MODIFY_REG(hcrc->Instance->CR, CRC_CR_REV_IN, InputReverseMode);
|
|
146 |
/* Change CRC peripheral state */
|
|
147 |
hcrc->State = HAL_CRC_STATE_READY;
|
|
148 |
|
|
149 |
/* Return function status */
|
|
150 |
return HAL_OK;
|
|
151 |
}
|
|
152 |
|
|
153 |
/**
|
|
154 |
* @brief Set the Reverse Output data mode.
|
|
155 |
* @param hcrc CRC handle
|
|
156 |
* @param OutputReverseMode Output Data inversion mode
|
|
157 |
* This parameter can be one of the following values:
|
|
158 |
* @arg CRC_OUTPUTDATA_INVERSION_DISABLE: no CRC inversion (default value)
|
|
159 |
* @arg CRC_OUTPUTDATA_INVERSION_ENABLE: bit-level inversion (e.g for a 8-bit CRC: 0xB5 becomes 0xAD)
|
|
160 |
* @retval HAL status
|
|
161 |
*/
|
|
162 |
HAL_StatusTypeDef HAL_CRCEx_Output_Data_Reverse(CRC_HandleTypeDef *hcrc, uint32_t OutputReverseMode)
|
|
163 |
{
|
|
164 |
/* Check the parameters */
|
|
165 |
assert_param(IS_CRC_OUTPUTDATA_INVERSION_MODE(OutputReverseMode));
|
|
166 |
|
|
167 |
/* Change CRC peripheral state */
|
|
168 |
hcrc->State = HAL_CRC_STATE_BUSY;
|
|
169 |
|
|
170 |
/* set output data inversion mode */
|
|
171 |
MODIFY_REG(hcrc->Instance->CR, CRC_CR_REV_OUT, OutputReverseMode);
|
|
172 |
|
|
173 |
/* Change CRC peripheral state */
|
|
174 |
hcrc->State = HAL_CRC_STATE_READY;
|
|
175 |
|
|
176 |
/* Return function status */
|
|
177 |
return HAL_OK;
|
|
178 |
}
|
|
179 |
|
|
180 |
#if defined (STM32F071xB) || defined (STM32F072xB) || defined (STM32F078xx) || defined (STM32F091xC) || defined (STM32F098xx)
|
|
181 |
/**
|
|
182 |
* @brief Initializes the CRC polynomial if different from default one.
|
|
183 |
* @param hcrc CRC handle
|
|
184 |
* @param Pol CRC generating polynomial (7, 8, 16 or 32-bit long)
|
|
185 |
* This parameter is written in normal representation, e.g.
|
|
186 |
* for a polynomial of degree 7, X^7 + X^6 + X^5 + X^2 + 1 is written 0x65
|
|
187 |
* for a polynomial of degree 16, X^16 + X^12 + X^5 + 1 is written 0x1021
|
|
188 |
* @param PolyLength CRC polynomial length
|
|
189 |
* This parameter can be one of the following values:
|
|
190 |
* @arg CRC_POLYLENGTH_7B: 7-bit long CRC (generating polynomial of degree 7)
|
|
191 |
* @arg CRC_POLYLENGTH_8B: 8-bit long CRC (generating polynomial of degree 8)
|
|
192 |
* @arg CRC_POLYLENGTH_16B: 16-bit long CRC (generating polynomial of degree 16)
|
|
193 |
* @arg CRC_POLYLENGTH_32B: 32-bit long CRC (generating polynomial of degree 32)
|
|
194 |
* @retval HAL status
|
|
195 |
*/
|
|
196 |
HAL_StatusTypeDef HAL_CRCEx_Polynomial_Set(CRC_HandleTypeDef *hcrc, uint32_t Pol, uint32_t PolyLength)
|
|
197 |
{
|
|
198 |
uint32_t msb = 31U; /* polynomial degree is 32 at most, so msb is initialized to max value */
|
|
199 |
|
|
200 |
/* Check the parameters */
|
|
201 |
assert_param(IS_CRC_POL_LENGTH(PolyLength));
|
|
202 |
|
|
203 |
/* check polynomial definition vs polynomial size:
|
|
204 |
* polynomial length must be aligned with polynomial
|
|
205 |
* definition. HAL_ERROR is reported if Pol degree is
|
|
206 |
* larger than that indicated by PolyLength.
|
|
207 |
* Look for MSB position: msb will contain the degree of
|
|
208 |
* the second to the largest polynomial member. E.g., for
|
|
209 |
* X^7 + X^6 + X^5 + X^2 + 1, msb = 6. */
|
|
210 |
while (((Pol & (1U << msb)) == 0U) && (msb-- > 0U))
|
|
211 |
{}
|
|
212 |
|
|
213 |
switch (PolyLength)
|
|
214 |
{
|
|
215 |
case CRC_POLYLENGTH_7B:
|
|
216 |
if (msb >= HAL_CRC_LENGTH_7B)
|
|
217 |
{
|
|
218 |
return HAL_ERROR;
|
|
219 |
}
|
|
220 |
break;
|
|
221 |
case CRC_POLYLENGTH_8B:
|
|
222 |
if (msb >= HAL_CRC_LENGTH_8B)
|
|
223 |
{
|
|
224 |
return HAL_ERROR;
|
|
225 |
}
|
|
226 |
break;
|
|
227 |
case CRC_POLYLENGTH_16B:
|
|
228 |
if (msb >= HAL_CRC_LENGTH_16B)
|
|
229 |
{
|
|
230 |
return HAL_ERROR;
|
|
231 |
}
|
|
232 |
break;
|
|
233 |
case CRC_POLYLENGTH_32B:
|
|
234 |
/* no polynomial definition vs. polynomial length issue possible */
|
|
235 |
break;
|
|
236 |
default:
|
|
237 |
break;
|
|
238 |
}
|
|
239 |
|
|
240 |
/* set generating polynomial */
|
|
241 |
WRITE_REG(hcrc->Instance->POL, Pol);
|
|
242 |
|
|
243 |
/* set generating polynomial size */
|
|
244 |
MODIFY_REG(hcrc->Instance->CR, CRC_CR_POLYSIZE, PolyLength);
|
|
245 |
|
|
246 |
/* Return function status */
|
|
247 |
return HAL_OK;
|
|
248 |
}
|
|
249 |
#endif /* #if defined (STM32F071xB) || defined (STM32F072xB) || defined (STM32F078xx) || defined (STM32F091xC) || defined (STM32F098xx) */
|
|
250 |
|
|
251 |
/**
|
|
252 |
* @}
|
|
253 |
*/
|
|
254 |
|
|
255 |
|
|
256 |
/**
|
|
257 |
* @}
|
|
258 |
*/
|
|
259 |
|
|
260 |
|
|
261 |
#endif /* HAL_CRC_MODULE_ENABLED */
|
|
262 |
/**
|
|
263 |
* @}
|
|
264 |
*/
|
|
265 |
|
|
266 |
/**
|
|
267 |
* @}
|
|
268 |
*/
|
|
269 |
|
|
270 |
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
|