stm32f1xx_hal_usart.c 98 KB

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