stm32f7xx_hal_crc_ex.c 8.4 KB

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