stm32l4xx_ll_crc.h 17 KB

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