inport from local

Dependents:   Hobbyking_Cheetah_0511

Committer:
NYX
Date:
Mon Mar 16 06:35:48 2020 +0000
Revision:
0:85b3fd62ea1a
reinport to mbed;

Who changed what in which revision?

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