stm32f0xx_crc.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /**
  2. ******************************************************************************
  3. * @file stm32f0xx_crc.c
  4. * @author MCD Application Team
  5. * @version V1.0.0
  6. * @date 23-March-2012
  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 2012 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. /* Reset IDR register */
  89. CRC->IDR = 0x00;
  90. /* Set INIT register to reset value */
  91. CRC->INIT = 0xFFFFFFFF;
  92. /* Reset the CRC calculation unit */
  93. CRC->CR = CRC_CR_RESET;
  94. }
  95. /**
  96. * @brief Resets the CRC calculation unit and sets INIT register content in DR register.
  97. * @param None
  98. * @retval None
  99. */
  100. void CRC_ResetDR(void)
  101. {
  102. /* Reset CRC generator */
  103. CRC->CR = CRC_CR_RESET;
  104. }
  105. /**
  106. * @brief Selects the reverse operation to be performed on input data.
  107. * @param CRC_ReverseInputData: Specifies the reverse operation on input data.
  108. * This parameter can be:
  109. * @arg CRC_ReverseInputData_No: No reverse operation is performed
  110. * @arg CRC_ReverseInputData_8bits: reverse operation performed on 8 bits
  111. * @arg CRC_ReverseInputData_16bits: reverse operation performed on 16 bits
  112. * @arg CRC_ReverseInputData_32bits: reverse operation performed on 32 bits
  113. * @retval None
  114. */
  115. void CRC_ReverseInputDataSelect(uint32_t CRC_ReverseInputData)
  116. {
  117. uint32_t tmpcr = 0;
  118. /* Check the parameter */
  119. assert_param(IS_CRC_REVERSE_INPUT_DATA(CRC_ReverseInputData));
  120. /* Get CR register value */
  121. tmpcr = CRC->CR;
  122. /* Reset REV_IN bits */
  123. tmpcr &= (uint32_t)~((uint32_t)CRC_CR_REV_IN);
  124. /* Set the reverse operation */
  125. tmpcr |= (uint32_t)CRC_ReverseInputData;
  126. /* Write to CR register */
  127. CRC->CR = (uint32_t)tmpcr;
  128. }
  129. /**
  130. * @brief Enables or disable the reverse operation on output data.
  131. * The reverse operation on output data is performed on 32-bit.
  132. * @param NewState: new state of the reverse operation on output data.
  133. * This parameter can be: ENABLE or DISABLE.
  134. * @retval None
  135. */
  136. void CRC_ReverseOutputDataCmd(FunctionalState NewState)
  137. {
  138. /* Check the parameters */
  139. assert_param(IS_FUNCTIONAL_STATE(NewState));
  140. if (NewState != DISABLE)
  141. {
  142. /* Enable reverse operation on output data */
  143. CRC->CR |= CRC_CR_REV_OUT;
  144. }
  145. else
  146. {
  147. /* Disable reverse operation on output data */
  148. CRC->CR &= (uint32_t)~((uint32_t)CRC_CR_REV_OUT);
  149. }
  150. }
  151. /**
  152. * @brief Initializes the INIT register.
  153. * @note After resetting CRC calculation unit, CRC_InitValue is stored in DR register
  154. * @param CRC_InitValue: Programmable initial CRC value
  155. * @retval None
  156. */
  157. void CRC_SetInitRegister(uint32_t CRC_InitValue)
  158. {
  159. CRC->INIT = CRC_InitValue;
  160. }
  161. /**
  162. * @}
  163. */
  164. /** @defgroup CRC_Group2 CRC computation of one/many 32-bit data functions
  165. * @brief CRC computation of one/many 32-bit data functions
  166. *
  167. @verbatim
  168. ===============================================================================
  169. ##### CRC computation functions #####
  170. ===============================================================================
  171. @endverbatim
  172. * @{
  173. */
  174. /**
  175. * @brief Computes the 32-bit CRC of a given data word(32-bit).
  176. * @param CRC_Data: data word(32-bit) to compute its CRC
  177. * @retval 32-bit CRC
  178. */
  179. uint32_t CRC_CalcCRC(uint32_t CRC_Data)
  180. {
  181. CRC->DR = CRC_Data;
  182. return (CRC->DR);
  183. }
  184. /**
  185. * @brief Computes the 32-bit CRC of a given buffer of data word(32-bit).
  186. * @param pBuffer: pointer to the buffer containing the data to be computed
  187. * @param BufferLength: length of the buffer to be computed
  188. * @retval 32-bit CRC
  189. */
  190. uint32_t CRC_CalcBlockCRC(uint32_t pBuffer[], uint32_t BufferLength)
  191. {
  192. uint32_t index = 0;
  193. for(index = 0; index < BufferLength; index++)
  194. {
  195. CRC->DR = pBuffer[index];
  196. }
  197. return (CRC->DR);
  198. }
  199. /**
  200. * @brief Returns the current CRC value.
  201. * @param None
  202. * @retval 32-bit CRC
  203. */
  204. uint32_t CRC_GetCRC(void)
  205. {
  206. return (CRC->DR);
  207. }
  208. /**
  209. * @}
  210. */
  211. /** @defgroup CRC_Group3 CRC Independent Register (IDR) access functions
  212. * @brief CRC Independent Register (IDR) access (write/read) functions
  213. *
  214. @verbatim
  215. ===============================================================================
  216. ##### CRC Independent Register (IDR) access functions #####
  217. ===============================================================================
  218. @endverbatim
  219. * @{
  220. */
  221. /**
  222. * @brief Stores an 8-bit data in the Independent Data(ID) register.
  223. * @param CRC_IDValue: 8-bit value to be stored in the ID register
  224. * @retval None
  225. */
  226. void CRC_SetIDRegister(uint8_t CRC_IDValue)
  227. {
  228. CRC->IDR = CRC_IDValue;
  229. }
  230. /**
  231. * @brief Returns the 8-bit data stored in the Independent Data(ID) register
  232. * @param None
  233. * @retval 8-bit value of the ID register
  234. */
  235. uint8_t CRC_GetIDRegister(void)
  236. {
  237. return (CRC->IDR);
  238. }
  239. /**
  240. * @}
  241. */
  242. /**
  243. * @}
  244. */
  245. /**
  246. * @}
  247. */
  248. /**
  249. * @}
  250. */
  251. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/