stm32f0xx_crc.c 11 KB

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