py32f0xx_hal_uart.c 111 KB

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