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.
 
 
 

238 lines
8.4 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32l4xx_hal_crc_ex.c
  4. * @author MCD Application Team
  5. * @version V1.7.2
  6. * @date 16-June-2017
  7. * @brief Extended CRC HAL module driver.
  8. * This file provides firmware functions to manage the extended
  9. * functionalities of the CRC peripheral.
  10. *
  11. @verbatim
  12. ================================================================================
  13. ##### How to use this driver #####
  14. ================================================================================
  15. [..]
  16. (+) Set user-defined generating polynomial thru HAL_CRCEx_Polynomial_Set()
  17. (+) Configure Input or Output data inversion
  18. @endverbatim
  19. ******************************************************************************
  20. * @attention
  21. *
  22. * <h2><center>&copy; COPYRIGHT(c) 2017 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 "stm32l4xx_hal.h"
  50. /** @addtogroup STM32L4xx_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. /* Exported functions --------------------------------------------------------*/
  64. /** @defgroup CRCEx_Exported_Functions CRC Extended 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. ##### Extended configuration functions #####
  73. ===============================================================================
  74. [..] This section provides functions allowing to:
  75. (+) Configure the generating polynomial
  76. (+) Configure the input data inversion
  77. (+) Configure the output data inversion
  78. @endverbatim
  79. * @{
  80. */
  81. /**
  82. * @brief Initialize 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. * @arg for a polynomial of degree 7, X^7 + X^6 + X^5 + X^2 + 1 is written 0x65
  87. * @arg 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 @ref CRC_POLYLENGTH_7B 7-bit long CRC (generating polynomial of degree 7)
  91. * @arg @ref CRC_POLYLENGTH_8B 8-bit long CRC (generating polynomial of degree 8)
  92. * @arg @ref CRC_POLYLENGTH_16B 16-bit long CRC (generating polynomial of degree 16)
  93. * @arg @ref 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 & (1U << msb)) == 0) && (msb-- > 0)) {}
  109. switch (PolyLength)
  110. {
  111. case CRC_POLYLENGTH_7B:
  112. if (msb >= HAL_CRC_LENGTH_7B)
  113. {
  114. return HAL_ERROR;
  115. }
  116. break;
  117. case CRC_POLYLENGTH_8B:
  118. if (msb >= HAL_CRC_LENGTH_8B)
  119. {
  120. return HAL_ERROR;
  121. }
  122. break;
  123. case CRC_POLYLENGTH_16B:
  124. if (msb >= HAL_CRC_LENGTH_16B)
  125. {
  126. return HAL_ERROR;
  127. }
  128. break;
  129. case CRC_POLYLENGTH_32B:
  130. /* no polynomial definition vs. polynomial length issue possible */
  131. break;
  132. default:
  133. return HAL_ERROR;
  134. }
  135. /* set generating polynomial */
  136. WRITE_REG(hcrc->Instance->POL, Pol);
  137. /* set generating polynomial size */
  138. MODIFY_REG(hcrc->Instance->CR, CRC_CR_POLYSIZE, PolyLength);
  139. /* Return function status */
  140. return HAL_OK;
  141. }
  142. /**
  143. * @brief Set the Reverse Input data mode.
  144. * @param hcrc: CRC handle
  145. * @param InputReverseMode: Input Data inversion mode.
  146. * This parameter can be one of the following values:
  147. * @arg @ref CRC_INPUTDATA_INVERSION_NONE no change in bit order (default value)
  148. * @arg @ref CRC_INPUTDATA_INVERSION_BYTE Byte-wise bit reversal
  149. * @arg @ref CRC_INPUTDATA_INVERSION_HALFWORD HalfWord-wise bit reversal
  150. * @arg @ref CRC_INPUTDATA_INVERSION_WORD Word-wise bit reversal
  151. * @retval HAL status
  152. */
  153. HAL_StatusTypeDef HAL_CRCEx_Input_Data_Reverse(CRC_HandleTypeDef *hcrc, uint32_t InputReverseMode)
  154. {
  155. /* Check the parameters */
  156. assert_param(IS_CRC_INPUTDATA_INVERSION_MODE(InputReverseMode));
  157. /* Change CRC peripheral state */
  158. hcrc->State = HAL_CRC_STATE_BUSY;
  159. /* set input data inversion mode */
  160. MODIFY_REG(hcrc->Instance->CR, CRC_CR_REV_IN, InputReverseMode);
  161. /* Change CRC peripheral state */
  162. hcrc->State = HAL_CRC_STATE_READY;
  163. /* Return function status */
  164. return HAL_OK;
  165. }
  166. /**
  167. * @brief Set the Reverse Output data mode.
  168. * @param hcrc: CRC handle
  169. * @param OutputReverseMode: Output Data inversion mode.
  170. * This parameter can be one of the following values:
  171. * @arg @ref CRC_OUTPUTDATA_INVERSION_DISABLE no CRC inversion (default value)
  172. * @arg @ref CRC_OUTPUTDATA_INVERSION_ENABLE bit-level inversion (e.g. for a 8-bit CRC: 0xB5 becomes 0xAD)
  173. * @retval HAL status
  174. */
  175. HAL_StatusTypeDef HAL_CRCEx_Output_Data_Reverse(CRC_HandleTypeDef *hcrc, uint32_t OutputReverseMode)
  176. {
  177. /* Check the parameters */
  178. assert_param(IS_CRC_OUTPUTDATA_INVERSION_MODE(OutputReverseMode));
  179. /* Change CRC peripheral state */
  180. hcrc->State = HAL_CRC_STATE_BUSY;
  181. /* set output data inversion mode */
  182. MODIFY_REG(hcrc->Instance->CR, CRC_CR_REV_OUT, OutputReverseMode);
  183. /* Change CRC peripheral state */
  184. hcrc->State = HAL_CRC_STATE_READY;
  185. /* Return function status */
  186. return HAL_OK;
  187. }
  188. /**
  189. * @}
  190. */
  191. /**
  192. * @}
  193. */
  194. #endif /* HAL_CRC_MODULE_ENABLED */
  195. /**
  196. * @}
  197. */
  198. /**
  199. * @}
  200. */
  201. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/