mbed library sources

Fork of mbed-src by mbed official

Committer:
mbed_official
Date:
Mon Nov 03 10:30:07 2014 +0000
Revision:
381:5460fc57b6e4
Synchronized with git revision 02478cd1f27fc7b9643486472635eb515b2bca81

Full URL: https://github.com/mbedmicro/mbed/commit/02478cd1f27fc7b9643486472635eb515b2bca81/

Target: LPC1549 - Fix serial interrupt issues (issue report #616)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 381:5460fc57b6e4 1 /**
mbed_official 381:5460fc57b6e4 2 ******************************************************************************
mbed_official 381:5460fc57b6e4 3 * @file stm32f3xx_hal_uart.c
mbed_official 381:5460fc57b6e4 4 * @author MCD Application Team
mbed_official 381:5460fc57b6e4 5 * @version V1.1.0
mbed_official 381:5460fc57b6e4 6 * @date 12-Sept-2014
mbed_official 381:5460fc57b6e4 7 * @brief UART HAL module driver.
mbed_official 381:5460fc57b6e4 8 *
mbed_official 381:5460fc57b6e4 9 * This file provides firmware functions to manage the following
mbed_official 381:5460fc57b6e4 10 * functionalities of the Universal Asynchronous Receiver Transmitter Peripheral (UART).
mbed_official 381:5460fc57b6e4 11 * + Initialization and de-initialization functions
mbed_official 381:5460fc57b6e4 12 * + IO operation functions
mbed_official 381:5460fc57b6e4 13 * + Peripheral Control functions
mbed_official 381:5460fc57b6e4 14 *
mbed_official 381:5460fc57b6e4 15 *
mbed_official 381:5460fc57b6e4 16 @verbatim
mbed_official 381:5460fc57b6e4 17 ===============================================================================
mbed_official 381:5460fc57b6e4 18 ##### How to use this driver #####
mbed_official 381:5460fc57b6e4 19 ===============================================================================
mbed_official 381:5460fc57b6e4 20 [..]
mbed_official 381:5460fc57b6e4 21 The UART HAL driver can be used as follows:
mbed_official 381:5460fc57b6e4 22
mbed_official 381:5460fc57b6e4 23 (#) Declare a UART_HandleTypeDef handle structure.
mbed_official 381:5460fc57b6e4 24 (#) Initialize the UART low level resources by implementing the HAL_UART_MspInit ()API:
mbed_official 381:5460fc57b6e4 25 (##) Enable the USARTx interface clock.
mbed_official 381:5460fc57b6e4 26 (##) UART pins configuration:
mbed_official 381:5460fc57b6e4 27 (+) Enable the clock for the UART GPIOs.
mbed_official 381:5460fc57b6e4 28 (+) Configure these UART pins as alternate function pull-up.
mbed_official 381:5460fc57b6e4 29 (##) NVIC configuration if you need to use interrupt process (HAL_UART_Transmit_IT()
mbed_official 381:5460fc57b6e4 30 and HAL_UART_Receive_IT() APIs):
mbed_official 381:5460fc57b6e4 31 (+) Configure the USARTx interrupt priority.
mbed_official 381:5460fc57b6e4 32 (+) Enable the NVIC USART IRQ handle.
mbed_official 381:5460fc57b6e4 33 (@) The specific UART interrupts (Transmission complete interrupt,
mbed_official 381:5460fc57b6e4 34 RXNE interrupt and Error Interrupts) will be managed using the macros
mbed_official 381:5460fc57b6e4 35 __HAL_UART_ENABLE_IT() and __HAL_UART_DISABLE_IT() inside the transmit and receive process.
mbed_official 381:5460fc57b6e4 36 (##) DMA Configuration if you need to use DMA process (HAL_UART_Transmit_DMA()
mbed_official 381:5460fc57b6e4 37 and HAL_UART_Receive_DMA() APIs):
mbed_official 381:5460fc57b6e4 38 (+) Declare a DMA handle structure for the Tx/Rx channel.
mbed_official 381:5460fc57b6e4 39 (+) Enable the DMAx interface clock.
mbed_official 381:5460fc57b6e4 40 (+) Configure the declared DMA handle structure with the required Tx/Rx parameters.
mbed_official 381:5460fc57b6e4 41 (+) Configure the DMA Tx/Rx channel.
mbed_official 381:5460fc57b6e4 42 (+) Associate the initilalized DMA handle to the UART DMA Tx/Rx handle.
mbed_official 381:5460fc57b6e4 43 (+) Configure the priority and enable the NVIC for the transfer complete interrupt on the DMA Tx/Rx channel.
mbed_official 381:5460fc57b6e4 44
mbed_official 381:5460fc57b6e4 45 (#) Program the Baud Rate, Word Length , Stop Bit, Parity, Hardware
mbed_official 381:5460fc57b6e4 46 flow control and Mode(Receiver/Transmitter) in the huart Init structure.
mbed_official 381:5460fc57b6e4 47
mbed_official 381:5460fc57b6e4 48 (#) If required, program UART advanced features (TX/RX pins swap, auto Baud rate detection,...)
mbed_official 381:5460fc57b6e4 49 in the huart AdvancedInit structure.
mbed_official 381:5460fc57b6e4 50
mbed_official 381:5460fc57b6e4 51 (#) For the UART asynchronous mode, initialize the UART registers by calling
mbed_official 381:5460fc57b6e4 52 the HAL_UART_Init() API.
mbed_official 381:5460fc57b6e4 53
mbed_official 381:5460fc57b6e4 54 (#) For the UART Half duplex mode, initialize the UART registers by calling
mbed_official 381:5460fc57b6e4 55 the HAL_HalfDuplex_Init() API.
mbed_official 381:5460fc57b6e4 56
mbed_official 381:5460fc57b6e4 57 (#) For the UART LIN (Local Interconnection Network) mode, initialize the UART registers
mbed_official 381:5460fc57b6e4 58 by calling the HAL_LIN_Init() API.
mbed_official 381:5460fc57b6e4 59
mbed_official 381:5460fc57b6e4 60 (#) For the UART Multiprocessor mode, initialize the UART registers
mbed_official 381:5460fc57b6e4 61 by calling the HAL_MultiProcessor_Init() API.
mbed_official 381:5460fc57b6e4 62
mbed_official 381:5460fc57b6e4 63 (#) For the UART RS485 Driver Enabled mode, initialize the UART registers
mbed_official 381:5460fc57b6e4 64 by calling the HAL_RS485Ex_Init() API.
mbed_official 381:5460fc57b6e4 65
mbed_official 381:5460fc57b6e4 66 (@) These API's(HAL_UART_Init(), HAL_HalfDuplex_Init(), HAL_LIN_Init(), HAL_MultiProcessor_Init(),
mbed_official 381:5460fc57b6e4 67 also configure also the low level Hardware GPIO, CLOCK, CORTEX...etc) by
mbed_official 381:5460fc57b6e4 68 calling the customized HAL_UART_MspInit() API.
mbed_official 381:5460fc57b6e4 69
mbed_official 381:5460fc57b6e4 70 @endverbatim
mbed_official 381:5460fc57b6e4 71 ******************************************************************************
mbed_official 381:5460fc57b6e4 72 * @attention
mbed_official 381:5460fc57b6e4 73 *
mbed_official 381:5460fc57b6e4 74 * <h2><center>&copy; COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
mbed_official 381:5460fc57b6e4 75 *
mbed_official 381:5460fc57b6e4 76 * Redistribution and use in source and binary forms, with or without modification,
mbed_official 381:5460fc57b6e4 77 * are permitted provided that the following conditions are met:
mbed_official 381:5460fc57b6e4 78 * 1. Redistributions of source code must retain the above copyright notice,
mbed_official 381:5460fc57b6e4 79 * this list of conditions and the following disclaimer.
mbed_official 381:5460fc57b6e4 80 * 2. Redistributions in binary form must reproduce the above copyright notice,
mbed_official 381:5460fc57b6e4 81 * this list of conditions and the following disclaimer in the documentation
mbed_official 381:5460fc57b6e4 82 * and/or other materials provided with the distribution.
mbed_official 381:5460fc57b6e4 83 * 3. Neither the name of STMicroelectronics nor the names of its contributors
mbed_official 381:5460fc57b6e4 84 * may be used to endorse or promote products derived from this software
mbed_official 381:5460fc57b6e4 85 * without specific prior written permission.
mbed_official 381:5460fc57b6e4 86 *
mbed_official 381:5460fc57b6e4 87 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
mbed_official 381:5460fc57b6e4 88 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
mbed_official 381:5460fc57b6e4 89 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
mbed_official 381:5460fc57b6e4 90 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
mbed_official 381:5460fc57b6e4 91 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
mbed_official 381:5460fc57b6e4 92 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
mbed_official 381:5460fc57b6e4 93 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
mbed_official 381:5460fc57b6e4 94 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
mbed_official 381:5460fc57b6e4 95 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
mbed_official 381:5460fc57b6e4 96 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
mbed_official 381:5460fc57b6e4 97 *
mbed_official 381:5460fc57b6e4 98 ******************************************************************************
mbed_official 381:5460fc57b6e4 99 */
mbed_official 381:5460fc57b6e4 100
mbed_official 381:5460fc57b6e4 101 /* Includes ------------------------------------------------------------------*/
mbed_official 381:5460fc57b6e4 102 #include "stm32f3xx_hal.h"
mbed_official 381:5460fc57b6e4 103
mbed_official 381:5460fc57b6e4 104 /** @addtogroup STM32F3xx_HAL_Driver
mbed_official 381:5460fc57b6e4 105 * @{
mbed_official 381:5460fc57b6e4 106 */
mbed_official 381:5460fc57b6e4 107
mbed_official 381:5460fc57b6e4 108 /** @defgroup UART UART HAL module driver
mbed_official 381:5460fc57b6e4 109 * @brief UART HAL module driver
mbed_official 381:5460fc57b6e4 110 * @{
mbed_official 381:5460fc57b6e4 111 */
mbed_official 381:5460fc57b6e4 112 #ifdef HAL_UART_MODULE_ENABLED
mbed_official 381:5460fc57b6e4 113
mbed_official 381:5460fc57b6e4 114 /* Private typedef -----------------------------------------------------------*/
mbed_official 381:5460fc57b6e4 115 /* Private define ------------------------------------------------------------*/
mbed_official 381:5460fc57b6e4 116 /** @defgroup UART_Private_Constants UART Private Constants
mbed_official 381:5460fc57b6e4 117 * @{
mbed_official 381:5460fc57b6e4 118 */
mbed_official 381:5460fc57b6e4 119 #define HAL_UART_TXDMA_TIMEOUTVALUE 22000
mbed_official 381:5460fc57b6e4 120 #define UART_CR1_FIELDS ((uint32_t)(USART_CR1_M | USART_CR1_PCE | USART_CR1_PS | \
mbed_official 381:5460fc57b6e4 121 USART_CR1_TE | USART_CR1_RE | USART_CR1_OVER8))
mbed_official 381:5460fc57b6e4 122 /**
mbed_official 381:5460fc57b6e4 123 * @}
mbed_official 381:5460fc57b6e4 124 */
mbed_official 381:5460fc57b6e4 125
mbed_official 381:5460fc57b6e4 126 /* Private macro -------------------------------------------------------------*/
mbed_official 381:5460fc57b6e4 127 /* Private variables ---------------------------------------------------------*/
mbed_official 381:5460fc57b6e4 128 /* Private function prototypes -----------------------------------------------*/
mbed_official 381:5460fc57b6e4 129 static void UART_DMATransmitCplt(DMA_HandleTypeDef *hdma);
mbed_official 381:5460fc57b6e4 130 static void UART_DMAReceiveCplt(DMA_HandleTypeDef *hdma);
mbed_official 381:5460fc57b6e4 131 static void UART_DMARxHalfCplt(DMA_HandleTypeDef *hdma);
mbed_official 381:5460fc57b6e4 132 static void UART_DMATxHalfCplt(DMA_HandleTypeDef *hdma);
mbed_official 381:5460fc57b6e4 133 static void UART_DMAError(DMA_HandleTypeDef *hdma);
mbed_official 381:5460fc57b6e4 134 static HAL_StatusTypeDef UART_Transmit_IT(UART_HandleTypeDef *huart);
mbed_official 381:5460fc57b6e4 135 static HAL_StatusTypeDef UART_EndTransmit_IT(UART_HandleTypeDef *huart);
mbed_official 381:5460fc57b6e4 136 static HAL_StatusTypeDef UART_Receive_IT(UART_HandleTypeDef *huart);
mbed_official 381:5460fc57b6e4 137 /* Exported functions ---------------------------------------------------------*/
mbed_official 381:5460fc57b6e4 138
mbed_official 381:5460fc57b6e4 139 /** @defgroup UART_Exported_Functions UART Exported Functions
mbed_official 381:5460fc57b6e4 140 * @{
mbed_official 381:5460fc57b6e4 141 */
mbed_official 381:5460fc57b6e4 142
mbed_official 381:5460fc57b6e4 143 /** @defgroup UART_Exported_Functions_Group1 Initialization and de-initialization functions
mbed_official 381:5460fc57b6e4 144 * @brief Initialization and Configuration functions
mbed_official 381:5460fc57b6e4 145 *
mbed_official 381:5460fc57b6e4 146 @verbatim
mbed_official 381:5460fc57b6e4 147 ===============================================================================
mbed_official 381:5460fc57b6e4 148 ##### Initialization and Configuration functions #####
mbed_official 381:5460fc57b6e4 149 ===============================================================================
mbed_official 381:5460fc57b6e4 150 [..]
mbed_official 381:5460fc57b6e4 151 This subsection provides a set of functions allowing to initialize the USARTx or the UARTy
mbed_official 381:5460fc57b6e4 152 in asynchronous mode.
mbed_official 381:5460fc57b6e4 153 (+) For the asynchronous mode the parameters below can be configured:
mbed_official 381:5460fc57b6e4 154 (++) Baud Rate
mbed_official 381:5460fc57b6e4 155 (++) Word Length
mbed_official 381:5460fc57b6e4 156 (++) Stop Bit
mbed_official 381:5460fc57b6e4 157 (++) Parity: If the parity is enabled, then the MSB bit of the data written
mbed_official 381:5460fc57b6e4 158 in the data register is transmitted but is changed by the parity bit.
mbed_official 381:5460fc57b6e4 159 Depending on the frame length defined by the M bit (8-bits or 9-bits)
mbed_official 381:5460fc57b6e4 160 or by the M1 and M0 bits (7-bit, 8-bit or 9-bit),
mbed_official 381:5460fc57b6e4 161 the possible UART frame formats are as listed in the following table:
mbed_official 381:5460fc57b6e4 162 +---------------------------------------------------------------+
mbed_official 381:5460fc57b6e4 163 | M bit | PCE bit | UART frame |
mbed_official 381:5460fc57b6e4 164 |-----------|-----------|---------------------------------------|
mbed_official 381:5460fc57b6e4 165 | 0 | 0 | | SB | 8-bit data | STB | |
mbed_official 381:5460fc57b6e4 166 |-----------|-----------|---------------------------------------|
mbed_official 381:5460fc57b6e4 167 | 0 | 1 | | SB | 7-bit data | PB | STB | |
mbed_official 381:5460fc57b6e4 168 |-----------|-----------|---------------------------------------|
mbed_official 381:5460fc57b6e4 169 | 1 | 0 | | SB | 9-bit data | STB | |
mbed_official 381:5460fc57b6e4 170 |-----------|-----------|---------------------------------------|
mbed_official 381:5460fc57b6e4 171 | 1 | 1 | | SB | 8-bit data | PB | STB | |
mbed_official 381:5460fc57b6e4 172 +---------------------------------------------------------------+
mbed_official 381:5460fc57b6e4 173 | M1M0 bits | PCE bit | UART frame |
mbed_official 381:5460fc57b6e4 174 |-----------------------|---------------------------------------|
mbed_official 381:5460fc57b6e4 175 | 10 | 0 | | SB | 7-bit data | STB | |
mbed_official 381:5460fc57b6e4 176 |-----------|-----------|---------------------------------------|
mbed_official 381:5460fc57b6e4 177 | 10 | 1 | | SB | 6-bit data | PB | STB | |
mbed_official 381:5460fc57b6e4 178 +---------------------------------------------------------------+
mbed_official 381:5460fc57b6e4 179 (++) Hardware flow control
mbed_official 381:5460fc57b6e4 180 (++) Receiver/transmitter modes
mbed_official 381:5460fc57b6e4 181 (++) Over Sampling Method
mbed_official 381:5460fc57b6e4 182 (++) One-Bit Sampling Method
mbed_official 381:5460fc57b6e4 183 (+) For the asynchronous mode, the following advanced features can be configured as well:
mbed_official 381:5460fc57b6e4 184 (++) TX and/or RX pin level inversion
mbed_official 381:5460fc57b6e4 185 (++) data logical level inversion
mbed_official 381:5460fc57b6e4 186 (++) RX and TX pins swap
mbed_official 381:5460fc57b6e4 187 (++) RX overrun detection disabling
mbed_official 381:5460fc57b6e4 188 (++) DMA disabling on RX error
mbed_official 381:5460fc57b6e4 189 (++) MSB first on communication line
mbed_official 381:5460fc57b6e4 190 (++) auto Baud rate detection
mbed_official 381:5460fc57b6e4 191 [..]
mbed_official 381:5460fc57b6e4 192 The HAL_UART_Init(), HAL_HalfDuplex_Init(), HAL_LIN_Init() and HAL_MultiProcessor_Init()
mbed_official 381:5460fc57b6e4 193 API follow respectively the UART asynchronous, UART Half duplex, UART LIN mode and
mbed_official 381:5460fc57b6e4 194 multiprocessor configuration procedures (details for the procedures are available in reference manual).
mbed_official 381:5460fc57b6e4 195
mbed_official 381:5460fc57b6e4 196 @endverbatim
mbed_official 381:5460fc57b6e4 197 * @{
mbed_official 381:5460fc57b6e4 198 */
mbed_official 381:5460fc57b6e4 199
mbed_official 381:5460fc57b6e4 200 /**
mbed_official 381:5460fc57b6e4 201 * @brief Initializes the UART mode according to the specified
mbed_official 381:5460fc57b6e4 202 * parameters in the UART_InitTypeDef and creates the associated handle .
mbed_official 381:5460fc57b6e4 203 * @param huart: uart handle
mbed_official 381:5460fc57b6e4 204 * @retval HAL status
mbed_official 381:5460fc57b6e4 205 */
mbed_official 381:5460fc57b6e4 206 HAL_StatusTypeDef HAL_UART_Init(UART_HandleTypeDef *huart)
mbed_official 381:5460fc57b6e4 207 {
mbed_official 381:5460fc57b6e4 208 /* Check the UART handle allocation */
mbed_official 381:5460fc57b6e4 209 if(huart == HAL_NULL)
mbed_official 381:5460fc57b6e4 210 {
mbed_official 381:5460fc57b6e4 211 return HAL_ERROR;
mbed_official 381:5460fc57b6e4 212 }
mbed_official 381:5460fc57b6e4 213
mbed_official 381:5460fc57b6e4 214 if(huart->Init.HwFlowCtl != UART_HWCONTROL_NONE)
mbed_official 381:5460fc57b6e4 215 {
mbed_official 381:5460fc57b6e4 216 /* Check the parameters */
mbed_official 381:5460fc57b6e4 217 assert_param(IS_UART_HWFLOW_INSTANCE(huart->Instance));
mbed_official 381:5460fc57b6e4 218 }
mbed_official 381:5460fc57b6e4 219 else
mbed_official 381:5460fc57b6e4 220 {
mbed_official 381:5460fc57b6e4 221 /* Check the parameters */
mbed_official 381:5460fc57b6e4 222 assert_param(IS_UART_INSTANCE(huart->Instance));
mbed_official 381:5460fc57b6e4 223 }
mbed_official 381:5460fc57b6e4 224
mbed_official 381:5460fc57b6e4 225 if(huart->State == HAL_UART_STATE_RESET)
mbed_official 381:5460fc57b6e4 226 {
mbed_official 381:5460fc57b6e4 227 /* Init the low level hardware : GPIO, CLOCK */
mbed_official 381:5460fc57b6e4 228 HAL_UART_MspInit(huart);
mbed_official 381:5460fc57b6e4 229 }
mbed_official 381:5460fc57b6e4 230
mbed_official 381:5460fc57b6e4 231 huart->State = HAL_UART_STATE_BUSY;
mbed_official 381:5460fc57b6e4 232
mbed_official 381:5460fc57b6e4 233 /* Disable the Peripheral */
mbed_official 381:5460fc57b6e4 234 __HAL_UART_DISABLE(huart);
mbed_official 381:5460fc57b6e4 235
mbed_official 381:5460fc57b6e4 236 /* Set the UART Communication parameters */
mbed_official 381:5460fc57b6e4 237 if (UART_SetConfig(huart) == HAL_ERROR)
mbed_official 381:5460fc57b6e4 238 {
mbed_official 381:5460fc57b6e4 239 return HAL_ERROR;
mbed_official 381:5460fc57b6e4 240 }
mbed_official 381:5460fc57b6e4 241
mbed_official 381:5460fc57b6e4 242 if (huart->AdvancedInit.AdvFeatureInit != UART_ADVFEATURE_NO_INIT)
mbed_official 381:5460fc57b6e4 243 {
mbed_official 381:5460fc57b6e4 244 UART_AdvFeatureConfig(huart);
mbed_official 381:5460fc57b6e4 245 }
mbed_official 381:5460fc57b6e4 246
mbed_official 381:5460fc57b6e4 247 /* In asynchronous mode, the following bits must be kept cleared:
mbed_official 381:5460fc57b6e4 248 - LINEN and CLKEN bits in the USART_CR2 register,
mbed_official 381:5460fc57b6e4 249 - SCEN, HDSEL and IREN bits in the USART_CR3 register.*/
mbed_official 381:5460fc57b6e4 250 huart->Instance->CR2 &= ~(USART_CR2_LINEN | USART_CR2_CLKEN);
mbed_official 381:5460fc57b6e4 251 huart->Instance->CR3 &= ~(USART_CR3_SCEN | USART_CR3_HDSEL | USART_CR3_IREN);
mbed_official 381:5460fc57b6e4 252
mbed_official 381:5460fc57b6e4 253 /* Enable the Peripheral */
mbed_official 381:5460fc57b6e4 254 __HAL_UART_ENABLE(huart);
mbed_official 381:5460fc57b6e4 255
mbed_official 381:5460fc57b6e4 256 /* TEACK and/or REACK to check before moving huart->State to Ready */
mbed_official 381:5460fc57b6e4 257 return (UART_CheckIdleState(huart));
mbed_official 381:5460fc57b6e4 258 }
mbed_official 381:5460fc57b6e4 259
mbed_official 381:5460fc57b6e4 260 /**
mbed_official 381:5460fc57b6e4 261 * @brief Initializes the half-duplex mode according to the specified
mbed_official 381:5460fc57b6e4 262 * parameters in the UART_InitTypeDef and creates the associated handle .
mbed_official 381:5460fc57b6e4 263 * @param huart: uart handle
mbed_official 381:5460fc57b6e4 264 * @retval HAL status
mbed_official 381:5460fc57b6e4 265 */
mbed_official 381:5460fc57b6e4 266 HAL_StatusTypeDef HAL_HalfDuplex_Init(UART_HandleTypeDef *huart)
mbed_official 381:5460fc57b6e4 267 {
mbed_official 381:5460fc57b6e4 268 /* Check the UART handle allocation */
mbed_official 381:5460fc57b6e4 269 if(huart == HAL_NULL)
mbed_official 381:5460fc57b6e4 270 {
mbed_official 381:5460fc57b6e4 271 return HAL_ERROR;
mbed_official 381:5460fc57b6e4 272 }
mbed_official 381:5460fc57b6e4 273
mbed_official 381:5460fc57b6e4 274 /* Check UART instance */
mbed_official 381:5460fc57b6e4 275 assert_param(IS_UART_HALFDUPLEX_INSTANCE(huart->Instance));
mbed_official 381:5460fc57b6e4 276
mbed_official 381:5460fc57b6e4 277 if(huart->State == HAL_UART_STATE_RESET)
mbed_official 381:5460fc57b6e4 278 {
mbed_official 381:5460fc57b6e4 279 /* Init the low level hardware : GPIO, CLOCK */
mbed_official 381:5460fc57b6e4 280 HAL_UART_MspInit(huart);
mbed_official 381:5460fc57b6e4 281 }
mbed_official 381:5460fc57b6e4 282
mbed_official 381:5460fc57b6e4 283 huart->State = HAL_UART_STATE_BUSY;
mbed_official 381:5460fc57b6e4 284
mbed_official 381:5460fc57b6e4 285 /* Disable the Peripheral */
mbed_official 381:5460fc57b6e4 286 __HAL_UART_DISABLE(huart);
mbed_official 381:5460fc57b6e4 287
mbed_official 381:5460fc57b6e4 288 /* Set the UART Communication parameters */
mbed_official 381:5460fc57b6e4 289 if (UART_SetConfig(huart) == HAL_ERROR)
mbed_official 381:5460fc57b6e4 290 {
mbed_official 381:5460fc57b6e4 291 return HAL_ERROR;
mbed_official 381:5460fc57b6e4 292 }
mbed_official 381:5460fc57b6e4 293
mbed_official 381:5460fc57b6e4 294 if (huart->AdvancedInit.AdvFeatureInit != UART_ADVFEATURE_NO_INIT)
mbed_official 381:5460fc57b6e4 295 {
mbed_official 381:5460fc57b6e4 296 UART_AdvFeatureConfig(huart);
mbed_official 381:5460fc57b6e4 297 }
mbed_official 381:5460fc57b6e4 298
mbed_official 381:5460fc57b6e4 299 /* In half-duplex mode, the following bits must be kept cleared:
mbed_official 381:5460fc57b6e4 300 - LINEN and CLKEN bits in the USART_CR2 register,
mbed_official 381:5460fc57b6e4 301 - SCEN and IREN bits in the USART_CR3 register.*/
mbed_official 381:5460fc57b6e4 302 huart->Instance->CR2 &= ~(USART_CR2_LINEN | USART_CR2_CLKEN);
mbed_official 381:5460fc57b6e4 303 huart->Instance->CR3 &= ~(USART_CR3_IREN | USART_CR3_SCEN);
mbed_official 381:5460fc57b6e4 304
mbed_official 381:5460fc57b6e4 305 /* Enable the Half-Duplex mode by setting the HDSEL bit in the CR3 register */
mbed_official 381:5460fc57b6e4 306 huart->Instance->CR3 |= USART_CR3_HDSEL;
mbed_official 381:5460fc57b6e4 307
mbed_official 381:5460fc57b6e4 308 /* Enable the Peripheral */
mbed_official 381:5460fc57b6e4 309 __HAL_UART_ENABLE(huart);
mbed_official 381:5460fc57b6e4 310
mbed_official 381:5460fc57b6e4 311 /* TEACK and/or REACK to check before moving huart->State to Ready */
mbed_official 381:5460fc57b6e4 312 return (UART_CheckIdleState(huart));
mbed_official 381:5460fc57b6e4 313 }
mbed_official 381:5460fc57b6e4 314
mbed_official 381:5460fc57b6e4 315
mbed_official 381:5460fc57b6e4 316 /**
mbed_official 381:5460fc57b6e4 317 * @brief Initializes the LIN mode according to the specified
mbed_official 381:5460fc57b6e4 318 * parameters in the UART_InitTypeDef and creates the associated handle .
mbed_official 381:5460fc57b6e4 319 * @param huart: uart handle
mbed_official 381:5460fc57b6e4 320 * @param BreakDetectLength: specifies the LIN break detection length.
mbed_official 381:5460fc57b6e4 321 * This parameter can be one of the following values:
mbed_official 381:5460fc57b6e4 322 * @arg UART_LINBREAKDETECTLENGTH_10B: 10-bit break detection
mbed_official 381:5460fc57b6e4 323 * @arg UART_LINBREAKDETECTLENGTH_11B: 11-bit break detection
mbed_official 381:5460fc57b6e4 324 * @retval HAL status
mbed_official 381:5460fc57b6e4 325 */
mbed_official 381:5460fc57b6e4 326 HAL_StatusTypeDef HAL_LIN_Init(UART_HandleTypeDef *huart, uint32_t BreakDetectLength)
mbed_official 381:5460fc57b6e4 327 {
mbed_official 381:5460fc57b6e4 328 /* Check the UART handle allocation */
mbed_official 381:5460fc57b6e4 329 if(huart == HAL_NULL)
mbed_official 381:5460fc57b6e4 330 {
mbed_official 381:5460fc57b6e4 331 return HAL_ERROR;
mbed_official 381:5460fc57b6e4 332 }
mbed_official 381:5460fc57b6e4 333
mbed_official 381:5460fc57b6e4 334 /* Check the LIN UART instance */
mbed_official 381:5460fc57b6e4 335 assert_param(IS_UART_LIN_INSTANCE(huart->Instance));
mbed_official 381:5460fc57b6e4 336 /* Check the Break detection length parameter */
mbed_official 381:5460fc57b6e4 337 assert_param(IS_UART_LIN_BREAK_DETECT_LENGTH(BreakDetectLength));
mbed_official 381:5460fc57b6e4 338
mbed_official 381:5460fc57b6e4 339 /* LIN mode limited to 16-bit oversampling only */
mbed_official 381:5460fc57b6e4 340 if(huart->Init.OverSampling == UART_OVERSAMPLING_8)
mbed_official 381:5460fc57b6e4 341 {
mbed_official 381:5460fc57b6e4 342 return HAL_ERROR;
mbed_official 381:5460fc57b6e4 343 }
mbed_official 381:5460fc57b6e4 344
mbed_official 381:5460fc57b6e4 345 if(huart->State == HAL_UART_STATE_RESET)
mbed_official 381:5460fc57b6e4 346 {
mbed_official 381:5460fc57b6e4 347 /* Init the low level hardware : GPIO, CLOCK */
mbed_official 381:5460fc57b6e4 348 HAL_UART_MspInit(huart);
mbed_official 381:5460fc57b6e4 349 }
mbed_official 381:5460fc57b6e4 350
mbed_official 381:5460fc57b6e4 351 huart->State = HAL_UART_STATE_BUSY;
mbed_official 381:5460fc57b6e4 352
mbed_official 381:5460fc57b6e4 353 /* Disable the Peripheral */
mbed_official 381:5460fc57b6e4 354 __HAL_UART_DISABLE(huart);
mbed_official 381:5460fc57b6e4 355
mbed_official 381:5460fc57b6e4 356 /* Set the UART Communication parameters */
mbed_official 381:5460fc57b6e4 357 if (UART_SetConfig(huart) == HAL_ERROR)
mbed_official 381:5460fc57b6e4 358 {
mbed_official 381:5460fc57b6e4 359 return HAL_ERROR;
mbed_official 381:5460fc57b6e4 360 }
mbed_official 381:5460fc57b6e4 361
mbed_official 381:5460fc57b6e4 362 if (huart->AdvancedInit.AdvFeatureInit != UART_ADVFEATURE_NO_INIT)
mbed_official 381:5460fc57b6e4 363 {
mbed_official 381:5460fc57b6e4 364 UART_AdvFeatureConfig(huart);
mbed_official 381:5460fc57b6e4 365 }
mbed_official 381:5460fc57b6e4 366
mbed_official 381:5460fc57b6e4 367 /* In LIN mode, the following bits must be kept cleared:
mbed_official 381:5460fc57b6e4 368 - LINEN and CLKEN bits in the USART_CR2 register,
mbed_official 381:5460fc57b6e4 369 - SCEN and IREN bits in the USART_CR3 register.*/
mbed_official 381:5460fc57b6e4 370 huart->Instance->CR2 &= ~(USART_CR2_CLKEN);
mbed_official 381:5460fc57b6e4 371 huart->Instance->CR3 &= ~(USART_CR3_HDSEL | USART_CR3_IREN | USART_CR3_SCEN);
mbed_official 381:5460fc57b6e4 372
mbed_official 381:5460fc57b6e4 373 /* Enable the LIN mode by setting the LINEN bit in the CR2 register */
mbed_official 381:5460fc57b6e4 374 huart->Instance->CR2 |= USART_CR2_LINEN;
mbed_official 381:5460fc57b6e4 375
mbed_official 381:5460fc57b6e4 376 /* Set the USART LIN Break detection length. */
mbed_official 381:5460fc57b6e4 377 MODIFY_REG(huart->Instance->CR2, USART_CR2_LBDL, BreakDetectLength);
mbed_official 381:5460fc57b6e4 378
mbed_official 381:5460fc57b6e4 379 /* Enable the Peripheral */
mbed_official 381:5460fc57b6e4 380 __HAL_UART_ENABLE(huart);
mbed_official 381:5460fc57b6e4 381
mbed_official 381:5460fc57b6e4 382 /* TEACK and/or REACK to check before moving huart->State to Ready */
mbed_official 381:5460fc57b6e4 383 return (UART_CheckIdleState(huart));
mbed_official 381:5460fc57b6e4 384 }
mbed_official 381:5460fc57b6e4 385
mbed_official 381:5460fc57b6e4 386
mbed_official 381:5460fc57b6e4 387
mbed_official 381:5460fc57b6e4 388 /**
mbed_official 381:5460fc57b6e4 389 * @brief Initializes the multiprocessor mode according to the specified
mbed_official 381:5460fc57b6e4 390 * parameters in the UART_InitTypeDef and creates the associated handle.
mbed_official 381:5460fc57b6e4 391 * @param huart: UART handle
mbed_official 381:5460fc57b6e4 392 * @param Address: UART node address (4-, 6-, 7- or 8-bit long)
mbed_official 381:5460fc57b6e4 393 * @param WakeUpMethod: specifies the UART wakeup method.
mbed_official 381:5460fc57b6e4 394 * This parameter can be one of the following values:
mbed_official 381:5460fc57b6e4 395 * @arg UART_WAKEUPMETHOD_IDLELINE: WakeUp by an idle line detection
mbed_official 381:5460fc57b6e4 396 * @arg UART_WAKEUPMETHOD_ADDRESSMARK: WakeUp by an address mark
mbed_official 381:5460fc57b6e4 397 * @note If the user resorts to idle line detection wake up, the Address parameter
mbed_official 381:5460fc57b6e4 398 * is useless and ignored by the initialization function.
mbed_official 381:5460fc57b6e4 399 * @note If the user resorts to address mark wake up, the address length detection
mbed_official 381:5460fc57b6e4 400 * is configured by default to 4 bits only. For the UART to be able to
mbed_official 381:5460fc57b6e4 401 * manage 6-, 7- or 8-bit long addresses detection, the API
mbed_official 381:5460fc57b6e4 402 * HAL_MultiProcessorEx_AddressLength_Set() must be called after
mbed_official 381:5460fc57b6e4 403 * HAL_MultiProcessor_Init().
mbed_official 381:5460fc57b6e4 404 * @retval HAL status
mbed_official 381:5460fc57b6e4 405 */
mbed_official 381:5460fc57b6e4 406 HAL_StatusTypeDef HAL_MultiProcessor_Init(UART_HandleTypeDef *huart, uint8_t Address, uint32_t WakeUpMethod)
mbed_official 381:5460fc57b6e4 407 {
mbed_official 381:5460fc57b6e4 408 /* Check the UART handle allocation */
mbed_official 381:5460fc57b6e4 409 if(huart == HAL_NULL)
mbed_official 381:5460fc57b6e4 410 {
mbed_official 381:5460fc57b6e4 411 return HAL_ERROR;
mbed_official 381:5460fc57b6e4 412 }
mbed_official 381:5460fc57b6e4 413
mbed_official 381:5460fc57b6e4 414 /* Check the wake up method parameter */
mbed_official 381:5460fc57b6e4 415 assert_param(IS_UART_WAKEUPMETHOD(WakeUpMethod));
mbed_official 381:5460fc57b6e4 416
mbed_official 381:5460fc57b6e4 417 if(huart->State == HAL_UART_STATE_RESET)
mbed_official 381:5460fc57b6e4 418 {
mbed_official 381:5460fc57b6e4 419 /* Init the low level hardware : GPIO, CLOCK */
mbed_official 381:5460fc57b6e4 420 HAL_UART_MspInit(huart);
mbed_official 381:5460fc57b6e4 421 }
mbed_official 381:5460fc57b6e4 422
mbed_official 381:5460fc57b6e4 423 huart->State = HAL_UART_STATE_BUSY;
mbed_official 381:5460fc57b6e4 424
mbed_official 381:5460fc57b6e4 425 /* Disable the Peripheral */
mbed_official 381:5460fc57b6e4 426 __HAL_UART_DISABLE(huart);
mbed_official 381:5460fc57b6e4 427
mbed_official 381:5460fc57b6e4 428 /* Set the UART Communication parameters */
mbed_official 381:5460fc57b6e4 429 if (UART_SetConfig(huart) == HAL_ERROR)
mbed_official 381:5460fc57b6e4 430 {
mbed_official 381:5460fc57b6e4 431 return HAL_ERROR;
mbed_official 381:5460fc57b6e4 432 }
mbed_official 381:5460fc57b6e4 433
mbed_official 381:5460fc57b6e4 434 if (huart->AdvancedInit.AdvFeatureInit != UART_ADVFEATURE_NO_INIT)
mbed_official 381:5460fc57b6e4 435 {
mbed_official 381:5460fc57b6e4 436 UART_AdvFeatureConfig(huart);
mbed_official 381:5460fc57b6e4 437 }
mbed_official 381:5460fc57b6e4 438
mbed_official 381:5460fc57b6e4 439 /* In multiprocessor mode, the following bits must be kept cleared:
mbed_official 381:5460fc57b6e4 440 - LINEN and CLKEN bits in the USART_CR2 register,
mbed_official 381:5460fc57b6e4 441 - SCEN, HDSEL and IREN bits in the USART_CR3 register. */
mbed_official 381:5460fc57b6e4 442 huart->Instance->CR2 &= ~(USART_CR2_LINEN | USART_CR2_CLKEN);
mbed_official 381:5460fc57b6e4 443 huart->Instance->CR3 &= ~(USART_CR3_SCEN | USART_CR3_HDSEL | USART_CR3_IREN);
mbed_official 381:5460fc57b6e4 444
mbed_official 381:5460fc57b6e4 445 if (WakeUpMethod == UART_WAKEUPMETHOD_ADDRESSMARK)
mbed_official 381:5460fc57b6e4 446 {
mbed_official 381:5460fc57b6e4 447 /* If address mark wake up method is chosen, set the USART address node */
mbed_official 381:5460fc57b6e4 448 MODIFY_REG(huart->Instance->CR2, USART_CR2_ADD, ((uint32_t)Address << UART_CR2_ADDRESS_LSB_POS));
mbed_official 381:5460fc57b6e4 449 }
mbed_official 381:5460fc57b6e4 450
mbed_official 381:5460fc57b6e4 451 /* Set the wake up method by setting the WAKE bit in the CR1 register */
mbed_official 381:5460fc57b6e4 452 MODIFY_REG(huart->Instance->CR1, USART_CR1_WAKE, WakeUpMethod);
mbed_official 381:5460fc57b6e4 453
mbed_official 381:5460fc57b6e4 454 /* Enable the Peripheral */
mbed_official 381:5460fc57b6e4 455 __HAL_UART_ENABLE(huart);
mbed_official 381:5460fc57b6e4 456
mbed_official 381:5460fc57b6e4 457 /* TEACK and/or REACK to check before moving huart->State to Ready */
mbed_official 381:5460fc57b6e4 458 return (UART_CheckIdleState(huart));
mbed_official 381:5460fc57b6e4 459 }
mbed_official 381:5460fc57b6e4 460
mbed_official 381:5460fc57b6e4 461
mbed_official 381:5460fc57b6e4 462
mbed_official 381:5460fc57b6e4 463
mbed_official 381:5460fc57b6e4 464 /**
mbed_official 381:5460fc57b6e4 465 * @brief DeInitializes the UART peripheral
mbed_official 381:5460fc57b6e4 466 * @param huart: uart handle
mbed_official 381:5460fc57b6e4 467 * @retval HAL status
mbed_official 381:5460fc57b6e4 468 */
mbed_official 381:5460fc57b6e4 469 HAL_StatusTypeDef HAL_UART_DeInit(UART_HandleTypeDef *huart)
mbed_official 381:5460fc57b6e4 470 {
mbed_official 381:5460fc57b6e4 471 /* Check the UART handle allocation */
mbed_official 381:5460fc57b6e4 472 if(huart == HAL_NULL)
mbed_official 381:5460fc57b6e4 473 {
mbed_official 381:5460fc57b6e4 474 return HAL_ERROR;
mbed_official 381:5460fc57b6e4 475 }
mbed_official 381:5460fc57b6e4 476
mbed_official 381:5460fc57b6e4 477 /* Check the parameters */
mbed_official 381:5460fc57b6e4 478 assert_param(IS_UART_INSTANCE(huart->Instance));
mbed_official 381:5460fc57b6e4 479
mbed_official 381:5460fc57b6e4 480 huart->State = HAL_UART_STATE_BUSY;
mbed_official 381:5460fc57b6e4 481
mbed_official 381:5460fc57b6e4 482 /* Disable the Peripheral */
mbed_official 381:5460fc57b6e4 483 __HAL_UART_DISABLE(huart);
mbed_official 381:5460fc57b6e4 484
mbed_official 381:5460fc57b6e4 485 huart->Instance->CR1 = 0x0;
mbed_official 381:5460fc57b6e4 486 huart->Instance->CR2 = 0x0;
mbed_official 381:5460fc57b6e4 487 huart->Instance->CR3 = 0x0;
mbed_official 381:5460fc57b6e4 488
mbed_official 381:5460fc57b6e4 489 /* DeInit the low level hardware */
mbed_official 381:5460fc57b6e4 490 HAL_UART_MspDeInit(huart);
mbed_official 381:5460fc57b6e4 491
mbed_official 381:5460fc57b6e4 492 huart->ErrorCode = HAL_UART_ERROR_NONE;
mbed_official 381:5460fc57b6e4 493 huart->State = HAL_UART_STATE_RESET;
mbed_official 381:5460fc57b6e4 494
mbed_official 381:5460fc57b6e4 495 /* Process Unlock */
mbed_official 381:5460fc57b6e4 496 __HAL_UNLOCK(huart);
mbed_official 381:5460fc57b6e4 497
mbed_official 381:5460fc57b6e4 498 return HAL_OK;
mbed_official 381:5460fc57b6e4 499 }
mbed_official 381:5460fc57b6e4 500
mbed_official 381:5460fc57b6e4 501 /**
mbed_official 381:5460fc57b6e4 502 * @brief UART MSP Init
mbed_official 381:5460fc57b6e4 503 * @param huart: uart handle
mbed_official 381:5460fc57b6e4 504 * @retval None
mbed_official 381:5460fc57b6e4 505 */
mbed_official 381:5460fc57b6e4 506 __weak void HAL_UART_MspInit(UART_HandleTypeDef *huart)
mbed_official 381:5460fc57b6e4 507 {
mbed_official 381:5460fc57b6e4 508 /* NOTE : This function should not be modified, when the callback is needed,
mbed_official 381:5460fc57b6e4 509 the HAL_UART_MspInit can be implemented in the user file
mbed_official 381:5460fc57b6e4 510 */
mbed_official 381:5460fc57b6e4 511 }
mbed_official 381:5460fc57b6e4 512
mbed_official 381:5460fc57b6e4 513 /**
mbed_official 381:5460fc57b6e4 514 * @brief UART MSP DeInit
mbed_official 381:5460fc57b6e4 515 * @param huart: uart handle
mbed_official 381:5460fc57b6e4 516 * @retval None
mbed_official 381:5460fc57b6e4 517 */
mbed_official 381:5460fc57b6e4 518 __weak void HAL_UART_MspDeInit(UART_HandleTypeDef *huart)
mbed_official 381:5460fc57b6e4 519 {
mbed_official 381:5460fc57b6e4 520 /* NOTE : This function should not be modified, when the callback is needed,
mbed_official 381:5460fc57b6e4 521 the HAL_UART_MspDeInit can be implemented in the user file
mbed_official 381:5460fc57b6e4 522 */
mbed_official 381:5460fc57b6e4 523 }
mbed_official 381:5460fc57b6e4 524
mbed_official 381:5460fc57b6e4 525 /**
mbed_official 381:5460fc57b6e4 526 * @}
mbed_official 381:5460fc57b6e4 527 */
mbed_official 381:5460fc57b6e4 528
mbed_official 381:5460fc57b6e4 529 /** @defgroup UART_Exported_Functions_Group2 Input and Output operation functions
mbed_official 381:5460fc57b6e4 530 * @brief UART Transmit/Receive functions
mbed_official 381:5460fc57b6e4 531 *
mbed_official 381:5460fc57b6e4 532 @verbatim
mbed_official 381:5460fc57b6e4 533 ===============================================================================
mbed_official 381:5460fc57b6e4 534 ##### I/O operation functions #####
mbed_official 381:5460fc57b6e4 535 ===============================================================================
mbed_official 381:5460fc57b6e4 536 This subsection provides a set of functions allowing to manage the UART asynchronous
mbed_official 381:5460fc57b6e4 537 and Half duplex data transfers.
mbed_official 381:5460fc57b6e4 538
mbed_official 381:5460fc57b6e4 539 (#) There are two mode of transfer:
mbed_official 381:5460fc57b6e4 540 (+) Blocking mode: The communication is performed in polling mode.
mbed_official 381:5460fc57b6e4 541 The HAL status of all data processing is returned by the same function
mbed_official 381:5460fc57b6e4 542 after finishing transfer.
mbed_official 381:5460fc57b6e4 543 (+) No-Blocking mode: The communication is performed using Interrupts
mbed_official 381:5460fc57b6e4 544 or DMA, These API's return the HAL status.
mbed_official 381:5460fc57b6e4 545 The end of the data processing will be indicated through the
mbed_official 381:5460fc57b6e4 546 dedicated UART IRQ when using Interrupt mode or the DMA IRQ when
mbed_official 381:5460fc57b6e4 547 using DMA mode.
mbed_official 381:5460fc57b6e4 548 The HAL_UART_TxCpltCallback(), HAL_UART_RxCpltCallback() user callbacks
mbed_official 381:5460fc57b6e4 549 will be executed respectivelly at the end of the transmit or Receive process
mbed_official 381:5460fc57b6e4 550 The HAL_UART_ErrorCallback()user callback will be executed when a communication error is detected
mbed_official 381:5460fc57b6e4 551
mbed_official 381:5460fc57b6e4 552 (#) Blocking mode API's are :
mbed_official 381:5460fc57b6e4 553 (+) HAL_UART_Transmit()
mbed_official 381:5460fc57b6e4 554 (+) HAL_UART_Receive()
mbed_official 381:5460fc57b6e4 555
mbed_official 381:5460fc57b6e4 556 (#) Non-Blocking mode API's with Interrupt are :
mbed_official 381:5460fc57b6e4 557 (+) HAL_UART_Transmit_IT()
mbed_official 381:5460fc57b6e4 558 (+) HAL_UART_Receive_IT()
mbed_official 381:5460fc57b6e4 559 (+) HAL_UART_IRQHandler()
mbed_official 381:5460fc57b6e4 560
mbed_official 381:5460fc57b6e4 561 (#) No-Blocking mode API's with DMA are :
mbed_official 381:5460fc57b6e4 562 (+) HAL_UART_Transmit_DMA()
mbed_official 381:5460fc57b6e4 563 (+) HAL_UART_Receive_DMA()
mbed_official 381:5460fc57b6e4 564 (+) HAL_UART_DMAPause()
mbed_official 381:5460fc57b6e4 565 (+) HAL_UART_DMAResume()
mbed_official 381:5460fc57b6e4 566 (+) HAL_UART_DMAStop()
mbed_official 381:5460fc57b6e4 567
mbed_official 381:5460fc57b6e4 568 (#) A set of Transfer Complete Callbacks are provided in No_Blocking mode:
mbed_official 381:5460fc57b6e4 569 (+) HAL_UART_TxHalfCpltCallback()
mbed_official 381:5460fc57b6e4 570 (+) HAL_UART_TxCpltCallback()
mbed_official 381:5460fc57b6e4 571 (+) HAL_UART_RxHalfCpltCallback()
mbed_official 381:5460fc57b6e4 572 (+) HAL_UART_RxCpltCallback()
mbed_official 381:5460fc57b6e4 573 (+) HAL_UART_ErrorCallback()
mbed_official 381:5460fc57b6e4 574
mbed_official 381:5460fc57b6e4 575
mbed_official 381:5460fc57b6e4 576 -@- In the Half duplex communication, it is forbidden to run the transmit
mbed_official 381:5460fc57b6e4 577 and receive process in parallel, the UART state HAL_UART_STATE_BUSY_TX_RX can't be useful.
mbed_official 381:5460fc57b6e4 578
mbed_official 381:5460fc57b6e4 579 @endverbatim
mbed_official 381:5460fc57b6e4 580 * @{
mbed_official 381:5460fc57b6e4 581 */
mbed_official 381:5460fc57b6e4 582
mbed_official 381:5460fc57b6e4 583 /**
mbed_official 381:5460fc57b6e4 584 * @brief Send an amount of data in blocking mode
mbed_official 381:5460fc57b6e4 585 * @param huart: uart handle
mbed_official 381:5460fc57b6e4 586 * @param pData: pointer to data buffer
mbed_official 381:5460fc57b6e4 587 * @param Size: amount of data to be sent
mbed_official 381:5460fc57b6e4 588 * @param Timeout : Timeout duration
mbed_official 381:5460fc57b6e4 589 * @retval HAL status
mbed_official 381:5460fc57b6e4 590 */
mbed_official 381:5460fc57b6e4 591 HAL_StatusTypeDef HAL_UART_Transmit(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint32_t Timeout)
mbed_official 381:5460fc57b6e4 592 {
mbed_official 381:5460fc57b6e4 593 uint16_t* tmp;
mbed_official 381:5460fc57b6e4 594
mbed_official 381:5460fc57b6e4 595 if((huart->State == HAL_UART_STATE_READY) || (huart->State == HAL_UART_STATE_BUSY_RX))
mbed_official 381:5460fc57b6e4 596 {
mbed_official 381:5460fc57b6e4 597 if((pData == HAL_NULL ) || (Size == 0))
mbed_official 381:5460fc57b6e4 598 {
mbed_official 381:5460fc57b6e4 599 return HAL_ERROR;
mbed_official 381:5460fc57b6e4 600 }
mbed_official 381:5460fc57b6e4 601
mbed_official 381:5460fc57b6e4 602 /* Process Locked */
mbed_official 381:5460fc57b6e4 603 __HAL_LOCK(huart);
mbed_official 381:5460fc57b6e4 604
mbed_official 381:5460fc57b6e4 605 huart->ErrorCode = HAL_UART_ERROR_NONE;
mbed_official 381:5460fc57b6e4 606 /* Check if a non-blocking receive process is ongoing or not */
mbed_official 381:5460fc57b6e4 607 if(huart->State == HAL_UART_STATE_BUSY_RX)
mbed_official 381:5460fc57b6e4 608 {
mbed_official 381:5460fc57b6e4 609 huart->State = HAL_UART_STATE_BUSY_TX_RX;
mbed_official 381:5460fc57b6e4 610 }
mbed_official 381:5460fc57b6e4 611 else
mbed_official 381:5460fc57b6e4 612 {
mbed_official 381:5460fc57b6e4 613 huart->State = HAL_UART_STATE_BUSY_TX;
mbed_official 381:5460fc57b6e4 614 }
mbed_official 381:5460fc57b6e4 615
mbed_official 381:5460fc57b6e4 616 huart->TxXferSize = Size;
mbed_official 381:5460fc57b6e4 617 huart->TxXferCount = Size;
mbed_official 381:5460fc57b6e4 618 while(huart->TxXferCount > 0)
mbed_official 381:5460fc57b6e4 619 {
mbed_official 381:5460fc57b6e4 620 huart->TxXferCount--;
mbed_official 381:5460fc57b6e4 621 if(UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_TXE, RESET, Timeout) != HAL_OK)
mbed_official 381:5460fc57b6e4 622 {
mbed_official 381:5460fc57b6e4 623 return HAL_TIMEOUT;
mbed_official 381:5460fc57b6e4 624 }
mbed_official 381:5460fc57b6e4 625 if ((huart->Init.WordLength == UART_WORDLENGTH_9B) && (huart->Init.Parity == UART_PARITY_NONE))
mbed_official 381:5460fc57b6e4 626 {
mbed_official 381:5460fc57b6e4 627 tmp = (uint16_t*) pData;
mbed_official 381:5460fc57b6e4 628 huart->Instance->TDR = (*tmp & (uint16_t)0x01FF);
mbed_official 381:5460fc57b6e4 629 pData += 2;
mbed_official 381:5460fc57b6e4 630 }
mbed_official 381:5460fc57b6e4 631 else
mbed_official 381:5460fc57b6e4 632 {
mbed_official 381:5460fc57b6e4 633 huart->Instance->TDR = (*pData++ & (uint8_t)0xFF);
mbed_official 381:5460fc57b6e4 634 }
mbed_official 381:5460fc57b6e4 635 }
mbed_official 381:5460fc57b6e4 636 if(UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_TC, RESET, Timeout) != HAL_OK)
mbed_official 381:5460fc57b6e4 637 {
mbed_official 381:5460fc57b6e4 638 return HAL_TIMEOUT;
mbed_official 381:5460fc57b6e4 639 }
mbed_official 381:5460fc57b6e4 640 /* Check if a non-blocking receive Process is ongoing or not */
mbed_official 381:5460fc57b6e4 641 if(huart->State == HAL_UART_STATE_BUSY_TX_RX)
mbed_official 381:5460fc57b6e4 642 {
mbed_official 381:5460fc57b6e4 643 huart->State = HAL_UART_STATE_BUSY_RX;
mbed_official 381:5460fc57b6e4 644 }
mbed_official 381:5460fc57b6e4 645 else
mbed_official 381:5460fc57b6e4 646 {
mbed_official 381:5460fc57b6e4 647 huart->State = HAL_UART_STATE_READY;
mbed_official 381:5460fc57b6e4 648 }
mbed_official 381:5460fc57b6e4 649
mbed_official 381:5460fc57b6e4 650 /* Process Unlocked */
mbed_official 381:5460fc57b6e4 651 __HAL_UNLOCK(huart);
mbed_official 381:5460fc57b6e4 652
mbed_official 381:5460fc57b6e4 653 return HAL_OK;
mbed_official 381:5460fc57b6e4 654 }
mbed_official 381:5460fc57b6e4 655 else
mbed_official 381:5460fc57b6e4 656 {
mbed_official 381:5460fc57b6e4 657 return HAL_BUSY;
mbed_official 381:5460fc57b6e4 658 }
mbed_official 381:5460fc57b6e4 659 }
mbed_official 381:5460fc57b6e4 660
mbed_official 381:5460fc57b6e4 661 /**
mbed_official 381:5460fc57b6e4 662 * @brief Receive an amount of data in blocking mode
mbed_official 381:5460fc57b6e4 663 * @param huart: uart handle
mbed_official 381:5460fc57b6e4 664 * @param pData: pointer to data buffer
mbed_official 381:5460fc57b6e4 665 * @param Size: amount of data to be received
mbed_official 381:5460fc57b6e4 666 * @param Timeout : Timeout duration
mbed_official 381:5460fc57b6e4 667 * @retval HAL status
mbed_official 381:5460fc57b6e4 668 */
mbed_official 381:5460fc57b6e4 669 HAL_StatusTypeDef HAL_UART_Receive(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint32_t Timeout)
mbed_official 381:5460fc57b6e4 670 {
mbed_official 381:5460fc57b6e4 671 uint16_t* tmp;
mbed_official 381:5460fc57b6e4 672 uint16_t uhMask;
mbed_official 381:5460fc57b6e4 673
mbed_official 381:5460fc57b6e4 674 if((huart->State == HAL_UART_STATE_READY) || (huart->State == HAL_UART_STATE_BUSY_TX))
mbed_official 381:5460fc57b6e4 675 {
mbed_official 381:5460fc57b6e4 676 if((pData == HAL_NULL ) || (Size == 0))
mbed_official 381:5460fc57b6e4 677 {
mbed_official 381:5460fc57b6e4 678 return HAL_ERROR;
mbed_official 381:5460fc57b6e4 679 }
mbed_official 381:5460fc57b6e4 680
mbed_official 381:5460fc57b6e4 681 /* Process Locked */
mbed_official 381:5460fc57b6e4 682 __HAL_LOCK(huart);
mbed_official 381:5460fc57b6e4 683
mbed_official 381:5460fc57b6e4 684 huart->ErrorCode = HAL_UART_ERROR_NONE;
mbed_official 381:5460fc57b6e4 685 /* Check if a non-blocking transmit process is ongoing or not */
mbed_official 381:5460fc57b6e4 686 if(huart->State == HAL_UART_STATE_BUSY_TX)
mbed_official 381:5460fc57b6e4 687 {
mbed_official 381:5460fc57b6e4 688 huart->State = HAL_UART_STATE_BUSY_TX_RX;
mbed_official 381:5460fc57b6e4 689 }
mbed_official 381:5460fc57b6e4 690 else
mbed_official 381:5460fc57b6e4 691 {
mbed_official 381:5460fc57b6e4 692 huart->State = HAL_UART_STATE_BUSY_RX;
mbed_official 381:5460fc57b6e4 693 }
mbed_official 381:5460fc57b6e4 694
mbed_official 381:5460fc57b6e4 695 huart->RxXferSize = Size;
mbed_official 381:5460fc57b6e4 696 huart->RxXferCount = Size;
mbed_official 381:5460fc57b6e4 697
mbed_official 381:5460fc57b6e4 698 /* Computation of UART mask to apply to RDR register */
mbed_official 381:5460fc57b6e4 699 __HAL_UART_MASK_COMPUTATION(huart);
mbed_official 381:5460fc57b6e4 700 uhMask = huart->Mask;
mbed_official 381:5460fc57b6e4 701
mbed_official 381:5460fc57b6e4 702 /* as long as data have to be received */
mbed_official 381:5460fc57b6e4 703 while(huart->RxXferCount > 0)
mbed_official 381:5460fc57b6e4 704 {
mbed_official 381:5460fc57b6e4 705 huart->RxXferCount--;
mbed_official 381:5460fc57b6e4 706 if(UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_RXNE, RESET, Timeout) != HAL_OK)
mbed_official 381:5460fc57b6e4 707 {
mbed_official 381:5460fc57b6e4 708 return HAL_TIMEOUT;
mbed_official 381:5460fc57b6e4 709 }
mbed_official 381:5460fc57b6e4 710 if ((huart->Init.WordLength == UART_WORDLENGTH_9B) && (huart->Init.Parity == UART_PARITY_NONE))
mbed_official 381:5460fc57b6e4 711 {
mbed_official 381:5460fc57b6e4 712 tmp = (uint16_t*) pData ;
mbed_official 381:5460fc57b6e4 713 *tmp = (uint16_t)(huart->Instance->RDR & uhMask);
mbed_official 381:5460fc57b6e4 714 pData +=2;
mbed_official 381:5460fc57b6e4 715 }
mbed_official 381:5460fc57b6e4 716 else
mbed_official 381:5460fc57b6e4 717 {
mbed_official 381:5460fc57b6e4 718 *pData++ = (uint8_t)(huart->Instance->RDR & (uint8_t)uhMask);
mbed_official 381:5460fc57b6e4 719 }
mbed_official 381:5460fc57b6e4 720 }
mbed_official 381:5460fc57b6e4 721
mbed_official 381:5460fc57b6e4 722 /* Check if a non-blocking transmit Process is ongoing or not */
mbed_official 381:5460fc57b6e4 723 if(huart->State == HAL_UART_STATE_BUSY_TX_RX)
mbed_official 381:5460fc57b6e4 724 {
mbed_official 381:5460fc57b6e4 725 huart->State = HAL_UART_STATE_BUSY_TX;
mbed_official 381:5460fc57b6e4 726 }
mbed_official 381:5460fc57b6e4 727 else
mbed_official 381:5460fc57b6e4 728 {
mbed_official 381:5460fc57b6e4 729 huart->State = HAL_UART_STATE_READY;
mbed_official 381:5460fc57b6e4 730 }
mbed_official 381:5460fc57b6e4 731 /* Process Unlocked */
mbed_official 381:5460fc57b6e4 732 __HAL_UNLOCK(huart);
mbed_official 381:5460fc57b6e4 733
mbed_official 381:5460fc57b6e4 734 return HAL_OK;
mbed_official 381:5460fc57b6e4 735 }
mbed_official 381:5460fc57b6e4 736 else
mbed_official 381:5460fc57b6e4 737 {
mbed_official 381:5460fc57b6e4 738 return HAL_BUSY;
mbed_official 381:5460fc57b6e4 739 }
mbed_official 381:5460fc57b6e4 740 }
mbed_official 381:5460fc57b6e4 741
mbed_official 381:5460fc57b6e4 742 /**
mbed_official 381:5460fc57b6e4 743 * @brief Send an amount of data in interrupt mode
mbed_official 381:5460fc57b6e4 744 * @param huart: uart handle
mbed_official 381:5460fc57b6e4 745 * @param pData: pointer to data buffer
mbed_official 381:5460fc57b6e4 746 * @param Size: amount of data to be sent
mbed_official 381:5460fc57b6e4 747 * @retval HAL status
mbed_official 381:5460fc57b6e4 748 */
mbed_official 381:5460fc57b6e4 749 HAL_StatusTypeDef HAL_UART_Transmit_IT(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
mbed_official 381:5460fc57b6e4 750 {
mbed_official 381:5460fc57b6e4 751 if((huart->State == HAL_UART_STATE_READY) || (huart->State == HAL_UART_STATE_BUSY_RX))
mbed_official 381:5460fc57b6e4 752 {
mbed_official 381:5460fc57b6e4 753 if((pData == HAL_NULL ) || (Size == 0))
mbed_official 381:5460fc57b6e4 754 {
mbed_official 381:5460fc57b6e4 755 return HAL_ERROR;
mbed_official 381:5460fc57b6e4 756 }
mbed_official 381:5460fc57b6e4 757
mbed_official 381:5460fc57b6e4 758 /* Process Locked */
mbed_official 381:5460fc57b6e4 759 __HAL_LOCK(huart);
mbed_official 381:5460fc57b6e4 760
mbed_official 381:5460fc57b6e4 761 huart->pTxBuffPtr = pData;
mbed_official 381:5460fc57b6e4 762 huart->TxXferSize = Size;
mbed_official 381:5460fc57b6e4 763 huart->TxXferCount = Size;
mbed_official 381:5460fc57b6e4 764
mbed_official 381:5460fc57b6e4 765 huart->ErrorCode = HAL_UART_ERROR_NONE;
mbed_official 381:5460fc57b6e4 766 /* Check if a receive process is ongoing or not */
mbed_official 381:5460fc57b6e4 767 if(huart->State == HAL_UART_STATE_BUSY_RX)
mbed_official 381:5460fc57b6e4 768 {
mbed_official 381:5460fc57b6e4 769 huart->State = HAL_UART_STATE_BUSY_TX_RX;
mbed_official 381:5460fc57b6e4 770 }
mbed_official 381:5460fc57b6e4 771 else
mbed_official 381:5460fc57b6e4 772 {
mbed_official 381:5460fc57b6e4 773 huart->State = HAL_UART_STATE_BUSY_TX;
mbed_official 381:5460fc57b6e4 774 }
mbed_official 381:5460fc57b6e4 775
mbed_official 381:5460fc57b6e4 776 /* Enable the UART Error Interrupt: (Frame error, noise error, overrun error) */
mbed_official 381:5460fc57b6e4 777 __HAL_UART_ENABLE_IT(huart, UART_IT_ERR);
mbed_official 381:5460fc57b6e4 778
mbed_official 381:5460fc57b6e4 779 /* Process Unlocked */
mbed_official 381:5460fc57b6e4 780 __HAL_UNLOCK(huart);
mbed_official 381:5460fc57b6e4 781
mbed_official 381:5460fc57b6e4 782 /* Enable the UART Transmit Data Register Empty Interrupt */
mbed_official 381:5460fc57b6e4 783 __HAL_UART_ENABLE_IT(huart, UART_IT_TXE);
mbed_official 381:5460fc57b6e4 784
mbed_official 381:5460fc57b6e4 785 return HAL_OK;
mbed_official 381:5460fc57b6e4 786 }
mbed_official 381:5460fc57b6e4 787 else
mbed_official 381:5460fc57b6e4 788 {
mbed_official 381:5460fc57b6e4 789 return HAL_BUSY;
mbed_official 381:5460fc57b6e4 790 }
mbed_official 381:5460fc57b6e4 791 }
mbed_official 381:5460fc57b6e4 792
mbed_official 381:5460fc57b6e4 793 /**
mbed_official 381:5460fc57b6e4 794 * @brief Receive an amount of data in interrupt mode
mbed_official 381:5460fc57b6e4 795 * @param huart: uart handle
mbed_official 381:5460fc57b6e4 796 * @param pData: pointer to data buffer
mbed_official 381:5460fc57b6e4 797 * @param Size: amount of data to be received
mbed_official 381:5460fc57b6e4 798 * @retval HAL status
mbed_official 381:5460fc57b6e4 799 */
mbed_official 381:5460fc57b6e4 800 HAL_StatusTypeDef HAL_UART_Receive_IT(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
mbed_official 381:5460fc57b6e4 801 {
mbed_official 381:5460fc57b6e4 802 if((huart->State == HAL_UART_STATE_READY) || (huart->State == HAL_UART_STATE_BUSY_TX))
mbed_official 381:5460fc57b6e4 803 {
mbed_official 381:5460fc57b6e4 804 if((pData == HAL_NULL ) || (Size == 0))
mbed_official 381:5460fc57b6e4 805 {
mbed_official 381:5460fc57b6e4 806 return HAL_ERROR;
mbed_official 381:5460fc57b6e4 807 }
mbed_official 381:5460fc57b6e4 808
mbed_official 381:5460fc57b6e4 809 /* Process Locked */
mbed_official 381:5460fc57b6e4 810 __HAL_LOCK(huart);
mbed_official 381:5460fc57b6e4 811
mbed_official 381:5460fc57b6e4 812 huart->pRxBuffPtr = pData;
mbed_official 381:5460fc57b6e4 813 huart->RxXferSize = Size;
mbed_official 381:5460fc57b6e4 814 huart->RxXferCount = Size;
mbed_official 381:5460fc57b6e4 815
mbed_official 381:5460fc57b6e4 816 /* Computation of UART mask to apply to RDR register */
mbed_official 381:5460fc57b6e4 817 __HAL_UART_MASK_COMPUTATION(huart);
mbed_official 381:5460fc57b6e4 818
mbed_official 381:5460fc57b6e4 819 huart->ErrorCode = HAL_UART_ERROR_NONE;
mbed_official 381:5460fc57b6e4 820 /* Check if a transmit process is ongoing or not */
mbed_official 381:5460fc57b6e4 821 if(huart->State == HAL_UART_STATE_BUSY_TX)
mbed_official 381:5460fc57b6e4 822 {
mbed_official 381:5460fc57b6e4 823 huart->State = HAL_UART_STATE_BUSY_TX_RX;
mbed_official 381:5460fc57b6e4 824 }
mbed_official 381:5460fc57b6e4 825 else
mbed_official 381:5460fc57b6e4 826 {
mbed_official 381:5460fc57b6e4 827 huart->State = HAL_UART_STATE_BUSY_RX;
mbed_official 381:5460fc57b6e4 828 }
mbed_official 381:5460fc57b6e4 829
mbed_official 381:5460fc57b6e4 830 /* Enable the UART Parity Error Interrupt */
mbed_official 381:5460fc57b6e4 831 __HAL_UART_ENABLE_IT(huart, UART_IT_PE);
mbed_official 381:5460fc57b6e4 832
mbed_official 381:5460fc57b6e4 833 /* Enable the UART Error Interrupt: (Frame error, noise error, overrun error) */
mbed_official 381:5460fc57b6e4 834 __HAL_UART_ENABLE_IT(huart, UART_IT_ERR);
mbed_official 381:5460fc57b6e4 835
mbed_official 381:5460fc57b6e4 836 /* Process Unlocked */
mbed_official 381:5460fc57b6e4 837 __HAL_UNLOCK(huart);
mbed_official 381:5460fc57b6e4 838
mbed_official 381:5460fc57b6e4 839 /* Enable the UART Data Register not empty Interrupt */
mbed_official 381:5460fc57b6e4 840 __HAL_UART_ENABLE_IT(huart, UART_IT_RXNE);
mbed_official 381:5460fc57b6e4 841
mbed_official 381:5460fc57b6e4 842 return HAL_OK;
mbed_official 381:5460fc57b6e4 843 }
mbed_official 381:5460fc57b6e4 844 else
mbed_official 381:5460fc57b6e4 845 {
mbed_official 381:5460fc57b6e4 846 return HAL_BUSY;
mbed_official 381:5460fc57b6e4 847 }
mbed_official 381:5460fc57b6e4 848 }
mbed_official 381:5460fc57b6e4 849
mbed_official 381:5460fc57b6e4 850 /**
mbed_official 381:5460fc57b6e4 851 * @brief Send an amount of data in DMA mode
mbed_official 381:5460fc57b6e4 852 * @param huart: uart handle
mbed_official 381:5460fc57b6e4 853 * @param pData: pointer to data buffer
mbed_official 381:5460fc57b6e4 854 * @param Size: amount of data to be sent
mbed_official 381:5460fc57b6e4 855 * @retval HAL status
mbed_official 381:5460fc57b6e4 856 */
mbed_official 381:5460fc57b6e4 857 HAL_StatusTypeDef HAL_UART_Transmit_DMA(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
mbed_official 381:5460fc57b6e4 858 {
mbed_official 381:5460fc57b6e4 859 uint32_t *tmp;
mbed_official 381:5460fc57b6e4 860
mbed_official 381:5460fc57b6e4 861 if((huart->State == HAL_UART_STATE_READY) || (huart->State == HAL_UART_STATE_BUSY_RX))
mbed_official 381:5460fc57b6e4 862 {
mbed_official 381:5460fc57b6e4 863 if((pData == HAL_NULL ) || (Size == 0))
mbed_official 381:5460fc57b6e4 864 {
mbed_official 381:5460fc57b6e4 865 return HAL_ERROR;
mbed_official 381:5460fc57b6e4 866 }
mbed_official 381:5460fc57b6e4 867
mbed_official 381:5460fc57b6e4 868 /* Process Locked */
mbed_official 381:5460fc57b6e4 869 __HAL_LOCK(huart);
mbed_official 381:5460fc57b6e4 870
mbed_official 381:5460fc57b6e4 871 huart->pTxBuffPtr = pData;
mbed_official 381:5460fc57b6e4 872 huart->TxXferSize = Size;
mbed_official 381:5460fc57b6e4 873 huart->TxXferCount = Size;
mbed_official 381:5460fc57b6e4 874
mbed_official 381:5460fc57b6e4 875 huart->ErrorCode = HAL_UART_ERROR_NONE;
mbed_official 381:5460fc57b6e4 876 /* Check if a receive process is ongoing or not */
mbed_official 381:5460fc57b6e4 877 if(huart->State == HAL_UART_STATE_BUSY_RX)
mbed_official 381:5460fc57b6e4 878 {
mbed_official 381:5460fc57b6e4 879 huart->State = HAL_UART_STATE_BUSY_TX_RX;
mbed_official 381:5460fc57b6e4 880 }
mbed_official 381:5460fc57b6e4 881 else
mbed_official 381:5460fc57b6e4 882 {
mbed_official 381:5460fc57b6e4 883 huart->State = HAL_UART_STATE_BUSY_TX;
mbed_official 381:5460fc57b6e4 884 }
mbed_official 381:5460fc57b6e4 885
mbed_official 381:5460fc57b6e4 886 /* Set the UART DMA transfer complete callback */
mbed_official 381:5460fc57b6e4 887 huart->hdmatx->XferCpltCallback = UART_DMATransmitCplt;
mbed_official 381:5460fc57b6e4 888
mbed_official 381:5460fc57b6e4 889 /* Set the UART DMA Half transfer complete callback */
mbed_official 381:5460fc57b6e4 890 huart->hdmatx->XferHalfCpltCallback = UART_DMATxHalfCplt;
mbed_official 381:5460fc57b6e4 891
mbed_official 381:5460fc57b6e4 892 /* Set the DMA error callback */
mbed_official 381:5460fc57b6e4 893 huart->hdmatx->XferErrorCallback = UART_DMAError;
mbed_official 381:5460fc57b6e4 894
mbed_official 381:5460fc57b6e4 895 /* Enable the UART transmit DMA channel */
mbed_official 381:5460fc57b6e4 896 tmp = (uint32_t*)&pData;
mbed_official 381:5460fc57b6e4 897 HAL_DMA_Start_IT(huart->hdmatx, *(uint32_t*)tmp, (uint32_t)&huart->Instance->TDR, Size);
mbed_official 381:5460fc57b6e4 898
mbed_official 381:5460fc57b6e4 899 /* Enable the DMA transfer for transmit request by setting the DMAT bit
mbed_official 381:5460fc57b6e4 900 in the UART CR3 register */
mbed_official 381:5460fc57b6e4 901 huart->Instance->CR3 |= USART_CR3_DMAT;
mbed_official 381:5460fc57b6e4 902
mbed_official 381:5460fc57b6e4 903 /* Process Unlocked */
mbed_official 381:5460fc57b6e4 904 __HAL_UNLOCK(huart);
mbed_official 381:5460fc57b6e4 905
mbed_official 381:5460fc57b6e4 906 return HAL_OK;
mbed_official 381:5460fc57b6e4 907 }
mbed_official 381:5460fc57b6e4 908 else
mbed_official 381:5460fc57b6e4 909 {
mbed_official 381:5460fc57b6e4 910 return HAL_BUSY;
mbed_official 381:5460fc57b6e4 911 }
mbed_official 381:5460fc57b6e4 912 }
mbed_official 381:5460fc57b6e4 913
mbed_official 381:5460fc57b6e4 914 /**
mbed_official 381:5460fc57b6e4 915 * @brief Receive an amount of data in DMA mode
mbed_official 381:5460fc57b6e4 916 * @param huart: uart handle
mbed_official 381:5460fc57b6e4 917 * @param pData: pointer to data buffer
mbed_official 381:5460fc57b6e4 918 * @param Size: amount of data to be received
mbed_official 381:5460fc57b6e4 919 * @note When the UART parity is enabled (PCE = 1), the received data contain
mbed_official 381:5460fc57b6e4 920 * the parity bit (MSB position)
mbed_official 381:5460fc57b6e4 921 * @retval HAL status
mbed_official 381:5460fc57b6e4 922 */
mbed_official 381:5460fc57b6e4 923 HAL_StatusTypeDef HAL_UART_Receive_DMA(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
mbed_official 381:5460fc57b6e4 924 {
mbed_official 381:5460fc57b6e4 925 uint32_t *tmp;
mbed_official 381:5460fc57b6e4 926
mbed_official 381:5460fc57b6e4 927 if((huart->State == HAL_UART_STATE_READY) || (huart->State == HAL_UART_STATE_BUSY_TX))
mbed_official 381:5460fc57b6e4 928 {
mbed_official 381:5460fc57b6e4 929 if((pData == HAL_NULL ) || (Size == 0))
mbed_official 381:5460fc57b6e4 930 {
mbed_official 381:5460fc57b6e4 931 return HAL_ERROR;
mbed_official 381:5460fc57b6e4 932 }
mbed_official 381:5460fc57b6e4 933
mbed_official 381:5460fc57b6e4 934 /* Process Locked */
mbed_official 381:5460fc57b6e4 935 __HAL_LOCK(huart);
mbed_official 381:5460fc57b6e4 936
mbed_official 381:5460fc57b6e4 937 huart->pRxBuffPtr = pData;
mbed_official 381:5460fc57b6e4 938 huart->RxXferSize = Size;
mbed_official 381:5460fc57b6e4 939
mbed_official 381:5460fc57b6e4 940 huart->ErrorCode = HAL_UART_ERROR_NONE;
mbed_official 381:5460fc57b6e4 941 /* Check if a transmit process is ongoing or not */
mbed_official 381:5460fc57b6e4 942 if(huart->State == HAL_UART_STATE_BUSY_TX)
mbed_official 381:5460fc57b6e4 943 {
mbed_official 381:5460fc57b6e4 944 huart->State = HAL_UART_STATE_BUSY_TX_RX;
mbed_official 381:5460fc57b6e4 945 }
mbed_official 381:5460fc57b6e4 946 else
mbed_official 381:5460fc57b6e4 947 {
mbed_official 381:5460fc57b6e4 948 huart->State = HAL_UART_STATE_BUSY_RX;
mbed_official 381:5460fc57b6e4 949 }
mbed_official 381:5460fc57b6e4 950
mbed_official 381:5460fc57b6e4 951 /* Set the UART DMA transfer complete callback */
mbed_official 381:5460fc57b6e4 952 huart->hdmarx->XferCpltCallback = UART_DMAReceiveCplt;
mbed_official 381:5460fc57b6e4 953
mbed_official 381:5460fc57b6e4 954 /* Set the UART DMA Half transfer complete callback */
mbed_official 381:5460fc57b6e4 955 huart->hdmarx->XferHalfCpltCallback = UART_DMARxHalfCplt;
mbed_official 381:5460fc57b6e4 956
mbed_official 381:5460fc57b6e4 957 /* Set the DMA error callback */
mbed_official 381:5460fc57b6e4 958 huart->hdmarx->XferErrorCallback = UART_DMAError;
mbed_official 381:5460fc57b6e4 959
mbed_official 381:5460fc57b6e4 960 /* Enable the DMA channel */
mbed_official 381:5460fc57b6e4 961 tmp = (uint32_t*)&pData;
mbed_official 381:5460fc57b6e4 962 HAL_DMA_Start_IT(huart->hdmarx, (uint32_t)&huart->Instance->RDR, *(uint32_t*)tmp, Size);
mbed_official 381:5460fc57b6e4 963
mbed_official 381:5460fc57b6e4 964 /* Enable the DMA transfer for the receiver request by setting the DMAR bit
mbed_official 381:5460fc57b6e4 965 in the UART CR3 register */
mbed_official 381:5460fc57b6e4 966 huart->Instance->CR3 |= USART_CR3_DMAR;
mbed_official 381:5460fc57b6e4 967
mbed_official 381:5460fc57b6e4 968 /* Process Unlocked */
mbed_official 381:5460fc57b6e4 969 __HAL_UNLOCK(huart);
mbed_official 381:5460fc57b6e4 970
mbed_official 381:5460fc57b6e4 971 return HAL_OK;
mbed_official 381:5460fc57b6e4 972 }
mbed_official 381:5460fc57b6e4 973 else
mbed_official 381:5460fc57b6e4 974 {
mbed_official 381:5460fc57b6e4 975 return HAL_BUSY;
mbed_official 381:5460fc57b6e4 976 }
mbed_official 381:5460fc57b6e4 977 }
mbed_official 381:5460fc57b6e4 978
mbed_official 381:5460fc57b6e4 979 /**
mbed_official 381:5460fc57b6e4 980 * @brief Pauses the DMA Transfer.
mbed_official 381:5460fc57b6e4 981 * @param huart: UART handle
mbed_official 381:5460fc57b6e4 982 * @retval None
mbed_official 381:5460fc57b6e4 983 */
mbed_official 381:5460fc57b6e4 984 HAL_StatusTypeDef HAL_UART_DMAPause(UART_HandleTypeDef *huart)
mbed_official 381:5460fc57b6e4 985 {
mbed_official 381:5460fc57b6e4 986 /* Process Locked */
mbed_official 381:5460fc57b6e4 987 __HAL_LOCK(huart);
mbed_official 381:5460fc57b6e4 988
mbed_official 381:5460fc57b6e4 989 if(huart->State == HAL_UART_STATE_BUSY_TX)
mbed_official 381:5460fc57b6e4 990 {
mbed_official 381:5460fc57b6e4 991 /* Disable the UART DMA Tx request */
mbed_official 381:5460fc57b6e4 992 huart->Instance->CR3 &= (uint32_t)(~USART_CR3_DMAT);
mbed_official 381:5460fc57b6e4 993 }
mbed_official 381:5460fc57b6e4 994 else if(huart->State == HAL_UART_STATE_BUSY_RX)
mbed_official 381:5460fc57b6e4 995 {
mbed_official 381:5460fc57b6e4 996 /* Disable the UART DMA Rx request */
mbed_official 381:5460fc57b6e4 997 huart->Instance->CR3 &= (uint32_t)(~USART_CR3_DMAR);
mbed_official 381:5460fc57b6e4 998 }
mbed_official 381:5460fc57b6e4 999 else if(huart->State == HAL_UART_STATE_BUSY_TX_RX)
mbed_official 381:5460fc57b6e4 1000 {
mbed_official 381:5460fc57b6e4 1001 /* Disable the UART DMA Tx request */
mbed_official 381:5460fc57b6e4 1002 huart->Instance->CR3 &= (uint32_t)(~USART_CR3_DMAT);
mbed_official 381:5460fc57b6e4 1003 /* Disable the UART DMA Rx request */
mbed_official 381:5460fc57b6e4 1004 huart->Instance->CR3 &= (uint32_t)(~USART_CR3_DMAR);
mbed_official 381:5460fc57b6e4 1005 }
mbed_official 381:5460fc57b6e4 1006
mbed_official 381:5460fc57b6e4 1007 /* Process Unlocked */
mbed_official 381:5460fc57b6e4 1008 __HAL_UNLOCK(huart);
mbed_official 381:5460fc57b6e4 1009
mbed_official 381:5460fc57b6e4 1010 return HAL_OK;
mbed_official 381:5460fc57b6e4 1011 }
mbed_official 381:5460fc57b6e4 1012
mbed_official 381:5460fc57b6e4 1013 /**
mbed_official 381:5460fc57b6e4 1014 * @brief Resumes the DMA Transfer.
mbed_official 381:5460fc57b6e4 1015 * @param huart: UART handle
mbed_official 381:5460fc57b6e4 1016 * @retval None
mbed_official 381:5460fc57b6e4 1017 */
mbed_official 381:5460fc57b6e4 1018 HAL_StatusTypeDef HAL_UART_DMAResume(UART_HandleTypeDef *huart)
mbed_official 381:5460fc57b6e4 1019 {
mbed_official 381:5460fc57b6e4 1020 /* Process Locked */
mbed_official 381:5460fc57b6e4 1021 __HAL_LOCK(huart);
mbed_official 381:5460fc57b6e4 1022
mbed_official 381:5460fc57b6e4 1023 if(huart->State == HAL_UART_STATE_BUSY_TX)
mbed_official 381:5460fc57b6e4 1024 {
mbed_official 381:5460fc57b6e4 1025 /* Enable the UART DMA Tx request */
mbed_official 381:5460fc57b6e4 1026 huart->Instance->CR3 |= USART_CR3_DMAT;
mbed_official 381:5460fc57b6e4 1027 }
mbed_official 381:5460fc57b6e4 1028 else if(huart->State == HAL_UART_STATE_BUSY_RX)
mbed_official 381:5460fc57b6e4 1029 {
mbed_official 381:5460fc57b6e4 1030 /* Enable the UART DMA Rx request */
mbed_official 381:5460fc57b6e4 1031 huart->Instance->CR3 |= USART_CR3_DMAR;
mbed_official 381:5460fc57b6e4 1032 }
mbed_official 381:5460fc57b6e4 1033 else if(huart->State == HAL_UART_STATE_BUSY_TX_RX)
mbed_official 381:5460fc57b6e4 1034 {
mbed_official 381:5460fc57b6e4 1035 /* Enable the UART DMA Rx request before the DMA Tx request */
mbed_official 381:5460fc57b6e4 1036 huart->Instance->CR3 |= USART_CR3_DMAR;
mbed_official 381:5460fc57b6e4 1037 /* Enable the UART DMA Tx request */
mbed_official 381:5460fc57b6e4 1038 huart->Instance->CR3 |= USART_CR3_DMAT;
mbed_official 381:5460fc57b6e4 1039 }
mbed_official 381:5460fc57b6e4 1040
mbed_official 381:5460fc57b6e4 1041 /* If the UART peripheral is still not enabled, enable it */
mbed_official 381:5460fc57b6e4 1042 if ((huart->Instance->CR1 & USART_CR1_UE) == 0)
mbed_official 381:5460fc57b6e4 1043 {
mbed_official 381:5460fc57b6e4 1044 /* Enable UART peripheral */
mbed_official 381:5460fc57b6e4 1045 __HAL_UART_ENABLE(huart);
mbed_official 381:5460fc57b6e4 1046 }
mbed_official 381:5460fc57b6e4 1047
mbed_official 381:5460fc57b6e4 1048 /* TEACK and/or REACK to check before moving huart->State to Ready */
mbed_official 381:5460fc57b6e4 1049 return (UART_CheckIdleState(huart));
mbed_official 381:5460fc57b6e4 1050 }
mbed_official 381:5460fc57b6e4 1051
mbed_official 381:5460fc57b6e4 1052 /**
mbed_official 381:5460fc57b6e4 1053 * @brief Stops the DMA Transfer.
mbed_official 381:5460fc57b6e4 1054 * @param huart: UART handle
mbed_official 381:5460fc57b6e4 1055 * @retval None
mbed_official 381:5460fc57b6e4 1056 */
mbed_official 381:5460fc57b6e4 1057 HAL_StatusTypeDef HAL_UART_DMAStop(UART_HandleTypeDef *huart)
mbed_official 381:5460fc57b6e4 1058 {
mbed_official 381:5460fc57b6e4 1059 /* Process Locked */
mbed_official 381:5460fc57b6e4 1060 __HAL_LOCK(huart);
mbed_official 381:5460fc57b6e4 1061
mbed_official 381:5460fc57b6e4 1062 /* Disable the UART Tx/Rx DMA requests */
mbed_official 381:5460fc57b6e4 1063 huart->Instance->CR3 &= ~USART_CR3_DMAT;
mbed_official 381:5460fc57b6e4 1064 huart->Instance->CR3 &= ~USART_CR3_DMAR;
mbed_official 381:5460fc57b6e4 1065
mbed_official 381:5460fc57b6e4 1066 /* Abort the UART DMA tx channel */
mbed_official 381:5460fc57b6e4 1067 if(huart->hdmatx != HAL_NULL)
mbed_official 381:5460fc57b6e4 1068 {
mbed_official 381:5460fc57b6e4 1069 HAL_DMA_Abort(huart->hdmatx);
mbed_official 381:5460fc57b6e4 1070 }
mbed_official 381:5460fc57b6e4 1071 /* Abort the UART DMA rx channel */
mbed_official 381:5460fc57b6e4 1072 if(huart->hdmarx != HAL_NULL)
mbed_official 381:5460fc57b6e4 1073 {
mbed_official 381:5460fc57b6e4 1074 HAL_DMA_Abort(huart->hdmarx);
mbed_official 381:5460fc57b6e4 1075 }
mbed_official 381:5460fc57b6e4 1076
mbed_official 381:5460fc57b6e4 1077 /* Disable UART peripheral */
mbed_official 381:5460fc57b6e4 1078 __HAL_UART_DISABLE(huart);
mbed_official 381:5460fc57b6e4 1079
mbed_official 381:5460fc57b6e4 1080 huart->State = HAL_UART_STATE_READY;
mbed_official 381:5460fc57b6e4 1081
mbed_official 381:5460fc57b6e4 1082 /* Process Unlocked */
mbed_official 381:5460fc57b6e4 1083 __HAL_UNLOCK(huart);
mbed_official 381:5460fc57b6e4 1084
mbed_official 381:5460fc57b6e4 1085 return HAL_OK;
mbed_official 381:5460fc57b6e4 1086 }
mbed_official 381:5460fc57b6e4 1087
mbed_official 381:5460fc57b6e4 1088 /**
mbed_official 381:5460fc57b6e4 1089 * @brief This function handles UART interrupt request.
mbed_official 381:5460fc57b6e4 1090 * @param huart: uart handle
mbed_official 381:5460fc57b6e4 1091 * @retval None
mbed_official 381:5460fc57b6e4 1092 */
mbed_official 381:5460fc57b6e4 1093 void HAL_UART_IRQHandler(UART_HandleTypeDef *huart)
mbed_official 381:5460fc57b6e4 1094 {
mbed_official 381:5460fc57b6e4 1095 /* UART parity error interrupt occurred -------------------------------------*/
mbed_official 381:5460fc57b6e4 1096 if((__HAL_UART_GET_IT(huart, UART_IT_PE) != RESET) && (__HAL_UART_GET_IT_SOURCE(huart, UART_IT_PE) != RESET))
mbed_official 381:5460fc57b6e4 1097 {
mbed_official 381:5460fc57b6e4 1098 __HAL_UART_CLEAR_IT(huart, UART_CLEAR_PEF);
mbed_official 381:5460fc57b6e4 1099
mbed_official 381:5460fc57b6e4 1100 huart->ErrorCode |= HAL_UART_ERROR_PE;
mbed_official 381:5460fc57b6e4 1101 /* Set the UART state ready to be able to start again the process */
mbed_official 381:5460fc57b6e4 1102 huart->State = HAL_UART_STATE_READY;
mbed_official 381:5460fc57b6e4 1103 }
mbed_official 381:5460fc57b6e4 1104
mbed_official 381:5460fc57b6e4 1105 /* UART frame error interrupt occured --------------------------------------*/
mbed_official 381:5460fc57b6e4 1106 if((__HAL_UART_GET_IT(huart, UART_IT_FE) != RESET) && (__HAL_UART_GET_IT_SOURCE(huart, UART_IT_ERR) != RESET))
mbed_official 381:5460fc57b6e4 1107 {
mbed_official 381:5460fc57b6e4 1108 __HAL_UART_CLEAR_IT(huart, UART_CLEAR_FEF);
mbed_official 381:5460fc57b6e4 1109
mbed_official 381:5460fc57b6e4 1110 huart->ErrorCode |= HAL_UART_ERROR_FE;
mbed_official 381:5460fc57b6e4 1111 /* Set the UART state ready to be able to start again the process */
mbed_official 381:5460fc57b6e4 1112 huart->State = HAL_UART_STATE_READY;
mbed_official 381:5460fc57b6e4 1113 }
mbed_official 381:5460fc57b6e4 1114
mbed_official 381:5460fc57b6e4 1115 /* UART noise error interrupt occured --------------------------------------*/
mbed_official 381:5460fc57b6e4 1116 if((__HAL_UART_GET_IT(huart, UART_IT_NE) != RESET) && (__HAL_UART_GET_IT_SOURCE(huart, UART_IT_ERR) != RESET))
mbed_official 381:5460fc57b6e4 1117 {
mbed_official 381:5460fc57b6e4 1118 __HAL_UART_CLEAR_IT(huart, UART_CLEAR_NEF);
mbed_official 381:5460fc57b6e4 1119
mbed_official 381:5460fc57b6e4 1120 huart->ErrorCode |= HAL_UART_ERROR_NE;
mbed_official 381:5460fc57b6e4 1121 /* Set the UART state ready to be able to start again the process */
mbed_official 381:5460fc57b6e4 1122 huart->State = HAL_UART_STATE_READY;
mbed_official 381:5460fc57b6e4 1123 }
mbed_official 381:5460fc57b6e4 1124
mbed_official 381:5460fc57b6e4 1125 /* UART Over-Run interrupt occured -----------------------------------------*/
mbed_official 381:5460fc57b6e4 1126 if((__HAL_UART_GET_IT(huart, UART_IT_ORE) != RESET) && (__HAL_UART_GET_IT_SOURCE(huart, UART_IT_ERR) != RESET))
mbed_official 381:5460fc57b6e4 1127 {
mbed_official 381:5460fc57b6e4 1128 __HAL_UART_CLEAR_IT(huart, UART_CLEAR_OREF);
mbed_official 381:5460fc57b6e4 1129
mbed_official 381:5460fc57b6e4 1130 huart->ErrorCode |= HAL_UART_ERROR_ORE;
mbed_official 381:5460fc57b6e4 1131 /* Set the UART state ready to be able to start again the process */
mbed_official 381:5460fc57b6e4 1132 huart->State = HAL_UART_STATE_READY;
mbed_official 381:5460fc57b6e4 1133 }
mbed_official 381:5460fc57b6e4 1134
mbed_official 381:5460fc57b6e4 1135 /* Call UART Error Call back function if need be --------------------------*/
mbed_official 381:5460fc57b6e4 1136 if(huart->ErrorCode != HAL_UART_ERROR_NONE)
mbed_official 381:5460fc57b6e4 1137 {
mbed_official 381:5460fc57b6e4 1138 HAL_UART_ErrorCallback(huart);
mbed_official 381:5460fc57b6e4 1139 }
mbed_official 381:5460fc57b6e4 1140
mbed_official 381:5460fc57b6e4 1141 /* UART wakeup from Stop mode interrupt occurred -------------------------------------*/
mbed_official 381:5460fc57b6e4 1142 if((__HAL_UART_GET_IT(huart, UART_IT_WUF) != RESET) && (__HAL_UART_GET_IT_SOURCE(huart, UART_IT_WUF) != RESET))
mbed_official 381:5460fc57b6e4 1143 {
mbed_official 381:5460fc57b6e4 1144 __HAL_UART_CLEAR_IT(huart, UART_CLEAR_WUF);
mbed_official 381:5460fc57b6e4 1145 /* Set the UART state ready to be able to start again the process */
mbed_official 381:5460fc57b6e4 1146 huart->State = HAL_UART_STATE_READY;
mbed_official 381:5460fc57b6e4 1147 HAL_UART_WakeupCallback(huart);
mbed_official 381:5460fc57b6e4 1148 }
mbed_official 381:5460fc57b6e4 1149
mbed_official 381:5460fc57b6e4 1150 /* UART in mode Receiver ---------------------------------------------------*/
mbed_official 381:5460fc57b6e4 1151 if((__HAL_UART_GET_IT(huart, UART_IT_RXNE) != RESET) && (__HAL_UART_GET_IT_SOURCE(huart, UART_IT_RXNE) != RESET))
mbed_official 381:5460fc57b6e4 1152 {
mbed_official 381:5460fc57b6e4 1153 UART_Receive_IT(huart);
mbed_official 381:5460fc57b6e4 1154 /* Clear RXNE interrupt flag */
mbed_official 381:5460fc57b6e4 1155 __HAL_UART_SEND_REQ(huart, UART_RXDATA_FLUSH_REQUEST);
mbed_official 381:5460fc57b6e4 1156 }
mbed_official 381:5460fc57b6e4 1157
mbed_official 381:5460fc57b6e4 1158
mbed_official 381:5460fc57b6e4 1159 /* UART in mode Transmitter ------------------------------------------------*/
mbed_official 381:5460fc57b6e4 1160 if((__HAL_UART_GET_IT(huart, UART_IT_TXE) != RESET) &&(__HAL_UART_GET_IT_SOURCE(huart, UART_IT_TXE) != RESET))
mbed_official 381:5460fc57b6e4 1161 {
mbed_official 381:5460fc57b6e4 1162 UART_Transmit_IT(huart);
mbed_official 381:5460fc57b6e4 1163 }
mbed_official 381:5460fc57b6e4 1164
mbed_official 381:5460fc57b6e4 1165 /* UART in mode Transmitter (transmission end) -----------------------------*/
mbed_official 381:5460fc57b6e4 1166 if((__HAL_UART_GET_IT(huart, UART_IT_TC) != RESET) &&(__HAL_UART_GET_IT_SOURCE(huart, UART_IT_TC) != RESET))
mbed_official 381:5460fc57b6e4 1167 {
mbed_official 381:5460fc57b6e4 1168 UART_EndTransmit_IT(huart);
mbed_official 381:5460fc57b6e4 1169 }
mbed_official 381:5460fc57b6e4 1170
mbed_official 381:5460fc57b6e4 1171 }
mbed_official 381:5460fc57b6e4 1172
mbed_official 381:5460fc57b6e4 1173
mbed_official 381:5460fc57b6e4 1174 /**
mbed_official 381:5460fc57b6e4 1175 * @brief This function handles UART Communication Timeout.
mbed_official 381:5460fc57b6e4 1176 * @param huart: UART handle
mbed_official 381:5460fc57b6e4 1177 * @param Flag: specifies the UART flag to check.
mbed_official 381:5460fc57b6e4 1178 * @param Status: The new Flag status (SET or RESET).
mbed_official 381:5460fc57b6e4 1179 * @param Timeout: Timeout duration
mbed_official 381:5460fc57b6e4 1180 * @retval HAL status
mbed_official 381:5460fc57b6e4 1181 */
mbed_official 381:5460fc57b6e4 1182 HAL_StatusTypeDef UART_WaitOnFlagUntilTimeout(UART_HandleTypeDef *huart, uint32_t Flag, FlagStatus Status, uint32_t Timeout)
mbed_official 381:5460fc57b6e4 1183 {
mbed_official 381:5460fc57b6e4 1184 uint32_t tickstart = HAL_GetTick();
mbed_official 381:5460fc57b6e4 1185
mbed_official 381:5460fc57b6e4 1186 /* Wait until flag is set */
mbed_official 381:5460fc57b6e4 1187 if(Status == RESET)
mbed_official 381:5460fc57b6e4 1188 {
mbed_official 381:5460fc57b6e4 1189 while(__HAL_UART_GET_FLAG(huart, Flag) == RESET)
mbed_official 381:5460fc57b6e4 1190 {
mbed_official 381:5460fc57b6e4 1191 /* Check for the Timeout */
mbed_official 381:5460fc57b6e4 1192 if(Timeout != HAL_MAX_DELAY)
mbed_official 381:5460fc57b6e4 1193 {
mbed_official 381:5460fc57b6e4 1194 if((Timeout == 0) || ((HAL_GetTick()-tickstart) > Timeout))
mbed_official 381:5460fc57b6e4 1195 {
mbed_official 381:5460fc57b6e4 1196 /* Disable TXE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts for the interrupt process */
mbed_official 381:5460fc57b6e4 1197 __HAL_UART_DISABLE_IT(huart, UART_IT_TXE);
mbed_official 381:5460fc57b6e4 1198 __HAL_UART_DISABLE_IT(huart, UART_IT_RXNE);
mbed_official 381:5460fc57b6e4 1199 __HAL_UART_DISABLE_IT(huart, UART_IT_PE);
mbed_official 381:5460fc57b6e4 1200 __HAL_UART_DISABLE_IT(huart, UART_IT_ERR);
mbed_official 381:5460fc57b6e4 1201
mbed_official 381:5460fc57b6e4 1202 huart->State = HAL_UART_STATE_TIMEOUT;
mbed_official 381:5460fc57b6e4 1203
mbed_official 381:5460fc57b6e4 1204 /* Process Unlocked */
mbed_official 381:5460fc57b6e4 1205 __HAL_UNLOCK(huart);
mbed_official 381:5460fc57b6e4 1206
mbed_official 381:5460fc57b6e4 1207 return HAL_TIMEOUT;
mbed_official 381:5460fc57b6e4 1208 }
mbed_official 381:5460fc57b6e4 1209 }
mbed_official 381:5460fc57b6e4 1210 }
mbed_official 381:5460fc57b6e4 1211 }
mbed_official 381:5460fc57b6e4 1212 else
mbed_official 381:5460fc57b6e4 1213 {
mbed_official 381:5460fc57b6e4 1214 while(__HAL_UART_GET_FLAG(huart, Flag) != RESET)
mbed_official 381:5460fc57b6e4 1215 {
mbed_official 381:5460fc57b6e4 1216 /* Check for the Timeout */
mbed_official 381:5460fc57b6e4 1217 if(Timeout != HAL_MAX_DELAY)
mbed_official 381:5460fc57b6e4 1218 {
mbed_official 381:5460fc57b6e4 1219 if((Timeout == 0) || ((HAL_GetTick()-tickstart) > Timeout))
mbed_official 381:5460fc57b6e4 1220 {
mbed_official 381:5460fc57b6e4 1221 /* Disable TXE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts for the interrupt process */
mbed_official 381:5460fc57b6e4 1222 __HAL_UART_DISABLE_IT(huart, UART_IT_TXE);
mbed_official 381:5460fc57b6e4 1223 __HAL_UART_DISABLE_IT(huart, UART_IT_RXNE);
mbed_official 381:5460fc57b6e4 1224 __HAL_UART_DISABLE_IT(huart, UART_IT_PE);
mbed_official 381:5460fc57b6e4 1225 __HAL_UART_DISABLE_IT(huart, UART_IT_ERR);
mbed_official 381:5460fc57b6e4 1226
mbed_official 381:5460fc57b6e4 1227 huart->State = HAL_UART_STATE_TIMEOUT;
mbed_official 381:5460fc57b6e4 1228
mbed_official 381:5460fc57b6e4 1229 /* Process Unlocked */
mbed_official 381:5460fc57b6e4 1230 __HAL_UNLOCK(huart);
mbed_official 381:5460fc57b6e4 1231
mbed_official 381:5460fc57b6e4 1232 return HAL_TIMEOUT;
mbed_official 381:5460fc57b6e4 1233 }
mbed_official 381:5460fc57b6e4 1234 }
mbed_official 381:5460fc57b6e4 1235 }
mbed_official 381:5460fc57b6e4 1236 }
mbed_official 381:5460fc57b6e4 1237 return HAL_OK;
mbed_official 381:5460fc57b6e4 1238 }
mbed_official 381:5460fc57b6e4 1239
mbed_official 381:5460fc57b6e4 1240 /**
mbed_official 381:5460fc57b6e4 1241 * @brief Tx Transfer completed callbacks
mbed_official 381:5460fc57b6e4 1242 * @param huart: uart handle
mbed_official 381:5460fc57b6e4 1243 * @retval None
mbed_official 381:5460fc57b6e4 1244 */
mbed_official 381:5460fc57b6e4 1245 __weak void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
mbed_official 381:5460fc57b6e4 1246 {
mbed_official 381:5460fc57b6e4 1247 /* NOTE : This function should not be modified, when the callback is needed,
mbed_official 381:5460fc57b6e4 1248 the HAL_UART_TxCpltCallback can be implemented in the user file
mbed_official 381:5460fc57b6e4 1249 */
mbed_official 381:5460fc57b6e4 1250 }
mbed_official 381:5460fc57b6e4 1251
mbed_official 381:5460fc57b6e4 1252 /**
mbed_official 381:5460fc57b6e4 1253 * @brief Tx Half Transfer completed callbacks.
mbed_official 381:5460fc57b6e4 1254 * @param huart: UART handle
mbed_official 381:5460fc57b6e4 1255 * @retval None
mbed_official 381:5460fc57b6e4 1256 */
mbed_official 381:5460fc57b6e4 1257 __weak void HAL_UART_TxHalfCpltCallback(UART_HandleTypeDef *huart)
mbed_official 381:5460fc57b6e4 1258 {
mbed_official 381:5460fc57b6e4 1259 /* NOTE: This function should not be modified, when the callback is needed,
mbed_official 381:5460fc57b6e4 1260 the HAL_UART_TxHalfCpltCallback can be implemented in the user file
mbed_official 381:5460fc57b6e4 1261 */
mbed_official 381:5460fc57b6e4 1262 }
mbed_official 381:5460fc57b6e4 1263
mbed_official 381:5460fc57b6e4 1264 /**
mbed_official 381:5460fc57b6e4 1265 * @brief Rx Transfer completed callbacks
mbed_official 381:5460fc57b6e4 1266 * @param huart: uart handle
mbed_official 381:5460fc57b6e4 1267 * @retval None
mbed_official 381:5460fc57b6e4 1268 */
mbed_official 381:5460fc57b6e4 1269 __weak void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
mbed_official 381:5460fc57b6e4 1270 {
mbed_official 381:5460fc57b6e4 1271 /* NOTE : This function should not be modified, when the callback is needed,
mbed_official 381:5460fc57b6e4 1272 the HAL_UART_RxCpltCallback can be implemented in the user file
mbed_official 381:5460fc57b6e4 1273 */
mbed_official 381:5460fc57b6e4 1274 }
mbed_official 381:5460fc57b6e4 1275
mbed_official 381:5460fc57b6e4 1276 /**
mbed_official 381:5460fc57b6e4 1277 * @brief Rx Half Transfer completed callbacks.
mbed_official 381:5460fc57b6e4 1278 * @param huart: UART handle
mbed_official 381:5460fc57b6e4 1279 * @retval None
mbed_official 381:5460fc57b6e4 1280 */
mbed_official 381:5460fc57b6e4 1281 __weak void HAL_UART_RxHalfCpltCallback(UART_HandleTypeDef *huart)
mbed_official 381:5460fc57b6e4 1282 {
mbed_official 381:5460fc57b6e4 1283 /* NOTE: This function should not be modified, when the callback is needed,
mbed_official 381:5460fc57b6e4 1284 the HAL_UART_RxHalfCpltCallback can be implemented in the user file
mbed_official 381:5460fc57b6e4 1285 */
mbed_official 381:5460fc57b6e4 1286 }
mbed_official 381:5460fc57b6e4 1287
mbed_official 381:5460fc57b6e4 1288 /**
mbed_official 381:5460fc57b6e4 1289 * @brief UART error callbacks
mbed_official 381:5460fc57b6e4 1290 * @param huart: uart handle
mbed_official 381:5460fc57b6e4 1291 * @retval None
mbed_official 381:5460fc57b6e4 1292 */
mbed_official 381:5460fc57b6e4 1293 __weak void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart)
mbed_official 381:5460fc57b6e4 1294 {
mbed_official 381:5460fc57b6e4 1295 /* NOTE : This function should not be modified, when the callback is needed,
mbed_official 381:5460fc57b6e4 1296 the HAL_UART_ErrorCallback can be implemented in the user file
mbed_official 381:5460fc57b6e4 1297 */
mbed_official 381:5460fc57b6e4 1298 }
mbed_official 381:5460fc57b6e4 1299
mbed_official 381:5460fc57b6e4 1300 /**
mbed_official 381:5460fc57b6e4 1301 * @brief UART wakeup from Stop mode callback
mbed_official 381:5460fc57b6e4 1302 * @param huart: uart handle
mbed_official 381:5460fc57b6e4 1303 * @retval None
mbed_official 381:5460fc57b6e4 1304 */
mbed_official 381:5460fc57b6e4 1305 __weak void HAL_UART_WakeupCallback(UART_HandleTypeDef *huart)
mbed_official 381:5460fc57b6e4 1306 {
mbed_official 381:5460fc57b6e4 1307 /* NOTE : This function should not be modified, when the callback is needed,
mbed_official 381:5460fc57b6e4 1308 the HAL_UART_WakeupCallback can be implemented in the user file
mbed_official 381:5460fc57b6e4 1309 */
mbed_official 381:5460fc57b6e4 1310 }
mbed_official 381:5460fc57b6e4 1311
mbed_official 381:5460fc57b6e4 1312 /**
mbed_official 381:5460fc57b6e4 1313 * @}
mbed_official 381:5460fc57b6e4 1314 */
mbed_official 381:5460fc57b6e4 1315
mbed_official 381:5460fc57b6e4 1316 /** @defgroup UART_Exported_Functions_Group3 Peripheral Control functions
mbed_official 381:5460fc57b6e4 1317 * @brief UART control functions
mbed_official 381:5460fc57b6e4 1318 *
mbed_official 381:5460fc57b6e4 1319 @verbatim
mbed_official 381:5460fc57b6e4 1320 ===============================================================================
mbed_official 381:5460fc57b6e4 1321 ##### Peripheral Control functions #####
mbed_official 381:5460fc57b6e4 1322 ===============================================================================
mbed_official 381:5460fc57b6e4 1323 [..]
mbed_official 381:5460fc57b6e4 1324 This subsection provides a set of functions allowing to control the UART.
mbed_official 381:5460fc57b6e4 1325 (+) HAL_MultiProcessor_EnableMuteMode() API enables mute mode
mbed_official 381:5460fc57b6e4 1326 (+) HAL_MultiProcessor_DisableMuteMode() API disables mute mode
mbed_official 381:5460fc57b6e4 1327 (+) HAL_MultiProcessor_EnterMuteMode() API enters mute mode
mbed_official 381:5460fc57b6e4 1328 (+) HAL_MultiProcessor_EnableMuteMode() API enables mute mode
mbed_official 381:5460fc57b6e4 1329 (+) UART_SetConfig() API configures the UART peripheral
mbed_official 381:5460fc57b6e4 1330 (+) UART_AdvFeatureConfig() API optionally configures the UART advanced features
mbed_official 381:5460fc57b6e4 1331 (+) UART_CheckIdleState() API ensures that TEACK and/or REACK are set after initialization
mbed_official 381:5460fc57b6e4 1332 (+) UART_Wakeup_AddressConfig() API configures the wake-up from stop mode parameters
mbed_official 381:5460fc57b6e4 1333 (+) HAL_HalfDuplex_EnableTransmitter() API disables receiver and enables transmitter
mbed_official 381:5460fc57b6e4 1334 (+) HAL_HalfDuplex_EnableReceiver() API disables transmitter and enables receiver
mbed_official 381:5460fc57b6e4 1335 (+) HAL_LIN_SendBreak() API transmits the break characters
mbed_official 381:5460fc57b6e4 1336 @endverbatim
mbed_official 381:5460fc57b6e4 1337 * @{
mbed_official 381:5460fc57b6e4 1338 */
mbed_official 381:5460fc57b6e4 1339
mbed_official 381:5460fc57b6e4 1340 /**
mbed_official 381:5460fc57b6e4 1341 * @brief Enable UART in mute mode (doesn't mean UART enters mute mode;
mbed_official 381:5460fc57b6e4 1342 * to enter mute mode, HAL_MultiProcessor_EnterMuteMode() API must be called)
mbed_official 381:5460fc57b6e4 1343 * @param huart: UART handle
mbed_official 381:5460fc57b6e4 1344 * @retval HAL status
mbed_official 381:5460fc57b6e4 1345 */
mbed_official 381:5460fc57b6e4 1346 HAL_StatusTypeDef HAL_MultiProcessor_EnableMuteMode(UART_HandleTypeDef *huart)
mbed_official 381:5460fc57b6e4 1347 {
mbed_official 381:5460fc57b6e4 1348 /* Process Locked */
mbed_official 381:5460fc57b6e4 1349 __HAL_LOCK(huart);
mbed_official 381:5460fc57b6e4 1350
mbed_official 381:5460fc57b6e4 1351 huart->State = HAL_UART_STATE_BUSY;
mbed_official 381:5460fc57b6e4 1352
mbed_official 381:5460fc57b6e4 1353 /* Enable USART mute mode by setting the MME bit in the CR1 register */
mbed_official 381:5460fc57b6e4 1354 huart->Instance->CR1 |= USART_CR1_MME;
mbed_official 381:5460fc57b6e4 1355
mbed_official 381:5460fc57b6e4 1356 huart->State = HAL_UART_STATE_READY;
mbed_official 381:5460fc57b6e4 1357
mbed_official 381:5460fc57b6e4 1358 return (UART_CheckIdleState(huart));
mbed_official 381:5460fc57b6e4 1359 }
mbed_official 381:5460fc57b6e4 1360
mbed_official 381:5460fc57b6e4 1361 /**
mbed_official 381:5460fc57b6e4 1362 * @brief Disable UART mute mode (doesn't mean it actually wakes up the software,
mbed_official 381:5460fc57b6e4 1363 * as it may not have been in mute mode at this very moment).
mbed_official 381:5460fc57b6e4 1364 * @param huart: uart handle
mbed_official 381:5460fc57b6e4 1365 * @retval HAL status
mbed_official 381:5460fc57b6e4 1366 */
mbed_official 381:5460fc57b6e4 1367 HAL_StatusTypeDef HAL_MultiProcessor_DisableMuteMode(UART_HandleTypeDef *huart)
mbed_official 381:5460fc57b6e4 1368 {
mbed_official 381:5460fc57b6e4 1369 /* Process Locked */
mbed_official 381:5460fc57b6e4 1370 __HAL_LOCK(huart);
mbed_official 381:5460fc57b6e4 1371
mbed_official 381:5460fc57b6e4 1372 huart->State = HAL_UART_STATE_BUSY;
mbed_official 381:5460fc57b6e4 1373
mbed_official 381:5460fc57b6e4 1374 /* Disable USART mute mode by clearing the MME bit in the CR1 register */
mbed_official 381:5460fc57b6e4 1375 huart->Instance->CR1 &= ~(USART_CR1_MME);
mbed_official 381:5460fc57b6e4 1376
mbed_official 381:5460fc57b6e4 1377 huart->State = HAL_UART_STATE_READY;
mbed_official 381:5460fc57b6e4 1378
mbed_official 381:5460fc57b6e4 1379 return (UART_CheckIdleState(huart));
mbed_official 381:5460fc57b6e4 1380 }
mbed_official 381:5460fc57b6e4 1381
mbed_official 381:5460fc57b6e4 1382 /**
mbed_official 381:5460fc57b6e4 1383 * @brief Enter UART mute mode (means UART actually enters mute mode).
mbed_official 381:5460fc57b6e4 1384 * To exit from mute mode, HAL_MultiProcessor_DisableMuteMode() API must be called.
mbed_official 381:5460fc57b6e4 1385 * @param huart: uart handle
mbed_official 381:5460fc57b6e4 1386 * @retval HAL status
mbed_official 381:5460fc57b6e4 1387 */
mbed_official 381:5460fc57b6e4 1388 void HAL_MultiProcessor_EnterMuteMode(UART_HandleTypeDef *huart)
mbed_official 381:5460fc57b6e4 1389 {
mbed_official 381:5460fc57b6e4 1390 __HAL_UART_SEND_REQ(huart, UART_MUTE_MODE_REQUEST);
mbed_official 381:5460fc57b6e4 1391 }
mbed_official 381:5460fc57b6e4 1392
mbed_official 381:5460fc57b6e4 1393 /**
mbed_official 381:5460fc57b6e4 1394 * @brief Configure the UART peripheral
mbed_official 381:5460fc57b6e4 1395 * @param huart: uart handle
mbed_official 381:5460fc57b6e4 1396 * @retval None
mbed_official 381:5460fc57b6e4 1397 */
mbed_official 381:5460fc57b6e4 1398 HAL_StatusTypeDef UART_SetConfig(UART_HandleTypeDef *huart)
mbed_official 381:5460fc57b6e4 1399 {
mbed_official 381:5460fc57b6e4 1400 uint32_t tmpreg = 0x00000000;
mbed_official 381:5460fc57b6e4 1401 UART_ClockSourceTypeDef clocksource = UART_CLOCKSOURCE_UNDEFINED;
mbed_official 381:5460fc57b6e4 1402 uint16_t brrtemp = 0x0000;
mbed_official 381:5460fc57b6e4 1403 uint16_t usartdiv = 0x0000;
mbed_official 381:5460fc57b6e4 1404 HAL_StatusTypeDef ret = HAL_OK;
mbed_official 381:5460fc57b6e4 1405
mbed_official 381:5460fc57b6e4 1406 /* Check the parameters */
mbed_official 381:5460fc57b6e4 1407 assert_param(IS_UART_BAUDRATE(huart->Init.BaudRate));
mbed_official 381:5460fc57b6e4 1408 assert_param(IS_UART_WORD_LENGTH(huart->Init.WordLength));
mbed_official 381:5460fc57b6e4 1409 assert_param(IS_UART_STOPBITS(huart->Init.StopBits));
mbed_official 381:5460fc57b6e4 1410 assert_param(IS_UART_PARITY(huart->Init.Parity));
mbed_official 381:5460fc57b6e4 1411 assert_param(IS_UART_MODE(huart->Init.Mode));
mbed_official 381:5460fc57b6e4 1412 assert_param(IS_UART_HARDWARE_FLOW_CONTROL(huart->Init.HwFlowCtl));
mbed_official 381:5460fc57b6e4 1413 assert_param(IS_UART_ONEBIT_SAMPLING(huart->Init.OneBitSampling));
mbed_official 381:5460fc57b6e4 1414 assert_param(IS_UART_OVERSAMPLING(huart->Init.OverSampling));
mbed_official 381:5460fc57b6e4 1415
mbed_official 381:5460fc57b6e4 1416
mbed_official 381:5460fc57b6e4 1417 /*-------------------------- USART CR1 Configuration -----------------------*/
mbed_official 381:5460fc57b6e4 1418 /* Clear M, PCE, PS, TE, RE and OVER8 bits and configure
mbed_official 381:5460fc57b6e4 1419 * the UART Word Length, Parity, Mode and oversampling:
mbed_official 381:5460fc57b6e4 1420 * set the M bits according to huart->Init.WordLength value
mbed_official 381:5460fc57b6e4 1421 * set PCE and PS bits according to huart->Init.Parity value
mbed_official 381:5460fc57b6e4 1422 * set TE and RE bits according to huart->Init.Mode value
mbed_official 381:5460fc57b6e4 1423 * set OVER8 bit according to huart->Init.OverSampling value */
mbed_official 381:5460fc57b6e4 1424 tmpreg = (uint32_t)huart->Init.WordLength | huart->Init.Parity | huart->Init.Mode | huart->Init.OverSampling ;
mbed_official 381:5460fc57b6e4 1425 MODIFY_REG(huart->Instance->CR1, UART_CR1_FIELDS, tmpreg);
mbed_official 381:5460fc57b6e4 1426
mbed_official 381:5460fc57b6e4 1427 /*-------------------------- USART CR2 Configuration -----------------------*/
mbed_official 381:5460fc57b6e4 1428 /* Configure the UART Stop Bits: Set STOP[13:12] bits according
mbed_official 381:5460fc57b6e4 1429 * to huart->Init.StopBits value */
mbed_official 381:5460fc57b6e4 1430 MODIFY_REG(huart->Instance->CR2, USART_CR2_STOP, huart->Init.StopBits);
mbed_official 381:5460fc57b6e4 1431
mbed_official 381:5460fc57b6e4 1432 /*-------------------------- USART CR3 Configuration -----------------------*/
mbed_official 381:5460fc57b6e4 1433 /* Configure
mbed_official 381:5460fc57b6e4 1434 * - UART HardWare Flow Control: set CTSE and RTSE bits according
mbed_official 381:5460fc57b6e4 1435 * to huart->Init.HwFlowCtl value
mbed_official 381:5460fc57b6e4 1436 * - one-bit sampling method versus three samples' majority rule according
mbed_official 381:5460fc57b6e4 1437 * to huart->Init.OneBitSampling */
mbed_official 381:5460fc57b6e4 1438 tmpreg = (uint32_t)huart->Init.HwFlowCtl | huart->Init.OneBitSampling ;
mbed_official 381:5460fc57b6e4 1439 MODIFY_REG(huart->Instance->CR3, (USART_CR3_RTSE | USART_CR3_CTSE | USART_CR3_ONEBIT), tmpreg);
mbed_official 381:5460fc57b6e4 1440
mbed_official 381:5460fc57b6e4 1441 /*-------------------------- USART BRR Configuration -----------------------*/
mbed_official 381:5460fc57b6e4 1442 __HAL_UART_GETCLOCKSOURCE(huart, clocksource);
mbed_official 381:5460fc57b6e4 1443
mbed_official 381:5460fc57b6e4 1444 /* Check the Over Sampling to set Baud Rate Register */
mbed_official 381:5460fc57b6e4 1445 if (huart->Init.OverSampling == UART_OVERSAMPLING_8)
mbed_official 381:5460fc57b6e4 1446 {
mbed_official 381:5460fc57b6e4 1447 switch (clocksource)
mbed_official 381:5460fc57b6e4 1448 {
mbed_official 381:5460fc57b6e4 1449 case UART_CLOCKSOURCE_PCLK1:
mbed_official 381:5460fc57b6e4 1450 usartdiv = (uint16_t)(__DIV_SAMPLING8(HAL_RCC_GetPCLK1Freq(), huart->Init.BaudRate));
mbed_official 381:5460fc57b6e4 1451 break;
mbed_official 381:5460fc57b6e4 1452 case UART_CLOCKSOURCE_PCLK2:
mbed_official 381:5460fc57b6e4 1453 usartdiv = (uint16_t)(__DIV_SAMPLING8(HAL_RCC_GetPCLK2Freq(), huart->Init.BaudRate));
mbed_official 381:5460fc57b6e4 1454 break;
mbed_official 381:5460fc57b6e4 1455 case UART_CLOCKSOURCE_HSI:
mbed_official 381:5460fc57b6e4 1456 usartdiv = (uint16_t)(__DIV_SAMPLING8(HSI_VALUE, huart->Init.BaudRate));
mbed_official 381:5460fc57b6e4 1457 break;
mbed_official 381:5460fc57b6e4 1458 case UART_CLOCKSOURCE_SYSCLK:
mbed_official 381:5460fc57b6e4 1459 usartdiv = (uint16_t)(__DIV_SAMPLING8(HAL_RCC_GetSysClockFreq(), huart->Init.BaudRate));
mbed_official 381:5460fc57b6e4 1460 break;
mbed_official 381:5460fc57b6e4 1461 case UART_CLOCKSOURCE_LSE:
mbed_official 381:5460fc57b6e4 1462 usartdiv = (uint16_t)(__DIV_SAMPLING8(LSE_VALUE, huart->Init.BaudRate));
mbed_official 381:5460fc57b6e4 1463 break;
mbed_official 381:5460fc57b6e4 1464 case UART_CLOCKSOURCE_UNDEFINED:
mbed_official 381:5460fc57b6e4 1465 default:
mbed_official 381:5460fc57b6e4 1466 ret = HAL_ERROR;
mbed_official 381:5460fc57b6e4 1467 break;
mbed_official 381:5460fc57b6e4 1468 }
mbed_official 381:5460fc57b6e4 1469
mbed_official 381:5460fc57b6e4 1470 brrtemp = usartdiv & 0xFFF0;
mbed_official 381:5460fc57b6e4 1471 brrtemp |= (uint16_t)((usartdiv & (uint16_t)0x000F) >> 1U);
mbed_official 381:5460fc57b6e4 1472 huart->Instance->BRR = brrtemp;
mbed_official 381:5460fc57b6e4 1473 }
mbed_official 381:5460fc57b6e4 1474 else
mbed_official 381:5460fc57b6e4 1475 {
mbed_official 381:5460fc57b6e4 1476 switch (clocksource)
mbed_official 381:5460fc57b6e4 1477 {
mbed_official 381:5460fc57b6e4 1478 case UART_CLOCKSOURCE_PCLK1:
mbed_official 381:5460fc57b6e4 1479 huart->Instance->BRR = (uint16_t)(__DIV_SAMPLING16(HAL_RCC_GetPCLK1Freq(), huart->Init.BaudRate));
mbed_official 381:5460fc57b6e4 1480 break;
mbed_official 381:5460fc57b6e4 1481 case UART_CLOCKSOURCE_PCLK2:
mbed_official 381:5460fc57b6e4 1482 huart->Instance->BRR = (uint16_t)(__DIV_SAMPLING16(HAL_RCC_GetPCLK2Freq(), huart->Init.BaudRate));
mbed_official 381:5460fc57b6e4 1483 break;
mbed_official 381:5460fc57b6e4 1484 case UART_CLOCKSOURCE_HSI:
mbed_official 381:5460fc57b6e4 1485 huart->Instance->BRR = (uint16_t)(__DIV_SAMPLING16(HSI_VALUE, huart->Init.BaudRate));
mbed_official 381:5460fc57b6e4 1486 break;
mbed_official 381:5460fc57b6e4 1487 case UART_CLOCKSOURCE_SYSCLK:
mbed_official 381:5460fc57b6e4 1488 huart->Instance->BRR = (uint16_t)(__DIV_SAMPLING16(HAL_RCC_GetSysClockFreq(), huart->Init.BaudRate));
mbed_official 381:5460fc57b6e4 1489 break;
mbed_official 381:5460fc57b6e4 1490 case UART_CLOCKSOURCE_LSE:
mbed_official 381:5460fc57b6e4 1491 huart->Instance->BRR = (uint16_t)(__DIV_SAMPLING16(LSE_VALUE, huart->Init.BaudRate));
mbed_official 381:5460fc57b6e4 1492 break;
mbed_official 381:5460fc57b6e4 1493 case UART_CLOCKSOURCE_UNDEFINED:
mbed_official 381:5460fc57b6e4 1494 default:
mbed_official 381:5460fc57b6e4 1495 ret = HAL_ERROR;
mbed_official 381:5460fc57b6e4 1496 break;
mbed_official 381:5460fc57b6e4 1497 }
mbed_official 381:5460fc57b6e4 1498 }
mbed_official 381:5460fc57b6e4 1499
mbed_official 381:5460fc57b6e4 1500 return ret;
mbed_official 381:5460fc57b6e4 1501
mbed_official 381:5460fc57b6e4 1502 }
mbed_official 381:5460fc57b6e4 1503
mbed_official 381:5460fc57b6e4 1504
mbed_official 381:5460fc57b6e4 1505 /**
mbed_official 381:5460fc57b6e4 1506 * @brief Configure the UART peripheral advanced feautures
mbed_official 381:5460fc57b6e4 1507 * @param huart: uart handle
mbed_official 381:5460fc57b6e4 1508 * @retval None
mbed_official 381:5460fc57b6e4 1509 */
mbed_official 381:5460fc57b6e4 1510 void UART_AdvFeatureConfig(UART_HandleTypeDef *huart)
mbed_official 381:5460fc57b6e4 1511 {
mbed_official 381:5460fc57b6e4 1512 /* Check whether the set of advanced features to configure is properly set */
mbed_official 381:5460fc57b6e4 1513 assert_param(IS_UART_ADVFEATURE_INIT(huart->AdvancedInit.AdvFeatureInit));
mbed_official 381:5460fc57b6e4 1514
mbed_official 381:5460fc57b6e4 1515 /* if required, configure TX pin active level inversion */
mbed_official 381:5460fc57b6e4 1516 if (HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_TXINVERT_INIT))
mbed_official 381:5460fc57b6e4 1517 {
mbed_official 381:5460fc57b6e4 1518 assert_param(IS_UART_ADVFEATURE_TXINV(huart->AdvancedInit.TxPinLevelInvert));
mbed_official 381:5460fc57b6e4 1519 MODIFY_REG(huart->Instance->CR2, USART_CR2_TXINV, huart->AdvancedInit.TxPinLevelInvert);
mbed_official 381:5460fc57b6e4 1520 }
mbed_official 381:5460fc57b6e4 1521
mbed_official 381:5460fc57b6e4 1522 /* if required, configure RX pin active level inversion */
mbed_official 381:5460fc57b6e4 1523 if (HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_RXINVERT_INIT))
mbed_official 381:5460fc57b6e4 1524 {
mbed_official 381:5460fc57b6e4 1525 assert_param(IS_UART_ADVFEATURE_RXINV(huart->AdvancedInit.RxPinLevelInvert));
mbed_official 381:5460fc57b6e4 1526 MODIFY_REG(huart->Instance->CR2, USART_CR2_RXINV, huart->AdvancedInit.RxPinLevelInvert);
mbed_official 381:5460fc57b6e4 1527 }
mbed_official 381:5460fc57b6e4 1528
mbed_official 381:5460fc57b6e4 1529 /* if required, configure data inversion */
mbed_official 381:5460fc57b6e4 1530 if (HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_DATAINVERT_INIT))
mbed_official 381:5460fc57b6e4 1531 {
mbed_official 381:5460fc57b6e4 1532 assert_param(IS_UART_ADVFEATURE_DATAINV(huart->AdvancedInit.DataInvert));
mbed_official 381:5460fc57b6e4 1533 MODIFY_REG(huart->Instance->CR2, USART_CR2_DATAINV, huart->AdvancedInit.DataInvert);
mbed_official 381:5460fc57b6e4 1534 }
mbed_official 381:5460fc57b6e4 1535
mbed_official 381:5460fc57b6e4 1536 /* if required, configure RX/TX pins swap */
mbed_official 381:5460fc57b6e4 1537 if (HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_SWAP_INIT))
mbed_official 381:5460fc57b6e4 1538 {
mbed_official 381:5460fc57b6e4 1539 assert_param(IS_UART_ADVFEATURE_SWAP(huart->AdvancedInit.Swap));
mbed_official 381:5460fc57b6e4 1540 MODIFY_REG(huart->Instance->CR2, USART_CR2_SWAP, huart->AdvancedInit.Swap);
mbed_official 381:5460fc57b6e4 1541 }
mbed_official 381:5460fc57b6e4 1542
mbed_official 381:5460fc57b6e4 1543 /* if required, configure RX overrun detection disabling */
mbed_official 381:5460fc57b6e4 1544 if (HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_RXOVERRUNDISABLE_INIT))
mbed_official 381:5460fc57b6e4 1545 {
mbed_official 381:5460fc57b6e4 1546 assert_param(IS_UART_OVERRUN(huart->AdvancedInit.OverrunDisable));
mbed_official 381:5460fc57b6e4 1547 MODIFY_REG(huart->Instance->CR3, USART_CR3_OVRDIS, huart->AdvancedInit.OverrunDisable);
mbed_official 381:5460fc57b6e4 1548 }
mbed_official 381:5460fc57b6e4 1549
mbed_official 381:5460fc57b6e4 1550 /* if required, configure DMA disabling on reception error */
mbed_official 381:5460fc57b6e4 1551 if (HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_DMADISABLEONERROR_INIT))
mbed_official 381:5460fc57b6e4 1552 {
mbed_official 381:5460fc57b6e4 1553 assert_param(IS_UART_ADVFEATURE_DMAONRXERROR(huart->AdvancedInit.DMADisableonRxError));
mbed_official 381:5460fc57b6e4 1554 MODIFY_REG(huart->Instance->CR3, USART_CR3_DDRE, huart->AdvancedInit.DMADisableonRxError);
mbed_official 381:5460fc57b6e4 1555 }
mbed_official 381:5460fc57b6e4 1556
mbed_official 381:5460fc57b6e4 1557 /* if required, configure auto Baud rate detection scheme */
mbed_official 381:5460fc57b6e4 1558 if (HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_AUTOBAUDRATE_INIT))
mbed_official 381:5460fc57b6e4 1559 {
mbed_official 381:5460fc57b6e4 1560 assert_param(IS_USART_AUTOBAUDRATE_DETECTION_INSTANCE(huart->Instance));
mbed_official 381:5460fc57b6e4 1561 assert_param(IS_UART_ADVFEATURE_AUTOBAUDRATE(huart->AdvancedInit.AutoBaudRateEnable));
mbed_official 381:5460fc57b6e4 1562 MODIFY_REG(huart->Instance->CR2, USART_CR2_ABREN, huart->AdvancedInit.AutoBaudRateEnable);
mbed_official 381:5460fc57b6e4 1563 /* set auto Baudrate detection parameters if detection is enabled */
mbed_official 381:5460fc57b6e4 1564 if (huart->AdvancedInit.AutoBaudRateEnable == UART_ADVFEATURE_AUTOBAUDRATE_ENABLE)
mbed_official 381:5460fc57b6e4 1565 {
mbed_official 381:5460fc57b6e4 1566 assert_param(IS_UART_ADVFEATURE_AUTOBAUDRATEMODE(huart->AdvancedInit.AutoBaudRateMode));
mbed_official 381:5460fc57b6e4 1567 MODIFY_REG(huart->Instance->CR2, USART_CR2_ABRMODE, huart->AdvancedInit.AutoBaudRateMode);
mbed_official 381:5460fc57b6e4 1568 }
mbed_official 381:5460fc57b6e4 1569 }
mbed_official 381:5460fc57b6e4 1570
mbed_official 381:5460fc57b6e4 1571 /* if required, configure MSB first on communication line */
mbed_official 381:5460fc57b6e4 1572 if (HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_MSBFIRST_INIT))
mbed_official 381:5460fc57b6e4 1573 {
mbed_official 381:5460fc57b6e4 1574 assert_param(IS_UART_ADVFEATURE_MSBFIRST(huart->AdvancedInit.MSBFirst));
mbed_official 381:5460fc57b6e4 1575 MODIFY_REG(huart->Instance->CR2, USART_CR2_MSBFIRST, huart->AdvancedInit.MSBFirst);
mbed_official 381:5460fc57b6e4 1576 }
mbed_official 381:5460fc57b6e4 1577 }
mbed_official 381:5460fc57b6e4 1578
mbed_official 381:5460fc57b6e4 1579
mbed_official 381:5460fc57b6e4 1580
mbed_official 381:5460fc57b6e4 1581 /**
mbed_official 381:5460fc57b6e4 1582 * @brief Check the UART Idle State
mbed_official 381:5460fc57b6e4 1583 * @param huart: uart handle
mbed_official 381:5460fc57b6e4 1584 * @retval HAL status
mbed_official 381:5460fc57b6e4 1585 */
mbed_official 381:5460fc57b6e4 1586 HAL_StatusTypeDef UART_CheckIdleState(UART_HandleTypeDef *huart)
mbed_official 381:5460fc57b6e4 1587 {
mbed_official 381:5460fc57b6e4 1588 /* Initialize the UART ErrorCode */
mbed_official 381:5460fc57b6e4 1589 huart->ErrorCode = HAL_UART_ERROR_NONE;
mbed_official 381:5460fc57b6e4 1590
mbed_official 381:5460fc57b6e4 1591 /* Check if the Transmitter is enabled */
mbed_official 381:5460fc57b6e4 1592 if((huart->Instance->CR1 & USART_CR1_TE) == USART_CR1_TE)
mbed_official 381:5460fc57b6e4 1593 {
mbed_official 381:5460fc57b6e4 1594 /* Wait until TEACK flag is set */
mbed_official 381:5460fc57b6e4 1595 if(UART_WaitOnFlagUntilTimeout(huart, USART_ISR_TEACK, RESET, HAL_UART_TIMEOUT_VALUE) != HAL_OK)
mbed_official 381:5460fc57b6e4 1596 {
mbed_official 381:5460fc57b6e4 1597 /* Timeout Occured */
mbed_official 381:5460fc57b6e4 1598 return HAL_TIMEOUT;
mbed_official 381:5460fc57b6e4 1599 }
mbed_official 381:5460fc57b6e4 1600 }
mbed_official 381:5460fc57b6e4 1601 /* Check if the Receiver is enabled */
mbed_official 381:5460fc57b6e4 1602 if((huart->Instance->CR1 & USART_CR1_RE) == USART_CR1_RE)
mbed_official 381:5460fc57b6e4 1603 {
mbed_official 381:5460fc57b6e4 1604 /* Wait until REACK flag is set */
mbed_official 381:5460fc57b6e4 1605 if(UART_WaitOnFlagUntilTimeout(huart, USART_ISR_REACK, RESET, HAL_UART_TIMEOUT_VALUE) != HAL_OK)
mbed_official 381:5460fc57b6e4 1606 {
mbed_official 381:5460fc57b6e4 1607 /* Timeout Occured */
mbed_official 381:5460fc57b6e4 1608 return HAL_TIMEOUT;
mbed_official 381:5460fc57b6e4 1609 }
mbed_official 381:5460fc57b6e4 1610 }
mbed_official 381:5460fc57b6e4 1611
mbed_official 381:5460fc57b6e4 1612 /* Initialize the UART State */
mbed_official 381:5460fc57b6e4 1613 huart->State= HAL_UART_STATE_READY;
mbed_official 381:5460fc57b6e4 1614
mbed_official 381:5460fc57b6e4 1615 /* Process Unlocked */
mbed_official 381:5460fc57b6e4 1616 __HAL_UNLOCK(huart);
mbed_official 381:5460fc57b6e4 1617
mbed_official 381:5460fc57b6e4 1618 return HAL_OK;
mbed_official 381:5460fc57b6e4 1619 }
mbed_official 381:5460fc57b6e4 1620
mbed_official 381:5460fc57b6e4 1621
mbed_official 381:5460fc57b6e4 1622
mbed_official 381:5460fc57b6e4 1623
mbed_official 381:5460fc57b6e4 1624 /**
mbed_official 381:5460fc57b6e4 1625 * @brief Initializes the UART wake-up from stop mode parameters when triggered by address detection.
mbed_official 381:5460fc57b6e4 1626 * @param huart: uart handle
mbed_official 381:5460fc57b6e4 1627 * @param WakeUpSelection: UART wake up from stop mode parameters
mbed_official 381:5460fc57b6e4 1628 * @retval HAL status
mbed_official 381:5460fc57b6e4 1629 */
mbed_official 381:5460fc57b6e4 1630 void UART_Wakeup_AddressConfig(UART_HandleTypeDef *huart, UART_WakeUpTypeDef WakeUpSelection)
mbed_official 381:5460fc57b6e4 1631 {
mbed_official 381:5460fc57b6e4 1632 assert_param(IS_UART_ADDRESSLENGTH_DETECT(WakeUpSelection.AddressLength));
mbed_official 381:5460fc57b6e4 1633
mbed_official 381:5460fc57b6e4 1634 /* Set the USART address length */
mbed_official 381:5460fc57b6e4 1635 MODIFY_REG(huart->Instance->CR2, USART_CR2_ADDM7, WakeUpSelection.AddressLength);
mbed_official 381:5460fc57b6e4 1636
mbed_official 381:5460fc57b6e4 1637 /* Set the USART address node */
mbed_official 381:5460fc57b6e4 1638 MODIFY_REG(huart->Instance->CR2, USART_CR2_ADD, ((uint32_t)WakeUpSelection.Address << UART_CR2_ADDRESS_LSB_POS));
mbed_official 381:5460fc57b6e4 1639 }
mbed_official 381:5460fc57b6e4 1640
mbed_official 381:5460fc57b6e4 1641 /**
mbed_official 381:5460fc57b6e4 1642 * @brief Enables the UART transmitter and disables the UART receiver.
mbed_official 381:5460fc57b6e4 1643 * @param huart: UART handle
mbed_official 381:5460fc57b6e4 1644 * @retval HAL status
mbed_official 381:5460fc57b6e4 1645 * @retval None
mbed_official 381:5460fc57b6e4 1646 */
mbed_official 381:5460fc57b6e4 1647 HAL_StatusTypeDef HAL_HalfDuplex_EnableTransmitter(UART_HandleTypeDef *huart)
mbed_official 381:5460fc57b6e4 1648 {
mbed_official 381:5460fc57b6e4 1649 /* Process Locked */
mbed_official 381:5460fc57b6e4 1650 __HAL_LOCK(huart);
mbed_official 381:5460fc57b6e4 1651 huart->State = HAL_UART_STATE_BUSY;
mbed_official 381:5460fc57b6e4 1652
mbed_official 381:5460fc57b6e4 1653 /* Clear TE and RE bits */
mbed_official 381:5460fc57b6e4 1654 CLEAR_BIT(huart->Instance->CR1, (USART_CR1_TE | USART_CR1_RE));
mbed_official 381:5460fc57b6e4 1655 /* Enable the USART's transmit interface by setting the TE bit in the USART CR1 register */
mbed_official 381:5460fc57b6e4 1656 SET_BIT(huart->Instance->CR1, USART_CR1_TE);
mbed_official 381:5460fc57b6e4 1657
mbed_official 381:5460fc57b6e4 1658 huart->State = HAL_UART_STATE_READY;
mbed_official 381:5460fc57b6e4 1659 /* Process Unlocked */
mbed_official 381:5460fc57b6e4 1660 __HAL_UNLOCK(huart);
mbed_official 381:5460fc57b6e4 1661
mbed_official 381:5460fc57b6e4 1662 return HAL_OK;
mbed_official 381:5460fc57b6e4 1663 }
mbed_official 381:5460fc57b6e4 1664
mbed_official 381:5460fc57b6e4 1665 /**
mbed_official 381:5460fc57b6e4 1666 * @brief Enables the UART receiver and disables the UART transmitter.
mbed_official 381:5460fc57b6e4 1667 * @param huart: UART handle
mbed_official 381:5460fc57b6e4 1668 * @retval HAL status
mbed_official 381:5460fc57b6e4 1669 */
mbed_official 381:5460fc57b6e4 1670 HAL_StatusTypeDef HAL_HalfDuplex_EnableReceiver(UART_HandleTypeDef *huart)
mbed_official 381:5460fc57b6e4 1671 {
mbed_official 381:5460fc57b6e4 1672 /* Process Locked */
mbed_official 381:5460fc57b6e4 1673 __HAL_LOCK(huart);
mbed_official 381:5460fc57b6e4 1674 huart->State = HAL_UART_STATE_BUSY;
mbed_official 381:5460fc57b6e4 1675
mbed_official 381:5460fc57b6e4 1676 /* Clear TE and RE bits */
mbed_official 381:5460fc57b6e4 1677 CLEAR_BIT(huart->Instance->CR1, (USART_CR1_TE | USART_CR1_RE));
mbed_official 381:5460fc57b6e4 1678 /* Enable the USART's receive interface by setting the RE bit in the USART CR1 register */
mbed_official 381:5460fc57b6e4 1679 SET_BIT(huart->Instance->CR1, USART_CR1_RE);
mbed_official 381:5460fc57b6e4 1680
mbed_official 381:5460fc57b6e4 1681 huart->State = HAL_UART_STATE_READY;
mbed_official 381:5460fc57b6e4 1682 /* Process Unlocked */
mbed_official 381:5460fc57b6e4 1683 __HAL_UNLOCK(huart);
mbed_official 381:5460fc57b6e4 1684
mbed_official 381:5460fc57b6e4 1685 return HAL_OK;
mbed_official 381:5460fc57b6e4 1686 }
mbed_official 381:5460fc57b6e4 1687
mbed_official 381:5460fc57b6e4 1688
mbed_official 381:5460fc57b6e4 1689 /**
mbed_official 381:5460fc57b6e4 1690 * @brief Transmits break characters.
mbed_official 381:5460fc57b6e4 1691 * @param huart: UART handle
mbed_official 381:5460fc57b6e4 1692 * @retval HAL status
mbed_official 381:5460fc57b6e4 1693 */
mbed_official 381:5460fc57b6e4 1694 HAL_StatusTypeDef HAL_LIN_SendBreak(UART_HandleTypeDef *huart)
mbed_official 381:5460fc57b6e4 1695 {
mbed_official 381:5460fc57b6e4 1696 /* Check the parameters */
mbed_official 381:5460fc57b6e4 1697 assert_param(IS_UART_INSTANCE(huart->Instance));
mbed_official 381:5460fc57b6e4 1698
mbed_official 381:5460fc57b6e4 1699 /* Process Locked */
mbed_official 381:5460fc57b6e4 1700 __HAL_LOCK(huart);
mbed_official 381:5460fc57b6e4 1701
mbed_official 381:5460fc57b6e4 1702 huart->State = HAL_UART_STATE_BUSY;
mbed_official 381:5460fc57b6e4 1703
mbed_official 381:5460fc57b6e4 1704 /* Send break characters */
mbed_official 381:5460fc57b6e4 1705 huart->Instance->RQR |= UART_SENDBREAK_REQUEST;
mbed_official 381:5460fc57b6e4 1706
mbed_official 381:5460fc57b6e4 1707 huart->State = HAL_UART_STATE_READY;
mbed_official 381:5460fc57b6e4 1708
mbed_official 381:5460fc57b6e4 1709 /* Process Unlocked */
mbed_official 381:5460fc57b6e4 1710 __HAL_UNLOCK(huart);
mbed_official 381:5460fc57b6e4 1711
mbed_official 381:5460fc57b6e4 1712 return HAL_OK;
mbed_official 381:5460fc57b6e4 1713 }
mbed_official 381:5460fc57b6e4 1714
mbed_official 381:5460fc57b6e4 1715
mbed_official 381:5460fc57b6e4 1716 /**
mbed_official 381:5460fc57b6e4 1717 * @}
mbed_official 381:5460fc57b6e4 1718 */
mbed_official 381:5460fc57b6e4 1719
mbed_official 381:5460fc57b6e4 1720 /** @defgroup UART_Exported_Functions_Group4 Peripheral State and Error functions
mbed_official 381:5460fc57b6e4 1721 * @brief UART Peripheral State functions
mbed_official 381:5460fc57b6e4 1722 *
mbed_official 381:5460fc57b6e4 1723 @verbatim
mbed_official 381:5460fc57b6e4 1724 ==============================================================================
mbed_official 381:5460fc57b6e4 1725 ##### Peripheral State and Error functions #####
mbed_official 381:5460fc57b6e4 1726 ==============================================================================
mbed_official 381:5460fc57b6e4 1727 [..]
mbed_official 381:5460fc57b6e4 1728 This subsection provides functions allowing to :
mbed_official 381:5460fc57b6e4 1729 (+) Returns the UART state.
mbed_official 381:5460fc57b6e4 1730 (+) Returns the UART error code
mbed_official 381:5460fc57b6e4 1731
mbed_official 381:5460fc57b6e4 1732 @endverbatim
mbed_official 381:5460fc57b6e4 1733 * @{
mbed_official 381:5460fc57b6e4 1734 */
mbed_official 381:5460fc57b6e4 1735
mbed_official 381:5460fc57b6e4 1736 /**
mbed_official 381:5460fc57b6e4 1737 * @brief return the UART state
mbed_official 381:5460fc57b6e4 1738 * @param huart: uart handle
mbed_official 381:5460fc57b6e4 1739 * @retval HAL state
mbed_official 381:5460fc57b6e4 1740 */
mbed_official 381:5460fc57b6e4 1741 HAL_UART_StateTypeDef HAL_UART_GetState(UART_HandleTypeDef *huart)
mbed_official 381:5460fc57b6e4 1742 {
mbed_official 381:5460fc57b6e4 1743 return huart->State;
mbed_official 381:5460fc57b6e4 1744 }
mbed_official 381:5460fc57b6e4 1745
mbed_official 381:5460fc57b6e4 1746 /**
mbed_official 381:5460fc57b6e4 1747 * @brief Return the UART error code
mbed_official 381:5460fc57b6e4 1748 * @param huart : pointer to a UART_HandleTypeDef structure that contains
mbed_official 381:5460fc57b6e4 1749 * the configuration information for the specified UART.
mbed_official 381:5460fc57b6e4 1750 * @retval UART Error Code
mbed_official 381:5460fc57b6e4 1751 */
mbed_official 381:5460fc57b6e4 1752 uint32_t HAL_UART_GetError(UART_HandleTypeDef *huart)
mbed_official 381:5460fc57b6e4 1753 {
mbed_official 381:5460fc57b6e4 1754 return huart->ErrorCode;
mbed_official 381:5460fc57b6e4 1755 }
mbed_official 381:5460fc57b6e4 1756 /**
mbed_official 381:5460fc57b6e4 1757 * @}
mbed_official 381:5460fc57b6e4 1758 */
mbed_official 381:5460fc57b6e4 1759
mbed_official 381:5460fc57b6e4 1760
mbed_official 381:5460fc57b6e4 1761 /**
mbed_official 381:5460fc57b6e4 1762 * @}
mbed_official 381:5460fc57b6e4 1763 */
mbed_official 381:5460fc57b6e4 1764
mbed_official 381:5460fc57b6e4 1765 /** @defgroup UART_Private_Functions UART Private Functions
mbed_official 381:5460fc57b6e4 1766 * @{
mbed_official 381:5460fc57b6e4 1767 */
mbed_official 381:5460fc57b6e4 1768 /**
mbed_official 381:5460fc57b6e4 1769 * @brief DMA UART transmit process complete callback
mbed_official 381:5460fc57b6e4 1770 * @param hdma: DMA handle
mbed_official 381:5460fc57b6e4 1771 * @retval None
mbed_official 381:5460fc57b6e4 1772 */
mbed_official 381:5460fc57b6e4 1773 static void UART_DMATransmitCplt(DMA_HandleTypeDef *hdma)
mbed_official 381:5460fc57b6e4 1774 {
mbed_official 381:5460fc57b6e4 1775 UART_HandleTypeDef* huart = ( UART_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
mbed_official 381:5460fc57b6e4 1776 huart->TxXferCount = 0;
mbed_official 381:5460fc57b6e4 1777
mbed_official 381:5460fc57b6e4 1778 /* Disable the DMA transfer for transmit request by setting the DMAT bit
mbed_official 381:5460fc57b6e4 1779 in the UART CR3 register */
mbed_official 381:5460fc57b6e4 1780 huart->Instance->CR3 &= (uint32_t)~((uint32_t)USART_CR3_DMAT);
mbed_official 381:5460fc57b6e4 1781
mbed_official 381:5460fc57b6e4 1782 /* Wait for UART TC Flag */
mbed_official 381:5460fc57b6e4 1783 if(UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_TC, RESET, HAL_UART_TXDMA_TIMEOUTVALUE) != HAL_OK)
mbed_official 381:5460fc57b6e4 1784 {
mbed_official 381:5460fc57b6e4 1785 /* Timeout Occured */
mbed_official 381:5460fc57b6e4 1786 huart->State = HAL_UART_STATE_TIMEOUT;
mbed_official 381:5460fc57b6e4 1787 HAL_UART_ErrorCallback(huart);
mbed_official 381:5460fc57b6e4 1788 }
mbed_official 381:5460fc57b6e4 1789 else
mbed_official 381:5460fc57b6e4 1790 {
mbed_official 381:5460fc57b6e4 1791 /* No Timeout */
mbed_official 381:5460fc57b6e4 1792 /* Check if a receive process is ongoing or not */
mbed_official 381:5460fc57b6e4 1793 if(huart->State == HAL_UART_STATE_BUSY_TX_RX)
mbed_official 381:5460fc57b6e4 1794 {
mbed_official 381:5460fc57b6e4 1795 huart->State = HAL_UART_STATE_BUSY_RX;
mbed_official 381:5460fc57b6e4 1796 }
mbed_official 381:5460fc57b6e4 1797 else
mbed_official 381:5460fc57b6e4 1798 {
mbed_official 381:5460fc57b6e4 1799 huart->State = HAL_UART_STATE_READY;
mbed_official 381:5460fc57b6e4 1800 }
mbed_official 381:5460fc57b6e4 1801 HAL_UART_TxCpltCallback(huart);
mbed_official 381:5460fc57b6e4 1802 }
mbed_official 381:5460fc57b6e4 1803 }
mbed_official 381:5460fc57b6e4 1804
mbed_official 381:5460fc57b6e4 1805 /**
mbed_official 381:5460fc57b6e4 1806 * @brief DMA UART transmit process half complete callback
mbed_official 381:5460fc57b6e4 1807 * @param hdma : DMA handle
mbed_official 381:5460fc57b6e4 1808 * @retval None
mbed_official 381:5460fc57b6e4 1809 */
mbed_official 381:5460fc57b6e4 1810 static void UART_DMATxHalfCplt(DMA_HandleTypeDef *hdma)
mbed_official 381:5460fc57b6e4 1811 {
mbed_official 381:5460fc57b6e4 1812 UART_HandleTypeDef* huart = (UART_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
mbed_official 381:5460fc57b6e4 1813
mbed_official 381:5460fc57b6e4 1814 HAL_UART_TxHalfCpltCallback(huart);
mbed_official 381:5460fc57b6e4 1815 }
mbed_official 381:5460fc57b6e4 1816
mbed_official 381:5460fc57b6e4 1817 /**
mbed_official 381:5460fc57b6e4 1818 * @brief DMA UART receive process complete callback
mbed_official 381:5460fc57b6e4 1819 * @param hdma: DMA handle
mbed_official 381:5460fc57b6e4 1820 * @retval None
mbed_official 381:5460fc57b6e4 1821 */
mbed_official 381:5460fc57b6e4 1822 static void UART_DMAReceiveCplt(DMA_HandleTypeDef *hdma)
mbed_official 381:5460fc57b6e4 1823 {
mbed_official 381:5460fc57b6e4 1824 UART_HandleTypeDef* huart = ( UART_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
mbed_official 381:5460fc57b6e4 1825 huart->RxXferCount = 0;
mbed_official 381:5460fc57b6e4 1826
mbed_official 381:5460fc57b6e4 1827 /* Disable the DMA transfer for the receiver request by setting the DMAR bit
mbed_official 381:5460fc57b6e4 1828 in the UART CR3 register */
mbed_official 381:5460fc57b6e4 1829 huart->Instance->CR3 &= (uint32_t)~((uint32_t)USART_CR3_DMAR);
mbed_official 381:5460fc57b6e4 1830
mbed_official 381:5460fc57b6e4 1831 /* Check if a transmit Process is ongoing or not */
mbed_official 381:5460fc57b6e4 1832 if(huart->State == HAL_UART_STATE_BUSY_TX_RX)
mbed_official 381:5460fc57b6e4 1833 {
mbed_official 381:5460fc57b6e4 1834 huart->State = HAL_UART_STATE_BUSY_TX;
mbed_official 381:5460fc57b6e4 1835 }
mbed_official 381:5460fc57b6e4 1836 else
mbed_official 381:5460fc57b6e4 1837 {
mbed_official 381:5460fc57b6e4 1838 huart->State = HAL_UART_STATE_READY;
mbed_official 381:5460fc57b6e4 1839 }
mbed_official 381:5460fc57b6e4 1840 HAL_UART_RxCpltCallback(huart);
mbed_official 381:5460fc57b6e4 1841 }
mbed_official 381:5460fc57b6e4 1842
mbed_official 381:5460fc57b6e4 1843 /**
mbed_official 381:5460fc57b6e4 1844 * @brief DMA UART receive process half complete callback
mbed_official 381:5460fc57b6e4 1845 * @param hdma : DMA handle
mbed_official 381:5460fc57b6e4 1846 * @retval None
mbed_official 381:5460fc57b6e4 1847 */
mbed_official 381:5460fc57b6e4 1848 static void UART_DMARxHalfCplt(DMA_HandleTypeDef *hdma)
mbed_official 381:5460fc57b6e4 1849 {
mbed_official 381:5460fc57b6e4 1850 UART_HandleTypeDef* huart = (UART_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
mbed_official 381:5460fc57b6e4 1851
mbed_official 381:5460fc57b6e4 1852 HAL_UART_RxHalfCpltCallback(huart);
mbed_official 381:5460fc57b6e4 1853 }
mbed_official 381:5460fc57b6e4 1854
mbed_official 381:5460fc57b6e4 1855 /**
mbed_official 381:5460fc57b6e4 1856 * @brief DMA UART communication error callback
mbed_official 381:5460fc57b6e4 1857 * @param hdma: DMA handle
mbed_official 381:5460fc57b6e4 1858 * @retval None
mbed_official 381:5460fc57b6e4 1859 */
mbed_official 381:5460fc57b6e4 1860 static void UART_DMAError(DMA_HandleTypeDef *hdma)
mbed_official 381:5460fc57b6e4 1861 {
mbed_official 381:5460fc57b6e4 1862 UART_HandleTypeDef* huart = ( UART_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
mbed_official 381:5460fc57b6e4 1863 huart->RxXferCount = 0;
mbed_official 381:5460fc57b6e4 1864 huart->TxXferCount = 0;
mbed_official 381:5460fc57b6e4 1865 huart->State= HAL_UART_STATE_READY;
mbed_official 381:5460fc57b6e4 1866 huart->ErrorCode |= HAL_UART_ERROR_DMA;
mbed_official 381:5460fc57b6e4 1867 HAL_UART_ErrorCallback(huart);
mbed_official 381:5460fc57b6e4 1868 }
mbed_official 381:5460fc57b6e4 1869
mbed_official 381:5460fc57b6e4 1870 /**
mbed_official 381:5460fc57b6e4 1871 * @brief Send an amount of data in interrupt mode
mbed_official 381:5460fc57b6e4 1872 * Function called under interruption only, once
mbed_official 381:5460fc57b6e4 1873 * interruptions have been enabled by HAL_UART_Transmit_IT()
mbed_official 381:5460fc57b6e4 1874 * @param huart: UART handle
mbed_official 381:5460fc57b6e4 1875 * @retval HAL status
mbed_official 381:5460fc57b6e4 1876 */
mbed_official 381:5460fc57b6e4 1877 static HAL_StatusTypeDef UART_Transmit_IT(UART_HandleTypeDef *huart)
mbed_official 381:5460fc57b6e4 1878 {
mbed_official 381:5460fc57b6e4 1879 uint16_t* tmp;
mbed_official 381:5460fc57b6e4 1880
mbed_official 381:5460fc57b6e4 1881 if ((huart->State == HAL_UART_STATE_BUSY_TX) || (huart->State == HAL_UART_STATE_BUSY_TX_RX))
mbed_official 381:5460fc57b6e4 1882 {
mbed_official 381:5460fc57b6e4 1883
mbed_official 381:5460fc57b6e4 1884 if(huart->TxXferCount == 0)
mbed_official 381:5460fc57b6e4 1885 {
mbed_official 381:5460fc57b6e4 1886 /* Disable the UART Transmit Data Register Empty Interrupt */
mbed_official 381:5460fc57b6e4 1887 __HAL_UART_DISABLE_IT(huart, UART_IT_TXE);
mbed_official 381:5460fc57b6e4 1888
mbed_official 381:5460fc57b6e4 1889 /* Enable the UART Transmit Complete Interrupt */
mbed_official 381:5460fc57b6e4 1890 __HAL_UART_ENABLE_IT(huart, UART_IT_TC);
mbed_official 381:5460fc57b6e4 1891
mbed_official 381:5460fc57b6e4 1892 return HAL_OK;
mbed_official 381:5460fc57b6e4 1893 }
mbed_official 381:5460fc57b6e4 1894 else
mbed_official 381:5460fc57b6e4 1895 {
mbed_official 381:5460fc57b6e4 1896 if ((huart->Init.WordLength == UART_WORDLENGTH_9B) && (huart->Init.Parity == UART_PARITY_NONE))
mbed_official 381:5460fc57b6e4 1897 {
mbed_official 381:5460fc57b6e4 1898 tmp = (uint16_t*) huart->pTxBuffPtr;
mbed_official 381:5460fc57b6e4 1899 huart->Instance->TDR = (*tmp & (uint16_t)0x01FF);
mbed_official 381:5460fc57b6e4 1900 huart->pTxBuffPtr += 2;
mbed_official 381:5460fc57b6e4 1901 }
mbed_official 381:5460fc57b6e4 1902 else
mbed_official 381:5460fc57b6e4 1903 {
mbed_official 381:5460fc57b6e4 1904 huart->Instance->TDR = (uint8_t)(*huart->pTxBuffPtr++ & (uint8_t)0xFF);
mbed_official 381:5460fc57b6e4 1905 }
mbed_official 381:5460fc57b6e4 1906
mbed_official 381:5460fc57b6e4 1907 huart->TxXferCount--;
mbed_official 381:5460fc57b6e4 1908
mbed_official 381:5460fc57b6e4 1909 return HAL_OK;
mbed_official 381:5460fc57b6e4 1910 }
mbed_official 381:5460fc57b6e4 1911 }
mbed_official 381:5460fc57b6e4 1912 else
mbed_official 381:5460fc57b6e4 1913 {
mbed_official 381:5460fc57b6e4 1914 return HAL_BUSY;
mbed_official 381:5460fc57b6e4 1915 }
mbed_official 381:5460fc57b6e4 1916 }
mbed_official 381:5460fc57b6e4 1917
mbed_official 381:5460fc57b6e4 1918
mbed_official 381:5460fc57b6e4 1919 /**
mbed_official 381:5460fc57b6e4 1920 * @brief Wraps up transmission in non blocking mode.
mbed_official 381:5460fc57b6e4 1921 * @param huart: pointer to a UART_HandleTypeDef structure that contains
mbed_official 381:5460fc57b6e4 1922 * the configuration information for the specified UART module.
mbed_official 381:5460fc57b6e4 1923 * @retval HAL status
mbed_official 381:5460fc57b6e4 1924 */
mbed_official 381:5460fc57b6e4 1925 static HAL_StatusTypeDef UART_EndTransmit_IT(UART_HandleTypeDef *huart)
mbed_official 381:5460fc57b6e4 1926 {
mbed_official 381:5460fc57b6e4 1927 /* Disable the UART Transmit Complete Interrupt */
mbed_official 381:5460fc57b6e4 1928 __HAL_UART_DISABLE_IT(huart, UART_IT_TC);
mbed_official 381:5460fc57b6e4 1929
mbed_official 381:5460fc57b6e4 1930 /* Check if a receive process is ongoing or not */
mbed_official 381:5460fc57b6e4 1931 if(huart->State == HAL_UART_STATE_BUSY_TX_RX)
mbed_official 381:5460fc57b6e4 1932 {
mbed_official 381:5460fc57b6e4 1933 huart->State = HAL_UART_STATE_BUSY_RX;
mbed_official 381:5460fc57b6e4 1934 }
mbed_official 381:5460fc57b6e4 1935 else
mbed_official 381:5460fc57b6e4 1936 {
mbed_official 381:5460fc57b6e4 1937 /* Disable the UART Error Interrupt: (Frame error, noise error, overrun error) */
mbed_official 381:5460fc57b6e4 1938 __HAL_UART_DISABLE_IT(huart, UART_IT_ERR);
mbed_official 381:5460fc57b6e4 1939
mbed_official 381:5460fc57b6e4 1940 huart->State = HAL_UART_STATE_READY;
mbed_official 381:5460fc57b6e4 1941 }
mbed_official 381:5460fc57b6e4 1942
mbed_official 381:5460fc57b6e4 1943 HAL_UART_TxCpltCallback(huart);
mbed_official 381:5460fc57b6e4 1944
mbed_official 381:5460fc57b6e4 1945 return HAL_OK;
mbed_official 381:5460fc57b6e4 1946 }
mbed_official 381:5460fc57b6e4 1947
mbed_official 381:5460fc57b6e4 1948
mbed_official 381:5460fc57b6e4 1949 /**
mbed_official 381:5460fc57b6e4 1950 * @brief Receive an amount of data in interrupt mode
mbed_official 381:5460fc57b6e4 1951 * Function called under interruption only, once
mbed_official 381:5460fc57b6e4 1952 * interruptions have been enabled by HAL_UART_Receive_IT()
mbed_official 381:5460fc57b6e4 1953 * @param huart: UART handle
mbed_official 381:5460fc57b6e4 1954 * @retval HAL status
mbed_official 381:5460fc57b6e4 1955 */
mbed_official 381:5460fc57b6e4 1956 static HAL_StatusTypeDef UART_Receive_IT(UART_HandleTypeDef *huart)
mbed_official 381:5460fc57b6e4 1957 {
mbed_official 381:5460fc57b6e4 1958 uint16_t* tmp;
mbed_official 381:5460fc57b6e4 1959 uint16_t uhMask = huart->Mask;
mbed_official 381:5460fc57b6e4 1960
mbed_official 381:5460fc57b6e4 1961 if((huart->State == HAL_UART_STATE_BUSY_RX) || (huart->State == HAL_UART_STATE_BUSY_TX_RX))
mbed_official 381:5460fc57b6e4 1962 {
mbed_official 381:5460fc57b6e4 1963
mbed_official 381:5460fc57b6e4 1964 if ((huart->Init.WordLength == UART_WORDLENGTH_9B) && (huart->Init.Parity == UART_PARITY_NONE))
mbed_official 381:5460fc57b6e4 1965 {
mbed_official 381:5460fc57b6e4 1966 tmp = (uint16_t*) huart->pRxBuffPtr ;
mbed_official 381:5460fc57b6e4 1967 *tmp = (uint16_t)(huart->Instance->RDR & uhMask);
mbed_official 381:5460fc57b6e4 1968 huart->pRxBuffPtr +=2;
mbed_official 381:5460fc57b6e4 1969 }
mbed_official 381:5460fc57b6e4 1970 else
mbed_official 381:5460fc57b6e4 1971 {
mbed_official 381:5460fc57b6e4 1972 *huart->pRxBuffPtr++ = (uint8_t)(huart->Instance->RDR & (uint8_t)uhMask);
mbed_official 381:5460fc57b6e4 1973 }
mbed_official 381:5460fc57b6e4 1974
mbed_official 381:5460fc57b6e4 1975 if(--huart->RxXferCount == 0)
mbed_official 381:5460fc57b6e4 1976 {
mbed_official 381:5460fc57b6e4 1977 __HAL_UART_DISABLE_IT(huart, UART_IT_RXNE);
mbed_official 381:5460fc57b6e4 1978
mbed_official 381:5460fc57b6e4 1979 /* Check if a transmit Process is ongoing or not */
mbed_official 381:5460fc57b6e4 1980 if(huart->State == HAL_UART_STATE_BUSY_TX_RX)
mbed_official 381:5460fc57b6e4 1981 {
mbed_official 381:5460fc57b6e4 1982 huart->State = HAL_UART_STATE_BUSY_TX;
mbed_official 381:5460fc57b6e4 1983 }
mbed_official 381:5460fc57b6e4 1984 else
mbed_official 381:5460fc57b6e4 1985 {
mbed_official 381:5460fc57b6e4 1986 /* Disable the UART Parity Error Interrupt */
mbed_official 381:5460fc57b6e4 1987 __HAL_UART_DISABLE_IT(huart, UART_IT_PE);
mbed_official 381:5460fc57b6e4 1988
mbed_official 381:5460fc57b6e4 1989 /* Disable the UART Error Interrupt: (Frame error, noise error, overrun error) */
mbed_official 381:5460fc57b6e4 1990 __HAL_UART_DISABLE_IT(huart, UART_IT_ERR);
mbed_official 381:5460fc57b6e4 1991
mbed_official 381:5460fc57b6e4 1992 huart->State = HAL_UART_STATE_READY;
mbed_official 381:5460fc57b6e4 1993 }
mbed_official 381:5460fc57b6e4 1994
mbed_official 381:5460fc57b6e4 1995 HAL_UART_RxCpltCallback(huart);
mbed_official 381:5460fc57b6e4 1996
mbed_official 381:5460fc57b6e4 1997 return HAL_OK;
mbed_official 381:5460fc57b6e4 1998 }
mbed_official 381:5460fc57b6e4 1999
mbed_official 381:5460fc57b6e4 2000 return HAL_OK;
mbed_official 381:5460fc57b6e4 2001 }
mbed_official 381:5460fc57b6e4 2002 else
mbed_official 381:5460fc57b6e4 2003 {
mbed_official 381:5460fc57b6e4 2004 return HAL_BUSY;
mbed_official 381:5460fc57b6e4 2005 }
mbed_official 381:5460fc57b6e4 2006 }
mbed_official 381:5460fc57b6e4 2007
mbed_official 381:5460fc57b6e4 2008 /**
mbed_official 381:5460fc57b6e4 2009 * @}
mbed_official 381:5460fc57b6e4 2010 */
mbed_official 381:5460fc57b6e4 2011
mbed_official 381:5460fc57b6e4 2012 #endif /* HAL_UART_MODULE_ENABLED */
mbed_official 381:5460fc57b6e4 2013 /**
mbed_official 381:5460fc57b6e4 2014 * @}
mbed_official 381:5460fc57b6e4 2015 */
mbed_official 381:5460fc57b6e4 2016
mbed_official 381:5460fc57b6e4 2017 /**
mbed_official 381:5460fc57b6e4 2018 * @}
mbed_official 381:5460fc57b6e4 2019 */
mbed_official 381:5460fc57b6e4 2020
mbed_official 381:5460fc57b6e4 2021 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/