You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

226 lines
7.3 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32l0xx_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. @verbatim
  10. ================================================================================
  11. ##### How to use this driver #####
  12. ================================================================================
  13. [..]
  14. (+) Set user-defined generating polynomial thru HAL_CRCEx_Polynomial_Set()
  15. (+) Configure Input or Output data inversion
  16. @endverbatim
  17. ******************************************************************************
  18. * @attention
  19. *
  20. * <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
  21. * All rights reserved.</center></h2>
  22. *
  23. * This software component is licensed by ST under BSD 3-Clause license,
  24. * the "License"; You may not use this file except in compliance with the
  25. * License. You may obtain a copy of the License at:
  26. * opensource.org/licenses/BSD-3-Clause
  27. *
  28. ******************************************************************************
  29. */
  30. /* Includes ------------------------------------------------------------------*/
  31. #include "stm32l0xx_hal.h"
  32. /** @addtogroup STM32L0xx_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. /* check polynomial definition vs polynomial size:
  85. * polynomial length must be aligned with polynomial
  86. * definition. HAL_ERROR is reported if Pol degree is
  87. * larger than that indicated by PolyLength.
  88. * Look for MSB position: msb will contain the degree of
  89. * the second to the largest polynomial member. E.g., for
  90. * X^7 + X^6 + X^5 + X^2 + 1, msb = 6. */
  91. while ((msb-- > 0U) && ((Pol & ((uint32_t)(0x1U) << (msb & 0x1FU))) == 0U))
  92. {
  93. }
  94. switch (PolyLength)
  95. {
  96. case CRC_POLYLENGTH_7B:
  97. if (msb >= HAL_CRC_LENGTH_7B)
  98. {
  99. status = HAL_ERROR;
  100. }
  101. break;
  102. case CRC_POLYLENGTH_8B:
  103. if (msb >= HAL_CRC_LENGTH_8B)
  104. {
  105. status = HAL_ERROR;
  106. }
  107. break;
  108. case CRC_POLYLENGTH_16B:
  109. if (msb >= HAL_CRC_LENGTH_16B)
  110. {
  111. status = HAL_ERROR;
  112. }
  113. break;
  114. case CRC_POLYLENGTH_32B:
  115. /* no polynomial definition vs. polynomial length issue possible */
  116. break;
  117. default:
  118. status = HAL_ERROR;
  119. break;
  120. }
  121. if (status == HAL_OK)
  122. {
  123. /* set generating polynomial */
  124. WRITE_REG(hcrc->Instance->POL, Pol);
  125. /* set generating polynomial size */
  126. MODIFY_REG(hcrc->Instance->CR, CRC_CR_POLYSIZE, PolyLength);
  127. }
  128. /* Return function status */
  129. return status;
  130. }
  131. /**
  132. * @brief Set the Reverse Input data mode.
  133. * @param hcrc CRC handle
  134. * @param InputReverseMode Input Data inversion mode.
  135. * This parameter can be one of the following values:
  136. * @arg @ref CRC_INPUTDATA_INVERSION_NONE no change in bit order (default value)
  137. * @arg @ref CRC_INPUTDATA_INVERSION_BYTE Byte-wise bit reversal
  138. * @arg @ref CRC_INPUTDATA_INVERSION_HALFWORD HalfWord-wise bit reversal
  139. * @arg @ref CRC_INPUTDATA_INVERSION_WORD Word-wise bit reversal
  140. * @retval HAL status
  141. */
  142. HAL_StatusTypeDef HAL_CRCEx_Input_Data_Reverse(CRC_HandleTypeDef *hcrc, uint32_t InputReverseMode)
  143. {
  144. /* Check the parameters */
  145. assert_param(IS_CRC_INPUTDATA_INVERSION_MODE(InputReverseMode));
  146. /* Change CRC peripheral state */
  147. hcrc->State = HAL_CRC_STATE_BUSY;
  148. /* set input data inversion mode */
  149. MODIFY_REG(hcrc->Instance->CR, CRC_CR_REV_IN, InputReverseMode);
  150. /* Change CRC peripheral state */
  151. hcrc->State = HAL_CRC_STATE_READY;
  152. /* Return function status */
  153. return HAL_OK;
  154. }
  155. /**
  156. * @brief Set the Reverse Output data mode.
  157. * @param hcrc CRC handle
  158. * @param OutputReverseMode Output Data inversion mode.
  159. * This parameter can be one of the following values:
  160. * @arg @ref CRC_OUTPUTDATA_INVERSION_DISABLE no CRC inversion (default value)
  161. * @arg @ref CRC_OUTPUTDATA_INVERSION_ENABLE bit-level inversion (e.g. for a 8-bit CRC: 0xB5 becomes 0xAD)
  162. * @retval HAL status
  163. */
  164. HAL_StatusTypeDef HAL_CRCEx_Output_Data_Reverse(CRC_HandleTypeDef *hcrc, uint32_t OutputReverseMode)
  165. {
  166. /* Check the parameters */
  167. assert_param(IS_CRC_OUTPUTDATA_INVERSION_MODE(OutputReverseMode));
  168. /* Change CRC peripheral state */
  169. hcrc->State = HAL_CRC_STATE_BUSY;
  170. /* set output data inversion mode */
  171. MODIFY_REG(hcrc->Instance->CR, CRC_CR_REV_OUT, OutputReverseMode);
  172. /* Change CRC peripheral state */
  173. hcrc->State = HAL_CRC_STATE_READY;
  174. /* Return function status */
  175. return HAL_OK;
  176. }
  177. /**
  178. * @}
  179. */
  180. /**
  181. * @}
  182. */
  183. #endif /* HAL_CRC_MODULE_ENABLED */
  184. /**
  185. * @}
  186. */
  187. /**
  188. * @}
  189. */
  190. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/