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