TUKS MCU Introductory course / TUKS-COURSE-TIMER
Committer:
elmot
Date:
Fri Feb 24 21:13:56 2017 +0000
Revision:
1:d0dfbce63a89
Ready-to-copy

Who changed what in which revision?

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