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