mbed library sources

Dependents:   Encrypted my_mbed lklk CyaSSL_DTLS_Cellular ... more

Superseded

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

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

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

Import librarymbed

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

Committer:
mbed_official
Date:
Wed Jul 01 09:45:11 2015 +0100
Revision:
579:53297373a894
Parent:
441:d2c15dda23c1
Child:
630:825f75ca301e
Synchronized with git revision d5b4d2ab9c47edb4dc5776e7177b0c2263459081

Full URL: https://github.com/mbedmicro/mbed/commit/d5b4d2ab9c47edb4dc5776e7177b0c2263459081/

Initial version of drivers for SAMR21

Who changed what in which revision?

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