stm32g0xx_hal_crc_ex.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /**
  2. ******************************************************************************
  3. * @file stm32g0xx_hal_crc_ex.c
  4. * @author MCD Application Team
  5. * @brief Extended CRC HAL module driver.
  6. * This file provides firmware functions to manage the extended
  7. * functionalities of the CRC peripheral.
  8. *
  9. ******************************************************************************
  10. * @attention
  11. *
  12. * Copyright (c) 2018 STMicroelectronics.
  13. * All rights reserved.
  14. *
  15. * This software is licensed under terms that can be found in the LICENSE file
  16. * in the root directory of this software component.
  17. * If no LICENSE file comes with this software, it is provided AS-IS.
  18. *
  19. ******************************************************************************
  20. @verbatim
  21. ================================================================================
  22. ##### How to use this driver #####
  23. ================================================================================
  24. [..]
  25. (+) Set user-defined generating polynomial through HAL_CRCEx_Polynomial_Set()
  26. (+) Configure Input or Output data inversion
  27. @endverbatim
  28. ******************************************************************************
  29. */
  30. /* Includes ------------------------------------------------------------------*/
  31. #include "stm32g0xx_hal.h"
  32. /** @addtogroup STM32G0xx_HAL_Driver
  33. * @{
  34. */
  35. /** @defgroup CRCEx CRCEx
  36. * @brief CRC Extended HAL module driver
  37. * @{
  38. */
  39. #ifdef HAL_CRC_MODULE_ENABLED
  40. /* Private typedef -----------------------------------------------------------*/
  41. /* Private define ------------------------------------------------------------*/
  42. /* Private macro -------------------------------------------------------------*/
  43. /* Private variables ---------------------------------------------------------*/
  44. /* Private function prototypes -----------------------------------------------*/
  45. /* Exported functions --------------------------------------------------------*/
  46. /** @defgroup CRCEx_Exported_Functions CRC Extended Exported Functions
  47. * @{
  48. */
  49. /** @defgroup CRCEx_Exported_Functions_Group1 Extended Initialization/de-initialization functions
  50. * @brief Extended Initialization and Configuration functions.
  51. *
  52. @verbatim
  53. ===============================================================================
  54. ##### Extended configuration functions #####
  55. ===============================================================================
  56. [..] This section provides functions allowing to:
  57. (+) Configure the generating polynomial
  58. (+) Configure the input data inversion
  59. (+) Configure the output data inversion
  60. @endverbatim
  61. * @{
  62. */
  63. /**
  64. * @brief Initialize the CRC polynomial if different from default one.
  65. * @param hcrc CRC handle
  66. * @param Pol CRC generating polynomial (7, 8, 16 or 32-bit long).
  67. * This parameter is written in normal representation, e.g.
  68. * @arg for a polynomial of degree 7, X^7 + X^6 + X^5 + X^2 + 1 is written 0x65
  69. * @arg for a polynomial of degree 16, X^16 + X^12 + X^5 + 1 is written 0x1021
  70. * @param PolyLength CRC polynomial length.
  71. * This parameter can be one of the following values:
  72. * @arg @ref CRC_POLYLENGTH_7B 7-bit long CRC (generating polynomial of degree 7)
  73. * @arg @ref CRC_POLYLENGTH_8B 8-bit long CRC (generating polynomial of degree 8)
  74. * @arg @ref CRC_POLYLENGTH_16B 16-bit long CRC (generating polynomial of degree 16)
  75. * @arg @ref CRC_POLYLENGTH_32B 32-bit long CRC (generating polynomial of degree 32)
  76. * @retval HAL status
  77. */
  78. HAL_StatusTypeDef HAL_CRCEx_Polynomial_Set(CRC_HandleTypeDef *hcrc, uint32_t Pol, uint32_t PolyLength)
  79. {
  80. HAL_StatusTypeDef status = HAL_OK;
  81. uint32_t msb = 31U; /* polynomial degree is 32 at most, so msb is initialized to max value */
  82. /* Check the parameters */
  83. assert_param(IS_CRC_POL_LENGTH(PolyLength));
  84. /* Ensure that the generating polynomial is odd */
  85. if ((Pol & (uint32_t)(0x1U)) == 0U)
  86. {
  87. status = HAL_ERROR;
  88. }
  89. else
  90. {
  91. /* check polynomial definition vs polynomial size:
  92. * polynomial length must be aligned with polynomial
  93. * definition. HAL_ERROR is reported if Pol degree is
  94. * larger than that indicated by PolyLength.
  95. * Look for MSB position: msb will contain the degree of
  96. * the second to the largest polynomial member. E.g., for
  97. * X^7 + X^6 + X^5 + X^2 + 1, msb = 6. */
  98. while ((msb-- > 0U) && ((Pol & ((uint32_t)(0x1U) << (msb & 0x1FU))) == 0U))
  99. {
  100. }
  101. switch (PolyLength)
  102. {
  103. case CRC_POLYLENGTH_7B:
  104. if (msb >= HAL_CRC_LENGTH_7B)
  105. {
  106. status = HAL_ERROR;
  107. }
  108. break;
  109. case CRC_POLYLENGTH_8B:
  110. if (msb >= HAL_CRC_LENGTH_8B)
  111. {
  112. status = HAL_ERROR;
  113. }
  114. break;
  115. case CRC_POLYLENGTH_16B:
  116. if (msb >= HAL_CRC_LENGTH_16B)
  117. {
  118. status = HAL_ERROR;
  119. }
  120. break;
  121. case CRC_POLYLENGTH_32B:
  122. /* no polynomial definition vs. polynomial length issue possible */
  123. break;
  124. default:
  125. status = HAL_ERROR;
  126. break;
  127. }
  128. }
  129. if (status == HAL_OK)
  130. {
  131. /* set generating polynomial */
  132. WRITE_REG(hcrc->Instance->POL, Pol);
  133. /* set generating polynomial size */
  134. MODIFY_REG(hcrc->Instance->CR, CRC_CR_POLYSIZE, PolyLength);
  135. }
  136. /* Return function status */
  137. return status;
  138. }
  139. /**
  140. * @brief Set the Reverse Input data mode.
  141. * @param hcrc CRC handle
  142. * @param InputReverseMode Input Data inversion mode.
  143. * This parameter can be one of the following values:
  144. * @arg @ref CRC_INPUTDATA_INVERSION_NONE no change in bit order (default value)
  145. * @arg @ref CRC_INPUTDATA_INVERSION_BYTE Byte-wise bit reversal
  146. * @arg @ref CRC_INPUTDATA_INVERSION_HALFWORD HalfWord-wise bit reversal
  147. * @arg @ref CRC_INPUTDATA_INVERSION_WORD Word-wise bit reversal
  148. * @retval HAL status
  149. */
  150. HAL_StatusTypeDef HAL_CRCEx_Input_Data_Reverse(CRC_HandleTypeDef *hcrc, uint32_t InputReverseMode)
  151. {
  152. /* Check the parameters */
  153. assert_param(IS_CRC_INPUTDATA_INVERSION_MODE(InputReverseMode));
  154. /* Change CRC peripheral state */
  155. hcrc->State = HAL_CRC_STATE_BUSY;
  156. /* set input data inversion mode */
  157. MODIFY_REG(hcrc->Instance->CR, CRC_CR_REV_IN, InputReverseMode);
  158. /* Change CRC peripheral state */
  159. hcrc->State = HAL_CRC_STATE_READY;
  160. /* Return function status */
  161. return HAL_OK;
  162. }
  163. /**
  164. * @brief Set the Reverse Output data mode.
  165. * @param hcrc CRC handle
  166. * @param OutputReverseMode Output Data inversion mode.
  167. * This parameter can be one of the following values:
  168. * @arg @ref CRC_OUTPUTDATA_INVERSION_DISABLE no CRC inversion (default value)
  169. * @arg @ref CRC_OUTPUTDATA_INVERSION_ENABLE bit-level inversion (e.g. for a 8-bit CRC: 0xB5 becomes 0xAD)
  170. * @retval HAL status
  171. */
  172. HAL_StatusTypeDef HAL_CRCEx_Output_Data_Reverse(CRC_HandleTypeDef *hcrc, uint32_t OutputReverseMode)
  173. {
  174. /* Check the parameters */
  175. assert_param(IS_CRC_OUTPUTDATA_INVERSION_MODE(OutputReverseMode));
  176. /* Change CRC peripheral state */
  177. hcrc->State = HAL_CRC_STATE_BUSY;
  178. /* set output data inversion mode */
  179. MODIFY_REG(hcrc->Instance->CR, CRC_CR_REV_OUT, OutputReverseMode);
  180. /* Change CRC peripheral state */
  181. hcrc->State = HAL_CRC_STATE_READY;
  182. /* Return function status */
  183. return HAL_OK;
  184. }
  185. /**
  186. * @}
  187. */
  188. /**
  189. * @}
  190. */
  191. #endif /* HAL_CRC_MODULE_ENABLED */
  192. /**
  193. * @}
  194. */
  195. /**
  196. * @}
  197. */