2 ******************************************************************************
3 * @file stm32f3xx_hal_uart_ex.c
4 * @author MCD Application Team
7 * @brief Extended UART HAL module driver.
9 * This file provides firmware functions to manage the following extended
10 * functionalities of the Universal Asynchronous Receiver Transmitter Peripheral (UART).
11 * + Initialization and de-initialization functions
12 * + Peripheral Control functions
16 ===============================================================================
17 ##### How to use this driver #####
18 ===============================================================================
20 The UART HAL driver can be used as follows:
22 (#) Declare a UART_HandleTypeDef handle structure.
24 (#) For the UART RS485 Driver Enabled mode, initialize the UART registers
25 by calling the HAL_RS485Ex_Init() API.
29 ******************************************************************************
32 * <h2><center>© COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
34 * Redistribution and use in source and binary forms, with or without modification,
35 * are permitted provided that the following conditions are met:
36 * 1. Redistributions of source code must retain the above copyright notice,
37 * this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright notice,
39 * this list of conditions and the following disclaimer in the documentation
40 * and/or other materials provided with the distribution.
41 * 3. Neither the name of STMicroelectronics nor the names of its contributors
42 * may be used to endorse or promote products derived from this software
43 * without specific prior written permission.
45 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
46 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
48 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
51 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
52 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
53 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
54 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
56 ******************************************************************************
59 /* Includes ------------------------------------------------------------------*/
60 #include "stm32f3xx_hal.h"
62 /** @addtogroup STM32F3xx_HAL_Driver
66 /** @defgroup UARTEx UART Extended HAL module driver
67 * @brief UART Extended HAL module driver
70 #ifdef HAL_UART_MODULE_ENABLED
72 /* Private typedef -----------------------------------------------------------*/
73 /* Private define ------------------------------------------------------------*/
74 /* Private macro -------------------------------------------------------------*/
75 /* Private variables ---------------------------------------------------------*/
76 /* Private function prototypes -----------------------------------------------*/
77 /* Exported functions --------------------------------------------------------*/
79 /** @defgroup UARTEx_Exported_Functions UART Extended Exported Functions
83 /** @defgroup UARTEx_Exported_Functions_Group1 Extended Initialization and de-initialization functions
84 * @brief Extended Initialization and Configuration Functions
87 ===============================================================================
88 ##### Initialization and Configuration functions #####
89 ===============================================================================
91 This subsection provides a set of functions allowing to initialize the USARTx or the UARTy
93 (+) For the asynchronous mode only these parameters can be configured:
97 (++) Parity: If the parity is enabled, then the MSB bit of the data written
98 in the data register is transmitted but is changed by the parity bit.
99 Depending on the frame length defined by the M bit (8-bits or 9-bits)
100 or by the M1 and M0 bits (7-bit, 8-bit or 9-bit),
101 the possible UART frame formats are as listed in the following table:
102 +---------------------------------------------------------------+
103 | M bit | PCE bit | UART frame |
104 |-----------|-----------|---------------------------------------|
105 | 0 | 0 | | SB | 8-bit data | STB | |
106 |-----------|-----------|---------------------------------------|
107 | 0 | 1 | | SB | 7-bit data | PB | STB | |
108 |-----------|-----------|---------------------------------------|
109 | 1 | 0 | | SB | 9-bit data | STB | |
110 |-----------|-----------|---------------------------------------|
111 | 1 | 1 | | SB | 8-bit data | PB | STB | |
112 +---------------------------------------------------------------+
113 | M1M0 bits | PCE bit | UART frame |
114 |-----------------------|---------------------------------------|
115 | 10 | 0 | | SB | 7-bit data | STB | |
116 |-----------|-----------|---------------------------------------|
117 | 10 | 1 | | SB | 6-bit data | PB | STB | |
118 +---------------------------------------------------------------+
119 (++) Hardware flow control
120 (++) Receiver/transmitter modes
121 (++) Over Sampling Method
122 (++) One-Bit Sampling Method
123 (+) For the asynchronous mode, the following advanced features can be configured as well:
124 (++) TX and/or RX pin level inversion
125 (++) data logical level inversion
126 (++) RX and TX pins swap
127 (++) RX overrun detection disabling
128 (++) DMA disabling on RX error
129 (++) MSB first on communication line
130 (++) auto Baud rate detection
132 The HAL_RS485Ex_Init() API follows respectively the UART RS485 mode
133 configuration procedures (details for the procedures are available in reference manual).
141 * @brief Initializes the RS485 Driver enable feature according to the specified
142 * parameters in the UART_InitTypeDef and creates the associated handle .
143 * @param huart: uart handle
144 * @param UART_DEPolarity: select the driver enable polarity
145 * This parameter can be one of the following values:
146 * @arg UART_DE_POLARITY_HIGH: DE signal is active high
147 * @arg UART_DE_POLARITY_LOW: DE signal is active low
148 * @param UART_DEAssertionTime: Driver Enable assertion time
149 * 5-bit value defining the time between the activation of the DE (Driver Enable)
150 * signal and the beginning of the start bit. It is expressed in sample time
151 * units (1/8 or 1/16 bit time, depending on the oversampling rate)
152 * @param UART_DEDeassertionTime: Driver Enable deassertion time
153 * 5-bit value defining the time between the end of the last stop bit, in a
154 * transmitted message, and the de-activation of the DE (Driver Enable) signal.
155 * It is expressed in sample time units (1/8 or 1/16 bit time, depending on the
156 * oversampling rate).
159 HAL_StatusTypeDef HAL_RS485Ex_Init(UART_HandleTypeDef *huart, uint32_t UART_DEPolarity, uint32_t UART_DEAssertionTime, uint32_t UART_DEDeassertionTime)
163 /* Check the UART handle allocation */
164 if(huart == HAL_NULL)
168 /* Check the Driver Enable UART instance */
169 assert_param(IS_UART_DRIVER_ENABLE_INSTANCE(huart->Instance));
171 /* Check the Driver Enable polarity */
172 assert_param(IS_UART_DE_POLARITY(UART_DEPolarity));
174 /* Check the Driver Enable assertion time */
175 assert_param(IS_UART_ASSERTIONTIME(UART_DEAssertionTime));
177 /* Check the Driver Enable deassertion time */
178 assert_param(IS_UART_DEASSERTIONTIME(UART_DEDeassertionTime));
180 if(huart->State == HAL_UART_STATE_RESET)
182 /* Init the low level hardware : GPIO, CLOCK */
183 HAL_UART_MspInit(huart);
186 huart->State = HAL_UART_STATE_BUSY;
188 /* Disable the Peripheral */
189 __HAL_UART_DISABLE(huart);
191 /* Set the UART Communication parameters */
192 if (UART_SetConfig(huart) == HAL_ERROR)
197 if (huart->AdvancedInit.AdvFeatureInit != UART_ADVFEATURE_NO_INIT)
199 UART_AdvFeatureConfig(huart);
202 /* Enable the Driver Enable mode by setting the DEM bit in the CR3 register */
203 huart->Instance->CR3 |= USART_CR3_DEM;
205 /* Set the Driver Enable polarity */
206 MODIFY_REG(huart->Instance->CR3, USART_CR3_DEP, UART_DEPolarity);
208 /* Set the Driver Enable assertion and deassertion times */
209 temp = (UART_DEAssertionTime << UART_CR1_DEAT_ADDRESS_LSB_POS);
210 temp |= (UART_DEDeassertionTime << UART_CR1_DEDT_ADDRESS_LSB_POS);
211 MODIFY_REG(huart->Instance->CR1, (USART_CR1_DEDT|USART_CR1_DEAT), temp);
213 /* Enable the Peripheral */
214 __HAL_UART_ENABLE(huart);
216 /* TEACK and/or REACK to check before moving huart->State to Ready */
217 return (UART_CheckIdleState(huart));
225 /** @defgroup UARTEx_Exported_Functions_Group2 Extended Peripheral Control functions
226 * @brief Extended Peripheral Control functions
229 ===============================================================================
230 ##### Peripheral Control functions #####
231 ===============================================================================
233 This subsection provides an extended function allowing to control the UART.
234 (+) HAL_MultiProcessorEx_AddressLength_Set() API optionally sets the UART node address
235 detection length to more than 4 bits for multiprocessor address mark wake up.
236 (+) HAL_UARTEx_StopModeWakeUpSourceConfig() configures the address for wake-up from
237 Stop mode based on address match
238 (+) HAL_UARTEx_EnableStopMode() API enables the UART to wake up the MCU from stop mode
239 (+) HAL_UARTEx_DisableStopMode() API disables the above functionality
247 * @brief By default in multiprocessor mode, when the wake up method is set
248 * to address mark, the UART handles only 4-bit long addresses detection.
249 * This API allows to enable longer addresses detection (6-, 7- or 8-bit
251 * - 6-bit address detection in 7-bit data mode
252 * - 7-bit address detection in 8-bit data mode
253 * - 8-bit address detection in 9-bit data mode
254 * @param huart: UART handle
255 * @param AddressLength: this parameter can be one of the following values:
256 * @arg UART_ADDRESS_DETECT_4B: 4-bit long address
257 * @arg UART_ADDRESS_DETECT_7B: 6-, 7- or 8-bit long address
260 HAL_StatusTypeDef HAL_MultiProcessorEx_AddressLength_Set(UART_HandleTypeDef *huart, uint32_t AddressLength)
262 /* Check the UART handle allocation */
263 if(huart == HAL_NULL)
268 /* Check the address length parameter */
269 assert_param(IS_UART_ADDRESSLENGTH_DETECT(AddressLength));
271 huart->State = HAL_UART_STATE_BUSY;
273 /* Disable the Peripheral */
274 __HAL_UART_DISABLE(huart);
276 /* Set the address length */
277 MODIFY_REG(huart->Instance->CR2, USART_CR2_ADDM7, AddressLength);
279 /* Enable the Peripheral */
280 __HAL_UART_ENABLE(huart);
282 /* TEACK and/or REACK to check before moving huart->State to Ready */
283 return (UART_CheckIdleState(huart));
288 * @brief Set Wakeup from Stop mode interrupt flag selection
289 * @param huart: uart handle,
290 * @param WakeUpSelection: address match, Start Bit detection or RXNE bit status.
291 * This parameter can be one of the following values:
292 * @arg UART_WAKEUP_ON_ADDRESS
293 * @arg UART_WAKEUP_ON_STARTBIT
294 * @arg UART_WAKEUP_ON_READDATA_NONEMPTY
297 HAL_StatusTypeDef HAL_UARTEx_StopModeWakeUpSourceConfig(UART_HandleTypeDef *huart, UART_WakeUpTypeDef WakeUpSelection)
300 /* check the wake-up from stop mode UART instance */
301 assert_param(IS_UART_WAKEUP_FROMSTOP_INSTANCE(huart->Instance));
302 /* check the wake-up selection parameter */
303 assert_param(IS_UART_WAKEUP_SELECTION(WakeUpSelection.WakeUpEvent));
308 huart->State = HAL_UART_STATE_BUSY;
310 /* Disable the Peripheral */
311 __HAL_UART_DISABLE(huart);
313 /* Set the wake-up selection scheme */
314 MODIFY_REG(huart->Instance->CR3, USART_CR3_WUS, WakeUpSelection.WakeUpEvent);
316 if (WakeUpSelection.WakeUpEvent == UART_WAKEUP_ON_ADDRESS)
318 UART_Wakeup_AddressConfig(huart, WakeUpSelection);
321 /* Enable the Peripheral */
322 __HAL_UART_ENABLE(huart);
324 /* Wait until REACK flag is set */
325 if(UART_WaitOnFlagUntilTimeout(huart, USART_ISR_REACK, RESET, HAL_UART_TIMEOUT_VALUE) != HAL_OK)
331 /* Initialize the UART State */
332 huart->State= HAL_UART_STATE_READY;
333 /* Process Unlocked */
341 * @brief Enable UART Stop Mode
342 * The UART is able to wake up the MCU from Stop mode as long as UART clock is HSI or LSE
343 * @param huart: uart handle
346 HAL_StatusTypeDef HAL_UARTEx_EnableStopMode(UART_HandleTypeDef *huart)
351 huart->State = HAL_UART_STATE_BUSY;
353 /* Set the USART UESM bit */
354 huart->Instance->CR1 |= USART_CR1_UESM;
356 huart->State = HAL_UART_STATE_READY;
358 /* Process Unlocked */
365 * @brief Disable UART Stop Mode
366 * @param huart: uart handle
369 HAL_StatusTypeDef HAL_UARTEx_DisableStopMode(UART_HandleTypeDef *huart)
374 huart->State = HAL_UART_STATE_BUSY;
376 /* Clear USART UESM bit */
377 huart->Instance->CR1 &= ~(USART_CR1_UESM);
379 huart->State = HAL_UART_STATE_READY;
381 /* Process Unlocked */
396 #endif /* HAL_UART_MODULE_ENABLED */
405 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/