stm32f7xx_hal_uart.c 130 KB

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