stm32f0xx_hal_usart.c 113 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291
  1. /**
  2. ******************************************************************************
  3. * @file stm32f0xx_hal_usart.c
  4. * @author MCD Application Team
  5. * @brief USART HAL module driver.
  6. * This file provides firmware functions to manage the following
  7. * functionalities of the Universal Synchronous/Asynchronous Receiver Transmitter
  8. * Peripheral (USART).
  9. * + Initialization and de-initialization functions
  10. * + IO operation functions
  11. * + Peripheral Control functions
  12. * + Peripheral State and Error functions
  13. *
  14. ******************************************************************************
  15. * @attention
  16. *
  17. * Copyright (c) 2016 STMicroelectronics.
  18. * All rights reserved.
  19. *
  20. * This software is licensed under terms that can be found in the LICENSE file
  21. * in the root directory of this software component.
  22. * If no LICENSE file comes with this software, it is provided AS-IS.
  23. *
  24. ******************************************************************************
  25. @verbatim
  26. ===============================================================================
  27. ##### How to use this driver #####
  28. ===============================================================================
  29. [..]
  30. The USART HAL driver can be used as follows:
  31. (#) Declare a USART_HandleTypeDef handle structure (eg. USART_HandleTypeDef husart).
  32. (#) Initialize the USART low level resources by implementing the HAL_USART_MspInit() API:
  33. (++) Enable the USARTx interface clock.
  34. (++) USART pins configuration:
  35. (+++) Enable the clock for the USART GPIOs.
  36. (+++) Configure these USART pins as alternate function pull-up.
  37. (++) NVIC configuration if you need to use interrupt process (HAL_USART_Transmit_IT(),
  38. HAL_USART_Receive_IT() and HAL_USART_TransmitReceive_IT() APIs):
  39. (+++) Configure the USARTx interrupt priority.
  40. (+++) Enable the NVIC USART IRQ handle.
  41. (++) USART interrupts handling:
  42. -@@- The specific USART interrupts (Transmission complete interrupt,
  43. RXNE interrupt and Error Interrupts) will be managed using the macros
  44. __HAL_USART_ENABLE_IT() and __HAL_USART_DISABLE_IT() inside the transmit and receive process.
  45. (++) DMA Configuration if you need to use DMA process (HAL_USART_Transmit_DMA()
  46. HAL_USART_Receive_DMA() and HAL_USART_TransmitReceive_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 USART DMA Tx/Rx handle.
  52. (+++) Configure the priority and enable the NVIC for the transfer
  53. complete interrupt on the DMA Tx/Rx channel.
  54. (#) Program the Baud Rate, Word Length, Stop Bit, Parity, and Mode
  55. (Receiver/Transmitter) in the husart handle Init structure.
  56. (#) Initialize the USART registers by calling the HAL_USART_Init() API:
  57. (++) This API configures also the low level Hardware GPIO, CLOCK, CORTEX...etc)
  58. by calling the customized HAL_USART_MspInit(&husart) API.
  59. [..]
  60. (@) To configure and enable/disable the USART to wake up the MCU from stop mode, resort to UART API's
  61. HAL_UARTEx_StopModeWakeUpSourceConfig(), HAL_UARTEx_EnableStopMode() and
  62. HAL_UARTEx_DisableStopMode() in casting the USART handle to UART type UART_HandleTypeDef.
  63. ##### Callback registration #####
  64. ==================================
  65. [..]
  66. The compilation define USE_HAL_USART_REGISTER_CALLBACKS when set to 1
  67. allows the user to configure dynamically the driver callbacks.
  68. [..]
  69. Use Function HAL_USART_RegisterCallback() to register a user callback.
  70. Function HAL_USART_RegisterCallback() allows to register following callbacks:
  71. (+) TxHalfCpltCallback : Tx Half Complete Callback.
  72. (+) TxCpltCallback : Tx Complete Callback.
  73. (+) RxHalfCpltCallback : Rx Half Complete Callback.
  74. (+) RxCpltCallback : Rx Complete Callback.
  75. (+) TxRxCpltCallback : Tx Rx Complete Callback.
  76. (+) ErrorCallback : Error Callback.
  77. (+) AbortCpltCallback : Abort Complete Callback.
  78. (+) MspInitCallback : USART MspInit.
  79. (+) MspDeInitCallback : USART MspDeInit.
  80. This function takes as parameters the HAL peripheral handle, the Callback ID
  81. and a pointer to the user callback function.
  82. [..]
  83. Use function HAL_USART_UnRegisterCallback() to reset a callback to the default
  84. weak (surcharged) function.
  85. HAL_USART_UnRegisterCallback() takes as parameters the HAL peripheral handle,
  86. and the Callback ID.
  87. This function allows to reset following callbacks:
  88. (+) TxHalfCpltCallback : Tx Half Complete Callback.
  89. (+) TxCpltCallback : Tx Complete Callback.
  90. (+) RxHalfCpltCallback : Rx Half Complete Callback.
  91. (+) RxCpltCallback : Rx Complete Callback.
  92. (+) TxRxCpltCallback : Tx Rx Complete Callback.
  93. (+) ErrorCallback : Error Callback.
  94. (+) AbortCpltCallback : Abort Complete Callback.
  95. (+) MspInitCallback : USART MspInit.
  96. (+) MspDeInitCallback : USART MspDeInit.
  97. [..]
  98. By default, after the HAL_USART_Init() and when the state is HAL_USART_STATE_RESET
  99. all callbacks are set to the corresponding weak (surcharged) functions:
  100. examples HAL_USART_TxCpltCallback(), HAL_USART_RxHalfCpltCallback().
  101. Exception done for MspInit and MspDeInit functions that are respectively
  102. reset to the legacy weak (surcharged) functions in the HAL_USART_Init()
  103. and HAL_USART_DeInit() only when these callbacks are null (not registered beforehand).
  104. If not, MspInit or MspDeInit are not null, the HAL_USART_Init() and HAL_USART_DeInit()
  105. keep and use the user MspInit/MspDeInit callbacks (registered beforehand).
  106. [..]
  107. Callbacks can be registered/unregistered in HAL_USART_STATE_READY state only.
  108. Exception done MspInit/MspDeInit that can be registered/unregistered
  109. in HAL_USART_STATE_READY or HAL_USART_STATE_RESET state, thus registered (user)
  110. MspInit/DeInit callbacks can be used during the Init/DeInit.
  111. In that case first register the MspInit/MspDeInit user callbacks
  112. using HAL_USART_RegisterCallback() before calling HAL_USART_DeInit()
  113. or HAL_USART_Init() function.
  114. [..]
  115. When The compilation define USE_HAL_USART_REGISTER_CALLBACKS is set to 0 or
  116. not defined, the callback registration feature is not available
  117. and weak (surcharged) callbacks are used.
  118. @endverbatim
  119. ******************************************************************************
  120. */
  121. /* Includes ------------------------------------------------------------------*/
  122. #include "stm32f0xx_hal.h"
  123. /** @addtogroup STM32F0xx_HAL_Driver
  124. * @{
  125. */
  126. /** @defgroup USART USART
  127. * @brief HAL USART Synchronous module driver
  128. * @{
  129. */
  130. #ifdef HAL_USART_MODULE_ENABLED
  131. /* Private typedef -----------------------------------------------------------*/
  132. /* Private define ------------------------------------------------------------*/
  133. /** @defgroup USART_Private_Constants USART Private Constants
  134. * @{
  135. */
  136. #define USART_DUMMY_DATA ((uint16_t) 0xFFFF) /*!< USART transmitted dummy data */
  137. #define USART_TEACK_REACK_TIMEOUT 1000U /*!< USART TX or RX enable acknowledge time-out value */
  138. #define USART_CR1_FIELDS ((uint32_t)(USART_CR1_M | USART_CR1_PCE | USART_CR1_PS | \
  139. USART_CR1_TE | USART_CR1_RE | USART_CR1_OVER8)) /*!< USART CR1 fields of parameters set by USART_SetConfig API */
  140. #define USART_CR2_FIELDS ((uint32_t)(USART_CR2_CPHA | USART_CR2_CPOL | \
  141. USART_CR2_CLKEN | USART_CR2_LBCL | USART_CR2_STOP)) /*!< USART CR2 fields of parameters set by USART_SetConfig API */
  142. #define USART_BRR_MIN 0x10U /* USART BRR minimum authorized value */
  143. #define USART_BRR_MAX 0xFFFFU /* USART BRR maximum authorized value */
  144. /**
  145. * @}
  146. */
  147. /* Private macros ------------------------------------------------------------*/
  148. /* Private variables ---------------------------------------------------------*/
  149. /* Private function prototypes -----------------------------------------------*/
  150. /** @addtogroup USART_Private_Functions
  151. * @{
  152. */
  153. #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
  154. void USART_InitCallbacksToDefault(USART_HandleTypeDef *husart);
  155. #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
  156. static void USART_EndTransfer(USART_HandleTypeDef *husart);
  157. static void USART_DMATransmitCplt(DMA_HandleTypeDef *hdma);
  158. static void USART_DMAReceiveCplt(DMA_HandleTypeDef *hdma);
  159. static void USART_DMATxHalfCplt(DMA_HandleTypeDef *hdma);
  160. static void USART_DMARxHalfCplt(DMA_HandleTypeDef *hdma);
  161. static void USART_DMAError(DMA_HandleTypeDef *hdma);
  162. static void USART_DMAAbortOnError(DMA_HandleTypeDef *hdma);
  163. static void USART_DMATxAbortCallback(DMA_HandleTypeDef *hdma);
  164. static void USART_DMARxAbortCallback(DMA_HandleTypeDef *hdma);
  165. static HAL_StatusTypeDef USART_WaitOnFlagUntilTimeout(USART_HandleTypeDef *husart, uint32_t Flag, FlagStatus Status,
  166. uint32_t Tickstart, uint32_t Timeout);
  167. static HAL_StatusTypeDef USART_SetConfig(USART_HandleTypeDef *husart);
  168. static HAL_StatusTypeDef USART_CheckIdleState(USART_HandleTypeDef *husart);
  169. static void USART_TxISR_8BIT(USART_HandleTypeDef *husart);
  170. static void USART_TxISR_16BIT(USART_HandleTypeDef *husart);
  171. static void USART_EndTransmit_IT(USART_HandleTypeDef *husart);
  172. static void USART_RxISR_8BIT(USART_HandleTypeDef *husart);
  173. static void USART_RxISR_16BIT(USART_HandleTypeDef *husart);
  174. /**
  175. * @}
  176. */
  177. /* Exported functions --------------------------------------------------------*/
  178. /** @defgroup USART_Exported_Functions USART Exported Functions
  179. * @{
  180. */
  181. /** @defgroup USART_Exported_Functions_Group1 Initialization and de-initialization functions
  182. * @brief Initialization and Configuration functions
  183. *
  184. @verbatim
  185. ===============================================================================
  186. ##### Initialization and Configuration functions #####
  187. ===============================================================================
  188. [..]
  189. This subsection provides a set of functions allowing to initialize the USART
  190. in asynchronous and in synchronous modes.
  191. (+) For the asynchronous mode only these parameters can be configured:
  192. (++) Baud Rate
  193. (++) Word Length
  194. (++) Stop Bit
  195. (++) Parity: If the parity is enabled, then the MSB bit of the data written
  196. in the data register is transmitted but is changed by the parity bit.
  197. (++) USART polarity
  198. (++) USART phase
  199. (++) USART LastBit
  200. (++) Receiver/transmitter modes
  201. [..]
  202. The HAL_USART_Init() function follows the USART synchronous configuration
  203. procedure (details for the procedure are available in reference manual).
  204. @endverbatim
  205. Depending on the frame length either defined by the M1 and M0 bits (7-bit,
  206. 8-bit or 9-bit) or by the M bit (8-bits or 9-bits), the possible USART formats
  207. are listed in the following table.
  208. Table 1. USART frame format.
  209. +-----------------------------------------------------------------------+
  210. | M bit | PCE bit | USART frame |
  211. |-------------------|-----------|---------------------------------------|
  212. | 0 | 0 | | SB | 8-bit data | STB | |
  213. |-------------------|-----------|---------------------------------------|
  214. | 0 | 1 | | SB | 7-bit data | PB | STB | |
  215. |-------------------|-----------|---------------------------------------|
  216. | 1 | 0 | | SB | 9-bit data | STB | |
  217. |-------------------|-----------|---------------------------------------|
  218. | 1 | 1 | | SB | 8-bit data | PB | STB | |
  219. +-----------------------------------------------------------------------+
  220. +-----------------------------------------------------------------------+
  221. | M1 bit | M0 bit | PCE bit | USART frame |
  222. |---------|---------|-----------|---------------------------------------|
  223. | 0 | 0 | 0 | | SB | 8 bit data | STB | |
  224. |---------|---------|-----------|---------------------------------------|
  225. | 0 | 0 | 1 | | SB | 7 bit data | PB | STB | |
  226. |---------|---------|-----------|---------------------------------------|
  227. | 0 | 1 | 0 | | SB | 9 bit data | STB | |
  228. |---------|---------|-----------|---------------------------------------|
  229. | 0 | 1 | 1 | | SB | 8 bit data | PB | STB | |
  230. |---------|---------|-----------|---------------------------------------|
  231. | 1 | 0 | 0 | | SB | 7 bit data | STB | |
  232. |---------|---------|-----------|---------------------------------------|
  233. | 1 | 0 | 1 | | SB | 6 bit data | PB | STB | |
  234. +-----------------------------------------------------------------------+
  235. * @{
  236. */
  237. /**
  238. * @brief Initialize the USART mode according to the specified
  239. * parameters in the USART_InitTypeDef and initialize the associated handle.
  240. * @param husart USART handle.
  241. * @retval HAL status
  242. */
  243. HAL_StatusTypeDef HAL_USART_Init(USART_HandleTypeDef *husart)
  244. {
  245. /* Check the USART handle allocation */
  246. if (husart == NULL)
  247. {
  248. return HAL_ERROR;
  249. }
  250. /* Check the parameters */
  251. assert_param(IS_USART_INSTANCE(husart->Instance));
  252. if (husart->State == HAL_USART_STATE_RESET)
  253. {
  254. /* Allocate lock resource and initialize it */
  255. husart->Lock = HAL_UNLOCKED;
  256. #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
  257. USART_InitCallbacksToDefault(husart);
  258. if (husart->MspInitCallback == NULL)
  259. {
  260. husart->MspInitCallback = HAL_USART_MspInit;
  261. }
  262. /* Init the low level hardware */
  263. husart->MspInitCallback(husart);
  264. #else
  265. /* Init the low level hardware : GPIO, CLOCK */
  266. HAL_USART_MspInit(husart);
  267. #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
  268. }
  269. husart->State = HAL_USART_STATE_BUSY;
  270. /* Disable the Peripheral */
  271. __HAL_USART_DISABLE(husart);
  272. /* Set the Usart Communication parameters */
  273. if (USART_SetConfig(husart) == HAL_ERROR)
  274. {
  275. return HAL_ERROR;
  276. }
  277. /* In Synchronous mode, the following bits must be kept cleared:
  278. - LINEN bit (if LIN is supported) in the USART_CR2 register
  279. - SCEN (if Smartcard is supported), HDSEL and IREN (if IrDA is supported) bits in the USART_CR3 register.
  280. */
  281. #if defined (USART_CR2_LINEN)
  282. husart->Instance->CR2 &= ~USART_CR2_LINEN;
  283. #endif /* USART_CR2_LINEN */
  284. #if defined (USART_CR3_SCEN)
  285. #if defined (USART_CR3_IREN)
  286. husart->Instance->CR3 &= ~(USART_CR3_SCEN | USART_CR3_HDSEL | USART_CR3_IREN);
  287. #else
  288. husart->Instance->CR3 &= ~(USART_CR3_SCEN | USART_CR3_HDSEL);
  289. #endif /* USART_CR3_IREN */
  290. #else
  291. #if defined (USART_CR3_IREN)
  292. husart->Instance->CR3 &= ~(USART_CR3_HDSEL | USART_CR3_IREN);
  293. #else
  294. husart->Instance->CR3 &= ~(USART_CR3_HDSEL);
  295. #endif /* USART_CR3_IREN */
  296. #endif /* USART_CR3_SCEN */
  297. /* Enable the Peripheral */
  298. __HAL_USART_ENABLE(husart);
  299. /* TEACK and/or REACK to check before moving husart->State to Ready */
  300. return (USART_CheckIdleState(husart));
  301. }
  302. /**
  303. * @brief DeInitialize the USART peripheral.
  304. * @param husart USART handle.
  305. * @retval HAL status
  306. */
  307. HAL_StatusTypeDef HAL_USART_DeInit(USART_HandleTypeDef *husart)
  308. {
  309. /* Check the USART handle allocation */
  310. if (husart == NULL)
  311. {
  312. return HAL_ERROR;
  313. }
  314. /* Check the parameters */
  315. assert_param(IS_USART_INSTANCE(husart->Instance));
  316. husart->State = HAL_USART_STATE_BUSY;
  317. husart->Instance->CR1 = 0x0U;
  318. husart->Instance->CR2 = 0x0U;
  319. husart->Instance->CR3 = 0x0U;
  320. #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
  321. if (husart->MspDeInitCallback == NULL)
  322. {
  323. husart->MspDeInitCallback = HAL_USART_MspDeInit;
  324. }
  325. /* DeInit the low level hardware */
  326. husart->MspDeInitCallback(husart);
  327. #else
  328. /* DeInit the low level hardware */
  329. HAL_USART_MspDeInit(husart);
  330. #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
  331. husart->ErrorCode = HAL_USART_ERROR_NONE;
  332. husart->State = HAL_USART_STATE_RESET;
  333. /* Process Unlock */
  334. __HAL_UNLOCK(husart);
  335. return HAL_OK;
  336. }
  337. /**
  338. * @brief Initialize the USART MSP.
  339. * @param husart USART handle.
  340. * @retval None
  341. */
  342. __weak void HAL_USART_MspInit(USART_HandleTypeDef *husart)
  343. {
  344. /* Prevent unused argument(s) compilation warning */
  345. UNUSED(husart);
  346. /* NOTE : This function should not be modified, when the callback is needed,
  347. the HAL_USART_MspInit can be implemented in the user file
  348. */
  349. }
  350. /**
  351. * @brief DeInitialize the USART MSP.
  352. * @param husart USART handle.
  353. * @retval None
  354. */
  355. __weak void HAL_USART_MspDeInit(USART_HandleTypeDef *husart)
  356. {
  357. /* Prevent unused argument(s) compilation warning */
  358. UNUSED(husart);
  359. /* NOTE : This function should not be modified, when the callback is needed,
  360. the HAL_USART_MspDeInit can be implemented in the user file
  361. */
  362. }
  363. #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
  364. /**
  365. * @brief Register a User USART Callback
  366. * To be used instead of the weak predefined callback
  367. * @note The HAL_USART_RegisterCallback() may be called before HAL_USART_Init() in HAL_USART_STATE_RESET
  368. * to register callbacks for HAL_USART_MSPINIT_CB_ID and HAL_USART_MSPDEINIT_CB_ID
  369. * @param husart usart handle
  370. * @param CallbackID ID of the callback to be registered
  371. * This parameter can be one of the following values:
  372. * @arg @ref HAL_USART_TX_HALFCOMPLETE_CB_ID Tx Half Complete Callback ID
  373. * @arg @ref HAL_USART_TX_COMPLETE_CB_ID Tx Complete Callback ID
  374. * @arg @ref HAL_USART_RX_HALFCOMPLETE_CB_ID Rx Half Complete Callback ID
  375. * @arg @ref HAL_USART_RX_COMPLETE_CB_ID Rx Complete Callback ID
  376. * @arg @ref HAL_USART_TX_RX_COMPLETE_CB_ID Rx Complete Callback ID
  377. * @arg @ref HAL_USART_ERROR_CB_ID Error Callback ID
  378. * @arg @ref HAL_USART_ABORT_COMPLETE_CB_ID Abort Complete Callback ID
  379. * @arg @ref HAL_USART_MSPINIT_CB_ID MspInit Callback ID
  380. * @arg @ref HAL_USART_MSPDEINIT_CB_ID MspDeInit Callback ID
  381. * @param pCallback pointer to the Callback function
  382. * @retval HAL status
  383. + */
  384. HAL_StatusTypeDef HAL_USART_RegisterCallback(USART_HandleTypeDef *husart, HAL_USART_CallbackIDTypeDef CallbackID,
  385. pUSART_CallbackTypeDef pCallback)
  386. {
  387. HAL_StatusTypeDef status = HAL_OK;
  388. if (pCallback == NULL)
  389. {
  390. /* Update the error code */
  391. husart->ErrorCode |= HAL_USART_ERROR_INVALID_CALLBACK;
  392. return HAL_ERROR;
  393. }
  394. if (husart->State == HAL_USART_STATE_READY)
  395. {
  396. switch (CallbackID)
  397. {
  398. case HAL_USART_TX_HALFCOMPLETE_CB_ID :
  399. husart->TxHalfCpltCallback = pCallback;
  400. break;
  401. case HAL_USART_TX_COMPLETE_CB_ID :
  402. husart->TxCpltCallback = pCallback;
  403. break;
  404. case HAL_USART_RX_HALFCOMPLETE_CB_ID :
  405. husart->RxHalfCpltCallback = pCallback;
  406. break;
  407. case HAL_USART_RX_COMPLETE_CB_ID :
  408. husart->RxCpltCallback = pCallback;
  409. break;
  410. case HAL_USART_TX_RX_COMPLETE_CB_ID :
  411. husart->TxRxCpltCallback = pCallback;
  412. break;
  413. case HAL_USART_ERROR_CB_ID :
  414. husart->ErrorCallback = pCallback;
  415. break;
  416. case HAL_USART_ABORT_COMPLETE_CB_ID :
  417. husart->AbortCpltCallback = pCallback;
  418. break;
  419. case HAL_USART_MSPINIT_CB_ID :
  420. husart->MspInitCallback = pCallback;
  421. break;
  422. case HAL_USART_MSPDEINIT_CB_ID :
  423. husart->MspDeInitCallback = pCallback;
  424. break;
  425. default :
  426. /* Update the error code */
  427. husart->ErrorCode |= HAL_USART_ERROR_INVALID_CALLBACK;
  428. /* Return error status */
  429. status = HAL_ERROR;
  430. break;
  431. }
  432. }
  433. else if (husart->State == HAL_USART_STATE_RESET)
  434. {
  435. switch (CallbackID)
  436. {
  437. case HAL_USART_MSPINIT_CB_ID :
  438. husart->MspInitCallback = pCallback;
  439. break;
  440. case HAL_USART_MSPDEINIT_CB_ID :
  441. husart->MspDeInitCallback = pCallback;
  442. break;
  443. default :
  444. /* Update the error code */
  445. husart->ErrorCode |= HAL_USART_ERROR_INVALID_CALLBACK;
  446. /* Return error status */
  447. status = HAL_ERROR;
  448. break;
  449. }
  450. }
  451. else
  452. {
  453. /* Update the error code */
  454. husart->ErrorCode |= HAL_USART_ERROR_INVALID_CALLBACK;
  455. /* Return error status */
  456. status = HAL_ERROR;
  457. }
  458. return status;
  459. }
  460. /**
  461. * @brief Unregister an USART Callback
  462. * USART callaback is redirected to the weak predefined callback
  463. * @note The HAL_USART_UnRegisterCallback() may be called before HAL_USART_Init() in HAL_USART_STATE_RESET
  464. * to un-register callbacks for HAL_USART_MSPINIT_CB_ID and HAL_USART_MSPDEINIT_CB_ID
  465. * @param husart usart handle
  466. * @param CallbackID ID of the callback to be unregistered
  467. * This parameter can be one of the following values:
  468. * @arg @ref HAL_USART_TX_HALFCOMPLETE_CB_ID Tx Half Complete Callback ID
  469. * @arg @ref HAL_USART_TX_COMPLETE_CB_ID Tx Complete Callback ID
  470. * @arg @ref HAL_USART_RX_HALFCOMPLETE_CB_ID Rx Half Complete Callback ID
  471. * @arg @ref HAL_USART_RX_COMPLETE_CB_ID Rx Complete Callback ID
  472. * @arg @ref HAL_USART_TX_RX_COMPLETE_CB_ID Rx Complete Callback ID
  473. * @arg @ref HAL_USART_ERROR_CB_ID Error Callback ID
  474. * @arg @ref HAL_USART_ABORT_COMPLETE_CB_ID Abort Complete Callback ID
  475. * @arg @ref HAL_USART_MSPINIT_CB_ID MspInit Callback ID
  476. * @arg @ref HAL_USART_MSPDEINIT_CB_ID MspDeInit Callback ID
  477. * @retval HAL status
  478. */
  479. HAL_StatusTypeDef HAL_USART_UnRegisterCallback(USART_HandleTypeDef *husart, HAL_USART_CallbackIDTypeDef CallbackID)
  480. {
  481. HAL_StatusTypeDef status = HAL_OK;
  482. if (HAL_USART_STATE_READY == husart->State)
  483. {
  484. switch (CallbackID)
  485. {
  486. case HAL_USART_TX_HALFCOMPLETE_CB_ID :
  487. husart->TxHalfCpltCallback = HAL_USART_TxHalfCpltCallback; /* Legacy weak TxHalfCpltCallback */
  488. break;
  489. case HAL_USART_TX_COMPLETE_CB_ID :
  490. husart->TxCpltCallback = HAL_USART_TxCpltCallback; /* Legacy weak TxCpltCallback */
  491. break;
  492. case HAL_USART_RX_HALFCOMPLETE_CB_ID :
  493. husart->RxHalfCpltCallback = HAL_USART_RxHalfCpltCallback; /* Legacy weak RxHalfCpltCallback */
  494. break;
  495. case HAL_USART_RX_COMPLETE_CB_ID :
  496. husart->RxCpltCallback = HAL_USART_RxCpltCallback; /* Legacy weak RxCpltCallback */
  497. break;
  498. case HAL_USART_TX_RX_COMPLETE_CB_ID :
  499. husart->TxRxCpltCallback = HAL_USART_TxRxCpltCallback; /* Legacy weak TxRxCpltCallback */
  500. break;
  501. case HAL_USART_ERROR_CB_ID :
  502. husart->ErrorCallback = HAL_USART_ErrorCallback; /* Legacy weak ErrorCallback */
  503. break;
  504. case HAL_USART_ABORT_COMPLETE_CB_ID :
  505. husart->AbortCpltCallback = HAL_USART_AbortCpltCallback; /* Legacy weak AbortCpltCallback */
  506. break;
  507. case HAL_USART_MSPINIT_CB_ID :
  508. husart->MspInitCallback = HAL_USART_MspInit; /* Legacy weak MspInitCallback */
  509. break;
  510. case HAL_USART_MSPDEINIT_CB_ID :
  511. husart->MspDeInitCallback = HAL_USART_MspDeInit; /* Legacy weak MspDeInitCallback */
  512. break;
  513. default :
  514. /* Update the error code */
  515. husart->ErrorCode |= HAL_USART_ERROR_INVALID_CALLBACK;
  516. /* Return error status */
  517. status = HAL_ERROR;
  518. break;
  519. }
  520. }
  521. else if (HAL_USART_STATE_RESET == husart->State)
  522. {
  523. switch (CallbackID)
  524. {
  525. case HAL_USART_MSPINIT_CB_ID :
  526. husart->MspInitCallback = HAL_USART_MspInit;
  527. break;
  528. case HAL_USART_MSPDEINIT_CB_ID :
  529. husart->MspDeInitCallback = HAL_USART_MspDeInit;
  530. break;
  531. default :
  532. /* Update the error code */
  533. husart->ErrorCode |= HAL_USART_ERROR_INVALID_CALLBACK;
  534. /* Return error status */
  535. status = HAL_ERROR;
  536. break;
  537. }
  538. }
  539. else
  540. {
  541. /* Update the error code */
  542. husart->ErrorCode |= HAL_USART_ERROR_INVALID_CALLBACK;
  543. /* Return error status */
  544. status = HAL_ERROR;
  545. }
  546. return status;
  547. }
  548. #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
  549. /**
  550. * @}
  551. */
  552. /** @defgroup USART_Exported_Functions_Group2 IO operation functions
  553. * @brief USART Transmit and Receive functions
  554. *
  555. @verbatim
  556. ===============================================================================
  557. ##### IO operation functions #####
  558. ===============================================================================
  559. [..] This subsection provides a set of functions allowing to manage the USART synchronous
  560. data transfers.
  561. [..] The USART supports master mode only: it cannot receive or send data related to an input
  562. clock (SCLK is always an output).
  563. [..]
  564. (#) There are two modes of transfer:
  565. (++) Blocking mode: The communication is performed in polling mode.
  566. The HAL status of all data processing is returned by the same function
  567. after finishing transfer.
  568. (++) No-Blocking mode: The communication is performed using Interrupts
  569. or DMA, These API's return the HAL status.
  570. The end of the data processing will be indicated through the
  571. dedicated USART IRQ when using Interrupt mode or the DMA IRQ when
  572. using DMA mode.
  573. The HAL_USART_TxCpltCallback(), HAL_USART_RxCpltCallback() and HAL_USART_TxRxCpltCallback() user callbacks
  574. will be executed respectively at the end of the transmit or Receive process
  575. The HAL_USART_ErrorCallback()user callback will be executed when a communication error is detected
  576. (#) Blocking mode API's are :
  577. (++) HAL_USART_Transmit() in simplex mode
  578. (++) HAL_USART_Receive() in full duplex receive only
  579. (++) HAL_USART_TransmitReceive() in full duplex mode
  580. (#) Non-Blocking mode API's with Interrupt are :
  581. (++) HAL_USART_Transmit_IT() in simplex mode
  582. (++) HAL_USART_Receive_IT() in full duplex receive only
  583. (++) HAL_USART_TransmitReceive_IT() in full duplex mode
  584. (++) HAL_USART_IRQHandler()
  585. (#) No-Blocking mode API's with DMA are :
  586. (++) HAL_USART_Transmit_DMA() in simplex mode
  587. (++) HAL_USART_Receive_DMA() in full duplex receive only
  588. (++) HAL_USART_TransmitReceive_DMA() in full duplex mode
  589. (++) HAL_USART_DMAPause()
  590. (++) HAL_USART_DMAResume()
  591. (++) HAL_USART_DMAStop()
  592. (#) A set of Transfer Complete Callbacks are provided in Non_Blocking mode:
  593. (++) HAL_USART_TxCpltCallback()
  594. (++) HAL_USART_RxCpltCallback()
  595. (++) HAL_USART_TxHalfCpltCallback()
  596. (++) HAL_USART_RxHalfCpltCallback()
  597. (++) HAL_USART_ErrorCallback()
  598. (++) HAL_USART_TxRxCpltCallback()
  599. (#) Non-Blocking mode transfers could be aborted using Abort API's :
  600. (++) HAL_USART_Abort()
  601. (++) HAL_USART_Abort_IT()
  602. (#) For Abort services based on interrupts (HAL_USART_Abort_IT), a Abort Complete Callbacks is provided:
  603. (++) HAL_USART_AbortCpltCallback()
  604. (#) In Non-Blocking mode transfers, possible errors are split into 2 categories.
  605. Errors are handled as follows :
  606. (++) Error is considered as Recoverable and non blocking : Transfer could go till end, but error severity is
  607. to be evaluated by user : this concerns Frame Error,
  608. Parity Error or Noise Error in Interrupt mode reception .
  609. Received character is then retrieved and stored in Rx buffer, Error code is set to allow user to identify
  610. error type, and HAL_USART_ErrorCallback() user callback is executed.
  611. Transfer is kept ongoing on USART side.
  612. If user wants to abort it, Abort services should be called by user.
  613. (++) Error is considered as Blocking : Transfer could not be completed properly and is aborted.
  614. This concerns Overrun Error In Interrupt mode reception and all errors in DMA mode.
  615. Error code is set to allow user to identify error type,
  616. and HAL_USART_ErrorCallback() user callback is executed.
  617. @endverbatim
  618. * @{
  619. */
  620. /**
  621. * @brief Simplex send an amount of data in blocking mode.
  622. * @note When USART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
  623. * the sent data is handled as a set of u16. In this case, Size must indicate the number
  624. * of u16 provided through pTxData.
  625. * @note When USART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
  626. * address of user data buffer containing data to be sent, should be aligned on a half word frontier (16 bits)
  627. * (as sent data will be handled using u16 pointer cast). Depending on compilation chain,
  628. * use of specific alignment compilation directives or pragmas might be required
  629. * to ensure proper alignment for pTxData.
  630. * @param husart USART handle.
  631. * @param pTxData Pointer to data buffer (u8 or u16 data elements).
  632. * @param Size Amount of data elements (u8 or u16) to be sent.
  633. * @param Timeout Timeout duration.
  634. * @retval HAL status
  635. */
  636. HAL_StatusTypeDef HAL_USART_Transmit(USART_HandleTypeDef *husart, const uint8_t *pTxData, uint16_t Size,
  637. uint32_t Timeout)
  638. {
  639. const uint8_t *ptxdata8bits;
  640. const uint16_t *ptxdata16bits;
  641. uint32_t tickstart;
  642. if (husart->State == HAL_USART_STATE_READY)
  643. {
  644. if ((pTxData == NULL) || (Size == 0U))
  645. {
  646. return HAL_ERROR;
  647. }
  648. /* In case of 9bits/No Parity transfer, pTxData buffer provided as input parameter
  649. should be aligned on a u16 frontier, as data to be filled into TDR will be
  650. handled through a u16 cast. */
  651. if ((husart->Init.WordLength == USART_WORDLENGTH_9B) && (husart->Init.Parity == USART_PARITY_NONE))
  652. {
  653. if ((((uint32_t)pTxData) & 1U) != 0U)
  654. {
  655. return HAL_ERROR;
  656. }
  657. }
  658. /* Process Locked */
  659. __HAL_LOCK(husart);
  660. husart->ErrorCode = HAL_USART_ERROR_NONE;
  661. husart->State = HAL_USART_STATE_BUSY_TX;
  662. /* Init tickstart for timeout management */
  663. tickstart = HAL_GetTick();
  664. husart->TxXferSize = Size;
  665. husart->TxXferCount = Size;
  666. /* In case of 9bits/No Parity transfer, pTxData needs to be handled as a uint16_t pointer */
  667. if ((husart->Init.WordLength == USART_WORDLENGTH_9B) && (husart->Init.Parity == USART_PARITY_NONE))
  668. {
  669. ptxdata8bits = NULL;
  670. ptxdata16bits = (const uint16_t *) pTxData;
  671. }
  672. else
  673. {
  674. ptxdata8bits = pTxData;
  675. ptxdata16bits = NULL;
  676. }
  677. /* Check the remaining data to be sent */
  678. while (husart->TxXferCount > 0U)
  679. {
  680. if (USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_TXE, RESET, tickstart, Timeout) != HAL_OK)
  681. {
  682. return HAL_TIMEOUT;
  683. }
  684. if (ptxdata8bits == NULL)
  685. {
  686. husart->Instance->TDR = (uint16_t)(*ptxdata16bits & 0x01FFU);
  687. ptxdata16bits++;
  688. }
  689. else
  690. {
  691. husart->Instance->TDR = (uint8_t)(*ptxdata8bits & 0xFFU);
  692. ptxdata8bits++;
  693. }
  694. husart->TxXferCount--;
  695. }
  696. if (USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_TC, RESET, tickstart, Timeout) != HAL_OK)
  697. {
  698. return HAL_TIMEOUT;
  699. }
  700. /* Clear Transmission Complete Flag */
  701. __HAL_USART_CLEAR_FLAG(husart, USART_CLEAR_TCF);
  702. /* Clear overrun flag and discard the received data */
  703. __HAL_USART_CLEAR_OREFLAG(husart);
  704. __HAL_USART_SEND_REQ(husart, USART_RXDATA_FLUSH_REQUEST);
  705. #if defined(USART_RQR_TXFRQ)
  706. __HAL_USART_SEND_REQ(husart, USART_TXDATA_FLUSH_REQUEST);
  707. #endif /* USART_RQR_TXFRQ */
  708. /* At end of Tx process, restore husart->State to Ready */
  709. husart->State = HAL_USART_STATE_READY;
  710. /* Process Unlocked */
  711. __HAL_UNLOCK(husart);
  712. return HAL_OK;
  713. }
  714. else
  715. {
  716. return HAL_BUSY;
  717. }
  718. }
  719. /**
  720. * @brief Receive an amount of data in blocking mode.
  721. * @note To receive synchronous data, dummy data are simultaneously transmitted.
  722. * @note When USART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
  723. * the received data is handled as a set of u16. In this case, Size must indicate the number
  724. * of u16 available through pRxData.
  725. * @note When USART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
  726. * address of user data buffer for storing data to be received, should be aligned on a half word frontier
  727. * (16 bits) (as received data will be handled using u16 pointer cast). Depending on compilation chain,
  728. * use of specific alignment compilation directives or pragmas might be required to ensure
  729. * proper alignment for pRxData.
  730. * @param husart USART handle.
  731. * @param pRxData Pointer to data buffer (u8 or u16 data elements).
  732. * @param Size Amount of data elements (u8 or u16) to be received.
  733. * @param Timeout Timeout duration.
  734. * @retval HAL status
  735. */
  736. HAL_StatusTypeDef HAL_USART_Receive(USART_HandleTypeDef *husart, uint8_t *pRxData, uint16_t Size, uint32_t Timeout)
  737. {
  738. uint8_t *prxdata8bits;
  739. uint16_t *prxdata16bits;
  740. uint16_t uhMask;
  741. uint32_t tickstart;
  742. if (husart->State == HAL_USART_STATE_READY)
  743. {
  744. if ((pRxData == NULL) || (Size == 0U))
  745. {
  746. return HAL_ERROR;
  747. }
  748. /* In case of 9bits/No Parity transfer, pRxData buffer provided as input parameter
  749. should be aligned on a u16 frontier, as data to be received from RDR will be
  750. handled through a u16 cast. */
  751. if ((husart->Init.WordLength == USART_WORDLENGTH_9B) && (husart->Init.Parity == USART_PARITY_NONE))
  752. {
  753. if ((((uint32_t)pRxData) & 1U) != 0U)
  754. {
  755. return HAL_ERROR;
  756. }
  757. }
  758. /* Process Locked */
  759. __HAL_LOCK(husart);
  760. husart->ErrorCode = HAL_USART_ERROR_NONE;
  761. husart->State = HAL_USART_STATE_BUSY_RX;
  762. /* Init tickstart for timeout management */
  763. tickstart = HAL_GetTick();
  764. husart->RxXferSize = Size;
  765. husart->RxXferCount = Size;
  766. /* Computation of USART mask to apply to RDR register */
  767. USART_MASK_COMPUTATION(husart);
  768. uhMask = husart->Mask;
  769. /* In case of 9bits/No Parity transfer, pRxData needs to be handled as a uint16_t pointer */
  770. if ((husart->Init.WordLength == USART_WORDLENGTH_9B) && (husart->Init.Parity == USART_PARITY_NONE))
  771. {
  772. prxdata8bits = NULL;
  773. prxdata16bits = (uint16_t *) pRxData;
  774. }
  775. else
  776. {
  777. prxdata8bits = pRxData;
  778. prxdata16bits = NULL;
  779. }
  780. /* as long as data have to be received */
  781. while (husart->RxXferCount > 0U)
  782. {
  783. {
  784. /* Wait until TXE flag is set to send dummy byte in order to generate the
  785. * clock for the slave to send data.
  786. * Whatever the frame length (7, 8 or 9-bit long), the same dummy value
  787. * can be written for all the cases. */
  788. if (USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_TXE, RESET, tickstart, Timeout) != HAL_OK)
  789. {
  790. return HAL_TIMEOUT;
  791. }
  792. husart->Instance->TDR = (USART_DUMMY_DATA & (uint16_t)0x0FF);
  793. }
  794. /* Wait for RXNE Flag */
  795. if (USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_RXNE, RESET, tickstart, Timeout) != HAL_OK)
  796. {
  797. return HAL_TIMEOUT;
  798. }
  799. if (prxdata8bits == NULL)
  800. {
  801. *prxdata16bits = (uint16_t)(husart->Instance->RDR & uhMask);
  802. prxdata16bits++;
  803. }
  804. else
  805. {
  806. *prxdata8bits = (uint8_t)(husart->Instance->RDR & (uint8_t)(uhMask & 0xFFU));
  807. prxdata8bits++;
  808. }
  809. husart->RxXferCount--;
  810. }
  811. /* At end of Rx process, restore husart->State to Ready */
  812. husart->State = HAL_USART_STATE_READY;
  813. /* Process Unlocked */
  814. __HAL_UNLOCK(husart);
  815. return HAL_OK;
  816. }
  817. else
  818. {
  819. return HAL_BUSY;
  820. }
  821. }
  822. /**
  823. * @brief Full-Duplex Send and Receive an amount of data in blocking mode.
  824. * @note When USART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
  825. * the sent data and the received data are handled as sets of u16. In this case, Size must indicate the number
  826. * of u16 available through pTxData and through pRxData.
  827. * @note When USART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
  828. * address of user data buffers containing data to be sent/received, should be aligned on a half word frontier
  829. * (16 bits) (as sent/received data will be handled using u16 pointer cast). Depending on compilation chain,
  830. * use of specific alignment compilation directives or pragmas might be required to ensure
  831. * proper alignment for pTxData and pRxData.
  832. * @param husart USART handle.
  833. * @param pTxData pointer to TX data buffer (u8 or u16 data elements).
  834. * @param pRxData pointer to RX data buffer (u8 or u16 data elements).
  835. * @param Size amount of data elements (u8 or u16) to be sent (same amount to be received).
  836. * @param Timeout Timeout duration.
  837. * @retval HAL status
  838. */
  839. HAL_StatusTypeDef HAL_USART_TransmitReceive(USART_HandleTypeDef *husart, const uint8_t *pTxData, uint8_t *pRxData,
  840. uint16_t Size, uint32_t Timeout)
  841. {
  842. uint8_t *prxdata8bits;
  843. uint16_t *prxdata16bits;
  844. const uint8_t *ptxdata8bits;
  845. const uint16_t *ptxdata16bits;
  846. uint16_t uhMask;
  847. uint16_t rxdatacount;
  848. uint32_t tickstart;
  849. if (husart->State == HAL_USART_STATE_READY)
  850. {
  851. if ((pTxData == NULL) || (pRxData == NULL) || (Size == 0U))
  852. {
  853. return HAL_ERROR;
  854. }
  855. /* In case of 9bits/No Parity transfer, pTxData and pRxData buffers provided as input parameter
  856. should be aligned on a u16 frontier, as data to be filled into TDR/retrieved from RDR will be
  857. handled through a u16 cast. */
  858. if ((husart->Init.WordLength == USART_WORDLENGTH_9B) && (husart->Init.Parity == USART_PARITY_NONE))
  859. {
  860. if (((((uint32_t)pTxData) & 1U) != 0U) || ((((uint32_t)pRxData) & 1U) != 0U))
  861. {
  862. return HAL_ERROR;
  863. }
  864. }
  865. /* Process Locked */
  866. __HAL_LOCK(husart);
  867. husart->ErrorCode = HAL_USART_ERROR_NONE;
  868. husart->State = HAL_USART_STATE_BUSY_RX;
  869. /* Init tickstart for timeout management */
  870. tickstart = HAL_GetTick();
  871. husart->RxXferSize = Size;
  872. husart->TxXferSize = Size;
  873. husart->TxXferCount = Size;
  874. husart->RxXferCount = Size;
  875. /* Computation of USART mask to apply to RDR register */
  876. USART_MASK_COMPUTATION(husart);
  877. uhMask = husart->Mask;
  878. /* In case of 9bits/No Parity transfer, pRxData needs to be handled as a uint16_t pointer */
  879. if ((husart->Init.WordLength == USART_WORDLENGTH_9B) && (husart->Init.Parity == USART_PARITY_NONE))
  880. {
  881. prxdata8bits = NULL;
  882. ptxdata8bits = NULL;
  883. ptxdata16bits = (const uint16_t *) pTxData;
  884. prxdata16bits = (uint16_t *) pRxData;
  885. }
  886. else
  887. {
  888. prxdata8bits = pRxData;
  889. ptxdata8bits = pTxData;
  890. ptxdata16bits = NULL;
  891. prxdata16bits = NULL;
  892. }
  893. if (husart->TxXferCount == 0x01U)
  894. {
  895. /* Wait until TXE flag is set to send data */
  896. if (USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_TXE, RESET, tickstart, Timeout) != HAL_OK)
  897. {
  898. return HAL_TIMEOUT;
  899. }
  900. if (ptxdata8bits == NULL)
  901. {
  902. husart->Instance->TDR = (uint16_t)(*ptxdata16bits & uhMask);
  903. ptxdata16bits++;
  904. }
  905. else
  906. {
  907. husart->Instance->TDR = (uint8_t)(*ptxdata8bits & (uint8_t)(uhMask & 0xFFU));
  908. ptxdata8bits++;
  909. }
  910. husart->TxXferCount--;
  911. }
  912. /* Check the remain data to be sent */
  913. /* rxdatacount is a temporary variable for MISRAC2012-Rule-13.5 */
  914. rxdatacount = husart->RxXferCount;
  915. while ((husart->TxXferCount > 0U) || (rxdatacount > 0U))
  916. {
  917. if (husart->TxXferCount > 0U)
  918. {
  919. /* Wait until TXE flag is set to send data */
  920. if (USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_TXE, RESET, tickstart, Timeout) != HAL_OK)
  921. {
  922. return HAL_TIMEOUT;
  923. }
  924. if (ptxdata8bits == NULL)
  925. {
  926. husart->Instance->TDR = (uint16_t)(*ptxdata16bits & uhMask);
  927. ptxdata16bits++;
  928. }
  929. else
  930. {
  931. husart->Instance->TDR = (uint8_t)(*ptxdata8bits & (uint8_t)(uhMask & 0xFFU));
  932. ptxdata8bits++;
  933. }
  934. husart->TxXferCount--;
  935. }
  936. if (husart->RxXferCount > 0U)
  937. {
  938. /* Wait for RXNE Flag */
  939. if (USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_RXNE, RESET, tickstart, Timeout) != HAL_OK)
  940. {
  941. return HAL_TIMEOUT;
  942. }
  943. if (prxdata8bits == NULL)
  944. {
  945. *prxdata16bits = (uint16_t)(husart->Instance->RDR & uhMask);
  946. prxdata16bits++;
  947. }
  948. else
  949. {
  950. *prxdata8bits = (uint8_t)(husart->Instance->RDR & (uint8_t)(uhMask & 0xFFU));
  951. prxdata8bits++;
  952. }
  953. husart->RxXferCount--;
  954. }
  955. rxdatacount = husart->RxXferCount;
  956. }
  957. /* At end of TxRx process, restore husart->State to Ready */
  958. husart->State = HAL_USART_STATE_READY;
  959. /* Process Unlocked */
  960. __HAL_UNLOCK(husart);
  961. return HAL_OK;
  962. }
  963. else
  964. {
  965. return HAL_BUSY;
  966. }
  967. }
  968. /**
  969. * @brief Send an amount of data in interrupt mode.
  970. * @note When USART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
  971. * the sent data is handled as a set of u16. In this case, Size must indicate the number
  972. * of u16 provided through pTxData.
  973. * @note When USART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
  974. * address of user data buffer containing data to be sent, should be aligned on a half word frontier
  975. * (16 bits) (as sent data will be handled using u16 pointer cast). Depending on compilation chain,
  976. * use of specific alignment compilation directives or pragmas might be required to ensure
  977. * proper alignment for pTxData.
  978. * @param husart USART handle.
  979. * @param pTxData pointer to data buffer (u8 or u16 data elements).
  980. * @param Size amount of data elements (u8 or u16) to be sent.
  981. * @retval HAL status
  982. */
  983. HAL_StatusTypeDef HAL_USART_Transmit_IT(USART_HandleTypeDef *husart, const uint8_t *pTxData, uint16_t Size)
  984. {
  985. if (husart->State == HAL_USART_STATE_READY)
  986. {
  987. if ((pTxData == NULL) || (Size == 0U))
  988. {
  989. return HAL_ERROR;
  990. }
  991. /* In case of 9bits/No Parity transfer, pTxData buffer provided as input parameter
  992. should be aligned on a u16 frontier, as data to be filled into TDR will be
  993. handled through a u16 cast. */
  994. if ((husart->Init.WordLength == USART_WORDLENGTH_9B) && (husart->Init.Parity == USART_PARITY_NONE))
  995. {
  996. if ((((uint32_t)pTxData) & 1U) != 0U)
  997. {
  998. return HAL_ERROR;
  999. }
  1000. }
  1001. /* Process Locked */
  1002. __HAL_LOCK(husart);
  1003. husart->pTxBuffPtr = pTxData;
  1004. husart->TxXferSize = Size;
  1005. husart->TxXferCount = Size;
  1006. husart->TxISR = NULL;
  1007. husart->ErrorCode = HAL_USART_ERROR_NONE;
  1008. husart->State = HAL_USART_STATE_BUSY_TX;
  1009. /* The USART Error Interrupts: (Frame error, noise error, overrun error)
  1010. are not managed by the USART Transmit Process to avoid the overrun interrupt
  1011. when the usart mode is configured for transmit and receive "USART_MODE_TX_RX"
  1012. to benefit for the frame error and noise interrupts the usart mode should be
  1013. configured only for transmit "USART_MODE_TX" */
  1014. {
  1015. /* Set the Tx ISR function pointer according to the data word length */
  1016. if ((husart->Init.WordLength == USART_WORDLENGTH_9B) && (husart->Init.Parity == USART_PARITY_NONE))
  1017. {
  1018. husart->TxISR = USART_TxISR_16BIT;
  1019. }
  1020. else
  1021. {
  1022. husart->TxISR = USART_TxISR_8BIT;
  1023. }
  1024. /* Process Unlocked */
  1025. __HAL_UNLOCK(husart);
  1026. /* Enable the USART Transmit Data Register Empty Interrupt */
  1027. __HAL_USART_ENABLE_IT(husart, USART_IT_TXE);
  1028. }
  1029. return HAL_OK;
  1030. }
  1031. else
  1032. {
  1033. return HAL_BUSY;
  1034. }
  1035. }
  1036. /**
  1037. * @brief Receive an amount of data in interrupt mode.
  1038. * @note To receive synchronous data, dummy data are simultaneously transmitted.
  1039. * @note When USART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
  1040. * the received data is handled as a set of u16. In this case, Size must indicate the number
  1041. * of u16 available through pRxData.
  1042. * @note When USART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
  1043. * address of user data buffer for storing data to be received, should be aligned on a half word frontier
  1044. * (16 bits) (as received data will be handled using u16 pointer cast). Depending on compilation chain,
  1045. * use of specific alignment compilation directives or pragmas might be required to ensure
  1046. * proper alignment for pRxData.
  1047. * @param husart USART handle.
  1048. * @param pRxData pointer to data buffer (u8 or u16 data elements).
  1049. * @param Size amount of data elements (u8 or u16) to be received.
  1050. * @retval HAL status
  1051. */
  1052. HAL_StatusTypeDef HAL_USART_Receive_IT(USART_HandleTypeDef *husart, uint8_t *pRxData, uint16_t Size)
  1053. {
  1054. if (husart->State == HAL_USART_STATE_READY)
  1055. {
  1056. if ((pRxData == NULL) || (Size == 0U))
  1057. {
  1058. return HAL_ERROR;
  1059. }
  1060. /* In case of 9bits/No Parity transfer, pRxData buffer provided as input parameter
  1061. should be aligned on a u16 frontier, as data to be received from RDR will be
  1062. handled through a u16 cast. */
  1063. if ((husart->Init.WordLength == USART_WORDLENGTH_9B) && (husart->Init.Parity == USART_PARITY_NONE))
  1064. {
  1065. if ((((uint32_t)pRxData) & 1U) != 0U)
  1066. {
  1067. return HAL_ERROR;
  1068. }
  1069. }
  1070. /* Process Locked */
  1071. __HAL_LOCK(husart);
  1072. husart->pRxBuffPtr = pRxData;
  1073. husart->RxXferSize = Size;
  1074. husart->RxXferCount = Size;
  1075. husart->RxISR = NULL;
  1076. USART_MASK_COMPUTATION(husart);
  1077. husart->ErrorCode = HAL_USART_ERROR_NONE;
  1078. husart->State = HAL_USART_STATE_BUSY_RX;
  1079. /* Enable the USART Error Interrupt: (Frame error, noise error, overrun error) */
  1080. SET_BIT(husart->Instance->CR3, USART_CR3_EIE);
  1081. {
  1082. /* Set the Rx ISR function pointer according to the data word length */
  1083. if ((husart->Init.WordLength == USART_WORDLENGTH_9B) && (husart->Init.Parity == USART_PARITY_NONE))
  1084. {
  1085. husart->RxISR = USART_RxISR_16BIT;
  1086. }
  1087. else
  1088. {
  1089. husart->RxISR = USART_RxISR_8BIT;
  1090. }
  1091. /* Process Unlocked */
  1092. __HAL_UNLOCK(husart);
  1093. /* Enable the USART Parity Error and Data Register not empty Interrupts */
  1094. if (husart->Init.Parity != USART_PARITY_NONE)
  1095. {
  1096. SET_BIT(husart->Instance->CR1, USART_CR1_PEIE | USART_CR1_RXNEIE);
  1097. }
  1098. else
  1099. {
  1100. SET_BIT(husart->Instance->CR1, USART_CR1_RXNEIE);
  1101. }
  1102. }
  1103. {
  1104. /* Send dummy data in order to generate the clock for the Slave to send the next data.
  1105. */
  1106. {
  1107. husart->Instance->TDR = (USART_DUMMY_DATA & (uint16_t)0x00FF);
  1108. }
  1109. }
  1110. return HAL_OK;
  1111. }
  1112. else
  1113. {
  1114. return HAL_BUSY;
  1115. }
  1116. }
  1117. /**
  1118. * @brief Full-Duplex Send and Receive an amount of data in interrupt mode.
  1119. * @note When USART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
  1120. * the sent data and the received data are handled as sets of u16. In this case, Size must indicate the number
  1121. * of u16 available through pTxData and through pRxData.
  1122. * @note When USART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
  1123. * address of user data buffers containing data to be sent/received, should be aligned on a half word frontier
  1124. * (16 bits) (as sent/received data will be handled using u16 pointer cast). Depending on compilation chain,
  1125. * use of specific alignment compilation directives or pragmas might be required to ensure
  1126. * proper alignment for pTxData and pRxData.
  1127. * @param husart USART handle.
  1128. * @param pTxData pointer to TX data buffer (u8 or u16 data elements).
  1129. * @param pRxData pointer to RX data buffer (u8 or u16 data elements).
  1130. * @param Size amount of data elements (u8 or u16) to be sent (same amount to be received).
  1131. * @retval HAL status
  1132. */
  1133. HAL_StatusTypeDef HAL_USART_TransmitReceive_IT(USART_HandleTypeDef *husart, const uint8_t *pTxData, uint8_t *pRxData,
  1134. uint16_t Size)
  1135. {
  1136. if (husart->State == HAL_USART_STATE_READY)
  1137. {
  1138. if ((pTxData == NULL) || (pRxData == NULL) || (Size == 0U))
  1139. {
  1140. return HAL_ERROR;
  1141. }
  1142. /* In case of 9bits/No Parity transfer, pTxData and pRxData buffers provided as input parameter
  1143. should be aligned on a u16 frontier, as data to be filled into TDR/retrieved from RDR will be
  1144. handled through a u16 cast. */
  1145. if ((husart->Init.WordLength == USART_WORDLENGTH_9B) && (husart->Init.Parity == USART_PARITY_NONE))
  1146. {
  1147. if (((((uint32_t)pTxData) & 1U) != 0U) || ((((uint32_t)pRxData) & 1U) != 0U))
  1148. {
  1149. return HAL_ERROR;
  1150. }
  1151. }
  1152. /* Process Locked */
  1153. __HAL_LOCK(husart);
  1154. husart->pRxBuffPtr = pRxData;
  1155. husart->RxXferSize = Size;
  1156. husart->RxXferCount = Size;
  1157. husart->pTxBuffPtr = pTxData;
  1158. husart->TxXferSize = Size;
  1159. husart->TxXferCount = Size;
  1160. /* Computation of USART mask to apply to RDR register */
  1161. USART_MASK_COMPUTATION(husart);
  1162. husart->ErrorCode = HAL_USART_ERROR_NONE;
  1163. husart->State = HAL_USART_STATE_BUSY_TX_RX;
  1164. {
  1165. if ((husart->Init.WordLength == USART_WORDLENGTH_9B) && (husart->Init.Parity == USART_PARITY_NONE))
  1166. {
  1167. husart->TxISR = USART_TxISR_16BIT;
  1168. husart->RxISR = USART_RxISR_16BIT;
  1169. }
  1170. else
  1171. {
  1172. husart->TxISR = USART_TxISR_8BIT;
  1173. husart->RxISR = USART_RxISR_8BIT;
  1174. }
  1175. /* Process Locked */
  1176. __HAL_UNLOCK(husart);
  1177. /* Enable the USART Error Interrupt: (Frame error, noise error, overrun error) */
  1178. SET_BIT(husart->Instance->CR3, USART_CR3_EIE);
  1179. /* Enable the USART Parity Error and USART Data Register not empty Interrupts */
  1180. if (husart->Init.Parity != USART_PARITY_NONE)
  1181. {
  1182. SET_BIT(husart->Instance->CR1, USART_CR1_PEIE | USART_CR1_RXNEIE);
  1183. }
  1184. else
  1185. {
  1186. SET_BIT(husart->Instance->CR1, USART_CR1_RXNEIE);
  1187. }
  1188. /* Enable the USART Transmit Data Register Empty Interrupt */
  1189. SET_BIT(husart->Instance->CR1, USART_CR1_TXEIE);
  1190. }
  1191. return HAL_OK;
  1192. }
  1193. else
  1194. {
  1195. return HAL_BUSY;
  1196. }
  1197. }
  1198. /**
  1199. * @brief Send an amount of data in DMA mode.
  1200. * @note When USART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
  1201. * the sent data is handled as a set of u16. In this case, Size must indicate the number
  1202. * of u16 provided through pTxData.
  1203. * @note When USART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
  1204. * address of user data buffer containing data to be sent, should be aligned on a half word frontier (16 bits)
  1205. * (as sent data will be handled by DMA from halfword frontier). Depending on compilation chain,
  1206. * use of specific alignment compilation directives or pragmas might be required
  1207. * to ensure proper alignment for pTxData.
  1208. * @param husart USART handle.
  1209. * @param pTxData pointer to data buffer (u8 or u16 data elements).
  1210. * @param Size amount of data elements (u8 or u16) to be sent.
  1211. * @retval HAL status
  1212. */
  1213. HAL_StatusTypeDef HAL_USART_Transmit_DMA(USART_HandleTypeDef *husart, const uint8_t *pTxData, uint16_t Size)
  1214. {
  1215. HAL_StatusTypeDef status = HAL_OK;
  1216. const uint32_t *tmp;
  1217. if (husart->State == HAL_USART_STATE_READY)
  1218. {
  1219. if ((pTxData == NULL) || (Size == 0U))
  1220. {
  1221. return HAL_ERROR;
  1222. }
  1223. /* In case of 9bits/No Parity transfer, pTxData buffer provided as input parameter
  1224. should be aligned on a u16 frontier, as data copy into TDR will be
  1225. handled by DMA from a u16 frontier. */
  1226. if ((husart->Init.WordLength == USART_WORDLENGTH_9B) && (husart->Init.Parity == USART_PARITY_NONE))
  1227. {
  1228. if ((((uint32_t)pTxData) & 1U) != 0U)
  1229. {
  1230. return HAL_ERROR;
  1231. }
  1232. }
  1233. /* Process Locked */
  1234. __HAL_LOCK(husart);
  1235. husart->pTxBuffPtr = pTxData;
  1236. husart->TxXferSize = Size;
  1237. husart->TxXferCount = Size;
  1238. husart->ErrorCode = HAL_USART_ERROR_NONE;
  1239. husart->State = HAL_USART_STATE_BUSY_TX;
  1240. if (husart->hdmatx != NULL)
  1241. {
  1242. /* Set the USART DMA transfer complete callback */
  1243. husart->hdmatx->XferCpltCallback = USART_DMATransmitCplt;
  1244. /* Set the USART DMA Half transfer complete callback */
  1245. husart->hdmatx->XferHalfCpltCallback = USART_DMATxHalfCplt;
  1246. /* Set the DMA error callback */
  1247. husart->hdmatx->XferErrorCallback = USART_DMAError;
  1248. /* Enable the USART transmit DMA channel */
  1249. tmp = (const uint32_t *)&pTxData;
  1250. status = HAL_DMA_Start_IT(husart->hdmatx, *(const uint32_t *)tmp, (uint32_t)&husart->Instance->TDR, Size);
  1251. }
  1252. if (status == HAL_OK)
  1253. {
  1254. /* Clear the TC flag in the ICR register */
  1255. __HAL_USART_CLEAR_FLAG(husart, USART_CLEAR_TCF);
  1256. /* Process Unlocked */
  1257. __HAL_UNLOCK(husart);
  1258. /* Enable the DMA transfer for transmit request by setting the DMAT bit
  1259. in the USART CR3 register */
  1260. SET_BIT(husart->Instance->CR3, USART_CR3_DMAT);
  1261. return HAL_OK;
  1262. }
  1263. else
  1264. {
  1265. /* Set error code to DMA */
  1266. husart->ErrorCode = HAL_USART_ERROR_DMA;
  1267. /* Process Unlocked */
  1268. __HAL_UNLOCK(husart);
  1269. /* Restore husart->State to ready */
  1270. husart->State = HAL_USART_STATE_READY;
  1271. return HAL_ERROR;
  1272. }
  1273. }
  1274. else
  1275. {
  1276. return HAL_BUSY;
  1277. }
  1278. }
  1279. /**
  1280. * @brief Receive an amount of data in DMA mode.
  1281. * @note When the USART parity is enabled (PCE = 1), the received data contain
  1282. * the parity bit (MSB position).
  1283. * @note The USART DMA transmit channel must be configured in order to generate the clock for the slave.
  1284. * @note When USART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
  1285. * the received data is handled as a set of u16. In this case, Size must indicate the number
  1286. * of u16 available through pRxData.
  1287. * @note When USART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
  1288. * address of user data buffer for storing data to be received, should be aligned on
  1289. * a half word frontier (16 bits) (as received data will be handled by DMA from halfword frontier).
  1290. * Depending on compilation chain, use of specific alignment compilation directives or pragmas
  1291. * might be required to ensure proper alignment for pRxData.
  1292. * @param husart USART handle.
  1293. * @param pRxData pointer to data buffer (u8 or u16 data elements).
  1294. * @param Size amount of data elements (u8 or u16) to be received.
  1295. * @retval HAL status
  1296. */
  1297. HAL_StatusTypeDef HAL_USART_Receive_DMA(USART_HandleTypeDef *husart, uint8_t *pRxData, uint16_t Size)
  1298. {
  1299. HAL_StatusTypeDef status = HAL_OK;
  1300. uint32_t *tmp = (uint32_t *)&pRxData;
  1301. /* Check that a Rx process is not already ongoing */
  1302. if (husart->State == HAL_USART_STATE_READY)
  1303. {
  1304. if ((pRxData == NULL) || (Size == 0U))
  1305. {
  1306. return HAL_ERROR;
  1307. }
  1308. /* In case of 9bits/No Parity transfer, pRxData buffer provided as input parameter
  1309. should be aligned on a u16 frontier, as data copy from RDR will be
  1310. handled by DMA from a u16 frontier. */
  1311. if ((husart->Init.WordLength == USART_WORDLENGTH_9B) && (husart->Init.Parity == USART_PARITY_NONE))
  1312. {
  1313. if ((((uint32_t)pRxData) & 1U) != 0U)
  1314. {
  1315. return HAL_ERROR;
  1316. }
  1317. }
  1318. /* Process Locked */
  1319. __HAL_LOCK(husart);
  1320. husart->pRxBuffPtr = pRxData;
  1321. husart->RxXferSize = Size;
  1322. husart->pTxBuffPtr = pRxData;
  1323. husart->TxXferSize = Size;
  1324. husart->ErrorCode = HAL_USART_ERROR_NONE;
  1325. husart->State = HAL_USART_STATE_BUSY_RX;
  1326. if (husart->hdmarx != NULL)
  1327. {
  1328. /* Set the USART DMA Rx transfer complete callback */
  1329. husart->hdmarx->XferCpltCallback = USART_DMAReceiveCplt;
  1330. /* Set the USART DMA Half transfer complete callback */
  1331. husart->hdmarx->XferHalfCpltCallback = USART_DMARxHalfCplt;
  1332. /* Set the USART DMA Rx transfer error callback */
  1333. husart->hdmarx->XferErrorCallback = USART_DMAError;
  1334. /* Enable the USART receive DMA channel */
  1335. status = HAL_DMA_Start_IT(husart->hdmarx, (uint32_t)&husart->Instance->RDR, *(uint32_t *)tmp, Size);
  1336. }
  1337. {
  1338. /* Enable the USART transmit DMA channel: the transmit channel is used in order
  1339. to generate in the non-blocking mode the clock to the slave device,
  1340. this mode isn't a simplex receive mode but a full-duplex receive mode */
  1341. /* Set the USART DMA Tx Complete and Error callback to Null */
  1342. if (husart->hdmatx != NULL)
  1343. {
  1344. husart->hdmatx->XferErrorCallback = NULL;
  1345. husart->hdmatx->XferHalfCpltCallback = NULL;
  1346. husart->hdmatx->XferCpltCallback = NULL;
  1347. status = HAL_DMA_Start_IT(husart->hdmatx, *(uint32_t *)tmp, (uint32_t)&husart->Instance->TDR, Size);
  1348. }
  1349. }
  1350. if (status == HAL_OK)
  1351. {
  1352. /* Process Unlocked */
  1353. __HAL_UNLOCK(husart);
  1354. if (husart->Init.Parity != USART_PARITY_NONE)
  1355. {
  1356. /* Enable the USART Parity Error Interrupt */
  1357. SET_BIT(husart->Instance->CR1, USART_CR1_PEIE);
  1358. }
  1359. /* Enable the USART Error Interrupt: (Frame error, noise error, overrun error) */
  1360. SET_BIT(husart->Instance->CR3, USART_CR3_EIE);
  1361. /* Enable the DMA transfer for the receiver request by setting the DMAR bit
  1362. in the USART CR3 register */
  1363. SET_BIT(husart->Instance->CR3, USART_CR3_DMAR);
  1364. /* Enable the DMA transfer for transmit request by setting the DMAT bit
  1365. in the USART CR3 register */
  1366. SET_BIT(husart->Instance->CR3, USART_CR3_DMAT);
  1367. return HAL_OK;
  1368. }
  1369. else
  1370. {
  1371. if (husart->hdmarx != NULL)
  1372. {
  1373. status = HAL_DMA_Abort(husart->hdmarx);
  1374. }
  1375. /* No need to check on error code */
  1376. UNUSED(status);
  1377. /* Set error code to DMA */
  1378. husart->ErrorCode = HAL_USART_ERROR_DMA;
  1379. /* Process Unlocked */
  1380. __HAL_UNLOCK(husart);
  1381. /* Restore husart->State to ready */
  1382. husart->State = HAL_USART_STATE_READY;
  1383. return HAL_ERROR;
  1384. }
  1385. }
  1386. else
  1387. {
  1388. return HAL_BUSY;
  1389. }
  1390. }
  1391. /**
  1392. * @brief Full-Duplex Transmit Receive an amount of data in non-blocking mode.
  1393. * @note When the USART parity is enabled (PCE = 1) the data received contain the parity bit.
  1394. * @note When USART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
  1395. * the sent data and the received data are handled as sets of u16. In this case, Size must indicate the number
  1396. * of u16 available through pTxData and through pRxData.
  1397. * @note When USART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
  1398. * address of user data buffers containing data to be sent/received, should be aligned on a half word frontier
  1399. * (16 bits) (as sent/received data will be handled by DMA from halfword frontier). Depending on compilation
  1400. * chain, use of specific alignment compilation directives or pragmas might be required
  1401. * to ensure proper alignment for pTxData and pRxData.
  1402. * @param husart USART handle.
  1403. * @param pTxData pointer to TX data buffer (u8 or u16 data elements).
  1404. * @param pRxData pointer to RX data buffer (u8 or u16 data elements).
  1405. * @param Size amount of data elements (u8 or u16) to be received/sent.
  1406. * @retval HAL status
  1407. */
  1408. HAL_StatusTypeDef HAL_USART_TransmitReceive_DMA(USART_HandleTypeDef *husart, const uint8_t *pTxData, uint8_t *pRxData,
  1409. uint16_t Size)
  1410. {
  1411. HAL_StatusTypeDef status;
  1412. const uint32_t *tmp;
  1413. if (husart->State == HAL_USART_STATE_READY)
  1414. {
  1415. if ((pTxData == NULL) || (pRxData == NULL) || (Size == 0U))
  1416. {
  1417. return HAL_ERROR;
  1418. }
  1419. /* In case of 9bits/No Parity transfer, pTxData and pRxData buffers provided as input parameter
  1420. should be aligned on a u16 frontier, as data copy to/from TDR/RDR will be
  1421. handled by DMA from a u16 frontier. */
  1422. if ((husart->Init.WordLength == USART_WORDLENGTH_9B) && (husart->Init.Parity == USART_PARITY_NONE))
  1423. {
  1424. if (((((uint32_t)pTxData) & 1U) != 0U) || ((((uint32_t)pRxData) & 1U) != 0U))
  1425. {
  1426. return HAL_ERROR;
  1427. }
  1428. }
  1429. /* Process Locked */
  1430. __HAL_LOCK(husart);
  1431. husart->pRxBuffPtr = pRxData;
  1432. husart->RxXferSize = Size;
  1433. husart->pTxBuffPtr = pTxData;
  1434. husart->TxXferSize = Size;
  1435. husart->ErrorCode = HAL_USART_ERROR_NONE;
  1436. husart->State = HAL_USART_STATE_BUSY_TX_RX;
  1437. if ((husart->hdmarx != NULL) && (husart->hdmatx != NULL))
  1438. {
  1439. /* Set the USART DMA Rx transfer complete callback */
  1440. husart->hdmarx->XferCpltCallback = USART_DMAReceiveCplt;
  1441. /* Set the USART DMA Half transfer complete callback */
  1442. husart->hdmarx->XferHalfCpltCallback = USART_DMARxHalfCplt;
  1443. /* Set the USART DMA Tx transfer complete callback */
  1444. husart->hdmatx->XferCpltCallback = USART_DMATransmitCplt;
  1445. /* Set the USART DMA Half transfer complete callback */
  1446. husart->hdmatx->XferHalfCpltCallback = USART_DMATxHalfCplt;
  1447. /* Set the USART DMA Tx transfer error callback */
  1448. husart->hdmatx->XferErrorCallback = USART_DMAError;
  1449. /* Set the USART DMA Rx transfer error callback */
  1450. husart->hdmarx->XferErrorCallback = USART_DMAError;
  1451. /* Enable the USART receive DMA channel */
  1452. tmp = (uint32_t *)&pRxData;
  1453. status = HAL_DMA_Start_IT(husart->hdmarx, (uint32_t)&husart->Instance->RDR, *(const uint32_t *)tmp, Size);
  1454. /* Enable the USART transmit DMA channel */
  1455. if (status == HAL_OK)
  1456. {
  1457. tmp = (const uint32_t *)&pTxData;
  1458. status = HAL_DMA_Start_IT(husart->hdmatx, *(const uint32_t *)tmp, (uint32_t)&husart->Instance->TDR, Size);
  1459. }
  1460. }
  1461. else
  1462. {
  1463. status = HAL_ERROR;
  1464. }
  1465. if (status == HAL_OK)
  1466. {
  1467. /* Process Unlocked */
  1468. __HAL_UNLOCK(husart);
  1469. if (husart->Init.Parity != USART_PARITY_NONE)
  1470. {
  1471. /* Enable the USART Parity Error Interrupt */
  1472. SET_BIT(husart->Instance->CR1, USART_CR1_PEIE);
  1473. }
  1474. /* Enable the USART Error Interrupt: (Frame error, noise error, overrun error) */
  1475. SET_BIT(husart->Instance->CR3, USART_CR3_EIE);
  1476. /* Clear the TC flag in the ICR register */
  1477. __HAL_USART_CLEAR_FLAG(husart, USART_CLEAR_TCF);
  1478. /* Enable the DMA transfer for the receiver request by setting the DMAR bit
  1479. in the USART CR3 register */
  1480. SET_BIT(husart->Instance->CR3, USART_CR3_DMAR);
  1481. /* Enable the DMA transfer for transmit request by setting the DMAT bit
  1482. in the USART CR3 register */
  1483. SET_BIT(husart->Instance->CR3, USART_CR3_DMAT);
  1484. return HAL_OK;
  1485. }
  1486. else
  1487. {
  1488. if (husart->hdmarx != NULL)
  1489. {
  1490. status = HAL_DMA_Abort(husart->hdmarx);
  1491. }
  1492. /* No need to check on error code */
  1493. UNUSED(status);
  1494. /* Set error code to DMA */
  1495. husart->ErrorCode = HAL_USART_ERROR_DMA;
  1496. /* Process Unlocked */
  1497. __HAL_UNLOCK(husart);
  1498. /* Restore husart->State to ready */
  1499. husart->State = HAL_USART_STATE_READY;
  1500. return HAL_ERROR;
  1501. }
  1502. }
  1503. else
  1504. {
  1505. return HAL_BUSY;
  1506. }
  1507. }
  1508. /**
  1509. * @brief Pause the DMA Transfer.
  1510. * @param husart USART handle.
  1511. * @retval HAL status
  1512. */
  1513. HAL_StatusTypeDef HAL_USART_DMAPause(USART_HandleTypeDef *husart)
  1514. {
  1515. const HAL_USART_StateTypeDef state = husart->State;
  1516. /* Process Locked */
  1517. __HAL_LOCK(husart);
  1518. if ((HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAT)) &&
  1519. (state == HAL_USART_STATE_BUSY_TX))
  1520. {
  1521. /* Disable the USART DMA Tx request */
  1522. CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAT);
  1523. }
  1524. else if ((state == HAL_USART_STATE_BUSY_RX) ||
  1525. (state == HAL_USART_STATE_BUSY_TX_RX))
  1526. {
  1527. if (HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAT))
  1528. {
  1529. /* Disable the USART DMA Tx request */
  1530. CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAT);
  1531. }
  1532. if (HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAR))
  1533. {
  1534. /* Disable PE and ERR (Frame error, noise error, overrun error) interrupts */
  1535. CLEAR_BIT(husart->Instance->CR1, USART_CR1_PEIE);
  1536. CLEAR_BIT(husart->Instance->CR3, USART_CR3_EIE);
  1537. /* Disable the USART DMA Rx request */
  1538. CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAR);
  1539. }
  1540. }
  1541. else
  1542. {
  1543. /* Nothing to do */
  1544. }
  1545. /* Process Unlocked */
  1546. __HAL_UNLOCK(husart);
  1547. return HAL_OK;
  1548. }
  1549. /**
  1550. * @brief Resume the DMA Transfer.
  1551. * @param husart USART handle.
  1552. * @retval HAL status
  1553. */
  1554. HAL_StatusTypeDef HAL_USART_DMAResume(USART_HandleTypeDef *husart)
  1555. {
  1556. const HAL_USART_StateTypeDef state = husart->State;
  1557. /* Process Locked */
  1558. __HAL_LOCK(husart);
  1559. if (state == HAL_USART_STATE_BUSY_TX)
  1560. {
  1561. /* Enable the USART DMA Tx request */
  1562. SET_BIT(husart->Instance->CR3, USART_CR3_DMAT);
  1563. }
  1564. else if ((state == HAL_USART_STATE_BUSY_RX) ||
  1565. (state == HAL_USART_STATE_BUSY_TX_RX))
  1566. {
  1567. /* Clear the Overrun flag before resuming the Rx transfer*/
  1568. __HAL_USART_CLEAR_FLAG(husart, USART_CLEAR_OREF);
  1569. /* Re-enable PE and ERR (Frame error, noise error, overrun error) interrupts */
  1570. if (husart->Init.Parity != USART_PARITY_NONE)
  1571. {
  1572. SET_BIT(husart->Instance->CR1, USART_CR1_PEIE);
  1573. }
  1574. SET_BIT(husart->Instance->CR3, USART_CR3_EIE);
  1575. /* Enable the USART DMA Rx request before the DMA Tx request */
  1576. SET_BIT(husart->Instance->CR3, USART_CR3_DMAR);
  1577. /* Enable the USART DMA Tx request */
  1578. SET_BIT(husart->Instance->CR3, USART_CR3_DMAT);
  1579. }
  1580. else
  1581. {
  1582. /* Nothing to do */
  1583. }
  1584. /* Process Unlocked */
  1585. __HAL_UNLOCK(husart);
  1586. return HAL_OK;
  1587. }
  1588. /**
  1589. * @brief Stop the DMA Transfer.
  1590. * @param husart USART handle.
  1591. * @retval HAL status
  1592. */
  1593. HAL_StatusTypeDef HAL_USART_DMAStop(USART_HandleTypeDef *husart)
  1594. {
  1595. /* The Lock is not implemented on this API to allow the user application
  1596. to call the HAL USART API under callbacks HAL_USART_TxCpltCallback() / HAL_USART_RxCpltCallback() /
  1597. HAL_USART_TxHalfCpltCallback / HAL_USART_RxHalfCpltCallback:
  1598. indeed, when HAL_DMA_Abort() API is called, the DMA TX/RX Transfer or Half Transfer complete
  1599. interrupt is generated if the DMA transfer interruption occurs at the middle or at the end of
  1600. the stream and the corresponding call back is executed. */
  1601. /* Disable the USART Tx/Rx DMA requests */
  1602. CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAT);
  1603. CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAR);
  1604. /* Abort the USART DMA tx channel */
  1605. if (husart->hdmatx != NULL)
  1606. {
  1607. if (HAL_DMA_Abort(husart->hdmatx) != HAL_OK)
  1608. {
  1609. if (HAL_DMA_GetError(husart->hdmatx) == HAL_DMA_ERROR_TIMEOUT)
  1610. {
  1611. /* Set error code to DMA */
  1612. husart->ErrorCode = HAL_USART_ERROR_DMA;
  1613. return HAL_TIMEOUT;
  1614. }
  1615. }
  1616. }
  1617. /* Abort the USART DMA rx channel */
  1618. if (husart->hdmarx != NULL)
  1619. {
  1620. if (HAL_DMA_Abort(husart->hdmarx) != HAL_OK)
  1621. {
  1622. if (HAL_DMA_GetError(husart->hdmarx) == HAL_DMA_ERROR_TIMEOUT)
  1623. {
  1624. /* Set error code to DMA */
  1625. husart->ErrorCode = HAL_USART_ERROR_DMA;
  1626. return HAL_TIMEOUT;
  1627. }
  1628. }
  1629. }
  1630. USART_EndTransfer(husart);
  1631. husart->State = HAL_USART_STATE_READY;
  1632. return HAL_OK;
  1633. }
  1634. /**
  1635. * @brief Abort ongoing transfers (blocking mode).
  1636. * @param husart USART handle.
  1637. * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
  1638. * This procedure performs following operations :
  1639. * - Disable USART Interrupts (Tx and Rx)
  1640. * - Disable the DMA transfer in the peripheral register (if enabled)
  1641. * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
  1642. * - Set handle State to READY
  1643. * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
  1644. * @retval HAL status
  1645. */
  1646. HAL_StatusTypeDef HAL_USART_Abort(USART_HandleTypeDef *husart)
  1647. {
  1648. CLEAR_BIT(husart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE | USART_CR1_TXEIE | USART_CR1_TCIE));
  1649. CLEAR_BIT(husart->Instance->CR3, USART_CR3_EIE);
  1650. /* Abort the USART DMA Tx channel if enabled */
  1651. if (HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAT))
  1652. {
  1653. /* Disable the USART DMA Tx request if enabled */
  1654. CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAT);
  1655. /* Abort the USART DMA Tx channel : use blocking DMA Abort API (no callback) */
  1656. if (husart->hdmatx != NULL)
  1657. {
  1658. /* Set the USART DMA Abort callback to Null.
  1659. No call back execution at end of DMA abort procedure */
  1660. husart->hdmatx->XferAbortCallback = NULL;
  1661. if (HAL_DMA_Abort(husart->hdmatx) != HAL_OK)
  1662. {
  1663. if (HAL_DMA_GetError(husart->hdmatx) == HAL_DMA_ERROR_TIMEOUT)
  1664. {
  1665. /* Set error code to DMA */
  1666. husart->ErrorCode = HAL_USART_ERROR_DMA;
  1667. return HAL_TIMEOUT;
  1668. }
  1669. }
  1670. }
  1671. }
  1672. /* Abort the USART DMA Rx channel if enabled */
  1673. if (HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAR))
  1674. {
  1675. /* Disable the USART DMA Rx request if enabled */
  1676. CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAR);
  1677. /* Abort the USART DMA Rx channel : use blocking DMA Abort API (no callback) */
  1678. if (husart->hdmarx != NULL)
  1679. {
  1680. /* Set the USART DMA Abort callback to Null.
  1681. No call back execution at end of DMA abort procedure */
  1682. husart->hdmarx->XferAbortCallback = NULL;
  1683. if (HAL_DMA_Abort(husart->hdmarx) != HAL_OK)
  1684. {
  1685. if (HAL_DMA_GetError(husart->hdmarx) == HAL_DMA_ERROR_TIMEOUT)
  1686. {
  1687. /* Set error code to DMA */
  1688. husart->ErrorCode = HAL_USART_ERROR_DMA;
  1689. return HAL_TIMEOUT;
  1690. }
  1691. }
  1692. }
  1693. }
  1694. /* Reset Tx and Rx transfer counters */
  1695. husart->TxXferCount = 0U;
  1696. husart->RxXferCount = 0U;
  1697. /* Clear the Error flags in the ICR register */
  1698. __HAL_USART_CLEAR_FLAG(husart, USART_CLEAR_OREF | USART_CLEAR_NEF | USART_CLEAR_PEF | USART_CLEAR_FEF);
  1699. /* Discard the received data */
  1700. __HAL_USART_SEND_REQ(husart, USART_RXDATA_FLUSH_REQUEST);
  1701. /* Restore husart->State to Ready */
  1702. husart->State = HAL_USART_STATE_READY;
  1703. /* Reset Handle ErrorCode to No Error */
  1704. husart->ErrorCode = HAL_USART_ERROR_NONE;
  1705. return HAL_OK;
  1706. }
  1707. /**
  1708. * @brief Abort ongoing transfers (Interrupt mode).
  1709. * @param husart USART handle.
  1710. * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
  1711. * This procedure performs following operations :
  1712. * - Disable USART Interrupts (Tx and Rx)
  1713. * - Disable the DMA transfer in the peripheral register (if enabled)
  1714. * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
  1715. * - Set handle State to READY
  1716. * - At abort completion, call user abort complete callback
  1717. * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be
  1718. * considered as completed only when user abort complete callback is executed (not when exiting function).
  1719. * @retval HAL status
  1720. */
  1721. HAL_StatusTypeDef HAL_USART_Abort_IT(USART_HandleTypeDef *husart)
  1722. {
  1723. uint32_t abortcplt = 1U;
  1724. CLEAR_BIT(husart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE | USART_CR1_TXEIE | USART_CR1_TCIE));
  1725. CLEAR_BIT(husart->Instance->CR3, USART_CR3_EIE);
  1726. /* If DMA Tx and/or DMA Rx Handles are associated to USART Handle, DMA Abort complete callbacks should be initialised
  1727. before any call to DMA Abort functions */
  1728. /* DMA Tx Handle is valid */
  1729. if (husart->hdmatx != NULL)
  1730. {
  1731. /* Set DMA Abort Complete callback if USART DMA Tx request if enabled.
  1732. Otherwise, set it to NULL */
  1733. if (HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAT))
  1734. {
  1735. husart->hdmatx->XferAbortCallback = USART_DMATxAbortCallback;
  1736. }
  1737. else
  1738. {
  1739. husart->hdmatx->XferAbortCallback = NULL;
  1740. }
  1741. }
  1742. /* DMA Rx Handle is valid */
  1743. if (husart->hdmarx != NULL)
  1744. {
  1745. /* Set DMA Abort Complete callback if USART DMA Rx request if enabled.
  1746. Otherwise, set it to NULL */
  1747. if (HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAR))
  1748. {
  1749. husart->hdmarx->XferAbortCallback = USART_DMARxAbortCallback;
  1750. }
  1751. else
  1752. {
  1753. husart->hdmarx->XferAbortCallback = NULL;
  1754. }
  1755. }
  1756. /* Abort the USART DMA Tx channel if enabled */
  1757. if (HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAT))
  1758. {
  1759. /* Disable DMA Tx at USART level */
  1760. CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAT);
  1761. /* Abort the USART DMA Tx channel : use non blocking DMA Abort API (callback) */
  1762. if (husart->hdmatx != NULL)
  1763. {
  1764. /* USART Tx DMA Abort callback has already been initialised :
  1765. will lead to call HAL_USART_AbortCpltCallback() at end of DMA abort procedure */
  1766. /* Abort DMA TX */
  1767. if (HAL_DMA_Abort_IT(husart->hdmatx) != HAL_OK)
  1768. {
  1769. husart->hdmatx->XferAbortCallback = NULL;
  1770. }
  1771. else
  1772. {
  1773. abortcplt = 0U;
  1774. }
  1775. }
  1776. }
  1777. /* Abort the USART DMA Rx channel if enabled */
  1778. if (HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAR))
  1779. {
  1780. /* Disable the USART DMA Rx request if enabled */
  1781. CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAR);
  1782. /* Abort the USART DMA Rx channel : use non blocking DMA Abort API (callback) */
  1783. if (husart->hdmarx != NULL)
  1784. {
  1785. /* USART Rx DMA Abort callback has already been initialised :
  1786. will lead to call HAL_USART_AbortCpltCallback() at end of DMA abort procedure */
  1787. /* Abort DMA RX */
  1788. if (HAL_DMA_Abort_IT(husart->hdmarx) != HAL_OK)
  1789. {
  1790. husart->hdmarx->XferAbortCallback = NULL;
  1791. abortcplt = 1U;
  1792. }
  1793. else
  1794. {
  1795. abortcplt = 0U;
  1796. }
  1797. }
  1798. }
  1799. /* if no DMA abort complete callback execution is required => call user Abort Complete callback */
  1800. if (abortcplt == 1U)
  1801. {
  1802. /* Reset Tx and Rx transfer counters */
  1803. husart->TxXferCount = 0U;
  1804. husart->RxXferCount = 0U;
  1805. /* Reset errorCode */
  1806. husart->ErrorCode = HAL_USART_ERROR_NONE;
  1807. /* Clear the Error flags in the ICR register */
  1808. __HAL_USART_CLEAR_FLAG(husart, USART_CLEAR_OREF | USART_CLEAR_NEF | USART_CLEAR_PEF | USART_CLEAR_FEF);
  1809. /* Discard the received data */
  1810. __HAL_USART_SEND_REQ(husart, USART_RXDATA_FLUSH_REQUEST);
  1811. /* Restore husart->State to Ready */
  1812. husart->State = HAL_USART_STATE_READY;
  1813. /* As no DMA to be aborted, call directly user Abort complete callback */
  1814. #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
  1815. /* Call registered Abort Complete Callback */
  1816. husart->AbortCpltCallback(husart);
  1817. #else
  1818. /* Call legacy weak Abort Complete Callback */
  1819. HAL_USART_AbortCpltCallback(husart);
  1820. #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
  1821. }
  1822. return HAL_OK;
  1823. }
  1824. /**
  1825. * @brief Handle USART interrupt request.
  1826. * @param husart USART handle.
  1827. * @retval None
  1828. */
  1829. void HAL_USART_IRQHandler(USART_HandleTypeDef *husart)
  1830. {
  1831. uint32_t isrflags = READ_REG(husart->Instance->ISR);
  1832. uint32_t cr1its = READ_REG(husart->Instance->CR1);
  1833. uint32_t cr3its = READ_REG(husart->Instance->CR3);
  1834. uint32_t errorflags;
  1835. uint32_t errorcode;
  1836. /* If no error occurs */
  1837. errorflags = (isrflags & (uint32_t)(USART_ISR_PE | USART_ISR_FE | USART_ISR_ORE | USART_ISR_NE | USART_ISR_RTOF));
  1838. if (errorflags == 0U)
  1839. {
  1840. /* USART in mode Receiver ---------------------------------------------------*/
  1841. if (((isrflags & USART_ISR_RXNE) != 0U)
  1842. && ((cr1its & USART_CR1_RXNEIE) != 0U))
  1843. {
  1844. if (husart->RxISR != NULL)
  1845. {
  1846. husart->RxISR(husart);
  1847. }
  1848. return;
  1849. }
  1850. }
  1851. /* If some errors occur */
  1852. if ((errorflags != 0U)
  1853. && (((cr3its & USART_CR3_EIE) != 0U)
  1854. || ((cr1its & (USART_CR1_RXNEIE | USART_CR1_PEIE)) != 0U)))
  1855. {
  1856. /* USART parity error interrupt occurred -------------------------------------*/
  1857. if (((isrflags & USART_ISR_PE) != 0U) && ((cr1its & USART_CR1_PEIE) != 0U))
  1858. {
  1859. __HAL_USART_CLEAR_IT(husart, USART_CLEAR_PEF);
  1860. husart->ErrorCode |= HAL_USART_ERROR_PE;
  1861. }
  1862. /* USART frame error interrupt occurred --------------------------------------*/
  1863. if (((isrflags & USART_ISR_FE) != 0U) && ((cr3its & USART_CR3_EIE) != 0U))
  1864. {
  1865. __HAL_USART_CLEAR_IT(husart, USART_CLEAR_FEF);
  1866. husart->ErrorCode |= HAL_USART_ERROR_FE;
  1867. }
  1868. /* USART noise error interrupt occurred --------------------------------------*/
  1869. if (((isrflags & USART_ISR_NE) != 0U) && ((cr3its & USART_CR3_EIE) != 0U))
  1870. {
  1871. __HAL_USART_CLEAR_IT(husart, USART_CLEAR_NEF);
  1872. husart->ErrorCode |= HAL_USART_ERROR_NE;
  1873. }
  1874. /* USART Over-Run interrupt occurred -----------------------------------------*/
  1875. if (((isrflags & USART_ISR_ORE) != 0U)
  1876. && (((cr1its & USART_CR1_RXNEIE) != 0U) ||
  1877. ((cr3its & USART_CR3_EIE) != 0U)))
  1878. {
  1879. __HAL_USART_CLEAR_IT(husart, USART_CLEAR_OREF);
  1880. husart->ErrorCode |= HAL_USART_ERROR_ORE;
  1881. }
  1882. /* USART Receiver Timeout interrupt occurred ---------------------------------*/
  1883. if (((isrflags & USART_ISR_RTOF) != 0U) && ((cr1its & USART_CR1_RTOIE) != 0U))
  1884. {
  1885. __HAL_USART_CLEAR_IT(husart, USART_CLEAR_RTOF);
  1886. husart->ErrorCode |= HAL_USART_ERROR_RTO;
  1887. }
  1888. /* Call USART Error Call back function if need be --------------------------*/
  1889. if (husart->ErrorCode != HAL_USART_ERROR_NONE)
  1890. {
  1891. /* USART in mode Receiver ---------------------------------------------------*/
  1892. if (((isrflags & USART_ISR_RXNE) != 0U)
  1893. && ((cr1its & USART_CR1_RXNEIE) != 0U))
  1894. {
  1895. if (husart->RxISR != NULL)
  1896. {
  1897. husart->RxISR(husart);
  1898. }
  1899. }
  1900. /* If Overrun error occurs, or if any error occurs in DMA mode reception,
  1901. consider error as blocking */
  1902. errorcode = husart->ErrorCode & HAL_USART_ERROR_ORE;
  1903. if ((HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAR)) ||
  1904. (errorcode != 0U))
  1905. {
  1906. /* Blocking error : transfer is aborted
  1907. Set the USART state ready to be able to start again the process,
  1908. Disable Interrupts, and disable DMA requests, if ongoing */
  1909. USART_EndTransfer(husart);
  1910. /* Abort the USART DMA Rx channel if enabled */
  1911. if (HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAR))
  1912. {
  1913. /* Disable the USART DMA Rx request if enabled */
  1914. CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAR | USART_CR3_DMAR);
  1915. /* Abort the USART DMA Tx channel */
  1916. if (husart->hdmatx != NULL)
  1917. {
  1918. /* Set the USART Tx DMA Abort callback to NULL : no callback
  1919. executed at end of DMA abort procedure */
  1920. husart->hdmatx->XferAbortCallback = NULL;
  1921. /* Abort DMA TX */
  1922. (void)HAL_DMA_Abort_IT(husart->hdmatx);
  1923. }
  1924. /* Abort the USART DMA Rx channel */
  1925. if (husart->hdmarx != NULL)
  1926. {
  1927. /* Set the USART Rx DMA Abort callback :
  1928. will lead to call HAL_USART_ErrorCallback() at end of DMA abort procedure */
  1929. husart->hdmarx->XferAbortCallback = USART_DMAAbortOnError;
  1930. /* Abort DMA RX */
  1931. if (HAL_DMA_Abort_IT(husart->hdmarx) != HAL_OK)
  1932. {
  1933. /* Call Directly husart->hdmarx->XferAbortCallback function in case of error */
  1934. husart->hdmarx->XferAbortCallback(husart->hdmarx);
  1935. }
  1936. }
  1937. else
  1938. {
  1939. /* Call user error callback */
  1940. #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
  1941. /* Call registered Error Callback */
  1942. husart->ErrorCallback(husart);
  1943. #else
  1944. /* Call legacy weak Error Callback */
  1945. HAL_USART_ErrorCallback(husart);
  1946. #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
  1947. }
  1948. }
  1949. else
  1950. {
  1951. /* Call user error callback */
  1952. #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
  1953. /* Call registered Error Callback */
  1954. husart->ErrorCallback(husart);
  1955. #else
  1956. /* Call legacy weak Error Callback */
  1957. HAL_USART_ErrorCallback(husart);
  1958. #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
  1959. }
  1960. }
  1961. else
  1962. {
  1963. /* Non Blocking error : transfer could go on.
  1964. Error is notified to user through user error callback */
  1965. #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
  1966. /* Call registered Error Callback */
  1967. husart->ErrorCallback(husart);
  1968. #else
  1969. /* Call legacy weak Error Callback */
  1970. HAL_USART_ErrorCallback(husart);
  1971. #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
  1972. husart->ErrorCode = HAL_USART_ERROR_NONE;
  1973. }
  1974. }
  1975. return;
  1976. } /* End if some error occurs */
  1977. /* USART in mode Transmitter ------------------------------------------------*/
  1978. if (((isrflags & USART_ISR_TXE) != 0U)
  1979. && ((cr1its & USART_CR1_TXEIE) != 0U))
  1980. {
  1981. if (husart->TxISR != NULL)
  1982. {
  1983. husart->TxISR(husart);
  1984. }
  1985. return;
  1986. }
  1987. /* USART in mode Transmitter (transmission end) -----------------------------*/
  1988. if (((isrflags & USART_ISR_TC) != 0U) && ((cr1its & USART_CR1_TCIE) != 0U))
  1989. {
  1990. USART_EndTransmit_IT(husart);
  1991. return;
  1992. }
  1993. }
  1994. /**
  1995. * @brief Tx Transfer completed callback.
  1996. * @param husart USART handle.
  1997. * @retval None
  1998. */
  1999. __weak void HAL_USART_TxCpltCallback(USART_HandleTypeDef *husart)
  2000. {
  2001. /* Prevent unused argument(s) compilation warning */
  2002. UNUSED(husart);
  2003. /* NOTE : This function should not be modified, when the callback is needed,
  2004. the HAL_USART_TxCpltCallback can be implemented in the user file.
  2005. */
  2006. }
  2007. /**
  2008. * @brief Tx Half Transfer completed callback.
  2009. * @param husart USART handle.
  2010. * @retval None
  2011. */
  2012. __weak void HAL_USART_TxHalfCpltCallback(USART_HandleTypeDef *husart)
  2013. {
  2014. /* Prevent unused argument(s) compilation warning */
  2015. UNUSED(husart);
  2016. /* NOTE: This function should not be modified, when the callback is needed,
  2017. the HAL_USART_TxHalfCpltCallback can be implemented in the user file.
  2018. */
  2019. }
  2020. /**
  2021. * @brief Rx Transfer completed callback.
  2022. * @param husart USART handle.
  2023. * @retval None
  2024. */
  2025. __weak void HAL_USART_RxCpltCallback(USART_HandleTypeDef *husart)
  2026. {
  2027. /* Prevent unused argument(s) compilation warning */
  2028. UNUSED(husart);
  2029. /* NOTE: This function should not be modified, when the callback is needed,
  2030. the HAL_USART_RxCpltCallback can be implemented in the user file.
  2031. */
  2032. }
  2033. /**
  2034. * @brief Rx Half Transfer completed callback.
  2035. * @param husart USART handle.
  2036. * @retval None
  2037. */
  2038. __weak void HAL_USART_RxHalfCpltCallback(USART_HandleTypeDef *husart)
  2039. {
  2040. /* Prevent unused argument(s) compilation warning */
  2041. UNUSED(husart);
  2042. /* NOTE : This function should not be modified, when the callback is needed,
  2043. the HAL_USART_RxHalfCpltCallback can be implemented in the user file
  2044. */
  2045. }
  2046. /**
  2047. * @brief Tx/Rx Transfers completed callback for the non-blocking process.
  2048. * @param husart USART handle.
  2049. * @retval None
  2050. */
  2051. __weak void HAL_USART_TxRxCpltCallback(USART_HandleTypeDef *husart)
  2052. {
  2053. /* Prevent unused argument(s) compilation warning */
  2054. UNUSED(husart);
  2055. /* NOTE : This function should not be modified, when the callback is needed,
  2056. the HAL_USART_TxRxCpltCallback can be implemented in the user file
  2057. */
  2058. }
  2059. /**
  2060. * @brief USART error callback.
  2061. * @param husart USART handle.
  2062. * @retval None
  2063. */
  2064. __weak void HAL_USART_ErrorCallback(USART_HandleTypeDef *husart)
  2065. {
  2066. /* Prevent unused argument(s) compilation warning */
  2067. UNUSED(husart);
  2068. /* NOTE : This function should not be modified, when the callback is needed,
  2069. the HAL_USART_ErrorCallback can be implemented in the user file.
  2070. */
  2071. }
  2072. /**
  2073. * @brief USART Abort Complete callback.
  2074. * @param husart USART handle.
  2075. * @retval None
  2076. */
  2077. __weak void HAL_USART_AbortCpltCallback(USART_HandleTypeDef *husart)
  2078. {
  2079. /* Prevent unused argument(s) compilation warning */
  2080. UNUSED(husart);
  2081. /* NOTE : This function should not be modified, when the callback is needed,
  2082. the HAL_USART_AbortCpltCallback can be implemented in the user file.
  2083. */
  2084. }
  2085. /**
  2086. * @}
  2087. */
  2088. /** @defgroup USART_Exported_Functions_Group4 Peripheral State and Error functions
  2089. * @brief USART Peripheral State and Error functions
  2090. *
  2091. @verbatim
  2092. ==============================================================================
  2093. ##### Peripheral State and Error functions #####
  2094. ==============================================================================
  2095. [..]
  2096. This subsection provides functions allowing to :
  2097. (+) Return the USART handle state
  2098. (+) Return the USART handle error code
  2099. @endverbatim
  2100. * @{
  2101. */
  2102. /**
  2103. * @brief Return the USART handle state.
  2104. * @param husart pointer to a USART_HandleTypeDef structure that contains
  2105. * the configuration information for the specified USART.
  2106. * @retval USART handle state
  2107. */
  2108. HAL_USART_StateTypeDef HAL_USART_GetState(const USART_HandleTypeDef *husart)
  2109. {
  2110. return husart->State;
  2111. }
  2112. /**
  2113. * @brief Return the USART error code.
  2114. * @param husart pointer to a USART_HandleTypeDef structure that contains
  2115. * the configuration information for the specified USART.
  2116. * @retval USART handle Error Code
  2117. */
  2118. uint32_t HAL_USART_GetError(const USART_HandleTypeDef *husart)
  2119. {
  2120. return husart->ErrorCode;
  2121. }
  2122. /**
  2123. * @}
  2124. */
  2125. /**
  2126. * @}
  2127. */
  2128. /** @defgroup USART_Private_Functions USART Private Functions
  2129. * @{
  2130. */
  2131. /**
  2132. * @brief Initialize the callbacks to their default values.
  2133. * @param husart USART handle.
  2134. * @retval none
  2135. */
  2136. #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
  2137. void USART_InitCallbacksToDefault(USART_HandleTypeDef *husart)
  2138. {
  2139. /* Init the USART Callback settings */
  2140. husart->TxHalfCpltCallback = HAL_USART_TxHalfCpltCallback; /* Legacy weak TxHalfCpltCallback */
  2141. husart->TxCpltCallback = HAL_USART_TxCpltCallback; /* Legacy weak TxCpltCallback */
  2142. husart->RxHalfCpltCallback = HAL_USART_RxHalfCpltCallback; /* Legacy weak RxHalfCpltCallback */
  2143. husart->RxCpltCallback = HAL_USART_RxCpltCallback; /* Legacy weak RxCpltCallback */
  2144. husart->TxRxCpltCallback = HAL_USART_TxRxCpltCallback; /* Legacy weak TxRxCpltCallback */
  2145. husart->ErrorCallback = HAL_USART_ErrorCallback; /* Legacy weak ErrorCallback */
  2146. husart->AbortCpltCallback = HAL_USART_AbortCpltCallback; /* Legacy weak AbortCpltCallback */
  2147. }
  2148. #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
  2149. /**
  2150. * @brief End ongoing transfer on USART peripheral (following error detection or Transfer completion).
  2151. * @param husart USART handle.
  2152. * @retval None
  2153. */
  2154. static void USART_EndTransfer(USART_HandleTypeDef *husart)
  2155. {
  2156. /* Disable TXEIE, TCIE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
  2157. CLEAR_BIT(husart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE | USART_CR1_TXEIE | USART_CR1_TCIE));
  2158. CLEAR_BIT(husart->Instance->CR3, USART_CR3_EIE);
  2159. /* At end of process, restore husart->State to Ready */
  2160. husart->State = HAL_USART_STATE_READY;
  2161. }
  2162. /**
  2163. * @brief DMA USART transmit process complete callback.
  2164. * @param hdma DMA handle.
  2165. * @retval None
  2166. */
  2167. static void USART_DMATransmitCplt(DMA_HandleTypeDef *hdma)
  2168. {
  2169. USART_HandleTypeDef *husart = (USART_HandleTypeDef *)(hdma->Parent);
  2170. /* DMA Normal mode */
  2171. if (HAL_IS_BIT_CLR(hdma->Instance->CCR, DMA_CCR_CIRC))
  2172. {
  2173. husart->TxXferCount = 0U;
  2174. if (husart->State == HAL_USART_STATE_BUSY_TX)
  2175. {
  2176. /* Disable the DMA transfer for transmit request by resetting the DMAT bit
  2177. in the USART CR3 register */
  2178. CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAT);
  2179. /* Enable the USART Transmit Complete Interrupt */
  2180. __HAL_USART_ENABLE_IT(husart, USART_IT_TC);
  2181. }
  2182. }
  2183. /* DMA Circular mode */
  2184. else
  2185. {
  2186. if (husart->State == HAL_USART_STATE_BUSY_TX)
  2187. {
  2188. #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
  2189. /* Call registered Tx Complete Callback */
  2190. husart->TxCpltCallback(husart);
  2191. #else
  2192. /* Call legacy weak Tx Complete Callback */
  2193. HAL_USART_TxCpltCallback(husart);
  2194. #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
  2195. }
  2196. }
  2197. }
  2198. /**
  2199. * @brief DMA USART transmit process half complete callback.
  2200. * @param hdma DMA handle.
  2201. * @retval None
  2202. */
  2203. static void USART_DMATxHalfCplt(DMA_HandleTypeDef *hdma)
  2204. {
  2205. USART_HandleTypeDef *husart = (USART_HandleTypeDef *)(hdma->Parent);
  2206. #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
  2207. /* Call registered Tx Half Complete Callback */
  2208. husart->TxHalfCpltCallback(husart);
  2209. #else
  2210. /* Call legacy weak Tx Half Complete Callback */
  2211. HAL_USART_TxHalfCpltCallback(husart);
  2212. #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
  2213. }
  2214. /**
  2215. * @brief DMA USART receive process complete callback.
  2216. * @param hdma DMA handle.
  2217. * @retval None
  2218. */
  2219. static void USART_DMAReceiveCplt(DMA_HandleTypeDef *hdma)
  2220. {
  2221. USART_HandleTypeDef *husart = (USART_HandleTypeDef *)(hdma->Parent);
  2222. /* DMA Normal mode */
  2223. if (HAL_IS_BIT_CLR(hdma->Instance->CCR, DMA_CCR_CIRC))
  2224. {
  2225. husart->RxXferCount = 0U;
  2226. /* Disable PE and ERR (Frame error, noise error, overrun error) interrupts */
  2227. CLEAR_BIT(husart->Instance->CR1, USART_CR1_PEIE);
  2228. CLEAR_BIT(husart->Instance->CR3, USART_CR3_EIE);
  2229. /* Disable the DMA RX transfer for the receiver request by resetting the DMAR bit
  2230. in USART CR3 register */
  2231. CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAR);
  2232. /* similarly, disable the DMA TX transfer that was started to provide the
  2233. clock to the slave device */
  2234. CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAT);
  2235. if (husart->State == HAL_USART_STATE_BUSY_RX)
  2236. {
  2237. #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
  2238. /* Call registered Rx Complete Callback */
  2239. husart->RxCpltCallback(husart);
  2240. #else
  2241. /* Call legacy weak Rx Complete Callback */
  2242. HAL_USART_RxCpltCallback(husart);
  2243. #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
  2244. }
  2245. /* The USART state is HAL_USART_STATE_BUSY_TX_RX */
  2246. else
  2247. {
  2248. #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
  2249. /* Call registered Tx Rx Complete Callback */
  2250. husart->TxRxCpltCallback(husart);
  2251. #else
  2252. /* Call legacy weak Tx Rx Complete Callback */
  2253. HAL_USART_TxRxCpltCallback(husart);
  2254. #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
  2255. }
  2256. husart->State = HAL_USART_STATE_READY;
  2257. }
  2258. /* DMA circular mode */
  2259. else
  2260. {
  2261. if (husart->State == HAL_USART_STATE_BUSY_RX)
  2262. {
  2263. #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
  2264. /* Call registered Rx Complete Callback */
  2265. husart->RxCpltCallback(husart);
  2266. #else
  2267. /* Call legacy weak Rx Complete Callback */
  2268. HAL_USART_RxCpltCallback(husart);
  2269. #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
  2270. }
  2271. /* The USART state is HAL_USART_STATE_BUSY_TX_RX */
  2272. else
  2273. {
  2274. #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
  2275. /* Call registered Tx Rx Complete Callback */
  2276. husart->TxRxCpltCallback(husart);
  2277. #else
  2278. /* Call legacy weak Tx Rx Complete Callback */
  2279. HAL_USART_TxRxCpltCallback(husart);
  2280. #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
  2281. }
  2282. }
  2283. }
  2284. /**
  2285. * @brief DMA USART receive process half complete callback.
  2286. * @param hdma DMA handle.
  2287. * @retval None
  2288. */
  2289. static void USART_DMARxHalfCplt(DMA_HandleTypeDef *hdma)
  2290. {
  2291. USART_HandleTypeDef *husart = (USART_HandleTypeDef *)(hdma->Parent);
  2292. #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
  2293. /* Call registered Rx Half Complete Callback */
  2294. husart->RxHalfCpltCallback(husart);
  2295. #else
  2296. /* Call legacy weak Rx Half Complete Callback */
  2297. HAL_USART_RxHalfCpltCallback(husart);
  2298. #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
  2299. }
  2300. /**
  2301. * @brief DMA USART communication error callback.
  2302. * @param hdma DMA handle.
  2303. * @retval None
  2304. */
  2305. static void USART_DMAError(DMA_HandleTypeDef *hdma)
  2306. {
  2307. USART_HandleTypeDef *husart = (USART_HandleTypeDef *)(hdma->Parent);
  2308. husart->RxXferCount = 0U;
  2309. husart->TxXferCount = 0U;
  2310. USART_EndTransfer(husart);
  2311. husart->ErrorCode |= HAL_USART_ERROR_DMA;
  2312. husart->State = HAL_USART_STATE_READY;
  2313. #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
  2314. /* Call registered Error Callback */
  2315. husart->ErrorCallback(husart);
  2316. #else
  2317. /* Call legacy weak Error Callback */
  2318. HAL_USART_ErrorCallback(husart);
  2319. #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
  2320. }
  2321. /**
  2322. * @brief DMA USART communication abort callback, when initiated by HAL services on Error
  2323. * (To be called at end of DMA Abort procedure following error occurrence).
  2324. * @param hdma DMA handle.
  2325. * @retval None
  2326. */
  2327. static void USART_DMAAbortOnError(DMA_HandleTypeDef *hdma)
  2328. {
  2329. USART_HandleTypeDef *husart = (USART_HandleTypeDef *)(hdma->Parent);
  2330. husart->RxXferCount = 0U;
  2331. husart->TxXferCount = 0U;
  2332. #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
  2333. /* Call registered Error Callback */
  2334. husart->ErrorCallback(husart);
  2335. #else
  2336. /* Call legacy weak Error Callback */
  2337. HAL_USART_ErrorCallback(husart);
  2338. #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
  2339. }
  2340. /**
  2341. * @brief DMA USART Tx communication abort callback, when initiated by user
  2342. * (To be called at end of DMA Tx Abort procedure following user abort request).
  2343. * @note When this callback is executed, User Abort complete call back is called only if no
  2344. * Abort still ongoing for Rx DMA Handle.
  2345. * @param hdma DMA handle.
  2346. * @retval None
  2347. */
  2348. static void USART_DMATxAbortCallback(DMA_HandleTypeDef *hdma)
  2349. {
  2350. USART_HandleTypeDef *husart = (USART_HandleTypeDef *)(hdma->Parent);
  2351. husart->hdmatx->XferAbortCallback = NULL;
  2352. /* Check if an Abort process is still ongoing */
  2353. if (husart->hdmarx != NULL)
  2354. {
  2355. if (husart->hdmarx->XferAbortCallback != NULL)
  2356. {
  2357. return;
  2358. }
  2359. }
  2360. /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */
  2361. husart->TxXferCount = 0U;
  2362. husart->RxXferCount = 0U;
  2363. /* Reset errorCode */
  2364. husart->ErrorCode = HAL_USART_ERROR_NONE;
  2365. /* Clear the Error flags in the ICR register */
  2366. __HAL_USART_CLEAR_FLAG(husart, USART_CLEAR_OREF | USART_CLEAR_NEF | USART_CLEAR_PEF | USART_CLEAR_FEF);
  2367. /* Restore husart->State to Ready */
  2368. husart->State = HAL_USART_STATE_READY;
  2369. /* Call user Abort complete callback */
  2370. #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
  2371. /* Call registered Abort Complete Callback */
  2372. husart->AbortCpltCallback(husart);
  2373. #else
  2374. /* Call legacy weak Abort Complete Callback */
  2375. HAL_USART_AbortCpltCallback(husart);
  2376. #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
  2377. }
  2378. /**
  2379. * @brief DMA USART Rx communication abort callback, when initiated by user
  2380. * (To be called at end of DMA Rx Abort procedure following user abort request).
  2381. * @note When this callback is executed, User Abort complete call back is called only if no
  2382. * Abort still ongoing for Tx DMA Handle.
  2383. * @param hdma DMA handle.
  2384. * @retval None
  2385. */
  2386. static void USART_DMARxAbortCallback(DMA_HandleTypeDef *hdma)
  2387. {
  2388. USART_HandleTypeDef *husart = (USART_HandleTypeDef *)(hdma->Parent);
  2389. husart->hdmarx->XferAbortCallback = NULL;
  2390. /* Check if an Abort process is still ongoing */
  2391. if (husart->hdmatx != NULL)
  2392. {
  2393. if (husart->hdmatx->XferAbortCallback != NULL)
  2394. {
  2395. return;
  2396. }
  2397. }
  2398. /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */
  2399. husart->TxXferCount = 0U;
  2400. husart->RxXferCount = 0U;
  2401. /* Reset errorCode */
  2402. husart->ErrorCode = HAL_USART_ERROR_NONE;
  2403. /* Clear the Error flags in the ICR register */
  2404. __HAL_USART_CLEAR_FLAG(husart, USART_CLEAR_OREF | USART_CLEAR_NEF | USART_CLEAR_PEF | USART_CLEAR_FEF);
  2405. /* Restore husart->State to Ready */
  2406. husart->State = HAL_USART_STATE_READY;
  2407. /* Call user Abort complete callback */
  2408. #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
  2409. /* Call registered Abort Complete Callback */
  2410. husart->AbortCpltCallback(husart);
  2411. #else
  2412. /* Call legacy weak Abort Complete Callback */
  2413. HAL_USART_AbortCpltCallback(husart);
  2414. #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
  2415. }
  2416. /**
  2417. * @brief Handle USART Communication Timeout. It waits
  2418. * until a flag is no longer in the specified status.
  2419. * @param husart USART handle.
  2420. * @param Flag Specifies the USART flag to check.
  2421. * @param Status the actual Flag status (SET or RESET).
  2422. * @param Tickstart Tick start value
  2423. * @param Timeout timeout duration.
  2424. * @retval HAL status
  2425. */
  2426. static HAL_StatusTypeDef USART_WaitOnFlagUntilTimeout(USART_HandleTypeDef *husart, uint32_t Flag, FlagStatus Status,
  2427. uint32_t Tickstart, uint32_t Timeout)
  2428. {
  2429. /* Wait until flag is set */
  2430. while ((__HAL_USART_GET_FLAG(husart, Flag) ? SET : RESET) == Status)
  2431. {
  2432. /* Check for the Timeout */
  2433. if (Timeout != HAL_MAX_DELAY)
  2434. {
  2435. if (((HAL_GetTick() - Tickstart) > Timeout) || (Timeout == 0U))
  2436. {
  2437. husart->State = HAL_USART_STATE_READY;
  2438. /* Process Unlocked */
  2439. __HAL_UNLOCK(husart);
  2440. return HAL_TIMEOUT;
  2441. }
  2442. }
  2443. }
  2444. return HAL_OK;
  2445. }
  2446. /**
  2447. * @brief Configure the USART peripheral.
  2448. * @param husart USART handle.
  2449. * @retval HAL status
  2450. */
  2451. static HAL_StatusTypeDef USART_SetConfig(USART_HandleTypeDef *husart)
  2452. {
  2453. uint32_t tmpreg;
  2454. USART_ClockSourceTypeDef clocksource;
  2455. HAL_StatusTypeDef ret = HAL_OK;
  2456. uint16_t brrtemp;
  2457. uint32_t usartdiv = 0x00000000;
  2458. uint32_t pclk;
  2459. /* Check the parameters */
  2460. assert_param(IS_USART_POLARITY(husart->Init.CLKPolarity));
  2461. assert_param(IS_USART_PHASE(husart->Init.CLKPhase));
  2462. assert_param(IS_USART_LASTBIT(husart->Init.CLKLastBit));
  2463. assert_param(IS_USART_BAUDRATE(husart->Init.BaudRate));
  2464. assert_param(IS_USART_WORD_LENGTH(husart->Init.WordLength));
  2465. assert_param(IS_USART_STOPBITS(husart->Init.StopBits));
  2466. assert_param(IS_USART_PARITY(husart->Init.Parity));
  2467. assert_param(IS_USART_MODE(husart->Init.Mode));
  2468. /*-------------------------- USART CR1 Configuration -----------------------*/
  2469. /* Clear M, PCE, PS, TE and RE bits and configure
  2470. * the USART Word Length, Parity and Mode:
  2471. * set the M bits according to husart->Init.WordLength value
  2472. * set PCE and PS bits according to husart->Init.Parity value
  2473. * set TE and RE bits according to husart->Init.Mode value
  2474. * force OVER8 to 1 to allow to reach the maximum speed (Fclock/8) */
  2475. tmpreg = (uint32_t)husart->Init.WordLength | husart->Init.Parity | husart->Init.Mode | USART_CR1_OVER8;
  2476. MODIFY_REG(husart->Instance->CR1, USART_CR1_FIELDS, tmpreg);
  2477. /*---------------------------- USART CR2 Configuration ---------------------*/
  2478. /* Clear and configure the USART Clock, CPOL, CPHA, LBCL and STOP bits:
  2479. * set CPOL bit according to husart->Init.CLKPolarity value
  2480. * set CPHA bit according to husart->Init.CLKPhase value
  2481. * set LBCL bit according to husart->Init.CLKLastBit value (used in SPI master mode only)
  2482. * set STOP[13:12] bits according to husart->Init.StopBits value */
  2483. tmpreg = (uint32_t)(USART_CLOCK_ENABLE);
  2484. tmpreg |= (uint32_t)husart->Init.CLKLastBit;
  2485. tmpreg |= ((uint32_t)husart->Init.CLKPolarity | (uint32_t)husart->Init.CLKPhase);
  2486. tmpreg |= (uint32_t)husart->Init.StopBits;
  2487. MODIFY_REG(husart->Instance->CR2, USART_CR2_FIELDS, tmpreg);
  2488. /*-------------------------- USART BRR Configuration -----------------------*/
  2489. /* BRR is filled-up according to OVER8 bit setting which is forced to 1 */
  2490. USART_GETCLOCKSOURCE(husart, clocksource);
  2491. switch (clocksource)
  2492. {
  2493. case USART_CLOCKSOURCE_PCLK1:
  2494. pclk = HAL_RCC_GetPCLK1Freq();
  2495. usartdiv = (uint32_t)(USART_DIV_SAMPLING8(pclk, husart->Init.BaudRate));
  2496. break;
  2497. case USART_CLOCKSOURCE_HSI:
  2498. usartdiv = (uint32_t)(USART_DIV_SAMPLING8(HSI_VALUE, husart->Init.BaudRate));
  2499. break;
  2500. case USART_CLOCKSOURCE_SYSCLK:
  2501. pclk = HAL_RCC_GetSysClockFreq();
  2502. usartdiv = (uint32_t)(USART_DIV_SAMPLING8(pclk, husart->Init.BaudRate));
  2503. break;
  2504. case USART_CLOCKSOURCE_LSE:
  2505. usartdiv = (uint32_t)(USART_DIV_SAMPLING8(LSE_VALUE, husart->Init.BaudRate));
  2506. break;
  2507. default:
  2508. ret = HAL_ERROR;
  2509. break;
  2510. }
  2511. /* USARTDIV must be greater than or equal to 0d16 and smaller than or equal to ffff */
  2512. if ((usartdiv >= USART_BRR_MIN) && (usartdiv <= USART_BRR_MAX))
  2513. {
  2514. brrtemp = (uint16_t)(usartdiv & 0xFFF0U);
  2515. brrtemp |= (uint16_t)((usartdiv & (uint16_t)0x000FU) >> 1U);
  2516. husart->Instance->BRR = brrtemp;
  2517. }
  2518. else
  2519. {
  2520. ret = HAL_ERROR;
  2521. }
  2522. /* Clear ISR function pointers */
  2523. husart->RxISR = NULL;
  2524. husart->TxISR = NULL;
  2525. return ret;
  2526. }
  2527. /**
  2528. * @brief Check the USART Idle State.
  2529. * @param husart USART handle.
  2530. * @retval HAL status
  2531. */
  2532. static HAL_StatusTypeDef USART_CheckIdleState(USART_HandleTypeDef *husart)
  2533. {
  2534. uint32_t tickstart;
  2535. /* Initialize the USART ErrorCode */
  2536. husart->ErrorCode = HAL_USART_ERROR_NONE;
  2537. /* Init tickstart for timeout management */
  2538. tickstart = HAL_GetTick();
  2539. /* Check if the Transmitter is enabled */
  2540. if ((husart->Instance->CR1 & USART_CR1_TE) == USART_CR1_TE)
  2541. {
  2542. /* Wait until TEACK flag is set */
  2543. if (USART_WaitOnFlagUntilTimeout(husart, USART_ISR_TEACK, RESET, tickstart, USART_TEACK_REACK_TIMEOUT) != HAL_OK)
  2544. {
  2545. /* Timeout occurred */
  2546. return HAL_TIMEOUT;
  2547. }
  2548. }
  2549. /* Check if the Receiver is enabled */
  2550. if ((husart->Instance->CR1 & USART_CR1_RE) == USART_CR1_RE)
  2551. {
  2552. /* Wait until REACK flag is set */
  2553. if (USART_WaitOnFlagUntilTimeout(husart, USART_ISR_REACK, RESET, tickstart, USART_TEACK_REACK_TIMEOUT) != HAL_OK)
  2554. {
  2555. /* Timeout occurred */
  2556. return HAL_TIMEOUT;
  2557. }
  2558. }
  2559. /* Initialize the USART state*/
  2560. husart->State = HAL_USART_STATE_READY;
  2561. /* Process Unlocked */
  2562. __HAL_UNLOCK(husart);
  2563. return HAL_OK;
  2564. }
  2565. /**
  2566. * @brief Simplex send an amount of data in non-blocking mode.
  2567. * @note Function called under interruption only, once
  2568. * interruptions have been enabled by HAL_USART_Transmit_IT().
  2569. * @note The USART errors are not managed to avoid the overrun error.
  2570. * @note ISR function executed when data word length is less than 9 bits long.
  2571. * @param husart USART handle.
  2572. * @retval None
  2573. */
  2574. static void USART_TxISR_8BIT(USART_HandleTypeDef *husart)
  2575. {
  2576. const HAL_USART_StateTypeDef state = husart->State;
  2577. /* Check that a Tx process is ongoing */
  2578. if ((state == HAL_USART_STATE_BUSY_TX) ||
  2579. (state == HAL_USART_STATE_BUSY_TX_RX))
  2580. {
  2581. if (husart->TxXferCount == 0U)
  2582. {
  2583. /* Disable the USART Transmit data register empty interrupt */
  2584. __HAL_USART_DISABLE_IT(husart, USART_IT_TXE);
  2585. /* Enable the USART Transmit Complete Interrupt */
  2586. __HAL_USART_ENABLE_IT(husart, USART_IT_TC);
  2587. }
  2588. else
  2589. {
  2590. husart->Instance->TDR = (uint8_t)(*husart->pTxBuffPtr & (uint8_t)0xFF);
  2591. husart->pTxBuffPtr++;
  2592. husart->TxXferCount--;
  2593. }
  2594. }
  2595. }
  2596. /**
  2597. * @brief Simplex send an amount of data in non-blocking mode.
  2598. * @note Function called under interruption only, once
  2599. * interruptions have been enabled by HAL_USART_Transmit_IT().
  2600. * @note The USART errors are not managed to avoid the overrun error.
  2601. * @note ISR function executed when data word length is 9 bits long.
  2602. * @param husart USART handle.
  2603. * @retval None
  2604. */
  2605. static void USART_TxISR_16BIT(USART_HandleTypeDef *husart)
  2606. {
  2607. const HAL_USART_StateTypeDef state = husart->State;
  2608. const uint16_t *tmp;
  2609. if ((state == HAL_USART_STATE_BUSY_TX) ||
  2610. (state == HAL_USART_STATE_BUSY_TX_RX))
  2611. {
  2612. if (husart->TxXferCount == 0U)
  2613. {
  2614. /* Disable the USART Transmit data register empty interrupt */
  2615. __HAL_USART_DISABLE_IT(husart, USART_IT_TXE);
  2616. /* Enable the USART Transmit Complete Interrupt */
  2617. __HAL_USART_ENABLE_IT(husart, USART_IT_TC);
  2618. }
  2619. else
  2620. {
  2621. tmp = (const uint16_t *) husart->pTxBuffPtr;
  2622. husart->Instance->TDR = (uint16_t)(*tmp & 0x01FFU);
  2623. husart->pTxBuffPtr += 2U;
  2624. husart->TxXferCount--;
  2625. }
  2626. }
  2627. }
  2628. /**
  2629. * @brief Wraps up transmission in non-blocking mode.
  2630. * @param husart Pointer to a USART_HandleTypeDef structure that contains
  2631. * the configuration information for the specified USART module.
  2632. * @retval None
  2633. */
  2634. static void USART_EndTransmit_IT(USART_HandleTypeDef *husart)
  2635. {
  2636. /* Disable the USART Transmit Complete Interrupt */
  2637. __HAL_USART_DISABLE_IT(husart, USART_IT_TC);
  2638. /* Disable the USART Error Interrupt: (Frame error, noise error, overrun error) */
  2639. __HAL_USART_DISABLE_IT(husart, USART_IT_ERR);
  2640. /* Clear TxISR function pointer */
  2641. husart->TxISR = NULL;
  2642. if (husart->State == HAL_USART_STATE_BUSY_TX)
  2643. {
  2644. /* Clear overrun flag and discard the received data */
  2645. __HAL_USART_CLEAR_OREFLAG(husart);
  2646. __HAL_USART_SEND_REQ(husart, USART_RXDATA_FLUSH_REQUEST);
  2647. /* Tx process is completed, restore husart->State to Ready */
  2648. husart->State = HAL_USART_STATE_READY;
  2649. #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
  2650. /* Call registered Tx Complete Callback */
  2651. husart->TxCpltCallback(husart);
  2652. #else
  2653. /* Call legacy weak Tx Complete Callback */
  2654. HAL_USART_TxCpltCallback(husart);
  2655. #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
  2656. }
  2657. else if (husart->RxXferCount == 0U)
  2658. {
  2659. /* TxRx process is completed, restore husart->State to Ready */
  2660. husart->State = HAL_USART_STATE_READY;
  2661. #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
  2662. /* Call registered Tx Rx Complete Callback */
  2663. husart->TxRxCpltCallback(husart);
  2664. #else
  2665. /* Call legacy weak Tx Rx Complete Callback */
  2666. HAL_USART_TxRxCpltCallback(husart);
  2667. #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
  2668. }
  2669. else
  2670. {
  2671. /* Nothing to do */
  2672. }
  2673. }
  2674. /**
  2675. * @brief Simplex receive an amount of data in non-blocking mode.
  2676. * @note Function called under interruption only, once
  2677. * interruptions have been enabled by HAL_USART_Receive_IT().
  2678. * @note ISR function executed when data word length is less than 9 bits long.
  2679. * @param husart USART handle
  2680. * @retval None
  2681. */
  2682. static void USART_RxISR_8BIT(USART_HandleTypeDef *husart)
  2683. {
  2684. const HAL_USART_StateTypeDef state = husart->State;
  2685. uint16_t txdatacount;
  2686. uint16_t uhMask = husart->Mask;
  2687. if ((state == HAL_USART_STATE_BUSY_RX) ||
  2688. (state == HAL_USART_STATE_BUSY_TX_RX))
  2689. {
  2690. *husart->pRxBuffPtr = (uint8_t)(husart->Instance->RDR & (uint8_t)uhMask);
  2691. husart->pRxBuffPtr++;
  2692. husart->RxXferCount--;
  2693. if (husart->RxXferCount == 0U)
  2694. {
  2695. /* Disable the USART Parity Error Interrupt and RXNE interrupt*/
  2696. CLEAR_BIT(husart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE));
  2697. /* Disable the USART Error Interrupt: (Frame error, noise error, overrun error) */
  2698. CLEAR_BIT(husart->Instance->CR3, USART_CR3_EIE);
  2699. /* Clear RxISR function pointer */
  2700. husart->RxISR = NULL;
  2701. /* txdatacount is a temporary variable for MISRAC2012-Rule-13.5 */
  2702. txdatacount = husart->TxXferCount;
  2703. if (state == HAL_USART_STATE_BUSY_RX)
  2704. {
  2705. /* Rx process is completed, restore husart->State to Ready */
  2706. husart->State = HAL_USART_STATE_READY;
  2707. #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
  2708. /* Call registered Rx Complete Callback */
  2709. husart->RxCpltCallback(husart);
  2710. #else
  2711. /* Call legacy weak Rx Complete Callback */
  2712. HAL_USART_RxCpltCallback(husart);
  2713. #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
  2714. }
  2715. else if ((READ_BIT(husart->Instance->CR1, USART_CR1_TCIE) != USART_CR1_TCIE) &&
  2716. (txdatacount == 0U))
  2717. {
  2718. /* TxRx process is completed, restore husart->State to Ready */
  2719. husart->State = HAL_USART_STATE_READY;
  2720. #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
  2721. /* Call registered Tx Rx Complete Callback */
  2722. husart->TxRxCpltCallback(husart);
  2723. #else
  2724. /* Call legacy weak Tx Rx Complete Callback */
  2725. HAL_USART_TxRxCpltCallback(husart);
  2726. #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
  2727. }
  2728. else
  2729. {
  2730. /* Nothing to do */
  2731. }
  2732. }
  2733. else if (state == HAL_USART_STATE_BUSY_RX)
  2734. {
  2735. /* Send dummy byte in order to generate the clock for the Slave to Send the next data */
  2736. husart->Instance->TDR = (USART_DUMMY_DATA & (uint16_t)0x00FF);
  2737. }
  2738. else
  2739. {
  2740. /* Nothing to do */
  2741. }
  2742. }
  2743. }
  2744. /**
  2745. * @brief Simplex receive an amount of data in non-blocking mode.
  2746. * @note Function called under interruption only, once
  2747. * interruptions have been enabled by HAL_USART_Receive_IT().
  2748. * @note ISR function executed when data word length is 9 bits long.
  2749. * @param husart USART handle
  2750. * @retval None
  2751. */
  2752. static void USART_RxISR_16BIT(USART_HandleTypeDef *husart)
  2753. {
  2754. const HAL_USART_StateTypeDef state = husart->State;
  2755. uint16_t txdatacount;
  2756. uint16_t *tmp;
  2757. uint16_t uhMask = husart->Mask;
  2758. if ((state == HAL_USART_STATE_BUSY_RX) ||
  2759. (state == HAL_USART_STATE_BUSY_TX_RX))
  2760. {
  2761. tmp = (uint16_t *) husart->pRxBuffPtr;
  2762. *tmp = (uint16_t)(husart->Instance->RDR & uhMask);
  2763. husart->pRxBuffPtr += 2U;
  2764. husart->RxXferCount--;
  2765. if (husart->RxXferCount == 0U)
  2766. {
  2767. /* Disable the USART Parity Error Interrupt and RXNE interrupt*/
  2768. CLEAR_BIT(husart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE));
  2769. /* Disable the USART Error Interrupt: (Frame error, noise error, overrun error) */
  2770. CLEAR_BIT(husart->Instance->CR3, USART_CR3_EIE);
  2771. /* Clear RxISR function pointer */
  2772. husart->RxISR = NULL;
  2773. /* txdatacount is a temporary variable for MISRAC2012-Rule-13.5 */
  2774. txdatacount = husart->TxXferCount;
  2775. if (state == HAL_USART_STATE_BUSY_RX)
  2776. {
  2777. /* Rx process is completed, restore husart->State to Ready */
  2778. husart->State = HAL_USART_STATE_READY;
  2779. #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
  2780. /* Call registered Rx Complete Callback */
  2781. husart->RxCpltCallback(husart);
  2782. #else
  2783. /* Call legacy weak Rx Complete Callback */
  2784. HAL_USART_RxCpltCallback(husart);
  2785. #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
  2786. }
  2787. else if ((READ_BIT(husart->Instance->CR1, USART_CR1_TCIE) != USART_CR1_TCIE) &&
  2788. (txdatacount == 0U))
  2789. {
  2790. /* TxRx process is completed, restore husart->State to Ready */
  2791. husart->State = HAL_USART_STATE_READY;
  2792. #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
  2793. /* Call registered Tx Rx Complete Callback */
  2794. husart->TxRxCpltCallback(husart);
  2795. #else
  2796. /* Call legacy weak Tx Rx Complete Callback */
  2797. HAL_USART_TxRxCpltCallback(husart);
  2798. #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
  2799. }
  2800. else
  2801. {
  2802. /* Nothing to do */
  2803. }
  2804. }
  2805. else if (state == HAL_USART_STATE_BUSY_RX)
  2806. {
  2807. /* Send dummy byte in order to generate the clock for the Slave to Send the next data */
  2808. husart->Instance->TDR = (USART_DUMMY_DATA & (uint16_t)0x00FF);
  2809. }
  2810. else
  2811. {
  2812. /* Nothing to do */
  2813. }
  2814. }
  2815. }
  2816. /**
  2817. * @}
  2818. */
  2819. #endif /* HAL_USART_MODULE_ENABLED */
  2820. /**
  2821. * @}
  2822. */
  2823. /**
  2824. * @}
  2825. */