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.
 
 
 

243 lines
8.2 KiB

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