stm32f1xx_hal_usart.c 97 KB

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