stm32f1xx_hal_usart.c 98 KB

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