ft32f0xx_crc.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /**
  2. ******************************************************************************
  3. * @file ft32f0xx_crc.c
  4. * @author FMD AE
  5. * @brief This file provides firmware functions to manage the following
  6. * functionalities of CRC computation unit peripheral:
  7. * + Configuration of the CRC computation unit
  8. * + CRC computation of one/many 32-bit data
  9. * + CRC Independent register (IDR) access
  10. * @version V1.0.0
  11. * @data 2021-07-01
  12. ******************************************************************************
  13. */
  14. /* Includes ------------------------------------------------------------------*/
  15. #include "ft32f0xx_crc.h"
  16. /**
  17. * @brief Deinitializes CRC peripheral registers to their default reset values.
  18. * @param None
  19. * @retval None
  20. */
  21. void CRC_DeInit(void)
  22. {
  23. /* Set DR register to reset value */
  24. CRC->DR = 0xFFFFFFFF;
  25. /* Reset IDR register */
  26. CRC->IDR = 0x00;
  27. /* Set INIT register to reset value */
  28. CRC->INIT = 0xFFFFFFFF;
  29. /* Reset the CRC calculation unit */
  30. CRC->CR = CRC_CR_RESET;
  31. }
  32. /**
  33. * @brief Resets the CRC calculation unit and sets INIT register content in DR register.
  34. * @param None
  35. * @retval None
  36. */
  37. void CRC_ResetDR(void)
  38. {
  39. /* Reset CRC generator */
  40. CRC->CR |= CRC_CR_RESET;
  41. }
  42. /**
  43. * @brief Selects the polynomial size. This function is only applicable for
  44. * FT32F072 devices.
  45. * @param CRC_PolSize: Specifies the polynomial size.
  46. * This parameter can be:
  47. * @arg CRC_PolSize_7: 7-bit polynomial for CRC calculation
  48. * @arg CRC_PolSize_8: 8-bit polynomial for CRC calculation
  49. * @arg CRC_PolSize_16: 16-bit polynomial for CRC calculation
  50. * @arg CRC_PolSize_32: 32-bit polynomial for CRC calculation
  51. * @retval None
  52. */
  53. //void CRC_PolynomialSizeSelect(uint32_t CRC_PolSize)
  54. //{
  55. // uint32_t tmpcr = 0;
  56. // /* Check the parameter */
  57. // assert_param(IS_CRC_POL_SIZE(CRC_PolSize));
  58. // /* Get CR register value */
  59. // tmpcr = CRC->CR;
  60. // /* Reset POL_SIZE bits */
  61. // tmpcr &= (uint32_t)~((uint32_t)CRC_CR_POLSIZE);
  62. // /* Set the polynomial size */
  63. // tmpcr |= (uint32_t)CRC_PolSize;
  64. // /* Write to CR register */
  65. // CRC->CR = (uint32_t)tmpcr;
  66. //}
  67. /**
  68. * @brief Selects the reverse operation to be performed on input data.
  69. * @param CRC_ReverseInputData: Specifies the reverse operation on input data.
  70. * This parameter can be:
  71. * @arg CRC_ReverseInputData_No: No reverse operation is performed
  72. * @arg CRC_ReverseInputData_8bits: reverse operation performed on 8 bits
  73. * @arg CRC_ReverseInputData_16bits: reverse operation performed on 16 bits
  74. * @arg CRC_ReverseInputData_32bits: reverse operation performed on 32 bits
  75. * @retval None
  76. */
  77. void CRC_ReverseInputDataSelect(uint32_t CRC_ReverseInputData)
  78. {
  79. uint32_t tmpcr = 0;
  80. /* Check the parameter */
  81. assert_param(IS_CRC_REVERSE_INPUT_DATA(CRC_ReverseInputData));
  82. /* Get CR register value */
  83. tmpcr = CRC->CR;
  84. /* Reset REV_IN bits */
  85. tmpcr &= (uint32_t)~((uint32_t)CRC_CR_REV_IN);
  86. /* Set the reverse operation */
  87. tmpcr |= (uint32_t)CRC_ReverseInputData;
  88. /* Write to CR register */
  89. CRC->CR = (uint32_t)tmpcr;
  90. }
  91. /**
  92. * @brief Enables or disable the reverse operation on output data.
  93. * The reverse operation on output data is performed on 32-bit.
  94. * @param NewState: new state of the reverse operation on output data.
  95. * This parameter can be: ENABLE or DISABLE.
  96. * @retval None
  97. */
  98. void CRC_ReverseOutputDataCmd(FunctionalState NewState)
  99. {
  100. /* Check the parameters */
  101. assert_param(IS_FUNCTIONAL_STATE(NewState));
  102. if (NewState != DISABLE)
  103. {
  104. /* Enable reverse operation on output data */
  105. CRC->CR |= CRC_CR_REV_OUT;
  106. }
  107. else
  108. {
  109. /* Disable reverse operation on output data */
  110. CRC->CR &= (uint32_t)~((uint32_t)CRC_CR_REV_OUT);
  111. }
  112. }
  113. /**
  114. * @brief Initializes the INIT register.
  115. * @note After resetting CRC calculation unit, CRC_InitValue is stored in DR register
  116. * @param CRC_InitValue: Programmable initial CRC value
  117. * @retval None
  118. */
  119. void CRC_SetInitRegister(uint32_t CRC_InitValue)
  120. {
  121. CRC->INIT = CRC_InitValue;
  122. }
  123. /**
  124. * @brief Initializes the polynomail coefficients. This function is only
  125. * applicable for FT32F072 devices.
  126. * @param CRC_Pol: Polynomial to be used for CRC calculation.
  127. * @retval None
  128. */
  129. void CRC_SetPolynomial(uint32_t CRC_Pol)
  130. {
  131. // CRC->POL = CRC_Pol;
  132. }
  133. /**
  134. * @}
  135. */
  136. /**
  137. * @brief Computes the 32-bit CRC of a given data word(32-bit).
  138. * @param CRC_Data: data word(32-bit) to compute its CRC
  139. * @retval 32-bit CRC
  140. */
  141. uint32_t CRC_CalcCRC(uint32_t CRC_Data)
  142. {
  143. CRC->DR = CRC_Data;
  144. return (CRC->DR);
  145. }
  146. /**
  147. * @brief Computes the 16-bit CRC of a given 16-bit data.
  148. * @param CRC_Data: data half-word(16-bit) to compute its CRC
  149. * @retval 16-bit CRC
  150. */
  151. uint32_t CRC_CalcCRC16bits(uint16_t CRC_Data)
  152. {
  153. *(uint16_t*)(CRC_BASE) = (uint16_t) CRC_Data;
  154. return (CRC->DR);
  155. }
  156. /**
  157. * @brief Computes the 8-bit CRC of a given 8-bit data.
  158. * @param CRC_Data: 8-bit data to compute its CRC
  159. * @retval 8-bit CRC
  160. */
  161. uint32_t CRC_CalcCRC8bits(uint8_t CRC_Data)
  162. {
  163. *(uint8_t*)(CRC_BASE) = (uint8_t) CRC_Data;
  164. return (CRC->DR);
  165. }
  166. /**
  167. * @brief Computes the 32-bit CRC of a given buffer of data word(32-bit).
  168. * @param pBuffer: pointer to the buffer containing the data to be computed
  169. * @param BufferLength: length of the buffer to be computed
  170. * @retval 32-bit CRC
  171. */
  172. uint32_t CRC_CalcBlockCRC(uint32_t pBuffer[], uint32_t BufferLength)
  173. {
  174. uint32_t index = 0;
  175. for(index = 0; index < BufferLength; index++)
  176. {
  177. CRC->DR = pBuffer[index];
  178. }
  179. return (CRC->DR);
  180. }
  181. /**
  182. * @brief Returns the current CRC value.
  183. * @param None
  184. * @retval 32-bit CRC
  185. */
  186. uint32_t CRC_GetCRC(void)
  187. {
  188. return (CRC->DR);
  189. }
  190. /**
  191. * @}
  192. */
  193. /**
  194. * @brief Stores an 8-bit data in the Independent Data(ID) register.
  195. * @param CRC_IDValue: 8-bit value to be stored in the ID register
  196. * @retval None
  197. */
  198. void CRC_SetIDRegister(uint8_t CRC_IDValue)
  199. {
  200. CRC->IDR = CRC_IDValue;
  201. }
  202. /**
  203. * @brief Returns the 8-bit data stored in the Independent Data(ID) register
  204. * @param None
  205. * @retval 8-bit value of the ID register
  206. */
  207. uint8_t CRC_GetIDRegister(void)
  208. {
  209. return (uint8_t)(CRC->IDR);
  210. }
  211. /**
  212. * @}
  213. */
  214. /**
  215. * @}
  216. */
  217. /**
  218. * @}
  219. */
  220. /**
  221. * @}
  222. */
  223. /************************ (C) COPYRIGHT FMD *****END OF FILE****/