stm32f0xx_hal_crc_ex.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /**
  2. ******************************************************************************
  3. * @file stm32f0xx_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) 2016 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 "stm32f0xx_hal.h"
  32. /** @addtogroup STM32F0xx_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. #if defined(CRC_POL_POL)
  64. /**
  65. * @brief Initialize the CRC polynomial if different from default one.
  66. * @param hcrc CRC handle
  67. * @param Pol CRC generating polynomial (7, 8, 16 or 32-bit long).
  68. * This parameter is written in normal representation, e.g.
  69. * @arg for a polynomial of degree 7, X^7 + X^6 + X^5 + X^2 + 1 is written 0x65
  70. * @arg for a polynomial of degree 16, X^16 + X^12 + X^5 + 1 is written 0x1021
  71. * @param PolyLength CRC polynomial length.
  72. * This parameter can be one of the following values:
  73. * @arg @ref CRC_POLYLENGTH_7B 7-bit long CRC (generating polynomial of degree 7)
  74. * @arg @ref CRC_POLYLENGTH_8B 8-bit long CRC (generating polynomial of degree 8)
  75. * @arg @ref CRC_POLYLENGTH_16B 16-bit long CRC (generating polynomial of degree 16)
  76. * @arg @ref CRC_POLYLENGTH_32B 32-bit long CRC (generating polynomial of degree 32)
  77. * @retval HAL status
  78. */
  79. HAL_StatusTypeDef HAL_CRCEx_Polynomial_Set(CRC_HandleTypeDef *hcrc, uint32_t Pol, uint32_t PolyLength)
  80. {
  81. HAL_StatusTypeDef status = HAL_OK;
  82. uint32_t msb = 31U; /* polynomial degree is 32 at most, so msb is initialized to max value */
  83. /* Check the parameters */
  84. assert_param(IS_CRC_POL_LENGTH(PolyLength));
  85. /* Ensure that the generating polynomial is odd */
  86. if ((Pol & (uint32_t)(0x1U)) == 0U)
  87. {
  88. status = HAL_ERROR;
  89. }
  90. else
  91. {
  92. /* check polynomial definition vs polynomial size:
  93. * polynomial length must be aligned with polynomial
  94. * definition. HAL_ERROR is reported if Pol degree is
  95. * larger than that indicated by PolyLength.
  96. * Look for MSB position: msb will contain the degree of
  97. * the second to the largest polynomial member. E.g., for
  98. * X^7 + X^6 + X^5 + X^2 + 1, msb = 6. */
  99. while ((msb-- > 0U) && ((Pol & ((uint32_t)(0x1U) << (msb & 0x1FU))) == 0U))
  100. {
  101. }
  102. switch (PolyLength)
  103. {
  104. case CRC_POLYLENGTH_7B:
  105. if (msb >= HAL_CRC_LENGTH_7B)
  106. {
  107. status = HAL_ERROR;
  108. }
  109. break;
  110. case CRC_POLYLENGTH_8B:
  111. if (msb >= HAL_CRC_LENGTH_8B)
  112. {
  113. status = HAL_ERROR;
  114. }
  115. break;
  116. case CRC_POLYLENGTH_16B:
  117. if (msb >= HAL_CRC_LENGTH_16B)
  118. {
  119. status = HAL_ERROR;
  120. }
  121. break;
  122. case CRC_POLYLENGTH_32B:
  123. /* no polynomial definition vs. polynomial length issue possible */
  124. break;
  125. default:
  126. status = HAL_ERROR;
  127. break;
  128. }
  129. }
  130. if (status == HAL_OK)
  131. {
  132. /* set generating polynomial */
  133. WRITE_REG(hcrc->Instance->POL, Pol);
  134. /* set generating polynomial size */
  135. MODIFY_REG(hcrc->Instance->CR, CRC_CR_POLYSIZE, PolyLength);
  136. }
  137. /* Return function status */
  138. return status;
  139. }
  140. #endif /* CRC_POL_POL */
  141. /**
  142. * @brief Set the Reverse Input data mode.
  143. * @param hcrc CRC handle
  144. * @param InputReverseMode Input Data inversion mode.
  145. * This parameter can be one of the following values:
  146. * @arg @ref CRC_INPUTDATA_INVERSION_NONE no change in bit order (default value)
  147. * @arg @ref CRC_INPUTDATA_INVERSION_BYTE Byte-wise bit reversal
  148. * @arg @ref CRC_INPUTDATA_INVERSION_HALFWORD HalfWord-wise bit reversal
  149. * @arg @ref CRC_INPUTDATA_INVERSION_WORD Word-wise bit reversal
  150. * @retval HAL status
  151. */
  152. HAL_StatusTypeDef HAL_CRCEx_Input_Data_Reverse(CRC_HandleTypeDef *hcrc, uint32_t InputReverseMode)
  153. {
  154. /* Check the parameters */
  155. assert_param(IS_CRC_INPUTDATA_INVERSION_MODE(InputReverseMode));
  156. /* Change CRC peripheral state */
  157. hcrc->State = HAL_CRC_STATE_BUSY;
  158. /* set input data inversion mode */
  159. MODIFY_REG(hcrc->Instance->CR, CRC_CR_REV_IN, InputReverseMode);
  160. /* Change CRC peripheral state */
  161. hcrc->State = HAL_CRC_STATE_READY;
  162. /* Return function status */
  163. return HAL_OK;
  164. }
  165. /**
  166. * @brief Set the Reverse Output data mode.
  167. * @param hcrc CRC handle
  168. * @param OutputReverseMode Output Data inversion mode.
  169. * This parameter can be one of the following values:
  170. * @arg @ref CRC_OUTPUTDATA_INVERSION_DISABLE no CRC inversion (default value)
  171. * @arg @ref CRC_OUTPUTDATA_INVERSION_ENABLE bit-level inversion (e.g. for a 8-bit CRC: 0xB5 becomes 0xAD)
  172. * @retval HAL status
  173. */
  174. HAL_StatusTypeDef HAL_CRCEx_Output_Data_Reverse(CRC_HandleTypeDef *hcrc, uint32_t OutputReverseMode)
  175. {
  176. /* Check the parameters */
  177. assert_param(IS_CRC_OUTPUTDATA_INVERSION_MODE(OutputReverseMode));
  178. /* Change CRC peripheral state */
  179. hcrc->State = HAL_CRC_STATE_BUSY;
  180. /* set output data inversion mode */
  181. MODIFY_REG(hcrc->Instance->CR, CRC_CR_REV_OUT, OutputReverseMode);
  182. /* Change CRC peripheral state */
  183. hcrc->State = HAL_CRC_STATE_READY;
  184. /* Return function status */
  185. return HAL_OK;
  186. }
  187. /**
  188. * @}
  189. */
  190. /**
  191. * @}
  192. */
  193. #endif /* HAL_CRC_MODULE_ENABLED */
  194. /**
  195. * @}
  196. */
  197. /**
  198. * @}
  199. */