stm32f0xx_hal_uart.c 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941
  1. /**
  2. ******************************************************************************
  3. * @file stm32f0xx_hal_uart.c
  4. * @author MCD Application Team
  5. * @version V1.3.1
  6. * @date 29-January-2016
  7. * @brief UART HAL module driver.
  8. * This file provides firmware functions to manage the following
  9. * functionalities of the Universal Asynchronous Receiver Transmitter (UART) peripheral:
  10. * + Initialization and de-initialization functions
  11. * + IO operation functions
  12. * + Peripheral Control functions
  13. * + Peripheral State and Errors functions
  14. *
  15. @verbatim
  16. ===============================================================================
  17. ##### How to use this driver #####
  18. ===============================================================================
  19. [..]
  20. The UART HAL driver can be used as follows:
  21. (#) Declare a UART_HandleTypeDef handle structure (eg. UART_HandleTypeDef huart).
  22. (#) Initialize the UART low level resources by implementing the HAL_UART_MspInit() API:
  23. (++) Enable the USARTx interface clock.
  24. (++) UART pins configuration:
  25. (+++) Enable the clock for the UART GPIOs.
  26. (+++) Configure these UART pins as alternate function pull-up.
  27. (++) NVIC configuration if you need to use interrupt process (HAL_UART_Transmit_IT()
  28. and HAL_UART_Receive_IT() APIs):
  29. (+++) Configure the USARTx interrupt priority.
  30. (+++) Enable the NVIC USART IRQ handle.
  31. (++) UART interrupts handling:
  32. -@@- The specific UART interrupts (Transmission complete interrupt,
  33. RXNE interrupt and Error Interrupts) are managed using the macros
  34. __HAL_UART_ENABLE_IT() and __HAL_UART_DISABLE_IT() inside the transmit and receive processes.
  35. (++) DMA Configuration if you need to use DMA process (HAL_UART_Transmit_DMA()
  36. and HAL_UART_Receive_DMA() APIs):
  37. (+++) Declare a DMA handle structure for the Tx/Rx channel.
  38. (+++) Enable the DMAx interface clock.
  39. (+++) Configure the declared DMA handle structure with the required Tx/Rx parameters.
  40. (+++) Configure the DMA Tx/Rx channel.
  41. (+++) Associate the initialized DMA handle to the UART DMA Tx/Rx handle.
  42. (+++) Configure the priority and enable the NVIC for the transfer complete interrupt on the DMA Tx/Rx channel.
  43. (#) Program the Baud Rate, Word Length, Stop Bit, Parity, Hardware
  44. flow control and Mode (Receiver/Transmitter) in the huart handle Init structure.
  45. (#) If required, program UART advanced features (TX/RX pins swap, auto Baud rate detection,...)
  46. in the huart handle AdvancedInit structure.
  47. (#) For the UART asynchronous mode, initialize the UART registers by calling
  48. the HAL_UART_Init() API.
  49. (#) For the UART Half duplex mode, initialize the UART registers by calling
  50. the HAL_HalfDuplex_Init() API.
  51. (#) For the UART Multiprocessor mode, initialize the UART registers
  52. by calling the HAL_MultiProcessor_Init() API.
  53. (#) For the UART RS485 Driver Enabled mode, initialize the UART registers
  54. by calling the HAL_RS485Ex_Init() API.
  55. [..]
  56. (@) These APIs(HAL_UART_Init(), HAL_HalfDuplex_Init(), HAL_MultiProcessor_Init(),
  57. also configure the low level Hardware GPIO, CLOCK, CORTEX...etc) by
  58. calling the customized HAL_UART_MspInit() API.
  59. Three operation modes are available within this driver :
  60. *** Polling mode IO operation ***
  61. =================================
  62. [..]
  63. (+) Send an amount of data in blocking mode using HAL_UART_Transmit()
  64. (+) Receive an amount of data in blocking mode using HAL_UART_Receive()
  65. *** Interrupt mode IO operation ***
  66. ===================================
  67. [..]
  68. (+) Send an amount of data in non blocking mode using HAL_UART_Transmit_IT()
  69. (+) At transmission end of half transfer HAL_UART_TxHalfCpltCallback is executed and user can
  70. add his own code by customization of function pointer HAL_UART_TxHalfCpltCallback
  71. (+) At transmission end of transfer HAL_UART_TxCpltCallback is executed and user can
  72. add his own code by customization of function pointer HAL_UART_TxCpltCallback
  73. (+) Receive an amount of data in non blocking mode using HAL_UART_Receive_IT()
  74. (+) At reception end of half transfer HAL_UART_RxHalfCpltCallback is executed and user can
  75. add his own code by customization of function pointer HAL_UART_RxHalfCpltCallback
  76. (+) At reception end of transfer HAL_UART_RxCpltCallback is executed and user can
  77. add his own code by customization of function pointer HAL_UART_RxCpltCallback
  78. (+) In case of transfer Error, HAL_UART_ErrorCallback() function is executed and user can
  79. add his own code by customization of function pointer HAL_UART_ErrorCallback
  80. *** DMA mode IO operation ***
  81. ==============================
  82. [..]
  83. (+) Send an amount of data in non blocking mode (DMA) using HAL_UART_Transmit_DMA()
  84. (+) At transmission end of half transfer HAL_UART_TxHalfCpltCallback is executed and user can
  85. add his own code by customization of function pointer HAL_UART_TxHalfCpltCallback
  86. (+) At transmission end of transfer HAL_UART_TxCpltCallback is executed and user can
  87. add his own code by customization of function pointer HAL_UART_TxCpltCallback
  88. (+) Receive an amount of data in non blocking mode (DMA) using HAL_UART_Receive_DMA()
  89. (+) At reception end of half transfer HAL_UART_RxHalfCpltCallback is executed and user can
  90. add his own code by customization of function pointer HAL_UART_RxHalfCpltCallback
  91. (+) At reception end of transfer HAL_UART_RxCpltCallback is executed and user can
  92. add his own code by customization of function pointer HAL_UART_RxCpltCallback
  93. (+) In case of transfer Error, HAL_UART_ErrorCallback() function is executed and user can
  94. add his own code by customization of function pointer HAL_UART_ErrorCallback
  95. (+) Pause the DMA Transfer using HAL_UART_DMAPause()
  96. (+) Resume the DMA Transfer using HAL_UART_DMAResume()
  97. (+) Stop the DMA Transfer using HAL_UART_DMAStop()
  98. *** UART HAL driver macros list ***
  99. =============================================
  100. [..]
  101. Below the list of most used macros in UART HAL driver.
  102. (+) __HAL_UART_ENABLE: Enable the UART peripheral
  103. (+) __HAL_UART_DISABLE: Disable the UART peripheral
  104. (+) __HAL_UART_GET_FLAG : Check whether the specified UART flag is set or not
  105. (+) __HAL_UART_CLEAR_FLAG : Clear the specified UART pending flag
  106. (+) __HAL_UART_ENABLE_IT: Enable the specified UART interrupt
  107. (+) __HAL_UART_DISABLE_IT: Disable the specified UART interrupt
  108. [..]
  109. (@) You can refer to the UART HAL driver header file for more useful macros
  110. @endverbatim
  111. ******************************************************************************
  112. * @attention
  113. *
  114. * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
  115. *
  116. * Redistribution and use in source and binary forms, with or without modification,
  117. * are permitted provided that the following conditions are met:
  118. * 1. Redistributions of source code must retain the above copyright notice,
  119. * this list of conditions and the following disclaimer.
  120. * 2. Redistributions in binary form must reproduce the above copyright notice,
  121. * this list of conditions and the following disclaimer in the documentation
  122. * and/or other materials provided with the distribution.
  123. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  124. * may be used to endorse or promote products derived from this software
  125. * without specific prior written permission.
  126. *
  127. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  128. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  129. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  130. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  131. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  132. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  133. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  134. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  135. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  136. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  137. *
  138. ******************************************************************************
  139. */
  140. /* Includes ------------------------------------------------------------------*/
  141. #include "stm32f0xx_hal.h"
  142. /** @addtogroup STM32F0xx_HAL_Driver
  143. * @{
  144. */
  145. /** @defgroup UART UART
  146. * @brief HAL UART module driver
  147. * @{
  148. */
  149. #ifdef HAL_UART_MODULE_ENABLED
  150. /* Private typedef -----------------------------------------------------------*/
  151. /* Private define ------------------------------------------------------------*/
  152. /** @defgroup UART_Private_Constants UART Private Constants
  153. * @{
  154. */
  155. #define UART_TEACK_REACK_TIMEOUT ((uint32_t) 1000) /*!< UART TX or RX enable acknowledge time-out value */
  156. #define UART_TXDMA_TIMEOUTVALUE 22000
  157. #define UART_CR1_FIELDS ((uint32_t)(USART_CR1_M | USART_CR1_PCE | USART_CR1_PS | \
  158. USART_CR1_TE | USART_CR1_RE | USART_CR1_OVER8)) /*!< UART or USART CR1 fields of parameters set by UART_SetConfig API */
  159. /**
  160. * @}
  161. */
  162. /* Private macros ------------------------------------------------------------*/
  163. /* Private variables ---------------------------------------------------------*/
  164. /* Private function prototypes -----------------------------------------------*/
  165. /** @addtogroup UART_Private_Functions UART Private Functions
  166. * @{
  167. */
  168. static void UART_DMATransmitCplt(DMA_HandleTypeDef *hdma);
  169. static void UART_DMATxHalfCplt(DMA_HandleTypeDef *hdma);
  170. static void UART_DMAReceiveCplt(DMA_HandleTypeDef *hdma);
  171. static void UART_DMARxHalfCplt(DMA_HandleTypeDef *hdma);
  172. static void UART_DMAError(DMA_HandleTypeDef *hdma);
  173. /**
  174. * @}
  175. */
  176. /* Exported functions --------------------------------------------------------*/
  177. /** @defgroup UART_Exported_Functions UART Exported Functions
  178. * @{
  179. */
  180. /** @defgroup UART_Exported_Functions_Group1 Initialization and de-initialization functions
  181. * @brief Initialization and Configuration functions
  182. *
  183. @verbatim
  184. ===============================================================================
  185. ##### Initialization and Configuration functions #####
  186. ===============================================================================
  187. [..]
  188. This subsection provides a set of functions allowing to initialize the USARTx or the UARTy
  189. in asynchronous mode.
  190. (+) For the asynchronous mode the parameters below can be configured:
  191. (++) Baud Rate
  192. (++) Word Length
  193. (++) Stop Bit
  194. (++) Parity: If the parity is enabled, then the MSB bit of the data written
  195. in the data register is transmitted but is changed by the parity bit.
  196. According to device capability (support or not of 7-bit word length),
  197. frame length is either defined by the M bit (8-bits or 9-bits)
  198. or by the M1 and M0 bits (7-bit, 8-bit or 9-bit).
  199. Possible UART frame formats are as listed in the following table:
  200. (+++) Table 1. UART frame format.
  201. (+++) +-----------------------------------------------------------------------+
  202. (+++) | M bit | PCE bit | UART frame |
  203. (+++) |-------------------|-----------|---------------------------------------|
  204. (+++) | 0 | 0 | | SB | 8-bit data | STB | |
  205. (+++) |-------------------|-----------|---------------------------------------|
  206. (+++) | 0 | 1 | | SB | 7-bit data | PB | STB | |
  207. (+++) |-------------------|-----------|---------------------------------------|
  208. (+++) | 1 | 0 | | SB | 9-bit data | STB | |
  209. (+++) |-------------------|-----------|---------------------------------------|
  210. (+++) | 1 | 1 | | SB | 8-bit data | PB | STB | |
  211. (+++) +-----------------------------------------------------------------------+
  212. (+++) | M1 bit | M0 bit | PCE bit | UART frame |
  213. (+++) |---------|---------|-----------|---------------------------------------|
  214. (+++) | 0 | 0 | 0 | | SB | 8 bit data | STB | |
  215. (+++) |---------|---------|-----------|---------------------------------------|
  216. (+++) | 0 | 0 | 1 | | SB | 7 bit data | PB | STB | |
  217. (+++) |---------|---------|-----------|---------------------------------------|
  218. (+++) | 0 | 1 | 0 | | SB | 9 bit data | STB | |
  219. (+++) |---------|---------|-----------|---------------------------------------|
  220. (+++) | 0 | 1 | 1 | | SB | 8 bit data | PB | STB | |
  221. (+++) |---------|---------|-----------|---------------------------------------|
  222. (+++) | 1 | 0 | 0 | | SB | 7 bit data | STB | |
  223. (+++) |---------|---------|-----------|---------------------------------------|
  224. (+++) | 1 | 0 | 1 | | SB | 6 bit data | PB | STB | |
  225. (+++) +-----------------------------------------------------------------------+
  226. (++) Hardware flow control
  227. (++) Receiver/transmitter modes
  228. (++) Over Sampling Method
  229. (++) One-Bit Sampling Method
  230. (+) For the asynchronous mode, the following advanced features can be configured as well:
  231. (++) TX and/or RX pin level inversion
  232. (++) data logical level inversion
  233. (++) RX and TX pins swap
  234. (++) RX overrun detection disabling
  235. (++) DMA disabling on RX error
  236. (++) MSB first on communication line
  237. (++) auto Baud rate detection
  238. [..]
  239. The HAL_UART_Init(), HAL_HalfDuplex_Init() and HAL_MultiProcessor_Init()
  240. API follow respectively the UART asynchronous, UART Half duplex and multiprocessor mode
  241. configuration procedures (details for the procedures are available in reference manual).
  242. @endverbatim
  243. * @{
  244. */
  245. /**
  246. * @brief Initialize the UART mode according to the specified
  247. * parameters in the UART_InitTypeDef and initialize the associated handle.
  248. * @param huart: UART handle.
  249. * @retval HAL status
  250. */
  251. HAL_StatusTypeDef HAL_UART_Init(UART_HandleTypeDef *huart)
  252. {
  253. /* Check the UART handle allocation */
  254. if(huart == NULL)
  255. {
  256. return HAL_ERROR;
  257. }
  258. if(huart->Init.HwFlowCtl != UART_HWCONTROL_NONE)
  259. {
  260. /* Check the parameters */
  261. assert_param(IS_UART_HWFLOW_INSTANCE(huart->Instance));
  262. }
  263. else
  264. {
  265. /* Check the parameters */
  266. assert_param(IS_UART_INSTANCE(huart->Instance));
  267. }
  268. if(huart->State == HAL_UART_STATE_RESET)
  269. {
  270. /* Allocate lock resource and initialize it */
  271. huart->Lock = HAL_UNLOCKED;
  272. /* Init the low level hardware : GPIO, CLOCK */
  273. HAL_UART_MspInit(huart);
  274. }
  275. huart->State = HAL_UART_STATE_BUSY;
  276. /* Disable the Peripheral */
  277. __HAL_UART_DISABLE(huart);
  278. /* Set the UART Communication parameters */
  279. if (UART_SetConfig(huart) == HAL_ERROR)
  280. {
  281. return HAL_ERROR;
  282. }
  283. if (huart->AdvancedInit.AdvFeatureInit != UART_ADVFEATURE_NO_INIT)
  284. {
  285. UART_AdvFeatureConfig(huart);
  286. }
  287. /* In asynchronous mode, the following bits must be kept cleared:
  288. - LINEN (if LIN is supported) and CLKEN bits in the USART_CR2 register,
  289. - SCEN (if Smartcard is supported), HDSEL and IREN (if IrDA is supported) bits in the USART_CR3 register. */
  290. #if defined (USART_CR2_LINEN)
  291. huart->Instance->CR2 &= ~(USART_CR2_LINEN | USART_CR2_CLKEN);
  292. #else
  293. huart->Instance->CR2 &= ~(USART_CR2_CLKEN);
  294. #endif
  295. #if defined (USART_CR3_SCEN)
  296. #if defined (USART_CR3_IREN)
  297. huart->Instance->CR3 &= ~(USART_CR3_SCEN | USART_CR3_HDSEL | USART_CR3_IREN);
  298. #else
  299. huart->Instance->CR3 &= ~(USART_CR3_SCEN | USART_CR3_HDSEL);
  300. #endif
  301. #else
  302. #if defined (USART_CR3_IREN)
  303. huart->Instance->CR3 &= ~(USART_CR3_HDSEL | USART_CR3_IREN);
  304. #else
  305. huart->Instance->CR3 &= ~(USART_CR3_HDSEL);
  306. #endif
  307. #endif
  308. /* Enable the Peripheral */
  309. __HAL_UART_ENABLE(huart);
  310. /* TEACK and/or REACK to check before moving huart->State to Ready */
  311. return (UART_CheckIdleState(huart));
  312. }
  313. /**
  314. * @brief Initialize the half-duplex mode according to the specified
  315. * parameters in the UART_InitTypeDef and creates the associated handle.
  316. * @param huart: UART handle.
  317. * @retval HAL status
  318. */
  319. HAL_StatusTypeDef HAL_HalfDuplex_Init(UART_HandleTypeDef *huart)
  320. {
  321. /* Check the UART handle allocation */
  322. if(huart == NULL)
  323. {
  324. return HAL_ERROR;
  325. }
  326. /* Check UART instance */
  327. assert_param(IS_UART_HALFDUPLEX_INSTANCE(huart->Instance));
  328. if(huart->State == HAL_UART_STATE_RESET)
  329. {
  330. /* Allocate lock resource and initialize it */
  331. huart->Lock = HAL_UNLOCKED;
  332. /* Init the low level hardware : GPIO, CLOCK */
  333. HAL_UART_MspInit(huart);
  334. }
  335. huart->State = HAL_UART_STATE_BUSY;
  336. /* Disable the Peripheral */
  337. __HAL_UART_DISABLE(huart);
  338. /* Set the UART Communication parameters */
  339. if (UART_SetConfig(huart) == HAL_ERROR)
  340. {
  341. return HAL_ERROR;
  342. }
  343. if (huart->AdvancedInit.AdvFeatureInit != UART_ADVFEATURE_NO_INIT)
  344. {
  345. UART_AdvFeatureConfig(huart);
  346. }
  347. /* In half-duplex mode, the following bits must be kept cleared:
  348. - LINEN (if LIN is supported) and CLKEN bits in the USART_CR2 register,
  349. - SCEN (if Smartcard is supported), and IREN (if IrDA is supported) bits in the USART_CR3 register. */
  350. #if defined (USART_CR2_LINEN)
  351. huart->Instance->CR2 &= ~(USART_CR2_LINEN | USART_CR2_CLKEN);
  352. #else
  353. huart->Instance->CR2 &= ~(USART_CR2_CLKEN);
  354. #endif
  355. #if defined (USART_CR3_SCEN)
  356. #if defined (USART_CR3_IREN)
  357. huart->Instance->CR3 &= ~(USART_CR3_SCEN | USART_CR3_IREN);
  358. #else
  359. huart->Instance->CR3 &= ~(USART_CR3_SCEN);
  360. #endif
  361. #else
  362. #if defined (USART_CR3_IREN)
  363. huart->Instance->CR3 &= ~(USART_CR3_IREN);
  364. #endif
  365. #endif
  366. /* Enable the Half-Duplex mode by setting the HDSEL bit in the CR3 register */
  367. huart->Instance->CR3 |= USART_CR3_HDSEL;
  368. /* Enable the Peripheral */
  369. __HAL_UART_ENABLE(huart);
  370. /* TEACK and/or REACK to check before moving huart->State to Ready */
  371. return (UART_CheckIdleState(huart));
  372. }
  373. /**
  374. * @brief Initialize the multiprocessor mode according to the specified
  375. * parameters in the UART_InitTypeDef and initialize the associated handle.
  376. * @param huart: UART handle.
  377. * @param Address: UART node address (4-, 6-, 7- or 8-bit long).
  378. * @param WakeUpMethod: specifies the UART wakeup method.
  379. * This parameter can be one of the following values:
  380. * @arg UART_WAKEUPMETHOD_IDLELINE: WakeUp by an idle line detection
  381. * @arg UART_WAKEUPMETHOD_ADDRESSMARK: WakeUp by an address mark
  382. * @note If the user resorts to idle line detection wake up, the Address parameter
  383. * is useless and ignored by the initialization function.
  384. * @note If the user resorts to address mark wake up, the address length detection
  385. * is configured by default to 4 bits only. For the UART to be able to
  386. * manage 6-, 7- or 8-bit long addresses detection, the API
  387. * HAL_MultiProcessorEx_AddressLength_Set() must be called after
  388. * HAL_MultiProcessor_Init().
  389. * @retval HAL status
  390. */
  391. HAL_StatusTypeDef HAL_MultiProcessor_Init(UART_HandleTypeDef *huart, uint8_t Address, uint32_t WakeUpMethod)
  392. {
  393. /* Check the UART handle allocation */
  394. if(huart == NULL)
  395. {
  396. return HAL_ERROR;
  397. }
  398. /* Check the wake up method parameter */
  399. assert_param(IS_UART_WAKEUPMETHOD(WakeUpMethod));
  400. if(huart->State == HAL_UART_STATE_RESET)
  401. {
  402. /* Allocate lock resource and initialize it */
  403. huart->Lock = HAL_UNLOCKED;
  404. /* Init the low level hardware : GPIO, CLOCK */
  405. HAL_UART_MspInit(huart);
  406. }
  407. huart->State = HAL_UART_STATE_BUSY;
  408. /* Disable the Peripheral */
  409. __HAL_UART_DISABLE(huart);
  410. /* Set the UART Communication parameters */
  411. if (UART_SetConfig(huart) == HAL_ERROR)
  412. {
  413. return HAL_ERROR;
  414. }
  415. if (huart->AdvancedInit.AdvFeatureInit != UART_ADVFEATURE_NO_INIT)
  416. {
  417. UART_AdvFeatureConfig(huart);
  418. }
  419. /* In multiprocessor mode, the following bits must be kept cleared:
  420. - LINEN (if LIN is supported) and CLKEN bits in the USART_CR2 register,
  421. - SCEN (if Smartcard is supported), HDSEL and IREN (if IrDA is supported) bits in the USART_CR3 register. */
  422. #if defined (USART_CR2_LINEN)
  423. huart->Instance->CR2 &= ~(USART_CR2_LINEN | USART_CR2_CLKEN);
  424. #else
  425. huart->Instance->CR2 &= ~(USART_CR2_CLKEN);
  426. #endif
  427. #if defined (USART_CR3_SCEN)
  428. #if defined (USART_CR3_IREN)
  429. huart->Instance->CR3 &= ~(USART_CR3_SCEN | USART_CR3_HDSEL | USART_CR3_IREN);
  430. #else
  431. huart->Instance->CR3 &= ~(USART_CR3_SCEN | USART_CR3_HDSEL);
  432. #endif
  433. #else
  434. #if defined (USART_CR3_IREN)
  435. huart->Instance->CR3 &= ~(USART_CR3_HDSEL | USART_CR3_IREN);
  436. #else
  437. huart->Instance->CR3 &= ~(USART_CR3_HDSEL);
  438. #endif
  439. #endif
  440. if (WakeUpMethod == UART_WAKEUPMETHOD_ADDRESSMARK)
  441. {
  442. /* If address mark wake up method is chosen, set the USART address node */
  443. MODIFY_REG(huart->Instance->CR2, USART_CR2_ADD, ((uint32_t)Address << UART_CR2_ADDRESS_LSB_POS));
  444. }
  445. /* Set the wake up method by setting the WAKE bit in the CR1 register */
  446. MODIFY_REG(huart->Instance->CR1, USART_CR1_WAKE, WakeUpMethod);
  447. /* Enable the Peripheral */
  448. __HAL_UART_ENABLE(huart);
  449. /* TEACK and/or REACK to check before moving huart->State to Ready */
  450. return (UART_CheckIdleState(huart));
  451. }
  452. /**
  453. * @brief DeInitialize the UART peripheral.
  454. * @param huart: UART handle.
  455. * @retval HAL status
  456. */
  457. HAL_StatusTypeDef HAL_UART_DeInit(UART_HandleTypeDef *huart)
  458. {
  459. /* Check the UART handle allocation */
  460. if(huart == NULL)
  461. {
  462. return HAL_ERROR;
  463. }
  464. /* Check the parameters */
  465. assert_param(IS_UART_INSTANCE(huart->Instance));
  466. huart->State = HAL_UART_STATE_BUSY;
  467. /* Disable the Peripheral */
  468. __HAL_UART_DISABLE(huart);
  469. huart->Instance->CR1 = 0x0;
  470. huart->Instance->CR2 = 0x0;
  471. huart->Instance->CR3 = 0x0;
  472. /* DeInit the low level hardware */
  473. HAL_UART_MspDeInit(huart);
  474. huart->ErrorCode = HAL_UART_ERROR_NONE;
  475. huart->State = HAL_UART_STATE_RESET;
  476. /* Process Unlock */
  477. __HAL_UNLOCK(huart);
  478. return HAL_OK;
  479. }
  480. /**
  481. * @brief Initialize the UART MSP.
  482. * @param huart: UART handle.
  483. * @retval None
  484. */
  485. __weak void HAL_UART_MspInit(UART_HandleTypeDef *huart)
  486. {
  487. /* Prevent unused argument(s) compilation warning */
  488. UNUSED(huart);
  489. /* NOTE : This function should not be modified, when the callback is needed,
  490. the HAL_UART_MspInit can be implemented in the user file
  491. */
  492. }
  493. /**
  494. * @brief DeInitialize the UART MSP.
  495. * @param huart: UART handle.
  496. * @retval None
  497. */
  498. __weak void HAL_UART_MspDeInit(UART_HandleTypeDef *huart)
  499. {
  500. /* Prevent unused argument(s) compilation warning */
  501. UNUSED(huart);
  502. /* NOTE : This function should not be modified, when the callback is needed,
  503. the HAL_UART_MspDeInit can be implemented in the user file
  504. */
  505. }
  506. /**
  507. * @}
  508. */
  509. /** @defgroup UART_Exported_Functions_Group2 IO operation functions
  510. * @brief UART Transmit/Receive functions
  511. *
  512. @verbatim
  513. ===============================================================================
  514. ##### IO operation functions #####
  515. ===============================================================================
  516. This subsection provides a set of functions allowing to manage the UART asynchronous
  517. and Half duplex data transfers.
  518. (#) There are two mode of transfer:
  519. (+) Blocking mode: The communication is performed in polling mode.
  520. The HAL status of all data processing is returned by the same function
  521. after finishing transfer.
  522. (+) No-Blocking mode: The communication is performed using Interrupts
  523. or DMA, These API's return the HAL status.
  524. The end of the data processing will be indicated through the
  525. dedicated UART IRQ when using Interrupt mode or the DMA IRQ when
  526. using DMA mode.
  527. The HAL_UART_TxCpltCallback(), HAL_UART_RxCpltCallback() user callbacks
  528. will be executed respectively at the end of the transmit or Receive process
  529. The HAL_UART_ErrorCallback()user callback will be executed when a communication error is detected
  530. (#) Blocking mode API's are :
  531. (+) HAL_UART_Transmit()
  532. (+) HAL_UART_Receive()
  533. (#) Non-Blocking mode API's with Interrupt are :
  534. (+) HAL_UART_Transmit_IT()
  535. (+) HAL_UART_Receive_IT()
  536. (+) HAL_UART_IRQHandler()
  537. (#) No-Blocking mode API's with DMA are :
  538. (+) HAL_UART_Transmit_DMA()
  539. (+) HAL_UART_Receive_DMA()
  540. (+) HAL_UART_DMAPause()
  541. (+) HAL_UART_DMAResume()
  542. (+) HAL_UART_DMAStop()
  543. (#) A set of Transfer Complete Callbacks are provided in No_Blocking mode:
  544. (+) HAL_UART_TxHalfCpltCallback()
  545. (+) HAL_UART_TxCpltCallback()
  546. (+) HAL_UART_RxHalfCpltCallback()
  547. (+) HAL_UART_RxCpltCallback()
  548. (+) HAL_UART_ErrorCallback()
  549. -@- In the Half duplex communication, it is forbidden to run the transmit
  550. and receive process in parallel, the UART state HAL_UART_STATE_BUSY_TX_RX can't be useful.
  551. @endverbatim
  552. * @{
  553. */
  554. /**
  555. * @brief Send an amount of data in blocking mode.
  556. * @param huart: UART handle.
  557. * @param pData: Pointer to data buffer.
  558. * @param Size: Amount of data to be sent.
  559. * @param Timeout: Timeout duration.
  560. * @retval HAL status
  561. */
  562. HAL_StatusTypeDef HAL_UART_Transmit(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint32_t Timeout)
  563. {
  564. uint16_t* tmp;
  565. if((huart->State == HAL_UART_STATE_READY) || (huart->State == HAL_UART_STATE_BUSY_RX))
  566. {
  567. if((pData == NULL ) || (Size == 0))
  568. {
  569. return HAL_ERROR;
  570. }
  571. /* Process Locked */
  572. __HAL_LOCK(huart);
  573. huart->ErrorCode = HAL_UART_ERROR_NONE;
  574. /* Check if a non-blocking receive process is ongoing or not */
  575. if(huart->State == HAL_UART_STATE_BUSY_RX)
  576. {
  577. huart->State = HAL_UART_STATE_BUSY_TX_RX;
  578. }
  579. else
  580. {
  581. huart->State = HAL_UART_STATE_BUSY_TX;
  582. }
  583. huart->TxXferSize = Size;
  584. huart->TxXferCount = Size;
  585. while(huart->TxXferCount > 0)
  586. {
  587. huart->TxXferCount--;
  588. if(UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_TXE, RESET, Timeout) != HAL_OK)
  589. {
  590. return HAL_TIMEOUT;
  591. }
  592. if ((huart->Init.WordLength == UART_WORDLENGTH_9B) && (huart->Init.Parity == UART_PARITY_NONE))
  593. {
  594. tmp = (uint16_t*) pData;
  595. huart->Instance->TDR = (*tmp & (uint16_t)0x01FF);
  596. pData += 2;
  597. }
  598. else
  599. {
  600. huart->Instance->TDR = (*pData++ & (uint8_t)0xFF);
  601. }
  602. }
  603. if(UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_TC, RESET, Timeout) != HAL_OK)
  604. {
  605. return HAL_TIMEOUT;
  606. }
  607. /* Check if a non-blocking receive Process is ongoing or not */
  608. if(huart->State == HAL_UART_STATE_BUSY_TX_RX)
  609. {
  610. huart->State = HAL_UART_STATE_BUSY_RX;
  611. }
  612. else
  613. {
  614. huart->State = HAL_UART_STATE_READY;
  615. }
  616. /* Process Unlocked */
  617. __HAL_UNLOCK(huart);
  618. return HAL_OK;
  619. }
  620. else
  621. {
  622. return HAL_BUSY;
  623. }
  624. }
  625. /**
  626. * @brief Receive an amount of data in blocking mode.
  627. * @param huart: UART handle.
  628. * @param pData: pointer to data buffer.
  629. * @param Size: amount of data to be received.
  630. * @param Timeout: Timeout duration.
  631. * @retval HAL status
  632. */
  633. HAL_StatusTypeDef HAL_UART_Receive(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint32_t Timeout)
  634. {
  635. uint16_t* tmp;
  636. uint16_t uhMask;
  637. if((huart->State == HAL_UART_STATE_READY) || (huart->State == HAL_UART_STATE_BUSY_TX))
  638. {
  639. if((pData == NULL ) || (Size == 0))
  640. {
  641. return HAL_ERROR;
  642. }
  643. /* Process Locked */
  644. __HAL_LOCK(huart);
  645. huart->ErrorCode = HAL_UART_ERROR_NONE;
  646. /* Check if a non-blocking transmit process is ongoing or not */
  647. if(huart->State == HAL_UART_STATE_BUSY_TX)
  648. {
  649. huart->State = HAL_UART_STATE_BUSY_TX_RX;
  650. }
  651. else
  652. {
  653. huart->State = HAL_UART_STATE_BUSY_RX;
  654. }
  655. huart->RxXferSize = Size;
  656. huart->RxXferCount = Size;
  657. /* Computation of UART mask to apply to RDR register */
  658. UART_MASK_COMPUTATION(huart);
  659. uhMask = huart->Mask;
  660. /* as long as data have to be received */
  661. while(huart->RxXferCount > 0)
  662. {
  663. huart->RxXferCount--;
  664. if(UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_RXNE, RESET, Timeout) != HAL_OK)
  665. {
  666. return HAL_TIMEOUT;
  667. }
  668. if ((huart->Init.WordLength == UART_WORDLENGTH_9B) && (huart->Init.Parity == UART_PARITY_NONE))
  669. {
  670. tmp = (uint16_t*) pData ;
  671. *tmp = (uint16_t)(huart->Instance->RDR & uhMask);
  672. pData +=2;
  673. }
  674. else
  675. {
  676. *pData++ = (uint8_t)(huart->Instance->RDR & (uint8_t)uhMask);
  677. }
  678. }
  679. /* Check if a non-blocking transmit Process is ongoing or not */
  680. if(huart->State == HAL_UART_STATE_BUSY_TX_RX)
  681. {
  682. huart->State = HAL_UART_STATE_BUSY_TX;
  683. }
  684. else
  685. {
  686. huart->State = HAL_UART_STATE_READY;
  687. }
  688. /* Process Unlocked */
  689. __HAL_UNLOCK(huart);
  690. return HAL_OK;
  691. }
  692. else
  693. {
  694. return HAL_BUSY;
  695. }
  696. }
  697. /**
  698. * @brief Send an amount of data in interrupt mode.
  699. * @param huart: UART handle.
  700. * @param pData: pointer to data buffer.
  701. * @param Size: amount of data to be sent.
  702. * @retval HAL status
  703. */
  704. HAL_StatusTypeDef HAL_UART_Transmit_IT(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
  705. {
  706. if((huart->State == HAL_UART_STATE_READY) || (huart->State == HAL_UART_STATE_BUSY_RX))
  707. {
  708. if((pData == NULL ) || (Size == 0))
  709. {
  710. return HAL_ERROR;
  711. }
  712. /* Process Locked */
  713. __HAL_LOCK(huart);
  714. huart->pTxBuffPtr = pData;
  715. huart->TxXferSize = Size;
  716. huart->TxXferCount = Size;
  717. huart->ErrorCode = HAL_UART_ERROR_NONE;
  718. /* Check if a receive process is ongoing or not */
  719. if(huart->State == HAL_UART_STATE_BUSY_RX)
  720. {
  721. huart->State = HAL_UART_STATE_BUSY_TX_RX;
  722. }
  723. else
  724. {
  725. huart->State = HAL_UART_STATE_BUSY_TX;
  726. }
  727. /* Process Unlocked */
  728. __HAL_UNLOCK(huart);
  729. /* Enable the UART Transmit Data Register Empty Interrupt */
  730. __HAL_UART_ENABLE_IT(huart, UART_IT_TXE);
  731. return HAL_OK;
  732. }
  733. else
  734. {
  735. return HAL_BUSY;
  736. }
  737. }
  738. /**
  739. * @brief Receive an amount of data in interrupt mode.
  740. * @param huart: UART handle.
  741. * @param pData: pointer to data buffer.
  742. * @param Size: amount of data to be received.
  743. * @retval HAL status
  744. */
  745. HAL_StatusTypeDef HAL_UART_Receive_IT(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
  746. {
  747. if((huart->State == HAL_UART_STATE_READY) || (huart->State == HAL_UART_STATE_BUSY_TX))
  748. {
  749. if((pData == NULL ) || (Size == 0))
  750. {
  751. return HAL_ERROR;
  752. }
  753. /* Process Locked */
  754. __HAL_LOCK(huart);
  755. huart->pRxBuffPtr = pData;
  756. huart->RxXferSize = Size;
  757. huart->RxXferCount = Size;
  758. /* Computation of UART mask to apply to RDR register */
  759. UART_MASK_COMPUTATION(huart);
  760. huart->ErrorCode = HAL_UART_ERROR_NONE;
  761. /* Check if a transmit process is ongoing or not */
  762. if(huart->State == HAL_UART_STATE_BUSY_TX)
  763. {
  764. huart->State = HAL_UART_STATE_BUSY_TX_RX;
  765. }
  766. else
  767. {
  768. huart->State = HAL_UART_STATE_BUSY_RX;
  769. }
  770. /* Enable the UART Parity Error Interrupt */
  771. __HAL_UART_ENABLE_IT(huart, UART_IT_PE);
  772. /* Enable the UART Error Interrupt: (Frame error, noise error, overrun error) */
  773. __HAL_UART_ENABLE_IT(huart, UART_IT_ERR);
  774. /* Process Unlocked */
  775. __HAL_UNLOCK(huart);
  776. /* Enable the UART Data Register not empty Interrupt */
  777. __HAL_UART_ENABLE_IT(huart, UART_IT_RXNE);
  778. return HAL_OK;
  779. }
  780. else
  781. {
  782. return HAL_BUSY;
  783. }
  784. }
  785. /**
  786. * @brief Send an amount of data in DMA mode.
  787. * @param huart: UART handle.
  788. * @param pData: pointer to data buffer.
  789. * @param Size: amount of data to be sent.
  790. * @retval HAL status
  791. */
  792. HAL_StatusTypeDef HAL_UART_Transmit_DMA(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
  793. {
  794. uint32_t *tmp;
  795. if((huart->State == HAL_UART_STATE_READY) || (huart->State == HAL_UART_STATE_BUSY_RX))
  796. {
  797. if((pData == NULL ) || (Size == 0))
  798. {
  799. return HAL_ERROR;
  800. }
  801. /* Process Locked */
  802. __HAL_LOCK(huart);
  803. huart->pTxBuffPtr = pData;
  804. huart->TxXferSize = Size;
  805. huart->TxXferCount = Size;
  806. huart->ErrorCode = HAL_UART_ERROR_NONE;
  807. /* Check if a receive process is ongoing or not */
  808. if(huart->State == HAL_UART_STATE_BUSY_RX)
  809. {
  810. huart->State = HAL_UART_STATE_BUSY_TX_RX;
  811. }
  812. else
  813. {
  814. huart->State = HAL_UART_STATE_BUSY_TX;
  815. }
  816. /* Set the UART DMA transfer complete callback */
  817. huart->hdmatx->XferCpltCallback = UART_DMATransmitCplt;
  818. /* Set the UART DMA Half transfer complete callback */
  819. huart->hdmatx->XferHalfCpltCallback = UART_DMATxHalfCplt;
  820. /* Set the DMA error callback */
  821. huart->hdmatx->XferErrorCallback = UART_DMAError;
  822. /* Enable the UART transmit DMA channel */
  823. tmp = (uint32_t*)&pData;
  824. HAL_DMA_Start_IT(huart->hdmatx, *(uint32_t*)tmp, (uint32_t)&huart->Instance->TDR, Size);
  825. /* Clear the TC flag in the ICR register */
  826. __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_TCF);
  827. /* Enable the DMA transfer for transmit request by setting the DMAT bit
  828. in the UART CR3 register */
  829. huart->Instance->CR3 |= USART_CR3_DMAT;
  830. /* Process Unlocked */
  831. __HAL_UNLOCK(huart);
  832. return HAL_OK;
  833. }
  834. else
  835. {
  836. return HAL_BUSY;
  837. }
  838. }
  839. /**
  840. * @brief Receive an amount of data in DMA mode.
  841. * @param huart: UART handle.
  842. * @param pData: pointer to data buffer.
  843. * @param Size: amount of data to be received.
  844. * @note When the UART parity is enabled (PCE = 1), the received data contain
  845. * the parity bit (MSB position).
  846. * @retval HAL status
  847. */
  848. HAL_StatusTypeDef HAL_UART_Receive_DMA(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
  849. {
  850. uint32_t *tmp;
  851. if((huart->State == HAL_UART_STATE_READY) || (huart->State == HAL_UART_STATE_BUSY_TX))
  852. {
  853. if((pData == NULL ) || (Size == 0))
  854. {
  855. return HAL_ERROR;
  856. }
  857. /* Process Locked */
  858. __HAL_LOCK(huart);
  859. huart->pRxBuffPtr = pData;
  860. huart->RxXferSize = Size;
  861. huart->ErrorCode = HAL_UART_ERROR_NONE;
  862. /* Check if a transmit process is ongoing or not */
  863. if(huart->State == HAL_UART_STATE_BUSY_TX)
  864. {
  865. huart->State = HAL_UART_STATE_BUSY_TX_RX;
  866. }
  867. else
  868. {
  869. huart->State = HAL_UART_STATE_BUSY_RX;
  870. }
  871. /* Set the UART DMA transfer complete callback */
  872. huart->hdmarx->XferCpltCallback = UART_DMAReceiveCplt;
  873. /* Set the UART DMA Half transfer complete callback */
  874. huart->hdmarx->XferHalfCpltCallback = UART_DMARxHalfCplt;
  875. /* Set the DMA error callback */
  876. huart->hdmarx->XferErrorCallback = UART_DMAError;
  877. /* Enable the DMA channel */
  878. tmp = (uint32_t*)&pData;
  879. HAL_DMA_Start_IT(huart->hdmarx, (uint32_t)&huart->Instance->RDR, *(uint32_t*)tmp, Size);
  880. /* Enable the DMA transfer for the receiver request by setting the DMAR bit
  881. in the UART CR3 register */
  882. huart->Instance->CR3 |= USART_CR3_DMAR;
  883. /* Process Unlocked */
  884. __HAL_UNLOCK(huart);
  885. return HAL_OK;
  886. }
  887. else
  888. {
  889. return HAL_BUSY;
  890. }
  891. }
  892. /**
  893. * @brief Pause the DMA Transfer.
  894. * @param huart: UART handle.
  895. * @retval HAL status
  896. */
  897. HAL_StatusTypeDef HAL_UART_DMAPause(UART_HandleTypeDef *huart)
  898. {
  899. /* Process Locked */
  900. __HAL_LOCK(huart);
  901. if(huart->State == HAL_UART_STATE_BUSY_TX)
  902. {
  903. /* Disable the UART DMA Tx request */
  904. huart->Instance->CR3 &= (uint32_t)(~USART_CR3_DMAT);
  905. }
  906. else if(huart->State == HAL_UART_STATE_BUSY_RX)
  907. {
  908. /* Disable the UART DMA Rx request */
  909. huart->Instance->CR3 &= (uint32_t)(~USART_CR3_DMAR);
  910. }
  911. else if(huart->State == HAL_UART_STATE_BUSY_TX_RX)
  912. {
  913. /* Disable the UART DMA Tx request */
  914. huart->Instance->CR3 &= (uint32_t)(~USART_CR3_DMAT);
  915. /* Disable the UART DMA Rx request */
  916. huart->Instance->CR3 &= (uint32_t)(~USART_CR3_DMAR);
  917. }
  918. /* Process Unlocked */
  919. __HAL_UNLOCK(huart);
  920. return HAL_OK;
  921. }
  922. /**
  923. * @brief Resume the DMA Transfer.
  924. * @param huart: UART handle.
  925. * @retval HAL status
  926. */
  927. HAL_StatusTypeDef HAL_UART_DMAResume(UART_HandleTypeDef *huart)
  928. {
  929. /* Process Locked */
  930. __HAL_LOCK(huart);
  931. if(huart->State == HAL_UART_STATE_BUSY_TX)
  932. {
  933. /* Enable the UART DMA Tx request */
  934. huart->Instance->CR3 |= USART_CR3_DMAT;
  935. }
  936. else if(huart->State == HAL_UART_STATE_BUSY_RX)
  937. {
  938. /* Clear the Overrun flag before resumming the Rx transfer*/
  939. __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_OREF);
  940. /* Enable the UART DMA Rx request */
  941. huart->Instance->CR3 |= USART_CR3_DMAR;
  942. }
  943. else if(huart->State == HAL_UART_STATE_BUSY_TX_RX)
  944. {
  945. /* Clear the Overrun flag before resumming the Rx transfer*/
  946. __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_OREF);
  947. /* Enable the UART DMA Rx request before the DMA Tx request */
  948. huart->Instance->CR3 |= USART_CR3_DMAR;
  949. /* Enable the UART DMA Tx request */
  950. huart->Instance->CR3 |= USART_CR3_DMAT;
  951. }
  952. /* Process Unlocked */
  953. __HAL_UNLOCK(huart);
  954. return HAL_OK;
  955. }
  956. /**
  957. * @brief Stop the DMA Transfer.
  958. * @param huart: UART handle.
  959. * @retval HAL status
  960. */
  961. HAL_StatusTypeDef HAL_UART_DMAStop(UART_HandleTypeDef *huart)
  962. {
  963. /* The Lock is not implemented on this API to allow the user application
  964. to call the HAL UART API under callbacks HAL_UART_TxCpltCallback() / HAL_UART_RxCpltCallback() /
  965. HAL_UART_TxHalfCpltCallback() / HAL_UART_RxHalfCpltCallback ():
  966. indeed, when HAL_DMA_Abort() API is called, the DMA TX/RX Transfer or Half Transfer complete interrupt is
  967. generated if the DMA transfer interruption occurs at the middle or at the end of the stream
  968. and the corresponding call back is executed.
  969. */
  970. /* Disable the UART Tx/Rx DMA requests */
  971. huart->Instance->CR3 &= ~USART_CR3_DMAT;
  972. huart->Instance->CR3 &= ~USART_CR3_DMAR;
  973. /* Abort the UART DMA tx channel */
  974. if(huart->hdmatx != NULL)
  975. {
  976. HAL_DMA_Abort(huart->hdmatx);
  977. }
  978. /* Abort the UART DMA rx channel */
  979. if(huart->hdmarx != NULL)
  980. {
  981. HAL_DMA_Abort(huart->hdmarx);
  982. }
  983. huart->State = HAL_UART_STATE_READY;
  984. return HAL_OK;
  985. }
  986. /**
  987. * @brief Tx Transfer completed callback.
  988. * @param huart: UART handle.
  989. * @retval None
  990. */
  991. __weak void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
  992. {
  993. /* Prevent unused argument(s) compilation warning */
  994. UNUSED(huart);
  995. /* NOTE : This function should not be modified, when the callback is needed,
  996. the HAL_UART_TxCpltCallback can be implemented in the user file.
  997. */
  998. }
  999. /**
  1000. * @brief Tx Half Transfer completed callback.
  1001. * @param huart: UART handle.
  1002. * @retval None
  1003. */
  1004. __weak void HAL_UART_TxHalfCpltCallback(UART_HandleTypeDef *huart)
  1005. {
  1006. /* Prevent unused argument(s) compilation warning */
  1007. UNUSED(huart);
  1008. /* NOTE: This function should not be modified, when the callback is needed,
  1009. the HAL_UART_TxHalfCpltCallback can be implemented in the user file.
  1010. */
  1011. }
  1012. /**
  1013. * @brief Rx Transfer completed callback.
  1014. * @param huart: UART handle.
  1015. * @retval None
  1016. */
  1017. __weak void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
  1018. {
  1019. /* Prevent unused argument(s) compilation warning */
  1020. UNUSED(huart);
  1021. /* NOTE : This function should not be modified, when the callback is needed,
  1022. the HAL_UART_RxCpltCallback can be implemented in the user file.
  1023. */
  1024. }
  1025. /**
  1026. * @brief Rx Half Transfer completed callback.
  1027. * @param huart: UART handle.
  1028. * @retval None
  1029. */
  1030. __weak void HAL_UART_RxHalfCpltCallback(UART_HandleTypeDef *huart)
  1031. {
  1032. /* Prevent unused argument(s) compilation warning */
  1033. UNUSED(huart);
  1034. /* NOTE: This function should not be modified, when the callback is needed,
  1035. the HAL_UART_RxHalfCpltCallback can be implemented in the user file.
  1036. */
  1037. }
  1038. /**
  1039. * @brief UART error callback.
  1040. * @param huart: UART handle.
  1041. * @retval None
  1042. */
  1043. __weak void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart)
  1044. {
  1045. /* Prevent unused argument(s) compilation warning */
  1046. UNUSED(huart);
  1047. /* NOTE : This function should not be modified, when the callback is needed,
  1048. the HAL_UART_ErrorCallback can be implemented in the user file.
  1049. */
  1050. }
  1051. /**
  1052. * @}
  1053. */
  1054. /** @defgroup UART_Exported_Functions_Group3 Peripheral Control functions
  1055. * @brief UART control functions
  1056. *
  1057. @verbatim
  1058. ===============================================================================
  1059. ##### Peripheral Control functions #####
  1060. ===============================================================================
  1061. [..]
  1062. This subsection provides a set of functions allowing to control the UART.
  1063. (+) HAL_MultiProcessor_EnableMuteMode() API enables mute mode
  1064. (+) HAL_MultiProcessor_DisableMuteMode() API disables mute mode
  1065. (+) HAL_MultiProcessor_EnterMuteMode() API enters mute mode
  1066. (+) HAL_HalfDuplex_EnableTransmitter() API disables receiver and enables transmitter
  1067. (+) HAL_HalfDuplex_EnableReceiver() API disables transmitter and enables receiver
  1068. @endverbatim
  1069. * @{
  1070. */
  1071. /**
  1072. * @brief Enable UART in mute mode (does not mean UART enters mute mode;
  1073. * to enter mute mode, HAL_MultiProcessor_EnterMuteMode() API must be called).
  1074. * @param huart: UART handle.
  1075. * @retval HAL status
  1076. */
  1077. HAL_StatusTypeDef HAL_MultiProcessor_EnableMuteMode(UART_HandleTypeDef *huart)
  1078. {
  1079. /* Process Locked */
  1080. __HAL_LOCK(huart);
  1081. huart->State = HAL_UART_STATE_BUSY;
  1082. /* Enable USART mute mode by setting the MME bit in the CR1 register */
  1083. huart->Instance->CR1 |= USART_CR1_MME;
  1084. huart->State = HAL_UART_STATE_READY;
  1085. return (UART_CheckIdleState(huart));
  1086. }
  1087. /**
  1088. * @brief Disable UART mute mode (does not mean the UART actually exits mute mode
  1089. * as it may not have been in mute mode at this very moment).
  1090. * @param huart: UART handle.
  1091. * @retval HAL status
  1092. */
  1093. HAL_StatusTypeDef HAL_MultiProcessor_DisableMuteMode(UART_HandleTypeDef *huart)
  1094. {
  1095. /* Process Locked */
  1096. __HAL_LOCK(huart);
  1097. huart->State = HAL_UART_STATE_BUSY;
  1098. /* Disable USART mute mode by clearing the MME bit in the CR1 register */
  1099. huart->Instance->CR1 &= ~(USART_CR1_MME);
  1100. huart->State = HAL_UART_STATE_READY;
  1101. return (UART_CheckIdleState(huart));
  1102. }
  1103. /**
  1104. * @brief Enter UART mute mode (means UART actually enters mute mode).
  1105. * @note To exit from mute mode, HAL_MultiProcessor_DisableMuteMode() API must be called.
  1106. * @param huart: UART handle.
  1107. * @retval None
  1108. */
  1109. void HAL_MultiProcessor_EnterMuteMode(UART_HandleTypeDef *huart)
  1110. {
  1111. __HAL_UART_SEND_REQ(huart, UART_MUTE_MODE_REQUEST);
  1112. }
  1113. /**
  1114. * @brief Enable the UART transmitter and disable the UART receiver.
  1115. * @param huart: UART handle.
  1116. * @retval HAL status
  1117. */
  1118. HAL_StatusTypeDef HAL_HalfDuplex_EnableTransmitter(UART_HandleTypeDef *huart)
  1119. {
  1120. /* Process Locked */
  1121. __HAL_LOCK(huart);
  1122. huart->State = HAL_UART_STATE_BUSY;
  1123. /* Clear TE and RE bits */
  1124. CLEAR_BIT(huart->Instance->CR1, (USART_CR1_TE | USART_CR1_RE));
  1125. /* Enable the USART's transmit interface by setting the TE bit in the USART CR1 register */
  1126. SET_BIT(huart->Instance->CR1, USART_CR1_TE);
  1127. huart->State = HAL_UART_STATE_READY;
  1128. /* Process Unlocked */
  1129. __HAL_UNLOCK(huart);
  1130. return HAL_OK;
  1131. }
  1132. /**
  1133. * @brief Enable the UART receiver and disable the UART transmitter.
  1134. * @param huart: UART handle.
  1135. * @retval HAL status.
  1136. */
  1137. HAL_StatusTypeDef HAL_HalfDuplex_EnableReceiver(UART_HandleTypeDef *huart)
  1138. {
  1139. /* Process Locked */
  1140. __HAL_LOCK(huart);
  1141. huart->State = HAL_UART_STATE_BUSY;
  1142. /* Clear TE and RE bits */
  1143. CLEAR_BIT(huart->Instance->CR1, (USART_CR1_TE | USART_CR1_RE));
  1144. /* Enable the USART's receive interface by setting the RE bit in the USART CR1 register */
  1145. SET_BIT(huart->Instance->CR1, USART_CR1_RE);
  1146. huart->State = HAL_UART_STATE_READY;
  1147. /* Process Unlocked */
  1148. __HAL_UNLOCK(huart);
  1149. return HAL_OK;
  1150. }
  1151. /**
  1152. * @}
  1153. */
  1154. /** @defgroup UART_Exported_Functions_Group4 Peripheral State and Error functions
  1155. * @brief UART Peripheral State functions
  1156. *
  1157. @verbatim
  1158. ==============================================================================
  1159. ##### Peripheral State and Error functions #####
  1160. ==============================================================================
  1161. [..]
  1162. This subsection provides functions allowing to :
  1163. (+) Return the UART handle state.
  1164. (+) Return the UART handle error code
  1165. @endverbatim
  1166. * @{
  1167. */
  1168. /**
  1169. * @brief Return the UART handle state.
  1170. * @param huart : pointer to a UART_HandleTypeDef structure that contains
  1171. * the configuration information for the specified UART.
  1172. * @retval HAL state
  1173. */
  1174. HAL_UART_StateTypeDef HAL_UART_GetState(UART_HandleTypeDef *huart)
  1175. {
  1176. return huart->State;
  1177. }
  1178. /**
  1179. * @brief Return the UART handle error code.
  1180. * @param huart : pointer to a UART_HandleTypeDef structure that contains
  1181. * the configuration information for the specified UART.
  1182. * @retval UART Error Code
  1183. */
  1184. uint32_t HAL_UART_GetError(UART_HandleTypeDef *huart)
  1185. {
  1186. return huart->ErrorCode;
  1187. }
  1188. /**
  1189. * @}
  1190. */
  1191. /**
  1192. * @}
  1193. */
  1194. /** @defgroup UART_Private_Functions UART Private Functions
  1195. * @{
  1196. */
  1197. /**
  1198. * @brief Configure the UART peripheral.
  1199. * @param huart: UART handle.
  1200. * @retval HAL status
  1201. */
  1202. HAL_StatusTypeDef UART_SetConfig(UART_HandleTypeDef *huart)
  1203. {
  1204. uint32_t tmpreg = 0x00000000;
  1205. UART_ClockSourceTypeDef clocksource = UART_CLOCKSOURCE_UNDEFINED;
  1206. uint16_t brrtemp = 0x0000;
  1207. uint16_t usartdiv = 0x0000;
  1208. HAL_StatusTypeDef ret = HAL_OK;
  1209. /* Check the parameters */
  1210. assert_param(IS_UART_BAUDRATE(huart->Init.BaudRate));
  1211. assert_param(IS_UART_WORD_LENGTH(huart->Init.WordLength));
  1212. assert_param(IS_UART_STOPBITS(huart->Init.StopBits));
  1213. assert_param(IS_UART_PARITY(huart->Init.Parity));
  1214. assert_param(IS_UART_MODE(huart->Init.Mode));
  1215. assert_param(IS_UART_HARDWARE_FLOW_CONTROL(huart->Init.HwFlowCtl));
  1216. assert_param(IS_UART_ONE_BIT_SAMPLE(huart->Init.OneBitSampling));
  1217. assert_param(IS_UART_OVERSAMPLING(huart->Init.OverSampling));
  1218. /*-------------------------- USART CR1 Configuration -----------------------*/
  1219. /* Clear M, PCE, PS, TE, RE and OVER8 bits and configure
  1220. * the UART Word Length, Parity, Mode and oversampling:
  1221. * set the M bits according to huart->Init.WordLength value
  1222. * set PCE and PS bits according to huart->Init.Parity value
  1223. * set TE and RE bits according to huart->Init.Mode value
  1224. * set OVER8 bit according to huart->Init.OverSampling value */
  1225. tmpreg = (uint32_t)huart->Init.WordLength | huart->Init.Parity | huart->Init.Mode | huart->Init.OverSampling ;
  1226. MODIFY_REG(huart->Instance->CR1, UART_CR1_FIELDS, tmpreg);
  1227. /*-------------------------- USART CR2 Configuration -----------------------*/
  1228. /* Configure the UART Stop Bits: Set STOP[13:12] bits according
  1229. * to huart->Init.StopBits value */
  1230. MODIFY_REG(huart->Instance->CR2, USART_CR2_STOP, huart->Init.StopBits);
  1231. /*-------------------------- USART CR3 Configuration -----------------------*/
  1232. /* Configure
  1233. * - UART HardWare Flow Control: set CTSE and RTSE bits according
  1234. * to huart->Init.HwFlowCtl value
  1235. * - one-bit sampling method versus three samples' majority rule according
  1236. * to huart->Init.OneBitSampling */
  1237. tmpreg = (uint32_t)huart->Init.HwFlowCtl | huart->Init.OneBitSampling ;
  1238. MODIFY_REG(huart->Instance->CR3, (USART_CR3_RTSE | USART_CR3_CTSE | USART_CR3_ONEBIT), tmpreg);
  1239. /*-------------------------- USART BRR Configuration -----------------------*/
  1240. UART_GETCLOCKSOURCE(huart, clocksource);
  1241. /* Check UART Over Sampling to set Baud Rate Register */
  1242. if (huart->Init.OverSampling == UART_OVERSAMPLING_8)
  1243. {
  1244. switch (clocksource)
  1245. {
  1246. case UART_CLOCKSOURCE_PCLK1:
  1247. usartdiv = (uint16_t)(UART_DIV_SAMPLING8(HAL_RCC_GetPCLK1Freq(), huart->Init.BaudRate));
  1248. break;
  1249. case UART_CLOCKSOURCE_HSI:
  1250. usartdiv = (uint16_t)(UART_DIV_SAMPLING8(HSI_VALUE, huart->Init.BaudRate));
  1251. break;
  1252. case UART_CLOCKSOURCE_SYSCLK:
  1253. usartdiv = (uint16_t)(UART_DIV_SAMPLING8(HAL_RCC_GetSysClockFreq(), huart->Init.BaudRate));
  1254. break;
  1255. case UART_CLOCKSOURCE_LSE:
  1256. usartdiv = (uint16_t)(UART_DIV_SAMPLING8(LSE_VALUE, huart->Init.BaudRate));
  1257. break;
  1258. case UART_CLOCKSOURCE_UNDEFINED:
  1259. default:
  1260. ret = HAL_ERROR;
  1261. break;
  1262. }
  1263. brrtemp = usartdiv & 0xFFF0;
  1264. brrtemp |= (uint16_t)((usartdiv & (uint16_t)0x000F) >> 1U);
  1265. huart->Instance->BRR = brrtemp;
  1266. }
  1267. else
  1268. {
  1269. switch (clocksource)
  1270. {
  1271. case UART_CLOCKSOURCE_PCLK1:
  1272. huart->Instance->BRR = (uint16_t)(UART_DIV_SAMPLING16(HAL_RCC_GetPCLK1Freq(), huart->Init.BaudRate));
  1273. break;
  1274. case UART_CLOCKSOURCE_HSI:
  1275. huart->Instance->BRR = (uint16_t)(UART_DIV_SAMPLING16(HSI_VALUE, huart->Init.BaudRate));
  1276. break;
  1277. case UART_CLOCKSOURCE_SYSCLK:
  1278. huart->Instance->BRR = (uint16_t)(UART_DIV_SAMPLING16(HAL_RCC_GetSysClockFreq(), huart->Init.BaudRate));
  1279. break;
  1280. case UART_CLOCKSOURCE_LSE:
  1281. huart->Instance->BRR = (uint16_t)(UART_DIV_SAMPLING16(LSE_VALUE, huart->Init.BaudRate));
  1282. break;
  1283. case UART_CLOCKSOURCE_UNDEFINED:
  1284. default:
  1285. ret = HAL_ERROR;
  1286. break;
  1287. }
  1288. }
  1289. return ret;
  1290. }
  1291. /**
  1292. * @brief Configure the UART peripheral advanced features.
  1293. * @param huart: UART handle.
  1294. * @retval None
  1295. */
  1296. void UART_AdvFeatureConfig(UART_HandleTypeDef *huart)
  1297. {
  1298. /* Check whether the set of advanced features to configure is properly set */
  1299. assert_param(IS_UART_ADVFEATURE_INIT(huart->AdvancedInit.AdvFeatureInit));
  1300. /* if required, configure TX pin active level inversion */
  1301. if(HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_TXINVERT_INIT))
  1302. {
  1303. assert_param(IS_UART_ADVFEATURE_TXINV(huart->AdvancedInit.TxPinLevelInvert));
  1304. MODIFY_REG(huart->Instance->CR2, USART_CR2_TXINV, huart->AdvancedInit.TxPinLevelInvert);
  1305. }
  1306. /* if required, configure RX pin active level inversion */
  1307. if(HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_RXINVERT_INIT))
  1308. {
  1309. assert_param(IS_UART_ADVFEATURE_RXINV(huart->AdvancedInit.RxPinLevelInvert));
  1310. MODIFY_REG(huart->Instance->CR2, USART_CR2_RXINV, huart->AdvancedInit.RxPinLevelInvert);
  1311. }
  1312. /* if required, configure data inversion */
  1313. if(HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_DATAINVERT_INIT))
  1314. {
  1315. assert_param(IS_UART_ADVFEATURE_DATAINV(huart->AdvancedInit.DataInvert));
  1316. MODIFY_REG(huart->Instance->CR2, USART_CR2_DATAINV, huart->AdvancedInit.DataInvert);
  1317. }
  1318. /* if required, configure RX/TX pins swap */
  1319. if(HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_SWAP_INIT))
  1320. {
  1321. assert_param(IS_UART_ADVFEATURE_SWAP(huart->AdvancedInit.Swap));
  1322. MODIFY_REG(huart->Instance->CR2, USART_CR2_SWAP, huart->AdvancedInit.Swap);
  1323. }
  1324. /* if required, configure RX overrun detection disabling */
  1325. if(HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_RXOVERRUNDISABLE_INIT))
  1326. {
  1327. assert_param(IS_UART_OVERRUN(huart->AdvancedInit.OverrunDisable));
  1328. MODIFY_REG(huart->Instance->CR3, USART_CR3_OVRDIS, huart->AdvancedInit.OverrunDisable);
  1329. }
  1330. /* if required, configure DMA disabling on reception error */
  1331. if(HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_DMADISABLEONERROR_INIT))
  1332. {
  1333. assert_param(IS_UART_ADVFEATURE_DMAONRXERROR(huart->AdvancedInit.DMADisableonRxError));
  1334. MODIFY_REG(huart->Instance->CR3, USART_CR3_DDRE, huart->AdvancedInit.DMADisableonRxError);
  1335. }
  1336. /* if required, configure auto Baud rate detection scheme */
  1337. if(HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_AUTOBAUDRATE_INIT))
  1338. {
  1339. assert_param(IS_USART_AUTOBAUDRATE_DETECTION_INSTANCE(huart->Instance));
  1340. assert_param(IS_UART_ADVFEATURE_AUTOBAUDRATE(huart->AdvancedInit.AutoBaudRateEnable));
  1341. MODIFY_REG(huart->Instance->CR2, USART_CR2_ABREN, huart->AdvancedInit.AutoBaudRateEnable);
  1342. /* set auto Baudrate detection parameters if detection is enabled */
  1343. if(huart->AdvancedInit.AutoBaudRateEnable == UART_ADVFEATURE_AUTOBAUDRATE_ENABLE)
  1344. {
  1345. assert_param(IS_UART_ADVFEATURE_AUTOBAUDRATEMODE(huart->AdvancedInit.AutoBaudRateMode));
  1346. MODIFY_REG(huart->Instance->CR2, USART_CR2_ABRMODE, huart->AdvancedInit.AutoBaudRateMode);
  1347. }
  1348. }
  1349. /* if required, configure MSB first on communication line */
  1350. if(HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_MSBFIRST_INIT))
  1351. {
  1352. assert_param(IS_UART_ADVFEATURE_MSBFIRST(huart->AdvancedInit.MSBFirst));
  1353. MODIFY_REG(huart->Instance->CR2, USART_CR2_MSBFIRST, huart->AdvancedInit.MSBFirst);
  1354. }
  1355. }
  1356. /**
  1357. * @brief Check the UART Idle State.
  1358. * @param huart: UART handle.
  1359. * @retval HAL status
  1360. */
  1361. HAL_StatusTypeDef UART_CheckIdleState(UART_HandleTypeDef *huart)
  1362. {
  1363. /* Initialize the UART ErrorCode */
  1364. huart->ErrorCode = HAL_UART_ERROR_NONE;
  1365. /* TEACK and REACK bits in ISR are checked only when available (not available on all F0 devices).
  1366. Bits are defined for some specific devices, and are available only for UART instances supporting WakeUp from Stop Mode feature.
  1367. */
  1368. #if !defined(STM32F030x6) && !defined(STM32F030x8)&& !defined(STM32F070xB)&& !defined(STM32F070x6)&& !defined(STM32F030xC)
  1369. if (IS_UART_WAKEUP_INSTANCE(huart->Instance))
  1370. {
  1371. /* Check if the Transmitter is enabled */
  1372. if((huart->Instance->CR1 & USART_CR1_TE) == USART_CR1_TE)
  1373. {
  1374. /* Wait until TEACK flag is set */
  1375. if(UART_WaitOnFlagUntilTimeout(huart, USART_ISR_TEACK, RESET, UART_TEACK_REACK_TIMEOUT) != HAL_OK)
  1376. {
  1377. /* Timeout occurred */
  1378. return HAL_TIMEOUT;
  1379. }
  1380. }
  1381. /* Check if the Receiver is enabled */
  1382. if((huart->Instance->CR1 & USART_CR1_RE) == USART_CR1_RE)
  1383. {
  1384. /* Wait until REACK flag is set */
  1385. if(UART_WaitOnFlagUntilTimeout(huart, USART_ISR_REACK, RESET, UART_TEACK_REACK_TIMEOUT) != HAL_OK)
  1386. {
  1387. /* Timeout occurred */
  1388. return HAL_TIMEOUT;
  1389. }
  1390. }
  1391. }
  1392. #endif /* !defined(STM32F030x6) && !defined(STM32F030x8)&& !defined(STM32F070xB)&& !defined(STM32F070x6)&& !defined(STM32F030xC) */
  1393. /* Initialize the UART State */
  1394. huart->State= HAL_UART_STATE_READY;
  1395. /* Process Unlocked */
  1396. __HAL_UNLOCK(huart);
  1397. return HAL_OK;
  1398. }
  1399. /**
  1400. * @brief Handle UART Communication Timeout.
  1401. * @param huart: UART handle.
  1402. * @param Flag: specifies the UART flag to check.
  1403. * @param Status: the Flag status (SET or RESET).
  1404. * @param Timeout: Timeout duration.
  1405. * @retval HAL status
  1406. */
  1407. HAL_StatusTypeDef UART_WaitOnFlagUntilTimeout(UART_HandleTypeDef *huart, uint32_t Flag, FlagStatus Status, uint32_t Timeout)
  1408. {
  1409. uint32_t tickstart = HAL_GetTick();
  1410. /* Wait until flag is set */
  1411. if(Status == RESET)
  1412. {
  1413. while(__HAL_UART_GET_FLAG(huart, Flag) == RESET)
  1414. {
  1415. /* Check for the Timeout */
  1416. if(Timeout != HAL_MAX_DELAY)
  1417. {
  1418. if((Timeout == 0) || ((HAL_GetTick()-tickstart) > Timeout))
  1419. {
  1420. /* Disable TXE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts for the interrupt process */
  1421. __HAL_UART_DISABLE_IT(huart, UART_IT_TXE);
  1422. __HAL_UART_DISABLE_IT(huart, UART_IT_RXNE);
  1423. __HAL_UART_DISABLE_IT(huart, UART_IT_PE);
  1424. __HAL_UART_DISABLE_IT(huart, UART_IT_ERR);
  1425. huart->State = HAL_UART_STATE_READY;
  1426. /* Process Unlocked */
  1427. __HAL_UNLOCK(huart);
  1428. return HAL_TIMEOUT;
  1429. }
  1430. }
  1431. }
  1432. }
  1433. else
  1434. {
  1435. while(__HAL_UART_GET_FLAG(huart, Flag) != RESET)
  1436. {
  1437. /* Check for the Timeout */
  1438. if(Timeout != HAL_MAX_DELAY)
  1439. {
  1440. if((Timeout == 0) || ((HAL_GetTick()-tickstart) > Timeout))
  1441. {
  1442. /* Disable TXE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts for the interrupt process */
  1443. __HAL_UART_DISABLE_IT(huart, UART_IT_TXE);
  1444. __HAL_UART_DISABLE_IT(huart, UART_IT_RXNE);
  1445. __HAL_UART_DISABLE_IT(huart, UART_IT_PE);
  1446. __HAL_UART_DISABLE_IT(huart, UART_IT_ERR);
  1447. huart->State = HAL_UART_STATE_READY;
  1448. /* Process Unlocked */
  1449. __HAL_UNLOCK(huart);
  1450. return HAL_TIMEOUT;
  1451. }
  1452. }
  1453. }
  1454. }
  1455. return HAL_OK;
  1456. }
  1457. /**
  1458. * @brief DMA UART transmit process complete callback.
  1459. * @param hdma: DMA handle.
  1460. * @retval None
  1461. */
  1462. static void UART_DMATransmitCplt(DMA_HandleTypeDef *hdma)
  1463. {
  1464. UART_HandleTypeDef* huart = ( UART_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1465. /* DMA Normal mode */
  1466. if ( HAL_IS_BIT_CLR(hdma->Instance->CCR, DMA_CCR_CIRC) )
  1467. {
  1468. huart->TxXferCount = 0;
  1469. /* Disable the DMA transfer for transmit request by resetting the DMAT bit
  1470. in the UART CR3 register */
  1471. huart->Instance->CR3 &= (uint32_t)~((uint32_t)USART_CR3_DMAT);
  1472. /* Enable the UART Transmit Complete Interrupt */
  1473. __HAL_UART_ENABLE_IT(huart, UART_IT_TC);
  1474. }
  1475. /* DMA Circular mode */
  1476. else
  1477. {
  1478. HAL_UART_TxCpltCallback(huart);
  1479. }
  1480. }
  1481. /**
  1482. * @brief DMA UART transmit process half complete callback.
  1483. * @param hdma : DMA handle.
  1484. * @retval None
  1485. */
  1486. static void UART_DMATxHalfCplt(DMA_HandleTypeDef *hdma)
  1487. {
  1488. UART_HandleTypeDef* huart = (UART_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
  1489. HAL_UART_TxHalfCpltCallback(huart);
  1490. }
  1491. /**
  1492. * @brief DMA UART receive process complete callback.
  1493. * @param hdma: DMA handle.
  1494. * @retval None
  1495. */
  1496. static void UART_DMAReceiveCplt(DMA_HandleTypeDef *hdma)
  1497. {
  1498. UART_HandleTypeDef* huart = ( UART_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1499. /* DMA Normal mode */
  1500. if ( HAL_IS_BIT_CLR(hdma->Instance->CCR, DMA_CCR_CIRC) )
  1501. {
  1502. huart->RxXferCount = 0;
  1503. /* Disable the DMA transfer for the receiver request by resetting the DMAR bit
  1504. in the UART CR3 register */
  1505. huart->Instance->CR3 &= (uint32_t)~((uint32_t)USART_CR3_DMAR);
  1506. /* Check if a transmit Process is ongoing or not */
  1507. if(huart->State == HAL_UART_STATE_BUSY_TX_RX)
  1508. {
  1509. huart->State = HAL_UART_STATE_BUSY_TX;
  1510. }
  1511. else
  1512. {
  1513. huart->State = HAL_UART_STATE_READY;
  1514. }
  1515. }
  1516. HAL_UART_RxCpltCallback(huart);
  1517. }
  1518. /**
  1519. * @brief DMA UART receive process half complete callback.
  1520. * @param hdma : DMA handle.
  1521. * @retval None
  1522. */
  1523. static void UART_DMARxHalfCplt(DMA_HandleTypeDef *hdma)
  1524. {
  1525. UART_HandleTypeDef* huart = (UART_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
  1526. HAL_UART_RxHalfCpltCallback(huart);
  1527. }
  1528. /**
  1529. * @brief DMA UART communication error callback.
  1530. * @param hdma: DMA handle.
  1531. * @retval None
  1532. */
  1533. static void UART_DMAError(DMA_HandleTypeDef *hdma)
  1534. {
  1535. UART_HandleTypeDef* huart = ( UART_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1536. huart->RxXferCount = 0;
  1537. huart->TxXferCount = 0;
  1538. huart->State= HAL_UART_STATE_READY;
  1539. huart->ErrorCode |= HAL_UART_ERROR_DMA;
  1540. HAL_UART_ErrorCallback(huart);
  1541. }
  1542. /**
  1543. * @brief Send an amount of data in interrupt mode.
  1544. * @note Function is called under interruption only, once
  1545. * interruptions have been enabled by HAL_UART_Transmit_IT().
  1546. * @param huart: UART handle.
  1547. * @retval HAL status
  1548. */
  1549. HAL_StatusTypeDef UART_Transmit_IT(UART_HandleTypeDef *huart)
  1550. {
  1551. uint16_t* tmp;
  1552. if ((huart->State == HAL_UART_STATE_BUSY_TX) || (huart->State == HAL_UART_STATE_BUSY_TX_RX))
  1553. {
  1554. if(huart->TxXferCount == 0)
  1555. {
  1556. /* Disable the UART Transmit Data Register Empty Interrupt */
  1557. __HAL_UART_DISABLE_IT(huart, UART_IT_TXE);
  1558. /* Enable the UART Transmit Complete Interrupt */
  1559. __HAL_UART_ENABLE_IT(huart, UART_IT_TC);
  1560. return HAL_OK;
  1561. }
  1562. else
  1563. {
  1564. if ((huart->Init.WordLength == UART_WORDLENGTH_9B) && (huart->Init.Parity == UART_PARITY_NONE))
  1565. {
  1566. tmp = (uint16_t*) huart->pTxBuffPtr;
  1567. huart->Instance->TDR = (*tmp & (uint16_t)0x01FF);
  1568. huart->pTxBuffPtr += 2;
  1569. }
  1570. else
  1571. {
  1572. huart->Instance->TDR = (uint8_t)(*huart->pTxBuffPtr++ & (uint8_t)0xFF);
  1573. }
  1574. huart->TxXferCount--;
  1575. return HAL_OK;
  1576. }
  1577. }
  1578. else
  1579. {
  1580. return HAL_BUSY;
  1581. }
  1582. }
  1583. /**
  1584. * @brief Wrap up transmission in non-blocking mode.
  1585. * @param huart: pointer to a UART_HandleTypeDef structure that contains
  1586. * the configuration information for the specified UART module.
  1587. * @retval HAL status
  1588. */
  1589. HAL_StatusTypeDef UART_EndTransmit_IT(UART_HandleTypeDef *huart)
  1590. {
  1591. /* Disable the UART Transmit Complete Interrupt */
  1592. __HAL_UART_DISABLE_IT(huart, UART_IT_TC);
  1593. /* Check if a receive process is ongoing or not */
  1594. if(huart->State == HAL_UART_STATE_BUSY_TX_RX)
  1595. {
  1596. huart->State = HAL_UART_STATE_BUSY_RX;
  1597. }
  1598. else
  1599. {
  1600. huart->State = HAL_UART_STATE_READY;
  1601. }
  1602. HAL_UART_TxCpltCallback(huart);
  1603. return HAL_OK;
  1604. }
  1605. /**
  1606. * @brief Receive an amount of data in interrupt mode.
  1607. * @note Function is called under interruption only, once
  1608. * interruptions have been enabled by HAL_UART_Receive_IT()
  1609. * @param huart: UART handle.
  1610. * @retval HAL status
  1611. */
  1612. HAL_StatusTypeDef UART_Receive_IT(UART_HandleTypeDef *huart)
  1613. {
  1614. uint16_t* tmp;
  1615. uint16_t uhMask = huart->Mask;
  1616. if((huart->State == HAL_UART_STATE_BUSY_RX) || (huart->State == HAL_UART_STATE_BUSY_TX_RX))
  1617. {
  1618. if ((huart->Init.WordLength == UART_WORDLENGTH_9B) && (huart->Init.Parity == UART_PARITY_NONE))
  1619. {
  1620. tmp = (uint16_t*) huart->pRxBuffPtr ;
  1621. *tmp = (uint16_t)(huart->Instance->RDR & uhMask);
  1622. huart->pRxBuffPtr +=2;
  1623. }
  1624. else
  1625. {
  1626. *huart->pRxBuffPtr++ = (uint8_t)(huart->Instance->RDR & (uint8_t)uhMask);
  1627. }
  1628. if(--huart->RxXferCount == 0)
  1629. {
  1630. __HAL_UART_DISABLE_IT(huart, UART_IT_RXNE);
  1631. /* Check if a transmit Process is ongoing or not */
  1632. if(huart->State == HAL_UART_STATE_BUSY_TX_RX)
  1633. {
  1634. huart->State = HAL_UART_STATE_BUSY_TX;
  1635. }
  1636. else
  1637. {
  1638. /* Disable the UART Parity Error Interrupt */
  1639. __HAL_UART_DISABLE_IT(huart, UART_IT_PE);
  1640. /* Disable the UART Error Interrupt: (Frame error, noise error, overrun error) */
  1641. __HAL_UART_DISABLE_IT(huart, UART_IT_ERR);
  1642. huart->State = HAL_UART_STATE_READY;
  1643. }
  1644. HAL_UART_RxCpltCallback(huart);
  1645. return HAL_OK;
  1646. }
  1647. return HAL_OK;
  1648. }
  1649. else
  1650. {
  1651. return HAL_BUSY;
  1652. }
  1653. }
  1654. /**
  1655. * @}
  1656. */
  1657. #endif /* HAL_UART_MODULE_ENABLED */
  1658. /**
  1659. * @}
  1660. */
  1661. /**
  1662. * @}
  1663. */
  1664. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/