2 ******************************************************************************
3 * @file stm32l0xx_hal_uart_ex.c
4 * @author MCD Application Team
6 * @date 06-February-2015
7 * @brief Extended UART HAL module driver.
9 * This file provides firmware functions to manage the following
10 * functionalities of the Inter Integrated Circuit (UART) peripheral:
11 * + Extended Control methods
14 ==============================================================================
15 ##### UART peripheral extended features #####
16 ==============================================================================
18 [..] Comparing to other previous devices, the UART interface for STM32L0XX
19 devices contains the following additional features
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
25 ##### How to use this driver #####
26 ==============================================================================
27 [..] This driver provides functions to configure Noise Filter
30 ******************************************************************************
33 * <h2><center>© COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
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.
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.
57 ******************************************************************************
60 /* Includes ------------------------------------------------------------------*/
61 #include "stm32l0xx_hal.h"
63 /** @addtogroup STM32L0xx_HAL_Driver
67 /** @addtogroup UARTEx
68 * @brief UARTEx module driver
72 #ifdef HAL_UART_MODULE_ENABLED
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 ---------------------------------------------------------*/
83 /** @addtogroup UARTEx_Exported_Functions
87 /** @addtogroup UARTEx_Exported_Functions_Group1
88 * @brief Extended Initialization and Configuration Functions
92 ===============================================================================
93 ##### Initialization and Configuration functions #####
94 ===============================================================================
96 The HAL_RS485Ex_Init() API follows respectively the UART RS485 mode
97 configuration procedures (details for the procedures are available in reference manual).
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).
122 HAL_StatusTypeDef
HAL_RS485Ex_Init(UART_HandleTypeDef
*huart
, uint32_t Polarity
, uint32_t AssertionTime
, uint32_t DeassertionTime
)
126 /* Check the UART handle allocation */
132 /* Check the Driver Enable polarity */
133 assert_param(IS_UART_DE_POLARITY(Polarity
));
135 /* Check the Driver Enable assertion time */
136 assert_param(IS_UART_ASSERTIONTIME(AssertionTime
));
138 /* Check the Driver Enable deassertion time */
139 assert_param(IS_UART_DEASSERTIONTIME(DeassertionTime
));
141 if(huart
->State
== HAL_UART_STATE_RESET
)
143 /* Init the low level hardware : GPIO, CLOCK, CORTEX */
144 HAL_UART_MspInit(huart
);
147 huart
->State
= HAL_UART_STATE_BUSY
;
149 /* Disable the Peripheral */
150 __HAL_UART_DISABLE(huart
);
152 /* Set the UART Communication parameters */
153 UART_SetConfig(huart
);
155 if(huart
->AdvancedInit
.AdvFeatureInit
!= UART_ADVFEATURE_NO_INIT
)
157 UART_AdvFeatureConfig(huart
);
160 /* Enable the Driver Enable mode by setting the DEM bit in the CR3 register */
161 huart
->Instance
->CR3
|= USART_CR3_DEM
;
163 /* Set the Driver Enable polarity */
164 MODIFY_REG(huart
->Instance
->CR3
, USART_CR3_DEP
, Polarity
);
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
);
171 /* Enable the Peripheral */
172 __HAL_UART_ENABLE(huart
);
174 /* TEACK and/or REACK to check before moving huart->State to Ready */
175 return (UART_CheckIdleState(huart
));
183 /** @addtogroup UARTEx_Exported_Functions_Group2
184 * @brief management functions
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
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
210 HAL_StatusTypeDef
HAL_UARTEx_EnableStopMode(UART_HandleTypeDef
*huart
)
215 huart
->State
= HAL_UART_STATE_BUSY
;
217 /* Set the USART UESM bit */
218 huart
->Instance
->CR1
|= USART_CR1_UESM
;
220 huart
->State
= HAL_UART_STATE_READY
;
222 /* Process Unlocked */
229 * @brief Enable UART Clock in Stop Mode
230 * The UART keeps the Clock ON during Stop mode
231 * @param huart: uart handle
234 HAL_StatusTypeDef
HAL_UARTEx_EnableClockStopMode(UART_HandleTypeDef
*huart
)
239 huart
->State
= HAL_UART_STATE_BUSY
;
241 /* Set the USART UESM bit */
242 huart
->Instance
->CR3
|= USART_CR3_UCESM
;
244 huart
->State
= HAL_UART_STATE_READY
;
246 /* Process Unlocked */
253 * @brief Disable UART Stop Mode
254 * @param huart: uart handle
257 HAL_StatusTypeDef
HAL_UARTEx_DisableStopMode(UART_HandleTypeDef
*huart
)
262 huart
->State
= HAL_UART_STATE_BUSY
;
264 /* Clear USART UESM bit */
265 huart
->Instance
->CR1
&= ~(USART_CR1_UESM
);
267 huart
->State
= HAL_UART_STATE_READY
;
269 /* Process Unlocked */
276 * @brief Disable UART Clock in Stop Mode
277 * @param huart: uart handle
280 HAL_StatusTypeDef
HAL_UARTEx_DisableClockStopMode(UART_HandleTypeDef
*huart
)
285 huart
->State
= HAL_UART_STATE_BUSY
;
287 /* Clear USART UESM bit */
288 huart
->Instance
->CR3
&= ~(USART_CR3_UCESM
);
290 huart
->State
= HAL_UART_STATE_READY
;
292 /* Process Unlocked */
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
308 HAL_StatusTypeDef
HAL_UARTEx_StopModeWakeUpSourceConfig(UART_HandleTypeDef
*huart
, UART_WakeUpTypeDef WakeUpSelection
)
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
));
319 huart
->State
= HAL_UART_STATE_BUSY
;
321 /* Disable the Peripheral */
322 __HAL_UART_DISABLE(huart
);
324 /* Set the wake-up selection scheme */
325 MODIFY_REG(huart
->Instance
->CR3
, USART_CR3_WUS
, WakeUpSelection
.WakeUpEvent
);
327 if(WakeUpSelection
.WakeUpEvent
== UART_WAKEUP_ON_ADDRESS
)
329 UART_Wakeup_AddressConfig(huart
, WakeUpSelection
);
332 /* Enable the Peripheral */
333 __HAL_UART_ENABLE(huart
);
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
)
341 /* Process Unlocked */
344 /* Initialize the UART state*/
345 huart
->ErrorCode
= HAL_UART_ERROR_NONE
;
346 huart
->State
= HAL_UART_STATE_READY
;
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
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
364 HAL_StatusTypeDef
HAL_MultiProcessorEx_AddressLength_Set(UART_HandleTypeDef
*huart
, uint32_t AddressLength
)
366 /* Check the UART handle allocation */
372 /* Check the address length parameter */
373 assert_param(IS_UART_ADDRESSLENGTH_DETECT(AddressLength
));
375 huart
->State
= HAL_UART_STATE_BUSY
;
377 /* Disable the Peripheral */
378 __HAL_UART_DISABLE(huart
);
380 /* Set the address length */
381 MODIFY_REG(huart
->Instance
->CR2
, USART_CR2_ADDM7
, AddressLength
);
383 /* Enable the Peripheral */
384 __HAL_UART_ENABLE(huart
);
386 /* TEACK and/or REACK to check before moving huart->State to Ready */
387 return (UART_CheckIdleState(huart
));
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
400 static void UART_Wakeup_AddressConfig(UART_HandleTypeDef
*huart
, UART_WakeUpTypeDef WakeUpSelection
)
402 assert_param(IS_UART_ADDRESSLENGTH_DETECT(WakeUpSelection
.AddressLength
));
403 if(WakeUpSelection
.AddressLength
== UART_ADDRESS_DETECT_4B
)
405 assert_param(IS_UART_4B_ADDRESS(WakeUpSelection
.Address
));
409 assert_param(IS_UART_7B_ADDRESS(WakeUpSelection
.Address
));
412 /* Set the USART address length */
413 MODIFY_REG(huart
->Instance
->CR2
, USART_CR2_ADDM7
, WakeUpSelection
.AddressLength
);
415 /* Set the USART address node */
416 MODIFY_REG(huart
->Instance
->CR2
, USART_CR2_ADD
, ((uint32_t)WakeUpSelection
.Address
<< UART_CR2_ADDRESS_LSB_POS
));
420 * @brief UART wakeup from Stop mode callback
421 * @param huart: uart handle
424 __weak
void HAL_UARTEx_WakeupCallback(UART_HandleTypeDef
*huart
)
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
435 #endif /* HAL_UART_MODULE_ENABLED */
444 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/