stm32l4xx_hal_crc_ex.c 8.9 KB

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