py32f002b_hal_crc.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. /**
  2. ******************************************************************************
  3. * @file py32f002b_hal_crc.c
  4. * @author MCU Application Team
  5. * @brief CRC HAL module driver.
  6. * This file provides firmware functions to manage the following
  7. * functionalities of the Cyclic Redundancy Check (CRC) peripheral:
  8. * + Initialization and de-initialization functions
  9. * + Peripheral Control functions
  10. * + Peripheral State functions
  11. *
  12. @verbatim
  13. ===============================================================================
  14. ##### How to use this driver #####
  15. ===============================================================================
  16. [..]
  17. (+) Enable CRC AHB clock using __HAL_RCC_CRC_CLK_ENABLE();
  18. (+) Initialize CRC calculator
  19. (++) specify generating polynomial (peripheral default or non-default one)
  20. (++) specify initialization value (peripheral default or non-default one)
  21. (++) specify input data format
  22. (++) specify input or output data inversion mode if any
  23. (+) Use HAL_CRC_Accumulate() function to compute the CRC value of the
  24. input data buffer starting with the previously computed CRC as
  25. initialization value
  26. (+) Use HAL_CRC_Calculate() function to compute the CRC value of the
  27. input data buffer starting with the defined initialization value
  28. (default or non-default) to initiate CRC calculation
  29. @endverbatim
  30. ******************************************************************************
  31. * @attention
  32. *
  33. * <h2><center>&copy; Copyright (c) 2023 Puya Semiconductor Co.
  34. * All rights reserved.</center></h2>
  35. *
  36. * This software component is licensed by Puya under BSD 3-Clause license,
  37. * the "License"; You may not use this file except in compliance with the
  38. * License. You may obtain a copy of the License at:
  39. * opensource.org/licenses/BSD-3-Clause
  40. *
  41. ******************************************************************************
  42. * @attention
  43. *
  44. * <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
  45. * All rights reserved.</center></h2>
  46. *
  47. * This software component is licensed by ST under BSD 3-Clause license,
  48. * the "License"; You may not use this file except in compliance with the
  49. * License. You may obtain a copy of the License at:
  50. * opensource.org/licenses/BSD-3-Clause
  51. *
  52. ******************************************************************************
  53. */
  54. /* Includes ------------------------------------------------------------------*/
  55. #include "py32f0xx_hal.h"
  56. /** @addtogroup PY32F002B_HAL_Driver
  57. * @{
  58. */
  59. /** @defgroup CRC CRC
  60. * @brief CRC HAL module driver.
  61. * @{
  62. */
  63. #ifdef HAL_CRC_MODULE_ENABLED
  64. /* Private typedef -----------------------------------------------------------*/
  65. /* Private define ------------------------------------------------------------*/
  66. /* Private macro -------------------------------------------------------------*/
  67. /* Private variables ---------------------------------------------------------*/
  68. /* Private function prototypes -----------------------------------------------*/
  69. /* Exported functions --------------------------------------------------------*/
  70. /** @defgroup CRC_Exported_Functions CRC Exported Functions
  71. * @{
  72. */
  73. /** @defgroup CRC_Exported_Functions_Group1 Initialization and de-initialization functions
  74. * @brief Initialization and Configuration functions.
  75. *
  76. @verbatim
  77. ===============================================================================
  78. ##### Initialization and de-initialization functions #####
  79. ===============================================================================
  80. [..] This section provides functions allowing to:
  81. (+) Initialize the CRC according to the specified parameters
  82. in the CRC_InitTypeDef and create the associated handle
  83. (+) DeInitialize the CRC peripheral
  84. (+) Initialize the CRC MSP (MCU Specific Package)
  85. (+) DeInitialize the CRC MSP
  86. @endverbatim
  87. * @{
  88. */
  89. /**
  90. * @brief Initialize the CRC according to the specified
  91. * parameters in the CRC_InitTypeDef and create the associated handle.
  92. * @param hcrc CRC handle
  93. * @retval HAL status
  94. */
  95. HAL_StatusTypeDef HAL_CRC_Init(CRC_HandleTypeDef *hcrc)
  96. {
  97. /* Check the CRC handle allocation */
  98. if (hcrc == NULL)
  99. {
  100. return HAL_ERROR;
  101. }
  102. /* Check the parameters */
  103. assert_param(IS_CRC_ALL_INSTANCE(hcrc->Instance));
  104. if (hcrc->State == HAL_CRC_STATE_RESET)
  105. {
  106. /* Allocate lock resource and initialize it */
  107. hcrc->Lock = HAL_UNLOCKED;
  108. /* Init the low level hardware */
  109. HAL_CRC_MspInit(hcrc);
  110. }
  111. /* Change CRC peripheral state */
  112. hcrc->State = HAL_CRC_STATE_READY;
  113. /* Return function status */
  114. return HAL_OK;
  115. }
  116. /**
  117. * @brief DeInitialize the CRC peripheral.
  118. * @param hcrc CRC handle
  119. * @retval HAL status
  120. */
  121. HAL_StatusTypeDef HAL_CRC_DeInit(CRC_HandleTypeDef *hcrc)
  122. {
  123. /* Check the CRC handle allocation */
  124. if (hcrc == NULL)
  125. {
  126. return HAL_ERROR;
  127. }
  128. /* Check the parameters */
  129. assert_param(IS_CRC_ALL_INSTANCE(hcrc->Instance));
  130. /* Check the CRC peripheral state */
  131. if (hcrc->State == HAL_CRC_STATE_BUSY)
  132. {
  133. return HAL_BUSY;
  134. }
  135. /* Change CRC peripheral state */
  136. hcrc->State = HAL_CRC_STATE_BUSY;
  137. /* Reset CRC calculation unit */
  138. __HAL_CRC_DR_RESET(hcrc);
  139. /* Reset IDR register content */
  140. CLEAR_BIT(hcrc->Instance->IDR, CRC_IDR_IDR);
  141. /* DeInit the low level hardware */
  142. HAL_CRC_MspDeInit(hcrc);
  143. /* Change CRC peripheral state */
  144. hcrc->State = HAL_CRC_STATE_RESET;
  145. /* Process unlocked */
  146. __HAL_UNLOCK(hcrc);
  147. /* Return function status */
  148. return HAL_OK;
  149. }
  150. /**
  151. * @brief Initializes the CRC MSP.
  152. * @param hcrc CRC handle
  153. * @retval None
  154. */
  155. __weak void HAL_CRC_MspInit(CRC_HandleTypeDef *hcrc)
  156. {
  157. /* Prevent unused argument(s) compilation warning */
  158. UNUSED(hcrc);
  159. /* NOTE : This function should not be modified, when the callback is needed,
  160. the HAL_CRC_MspInit can be implemented in the user file
  161. */
  162. }
  163. /**
  164. * @brief DeInitialize the CRC MSP.
  165. * @param hcrc CRC handle
  166. * @retval None
  167. */
  168. __weak void HAL_CRC_MspDeInit(CRC_HandleTypeDef *hcrc)
  169. {
  170. /* Prevent unused argument(s) compilation warning */
  171. UNUSED(hcrc);
  172. /* NOTE : This function should not be modified, when the callback is needed,
  173. the HAL_CRC_MspDeInit can be implemented in the user file
  174. */
  175. }
  176. /**
  177. * @}
  178. */
  179. /** @defgroup CRC_Exported_Functions_Group2 Peripheral Control functions
  180. * @brief management functions.
  181. *
  182. @verbatim
  183. ===============================================================================
  184. ##### Peripheral Control functions #####
  185. ===============================================================================
  186. [..] This section provides functions allowing to:
  187. (+) compute the 32-bit CRC value of a 32-bit data buffer
  188. using combination of the previous CRC value and the new one.
  189. [..] or
  190. (+) compute the 32-bit CRC value of a 32-bit data buffer
  191. independently of the previous CRC value.
  192. @endverbatim
  193. * @{
  194. */
  195. /**
  196. * @brief Compute the 32-bit CRC value of a 32-bit data buffer
  197. * starting with the previously computed CRC as initialization value.
  198. * @param hcrc CRC handle
  199. * @param pBuffer pointer to the input data buffer.
  200. * @param BufferLength input data buffer length (number of uint32_t words).
  201. * @retval uint32_t CRC (returned value LSBs for CRC shorter than 32 bits)
  202. */
  203. uint32_t HAL_CRC_Accumulate(CRC_HandleTypeDef *hcrc, uint32_t pBuffer[], uint32_t BufferLength)
  204. {
  205. uint32_t index; /* CRC input data buffer index */
  206. uint32_t temp = 0U; /* CRC output (read from hcrc->Instance->DR register) */
  207. /* Change CRC peripheral state */
  208. hcrc->State = HAL_CRC_STATE_BUSY;
  209. /* Enter Data to the CRC calculator */
  210. for (index = 0U; index < BufferLength; index++)
  211. {
  212. hcrc->Instance->DR = pBuffer[index];
  213. }
  214. temp = hcrc->Instance->DR;
  215. /* Change CRC peripheral state */
  216. hcrc->State = HAL_CRC_STATE_READY;
  217. /* Return the CRC computed value */
  218. return temp;
  219. }
  220. /**
  221. * @brief Compute the 32-bit CRC value of a 32-bit data buffer
  222. * starting with hcrc->Instance->INIT as initialization value.
  223. * @param hcrc CRC handle
  224. * @param pBuffer pointer to the input data buffer.
  225. * @param BufferLength input data buffer length (number of uint32_t words).
  226. * @retval uint32_t CRC (returned value LSBs for CRC shorter than 32 bits)
  227. */
  228. uint32_t HAL_CRC_Calculate(CRC_HandleTypeDef *hcrc, uint32_t pBuffer[], uint32_t BufferLength)
  229. {
  230. uint32_t index; /* CRC input data buffer index */
  231. uint32_t temp = 0U; /* CRC output (read from hcrc->Instance->DR register) */
  232. /* Change CRC peripheral state */
  233. hcrc->State = HAL_CRC_STATE_BUSY;
  234. /* Reset CRC Calculation Unit (hcrc->Instance->INIT is
  235. * written in hcrc->Instance->DR) */
  236. __HAL_CRC_DR_RESET(hcrc);
  237. /* Enter 32-bit input data to the CRC calculator */
  238. for (index = 0U; index < BufferLength; index++)
  239. {
  240. hcrc->Instance->DR = pBuffer[index];
  241. }
  242. temp = hcrc->Instance->DR;
  243. /* Change CRC peripheral state */
  244. hcrc->State = HAL_CRC_STATE_READY;
  245. /* Return the CRC computed value */
  246. return temp;
  247. }
  248. /**
  249. * @}
  250. */
  251. /** @defgroup CRC_Exported_Functions_Group3 Peripheral State functions
  252. * @brief Peripheral State functions.
  253. *
  254. @verbatim
  255. ===============================================================================
  256. ##### Peripheral State functions #####
  257. ===============================================================================
  258. [..]
  259. This subsection permits to get in run-time the status of the peripheral.
  260. @endverbatim
  261. * @{
  262. */
  263. /**
  264. * @brief Return the CRC handle state.
  265. * @param hcrc CRC handle
  266. * @retval HAL state
  267. */
  268. HAL_CRC_StateTypeDef HAL_CRC_GetState(CRC_HandleTypeDef *hcrc)
  269. {
  270. /* Return CRC handle state */
  271. return hcrc->State;
  272. }
  273. /**
  274. * @}
  275. */
  276. /**
  277. * @}
  278. */
  279. #endif /* HAL_CRC_MODULE_ENABLED */
  280. /**
  281. * @}
  282. */
  283. /**
  284. * @}
  285. */
  286. /************************ (C) COPYRIGHT Puya *****END OF FILE****/