Hal Drivers for L4

Dependents:   BSP OneHopeOnePrayer FINAL_AUDIO_RECORD AudioDemo

Fork of STM32L4xx_HAL_Driver by Senior Design: Sound Monitor

Committer:
EricLew
Date:
Mon Nov 02 19:37:23 2015 +0000
Revision:
0:80ee8f3b695e
Errors are with definitions of LCD and QSPI functions. I believe all .h and .c files are  uploaded, but there may need to be certain functions called.

Who changed what in which revision?

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