]> git.donarmstrong.com Git - qmk_firmware.git/blob - tool/mbed/mbed-sdk/libraries/mbed/targets/cmsis/TARGET_STM/TARGET_STM32F3XX/stm32f30x_crc.c
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[qmk_firmware.git] / tool / mbed / mbed-sdk / libraries / mbed / targets / cmsis / TARGET_STM / TARGET_STM32F3XX / stm32f30x_crc.c
1 /**
2   ******************************************************************************
3   * @file    stm32f30x_crc.c
4   * @author  MCD Application Team
5   * @version V1.1.0
6   * @date    27-February-2014
7   * @brief   This file provides firmware functions to manage the following 
8   *          functionalities of CRC computation unit peripheral:
9   *            + Configuration of the CRC computation unit
10   *            + CRC computation of one/many 32-bit data
11   *            + CRC Independent register (IDR) access
12   *
13   @verbatim
14     
15  ===============================================================================
16                       ##### How to use this driver #####
17  ===============================================================================
18     [..] 
19     (#) Enable CRC AHB clock using RCC_AHBPeriphClockCmd(RCC_AHBPeriph_CRC, ENABLE)
20         function.
21     (#) Select the polynomial size: 7-bit, 8-bit, 16-bit or 32-bit.
22     (#) Set the polynomial coefficients using CRC_SetPolynomial();  
23     (#) If required, select the reverse operation on input data 
24         using CRC_ReverseInputDataSelect();  
25     (#) If required, enable the reverse operation on output data
26         using CRC_ReverseOutputDataCmd(Enable);
27     (#) If required, set the initialization remainder value using
28         CRC_SetInitRegister();
29     (#) use CRC_CalcCRC() function to compute the CRC of a 32-bit data
30         or use CRC_CalcBlockCRC() function to compute the CRC if a 32-bit 
31         data buffer.
32
33   @endverbatim
34   
35   ******************************************************************************
36   * @attention
37   *
38   * <h2><center>&copy; COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
39   *
40   * Redistribution and use in source and binary forms, with or without modification,
41   * are permitted provided that the following conditions are met:
42   *   1. Redistributions of source code must retain the above copyright notice,
43   *      this list of conditions and the following disclaimer.
44   *   2. Redistributions in binary form must reproduce the above copyright notice,
45   *      this list of conditions and the following disclaimer in the documentation
46   *      and/or other materials provided with the distribution.
47   *   3. Neither the name of STMicroelectronics nor the names of its contributors
48   *      may be used to endorse or promote products derived from this software
49   *      without specific prior written permission.
50   *
51   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
52   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
53   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
54   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
55   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
56   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
57   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
58   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
59   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
60   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
61   *
62   ******************************************************************************
63   */
64
65 /* Includes ------------------------------------------------------------------*/
66 #include "stm32f30x_crc.h"
67
68 /** @addtogroup STM32F30x_StdPeriph_Driver
69   * @{
70   */
71
72 /** @defgroup CRC 
73   * @brief CRC driver modules
74   * @{
75   */
76
77 /* Private typedef -----------------------------------------------------------*/
78 /* Private define ------------------------------------------------------------*/
79 /* Private macro -------------------------------------------------------------*/
80 /* Private variables ---------------------------------------------------------*/
81 /* Private function prototypes -----------------------------------------------*/
82 /* Private functions ---------------------------------------------------------*/
83
84 /** @defgroup CRC_Private_Functions
85   * @{
86   */
87
88 /** @defgroup CRC_Group1 Configuration of the CRC computation unit functions
89  *  @brief   Configuration of the CRC computation unit functions 
90  *
91 @verbatim
92  ===============================================================================
93                   ##### CRC configuration functions #####
94  ===============================================================================
95
96 @endverbatim
97   * @{
98   */
99
100 /**
101   * @brief  Deinitializes CRC peripheral registers to their default reset values.
102   * @param  None
103   * @retval None
104   */
105 void CRC_DeInit(void)
106 {
107   /* Set DR register to reset value */
108   CRC->DR = 0xFFFFFFFF;
109   /* Set the POL register to the reset value: 0x04C11DB7 */
110   CRC->POL = 0x04C11DB7;
111   /* Reset IDR register */
112   CRC->IDR = 0x00;
113   /* Set INIT register to reset value */
114   CRC->INIT = 0xFFFFFFFF;
115   /* Reset the CRC calculation unit */
116   CRC->CR = CRC_CR_RESET;
117 }
118
119 /**
120   * @brief  Resets the CRC calculation unit and sets INIT register content in DR register.
121   * @param  None
122   * @retval None
123   */
124 void CRC_ResetDR(void)
125 {
126   /* Reset CRC generator */
127   CRC->CR |= CRC_CR_RESET;
128 }
129
130 /**
131   * @brief  Selects the polynomial size.
132   * @param  CRC_PolSize: Specifies the polynomial size.
133   *         This parameter can be:
134   *          @arg CRC_PolSize_7: 7-bit polynomial for CRC calculation
135   *          @arg CRC_PolSize_8: 8-bit polynomial for CRC calculation
136   *          @arg CRC_PolSize_16: 16-bit polynomial for CRC calculation
137   *          @arg CRC_PolSize_32: 32-bit polynomial for CRC calculation
138   * @retval None
139   */
140 void CRC_PolynomialSizeSelect(uint32_t CRC_PolSize)
141 {
142   uint32_t tmpcr = 0;
143
144   /* Check the parameter */
145   assert_param(IS_CRC_POL_SIZE(CRC_PolSize));
146
147   /* Get CR register value */
148   tmpcr = CRC->CR;
149
150   /* Reset POL_SIZE bits */
151   tmpcr &= (uint32_t)~((uint32_t)CRC_CR_POLSIZE);
152   /* Set the polynomial size */
153   tmpcr |= (uint32_t)CRC_PolSize;
154
155   /* Write to CR register */
156   CRC->CR = (uint32_t)tmpcr;
157 }
158
159 /**
160   * @brief  Selects the reverse operation to be performed on input data.
161   * @param  CRC_ReverseInputData: Specifies the reverse operation on input data.
162   *         This parameter can be:
163   *          @arg CRC_ReverseInputData_No: No reverse operation is performed
164   *          @arg CRC_ReverseInputData_8bits: reverse operation performed on 8 bits
165   *          @arg CRC_ReverseInputData_16bits: reverse operation performed on 16 bits
166   *          @arg CRC_ReverseInputData_32bits: reverse operation performed on 32 bits
167   * @retval None
168   */
169 void CRC_ReverseInputDataSelect(uint32_t CRC_ReverseInputData)
170 {
171   uint32_t tmpcr = 0;
172
173   /* Check the parameter */
174   assert_param(IS_CRC_REVERSE_INPUT_DATA(CRC_ReverseInputData));
175
176   /* Get CR register value */
177   tmpcr = CRC->CR;
178
179   /* Reset REV_IN bits */
180   tmpcr &= (uint32_t)~((uint32_t)CRC_CR_REV_IN);
181   /* Set the reverse operation */
182   tmpcr |= (uint32_t)CRC_ReverseInputData;
183
184   /* Write to CR register */
185   CRC->CR = (uint32_t)tmpcr;
186 }
187
188 /**
189   * @brief  Enables or disable the reverse operation on output data.
190   *         The reverse operation on output data is performed on 32-bit.
191   * @param  NewState: new state of the reverse operation on output data.
192   *   This parameter can be: ENABLE or DISABLE.
193   * @retval None
194   */
195 void CRC_ReverseOutputDataCmd(FunctionalState NewState)
196 {
197   /* Check the parameters */
198   assert_param(IS_FUNCTIONAL_STATE(NewState));
199
200   if (NewState != DISABLE)
201   {
202     /* Enable reverse operation on output data */
203     CRC->CR |= CRC_CR_REV_OUT;
204   }
205   else
206   {
207     /* Disable reverse operation on output data */
208     CRC->CR &= (uint32_t)~((uint32_t)CRC_CR_REV_OUT);
209   }
210 }
211
212 /**
213   * @brief  Initializes the INIT register.
214   * @note   After resetting CRC calculation unit, CRC_InitValue is stored in DR register
215   * @param  CRC_InitValue: Programmable initial CRC value
216   * @retval None
217   */
218 void CRC_SetInitRegister(uint32_t CRC_InitValue)
219 {
220   CRC->INIT = CRC_InitValue;
221 }
222
223 /**
224   * @brief  Initializes the polynomail coefficients.
225   * @param  CRC_Pol: Polynomial to be used for CRC calculation.
226   * @retval None
227   */
228 void CRC_SetPolynomial(uint32_t CRC_Pol)
229 {
230   CRC->POL = CRC_Pol;
231 }
232
233 /**
234   * @}
235   */
236
237 /** @defgroup CRC_Group2 CRC computation of one/many 32-bit data functions
238  *  @brief   CRC computation of one/many 32-bit data functions
239  *
240 @verbatim
241  ===============================================================================
242                       ##### CRC computation functions #####
243  ===============================================================================
244
245 @endverbatim
246   * @{
247   */
248
249 /**
250   * @brief  Computes the 32-bit CRC of a given data word(32-bit).
251   * @param  CRC_Data: data word(32-bit) to compute its CRC
252   * @retval 32-bit CRC
253   */
254 uint32_t CRC_CalcCRC(uint32_t CRC_Data)
255 {
256   CRC->DR = CRC_Data;
257   
258   return (CRC->DR);
259 }
260
261 /**
262   * @brief  Computes the 16-bit CRC of a given 16-bit data.
263   * @param  CRC_Data: data half-word(16-bit) to compute its CRC
264   * @retval 16-bit CRC
265   */
266 uint32_t CRC_CalcCRC16bits(uint16_t CRC_Data)
267 {
268   *(uint16_t*)(CRC_BASE) = (uint16_t) CRC_Data;
269   
270   return (CRC->DR);
271 }
272
273 /**
274   * @brief  Computes the 8-bit CRC of a given 8-bit data.
275   * @param  CRC_Data: 8-bit data to compute its CRC
276   * @retval 8-bit CRC
277   */
278 uint32_t CRC_CalcCRC8bits(uint8_t CRC_Data)
279 {
280   *(uint8_t*)(CRC_BASE) = (uint8_t) CRC_Data;
281
282   return (CRC->DR);
283 }
284
285 /**
286   * @brief  Computes the 32-bit CRC of a given buffer of data word(32-bit).
287   * @param  pBuffer: pointer to the buffer containing the data to be computed
288   * @param  BufferLength: length of the buffer to be computed
289   * @retval 32-bit CRC
290   */
291 uint32_t CRC_CalcBlockCRC(uint32_t pBuffer[], uint32_t BufferLength)
292 {
293   uint32_t index = 0;
294   
295   for(index = 0; index < BufferLength; index++)
296   {
297     CRC->DR = pBuffer[index];
298   }
299   return (CRC->DR);
300 }
301
302 /**
303   * @brief  Returns the current CRC value.
304   * @param  None
305   * @retval 32-bit CRC
306   */
307 uint32_t CRC_GetCRC(void)
308 {
309   return (CRC->DR);
310 }
311
312 /**
313   * @}
314   */
315
316 /** @defgroup CRC_Group3 CRC Independent Register (IDR) access functions
317  *  @brief   CRC Independent Register (IDR) access (write/read) functions
318  *
319 @verbatim
320  ===============================================================================
321            ##### CRC Independent Register (IDR) access functions #####
322  ===============================================================================
323
324 @endverbatim
325   * @{
326   */
327
328 /**
329   * @brief  Stores an 8-bit data in the Independent Data(ID) register.
330   * @param  CRC_IDValue: 8-bit value to be stored in the ID register                                    
331   * @retval None
332   */
333 void CRC_SetIDRegister(uint8_t CRC_IDValue)
334 {
335   CRC->IDR = CRC_IDValue;
336 }
337
338 /**
339   * @brief  Returns the 8-bit data stored in the Independent Data(ID) register
340   * @param  None
341   * @retval 8-bit value of the ID register 
342   */
343 uint8_t CRC_GetIDRegister(void)
344 {
345   return (CRC->IDR);
346 }
347
348 /**
349   * @}
350   */
351
352 /**
353   * @}
354   */
355
356 /**
357   * @}
358   */
359
360 /**
361   * @}
362   */
363
364 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/