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 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 through HAL_CRCEx_Polynomial_Set()
15          (+) Configure Input or Output data inversion
16 
17   @endverbatim
18   ******************************************************************************
19   * @attention
20   *
21   * <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
22   * All rights reserved.</center></h2>
23   *
24   * This software component is licensed by ST under BSD 3-Clause license,
25   * the "License"; You may not use this file except in compliance with the
26   * License. You may obtain a copy of the License at:
27   *                        opensource.org/licenses/BSD-3-Clause
28   *
29   ******************************************************************************
30   */
31 
32 /* Includes ------------------------------------------------------------------*/
33 #include "stm32f0xx_hal.h"
34 
35 /** @addtogroup STM32F0xx_HAL_Driver
36   * @{
37   */
38 
39 /** @defgroup CRCEx CRCEx
40   * @brief CRC Extended HAL module driver
41   * @{
42   */
43 
44 #ifdef HAL_CRC_MODULE_ENABLED
45 
46 /* Private typedef -----------------------------------------------------------*/
47 /* Private define ------------------------------------------------------------*/
48 /* Private macro -------------------------------------------------------------*/
49 /* Private variables ---------------------------------------------------------*/
50 /* Private function prototypes -----------------------------------------------*/
51 /* Exported functions --------------------------------------------------------*/
52 
53 /** @defgroup CRCEx_Exported_Functions CRC Extended Exported Functions
54   * @{
55   */
56 
57 /** @defgroup CRCEx_Exported_Functions_Group1 Extended Initialization/de-initialization functions
58   * @brief    Extended Initialization and Configuration functions.
59   *
60 @verbatim
61  ===============================================================================
62             ##### Extended configuration functions #####
63  ===============================================================================
64     [..]  This section provides functions allowing to:
65       (+) Configure the generating polynomial
66       (+) Configure the input data inversion
67       (+) Configure the output data inversion
68 
69 @endverbatim
70   * @{
71   */
72 
73 
74 #if defined(CRC_POL_POL)
75 /**
76   * @brief  Initialize the CRC polynomial if different from default one.
77   * @param  hcrc CRC handle
78   * @param  Pol CRC generating polynomial (7, 8, 16 or 32-bit long).
79   *         This parameter is written in normal representation, e.g.
80   *         @arg for a polynomial of degree 7, X^7 + X^6 + X^5 + X^2 + 1 is written 0x65
81   *         @arg for a polynomial of degree 16, X^16 + X^12 + X^5 + 1 is written 0x1021
82   * @param  PolyLength CRC polynomial length.
83   *         This parameter can be one of the following values:
84   *          @arg @ref CRC_POLYLENGTH_7B  7-bit long CRC (generating polynomial of degree 7)
85   *          @arg @ref CRC_POLYLENGTH_8B  8-bit long CRC (generating polynomial of degree 8)
86   *          @arg @ref CRC_POLYLENGTH_16B 16-bit long CRC (generating polynomial of degree 16)
87   *          @arg @ref CRC_POLYLENGTH_32B 32-bit long CRC (generating polynomial of degree 32)
88   * @retval HAL status
89   */
HAL_CRCEx_Polynomial_Set(CRC_HandleTypeDef * hcrc,uint32_t Pol,uint32_t PolyLength)90 HAL_StatusTypeDef HAL_CRCEx_Polynomial_Set(CRC_HandleTypeDef *hcrc, uint32_t Pol, uint32_t PolyLength)
91 {
92   HAL_StatusTypeDef status = HAL_OK;
93   uint32_t msb = 31U; /* polynomial degree is 32 at most, so msb is initialized to max value */
94 
95   /* Check the parameters */
96   assert_param(IS_CRC_POL_LENGTH(PolyLength));
97 
98   /* check polynomial definition vs polynomial size:
99    * polynomial length must be aligned with polynomial
100    * definition. HAL_ERROR is reported if Pol degree is
101    * larger than that indicated by PolyLength.
102    * Look for MSB position: msb will contain the degree of
103    *  the second to the largest polynomial member. E.g., for
104    *  X^7 + X^6 + X^5 + X^2 + 1, msb = 6. */
105   while ((msb-- > 0U) && ((Pol & ((uint32_t)(0x1U) << (msb & 0x1FU))) == 0U))
106   {
107   }
108 
109   switch (PolyLength)
110   {
111     case CRC_POLYLENGTH_7B:
112       if (msb >= HAL_CRC_LENGTH_7B)
113       {
114         status =   HAL_ERROR;
115       }
116       break;
117     case CRC_POLYLENGTH_8B:
118       if (msb >= HAL_CRC_LENGTH_8B)
119       {
120         status =   HAL_ERROR;
121       }
122       break;
123     case CRC_POLYLENGTH_16B:
124       if (msb >= HAL_CRC_LENGTH_16B)
125       {
126         status =   HAL_ERROR;
127       }
128       break;
129 
130     case CRC_POLYLENGTH_32B:
131       /* no polynomial definition vs. polynomial length issue possible */
132       break;
133     default:
134       status =  HAL_ERROR;
135       break;
136   }
137   if (status == HAL_OK)
138   {
139     /* set generating polynomial */
140     WRITE_REG(hcrc->Instance->POL, Pol);
141 
142     /* set generating polynomial size */
143     MODIFY_REG(hcrc->Instance->CR, CRC_CR_POLYSIZE, PolyLength);
144   }
145   /* Return function status */
146   return status;
147 }
148 #endif /* CRC_POL_POL */
149 
150 /**
151   * @brief  Set the Reverse Input data mode.
152   * @param  hcrc CRC handle
153   * @param  InputReverseMode Input Data inversion mode.
154   *         This parameter can be one of the following values:
155   *          @arg @ref CRC_INPUTDATA_INVERSION_NONE     no change in bit order (default value)
156   *          @arg @ref CRC_INPUTDATA_INVERSION_BYTE     Byte-wise bit reversal
157   *          @arg @ref CRC_INPUTDATA_INVERSION_HALFWORD HalfWord-wise bit reversal
158   *          @arg @ref CRC_INPUTDATA_INVERSION_WORD     Word-wise bit reversal
159   * @retval HAL status
160   */
HAL_CRCEx_Input_Data_Reverse(CRC_HandleTypeDef * hcrc,uint32_t InputReverseMode)161 HAL_StatusTypeDef HAL_CRCEx_Input_Data_Reverse(CRC_HandleTypeDef *hcrc, uint32_t InputReverseMode)
162 {
163   /* Check the parameters */
164   assert_param(IS_CRC_INPUTDATA_INVERSION_MODE(InputReverseMode));
165 
166   /* Change CRC peripheral state */
167   hcrc->State = HAL_CRC_STATE_BUSY;
168 
169   /* set input data inversion mode */
170   MODIFY_REG(hcrc->Instance->CR, CRC_CR_REV_IN, InputReverseMode);
171   /* Change CRC peripheral state */
172   hcrc->State = HAL_CRC_STATE_READY;
173 
174   /* Return function status */
175   return HAL_OK;
176 }
177 
178 /**
179   * @brief  Set the Reverse Output data mode.
180   * @param  hcrc CRC handle
181   * @param  OutputReverseMode Output Data inversion mode.
182   *         This parameter can be one of the following values:
183   *          @arg @ref CRC_OUTPUTDATA_INVERSION_DISABLE no CRC inversion (default value)
184   *          @arg @ref CRC_OUTPUTDATA_INVERSION_ENABLE  bit-level inversion (e.g. for a 8-bit CRC: 0xB5 becomes 0xAD)
185   * @retval HAL status
186   */
HAL_CRCEx_Output_Data_Reverse(CRC_HandleTypeDef * hcrc,uint32_t OutputReverseMode)187 HAL_StatusTypeDef HAL_CRCEx_Output_Data_Reverse(CRC_HandleTypeDef *hcrc, uint32_t OutputReverseMode)
188 {
189   /* Check the parameters */
190   assert_param(IS_CRC_OUTPUTDATA_INVERSION_MODE(OutputReverseMode));
191 
192   /* Change CRC peripheral state */
193   hcrc->State = HAL_CRC_STATE_BUSY;
194 
195   /* set output data inversion mode */
196   MODIFY_REG(hcrc->Instance->CR, CRC_CR_REV_OUT, OutputReverseMode);
197 
198   /* Change CRC peripheral state */
199   hcrc->State = HAL_CRC_STATE_READY;
200 
201   /* Return function status */
202   return HAL_OK;
203 }
204 
205 
206 
207 
208 /**
209   * @}
210   */
211 
212 
213 /**
214   * @}
215   */
216 
217 
218 #endif /* HAL_CRC_MODULE_ENABLED */
219 /**
220   * @}
221   */
222 
223 /**
224   * @}
225   */
226 
227 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
228