py32f0xx_hal_usart.c 100 KB

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