mbed library sources

Fork of mbed-src by mbed official

Committer:
mbed_official
Date:
Mon Nov 03 10:30:07 2014 +0000
Revision:
381:5460fc57b6e4
Synchronized with git revision 02478cd1f27fc7b9643486472635eb515b2bca81

Full URL: https://github.com/mbedmicro/mbed/commit/02478cd1f27fc7b9643486472635eb515b2bca81/

Target: LPC1549 - Fix serial interrupt issues (issue report #616)

Who changed what in which revision?

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