stm32f7xx_hal_uart.c 135 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014
  1. /**
  2. ******************************************************************************
  3. * @file stm32f7xx_hal_uart.c
  4. * @author MCD 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. *
  12. *
  13. ******************************************************************************
  14. * @attention
  15. *
  16. * Copyright (c) 2017 STMicroelectronics.
  17. * All rights reserved.
  18. *
  19. * This software is licensed under terms that can be found in the LICENSE file
  20. * in the root directory of this software component.
  21. * If no LICENSE file comes with this software, it is provided AS-IS.
  22. *
  23. ******************************************************************************
  24. @verbatim
  25. ===============================================================================
  26. ##### How to use this driver #####
  27. ===============================================================================
  28. [..]
  29. The UART HAL driver can be used as follows:
  30. (#) Declare a UART_HandleTypeDef handle structure (eg. UART_HandleTypeDef huart).
  31. (#) Initialize the UART low level resources by implementing the HAL_UART_MspInit() API:
  32. (++) Enable the USARTx interface clock.
  33. (++) UART pins configuration:
  34. (+++) Enable the clock for the UART GPIOs.
  35. (+++) Configure these UART pins as alternate function pull-up.
  36. (++) NVIC configuration if you need to use interrupt process (HAL_UART_Transmit_IT()
  37. and HAL_UART_Receive_IT() APIs):
  38. (+++) Configure the USARTx interrupt priority.
  39. (+++) Enable the NVIC USART IRQ handle.
  40. (++) UART interrupts handling:
  41. -@@- The specific UART interrupts (Transmission complete interrupt,
  42. RXNE interrupt, RX/TX FIFOs related interrupts and Error Interrupts)
  43. are managed using the macros __HAL_UART_ENABLE_IT() and __HAL_UART_DISABLE_IT()
  44. inside the transmit and receive processes.
  45. (++) DMA Configuration if you need to use DMA process (HAL_UART_Transmit_DMA()
  46. and HAL_UART_Receive_DMA() APIs):
  47. (+++) Declare a DMA handle structure for the Tx/Rx channel.
  48. (+++) Enable the DMAx interface clock.
  49. (+++) Configure the declared DMA handle structure with the required Tx/Rx parameters.
  50. (+++) Configure the DMA Tx/Rx channel.
  51. (+++) Associate the initialized DMA handle to the UART DMA Tx/Rx handle.
  52. (+++) Configure the priority and enable the NVIC for the transfer complete
  53. interrupt on the DMA Tx/Rx channel.
  54. (#) Program the Baud Rate, Word Length, Stop Bit, Parity, Hardware
  55. flow control and Mode (Receiver/Transmitter) in the huart handle Init structure.
  56. (#) If required, program UART advanced features (TX/RX pins swap, auto Baud rate detection,...)
  57. in the huart handle AdvancedInit structure.
  58. (#) For the UART asynchronous mode, initialize the UART registers by calling
  59. the HAL_UART_Init() API.
  60. (#) For the UART Half duplex mode, initialize the UART registers by calling
  61. the HAL_HalfDuplex_Init() API.
  62. (#) For the UART LIN (Local Interconnection Network) mode, initialize the UART registers
  63. by calling the HAL_LIN_Init() API.
  64. (#) For the UART Multiprocessor mode, initialize the UART registers
  65. by calling the HAL_MultiProcessor_Init() API.
  66. (#) For the UART RS485 Driver Enabled mode, initialize the UART registers
  67. by calling the HAL_RS485Ex_Init() API.
  68. [..]
  69. (@) These API's (HAL_UART_Init(), HAL_HalfDuplex_Init(), HAL_LIN_Init(), HAL_MultiProcessor_Init(),
  70. also configure the low level Hardware GPIO, CLOCK, CORTEX...etc) by
  71. calling the customized HAL_UART_MspInit() API.
  72. ##### Callback registration #####
  73. ==================================
  74. [..]
  75. The compilation define USE_HAL_UART_REGISTER_CALLBACKS when set to 1
  76. allows the user to configure dynamically the driver callbacks.
  77. [..]
  78. Use Function HAL_UART_RegisterCallback() to register a user callback.
  79. Function HAL_UART_RegisterCallback() allows to register following callbacks:
  80. (+) TxHalfCpltCallback : Tx Half Complete Callback.
  81. (+) TxCpltCallback : Tx Complete Callback.
  82. (+) RxHalfCpltCallback : Rx Half Complete Callback.
  83. (+) RxCpltCallback : Rx Complete Callback.
  84. (+) ErrorCallback : Error Callback.
  85. (+) AbortCpltCallback : Abort Complete Callback.
  86. (+) AbortTransmitCpltCallback : Abort Transmit Complete Callback.
  87. (+) AbortReceiveCpltCallback : Abort Receive Complete Callback.
  88. (+) WakeupCallback : Wakeup Callback.
  89. (+) MspInitCallback : UART MspInit.
  90. (+) MspDeInitCallback : UART MspDeInit.
  91. This function takes as parameters the HAL peripheral handle, the Callback ID
  92. and a pointer to the user callback function.
  93. [..]
  94. Use function HAL_UART_UnRegisterCallback() to reset a callback to the default
  95. weak (surcharged) function.
  96. HAL_UART_UnRegisterCallback() takes as parameters the HAL peripheral handle,
  97. and the Callback ID.
  98. This function allows to reset following callbacks:
  99. (+) TxHalfCpltCallback : Tx Half Complete Callback.
  100. (+) TxCpltCallback : Tx Complete Callback.
  101. (+) RxHalfCpltCallback : Rx Half Complete Callback.
  102. (+) RxCpltCallback : Rx Complete Callback.
  103. (+) ErrorCallback : Error Callback.
  104. (+) AbortCpltCallback : Abort Complete Callback.
  105. (+) AbortTransmitCpltCallback : Abort Transmit Complete Callback.
  106. (+) AbortReceiveCpltCallback : Abort Receive Complete Callback.
  107. (+) WakeupCallback : Wakeup Callback.
  108. (+) MspInitCallback : UART MspInit.
  109. (+) MspDeInitCallback : UART MspDeInit.
  110. [..]
  111. For specific callback RxEventCallback, use dedicated registration/reset functions:
  112. respectively HAL_UART_RegisterRxEventCallback() , HAL_UART_UnRegisterRxEventCallback().
  113. [..]
  114. By default, after the HAL_UART_Init() and when the state is HAL_UART_STATE_RESET
  115. all callbacks are set to the corresponding weak (surcharged) functions:
  116. examples HAL_UART_TxCpltCallback(), HAL_UART_RxHalfCpltCallback().
  117. Exception done for MspInit and MspDeInit functions that are respectively
  118. reset to the legacy weak (surcharged) functions in the HAL_UART_Init()
  119. and HAL_UART_DeInit() only when these callbacks are null (not registered beforehand).
  120. If not, MspInit or MspDeInit are not null, the HAL_UART_Init() and HAL_UART_DeInit()
  121. keep and use the user MspInit/MspDeInit callbacks (registered beforehand).
  122. [..]
  123. Callbacks can be registered/unregistered in HAL_UART_STATE_READY state only.
  124. Exception done MspInit/MspDeInit that can be registered/unregistered
  125. in HAL_UART_STATE_READY or HAL_UART_STATE_RESET state, thus registered (user)
  126. MspInit/DeInit callbacks can be used during the Init/DeInit.
  127. In that case first register the MspInit/MspDeInit user callbacks
  128. using HAL_UART_RegisterCallback() before calling HAL_UART_DeInit()
  129. or HAL_UART_Init() function.
  130. [..]
  131. When The compilation define USE_HAL_UART_REGISTER_CALLBACKS is set to 0 or
  132. not defined, the callback registration feature is not available
  133. and weak (surcharged) callbacks are used.
  134. @endverbatim
  135. ******************************************************************************
  136. */
  137. /* Includes ------------------------------------------------------------------*/
  138. #include "stm32f7xx_hal.h"
  139. /** @addtogroup STM32F7xx_HAL_Driver
  140. * @{
  141. */
  142. /** @defgroup UART UART
  143. * @brief HAL UART module driver
  144. * @{
  145. */
  146. #ifdef HAL_UART_MODULE_ENABLED
  147. /* Private typedef -----------------------------------------------------------*/
  148. /* Private define ------------------------------------------------------------*/
  149. /** @defgroup UART_Private_Constants UART Private Constants
  150. * @{
  151. */
  152. #define USART_CR1_FIELDS ((uint32_t)(USART_CR1_M | USART_CR1_PCE | USART_CR1_PS | USART_CR1_TE | USART_CR1_RE | \
  153. USART_CR1_OVER8)) /*!< UART or USART CR1 fields of parameters set by UART_SetConfig API */
  154. #define USART_CR3_FIELDS ((uint32_t)(USART_CR3_RTSE | USART_CR3_CTSE |\
  155. USART_CR3_ONEBIT)) /*!< UART or USART CR3 fields of parameters set by UART_SetConfig API */
  156. #define UART_BRR_MIN 0x10U /* UART BRR minimum authorized value */
  157. #define UART_BRR_MAX 0x0000FFFFU /* UART BRR maximum authorized value */
  158. /**
  159. * @}
  160. */
  161. /* Private macros ------------------------------------------------------------*/
  162. /* Private function prototypes -----------------------------------------------*/
  163. /** @addtogroup UART_Private_Functions
  164. * @{
  165. */
  166. static void UART_EndTxTransfer(UART_HandleTypeDef *huart);
  167. static void UART_EndRxTransfer(UART_HandleTypeDef *huart);
  168. static void UART_DMATransmitCplt(DMA_HandleTypeDef *hdma);
  169. static void UART_DMAReceiveCplt(DMA_HandleTypeDef *hdma);
  170. static void UART_DMARxHalfCplt(DMA_HandleTypeDef *hdma);
  171. static void UART_DMATxHalfCplt(DMA_HandleTypeDef *hdma);
  172. static void UART_DMAError(DMA_HandleTypeDef *hdma);
  173. static void UART_DMAAbortOnError(DMA_HandleTypeDef *hdma);
  174. static void UART_DMATxAbortCallback(DMA_HandleTypeDef *hdma);
  175. static void UART_DMARxAbortCallback(DMA_HandleTypeDef *hdma);
  176. static void UART_DMATxOnlyAbortCallback(DMA_HandleTypeDef *hdma);
  177. static void UART_DMARxOnlyAbortCallback(DMA_HandleTypeDef *hdma);
  178. static void UART_TxISR_8BIT(UART_HandleTypeDef *huart);
  179. static void UART_TxISR_16BIT(UART_HandleTypeDef *huart);
  180. static void UART_EndTransmit_IT(UART_HandleTypeDef *huart);
  181. static void UART_RxISR_8BIT(UART_HandleTypeDef *huart);
  182. static void UART_RxISR_16BIT(UART_HandleTypeDef *huart);
  183. /**
  184. * @}
  185. */
  186. /* Private variables ---------------------------------------------------------*/
  187. /* Exported Constants --------------------------------------------------------*/
  188. /* Exported functions --------------------------------------------------------*/
  189. /** @defgroup UART_Exported_Functions UART Exported Functions
  190. * @{
  191. */
  192. /** @defgroup UART_Exported_Functions_Group1 Initialization and de-initialization functions
  193. * @brief Initialization and Configuration functions
  194. *
  195. @verbatim
  196. ===============================================================================
  197. ##### Initialization and Configuration functions #####
  198. ===============================================================================
  199. [..]
  200. This subsection provides a set of functions allowing to initialize the USARTx or the UARTy
  201. in asynchronous mode.
  202. (+) For the asynchronous mode the parameters below can be configured:
  203. (++) Baud Rate
  204. (++) Word Length
  205. (++) Stop Bit
  206. (++) Parity: If the parity is enabled, then the MSB bit of the data written
  207. in the data register is transmitted but is changed by the parity bit.
  208. (++) Hardware flow control
  209. (++) Receiver/transmitter modes
  210. (++) Over Sampling Method
  211. (++) One-Bit Sampling Method
  212. (+) For the asynchronous mode, the following advanced features can be configured as well:
  213. (++) TX and/or RX pin level inversion
  214. (++) data logical level inversion
  215. (++) RX and TX pins swap
  216. (++) RX overrun detection disabling
  217. (++) DMA disabling on RX error
  218. (++) MSB first on communication line
  219. (++) auto Baud rate detection
  220. [..]
  221. The HAL_UART_Init(), HAL_HalfDuplex_Init(), HAL_LIN_Init()and HAL_MultiProcessor_Init()API
  222. follow respectively the UART asynchronous, UART Half duplex, UART LIN mode
  223. and UART multiprocessor mode configuration procedures (details for the procedures
  224. are available in reference manual).
  225. @endverbatim
  226. Depending on the frame length defined by the M1 and M0 bits (7-bit,
  227. 8-bit or 9-bit), the possible UART formats are listed in the
  228. following table.
  229. Table 1. UART frame format.
  230. +-----------------------------------------------------------------------+
  231. | M1 bit | M0 bit | PCE bit | UART frame |
  232. |---------|---------|-----------|---------------------------------------|
  233. | 0 | 0 | 0 | | SB | 8 bit data | STB | |
  234. |---------|---------|-----------|---------------------------------------|
  235. | 0 | 0 | 1 | | SB | 7 bit data | PB | STB | |
  236. |---------|---------|-----------|---------------------------------------|
  237. | 0 | 1 | 0 | | SB | 9 bit data | STB | |
  238. |---------|---------|-----------|---------------------------------------|
  239. | 0 | 1 | 1 | | SB | 8 bit data | PB | STB | |
  240. |---------|---------|-----------|---------------------------------------|
  241. | 1 | 0 | 0 | | SB | 7 bit data | STB | |
  242. |---------|---------|-----------|---------------------------------------|
  243. | 1 | 0 | 1 | | SB | 6 bit data | PB | STB | |
  244. +-----------------------------------------------------------------------+
  245. * @{
  246. */
  247. /**
  248. * @brief Initialize the UART mode according to the specified
  249. * parameters in the UART_InitTypeDef and initialize the associated handle.
  250. * @param huart UART handle.
  251. * @retval HAL status
  252. */
  253. HAL_StatusTypeDef HAL_UART_Init(UART_HandleTypeDef *huart)
  254. {
  255. /* Check the UART handle allocation */
  256. if (huart == NULL)
  257. {
  258. return HAL_ERROR;
  259. }
  260. if (huart->Init.HwFlowCtl != UART_HWCONTROL_NONE)
  261. {
  262. /* Check the parameters */
  263. assert_param(IS_UART_HWFLOW_INSTANCE(huart->Instance));
  264. }
  265. else
  266. {
  267. /* Check the parameters */
  268. assert_param(IS_UART_INSTANCE(huart->Instance));
  269. }
  270. if (huart->gState == HAL_UART_STATE_RESET)
  271. {
  272. /* Allocate lock resource and initialize it */
  273. huart->Lock = HAL_UNLOCKED;
  274. #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
  275. UART_InitCallbacksToDefault(huart);
  276. if (huart->MspInitCallback == NULL)
  277. {
  278. huart->MspInitCallback = HAL_UART_MspInit;
  279. }
  280. /* Init the low level hardware */
  281. huart->MspInitCallback(huart);
  282. #else
  283. /* Init the low level hardware : GPIO, CLOCK */
  284. HAL_UART_MspInit(huart);
  285. #endif /* (USE_HAL_UART_REGISTER_CALLBACKS) */
  286. }
  287. huart->gState = HAL_UART_STATE_BUSY;
  288. __HAL_UART_DISABLE(huart);
  289. /* Set the UART Communication parameters */
  290. if (UART_SetConfig(huart) == HAL_ERROR)
  291. {
  292. return HAL_ERROR;
  293. }
  294. if (huart->AdvancedInit.AdvFeatureInit != UART_ADVFEATURE_NO_INIT)
  295. {
  296. UART_AdvFeatureConfig(huart);
  297. }
  298. /* In asynchronous mode, the following bits must be kept cleared:
  299. - LINEN and CLKEN bits in the USART_CR2 register,
  300. - SCEN, HDSEL and IREN bits in the USART_CR3 register.*/
  301. CLEAR_BIT(huart->Instance->CR2, (USART_CR2_LINEN | USART_CR2_CLKEN));
  302. CLEAR_BIT(huart->Instance->CR3, (USART_CR3_SCEN | USART_CR3_HDSEL | USART_CR3_IREN));
  303. __HAL_UART_ENABLE(huart);
  304. /* TEACK and/or REACK to check before moving huart->gState and huart->RxState to Ready */
  305. return (UART_CheckIdleState(huart));
  306. }
  307. /**
  308. * @brief Initialize the half-duplex mode according to the specified
  309. * parameters in the UART_InitTypeDef and creates the associated handle.
  310. * @param huart UART handle.
  311. * @retval HAL status
  312. */
  313. HAL_StatusTypeDef HAL_HalfDuplex_Init(UART_HandleTypeDef *huart)
  314. {
  315. /* Check the UART handle allocation */
  316. if (huart == NULL)
  317. {
  318. return HAL_ERROR;
  319. }
  320. /* Check UART instance */
  321. assert_param(IS_UART_HALFDUPLEX_INSTANCE(huart->Instance));
  322. if (huart->gState == HAL_UART_STATE_RESET)
  323. {
  324. /* Allocate lock resource and initialize it */
  325. huart->Lock = HAL_UNLOCKED;
  326. #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
  327. UART_InitCallbacksToDefault(huart);
  328. if (huart->MspInitCallback == NULL)
  329. {
  330. huart->MspInitCallback = HAL_UART_MspInit;
  331. }
  332. /* Init the low level hardware */
  333. huart->MspInitCallback(huart);
  334. #else
  335. /* Init the low level hardware : GPIO, CLOCK */
  336. HAL_UART_MspInit(huart);
  337. #endif /* (USE_HAL_UART_REGISTER_CALLBACKS) */
  338. }
  339. huart->gState = HAL_UART_STATE_BUSY;
  340. __HAL_UART_DISABLE(huart);
  341. /* Set the UART Communication parameters */
  342. if (UART_SetConfig(huart) == HAL_ERROR)
  343. {
  344. return HAL_ERROR;
  345. }
  346. if (huart->AdvancedInit.AdvFeatureInit != UART_ADVFEATURE_NO_INIT)
  347. {
  348. UART_AdvFeatureConfig(huart);
  349. }
  350. /* In half-duplex mode, the following bits must be kept cleared:
  351. - LINEN and CLKEN bits in the USART_CR2 register,
  352. - SCEN and IREN bits in the USART_CR3 register.*/
  353. CLEAR_BIT(huart->Instance->CR2, (USART_CR2_LINEN | USART_CR2_CLKEN));
  354. CLEAR_BIT(huart->Instance->CR3, (USART_CR3_IREN | USART_CR3_SCEN));
  355. /* Enable the Half-Duplex mode by setting the HDSEL bit in the CR3 register */
  356. SET_BIT(huart->Instance->CR3, USART_CR3_HDSEL);
  357. __HAL_UART_ENABLE(huart);
  358. /* TEACK and/or REACK to check before moving huart->gState and huart->RxState to Ready */
  359. return (UART_CheckIdleState(huart));
  360. }
  361. /**
  362. * @brief Initialize the LIN mode according to the specified
  363. * parameters in the UART_InitTypeDef and creates the associated handle.
  364. * @param huart UART handle.
  365. * @param BreakDetectLength Specifies the LIN break detection length.
  366. * This parameter can be one of the following values:
  367. * @arg @ref UART_LINBREAKDETECTLENGTH_10B 10-bit break detection
  368. * @arg @ref UART_LINBREAKDETECTLENGTH_11B 11-bit break detection
  369. * @retval HAL status
  370. */
  371. HAL_StatusTypeDef HAL_LIN_Init(UART_HandleTypeDef *huart, uint32_t BreakDetectLength)
  372. {
  373. /* Check the UART handle allocation */
  374. if (huart == NULL)
  375. {
  376. return HAL_ERROR;
  377. }
  378. /* Check the LIN UART instance */
  379. assert_param(IS_UART_LIN_INSTANCE(huart->Instance));
  380. /* Check the Break detection length parameter */
  381. assert_param(IS_UART_LIN_BREAK_DETECT_LENGTH(BreakDetectLength));
  382. /* LIN mode limited to 16-bit oversampling only */
  383. if (huart->Init.OverSampling == UART_OVERSAMPLING_8)
  384. {
  385. return HAL_ERROR;
  386. }
  387. /* LIN mode limited to 8-bit data length */
  388. if (huart->Init.WordLength != UART_WORDLENGTH_8B)
  389. {
  390. return HAL_ERROR;
  391. }
  392. if (huart->gState == HAL_UART_STATE_RESET)
  393. {
  394. /* Allocate lock resource and initialize it */
  395. huart->Lock = HAL_UNLOCKED;
  396. #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
  397. UART_InitCallbacksToDefault(huart);
  398. if (huart->MspInitCallback == NULL)
  399. {
  400. huart->MspInitCallback = HAL_UART_MspInit;
  401. }
  402. /* Init the low level hardware */
  403. huart->MspInitCallback(huart);
  404. #else
  405. /* Init the low level hardware : GPIO, CLOCK */
  406. HAL_UART_MspInit(huart);
  407. #endif /* (USE_HAL_UART_REGISTER_CALLBACKS) */
  408. }
  409. huart->gState = HAL_UART_STATE_BUSY;
  410. __HAL_UART_DISABLE(huart);
  411. /* Set the UART Communication parameters */
  412. if (UART_SetConfig(huart) == HAL_ERROR)
  413. {
  414. return HAL_ERROR;
  415. }
  416. if (huart->AdvancedInit.AdvFeatureInit != UART_ADVFEATURE_NO_INIT)
  417. {
  418. UART_AdvFeatureConfig(huart);
  419. }
  420. /* In LIN mode, the following bits must be kept cleared:
  421. - LINEN and CLKEN bits in the USART_CR2 register,
  422. - SCEN and IREN bits in the USART_CR3 register.*/
  423. CLEAR_BIT(huart->Instance->CR2, USART_CR2_CLKEN);
  424. CLEAR_BIT(huart->Instance->CR3, (USART_CR3_HDSEL | USART_CR3_IREN | USART_CR3_SCEN));
  425. /* Enable the LIN mode by setting the LINEN bit in the CR2 register */
  426. SET_BIT(huart->Instance->CR2, USART_CR2_LINEN);
  427. /* Set the USART LIN Break detection length. */
  428. MODIFY_REG(huart->Instance->CR2, USART_CR2_LBDL, BreakDetectLength);
  429. __HAL_UART_ENABLE(huart);
  430. /* TEACK and/or REACK to check before moving huart->gState and huart->RxState to Ready */
  431. return (UART_CheckIdleState(huart));
  432. }
  433. /**
  434. * @brief Initialize the multiprocessor mode according to the specified
  435. * parameters in the UART_InitTypeDef and initialize the associated handle.
  436. * @param huart UART handle.
  437. * @param Address UART node address (4-, 6-, 7- or 8-bit long).
  438. * @param WakeUpMethod Specifies the UART wakeup method.
  439. * This parameter can be one of the following values:
  440. * @arg @ref UART_WAKEUPMETHOD_IDLELINE WakeUp by an idle line detection
  441. * @arg @ref UART_WAKEUPMETHOD_ADDRESSMARK WakeUp by an address mark
  442. * @note If the user resorts to idle line detection wake up, the Address parameter
  443. * is useless and ignored by the initialization function.
  444. * @note If the user resorts to address mark wake up, the address length detection
  445. * is configured by default to 4 bits only. For the UART to be able to
  446. * manage 6-, 7- or 8-bit long addresses detection, the API
  447. * HAL_MultiProcessorEx_AddressLength_Set() must be called after
  448. * HAL_MultiProcessor_Init().
  449. * @retval HAL status
  450. */
  451. HAL_StatusTypeDef HAL_MultiProcessor_Init(UART_HandleTypeDef *huart, uint8_t Address, uint32_t WakeUpMethod)
  452. {
  453. /* Check the UART handle allocation */
  454. if (huart == NULL)
  455. {
  456. return HAL_ERROR;
  457. }
  458. /* Check the wake up method parameter */
  459. assert_param(IS_UART_WAKEUPMETHOD(WakeUpMethod));
  460. if (huart->gState == HAL_UART_STATE_RESET)
  461. {
  462. /* Allocate lock resource and initialize it */
  463. huart->Lock = HAL_UNLOCKED;
  464. #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
  465. UART_InitCallbacksToDefault(huart);
  466. if (huart->MspInitCallback == NULL)
  467. {
  468. huart->MspInitCallback = HAL_UART_MspInit;
  469. }
  470. /* Init the low level hardware */
  471. huart->MspInitCallback(huart);
  472. #else
  473. /* Init the low level hardware : GPIO, CLOCK */
  474. HAL_UART_MspInit(huart);
  475. #endif /* (USE_HAL_UART_REGISTER_CALLBACKS) */
  476. }
  477. huart->gState = HAL_UART_STATE_BUSY;
  478. __HAL_UART_DISABLE(huart);
  479. /* Set the UART Communication parameters */
  480. if (UART_SetConfig(huart) == HAL_ERROR)
  481. {
  482. return HAL_ERROR;
  483. }
  484. if (huart->AdvancedInit.AdvFeatureInit != UART_ADVFEATURE_NO_INIT)
  485. {
  486. UART_AdvFeatureConfig(huart);
  487. }
  488. /* In multiprocessor mode, the following bits must be kept cleared:
  489. - LINEN and CLKEN bits in the USART_CR2 register,
  490. - SCEN, HDSEL and IREN bits in the USART_CR3 register. */
  491. CLEAR_BIT(huart->Instance->CR2, (USART_CR2_LINEN | USART_CR2_CLKEN));
  492. CLEAR_BIT(huart->Instance->CR3, (USART_CR3_SCEN | USART_CR3_HDSEL | USART_CR3_IREN));
  493. if (WakeUpMethod == UART_WAKEUPMETHOD_ADDRESSMARK)
  494. {
  495. /* If address mark wake up method is chosen, set the USART address node */
  496. MODIFY_REG(huart->Instance->CR2, USART_CR2_ADD, ((uint32_t)Address << UART_CR2_ADDRESS_LSB_POS));
  497. }
  498. /* Set the wake up method by setting the WAKE bit in the CR1 register */
  499. MODIFY_REG(huart->Instance->CR1, USART_CR1_WAKE, WakeUpMethod);
  500. __HAL_UART_ENABLE(huart);
  501. /* TEACK and/or REACK to check before moving huart->gState and huart->RxState to Ready */
  502. return (UART_CheckIdleState(huart));
  503. }
  504. /**
  505. * @brief DeInitialize the UART peripheral.
  506. * @param huart UART handle.
  507. * @retval HAL status
  508. */
  509. HAL_StatusTypeDef HAL_UART_DeInit(UART_HandleTypeDef *huart)
  510. {
  511. /* Check the UART handle allocation */
  512. if (huart == NULL)
  513. {
  514. return HAL_ERROR;
  515. }
  516. /* Check the parameters */
  517. assert_param(IS_UART_INSTANCE(huart->Instance));
  518. huart->gState = HAL_UART_STATE_BUSY;
  519. __HAL_UART_DISABLE(huart);
  520. huart->Instance->CR1 = 0x0U;
  521. huart->Instance->CR2 = 0x0U;
  522. huart->Instance->CR3 = 0x0U;
  523. #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
  524. if (huart->MspDeInitCallback == NULL)
  525. {
  526. huart->MspDeInitCallback = HAL_UART_MspDeInit;
  527. }
  528. /* DeInit the low level hardware */
  529. huart->MspDeInitCallback(huart);
  530. #else
  531. /* DeInit the low level hardware */
  532. HAL_UART_MspDeInit(huart);
  533. #endif /* (USE_HAL_UART_REGISTER_CALLBACKS) */
  534. huart->ErrorCode = HAL_UART_ERROR_NONE;
  535. huart->gState = HAL_UART_STATE_RESET;
  536. huart->RxState = HAL_UART_STATE_RESET;
  537. huart->ReceptionType = HAL_UART_RECEPTION_STANDARD;
  538. huart->RxEventType = HAL_UART_RXEVENT_TC;
  539. __HAL_UNLOCK(huart);
  540. return HAL_OK;
  541. }
  542. /**
  543. * @brief Initialize the UART MSP.
  544. * @param huart UART handle.
  545. * @retval None
  546. */
  547. __weak void HAL_UART_MspInit(UART_HandleTypeDef *huart)
  548. {
  549. /* Prevent unused argument(s) compilation warning */
  550. UNUSED(huart);
  551. /* NOTE : This function should not be modified, when the callback is needed,
  552. the HAL_UART_MspInit can be implemented in the user file
  553. */
  554. }
  555. /**
  556. * @brief DeInitialize the UART MSP.
  557. * @param huart UART handle.
  558. * @retval None
  559. */
  560. __weak void HAL_UART_MspDeInit(UART_HandleTypeDef *huart)
  561. {
  562. /* Prevent unused argument(s) compilation warning */
  563. UNUSED(huart);
  564. /* NOTE : This function should not be modified, when the callback is needed,
  565. the HAL_UART_MspDeInit can be implemented in the user file
  566. */
  567. }
  568. #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
  569. /**
  570. * @brief Register a User UART Callback
  571. * To be used instead of the weak predefined callback
  572. * @param huart uart handle
  573. * @param CallbackID ID of the callback to be registered
  574. * This parameter can be one of the following values:
  575. * @arg @ref HAL_UART_TX_HALFCOMPLETE_CB_ID Tx Half Complete Callback ID
  576. * @arg @ref HAL_UART_TX_COMPLETE_CB_ID Tx Complete Callback ID
  577. * @arg @ref HAL_UART_RX_HALFCOMPLETE_CB_ID Rx Half Complete Callback ID
  578. * @arg @ref HAL_UART_RX_COMPLETE_CB_ID Rx Complete Callback ID
  579. * @arg @ref HAL_UART_ERROR_CB_ID Error Callback ID
  580. * @arg @ref HAL_UART_ABORT_COMPLETE_CB_ID Abort Complete Callback ID
  581. * @arg @ref HAL_UART_ABORT_TRANSMIT_COMPLETE_CB_ID Abort Transmit Complete Callback ID
  582. * @arg @ref HAL_UART_ABORT_RECEIVE_COMPLETE_CB_ID Abort Receive Complete Callback ID
  583. * @arg @ref HAL_UART_WAKEUP_CB_ID Wakeup Callback ID
  584. * @arg @ref HAL_UART_MSPINIT_CB_ID MspInit Callback ID
  585. * @arg @ref HAL_UART_MSPDEINIT_CB_ID MspDeInit Callback ID
  586. * @param pCallback pointer to the Callback function
  587. * @retval HAL status
  588. */
  589. HAL_StatusTypeDef HAL_UART_RegisterCallback(UART_HandleTypeDef *huart, HAL_UART_CallbackIDTypeDef CallbackID,
  590. pUART_CallbackTypeDef pCallback)
  591. {
  592. HAL_StatusTypeDef status = HAL_OK;
  593. if (pCallback == NULL)
  594. {
  595. huart->ErrorCode |= HAL_UART_ERROR_INVALID_CALLBACK;
  596. return HAL_ERROR;
  597. }
  598. __HAL_LOCK(huart);
  599. if (huart->gState == HAL_UART_STATE_READY)
  600. {
  601. switch (CallbackID)
  602. {
  603. case HAL_UART_TX_HALFCOMPLETE_CB_ID :
  604. huart->TxHalfCpltCallback = pCallback;
  605. break;
  606. case HAL_UART_TX_COMPLETE_CB_ID :
  607. huart->TxCpltCallback = pCallback;
  608. break;
  609. case HAL_UART_RX_HALFCOMPLETE_CB_ID :
  610. huart->RxHalfCpltCallback = pCallback;
  611. break;
  612. case HAL_UART_RX_COMPLETE_CB_ID :
  613. huart->RxCpltCallback = pCallback;
  614. break;
  615. case HAL_UART_ERROR_CB_ID :
  616. huart->ErrorCallback = pCallback;
  617. break;
  618. case HAL_UART_ABORT_COMPLETE_CB_ID :
  619. huart->AbortCpltCallback = pCallback;
  620. break;
  621. case HAL_UART_ABORT_TRANSMIT_COMPLETE_CB_ID :
  622. huart->AbortTransmitCpltCallback = pCallback;
  623. break;
  624. case HAL_UART_ABORT_RECEIVE_COMPLETE_CB_ID :
  625. huart->AbortReceiveCpltCallback = pCallback;
  626. break;
  627. #if defined(USART_CR1_UESM)
  628. #if defined(USART_CR3_WUFIE)
  629. case HAL_UART_WAKEUP_CB_ID :
  630. huart->WakeupCallback = pCallback;
  631. break;
  632. #endif /* USART_CR3_WUFIE */
  633. #endif /* USART_CR1_UESM */
  634. case HAL_UART_MSPINIT_CB_ID :
  635. huart->MspInitCallback = pCallback;
  636. break;
  637. case HAL_UART_MSPDEINIT_CB_ID :
  638. huart->MspDeInitCallback = pCallback;
  639. break;
  640. default :
  641. huart->ErrorCode |= HAL_UART_ERROR_INVALID_CALLBACK;
  642. status = HAL_ERROR;
  643. break;
  644. }
  645. }
  646. else if (huart->gState == HAL_UART_STATE_RESET)
  647. {
  648. switch (CallbackID)
  649. {
  650. case HAL_UART_MSPINIT_CB_ID :
  651. huart->MspInitCallback = pCallback;
  652. break;
  653. case HAL_UART_MSPDEINIT_CB_ID :
  654. huart->MspDeInitCallback = pCallback;
  655. break;
  656. default :
  657. huart->ErrorCode |= HAL_UART_ERROR_INVALID_CALLBACK;
  658. status = HAL_ERROR;
  659. break;
  660. }
  661. }
  662. else
  663. {
  664. huart->ErrorCode |= HAL_UART_ERROR_INVALID_CALLBACK;
  665. status = HAL_ERROR;
  666. }
  667. __HAL_UNLOCK(huart);
  668. return status;
  669. }
  670. /**
  671. * @brief Unregister an UART Callback
  672. * UART callaback is redirected to the weak predefined callback
  673. * @param huart uart handle
  674. * @param CallbackID ID of the callback to be unregistered
  675. * This parameter can be one of the following values:
  676. * @arg @ref HAL_UART_TX_HALFCOMPLETE_CB_ID Tx Half Complete Callback ID
  677. * @arg @ref HAL_UART_TX_COMPLETE_CB_ID Tx Complete Callback ID
  678. * @arg @ref HAL_UART_RX_HALFCOMPLETE_CB_ID Rx Half Complete Callback ID
  679. * @arg @ref HAL_UART_RX_COMPLETE_CB_ID Rx Complete Callback ID
  680. * @arg @ref HAL_UART_ERROR_CB_ID Error Callback ID
  681. * @arg @ref HAL_UART_ABORT_COMPLETE_CB_ID Abort Complete Callback ID
  682. * @arg @ref HAL_UART_ABORT_TRANSMIT_COMPLETE_CB_ID Abort Transmit Complete Callback ID
  683. * @arg @ref HAL_UART_ABORT_RECEIVE_COMPLETE_CB_ID Abort Receive Complete Callback ID
  684. * @arg @ref HAL_UART_WAKEUP_CB_ID Wakeup Callback ID
  685. * @arg @ref HAL_UART_MSPINIT_CB_ID MspInit Callback ID
  686. * @arg @ref HAL_UART_MSPDEINIT_CB_ID MspDeInit Callback ID
  687. * @retval HAL status
  688. */
  689. HAL_StatusTypeDef HAL_UART_UnRegisterCallback(UART_HandleTypeDef *huart, HAL_UART_CallbackIDTypeDef CallbackID)
  690. {
  691. HAL_StatusTypeDef status = HAL_OK;
  692. __HAL_LOCK(huart);
  693. if (HAL_UART_STATE_READY == huart->gState)
  694. {
  695. switch (CallbackID)
  696. {
  697. case HAL_UART_TX_HALFCOMPLETE_CB_ID :
  698. huart->TxHalfCpltCallback = HAL_UART_TxHalfCpltCallback; /* Legacy weak TxHalfCpltCallback */
  699. break;
  700. case HAL_UART_TX_COMPLETE_CB_ID :
  701. huart->TxCpltCallback = HAL_UART_TxCpltCallback; /* Legacy weak TxCpltCallback */
  702. break;
  703. case HAL_UART_RX_HALFCOMPLETE_CB_ID :
  704. huart->RxHalfCpltCallback = HAL_UART_RxHalfCpltCallback; /* Legacy weak RxHalfCpltCallback */
  705. break;
  706. case HAL_UART_RX_COMPLETE_CB_ID :
  707. huart->RxCpltCallback = HAL_UART_RxCpltCallback; /* Legacy weak RxCpltCallback */
  708. break;
  709. case HAL_UART_ERROR_CB_ID :
  710. huart->ErrorCallback = HAL_UART_ErrorCallback; /* Legacy weak ErrorCallback */
  711. break;
  712. case HAL_UART_ABORT_COMPLETE_CB_ID :
  713. huart->AbortCpltCallback = HAL_UART_AbortCpltCallback; /* Legacy weak AbortCpltCallback */
  714. break;
  715. case HAL_UART_ABORT_TRANSMIT_COMPLETE_CB_ID :
  716. huart->AbortTransmitCpltCallback = HAL_UART_AbortTransmitCpltCallback; /* Legacy weak
  717. AbortTransmitCpltCallback */
  718. break;
  719. case HAL_UART_ABORT_RECEIVE_COMPLETE_CB_ID :
  720. huart->AbortReceiveCpltCallback = HAL_UART_AbortReceiveCpltCallback; /* Legacy weak
  721. AbortReceiveCpltCallback */
  722. break;
  723. #if defined(USART_CR1_UESM)
  724. #if defined(USART_CR3_WUFIE)
  725. case HAL_UART_WAKEUP_CB_ID :
  726. huart->WakeupCallback = HAL_UARTEx_WakeupCallback; /* Legacy weak WakeupCallback */
  727. break;
  728. #endif /* USART_CR3_WUFIE */
  729. #endif /* USART_CR1_UESM */
  730. case HAL_UART_MSPINIT_CB_ID :
  731. huart->MspInitCallback = HAL_UART_MspInit; /* Legacy weak MspInitCallback */
  732. break;
  733. case HAL_UART_MSPDEINIT_CB_ID :
  734. huart->MspDeInitCallback = HAL_UART_MspDeInit; /* Legacy weak MspDeInitCallback */
  735. break;
  736. default :
  737. huart->ErrorCode |= HAL_UART_ERROR_INVALID_CALLBACK;
  738. status = HAL_ERROR;
  739. break;
  740. }
  741. }
  742. else if (HAL_UART_STATE_RESET == huart->gState)
  743. {
  744. switch (CallbackID)
  745. {
  746. case HAL_UART_MSPINIT_CB_ID :
  747. huart->MspInitCallback = HAL_UART_MspInit;
  748. break;
  749. case HAL_UART_MSPDEINIT_CB_ID :
  750. huart->MspDeInitCallback = HAL_UART_MspDeInit;
  751. break;
  752. default :
  753. huart->ErrorCode |= HAL_UART_ERROR_INVALID_CALLBACK;
  754. status = HAL_ERROR;
  755. break;
  756. }
  757. }
  758. else
  759. {
  760. huart->ErrorCode |= HAL_UART_ERROR_INVALID_CALLBACK;
  761. status = HAL_ERROR;
  762. }
  763. __HAL_UNLOCK(huart);
  764. return status;
  765. }
  766. /**
  767. * @brief Register a User UART Rx Event Callback
  768. * To be used instead of the weak predefined callback
  769. * @param huart Uart handle
  770. * @param pCallback Pointer to the Rx Event Callback function
  771. * @retval HAL status
  772. */
  773. HAL_StatusTypeDef HAL_UART_RegisterRxEventCallback(UART_HandleTypeDef *huart, pUART_RxEventCallbackTypeDef pCallback)
  774. {
  775. HAL_StatusTypeDef status = HAL_OK;
  776. if (pCallback == NULL)
  777. {
  778. huart->ErrorCode |= HAL_UART_ERROR_INVALID_CALLBACK;
  779. return HAL_ERROR;
  780. }
  781. /* Process locked */
  782. __HAL_LOCK(huart);
  783. if (huart->gState == HAL_UART_STATE_READY)
  784. {
  785. huart->RxEventCallback = pCallback;
  786. }
  787. else
  788. {
  789. huart->ErrorCode |= HAL_UART_ERROR_INVALID_CALLBACK;
  790. status = HAL_ERROR;
  791. }
  792. /* Release Lock */
  793. __HAL_UNLOCK(huart);
  794. return status;
  795. }
  796. /**
  797. * @brief UnRegister the UART Rx Event Callback
  798. * UART Rx Event Callback is redirected to the weak HAL_UARTEx_RxEventCallback() predefined callback
  799. * @param huart Uart handle
  800. * @retval HAL status
  801. */
  802. HAL_StatusTypeDef HAL_UART_UnRegisterRxEventCallback(UART_HandleTypeDef *huart)
  803. {
  804. HAL_StatusTypeDef status = HAL_OK;
  805. /* Process locked */
  806. __HAL_LOCK(huart);
  807. if (huart->gState == HAL_UART_STATE_READY)
  808. {
  809. huart->RxEventCallback = HAL_UARTEx_RxEventCallback; /* Legacy weak UART Rx Event Callback */
  810. }
  811. else
  812. {
  813. huart->ErrorCode |= HAL_UART_ERROR_INVALID_CALLBACK;
  814. status = HAL_ERROR;
  815. }
  816. /* Release Lock */
  817. __HAL_UNLOCK(huart);
  818. return status;
  819. }
  820. #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
  821. /**
  822. * @}
  823. */
  824. /** @defgroup UART_Exported_Functions_Group2 IO operation functions
  825. * @brief UART Transmit/Receive functions
  826. *
  827. @verbatim
  828. ===============================================================================
  829. ##### IO operation functions #####
  830. ===============================================================================
  831. This subsection provides a set of functions allowing to manage the UART asynchronous
  832. and Half duplex data transfers.
  833. (#) There are two mode of transfer:
  834. (+) Blocking mode: The communication is performed in polling mode.
  835. The HAL status of all data processing is returned by the same function
  836. after finishing transfer.
  837. (+) Non-Blocking mode: The communication is performed using Interrupts
  838. or DMA, These API's return the HAL status.
  839. The end of the data processing will be indicated through the
  840. dedicated UART IRQ when using Interrupt mode or the DMA IRQ when
  841. using DMA mode.
  842. The HAL_UART_TxCpltCallback(), HAL_UART_RxCpltCallback() user callbacks
  843. will be executed respectively at the end of the transmit or Receive process
  844. The HAL_UART_ErrorCallback()user callback will be executed when a communication error is detected
  845. (#) Blocking mode API's are :
  846. (+) HAL_UART_Transmit()
  847. (+) HAL_UART_Receive()
  848. (#) Non-Blocking mode API's with Interrupt are :
  849. (+) HAL_UART_Transmit_IT()
  850. (+) HAL_UART_Receive_IT()
  851. (+) HAL_UART_IRQHandler()
  852. (#) Non-Blocking mode API's with DMA are :
  853. (+) HAL_UART_Transmit_DMA()
  854. (+) HAL_UART_Receive_DMA()
  855. (+) HAL_UART_DMAPause()
  856. (+) HAL_UART_DMAResume()
  857. (+) HAL_UART_DMAStop()
  858. (#) A set of Transfer Complete Callbacks are provided in Non_Blocking mode:
  859. (+) HAL_UART_TxHalfCpltCallback()
  860. (+) HAL_UART_TxCpltCallback()
  861. (+) HAL_UART_RxHalfCpltCallback()
  862. (+) HAL_UART_RxCpltCallback()
  863. (+) HAL_UART_ErrorCallback()
  864. (#) Non-Blocking mode transfers could be aborted using Abort API's :
  865. (+) HAL_UART_Abort()
  866. (+) HAL_UART_AbortTransmit()
  867. (+) HAL_UART_AbortReceive()
  868. (+) HAL_UART_Abort_IT()
  869. (+) HAL_UART_AbortTransmit_IT()
  870. (+) HAL_UART_AbortReceive_IT()
  871. (#) For Abort services based on interrupts (HAL_UART_Abortxxx_IT), a set of Abort Complete Callbacks are provided:
  872. (+) HAL_UART_AbortCpltCallback()
  873. (+) HAL_UART_AbortTransmitCpltCallback()
  874. (+) HAL_UART_AbortReceiveCpltCallback()
  875. (#) A Rx Event Reception Callback (Rx event notification) is available for Non_Blocking modes of enhanced
  876. reception services:
  877. (+) HAL_UARTEx_RxEventCallback()
  878. #if defined(USART_CR1_UESM)
  879. (#) Wakeup from Stop mode Callback:
  880. (+) HAL_UARTEx_WakeupCallback()
  881. #endif
  882. (#) In Non-Blocking mode transfers, possible errors are split into 2 categories.
  883. Errors are handled as follows :
  884. (+) Error is considered as Recoverable and non blocking : Transfer could go till end, but error severity is
  885. to be evaluated by user : this concerns Frame Error, Parity Error or Noise Error
  886. in Interrupt mode reception .
  887. Received character is then retrieved and stored in Rx buffer, Error code is set to allow user
  888. to identify error type, and HAL_UART_ErrorCallback() user callback is executed.
  889. Transfer is kept ongoing on UART side.
  890. If user wants to abort it, Abort services should be called by user.
  891. (+) Error is considered as Blocking : Transfer could not be completed properly and is aborted.
  892. This concerns Overrun Error In Interrupt mode reception and all errors in DMA mode.
  893. Error code is set to allow user to identify error type, and HAL_UART_ErrorCallback()
  894. user callback is executed.
  895. -@- In the Half duplex communication, it is forbidden to run the transmit
  896. and receive process in parallel, the UART state HAL_UART_STATE_BUSY_TX_RX can't be useful.
  897. @endverbatim
  898. * @{
  899. */
  900. /**
  901. * @brief Send an amount of data in blocking mode.
  902. * @note When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
  903. * the sent data is handled as a set of u16. In this case, Size must indicate the number
  904. * of u16 provided through pData.
  905. * @param huart UART handle.
  906. * @param pData Pointer to data buffer (u8 or u16 data elements).
  907. * @param Size Amount of data elements (u8 or u16) to be sent.
  908. * @param Timeout Timeout duration.
  909. * @retval HAL status
  910. */
  911. HAL_StatusTypeDef HAL_UART_Transmit(UART_HandleTypeDef *huart, const uint8_t *pData, uint16_t Size, uint32_t Timeout)
  912. {
  913. const uint8_t *pdata8bits;
  914. const uint16_t *pdata16bits;
  915. uint32_t tickstart;
  916. /* Check that a Tx process is not already ongoing */
  917. if (huart->gState == HAL_UART_STATE_READY)
  918. {
  919. if ((pData == NULL) || (Size == 0U))
  920. {
  921. return HAL_ERROR;
  922. }
  923. huart->ErrorCode = HAL_UART_ERROR_NONE;
  924. huart->gState = HAL_UART_STATE_BUSY_TX;
  925. /* Init tickstart for timeout management */
  926. tickstart = HAL_GetTick();
  927. huart->TxXferSize = Size;
  928. huart->TxXferCount = Size;
  929. /* In case of 9bits/No Parity transfer, pData needs to be handled as a uint16_t pointer */
  930. if ((huart->Init.WordLength == UART_WORDLENGTH_9B) && (huart->Init.Parity == UART_PARITY_NONE))
  931. {
  932. pdata8bits = NULL;
  933. pdata16bits = (const uint16_t *) pData;
  934. }
  935. else
  936. {
  937. pdata8bits = pData;
  938. pdata16bits = NULL;
  939. }
  940. while (huart->TxXferCount > 0U)
  941. {
  942. if (UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_TXE, RESET, tickstart, Timeout) != HAL_OK)
  943. {
  944. return HAL_TIMEOUT;
  945. }
  946. if (pdata8bits == NULL)
  947. {
  948. huart->Instance->TDR = (uint16_t)(*pdata16bits & 0x01FFU);
  949. pdata16bits++;
  950. }
  951. else
  952. {
  953. huart->Instance->TDR = (uint8_t)(*pdata8bits & 0xFFU);
  954. pdata8bits++;
  955. }
  956. huart->TxXferCount--;
  957. }
  958. if (UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_TC, RESET, tickstart, Timeout) != HAL_OK)
  959. {
  960. return HAL_TIMEOUT;
  961. }
  962. /* At end of Tx process, restore huart->gState to Ready */
  963. huart->gState = HAL_UART_STATE_READY;
  964. return HAL_OK;
  965. }
  966. else
  967. {
  968. return HAL_BUSY;
  969. }
  970. }
  971. /**
  972. * @brief Receive an amount of data in blocking mode.
  973. * @note When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
  974. * the received data is handled as a set of u16. In this case, Size must indicate the number
  975. * of u16 available through pData.
  976. * @param huart UART handle.
  977. * @param pData Pointer to data buffer (u8 or u16 data elements).
  978. * @param Size Amount of data elements (u8 or u16) to be received.
  979. * @param Timeout Timeout duration.
  980. * @retval HAL status
  981. */
  982. HAL_StatusTypeDef HAL_UART_Receive(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint32_t Timeout)
  983. {
  984. uint8_t *pdata8bits;
  985. uint16_t *pdata16bits;
  986. uint16_t uhMask;
  987. uint32_t tickstart;
  988. /* Check that a Rx process is not already ongoing */
  989. if (huart->RxState == HAL_UART_STATE_READY)
  990. {
  991. if ((pData == NULL) || (Size == 0U))
  992. {
  993. return HAL_ERROR;
  994. }
  995. huart->ErrorCode = HAL_UART_ERROR_NONE;
  996. huart->RxState = HAL_UART_STATE_BUSY_RX;
  997. huart->ReceptionType = HAL_UART_RECEPTION_STANDARD;
  998. /* Init tickstart for timeout management */
  999. tickstart = HAL_GetTick();
  1000. huart->RxXferSize = Size;
  1001. huart->RxXferCount = Size;
  1002. /* Computation of UART mask to apply to RDR register */
  1003. UART_MASK_COMPUTATION(huart);
  1004. uhMask = huart->Mask;
  1005. /* In case of 9bits/No Parity transfer, pRxData needs to be handled as a uint16_t pointer */
  1006. if ((huart->Init.WordLength == UART_WORDLENGTH_9B) && (huart->Init.Parity == UART_PARITY_NONE))
  1007. {
  1008. pdata8bits = NULL;
  1009. pdata16bits = (uint16_t *) pData;
  1010. }
  1011. else
  1012. {
  1013. pdata8bits = pData;
  1014. pdata16bits = NULL;
  1015. }
  1016. /* as long as data have to be received */
  1017. while (huart->RxXferCount > 0U)
  1018. {
  1019. if (UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_RXNE, RESET, tickstart, Timeout) != HAL_OK)
  1020. {
  1021. return HAL_TIMEOUT;
  1022. }
  1023. if (pdata8bits == NULL)
  1024. {
  1025. *pdata16bits = (uint16_t)(huart->Instance->RDR & uhMask);
  1026. pdata16bits++;
  1027. }
  1028. else
  1029. {
  1030. *pdata8bits = (uint8_t)(huart->Instance->RDR & (uint8_t)uhMask);
  1031. pdata8bits++;
  1032. }
  1033. huart->RxXferCount--;
  1034. }
  1035. /* At end of Rx process, restore huart->RxState to Ready */
  1036. huart->RxState = HAL_UART_STATE_READY;
  1037. return HAL_OK;
  1038. }
  1039. else
  1040. {
  1041. return HAL_BUSY;
  1042. }
  1043. }
  1044. /**
  1045. * @brief Send an amount of data in interrupt mode.
  1046. * @note When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
  1047. * the sent data is handled as a set of u16. In this case, Size must indicate the number
  1048. * of u16 provided through pData.
  1049. * @param huart UART handle.
  1050. * @param pData Pointer to data buffer (u8 or u16 data elements).
  1051. * @param Size Amount of data elements (u8 or u16) to be sent.
  1052. * @retval HAL status
  1053. */
  1054. HAL_StatusTypeDef HAL_UART_Transmit_IT(UART_HandleTypeDef *huart, const uint8_t *pData, uint16_t Size)
  1055. {
  1056. /* Check that a Tx process is not already ongoing */
  1057. if (huart->gState == HAL_UART_STATE_READY)
  1058. {
  1059. if ((pData == NULL) || (Size == 0U))
  1060. {
  1061. return HAL_ERROR;
  1062. }
  1063. huart->pTxBuffPtr = pData;
  1064. huart->TxXferSize = Size;
  1065. huart->TxXferCount = Size;
  1066. huart->TxISR = NULL;
  1067. huart->ErrorCode = HAL_UART_ERROR_NONE;
  1068. huart->gState = HAL_UART_STATE_BUSY_TX;
  1069. /* Set the Tx ISR function pointer according to the data word length */
  1070. if ((huart->Init.WordLength == UART_WORDLENGTH_9B) && (huart->Init.Parity == UART_PARITY_NONE))
  1071. {
  1072. huart->TxISR = UART_TxISR_16BIT;
  1073. }
  1074. else
  1075. {
  1076. huart->TxISR = UART_TxISR_8BIT;
  1077. }
  1078. /* Enable the Transmit Data Register Empty interrupt */
  1079. ATOMIC_SET_BIT(huart->Instance->CR1, USART_CR1_TXEIE);
  1080. return HAL_OK;
  1081. }
  1082. else
  1083. {
  1084. return HAL_BUSY;
  1085. }
  1086. }
  1087. /**
  1088. * @brief Receive an amount of data in interrupt mode.
  1089. * @note When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
  1090. * the received data is handled as a set of u16. In this case, Size must indicate the number
  1091. * of u16 available through pData.
  1092. * @param huart UART handle.
  1093. * @param pData Pointer to data buffer (u8 or u16 data elements).
  1094. * @param Size Amount of data elements (u8 or u16) to be received.
  1095. * @retval HAL status
  1096. */
  1097. HAL_StatusTypeDef HAL_UART_Receive_IT(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
  1098. {
  1099. /* Check that a Rx process is not already ongoing */
  1100. if (huart->RxState == HAL_UART_STATE_READY)
  1101. {
  1102. if ((pData == NULL) || (Size == 0U))
  1103. {
  1104. return HAL_ERROR;
  1105. }
  1106. /* Set Reception type to Standard reception */
  1107. huart->ReceptionType = HAL_UART_RECEPTION_STANDARD;
  1108. /* Check that USART RTOEN bit is set */
  1109. if (READ_BIT(huart->Instance->CR2, USART_CR2_RTOEN) != 0U)
  1110. {
  1111. /* Enable the UART Receiver Timeout Interrupt */
  1112. ATOMIC_SET_BIT(huart->Instance->CR1, USART_CR1_RTOIE);
  1113. }
  1114. return (UART_Start_Receive_IT(huart, pData, Size));
  1115. }
  1116. else
  1117. {
  1118. return HAL_BUSY;
  1119. }
  1120. }
  1121. /**
  1122. * @brief Send an amount of data in DMA mode.
  1123. * @note When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
  1124. * the sent data is handled as a set of u16. In this case, Size must indicate the number
  1125. * of u16 provided through pData.
  1126. * @param huart UART handle.
  1127. * @param pData Pointer to data buffer (u8 or u16 data elements).
  1128. * @param Size Amount of data elements (u8 or u16) to be sent.
  1129. * @retval HAL status
  1130. */
  1131. HAL_StatusTypeDef HAL_UART_Transmit_DMA(UART_HandleTypeDef *huart, const uint8_t *pData, uint16_t Size)
  1132. {
  1133. /* Check that a Tx process is not already ongoing */
  1134. if (huart->gState == HAL_UART_STATE_READY)
  1135. {
  1136. if ((pData == NULL) || (Size == 0U))
  1137. {
  1138. return HAL_ERROR;
  1139. }
  1140. huart->pTxBuffPtr = pData;
  1141. huart->TxXferSize = Size;
  1142. huart->TxXferCount = Size;
  1143. huart->ErrorCode = HAL_UART_ERROR_NONE;
  1144. huart->gState = HAL_UART_STATE_BUSY_TX;
  1145. if (huart->hdmatx != NULL)
  1146. {
  1147. /* Set the UART DMA transfer complete callback */
  1148. huart->hdmatx->XferCpltCallback = UART_DMATransmitCplt;
  1149. /* Set the UART DMA Half transfer complete callback */
  1150. huart->hdmatx->XferHalfCpltCallback = UART_DMATxHalfCplt;
  1151. /* Set the DMA error callback */
  1152. huart->hdmatx->XferErrorCallback = UART_DMAError;
  1153. /* Set the DMA abort callback */
  1154. huart->hdmatx->XferAbortCallback = NULL;
  1155. /* Enable the UART transmit DMA channel */
  1156. if (HAL_DMA_Start_IT(huart->hdmatx, (uint32_t)huart->pTxBuffPtr, (uint32_t)&huart->Instance->TDR, Size) != HAL_OK)
  1157. {
  1158. /* Set error code to DMA */
  1159. huart->ErrorCode = HAL_UART_ERROR_DMA;
  1160. /* Restore huart->gState to ready */
  1161. huart->gState = HAL_UART_STATE_READY;
  1162. return HAL_ERROR;
  1163. }
  1164. }
  1165. /* Clear the TC flag in the ICR register */
  1166. __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_TCF);
  1167. /* Enable the DMA transfer for transmit request by setting the DMAT bit
  1168. in the UART CR3 register */
  1169. ATOMIC_SET_BIT(huart->Instance->CR3, USART_CR3_DMAT);
  1170. return HAL_OK;
  1171. }
  1172. else
  1173. {
  1174. return HAL_BUSY;
  1175. }
  1176. }
  1177. /**
  1178. * @brief Receive an amount of data in DMA mode.
  1179. * @note When the UART parity is enabled (PCE = 1), the received data contain
  1180. * the parity bit (MSB position).
  1181. * @note When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
  1182. * the received data is handled as a set of u16. In this case, Size must indicate the number
  1183. * of u16 available through pData.
  1184. * @param huart UART handle.
  1185. * @param pData Pointer to data buffer (u8 or u16 data elements).
  1186. * @param Size Amount of data elements (u8 or u16) to be received.
  1187. * @retval HAL status
  1188. */
  1189. HAL_StatusTypeDef HAL_UART_Receive_DMA(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
  1190. {
  1191. /* Check that a Rx process is not already ongoing */
  1192. if (huart->RxState == HAL_UART_STATE_READY)
  1193. {
  1194. if ((pData == NULL) || (Size == 0U))
  1195. {
  1196. return HAL_ERROR;
  1197. }
  1198. /* Set Reception type to Standard reception */
  1199. huart->ReceptionType = HAL_UART_RECEPTION_STANDARD;
  1200. /* Check that USART RTOEN bit is set */
  1201. if (READ_BIT(huart->Instance->CR2, USART_CR2_RTOEN) != 0U)
  1202. {
  1203. /* Enable the UART Receiver Timeout Interrupt */
  1204. ATOMIC_SET_BIT(huart->Instance->CR1, USART_CR1_RTOIE);
  1205. }
  1206. return (UART_Start_Receive_DMA(huart, pData, Size));
  1207. }
  1208. else
  1209. {
  1210. return HAL_BUSY;
  1211. }
  1212. }
  1213. /**
  1214. * @brief Pause the DMA Transfer.
  1215. * @param huart UART handle.
  1216. * @retval HAL status
  1217. */
  1218. HAL_StatusTypeDef HAL_UART_DMAPause(UART_HandleTypeDef *huart)
  1219. {
  1220. const HAL_UART_StateTypeDef gstate = huart->gState;
  1221. const HAL_UART_StateTypeDef rxstate = huart->RxState;
  1222. if ((HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT)) &&
  1223. (gstate == HAL_UART_STATE_BUSY_TX))
  1224. {
  1225. /* Disable the UART DMA Tx request */
  1226. ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAT);
  1227. }
  1228. if ((HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR)) &&
  1229. (rxstate == HAL_UART_STATE_BUSY_RX))
  1230. {
  1231. /* Disable PE and ERR (Frame error, noise error, overrun error) interrupts */
  1232. ATOMIC_CLEAR_BIT(huart->Instance->CR1, USART_CR1_PEIE);
  1233. ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
  1234. /* Disable the UART DMA Rx request */
  1235. ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR);
  1236. }
  1237. return HAL_OK;
  1238. }
  1239. /**
  1240. * @brief Resume the DMA Transfer.
  1241. * @param huart UART handle.
  1242. * @retval HAL status
  1243. */
  1244. HAL_StatusTypeDef HAL_UART_DMAResume(UART_HandleTypeDef *huart)
  1245. {
  1246. if (huart->gState == HAL_UART_STATE_BUSY_TX)
  1247. {
  1248. /* Enable the UART DMA Tx request */
  1249. ATOMIC_SET_BIT(huart->Instance->CR3, USART_CR3_DMAT);
  1250. }
  1251. if (huart->RxState == HAL_UART_STATE_BUSY_RX)
  1252. {
  1253. /* Clear the Overrun flag before resuming the Rx transfer */
  1254. __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_OREF);
  1255. /* Re-enable PE and ERR (Frame error, noise error, overrun error) interrupts */
  1256. if (huart->Init.Parity != UART_PARITY_NONE)
  1257. {
  1258. ATOMIC_SET_BIT(huart->Instance->CR1, USART_CR1_PEIE);
  1259. }
  1260. ATOMIC_SET_BIT(huart->Instance->CR3, USART_CR3_EIE);
  1261. /* Enable the UART DMA Rx request */
  1262. ATOMIC_SET_BIT(huart->Instance->CR3, USART_CR3_DMAR);
  1263. }
  1264. return HAL_OK;
  1265. }
  1266. /**
  1267. * @brief Stop the DMA Transfer.
  1268. * @param huart UART handle.
  1269. * @retval HAL status
  1270. */
  1271. HAL_StatusTypeDef HAL_UART_DMAStop(UART_HandleTypeDef *huart)
  1272. {
  1273. /* The Lock is not implemented on this API to allow the user application
  1274. to call the HAL UART API under callbacks HAL_UART_TxCpltCallback() / HAL_UART_RxCpltCallback() /
  1275. HAL_UART_TxHalfCpltCallback / HAL_UART_RxHalfCpltCallback:
  1276. indeed, when HAL_DMA_Abort() API is called, the DMA TX/RX Transfer or Half Transfer complete
  1277. interrupt is generated if the DMA transfer interruption occurs at the middle or at the end of
  1278. the stream and the corresponding call back is executed. */
  1279. const HAL_UART_StateTypeDef gstate = huart->gState;
  1280. const HAL_UART_StateTypeDef rxstate = huart->RxState;
  1281. /* Stop UART DMA Tx request if ongoing */
  1282. if ((HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT)) &&
  1283. (gstate == HAL_UART_STATE_BUSY_TX))
  1284. {
  1285. ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAT);
  1286. /* Abort the UART DMA Tx channel */
  1287. if (huart->hdmatx != NULL)
  1288. {
  1289. if (HAL_DMA_Abort(huart->hdmatx) != HAL_OK)
  1290. {
  1291. if (HAL_DMA_GetError(huart->hdmatx) == HAL_DMA_ERROR_TIMEOUT)
  1292. {
  1293. /* Set error code to DMA */
  1294. huart->ErrorCode = HAL_UART_ERROR_DMA;
  1295. return HAL_TIMEOUT;
  1296. }
  1297. }
  1298. }
  1299. UART_EndTxTransfer(huart);
  1300. }
  1301. /* Stop UART DMA Rx request if ongoing */
  1302. if ((HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR)) &&
  1303. (rxstate == HAL_UART_STATE_BUSY_RX))
  1304. {
  1305. ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR);
  1306. /* Abort the UART DMA Rx channel */
  1307. if (huart->hdmarx != NULL)
  1308. {
  1309. if (HAL_DMA_Abort(huart->hdmarx) != HAL_OK)
  1310. {
  1311. if (HAL_DMA_GetError(huart->hdmarx) == HAL_DMA_ERROR_TIMEOUT)
  1312. {
  1313. /* Set error code to DMA */
  1314. huart->ErrorCode = HAL_UART_ERROR_DMA;
  1315. return HAL_TIMEOUT;
  1316. }
  1317. }
  1318. }
  1319. UART_EndRxTransfer(huart);
  1320. }
  1321. return HAL_OK;
  1322. }
  1323. /**
  1324. * @brief Abort ongoing transfers (blocking mode).
  1325. * @param huart UART handle.
  1326. * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
  1327. * This procedure performs following operations :
  1328. * - Disable UART Interrupts (Tx and Rx)
  1329. * - Disable the DMA transfer in the peripheral register (if enabled)
  1330. * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
  1331. * - Set handle State to READY
  1332. * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
  1333. * @retval HAL status
  1334. */
  1335. HAL_StatusTypeDef HAL_UART_Abort(UART_HandleTypeDef *huart)
  1336. {
  1337. /* Disable TXEIE, TCIE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
  1338. ATOMIC_CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE | USART_CR1_TXEIE | USART_CR1_TCIE));
  1339. ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
  1340. /* If Reception till IDLE event was ongoing, disable IDLEIE interrupt */
  1341. if (huart->ReceptionType == HAL_UART_RECEPTION_TOIDLE)
  1342. {
  1343. ATOMIC_CLEAR_BIT(huart->Instance->CR1, (USART_CR1_IDLEIE));
  1344. }
  1345. /* Abort the UART DMA Tx channel if enabled */
  1346. if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT))
  1347. {
  1348. /* Disable the UART DMA Tx request if enabled */
  1349. ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAT);
  1350. /* Abort the UART DMA Tx channel : use blocking DMA Abort API (no callback) */
  1351. if (huart->hdmatx != NULL)
  1352. {
  1353. /* Set the UART DMA Abort callback to Null.
  1354. No call back execution at end of DMA abort procedure */
  1355. huart->hdmatx->XferAbortCallback = NULL;
  1356. if (HAL_DMA_Abort(huart->hdmatx) != HAL_OK)
  1357. {
  1358. if (HAL_DMA_GetError(huart->hdmatx) == HAL_DMA_ERROR_TIMEOUT)
  1359. {
  1360. /* Set error code to DMA */
  1361. huart->ErrorCode = HAL_UART_ERROR_DMA;
  1362. return HAL_TIMEOUT;
  1363. }
  1364. }
  1365. }
  1366. }
  1367. /* Abort the UART DMA Rx channel if enabled */
  1368. if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR))
  1369. {
  1370. /* Disable the UART DMA Rx request if enabled */
  1371. ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR);
  1372. /* Abort the UART DMA Rx channel : use blocking DMA Abort API (no callback) */
  1373. if (huart->hdmarx != NULL)
  1374. {
  1375. /* Set the UART DMA Abort callback to Null.
  1376. No call back execution at end of DMA abort procedure */
  1377. huart->hdmarx->XferAbortCallback = NULL;
  1378. if (HAL_DMA_Abort(huart->hdmarx) != HAL_OK)
  1379. {
  1380. if (HAL_DMA_GetError(huart->hdmarx) == HAL_DMA_ERROR_TIMEOUT)
  1381. {
  1382. /* Set error code to DMA */
  1383. huart->ErrorCode = HAL_UART_ERROR_DMA;
  1384. return HAL_TIMEOUT;
  1385. }
  1386. }
  1387. }
  1388. }
  1389. /* Reset Tx and Rx transfer counters */
  1390. huart->TxXferCount = 0U;
  1391. huart->RxXferCount = 0U;
  1392. /* Clear the Error flags in the ICR register */
  1393. __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_OREF | UART_CLEAR_NEF | UART_CLEAR_PEF | UART_CLEAR_FEF);
  1394. /* Discard the received data */
  1395. __HAL_UART_SEND_REQ(huart, UART_RXDATA_FLUSH_REQUEST);
  1396. /* Restore huart->gState and huart->RxState to Ready */
  1397. huart->gState = HAL_UART_STATE_READY;
  1398. huart->RxState = HAL_UART_STATE_READY;
  1399. huart->ReceptionType = HAL_UART_RECEPTION_STANDARD;
  1400. huart->ErrorCode = HAL_UART_ERROR_NONE;
  1401. return HAL_OK;
  1402. }
  1403. /**
  1404. * @brief Abort ongoing Transmit transfer (blocking mode).
  1405. * @param huart UART handle.
  1406. * @note This procedure could be used for aborting any ongoing Tx transfer started in Interrupt or DMA mode.
  1407. * This procedure performs following operations :
  1408. * - Disable UART Interrupts (Tx)
  1409. * - Disable the DMA transfer in the peripheral register (if enabled)
  1410. * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
  1411. * - Set handle State to READY
  1412. * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
  1413. * @retval HAL status
  1414. */
  1415. HAL_StatusTypeDef HAL_UART_AbortTransmit(UART_HandleTypeDef *huart)
  1416. {
  1417. /* Disable TXEIE and TCIE interrupts */
  1418. ATOMIC_CLEAR_BIT(huart->Instance->CR1, (USART_CR1_TXEIE | USART_CR1_TCIE));
  1419. /* Abort the UART DMA Tx channel if enabled */
  1420. if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT))
  1421. {
  1422. /* Disable the UART DMA Tx request if enabled */
  1423. ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAT);
  1424. /* Abort the UART DMA Tx channel : use blocking DMA Abort API (no callback) */
  1425. if (huart->hdmatx != NULL)
  1426. {
  1427. /* Set the UART DMA Abort callback to Null.
  1428. No call back execution at end of DMA abort procedure */
  1429. huart->hdmatx->XferAbortCallback = NULL;
  1430. if (HAL_DMA_Abort(huart->hdmatx) != HAL_OK)
  1431. {
  1432. if (HAL_DMA_GetError(huart->hdmatx) == HAL_DMA_ERROR_TIMEOUT)
  1433. {
  1434. /* Set error code to DMA */
  1435. huart->ErrorCode = HAL_UART_ERROR_DMA;
  1436. return HAL_TIMEOUT;
  1437. }
  1438. }
  1439. }
  1440. }
  1441. /* Reset Tx transfer counter */
  1442. huart->TxXferCount = 0U;
  1443. /* Restore huart->gState to Ready */
  1444. huart->gState = HAL_UART_STATE_READY;
  1445. return HAL_OK;
  1446. }
  1447. /**
  1448. * @brief Abort ongoing Receive transfer (blocking mode).
  1449. * @param huart UART handle.
  1450. * @note This procedure could be used for aborting any ongoing Rx transfer started in Interrupt or DMA mode.
  1451. * This procedure performs following operations :
  1452. * - Disable UART Interrupts (Rx)
  1453. * - Disable the DMA transfer in the peripheral register (if enabled)
  1454. * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
  1455. * - Set handle State to READY
  1456. * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
  1457. * @retval HAL status
  1458. */
  1459. HAL_StatusTypeDef HAL_UART_AbortReceive(UART_HandleTypeDef *huart)
  1460. {
  1461. /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
  1462. ATOMIC_CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE));
  1463. ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
  1464. /* If Reception till IDLE event was ongoing, disable IDLEIE interrupt */
  1465. if (huart->ReceptionType == HAL_UART_RECEPTION_TOIDLE)
  1466. {
  1467. ATOMIC_CLEAR_BIT(huart->Instance->CR1, (USART_CR1_IDLEIE));
  1468. }
  1469. /* Abort the UART DMA Rx channel if enabled */
  1470. if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR))
  1471. {
  1472. /* Disable the UART DMA Rx request if enabled */
  1473. ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR);
  1474. /* Abort the UART DMA Rx channel : use blocking DMA Abort API (no callback) */
  1475. if (huart->hdmarx != NULL)
  1476. {
  1477. /* Set the UART DMA Abort callback to Null.
  1478. No call back execution at end of DMA abort procedure */
  1479. huart->hdmarx->XferAbortCallback = NULL;
  1480. if (HAL_DMA_Abort(huart->hdmarx) != HAL_OK)
  1481. {
  1482. if (HAL_DMA_GetError(huart->hdmarx) == HAL_DMA_ERROR_TIMEOUT)
  1483. {
  1484. /* Set error code to DMA */
  1485. huart->ErrorCode = HAL_UART_ERROR_DMA;
  1486. return HAL_TIMEOUT;
  1487. }
  1488. }
  1489. }
  1490. }
  1491. /* Reset Rx transfer counter */
  1492. huart->RxXferCount = 0U;
  1493. /* Clear the Error flags in the ICR register */
  1494. __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_OREF | UART_CLEAR_NEF | UART_CLEAR_PEF | UART_CLEAR_FEF);
  1495. /* Discard the received data */
  1496. __HAL_UART_SEND_REQ(huart, UART_RXDATA_FLUSH_REQUEST);
  1497. /* Restore huart->RxState to Ready */
  1498. huart->RxState = HAL_UART_STATE_READY;
  1499. huart->ReceptionType = HAL_UART_RECEPTION_STANDARD;
  1500. return HAL_OK;
  1501. }
  1502. /**
  1503. * @brief Abort ongoing transfers (Interrupt mode).
  1504. * @param huart UART handle.
  1505. * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
  1506. * This procedure performs following operations :
  1507. * - Disable UART Interrupts (Tx and Rx)
  1508. * - Disable the DMA transfer in the peripheral register (if enabled)
  1509. * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
  1510. * - Set handle State to READY
  1511. * - At abort completion, call user abort complete callback
  1512. * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be
  1513. * considered as completed only when user abort complete callback is executed (not when exiting function).
  1514. * @retval HAL status
  1515. */
  1516. HAL_StatusTypeDef HAL_UART_Abort_IT(UART_HandleTypeDef *huart)
  1517. {
  1518. uint32_t abortcplt = 1U;
  1519. /* Disable interrupts */
  1520. ATOMIC_CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE | USART_CR1_TXEIE | USART_CR1_TCIE));
  1521. ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
  1522. /* If Reception till IDLE event was ongoing, disable IDLEIE interrupt */
  1523. if (huart->ReceptionType == HAL_UART_RECEPTION_TOIDLE)
  1524. {
  1525. ATOMIC_CLEAR_BIT(huart->Instance->CR1, (USART_CR1_IDLEIE));
  1526. }
  1527. /* If DMA Tx and/or DMA Rx Handles are associated to UART Handle, DMA Abort complete callbacks should be initialised
  1528. before any call to DMA Abort functions */
  1529. /* DMA Tx Handle is valid */
  1530. if (huart->hdmatx != NULL)
  1531. {
  1532. /* Set DMA Abort Complete callback if UART DMA Tx request if enabled.
  1533. Otherwise, set it to NULL */
  1534. if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT))
  1535. {
  1536. huart->hdmatx->XferAbortCallback = UART_DMATxAbortCallback;
  1537. }
  1538. else
  1539. {
  1540. huart->hdmatx->XferAbortCallback = NULL;
  1541. }
  1542. }
  1543. /* DMA Rx Handle is valid */
  1544. if (huart->hdmarx != NULL)
  1545. {
  1546. /* Set DMA Abort Complete callback if UART DMA Rx request if enabled.
  1547. Otherwise, set it to NULL */
  1548. if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR))
  1549. {
  1550. huart->hdmarx->XferAbortCallback = UART_DMARxAbortCallback;
  1551. }
  1552. else
  1553. {
  1554. huart->hdmarx->XferAbortCallback = NULL;
  1555. }
  1556. }
  1557. /* Abort the UART DMA Tx channel if enabled */
  1558. if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT))
  1559. {
  1560. /* Disable DMA Tx at UART level */
  1561. ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAT);
  1562. /* Abort the UART DMA Tx channel : use non blocking DMA Abort API (callback) */
  1563. if (huart->hdmatx != NULL)
  1564. {
  1565. /* UART Tx DMA Abort callback has already been initialised :
  1566. will lead to call HAL_UART_AbortCpltCallback() at end of DMA abort procedure */
  1567. /* Abort DMA TX */
  1568. if (HAL_DMA_Abort_IT(huart->hdmatx) != HAL_OK)
  1569. {
  1570. huart->hdmatx->XferAbortCallback = NULL;
  1571. }
  1572. else
  1573. {
  1574. abortcplt = 0U;
  1575. }
  1576. }
  1577. }
  1578. /* Abort the UART DMA Rx channel if enabled */
  1579. if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR))
  1580. {
  1581. /* Disable the UART DMA Rx request if enabled */
  1582. ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR);
  1583. /* Abort the UART DMA Rx channel : use non blocking DMA Abort API (callback) */
  1584. if (huart->hdmarx != NULL)
  1585. {
  1586. /* UART Rx DMA Abort callback has already been initialised :
  1587. will lead to call HAL_UART_AbortCpltCallback() at end of DMA abort procedure */
  1588. /* Abort DMA RX */
  1589. if (HAL_DMA_Abort_IT(huart->hdmarx) != HAL_OK)
  1590. {
  1591. huart->hdmarx->XferAbortCallback = NULL;
  1592. abortcplt = 1U;
  1593. }
  1594. else
  1595. {
  1596. abortcplt = 0U;
  1597. }
  1598. }
  1599. }
  1600. /* if no DMA abort complete callback execution is required => call user Abort Complete callback */
  1601. if (abortcplt == 1U)
  1602. {
  1603. /* Reset Tx and Rx transfer counters */
  1604. huart->TxXferCount = 0U;
  1605. huart->RxXferCount = 0U;
  1606. /* Clear ISR function pointers */
  1607. huart->RxISR = NULL;
  1608. huart->TxISR = NULL;
  1609. /* Reset errorCode */
  1610. huart->ErrorCode = HAL_UART_ERROR_NONE;
  1611. /* Clear the Error flags in the ICR register */
  1612. __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_OREF | UART_CLEAR_NEF | UART_CLEAR_PEF | UART_CLEAR_FEF);
  1613. /* Discard the received data */
  1614. __HAL_UART_SEND_REQ(huart, UART_RXDATA_FLUSH_REQUEST);
  1615. /* Restore huart->gState and huart->RxState to Ready */
  1616. huart->gState = HAL_UART_STATE_READY;
  1617. huart->RxState = HAL_UART_STATE_READY;
  1618. huart->ReceptionType = HAL_UART_RECEPTION_STANDARD;
  1619. /* As no DMA to be aborted, call directly user Abort complete callback */
  1620. #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
  1621. /* Call registered Abort complete callback */
  1622. huart->AbortCpltCallback(huart);
  1623. #else
  1624. /* Call legacy weak Abort complete callback */
  1625. HAL_UART_AbortCpltCallback(huart);
  1626. #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
  1627. }
  1628. return HAL_OK;
  1629. }
  1630. /**
  1631. * @brief Abort ongoing Transmit transfer (Interrupt mode).
  1632. * @param huart UART handle.
  1633. * @note This procedure could be used for aborting any ongoing Tx transfer started in Interrupt or DMA mode.
  1634. * This procedure performs following operations :
  1635. * - Disable UART Interrupts (Tx)
  1636. * - Disable the DMA transfer in the peripheral register (if enabled)
  1637. * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
  1638. * - Set handle State to READY
  1639. * - At abort completion, call user abort complete callback
  1640. * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be
  1641. * considered as completed only when user abort complete callback is executed (not when exiting function).
  1642. * @retval HAL status
  1643. */
  1644. HAL_StatusTypeDef HAL_UART_AbortTransmit_IT(UART_HandleTypeDef *huart)
  1645. {
  1646. /* Disable interrupts */
  1647. ATOMIC_CLEAR_BIT(huart->Instance->CR1, (USART_CR1_TXEIE | USART_CR1_TCIE));
  1648. /* Abort the UART DMA Tx channel if enabled */
  1649. if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT))
  1650. {
  1651. /* Disable the UART DMA Tx request if enabled */
  1652. ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAT);
  1653. /* Abort the UART DMA Tx channel : use non blocking DMA Abort API (callback) */
  1654. if (huart->hdmatx != NULL)
  1655. {
  1656. /* Set the UART DMA Abort callback :
  1657. will lead to call HAL_UART_AbortCpltCallback() at end of DMA abort procedure */
  1658. huart->hdmatx->XferAbortCallback = UART_DMATxOnlyAbortCallback;
  1659. /* Abort DMA TX */
  1660. if (HAL_DMA_Abort_IT(huart->hdmatx) != HAL_OK)
  1661. {
  1662. /* Call Directly huart->hdmatx->XferAbortCallback function in case of error */
  1663. huart->hdmatx->XferAbortCallback(huart->hdmatx);
  1664. }
  1665. }
  1666. else
  1667. {
  1668. /* Reset Tx transfer counter */
  1669. huart->TxXferCount = 0U;
  1670. /* Clear TxISR function pointers */
  1671. huart->TxISR = NULL;
  1672. /* Restore huart->gState to Ready */
  1673. huart->gState = HAL_UART_STATE_READY;
  1674. /* As no DMA to be aborted, call directly user Abort complete callback */
  1675. #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
  1676. /* Call registered Abort Transmit Complete Callback */
  1677. huart->AbortTransmitCpltCallback(huart);
  1678. #else
  1679. /* Call legacy weak Abort Transmit Complete Callback */
  1680. HAL_UART_AbortTransmitCpltCallback(huart);
  1681. #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
  1682. }
  1683. }
  1684. else
  1685. {
  1686. /* Reset Tx transfer counter */
  1687. huart->TxXferCount = 0U;
  1688. /* Clear TxISR function pointers */
  1689. huart->TxISR = NULL;
  1690. /* Restore huart->gState to Ready */
  1691. huart->gState = HAL_UART_STATE_READY;
  1692. /* As no DMA to be aborted, call directly user Abort complete callback */
  1693. #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
  1694. /* Call registered Abort Transmit Complete Callback */
  1695. huart->AbortTransmitCpltCallback(huart);
  1696. #else
  1697. /* Call legacy weak Abort Transmit Complete Callback */
  1698. HAL_UART_AbortTransmitCpltCallback(huart);
  1699. #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
  1700. }
  1701. return HAL_OK;
  1702. }
  1703. /**
  1704. * @brief Abort ongoing Receive transfer (Interrupt mode).
  1705. * @param huart UART handle.
  1706. * @note This procedure could be used for aborting any ongoing Rx transfer started in Interrupt or DMA mode.
  1707. * This procedure performs following operations :
  1708. * - Disable UART Interrupts (Rx)
  1709. * - Disable the DMA transfer in the peripheral register (if enabled)
  1710. * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
  1711. * - Set handle State to READY
  1712. * - At abort completion, call user abort complete callback
  1713. * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be
  1714. * considered as completed only when user abort complete callback is executed (not when exiting function).
  1715. * @retval HAL status
  1716. */
  1717. HAL_StatusTypeDef HAL_UART_AbortReceive_IT(UART_HandleTypeDef *huart)
  1718. {
  1719. /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
  1720. ATOMIC_CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE));
  1721. ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
  1722. /* If Reception till IDLE event was ongoing, disable IDLEIE interrupt */
  1723. if (huart->ReceptionType == HAL_UART_RECEPTION_TOIDLE)
  1724. {
  1725. ATOMIC_CLEAR_BIT(huart->Instance->CR1, (USART_CR1_IDLEIE));
  1726. }
  1727. /* Abort the UART DMA Rx channel if enabled */
  1728. if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR))
  1729. {
  1730. /* Disable the UART DMA Rx request if enabled */
  1731. ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR);
  1732. /* Abort the UART DMA Rx channel : use non blocking DMA Abort API (callback) */
  1733. if (huart->hdmarx != NULL)
  1734. {
  1735. /* Set the UART DMA Abort callback :
  1736. will lead to call HAL_UART_AbortCpltCallback() at end of DMA abort procedure */
  1737. huart->hdmarx->XferAbortCallback = UART_DMARxOnlyAbortCallback;
  1738. /* Abort DMA RX */
  1739. if (HAL_DMA_Abort_IT(huart->hdmarx) != HAL_OK)
  1740. {
  1741. /* Call Directly huart->hdmarx->XferAbortCallback function in case of error */
  1742. huart->hdmarx->XferAbortCallback(huart->hdmarx);
  1743. }
  1744. }
  1745. else
  1746. {
  1747. /* Reset Rx transfer counter */
  1748. huart->RxXferCount = 0U;
  1749. /* Clear RxISR function pointer */
  1750. huart->pRxBuffPtr = NULL;
  1751. /* Clear the Error flags in the ICR register */
  1752. __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_OREF | UART_CLEAR_NEF | UART_CLEAR_PEF | UART_CLEAR_FEF);
  1753. /* Discard the received data */
  1754. __HAL_UART_SEND_REQ(huart, UART_RXDATA_FLUSH_REQUEST);
  1755. /* Restore huart->RxState to Ready */
  1756. huart->RxState = HAL_UART_STATE_READY;
  1757. huart->ReceptionType = HAL_UART_RECEPTION_STANDARD;
  1758. /* As no DMA to be aborted, call directly user Abort complete callback */
  1759. #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
  1760. /* Call registered Abort Receive Complete Callback */
  1761. huart->AbortReceiveCpltCallback(huart);
  1762. #else
  1763. /* Call legacy weak Abort Receive Complete Callback */
  1764. HAL_UART_AbortReceiveCpltCallback(huart);
  1765. #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
  1766. }
  1767. }
  1768. else
  1769. {
  1770. /* Reset Rx transfer counter */
  1771. huart->RxXferCount = 0U;
  1772. /* Clear RxISR function pointer */
  1773. huart->pRxBuffPtr = NULL;
  1774. /* Clear the Error flags in the ICR register */
  1775. __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_OREF | UART_CLEAR_NEF | UART_CLEAR_PEF | UART_CLEAR_FEF);
  1776. /* Restore huart->RxState to Ready */
  1777. huart->RxState = HAL_UART_STATE_READY;
  1778. huart->ReceptionType = HAL_UART_RECEPTION_STANDARD;
  1779. /* As no DMA to be aborted, call directly user Abort complete callback */
  1780. #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
  1781. /* Call registered Abort Receive Complete Callback */
  1782. huart->AbortReceiveCpltCallback(huart);
  1783. #else
  1784. /* Call legacy weak Abort Receive Complete Callback */
  1785. HAL_UART_AbortReceiveCpltCallback(huart);
  1786. #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
  1787. }
  1788. return HAL_OK;
  1789. }
  1790. /**
  1791. * @brief Handle UART interrupt request.
  1792. * @param huart UART handle.
  1793. * @retval None
  1794. */
  1795. void HAL_UART_IRQHandler(UART_HandleTypeDef *huart)
  1796. {
  1797. uint32_t isrflags = READ_REG(huart->Instance->ISR);
  1798. uint32_t cr1its = READ_REG(huart->Instance->CR1);
  1799. uint32_t cr3its = READ_REG(huart->Instance->CR3);
  1800. uint32_t errorflags;
  1801. uint32_t errorcode;
  1802. /* If no error occurs */
  1803. errorflags = (isrflags & (uint32_t)(USART_ISR_PE | USART_ISR_FE | USART_ISR_ORE | USART_ISR_NE | USART_ISR_RTOF));
  1804. if (errorflags == 0U)
  1805. {
  1806. /* UART in mode Receiver ---------------------------------------------------*/
  1807. if (((isrflags & USART_ISR_RXNE) != 0U)
  1808. && ((cr1its & USART_CR1_RXNEIE) != 0U))
  1809. {
  1810. if (huart->RxISR != NULL)
  1811. {
  1812. huart->RxISR(huart);
  1813. }
  1814. return;
  1815. }
  1816. }
  1817. /* If some errors occur */
  1818. if ((errorflags != 0U)
  1819. && (((cr3its & USART_CR3_EIE) != 0U)
  1820. || ((cr1its & (USART_CR1_RXNEIE | USART_CR1_PEIE | USART_CR1_RTOIE)) != 0U)))
  1821. {
  1822. /* UART parity error interrupt occurred -------------------------------------*/
  1823. if (((isrflags & USART_ISR_PE) != 0U) && ((cr1its & USART_CR1_PEIE) != 0U))
  1824. {
  1825. __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_PEF);
  1826. huart->ErrorCode |= HAL_UART_ERROR_PE;
  1827. }
  1828. /* UART frame error interrupt occurred --------------------------------------*/
  1829. if (((isrflags & USART_ISR_FE) != 0U) && ((cr3its & USART_CR3_EIE) != 0U))
  1830. {
  1831. __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_FEF);
  1832. huart->ErrorCode |= HAL_UART_ERROR_FE;
  1833. }
  1834. /* UART noise error interrupt occurred --------------------------------------*/
  1835. if (((isrflags & USART_ISR_NE) != 0U) && ((cr3its & USART_CR3_EIE) != 0U))
  1836. {
  1837. __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_NEF);
  1838. huart->ErrorCode |= HAL_UART_ERROR_NE;
  1839. }
  1840. /* UART Over-Run interrupt occurred -----------------------------------------*/
  1841. if (((isrflags & USART_ISR_ORE) != 0U)
  1842. && (((cr1its & USART_CR1_RXNEIE) != 0U) ||
  1843. ((cr3its & USART_CR3_EIE) != 0U)))
  1844. {
  1845. __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_OREF);
  1846. huart->ErrorCode |= HAL_UART_ERROR_ORE;
  1847. }
  1848. /* UART Receiver Timeout interrupt occurred ---------------------------------*/
  1849. if (((isrflags & USART_ISR_RTOF) != 0U) && ((cr1its & USART_CR1_RTOIE) != 0U))
  1850. {
  1851. __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_RTOF);
  1852. huart->ErrorCode |= HAL_UART_ERROR_RTO;
  1853. }
  1854. /* Call UART Error Call back function if need be ----------------------------*/
  1855. if (huart->ErrorCode != HAL_UART_ERROR_NONE)
  1856. {
  1857. /* UART in mode Receiver --------------------------------------------------*/
  1858. if (((isrflags & USART_ISR_RXNE) != 0U)
  1859. && ((cr1its & USART_CR1_RXNEIE) != 0U))
  1860. {
  1861. if (huart->RxISR != NULL)
  1862. {
  1863. huart->RxISR(huart);
  1864. }
  1865. }
  1866. /* If Error is to be considered as blocking :
  1867. - Receiver Timeout error in Reception
  1868. - Overrun error in Reception
  1869. - any error occurs in DMA mode reception
  1870. */
  1871. errorcode = huart->ErrorCode;
  1872. if ((HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR)) ||
  1873. ((errorcode & (HAL_UART_ERROR_RTO | HAL_UART_ERROR_ORE)) != 0U))
  1874. {
  1875. /* Blocking error : transfer is aborted
  1876. Set the UART state ready to be able to start again the process,
  1877. Disable Rx Interrupts, and disable Rx DMA request, if ongoing */
  1878. UART_EndRxTransfer(huart);
  1879. /* Abort the UART DMA Rx channel if enabled */
  1880. if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR))
  1881. {
  1882. /* Disable the UART DMA Rx request if enabled */
  1883. ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR);
  1884. /* Abort the UART DMA Rx channel */
  1885. if (huart->hdmarx != NULL)
  1886. {
  1887. /* Set the UART DMA Abort callback :
  1888. will lead to call HAL_UART_ErrorCallback() at end of DMA abort procedure */
  1889. huart->hdmarx->XferAbortCallback = UART_DMAAbortOnError;
  1890. /* Abort DMA RX */
  1891. if (HAL_DMA_Abort_IT(huart->hdmarx) != HAL_OK)
  1892. {
  1893. /* Call Directly huart->hdmarx->XferAbortCallback function in case of error */
  1894. huart->hdmarx->XferAbortCallback(huart->hdmarx);
  1895. }
  1896. }
  1897. else
  1898. {
  1899. /* Call user error callback */
  1900. #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
  1901. /*Call registered error callback*/
  1902. huart->ErrorCallback(huart);
  1903. #else
  1904. /*Call legacy weak error callback*/
  1905. HAL_UART_ErrorCallback(huart);
  1906. #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
  1907. }
  1908. }
  1909. else
  1910. {
  1911. /* Call user error callback */
  1912. #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
  1913. /*Call registered error callback*/
  1914. huart->ErrorCallback(huart);
  1915. #else
  1916. /*Call legacy weak error callback*/
  1917. HAL_UART_ErrorCallback(huart);
  1918. #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
  1919. }
  1920. }
  1921. else
  1922. {
  1923. /* Non Blocking error : transfer could go on.
  1924. Error is notified to user through user error callback */
  1925. #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
  1926. /*Call registered error callback*/
  1927. huart->ErrorCallback(huart);
  1928. #else
  1929. /*Call legacy weak error callback*/
  1930. HAL_UART_ErrorCallback(huart);
  1931. #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
  1932. huart->ErrorCode = HAL_UART_ERROR_NONE;
  1933. }
  1934. }
  1935. return;
  1936. } /* End if some error occurs */
  1937. /* Check current reception Mode :
  1938. If Reception till IDLE event has been selected : */
  1939. if ((huart->ReceptionType == HAL_UART_RECEPTION_TOIDLE)
  1940. && ((isrflags & USART_ISR_IDLE) != 0U)
  1941. && ((cr1its & USART_ISR_IDLE) != 0U))
  1942. {
  1943. __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_IDLEF);
  1944. /* Check if DMA mode is enabled in UART */
  1945. if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR))
  1946. {
  1947. /* DMA mode enabled */
  1948. /* Check received length : If all expected data are received, do nothing,
  1949. (DMA cplt callback will be called).
  1950. Otherwise, if at least one data has already been received, IDLE event is to be notified to user */
  1951. uint16_t nb_remaining_rx_data = (uint16_t) __HAL_DMA_GET_COUNTER(huart->hdmarx);
  1952. if ((nb_remaining_rx_data > 0U)
  1953. && (nb_remaining_rx_data < huart->RxXferSize))
  1954. {
  1955. /* Reception is not complete */
  1956. huart->RxXferCount = nb_remaining_rx_data;
  1957. /* In Normal mode, end DMA xfer and HAL UART Rx process*/
  1958. if (huart->hdmarx->Init.Mode != DMA_CIRCULAR)
  1959. {
  1960. /* Disable PE and ERR (Frame error, noise error, overrun error) interrupts */
  1961. ATOMIC_CLEAR_BIT(huart->Instance->CR1, USART_CR1_PEIE);
  1962. ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
  1963. /* Disable the DMA transfer for the receiver request by resetting the DMAR bit
  1964. in the UART CR3 register */
  1965. ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR);
  1966. /* At end of Rx process, restore huart->RxState to Ready */
  1967. huart->RxState = HAL_UART_STATE_READY;
  1968. huart->ReceptionType = HAL_UART_RECEPTION_STANDARD;
  1969. ATOMIC_CLEAR_BIT(huart->Instance->CR1, USART_CR1_IDLEIE);
  1970. /* Last bytes received, so no need as the abort is immediate */
  1971. (void)HAL_DMA_Abort(huart->hdmarx);
  1972. }
  1973. /* Initialize type of RxEvent that correspond to RxEvent callback execution;
  1974. In this case, Rx Event type is Idle Event */
  1975. huart->RxEventType = HAL_UART_RXEVENT_IDLE;
  1976. #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
  1977. /*Call registered Rx Event callback*/
  1978. huart->RxEventCallback(huart, (huart->RxXferSize - huart->RxXferCount));
  1979. #else
  1980. /*Call legacy weak Rx Event callback*/
  1981. HAL_UARTEx_RxEventCallback(huart, (huart->RxXferSize - huart->RxXferCount));
  1982. #endif /* (USE_HAL_UART_REGISTER_CALLBACKS) */
  1983. }
  1984. return;
  1985. }
  1986. else
  1987. {
  1988. /* DMA mode not enabled */
  1989. /* Check received length : If all expected data are received, do nothing.
  1990. Otherwise, if at least one data has already been received, IDLE event is to be notified to user */
  1991. uint16_t nb_rx_data = huart->RxXferSize - huart->RxXferCount;
  1992. if ((huart->RxXferCount > 0U)
  1993. && (nb_rx_data > 0U))
  1994. {
  1995. /* Disable the UART Parity Error Interrupt and RXNE interrupts */
  1996. ATOMIC_CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE));
  1997. /* Disable the UART Error Interrupt: (Frame error, noise error, overrun error) */
  1998. ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
  1999. /* Rx process is completed, restore huart->RxState to Ready */
  2000. huart->RxState = HAL_UART_STATE_READY;
  2001. huart->ReceptionType = HAL_UART_RECEPTION_STANDARD;
  2002. /* Clear RxISR function pointer */
  2003. huart->RxISR = NULL;
  2004. ATOMIC_CLEAR_BIT(huart->Instance->CR1, USART_CR1_IDLEIE);
  2005. /* Initialize type of RxEvent that correspond to RxEvent callback execution;
  2006. In this case, Rx Event type is Idle Event */
  2007. huart->RxEventType = HAL_UART_RXEVENT_IDLE;
  2008. #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
  2009. /*Call registered Rx complete callback*/
  2010. huart->RxEventCallback(huart, nb_rx_data);
  2011. #else
  2012. /*Call legacy weak Rx Event callback*/
  2013. HAL_UARTEx_RxEventCallback(huart, nb_rx_data);
  2014. #endif /* (USE_HAL_UART_REGISTER_CALLBACKS) */
  2015. }
  2016. return;
  2017. }
  2018. }
  2019. #if defined(USART_CR1_UESM)
  2020. #if defined(USART_CR3_WUFIE)
  2021. /* UART wakeup from Stop mode interrupt occurred ---------------------------*/
  2022. if (((isrflags & USART_ISR_WUF) != 0U) && ((cr3its & USART_CR3_WUFIE) != 0U))
  2023. {
  2024. __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_WUF);
  2025. /* UART Rx state is not reset as a reception process might be ongoing.
  2026. If UART handle state fields need to be reset to READY, this could be done in Wakeup callback */
  2027. #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
  2028. /* Call registered Wakeup Callback */
  2029. huart->WakeupCallback(huart);
  2030. #else
  2031. /* Call legacy weak Wakeup Callback */
  2032. HAL_UARTEx_WakeupCallback(huart);
  2033. #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
  2034. return;
  2035. }
  2036. #endif /* USART_CR3_WUFIE */
  2037. #endif /* USART_CR1_UESM */
  2038. /* UART in mode Transmitter ------------------------------------------------*/
  2039. if (((isrflags & USART_ISR_TXE) != 0U)
  2040. && ((cr1its & USART_CR1_TXEIE) != 0U))
  2041. {
  2042. if (huart->TxISR != NULL)
  2043. {
  2044. huart->TxISR(huart);
  2045. }
  2046. return;
  2047. }
  2048. /* UART in mode Transmitter (transmission end) -----------------------------*/
  2049. if (((isrflags & USART_ISR_TC) != 0U) && ((cr1its & USART_CR1_TCIE) != 0U))
  2050. {
  2051. UART_EndTransmit_IT(huart);
  2052. return;
  2053. }
  2054. }
  2055. /**
  2056. * @brief Tx Transfer completed callback.
  2057. * @param huart UART handle.
  2058. * @retval None
  2059. */
  2060. __weak void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
  2061. {
  2062. /* Prevent unused argument(s) compilation warning */
  2063. UNUSED(huart);
  2064. /* NOTE : This function should not be modified, when the callback is needed,
  2065. the HAL_UART_TxCpltCallback can be implemented in the user file.
  2066. */
  2067. }
  2068. /**
  2069. * @brief Tx Half Transfer completed callback.
  2070. * @param huart UART handle.
  2071. * @retval None
  2072. */
  2073. __weak void HAL_UART_TxHalfCpltCallback(UART_HandleTypeDef *huart)
  2074. {
  2075. /* Prevent unused argument(s) compilation warning */
  2076. UNUSED(huart);
  2077. /* NOTE: This function should not be modified, when the callback is needed,
  2078. the HAL_UART_TxHalfCpltCallback can be implemented in the user file.
  2079. */
  2080. }
  2081. /**
  2082. * @brief Rx Transfer completed callback.
  2083. * @param huart UART handle.
  2084. * @retval None
  2085. */
  2086. __weak void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
  2087. {
  2088. /* Prevent unused argument(s) compilation warning */
  2089. UNUSED(huart);
  2090. /* NOTE : This function should not be modified, when the callback is needed,
  2091. the HAL_UART_RxCpltCallback can be implemented in the user file.
  2092. */
  2093. }
  2094. /**
  2095. * @brief Rx Half Transfer completed callback.
  2096. * @param huart UART handle.
  2097. * @retval None
  2098. */
  2099. __weak void HAL_UART_RxHalfCpltCallback(UART_HandleTypeDef *huart)
  2100. {
  2101. /* Prevent unused argument(s) compilation warning */
  2102. UNUSED(huart);
  2103. /* NOTE: This function should not be modified, when the callback is needed,
  2104. the HAL_UART_RxHalfCpltCallback can be implemented in the user file.
  2105. */
  2106. }
  2107. /**
  2108. * @brief UART error callback.
  2109. * @param huart UART handle.
  2110. * @retval None
  2111. */
  2112. __weak void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart)
  2113. {
  2114. /* Prevent unused argument(s) compilation warning */
  2115. UNUSED(huart);
  2116. /* NOTE : This function should not be modified, when the callback is needed,
  2117. the HAL_UART_ErrorCallback can be implemented in the user file.
  2118. */
  2119. }
  2120. /**
  2121. * @brief UART Abort Complete callback.
  2122. * @param huart UART handle.
  2123. * @retval None
  2124. */
  2125. __weak void HAL_UART_AbortCpltCallback(UART_HandleTypeDef *huart)
  2126. {
  2127. /* Prevent unused argument(s) compilation warning */
  2128. UNUSED(huart);
  2129. /* NOTE : This function should not be modified, when the callback is needed,
  2130. the HAL_UART_AbortCpltCallback can be implemented in the user file.
  2131. */
  2132. }
  2133. /**
  2134. * @brief UART Abort Complete callback.
  2135. * @param huart UART handle.
  2136. * @retval None
  2137. */
  2138. __weak void HAL_UART_AbortTransmitCpltCallback(UART_HandleTypeDef *huart)
  2139. {
  2140. /* Prevent unused argument(s) compilation warning */
  2141. UNUSED(huart);
  2142. /* NOTE : This function should not be modified, when the callback is needed,
  2143. the HAL_UART_AbortTransmitCpltCallback can be implemented in the user file.
  2144. */
  2145. }
  2146. /**
  2147. * @brief UART Abort Receive Complete callback.
  2148. * @param huart UART handle.
  2149. * @retval None
  2150. */
  2151. __weak void HAL_UART_AbortReceiveCpltCallback(UART_HandleTypeDef *huart)
  2152. {
  2153. /* Prevent unused argument(s) compilation warning */
  2154. UNUSED(huart);
  2155. /* NOTE : This function should not be modified, when the callback is needed,
  2156. the HAL_UART_AbortReceiveCpltCallback can be implemented in the user file.
  2157. */
  2158. }
  2159. /**
  2160. * @brief Reception Event Callback (Rx event notification called after use of advanced reception service).
  2161. * @param huart UART handle
  2162. * @param Size Number of data available in application reception buffer (indicates a position in
  2163. * reception buffer until which, data are available)
  2164. * @retval None
  2165. */
  2166. __weak void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size)
  2167. {
  2168. /* Prevent unused argument(s) compilation warning */
  2169. UNUSED(huart);
  2170. UNUSED(Size);
  2171. /* NOTE : This function should not be modified, when the callback is needed,
  2172. the HAL_UARTEx_RxEventCallback can be implemented in the user file.
  2173. */
  2174. }
  2175. #if defined(USART_CR1_UESM)
  2176. /**
  2177. * @brief UART wakeup from Stop mode callback.
  2178. * @param huart UART handle.
  2179. * @retval None
  2180. */
  2181. __weak void HAL_UARTEx_WakeupCallback(UART_HandleTypeDef *huart)
  2182. {
  2183. /* Prevent unused argument(s) compilation warning */
  2184. UNUSED(huart);
  2185. /* NOTE : This function should not be modified, when the callback is needed,
  2186. the HAL_UARTEx_WakeupCallback can be implemented in the user file.
  2187. */
  2188. }
  2189. #endif /* USART_CR1_UESM */
  2190. /**
  2191. * @}
  2192. */
  2193. /** @defgroup UART_Exported_Functions_Group3 Peripheral Control functions
  2194. * @brief UART control functions
  2195. *
  2196. @verbatim
  2197. ===============================================================================
  2198. ##### Peripheral Control functions #####
  2199. ===============================================================================
  2200. [..]
  2201. This subsection provides a set of functions allowing to control the UART.
  2202. (+) HAL_UART_ReceiverTimeout_Config() API allows to configure the receiver timeout value on the fly
  2203. (+) HAL_UART_EnableReceiverTimeout() API enables the receiver timeout feature
  2204. (+) HAL_UART_DisableReceiverTimeout() API disables the receiver timeout feature
  2205. (+) HAL_MultiProcessor_EnableMuteMode() API enables mute mode
  2206. (+) HAL_MultiProcessor_DisableMuteMode() API disables mute mode
  2207. (+) HAL_MultiProcessor_EnterMuteMode() API enters mute mode
  2208. (+) UART_SetConfig() API configures the UART peripheral
  2209. (+) UART_AdvFeatureConfig() API optionally configures the UART advanced features
  2210. (+) UART_CheckIdleState() API ensures that TEACK and/or REACK are set after initialization
  2211. (+) HAL_HalfDuplex_EnableTransmitter() API disables receiver and enables transmitter
  2212. (+) HAL_HalfDuplex_EnableReceiver() API disables transmitter and enables receiver
  2213. (+) HAL_LIN_SendBreak() API transmits the break characters
  2214. @endverbatim
  2215. * @{
  2216. */
  2217. /**
  2218. * @brief Update on the fly the receiver timeout value in RTOR register.
  2219. * @param huart Pointer to a UART_HandleTypeDef structure that contains
  2220. * the configuration information for the specified UART module.
  2221. * @param TimeoutValue receiver timeout value in number of baud blocks. The timeout
  2222. * value must be less or equal to 0x0FFFFFFFF.
  2223. * @retval None
  2224. */
  2225. void HAL_UART_ReceiverTimeout_Config(UART_HandleTypeDef *huart, uint32_t TimeoutValue)
  2226. {
  2227. assert_param(IS_UART_RECEIVER_TIMEOUT_VALUE(TimeoutValue));
  2228. MODIFY_REG(huart->Instance->RTOR, USART_RTOR_RTO, TimeoutValue);
  2229. }
  2230. /**
  2231. * @brief Enable the UART receiver timeout feature.
  2232. * @param huart Pointer to a UART_HandleTypeDef structure that contains
  2233. * the configuration information for the specified UART module.
  2234. * @retval HAL status
  2235. */
  2236. HAL_StatusTypeDef HAL_UART_EnableReceiverTimeout(UART_HandleTypeDef *huart)
  2237. {
  2238. if (huart->gState == HAL_UART_STATE_READY)
  2239. {
  2240. /* Process Locked */
  2241. __HAL_LOCK(huart);
  2242. huart->gState = HAL_UART_STATE_BUSY;
  2243. /* Set the USART RTOEN bit */
  2244. SET_BIT(huart->Instance->CR2, USART_CR2_RTOEN);
  2245. huart->gState = HAL_UART_STATE_READY;
  2246. /* Process Unlocked */
  2247. __HAL_UNLOCK(huart);
  2248. return HAL_OK;
  2249. }
  2250. else
  2251. {
  2252. return HAL_BUSY;
  2253. }
  2254. }
  2255. /**
  2256. * @brief Disable the UART receiver timeout feature.
  2257. * @param huart Pointer to a UART_HandleTypeDef structure that contains
  2258. * the configuration information for the specified UART module.
  2259. * @retval HAL status
  2260. */
  2261. HAL_StatusTypeDef HAL_UART_DisableReceiverTimeout(UART_HandleTypeDef *huart)
  2262. {
  2263. if (huart->gState == HAL_UART_STATE_READY)
  2264. {
  2265. /* Process Locked */
  2266. __HAL_LOCK(huart);
  2267. huart->gState = HAL_UART_STATE_BUSY;
  2268. /* Clear the USART RTOEN bit */
  2269. CLEAR_BIT(huart->Instance->CR2, USART_CR2_RTOEN);
  2270. huart->gState = HAL_UART_STATE_READY;
  2271. /* Process Unlocked */
  2272. __HAL_UNLOCK(huart);
  2273. return HAL_OK;
  2274. }
  2275. else
  2276. {
  2277. return HAL_BUSY;
  2278. }
  2279. }
  2280. /**
  2281. * @brief Enable UART in mute mode (does not mean UART enters mute mode;
  2282. * to enter mute mode, HAL_MultiProcessor_EnterMuteMode() API must be called).
  2283. * @param huart UART handle.
  2284. * @retval HAL status
  2285. */
  2286. HAL_StatusTypeDef HAL_MultiProcessor_EnableMuteMode(UART_HandleTypeDef *huart)
  2287. {
  2288. __HAL_LOCK(huart);
  2289. huart->gState = HAL_UART_STATE_BUSY;
  2290. /* Enable USART mute mode by setting the MME bit in the CR1 register */
  2291. ATOMIC_SET_BIT(huart->Instance->CR1, USART_CR1_MME);
  2292. huart->gState = HAL_UART_STATE_READY;
  2293. return (UART_CheckIdleState(huart));
  2294. }
  2295. /**
  2296. * @brief Disable UART mute mode (does not mean the UART actually exits mute mode
  2297. * as it may not have been in mute mode at this very moment).
  2298. * @param huart UART handle.
  2299. * @retval HAL status
  2300. */
  2301. HAL_StatusTypeDef HAL_MultiProcessor_DisableMuteMode(UART_HandleTypeDef *huart)
  2302. {
  2303. __HAL_LOCK(huart);
  2304. huart->gState = HAL_UART_STATE_BUSY;
  2305. /* Disable USART mute mode by clearing the MME bit in the CR1 register */
  2306. ATOMIC_CLEAR_BIT(huart->Instance->CR1, USART_CR1_MME);
  2307. huart->gState = HAL_UART_STATE_READY;
  2308. return (UART_CheckIdleState(huart));
  2309. }
  2310. /**
  2311. * @brief Enter UART mute mode (means UART actually enters mute mode).
  2312. * @note To exit from mute mode, HAL_MultiProcessor_DisableMuteMode() API must be called.
  2313. * @param huart UART handle.
  2314. * @retval None
  2315. */
  2316. void HAL_MultiProcessor_EnterMuteMode(UART_HandleTypeDef *huart)
  2317. {
  2318. __HAL_UART_SEND_REQ(huart, UART_MUTE_MODE_REQUEST);
  2319. }
  2320. /**
  2321. * @brief Enable the UART transmitter and disable the UART receiver.
  2322. * @param huart UART handle.
  2323. * @retval HAL status
  2324. */
  2325. HAL_StatusTypeDef HAL_HalfDuplex_EnableTransmitter(UART_HandleTypeDef *huart)
  2326. {
  2327. __HAL_LOCK(huart);
  2328. huart->gState = HAL_UART_STATE_BUSY;
  2329. /* Clear TE and RE bits */
  2330. ATOMIC_CLEAR_BIT(huart->Instance->CR1, (USART_CR1_TE | USART_CR1_RE));
  2331. /* Enable the USART's transmit interface by setting the TE bit in the USART CR1 register */
  2332. ATOMIC_SET_BIT(huart->Instance->CR1, USART_CR1_TE);
  2333. huart->gState = HAL_UART_STATE_READY;
  2334. __HAL_UNLOCK(huart);
  2335. return HAL_OK;
  2336. }
  2337. /**
  2338. * @brief Enable the UART receiver and disable the UART transmitter.
  2339. * @param huart UART handle.
  2340. * @retval HAL status.
  2341. */
  2342. HAL_StatusTypeDef HAL_HalfDuplex_EnableReceiver(UART_HandleTypeDef *huart)
  2343. {
  2344. __HAL_LOCK(huart);
  2345. huart->gState = HAL_UART_STATE_BUSY;
  2346. /* Clear TE and RE bits */
  2347. ATOMIC_CLEAR_BIT(huart->Instance->CR1, (USART_CR1_TE | USART_CR1_RE));
  2348. /* Enable the USART's receive interface by setting the RE bit in the USART CR1 register */
  2349. ATOMIC_SET_BIT(huart->Instance->CR1, USART_CR1_RE);
  2350. huart->gState = HAL_UART_STATE_READY;
  2351. __HAL_UNLOCK(huart);
  2352. return HAL_OK;
  2353. }
  2354. /**
  2355. * @brief Transmit break characters.
  2356. * @param huart UART handle.
  2357. * @retval HAL status
  2358. */
  2359. HAL_StatusTypeDef HAL_LIN_SendBreak(UART_HandleTypeDef *huart)
  2360. {
  2361. /* Check the parameters */
  2362. assert_param(IS_UART_LIN_INSTANCE(huart->Instance));
  2363. __HAL_LOCK(huart);
  2364. huart->gState = HAL_UART_STATE_BUSY;
  2365. /* Send break characters */
  2366. __HAL_UART_SEND_REQ(huart, UART_SENDBREAK_REQUEST);
  2367. huart->gState = HAL_UART_STATE_READY;
  2368. __HAL_UNLOCK(huart);
  2369. return HAL_OK;
  2370. }
  2371. /**
  2372. * @}
  2373. */
  2374. /** @defgroup UART_Exported_Functions_Group4 Peripheral State and Error functions
  2375. * @brief UART Peripheral State functions
  2376. *
  2377. @verbatim
  2378. ==============================================================================
  2379. ##### Peripheral State and Error functions #####
  2380. ==============================================================================
  2381. [..]
  2382. This subsection provides functions allowing to :
  2383. (+) Return the UART handle state.
  2384. (+) Return the UART handle error code
  2385. @endverbatim
  2386. * @{
  2387. */
  2388. /**
  2389. * @brief Return the UART handle state.
  2390. * @param huart Pointer to a UART_HandleTypeDef structure that contains
  2391. * the configuration information for the specified UART.
  2392. * @retval HAL state
  2393. */
  2394. HAL_UART_StateTypeDef HAL_UART_GetState(const UART_HandleTypeDef *huart)
  2395. {
  2396. uint32_t temp1;
  2397. uint32_t temp2;
  2398. temp1 = huart->gState;
  2399. temp2 = huart->RxState;
  2400. return (HAL_UART_StateTypeDef)(temp1 | temp2);
  2401. }
  2402. /**
  2403. * @brief Return the UART handle error code.
  2404. * @param huart Pointer to a UART_HandleTypeDef structure that contains
  2405. * the configuration information for the specified UART.
  2406. * @retval UART Error Code
  2407. */
  2408. uint32_t HAL_UART_GetError(const UART_HandleTypeDef *huart)
  2409. {
  2410. return huart->ErrorCode;
  2411. }
  2412. /**
  2413. * @}
  2414. */
  2415. /**
  2416. * @}
  2417. */
  2418. /** @defgroup UART_Private_Functions UART Private Functions
  2419. * @{
  2420. */
  2421. /**
  2422. * @brief Initialize the callbacks to their default values.
  2423. * @param huart UART handle.
  2424. * @retval none
  2425. */
  2426. #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
  2427. void UART_InitCallbacksToDefault(UART_HandleTypeDef *huart)
  2428. {
  2429. /* Init the UART Callback settings */
  2430. huart->TxHalfCpltCallback = HAL_UART_TxHalfCpltCallback; /* Legacy weak TxHalfCpltCallback */
  2431. huart->TxCpltCallback = HAL_UART_TxCpltCallback; /* Legacy weak TxCpltCallback */
  2432. huart->RxHalfCpltCallback = HAL_UART_RxHalfCpltCallback; /* Legacy weak RxHalfCpltCallback */
  2433. huart->RxCpltCallback = HAL_UART_RxCpltCallback; /* Legacy weak RxCpltCallback */
  2434. huart->ErrorCallback = HAL_UART_ErrorCallback; /* Legacy weak ErrorCallback */
  2435. huart->AbortCpltCallback = HAL_UART_AbortCpltCallback; /* Legacy weak AbortCpltCallback */
  2436. huart->AbortTransmitCpltCallback = HAL_UART_AbortTransmitCpltCallback; /* Legacy weak AbortTransmitCpltCallback */
  2437. huart->AbortReceiveCpltCallback = HAL_UART_AbortReceiveCpltCallback; /* Legacy weak AbortReceiveCpltCallback */
  2438. #if defined(USART_CR1_UESM)
  2439. #if defined(USART_CR3_WUFIE)
  2440. huart->WakeupCallback = HAL_UARTEx_WakeupCallback; /* Legacy weak WakeupCallback */
  2441. #endif /* USART_CR3_WUFIE */
  2442. #endif /* USART_CR1_UESM */
  2443. huart->RxEventCallback = HAL_UARTEx_RxEventCallback; /* Legacy weak RxEventCallback */
  2444. }
  2445. #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
  2446. /**
  2447. * @brief Configure the UART peripheral.
  2448. * @param huart UART handle.
  2449. * @retval HAL status
  2450. */
  2451. HAL_StatusTypeDef UART_SetConfig(UART_HandleTypeDef *huart)
  2452. {
  2453. uint32_t tmpreg;
  2454. uint16_t brrtemp;
  2455. UART_ClockSourceTypeDef clocksource;
  2456. uint32_t usartdiv;
  2457. HAL_StatusTypeDef ret = HAL_OK;
  2458. uint32_t pclk;
  2459. /* Check the parameters */
  2460. assert_param(IS_UART_BAUDRATE(huart->Init.BaudRate));
  2461. assert_param(IS_UART_WORD_LENGTH(huart->Init.WordLength));
  2462. assert_param(IS_UART_STOPBITS(huart->Init.StopBits));
  2463. assert_param(IS_UART_ONE_BIT_SAMPLE(huart->Init.OneBitSampling));
  2464. assert_param(IS_UART_PARITY(huart->Init.Parity));
  2465. assert_param(IS_UART_MODE(huart->Init.Mode));
  2466. assert_param(IS_UART_HARDWARE_FLOW_CONTROL(huart->Init.HwFlowCtl));
  2467. assert_param(IS_UART_OVERSAMPLING(huart->Init.OverSampling));
  2468. /*-------------------------- USART CR1 Configuration -----------------------*/
  2469. /* Clear M, PCE, PS, TE, RE and OVER8 bits and configure
  2470. * the UART Word Length, Parity, Mode and oversampling:
  2471. * set the M bits according to huart->Init.WordLength value
  2472. * set PCE and PS bits according to huart->Init.Parity value
  2473. * set TE and RE bits according to huart->Init.Mode value
  2474. * set OVER8 bit according to huart->Init.OverSampling value */
  2475. tmpreg = (uint32_t)huart->Init.WordLength | huart->Init.Parity | huart->Init.Mode | huart->Init.OverSampling ;
  2476. MODIFY_REG(huart->Instance->CR1, USART_CR1_FIELDS, tmpreg);
  2477. /*-------------------------- USART CR2 Configuration -----------------------*/
  2478. /* Configure the UART Stop Bits: Set STOP[13:12] bits according
  2479. * to huart->Init.StopBits value */
  2480. MODIFY_REG(huart->Instance->CR2, USART_CR2_STOP, huart->Init.StopBits);
  2481. /*-------------------------- USART CR3 Configuration -----------------------*/
  2482. /* Configure
  2483. * - UART HardWare Flow Control: set CTSE and RTSE bits according
  2484. * to huart->Init.HwFlowCtl value
  2485. * - one-bit sampling method versus three samples' majority rule according
  2486. * to huart->Init.OneBitSampling (not applicable to LPUART) */
  2487. tmpreg = (uint32_t)huart->Init.HwFlowCtl;
  2488. tmpreg |= huart->Init.OneBitSampling;
  2489. MODIFY_REG(huart->Instance->CR3, USART_CR3_FIELDS, tmpreg);
  2490. /*-------------------------- USART BRR Configuration -----------------------*/
  2491. UART_GETCLOCKSOURCE(huart, clocksource);
  2492. if (huart->Init.OverSampling == UART_OVERSAMPLING_8)
  2493. {
  2494. switch (clocksource)
  2495. {
  2496. case UART_CLOCKSOURCE_PCLK1:
  2497. pclk = HAL_RCC_GetPCLK1Freq();
  2498. break;
  2499. case UART_CLOCKSOURCE_PCLK2:
  2500. pclk = HAL_RCC_GetPCLK2Freq();
  2501. break;
  2502. case UART_CLOCKSOURCE_HSI:
  2503. pclk = (uint32_t) HSI_VALUE;
  2504. break;
  2505. case UART_CLOCKSOURCE_SYSCLK:
  2506. pclk = HAL_RCC_GetSysClockFreq();
  2507. break;
  2508. case UART_CLOCKSOURCE_LSE:
  2509. pclk = (uint32_t) LSE_VALUE;
  2510. break;
  2511. default:
  2512. pclk = 0U;
  2513. ret = HAL_ERROR;
  2514. break;
  2515. }
  2516. /* USARTDIV must be greater than or equal to 0d16 */
  2517. if (pclk != 0U)
  2518. {
  2519. usartdiv = (uint32_t)(UART_DIV_SAMPLING8(pclk, huart->Init.BaudRate));
  2520. if ((usartdiv >= UART_BRR_MIN) && (usartdiv <= UART_BRR_MAX))
  2521. {
  2522. brrtemp = (uint16_t)(usartdiv & 0xFFF0U);
  2523. brrtemp |= (uint16_t)((usartdiv & (uint16_t)0x000FU) >> 1U);
  2524. huart->Instance->BRR = brrtemp;
  2525. }
  2526. else
  2527. {
  2528. ret = HAL_ERROR;
  2529. }
  2530. }
  2531. }
  2532. else
  2533. {
  2534. switch (clocksource)
  2535. {
  2536. case UART_CLOCKSOURCE_PCLK1:
  2537. pclk = HAL_RCC_GetPCLK1Freq();
  2538. break;
  2539. case UART_CLOCKSOURCE_PCLK2:
  2540. pclk = HAL_RCC_GetPCLK2Freq();
  2541. break;
  2542. case UART_CLOCKSOURCE_HSI:
  2543. pclk = (uint32_t) HSI_VALUE;
  2544. break;
  2545. case UART_CLOCKSOURCE_SYSCLK:
  2546. pclk = HAL_RCC_GetSysClockFreq();
  2547. break;
  2548. case UART_CLOCKSOURCE_LSE:
  2549. pclk = (uint32_t) LSE_VALUE;
  2550. break;
  2551. default:
  2552. pclk = 0U;
  2553. ret = HAL_ERROR;
  2554. break;
  2555. }
  2556. if (pclk != 0U)
  2557. {
  2558. /* USARTDIV must be greater than or equal to 0d16 */
  2559. usartdiv = (uint32_t)(UART_DIV_SAMPLING16(pclk, huart->Init.BaudRate));
  2560. if ((usartdiv >= UART_BRR_MIN) && (usartdiv <= UART_BRR_MAX))
  2561. {
  2562. huart->Instance->BRR = (uint16_t)usartdiv;
  2563. }
  2564. else
  2565. {
  2566. ret = HAL_ERROR;
  2567. }
  2568. }
  2569. }
  2570. /* Clear ISR function pointers */
  2571. huart->RxISR = NULL;
  2572. huart->TxISR = NULL;
  2573. return ret;
  2574. }
  2575. /**
  2576. * @brief Configure the UART peripheral advanced features.
  2577. * @param huart UART handle.
  2578. * @retval None
  2579. */
  2580. void UART_AdvFeatureConfig(UART_HandleTypeDef *huart)
  2581. {
  2582. /* Check whether the set of advanced features to configure is properly set */
  2583. assert_param(IS_UART_ADVFEATURE_INIT(huart->AdvancedInit.AdvFeatureInit));
  2584. /* if required, configure TX pin active level inversion */
  2585. if (HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_TXINVERT_INIT))
  2586. {
  2587. assert_param(IS_UART_ADVFEATURE_TXINV(huart->AdvancedInit.TxPinLevelInvert));
  2588. MODIFY_REG(huart->Instance->CR2, USART_CR2_TXINV, huart->AdvancedInit.TxPinLevelInvert);
  2589. }
  2590. /* if required, configure RX pin active level inversion */
  2591. if (HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_RXINVERT_INIT))
  2592. {
  2593. assert_param(IS_UART_ADVFEATURE_RXINV(huart->AdvancedInit.RxPinLevelInvert));
  2594. MODIFY_REG(huart->Instance->CR2, USART_CR2_RXINV, huart->AdvancedInit.RxPinLevelInvert);
  2595. }
  2596. /* if required, configure data inversion */
  2597. if (HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_DATAINVERT_INIT))
  2598. {
  2599. assert_param(IS_UART_ADVFEATURE_DATAINV(huart->AdvancedInit.DataInvert));
  2600. MODIFY_REG(huart->Instance->CR2, USART_CR2_DATAINV, huart->AdvancedInit.DataInvert);
  2601. }
  2602. /* if required, configure RX/TX pins swap */
  2603. if (HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_SWAP_INIT))
  2604. {
  2605. assert_param(IS_UART_ADVFEATURE_SWAP(huart->AdvancedInit.Swap));
  2606. MODIFY_REG(huart->Instance->CR2, USART_CR2_SWAP, huart->AdvancedInit.Swap);
  2607. }
  2608. /* if required, configure RX overrun detection disabling */
  2609. if (HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_RXOVERRUNDISABLE_INIT))
  2610. {
  2611. assert_param(IS_UART_OVERRUN(huart->AdvancedInit.OverrunDisable));
  2612. MODIFY_REG(huart->Instance->CR3, USART_CR3_OVRDIS, huart->AdvancedInit.OverrunDisable);
  2613. }
  2614. /* if required, configure DMA disabling on reception error */
  2615. if (HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_DMADISABLEONERROR_INIT))
  2616. {
  2617. assert_param(IS_UART_ADVFEATURE_DMAONRXERROR(huart->AdvancedInit.DMADisableonRxError));
  2618. MODIFY_REG(huart->Instance->CR3, USART_CR3_DDRE, huart->AdvancedInit.DMADisableonRxError);
  2619. }
  2620. /* if required, configure auto Baud rate detection scheme */
  2621. if (HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_AUTOBAUDRATE_INIT))
  2622. {
  2623. assert_param(IS_USART_AUTOBAUDRATE_DETECTION_INSTANCE(huart->Instance));
  2624. assert_param(IS_UART_ADVFEATURE_AUTOBAUDRATE(huart->AdvancedInit.AutoBaudRateEnable));
  2625. MODIFY_REG(huart->Instance->CR2, USART_CR2_ABREN, huart->AdvancedInit.AutoBaudRateEnable);
  2626. /* set auto Baudrate detection parameters if detection is enabled */
  2627. if (huart->AdvancedInit.AutoBaudRateEnable == UART_ADVFEATURE_AUTOBAUDRATE_ENABLE)
  2628. {
  2629. assert_param(IS_UART_ADVFEATURE_AUTOBAUDRATEMODE(huart->AdvancedInit.AutoBaudRateMode));
  2630. MODIFY_REG(huart->Instance->CR2, USART_CR2_ABRMODE, huart->AdvancedInit.AutoBaudRateMode);
  2631. }
  2632. }
  2633. /* if required, configure MSB first on communication line */
  2634. if (HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_MSBFIRST_INIT))
  2635. {
  2636. assert_param(IS_UART_ADVFEATURE_MSBFIRST(huart->AdvancedInit.MSBFirst));
  2637. MODIFY_REG(huart->Instance->CR2, USART_CR2_MSBFIRST, huart->AdvancedInit.MSBFirst);
  2638. }
  2639. }
  2640. /**
  2641. * @brief Check the UART Idle State.
  2642. * @param huart UART handle.
  2643. * @retval HAL status
  2644. */
  2645. HAL_StatusTypeDef UART_CheckIdleState(UART_HandleTypeDef *huart)
  2646. {
  2647. uint32_t tickstart;
  2648. /* Initialize the UART ErrorCode */
  2649. huart->ErrorCode = HAL_UART_ERROR_NONE;
  2650. /* Init tickstart for timeout management */
  2651. tickstart = HAL_GetTick();
  2652. /* Check if the Transmitter is enabled */
  2653. if ((huart->Instance->CR1 & USART_CR1_TE) == USART_CR1_TE)
  2654. {
  2655. /* Wait until TEACK flag is set */
  2656. if (UART_WaitOnFlagUntilTimeout(huart, USART_ISR_TEACK, RESET, tickstart, HAL_UART_TIMEOUT_VALUE) != HAL_OK)
  2657. {
  2658. /* Timeout occurred */
  2659. return HAL_TIMEOUT;
  2660. }
  2661. }
  2662. #if defined(USART_ISR_REACK)
  2663. /* Check if the Receiver is enabled */
  2664. if ((huart->Instance->CR1 & USART_CR1_RE) == USART_CR1_RE)
  2665. {
  2666. /* Wait until REACK flag is set */
  2667. if (UART_WaitOnFlagUntilTimeout(huart, USART_ISR_REACK, RESET, tickstart, HAL_UART_TIMEOUT_VALUE) != HAL_OK)
  2668. {
  2669. /* Timeout occurred */
  2670. return HAL_TIMEOUT;
  2671. }
  2672. }
  2673. #endif /* USART_ISR_REACK */
  2674. /* Initialize the UART State */
  2675. huart->gState = HAL_UART_STATE_READY;
  2676. huart->RxState = HAL_UART_STATE_READY;
  2677. huart->ReceptionType = HAL_UART_RECEPTION_STANDARD;
  2678. huart->RxEventType = HAL_UART_RXEVENT_TC;
  2679. __HAL_UNLOCK(huart);
  2680. return HAL_OK;
  2681. }
  2682. /**
  2683. * @brief This function handles UART Communication Timeout. It waits
  2684. * until a flag is no longer in the specified status.
  2685. * @param huart UART handle.
  2686. * @param Flag Specifies the UART flag to check
  2687. * @param Status The actual Flag status (SET or RESET)
  2688. * @param Tickstart Tick start value
  2689. * @param Timeout Timeout duration
  2690. * @retval HAL status
  2691. */
  2692. HAL_StatusTypeDef UART_WaitOnFlagUntilTimeout(UART_HandleTypeDef *huart, uint32_t Flag, FlagStatus Status,
  2693. uint32_t Tickstart, uint32_t Timeout)
  2694. {
  2695. /* Wait until flag is set */
  2696. while ((__HAL_UART_GET_FLAG(huart, Flag) ? SET : RESET) == Status)
  2697. {
  2698. /* Check for the Timeout */
  2699. if (Timeout != HAL_MAX_DELAY)
  2700. {
  2701. if (((HAL_GetTick() - Tickstart) > Timeout) || (Timeout == 0U))
  2702. {
  2703. /* Disable TXE, RXNE, PE and ERR (Frame error, noise error, overrun error)
  2704. interrupts for the interrupt process */
  2705. ATOMIC_CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE | USART_CR1_TXEIE));
  2706. ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
  2707. huart->gState = HAL_UART_STATE_READY;
  2708. huart->RxState = HAL_UART_STATE_READY;
  2709. __HAL_UNLOCK(huart);
  2710. return HAL_TIMEOUT;
  2711. }
  2712. if (READ_BIT(huart->Instance->CR1, USART_CR1_RE) != 0U)
  2713. {
  2714. if (__HAL_UART_GET_FLAG(huart, UART_FLAG_RTOF) == SET)
  2715. {
  2716. /* Clear Receiver Timeout flag*/
  2717. __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_RTOF);
  2718. /* Disable TXE, RXNE, PE and ERR (Frame error, noise error, overrun error)
  2719. interrupts for the interrupt process */
  2720. ATOMIC_CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE | USART_CR1_TXEIE));
  2721. ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
  2722. huart->gState = HAL_UART_STATE_READY;
  2723. huart->RxState = HAL_UART_STATE_READY;
  2724. huart->ErrorCode = HAL_UART_ERROR_RTO;
  2725. /* Process Unlocked */
  2726. __HAL_UNLOCK(huart);
  2727. return HAL_TIMEOUT;
  2728. }
  2729. }
  2730. }
  2731. }
  2732. return HAL_OK;
  2733. }
  2734. /**
  2735. * @brief Start Receive operation in interrupt mode.
  2736. * @note This function could be called by all HAL UART API providing reception in Interrupt mode.
  2737. * @note When calling this function, parameters validity is considered as already checked,
  2738. * i.e. Rx State, buffer address, ...
  2739. * UART Handle is assumed as Locked.
  2740. * @param huart UART handle.
  2741. * @param pData Pointer to data buffer (u8 or u16 data elements).
  2742. * @param Size Amount of data elements (u8 or u16) to be received.
  2743. * @retval HAL status
  2744. */
  2745. HAL_StatusTypeDef UART_Start_Receive_IT(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
  2746. {
  2747. huart->pRxBuffPtr = pData;
  2748. huart->RxXferSize = Size;
  2749. huart->RxXferCount = Size;
  2750. huart->RxISR = NULL;
  2751. /* Computation of UART mask to apply to RDR register */
  2752. UART_MASK_COMPUTATION(huart);
  2753. huart->ErrorCode = HAL_UART_ERROR_NONE;
  2754. huart->RxState = HAL_UART_STATE_BUSY_RX;
  2755. /* Enable the UART Error Interrupt: (Frame error, noise error, overrun error) */
  2756. ATOMIC_SET_BIT(huart->Instance->CR3, USART_CR3_EIE);
  2757. /* Set the Rx ISR function pointer according to the data word length */
  2758. if ((huart->Init.WordLength == UART_WORDLENGTH_9B) && (huart->Init.Parity == UART_PARITY_NONE))
  2759. {
  2760. huart->RxISR = UART_RxISR_16BIT;
  2761. }
  2762. else
  2763. {
  2764. huart->RxISR = UART_RxISR_8BIT;
  2765. }
  2766. /* Enable the UART Parity Error interrupt and Data Register Not Empty interrupt */
  2767. if (huart->Init.Parity != UART_PARITY_NONE)
  2768. {
  2769. ATOMIC_SET_BIT(huart->Instance->CR1, USART_CR1_PEIE | USART_CR1_RXNEIE);
  2770. }
  2771. else
  2772. {
  2773. ATOMIC_SET_BIT(huart->Instance->CR1, USART_CR1_RXNEIE);
  2774. }
  2775. return HAL_OK;
  2776. }
  2777. /**
  2778. * @brief Start Receive operation in DMA mode.
  2779. * @note This function could be called by all HAL UART API providing reception in DMA mode.
  2780. * @note When calling this function, parameters validity is considered as already checked,
  2781. * i.e. Rx State, buffer address, ...
  2782. * UART Handle is assumed as Locked.
  2783. * @param huart UART handle.
  2784. * @param pData Pointer to data buffer (u8 or u16 data elements).
  2785. * @param Size Amount of data elements (u8 or u16) to be received.
  2786. * @retval HAL status
  2787. */
  2788. HAL_StatusTypeDef UART_Start_Receive_DMA(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
  2789. {
  2790. huart->pRxBuffPtr = pData;
  2791. huart->RxXferSize = Size;
  2792. huart->ErrorCode = HAL_UART_ERROR_NONE;
  2793. huart->RxState = HAL_UART_STATE_BUSY_RX;
  2794. if (huart->hdmarx != NULL)
  2795. {
  2796. /* Set the UART DMA transfer complete callback */
  2797. huart->hdmarx->XferCpltCallback = UART_DMAReceiveCplt;
  2798. /* Set the UART DMA Half transfer complete callback */
  2799. huart->hdmarx->XferHalfCpltCallback = UART_DMARxHalfCplt;
  2800. /* Set the DMA error callback */
  2801. huart->hdmarx->XferErrorCallback = UART_DMAError;
  2802. /* Set the DMA abort callback */
  2803. huart->hdmarx->XferAbortCallback = NULL;
  2804. /* Enable the DMA channel */
  2805. if (HAL_DMA_Start_IT(huart->hdmarx, (uint32_t)&huart->Instance->RDR, (uint32_t)huart->pRxBuffPtr, Size) != HAL_OK)
  2806. {
  2807. /* Set error code to DMA */
  2808. huart->ErrorCode = HAL_UART_ERROR_DMA;
  2809. /* Restore huart->RxState to ready */
  2810. huart->RxState = HAL_UART_STATE_READY;
  2811. return HAL_ERROR;
  2812. }
  2813. }
  2814. /* Enable the UART Parity Error Interrupt */
  2815. if (huart->Init.Parity != UART_PARITY_NONE)
  2816. {
  2817. ATOMIC_SET_BIT(huart->Instance->CR1, USART_CR1_PEIE);
  2818. }
  2819. /* Enable the UART Error Interrupt: (Frame error, noise error, overrun error) */
  2820. ATOMIC_SET_BIT(huart->Instance->CR3, USART_CR3_EIE);
  2821. /* Enable the DMA transfer for the receiver request by setting the DMAR bit
  2822. in the UART CR3 register */
  2823. ATOMIC_SET_BIT(huart->Instance->CR3, USART_CR3_DMAR);
  2824. return HAL_OK;
  2825. }
  2826. /**
  2827. * @brief End ongoing Tx transfer on UART peripheral (following error detection or Transmit completion).
  2828. * @param huart UART handle.
  2829. * @retval None
  2830. */
  2831. static void UART_EndTxTransfer(UART_HandleTypeDef *huart)
  2832. {
  2833. /* Disable TXEIE and TCIE interrupts */
  2834. ATOMIC_CLEAR_BIT(huart->Instance->CR1, (USART_CR1_TXEIE | USART_CR1_TCIE));
  2835. /* At end of Tx process, restore huart->gState to Ready */
  2836. huart->gState = HAL_UART_STATE_READY;
  2837. }
  2838. /**
  2839. * @brief End ongoing Rx transfer on UART peripheral (following error detection or Reception completion).
  2840. * @param huart UART handle.
  2841. * @retval None
  2842. */
  2843. static void UART_EndRxTransfer(UART_HandleTypeDef *huart)
  2844. {
  2845. /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
  2846. ATOMIC_CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE));
  2847. ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
  2848. /* In case of reception waiting for IDLE event, disable also the IDLE IE interrupt source */
  2849. if (huart->ReceptionType == HAL_UART_RECEPTION_TOIDLE)
  2850. {
  2851. ATOMIC_CLEAR_BIT(huart->Instance->CR1, USART_CR1_IDLEIE);
  2852. }
  2853. /* At end of Rx process, restore huart->RxState to Ready */
  2854. huart->RxState = HAL_UART_STATE_READY;
  2855. huart->ReceptionType = HAL_UART_RECEPTION_STANDARD;
  2856. /* Reset RxIsr function pointer */
  2857. huart->RxISR = NULL;
  2858. }
  2859. /**
  2860. * @brief DMA UART transmit process complete callback.
  2861. * @param hdma DMA handle.
  2862. * @retval None
  2863. */
  2864. static void UART_DMATransmitCplt(DMA_HandleTypeDef *hdma)
  2865. {
  2866. UART_HandleTypeDef *huart = (UART_HandleTypeDef *)(hdma->Parent);
  2867. /* DMA Normal mode */
  2868. if (hdma->Init.Mode != DMA_CIRCULAR)
  2869. {
  2870. huart->TxXferCount = 0U;
  2871. /* Disable the DMA transfer for transmit request by resetting the DMAT bit
  2872. in the UART CR3 register */
  2873. ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAT);
  2874. /* Enable the UART Transmit Complete Interrupt */
  2875. ATOMIC_SET_BIT(huart->Instance->CR1, USART_CR1_TCIE);
  2876. }
  2877. /* DMA Circular mode */
  2878. else
  2879. {
  2880. #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
  2881. /*Call registered Tx complete callback*/
  2882. huart->TxCpltCallback(huart);
  2883. #else
  2884. /*Call legacy weak Tx complete callback*/
  2885. HAL_UART_TxCpltCallback(huart);
  2886. #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
  2887. }
  2888. }
  2889. /**
  2890. * @brief DMA UART transmit process half complete callback.
  2891. * @param hdma DMA handle.
  2892. * @retval None
  2893. */
  2894. static void UART_DMATxHalfCplt(DMA_HandleTypeDef *hdma)
  2895. {
  2896. UART_HandleTypeDef *huart = (UART_HandleTypeDef *)(hdma->Parent);
  2897. #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
  2898. /*Call registered Tx Half complete callback*/
  2899. huart->TxHalfCpltCallback(huart);
  2900. #else
  2901. /*Call legacy weak Tx Half complete callback*/
  2902. HAL_UART_TxHalfCpltCallback(huart);
  2903. #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
  2904. }
  2905. /**
  2906. * @brief DMA UART receive process complete callback.
  2907. * @param hdma DMA handle.
  2908. * @retval None
  2909. */
  2910. static void UART_DMAReceiveCplt(DMA_HandleTypeDef *hdma)
  2911. {
  2912. UART_HandleTypeDef *huart = (UART_HandleTypeDef *)(hdma->Parent);
  2913. /* DMA Normal mode */
  2914. if (hdma->Init.Mode != DMA_CIRCULAR)
  2915. {
  2916. huart->RxXferCount = 0U;
  2917. /* Disable PE and ERR (Frame error, noise error, overrun error) interrupts */
  2918. ATOMIC_CLEAR_BIT(huart->Instance->CR1, USART_CR1_PEIE);
  2919. ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
  2920. /* Disable the DMA transfer for the receiver request by resetting the DMAR bit
  2921. in the UART CR3 register */
  2922. ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR);
  2923. /* At end of Rx process, restore huart->RxState to Ready */
  2924. huart->RxState = HAL_UART_STATE_READY;
  2925. /* If Reception till IDLE event has been selected, Disable IDLE Interrupt */
  2926. if (huart->ReceptionType == HAL_UART_RECEPTION_TOIDLE)
  2927. {
  2928. ATOMIC_CLEAR_BIT(huart->Instance->CR1, USART_CR1_IDLEIE);
  2929. }
  2930. }
  2931. /* Initialize type of RxEvent that correspond to RxEvent callback execution;
  2932. In this case, Rx Event type is Transfer Complete */
  2933. huart->RxEventType = HAL_UART_RXEVENT_TC;
  2934. /* Check current reception Mode :
  2935. If Reception till IDLE event has been selected : use Rx Event callback */
  2936. if (huart->ReceptionType == HAL_UART_RECEPTION_TOIDLE)
  2937. {
  2938. #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
  2939. /*Call registered Rx Event callback*/
  2940. huart->RxEventCallback(huart, huart->RxXferSize);
  2941. #else
  2942. /*Call legacy weak Rx Event callback*/
  2943. HAL_UARTEx_RxEventCallback(huart, huart->RxXferSize);
  2944. #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
  2945. }
  2946. else
  2947. {
  2948. /* In other cases : use Rx Complete callback */
  2949. #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
  2950. /*Call registered Rx complete callback*/
  2951. huart->RxCpltCallback(huart);
  2952. #else
  2953. /*Call legacy weak Rx complete callback*/
  2954. HAL_UART_RxCpltCallback(huart);
  2955. #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
  2956. }
  2957. }
  2958. /**
  2959. * @brief DMA UART receive process half complete callback.
  2960. * @param hdma DMA handle.
  2961. * @retval None
  2962. */
  2963. static void UART_DMARxHalfCplt(DMA_HandleTypeDef *hdma)
  2964. {
  2965. UART_HandleTypeDef *huart = (UART_HandleTypeDef *)(hdma->Parent);
  2966. /* Initialize type of RxEvent that correspond to RxEvent callback execution;
  2967. In this case, Rx Event type is Half Transfer */
  2968. huart->RxEventType = HAL_UART_RXEVENT_HT;
  2969. /* Check current reception Mode :
  2970. If Reception till IDLE event has been selected : use Rx Event callback */
  2971. if (huart->ReceptionType == HAL_UART_RECEPTION_TOIDLE)
  2972. {
  2973. #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
  2974. /*Call registered Rx Event callback*/
  2975. huart->RxEventCallback(huart, huart->RxXferSize / 2U);
  2976. #else
  2977. /*Call legacy weak Rx Event callback*/
  2978. HAL_UARTEx_RxEventCallback(huart, huart->RxXferSize / 2U);
  2979. #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
  2980. }
  2981. else
  2982. {
  2983. /* In other cases : use Rx Half Complete callback */
  2984. #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
  2985. /*Call registered Rx Half complete callback*/
  2986. huart->RxHalfCpltCallback(huart);
  2987. #else
  2988. /*Call legacy weak Rx Half complete callback*/
  2989. HAL_UART_RxHalfCpltCallback(huart);
  2990. #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
  2991. }
  2992. }
  2993. /**
  2994. * @brief DMA UART communication error callback.
  2995. * @param hdma DMA handle.
  2996. * @retval None
  2997. */
  2998. static void UART_DMAError(DMA_HandleTypeDef *hdma)
  2999. {
  3000. UART_HandleTypeDef *huart = (UART_HandleTypeDef *)(hdma->Parent);
  3001. const HAL_UART_StateTypeDef gstate = huart->gState;
  3002. const HAL_UART_StateTypeDef rxstate = huart->RxState;
  3003. /* Stop UART DMA Tx request if ongoing */
  3004. if ((HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT)) &&
  3005. (gstate == HAL_UART_STATE_BUSY_TX))
  3006. {
  3007. huart->TxXferCount = 0U;
  3008. UART_EndTxTransfer(huart);
  3009. }
  3010. /* Stop UART DMA Rx request if ongoing */
  3011. if ((HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR)) &&
  3012. (rxstate == HAL_UART_STATE_BUSY_RX))
  3013. {
  3014. huart->RxXferCount = 0U;
  3015. UART_EndRxTransfer(huart);
  3016. }
  3017. huart->ErrorCode |= HAL_UART_ERROR_DMA;
  3018. #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
  3019. /*Call registered error callback*/
  3020. huart->ErrorCallback(huart);
  3021. #else
  3022. /*Call legacy weak error callback*/
  3023. HAL_UART_ErrorCallback(huart);
  3024. #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
  3025. }
  3026. /**
  3027. * @brief DMA UART communication abort callback, when initiated by HAL services on Error
  3028. * (To be called at end of DMA Abort procedure following error occurrence).
  3029. * @param hdma DMA handle.
  3030. * @retval None
  3031. */
  3032. static void UART_DMAAbortOnError(DMA_HandleTypeDef *hdma)
  3033. {
  3034. UART_HandleTypeDef *huart = (UART_HandleTypeDef *)(hdma->Parent);
  3035. huart->RxXferCount = 0U;
  3036. huart->TxXferCount = 0U;
  3037. #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
  3038. /*Call registered error callback*/
  3039. huart->ErrorCallback(huart);
  3040. #else
  3041. /*Call legacy weak error callback*/
  3042. HAL_UART_ErrorCallback(huart);
  3043. #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
  3044. }
  3045. /**
  3046. * @brief DMA UART Tx communication abort callback, when initiated by user
  3047. * (To be called at end of DMA Tx Abort procedure following user abort request).
  3048. * @note When this callback is executed, User Abort complete call back is called only if no
  3049. * Abort still ongoing for Rx DMA Handle.
  3050. * @param hdma DMA handle.
  3051. * @retval None
  3052. */
  3053. static void UART_DMATxAbortCallback(DMA_HandleTypeDef *hdma)
  3054. {
  3055. UART_HandleTypeDef *huart = (UART_HandleTypeDef *)(hdma->Parent);
  3056. huart->hdmatx->XferAbortCallback = NULL;
  3057. /* Check if an Abort process is still ongoing */
  3058. if (huart->hdmarx != NULL)
  3059. {
  3060. if (huart->hdmarx->XferAbortCallback != NULL)
  3061. {
  3062. return;
  3063. }
  3064. }
  3065. /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */
  3066. huart->TxXferCount = 0U;
  3067. huart->RxXferCount = 0U;
  3068. /* Reset errorCode */
  3069. huart->ErrorCode = HAL_UART_ERROR_NONE;
  3070. /* Clear the Error flags in the ICR register */
  3071. __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_OREF | UART_CLEAR_NEF | UART_CLEAR_PEF | UART_CLEAR_FEF);
  3072. /* Restore huart->gState and huart->RxState to Ready */
  3073. huart->gState = HAL_UART_STATE_READY;
  3074. huart->RxState = HAL_UART_STATE_READY;
  3075. huart->ReceptionType = HAL_UART_RECEPTION_STANDARD;
  3076. /* Call user Abort complete callback */
  3077. #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
  3078. /* Call registered Abort complete callback */
  3079. huart->AbortCpltCallback(huart);
  3080. #else
  3081. /* Call legacy weak Abort complete callback */
  3082. HAL_UART_AbortCpltCallback(huart);
  3083. #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
  3084. }
  3085. /**
  3086. * @brief DMA UART Rx communication abort callback, when initiated by user
  3087. * (To be called at end of DMA Rx Abort procedure following user abort request).
  3088. * @note When this callback is executed, User Abort complete call back is called only if no
  3089. * Abort still ongoing for Tx DMA Handle.
  3090. * @param hdma DMA handle.
  3091. * @retval None
  3092. */
  3093. static void UART_DMARxAbortCallback(DMA_HandleTypeDef *hdma)
  3094. {
  3095. UART_HandleTypeDef *huart = (UART_HandleTypeDef *)(hdma->Parent);
  3096. huart->hdmarx->XferAbortCallback = NULL;
  3097. /* Check if an Abort process is still ongoing */
  3098. if (huart->hdmatx != NULL)
  3099. {
  3100. if (huart->hdmatx->XferAbortCallback != NULL)
  3101. {
  3102. return;
  3103. }
  3104. }
  3105. /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */
  3106. huart->TxXferCount = 0U;
  3107. huart->RxXferCount = 0U;
  3108. /* Reset errorCode */
  3109. huart->ErrorCode = HAL_UART_ERROR_NONE;
  3110. /* Clear the Error flags in the ICR register */
  3111. __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_OREF | UART_CLEAR_NEF | UART_CLEAR_PEF | UART_CLEAR_FEF);
  3112. /* Discard the received data */
  3113. __HAL_UART_SEND_REQ(huart, UART_RXDATA_FLUSH_REQUEST);
  3114. /* Restore huart->gState and huart->RxState to Ready */
  3115. huart->gState = HAL_UART_STATE_READY;
  3116. huart->RxState = HAL_UART_STATE_READY;
  3117. huart->ReceptionType = HAL_UART_RECEPTION_STANDARD;
  3118. /* Call user Abort complete callback */
  3119. #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
  3120. /* Call registered Abort complete callback */
  3121. huart->AbortCpltCallback(huart);
  3122. #else
  3123. /* Call legacy weak Abort complete callback */
  3124. HAL_UART_AbortCpltCallback(huart);
  3125. #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
  3126. }
  3127. /**
  3128. * @brief DMA UART Tx communication abort callback, when initiated by user by a call to
  3129. * HAL_UART_AbortTransmit_IT API (Abort only Tx transfer)
  3130. * (This callback is executed at end of DMA Tx Abort procedure following user abort request,
  3131. * and leads to user Tx Abort Complete callback execution).
  3132. * @param hdma DMA handle.
  3133. * @retval None
  3134. */
  3135. static void UART_DMATxOnlyAbortCallback(DMA_HandleTypeDef *hdma)
  3136. {
  3137. UART_HandleTypeDef *huart = (UART_HandleTypeDef *)(hdma->Parent);
  3138. huart->TxXferCount = 0U;
  3139. /* Restore huart->gState to Ready */
  3140. huart->gState = HAL_UART_STATE_READY;
  3141. /* Call user Abort complete callback */
  3142. #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
  3143. /* Call registered Abort Transmit Complete Callback */
  3144. huart->AbortTransmitCpltCallback(huart);
  3145. #else
  3146. /* Call legacy weak Abort Transmit Complete Callback */
  3147. HAL_UART_AbortTransmitCpltCallback(huart);
  3148. #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
  3149. }
  3150. /**
  3151. * @brief DMA UART Rx communication abort callback, when initiated by user by a call to
  3152. * HAL_UART_AbortReceive_IT API (Abort only Rx transfer)
  3153. * (This callback is executed at end of DMA Rx Abort procedure following user abort request,
  3154. * and leads to user Rx Abort Complete callback execution).
  3155. * @param hdma DMA handle.
  3156. * @retval None
  3157. */
  3158. static void UART_DMARxOnlyAbortCallback(DMA_HandleTypeDef *hdma)
  3159. {
  3160. UART_HandleTypeDef *huart = (UART_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  3161. huart->RxXferCount = 0U;
  3162. /* Clear the Error flags in the ICR register */
  3163. __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_OREF | UART_CLEAR_NEF | UART_CLEAR_PEF | UART_CLEAR_FEF);
  3164. /* Discard the received data */
  3165. __HAL_UART_SEND_REQ(huart, UART_RXDATA_FLUSH_REQUEST);
  3166. /* Restore huart->RxState to Ready */
  3167. huart->RxState = HAL_UART_STATE_READY;
  3168. huart->ReceptionType = HAL_UART_RECEPTION_STANDARD;
  3169. /* Call user Abort complete callback */
  3170. #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
  3171. /* Call registered Abort Receive Complete Callback */
  3172. huart->AbortReceiveCpltCallback(huart);
  3173. #else
  3174. /* Call legacy weak Abort Receive Complete Callback */
  3175. HAL_UART_AbortReceiveCpltCallback(huart);
  3176. #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
  3177. }
  3178. /**
  3179. * @brief TX interrupt handler for 7 or 8 bits data word length .
  3180. * @note Function is called under interruption only, once
  3181. * interruptions have been enabled by HAL_UART_Transmit_IT().
  3182. * @param huart UART handle.
  3183. * @retval None
  3184. */
  3185. static void UART_TxISR_8BIT(UART_HandleTypeDef *huart)
  3186. {
  3187. /* Check that a Tx process is ongoing */
  3188. if (huart->gState == HAL_UART_STATE_BUSY_TX)
  3189. {
  3190. if (huart->TxXferCount == 0U)
  3191. {
  3192. /* Disable the UART Transmit Data Register Empty Interrupt */
  3193. ATOMIC_CLEAR_BIT(huart->Instance->CR1, USART_CR1_TXEIE);
  3194. /* Enable the UART Transmit Complete Interrupt */
  3195. ATOMIC_SET_BIT(huart->Instance->CR1, USART_CR1_TCIE);
  3196. }
  3197. else
  3198. {
  3199. huart->Instance->TDR = (uint8_t)(*huart->pTxBuffPtr & (uint8_t)0xFF);
  3200. huart->pTxBuffPtr++;
  3201. huart->TxXferCount--;
  3202. }
  3203. }
  3204. }
  3205. /**
  3206. * @brief TX interrupt handler for 9 bits data word length.
  3207. * @note Function is called under interruption only, once
  3208. * interruptions have been enabled by HAL_UART_Transmit_IT().
  3209. * @param huart UART handle.
  3210. * @retval None
  3211. */
  3212. static void UART_TxISR_16BIT(UART_HandleTypeDef *huart)
  3213. {
  3214. const uint16_t *tmp;
  3215. /* Check that a Tx process is ongoing */
  3216. if (huart->gState == HAL_UART_STATE_BUSY_TX)
  3217. {
  3218. if (huart->TxXferCount == 0U)
  3219. {
  3220. /* Disable the UART Transmit Data Register Empty Interrupt */
  3221. ATOMIC_CLEAR_BIT(huart->Instance->CR1, USART_CR1_TXEIE);
  3222. /* Enable the UART Transmit Complete Interrupt */
  3223. ATOMIC_SET_BIT(huart->Instance->CR1, USART_CR1_TCIE);
  3224. }
  3225. else
  3226. {
  3227. tmp = (const uint16_t *) huart->pTxBuffPtr;
  3228. huart->Instance->TDR = (((uint32_t)(*tmp)) & 0x01FFUL);
  3229. huart->pTxBuffPtr += 2U;
  3230. huart->TxXferCount--;
  3231. }
  3232. }
  3233. }
  3234. /**
  3235. * @brief Wrap up transmission in non-blocking mode.
  3236. * @param huart pointer to a UART_HandleTypeDef structure that contains
  3237. * the configuration information for the specified UART module.
  3238. * @retval None
  3239. */
  3240. static void UART_EndTransmit_IT(UART_HandleTypeDef *huart)
  3241. {
  3242. /* Disable the UART Transmit Complete Interrupt */
  3243. ATOMIC_CLEAR_BIT(huart->Instance->CR1, USART_CR1_TCIE);
  3244. /* Tx process is ended, restore huart->gState to Ready */
  3245. huart->gState = HAL_UART_STATE_READY;
  3246. /* Cleat TxISR function pointer */
  3247. huart->TxISR = NULL;
  3248. #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
  3249. /*Call registered Tx complete callback*/
  3250. huart->TxCpltCallback(huart);
  3251. #else
  3252. /*Call legacy weak Tx complete callback*/
  3253. HAL_UART_TxCpltCallback(huart);
  3254. #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
  3255. }
  3256. /**
  3257. * @brief RX interrupt handler for 7 or 8 bits data word length .
  3258. * @param huart UART handle.
  3259. * @retval None
  3260. */
  3261. static void UART_RxISR_8BIT(UART_HandleTypeDef *huart)
  3262. {
  3263. uint16_t uhMask = huart->Mask;
  3264. uint16_t uhdata;
  3265. /* Check that a Rx process is ongoing */
  3266. if (huart->RxState == HAL_UART_STATE_BUSY_RX)
  3267. {
  3268. uhdata = (uint16_t) READ_REG(huart->Instance->RDR);
  3269. *huart->pRxBuffPtr = (uint8_t)(uhdata & (uint8_t)uhMask);
  3270. huart->pRxBuffPtr++;
  3271. huart->RxXferCount--;
  3272. if (huart->RxXferCount == 0U)
  3273. {
  3274. /* Disable the UART Parity Error Interrupt and RXNE interrupts */
  3275. ATOMIC_CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE));
  3276. /* Disable the UART Error Interrupt: (Frame error, noise error, overrun error) */
  3277. ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
  3278. /* Rx process is completed, restore huart->RxState to Ready */
  3279. huart->RxState = HAL_UART_STATE_READY;
  3280. /* Clear RxISR function pointer */
  3281. huart->RxISR = NULL;
  3282. /* Initialize type of RxEvent to Transfer Complete */
  3283. huart->RxEventType = HAL_UART_RXEVENT_TC;
  3284. /* Check current reception Mode :
  3285. If Reception till IDLE event has been selected : */
  3286. if (huart->ReceptionType == HAL_UART_RECEPTION_TOIDLE)
  3287. {
  3288. /* Set reception type to Standard */
  3289. huart->ReceptionType = HAL_UART_RECEPTION_STANDARD;
  3290. /* Disable IDLE interrupt */
  3291. ATOMIC_CLEAR_BIT(huart->Instance->CR1, USART_CR1_IDLEIE);
  3292. if (__HAL_UART_GET_FLAG(huart, UART_FLAG_IDLE) == SET)
  3293. {
  3294. /* Clear IDLE Flag */
  3295. __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_IDLEF);
  3296. }
  3297. #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
  3298. /*Call registered Rx Event callback*/
  3299. huart->RxEventCallback(huart, huart->RxXferSize);
  3300. #else
  3301. /*Call legacy weak Rx Event callback*/
  3302. HAL_UARTEx_RxEventCallback(huart, huart->RxXferSize);
  3303. #endif /* (USE_HAL_UART_REGISTER_CALLBACKS) */
  3304. }
  3305. else
  3306. {
  3307. /* Standard reception API called */
  3308. #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
  3309. /*Call registered Rx complete callback*/
  3310. huart->RxCpltCallback(huart);
  3311. #else
  3312. /*Call legacy weak Rx complete callback*/
  3313. HAL_UART_RxCpltCallback(huart);
  3314. #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
  3315. }
  3316. }
  3317. }
  3318. else
  3319. {
  3320. /* Clear RXNE interrupt flag */
  3321. __HAL_UART_SEND_REQ(huart, UART_RXDATA_FLUSH_REQUEST);
  3322. }
  3323. }
  3324. /**
  3325. * @brief RX interrupt handler for 9 bits data word length .
  3326. * @note Function is called under interruption only, once
  3327. * interruptions have been enabled by HAL_UART_Receive_IT()
  3328. * @param huart UART handle.
  3329. * @retval None
  3330. */
  3331. static void UART_RxISR_16BIT(UART_HandleTypeDef *huart)
  3332. {
  3333. uint16_t *tmp;
  3334. uint16_t uhMask = huart->Mask;
  3335. uint16_t uhdata;
  3336. /* Check that a Rx process is ongoing */
  3337. if (huart->RxState == HAL_UART_STATE_BUSY_RX)
  3338. {
  3339. uhdata = (uint16_t) READ_REG(huart->Instance->RDR);
  3340. tmp = (uint16_t *) huart->pRxBuffPtr ;
  3341. *tmp = (uint16_t)(uhdata & uhMask);
  3342. huart->pRxBuffPtr += 2U;
  3343. huart->RxXferCount--;
  3344. if (huart->RxXferCount == 0U)
  3345. {
  3346. /* Disable the UART Parity Error Interrupt and RXNE interrupt*/
  3347. ATOMIC_CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE));
  3348. /* Disable the UART Error Interrupt: (Frame error, noise error, overrun error) */
  3349. ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
  3350. /* Rx process is completed, restore huart->RxState to Ready */
  3351. huart->RxState = HAL_UART_STATE_READY;
  3352. /* Clear RxISR function pointer */
  3353. huart->RxISR = NULL;
  3354. /* Initialize type of RxEvent to Transfer Complete */
  3355. huart->RxEventType = HAL_UART_RXEVENT_TC;
  3356. /* Check current reception Mode :
  3357. If Reception till IDLE event has been selected : */
  3358. if (huart->ReceptionType == HAL_UART_RECEPTION_TOIDLE)
  3359. {
  3360. /* Set reception type to Standard */
  3361. huart->ReceptionType = HAL_UART_RECEPTION_STANDARD;
  3362. /* Disable IDLE interrupt */
  3363. ATOMIC_CLEAR_BIT(huart->Instance->CR1, USART_CR1_IDLEIE);
  3364. if (__HAL_UART_GET_FLAG(huart, UART_FLAG_IDLE) == SET)
  3365. {
  3366. /* Clear IDLE Flag */
  3367. __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_IDLEF);
  3368. }
  3369. #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
  3370. /*Call registered Rx Event callback*/
  3371. huart->RxEventCallback(huart, huart->RxXferSize);
  3372. #else
  3373. /*Call legacy weak Rx Event callback*/
  3374. HAL_UARTEx_RxEventCallback(huart, huart->RxXferSize);
  3375. #endif /* (USE_HAL_UART_REGISTER_CALLBACKS) */
  3376. }
  3377. else
  3378. {
  3379. /* Standard reception API called */
  3380. #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
  3381. /*Call registered Rx complete callback*/
  3382. huart->RxCpltCallback(huart);
  3383. #else
  3384. /*Call legacy weak Rx complete callback*/
  3385. HAL_UART_RxCpltCallback(huart);
  3386. #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
  3387. }
  3388. }
  3389. }
  3390. else
  3391. {
  3392. /* Clear RXNE interrupt flag */
  3393. __HAL_UART_SEND_REQ(huart, UART_RXDATA_FLUSH_REQUEST);
  3394. }
  3395. }
  3396. /**
  3397. * @}
  3398. */
  3399. #endif /* HAL_UART_MODULE_ENABLED */
  3400. /**
  3401. * @}
  3402. */
  3403. /**
  3404. * @}
  3405. */