1 /**
2   ******************************************************************************
3   * @file    stm32l4xx_ll_crc.h
4   * @author  MCD Application Team
5   * @brief   Header file of CRC LL module.
6   ******************************************************************************
7   * @attention
8   *
9   * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
10   *
11   * Redistribution and use in source and binary forms, with or without modification,
12   * are permitted provided that the following conditions are met:
13   *   1. Redistributions of source code must retain the above copyright notice,
14   *      this list of conditions and the following disclaimer.
15   *   2. Redistributions in binary form must reproduce the above copyright notice,
16   *      this list of conditions and the following disclaimer in the documentation
17   *      and/or other materials provided with the distribution.
18   *   3. Neither the name of STMicroelectronics nor the names of its contributors
19   *      may be used to endorse or promote products derived from this software
20   *      without specific prior written permission.
21   *
22   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
26   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32   *
33   ******************************************************************************
34   */
35 
36 /* Define to prevent recursive inclusion -------------------------------------*/
37 #ifndef __STM32L4xx_LL_CRC_H
38 #define __STM32L4xx_LL_CRC_H
39 
40 #ifdef __cplusplus
41 extern "C" {
42 #endif
43 
44 /* Includes ------------------------------------------------------------------*/
45 #include "stm32l4xx.h"
46 
47 /** @addtogroup STM32L4xx_LL_Driver
48   * @{
49   */
50 
51 #if defined(CRC)
52 
53 /** @defgroup CRC_LL CRC
54   * @{
55   */
56 
57 /* Private types -------------------------------------------------------------*/
58 /* Private variables ---------------------------------------------------------*/
59 /* Private constants ---------------------------------------------------------*/
60 /* Private macros ------------------------------------------------------------*/
61 
62 /* Exported types ------------------------------------------------------------*/
63 /* Exported constants --------------------------------------------------------*/
64 /** @defgroup CRC_LL_Exported_Constants CRC Exported Constants
65   * @{
66   */
67 
68 /** @defgroup CRC_LL_EC_POLYLENGTH Polynomial length
69   * @{
70   */
71 #define LL_CRC_POLYLENGTH_32B              0x00000000U                              /*!< 32 bits Polynomial size */
72 #define LL_CRC_POLYLENGTH_16B              CRC_CR_POLYSIZE_0                        /*!< 16 bits Polynomial size */
73 #define LL_CRC_POLYLENGTH_8B               CRC_CR_POLYSIZE_1                        /*!< 8 bits Polynomial size */
74 #define LL_CRC_POLYLENGTH_7B               (CRC_CR_POLYSIZE_1 | CRC_CR_POLYSIZE_0)  /*!< 7 bits Polynomial size */
75 /**
76   * @}
77   */
78 
79 /** @defgroup CRC_LL_EC_INDATA_REVERSE Input Data Reverse
80   * @{
81   */
82 #define LL_CRC_INDATA_REVERSE_NONE         0x00000000U                              /*!< Input Data bit order not affected */
83 #define LL_CRC_INDATA_REVERSE_BYTE         CRC_CR_REV_IN_0                          /*!< Input Data bit reversal done by byte */
84 #define LL_CRC_INDATA_REVERSE_HALFWORD     CRC_CR_REV_IN_1                          /*!< Input Data bit reversal done by half-word */
85 #define LL_CRC_INDATA_REVERSE_WORD         (CRC_CR_REV_IN_1 | CRC_CR_REV_IN_0)      /*!< Input Data bit reversal done by word */
86 /**
87   * @}
88   */
89 
90 /** @defgroup CRC_LL_EC_OUTDATA_REVERSE Output Data Reverse
91   * @{
92   */
93 #define LL_CRC_OUTDATA_REVERSE_NONE        0x00000000U                               /*!< Output Data bit order not affected */
94 #define LL_CRC_OUTDATA_REVERSE_BIT         CRC_CR_REV_OUT                            /*!< Output Data bit reversal done by bit */
95 /**
96   * @}
97   */
98 
99 /** @defgroup CRC_LL_EC_Default_Polynomial_Value    Default CRC generating polynomial value
100   * @brief    Normal representation of this polynomial value is
101   *           X^32 + X^26 + X^23 + X^22 + X^16 + X^12 + X^11 + X^10 +X^8 + X^7 + X^5 + X^4 + X^2 + X + 1 .
102   * @{
103   */
104 #define LL_CRC_DEFAULT_CRC32_POLY          0x04C11DB7U                               /*!< Default CRC generating polynomial value */
105 /**
106   * @}
107   */
108 
109 /** @defgroup CRC_LL_EC_Default_InitValue    Default CRC computation initialization value
110   * @{
111   */
112 #define LL_CRC_DEFAULT_CRC_INITVALUE       0xFFFFFFFFU                               /*!< Default CRC computation initialization value */
113 /**
114   * @}
115   */
116 
117 /**
118   * @}
119   */
120 
121 /* Exported macro ------------------------------------------------------------*/
122 /** @defgroup CRC_LL_Exported_Macros CRC Exported Macros
123   * @{
124   */
125 
126 /** @defgroup CRC_LL_EM_WRITE_READ Common Write and read registers Macros
127   * @{
128   */
129 
130 /**
131   * @brief  Write a value in CRC register
132   * @param  __INSTANCE__ CRC Instance
133   * @param  __REG__ Register to be written
134   * @param  __VALUE__ Value to be written in the register
135   * @retval None
136   */
137 #define LL_CRC_WriteReg(__INSTANCE__, __REG__, __VALUE__) WRITE_REG(__INSTANCE__->__REG__, __VALUE__)
138 
139 /**
140   * @brief  Read a value in CRC register
141   * @param  __INSTANCE__ CRC Instance
142   * @param  __REG__ Register to be read
143   * @retval Register value
144   */
145 #define LL_CRC_ReadReg(__INSTANCE__, __REG__) READ_REG(__INSTANCE__->__REG__)
146 /**
147   * @}
148   */
149 
150 /**
151   * @}
152   */
153 
154 
155 /* Exported functions --------------------------------------------------------*/
156 /** @defgroup CRC_LL_Exported_Functions CRC Exported Functions
157   * @{
158   */
159 
160 /** @defgroup CRC_LL_EF_Configuration CRC Configuration functions
161   * @{
162   */
163 
164 /**
165   * @brief  Reset the CRC calculation unit.
166   * @note   If Programmable Initial CRC value feature
167   *         is available, also set the Data Register to the value stored in the
168   *         CRC_INIT register, otherwise, reset Data Register to its default value.
169   * @rmtoll CR           RESET         LL_CRC_ResetCRCCalculationUnit
170   * @param  CRCx CRC Instance
171   * @retval None
172   */
LL_CRC_ResetCRCCalculationUnit(CRC_TypeDef * CRCx)173 __STATIC_INLINE void LL_CRC_ResetCRCCalculationUnit(CRC_TypeDef *CRCx)
174 {
175   SET_BIT(CRCx->CR, CRC_CR_RESET);
176 }
177 
178 /**
179   * @brief  Configure size of the polynomial.
180   * @rmtoll CR           POLYSIZE      LL_CRC_SetPolynomialSize
181   * @param  CRCx CRC Instance
182   * @param  PolySize This parameter can be one of the following values:
183   *         @arg @ref LL_CRC_POLYLENGTH_32B
184   *         @arg @ref LL_CRC_POLYLENGTH_16B
185   *         @arg @ref LL_CRC_POLYLENGTH_8B
186   *         @arg @ref LL_CRC_POLYLENGTH_7B
187   * @retval None
188   */
LL_CRC_SetPolynomialSize(CRC_TypeDef * CRCx,uint32_t PolySize)189 __STATIC_INLINE void LL_CRC_SetPolynomialSize(CRC_TypeDef *CRCx, uint32_t PolySize)
190 {
191   MODIFY_REG(CRCx->CR, CRC_CR_POLYSIZE, PolySize);
192 }
193 
194 /**
195   * @brief  Return size of the polynomial.
196   * @rmtoll CR           POLYSIZE      LL_CRC_GetPolynomialSize
197   * @param  CRCx CRC Instance
198   * @retval Returned value can be one of the following values:
199   *         @arg @ref LL_CRC_POLYLENGTH_32B
200   *         @arg @ref LL_CRC_POLYLENGTH_16B
201   *         @arg @ref LL_CRC_POLYLENGTH_8B
202   *         @arg @ref LL_CRC_POLYLENGTH_7B
203   */
LL_CRC_GetPolynomialSize(CRC_TypeDef * CRCx)204 __STATIC_INLINE uint32_t LL_CRC_GetPolynomialSize(CRC_TypeDef *CRCx)
205 {
206   return (uint32_t)(READ_BIT(CRCx->CR, CRC_CR_POLYSIZE));
207 }
208 
209 /**
210   * @brief  Configure the reversal of the bit order of the input data
211   * @rmtoll CR           REV_IN        LL_CRC_SetInputDataReverseMode
212   * @param  CRCx CRC Instance
213   * @param  ReverseMode This parameter can be one of the following values:
214   *         @arg @ref LL_CRC_INDATA_REVERSE_NONE
215   *         @arg @ref LL_CRC_INDATA_REVERSE_BYTE
216   *         @arg @ref LL_CRC_INDATA_REVERSE_HALFWORD
217   *         @arg @ref LL_CRC_INDATA_REVERSE_WORD
218   * @retval None
219   */
LL_CRC_SetInputDataReverseMode(CRC_TypeDef * CRCx,uint32_t ReverseMode)220 __STATIC_INLINE void LL_CRC_SetInputDataReverseMode(CRC_TypeDef *CRCx, uint32_t ReverseMode)
221 {
222   MODIFY_REG(CRCx->CR, CRC_CR_REV_IN, ReverseMode);
223 }
224 
225 /**
226   * @brief  Return type of reversal for input data bit order
227   * @rmtoll CR           REV_IN        LL_CRC_GetInputDataReverseMode
228   * @param  CRCx CRC Instance
229   * @retval Returned value can be one of the following values:
230   *         @arg @ref LL_CRC_INDATA_REVERSE_NONE
231   *         @arg @ref LL_CRC_INDATA_REVERSE_BYTE
232   *         @arg @ref LL_CRC_INDATA_REVERSE_HALFWORD
233   *         @arg @ref LL_CRC_INDATA_REVERSE_WORD
234   */
LL_CRC_GetInputDataReverseMode(CRC_TypeDef * CRCx)235 __STATIC_INLINE uint32_t LL_CRC_GetInputDataReverseMode(CRC_TypeDef *CRCx)
236 {
237   return (uint32_t)(READ_BIT(CRCx->CR, CRC_CR_REV_IN));
238 }
239 
240 /**
241   * @brief  Configure the reversal of the bit order of the Output data
242   * @rmtoll CR           REV_OUT       LL_CRC_SetOutputDataReverseMode
243   * @param  CRCx CRC Instance
244   * @param  ReverseMode This parameter can be one of the following values:
245   *         @arg @ref LL_CRC_OUTDATA_REVERSE_NONE
246   *         @arg @ref LL_CRC_OUTDATA_REVERSE_BIT
247   * @retval None
248   */
LL_CRC_SetOutputDataReverseMode(CRC_TypeDef * CRCx,uint32_t ReverseMode)249 __STATIC_INLINE void LL_CRC_SetOutputDataReverseMode(CRC_TypeDef *CRCx, uint32_t ReverseMode)
250 {
251   MODIFY_REG(CRCx->CR, CRC_CR_REV_OUT, ReverseMode);
252 }
253 
254 /**
255   * @brief  Configure the reversal of the bit order of the Output data
256   * @rmtoll CR           REV_OUT       LL_CRC_GetOutputDataReverseMode
257   * @param  CRCx CRC Instance
258   * @retval Returned value can be one of the following values:
259   *         @arg @ref LL_CRC_OUTDATA_REVERSE_NONE
260   *         @arg @ref LL_CRC_OUTDATA_REVERSE_BIT
261   */
LL_CRC_GetOutputDataReverseMode(CRC_TypeDef * CRCx)262 __STATIC_INLINE uint32_t LL_CRC_GetOutputDataReverseMode(CRC_TypeDef *CRCx)
263 {
264   return (uint32_t)(READ_BIT(CRCx->CR, CRC_CR_REV_OUT));
265 }
266 
267 /**
268   * @brief  Initialize the Programmable initial CRC value.
269   * @note   If the CRC size is less than 32 bits, the least significant bits
270   *         are used to write the correct value
271   * @note   LL_CRC_DEFAULT_CRC_INITVALUE could be used as value for InitCrc parameter.
272   * @rmtoll INIT         INIT          LL_CRC_SetInitialData
273   * @param  CRCx CRC Instance
274   * @param  InitCrc Value to be programmed in Programmable initial CRC value register
275   * @retval None
276   */
LL_CRC_SetInitialData(CRC_TypeDef * CRCx,uint32_t InitCrc)277 __STATIC_INLINE void LL_CRC_SetInitialData(CRC_TypeDef *CRCx, uint32_t InitCrc)
278 {
279   WRITE_REG(CRCx->INIT, InitCrc);
280 }
281 
282 /**
283   * @brief  Return current Initial CRC value.
284   * @note   If the CRC size is less than 32 bits, the least significant bits
285   *         are used to read the correct value
286   * @rmtoll INIT         INIT          LL_CRC_GetInitialData
287   * @param  CRCx CRC Instance
288   * @retval Value programmed in Programmable initial CRC value register
289   */
LL_CRC_GetInitialData(CRC_TypeDef * CRCx)290 __STATIC_INLINE uint32_t LL_CRC_GetInitialData(CRC_TypeDef *CRCx)
291 {
292   return (uint32_t)(READ_REG(CRCx->INIT));
293 }
294 
295 /**
296   * @brief  Initialize the Programmable polynomial value
297   *         (coefficients of the polynomial to be used for CRC calculation).
298   * @note   LL_CRC_DEFAULT_CRC32_POLY could be used as value for PolynomCoef parameter.
299   * @note   Please check Reference Manual and existing Errata Sheets,
300   *         regarding possible limitations for Polynomial values usage.
301   *         For example, for a polynomial of degree 7, X^7 + X^6 + X^5 + X^2 + 1 is written 0x65
302   * @rmtoll POL          POL           LL_CRC_SetPolynomialCoef
303   * @param  CRCx CRC Instance
304   * @param  PolynomCoef Value to be programmed in Programmable Polynomial value register
305   * @retval None
306   */
LL_CRC_SetPolynomialCoef(CRC_TypeDef * CRCx,uint32_t PolynomCoef)307 __STATIC_INLINE void LL_CRC_SetPolynomialCoef(CRC_TypeDef *CRCx, uint32_t PolynomCoef)
308 {
309   WRITE_REG(CRCx->POL, PolynomCoef);
310 }
311 
312 /**
313   * @brief  Return current Programmable polynomial value
314   * @note   Please check Reference Manual and existing Errata Sheets,
315   *         regarding possible limitations for Polynomial values usage.
316   *         For example, for a polynomial of degree 7, X^7 + X^6 + X^5 + X^2 + 1 is written 0x65
317   * @rmtoll POL          POL           LL_CRC_GetPolynomialCoef
318   * @param  CRCx CRC Instance
319   * @retval Value programmed in Programmable Polynomial value register
320   */
LL_CRC_GetPolynomialCoef(CRC_TypeDef * CRCx)321 __STATIC_INLINE uint32_t LL_CRC_GetPolynomialCoef(CRC_TypeDef *CRCx)
322 {
323   return (uint32_t)(READ_REG(CRCx->POL));
324 }
325 
326 /**
327   * @}
328   */
329 
330 /** @defgroup CRC_LL_EF_Data_Management Data_Management
331   * @{
332   */
333 
334 /**
335   * @brief  Write given 32-bit data to the CRC calculator
336   * @rmtoll DR           DR            LL_CRC_FeedData32
337   * @param  CRCx CRC Instance
338   * @param  InData value to be provided to CRC calculator between between Min_Data=0 and Max_Data=0xFFFFFFFF
339   * @retval None
340   */
LL_CRC_FeedData32(CRC_TypeDef * CRCx,uint32_t InData)341 __STATIC_INLINE void LL_CRC_FeedData32(CRC_TypeDef *CRCx, uint32_t InData)
342 {
343   WRITE_REG(CRCx->DR, InData);
344 }
345 
346 /**
347   * @brief  Write given 16-bit data to the CRC calculator
348   * @rmtoll DR           DR            LL_CRC_FeedData16
349   * @param  CRCx CRC Instance
350   * @param  InData 16 bit value to be provided to CRC calculator between between Min_Data=0 and Max_Data=0xFFFF
351   * @retval None
352   */
LL_CRC_FeedData16(CRC_TypeDef * CRCx,uint16_t InData)353 __STATIC_INLINE void LL_CRC_FeedData16(CRC_TypeDef *CRCx, uint16_t InData)
354 {
355   __IO uint16_t *pReg;
356 
357   pReg = (__IO uint16_t *)(__IO void *)(&CRCx->DR);
358   *pReg = InData;
359 }
360 
361 /**
362   * @brief  Write given 8-bit data to the CRC calculator
363   * @rmtoll DR           DR            LL_CRC_FeedData8
364   * @param  CRCx CRC Instance
365   * @param  InData 8 bit value to be provided to CRC calculator between between Min_Data=0 and Max_Data=0xFF
366   * @retval None
367   */
LL_CRC_FeedData8(CRC_TypeDef * CRCx,uint8_t InData)368 __STATIC_INLINE void LL_CRC_FeedData8(CRC_TypeDef *CRCx, uint8_t InData)
369 {
370   *(uint8_t __IO *)(&CRCx->DR) = (uint8_t) InData;
371 }
372 
373 /**
374   * @brief  Return current CRC calculation result. 32 bits value is returned.
375   * @rmtoll DR           DR            LL_CRC_ReadData32
376   * @param  CRCx CRC Instance
377   * @retval Current CRC calculation result as stored in CRC_DR register (32 bits).
378   */
LL_CRC_ReadData32(CRC_TypeDef * CRCx)379 __STATIC_INLINE uint32_t LL_CRC_ReadData32(CRC_TypeDef *CRCx)
380 {
381   return (uint32_t)(READ_REG(CRCx->DR));
382 }
383 
384 /**
385   * @brief  Return current CRC calculation result. 16 bits value is returned.
386   * @note   This function is expected to be used in a 16 bits CRC polynomial size context.
387   * @rmtoll DR           DR            LL_CRC_ReadData16
388   * @param  CRCx CRC Instance
389   * @retval Current CRC calculation result as stored in CRC_DR register (16 bits).
390   */
LL_CRC_ReadData16(CRC_TypeDef * CRCx)391 __STATIC_INLINE uint16_t LL_CRC_ReadData16(CRC_TypeDef *CRCx)
392 {
393   return (uint16_t)READ_REG(CRCx->DR);
394 }
395 
396 /**
397   * @brief  Return current CRC calculation result. 8 bits value is returned.
398   * @note   This function is expected to be used in a 8 bits CRC polynomial size context.
399   * @rmtoll DR           DR            LL_CRC_ReadData8
400   * @param  CRCx CRC Instance
401   * @retval Current CRC calculation result as stored in CRC_DR register (8 bits).
402   */
LL_CRC_ReadData8(CRC_TypeDef * CRCx)403 __STATIC_INLINE uint8_t LL_CRC_ReadData8(CRC_TypeDef *CRCx)
404 {
405   return (uint8_t)READ_REG(CRCx->DR);
406 }
407 
408 /**
409   * @brief  Return current CRC calculation result. 7 bits value is returned.
410   * @note   This function is expected to be used in a 7 bits CRC polynomial size context.
411   * @rmtoll DR           DR            LL_CRC_ReadData7
412   * @param  CRCx CRC Instance
413   * @retval Current CRC calculation result as stored in CRC_DR register (7 bits).
414   */
LL_CRC_ReadData7(CRC_TypeDef * CRCx)415 __STATIC_INLINE uint8_t LL_CRC_ReadData7(CRC_TypeDef *CRCx)
416 {
417   return (uint8_t)(READ_REG(CRCx->DR) & 0x7FU);
418 }
419 
420 /**
421   * @brief  Return data stored in the Independent Data(IDR) register.
422   * @note   This register can be used as a temporary storage location.
423   * @note   Refer to the Reference Manual to get the authorized data length in bits.
424   * @rmtoll IDR          IDR           LL_CRC_Read_IDR
425   * @param  CRCx CRC Instance
426   * @retval Value stored in CRC_IDR register
427   */
LL_CRC_Read_IDR(CRC_TypeDef * CRCx)428 __STATIC_INLINE uint32_t LL_CRC_Read_IDR(CRC_TypeDef *CRCx)
429 {
430   return (uint32_t)(READ_REG(CRCx->IDR));
431 }
432 
433 /**
434   * @brief  Store data in the Independent Data(IDR) register.
435   * @note   This register can be used as a temporary storage location.
436   * @note   Refer to the Reference Manual to get the authorized data length in bits.
437   * @rmtoll IDR          IDR           LL_CRC_Write_IDR
438   * @param  CRCx CRC Instance
439   * @param  InData value to be stored in CRC_IDR register
440   * @retval None
441   */
LL_CRC_Write_IDR(CRC_TypeDef * CRCx,uint32_t InData)442 __STATIC_INLINE void LL_CRC_Write_IDR(CRC_TypeDef *CRCx, uint32_t InData)
443 {
444 #if (CRC_IDR_IDR == 0x0FFU)
445   *((uint8_t __IO *)(&CRCx->IDR)) = (uint8_t) InData;
446 #else
447   WRITE_REG(CRCx->IDR, InData);
448 #endif
449 }
450 /**
451   * @}
452   */
453 
454 #if defined(USE_FULL_LL_DRIVER)
455 /** @defgroup CRC_LL_EF_Init Initialization and de-initialization functions
456   * @{
457   */
458 
459 ErrorStatus LL_CRC_DeInit(CRC_TypeDef *CRCx);
460 
461 /**
462   * @}
463   */
464 #endif /* USE_FULL_LL_DRIVER */
465 
466 /**
467   * @}
468   */
469 
470 /**
471   * @}
472   */
473 
474 #endif /* defined(CRC) */
475 
476 /**
477   * @}
478   */
479 
480 #ifdef __cplusplus
481 }
482 #endif
483 
484 #endif /* __STM32L4xx_LL_CRC_H */
485 
486 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
487