mbed library sources

Dependents:   frdm_kl05z_gpio_test

Fork of mbed-src by mbed official

Committer:
mbed_official
Date:
Mon Mar 24 17:45:07 2014 +0000
Revision:
133:d4dda5c437f0
Child:
242:7074e42da0b2
Synchronized with git revision 47b961246bed973fe4cb8932781ffc8025b78a61

Full URL: https://github.com/mbedmicro/mbed/commit/47b961246bed973fe4cb8932781ffc8025b78a61/

[STM32F4-Discovery (STM32F407VG)] initial port

Who changed what in which revision?

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