]> git.gir.st - tmk_keyboard.git/blob - tool/mbed/mbed-sdk/libraries/mbed/targets/cmsis/TARGET_STM/TARGET_STM32F0/stm32f0xx_hal_crc.c
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[tmk_keyboard.git] / tool / mbed / mbed-sdk / libraries / mbed / targets / cmsis / TARGET_STM / TARGET_STM32F0 / stm32f0xx_hal_crc.c
1 /**
2 ******************************************************************************
3 * @file stm32f0xx_hal_crc.c
4 * @author MCD Application Team
5 * @version V1.2.0
6 * @date 11-December-2014
7 * @brief CRC HAL module driver.
8 * This file provides firmware functions to manage the following
9 * functionalities of the Cyclic Redundancy Check (CRC) peripheral:
10 * + Initialization and de-initialization functions
11 * + Peripheral Control functions
12 * + Peripheral State functions
13 *
14 @verbatim
15 ===============================================================================
16 ##### How to use this driver #####
17 ===============================================================================
18 [..]
19 (#) Enable CRC AHB clock using __CRC_CLK_ENABLE();
20 (#) Initialize CRC calculator
21 (++)specify generating polynomial (IP default or non-default one)
22 (++)specify initialization value (IP default or non-default one)
23 (++)specify input data format
24 (++)specify input or output data inversion mode if any
25 (#) Use HAL_CRC_Accumulate() function to compute the CRC value of the
26 input data buffer starting with the previously computed CRC as
27 initialization value
28 (#) Use HAL_CRC_Calculate() function to compute the CRC value of the
29 input data buffer starting with the defined initialization value
30 (default or non-default) to initiate CRC calculation
31
32 @endverbatim
33 ******************************************************************************
34 * @attention
35 *
36 * <h2><center>&copy; COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
37 *
38 * Redistribution and use in source and binary forms, with or without modification,
39 * are permitted provided that the following conditions are met:
40 * 1. Redistributions of source code must retain the above copyright notice,
41 * this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright notice,
43 * this list of conditions and the following disclaimer in the documentation
44 * and/or other materials provided with the distribution.
45 * 3. Neither the name of STMicroelectronics nor the names of its contributors
46 * may be used to endorse or promote products derived from this software
47 * without specific prior written permission.
48 *
49 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
50 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
52 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
55 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
56 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
57 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
58 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
59 *
60 ******************************************************************************
61 */
62
63 /* Includes ------------------------------------------------------------------*/
64 #include "stm32f0xx_hal.h"
65
66 /** @addtogroup STM32F0xx_HAL_Driver
67 * @{
68 */
69
70 /** @defgroup CRC CRC HAL module driver
71 * @brief CRC HAL module driver.
72 * @{
73 */
74
75 #ifdef HAL_CRC_MODULE_ENABLED
76
77 /* Private typedef -----------------------------------------------------------*/
78 /* Private define ------------------------------------------------------------*/
79 /* Private macro -------------------------------------------------------------*/
80 /* Private variables ---------------------------------------------------------*/
81 /* Private function prototypes -----------------------------------------------*/
82 /** @defgroup CRC_Private_Functions CRC Private Functions
83 * @{
84 */
85 static uint32_t CRC_Handle_8(CRC_HandleTypeDef *hcrc, uint8_t pBuffer[], uint32_t BufferLength);
86 static uint32_t CRC_Handle_16(CRC_HandleTypeDef *hcrc, uint16_t pBuffer[], uint32_t BufferLength);
87 /**
88 * @}
89 */
90
91 /* Exported functions ---------------------------------------------------------*/
92 /** @defgroup CRC_Exported_Functions CRC Exported Functions
93 * @{
94 */
95
96 /** @defgroup CRC_Exported_Functions_Group1 Initialization/de-initialization functions
97 * @brief Initialization and Configuration functions.
98 *
99 @verbatim
100 ===============================================================================
101 ##### Initialization and Configuration functions #####
102 ===============================================================================
103 [..] This section provides functions allowing to:
104 (+) Initialize the CRC according to the specified parameters
105 in the CRC_InitTypeDef and create the associated handle
106 (+) DeInitialize the CRC peripheral
107 (+) Initialize the CRC MSP
108 (+) DeInitialize CRC MSP
109
110 @endverbatim
111 * @{
112 */
113
114 /**
115 * @brief Initializes the CRC according to the specified
116 * parameters in the CRC_InitTypeDef and creates the associated handle.
117 * @param hcrc: CRC handle
118 * @retval HAL status
119 */
120 HAL_StatusTypeDef HAL_CRC_Init(CRC_HandleTypeDef *hcrc)
121 {
122 /* Check the CRC handle allocation */
123 if(hcrc == NULL)
124 {
125 return HAL_ERROR;
126 }
127
128 /* Check the parameters */
129 assert_param(IS_CRC_ALL_INSTANCE(hcrc->Instance));
130
131 if(hcrc->State == HAL_CRC_STATE_RESET)
132 {
133 /* Init the low level hardware */
134 HAL_CRC_MspInit(hcrc);
135 }
136
137 hcrc->State = HAL_CRC_STATE_BUSY;
138
139 /* Extended initialization: if programmable polynomial feature is
140 applicable to device, set default or non-default generating
141 polynomial according to hcrc->Init parameters.
142 If feature is non-applicable to device in use, HAL_CRCEx_Init straight
143 away reports HAL_OK. */
144 if (HAL_CRCEx_Init(hcrc) != HAL_OK)
145 {
146 return HAL_ERROR;
147 }
148
149 /* check whether or not non-default CRC initial value has been
150 * picked up by user */
151 assert_param(IS_DEFAULT_INIT_VALUE(hcrc->Init.DefaultInitValueUse));
152 if (hcrc->Init.DefaultInitValueUse == DEFAULT_INIT_VALUE_ENABLE)
153 {
154 WRITE_REG(hcrc->Instance->INIT, DEFAULT_CRC_INITVALUE);
155 }
156 else
157 {
158 WRITE_REG(hcrc->Instance->INIT, hcrc->Init.InitValue);
159 }
160
161
162 /* set input data inversion mode */
163 assert_param(IS_CRC_INPUTDATA_INVERSION_MODE(hcrc->Init.InputDataInversionMode));
164 MODIFY_REG(hcrc->Instance->CR, CRC_CR_REV_IN, hcrc->Init.InputDataInversionMode);
165
166 /* set output data inversion mode */
167 assert_param(IS_CRC_OUTPUTDATA_INVERSION_MODE(hcrc->Init.OutputDataInversionMode));
168 MODIFY_REG(hcrc->Instance->CR, CRC_CR_REV_OUT, hcrc->Init.OutputDataInversionMode);
169
170 /* makes sure the input data format (bytes, halfwords or words stream)
171 * is properly specified by user */
172 assert_param(IS_CRC_INPUTDATA_FORMAT(hcrc->InputDataFormat));
173
174 /* Change CRC peripheral state */
175 hcrc->State = HAL_CRC_STATE_READY;
176
177 /* Return function status */
178 return HAL_OK;
179 }
180
181 /**
182 * @brief DeInitializes the CRC peripheral.
183 * @param hcrc: CRC handle
184 * @retval HAL status
185 */
186 HAL_StatusTypeDef HAL_CRC_DeInit(CRC_HandleTypeDef *hcrc)
187 {
188 /* Check the CRC handle allocation */
189 if(hcrc == NULL)
190 {
191 return HAL_ERROR;
192 }
193
194 /* Check the parameters */
195 assert_param(IS_CRC_ALL_INSTANCE(hcrc->Instance));
196
197 /* Check the CRC peripheral state */
198 if(hcrc->State == HAL_CRC_STATE_BUSY)
199 {
200 return HAL_BUSY;
201 }
202
203 /* Change CRC peripheral state */
204 hcrc->State = HAL_CRC_STATE_BUSY;
205
206 /* DeInit the low level hardware */
207 HAL_CRC_MspDeInit(hcrc);
208
209 /* Change CRC peripheral state */
210 hcrc->State = HAL_CRC_STATE_RESET;
211
212 /* Process unlocked */
213 __HAL_UNLOCK(hcrc);
214
215 /* Return function status */
216 return HAL_OK;
217 }
218
219 /**
220 * @brief Initializes the CRC MSP.
221 * @param hcrc: CRC handle
222 * @retval None
223 */
224 __weak void HAL_CRC_MspInit(CRC_HandleTypeDef *hcrc)
225 {
226 /* NOTE : This function should not be modified, when the callback is needed,
227 the HAL_CRC_MspInit can be implemented in the user file
228 */
229 }
230
231 /**
232 * @brief DeInitializes the CRC MSP.
233 * @param hcrc: CRC handle
234 * @retval None
235 */
236 __weak void HAL_CRC_MspDeInit(CRC_HandleTypeDef *hcrc)
237 {
238 /* NOTE : This function should not be modified, when the callback is needed,
239 the HAL_CRC_MspDeInit can be implemented in the user file
240 */
241 }
242
243 /**
244 * @}
245 */
246
247 /** @defgroup CRC_Exported_Functions_Group2 Peripheral Control functions
248 * @brief management functions.
249 *
250 @verbatim
251 ===============================================================================
252 ##### Peripheral Control functions #####
253 ===============================================================================
254 [..] This section provides functions allowing to:
255 (+) Compute the 7, 8, 16 or 32-bit CRC value of an 8, 16 or 32-bit data buffer
256 using combination of the previous CRC value and the new one.
257
258 or
259
260 (+) Compute the 7, 8, 16 or 32-bit CRC value of an 8, 16 or 32-bit data buffer
261 independently of the previous CRC value.
262
263 @endverbatim
264 * @{
265 */
266
267 /**
268 * @brief Compute the 7, 8, 16 or 32-bit CRC value of an 8, 16 or 32-bit data buffer
269 * starting with the previously computed CRC as initialization value.
270 * @param hcrc: CRC handle
271 * @param pBuffer: pointer to the input data buffer, exact input data format is
272 * provided by hcrc->InputDataFormat.
273 * @param BufferLength: input data buffer length
274 * @retval uint32_t CRC (returned value LSBs for CRC shorter than 32 bits)
275 */
276 uint32_t HAL_CRC_Accumulate(CRC_HandleTypeDef *hcrc, uint32_t pBuffer[], uint32_t BufferLength)
277 {
278 uint32_t index = 0; /* CRC input data buffer index */
279 uint32_t temp = 0; /* CRC output (read from hcrc->Instance->DR register) */
280
281 /* Process locked */
282 __HAL_LOCK(hcrc);
283
284 /* Change CRC peripheral state */
285 hcrc->State = HAL_CRC_STATE_BUSY;
286
287 switch (hcrc->InputDataFormat)
288 {
289 case CRC_INPUTDATA_FORMAT_WORDS:
290 /* Enter Data to the CRC calculator */
291 for(index = 0; index < BufferLength; index++)
292 {
293 hcrc->Instance->DR = pBuffer[index];
294 }
295 temp = hcrc->Instance->DR;
296 break;
297
298 case CRC_INPUTDATA_FORMAT_BYTES:
299 temp = CRC_Handle_8(hcrc, (uint8_t*)pBuffer, BufferLength);
300 break;
301
302 case CRC_INPUTDATA_FORMAT_HALFWORDS:
303 temp = CRC_Handle_16(hcrc, (uint16_t*)pBuffer, BufferLength);
304 break;
305
306 default:
307 break;
308 }
309
310 /* Change CRC peripheral state */
311 hcrc->State = HAL_CRC_STATE_READY;
312
313 /* Process unlocked */
314 __HAL_UNLOCK(hcrc);
315
316 /* Return the CRC computed value */
317 return temp;
318 }
319
320
321 /**
322 * @brief Compute the 7, 8, 16 or 32-bit CRC value of an 8, 16 or 32-bit data buffer
323 * starting with hcrc->Instance->INIT as initialization value.
324 * @param hcrc: CRC handle
325 * @param pBuffer: pointer to the input data buffer, exact input data format is
326 * provided by hcrc->InputDataFormat.
327 * @param BufferLength: input data buffer length
328 * @retval uint32_t CRC (returned value LSBs for CRC shorter than 32 bits)
329 */
330 uint32_t HAL_CRC_Calculate(CRC_HandleTypeDef *hcrc, uint32_t pBuffer[], uint32_t BufferLength)
331 {
332 uint32_t index = 0; /* CRC input data buffer index */
333 uint32_t temp = 0; /* CRC output (read from hcrc->Instance->DR register) */
334
335 /* Process locked */
336 __HAL_LOCK(hcrc);
337
338 /* Change CRC peripheral state */
339 hcrc->State = HAL_CRC_STATE_BUSY;
340
341 /* Reset CRC Calculation Unit (hcrc->Instance->INIT is
342 * written in hcrc->Instance->DR) */
343 __HAL_CRC_DR_RESET(hcrc);
344
345 switch (hcrc->InputDataFormat)
346 {
347 case CRC_INPUTDATA_FORMAT_WORDS:
348 /* Enter 32-bit input data to the CRC calculator */
349 for(index = 0; index < BufferLength; index++)
350 {
351 hcrc->Instance->DR = pBuffer[index];
352 }
353 temp = hcrc->Instance->DR;
354 break;
355
356 case CRC_INPUTDATA_FORMAT_BYTES:
357 /* Specific 8-bit input data handling */
358 temp = CRC_Handle_8(hcrc, (uint8_t*)pBuffer, BufferLength);
359 break;
360
361 case CRC_INPUTDATA_FORMAT_HALFWORDS:
362 /* Specific 16-bit input data handling */
363 temp = CRC_Handle_16(hcrc, (uint16_t*)pBuffer, BufferLength);
364 break;
365
366 default:
367 break;
368 }
369
370 /* Change CRC peripheral state */
371 hcrc->State = HAL_CRC_STATE_READY;
372
373 /* Process unlocked */
374 __HAL_UNLOCK(hcrc);
375
376 /* Return the CRC computed value */
377 return temp;
378 }
379 /**
380 * @}
381 */
382
383 /** @defgroup CRC_Exported_Functions_Group3 Peripheral State functions
384 * @brief Peripheral State functions.
385 *
386 @verbatim
387 ===============================================================================
388 ##### Peripheral State functions #####
389 ===============================================================================
390 [..]
391 This subsection permits to get in run-time the status of the peripheral
392 and the data flow.
393
394 @endverbatim
395 * @{
396 */
397
398 /**
399 * @brief Returns the CRC state.
400 * @param hcrc: CRC handle
401 * @retval HAL state
402 */
403 HAL_CRC_StateTypeDef HAL_CRC_GetState(CRC_HandleTypeDef *hcrc)
404 {
405 return hcrc->State;
406 }
407
408 /**
409 * @}
410 */
411
412 /**
413 * @}
414 */
415
416 /** @addtogroup CRC_Private_Functions CRC Private Functions
417 * @{
418 */
419 /**
420 * @brief Enter 8-bit input data to the CRC calculator.
421 * Specific data handling to optimize processing time.
422 * @param hcrc: CRC handle
423 * @param pBuffer: pointer to the input data buffer
424 * @param BufferLength: input data buffer length
425 * @retval uint32_t CRC (returned value LSBs for CRC shorter than 32 bits)
426 */
427 static uint32_t CRC_Handle_8(CRC_HandleTypeDef *hcrc, uint8_t pBuffer[], uint32_t BufferLength)
428 {
429 uint32_t i = 0; /* input data buffer index */
430
431 /* Processing time optimization: 4 bytes are entered in a row with a single word write,
432 * last bytes must be carefully fed to the CRC calculator to ensure a correct type
433 * handling by the IP */
434 for(i = 0; i < (BufferLength/4); i++)
435 {
436 hcrc->Instance->DR = ((uint32_t)pBuffer[4*i]<<24) | ((uint32_t)pBuffer[4*i+1]<<16) | ((uint32_t)pBuffer[4*i+2]<<8) | (uint32_t)pBuffer[4*i+3];
437 }
438 /* last bytes specific handling */
439 if ((BufferLength%4) != 0)
440 {
441 if (BufferLength%4 == 1)
442 {
443 *(uint8_t*) (&hcrc->Instance->DR) = pBuffer[4*i];
444 }
445 if (BufferLength%4 == 2)
446 {
447 *(uint16_t*) (&hcrc->Instance->DR) = ((uint16_t)pBuffer[4*i]<<8) | (uint16_t)pBuffer[4*i+1];
448 }
449 if (BufferLength%4 == 3)
450 {
451 *(uint16_t*) (&hcrc->Instance->DR) = ((uint16_t)pBuffer[4*i]<<8) | (uint16_t)pBuffer[4*i+1];
452 *(uint8_t*) (&hcrc->Instance->DR) = pBuffer[4*i+2];
453 }
454 }
455
456 /* Return the CRC computed value */
457 return hcrc->Instance->DR;
458 }
459
460
461
462 /**
463 * @brief Enter 16-bit input data to the CRC calculator.
464 * Specific data handling to optimize processing time.
465 * @param hcrc: CRC handle
466 * @param pBuffer: pointer to the input data buffer
467 * @param BufferLength: input data buffer length
468 * @retval uint32_t CRC (returned value LSBs for CRC shorter than 32 bits)
469 */
470 static uint32_t CRC_Handle_16(CRC_HandleTypeDef *hcrc, uint16_t pBuffer[], uint32_t BufferLength)
471 {
472 uint32_t i = 0; /* input data buffer index */
473
474 /* Processing time optimization: 2 HalfWords are entered in a row with a single word write,
475 * in case of odd length, last HalfWord must be carefully fed to the CRC calculator to ensure
476 * a correct type handling by the IP */
477 for(i = 0; i < (BufferLength/2); i++)
478 {
479 hcrc->Instance->DR = (pBuffer[2*i]<<16) | pBuffer[2*i+1];
480 }
481 if ((BufferLength%2) != 0)
482 {
483 *(uint16_t*) (&hcrc->Instance->DR) = pBuffer[2*i];
484 }
485
486 /* Return the CRC computed value */
487 return hcrc->Instance->DR;
488 }
489 /**
490 * @}
491 */
492
493 /**
494 * @}
495 */
496
497 #endif /* HAL_CRC_MODULE_ENABLED */
498 /**
499 * @}
500 */
501
502 /**
503 * @}
504 */
505
506 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Imprint / Impressum