stm32f1xx_hal_uart.c 132 KB

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