1 /**
2   ******************************************************************************
3   * @file    stm32f30x_crc.c
4   * @author  MCD Application Team
5   * @version V1.2.3
6   * @date    10-July-2015
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 2014 STMicroelectronics</center></h2>
39   *
40   * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
41   * You may not use this file except in compliance with the License.
42   * You may obtain a copy of the License at:
43   *
44   *        http://www.st.com/software_license_agreement_liberty_v2
45   *
46   * Unless required by applicable law or agreed to in writing, software
47   * distributed under the License is distributed on an "AS IS" BASIS,
48   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
49   * See the License for the specific language governing permissions and
50   * limitations under the License.
51   *
52   ******************************************************************************
53   */
54 
55 /* Includes ------------------------------------------------------------------*/
56 #include "stm32f30x_crc.h"
57 
58 /** @addtogroup STM32F30x_StdPeriph_Driver
59   * @{
60   */
61 
62 /** @defgroup CRC
63   * @brief CRC driver modules
64   * @{
65   */
66 
67 /* Private typedef -----------------------------------------------------------*/
68 /* Private define ------------------------------------------------------------*/
69 /* Private macro -------------------------------------------------------------*/
70 /* Private variables ---------------------------------------------------------*/
71 /* Private function prototypes -----------------------------------------------*/
72 /* Private functions ---------------------------------------------------------*/
73 
74 /** @defgroup CRC_Private_Functions
75   * @{
76   */
77 
78 /** @defgroup CRC_Group1 Configuration of the CRC computation unit functions
79  *  @brief   Configuration of the CRC computation unit functions
80  *
81 @verbatim
82  ===============================================================================
83                   ##### CRC configuration functions #####
84  ===============================================================================
85 
86 @endverbatim
87   * @{
88   */
89 
90 /**
91   * @brief  Deinitializes CRC peripheral registers to their default reset values.
92   * @param  None
93   * @retval None
94   */
CRC_DeInit(void)95 void CRC_DeInit(void)
96 {
97   /* Set DR register to reset value */
98   CRC->DR = 0xFFFFFFFF;
99   /* Set the POL register to the reset value: 0x04C11DB7 */
100   CRC->POL = 0x04C11DB7;
101   /* Reset IDR register */
102   CRC->IDR = 0x00;
103   /* Set INIT register to reset value */
104   CRC->INIT = 0xFFFFFFFF;
105   /* Reset the CRC calculation unit */
106   CRC->CR = CRC_CR_RESET;
107 }
108 
109 /**
110   * @brief  Resets the CRC calculation unit and sets INIT register content in DR register.
111   * @param  None
112   * @retval None
113   */
CRC_ResetDR(void)114 void CRC_ResetDR(void)
115 {
116   /* Reset CRC generator */
117   CRC->CR |= CRC_CR_RESET;
118 }
119 
120 /**
121   * @brief  Selects the polynomial size.
122   * @param  CRC_PolSize: Specifies the polynomial size.
123   *         This parameter can be:
124   *          @arg CRC_PolSize_7: 7-bit polynomial for CRC calculation
125   *          @arg CRC_PolSize_8: 8-bit polynomial for CRC calculation
126   *          @arg CRC_PolSize_16: 16-bit polynomial for CRC calculation
127   *          @arg CRC_PolSize_32: 32-bit polynomial for CRC calculation
128   * @retval None
129   */
CRC_PolynomialSizeSelect(uint32_t CRC_PolSize)130 void CRC_PolynomialSizeSelect(uint32_t CRC_PolSize)
131 {
132   uint32_t tmpcr = 0;
133 
134   /* Check the parameter */
135   assert_param(IS_CRC_POL_SIZE(CRC_PolSize));
136 
137   /* Get CR register value */
138   tmpcr = CRC->CR;
139 
140   /* Reset POL_SIZE bits */
141   tmpcr &= (uint32_t)~((uint32_t)CRC_CR_POLSIZE);
142   /* Set the polynomial size */
143   tmpcr |= (uint32_t)CRC_PolSize;
144 
145   /* Write to CR register */
146   CRC->CR = (uint32_t)tmpcr;
147 }
148 
149 /**
150   * @brief  Selects the reverse operation to be performed on input data.
151   * @param  CRC_ReverseInputData: Specifies the reverse operation on input data.
152   *         This parameter can be:
153   *          @arg CRC_ReverseInputData_No: No reverse operation is performed
154   *          @arg CRC_ReverseInputData_8bits: reverse operation performed on 8 bits
155   *          @arg CRC_ReverseInputData_16bits: reverse operation performed on 16 bits
156   *          @arg CRC_ReverseInputData_32bits: reverse operation performed on 32 bits
157   * @retval None
158   */
CRC_ReverseInputDataSelect(uint32_t CRC_ReverseInputData)159 void CRC_ReverseInputDataSelect(uint32_t CRC_ReverseInputData)
160 {
161   uint32_t tmpcr = 0;
162 
163   /* Check the parameter */
164   assert_param(IS_CRC_REVERSE_INPUT_DATA(CRC_ReverseInputData));
165 
166   /* Get CR register value */
167   tmpcr = CRC->CR;
168 
169   /* Reset REV_IN bits */
170   tmpcr &= (uint32_t)~((uint32_t)CRC_CR_REV_IN);
171   /* Set the reverse operation */
172   tmpcr |= (uint32_t)CRC_ReverseInputData;
173 
174   /* Write to CR register */
175   CRC->CR = (uint32_t)tmpcr;
176 }
177 
178 /**
179   * @brief  Enables or disable the reverse operation on output data.
180   *         The reverse operation on output data is performed on 32-bit.
181   * @param  NewState: new state of the reverse operation on output data.
182   *   This parameter can be: ENABLE or DISABLE.
183   * @retval None
184   */
CRC_ReverseOutputDataCmd(FunctionalState NewState)185 void CRC_ReverseOutputDataCmd(FunctionalState NewState)
186 {
187   /* Check the parameters */
188   assert_param(IS_FUNCTIONAL_STATE(NewState));
189 
190   if (NewState != DISABLE)
191   {
192     /* Enable reverse operation on output data */
193     CRC->CR |= CRC_CR_REV_OUT;
194   }
195   else
196   {
197     /* Disable reverse operation on output data */
198     CRC->CR &= (uint32_t)~((uint32_t)CRC_CR_REV_OUT);
199   }
200 }
201 
202 /**
203   * @brief  Initializes the INIT register.
204   * @note   After resetting CRC calculation unit, CRC_InitValue is stored in DR register
205   * @param  CRC_InitValue: Programmable initial CRC value
206   * @retval None
207   */
CRC_SetInitRegister(uint32_t CRC_InitValue)208 void CRC_SetInitRegister(uint32_t CRC_InitValue)
209 {
210   CRC->INIT = CRC_InitValue;
211 }
212 
213 /**
214   * @brief  Initializes the polynomial coefficients.
215   * @param  CRC_Pol: Polynomial to be used for CRC calculation.
216   * @retval None
217   */
CRC_SetPolynomial(uint32_t CRC_Pol)218 void CRC_SetPolynomial(uint32_t CRC_Pol)
219 {
220   CRC->POL = CRC_Pol;
221 }
222 
223 /**
224   * @}
225   */
226 
227 /** @defgroup CRC_Group2 CRC computation of one/many 32-bit data functions
228  *  @brief   CRC computation of one/many 32-bit data functions
229  *
230 @verbatim
231  ===============================================================================
232                       ##### CRC computation functions #####
233  ===============================================================================
234 
235 @endverbatim
236   * @{
237   */
238 
239 /**
240   * @brief  Computes the 32-bit CRC of a given data word(32-bit).
241   * @param  CRC_Data: data word(32-bit) to compute its CRC
242   * @retval 32-bit CRC
243   */
CRC_CalcCRC(uint32_t CRC_Data)244 uint32_t CRC_CalcCRC(uint32_t CRC_Data)
245 {
246   CRC->DR = CRC_Data;
247 
248   return (CRC->DR);
249 }
250 
251 /**
252   * @brief  Computes the 16-bit CRC of a given 16-bit data.
253   * @param  CRC_Data: data half-word(16-bit) to compute its CRC
254   * @retval 16-bit CRC
255   */
CRC_CalcCRC16bits(uint16_t CRC_Data)256 uint32_t CRC_CalcCRC16bits(uint16_t CRC_Data)
257 {
258   *(uint16_t*)(CRC_BASE) = (uint16_t) CRC_Data;
259 
260   return (CRC->DR);
261 }
262 
263 /**
264   * @brief  Computes the 8-bit CRC of a given 8-bit data.
265   * @param  CRC_Data: 8-bit data to compute its CRC
266   * @retval 8-bit CRC
267   */
CRC_CalcCRC8bits(uint8_t CRC_Data)268 uint32_t CRC_CalcCRC8bits(uint8_t CRC_Data)
269 {
270   *(uint8_t*)(CRC_BASE) = (uint8_t) CRC_Data;
271 
272   return (CRC->DR);
273 }
274 
275 /**
276   * @brief  Computes the 32-bit CRC of a given buffer of data word(32-bit).
277   * @param  pBuffer: pointer to the buffer containing the data to be computed
278   * @param  BufferLength: length of the buffer to be computed
279   * @retval 32-bit CRC
280   */
CRC_CalcBlockCRC(uint32_t pBuffer[],uint32_t BufferLength)281 uint32_t CRC_CalcBlockCRC(uint32_t pBuffer[], uint32_t BufferLength)
282 {
283   uint32_t index = 0;
284 
285   for(index = 0; index < BufferLength; index++)
286   {
287     CRC->DR = pBuffer[index];
288   }
289   return (CRC->DR);
290 }
291 
292 /**
293   * @brief  Returns the current CRC value.
294   * @param  None
295   * @retval 32-bit CRC
296   */
CRC_GetCRC(void)297 uint32_t CRC_GetCRC(void)
298 {
299   return (CRC->DR);
300 }
301 
302 /**
303   * @}
304   */
305 
306 /** @defgroup CRC_Group3 CRC Independent Register (IDR) access functions
307  *  @brief   CRC Independent Register (IDR) access (write/read) functions
308  *
309 @verbatim
310  ===============================================================================
311            ##### CRC Independent Register (IDR) access functions #####
312  ===============================================================================
313 
314 @endverbatim
315   * @{
316   */
317 
318 /**
319   * @brief  Stores an 8-bit data in the Independent Data(ID) register.
320   * @param  CRC_IDValue: 8-bit value to be stored in the ID register
321   * @retval None
322   */
CRC_SetIDRegister(uint8_t CRC_IDValue)323 void CRC_SetIDRegister(uint8_t CRC_IDValue)
324 {
325   CRC->IDR = CRC_IDValue;
326 }
327 
328 /**
329   * @brief  Returns the 8-bit data stored in the Independent Data(ID) register
330   * @param  None
331   * @retval 8-bit value of the ID register
332   */
CRC_GetIDRegister(void)333 uint8_t CRC_GetIDRegister(void)
334 {
335   return (CRC->IDR);
336 }
337 
338 /**
339   * @}
340   */
341 
342 /**
343   * @}
344   */
345 
346 /**
347   * @}
348   */
349 
350 /**
351   * @}
352   */
353 
354 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
355