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