stm32f1xx_hal_uart.c 132 KB

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