mbed library sources

Dependents:   frdm_kl05z_gpio_test

Fork of mbed-src by mbed official

Committer:
shaoziyang
Date:
Sat Sep 13 14:25:46 2014 +0000
Revision:
323:9e901b0a5aa1
Parent:
237:f3da66175598
test with CLOCK_SETUP = 0

Who changed what in which revision?

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