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