py32f002b_hal_usart.c 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940
  1. /**
  2. ******************************************************************************
  3. * @file py32f002b_hal_usart.c
  4. * @author MCU Application Team
  5. * @brief USART HAL module driver.
  6. * This file provides firmware functions to manage the following
  7. * functionalities of the Universal Synchronous/Asynchronous Receiver Transmitter
  8. * Peripheral (USART).
  9. * + Initialization and de-initialization functions
  10. * + IO operation functions
  11. * + Peripheral Control functions
  12. @verbatim
  13. ==============================================================================
  14. ##### How to use this driver #####
  15. ==============================================================================
  16. [..]
  17. The USART HAL driver can be used as follows:
  18. (#) Declare a USART_HandleTypeDef handle structure (eg. USART_HandleTypeDef husart).
  19. (#) Initialize the USART low level resources by implementing the HAL_USART_MspInit() API:
  20. (##) Enable the USARTx interface clock.
  21. (##) USART pins configuration:
  22. (+++) Enable the clock for the USART GPIOs.
  23. (+++) Configure the USART pins as alternate function pull-up.
  24. (##) NVIC configuration if you need to use interrupt process (HAL_USART_Transmit_IT(),
  25. HAL_USART_Receive_IT() and HAL_USART_TransmitReceive_IT() APIs):
  26. (+++) Configure the USARTx interrupt priority.
  27. (+++) Enable the NVIC USART IRQ handle.
  28. (#) Program the Baud Rate, Word Length, Stop Bit, Parity, Hardware
  29. flow control and Mode(Receiver/Transmitter) in the husart Init structure.
  30. (#) Initialize the USART registers by calling the HAL_USART_Init() API:
  31. (++) These APIs configures also the low level Hardware GPIO, CLOCK, CORTEX...etc)
  32. by calling the customized HAL_USART_MspInit(&husart) API.
  33. -@@- The specific USART interrupts (Transmission complete interrupt,
  34. RXNE interrupt and Error Interrupts) will be managed using the macros
  35. __HAL_USART_ENABLE_IT() and __HAL_USART_DISABLE_IT() inside the transmit and receive process.
  36. (#) Three operation modes are available within this driver :
  37. *** Polling mode IO operation ***
  38. =================================
  39. [..]
  40. (+) Send an amount of data in blocking mode using HAL_USART_Transmit()
  41. (+) Receive an amount of data in blocking mode using HAL_USART_Receive()
  42. *** Interrupt mode IO operation ***
  43. ===================================
  44. [..]
  45. (+) Send an amount of data in non blocking mode using HAL_USART_Transmit_IT()
  46. (+) At transmission end of transfer HAL_USART_TxHalfCpltCallback is executed and user can
  47. add his own code by customization of function pointer HAL_USART_TxCpltCallback
  48. (+) Receive an amount of data in non blocking mode using HAL_USART_Receive_IT()
  49. (+) At reception end of transfer HAL_USART_RxCpltCallback is executed and user can
  50. add his own code by customization of function pointer HAL_USART_RxCpltCallback
  51. (+) In case of transfer Error, HAL_USART_ErrorCallback() function is executed and user can
  52. add his own code by customization of function pointer HAL_USART_ErrorCallback
  53. *** USART HAL driver macros list ***
  54. =============================================
  55. [..]
  56. Below the list of most used macros in USART HAL driver.
  57. (+) __HAL_USART_ENABLE: Enable the USART peripheral
  58. (+) __HAL_USART_DISABLE: Disable the USART peripheral
  59. (+) __HAL_USART_GET_FLAG : Check whether the specified USART flag is set or not
  60. (+) __HAL_USART_CLEAR_FLAG : Clear the specified USART pending flag
  61. (+) __HAL_USART_ENABLE_IT: Enable the specified USART interrupt
  62. (+) __HAL_USART_DISABLE_IT: Disable the specified USART interrupt
  63. [..]
  64. (@) You can refer to the USART HAL driver header file for more useful macros
  65. ##### Callback registration #####
  66. ==================================
  67. [..]
  68. The compilation define USE_HAL_USART_REGISTER_CALLBACKS when set to 1
  69. allows the user to configure dynamically the driver callbacks.
  70. [..]
  71. Use Function @ref HAL_USART_RegisterCallback() to register a user callback.
  72. Function @ref HAL_USART_RegisterCallback() allows to register following callbacks:
  73. (+) TxHalfCpltCallback : Tx Half Complete Callback.
  74. (+) TxCpltCallback : Tx Complete Callback.
  75. (+) RxHalfCpltCallback : Rx Half Complete Callback.
  76. (+) RxCpltCallback : Rx Complete Callback.
  77. (+) TxRxCpltCallback : Tx Rx Complete Callback.
  78. (+) ErrorCallback : Error Callback.
  79. (+) AbortCpltCallback : Abort Complete Callback.
  80. (+) MspInitCallback : USART MspInit.
  81. (+) MspDeInitCallback : USART MspDeInit.
  82. This function takes as parameters the HAL peripheral handle, the Callback ID
  83. and a pointer to the user callback function.
  84. [..]
  85. Use function @ref HAL_USART_UnRegisterCallback() to reset a callback to the default
  86. weak (surcharged) function.
  87. @ref HAL_USART_UnRegisterCallback() takes as parameters the HAL peripheral handle,
  88. and the Callback ID.
  89. This function allows to reset following callbacks:
  90. (+) TxHalfCpltCallback : Tx Half Complete Callback.
  91. (+) TxCpltCallback : Tx Complete Callback.
  92. (+) RxHalfCpltCallback : Rx Half Complete Callback.
  93. (+) RxCpltCallback : Rx Complete Callback.
  94. (+) TxRxCpltCallback : Tx Rx Complete Callback.
  95. (+) ErrorCallback : Error Callback.
  96. (+) AbortCpltCallback : Abort Complete Callback.
  97. (+) MspInitCallback : USART MspInit.
  98. (+) MspDeInitCallback : USART MspDeInit.
  99. [..]
  100. By default, after the @ref HAL_USART_Init() and when the state is HAL_USART_STATE_RESET
  101. all callbacks are set to the corresponding weak (surcharged) functions:
  102. examples @ref HAL_USART_TxCpltCallback(), @ref HAL_USART_RxHalfCpltCallback().
  103. Exception done for MspInit and MspDeInit functions that are respectively
  104. reset to the legacy weak (surcharged) functions in the @ref HAL_USART_Init()
  105. and @ref HAL_USART_DeInit() only when these callbacks are null (not registered beforehand).
  106. If not, MspInit or MspDeInit are not null, the @ref HAL_USART_Init() and @ref HAL_USART_DeInit()
  107. keep and use the user MspInit/MspDeInit callbacks (registered beforehand).
  108. [..]
  109. Callbacks can be registered/unregistered in HAL_USART_STATE_READY state only.
  110. Exception done MspInit/MspDeInit that can be registered/unregistered
  111. in HAL_USART_STATE_READY or HAL_USART_STATE_RESET state, thus registered (user)
  112. MspInit/DeInit callbacks can be used during the Init/DeInit.
  113. In that case first register the MspInit/MspDeInit user callbacks
  114. using @ref HAL_USART_RegisterCallback() before calling @ref HAL_USART_DeInit()
  115. or @ref HAL_USART_Init() function.
  116. [..]
  117. When The compilation define USE_HAL_USART_REGISTER_CALLBACKS is set to 0 or
  118. not defined, the callback registration feature is not available
  119. and weak (surcharged) callbacks are used.
  120. @endverbatim
  121. [..]
  122. (@) Additionnal remark: If the parity is enabled, then the MSB bit of the data written
  123. in the data register is transmitted but is changed by the parity bit.
  124. Depending on the frame length defined by the M bit (8-bits or 9-bits),
  125. the possible USART frame formats are as listed in the following table:
  126. +-------------------------------------------------------------+
  127. | M bit | PCE bit | USART frame |
  128. |---------------------|---------------------------------------|
  129. | 0 | 0 | | SB | 8 bit data | STB | |
  130. |---------|-----------|---------------------------------------|
  131. | 0 | 1 | | SB | 7 bit data | PB | STB | |
  132. |---------|-----------|---------------------------------------|
  133. | 1 | 0 | | SB | 9 bit data | STB | |
  134. |---------|-----------|---------------------------------------|
  135. | 1 | 1 | | SB | 8 bit data | PB | STB | |
  136. +-------------------------------------------------------------+
  137. ******************************************************************************
  138. * @attention
  139. *
  140. * <h2><center>&copy; Copyright (c) 2023 Puya Semiconductor Co.
  141. * All rights reserved.</center></h2>
  142. *
  143. * This software component is licensed by Puya under BSD 3-Clause license,
  144. * the "License"; You may not use this file except in compliance with the
  145. * License. You may obtain a copy of the License at:
  146. * opensource.org/licenses/BSD-3-Clause
  147. *
  148. ******************************************************************************
  149. * @attention
  150. *
  151. * <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
  152. * All rights reserved.</center></h2>
  153. *
  154. * This software component is licensed by ST under BSD 3-Clause license,
  155. * the "License"; You may not use this file except in compliance with the
  156. * License. You may obtain a copy of the License at:
  157. * opensource.org/licenses/BSD-3-Clause
  158. *
  159. ******************************************************************************
  160. */
  161. /* Includes ------------------------------------------------------------------*/
  162. #include "py32f0xx_hal.h"
  163. /** @addtogroup PY32F002B_HAL_Driver
  164. * @{
  165. */
  166. /** @defgroup USART USART
  167. * @brief HAL USART Synchronous module driver
  168. * @{
  169. */
  170. #ifdef HAL_USART_MODULE_ENABLED
  171. /* Private typedef -----------------------------------------------------------*/
  172. /* Private define ------------------------------------------------------------*/
  173. /** @addtogroup USART_Private_Constants
  174. * @{
  175. */
  176. #define DUMMY_DATA 0xFFFFU
  177. /**
  178. * @}
  179. */
  180. /* Private macro -------------------------------------------------------------*/
  181. /* Private variables ---------------------------------------------------------*/
  182. /* Private function prototypes -----------------------------------------------*/
  183. /* Private functions ---------------------------------------------------------*/
  184. /** @addtogroup USART_Private_Functions
  185. * @{
  186. */
  187. #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
  188. void USART_InitCallbacksToDefault(USART_HandleTypeDef *husart);
  189. #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
  190. static void USART_EndRxTransfer(USART_HandleTypeDef *husart);
  191. static HAL_StatusTypeDef USART_Transmit_IT(USART_HandleTypeDef *husart);
  192. static HAL_StatusTypeDef USART_EndTransmit_IT(USART_HandleTypeDef *husart);
  193. static HAL_StatusTypeDef USART_Receive_IT(USART_HandleTypeDef *husart);
  194. static HAL_StatusTypeDef USART_TransmitReceive_IT(USART_HandleTypeDef *husart);
  195. static void USART_SetConfig(USART_HandleTypeDef *husart);
  196. static HAL_StatusTypeDef USART_WaitOnFlagUntilTimeout(USART_HandleTypeDef *husart, uint32_t Flag, FlagStatus Status, uint32_t Tickstart, uint32_t Timeout);
  197. /**
  198. * @}
  199. */
  200. /* Exported functions --------------------------------------------------------*/
  201. /** @defgroup USART_Exported_Functions USART Exported Functions
  202. * @{
  203. */
  204. /** @defgroup USART_Exported_Functions_Group1 USART Initialization and de-initialization functions
  205. * @brief Initialization and Configuration functions
  206. *
  207. @verbatim
  208. ==============================================================================
  209. ##### Initialization and Configuration functions #####
  210. ==============================================================================
  211. [..]
  212. This subsection provides a set of functions allowing to initialize the USART
  213. in asynchronous and in synchronous modes.
  214. (+) For the asynchronous mode only these parameters can be configured:
  215. (++) Baud Rate
  216. (++) Word Length
  217. (++) Stop Bit
  218. (++) Parity: If the parity is enabled, then the MSB bit of the data written
  219. in the data register is transmitted but is changed by the parity bit.
  220. Depending on the frame length defined by the M bit (8-bits or 9-bits),
  221. please refer to Reference manual for possible USART frame formats.
  222. (++) USART polarity
  223. (++) USART phase
  224. (++) USART LastBit
  225. (++) Receiver/transmitter modes
  226. [..]
  227. The HAL_USART_Init() function follows the USART synchronous configuration
  228. procedures (details for the procedures are available in reference manuals).
  229. @endverbatim
  230. * @{
  231. */
  232. /**
  233. * @brief Initialize the USART mode according to the specified
  234. * parameters in the USART_InitTypeDef and initialize the associated handle.
  235. * @param husart Pointer to a USART_HandleTypeDef structure that contains
  236. * the configuration information for the specified USART module.
  237. * @retval HAL status
  238. */
  239. HAL_StatusTypeDef HAL_USART_Init(USART_HandleTypeDef *husart)
  240. {
  241. /* Check the USART handle allocation */
  242. if (husart == NULL)
  243. {
  244. return HAL_ERROR;
  245. }
  246. /* Check the parameters */
  247. assert_param(IS_USART_INSTANCE(husart->Instance));
  248. if (husart->State == HAL_USART_STATE_RESET)
  249. {
  250. /* Allocate lock resource and initialize it */
  251. husart->Lock = HAL_UNLOCKED;
  252. #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
  253. USART_InitCallbacksToDefault(husart);
  254. if (husart->MspInitCallback == NULL)
  255. {
  256. husart->MspInitCallback = HAL_USART_MspInit;
  257. }
  258. /* Init the low level hardware */
  259. husart->MspInitCallback(husart);
  260. #else
  261. /* Init the low level hardware : GPIO, CLOCK */
  262. HAL_USART_MspInit(husart);
  263. #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
  264. }
  265. husart->State = HAL_USART_STATE_BUSY;
  266. /* Set the USART Communication parameters */
  267. USART_SetConfig(husart);
  268. /* Enable the Peripheral */
  269. __HAL_USART_ENABLE(husart);
  270. /* Initialize the USART state */
  271. husart->ErrorCode = HAL_USART_ERROR_NONE;
  272. husart->State = HAL_USART_STATE_READY;
  273. return HAL_OK;
  274. }
  275. /**
  276. * @brief DeInitializes the USART peripheral.
  277. * @param husart Pointer to a USART_HandleTypeDef structure that contains
  278. * the configuration information for the specified USART module.
  279. * @retval HAL status
  280. */
  281. HAL_StatusTypeDef HAL_USART_DeInit(USART_HandleTypeDef *husart)
  282. {
  283. /* Check the USART handle allocation */
  284. if (husart == NULL)
  285. {
  286. return HAL_ERROR;
  287. }
  288. /* Check the parameters */
  289. assert_param(IS_USART_INSTANCE(husart->Instance));
  290. husart->State = HAL_USART_STATE_BUSY;
  291. /* Disable the Peripheral */
  292. __HAL_USART_DISABLE(husart);
  293. #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
  294. if (husart->MspDeInitCallback == NULL)
  295. {
  296. husart->MspDeInitCallback = HAL_USART_MspDeInit;
  297. }
  298. /* DeInit the low level hardware */
  299. husart->MspDeInitCallback(husart);
  300. #else
  301. /* DeInit the low level hardware */
  302. HAL_USART_MspDeInit(husart);
  303. #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
  304. husart->ErrorCode = HAL_USART_ERROR_NONE;
  305. husart->State = HAL_USART_STATE_RESET;
  306. /* Release Lock */
  307. __HAL_UNLOCK(husart);
  308. return HAL_OK;
  309. }
  310. /**
  311. * @brief USART MSP Init.
  312. * @param husart Pointer to a USART_HandleTypeDef structure that contains
  313. * the configuration information for the specified USART module.
  314. * @retval None
  315. */
  316. __weak void HAL_USART_MspInit(USART_HandleTypeDef *husart)
  317. {
  318. /* Prevent unused argument(s) compilation warning */
  319. UNUSED(husart);
  320. /* NOTE: This function should not be modified, when the callback is needed,
  321. the HAL_USART_MspInit could be implemented in the user file
  322. */
  323. }
  324. /**
  325. * @brief USART MSP DeInit.
  326. * @param husart Pointer to a USART_HandleTypeDef structure that contains
  327. * the configuration information for the specified USART module.
  328. * @retval None
  329. */
  330. __weak void HAL_USART_MspDeInit(USART_HandleTypeDef *husart)
  331. {
  332. /* Prevent unused argument(s) compilation warning */
  333. UNUSED(husart);
  334. /* NOTE: This function should not be modified, when the callback is needed,
  335. the HAL_USART_MspDeInit could be implemented in the user file
  336. */
  337. }
  338. #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
  339. /**
  340. * @brief Register a User USART Callback
  341. * To be used instead of the weak predefined callback
  342. * @param husart usart handle
  343. * @param CallbackID ID of the callback to be registered
  344. * This parameter can be one of the following values:
  345. * @arg @ref HAL_USART_TX_HALFCOMPLETE_CB_ID Tx Half Complete Callback ID
  346. * @arg @ref HAL_USART_TX_COMPLETE_CB_ID Tx Complete Callback ID
  347. * @arg @ref HAL_USART_RX_HALFCOMPLETE_CB_ID Rx Half Complete Callback ID
  348. * @arg @ref HAL_USART_RX_COMPLETE_CB_ID Rx Complete Callback ID
  349. * @arg @ref HAL_USART_TX_RX_COMPLETE_CB_ID Rx Complete Callback ID
  350. * @arg @ref HAL_USART_ERROR_CB_ID Error Callback ID
  351. * @arg @ref HAL_USART_ABORT_COMPLETE_CB_ID Abort Complete Callback ID
  352. * @arg @ref HAL_USART_MSPINIT_CB_ID MspInit Callback ID
  353. * @arg @ref HAL_USART_MSPDEINIT_CB_ID MspDeInit Callback ID
  354. * @param pCallback pointer to the Callback function
  355. * @retval HAL status
  356. + */
  357. HAL_StatusTypeDef HAL_USART_RegisterCallback(USART_HandleTypeDef *husart, HAL_USART_CallbackIDTypeDef CallbackID, pUSART_CallbackTypeDef pCallback)
  358. {
  359. HAL_StatusTypeDef status = HAL_OK;
  360. if (pCallback == NULL)
  361. {
  362. /* Update the error code */
  363. husart->ErrorCode |= HAL_USART_ERROR_INVALID_CALLBACK;
  364. return HAL_ERROR;
  365. }
  366. /* Process locked */
  367. __HAL_LOCK(husart);
  368. if (husart->State == HAL_USART_STATE_READY)
  369. {
  370. switch (CallbackID)
  371. {
  372. case HAL_USART_TX_HALFCOMPLETE_CB_ID :
  373. husart->TxHalfCpltCallback = pCallback;
  374. break;
  375. case HAL_USART_TX_COMPLETE_CB_ID :
  376. husart->TxCpltCallback = pCallback;
  377. break;
  378. case HAL_USART_RX_HALFCOMPLETE_CB_ID :
  379. husart->RxHalfCpltCallback = pCallback;
  380. break;
  381. case HAL_USART_RX_COMPLETE_CB_ID :
  382. husart->RxCpltCallback = pCallback;
  383. break;
  384. case HAL_USART_TX_RX_COMPLETE_CB_ID :
  385. husart->TxRxCpltCallback = pCallback;
  386. break;
  387. case HAL_USART_ERROR_CB_ID :
  388. husart->ErrorCallback = pCallback;
  389. break;
  390. case HAL_USART_ABORT_COMPLETE_CB_ID :
  391. husart->AbortCpltCallback = pCallback;
  392. break;
  393. case HAL_USART_MSPINIT_CB_ID :
  394. husart->MspInitCallback = pCallback;
  395. break;
  396. case HAL_USART_MSPDEINIT_CB_ID :
  397. husart->MspDeInitCallback = pCallback;
  398. break;
  399. default :
  400. /* Update the error code */
  401. husart->ErrorCode |= HAL_USART_ERROR_INVALID_CALLBACK;
  402. /* Return error status */
  403. status = HAL_ERROR;
  404. break;
  405. }
  406. }
  407. else if (husart->State == HAL_USART_STATE_RESET)
  408. {
  409. switch (CallbackID)
  410. {
  411. case HAL_USART_MSPINIT_CB_ID :
  412. husart->MspInitCallback = pCallback;
  413. break;
  414. case HAL_USART_MSPDEINIT_CB_ID :
  415. husart->MspDeInitCallback = pCallback;
  416. break;
  417. default :
  418. /* Update the error code */
  419. husart->ErrorCode |= HAL_USART_ERROR_INVALID_CALLBACK;
  420. /* Return error status */
  421. status = HAL_ERROR;
  422. break;
  423. }
  424. }
  425. else
  426. {
  427. /* Update the error code */
  428. husart->ErrorCode |= HAL_USART_ERROR_INVALID_CALLBACK;
  429. /* Return error status */
  430. status = HAL_ERROR;
  431. }
  432. /* Release Lock */
  433. __HAL_UNLOCK(husart);
  434. return status;
  435. }
  436. /**
  437. * @brief Unregister an USART Callback
  438. * USART callaback is redirected to the weak predefined callback
  439. * @param husart uart handle
  440. * @param CallbackID ID of the callback to be unregistered
  441. * This parameter can be one of the following values:
  442. * @arg @ref HAL_USART_TX_HALFCOMPLETE_CB_ID Tx Half Complete Callback ID
  443. * @arg @ref HAL_USART_TX_COMPLETE_CB_ID Tx Complete Callback ID
  444. * @arg @ref HAL_USART_RX_HALFCOMPLETE_CB_ID Rx Half Complete Callback ID
  445. * @arg @ref HAL_USART_RX_COMPLETE_CB_ID Rx Complete Callback ID
  446. * @arg @ref HAL_USART_TX_RX_COMPLETE_CB_ID Rx Complete Callback ID
  447. * @arg @ref HAL_USART_ERROR_CB_ID Error Callback ID
  448. * @arg @ref HAL_USART_ABORT_COMPLETE_CB_ID Abort Complete Callback ID
  449. * @arg @ref HAL_USART_MSPINIT_CB_ID MspInit Callback ID
  450. * @arg @ref HAL_USART_MSPDEINIT_CB_ID MspDeInit Callback ID
  451. * @retval HAL status
  452. */
  453. HAL_StatusTypeDef HAL_USART_UnRegisterCallback(USART_HandleTypeDef *husart, HAL_USART_CallbackIDTypeDef CallbackID)
  454. {
  455. HAL_StatusTypeDef status = HAL_OK;
  456. /* Process locked */
  457. __HAL_LOCK(husart);
  458. if (husart->State == HAL_USART_STATE_READY)
  459. {
  460. switch (CallbackID)
  461. {
  462. case HAL_USART_TX_HALFCOMPLETE_CB_ID :
  463. husart->TxHalfCpltCallback = HAL_USART_TxHalfCpltCallback; /* Legacy weak TxHalfCpltCallback */
  464. break;
  465. case HAL_USART_TX_COMPLETE_CB_ID :
  466. husart->TxCpltCallback = HAL_USART_TxCpltCallback; /* Legacy weak TxCpltCallback */
  467. break;
  468. case HAL_USART_RX_HALFCOMPLETE_CB_ID :
  469. husart->RxHalfCpltCallback = HAL_USART_RxHalfCpltCallback; /* Legacy weak RxHalfCpltCallback */
  470. break;
  471. case HAL_USART_RX_COMPLETE_CB_ID :
  472. husart->RxCpltCallback = HAL_USART_RxCpltCallback; /* Legacy weak RxCpltCallback */
  473. break;
  474. case HAL_USART_TX_RX_COMPLETE_CB_ID :
  475. husart->TxRxCpltCallback = HAL_USART_TxRxCpltCallback; /* Legacy weak TxRxCpltCallback */
  476. break;
  477. case HAL_USART_ERROR_CB_ID :
  478. husart->ErrorCallback = HAL_USART_ErrorCallback; /* Legacy weak ErrorCallback */
  479. break;
  480. case HAL_USART_ABORT_COMPLETE_CB_ID :
  481. husart->AbortCpltCallback = HAL_USART_AbortCpltCallback; /* Legacy weak AbortCpltCallback */
  482. break;
  483. case HAL_USART_MSPINIT_CB_ID :
  484. husart->MspInitCallback = HAL_USART_MspInit; /* Legacy weak MspInitCallback */
  485. break;
  486. case HAL_USART_MSPDEINIT_CB_ID :
  487. husart->MspDeInitCallback = HAL_USART_MspDeInit; /* Legacy weak MspDeInitCallback */
  488. break;
  489. default :
  490. /* Update the error code */
  491. husart->ErrorCode |= HAL_USART_ERROR_INVALID_CALLBACK;
  492. /* Return error status */
  493. status = HAL_ERROR;
  494. break;
  495. }
  496. }
  497. else if (husart->State == HAL_USART_STATE_RESET)
  498. {
  499. switch (CallbackID)
  500. {
  501. case HAL_USART_MSPINIT_CB_ID :
  502. husart->MspInitCallback = HAL_USART_MspInit;
  503. break;
  504. case HAL_USART_MSPDEINIT_CB_ID :
  505. husart->MspDeInitCallback = HAL_USART_MspDeInit;
  506. break;
  507. default :
  508. /* Update the error code */
  509. husart->ErrorCode |= HAL_USART_ERROR_INVALID_CALLBACK;
  510. /* Return error status */
  511. status = HAL_ERROR;
  512. break;
  513. }
  514. }
  515. else
  516. {
  517. /* Update the error code */
  518. husart->ErrorCode |= HAL_USART_ERROR_INVALID_CALLBACK;
  519. /* Return error status */
  520. status = HAL_ERROR;
  521. }
  522. /* Release Lock */
  523. __HAL_UNLOCK(husart);
  524. return status;
  525. }
  526. #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
  527. /**
  528. * @}
  529. */
  530. /** @defgroup USART_Exported_Functions_Group2 IO operation functions
  531. * @brief USART Transmit and Receive functions
  532. *
  533. @verbatim
  534. ==============================================================================
  535. ##### IO operation functions #####
  536. ==============================================================================
  537. [..]
  538. This subsection provides a set of functions allowing to manage the USART synchronous
  539. data transfers.
  540. [..]
  541. The USART supports master mode only: it cannot receive or send data related to an input
  542. clock (SCLK is always an output).
  543. (#) There are two modes of transfer:
  544. (++) Blocking mode: The communication is performed in polling mode.
  545. The HAL status of all data processing is returned by the same function
  546. after finishing transfer.
  547. (++) No-Blocking mode: The communication is performed using Interrupts
  548. , These API's return the HAL status.
  549. The end of the data processing will be indicated through the
  550. dedicated USART IRQ when using Interrupt mode.
  551. The HAL_USART_TxCpltCallback(), HAL_USART_RxCpltCallback() and HAL_USART_TxRxCpltCallback()
  552. user callbacks
  553. will be executed respectively at the end of the transmit or Receive process
  554. The HAL_USART_ErrorCallback() user callback will be executed when a communication
  555. error is detected
  556. (#) Blocking mode APIs are :
  557. (++) HAL_USART_Transmit() in simplex mode
  558. (++) HAL_USART_Receive() in full duplex receive only
  559. (++) HAL_USART_TransmitReceive() in full duplex mode
  560. (#) Non Blocking mode APIs with Interrupt are :
  561. (++) HAL_USART_Transmit_IT()in simplex mode
  562. (++) HAL_USART_Receive_IT() in full duplex receive only
  563. (++) HAL_USART_TransmitReceive_IT() in full duplex mode
  564. (++) HAL_USART_IRQHandler()
  565. (#) A set of Transfer Complete Callbacks are provided in non Blocking mode:
  566. (++) HAL_USART_TxHalfCpltCallback()
  567. (++) HAL_USART_TxCpltCallback()
  568. (++) HAL_USART_RxHalfCpltCallback()
  569. (++) HAL_USART_RxCpltCallback()
  570. (++) HAL_USART_ErrorCallback()
  571. (++) HAL_USART_TxRxCpltCallback()
  572. (#) Non-Blocking mode transfers could be aborted using Abort API's :
  573. (++) HAL_USART_Abort()
  574. (++) HAL_USART_Abort_IT()
  575. (#) For Abort services based on interrupts (HAL_USART_Abort_IT), a Abort Complete Callbacks is provided:
  576. (++) HAL_USART_AbortCpltCallback()
  577. (#) In Non-Blocking mode transfers, possible errors are split into 2 categories.
  578. Errors are handled as follows :
  579. (++) Error is considered as Recoverable and non blocking : Transfer could go till end, but error severity is
  580. to be evaluated by user : this concerns Frame Error, Parity Error or Noise Error in Interrupt mode reception .
  581. Received character is then retrieved and stored in Rx buffer, Error code is set to allow user to identify error type,
  582. and HAL_USART_ErrorCallback() user callback is executed. Transfer is kept ongoing on USART side.
  583. If user wants to abort it, Abort services should be called by user.
  584. (++) Error is considered as Blocking : Transfer could not be completed properly and is aborted.
  585. This concerns Overrun Error In Interrupt mode reception.
  586. Error code is set to allow user to identify error type, and HAL_USART_ErrorCallback() user callback is executed.
  587. @endverbatim
  588. * @{
  589. */
  590. /**
  591. * @brief Simplex Send an amount of data in blocking mode.
  592. * @note When USART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M=1),
  593. * the sent data is handled as a set of u16. In this case, Size must indicate the number
  594. * of u16 provided through pTxData.
  595. * @param husart Pointer to a USART_HandleTypeDef structure that contains
  596. * the configuration information for the specified USART module.
  597. * @param pTxData Pointer to data buffer (u8 or u16 data elements).
  598. * @param Size Amount of data elements (u8 or u16) to be sent.
  599. * @param Timeout Timeout duration.
  600. * @retval HAL status
  601. */
  602. HAL_StatusTypeDef HAL_USART_Transmit(USART_HandleTypeDef *husart, uint8_t *pTxData, uint16_t Size, uint32_t Timeout)
  603. {
  604. uint16_t *tmp;
  605. uint32_t tickstart = 0U;
  606. if (husart->State == HAL_USART_STATE_READY)
  607. {
  608. if ((pTxData == NULL) || (Size == 0))
  609. {
  610. return HAL_ERROR;
  611. }
  612. /* Process Locked */
  613. __HAL_LOCK(husart);
  614. husart->ErrorCode = HAL_USART_ERROR_NONE;
  615. husart->State = HAL_USART_STATE_BUSY_TX;
  616. /* Init tickstart for timeout management */
  617. tickstart = HAL_GetTick();
  618. husart->TxXferSize = Size;
  619. husart->TxXferCount = Size;
  620. while (husart->TxXferCount > 0U)
  621. {
  622. husart->TxXferCount--;
  623. if (husart->Init.WordLength == USART_WORDLENGTH_9B)
  624. {
  625. /* Wait for TC flag in order to write data in DR */
  626. if (USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_TXE, RESET, tickstart, Timeout) != HAL_OK)
  627. {
  628. return HAL_TIMEOUT;
  629. }
  630. tmp = (uint16_t *) pTxData;
  631. husart->Instance->DR = (*tmp & (uint16_t)0x01FF);
  632. if (husart->Init.Parity == USART_PARITY_NONE)
  633. {
  634. pTxData += 2U;
  635. }
  636. else
  637. {
  638. pTxData += 1U;
  639. }
  640. }
  641. else
  642. {
  643. if (USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_TXE, RESET, tickstart, Timeout) != HAL_OK)
  644. {
  645. return HAL_TIMEOUT;
  646. }
  647. husart->Instance->DR = (*pTxData++ & (uint8_t)0xFF);
  648. }
  649. }
  650. if (USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_TC, RESET, tickstart, Timeout) != HAL_OK)
  651. {
  652. return HAL_TIMEOUT;
  653. }
  654. husart->State = HAL_USART_STATE_READY;
  655. /* Process Unlocked */
  656. __HAL_UNLOCK(husart);
  657. return HAL_OK;
  658. }
  659. else
  660. {
  661. return HAL_BUSY;
  662. }
  663. }
  664. /**
  665. * @brief Full-Duplex Receive an amount of data in blocking mode.
  666. * @note To receive synchronous data, dummy data are simultaneously transmitted.
  667. * @note When USART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M=1),
  668. * the received data is handled as a set of u16. In this case, Size must indicate the number
  669. * of u16 available through pRxData.
  670. * @param husart Pointer to a USART_HandleTypeDef structure that contains
  671. * the configuration information for the specified USART module.
  672. * @param pRxData Pointer to data buffer (u8 or u16 data elements).
  673. * @param Size Amount of data elements (u8 or u16) to be received.
  674. * @param Timeout Timeout duration.
  675. * @retval HAL status
  676. */
  677. HAL_StatusTypeDef HAL_USART_Receive(USART_HandleTypeDef *husart, uint8_t *pRxData, uint16_t Size, uint32_t Timeout)
  678. {
  679. uint16_t *tmp;
  680. uint32_t tickstart = 0U;
  681. if (husart->State == HAL_USART_STATE_READY)
  682. {
  683. if ((pRxData == NULL) || (Size == 0))
  684. {
  685. return HAL_ERROR;
  686. }
  687. /* Process Locked */
  688. __HAL_LOCK(husart);
  689. husart->ErrorCode = HAL_USART_ERROR_NONE;
  690. husart->State = HAL_USART_STATE_BUSY_RX;
  691. /* Init tickstart for timeout management */
  692. tickstart = HAL_GetTick();
  693. husart->RxXferSize = Size;
  694. husart->RxXferCount = Size;
  695. /* Check the remain data to be received */
  696. while (husart->RxXferCount > 0U)
  697. {
  698. husart->RxXferCount--;
  699. if (husart->Init.WordLength == USART_WORDLENGTH_9B)
  700. {
  701. /* Wait until TXE flag is set to send dummy byte in order to generate the clock for the slave to send data */
  702. if (USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_TXE, RESET, tickstart, Timeout) != HAL_OK)
  703. {
  704. return HAL_TIMEOUT;
  705. }
  706. /* Send dummy byte in order to generate clock */
  707. husart->Instance->DR = (DUMMY_DATA & (uint16_t)0x01FF);
  708. /* Wait for RXNE Flag */
  709. if (USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_RXNE, RESET, tickstart, Timeout) != HAL_OK)
  710. {
  711. return HAL_TIMEOUT;
  712. }
  713. tmp = (uint16_t *) pRxData ;
  714. if (husart->Init.Parity == USART_PARITY_NONE)
  715. {
  716. *tmp = (uint16_t)(husart->Instance->DR & (uint16_t)0x01FF);
  717. pRxData += 2U;
  718. }
  719. else
  720. {
  721. *tmp = (uint16_t)(husart->Instance->DR & (uint16_t)0x00FF);
  722. pRxData += 1U;
  723. }
  724. }
  725. else
  726. {
  727. /* Wait until TXE flag is set to send dummy byte in order to generate the clock for the slave to send data */
  728. if (USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_TXE, RESET, tickstart, Timeout) != HAL_OK)
  729. {
  730. return HAL_TIMEOUT;
  731. }
  732. /* Send Dummy Byte in order to generate clock */
  733. husart->Instance->DR = (DUMMY_DATA & (uint16_t)0x00FF);
  734. /* Wait until RXNE flag is set to receive the byte */
  735. if (USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_RXNE, RESET, tickstart, Timeout) != HAL_OK)
  736. {
  737. return HAL_TIMEOUT;
  738. }
  739. if (husart->Init.Parity == USART_PARITY_NONE)
  740. {
  741. /* Receive data */
  742. *pRxData++ = (uint8_t)(husart->Instance->DR & (uint8_t)0x00FF);
  743. }
  744. else
  745. {
  746. /* Receive data */
  747. *pRxData++ = (uint8_t)(husart->Instance->DR & (uint8_t)0x007F);
  748. }
  749. }
  750. }
  751. husart->State = HAL_USART_STATE_READY;
  752. /* Process Unlocked */
  753. __HAL_UNLOCK(husart);
  754. return HAL_OK;
  755. }
  756. else
  757. {
  758. return HAL_BUSY;
  759. }
  760. }
  761. /**
  762. * @brief Full-Duplex Send and Receive an amount of data in full-duplex mode (blocking mode).
  763. * @note When USART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M=1),
  764. * the sent data and the received data are handled as sets of u16. In this case, Size must indicate the number
  765. * of u16 available through pTxData and through pRxData.
  766. * @param husart Pointer to a USART_HandleTypeDef structure that contains
  767. * the configuration information for the specified USART module.
  768. * @param pTxData Pointer to TX data buffer (u8 or u16 data elements).
  769. * @param pRxData Pointer to RX data buffer (u8 or u16 data elements).
  770. * @param Size Amount of data elements (u8 or u16) to be sent (same amount to be received).
  771. * @param Timeout Timeout duration
  772. * @retval HAL status
  773. */
  774. HAL_StatusTypeDef HAL_USART_TransmitReceive(USART_HandleTypeDef *husart, uint8_t *pTxData, uint8_t *pRxData, uint16_t Size, uint32_t Timeout)
  775. {
  776. uint16_t *tmp;
  777. uint32_t tickstart = 0U;
  778. if (husart->State == HAL_USART_STATE_READY)
  779. {
  780. if ((pTxData == NULL) || (pRxData == NULL) || (Size == 0))
  781. {
  782. return HAL_ERROR;
  783. }
  784. /* Process Locked */
  785. __HAL_LOCK(husart);
  786. husart->ErrorCode = HAL_USART_ERROR_NONE;
  787. husart->State = HAL_USART_STATE_BUSY_RX;
  788. /* Init tickstart for timeout management */
  789. tickstart = HAL_GetTick();
  790. husart->RxXferSize = Size;
  791. husart->TxXferSize = Size;
  792. husart->TxXferCount = Size;
  793. husart->RxXferCount = Size;
  794. /* Check the remain data to be received */
  795. while (husart->TxXferCount > 0U)
  796. {
  797. husart->TxXferCount--;
  798. husart->RxXferCount--;
  799. if (husart->Init.WordLength == USART_WORDLENGTH_9B)
  800. {
  801. /* Wait for TC flag in order to write data in DR */
  802. if (USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_TXE, RESET, tickstart, Timeout) != HAL_OK)
  803. {
  804. return HAL_TIMEOUT;
  805. }
  806. tmp = (uint16_t *) pTxData;
  807. husart->Instance->DR = (*tmp & (uint16_t)0x01FF);
  808. if (husart->Init.Parity == USART_PARITY_NONE)
  809. {
  810. pTxData += 2U;
  811. }
  812. else
  813. {
  814. pTxData += 1U;
  815. }
  816. /* Wait for RXNE Flag */
  817. if (USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_RXNE, RESET, tickstart, Timeout) != HAL_OK)
  818. {
  819. return HAL_TIMEOUT;
  820. }
  821. tmp = (uint16_t *) pRxData ;
  822. if (husart->Init.Parity == USART_PARITY_NONE)
  823. {
  824. *tmp = (uint16_t)(husart->Instance->DR & (uint16_t)0x01FF);
  825. pRxData += 2U;
  826. }
  827. else
  828. {
  829. *tmp = (uint16_t)(husart->Instance->DR & (uint16_t)0x00FF);
  830. pRxData += 1U;
  831. }
  832. }
  833. else
  834. {
  835. /* Wait for TC flag in order to write data in DR */
  836. if (USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_TXE, RESET, tickstart, Timeout) != HAL_OK)
  837. {
  838. return HAL_TIMEOUT;
  839. }
  840. husart->Instance->DR = (*pTxData++ & (uint8_t)0x00FF);
  841. /* Wait for RXNE Flag */
  842. if (USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_RXNE, RESET, tickstart, Timeout) != HAL_OK)
  843. {
  844. return HAL_TIMEOUT;
  845. }
  846. if (husart->Init.Parity == USART_PARITY_NONE)
  847. {
  848. /* Receive data */
  849. *pRxData++ = (uint8_t)(husart->Instance->DR & (uint8_t)0x00FF);
  850. }
  851. else
  852. {
  853. /* Receive data */
  854. *pRxData++ = (uint8_t)(husart->Instance->DR & (uint8_t)0x007F);
  855. }
  856. }
  857. }
  858. husart->State = HAL_USART_STATE_READY;
  859. /* Process Unlocked */
  860. __HAL_UNLOCK(husart);
  861. return HAL_OK;
  862. }
  863. else
  864. {
  865. return HAL_BUSY;
  866. }
  867. }
  868. /**
  869. * @brief Simplex Send an amount of data in non-blocking mode.
  870. * @note When USART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M=1),
  871. * the sent data is handled as a set of u16. In this case, Size must indicate the number
  872. * of u16 provided through pTxData.
  873. * @param husart Pointer to a USART_HandleTypeDef structure that contains
  874. * the configuration information for the specified USART module.
  875. * @param pTxData Pointer to data buffer (u8 or u16 data elements).
  876. * @param Size Amount of data elements (u8 or u16) to be sent.
  877. * @retval HAL status
  878. * @note The USART errors are not managed to avoid the overrun error.
  879. */
  880. HAL_StatusTypeDef HAL_USART_Transmit_IT(USART_HandleTypeDef *husart, uint8_t *pTxData, uint16_t Size)
  881. {
  882. if (husart->State == HAL_USART_STATE_READY)
  883. {
  884. if ((pTxData == NULL) || (Size == 0))
  885. {
  886. return HAL_ERROR;
  887. }
  888. /* Process Locked */
  889. __HAL_LOCK(husart);
  890. husart->pTxBuffPtr = pTxData;
  891. husart->TxXferSize = Size;
  892. husart->TxXferCount = Size;
  893. husart->ErrorCode = HAL_USART_ERROR_NONE;
  894. husart->State = HAL_USART_STATE_BUSY_TX;
  895. /* The USART Error Interrupts: (Frame error, Noise error, Overrun error)
  896. are not managed by the USART transmit process to avoid the overrun interrupt
  897. when the USART mode is configured for transmit and receive "USART_MODE_TX_RX"
  898. to benefit for the frame error and noise interrupts the USART mode should be
  899. configured only for transmit "USART_MODE_TX"
  900. The __HAL_USART_ENABLE_IT(husart, USART_IT_ERR) can be used to enable the Frame error,
  901. Noise error interrupt */
  902. /* Process Unlocked */
  903. __HAL_UNLOCK(husart);
  904. /* Enable the USART Transmit Data Register Empty Interrupt */
  905. SET_BIT(husart->Instance->CR1, USART_CR1_TXEIE);
  906. return HAL_OK;
  907. }
  908. else
  909. {
  910. return HAL_BUSY;
  911. }
  912. }
  913. /**
  914. * @brief Simplex Receive an amount of data in non-blocking mode.
  915. * @note To receive synchronous data, dummy data are simultaneously transmitted.
  916. * @note When USART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M=1),
  917. * the received data is handled as a set of u16. In this case, Size must indicate the number
  918. * of u16 available through pRxData.
  919. * @param husart Pointer to a USART_HandleTypeDef structure that contains
  920. * the configuration information for the specified USART module.
  921. * @param pRxData Pointer to data buffer (u8 or u16 data elements).
  922. * @param Size Amount of data elements (u8 or u16) to be received.
  923. * @retval HAL status
  924. */
  925. HAL_StatusTypeDef HAL_USART_Receive_IT(USART_HandleTypeDef *husart, uint8_t *pRxData, uint16_t Size)
  926. {
  927. if (husart->State == HAL_USART_STATE_READY)
  928. {
  929. if ((pRxData == NULL) || (Size == 0))
  930. {
  931. return HAL_ERROR;
  932. }
  933. /* Process Locked */
  934. __HAL_LOCK(husart);
  935. husart->pRxBuffPtr = pRxData;
  936. husart->RxXferSize = Size;
  937. husart->RxXferCount = Size;
  938. husart->ErrorCode = HAL_USART_ERROR_NONE;
  939. husart->State = HAL_USART_STATE_BUSY_RX;
  940. /* Process Unlocked */
  941. __HAL_UNLOCK(husart);
  942. /* Enable the USART Parity Error and Data Register not empty Interrupts */
  943. SET_BIT(husart->Instance->CR1, USART_CR1_PEIE | USART_CR1_RXNEIE);
  944. /* Enable the USART Error Interrupt: (Frame error, noise error, overrun error) */
  945. SET_BIT(husart->Instance->CR3, USART_CR3_EIE);
  946. /* Send dummy byte in order to generate the clock for the slave to send data */
  947. husart->Instance->DR = (DUMMY_DATA & (uint16_t)0x01FF);
  948. return HAL_OK;
  949. }
  950. else
  951. {
  952. return HAL_BUSY;
  953. }
  954. }
  955. /**
  956. * @brief Full-Duplex Send and Receive an amount of data in full-duplex mode (non-blocking).
  957. * @note When USART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M=1),
  958. * the sent data and the received data are handled as sets of u16. In this case, Size must indicate the number
  959. * of u16 available through pTxData and through pRxData.
  960. * @param husart Pointer to a USART_HandleTypeDef structure that contains
  961. * the configuration information for the specified USART module.
  962. * @param pTxData Pointer to TX data buffer (u8 or u16 data elements).
  963. * @param pRxData Pointer to RX data buffer (u8 or u16 data elements).
  964. * @param Size Amount of data elements (u8 or u16) to be sent (same amount to be received).
  965. * @retval HAL status
  966. */
  967. HAL_StatusTypeDef HAL_USART_TransmitReceive_IT(USART_HandleTypeDef *husart, uint8_t *pTxData, uint8_t *pRxData, uint16_t Size)
  968. {
  969. if (husart->State == HAL_USART_STATE_READY)
  970. {
  971. if ((pTxData == NULL) || (pRxData == NULL) || (Size == 0))
  972. {
  973. return HAL_ERROR;
  974. }
  975. /* Process Locked */
  976. __HAL_LOCK(husart);
  977. husart->pRxBuffPtr = pRxData;
  978. husart->RxXferSize = Size;
  979. husart->RxXferCount = Size;
  980. husart->pTxBuffPtr = pTxData;
  981. husart->TxXferSize = Size;
  982. husart->TxXferCount = Size;
  983. husart->ErrorCode = HAL_USART_ERROR_NONE;
  984. husart->State = HAL_USART_STATE_BUSY_TX_RX;
  985. /* Process Unlocked */
  986. __HAL_UNLOCK(husart);
  987. /* Enable the USART Data Register not empty Interrupt */
  988. SET_BIT(husart->Instance->CR1, USART_CR1_RXNEIE);
  989. /* Enable the USART Parity Error Interrupt */
  990. SET_BIT(husart->Instance->CR1, USART_CR1_PEIE);
  991. /* Enable the USART Error Interrupt: (Frame error, noise error, overrun error) */
  992. SET_BIT(husart->Instance->CR3, USART_CR3_EIE);
  993. /* Enable the USART Transmit Data Register Empty Interrupt */
  994. SET_BIT(husart->Instance->CR1, USART_CR1_TXEIE);
  995. return HAL_OK;
  996. }
  997. else
  998. {
  999. return HAL_BUSY;
  1000. }
  1001. }
  1002. /**
  1003. * @brief Abort ongoing transfer (blocking mode).
  1004. * @param husart USART handle.
  1005. * @note This procedure could be used for aborting any ongoing transfer (either Tx or Rx,
  1006. * as described by TransferType parameter) started in Interrupt mode.
  1007. * This procedure performs following operations :
  1008. * - Disable PPP Interrupts (depending of transfer direction)
  1009. * - Set handle State to READY
  1010. * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
  1011. * @retval HAL status
  1012. */
  1013. HAL_StatusTypeDef HAL_USART_Abort(USART_HandleTypeDef *husart)
  1014. {
  1015. /* Disable TXEIE, TCIE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
  1016. CLEAR_BIT(husart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE | USART_CR1_TXEIE | USART_CR1_TCIE));
  1017. CLEAR_BIT(husart->Instance->CR3, USART_CR3_EIE);
  1018. /* Reset Tx and Rx transfer counters */
  1019. husart->TxXferCount = 0x00U;
  1020. husart->RxXferCount = 0x00U;
  1021. /* Restore husart->State to Ready */
  1022. husart->State = HAL_USART_STATE_READY;
  1023. /* Reset Handle ErrorCode to No Error */
  1024. husart->ErrorCode = HAL_USART_ERROR_NONE;
  1025. return HAL_OK;
  1026. }
  1027. /**
  1028. * @brief Abort ongoing transfer (Interrupt mode).
  1029. * @param husart USART handle.
  1030. * @note This procedure could be used for aborting any ongoing transfer (either Tx or Rx,
  1031. * as described by TransferType parameter) started in Interrupt mode.
  1032. * This procedure performs following operations :
  1033. * - Disable PPP Interrupts (depending of transfer direction)
  1034. * - Set handle State to READY
  1035. * - At abort completion, call user abort complete callback
  1036. * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be
  1037. * considered as completed only when user abort complete callback is executed (not when exiting function).
  1038. * @retval HAL status
  1039. */
  1040. HAL_StatusTypeDef HAL_USART_Abort_IT(USART_HandleTypeDef *husart)
  1041. {
  1042. uint32_t AbortCplt = 0x01U;
  1043. /* Disable TXEIE, TCIE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
  1044. CLEAR_BIT(husart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE | USART_CR1_TXEIE | USART_CR1_TCIE));
  1045. CLEAR_BIT(husart->Instance->CR3, USART_CR3_EIE);
  1046. /* call user Abort Complete callback */
  1047. if (AbortCplt == 0x01U)
  1048. {
  1049. /* Reset Tx and Rx transfer counters */
  1050. husart->TxXferCount = 0x00U;
  1051. husart->RxXferCount = 0x00U;
  1052. /* Reset errorCode */
  1053. husart->ErrorCode = HAL_USART_ERROR_NONE;
  1054. /* Restore husart->State to Ready */
  1055. husart->State = HAL_USART_STATE_READY;
  1056. /* call directly user Abort complete callback */
  1057. #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
  1058. /* Call registered Abort Complete Callback */
  1059. husart->AbortCpltCallback(husart);
  1060. #else
  1061. /* Call legacy weak Abort Complete Callback */
  1062. HAL_USART_AbortCpltCallback(husart);
  1063. #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
  1064. }
  1065. return HAL_OK;
  1066. }
  1067. /**
  1068. * @brief This function handles USART interrupt request.
  1069. * @param husart Pointer to a USART_HandleTypeDef structure that contains
  1070. * the configuration information for the specified USART module.
  1071. * @retval None
  1072. */
  1073. void HAL_USART_IRQHandler(USART_HandleTypeDef *husart)
  1074. {
  1075. uint32_t isrflags = READ_REG(husart->Instance->SR);
  1076. uint32_t cr1its = READ_REG(husart->Instance->CR1);
  1077. uint32_t cr3its = READ_REG(husart->Instance->CR3);
  1078. uint32_t errorflags = 0x00U;
  1079. /* If no error occurs */
  1080. errorflags = (isrflags & (uint32_t)(USART_SR_PE | USART_SR_FE | USART_SR_ORE | USART_SR_NE));
  1081. if (errorflags == RESET)
  1082. {
  1083. /* USART in mode Receiver -------------------------------------------------*/
  1084. if (((isrflags & USART_SR_RXNE) != RESET) && ((cr1its & USART_CR1_RXNEIE) != RESET))
  1085. {
  1086. if (husart->State == HAL_USART_STATE_BUSY_RX)
  1087. {
  1088. USART_Receive_IT(husart);
  1089. }
  1090. else
  1091. {
  1092. USART_TransmitReceive_IT(husart);
  1093. }
  1094. return;
  1095. }
  1096. }
  1097. /* If some errors occur */
  1098. if ((errorflags != RESET) && (((cr3its & USART_CR3_EIE) != RESET) || ((cr1its & (USART_CR1_RXNEIE | USART_CR1_PEIE)) != RESET)))
  1099. {
  1100. /* USART parity error interrupt occurred ----------------------------------*/
  1101. if (((isrflags & USART_SR_PE) != RESET) && ((cr1its & USART_CR1_PEIE) != RESET))
  1102. {
  1103. husart->ErrorCode |= HAL_USART_ERROR_PE;
  1104. }
  1105. /* USART noise error interrupt occurred --------------------------------*/
  1106. if (((isrflags & USART_SR_NE) != RESET) && ((cr3its & USART_CR3_EIE) != RESET))
  1107. {
  1108. husart->ErrorCode |= HAL_USART_ERROR_NE;
  1109. }
  1110. /* USART frame error interrupt occurred --------------------------------*/
  1111. if (((isrflags & USART_SR_FE) != RESET) && ((cr3its & USART_CR3_EIE) != RESET))
  1112. {
  1113. husart->ErrorCode |= HAL_USART_ERROR_FE;
  1114. }
  1115. /* USART Over-Run interrupt occurred -----------------------------------*/
  1116. if (((isrflags & USART_SR_ORE) != RESET) && ((cr3its & USART_CR3_EIE) != RESET))
  1117. {
  1118. husart->ErrorCode |= HAL_USART_ERROR_ORE;
  1119. }
  1120. if (husart->ErrorCode != HAL_USART_ERROR_NONE)
  1121. {
  1122. /* USART in mode Receiver -----------------------------------------------*/
  1123. if (((isrflags & USART_SR_RXNE) != RESET) && ((cr1its & USART_CR1_RXNEIE) != RESET))
  1124. {
  1125. if (husart->State == HAL_USART_STATE_BUSY_RX)
  1126. {
  1127. USART_Receive_IT(husart);
  1128. }
  1129. else
  1130. {
  1131. USART_TransmitReceive_IT(husart);
  1132. }
  1133. }
  1134. /* If Overrun error occurs,
  1135. consider error as blocking */
  1136. if ((husart->ErrorCode & HAL_USART_ERROR_ORE) != RESET)
  1137. {
  1138. /* Set the USART state ready to be able to start again the process,
  1139. Disable Rx Interrupts, if ongoing */
  1140. USART_EndRxTransfer(husart);
  1141. /* Call user error callback */
  1142. #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
  1143. /* Call registered Error Callback */
  1144. husart->ErrorCallback(husart);
  1145. #else
  1146. /* Call legacy weak Error Callback */
  1147. HAL_USART_ErrorCallback(husart);
  1148. #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
  1149. }
  1150. else
  1151. {
  1152. /* Call user error callback */
  1153. #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
  1154. /* Call registered Error Callback */
  1155. husart->ErrorCallback(husart);
  1156. #else
  1157. /* Call legacy weak Error Callback */
  1158. HAL_USART_ErrorCallback(husart);
  1159. #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
  1160. husart->ErrorCode = HAL_USART_ERROR_NONE;
  1161. }
  1162. }
  1163. return;
  1164. }
  1165. /* USART in mode Transmitter -----------------------------------------------*/
  1166. if (((isrflags & USART_SR_TXE) != RESET) && ((cr1its & USART_CR1_TXEIE) != RESET))
  1167. {
  1168. if (husart->State == HAL_USART_STATE_BUSY_TX)
  1169. {
  1170. USART_Transmit_IT(husart);
  1171. }
  1172. else
  1173. {
  1174. USART_TransmitReceive_IT(husart);
  1175. }
  1176. return;
  1177. }
  1178. /* USART in mode Transmitter (transmission end) ----------------------------*/
  1179. if (((isrflags & USART_SR_TC) != RESET) && ((cr1its & USART_CR1_TCIE) != RESET))
  1180. {
  1181. USART_EndTransmit_IT(husart);
  1182. return;
  1183. }
  1184. }
  1185. /**
  1186. * @brief Tx Transfer completed callbacks.
  1187. * @param husart Pointer to a USART_HandleTypeDef structure that contains
  1188. * the configuration information for the specified USART module.
  1189. * @retval None
  1190. */
  1191. __weak void HAL_USART_TxCpltCallback(USART_HandleTypeDef *husart)
  1192. {
  1193. /* Prevent unused argument(s) compilation warning */
  1194. UNUSED(husart);
  1195. /* NOTE: This function should not be modified, when the callback is needed,
  1196. the HAL_USART_TxCpltCallback could be implemented in the user file
  1197. */
  1198. }
  1199. /**
  1200. * @brief Tx Half Transfer completed callbacks.
  1201. * @param husart Pointer to a USART_HandleTypeDef structure that contains
  1202. * the configuration information for the specified USART module.
  1203. * @retval None
  1204. */
  1205. __weak void HAL_USART_TxHalfCpltCallback(USART_HandleTypeDef *husart)
  1206. {
  1207. /* Prevent unused argument(s) compilation warning */
  1208. UNUSED(husart);
  1209. /* NOTE: This function should not be modified, when the callback is needed,
  1210. the HAL_USART_TxHalfCpltCallback could be implemented in the user file
  1211. */
  1212. }
  1213. /**
  1214. * @brief Rx Transfer completed callbacks.
  1215. * @param husart Pointer to a USART_HandleTypeDef structure that contains
  1216. * the configuration information for the specified USART module.
  1217. * @retval None
  1218. */
  1219. __weak void HAL_USART_RxCpltCallback(USART_HandleTypeDef *husart)
  1220. {
  1221. /* Prevent unused argument(s) compilation warning */
  1222. UNUSED(husart);
  1223. /* NOTE: This function should not be modified, when the callback is needed,
  1224. the HAL_USART_RxCpltCallback could be implemented in the user file
  1225. */
  1226. }
  1227. /**
  1228. * @brief Rx Half Transfer completed callbacks.
  1229. * @param husart Pointer to a USART_HandleTypeDef structure that contains
  1230. * the configuration information for the specified USART module.
  1231. * @retval None
  1232. */
  1233. __weak void HAL_USART_RxHalfCpltCallback(USART_HandleTypeDef *husart)
  1234. {
  1235. /* Prevent unused argument(s) compilation warning */
  1236. UNUSED(husart);
  1237. /* NOTE: This function should not be modified, when the callback is needed,
  1238. the HAL_USART_RxHalfCpltCallback could be implemented in the user file
  1239. */
  1240. }
  1241. /**
  1242. * @brief Tx/Rx Transfers completed callback for the non-blocking process.
  1243. * @param husart Pointer to a USART_HandleTypeDef structure that contains
  1244. * the configuration information for the specified USART module.
  1245. * @retval None
  1246. */
  1247. __weak void HAL_USART_TxRxCpltCallback(USART_HandleTypeDef *husart)
  1248. {
  1249. /* Prevent unused argument(s) compilation warning */
  1250. UNUSED(husart);
  1251. /* NOTE: This function should not be modified, when the callback is needed,
  1252. the HAL_USART_TxRxCpltCallback could be implemented in the user file
  1253. */
  1254. }
  1255. /**
  1256. * @brief USART error callbacks.
  1257. * @param husart Pointer to a USART_HandleTypeDef structure that contains
  1258. * the configuration information for the specified USART module.
  1259. * @retval None
  1260. */
  1261. __weak void HAL_USART_ErrorCallback(USART_HandleTypeDef *husart)
  1262. {
  1263. /* Prevent unused argument(s) compilation warning */
  1264. UNUSED(husart);
  1265. /* NOTE: This function should not be modified, when the callback is needed,
  1266. the HAL_USART_ErrorCallback could be implemented in the user file
  1267. */
  1268. }
  1269. /**
  1270. * @brief USART Abort Complete callback.
  1271. * @param husart USART handle.
  1272. * @retval None
  1273. */
  1274. __weak void HAL_USART_AbortCpltCallback(USART_HandleTypeDef *husart)
  1275. {
  1276. /* Prevent unused argument(s) compilation warning */
  1277. UNUSED(husart);
  1278. /* NOTE : This function should not be modified, when the callback is needed,
  1279. the HAL_USART_AbortCpltCallback can be implemented in the user file.
  1280. */
  1281. }
  1282. /**
  1283. * @}
  1284. */
  1285. /** @defgroup USART_Exported_Functions_Group3 Peripheral State and Errors functions
  1286. * @brief USART State and Errors functions
  1287. *
  1288. @verbatim
  1289. ==============================================================================
  1290. ##### Peripheral State and Errors functions #####
  1291. ==============================================================================
  1292. [..]
  1293. This subsection provides a set of functions allowing to return the State of
  1294. USART communication
  1295. process, return Peripheral Errors occurred during communication process
  1296. (+) HAL_USART_GetState() API can be helpful to check in run-time the state
  1297. of the USART peripheral.
  1298. (+) HAL_USART_GetError() check in run-time errors that could be occurred during
  1299. communication.
  1300. @endverbatim
  1301. * @{
  1302. */
  1303. /**
  1304. * @brief Returns the USART state.
  1305. * @param husart Pointer to a USART_HandleTypeDef structure that contains
  1306. * the configuration information for the specified USART module.
  1307. * @retval HAL state
  1308. */
  1309. HAL_USART_StateTypeDef HAL_USART_GetState(USART_HandleTypeDef *husart)
  1310. {
  1311. return husart->State;
  1312. }
  1313. /**
  1314. * @brief Return the USART error code
  1315. * @param husart Pointer to a USART_HandleTypeDef structure that contains
  1316. * the configuration information for the specified USART.
  1317. * @retval USART Error Code
  1318. */
  1319. uint32_t HAL_USART_GetError(USART_HandleTypeDef *husart)
  1320. {
  1321. return husart->ErrorCode;
  1322. }
  1323. /**
  1324. * @}
  1325. */
  1326. /**
  1327. * @}
  1328. */
  1329. /** @defgroup USART_Private_Functions USART Private Functions
  1330. * @{
  1331. */
  1332. /**
  1333. * @brief Initialize the callbacks to their default values.
  1334. * @param husart USART handle.
  1335. * @retval none
  1336. */
  1337. #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
  1338. void USART_InitCallbacksToDefault(USART_HandleTypeDef *husart)
  1339. {
  1340. /* Init the USART Callback settings */
  1341. husart->TxHalfCpltCallback = HAL_USART_TxHalfCpltCallback; /* Legacy weak TxHalfCpltCallback */
  1342. husart->TxCpltCallback = HAL_USART_TxCpltCallback; /* Legacy weak TxCpltCallback */
  1343. husart->RxHalfCpltCallback = HAL_USART_RxHalfCpltCallback; /* Legacy weak RxHalfCpltCallback */
  1344. husart->RxCpltCallback = HAL_USART_RxCpltCallback; /* Legacy weak RxCpltCallback */
  1345. husart->TxRxCpltCallback = HAL_USART_TxRxCpltCallback; /* Legacy weak TxRxCpltCallback */
  1346. husart->ErrorCallback = HAL_USART_ErrorCallback; /* Legacy weak ErrorCallback */
  1347. husart->AbortCpltCallback = HAL_USART_AbortCpltCallback; /* Legacy weak AbortCpltCallback */
  1348. }
  1349. #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
  1350. /**
  1351. * @brief This function handles USART Communication Timeout.
  1352. * @param husart Pointer to a USART_HandleTypeDef structure that contains
  1353. * the configuration information for the specified USART module.
  1354. * @param Flag specifies the USART flag to check.
  1355. * @param Status The new Flag status (SET or RESET).
  1356. * @param Tickstart Tick start value.
  1357. * @param Timeout Timeout duration.
  1358. * @retval HAL status
  1359. */
  1360. static HAL_StatusTypeDef USART_WaitOnFlagUntilTimeout(USART_HandleTypeDef *husart, uint32_t Flag, FlagStatus Status, uint32_t Tickstart, uint32_t Timeout)
  1361. {
  1362. /* Wait until flag is set */
  1363. while ((__HAL_USART_GET_FLAG(husart, Flag) ? SET : RESET) == Status)
  1364. {
  1365. /* Check for the Timeout */
  1366. if (Timeout != HAL_MAX_DELAY)
  1367. {
  1368. if ((Timeout == 0U) || ((HAL_GetTick() - Tickstart) > Timeout))
  1369. {
  1370. /* Disable the USART Transmit Complete Interrupt */
  1371. CLEAR_BIT(husart->Instance->CR1, USART_CR1_TXEIE);
  1372. /* Disable the USART RXNE Interrupt */
  1373. CLEAR_BIT(husart->Instance->CR1, USART_CR1_RXNEIE);
  1374. /* Disable the USART Parity Error Interrupt */
  1375. CLEAR_BIT(husart->Instance->CR1, USART_CR1_PEIE);
  1376. /* Disable the USART Error Interrupt: (Frame error, noise error, overrun error) */
  1377. CLEAR_BIT(husart->Instance->CR3, USART_CR3_EIE);
  1378. husart->State = HAL_USART_STATE_READY;
  1379. /* Process Unlocked */
  1380. __HAL_UNLOCK(husart);
  1381. return HAL_TIMEOUT;
  1382. }
  1383. }
  1384. }
  1385. return HAL_OK;
  1386. }
  1387. /**
  1388. * @brief End ongoing Rx transfer on USART peripheral (following error detection or Reception completion).
  1389. * @param husart USART handle.
  1390. * @retval None
  1391. */
  1392. static void USART_EndRxTransfer(USART_HandleTypeDef *husart)
  1393. {
  1394. /* Disable RXNE, PE and ERR interrupts */
  1395. CLEAR_BIT(husart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE));
  1396. CLEAR_BIT(husart->Instance->CR3, USART_CR3_EIE);
  1397. /* At end of Rx process, restore husart->State to Ready */
  1398. husart->State = HAL_USART_STATE_READY;
  1399. }
  1400. /**
  1401. * @brief Simplex Send an amount of data in non-blocking mode.
  1402. * @param husart Pointer to a USART_HandleTypeDef structure that contains
  1403. * the configuration information for the specified USART module.
  1404. * @retval HAL status
  1405. * @note The USART errors are not managed to avoid the overrun error.
  1406. */
  1407. static HAL_StatusTypeDef USART_Transmit_IT(USART_HandleTypeDef *husart)
  1408. {
  1409. uint16_t *tmp;
  1410. if (husart->State == HAL_USART_STATE_BUSY_TX)
  1411. {
  1412. if (husart->Init.WordLength == USART_WORDLENGTH_9B)
  1413. {
  1414. tmp = (uint16_t *) husart->pTxBuffPtr;
  1415. husart->Instance->DR = (uint16_t)(*tmp & (uint16_t)0x01FF);
  1416. if (husart->Init.Parity == USART_PARITY_NONE)
  1417. {
  1418. husart->pTxBuffPtr += 2U;
  1419. }
  1420. else
  1421. {
  1422. husart->pTxBuffPtr += 1U;
  1423. }
  1424. }
  1425. else
  1426. {
  1427. husart->Instance->DR = (uint8_t)(*husart->pTxBuffPtr++ & (uint8_t)0x00FF);
  1428. }
  1429. if (--husart->TxXferCount == 0U)
  1430. {
  1431. /* Disable the USART Transmit data register empty Interrupt */
  1432. CLEAR_BIT(husart->Instance->CR1, USART_CR1_TXEIE);
  1433. /* Enable the USART Transmit Complete Interrupt */
  1434. SET_BIT(husart->Instance->CR1, USART_CR1_TCIE);
  1435. }
  1436. return HAL_OK;
  1437. }
  1438. else
  1439. {
  1440. return HAL_BUSY;
  1441. }
  1442. }
  1443. /**
  1444. * @brief Wraps up transmission in non blocking mode.
  1445. * @param husart Pointer to a USART_HandleTypeDef structure that contains
  1446. * the configuration information for the specified USART module.
  1447. * @retval HAL status
  1448. */
  1449. static HAL_StatusTypeDef USART_EndTransmit_IT(USART_HandleTypeDef *husart)
  1450. {
  1451. /* Disable the USART Transmit Complete Interrupt */
  1452. CLEAR_BIT(husart->Instance->CR1, USART_CR1_TCIE);
  1453. /* Disable the USART Error Interrupt: (Frame error, noise error, overrun error) */
  1454. CLEAR_BIT(husart->Instance->CR3, USART_CR3_EIE);
  1455. husart->State = HAL_USART_STATE_READY;
  1456. #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
  1457. /* Call registered Tx Complete Callback */
  1458. husart->TxCpltCallback(husart);
  1459. #else
  1460. /* Call legacy weak Tx Complete Callback */
  1461. HAL_USART_TxCpltCallback(husart);
  1462. #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
  1463. return HAL_OK;
  1464. }
  1465. /**
  1466. * @brief Simplex Receive an amount of data in non-blocking mode.
  1467. * @param husart Pointer to a USART_HandleTypeDef structure that contains
  1468. * the configuration information for the specified USART module.
  1469. * @retval HAL status
  1470. */
  1471. static HAL_StatusTypeDef USART_Receive_IT(USART_HandleTypeDef *husart)
  1472. {
  1473. uint16_t *tmp;
  1474. if (husart->State == HAL_USART_STATE_BUSY_RX)
  1475. {
  1476. if (husart->Init.WordLength == USART_WORDLENGTH_9B)
  1477. {
  1478. tmp = (uint16_t *) husart->pRxBuffPtr;
  1479. if (husart->Init.Parity == USART_PARITY_NONE)
  1480. {
  1481. *tmp = (uint16_t)(husart->Instance->DR & (uint16_t)0x01FF);
  1482. husart->pRxBuffPtr += 2U;
  1483. }
  1484. else
  1485. {
  1486. *tmp = (uint16_t)(husart->Instance->DR & (uint16_t)0x00FF);
  1487. husart->pRxBuffPtr += 1U;
  1488. }
  1489. if (--husart->RxXferCount != 0x00U)
  1490. {
  1491. /* Send dummy byte in order to generate the clock for the slave to send the next data */
  1492. husart->Instance->DR = (DUMMY_DATA & (uint16_t)0x01FF);
  1493. }
  1494. }
  1495. else
  1496. {
  1497. if (husart->Init.Parity == USART_PARITY_NONE)
  1498. {
  1499. *husart->pRxBuffPtr++ = (uint8_t)(husart->Instance->DR & (uint8_t)0x00FF);
  1500. }
  1501. else
  1502. {
  1503. *husart->pRxBuffPtr++ = (uint8_t)(husart->Instance->DR & (uint8_t)0x007F);
  1504. }
  1505. if (--husart->RxXferCount != 0x00U)
  1506. {
  1507. /* Send dummy byte in order to generate the clock for the slave to send the next data */
  1508. husart->Instance->DR = (DUMMY_DATA & (uint16_t)0x00FF);
  1509. }
  1510. }
  1511. if (husart->RxXferCount == 0U)
  1512. {
  1513. /* Disable the USART RXNE Interrupt */
  1514. CLEAR_BIT(husart->Instance->CR1, USART_CR1_RXNEIE);
  1515. /* Disable the USART Parity Error Interrupt */
  1516. CLEAR_BIT(husart->Instance->CR1, USART_CR1_PEIE);
  1517. /* Disable the USART Error Interrupt: (Frame error, noise error, overrun error) */
  1518. CLEAR_BIT(husart->Instance->CR3, USART_CR3_EIE);
  1519. husart->State = HAL_USART_STATE_READY;
  1520. #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
  1521. /* Call registered Rx Complete Callback */
  1522. husart->RxCpltCallback(husart);
  1523. #else
  1524. /* Call legacy weak Rx Complete Callback */
  1525. HAL_USART_RxCpltCallback(husart);
  1526. #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
  1527. return HAL_OK;
  1528. }
  1529. return HAL_OK;
  1530. }
  1531. else
  1532. {
  1533. return HAL_BUSY;
  1534. }
  1535. }
  1536. /**
  1537. * @brief Full-Duplex Send receive an amount of data in full-duplex mode (non-blocking).
  1538. * @param husart Pointer to a USART_HandleTypeDef structure that contains
  1539. * the configuration information for the specified USART module.
  1540. * @retval HAL status
  1541. */
  1542. static HAL_StatusTypeDef USART_TransmitReceive_IT(USART_HandleTypeDef *husart)
  1543. {
  1544. uint16_t *tmp;
  1545. if (husart->State == HAL_USART_STATE_BUSY_TX_RX)
  1546. {
  1547. if (husart->TxXferCount != 0x00U)
  1548. {
  1549. if (__HAL_USART_GET_FLAG(husart, USART_FLAG_TXE) != RESET)
  1550. {
  1551. if (husart->Init.WordLength == USART_WORDLENGTH_9B)
  1552. {
  1553. tmp = (uint16_t *) husart->pTxBuffPtr;
  1554. husart->Instance->DR = (uint16_t)(*tmp & (uint16_t)0x01FF);
  1555. if (husart->Init.Parity == USART_PARITY_NONE)
  1556. {
  1557. husart->pTxBuffPtr += 2U;
  1558. }
  1559. else
  1560. {
  1561. husart->pTxBuffPtr += 1U;
  1562. }
  1563. }
  1564. else
  1565. {
  1566. husart->Instance->DR = (uint8_t)(*husart->pTxBuffPtr++ & (uint8_t)0x00FF);
  1567. }
  1568. husart->TxXferCount--;
  1569. /* Check the latest data transmitted */
  1570. if (husart->TxXferCount == 0U)
  1571. {
  1572. CLEAR_BIT(husart->Instance->CR1, USART_CR1_TXEIE);
  1573. }
  1574. }
  1575. }
  1576. if (husart->RxXferCount != 0x00U)
  1577. {
  1578. if (__HAL_USART_GET_FLAG(husart, USART_FLAG_RXNE) != RESET)
  1579. {
  1580. if (husart->Init.WordLength == USART_WORDLENGTH_9B)
  1581. {
  1582. tmp = (uint16_t *) husart->pRxBuffPtr;
  1583. if (husart->Init.Parity == USART_PARITY_NONE)
  1584. {
  1585. *tmp = (uint16_t)(husart->Instance->DR & (uint16_t)0x01FF);
  1586. husart->pRxBuffPtr += 2U;
  1587. }
  1588. else
  1589. {
  1590. *tmp = (uint16_t)(husart->Instance->DR & (uint16_t)0x00FF);
  1591. husart->pRxBuffPtr += 1U;
  1592. }
  1593. }
  1594. else
  1595. {
  1596. if (husart->Init.Parity == USART_PARITY_NONE)
  1597. {
  1598. *husart->pRxBuffPtr++ = (uint8_t)(husart->Instance->DR & (uint8_t)0x00FF);
  1599. }
  1600. else
  1601. {
  1602. *husart->pRxBuffPtr++ = (uint8_t)(husart->Instance->DR & (uint8_t)0x007F);
  1603. }
  1604. }
  1605. husart->RxXferCount--;
  1606. }
  1607. }
  1608. /* Check the latest data received */
  1609. if (husart->RxXferCount == 0U)
  1610. {
  1611. /* Disable the USART RXNE Interrupt */
  1612. CLEAR_BIT(husart->Instance->CR1, USART_CR1_RXNEIE);
  1613. /* Disable the USART Parity Error Interrupt */
  1614. CLEAR_BIT(husart->Instance->CR1, USART_CR1_PEIE);
  1615. /* Disable the USART Error Interrupt: (Frame error, noise error, overrun error) */
  1616. CLEAR_BIT(husart->Instance->CR3, USART_CR3_EIE);
  1617. husart->State = HAL_USART_STATE_READY;
  1618. #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
  1619. /* Call registered Tx Rx Complete Callback */
  1620. husart->TxRxCpltCallback(husart);
  1621. #else
  1622. /* Call legacy weak Tx Rx Complete Callback */
  1623. HAL_USART_TxRxCpltCallback(husart);
  1624. #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
  1625. return HAL_OK;
  1626. }
  1627. return HAL_OK;
  1628. }
  1629. else
  1630. {
  1631. return HAL_BUSY;
  1632. }
  1633. }
  1634. /**
  1635. * @brief Configures the USART peripheral.
  1636. * @param husart Pointer to a USART_HandleTypeDef structure that contains
  1637. * the configuration information for the specified USART module.
  1638. * @retval None
  1639. */
  1640. static void USART_SetConfig(USART_HandleTypeDef *husart)
  1641. {
  1642. uint32_t tmpreg = 0x00U;
  1643. uint32_t pclk;
  1644. /* Check the parameters */
  1645. assert_param(IS_USART_INSTANCE(husart->Instance));
  1646. assert_param(IS_USART_POLARITY(husart->Init.CLKPolarity));
  1647. assert_param(IS_USART_PHASE(husart->Init.CLKPhase));
  1648. assert_param(IS_USART_LASTBIT(husart->Init.CLKLastBit));
  1649. assert_param(IS_USART_BAUDRATE(husart->Init.BaudRate));
  1650. assert_param(IS_USART_WORD_LENGTH(husart->Init.WordLength));
  1651. assert_param(IS_USART_STOPBITS(husart->Init.StopBits));
  1652. assert_param(IS_USART_PARITY(husart->Init.Parity));
  1653. assert_param(IS_USART_MODE(husart->Init.Mode));
  1654. /* The LBCL, CPOL and CPHA bits have to be selected when both the transmitter and the
  1655. receiver are disabled (TE=RE=0) to ensure that the clock pulses function correctly. */
  1656. CLEAR_BIT(husart->Instance->CR1, (USART_CR1_TE | USART_CR1_RE));
  1657. /*---------------------------- USART CR2 Configuration ---------------------*/
  1658. tmpreg = husart->Instance->CR2;
  1659. /* Clear CLKEN, CPOL, CPHA and LBCL bits */
  1660. tmpreg &= (uint32_t)~((uint32_t)(USART_CR2_CPHA | USART_CR2_CPOL | USART_CR2_CLKEN | USART_CR2_LBCL | USART_CR2_STOP));
  1661. /* Configure the USART Clock, CPOL, CPHA and LastBit -----------------------*/
  1662. /* Set CPOL bit according to husart->Init.CLKPolarity value */
  1663. /* Set CPHA bit according to husart->Init.CLKPhase value */
  1664. /* Set LBCL bit according to husart->Init.CLKLastBit value */
  1665. /* Set Stop Bits: Set STOP[13:12] bits according to husart->Init.StopBits value */
  1666. tmpreg |= (uint32_t)(USART_CLOCK_ENABLE | husart->Init.CLKPolarity |
  1667. husart->Init.CLKPhase | husart->Init.CLKLastBit | husart->Init.StopBits);
  1668. /* Write to USART CR2 */
  1669. WRITE_REG(husart->Instance->CR2, (uint32_t)tmpreg);
  1670. /*-------------------------- USART CR1 Configuration -----------------------*/
  1671. tmpreg = husart->Instance->CR1;
  1672. /* Clear M, PCE, PS, TE and RE bits */
  1673. tmpreg &= (uint32_t)~((uint32_t)(USART_CR1_M | USART_CR1_PCE | USART_CR1_PS | USART_CR1_TE | USART_CR1_RE));
  1674. /* Configure the USART Word Length, Parity and mode:
  1675. Set the M bits according to husart->Init.WordLength value
  1676. Set PCE and PS bits according to husart->Init.Parity value
  1677. Set TE and RE bits according to husart->Init.Mode value
  1678. */
  1679. tmpreg |= (uint32_t)husart->Init.WordLength | husart->Init.Parity | husart->Init.Mode;
  1680. /* Write to USART CR1 */
  1681. WRITE_REG(husart->Instance->CR1, (uint32_t)tmpreg);
  1682. /*-------------------------- USART CR3 Configuration -----------------------*/
  1683. /* Clear CTSE and RTSE bits */
  1684. CLEAR_BIT(husart->Instance->CR3, (USART_CR3_RTSE | USART_CR3_CTSE));
  1685. /*-------------------------- USART BRR Configuration -----------------------*/
  1686. pclk = HAL_RCC_GetPCLK1Freq();
  1687. husart->Instance->BRR = USART_BRR(pclk, husart->Init.BaudRate);
  1688. }
  1689. /**
  1690. * @}
  1691. */
  1692. #endif /* HAL_USART_MODULE_ENABLED */
  1693. /**
  1694. * @}
  1695. */
  1696. /**
  1697. * @}
  1698. */
  1699. /************************ (C) COPYRIGHT Puya *****END OF FILE****/