2 ******************************************************************************
\r
3 * @file stm32f7xx_hal_uart_ex.c
\r
4 * @author MCD Application Team
\r
5 * @brief Extended UART HAL module driver.
\r
6 * This file provides firmware functions to manage the following extended
\r
7 * functionalities of the Universal Asynchronous Receiver Transmitter Peripheral (UART).
\r
8 * + Initialization and de-initialization functions
\r
9 * + Peripheral Control functions
\r
13 ==============================================================================
\r
14 ##### UART peripheral extended features #####
\r
15 ==============================================================================
\r
17 (#) Declare a UART_HandleTypeDef handle structure.
\r
19 (#) For the UART RS485 Driver Enable mode, initialize the UART registers
\r
20 by calling the HAL_RS485Ex_Init() API.
\r
23 ******************************************************************************
\r
26 * <h2><center>© Copyright (c) 2017 STMicroelectronics.
\r
27 * All rights reserved.</center></h2>
\r
29 * This software component is licensed by ST under BSD 3-Clause license,
\r
30 * the "License"; You may not use this file except in compliance with the
\r
31 * License. You may obtain a copy of the License at:
\r
32 * opensource.org/licenses/BSD-3-Clause
\r
34 ******************************************************************************
\r
37 /* Includes ------------------------------------------------------------------*/
\r
38 #include "stm32f7xx_hal.h"
\r
40 /** @addtogroup STM32F7xx_HAL_Driver
\r
44 /** @defgroup UARTEx UARTEx
\r
45 * @brief UART Extended HAL module driver
\r
49 #ifdef HAL_UART_MODULE_ENABLED
\r
51 /* Private typedef -----------------------------------------------------------*/
\r
52 /* Private define ------------------------------------------------------------*/
\r
54 /* Private macros ------------------------------------------------------------*/
\r
55 /* Private variables ---------------------------------------------------------*/
\r
56 /* Private function prototypes -----------------------------------------------*/
\r
57 /** @defgroup UARTEx_Private_Functions UARTEx Private Functions
\r
64 /* Exported functions --------------------------------------------------------*/
\r
66 /** @defgroup UARTEx_Exported_Functions UARTEx Exported Functions
\r
70 /** @defgroup UARTEx_Exported_Functions_Group1 Initialization and de-initialization functions
\r
71 * @brief Extended Initialization and Configuration Functions
\r
74 ===============================================================================
\r
75 ##### Initialization and Configuration functions #####
\r
76 ===============================================================================
\r
78 This subsection provides a set of functions allowing to initialize the USARTx or the UARTy
\r
79 in asynchronous mode.
\r
80 (+) For the asynchronous mode the parameters below can be configured:
\r
84 (++) Parity: If the parity is enabled, then the MSB bit of the data written
\r
85 in the data register is transmitted but is changed by the parity bit.
\r
86 (++) Hardware flow control
\r
87 (++) Receiver/transmitter modes
\r
88 (++) Over Sampling Method
\r
89 (++) One-Bit Sampling Method
\r
90 (+) For the asynchronous mode, the following advanced features can be configured as well:
\r
91 (++) TX and/or RX pin level inversion
\r
92 (++) data logical level inversion
\r
93 (++) RX and TX pins swap
\r
94 (++) RX overrun detection disabling
\r
95 (++) DMA disabling on RX error
\r
96 (++) MSB first on communication line
\r
97 (++) auto Baud rate detection
\r
99 The HAL_RS485Ex_Init() API follows the UART RS485 mode configuration
\r
100 procedures (details for the procedures are available in reference manual).
\r
104 Depending on the frame length defined by the M1 and M0 bits (7-bit,
\r
105 8-bit or 9-bit), the possible UART formats are listed in the
\r
108 Table 1. UART frame format.
\r
109 +-----------------------------------------------------------------------+
\r
110 | M1 bit | M0 bit | PCE bit | UART frame |
\r
111 |---------|---------|-----------|---------------------------------------|
\r
112 | 0 | 0 | 0 | | SB | 8 bit data | STB | |
\r
113 |---------|---------|-----------|---------------------------------------|
\r
114 | 0 | 0 | 1 | | SB | 7 bit data | PB | STB | |
\r
115 |---------|---------|-----------|---------------------------------------|
\r
116 | 0 | 1 | 0 | | SB | 9 bit data | STB | |
\r
117 |---------|---------|-----------|---------------------------------------|
\r
118 | 0 | 1 | 1 | | SB | 8 bit data | PB | STB | |
\r
119 |---------|---------|-----------|---------------------------------------|
\r
120 | 1 | 0 | 0 | | SB | 7 bit data | STB | |
\r
121 |---------|---------|-----------|---------------------------------------|
\r
122 | 1 | 0 | 1 | | SB | 6 bit data | PB | STB | |
\r
123 +-----------------------------------------------------------------------+
\r
129 * @brief Initialize the RS485 Driver enable feature according to the specified
\r
130 * parameters in the UART_InitTypeDef and creates the associated handle.
\r
131 * @param huart UART handle.
\r
132 * @param Polarity Select the driver enable polarity.
\r
133 * This parameter can be one of the following values:
\r
134 * @arg @ref UART_DE_POLARITY_HIGH DE signal is active high
\r
135 * @arg @ref UART_DE_POLARITY_LOW DE signal is active low
\r
136 * @param AssertionTime Driver Enable assertion time:
\r
137 * 5-bit value defining the time between the activation of the DE (Driver Enable)
\r
138 * signal and the beginning of the start bit. It is expressed in sample time
\r
139 * units (1/8 or 1/16 bit time, depending on the oversampling rate)
\r
140 * @param DeassertionTime Driver Enable deassertion time:
\r
141 * 5-bit value defining the time between the end of the last stop bit, in a
\r
142 * transmitted message, and the de-activation of the DE (Driver Enable) signal.
\r
143 * It is expressed in sample time units (1/8 or 1/16 bit time, depending on the
\r
144 * oversampling rate).
\r
145 * @retval HAL status
\r
147 HAL_StatusTypeDef HAL_RS485Ex_Init(UART_HandleTypeDef *huart, uint32_t Polarity, uint32_t AssertionTime, uint32_t DeassertionTime)
\r
151 /* Check the UART handle allocation */
\r
156 /* Check the Driver Enable UART instance */
\r
157 assert_param(IS_UART_DRIVER_ENABLE_INSTANCE(huart->Instance));
\r
159 /* Check the Driver Enable polarity */
\r
160 assert_param(IS_UART_DE_POLARITY(Polarity));
\r
162 /* Check the Driver Enable assertion time */
\r
163 assert_param(IS_UART_ASSERTIONTIME(AssertionTime));
\r
165 /* Check the Driver Enable deassertion time */
\r
166 assert_param(IS_UART_DEASSERTIONTIME(DeassertionTime));
\r
168 if (huart->gState == HAL_UART_STATE_RESET)
\r
170 /* Allocate lock resource and initialize it */
\r
171 huart->Lock = HAL_UNLOCKED;
\r
173 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
\r
174 UART_InitCallbacksToDefault(huart);
\r
176 if (huart->MspInitCallback == NULL)
\r
178 huart->MspInitCallback = HAL_UART_MspInit;
\r
181 /* Init the low level hardware */
\r
182 huart->MspInitCallback(huart);
\r
184 /* Init the low level hardware : GPIO, CLOCK, CORTEX */
\r
185 HAL_UART_MspInit(huart);
\r
186 #endif /* (USE_HAL_UART_REGISTER_CALLBACKS) */
\r
189 huart->gState = HAL_UART_STATE_BUSY;
\r
191 /* Disable the Peripheral */
\r
192 __HAL_UART_DISABLE(huart);
\r
194 /* Set the UART Communication parameters */
\r
195 if (UART_SetConfig(huart) == HAL_ERROR)
\r
200 if (huart->AdvancedInit.AdvFeatureInit != UART_ADVFEATURE_NO_INIT)
\r
202 UART_AdvFeatureConfig(huart);
\r
205 /* Enable the Driver Enable mode by setting the DEM bit in the CR3 register */
\r
206 SET_BIT(huart->Instance->CR3, USART_CR3_DEM);
\r
208 /* Set the Driver Enable polarity */
\r
209 MODIFY_REG(huart->Instance->CR3, USART_CR3_DEP, Polarity);
\r
211 /* Set the Driver Enable assertion and deassertion times */
\r
212 temp = (AssertionTime << UART_CR1_DEAT_ADDRESS_LSB_POS);
\r
213 temp |= (DeassertionTime << UART_CR1_DEDT_ADDRESS_LSB_POS);
\r
214 MODIFY_REG(huart->Instance->CR1, (USART_CR1_DEDT | USART_CR1_DEAT), temp);
\r
216 /* Enable the Peripheral */
\r
217 __HAL_UART_ENABLE(huart);
\r
219 /* TEACK and/or REACK to check before moving huart->gState and huart->RxState to Ready */
\r
220 return (UART_CheckIdleState(huart));
\r
227 /** @defgroup UARTEx_Exported_Functions_Group2 IO operation functions
\r
228 * @brief Extended functions
\r
231 ===============================================================================
\r
232 ##### IO operation functions #####
\r
233 ===============================================================================
\r
234 This subsection provides a set of Wakeup and FIFO mode related callback functions.
\r
245 /** @defgroup UARTEx_Exported_Functions_Group3 Peripheral Control functions
\r
246 * @brief Extended Peripheral Control functions
\r
249 ===============================================================================
\r
250 ##### Peripheral Control functions #####
\r
251 ===============================================================================
\r
252 [..] This section provides the following functions:
\r
253 (+) HAL_MultiProcessorEx_AddressLength_Set() API optionally sets the UART node address
\r
254 detection length to more than 4 bits for multiprocessor address mark wake up.
\r
264 * @brief By default in multiprocessor mode, when the wake up method is set
\r
265 * to address mark, the UART handles only 4-bit long addresses detection;
\r
266 * this API allows to enable longer addresses detection (6-, 7- or 8-bit
\r
268 * @note Addresses detection lengths are: 6-bit address detection in 7-bit data mode,
\r
269 * 7-bit address detection in 8-bit data mode, 8-bit address detection in 9-bit data mode.
\r
270 * @param huart UART handle.
\r
271 * @param AddressLength This parameter can be one of the following values:
\r
272 * @arg @ref UART_ADDRESS_DETECT_4B 4-bit long address
\r
273 * @arg @ref UART_ADDRESS_DETECT_7B 6-, 7- or 8-bit long address
\r
274 * @retval HAL status
\r
276 HAL_StatusTypeDef HAL_MultiProcessorEx_AddressLength_Set(UART_HandleTypeDef *huart, uint32_t AddressLength)
\r
278 /* Check the UART handle allocation */
\r
284 /* Check the address length parameter */
\r
285 assert_param(IS_UART_ADDRESSLENGTH_DETECT(AddressLength));
\r
287 huart->gState = HAL_UART_STATE_BUSY;
\r
289 /* Disable the Peripheral */
\r
290 __HAL_UART_DISABLE(huart);
\r
292 /* Set the address length */
\r
293 MODIFY_REG(huart->Instance->CR2, USART_CR2_ADDM7, AddressLength);
\r
295 /* Enable the Peripheral */
\r
296 __HAL_UART_ENABLE(huart);
\r
298 /* TEACK and/or REACK to check before moving huart->gState to Ready */
\r
299 return (UART_CheckIdleState(huart));
\r
312 /** @addtogroup UARTEx_Private_Functions
\r
320 #endif /* HAL_UART_MODULE_ENABLED */
\r
330 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
\r