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.
 
 
 

271 lines
10 KiB

  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 following
  7. * functionalities of the CRC peripheral:
  8. * + Extended initialization functions
  9. *
  10. @verbatim
  11. ================================================================================
  12. ##### How to use this driver #####
  13. ================================================================================
  14. [..]
  15. (+) Extended initialization
  16. (+) Set or not user-defined generating
  17. polynomial other than default one
  18. @endverbatim
  19. ******************************************************************************
  20. * @attention
  21. *
  22. * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
  23. *
  24. * Redistribution and use in source and binary forms, with or without modification,
  25. * are permitted provided that the following conditions are met:
  26. * 1. Redistributions of source code must retain the above copyright notice,
  27. * this list of conditions and the following disclaimer.
  28. * 2. Redistributions in binary form must reproduce the above copyright notice,
  29. * this list of conditions and the following disclaimer in the documentation
  30. * and/or other materials provided with the distribution.
  31. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  32. * may be used to endorse or promote products derived from this software
  33. * without specific prior written permission.
  34. *
  35. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  36. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  37. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  38. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  39. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  40. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  41. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  42. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  43. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  44. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  45. *
  46. ******************************************************************************
  47. */
  48. /* Includes ------------------------------------------------------------------*/
  49. #include "stm32f0xx_hal.h"
  50. /** @addtogroup STM32F0xx_HAL_Driver
  51. * @{
  52. */
  53. /** @defgroup CRCEx CRCEx
  54. * @brief CRC Extended HAL module driver
  55. * @{
  56. */
  57. #ifdef HAL_CRC_MODULE_ENABLED
  58. /* Private typedef -----------------------------------------------------------*/
  59. /* Private define ------------------------------------------------------------*/
  60. /* Private macro -------------------------------------------------------------*/
  61. /* Private variables ---------------------------------------------------------*/
  62. /* Private function prototypes -----------------------------------------------*/
  63. /* Private functions ---------------------------------------------------------*/
  64. /** @defgroup CRCEx_Exported_Functions CRCEx Exported Functions
  65. * @{
  66. */
  67. /** @defgroup CRCEx_Exported_Functions_Group1 Extended Initialization/de-initialization functions
  68. * @brief Extended Initialization and Configuration functions.
  69. *
  70. @verbatim
  71. ===============================================================================
  72. ##### Initialization and Configuration functions #####
  73. ===============================================================================
  74. [..] This section provides functions allowing to:
  75. (+) Initialize the CRC generating polynomial: if programmable polynomial
  76. feature is applicable to device, set default or non-default generating
  77. polynomial according to hcrc->Init.DefaultPolynomialUse parameter.
  78. If feature is non-applicable to device in use, HAL_CRCEx_Init straight
  79. away reports HAL_OK.
  80. (+) Set the generating polynomial
  81. @endverbatim
  82. * @{
  83. */
  84. /**
  85. * @brief Extended initialization to set generating polynomial
  86. * @param hcrc CRC handle
  87. * @retval HAL status
  88. */
  89. HAL_StatusTypeDef HAL_CRCEx_Init(CRC_HandleTypeDef *hcrc)
  90. {
  91. #if defined(STM32F071xB) || defined(STM32F072xB) || defined(STM32F078xx) || defined(STM32F091xC) || defined (STM32F098xx)
  92. /* check whether or not non-default generating polynomial has been
  93. * picked up by user */
  94. assert_param(IS_DEFAULT_POLYNOMIAL(hcrc->Init.DefaultPolynomialUse));
  95. if (hcrc->Init.DefaultPolynomialUse == DEFAULT_POLYNOMIAL_ENABLE)
  96. {
  97. /* initialize IP with default generating polynomial */
  98. WRITE_REG(hcrc->Instance->POL, DEFAULT_CRC32_POLY);
  99. MODIFY_REG(hcrc->Instance->CR, CRC_CR_POLYSIZE, CRC_POLYLENGTH_32B);
  100. }
  101. else
  102. {
  103. /* initialize CRC IP with generating polynomial defined by user */
  104. if (HAL_CRCEx_Polynomial_Set(hcrc, hcrc->Init.GeneratingPolynomial, hcrc->Init.CRCLength) != HAL_OK)
  105. {
  106. return HAL_ERROR;
  107. }
  108. }
  109. #endif /* defined(STM32F071xB) || defined(STM32F072xB) || defined(STM32F078xx) || defined(STM32F091xC) || defined (STM32F098xx) */
  110. return HAL_OK;
  111. }
  112. /**
  113. * @brief Set the Reverse Input data mode.
  114. * @param hcrc CRC handle
  115. * @param InputReverseMode Input Data inversion mode
  116. * This parameter can be one of the following values:
  117. * @arg CRC_INPUTDATA_NOINVERSION: no change in bit order (default value)
  118. * @arg CRC_INPUTDATA_INVERSION_BYTE: Byte-wise bit reversal
  119. * @arg CRC_INPUTDATA_INVERSION_HALFWORD: HalfWord-wise bit reversal
  120. * @arg CRC_INPUTDATA_INVERSION_WORD: Word-wise bit reversal
  121. * @retval HAL status
  122. */
  123. HAL_StatusTypeDef HAL_CRCEx_Input_Data_Reverse(CRC_HandleTypeDef *hcrc, uint32_t InputReverseMode)
  124. {
  125. /* Check the parameters */
  126. assert_param(IS_CRC_INPUTDATA_INVERSION_MODE(InputReverseMode));
  127. /* Change CRC peripheral state */
  128. hcrc->State = HAL_CRC_STATE_BUSY;
  129. /* set input data inversion mode */
  130. MODIFY_REG(hcrc->Instance->CR, CRC_CR_REV_IN, InputReverseMode);
  131. /* Change CRC peripheral state */
  132. hcrc->State = HAL_CRC_STATE_READY;
  133. /* Return function status */
  134. return HAL_OK;
  135. }
  136. /**
  137. * @brief Set the Reverse Output data mode.
  138. * @param hcrc CRC handle
  139. * @param OutputReverseMode Output Data inversion mode
  140. * This parameter can be one of the following values:
  141. * @arg CRC_OUTPUTDATA_INVERSION_DISABLE: no CRC inversion (default value)
  142. * @arg CRC_OUTPUTDATA_INVERSION_ENABLE: bit-level inversion (e.g for a 8-bit CRC: 0xB5 becomes 0xAD)
  143. * @retval HAL status
  144. */
  145. HAL_StatusTypeDef HAL_CRCEx_Output_Data_Reverse(CRC_HandleTypeDef *hcrc, uint32_t OutputReverseMode)
  146. {
  147. /* Check the parameters */
  148. assert_param(IS_CRC_OUTPUTDATA_INVERSION_MODE(OutputReverseMode));
  149. /* Change CRC peripheral state */
  150. hcrc->State = HAL_CRC_STATE_BUSY;
  151. /* set output data inversion mode */
  152. MODIFY_REG(hcrc->Instance->CR, CRC_CR_REV_OUT, OutputReverseMode);
  153. /* Change CRC peripheral state */
  154. hcrc->State = HAL_CRC_STATE_READY;
  155. /* Return function status */
  156. return HAL_OK;
  157. }
  158. #if defined (STM32F071xB) || defined (STM32F072xB) || defined (STM32F078xx) || defined (STM32F091xC) || defined (STM32F098xx)
  159. /**
  160. * @brief Initializes the CRC polynomial if different from default one.
  161. * @param hcrc CRC handle
  162. * @param Pol CRC generating polynomial (7, 8, 16 or 32-bit long)
  163. * This parameter is written in normal representation, e.g.
  164. * for a polynomial of degree 7, X^7 + X^6 + X^5 + X^2 + 1 is written 0x65
  165. * for a polynomial of degree 16, X^16 + X^12 + X^5 + 1 is written 0x1021
  166. * @param PolyLength CRC polynomial length
  167. * This parameter can be one of the following values:
  168. * @arg CRC_POLYLENGTH_7B: 7-bit long CRC (generating polynomial of degree 7)
  169. * @arg CRC_POLYLENGTH_8B: 8-bit long CRC (generating polynomial of degree 8)
  170. * @arg CRC_POLYLENGTH_16B: 16-bit long CRC (generating polynomial of degree 16)
  171. * @arg CRC_POLYLENGTH_32B: 32-bit long CRC (generating polynomial of degree 32)
  172. * @retval HAL status
  173. */
  174. HAL_StatusTypeDef HAL_CRCEx_Polynomial_Set(CRC_HandleTypeDef *hcrc, uint32_t Pol, uint32_t PolyLength)
  175. {
  176. uint32_t msb = 31U; /* polynomial degree is 32 at most, so msb is initialized to max value */
  177. /* Check the parameters */
  178. assert_param(IS_CRC_POL_LENGTH(PolyLength));
  179. /* check polynomial definition vs polynomial size:
  180. * polynomial length must be aligned with polynomial
  181. * definition. HAL_ERROR is reported if Pol degree is
  182. * larger than that indicated by PolyLength.
  183. * Look for MSB position: msb will contain the degree of
  184. * the second to the largest polynomial member. E.g., for
  185. * X^7 + X^6 + X^5 + X^2 + 1, msb = 6. */
  186. while (((Pol & (1U << msb)) == 0U) && (msb-- > 0U))
  187. {}
  188. switch (PolyLength)
  189. {
  190. case CRC_POLYLENGTH_7B:
  191. if (msb >= HAL_CRC_LENGTH_7B)
  192. {
  193. return HAL_ERROR;
  194. }
  195. break;
  196. case CRC_POLYLENGTH_8B:
  197. if (msb >= HAL_CRC_LENGTH_8B)
  198. {
  199. return HAL_ERROR;
  200. }
  201. break;
  202. case CRC_POLYLENGTH_16B:
  203. if (msb >= HAL_CRC_LENGTH_16B)
  204. {
  205. return HAL_ERROR;
  206. }
  207. break;
  208. case CRC_POLYLENGTH_32B:
  209. /* no polynomial definition vs. polynomial length issue possible */
  210. break;
  211. default:
  212. break;
  213. }
  214. /* set generating polynomial */
  215. WRITE_REG(hcrc->Instance->POL, Pol);
  216. /* set generating polynomial size */
  217. MODIFY_REG(hcrc->Instance->CR, CRC_CR_POLYSIZE, PolyLength);
  218. /* Return function status */
  219. return HAL_OK;
  220. }
  221. #endif /* #if defined (STM32F071xB) || defined (STM32F072xB) || defined (STM32F078xx) || defined (STM32F091xC) || defined (STM32F098xx) */
  222. /**
  223. * @}
  224. */
  225. /**
  226. * @}
  227. */
  228. #endif /* HAL_CRC_MODULE_ENABLED */
  229. /**
  230. * @}
  231. */
  232. /**
  233. * @}
  234. */
  235. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/