stm32f4xx_hal_usart.c 98 KB

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