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