mbed library sources

Dependents:   frdm_kl05z_gpio_test

Fork of mbed-src by mbed official

Committer:
mbed_official
Date:
Thu Aug 21 15:00:08 2014 +0100
Revision:
296:ec1b66a3d094
Parent:
targets/cmsis/TARGET_STM/TARGET_DISCO_F407VG/stm32f4xx_hal_usart.c@242:7074e42da0b2
Synchronized with git revision bbc120c4786e99dfa586e7a13f8638064f1e5938

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

DISCO_F407VG - add USBDevice support and a variant - ARCH_MAX

Who changed what in which revision?

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