stm32l4xx_hal_uart.c 164 KB

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