|
STM32G474xx HAL用户手册
|
00001 /** 00002 ****************************************************************************** 00003 * @file stm32g4xx_hal_usart.c 00004 * @author MCD Application Team 00005 * @brief USART HAL module driver. 00006 * This file provides firmware functions to manage the following 00007 * functionalities of the Universal Synchronous/Asynchronous Receiver Transmitter 00008 * Peripheral (USART). 00009 * + Initialization and de-initialization functions 00010 * + IO operation functions 00011 * + Peripheral Control functions 00012 * + Peripheral State and Error functions 00013 * 00014 ****************************************************************************** 00015 * @attention 00016 * 00017 * Copyright (c) 2019 STMicroelectronics. 00018 * All rights reserved. 00019 * 00020 * This software is licensed under terms that can be found in the LICENSE file 00021 * in the root directory of this software component. 00022 * If no LICENSE file comes with this software, it is provided AS-IS. 00023 * 00024 ****************************************************************************** 00025 @verbatim 00026 =============================================================================== 00027 ##### How to use this driver ##### 00028 =============================================================================== 00029 [..] 00030 The USART HAL driver can be used as follows: 00031 00032 (#) Declare a USART_HandleTypeDef handle structure (eg. USART_HandleTypeDef husart). 00033 (#) Initialize the USART low level resources by implementing the HAL_USART_MspInit() API: 00034 (++) Enable the USARTx interface clock. 00035 (++) USART pins configuration: 00036 (+++) Enable the clock for the USART GPIOs. 00037 (+++) Configure these USART pins as alternate function pull-up. 00038 (++) NVIC configuration if you need to use interrupt process (HAL_USART_Transmit_IT(), 00039 HAL_USART_Receive_IT() and HAL_USART_TransmitReceive_IT() APIs): 00040 (+++) Configure the USARTx interrupt priority. 00041 (+++) Enable the NVIC USART IRQ handle. 00042 (++) USART interrupts handling: 00043 -@@- The specific USART interrupts (Transmission complete interrupt, 00044 RXNE interrupt and Error Interrupts) will be managed using the macros 00045 __HAL_USART_ENABLE_IT() and __HAL_USART_DISABLE_IT() inside the transmit and receive process. 00046 (++) DMA Configuration if you need to use DMA process (HAL_USART_Transmit_DMA() 00047 HAL_USART_Receive_DMA() and HAL_USART_TransmitReceive_DMA() APIs): 00048 (+++) Declare a DMA handle structure for the Tx/Rx channel. 00049 (+++) Enable the DMAx interface clock. 00050 (+++) Configure the declared DMA handle structure with the required Tx/Rx parameters. 00051 (+++) Configure the DMA Tx/Rx channel. 00052 (+++) Associate the initialized DMA handle to the USART DMA Tx/Rx handle. 00053 (+++) Configure the priority and enable the NVIC for the transfer 00054 complete interrupt on the DMA Tx/Rx channel. 00055 00056 (#) Program the Baud Rate, Word Length, Stop Bit, Parity, and Mode 00057 (Receiver/Transmitter) in the husart handle Init structure. 00058 00059 (#) Initialize the USART registers by calling the HAL_USART_Init() API: 00060 (++) This API configures also the low level Hardware GPIO, CLOCK, CORTEX...etc) 00061 by calling the customized HAL_USART_MspInit(&husart) API. 00062 00063 [..] 00064 (@) To configure and enable/disable the USART to wake up the MCU from stop mode, resort to UART API's 00065 HAL_UARTEx_StopModeWakeUpSourceConfig(), HAL_UARTEx_EnableStopMode() and 00066 HAL_UARTEx_DisableStopMode() in casting the USART handle to UART type UART_HandleTypeDef. 00067 00068 ##### Callback registration ##### 00069 ================================== 00070 00071 [..] 00072 The compilation define USE_HAL_USART_REGISTER_CALLBACKS when set to 1 00073 allows the user to configure dynamically the driver callbacks. 00074 00075 [..] 00076 Use Function HAL_USART_RegisterCallback() to register a user callback. 00077 Function HAL_USART_RegisterCallback() allows to register following callbacks: 00078 (+) TxHalfCpltCallback : Tx Half Complete Callback. 00079 (+) TxCpltCallback : Tx Complete Callback. 00080 (+) RxHalfCpltCallback : Rx Half Complete Callback. 00081 (+) RxCpltCallback : Rx Complete Callback. 00082 (+) TxRxCpltCallback : Tx Rx Complete Callback. 00083 (+) ErrorCallback : Error Callback. 00084 (+) AbortCpltCallback : Abort Complete Callback. 00085 (+) RxFifoFullCallback : Rx Fifo Full Callback. 00086 (+) TxFifoEmptyCallback : Tx Fifo Empty Callback. 00087 (+) MspInitCallback : USART MspInit. 00088 (+) MspDeInitCallback : USART MspDeInit. 00089 This function takes as parameters the HAL peripheral handle, the Callback ID 00090 and a pointer to the user callback function. 00091 00092 [..] 00093 Use function HAL_USART_UnRegisterCallback() to reset a callback to the default 00094 weak function. 00095 HAL_USART_UnRegisterCallback() takes as parameters the HAL peripheral handle, 00096 and the Callback ID. 00097 This function allows to reset following callbacks: 00098 (+) TxHalfCpltCallback : Tx Half Complete Callback. 00099 (+) TxCpltCallback : Tx Complete Callback. 00100 (+) RxHalfCpltCallback : Rx Half Complete Callback. 00101 (+) RxCpltCallback : Rx Complete Callback. 00102 (+) TxRxCpltCallback : Tx Rx Complete Callback. 00103 (+) ErrorCallback : Error Callback. 00104 (+) AbortCpltCallback : Abort Complete Callback. 00105 (+) RxFifoFullCallback : Rx Fifo Full Callback. 00106 (+) TxFifoEmptyCallback : Tx Fifo Empty Callback. 00107 (+) MspInitCallback : USART MspInit. 00108 (+) MspDeInitCallback : USART MspDeInit. 00109 00110 [..] 00111 By default, after the HAL_USART_Init() and when the state is HAL_USART_STATE_RESET 00112 all callbacks are set to the corresponding weak functions: 00113 examples HAL_USART_TxCpltCallback(), HAL_USART_RxHalfCpltCallback(). 00114 Exception done for MspInit and MspDeInit functions that are respectively 00115 reset to the legacy weak functions in the HAL_USART_Init() 00116 and HAL_USART_DeInit() only when these callbacks are null (not registered beforehand). 00117 If not, MspInit or MspDeInit are not null, the HAL_USART_Init() and HAL_USART_DeInit() 00118 keep and use the user MspInit/MspDeInit callbacks (registered beforehand). 00119 00120 [..] 00121 Callbacks can be registered/unregistered in HAL_USART_STATE_READY state only. 00122 Exception done MspInit/MspDeInit that can be registered/unregistered 00123 in HAL_USART_STATE_READY or HAL_USART_STATE_RESET state, thus registered (user) 00124 MspInit/DeInit callbacks can be used during the Init/DeInit. 00125 In that case first register the MspInit/MspDeInit user callbacks 00126 using HAL_USART_RegisterCallback() before calling HAL_USART_DeInit() 00127 or HAL_USART_Init() function. 00128 00129 [..] 00130 When The compilation define USE_HAL_USART_REGISTER_CALLBACKS is set to 0 or 00131 not defined, the callback registration feature is not available 00132 and weak callbacks are used. 00133 00134 00135 @endverbatim 00136 ****************************************************************************** 00137 */ 00138 00139 /* Includes ------------------------------------------------------------------*/ 00140 #include "stm32g4xx_hal.h" 00141 00142 /** @addtogroup STM32G4xx_HAL_Driver 00143 * @{ 00144 */ 00145 00146 /** @defgroup USART USART 00147 * @brief HAL USART Synchronous SPI module driver 00148 * @{ 00149 */ 00150 00151 #ifdef HAL_USART_MODULE_ENABLED 00152 00153 /* Private typedef -----------------------------------------------------------*/ 00154 /* Private define ------------------------------------------------------------*/ 00155 /** @defgroup USART_Private_Constants USART Private Constants 00156 * @{ 00157 */ 00158 #define USART_DUMMY_DATA ((uint16_t) 0xFFFF) /*!< USART transmitted dummy data */ 00159 #define USART_TEACK_REACK_TIMEOUT 1000U /*!< USART TX or RX enable acknowledge time-out value */ 00160 #define USART_CR1_FIELDS ((uint32_t)(USART_CR1_M | USART_CR1_PCE | USART_CR1_PS | \ 00161 USART_CR1_TE | USART_CR1_RE | USART_CR1_OVER8 | \ 00162 USART_CR1_FIFOEN )) /*!< USART CR1 fields of parameters set by USART_SetConfig API */ 00163 00164 #define USART_CR2_FIELDS ((uint32_t)(USART_CR2_CPHA | USART_CR2_CPOL | USART_CR2_CLKEN | \ 00165 USART_CR2_LBCL | USART_CR2_STOP | USART_CR2_SLVEN | \ 00166 USART_CR2_DIS_NSS)) /*!< USART CR2 fields of parameters set by USART_SetConfig API */ 00167 00168 #define USART_CR3_FIELDS ((uint32_t)(USART_CR3_TXFTCFG | USART_CR3_RXFTCFG )) /*!< USART or USART CR3 fields of parameters set by USART_SetConfig API */ 00169 00170 #define USART_BRR_MIN 0x10U /* USART BRR minimum authorized value */ 00171 #define USART_BRR_MAX 0xFFFFU /* USART BRR maximum authorized value */ 00172 /** 00173 * @} 00174 */ 00175 00176 /* Private macros ------------------------------------------------------------*/ 00177 /* Private variables ---------------------------------------------------------*/ 00178 /* Private function prototypes -----------------------------------------------*/ 00179 /** @addtogroup USART_Private_Functions 00180 * @{ 00181 */ 00182 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1) 00183 void USART_InitCallbacksToDefault(USART_HandleTypeDef *husart); 00184 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */ 00185 static void USART_EndTransfer(USART_HandleTypeDef *husart); 00186 static void USART_DMATransmitCplt(DMA_HandleTypeDef *hdma); 00187 static void USART_DMAReceiveCplt(DMA_HandleTypeDef *hdma); 00188 static void USART_DMATxHalfCplt(DMA_HandleTypeDef *hdma); 00189 static void USART_DMARxHalfCplt(DMA_HandleTypeDef *hdma); 00190 static void USART_DMAError(DMA_HandleTypeDef *hdma); 00191 static void USART_DMAAbortOnError(DMA_HandleTypeDef *hdma);