stm32f1xx_hal_uart.c 128 KB

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