mbed library sources

Dependents:   Encrypted my_mbed lklk CyaSSL_DTLS_Cellular ... more

Superseded

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

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

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

Import librarymbed

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

Committer:
mbed_official
Date:
Wed Jul 01 09:45:11 2015 +0100
Revision:
579:53297373a894
Parent:
394:83f921546702
Synchronized with git revision d5b4d2ab9c47edb4dc5776e7177b0c2263459081

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

Initial version of drivers for SAMR21

Who changed what in which revision?

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