]> git.donarmstrong.com Git - qmk_firmware.git/blob - tool/mbed/mbed-sdk/libraries/mbed/targets/cmsis/TARGET_STM/TARGET_STM32L0/stm32l0xx_hal_uart_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_uart_ex.c
1 /**
2   ******************************************************************************
3   * @file    stm32l0xx_hal_uart_ex.c
4   * @author  MCD Application Team
5   * @version V1.2.0
6   * @date    06-February-2015
7   * @brief   Extended UART HAL module driver.
8   *    
9   *          This file provides firmware functions to manage the following 
10   *          functionalities of the Inter Integrated Circuit (UART) peripheral:
11   *           + Extended Control methods
12   *         
13   @verbatim
14   ==============================================================================
15                ##### UART peripheral extended features  #####
16   ==============================================================================
17            
18   [..] Comparing to other previous devices, the UART interface for STM32L0XX
19        devices contains the following additional features
20        
21        (+) Possibility to disable or enable Analog Noise Filter
22        (+) Use of a configured Digital Noise Filter
23        (+) Disable or enable wakeup from Stop mode
24    
25                      ##### How to use this driver #####
26   ==============================================================================
27   [..] This driver provides functions to configure Noise Filter
28   
29   @endverbatim
30   ******************************************************************************
31   * @attention
32   *
33   * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
34   *
35   * Redistribution and use in source and binary forms, with or without modification,
36   * are permitted provided that the following conditions are met:
37   *   1. Redistributions of source code must retain the above copyright notice,
38   *      this list of conditions and the following disclaimer.
39   *   2. Redistributions in binary form must reproduce the above copyright notice,
40   *      this list of conditions and the following disclaimer in the documentation
41   *      and/or other materials provided with the distribution.
42   *   3. Neither the name of STMicroelectronics nor the names of its contributors
43   *      may be used to endorse or promote products derived from this software
44   *      without specific prior written permission.
45   *
46   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
47   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
49   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
50   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
51   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
52   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
53   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
54   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
55   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
56   *
57   ******************************************************************************  
58   */ 
59
60 /* Includes ------------------------------------------------------------------*/
61 #include "stm32l0xx_hal.h"
62
63 /** @addtogroup STM32L0xx_HAL_Driver
64   * @{
65   */
66
67 /** @addtogroup UARTEx
68   * @brief UARTEx module driver
69   * @{
70   */
71
72 #ifdef HAL_UART_MODULE_ENABLED
73
74 /* Private typedef -----------------------------------------------------------*/
75 /* Private define ------------------------------------------------------------*/
76 #define UART_REACK_TIMEOUT       ((uint32_t) 1000)
77 /* Private macro -------------------------------------------------------------*/
78 /* Private variables ---------------------------------------------------------*/
79 /* Private function prototypes -----------------------------------------------*/
80 static void UART_Wakeup_AddressConfig(UART_HandleTypeDef *huart, UART_WakeUpTypeDef WakeUpSelection);
81 /* Private functions ---------------------------------------------------------*/
82
83 /** @addtogroup UARTEx_Exported_Functions
84   * @{
85   */
86
87 /** @addtogroup UARTEx_Exported_Functions_Group1
88   * @brief    Extended Initialization and Configuration Functions
89
90   *
91 @verbatim    
92 ===============================================================================
93             ##### Initialization and Configuration functions #####
94  ===============================================================================  
95     [..]
96     The HAL_RS485Ex_Init() API follows respectively the UART RS485 mode 
97     configuration procedures (details for the procedures are available in reference manual).
98
99 @endverbatim
100   * @{
101   */
102
103 /**
104   * @brief Initializes the RS485 Driver enable feature according to the specified
105   *         parameters in the UART_InitTypeDef and creates the associated handle .
106   * @param huart: uart handle
107   * @param Polarity: select the driver enable polarity
108   *        This parameter can be one of the following values:
109   *          @arg UART_DE_POLARITY_HIGH: DE signal is active high
110   *          @arg UART_DE_POLARITY_LOW: DE signal is active low
111   * @param AssertionTime: Driver Enable assertion time
112   *                         5-bit value defining the time between the activation of the DE (Driver Enable)
113   *                         signal and the beginning of the start bit. It is expressed in sample time
114   *                         units (1/8 or 1/16 bit time, depending on the oversampling rate)         
115   * @param DeassertionTime: Driver Enable deassertion time          
116   *                         5-bit value defining the time between the end of the last stop bit, in a
117   *                         transmitted message, and the de-activation of the DE (Driver Enable) signal.
118   *                         It is expressed in sample time units (1/8 or 1/16 bit time, depending on the
119   *                         oversampling rate).
120   * @retval HAL status
121   */
122 HAL_StatusTypeDef HAL_RS485Ex_Init(UART_HandleTypeDef *huart, uint32_t Polarity, uint32_t AssertionTime, uint32_t DeassertionTime)
123 {
124   uint32_t temp = 0x0;
125   
126   /* Check the UART handle allocation */
127   if(huart == NULL)
128   {
129     return HAL_ERROR;
130   }
131
132   /* Check the Driver Enable polarity */
133   assert_param(IS_UART_DE_POLARITY(Polarity));
134   
135   /* Check the Driver Enable assertion time */
136   assert_param(IS_UART_ASSERTIONTIME(AssertionTime));
137   
138   /* Check the Driver Enable deassertion time */
139   assert_param(IS_UART_DEASSERTIONTIME(DeassertionTime));
140   
141   if(huart->State == HAL_UART_STATE_RESET)
142   {   
143     /* Init the low level hardware : GPIO, CLOCK, CORTEX */
144     HAL_UART_MspInit(huart);
145   }
146
147   huart->State = HAL_UART_STATE_BUSY;
148
149   /* Disable the Peripheral */
150   __HAL_UART_DISABLE(huart);
151   
152   /* Set the UART Communication parameters */
153   UART_SetConfig(huart);
154   
155   if(huart->AdvancedInit.AdvFeatureInit != UART_ADVFEATURE_NO_INIT)
156   {
157     UART_AdvFeatureConfig(huart);
158   }
159   
160   /* Enable the Driver Enable mode by setting the DEM bit in the CR3 register */
161   huart->Instance->CR3 |= USART_CR3_DEM;
162   
163   /* Set the Driver Enable polarity */
164   MODIFY_REG(huart->Instance->CR3, USART_CR3_DEP, Polarity);
165   
166   /* Set the Driver Enable assertion and deassertion times */
167   temp = (AssertionTime << UART_CR1_DEAT_ADDRESS_LSB_POS);
168   temp |= (DeassertionTime << UART_CR1_DEDT_ADDRESS_LSB_POS);
169   MODIFY_REG(huart->Instance->CR1, (USART_CR1_DEDT|USART_CR1_DEAT), temp);
170   
171   /* Enable the Peripheral */
172   __HAL_UART_ENABLE(huart);
173   
174   /* TEACK and/or REACK to check before moving huart->State to Ready */
175   return (UART_CheckIdleState(huart));
176 }
177
178
179 /**
180   * @}
181   */
182   
183 /** @addtogroup UARTEx_Exported_Functions_Group2
184  *  @brief   management functions 
185  *
186 @verbatim   
187  ===============================================================================
188                       ##### Peripheral Control funtions #####
189  ===============================================================================  
190     [..] This section provides functions allowing to:
191      (+) UART_AdvFeatureConfig() API optionally configures the UART advanced features 
192      (+) HAL_MultiProcessorEx_AddressLength_Set() API optionally sets the UART node address
193          detection length to more than 4 bits for multiprocessor address mark wake up.
194      (+) HAL_UARTEx_EnableStopMode() API enables the UART to wake up the MCU from stop mode   
195      (+) HAL_UARTEx_DisableStopMode() API disables the above functionality
196      (+) HAL_UARTEx_EnableClockStopMode() API enables the UART HSI clock during stop mode   
197      (+) HAL_UARTEx_DisableClockStopMode() API disables the above functionality   
198      (+) UART_Wakeup_AddressConfig() API configures the wake-up from stop mode parameters
199
200 @endverbatim
201   * @{
202   */
203   
204 /**
205   * @brief Enable UART Stop Mode
206   * The UART is able to wake up the MCU from Stop mode as long as UART clock is HSI or LSE
207   * @param huart: uart handle
208   * @retval HAL status
209   */
210 HAL_StatusTypeDef HAL_UARTEx_EnableStopMode(UART_HandleTypeDef *huart)
211 {
212   /* Process Locked */
213   __HAL_LOCK(huart);
214   
215   huart->State = HAL_UART_STATE_BUSY;
216   
217   /* Set the USART UESM bit */
218   huart->Instance->CR1 |= USART_CR1_UESM;
219   
220   huart->State = HAL_UART_STATE_READY;
221   
222   /* Process Unlocked */
223   __HAL_UNLOCK(huart);
224   
225   return HAL_OK; 
226 }
227
228 /**
229   * @brief Enable UART Clock in Stop Mode
230   * The UART keeps the Clock ON during Stop mode
231   * @param huart: uart handle
232   * @retval HAL status
233   */
234 HAL_StatusTypeDef HAL_UARTEx_EnableClockStopMode(UART_HandleTypeDef *huart)
235 {
236   /* Process Locked */
237   __HAL_LOCK(huart);
238   
239   huart->State = HAL_UART_STATE_BUSY;
240   
241   /* Set the USART UESM bit */
242   huart->Instance->CR3 |= USART_CR3_UCESM;
243   
244   huart->State = HAL_UART_STATE_READY;
245   
246   /* Process Unlocked */
247   __HAL_UNLOCK(huart);
248   
249   return HAL_OK; 
250 }
251
252 /**
253   * @brief Disable UART Stop Mode 
254   * @param huart: uart handle
255   * @retval HAL status
256   */
257 HAL_StatusTypeDef HAL_UARTEx_DisableStopMode(UART_HandleTypeDef *huart)
258 {
259   /* Process Locked */
260   __HAL_LOCK(huart);
261
262   huart->State = HAL_UART_STATE_BUSY; 
263
264   /* Clear USART UESM bit */
265   huart->Instance->CR1 &= ~(USART_CR1_UESM);
266   
267   huart->State = HAL_UART_STATE_READY;
268
269   /* Process Unlocked */
270   __HAL_UNLOCK(huart);
271
272   return HAL_OK;
273 }
274
275 /**
276   * @brief Disable UART Clock in Stop Mode 
277   * @param huart: uart handle
278   * @retval HAL status
279   */
280 HAL_StatusTypeDef HAL_UARTEx_DisableClockStopMode(UART_HandleTypeDef *huart)
281 {
282   /* Process Locked */
283   __HAL_LOCK(huart);
284
285   huart->State = HAL_UART_STATE_BUSY;
286
287   /* Clear USART UESM bit */
288   huart->Instance->CR3 &= ~(USART_CR3_UCESM);
289
290   huart->State = HAL_UART_STATE_READY;
291
292   /* Process Unlocked */
293   __HAL_UNLOCK(huart);
294
295   return HAL_OK;
296 }
297
298 /**
299   * @brief Set Wakeup from Stop mode interrupt flag selection
300   * @param huart: uart handle, 
301   * @param WakeUpSelection: address match, Start Bit detection or RXNE bit status.
302   * This parameter can be one of the following values:
303   *      @arg UART_WAKEUP_ON_ADDRESS
304   *      @arg UART_WAKEUP_ON_STARTBIT
305   *      @arg UART_WAKEUP_ON_READDATA_NONEMPTY
306   * @retval HAL status
307   */
308 HAL_StatusTypeDef HAL_UARTEx_StopModeWakeUpSourceConfig(UART_HandleTypeDef *huart, UART_WakeUpTypeDef WakeUpSelection)
309 {
310
311   /* check the wake-up from stop mode UART instance */
312   assert_param(IS_UART_WAKEUP_FROMSTOP_INSTANCE(huart->Instance));
313   /* Check the wake-up selection parameter */
314   assert_param(IS_UART_WAKEUP_SELECTION(WakeUpSelection.WakeUpEvent));
315
316   /* Process Locked */
317   __HAL_LOCK(huart);
318
319   huart->State = HAL_UART_STATE_BUSY;
320
321   /* Disable the Peripheral */
322   __HAL_UART_DISABLE(huart);
323
324   /* Set the wake-up selection scheme */
325   MODIFY_REG(huart->Instance->CR3, USART_CR3_WUS, WakeUpSelection.WakeUpEvent);
326
327   if(WakeUpSelection.WakeUpEvent == UART_WAKEUP_ON_ADDRESS)
328   {
329     UART_Wakeup_AddressConfig(huart, WakeUpSelection);
330   }
331
332   /* Enable the Peripheral */
333   __HAL_UART_ENABLE(huart);
334
335   /* Wait until REACK flag is set before moving huart->State to Ready */
336   if(UART_WaitOnFlagUntilTimeout(huart, USART_ISR_REACK, RESET, UART_REACK_TIMEOUT) != HAL_OK)
337   {
338     return HAL_TIMEOUT;
339   }
340
341   /* Process Unlocked */
342   __HAL_UNLOCK(huart);
343
344   /* Initialize the UART state*/
345   huart->ErrorCode = HAL_UART_ERROR_NONE;
346   huart->State= HAL_UART_STATE_READY;
347
348   return HAL_OK;
349 }
350 /**
351   * @brief By default in multiprocessor mode, when the wake up method is set 
352   *        to address mark, the UART handles only 4-bit long addresses detection. 
353   *        This API allows to enable longer addresses detection (6-, 7- or 8-bit
354   *        long):
355   *        - 6-bit address detection in 7-bit data mode
356   *        - 7-bit address detection in 8-bit data mode
357   *        - 8-bit address detection in 9-bit data mode
358   * @param huart: UART handle
359   * @param AddressLength: this parameter can be one of the following values:
360   *          @arg UART_ADDRESS_DETECT_4B: 4-bit long address
361   *          @arg UART_ADDRESS_DETECT_7B: 6-, 7- or 8-bit long address
362   * @retval HAL status
363   */
364 HAL_StatusTypeDef HAL_MultiProcessorEx_AddressLength_Set(UART_HandleTypeDef *huart, uint32_t AddressLength)
365 {
366   /* Check the UART handle allocation */
367   if(huart == NULL)
368   {
369     return HAL_ERROR;
370   }
371
372   /* Check the address length parameter */
373   assert_param(IS_UART_ADDRESSLENGTH_DETECT(AddressLength));
374   
375   huart->State = HAL_UART_STATE_BUSY;
376   
377   /* Disable the Peripheral */
378   __HAL_UART_DISABLE(huart);
379   
380   /* Set the address length */
381   MODIFY_REG(huart->Instance->CR2, USART_CR2_ADDM7, AddressLength);
382   
383   /* Enable the Peripheral */
384   __HAL_UART_ENABLE(huart); 
385   
386   /* TEACK and/or REACK to check before moving huart->State to Ready */
387   return (UART_CheckIdleState(huart));
388 }
389
390 /**
391   * @}
392   */  
393
394 /**
395   * @brief Initializes the UART wake-up from stop mode parameters when triggered by address detection.
396   * @param huart: uart handle
397   * @param WakeUpSelection: UART wake up from stop mode parameters
398   * @retval HAL status
399   */                        
400 static void UART_Wakeup_AddressConfig(UART_HandleTypeDef *huart, UART_WakeUpTypeDef WakeUpSelection)
401 {
402   assert_param(IS_UART_ADDRESSLENGTH_DETECT(WakeUpSelection.AddressLength));
403   if(WakeUpSelection.AddressLength == UART_ADDRESS_DETECT_4B)
404   {
405     assert_param(IS_UART_4B_ADDRESS(WakeUpSelection.Address));
406   }
407   else
408   {
409     assert_param(IS_UART_7B_ADDRESS(WakeUpSelection.Address));
410   }
411
412   /* Set the USART address length */
413   MODIFY_REG(huart->Instance->CR2, USART_CR2_ADDM7, WakeUpSelection.AddressLength);
414
415   /* Set the USART address node */
416   MODIFY_REG(huart->Instance->CR2, USART_CR2_ADD, ((uint32_t)WakeUpSelection.Address << UART_CR2_ADDRESS_LSB_POS));
417 }
418
419 /**
420   * @brief UART wakeup from Stop mode callback
421   * @param huart: uart handle
422   * @retval None
423   */
424  __weak void HAL_UARTEx_WakeupCallback(UART_HandleTypeDef *huart)
425 {
426   /* NOTE : This function should not be modified, when the callback is needed,
427             the HAL_UART_WakeupCallback can be implemented in the user file
428    */
429 }
430
431 /**
432   * @}
433   */
434
435 #endif /* HAL_UART_MODULE_ENABLED */
436 /**
437   * @}
438   */
439
440 /**
441   * @}
442   */
443
444 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
445