mbed library sources

Dependents:   Encrypted my_mbed lklk CyaSSL_DTLS_Cellular ... more

Superseded

This library was superseded by mbed-dev - https://os.mbed.com/users/mbed_official/code/mbed-dev/.

Development branch of the mbed library sources. This library is kept in synch with the latest changes from the mbed SDK and it is not guaranteed to work.

If you are looking for a stable and tested release, please import one of the official mbed library releases:

Import librarymbed

The official Mbed 2 C/C++ SDK provides the software platform and libraries to build your applications.

Committer:
mbed_official
Date:
Fri Aug 14 13:15:17 2015 +0100
Revision:
610:813dcc80987e
Synchronized with git revision 6d84db41c6833e0b9b024741eb0616a5f62d5599

Full URL: https://github.com/mbedmicro/mbed/commit/6d84db41c6833e0b9b024741eb0616a5f62d5599/

DISCO_F746NG - Improvements

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 610:813dcc80987e 1 /**
mbed_official 610:813dcc80987e 2 ******************************************************************************
mbed_official 610:813dcc80987e 3 * @file stm32l4xx_hal_usart.c
mbed_official 610:813dcc80987e 4 * @author MCD Application Team
mbed_official 610:813dcc80987e 5 * @version V1.0.0
mbed_official 610:813dcc80987e 6 * @date 26-June-2015
mbed_official 610:813dcc80987e 7 * @brief USART HAL module driver.
mbed_official 610:813dcc80987e 8 * This file provides firmware functions to manage the following
mbed_official 610:813dcc80987e 9 * functionalities of the Universal Synchronous/Asynchronous Receiver Transmitter
mbed_official 610:813dcc80987e 10 * Peripheral (USART).
mbed_official 610:813dcc80987e 11 * + Initialization and de-initialization functions
mbed_official 610:813dcc80987e 12 * + IO operation functions
mbed_official 610:813dcc80987e 13 * + Peripheral Control functions
mbed_official 610:813dcc80987e 14 * + Peripheral State and Error functions
mbed_official 610:813dcc80987e 15 *
mbed_official 610:813dcc80987e 16 @verbatim
mbed_official 610:813dcc80987e 17 ===============================================================================
mbed_official 610:813dcc80987e 18 ##### How to use this driver #####
mbed_official 610:813dcc80987e 19 ===============================================================================
mbed_official 610:813dcc80987e 20 [..]
mbed_official 610:813dcc80987e 21 The USART HAL driver can be used as follows:
mbed_official 610:813dcc80987e 22
mbed_official 610:813dcc80987e 23 (#) Declare a USART_HandleTypeDef handle structure (eg. USART_HandleTypeDef husart).
mbed_official 610:813dcc80987e 24 (#) Initialize the USART low level resources by implementing the HAL_USART_MspInit() API:
mbed_official 610:813dcc80987e 25 (++) Enable the USARTx interface clock.
mbed_official 610:813dcc80987e 26 (++) USART pins configuration:
mbed_official 610:813dcc80987e 27 (+++) Enable the clock for the USART GPIOs.
mbed_official 610:813dcc80987e 28 (+++) Configure these USART pins as alternate function pull-up.
mbed_official 610:813dcc80987e 29 (++) NVIC configuration if you need to use interrupt process (HAL_USART_Transmit_IT(),
mbed_official 610:813dcc80987e 30 HAL_USART_Receive_IT() and HAL_USART_TransmitReceive_IT() APIs):
mbed_official 610:813dcc80987e 31 (+++) Configure the USARTx interrupt priority.
mbed_official 610:813dcc80987e 32 (+++) Enable the NVIC USART IRQ handle.
mbed_official 610:813dcc80987e 33 (++) USART interrupts handling:
mbed_official 610:813dcc80987e 34 -@@- The specific USART interrupts (Transmission complete interrupt,
mbed_official 610:813dcc80987e 35 RXNE interrupt and Error Interrupts) will be managed using the macros
mbed_official 610:813dcc80987e 36 __HAL_USART_ENABLE_IT() and __HAL_USART_DISABLE_IT() inside the transmit and receive process.
mbed_official 610:813dcc80987e 37 (++) DMA Configuration if you need to use DMA process (HAL_USART_Transmit_DMA()
mbed_official 610:813dcc80987e 38 HAL_USART_Receive_DMA() and HAL_USART_TransmitReceive_DMA() APIs):
mbed_official 610:813dcc80987e 39 (+++) Declare a DMA handle structure for the Tx/Rx channel.
mbed_official 610:813dcc80987e 40 (+++) Enable the DMAx interface clock.
mbed_official 610:813dcc80987e 41 (+++) Configure the declared DMA handle structure with the required Tx/Rx parameters.
mbed_official 610:813dcc80987e 42 (+++) Configure the DMA Tx/Rx channel.
mbed_official 610:813dcc80987e 43 (+++) Associate the initialized DMA handle to the USART DMA Tx/Rx handle.
mbed_official 610:813dcc80987e 44 (+++) Configure the priority and enable the NVIC for the transfer complete interrupt on the DMA Tx/Rx channel.
mbed_official 610:813dcc80987e 45
mbed_official 610:813dcc80987e 46 (#) Program the Baud Rate, Word Length, Stop Bit, Parity, Hardware
mbed_official 610:813dcc80987e 47 flow control and Mode (Receiver/Transmitter) in the husart handle Init structure.
mbed_official 610:813dcc80987e 48
mbed_official 610:813dcc80987e 49 (#) Initialize the USART registers by calling the HAL_USART_Init() API:
mbed_official 610:813dcc80987e 50 (++) This API configures also the low level Hardware GPIO, CLOCK, CORTEX...etc)
mbed_official 610:813dcc80987e 51 by calling the customized HAL_USART_MspInit(&husart) API.
mbed_official 610:813dcc80987e 52
mbed_official 610:813dcc80987e 53 [..]
mbed_official 610:813dcc80987e 54 (@) To configure and enable/disable the USART to wake up the MCU from stop mode, resort to UART API's
mbed_official 610:813dcc80987e 55 HAL_UARTEx_StopModeWakeUpSourceConfig(), HAL_UARTEx_EnableStopMode() and
mbed_official 610:813dcc80987e 56 HAL_UARTEx_DisableStopMode() in casting the USART handle to UART type UART_HandleTypeDef.
mbed_official 610:813dcc80987e 57
mbed_official 610:813dcc80987e 58 @endverbatim
mbed_official 610:813dcc80987e 59 ******************************************************************************
mbed_official 610:813dcc80987e 60 * @attention
mbed_official 610:813dcc80987e 61 *
mbed_official 610:813dcc80987e 62 * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
mbed_official 610:813dcc80987e 63 *
mbed_official 610:813dcc80987e 64 * Redistribution and use in source and binary forms, with or without modification,
mbed_official 610:813dcc80987e 65 * are permitted provided that the following conditions are met:
mbed_official 610:813dcc80987e 66 * 1. Redistributions of source code must retain the above copyright notice,
mbed_official 610:813dcc80987e 67 * this list of conditions and the following disclaimer.
mbed_official 610:813dcc80987e 68 * 2. Redistributions in binary form must reproduce the above copyright notice,
mbed_official 610:813dcc80987e 69 * this list of conditions and the following disclaimer in the documentation
mbed_official 610:813dcc80987e 70 * and/or other materials provided with the distribution.
mbed_official 610:813dcc80987e 71 * 3. Neither the name of STMicroelectronics nor the names of its contributors
mbed_official 610:813dcc80987e 72 * may be used to endorse or promote products derived from this software
mbed_official 610:813dcc80987e 73 * without specific prior written permission.
mbed_official 610:813dcc80987e 74 *
mbed_official 610:813dcc80987e 75 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
mbed_official 610:813dcc80987e 76 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
mbed_official 610:813dcc80987e 77 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
mbed_official 610:813dcc80987e 78 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
mbed_official 610:813dcc80987e 79 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
mbed_official 610:813dcc80987e 80 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
mbed_official 610:813dcc80987e 81 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
mbed_official 610:813dcc80987e 82 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
mbed_official 610:813dcc80987e 83 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
mbed_official 610:813dcc80987e 84 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
mbed_official 610:813dcc80987e 85 *
mbed_official 610:813dcc80987e 86 ******************************************************************************
mbed_official 610:813dcc80987e 87 */
mbed_official 610:813dcc80987e 88
mbed_official 610:813dcc80987e 89 /* Includes ------------------------------------------------------------------*/
mbed_official 610:813dcc80987e 90 #include "stm32l4xx_hal.h"
mbed_official 610:813dcc80987e 91
mbed_official 610:813dcc80987e 92 /** @addtogroup STM32L4xx_HAL_Driver
mbed_official 610:813dcc80987e 93 * @{
mbed_official 610:813dcc80987e 94 */
mbed_official 610:813dcc80987e 95
mbed_official 610:813dcc80987e 96 /** @defgroup USART USART
mbed_official 610:813dcc80987e 97 * @brief HAL USART Synchronous module driver
mbed_official 610:813dcc80987e 98 * @{
mbed_official 610:813dcc80987e 99 */
mbed_official 610:813dcc80987e 100
mbed_official 610:813dcc80987e 101 #ifdef HAL_USART_MODULE_ENABLED
mbed_official 610:813dcc80987e 102
mbed_official 610:813dcc80987e 103 /* Private typedef -----------------------------------------------------------*/
mbed_official 610:813dcc80987e 104 /* Private define ------------------------------------------------------------*/
mbed_official 610:813dcc80987e 105 /** @defgroup USART_Private_Constants USART Private Constants
mbed_official 610:813dcc80987e 106 * @{
mbed_official 610:813dcc80987e 107 */
mbed_official 610:813dcc80987e 108 #define USART_DUMMY_DATA ((uint16_t) 0xFFFF) /*!< USART transmitted dummy data */
mbed_official 610:813dcc80987e 109 #define USART_TEACK_REACK_TIMEOUT ((uint32_t) 1000) /*!< USART TX or RX enable acknowledge time-out value */
mbed_official 610:813dcc80987e 110 #define USART_CR1_FIELDS ((uint32_t)(USART_CR1_M | USART_CR1_PCE | USART_CR1_PS | \
mbed_official 610:813dcc80987e 111 USART_CR1_TE | USART_CR1_RE | USART_CR1_OVER8)) /*!< USART CR1 fields of parameters set by USART_SetConfig API */
mbed_official 610:813dcc80987e 112 #define USART_CR2_FIELDS ((uint32_t)(USART_CR2_CPHA | USART_CR2_CPOL | \
mbed_official 610:813dcc80987e 113 USART_CR2_CLKEN | USART_CR2_LBCL | USART_CR2_STOP)) /*!< USART CR2 fields of parameters set by USART_SetConfig API */
mbed_official 610:813dcc80987e 114 /**
mbed_official 610:813dcc80987e 115 * @}
mbed_official 610:813dcc80987e 116 */
mbed_official 610:813dcc80987e 117
mbed_official 610:813dcc80987e 118 /* Private macros ------------------------------------------------------------*/
mbed_official 610:813dcc80987e 119 /* Private variables ---------------------------------------------------------*/
mbed_official 610:813dcc80987e 120 /* Private function prototypes -----------------------------------------------*/
mbed_official 610:813dcc80987e 121 /** @addtogroup USART_Private_Functions
mbed_official 610:813dcc80987e 122 * @{
mbed_official 610:813dcc80987e 123 */
mbed_official 610:813dcc80987e 124 static void USART_DMATransmitCplt(DMA_HandleTypeDef *hdma);
mbed_official 610:813dcc80987e 125 static void USART_DMAReceiveCplt(DMA_HandleTypeDef *hdma);
mbed_official 610:813dcc80987e 126 static void USART_DMATxHalfCplt(DMA_HandleTypeDef *hdma);
mbed_official 610:813dcc80987e 127 static void USART_DMARxHalfCplt(DMA_HandleTypeDef *hdma);
mbed_official 610:813dcc80987e 128 static void USART_DMAError(DMA_HandleTypeDef *hdma);
mbed_official 610:813dcc80987e 129 static HAL_StatusTypeDef USART_WaitOnFlagUntilTimeout(USART_HandleTypeDef *husart, uint32_t Flag, FlagStatus Status, uint32_t Timeout);
mbed_official 610:813dcc80987e 130 static HAL_StatusTypeDef USART_SetConfig(USART_HandleTypeDef *husart);
mbed_official 610:813dcc80987e 131 static HAL_StatusTypeDef USART_CheckIdleState(USART_HandleTypeDef *husart);
mbed_official 610:813dcc80987e 132 static HAL_StatusTypeDef USART_Transmit_IT(USART_HandleTypeDef *husart);
mbed_official 610:813dcc80987e 133 static HAL_StatusTypeDef USART_EndTransmit_IT(USART_HandleTypeDef *husart);
mbed_official 610:813dcc80987e 134 static HAL_StatusTypeDef USART_Receive_IT(USART_HandleTypeDef *husart);
mbed_official 610:813dcc80987e 135 static HAL_StatusTypeDef USART_TransmitReceive_IT(USART_HandleTypeDef *husart);
mbed_official 610:813dcc80987e 136 /**
mbed_official 610:813dcc80987e 137 * @}
mbed_official 610:813dcc80987e 138 */
mbed_official 610:813dcc80987e 139
mbed_official 610:813dcc80987e 140 /* Exported functions --------------------------------------------------------*/
mbed_official 610:813dcc80987e 141
mbed_official 610:813dcc80987e 142 /** @defgroup USART_Exported_Functions USART Exported Functions
mbed_official 610:813dcc80987e 143 * @{
mbed_official 610:813dcc80987e 144 */
mbed_official 610:813dcc80987e 145
mbed_official 610:813dcc80987e 146 /** @defgroup USART_Exported_Functions_Group1 Initialization and de-initialization functions
mbed_official 610:813dcc80987e 147 * @brief Initialization and Configuration functions
mbed_official 610:813dcc80987e 148 *
mbed_official 610:813dcc80987e 149 @verbatim
mbed_official 610:813dcc80987e 150 ===============================================================================
mbed_official 610:813dcc80987e 151 ##### Initialization and Configuration functions #####
mbed_official 610:813dcc80987e 152 ===============================================================================
mbed_official 610:813dcc80987e 153 [..]
mbed_official 610:813dcc80987e 154 This subsection provides a set of functions allowing to initialize the USART
mbed_official 610:813dcc80987e 155 in asynchronous and in synchronous modes.
mbed_official 610:813dcc80987e 156 (+) For the asynchronous mode only these parameters can be configured:
mbed_official 610:813dcc80987e 157 (++) Baud Rate
mbed_official 610:813dcc80987e 158 (++) Word Length
mbed_official 610:813dcc80987e 159 (++) Stop Bit
mbed_official 610:813dcc80987e 160 (++) Parity: If the parity is enabled, then the MSB bit of the data written
mbed_official 610:813dcc80987e 161 in the data register is transmitted but is changed by the parity bit.
mbed_official 610:813dcc80987e 162 Depending on the frame length defined by the M1 and M0 bits (7-bit,
mbed_official 610:813dcc80987e 163 8-bit or 9-bit), the possible USART frame formats are as listed in the
mbed_official 610:813dcc80987e 164 following table:
mbed_official 610:813dcc80987e 165
mbed_official 610:813dcc80987e 166 (+++) Table 1. USART frame format.
mbed_official 610:813dcc80987e 167 (+++) +-----------------------------------------------------------------------+
mbed_official 610:813dcc80987e 168 (+++) | M1 bit | M0 bit | PCE bit | USART frame |
mbed_official 610:813dcc80987e 169 (+++) |---------|---------|-----------|---------------------------------------|
mbed_official 610:813dcc80987e 170 (+++) | 0 | 0 | 0 | | SB | 8 bit data | STB | |
mbed_official 610:813dcc80987e 171 (+++) |---------|---------|-----------|---------------------------------------|
mbed_official 610:813dcc80987e 172 (+++) | 0 | 0 | 1 | | SB | 7 bit data | PB | STB | |
mbed_official 610:813dcc80987e 173 (+++) |---------|---------|-----------|---------------------------------------|
mbed_official 610:813dcc80987e 174 (+++) | 0 | 1 | 0 | | SB | 9 bit data | STB | |
mbed_official 610:813dcc80987e 175 (+++) |---------|---------|-----------|---------------------------------------|
mbed_official 610:813dcc80987e 176 (+++) | 0 | 1 | 1 | | SB | 8 bit data | PB | STB | |
mbed_official 610:813dcc80987e 177 (+++) |---------|---------|-----------|---------------------------------------|
mbed_official 610:813dcc80987e 178 (+++) | 1 | 0 | 0 | | SB | 7 bit data | STB | |
mbed_official 610:813dcc80987e 179 (+++) |---------|---------|-----------|---------------------------------------|
mbed_official 610:813dcc80987e 180 (+++) | 1 | 0 | 1 | | SB | 6 bit data | PB | STB | |
mbed_official 610:813dcc80987e 181 (+++) +-----------------------------------------------------------------------+
mbed_official 610:813dcc80987e 182 (++) USART polarity
mbed_official 610:813dcc80987e 183 (++) USART phase
mbed_official 610:813dcc80987e 184 (++) USART LastBit
mbed_official 610:813dcc80987e 185 (++) Receiver/transmitter modes
mbed_official 610:813dcc80987e 186
mbed_official 610:813dcc80987e 187 [..]
mbed_official 610:813dcc80987e 188 The HAL_USART_Init() function follows the USART synchronous configuration
mbed_official 610:813dcc80987e 189 procedure (details for the procedure are available in reference manual).
mbed_official 610:813dcc80987e 190
mbed_official 610:813dcc80987e 191 @endverbatim
mbed_official 610:813dcc80987e 192 * @{
mbed_official 610:813dcc80987e 193 */
mbed_official 610:813dcc80987e 194
mbed_official 610:813dcc80987e 195 /**
mbed_official 610:813dcc80987e 196 * @brief Initializes the USART mode according to the specified
mbed_official 610:813dcc80987e 197 * parameters in the USART_InitTypeDef and initialize the associated handle.
mbed_official 610:813dcc80987e 198 * @param husart: USART handle.
mbed_official 610:813dcc80987e 199 * @retval HAL status
mbed_official 610:813dcc80987e 200 */
mbed_official 610:813dcc80987e 201 HAL_StatusTypeDef HAL_USART_Init(USART_HandleTypeDef *husart)
mbed_official 610:813dcc80987e 202 {
mbed_official 610:813dcc80987e 203 /* Check the USART handle allocation */
mbed_official 610:813dcc80987e 204 if(husart == NULL)
mbed_official 610:813dcc80987e 205 {
mbed_official 610:813dcc80987e 206 return HAL_ERROR;
mbed_official 610:813dcc80987e 207 }
mbed_official 610:813dcc80987e 208
mbed_official 610:813dcc80987e 209 /* Check the parameters */
mbed_official 610:813dcc80987e 210 assert_param(IS_USART_INSTANCE(husart->Instance));
mbed_official 610:813dcc80987e 211
mbed_official 610:813dcc80987e 212 if(husart->State == HAL_USART_STATE_RESET)
mbed_official 610:813dcc80987e 213 {
mbed_official 610:813dcc80987e 214 /* Allocate lock resource and initialize it */
mbed_official 610:813dcc80987e 215 husart->Lock = HAL_UNLOCKED;
mbed_official 610:813dcc80987e 216
mbed_official 610:813dcc80987e 217 /* Init the low level hardware : GPIO, CLOCK */
mbed_official 610:813dcc80987e 218 HAL_USART_MspInit(husart);
mbed_official 610:813dcc80987e 219 }
mbed_official 610:813dcc80987e 220
mbed_official 610:813dcc80987e 221 husart->State = HAL_USART_STATE_BUSY;
mbed_official 610:813dcc80987e 222
mbed_official 610:813dcc80987e 223 /* Disable the Peripheral */
mbed_official 610:813dcc80987e 224 __HAL_USART_DISABLE(husart);
mbed_official 610:813dcc80987e 225
mbed_official 610:813dcc80987e 226 /* Set the Usart Communication parameters */
mbed_official 610:813dcc80987e 227 if (USART_SetConfig(husart) == HAL_ERROR)
mbed_official 610:813dcc80987e 228 {
mbed_official 610:813dcc80987e 229 return HAL_ERROR;
mbed_official 610:813dcc80987e 230 }
mbed_official 610:813dcc80987e 231
mbed_official 610:813dcc80987e 232 /* In Synchronous mode, the following bits must be kept cleared:
mbed_official 610:813dcc80987e 233 - LINEN bit in the USART_CR2 register
mbed_official 610:813dcc80987e 234 - HDSEL, SCEN and IREN bits in the USART_CR3 register.*/
mbed_official 610:813dcc80987e 235 husart->Instance->CR2 &= ~USART_CR2_LINEN;
mbed_official 610:813dcc80987e 236 husart->Instance->CR3 &= ~(USART_CR3_SCEN | USART_CR3_HDSEL | USART_CR3_IREN);
mbed_official 610:813dcc80987e 237
mbed_official 610:813dcc80987e 238 /* Enable the Peripheral */
mbed_official 610:813dcc80987e 239 __HAL_USART_ENABLE(husart);
mbed_official 610:813dcc80987e 240
mbed_official 610:813dcc80987e 241 /* TEACK and/or REACK to check before moving husart->State to Ready */
mbed_official 610:813dcc80987e 242 return (USART_CheckIdleState(husart));
mbed_official 610:813dcc80987e 243 }
mbed_official 610:813dcc80987e 244
mbed_official 610:813dcc80987e 245 /**
mbed_official 610:813dcc80987e 246 * @brief DeInitialize the USART peripheral.
mbed_official 610:813dcc80987e 247 * @param husart: USART handle.
mbed_official 610:813dcc80987e 248 * @retval HAL status
mbed_official 610:813dcc80987e 249 */
mbed_official 610:813dcc80987e 250 HAL_StatusTypeDef HAL_USART_DeInit(USART_HandleTypeDef *husart)
mbed_official 610:813dcc80987e 251 {
mbed_official 610:813dcc80987e 252 /* Check the USART handle allocation */
mbed_official 610:813dcc80987e 253 if(husart == NULL)
mbed_official 610:813dcc80987e 254 {
mbed_official 610:813dcc80987e 255 return HAL_ERROR;
mbed_official 610:813dcc80987e 256 }
mbed_official 610:813dcc80987e 257
mbed_official 610:813dcc80987e 258 /* Check the parameters */
mbed_official 610:813dcc80987e 259 assert_param(IS_USART_INSTANCE(husart->Instance));
mbed_official 610:813dcc80987e 260
mbed_official 610:813dcc80987e 261 husart->State = HAL_USART_STATE_BUSY;
mbed_official 610:813dcc80987e 262
mbed_official 610:813dcc80987e 263 husart->Instance->CR1 = 0x0;
mbed_official 610:813dcc80987e 264 husart->Instance->CR2 = 0x0;
mbed_official 610:813dcc80987e 265 husart->Instance->CR3 = 0x0;
mbed_official 610:813dcc80987e 266
mbed_official 610:813dcc80987e 267 /* DeInit the low level hardware */
mbed_official 610:813dcc80987e 268 HAL_USART_MspDeInit(husart);
mbed_official 610:813dcc80987e 269
mbed_official 610:813dcc80987e 270 husart->ErrorCode = HAL_USART_ERROR_NONE;
mbed_official 610:813dcc80987e 271 husart->State = HAL_USART_STATE_RESET;
mbed_official 610:813dcc80987e 272
mbed_official 610:813dcc80987e 273 /* Process Unlock */
mbed_official 610:813dcc80987e 274 __HAL_UNLOCK(husart);
mbed_official 610:813dcc80987e 275
mbed_official 610:813dcc80987e 276 return HAL_OK;
mbed_official 610:813dcc80987e 277 }
mbed_official 610:813dcc80987e 278
mbed_official 610:813dcc80987e 279 /**
mbed_official 610:813dcc80987e 280 * @brief Initialize the USART MSP.
mbed_official 610:813dcc80987e 281 * @param husart: USART handle.
mbed_official 610:813dcc80987e 282 * @retval None
mbed_official 610:813dcc80987e 283 */
mbed_official 610:813dcc80987e 284 __weak void HAL_USART_MspInit(USART_HandleTypeDef *husart)
mbed_official 610:813dcc80987e 285 {
mbed_official 610:813dcc80987e 286 /* NOTE : This function should not be modified, when the callback is needed,
mbed_official 610:813dcc80987e 287 the HAL_USART_MspInit can be implemented in the user file
mbed_official 610:813dcc80987e 288 */
mbed_official 610:813dcc80987e 289 }
mbed_official 610:813dcc80987e 290
mbed_official 610:813dcc80987e 291 /**
mbed_official 610:813dcc80987e 292 * @brief DeInitialize the USART MSP.
mbed_official 610:813dcc80987e 293 * @param husart: USART handle.
mbed_official 610:813dcc80987e 294 * @retval None
mbed_official 610:813dcc80987e 295 */
mbed_official 610:813dcc80987e 296 __weak void HAL_USART_MspDeInit(USART_HandleTypeDef *husart)
mbed_official 610:813dcc80987e 297 {
mbed_official 610:813dcc80987e 298 /* NOTE : This function should not be modified, when the callback is needed,
mbed_official 610:813dcc80987e 299 the HAL_USART_MspDeInit can be implemented in the user file
mbed_official 610:813dcc80987e 300 */
mbed_official 610:813dcc80987e 301 }
mbed_official 610:813dcc80987e 302
mbed_official 610:813dcc80987e 303 /**
mbed_official 610:813dcc80987e 304 * @}
mbed_official 610:813dcc80987e 305 */
mbed_official 610:813dcc80987e 306
mbed_official 610:813dcc80987e 307 /** @defgroup USART_Exported_Functions_Group2 IO operation functions
mbed_official 610:813dcc80987e 308 * @brief USART Transmit and Receive functions
mbed_official 610:813dcc80987e 309 *
mbed_official 610:813dcc80987e 310 @verbatim
mbed_official 610:813dcc80987e 311 ===============================================================================
mbed_official 610:813dcc80987e 312 ##### IO operation functions #####
mbed_official 610:813dcc80987e 313 ===============================================================================
mbed_official 610:813dcc80987e 314 [..] This subsection provides a set of functions allowing to manage the USART synchronous
mbed_official 610:813dcc80987e 315 data transfers.
mbed_official 610:813dcc80987e 316
mbed_official 610:813dcc80987e 317 [..] The USART supports master mode only: it cannot receive or send data related to an input
mbed_official 610:813dcc80987e 318 clock (SCLK is always an output).
mbed_official 610:813dcc80987e 319
mbed_official 610:813dcc80987e 320 (#) There are two modes of transfer:
mbed_official 610:813dcc80987e 321 (++) Blocking mode: The communication is performed in polling mode.
mbed_official 610:813dcc80987e 322 The HAL status of all data processing is returned by the same function
mbed_official 610:813dcc80987e 323 after finishing transfer.
mbed_official 610:813dcc80987e 324 (++) No-Blocking mode: The communication is performed using Interrupts
mbed_official 610:813dcc80987e 325 or DMA, These API's return the HAL status.
mbed_official 610:813dcc80987e 326 The end of the data processing will be indicated through the
mbed_official 610:813dcc80987e 327 dedicated USART IRQ when using Interrupt mode or the DMA IRQ when
mbed_official 610:813dcc80987e 328 using DMA mode.
mbed_official 610:813dcc80987e 329 The HAL_USART_TxCpltCallback(), HAL_USART_RxCpltCallback() and HAL_USART_TxRxCpltCallback() user callbacks
mbed_official 610:813dcc80987e 330 will be executed respectively at the end of the transmit or Receive process
mbed_official 610:813dcc80987e 331 The HAL_USART_ErrorCallback()user callback will be executed when a communication error is detected
mbed_official 610:813dcc80987e 332
mbed_official 610:813dcc80987e 333 (#) Blocking mode API's are :
mbed_official 610:813dcc80987e 334 (++) HAL_USART_Transmit()in simplex mode
mbed_official 610:813dcc80987e 335 (++) HAL_USART_Receive() in full duplex receive only
mbed_official 610:813dcc80987e 336 (++) HAL_USART_TransmitReceive() in full duplex mode
mbed_official 610:813dcc80987e 337
mbed_official 610:813dcc80987e 338 (#) Non-Blocking mode API's with Interrupt are :
mbed_official 610:813dcc80987e 339 (++) HAL_USART_Transmit_IT()in simplex mode
mbed_official 610:813dcc80987e 340 (++) HAL_USART_Receive_IT() in full duplex receive only
mbed_official 610:813dcc80987e 341 (++) HAL_USART_TransmitReceive_IT()in full duplex mode
mbed_official 610:813dcc80987e 342 (++) HAL_USART_IRQHandler()
mbed_official 610:813dcc80987e 343
mbed_official 610:813dcc80987e 344 (#) No-Blocking mode API's with DMA are :
mbed_official 610:813dcc80987e 345 (++) HAL_USART_Transmit_DMA()in simplex mode
mbed_official 610:813dcc80987e 346 (++) HAL_USART_Receive_DMA() in full duplex receive only
mbed_official 610:813dcc80987e 347 (++) HAL_USART_TransmitReceive_DMA() in full duplex mode
mbed_official 610:813dcc80987e 348 (++) HAL_USART_DMAPause()
mbed_official 610:813dcc80987e 349 (++) HAL_USART_DMAResume()
mbed_official 610:813dcc80987e 350 (++) HAL_USART_DMAStop()
mbed_official 610:813dcc80987e 351
mbed_official 610:813dcc80987e 352 (#) A set of Transfer Complete Callbacks are provided in No_Blocking mode:
mbed_official 610:813dcc80987e 353 (++) HAL_USART_TxCpltCallback()
mbed_official 610:813dcc80987e 354 (++) HAL_USART_RxCpltCallback()
mbed_official 610:813dcc80987e 355 (++) HAL_USART_TxHalfCpltCallback()
mbed_official 610:813dcc80987e 356 (++) HAL_USART_RxHalfCpltCallback()
mbed_official 610:813dcc80987e 357 (++) HAL_USART_ErrorCallback()
mbed_official 610:813dcc80987e 358 (++) HAL_USART_TxRxCpltCallback()
mbed_official 610:813dcc80987e 359
mbed_official 610:813dcc80987e 360 @endverbatim
mbed_official 610:813dcc80987e 361 * @{
mbed_official 610:813dcc80987e 362 */
mbed_official 610:813dcc80987e 363
mbed_official 610:813dcc80987e 364 /**
mbed_official 610:813dcc80987e 365 * @brief Simplex send an amount of data in blocking mode.
mbed_official 610:813dcc80987e 366 * @param husart: USART handle.
mbed_official 610:813dcc80987e 367 * @param pTxData: Pointer to data buffer.
mbed_official 610:813dcc80987e 368 * @param Size: Amount of data to be sent.
mbed_official 610:813dcc80987e 369 * @param Timeout: Timeout duration.
mbed_official 610:813dcc80987e 370 * @retval HAL status
mbed_official 610:813dcc80987e 371 */
mbed_official 610:813dcc80987e 372 HAL_StatusTypeDef HAL_USART_Transmit(USART_HandleTypeDef *husart, uint8_t *pTxData, uint16_t Size, uint32_t Timeout)
mbed_official 610:813dcc80987e 373 {
mbed_official 610:813dcc80987e 374 uint16_t* tmp;
mbed_official 610:813dcc80987e 375
mbed_official 610:813dcc80987e 376 if(husart->State == HAL_USART_STATE_READY)
mbed_official 610:813dcc80987e 377 {
mbed_official 610:813dcc80987e 378 if((pTxData == NULL) || (Size == 0))
mbed_official 610:813dcc80987e 379 {
mbed_official 610:813dcc80987e 380 return HAL_ERROR;
mbed_official 610:813dcc80987e 381 }
mbed_official 610:813dcc80987e 382
mbed_official 610:813dcc80987e 383 /* Process Locked */
mbed_official 610:813dcc80987e 384 __HAL_LOCK(husart);
mbed_official 610:813dcc80987e 385
mbed_official 610:813dcc80987e 386 husart->ErrorCode = HAL_USART_ERROR_NONE;
mbed_official 610:813dcc80987e 387 husart->State = HAL_USART_STATE_BUSY_TX;
mbed_official 610:813dcc80987e 388
mbed_official 610:813dcc80987e 389 husart->TxXferSize = Size;
mbed_official 610:813dcc80987e 390 husart->TxXferCount = Size;
mbed_official 610:813dcc80987e 391
mbed_official 610:813dcc80987e 392 /* Check the remaining data to be sent */
mbed_official 610:813dcc80987e 393 while(husart->TxXferCount > 0)
mbed_official 610:813dcc80987e 394 {
mbed_official 610:813dcc80987e 395 husart->TxXferCount--;
mbed_official 610:813dcc80987e 396 if(USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_TC, RESET, Timeout) != HAL_OK)
mbed_official 610:813dcc80987e 397 {
mbed_official 610:813dcc80987e 398 return HAL_TIMEOUT;
mbed_official 610:813dcc80987e 399 }
mbed_official 610:813dcc80987e 400 if((husart->Init.WordLength == USART_WORDLENGTH_9B) && (husart->Init.Parity == USART_PARITY_NONE))
mbed_official 610:813dcc80987e 401 {
mbed_official 610:813dcc80987e 402 tmp = (uint16_t*) pTxData;
mbed_official 610:813dcc80987e 403 husart->Instance->TDR = (*tmp & (uint16_t)0x01FF);
mbed_official 610:813dcc80987e 404 pTxData += 2;
mbed_official 610:813dcc80987e 405 }
mbed_official 610:813dcc80987e 406 else
mbed_official 610:813dcc80987e 407 {
mbed_official 610:813dcc80987e 408 husart->Instance->TDR = (*pTxData++ & (uint8_t)0xFF);
mbed_official 610:813dcc80987e 409 }
mbed_official 610:813dcc80987e 410 }
mbed_official 610:813dcc80987e 411
mbed_official 610:813dcc80987e 412 if(USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_TC, RESET, Timeout) != HAL_OK)
mbed_official 610:813dcc80987e 413 {
mbed_official 610:813dcc80987e 414 return HAL_TIMEOUT;
mbed_official 610:813dcc80987e 415 }
mbed_official 610:813dcc80987e 416
mbed_official 610:813dcc80987e 417 husart->State = HAL_USART_STATE_READY;
mbed_official 610:813dcc80987e 418
mbed_official 610:813dcc80987e 419 /* Process Unlocked */
mbed_official 610:813dcc80987e 420 __HAL_UNLOCK(husart);
mbed_official 610:813dcc80987e 421
mbed_official 610:813dcc80987e 422 return HAL_OK;
mbed_official 610:813dcc80987e 423 }
mbed_official 610:813dcc80987e 424 else
mbed_official 610:813dcc80987e 425 {
mbed_official 610:813dcc80987e 426 return HAL_BUSY;
mbed_official 610:813dcc80987e 427 }
mbed_official 610:813dcc80987e 428 }
mbed_official 610:813dcc80987e 429
mbed_official 610:813dcc80987e 430 /**
mbed_official 610:813dcc80987e 431 * @brief Receive an amount of data in blocking mode.
mbed_official 610:813dcc80987e 432 * @note To receive synchronous data, dummy data are simultaneously transmitted.
mbed_official 610:813dcc80987e 433 * @param husart: USART handle.
mbed_official 610:813dcc80987e 434 * @param pRxData: Pointer to data buffer.
mbed_official 610:813dcc80987e 435 * @param Size: Amount of data to be received.
mbed_official 610:813dcc80987e 436 * @param Timeout: Timeout duration.
mbed_official 610:813dcc80987e 437 * @retval HAL status
mbed_official 610:813dcc80987e 438 */
mbed_official 610:813dcc80987e 439 HAL_StatusTypeDef HAL_USART_Receive(USART_HandleTypeDef *husart, uint8_t *pRxData, uint16_t Size, uint32_t Timeout)
mbed_official 610:813dcc80987e 440 {
mbed_official 610:813dcc80987e 441 uint16_t* tmp;
mbed_official 610:813dcc80987e 442 uint16_t uhMask;
mbed_official 610:813dcc80987e 443
mbed_official 610:813dcc80987e 444 if(husart->State == HAL_USART_STATE_READY)
mbed_official 610:813dcc80987e 445 {
mbed_official 610:813dcc80987e 446 if((pRxData == NULL) || (Size == 0))
mbed_official 610:813dcc80987e 447 {
mbed_official 610:813dcc80987e 448 return HAL_ERROR;
mbed_official 610:813dcc80987e 449 }
mbed_official 610:813dcc80987e 450 /* Process Locked */
mbed_official 610:813dcc80987e 451 __HAL_LOCK(husart);
mbed_official 610:813dcc80987e 452
mbed_official 610:813dcc80987e 453 husart->ErrorCode = HAL_USART_ERROR_NONE;
mbed_official 610:813dcc80987e 454 husart->State = HAL_USART_STATE_BUSY_RX;
mbed_official 610:813dcc80987e 455
mbed_official 610:813dcc80987e 456 husart->RxXferSize = Size;
mbed_official 610:813dcc80987e 457 husart->RxXferCount = Size;
mbed_official 610:813dcc80987e 458
mbed_official 610:813dcc80987e 459 /* Computation of USART mask to apply to RDR register */
mbed_official 610:813dcc80987e 460 USART_MASK_COMPUTATION(husart);
mbed_official 610:813dcc80987e 461 uhMask = husart->Mask;
mbed_official 610:813dcc80987e 462
mbed_official 610:813dcc80987e 463 /* as long as data have to be received */
mbed_official 610:813dcc80987e 464 while(husart->RxXferCount > 0)
mbed_official 610:813dcc80987e 465 {
mbed_official 610:813dcc80987e 466 husart->RxXferCount--;
mbed_official 610:813dcc80987e 467
mbed_official 610:813dcc80987e 468 /* Wait until TC flag is set to send dummy byte in order to generate the
mbed_official 610:813dcc80987e 469 * clock for the slave to send data.
mbed_official 610:813dcc80987e 470 * Whatever the frame length (7, 8 or 9-bit long), the same dummy value
mbed_official 610:813dcc80987e 471 * can be written for all the cases. */
mbed_official 610:813dcc80987e 472 if(USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_TC, RESET, Timeout) != HAL_OK)
mbed_official 610:813dcc80987e 473 {
mbed_official 610:813dcc80987e 474 return HAL_TIMEOUT;
mbed_official 610:813dcc80987e 475 }
mbed_official 610:813dcc80987e 476 husart->Instance->TDR = (USART_DUMMY_DATA & (uint16_t)0x0FF);
mbed_official 610:813dcc80987e 477
mbed_official 610:813dcc80987e 478 /* Wait for RXNE Flag */
mbed_official 610:813dcc80987e 479 if(USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_RXNE, RESET, Timeout) != HAL_OK)
mbed_official 610:813dcc80987e 480 {
mbed_official 610:813dcc80987e 481 return HAL_TIMEOUT;
mbed_official 610:813dcc80987e 482 }
mbed_official 610:813dcc80987e 483
mbed_official 610:813dcc80987e 484 if((husart->Init.WordLength == USART_WORDLENGTH_9B) && (husart->Init.Parity == USART_PARITY_NONE))
mbed_official 610:813dcc80987e 485 {
mbed_official 610:813dcc80987e 486 tmp = (uint16_t*) pRxData ;
mbed_official 610:813dcc80987e 487 *tmp = (uint16_t)(husart->Instance->RDR & uhMask);
mbed_official 610:813dcc80987e 488 pRxData +=2;
mbed_official 610:813dcc80987e 489 }
mbed_official 610:813dcc80987e 490 else
mbed_official 610:813dcc80987e 491 {
mbed_official 610:813dcc80987e 492 *pRxData++ = (uint8_t)(husart->Instance->RDR & (uint8_t)uhMask);
mbed_official 610:813dcc80987e 493 }
mbed_official 610:813dcc80987e 494 }
mbed_official 610:813dcc80987e 495
mbed_official 610:813dcc80987e 496 husart->State = HAL_USART_STATE_READY;
mbed_official 610:813dcc80987e 497
mbed_official 610:813dcc80987e 498 /* Process Unlocked */
mbed_official 610:813dcc80987e 499 __HAL_UNLOCK(husart);
mbed_official 610:813dcc80987e 500
mbed_official 610:813dcc80987e 501 return HAL_OK;
mbed_official 610:813dcc80987e 502 }
mbed_official 610:813dcc80987e 503 else
mbed_official 610:813dcc80987e 504 {
mbed_official 610:813dcc80987e 505 return HAL_BUSY;
mbed_official 610:813dcc80987e 506 }
mbed_official 610:813dcc80987e 507 }
mbed_official 610:813dcc80987e 508
mbed_official 610:813dcc80987e 509 /**
mbed_official 610:813dcc80987e 510 * @brief Full-Duplex Send and Receive an amount of data in blocking mode.
mbed_official 610:813dcc80987e 511 * @param husart: USART handle.
mbed_official 610:813dcc80987e 512 * @param pTxData: pointer to TX data buffer.
mbed_official 610:813dcc80987e 513 * @param pRxData: pointer to RX data buffer.
mbed_official 610:813dcc80987e 514 * @param Size: amount of data to be sent (same amount to be received).
mbed_official 610:813dcc80987e 515 * @param Timeout: Timeout duration.
mbed_official 610:813dcc80987e 516 * @retval HAL status
mbed_official 610:813dcc80987e 517 */
mbed_official 610:813dcc80987e 518 HAL_StatusTypeDef HAL_USART_TransmitReceive(USART_HandleTypeDef *husart, uint8_t *pTxData, uint8_t *pRxData, uint16_t Size, uint32_t Timeout)
mbed_official 610:813dcc80987e 519 {
mbed_official 610:813dcc80987e 520 uint16_t* tmp;
mbed_official 610:813dcc80987e 521 uint16_t uhMask;
mbed_official 610:813dcc80987e 522
mbed_official 610:813dcc80987e 523 if(husart->State == HAL_USART_STATE_READY)
mbed_official 610:813dcc80987e 524 {
mbed_official 610:813dcc80987e 525 if((pTxData == NULL) || (pRxData == NULL) || (Size == 0))
mbed_official 610:813dcc80987e 526 {
mbed_official 610:813dcc80987e 527 return HAL_ERROR;
mbed_official 610:813dcc80987e 528 }
mbed_official 610:813dcc80987e 529 /* Process Locked */
mbed_official 610:813dcc80987e 530 __HAL_LOCK(husart);
mbed_official 610:813dcc80987e 531
mbed_official 610:813dcc80987e 532 husart->ErrorCode = HAL_USART_ERROR_NONE;
mbed_official 610:813dcc80987e 533 husart->State = HAL_USART_STATE_BUSY_RX;
mbed_official 610:813dcc80987e 534
mbed_official 610:813dcc80987e 535 husart->RxXferSize = Size;
mbed_official 610:813dcc80987e 536 husart->TxXferSize = Size;
mbed_official 610:813dcc80987e 537 husart->TxXferCount = Size;
mbed_official 610:813dcc80987e 538 husart->RxXferCount = Size;
mbed_official 610:813dcc80987e 539
mbed_official 610:813dcc80987e 540 /* Computation of USART mask to apply to RDR register */
mbed_official 610:813dcc80987e 541 USART_MASK_COMPUTATION(husart);
mbed_official 610:813dcc80987e 542 uhMask = husart->Mask;
mbed_official 610:813dcc80987e 543
mbed_official 610:813dcc80987e 544 /* Check the remain data to be sent */
mbed_official 610:813dcc80987e 545 while(husart->TxXferCount > 0)
mbed_official 610:813dcc80987e 546 {
mbed_official 610:813dcc80987e 547 husart->TxXferCount--;
mbed_official 610:813dcc80987e 548 husart->RxXferCount--;
mbed_official 610:813dcc80987e 549
mbed_official 610:813dcc80987e 550 /* Wait until TC flag is set to send data */
mbed_official 610:813dcc80987e 551 if(USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_TC, RESET, Timeout) != HAL_OK)
mbed_official 610:813dcc80987e 552 {
mbed_official 610:813dcc80987e 553 return HAL_TIMEOUT;
mbed_official 610:813dcc80987e 554 }
mbed_official 610:813dcc80987e 555 if((husart->Init.WordLength == USART_WORDLENGTH_9B) && (husart->Init.Parity == USART_PARITY_NONE))
mbed_official 610:813dcc80987e 556 {
mbed_official 610:813dcc80987e 557 tmp = (uint16_t*) pTxData;
mbed_official 610:813dcc80987e 558 husart->Instance->TDR = (*tmp & uhMask);
mbed_official 610:813dcc80987e 559 pTxData += 2;
mbed_official 610:813dcc80987e 560 }
mbed_official 610:813dcc80987e 561 else
mbed_official 610:813dcc80987e 562 {
mbed_official 610:813dcc80987e 563 husart->Instance->TDR = (*pTxData++ & (uint8_t)uhMask);
mbed_official 610:813dcc80987e 564 }
mbed_official 610:813dcc80987e 565
mbed_official 610:813dcc80987e 566 /* Wait for RXNE Flag */
mbed_official 610:813dcc80987e 567 if(USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_RXNE, RESET, Timeout) != HAL_OK)
mbed_official 610:813dcc80987e 568 {
mbed_official 610:813dcc80987e 569 return HAL_TIMEOUT;
mbed_official 610:813dcc80987e 570 }
mbed_official 610:813dcc80987e 571
mbed_official 610:813dcc80987e 572 if((husart->Init.WordLength == USART_WORDLENGTH_9B) && (husart->Init.Parity == USART_PARITY_NONE))
mbed_official 610:813dcc80987e 573 {
mbed_official 610:813dcc80987e 574 tmp = (uint16_t*) pRxData ;
mbed_official 610:813dcc80987e 575 *tmp = (uint16_t)(husart->Instance->RDR & uhMask);
mbed_official 610:813dcc80987e 576 pRxData +=2;
mbed_official 610:813dcc80987e 577 }
mbed_official 610:813dcc80987e 578 else
mbed_official 610:813dcc80987e 579 {
mbed_official 610:813dcc80987e 580 *pRxData++ = (uint8_t)(husart->Instance->RDR & (uint8_t)uhMask);
mbed_official 610:813dcc80987e 581 }
mbed_official 610:813dcc80987e 582 }
mbed_official 610:813dcc80987e 583
mbed_official 610:813dcc80987e 584 husart->State = HAL_USART_STATE_READY;
mbed_official 610:813dcc80987e 585
mbed_official 610:813dcc80987e 586 /* Process Unlocked */
mbed_official 610:813dcc80987e 587 __HAL_UNLOCK(husart);
mbed_official 610:813dcc80987e 588
mbed_official 610:813dcc80987e 589 return HAL_OK;
mbed_official 610:813dcc80987e 590 }
mbed_official 610:813dcc80987e 591 else
mbed_official 610:813dcc80987e 592 {
mbed_official 610:813dcc80987e 593 return HAL_BUSY;
mbed_official 610:813dcc80987e 594 }
mbed_official 610:813dcc80987e 595 }
mbed_official 610:813dcc80987e 596
mbed_official 610:813dcc80987e 597 /**
mbed_official 610:813dcc80987e 598 * @brief Send an amount of data in interrupt mode.
mbed_official 610:813dcc80987e 599 * @param husart: USART handle.
mbed_official 610:813dcc80987e 600 * @param pTxData: pointer to data buffer.
mbed_official 610:813dcc80987e 601 * @param Size: amount of data to be sent.
mbed_official 610:813dcc80987e 602 * @retval HAL status
mbed_official 610:813dcc80987e 603 */
mbed_official 610:813dcc80987e 604 HAL_StatusTypeDef HAL_USART_Transmit_IT(USART_HandleTypeDef *husart, uint8_t *pTxData, uint16_t Size)
mbed_official 610:813dcc80987e 605 {
mbed_official 610:813dcc80987e 606 if(husart->State == HAL_USART_STATE_READY)
mbed_official 610:813dcc80987e 607 {
mbed_official 610:813dcc80987e 608 if((pTxData == NULL ) || (Size == 0))
mbed_official 610:813dcc80987e 609 {
mbed_official 610:813dcc80987e 610 return HAL_ERROR;
mbed_official 610:813dcc80987e 611 }
mbed_official 610:813dcc80987e 612
mbed_official 610:813dcc80987e 613 /* Process Locked */
mbed_official 610:813dcc80987e 614 __HAL_LOCK(husart);
mbed_official 610:813dcc80987e 615
mbed_official 610:813dcc80987e 616 husart->pTxBuffPtr = pTxData;
mbed_official 610:813dcc80987e 617 husart->TxXferSize = Size;
mbed_official 610:813dcc80987e 618 husart->TxXferCount = Size;
mbed_official 610:813dcc80987e 619
mbed_official 610:813dcc80987e 620 husart->ErrorCode = HAL_USART_ERROR_NONE;
mbed_official 610:813dcc80987e 621 husart->State = HAL_USART_STATE_BUSY_TX;
mbed_official 610:813dcc80987e 622
mbed_official 610:813dcc80987e 623 /* The USART Error Interrupts: (Frame error, noise error, overrun error)
mbed_official 610:813dcc80987e 624 are not managed by the USART Transmit Process to avoid the overrun interrupt
mbed_official 610:813dcc80987e 625 when the usart mode is configured for transmit and receive "USART_MODE_TX_RX"
mbed_official 610:813dcc80987e 626 to benefit for the frame error and noise interrupts the usart mode should be
mbed_official 610:813dcc80987e 627 configured only for transmit "USART_MODE_TX" */
mbed_official 610:813dcc80987e 628
mbed_official 610:813dcc80987e 629 /* Process Unlocked */
mbed_official 610:813dcc80987e 630 __HAL_UNLOCK(husart);
mbed_official 610:813dcc80987e 631
mbed_official 610:813dcc80987e 632 /* Enable the USART Transmit Data Register Empty Interrupt */
mbed_official 610:813dcc80987e 633 __HAL_USART_ENABLE_IT(husart, USART_IT_TXE);
mbed_official 610:813dcc80987e 634
mbed_official 610:813dcc80987e 635 return HAL_OK;
mbed_official 610:813dcc80987e 636 }
mbed_official 610:813dcc80987e 637 else
mbed_official 610:813dcc80987e 638 {
mbed_official 610:813dcc80987e 639 return HAL_BUSY;
mbed_official 610:813dcc80987e 640 }
mbed_official 610:813dcc80987e 641 }
mbed_official 610:813dcc80987e 642
mbed_official 610:813dcc80987e 643 /**
mbed_official 610:813dcc80987e 644 * @brief Receive an amount of data in blocking mode.
mbed_official 610:813dcc80987e 645 * @note To receive synchronous data, dummy data are simultaneously transmitted.
mbed_official 610:813dcc80987e 646 * @param husart: USART handle.
mbed_official 610:813dcc80987e 647 * @param pRxData: pointer to data buffer.
mbed_official 610:813dcc80987e 648 * @param Size: amount of data to be received.
mbed_official 610:813dcc80987e 649 * @retval HAL status
mbed_official 610:813dcc80987e 650 */
mbed_official 610:813dcc80987e 651 HAL_StatusTypeDef HAL_USART_Receive_IT(USART_HandleTypeDef *husart, uint8_t *pRxData, uint16_t Size)
mbed_official 610:813dcc80987e 652 {
mbed_official 610:813dcc80987e 653 if(husart->State == HAL_USART_STATE_READY)
mbed_official 610:813dcc80987e 654 {
mbed_official 610:813dcc80987e 655 if((pRxData == NULL ) || (Size == 0))
mbed_official 610:813dcc80987e 656 {
mbed_official 610:813dcc80987e 657 return HAL_ERROR;
mbed_official 610:813dcc80987e 658 }
mbed_official 610:813dcc80987e 659 /* Process Locked */
mbed_official 610:813dcc80987e 660 __HAL_LOCK(husart);
mbed_official 610:813dcc80987e 661
mbed_official 610:813dcc80987e 662 husart->pRxBuffPtr = pRxData;
mbed_official 610:813dcc80987e 663 husart->RxXferSize = Size;
mbed_official 610:813dcc80987e 664 husart->RxXferCount = Size;
mbed_official 610:813dcc80987e 665
mbed_official 610:813dcc80987e 666 USART_MASK_COMPUTATION(husart);
mbed_official 610:813dcc80987e 667
mbed_official 610:813dcc80987e 668 husart->ErrorCode = HAL_USART_ERROR_NONE;
mbed_official 610:813dcc80987e 669 husart->State = HAL_USART_STATE_BUSY_RX;
mbed_official 610:813dcc80987e 670
mbed_official 610:813dcc80987e 671 /* Enable the USART Parity Error Interrupt */
mbed_official 610:813dcc80987e 672 __HAL_USART_ENABLE_IT(husart, USART_IT_PE);
mbed_official 610:813dcc80987e 673
mbed_official 610:813dcc80987e 674 /* Enable the USART Error Interrupt: (Frame error, noise error, overrun error) */
mbed_official 610:813dcc80987e 675 __HAL_USART_ENABLE_IT(husart, USART_IT_ERR);
mbed_official 610:813dcc80987e 676
mbed_official 610:813dcc80987e 677 /* Enable the USART Data Register not empty Interrupt */
mbed_official 610:813dcc80987e 678 __HAL_USART_ENABLE_IT(husart, USART_IT_RXNE);
mbed_official 610:813dcc80987e 679
mbed_official 610:813dcc80987e 680 /* Process Unlocked */
mbed_official 610:813dcc80987e 681 __HAL_UNLOCK(husart);
mbed_official 610:813dcc80987e 682
mbed_official 610:813dcc80987e 683
mbed_official 610:813dcc80987e 684 /* Send dummy byte in order to generate the clock for the Slave to send the next data */
mbed_official 610:813dcc80987e 685 if(husart->Init.WordLength == USART_WORDLENGTH_9B)
mbed_official 610:813dcc80987e 686 {
mbed_official 610:813dcc80987e 687 husart->Instance->TDR = (USART_DUMMY_DATA & (uint16_t)0x01FF);
mbed_official 610:813dcc80987e 688 }
mbed_official 610:813dcc80987e 689 else
mbed_official 610:813dcc80987e 690 {
mbed_official 610:813dcc80987e 691 husart->Instance->TDR = (USART_DUMMY_DATA & (uint16_t)0x00FF);
mbed_official 610:813dcc80987e 692 }
mbed_official 610:813dcc80987e 693
mbed_official 610:813dcc80987e 694 return HAL_OK;
mbed_official 610:813dcc80987e 695 }
mbed_official 610:813dcc80987e 696 else
mbed_official 610:813dcc80987e 697 {
mbed_official 610:813dcc80987e 698 return HAL_BUSY;
mbed_official 610:813dcc80987e 699 }
mbed_official 610:813dcc80987e 700 }
mbed_official 610:813dcc80987e 701
mbed_official 610:813dcc80987e 702 /**
mbed_official 610:813dcc80987e 703 * @brief Full-Duplex Send and Receive an amount of data in interrupt mode.
mbed_official 610:813dcc80987e 704 * @param husart: USART handle.
mbed_official 610:813dcc80987e 705 * @param pTxData: pointer to TX data buffer.
mbed_official 610:813dcc80987e 706 * @param pRxData: pointer to RX data buffer.
mbed_official 610:813dcc80987e 707 * @param Size: amount of data to be sent (same amount to be received).
mbed_official 610:813dcc80987e 708 * @retval HAL status
mbed_official 610:813dcc80987e 709 */
mbed_official 610:813dcc80987e 710 HAL_StatusTypeDef HAL_USART_TransmitReceive_IT(USART_HandleTypeDef *husart, uint8_t *pTxData, uint8_t *pRxData, uint16_t Size)
mbed_official 610:813dcc80987e 711 {
mbed_official 610:813dcc80987e 712
mbed_official 610:813dcc80987e 713 if(husart->State == HAL_USART_STATE_READY)
mbed_official 610:813dcc80987e 714 {
mbed_official 610:813dcc80987e 715 if((pTxData == NULL) || (pRxData == NULL) || (Size == 0))
mbed_official 610:813dcc80987e 716 {
mbed_official 610:813dcc80987e 717 return HAL_ERROR;
mbed_official 610:813dcc80987e 718 }
mbed_official 610:813dcc80987e 719 /* Process Locked */
mbed_official 610:813dcc80987e 720 __HAL_LOCK(husart);
mbed_official 610:813dcc80987e 721
mbed_official 610:813dcc80987e 722 husart->pRxBuffPtr = pRxData;
mbed_official 610:813dcc80987e 723 husart->RxXferSize = Size;
mbed_official 610:813dcc80987e 724 husart->RxXferCount = Size;
mbed_official 610:813dcc80987e 725 husart->pTxBuffPtr = pTxData;
mbed_official 610:813dcc80987e 726 husart->TxXferSize = Size;
mbed_official 610:813dcc80987e 727 husart->TxXferCount = Size;
mbed_official 610:813dcc80987e 728
mbed_official 610:813dcc80987e 729 /* Computation of USART mask to apply to RDR register */
mbed_official 610:813dcc80987e 730 USART_MASK_COMPUTATION(husart);
mbed_official 610:813dcc80987e 731
mbed_official 610:813dcc80987e 732 husart->ErrorCode = HAL_USART_ERROR_NONE;
mbed_official 610:813dcc80987e 733 husart->State = HAL_USART_STATE_BUSY_TX_RX;
mbed_official 610:813dcc80987e 734
mbed_official 610:813dcc80987e 735 /* Enable the USART Data Register not empty Interrupt */
mbed_official 610:813dcc80987e 736 __HAL_USART_ENABLE_IT(husart, USART_IT_RXNE);
mbed_official 610:813dcc80987e 737
mbed_official 610:813dcc80987e 738 /* Enable the USART Parity Error Interrupt */
mbed_official 610:813dcc80987e 739 __HAL_USART_ENABLE_IT(husart, USART_IT_PE);
mbed_official 610:813dcc80987e 740
mbed_official 610:813dcc80987e 741 /* Enable the USART Error Interrupt: (Frame error, noise error, overrun error) */
mbed_official 610:813dcc80987e 742 __HAL_USART_ENABLE_IT(husart, USART_IT_ERR);
mbed_official 610:813dcc80987e 743
mbed_official 610:813dcc80987e 744 /* Process Unlocked */
mbed_official 610:813dcc80987e 745 __HAL_UNLOCK(husart);
mbed_official 610:813dcc80987e 746
mbed_official 610:813dcc80987e 747 /* Enable the USART Transmit Data Register Empty Interrupt */
mbed_official 610:813dcc80987e 748 __HAL_USART_ENABLE_IT(husart, USART_IT_TXE);
mbed_official 610:813dcc80987e 749
mbed_official 610:813dcc80987e 750 return HAL_OK;
mbed_official 610:813dcc80987e 751 }
mbed_official 610:813dcc80987e 752 else
mbed_official 610:813dcc80987e 753 {
mbed_official 610:813dcc80987e 754 return HAL_BUSY;
mbed_official 610:813dcc80987e 755 }
mbed_official 610:813dcc80987e 756
mbed_official 610:813dcc80987e 757 }
mbed_official 610:813dcc80987e 758
mbed_official 610:813dcc80987e 759 /**
mbed_official 610:813dcc80987e 760 * @brief Send an amount of data in DMA mode.
mbed_official 610:813dcc80987e 761 * @param husart: USART handle.
mbed_official 610:813dcc80987e 762 * @param pTxData: pointer to data buffer.
mbed_official 610:813dcc80987e 763 * @param Size: amount of data to be sent.
mbed_official 610:813dcc80987e 764 * @retval HAL status
mbed_official 610:813dcc80987e 765 */
mbed_official 610:813dcc80987e 766 HAL_StatusTypeDef HAL_USART_Transmit_DMA(USART_HandleTypeDef *husart, uint8_t *pTxData, uint16_t Size)
mbed_official 610:813dcc80987e 767 {
mbed_official 610:813dcc80987e 768 uint32_t *tmp;
mbed_official 610:813dcc80987e 769
mbed_official 610:813dcc80987e 770 if(husart->State == HAL_USART_STATE_READY)
mbed_official 610:813dcc80987e 771 {
mbed_official 610:813dcc80987e 772 if((pTxData == NULL ) || (Size == 0))
mbed_official 610:813dcc80987e 773 {
mbed_official 610:813dcc80987e 774 return HAL_ERROR;
mbed_official 610:813dcc80987e 775 }
mbed_official 610:813dcc80987e 776 /* Process Locked */
mbed_official 610:813dcc80987e 777 __HAL_LOCK(husart);
mbed_official 610:813dcc80987e 778
mbed_official 610:813dcc80987e 779 husart->pTxBuffPtr = pTxData;
mbed_official 610:813dcc80987e 780 husart->TxXferSize = Size;
mbed_official 610:813dcc80987e 781 husart->TxXferCount = Size;
mbed_official 610:813dcc80987e 782
mbed_official 610:813dcc80987e 783 husart->ErrorCode = HAL_USART_ERROR_NONE;
mbed_official 610:813dcc80987e 784 husart->State = HAL_USART_STATE_BUSY_TX;
mbed_official 610:813dcc80987e 785
mbed_official 610:813dcc80987e 786 /* Set the USART DMA transfer complete callback */
mbed_official 610:813dcc80987e 787 husart->hdmatx->XferCpltCallback = USART_DMATransmitCplt;
mbed_official 610:813dcc80987e 788
mbed_official 610:813dcc80987e 789 /* Set the USART DMA Half transfer complete callback */
mbed_official 610:813dcc80987e 790 husart->hdmatx->XferHalfCpltCallback = USART_DMATxHalfCplt;
mbed_official 610:813dcc80987e 791
mbed_official 610:813dcc80987e 792 /* Set the DMA error callback */
mbed_official 610:813dcc80987e 793 husart->hdmatx->XferErrorCallback = USART_DMAError;
mbed_official 610:813dcc80987e 794
mbed_official 610:813dcc80987e 795 /* Enable the USART transmit DMA channel */
mbed_official 610:813dcc80987e 796 tmp = (uint32_t*)&pTxData;
mbed_official 610:813dcc80987e 797 HAL_DMA_Start_IT(husart->hdmatx, *(uint32_t*)tmp, (uint32_t)&husart->Instance->TDR, Size);
mbed_official 610:813dcc80987e 798
mbed_official 610:813dcc80987e 799 /* Clear the TC flag in the ICR register */
mbed_official 610:813dcc80987e 800 __HAL_USART_CLEAR_FLAG(husart, USART_CLEAR_TCF);
mbed_official 610:813dcc80987e 801
mbed_official 610:813dcc80987e 802 /* Enable the DMA transfer for transmit request by setting the DMAT bit
mbed_official 610:813dcc80987e 803 in the USART CR3 register */
mbed_official 610:813dcc80987e 804 husart->Instance->CR3 |= USART_CR3_DMAT;
mbed_official 610:813dcc80987e 805
mbed_official 610:813dcc80987e 806 /* Process Unlocked */
mbed_official 610:813dcc80987e 807 __HAL_UNLOCK(husart);
mbed_official 610:813dcc80987e 808
mbed_official 610:813dcc80987e 809 return HAL_OK;
mbed_official 610:813dcc80987e 810 }
mbed_official 610:813dcc80987e 811 else
mbed_official 610:813dcc80987e 812 {
mbed_official 610:813dcc80987e 813 return HAL_BUSY;
mbed_official 610:813dcc80987e 814 }
mbed_official 610:813dcc80987e 815 }
mbed_official 610:813dcc80987e 816
mbed_official 610:813dcc80987e 817 /**
mbed_official 610:813dcc80987e 818 * @brief Receive an amount of data in DMA mode.
mbed_official 610:813dcc80987e 819 * @param husart: USART handle.
mbed_official 610:813dcc80987e 820 * @param pRxData: pointer to data buffer.
mbed_official 610:813dcc80987e 821 * @param Size: amount of data to be received.
mbed_official 610:813dcc80987e 822 * @note When the USART parity is enabled (PCE = 1), the received data contain
mbed_official 610:813dcc80987e 823 * the parity bit (MSB position).
mbed_official 610:813dcc80987e 824 * @note The USART DMA transmit channel must be configured in order to generate the clock for the slave.
mbed_official 610:813dcc80987e 825 * @retval HAL status
mbed_official 610:813dcc80987e 826 */
mbed_official 610:813dcc80987e 827 HAL_StatusTypeDef HAL_USART_Receive_DMA(USART_HandleTypeDef *husart, uint8_t *pRxData, uint16_t Size)
mbed_official 610:813dcc80987e 828 {
mbed_official 610:813dcc80987e 829 uint32_t *tmp;
mbed_official 610:813dcc80987e 830
mbed_official 610:813dcc80987e 831 if(husart->State == HAL_USART_STATE_READY)
mbed_official 610:813dcc80987e 832 {
mbed_official 610:813dcc80987e 833 if((pRxData == NULL ) || (Size == 0))
mbed_official 610:813dcc80987e 834 {
mbed_official 610:813dcc80987e 835 return HAL_ERROR;
mbed_official 610:813dcc80987e 836 }
mbed_official 610:813dcc80987e 837
mbed_official 610:813dcc80987e 838 /* Process Locked */
mbed_official 610:813dcc80987e 839 __HAL_LOCK(husart);
mbed_official 610:813dcc80987e 840
mbed_official 610:813dcc80987e 841 husart->pRxBuffPtr = pRxData;
mbed_official 610:813dcc80987e 842 husart->RxXferSize = Size;
mbed_official 610:813dcc80987e 843 husart->pTxBuffPtr = pRxData;
mbed_official 610:813dcc80987e 844 husart->TxXferSize = Size;
mbed_official 610:813dcc80987e 845
mbed_official 610:813dcc80987e 846 husart->ErrorCode = HAL_USART_ERROR_NONE;
mbed_official 610:813dcc80987e 847 husart->State = HAL_USART_STATE_BUSY_RX;
mbed_official 610:813dcc80987e 848
mbed_official 610:813dcc80987e 849 /* Set the USART DMA Rx transfer complete callback */
mbed_official 610:813dcc80987e 850 husart->hdmarx->XferCpltCallback = USART_DMAReceiveCplt;
mbed_official 610:813dcc80987e 851
mbed_official 610:813dcc80987e 852 /* Set the USART DMA Half transfer complete callback */
mbed_official 610:813dcc80987e 853 husart->hdmarx->XferHalfCpltCallback = USART_DMARxHalfCplt;
mbed_official 610:813dcc80987e 854
mbed_official 610:813dcc80987e 855 /* Set the USART DMA Rx transfer error callback */
mbed_official 610:813dcc80987e 856 husart->hdmarx->XferErrorCallback = USART_DMAError;
mbed_official 610:813dcc80987e 857
mbed_official 610:813dcc80987e 858 /* Enable the USART receive DMA channel */
mbed_official 610:813dcc80987e 859 tmp = (uint32_t*)&pRxData;
mbed_official 610:813dcc80987e 860 HAL_DMA_Start_IT(husart->hdmarx, (uint32_t)&husart->Instance->RDR, *(uint32_t*)tmp, Size);
mbed_official 610:813dcc80987e 861
mbed_official 610:813dcc80987e 862 /* Enable the USART transmit DMA channel: the transmit channel is used in order
mbed_official 610:813dcc80987e 863 to generate in the non-blocking mode the clock to the slave device,
mbed_official 610:813dcc80987e 864 this mode isn't a simplex receive mode but a full-duplex receive mode */
mbed_official 610:813dcc80987e 865 tmp = (uint32_t*)&pRxData;
mbed_official 610:813dcc80987e 866 HAL_DMA_Start_IT(husart->hdmatx, *(uint32_t*)tmp, (uint32_t)&husart->Instance->TDR, Size);
mbed_official 610:813dcc80987e 867
mbed_official 610:813dcc80987e 868 /* Enable the DMA transfer for the receiver request by setting the DMAR bit
mbed_official 610:813dcc80987e 869 in the USART CR3 register */
mbed_official 610:813dcc80987e 870 husart->Instance->CR3 |= USART_CR3_DMAR;
mbed_official 610:813dcc80987e 871
mbed_official 610:813dcc80987e 872 /* Enable the DMA transfer for transmit request by setting the DMAT bit
mbed_official 610:813dcc80987e 873 in the USART CR3 register */
mbed_official 610:813dcc80987e 874 husart->Instance->CR3 |= USART_CR3_DMAT;
mbed_official 610:813dcc80987e 875
mbed_official 610:813dcc80987e 876 /* Process Unlocked */
mbed_official 610:813dcc80987e 877 __HAL_UNLOCK(husart);
mbed_official 610:813dcc80987e 878
mbed_official 610:813dcc80987e 879 return HAL_OK;
mbed_official 610:813dcc80987e 880 }
mbed_official 610:813dcc80987e 881 else
mbed_official 610:813dcc80987e 882 {
mbed_official 610:813dcc80987e 883 return HAL_BUSY;
mbed_official 610:813dcc80987e 884 }
mbed_official 610:813dcc80987e 885 }
mbed_official 610:813dcc80987e 886
mbed_official 610:813dcc80987e 887 /**
mbed_official 610:813dcc80987e 888 * @brief Full-Duplex Transmit Receive an amount of data in non-blocking mode.
mbed_official 610:813dcc80987e 889 * @param husart: USART handle.
mbed_official 610:813dcc80987e 890 * @param pTxData: pointer to TX data buffer.
mbed_official 610:813dcc80987e 891 * @param pRxData: pointer to RX data buffer.
mbed_official 610:813dcc80987e 892 * @param Size: amount of data to be received/sent.
mbed_official 610:813dcc80987e 893 * @note When the USART parity is enabled (PCE = 1) the data received contain the parity bit.
mbed_official 610:813dcc80987e 894 * @retval HAL status
mbed_official 610:813dcc80987e 895 */
mbed_official 610:813dcc80987e 896 HAL_StatusTypeDef HAL_USART_TransmitReceive_DMA(USART_HandleTypeDef *husart, uint8_t *pTxData, uint8_t *pRxData, uint16_t Size)
mbed_official 610:813dcc80987e 897 {
mbed_official 610:813dcc80987e 898 uint32_t *tmp;
mbed_official 610:813dcc80987e 899
mbed_official 610:813dcc80987e 900 if(husart->State == HAL_USART_STATE_READY)
mbed_official 610:813dcc80987e 901 {
mbed_official 610:813dcc80987e 902 if((pTxData == NULL) || (pRxData == NULL) || (Size == 0))
mbed_official 610:813dcc80987e 903 {
mbed_official 610:813dcc80987e 904 return HAL_ERROR;
mbed_official 610:813dcc80987e 905 }
mbed_official 610:813dcc80987e 906 /* Process Locked */
mbed_official 610:813dcc80987e 907 __HAL_LOCK(husart);
mbed_official 610:813dcc80987e 908
mbed_official 610:813dcc80987e 909 husart->pRxBuffPtr = pRxData;
mbed_official 610:813dcc80987e 910 husart->RxXferSize = Size;
mbed_official 610:813dcc80987e 911 husart->pTxBuffPtr = pTxData;
mbed_official 610:813dcc80987e 912 husart->TxXferSize = Size;
mbed_official 610:813dcc80987e 913
mbed_official 610:813dcc80987e 914 husart->ErrorCode = HAL_USART_ERROR_NONE;
mbed_official 610:813dcc80987e 915 husart->State = HAL_USART_STATE_BUSY_TX_RX;
mbed_official 610:813dcc80987e 916
mbed_official 610:813dcc80987e 917 /* Set the USART DMA Rx transfer complete callback */
mbed_official 610:813dcc80987e 918 husart->hdmarx->XferCpltCallback = USART_DMAReceiveCplt;
mbed_official 610:813dcc80987e 919
mbed_official 610:813dcc80987e 920 /* Set the USART DMA Half transfer complete callback */
mbed_official 610:813dcc80987e 921 husart->hdmarx->XferHalfCpltCallback = USART_DMARxHalfCplt;
mbed_official 610:813dcc80987e 922
mbed_official 610:813dcc80987e 923 /* Set the USART DMA Tx transfer complete callback */
mbed_official 610:813dcc80987e 924 husart->hdmatx->XferCpltCallback = USART_DMATransmitCplt;
mbed_official 610:813dcc80987e 925
mbed_official 610:813dcc80987e 926 /* Set the USART DMA Half transfer complete callback */
mbed_official 610:813dcc80987e 927 husart->hdmatx->XferHalfCpltCallback = USART_DMATxHalfCplt;
mbed_official 610:813dcc80987e 928
mbed_official 610:813dcc80987e 929 /* Set the USART DMA Tx transfer error callback */
mbed_official 610:813dcc80987e 930 husart->hdmatx->XferErrorCallback = USART_DMAError;
mbed_official 610:813dcc80987e 931
mbed_official 610:813dcc80987e 932 /* Set the USART DMA Rx transfer error callback */
mbed_official 610:813dcc80987e 933 husart->hdmarx->XferErrorCallback = USART_DMAError;
mbed_official 610:813dcc80987e 934
mbed_official 610:813dcc80987e 935 /* Enable the USART receive DMA channel */
mbed_official 610:813dcc80987e 936 tmp = (uint32_t*)&pRxData;
mbed_official 610:813dcc80987e 937 HAL_DMA_Start_IT(husart->hdmarx, (uint32_t)&husart->Instance->RDR, *(uint32_t*)tmp, Size);
mbed_official 610:813dcc80987e 938
mbed_official 610:813dcc80987e 939 /* Enable the USART transmit DMA channel */
mbed_official 610:813dcc80987e 940 tmp = (uint32_t*)&pTxData;
mbed_official 610:813dcc80987e 941 HAL_DMA_Start_IT(husart->hdmatx, *(uint32_t*)tmp, (uint32_t)&husart->Instance->TDR, Size);
mbed_official 610:813dcc80987e 942
mbed_official 610:813dcc80987e 943 /* Clear the TC flag in the ICR register */
mbed_official 610:813dcc80987e 944 __HAL_USART_CLEAR_FLAG(husart, USART_CLEAR_TCF);
mbed_official 610:813dcc80987e 945
mbed_official 610:813dcc80987e 946 /* Enable the DMA transfer for the receiver request by setting the DMAR bit
mbed_official 610:813dcc80987e 947 in the USART CR3 register */
mbed_official 610:813dcc80987e 948 husart->Instance->CR3 |= USART_CR3_DMAR;
mbed_official 610:813dcc80987e 949
mbed_official 610:813dcc80987e 950 /* Enable the DMA transfer for transmit request by setting the DMAT bit
mbed_official 610:813dcc80987e 951 in the USART CR3 register */
mbed_official 610:813dcc80987e 952 husart->Instance->CR3 |= USART_CR3_DMAT;
mbed_official 610:813dcc80987e 953
mbed_official 610:813dcc80987e 954 /* Process Unlocked */
mbed_official 610:813dcc80987e 955 __HAL_UNLOCK(husart);
mbed_official 610:813dcc80987e 956
mbed_official 610:813dcc80987e 957 return HAL_OK;
mbed_official 610:813dcc80987e 958 }
mbed_official 610:813dcc80987e 959 else
mbed_official 610:813dcc80987e 960 {
mbed_official 610:813dcc80987e 961 return HAL_BUSY;
mbed_official 610:813dcc80987e 962 }
mbed_official 610:813dcc80987e 963 }
mbed_official 610:813dcc80987e 964
mbed_official 610:813dcc80987e 965 /**
mbed_official 610:813dcc80987e 966 * @brief Pause the DMA Transfer.
mbed_official 610:813dcc80987e 967 * @param husart: USART handle.
mbed_official 610:813dcc80987e 968 * @retval HAL status
mbed_official 610:813dcc80987e 969 */
mbed_official 610:813dcc80987e 970 HAL_StatusTypeDef HAL_USART_DMAPause(USART_HandleTypeDef *husart)
mbed_official 610:813dcc80987e 971 {
mbed_official 610:813dcc80987e 972 /* Process Locked */
mbed_official 610:813dcc80987e 973 __HAL_LOCK(husart);
mbed_official 610:813dcc80987e 974
mbed_official 610:813dcc80987e 975 if(husart->State == HAL_USART_STATE_BUSY_TX)
mbed_official 610:813dcc80987e 976 {
mbed_official 610:813dcc80987e 977 /* Disable the USART DMA Tx request */
mbed_official 610:813dcc80987e 978 husart->Instance->CR3 &= (uint32_t)(~USART_CR3_DMAT);
mbed_official 610:813dcc80987e 979 }
mbed_official 610:813dcc80987e 980 else if(husart->State == HAL_USART_STATE_BUSY_RX)
mbed_official 610:813dcc80987e 981 {
mbed_official 610:813dcc80987e 982 /* Disable the USART DMA Rx request */
mbed_official 610:813dcc80987e 983 husart->Instance->CR3 &= (uint32_t)(~USART_CR3_DMAR);
mbed_official 610:813dcc80987e 984 }
mbed_official 610:813dcc80987e 985 else if(husart->State == HAL_USART_STATE_BUSY_TX_RX)
mbed_official 610:813dcc80987e 986 {
mbed_official 610:813dcc80987e 987 /* Disable the USART DMA Tx request */
mbed_official 610:813dcc80987e 988 husart->Instance->CR3 &= (uint32_t)(~USART_CR3_DMAT);
mbed_official 610:813dcc80987e 989 /* Disable the USART DMA Rx request */
mbed_official 610:813dcc80987e 990 husart->Instance->CR3 &= (uint32_t)(~USART_CR3_DMAR);
mbed_official 610:813dcc80987e 991 }
mbed_official 610:813dcc80987e 992
mbed_official 610:813dcc80987e 993 /* Process Unlocked */
mbed_official 610:813dcc80987e 994 __HAL_UNLOCK(husart);
mbed_official 610:813dcc80987e 995
mbed_official 610:813dcc80987e 996 return HAL_OK;
mbed_official 610:813dcc80987e 997 }
mbed_official 610:813dcc80987e 998
mbed_official 610:813dcc80987e 999 /**
mbed_official 610:813dcc80987e 1000 * @brief Resume the DMA Transfer.
mbed_official 610:813dcc80987e 1001 * @param husart: USART handle.
mbed_official 610:813dcc80987e 1002 * @retval HAL status
mbed_official 610:813dcc80987e 1003 */
mbed_official 610:813dcc80987e 1004 HAL_StatusTypeDef HAL_USART_DMAResume(USART_HandleTypeDef *husart)
mbed_official 610:813dcc80987e 1005 {
mbed_official 610:813dcc80987e 1006 /* Process Locked */
mbed_official 610:813dcc80987e 1007 __HAL_LOCK(husart);
mbed_official 610:813dcc80987e 1008
mbed_official 610:813dcc80987e 1009 if(husart->State == HAL_USART_STATE_BUSY_TX)
mbed_official 610:813dcc80987e 1010 {
mbed_official 610:813dcc80987e 1011 /* Enable the USART DMA Tx request */
mbed_official 610:813dcc80987e 1012 husart->Instance->CR3 |= USART_CR3_DMAT;
mbed_official 610:813dcc80987e 1013 }
mbed_official 610:813dcc80987e 1014 else if(husart->State == HAL_USART_STATE_BUSY_RX)
mbed_official 610:813dcc80987e 1015 {
mbed_official 610:813dcc80987e 1016 /* Clear the Overrun flag before resuming the Rx transfer*/
mbed_official 610:813dcc80987e 1017 __HAL_USART_CLEAR_FLAG(husart, USART_CLEAR_OREF);
mbed_official 610:813dcc80987e 1018
mbed_official 610:813dcc80987e 1019 /* Enable the USART DMA Rx request */
mbed_official 610:813dcc80987e 1020 husart->Instance->CR3 |= USART_CR3_DMAR;
mbed_official 610:813dcc80987e 1021 }
mbed_official 610:813dcc80987e 1022 else if(husart->State == HAL_USART_STATE_BUSY_TX_RX)
mbed_official 610:813dcc80987e 1023 {
mbed_official 610:813dcc80987e 1024 /* Clear the Overrun flag before resuming the Rx transfer*/
mbed_official 610:813dcc80987e 1025 __HAL_USART_CLEAR_FLAG(husart, USART_CLEAR_OREF);
mbed_official 610:813dcc80987e 1026
mbed_official 610:813dcc80987e 1027 /* Enable the USART DMA Rx request before the DMA Tx request */
mbed_official 610:813dcc80987e 1028 husart->Instance->CR3 |= USART_CR3_DMAR;
mbed_official 610:813dcc80987e 1029
mbed_official 610:813dcc80987e 1030 /* Enable the USART DMA Tx request */
mbed_official 610:813dcc80987e 1031 husart->Instance->CR3 |= USART_CR3_DMAT;
mbed_official 610:813dcc80987e 1032 }
mbed_official 610:813dcc80987e 1033
mbed_official 610:813dcc80987e 1034 /* Process Unlocked */
mbed_official 610:813dcc80987e 1035 __HAL_UNLOCK(husart);
mbed_official 610:813dcc80987e 1036
mbed_official 610:813dcc80987e 1037 return HAL_OK;
mbed_official 610:813dcc80987e 1038 }
mbed_official 610:813dcc80987e 1039
mbed_official 610:813dcc80987e 1040 /**
mbed_official 610:813dcc80987e 1041 * @brief Stop the DMA Transfer.
mbed_official 610:813dcc80987e 1042 * @param husart: USART handle.
mbed_official 610:813dcc80987e 1043 * @retval HAL status
mbed_official 610:813dcc80987e 1044 */
mbed_official 610:813dcc80987e 1045 HAL_StatusTypeDef HAL_USART_DMAStop(USART_HandleTypeDef *husart)
mbed_official 610:813dcc80987e 1046 {
mbed_official 610:813dcc80987e 1047 /* The Lock is not implemented on this API to allow the user application
mbed_official 610:813dcc80987e 1048 to call the HAL USART API under callbacks HAL_USART_TxCpltCallback() / HAL_USART_RxCpltCallback() /
mbed_official 610:813dcc80987e 1049 HAL_USART_TxHalfCpltCallback / HAL_USART_RxHalfCpltCallback:
mbed_official 610:813dcc80987e 1050 indeed, when HAL_DMA_Abort() API is called, the DMA TX/RX Transfer or Half Transfer complete
mbed_official 610:813dcc80987e 1051 interrupt is generated if the DMA transfer interruption occurs at the middle or at the end of
mbed_official 610:813dcc80987e 1052 the stream and the corresponding call back is executed. */
mbed_official 610:813dcc80987e 1053
mbed_official 610:813dcc80987e 1054 /* Disable the USART Tx/Rx DMA requests */
mbed_official 610:813dcc80987e 1055 husart->Instance->CR3 &= ~USART_CR3_DMAT;
mbed_official 610:813dcc80987e 1056 husart->Instance->CR3 &= ~USART_CR3_DMAR;
mbed_official 610:813dcc80987e 1057
mbed_official 610:813dcc80987e 1058 /* Abort the USART DMA tx channel */
mbed_official 610:813dcc80987e 1059 if(husart->hdmatx != NULL)
mbed_official 610:813dcc80987e 1060 {
mbed_official 610:813dcc80987e 1061 HAL_DMA_Abort(husart->hdmatx);
mbed_official 610:813dcc80987e 1062 }
mbed_official 610:813dcc80987e 1063 /* Abort the USART DMA rx channel */
mbed_official 610:813dcc80987e 1064 if(husart->hdmarx != NULL)
mbed_official 610:813dcc80987e 1065 {
mbed_official 610:813dcc80987e 1066 HAL_DMA_Abort(husart->hdmarx);
mbed_official 610:813dcc80987e 1067 }
mbed_official 610:813dcc80987e 1068
mbed_official 610:813dcc80987e 1069 husart->State = HAL_USART_STATE_READY;
mbed_official 610:813dcc80987e 1070
mbed_official 610:813dcc80987e 1071 return HAL_OK;
mbed_official 610:813dcc80987e 1072 }
mbed_official 610:813dcc80987e 1073
mbed_official 610:813dcc80987e 1074 /**
mbed_official 610:813dcc80987e 1075 * @brief Handle USART interrupt request.
mbed_official 610:813dcc80987e 1076 * @param husart: USART handle.
mbed_official 610:813dcc80987e 1077 * @retval None
mbed_official 610:813dcc80987e 1078 */
mbed_official 610:813dcc80987e 1079 void HAL_USART_IRQHandler(USART_HandleTypeDef *husart)
mbed_official 610:813dcc80987e 1080 {
mbed_official 610:813dcc80987e 1081
mbed_official 610:813dcc80987e 1082 /* USART parity error interrupt occurred ------------------------------------*/
mbed_official 610:813dcc80987e 1083 if((__HAL_USART_GET_IT(husart, USART_IT_PE) != RESET) && (__HAL_USART_GET_IT_SOURCE(husart, USART_IT_PE) != RESET))
mbed_official 610:813dcc80987e 1084 {
mbed_official 610:813dcc80987e 1085 __HAL_USART_CLEAR_IT(husart, USART_CLEAR_PEF);
mbed_official 610:813dcc80987e 1086 husart->ErrorCode |= HAL_USART_ERROR_PE;
mbed_official 610:813dcc80987e 1087 /* Set the USART state ready to be able to start again the process */
mbed_official 610:813dcc80987e 1088 husart->State = HAL_USART_STATE_READY;
mbed_official 610:813dcc80987e 1089 }
mbed_official 610:813dcc80987e 1090
mbed_official 610:813dcc80987e 1091 /* USART frame error interrupt occurred -------------------------------------*/
mbed_official 610:813dcc80987e 1092 if((__HAL_USART_GET_IT(husart, USART_IT_FE) != RESET) && (__HAL_USART_GET_IT_SOURCE(husart, USART_IT_ERR) != RESET))
mbed_official 610:813dcc80987e 1093 {
mbed_official 610:813dcc80987e 1094 __HAL_USART_CLEAR_IT(husart, USART_CLEAR_FEF);
mbed_official 610:813dcc80987e 1095 husart->ErrorCode |= HAL_USART_ERROR_FE;
mbed_official 610:813dcc80987e 1096 /* Set the USART state ready to be able to start again the process */
mbed_official 610:813dcc80987e 1097 husart->State = HAL_USART_STATE_READY;
mbed_official 610:813dcc80987e 1098 }
mbed_official 610:813dcc80987e 1099
mbed_official 610:813dcc80987e 1100 /* USART noise error interrupt occurred -------------------------------------*/
mbed_official 610:813dcc80987e 1101 if((__HAL_USART_GET_IT(husart, USART_IT_NE) != RESET) && (__HAL_USART_GET_IT_SOURCE(husart, USART_IT_ERR) != RESET))
mbed_official 610:813dcc80987e 1102 {
mbed_official 610:813dcc80987e 1103 __HAL_USART_CLEAR_IT(husart, USART_CLEAR_NEF);
mbed_official 610:813dcc80987e 1104 husart->ErrorCode |= HAL_USART_ERROR_NE;
mbed_official 610:813dcc80987e 1105 /* Set the USART state ready to be able to start again the process */
mbed_official 610:813dcc80987e 1106 husart->State = HAL_USART_STATE_READY;
mbed_official 610:813dcc80987e 1107 }
mbed_official 610:813dcc80987e 1108
mbed_official 610:813dcc80987e 1109 /* USART Over-Run interrupt occurred ----------------------------------------*/
mbed_official 610:813dcc80987e 1110 if((__HAL_USART_GET_IT(husart, USART_IT_ORE) != RESET) && (__HAL_USART_GET_IT_SOURCE(husart, USART_IT_ERR) != RESET))
mbed_official 610:813dcc80987e 1111 {
mbed_official 610:813dcc80987e 1112 __HAL_USART_CLEAR_IT(husart, USART_CLEAR_OREF);
mbed_official 610:813dcc80987e 1113 husart->ErrorCode |= HAL_USART_ERROR_ORE;
mbed_official 610:813dcc80987e 1114 /* Set the USART state ready to be able to start again the process */
mbed_official 610:813dcc80987e 1115 husart->State = HAL_USART_STATE_READY;
mbed_official 610:813dcc80987e 1116 }
mbed_official 610:813dcc80987e 1117
mbed_official 610:813dcc80987e 1118 /* Call USART Error Call back function if need be --------------------------*/
mbed_official 610:813dcc80987e 1119 if(husart->ErrorCode != HAL_USART_ERROR_NONE)
mbed_official 610:813dcc80987e 1120 {
mbed_official 610:813dcc80987e 1121 HAL_USART_ErrorCallback(husart);
mbed_official 610:813dcc80987e 1122 }
mbed_official 610:813dcc80987e 1123
mbed_official 610:813dcc80987e 1124 /* USART in mode Receiver --------------------------------------------------*/
mbed_official 610:813dcc80987e 1125 if((__HAL_USART_GET_IT(husart, USART_IT_RXNE) != RESET) && (__HAL_USART_GET_IT_SOURCE(husart, USART_IT_RXNE) != RESET))
mbed_official 610:813dcc80987e 1126 {
mbed_official 610:813dcc80987e 1127 if(husart->State == HAL_USART_STATE_BUSY_RX)
mbed_official 610:813dcc80987e 1128 {
mbed_official 610:813dcc80987e 1129 USART_Receive_IT(husart);
mbed_official 610:813dcc80987e 1130 }
mbed_official 610:813dcc80987e 1131 else
mbed_official 610:813dcc80987e 1132 {
mbed_official 610:813dcc80987e 1133 USART_TransmitReceive_IT(husart);
mbed_official 610:813dcc80987e 1134 }
mbed_official 610:813dcc80987e 1135 }
mbed_official 610:813dcc80987e 1136
mbed_official 610:813dcc80987e 1137 /* USART in mode Transmitter -----------------------------------------------*/
mbed_official 610:813dcc80987e 1138 if((__HAL_USART_GET_IT(husart, USART_IT_TXE) != RESET) &&(__HAL_USART_GET_IT_SOURCE(husart, USART_IT_TXE) != RESET))
mbed_official 610:813dcc80987e 1139 {
mbed_official 610:813dcc80987e 1140 if(husart->State == HAL_USART_STATE_BUSY_TX)
mbed_official 610:813dcc80987e 1141 {
mbed_official 610:813dcc80987e 1142 USART_Transmit_IT(husart);
mbed_official 610:813dcc80987e 1143 }
mbed_official 610:813dcc80987e 1144 else
mbed_official 610:813dcc80987e 1145 {
mbed_official 610:813dcc80987e 1146 USART_TransmitReceive_IT(husart);
mbed_official 610:813dcc80987e 1147 }
mbed_official 610:813dcc80987e 1148 }
mbed_official 610:813dcc80987e 1149
mbed_official 610:813dcc80987e 1150 /* USART in mode Transmitter (transmission end) -----------------------------*/
mbed_official 610:813dcc80987e 1151 if((__HAL_USART_GET_IT(husart, USART_IT_TC) != RESET) &&(__HAL_USART_GET_IT_SOURCE(husart, USART_IT_TC) != RESET))
mbed_official 610:813dcc80987e 1152 {
mbed_official 610:813dcc80987e 1153 USART_EndTransmit_IT(husart);
mbed_official 610:813dcc80987e 1154 }
mbed_official 610:813dcc80987e 1155
mbed_official 610:813dcc80987e 1156 }
mbed_official 610:813dcc80987e 1157
mbed_official 610:813dcc80987e 1158 /**
mbed_official 610:813dcc80987e 1159 * @brief Tx Transfer completed callback.
mbed_official 610:813dcc80987e 1160 * @param husart: USART handle.
mbed_official 610:813dcc80987e 1161 * @retval None
mbed_official 610:813dcc80987e 1162 */
mbed_official 610:813dcc80987e 1163 __weak void HAL_USART_TxCpltCallback(USART_HandleTypeDef *husart)
mbed_official 610:813dcc80987e 1164 {
mbed_official 610:813dcc80987e 1165 /* NOTE : This function should not be modified, when the callback is needed,
mbed_official 610:813dcc80987e 1166 the HAL_USART_TxCpltCallback can be implemented in the user file.
mbed_official 610:813dcc80987e 1167 */
mbed_official 610:813dcc80987e 1168 }
mbed_official 610:813dcc80987e 1169
mbed_official 610:813dcc80987e 1170 /**
mbed_official 610:813dcc80987e 1171 * @brief Tx Half Transfer completed callback.
mbed_official 610:813dcc80987e 1172 * @param husart: USART handle.
mbed_official 610:813dcc80987e 1173 * @retval None
mbed_official 610:813dcc80987e 1174 */
mbed_official 610:813dcc80987e 1175 __weak void HAL_USART_TxHalfCpltCallback(USART_HandleTypeDef *husart)
mbed_official 610:813dcc80987e 1176 {
mbed_official 610:813dcc80987e 1177 /* NOTE: This function should not be modified, when the callback is needed,
mbed_official 610:813dcc80987e 1178 the HAL_USART_TxHalfCpltCallback can be implemented in the user file.
mbed_official 610:813dcc80987e 1179 */
mbed_official 610:813dcc80987e 1180 }
mbed_official 610:813dcc80987e 1181
mbed_official 610:813dcc80987e 1182 /**
mbed_official 610:813dcc80987e 1183 * @brief Rx Transfer completed callback.
mbed_official 610:813dcc80987e 1184 * @param husart: USART handle.
mbed_official 610:813dcc80987e 1185 * @retval None
mbed_official 610:813dcc80987e 1186 */
mbed_official 610:813dcc80987e 1187 __weak void HAL_USART_RxCpltCallback(USART_HandleTypeDef *husart)
mbed_official 610:813dcc80987e 1188 {
mbed_official 610:813dcc80987e 1189 /* NOTE: This function should not be modified, when the callback is needed,
mbed_official 610:813dcc80987e 1190 the HAL_USART_RxCpltCallback can be implemented in the user file.
mbed_official 610:813dcc80987e 1191 */
mbed_official 610:813dcc80987e 1192 }
mbed_official 610:813dcc80987e 1193
mbed_official 610:813dcc80987e 1194 /**
mbed_official 610:813dcc80987e 1195 * @brief Rx Half Transfer completed callback.
mbed_official 610:813dcc80987e 1196 * @param husart: USART handle.
mbed_official 610:813dcc80987e 1197 * @retval None
mbed_official 610:813dcc80987e 1198 */
mbed_official 610:813dcc80987e 1199 __weak void HAL_USART_RxHalfCpltCallback(USART_HandleTypeDef *husart)
mbed_official 610:813dcc80987e 1200 {
mbed_official 610:813dcc80987e 1201 /* NOTE : This function should not be modified, when the callback is needed,
mbed_official 610:813dcc80987e 1202 the HAL_USART_RxHalfCpltCallback can be implemented in the user file
mbed_official 610:813dcc80987e 1203 */
mbed_official 610:813dcc80987e 1204 }
mbed_official 610:813dcc80987e 1205
mbed_official 610:813dcc80987e 1206 /**
mbed_official 610:813dcc80987e 1207 * @brief Tx/Rx Transfers completed callback for the non-blocking process.
mbed_official 610:813dcc80987e 1208 * @param husart: USART handle.
mbed_official 610:813dcc80987e 1209 * @retval None
mbed_official 610:813dcc80987e 1210 */
mbed_official 610:813dcc80987e 1211 __weak void HAL_USART_TxRxCpltCallback(USART_HandleTypeDef *husart)
mbed_official 610:813dcc80987e 1212 {
mbed_official 610:813dcc80987e 1213 /* NOTE : This function should not be modified, when the callback is needed,
mbed_official 610:813dcc80987e 1214 the HAL_USART_TxRxCpltCallback can be implemented in the user file
mbed_official 610:813dcc80987e 1215 */
mbed_official 610:813dcc80987e 1216 }
mbed_official 610:813dcc80987e 1217
mbed_official 610:813dcc80987e 1218 /**
mbed_official 610:813dcc80987e 1219 * @brief USART error callback.
mbed_official 610:813dcc80987e 1220 * @param husart: USART handle.
mbed_official 610:813dcc80987e 1221 * @retval None
mbed_official 610:813dcc80987e 1222 */
mbed_official 610:813dcc80987e 1223 __weak void HAL_USART_ErrorCallback(USART_HandleTypeDef *husart)
mbed_official 610:813dcc80987e 1224 {
mbed_official 610:813dcc80987e 1225 /* NOTE : This function should not be modified, when the callback is needed,
mbed_official 610:813dcc80987e 1226 the HAL_USART_ErrorCallback can be implemented in the user file.
mbed_official 610:813dcc80987e 1227 */
mbed_official 610:813dcc80987e 1228 }
mbed_official 610:813dcc80987e 1229
mbed_official 610:813dcc80987e 1230 /**
mbed_official 610:813dcc80987e 1231 * @}
mbed_official 610:813dcc80987e 1232 */
mbed_official 610:813dcc80987e 1233
mbed_official 610:813dcc80987e 1234 /** @defgroup USART_Exported_Functions_Group4 Peripheral State and Error functions
mbed_official 610:813dcc80987e 1235 * @brief USART Peripheral State and Error functions
mbed_official 610:813dcc80987e 1236 *
mbed_official 610:813dcc80987e 1237 @verbatim
mbed_official 610:813dcc80987e 1238 ==============================================================================
mbed_official 610:813dcc80987e 1239 ##### Peripheral State and Error functions #####
mbed_official 610:813dcc80987e 1240 ==============================================================================
mbed_official 610:813dcc80987e 1241 [..]
mbed_official 610:813dcc80987e 1242 This subsection provides functions allowing to :
mbed_official 610:813dcc80987e 1243 (+) Return the USART handle state
mbed_official 610:813dcc80987e 1244 (+) Return the USART handle error code
mbed_official 610:813dcc80987e 1245
mbed_official 610:813dcc80987e 1246 @endverbatim
mbed_official 610:813dcc80987e 1247 * @{
mbed_official 610:813dcc80987e 1248 */
mbed_official 610:813dcc80987e 1249
mbed_official 610:813dcc80987e 1250
mbed_official 610:813dcc80987e 1251 /**
mbed_official 610:813dcc80987e 1252 * @brief Return the USART handle state.
mbed_official 610:813dcc80987e 1253 * @param husart : pointer to a USART_HandleTypeDef structure that contains
mbed_official 610:813dcc80987e 1254 * the configuration information for the specified USART.
mbed_official 610:813dcc80987e 1255 * @retval USART handle state
mbed_official 610:813dcc80987e 1256 */
mbed_official 610:813dcc80987e 1257 HAL_USART_StateTypeDef HAL_USART_GetState(USART_HandleTypeDef *husart)
mbed_official 610:813dcc80987e 1258 {
mbed_official 610:813dcc80987e 1259 return husart->State;
mbed_official 610:813dcc80987e 1260 }
mbed_official 610:813dcc80987e 1261
mbed_official 610:813dcc80987e 1262 /**
mbed_official 610:813dcc80987e 1263 * @brief Return the USART error code.
mbed_official 610:813dcc80987e 1264 * @param husart : pointer to a USART_HandleTypeDef structure that contains
mbed_official 610:813dcc80987e 1265 * the configuration information for the specified USART.
mbed_official 610:813dcc80987e 1266 * @retval USART handle Error Code
mbed_official 610:813dcc80987e 1267 */
mbed_official 610:813dcc80987e 1268 uint32_t HAL_USART_GetError(USART_HandleTypeDef *husart)
mbed_official 610:813dcc80987e 1269 {
mbed_official 610:813dcc80987e 1270 return husart->ErrorCode;
mbed_official 610:813dcc80987e 1271 }
mbed_official 610:813dcc80987e 1272
mbed_official 610:813dcc80987e 1273 /**
mbed_official 610:813dcc80987e 1274 * @}
mbed_official 610:813dcc80987e 1275 */
mbed_official 610:813dcc80987e 1276
mbed_official 610:813dcc80987e 1277 /**
mbed_official 610:813dcc80987e 1278 * @}
mbed_official 610:813dcc80987e 1279 */
mbed_official 610:813dcc80987e 1280
mbed_official 610:813dcc80987e 1281 /** @defgroup USART_Private_Functions USART Private Functions
mbed_official 610:813dcc80987e 1282 * @{
mbed_official 610:813dcc80987e 1283 */
mbed_official 610:813dcc80987e 1284
mbed_official 610:813dcc80987e 1285 /**
mbed_official 610:813dcc80987e 1286 * @brief DMA USART transmit process complete callback.
mbed_official 610:813dcc80987e 1287 * @param hdma: DMA handle.
mbed_official 610:813dcc80987e 1288 * @retval None
mbed_official 610:813dcc80987e 1289 */
mbed_official 610:813dcc80987e 1290 static void USART_DMATransmitCplt(DMA_HandleTypeDef *hdma)
mbed_official 610:813dcc80987e 1291 {
mbed_official 610:813dcc80987e 1292 USART_HandleTypeDef* husart = ( USART_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
mbed_official 610:813dcc80987e 1293
mbed_official 610:813dcc80987e 1294 /* DMA Normal mode */
mbed_official 610:813dcc80987e 1295 if ( HAL_IS_BIT_CLR(hdma->Instance->CCR, DMA_CCR_CIRC) )
mbed_official 610:813dcc80987e 1296 {
mbed_official 610:813dcc80987e 1297 husart->TxXferCount = 0;
mbed_official 610:813dcc80987e 1298
mbed_official 610:813dcc80987e 1299 if(husart->State == HAL_USART_STATE_BUSY_TX)
mbed_official 610:813dcc80987e 1300 {
mbed_official 610:813dcc80987e 1301 /* Disable the DMA transfer for transmit request by resetting the DMAT bit
mbed_official 610:813dcc80987e 1302 in the USART CR3 register */
mbed_official 610:813dcc80987e 1303 husart->Instance->CR3 &= ~(USART_CR3_DMAT);
mbed_official 610:813dcc80987e 1304
mbed_official 610:813dcc80987e 1305 /* Enable the USART Transmit Complete Interrupt */
mbed_official 610:813dcc80987e 1306 __HAL_USART_ENABLE_IT(husart, USART_IT_TC);
mbed_official 610:813dcc80987e 1307 }
mbed_official 610:813dcc80987e 1308 }
mbed_official 610:813dcc80987e 1309 /* DMA Circular mode */
mbed_official 610:813dcc80987e 1310 else
mbed_official 610:813dcc80987e 1311 {
mbed_official 610:813dcc80987e 1312 if(husart->State == HAL_USART_STATE_BUSY_TX)
mbed_official 610:813dcc80987e 1313 {
mbed_official 610:813dcc80987e 1314 HAL_USART_TxCpltCallback(husart);
mbed_official 610:813dcc80987e 1315 }
mbed_official 610:813dcc80987e 1316 }
mbed_official 610:813dcc80987e 1317 }
mbed_official 610:813dcc80987e 1318
mbed_official 610:813dcc80987e 1319
mbed_official 610:813dcc80987e 1320 /**
mbed_official 610:813dcc80987e 1321 * @brief DMA USART transmit process half complete callback.
mbed_official 610:813dcc80987e 1322 * @param hdma : DMA handle.
mbed_official 610:813dcc80987e 1323 * @retval None
mbed_official 610:813dcc80987e 1324 */
mbed_official 610:813dcc80987e 1325 static void USART_DMATxHalfCplt(DMA_HandleTypeDef *hdma)
mbed_official 610:813dcc80987e 1326 {
mbed_official 610:813dcc80987e 1327 USART_HandleTypeDef* husart = (USART_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
mbed_official 610:813dcc80987e 1328
mbed_official 610:813dcc80987e 1329 HAL_USART_TxHalfCpltCallback(husart);
mbed_official 610:813dcc80987e 1330 }
mbed_official 610:813dcc80987e 1331
mbed_official 610:813dcc80987e 1332 /**
mbed_official 610:813dcc80987e 1333 * @brief DMA USART receive process complete callback.
mbed_official 610:813dcc80987e 1334 * @param hdma: DMA handle.
mbed_official 610:813dcc80987e 1335 * @retval None
mbed_official 610:813dcc80987e 1336 */
mbed_official 610:813dcc80987e 1337 static void USART_DMAReceiveCplt(DMA_HandleTypeDef *hdma)
mbed_official 610:813dcc80987e 1338 {
mbed_official 610:813dcc80987e 1339 USART_HandleTypeDef* husart = ( USART_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
mbed_official 610:813dcc80987e 1340
mbed_official 610:813dcc80987e 1341 /* DMA Normal mode */
mbed_official 610:813dcc80987e 1342 if ( HAL_IS_BIT_CLR(hdma->Instance->CCR, DMA_CCR_CIRC) )
mbed_official 610:813dcc80987e 1343 {
mbed_official 610:813dcc80987e 1344 husart->RxXferCount = 0;
mbed_official 610:813dcc80987e 1345
mbed_official 610:813dcc80987e 1346 /* Disable the DMA RX transfer for the receiver request by resetting the DMAR bit
mbed_official 610:813dcc80987e 1347 in USART CR3 register */
mbed_official 610:813dcc80987e 1348 husart->Instance->CR3 &= (uint32_t)~((uint32_t)USART_CR3_DMAR);
mbed_official 610:813dcc80987e 1349 /* similarly, disable the DMA TX transfer that was started to provide the
mbed_official 610:813dcc80987e 1350 clock to the slave device */
mbed_official 610:813dcc80987e 1351 husart->Instance->CR3 &= (uint32_t)~((uint32_t)USART_CR3_DMAT);
mbed_official 610:813dcc80987e 1352
mbed_official 610:813dcc80987e 1353 if(husart->State == HAL_USART_STATE_BUSY_RX)
mbed_official 610:813dcc80987e 1354 {
mbed_official 610:813dcc80987e 1355 HAL_USART_RxCpltCallback(husart);
mbed_official 610:813dcc80987e 1356 }
mbed_official 610:813dcc80987e 1357 /* The USART state is HAL_USART_STATE_BUSY_TX_RX */
mbed_official 610:813dcc80987e 1358 else
mbed_official 610:813dcc80987e 1359 {
mbed_official 610:813dcc80987e 1360 HAL_USART_TxRxCpltCallback(husart);
mbed_official 610:813dcc80987e 1361 }
mbed_official 610:813dcc80987e 1362 husart->State= HAL_USART_STATE_READY;
mbed_official 610:813dcc80987e 1363 }
mbed_official 610:813dcc80987e 1364 /* DMA circular mode */
mbed_official 610:813dcc80987e 1365 else
mbed_official 610:813dcc80987e 1366 {
mbed_official 610:813dcc80987e 1367 if(husart->State == HAL_USART_STATE_BUSY_RX)
mbed_official 610:813dcc80987e 1368 {
mbed_official 610:813dcc80987e 1369 HAL_USART_RxCpltCallback(husart);
mbed_official 610:813dcc80987e 1370 }
mbed_official 610:813dcc80987e 1371 /* The USART state is HAL_USART_STATE_BUSY_TX_RX */
mbed_official 610:813dcc80987e 1372 else
mbed_official 610:813dcc80987e 1373 {
mbed_official 610:813dcc80987e 1374 HAL_USART_TxRxCpltCallback(husart);
mbed_official 610:813dcc80987e 1375 }
mbed_official 610:813dcc80987e 1376 }
mbed_official 610:813dcc80987e 1377
mbed_official 610:813dcc80987e 1378 }
mbed_official 610:813dcc80987e 1379
mbed_official 610:813dcc80987e 1380 /**
mbed_official 610:813dcc80987e 1381 * @brief DMA USART receive process half complete callback.
mbed_official 610:813dcc80987e 1382 * @param hdma : DMA handle.
mbed_official 610:813dcc80987e 1383 * @retval None
mbed_official 610:813dcc80987e 1384 */
mbed_official 610:813dcc80987e 1385 static void USART_DMARxHalfCplt(DMA_HandleTypeDef *hdma)
mbed_official 610:813dcc80987e 1386 {
mbed_official 610:813dcc80987e 1387 USART_HandleTypeDef* husart = (USART_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
mbed_official 610:813dcc80987e 1388
mbed_official 610:813dcc80987e 1389 HAL_USART_RxHalfCpltCallback(husart);
mbed_official 610:813dcc80987e 1390 }
mbed_official 610:813dcc80987e 1391
mbed_official 610:813dcc80987e 1392 /**
mbed_official 610:813dcc80987e 1393 * @brief DMA USART communication error callback.
mbed_official 610:813dcc80987e 1394 * @param hdma: DMA handle.
mbed_official 610:813dcc80987e 1395 * @retval None
mbed_official 610:813dcc80987e 1396 */
mbed_official 610:813dcc80987e 1397 static void USART_DMAError(DMA_HandleTypeDef *hdma)
mbed_official 610:813dcc80987e 1398 {
mbed_official 610:813dcc80987e 1399 USART_HandleTypeDef* husart = ( USART_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
mbed_official 610:813dcc80987e 1400
mbed_official 610:813dcc80987e 1401 husart->RxXferCount = 0;
mbed_official 610:813dcc80987e 1402 husart->TxXferCount = 0;
mbed_official 610:813dcc80987e 1403 husart->ErrorCode |= HAL_USART_ERROR_DMA;
mbed_official 610:813dcc80987e 1404 husart->State= HAL_USART_STATE_READY;
mbed_official 610:813dcc80987e 1405
mbed_official 610:813dcc80987e 1406 HAL_USART_ErrorCallback(husart);
mbed_official 610:813dcc80987e 1407 }
mbed_official 610:813dcc80987e 1408
mbed_official 610:813dcc80987e 1409 /**
mbed_official 610:813dcc80987e 1410 * @brief Handle USART Communication Timeout.
mbed_official 610:813dcc80987e 1411 * @param husart: USART handle.
mbed_official 610:813dcc80987e 1412 * @param Flag: specifies the USART flag to check.
mbed_official 610:813dcc80987e 1413 * @param Status: the Flag status (SET or RESET).
mbed_official 610:813dcc80987e 1414 * @param Timeout: timeout duration.
mbed_official 610:813dcc80987e 1415 * @retval HAL status
mbed_official 610:813dcc80987e 1416 */
mbed_official 610:813dcc80987e 1417 static HAL_StatusTypeDef USART_WaitOnFlagUntilTimeout(USART_HandleTypeDef *husart, uint32_t Flag, FlagStatus Status, uint32_t Timeout)
mbed_official 610:813dcc80987e 1418 {
mbed_official 610:813dcc80987e 1419 uint32_t tickstart = HAL_GetTick();
mbed_official 610:813dcc80987e 1420
mbed_official 610:813dcc80987e 1421 /* Wait until flag is set */
mbed_official 610:813dcc80987e 1422 if(Status == RESET)
mbed_official 610:813dcc80987e 1423 {
mbed_official 610:813dcc80987e 1424 while(__HAL_USART_GET_FLAG(husart, Flag) == RESET)
mbed_official 610:813dcc80987e 1425 {
mbed_official 610:813dcc80987e 1426 /* Check for the Timeout */
mbed_official 610:813dcc80987e 1427 if(Timeout != HAL_MAX_DELAY)
mbed_official 610:813dcc80987e 1428 {
mbed_official 610:813dcc80987e 1429 if((Timeout == 0) || ((HAL_GetTick()-tickstart) > Timeout))
mbed_official 610:813dcc80987e 1430 {
mbed_official 610:813dcc80987e 1431 /* Disable TXE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts for the interrupt process */
mbed_official 610:813dcc80987e 1432 __HAL_USART_DISABLE_IT(husart, USART_IT_TXE);
mbed_official 610:813dcc80987e 1433 __HAL_USART_DISABLE_IT(husart, USART_IT_RXNE);
mbed_official 610:813dcc80987e 1434 __HAL_USART_DISABLE_IT(husart, USART_IT_PE);
mbed_official 610:813dcc80987e 1435 __HAL_USART_DISABLE_IT(husart, USART_IT_ERR);
mbed_official 610:813dcc80987e 1436
mbed_official 610:813dcc80987e 1437 husart->State= HAL_USART_STATE_READY;
mbed_official 610:813dcc80987e 1438
mbed_official 610:813dcc80987e 1439 /* Process Unlocked */
mbed_official 610:813dcc80987e 1440 __HAL_UNLOCK(husart);
mbed_official 610:813dcc80987e 1441
mbed_official 610:813dcc80987e 1442 return HAL_TIMEOUT;
mbed_official 610:813dcc80987e 1443 }
mbed_official 610:813dcc80987e 1444 }
mbed_official 610:813dcc80987e 1445 }
mbed_official 610:813dcc80987e 1446 }
mbed_official 610:813dcc80987e 1447 else
mbed_official 610:813dcc80987e 1448 {
mbed_official 610:813dcc80987e 1449 while(__HAL_USART_GET_FLAG(husart, Flag) != RESET)
mbed_official 610:813dcc80987e 1450 {
mbed_official 610:813dcc80987e 1451 /* Check for the Timeout */
mbed_official 610:813dcc80987e 1452 if(Timeout != HAL_MAX_DELAY)
mbed_official 610:813dcc80987e 1453 {
mbed_official 610:813dcc80987e 1454 if((Timeout == 0) || ((HAL_GetTick()-tickstart) > Timeout))
mbed_official 610:813dcc80987e 1455 {
mbed_official 610:813dcc80987e 1456 /* Disable TXE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts for the interrupt process */
mbed_official 610:813dcc80987e 1457 __HAL_USART_DISABLE_IT(husart, USART_IT_TXE);
mbed_official 610:813dcc80987e 1458 __HAL_USART_DISABLE_IT(husart, USART_IT_RXNE);
mbed_official 610:813dcc80987e 1459 __HAL_USART_DISABLE_IT(husart, USART_IT_PE);
mbed_official 610:813dcc80987e 1460 __HAL_USART_DISABLE_IT(husart, USART_IT_ERR);
mbed_official 610:813dcc80987e 1461
mbed_official 610:813dcc80987e 1462 husart->State= HAL_USART_STATE_READY;
mbed_official 610:813dcc80987e 1463
mbed_official 610:813dcc80987e 1464 /* Process Unlocked */
mbed_official 610:813dcc80987e 1465 __HAL_UNLOCK(husart);
mbed_official 610:813dcc80987e 1466
mbed_official 610:813dcc80987e 1467 return HAL_TIMEOUT;
mbed_official 610:813dcc80987e 1468 }
mbed_official 610:813dcc80987e 1469 }
mbed_official 610:813dcc80987e 1470 }
mbed_official 610:813dcc80987e 1471 }
mbed_official 610:813dcc80987e 1472 return HAL_OK;
mbed_official 610:813dcc80987e 1473 }
mbed_official 610:813dcc80987e 1474
mbed_official 610:813dcc80987e 1475 /**
mbed_official 610:813dcc80987e 1476 * @brief Configure the USART peripheral.
mbed_official 610:813dcc80987e 1477 * @param husart: USART handle.
mbed_official 610:813dcc80987e 1478 * @retval HAL status
mbed_official 610:813dcc80987e 1479 */
mbed_official 610:813dcc80987e 1480 static HAL_StatusTypeDef USART_SetConfig(USART_HandleTypeDef *husart)
mbed_official 610:813dcc80987e 1481 {
mbed_official 610:813dcc80987e 1482 uint32_t tmpreg = 0x0;
mbed_official 610:813dcc80987e 1483 USART_ClockSourceTypeDef clocksource = USART_CLOCKSOURCE_UNDEFINED;
mbed_official 610:813dcc80987e 1484 HAL_StatusTypeDef ret = HAL_OK;
mbed_official 610:813dcc80987e 1485 uint16_t brrtemp = 0x0000;
mbed_official 610:813dcc80987e 1486 uint16_t usartdiv = 0x0000;
mbed_official 610:813dcc80987e 1487
mbed_official 610:813dcc80987e 1488 /* Check the parameters */
mbed_official 610:813dcc80987e 1489 assert_param(IS_USART_POLARITY(husart->Init.CLKPolarity));
mbed_official 610:813dcc80987e 1490 assert_param(IS_USART_PHASE(husart->Init.CLKPhase));
mbed_official 610:813dcc80987e 1491 assert_param(IS_USART_LASTBIT(husart->Init.CLKLastBit));
mbed_official 610:813dcc80987e 1492 assert_param(IS_USART_BAUDRATE(husart->Init.BaudRate));
mbed_official 610:813dcc80987e 1493 assert_param(IS_USART_WORD_LENGTH(husart->Init.WordLength));
mbed_official 610:813dcc80987e 1494 assert_param(IS_USART_STOPBITS(husart->Init.StopBits));
mbed_official 610:813dcc80987e 1495 assert_param(IS_USART_PARITY(husart->Init.Parity));
mbed_official 610:813dcc80987e 1496 assert_param(IS_USART_MODE(husart->Init.Mode));
mbed_official 610:813dcc80987e 1497
mbed_official 610:813dcc80987e 1498
mbed_official 610:813dcc80987e 1499 /*-------------------------- USART CR1 Configuration -----------------------*/
mbed_official 610:813dcc80987e 1500 /* Clear M, PCE, PS, TE and RE bits and configure
mbed_official 610:813dcc80987e 1501 * the USART Word Length, Parity and Mode:
mbed_official 610:813dcc80987e 1502 * set the M bits according to husart->Init.WordLength value
mbed_official 610:813dcc80987e 1503 * set PCE and PS bits according to husart->Init.Parity value
mbed_official 610:813dcc80987e 1504 * set TE and RE bits according to husart->Init.Mode value
mbed_official 610:813dcc80987e 1505 * force OVER8 to 1 to allow to reach the maximum speed (Fclock/8) */
mbed_official 610:813dcc80987e 1506 tmpreg = (uint32_t)husart->Init.WordLength | husart->Init.Parity | husart->Init.Mode | USART_CR1_OVER8;
mbed_official 610:813dcc80987e 1507 MODIFY_REG(husart->Instance->CR1, USART_CR1_FIELDS, tmpreg);
mbed_official 610:813dcc80987e 1508
mbed_official 610:813dcc80987e 1509 /*---------------------------- USART CR2 Configuration ---------------------*/
mbed_official 610:813dcc80987e 1510 /* Clear and configure the USART Clock, CPOL, CPHA, LBCL and STOP bits:
mbed_official 610:813dcc80987e 1511 * set CPOL bit according to husart->Init.CLKPolarity value
mbed_official 610:813dcc80987e 1512 * set CPHA bit according to husart->Init.CLKPhase value
mbed_official 610:813dcc80987e 1513 * set LBCL bit according to husart->Init.CLKLastBit value
mbed_official 610:813dcc80987e 1514 * set STOP[13:12] bits according to husart->Init.StopBits value */
mbed_official 610:813dcc80987e 1515 tmpreg = (uint32_t)(USART_CLOCK_ENABLE);
mbed_official 610:813dcc80987e 1516 tmpreg |= ((uint32_t)husart->Init.CLKPolarity | (uint32_t)husart->Init.CLKPhase);
mbed_official 610:813dcc80987e 1517 tmpreg |= ((uint32_t)husart->Init.CLKLastBit | (uint32_t)husart->Init.StopBits);
mbed_official 610:813dcc80987e 1518 MODIFY_REG(husart->Instance->CR2, USART_CR2_FIELDS, tmpreg);
mbed_official 610:813dcc80987e 1519
mbed_official 610:813dcc80987e 1520 /*-------------------------- USART CR3 Configuration -----------------------*/
mbed_official 610:813dcc80987e 1521 /* no CR3 register configuration */
mbed_official 610:813dcc80987e 1522
mbed_official 610:813dcc80987e 1523 /*-------------------------- USART BRR Configuration -----------------------*/
mbed_official 610:813dcc80987e 1524 /* BRR is filled-up according to OVER8 bit setting which is forced to 1 */
mbed_official 610:813dcc80987e 1525 USART_GETCLOCKSOURCE(husart, clocksource);
mbed_official 610:813dcc80987e 1526 switch (clocksource)
mbed_official 610:813dcc80987e 1527 {
mbed_official 610:813dcc80987e 1528 case USART_CLOCKSOURCE_PCLK1:
mbed_official 610:813dcc80987e 1529 usartdiv = (uint16_t)((2*HAL_RCC_GetPCLK1Freq()) / husart->Init.BaudRate);
mbed_official 610:813dcc80987e 1530 break;
mbed_official 610:813dcc80987e 1531 case USART_CLOCKSOURCE_PCLK2:
mbed_official 610:813dcc80987e 1532 usartdiv = (uint16_t)((2*HAL_RCC_GetPCLK2Freq()) / husart->Init.BaudRate);
mbed_official 610:813dcc80987e 1533 break;
mbed_official 610:813dcc80987e 1534 case USART_CLOCKSOURCE_HSI:
mbed_official 610:813dcc80987e 1535 usartdiv = (uint16_t)((2*HSI_VALUE) / husart->Init.BaudRate);
mbed_official 610:813dcc80987e 1536 break;
mbed_official 610:813dcc80987e 1537 case USART_CLOCKSOURCE_SYSCLK:
mbed_official 610:813dcc80987e 1538 usartdiv = (uint16_t)((2*HAL_RCC_GetSysClockFreq()) / husart->Init.BaudRate);
mbed_official 610:813dcc80987e 1539 break;
mbed_official 610:813dcc80987e 1540 case USART_CLOCKSOURCE_LSE:
mbed_official 610:813dcc80987e 1541 usartdiv = (uint16_t)((2*LSE_VALUE) / husart->Init.BaudRate);
mbed_official 610:813dcc80987e 1542 break;
mbed_official 610:813dcc80987e 1543 case USART_CLOCKSOURCE_UNDEFINED:
mbed_official 610:813dcc80987e 1544 default:
mbed_official 610:813dcc80987e 1545 ret = HAL_ERROR;
mbed_official 610:813dcc80987e 1546 break;
mbed_official 610:813dcc80987e 1547 }
mbed_official 610:813dcc80987e 1548
mbed_official 610:813dcc80987e 1549 brrtemp = usartdiv & 0xFFF0;
mbed_official 610:813dcc80987e 1550 brrtemp |= (uint16_t)((usartdiv & (uint16_t)0x000F) >> 1U);
mbed_official 610:813dcc80987e 1551 husart->Instance->BRR = brrtemp;
mbed_official 610:813dcc80987e 1552
mbed_official 610:813dcc80987e 1553 return ret;
mbed_official 610:813dcc80987e 1554 }
mbed_official 610:813dcc80987e 1555
mbed_official 610:813dcc80987e 1556 /**
mbed_official 610:813dcc80987e 1557 * @brief Check the USART Idle State.
mbed_official 610:813dcc80987e 1558 * @param husart: USART handle.
mbed_official 610:813dcc80987e 1559 * @retval HAL status
mbed_official 610:813dcc80987e 1560 */
mbed_official 610:813dcc80987e 1561 static HAL_StatusTypeDef USART_CheckIdleState(USART_HandleTypeDef *husart)
mbed_official 610:813dcc80987e 1562 {
mbed_official 610:813dcc80987e 1563 /* Initialize the USART ErrorCode */
mbed_official 610:813dcc80987e 1564 husart->ErrorCode = HAL_USART_ERROR_NONE;
mbed_official 610:813dcc80987e 1565
mbed_official 610:813dcc80987e 1566 /* Check if the Transmitter is enabled */
mbed_official 610:813dcc80987e 1567 if((husart->Instance->CR1 & USART_CR1_TE) == USART_CR1_TE)
mbed_official 610:813dcc80987e 1568 {
mbed_official 610:813dcc80987e 1569 /* Wait until TEACK flag is set */
mbed_official 610:813dcc80987e 1570 if(USART_WaitOnFlagUntilTimeout(husart, USART_ISR_TEACK, RESET, USART_TEACK_REACK_TIMEOUT) != HAL_OK)
mbed_official 610:813dcc80987e 1571 {
mbed_official 610:813dcc80987e 1572 /* Timeout occurred */
mbed_official 610:813dcc80987e 1573 return HAL_TIMEOUT;
mbed_official 610:813dcc80987e 1574 }
mbed_official 610:813dcc80987e 1575 }
mbed_official 610:813dcc80987e 1576 /* Check if the Receiver is enabled */
mbed_official 610:813dcc80987e 1577 if((husart->Instance->CR1 & USART_CR1_RE) == USART_CR1_RE)
mbed_official 610:813dcc80987e 1578 {
mbed_official 610:813dcc80987e 1579 /* Wait until REACK flag is set */
mbed_official 610:813dcc80987e 1580 if(USART_WaitOnFlagUntilTimeout(husart, USART_ISR_REACK, RESET, USART_TEACK_REACK_TIMEOUT) != HAL_OK)
mbed_official 610:813dcc80987e 1581 {
mbed_official 610:813dcc80987e 1582 /* Timeout occurred */
mbed_official 610:813dcc80987e 1583 return HAL_TIMEOUT;
mbed_official 610:813dcc80987e 1584 }
mbed_official 610:813dcc80987e 1585 }
mbed_official 610:813dcc80987e 1586
mbed_official 610:813dcc80987e 1587 /* Initialize the USART state*/
mbed_official 610:813dcc80987e 1588 husart->State= HAL_USART_STATE_READY;
mbed_official 610:813dcc80987e 1589
mbed_official 610:813dcc80987e 1590 /* Process Unlocked */
mbed_official 610:813dcc80987e 1591 __HAL_UNLOCK(husart);
mbed_official 610:813dcc80987e 1592
mbed_official 610:813dcc80987e 1593 return HAL_OK;
mbed_official 610:813dcc80987e 1594 }
mbed_official 610:813dcc80987e 1595
mbed_official 610:813dcc80987e 1596 /**
mbed_official 610:813dcc80987e 1597 * @brief Simplex send an amount of data in non-blocking mode.
mbed_official 610:813dcc80987e 1598 * @note Function called under interruption only, once
mbed_official 610:813dcc80987e 1599 * interruptions have been enabled by HAL_USART_Transmit_IT().
mbed_official 610:813dcc80987e 1600 * @note The USART errors are not managed to avoid the overrun error.
mbed_official 610:813dcc80987e 1601 * @param husart: USART handle.
mbed_official 610:813dcc80987e 1602 * @retval HAL status
mbed_official 610:813dcc80987e 1603 */
mbed_official 610:813dcc80987e 1604 static HAL_StatusTypeDef USART_Transmit_IT(USART_HandleTypeDef *husart)
mbed_official 610:813dcc80987e 1605 {
mbed_official 610:813dcc80987e 1606 uint16_t* tmp;
mbed_official 610:813dcc80987e 1607
mbed_official 610:813dcc80987e 1608 if(husart->State == HAL_USART_STATE_BUSY_TX)
mbed_official 610:813dcc80987e 1609 {
mbed_official 610:813dcc80987e 1610
mbed_official 610:813dcc80987e 1611 if(husart->TxXferCount == 0)
mbed_official 610:813dcc80987e 1612 {
mbed_official 610:813dcc80987e 1613 /* Disable the USART Transmit Complete Interrupt */
mbed_official 610:813dcc80987e 1614 __HAL_USART_DISABLE_IT(husart, USART_IT_TXE);
mbed_official 610:813dcc80987e 1615
mbed_official 610:813dcc80987e 1616 /* Enable the USART Transmit Complete Interrupt */
mbed_official 610:813dcc80987e 1617 __HAL_USART_ENABLE_IT(husart, USART_IT_TC);
mbed_official 610:813dcc80987e 1618
mbed_official 610:813dcc80987e 1619 return HAL_OK;
mbed_official 610:813dcc80987e 1620 }
mbed_official 610:813dcc80987e 1621 else
mbed_official 610:813dcc80987e 1622 {
mbed_official 610:813dcc80987e 1623 if((husart->Init.WordLength == USART_WORDLENGTH_9B) && (husart->Init.Parity == USART_PARITY_NONE))
mbed_official 610:813dcc80987e 1624 {
mbed_official 610:813dcc80987e 1625 tmp = (uint16_t*) husart->pTxBuffPtr;
mbed_official 610:813dcc80987e 1626 husart->Instance->TDR = (*tmp & (uint16_t)0x01FF);
mbed_official 610:813dcc80987e 1627 husart->pTxBuffPtr += 2;
mbed_official 610:813dcc80987e 1628 }
mbed_official 610:813dcc80987e 1629 else
mbed_official 610:813dcc80987e 1630 {
mbed_official 610:813dcc80987e 1631 husart->Instance->TDR = (uint8_t)(*husart->pTxBuffPtr++ & (uint8_t)0xFF);
mbed_official 610:813dcc80987e 1632 }
mbed_official 610:813dcc80987e 1633
mbed_official 610:813dcc80987e 1634 husart->TxXferCount--;
mbed_official 610:813dcc80987e 1635
mbed_official 610:813dcc80987e 1636 return HAL_OK;
mbed_official 610:813dcc80987e 1637 }
mbed_official 610:813dcc80987e 1638 }
mbed_official 610:813dcc80987e 1639 else
mbed_official 610:813dcc80987e 1640 {
mbed_official 610:813dcc80987e 1641 return HAL_BUSY;
mbed_official 610:813dcc80987e 1642 }
mbed_official 610:813dcc80987e 1643 }
mbed_official 610:813dcc80987e 1644
mbed_official 610:813dcc80987e 1645
mbed_official 610:813dcc80987e 1646 /**
mbed_official 610:813dcc80987e 1647 * @brief Wraps up transmission in non-blocking mode.
mbed_official 610:813dcc80987e 1648 * @param husart: pointer to a USART_HandleTypeDef structure that contains
mbed_official 610:813dcc80987e 1649 * the configuration information for the specified USART module.
mbed_official 610:813dcc80987e 1650 * @retval HAL status
mbed_official 610:813dcc80987e 1651 */
mbed_official 610:813dcc80987e 1652 static HAL_StatusTypeDef USART_EndTransmit_IT(USART_HandleTypeDef *husart)
mbed_official 610:813dcc80987e 1653 {
mbed_official 610:813dcc80987e 1654 /* Disable the USART Transmit Complete Interrupt */
mbed_official 610:813dcc80987e 1655 __HAL_USART_DISABLE_IT(husart, USART_IT_TC);
mbed_official 610:813dcc80987e 1656
mbed_official 610:813dcc80987e 1657 /* Disable the USART Error Interrupt: (Frame error, noise error, overrun error) */
mbed_official 610:813dcc80987e 1658 __HAL_USART_DISABLE_IT(husart, USART_IT_ERR);
mbed_official 610:813dcc80987e 1659
mbed_official 610:813dcc80987e 1660 husart->State = HAL_USART_STATE_READY;
mbed_official 610:813dcc80987e 1661
mbed_official 610:813dcc80987e 1662 HAL_USART_TxCpltCallback(husart);
mbed_official 610:813dcc80987e 1663
mbed_official 610:813dcc80987e 1664 return HAL_OK;
mbed_official 610:813dcc80987e 1665 }
mbed_official 610:813dcc80987e 1666
mbed_official 610:813dcc80987e 1667
mbed_official 610:813dcc80987e 1668 /**
mbed_official 610:813dcc80987e 1669 * @brief Simplex receive an amount of data in non-blocking mode.
mbed_official 610:813dcc80987e 1670 * @note Function called under interruption only, once
mbed_official 610:813dcc80987e 1671 * interruptions have been enabled by HAL_USART_Receive_IT().
mbed_official 610:813dcc80987e 1672 * @param husart: USART handle
mbed_official 610:813dcc80987e 1673 * @retval HAL status
mbed_official 610:813dcc80987e 1674 */
mbed_official 610:813dcc80987e 1675 static HAL_StatusTypeDef USART_Receive_IT(USART_HandleTypeDef *husart)
mbed_official 610:813dcc80987e 1676 {
mbed_official 610:813dcc80987e 1677 uint16_t* tmp;
mbed_official 610:813dcc80987e 1678 uint16_t uhMask = husart->Mask;
mbed_official 610:813dcc80987e 1679
mbed_official 610:813dcc80987e 1680 if(husart->State == HAL_USART_STATE_BUSY_RX)
mbed_official 610:813dcc80987e 1681 {
mbed_official 610:813dcc80987e 1682
mbed_official 610:813dcc80987e 1683 if((husart->Init.WordLength == USART_WORDLENGTH_9B) && (husart->Init.Parity == USART_PARITY_NONE))
mbed_official 610:813dcc80987e 1684 {
mbed_official 610:813dcc80987e 1685 tmp = (uint16_t*) husart->pRxBuffPtr;
mbed_official 610:813dcc80987e 1686 *tmp = (uint16_t)(husart->Instance->RDR & uhMask);
mbed_official 610:813dcc80987e 1687 husart->pRxBuffPtr += 2;
mbed_official 610:813dcc80987e 1688 }
mbed_official 610:813dcc80987e 1689 else
mbed_official 610:813dcc80987e 1690 {
mbed_official 610:813dcc80987e 1691 *husart->pRxBuffPtr++ = (uint8_t)(husart->Instance->RDR & (uint8_t)uhMask);
mbed_official 610:813dcc80987e 1692 }
mbed_official 610:813dcc80987e 1693 /* Send dummy byte in order to generate the clock for the Slave to Send the next data */
mbed_official 610:813dcc80987e 1694 husart->Instance->TDR = (USART_DUMMY_DATA & (uint16_t)0x00FF);
mbed_official 610:813dcc80987e 1695
mbed_official 610:813dcc80987e 1696 if(--husart->RxXferCount == 0)
mbed_official 610:813dcc80987e 1697 {
mbed_official 610:813dcc80987e 1698 __HAL_USART_DISABLE_IT(husart, USART_IT_RXNE);
mbed_official 610:813dcc80987e 1699
mbed_official 610:813dcc80987e 1700 /* Disable the USART Parity Error Interrupt */
mbed_official 610:813dcc80987e 1701 __HAL_USART_DISABLE_IT(husart, USART_IT_PE);
mbed_official 610:813dcc80987e 1702
mbed_official 610:813dcc80987e 1703 /* Disable the USART Error Interrupt: (Frame error, noise error, overrun error) */
mbed_official 610:813dcc80987e 1704 __HAL_USART_DISABLE_IT(husart, USART_IT_ERR);
mbed_official 610:813dcc80987e 1705
mbed_official 610:813dcc80987e 1706 husart->State = HAL_USART_STATE_READY;
mbed_official 610:813dcc80987e 1707
mbed_official 610:813dcc80987e 1708 HAL_USART_RxCpltCallback(husart);
mbed_official 610:813dcc80987e 1709
mbed_official 610:813dcc80987e 1710 return HAL_OK;
mbed_official 610:813dcc80987e 1711 }
mbed_official 610:813dcc80987e 1712
mbed_official 610:813dcc80987e 1713 return HAL_OK;
mbed_official 610:813dcc80987e 1714 }
mbed_official 610:813dcc80987e 1715 else
mbed_official 610:813dcc80987e 1716 {
mbed_official 610:813dcc80987e 1717 return HAL_BUSY;
mbed_official 610:813dcc80987e 1718 }
mbed_official 610:813dcc80987e 1719 }
mbed_official 610:813dcc80987e 1720
mbed_official 610:813dcc80987e 1721 /**
mbed_official 610:813dcc80987e 1722 * @brief Full-Duplex Send receive an amount of data in full-duplex mode (non-blocking).
mbed_official 610:813dcc80987e 1723 * @note Function called under interruption only, once
mbed_official 610:813dcc80987e 1724 * interruptions have been enabled by HAL_USART_TransmitReceive_IT().
mbed_official 610:813dcc80987e 1725 * @param husart: USART handle.
mbed_official 610:813dcc80987e 1726 * @retval HAL status
mbed_official 610:813dcc80987e 1727 */
mbed_official 610:813dcc80987e 1728 static HAL_StatusTypeDef USART_TransmitReceive_IT(USART_HandleTypeDef *husart)
mbed_official 610:813dcc80987e 1729 {
mbed_official 610:813dcc80987e 1730 uint16_t* tmp;
mbed_official 610:813dcc80987e 1731 uint16_t uhMask = husart->Mask;
mbed_official 610:813dcc80987e 1732
mbed_official 610:813dcc80987e 1733 if(husart->State == HAL_USART_STATE_BUSY_TX_RX)
mbed_official 610:813dcc80987e 1734 {
mbed_official 610:813dcc80987e 1735
mbed_official 610:813dcc80987e 1736 if(husart->TxXferCount != 0x00)
mbed_official 610:813dcc80987e 1737 {
mbed_official 610:813dcc80987e 1738 if(__HAL_USART_GET_FLAG(husart, USART_FLAG_TXE) != RESET)
mbed_official 610:813dcc80987e 1739 {
mbed_official 610:813dcc80987e 1740 if((husart->Init.WordLength == USART_WORDLENGTH_9B) && (husart->Init.Parity == USART_PARITY_NONE))
mbed_official 610:813dcc80987e 1741 {
mbed_official 610:813dcc80987e 1742 tmp = (uint16_t*) husart->pTxBuffPtr;
mbed_official 610:813dcc80987e 1743 husart->Instance->TDR = (uint16_t)(*tmp & uhMask);
mbed_official 610:813dcc80987e 1744 husart->pTxBuffPtr += 2;
mbed_official 610:813dcc80987e 1745 }
mbed_official 610:813dcc80987e 1746 else
mbed_official 610:813dcc80987e 1747 {
mbed_official 610:813dcc80987e 1748 husart->Instance->TDR = (uint8_t)(*husart->pTxBuffPtr++ & (uint8_t)uhMask);
mbed_official 610:813dcc80987e 1749 }
mbed_official 610:813dcc80987e 1750 husart->TxXferCount--;
mbed_official 610:813dcc80987e 1751
mbed_official 610:813dcc80987e 1752 /* Check the latest data transmitted */
mbed_official 610:813dcc80987e 1753 if(husart->TxXferCount == 0)
mbed_official 610:813dcc80987e 1754 {
mbed_official 610:813dcc80987e 1755 __HAL_USART_DISABLE_IT(husart, USART_IT_TXE);
mbed_official 610:813dcc80987e 1756 }
mbed_official 610:813dcc80987e 1757 }
mbed_official 610:813dcc80987e 1758 }
mbed_official 610:813dcc80987e 1759
mbed_official 610:813dcc80987e 1760 if(husart->RxXferCount != 0x00)
mbed_official 610:813dcc80987e 1761 {
mbed_official 610:813dcc80987e 1762 if(__HAL_USART_GET_FLAG(husart, USART_FLAG_RXNE) != RESET)
mbed_official 610:813dcc80987e 1763 {
mbed_official 610:813dcc80987e 1764 if((husart->Init.WordLength == USART_WORDLENGTH_9B) && (husart->Init.Parity == USART_PARITY_NONE))
mbed_official 610:813dcc80987e 1765 {
mbed_official 610:813dcc80987e 1766 tmp = (uint16_t*) husart->pRxBuffPtr;
mbed_official 610:813dcc80987e 1767 *tmp = (uint16_t)(husart->Instance->RDR & uhMask);
mbed_official 610:813dcc80987e 1768 husart->pRxBuffPtr += 2;
mbed_official 610:813dcc80987e 1769 }
mbed_official 610:813dcc80987e 1770 else
mbed_official 610:813dcc80987e 1771 {
mbed_official 610:813dcc80987e 1772 *husart->pRxBuffPtr++ = (uint8_t)(husart->Instance->RDR & (uint8_t)uhMask);
mbed_official 610:813dcc80987e 1773 }
mbed_official 610:813dcc80987e 1774 husart->RxXferCount--;
mbed_official 610:813dcc80987e 1775 }
mbed_official 610:813dcc80987e 1776 }
mbed_official 610:813dcc80987e 1777
mbed_official 610:813dcc80987e 1778 /* Check the latest data received */
mbed_official 610:813dcc80987e 1779 if(husart->RxXferCount == 0)
mbed_official 610:813dcc80987e 1780 {
mbed_official 610:813dcc80987e 1781 __HAL_USART_DISABLE_IT(husart, USART_IT_RXNE);
mbed_official 610:813dcc80987e 1782
mbed_official 610:813dcc80987e 1783 /* Disable the USART Parity Error Interrupt */
mbed_official 610:813dcc80987e 1784 __HAL_USART_DISABLE_IT(husart, USART_IT_PE);
mbed_official 610:813dcc80987e 1785
mbed_official 610:813dcc80987e 1786 /* Disable the USART Error Interrupt: (Frame error, noise error, overrun error) */
mbed_official 610:813dcc80987e 1787 __HAL_USART_DISABLE_IT(husart, USART_IT_ERR);
mbed_official 610:813dcc80987e 1788
mbed_official 610:813dcc80987e 1789 husart->State = HAL_USART_STATE_READY;
mbed_official 610:813dcc80987e 1790
mbed_official 610:813dcc80987e 1791 HAL_USART_TxRxCpltCallback(husart);
mbed_official 610:813dcc80987e 1792
mbed_official 610:813dcc80987e 1793 return HAL_OK;
mbed_official 610:813dcc80987e 1794 }
mbed_official 610:813dcc80987e 1795
mbed_official 610:813dcc80987e 1796 return HAL_OK;
mbed_official 610:813dcc80987e 1797 }
mbed_official 610:813dcc80987e 1798 else
mbed_official 610:813dcc80987e 1799 {
mbed_official 610:813dcc80987e 1800 return HAL_BUSY;
mbed_official 610:813dcc80987e 1801 }
mbed_official 610:813dcc80987e 1802 }
mbed_official 610:813dcc80987e 1803
mbed_official 610:813dcc80987e 1804 /**
mbed_official 610:813dcc80987e 1805 * @}
mbed_official 610:813dcc80987e 1806 */
mbed_official 610:813dcc80987e 1807
mbed_official 610:813dcc80987e 1808 #endif /* HAL_USART_MODULE_ENABLED */
mbed_official 610:813dcc80987e 1809 /**
mbed_official 610:813dcc80987e 1810 * @}
mbed_official 610:813dcc80987e 1811 */
mbed_official 610:813dcc80987e 1812
mbed_official 610:813dcc80987e 1813 /**
mbed_official 610:813dcc80987e 1814 * @}
mbed_official 610:813dcc80987e 1815 */
mbed_official 610:813dcc80987e 1816
mbed_official 610:813dcc80987e 1817 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/