You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

stm32l1xx_hal_usart.c 94KB

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