stm32f1xx_hal_usart.c 77 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310
  1. /**
  2. ******************************************************************************
  3. * @file stm32f1xx_hal_usart.c
  4. * @author MCD Application Team
  5. * @version V1.1.1
  6. * @date 12-May-2017
  7. * @brief USART HAL module driver.
  8. * This file provides firmware functions to manage the following
  9. * functionalities of the Universal Synchronous Asynchronous Receiver Transmitter (USART) peripheral:
  10. * + Initialization and de-initialization functions
  11. * + IO operation functions
  12. * + Peripheral Control functions
  13. @verbatim
  14. ==============================================================================
  15. ##### How to use this driver #####
  16. ==============================================================================
  17. [..]
  18. The USART HAL driver can be used as follows:
  19. (#) Declare a USART_HandleTypeDef handle structure.
  20. (#) Initialize the USART low level resources by implementing the HAL_USART_MspInit () API:
  21. (##) Enable the USARTx interface clock.
  22. (##) USART pins configuration:
  23. (+++) Enable the clock for the USART GPIOs.
  24. (+++) Configure the USART pins (TX as alternate function pull-up, RX as alternate function Input).
  25. (##) NVIC configuration if you need to use interrupt process (HAL_USART_Transmit_IT(),
  26. HAL_USART_Receive_IT() and HAL_USART_TransmitReceive_IT() APIs):
  27. (+++) Configure the USARTx interrupt priority.
  28. (+++) Enable the NVIC USART IRQ handle.
  29. (##) DMA Configuration if you need to use DMA process (HAL_USART_Transmit_DMA()
  30. HAL_USART_Receive_DMA() and HAL_USART_TransmitReceive_DMA() APIs):
  31. (+++) Declare a DMA handle structure for the Tx/Rx channel.
  32. (+++) Enable the DMAx interface clock.
  33. (+++) Configure the declared DMA handle structure with the required Tx/Rx parameters.
  34. (+++) Configure the DMA Tx/Rx channel.
  35. (+++) Associate the initialized DMA handle to the USART DMA Tx/Rx handle.
  36. (+++) Configure the priority and enable the NVIC for the transfer complete interrupt on the DMA Tx/Rx channel.
  37. (+++) Configure the priority and enable the NVIC for the transfer complete
  38. interrupt on the DMA Tx/Rx channel.
  39. (+++) Configure the USARTx interrupt priority and enable the NVIC USART IRQ handle
  40. (used for last byte sending completion detection in DMA non circular mode)
  41. (#) Program the Baud Rate, Word Length, Stop Bit, Parity, Hardware
  42. flow control and Mode(Receiver/Transmitter) in the husart Init structure.
  43. (#) Initialize the USART registers by calling the HAL_USART_Init() API:
  44. (++) These APIs configures also the low level Hardware GPIO, CLOCK, CORTEX...etc)
  45. by calling the customized HAL_USART_MspInit(&husart) API.
  46. -@@- The specific USART interrupts (Transmission complete interrupt,
  47. RXNE interrupt and Error Interrupts) will be managed using the macros
  48. __HAL_USART_ENABLE_IT() and __HAL_USART_DISABLE_IT() inside the transmit and receive process.
  49. (#) Three operation modes are available within this driver :
  50. *** Polling mode IO operation ***
  51. =================================
  52. [..]
  53. (+) Send an amount of data in blocking mode using HAL_USART_Transmit()
  54. (+) Receive an amount of data in blocking mode using HAL_USART_Receive()
  55. *** Interrupt mode IO operation ***
  56. ===================================
  57. [..]
  58. (+) Send an amount of data in non blocking mode using HAL_USART_Transmit_IT()
  59. (+) At transmission end of transfer HAL_USART_TxHalfCpltCallback is executed and user can
  60. add his own code by customization of function pointer HAL_USART_TxCpltCallback
  61. (+) Receive an amount of data in non blocking mode using HAL_USART_Receive_IT()
  62. (+) At reception end of transfer HAL_USART_RxCpltCallback is executed and user can
  63. add his own code by customization of function pointer HAL_USART_RxCpltCallback
  64. (+) In case of transfer Error, HAL_USART_ErrorCallback() function is executed and user can
  65. add his own code by customization of function pointer HAL_USART_ErrorCallback
  66. *** DMA mode IO operation ***
  67. ==============================
  68. [..]
  69. (+) Send an amount of data in non blocking mode (DMA) using HAL_USART_Transmit_DMA()
  70. (+) At transmission end of half transfer HAL_USART_TxHalfCpltCallback is executed and user can
  71. add his own code by customization of function pointer HAL_USART_TxHalfCpltCallback
  72. (+) At transmission end of transfer HAL_USART_TxCpltCallback is executed and user can
  73. add his own code by customization of function pointer HAL_USART_TxCpltCallback
  74. (+) Receive an amount of data in non blocking mode (DMA) using HAL_USART_Receive_DMA()
  75. (+) At reception end of half transfer HAL_USART_RxHalfCpltCallback is executed and user can
  76. add his own code by customization of function pointer HAL_USART_RxHalfCpltCallback
  77. (+) At reception end of transfer HAL_USART_RxCpltCallback is executed and user can
  78. add his own code by customization of function pointer HAL_USART_RxCpltCallback
  79. (+) In case of transfer Error, HAL_USART_ErrorCallback() function is executed and user can
  80. add his own code by customization of function pointer HAL_USART_ErrorCallback
  81. (+) Pause the DMA Transfer using HAL_USART_DMAPause()
  82. (+) Resume the DMA Transfer using HAL_USART_DMAResume()
  83. (+) Stop the DMA Transfer using HAL_USART_DMAStop()
  84. *** USART HAL driver macros list ***
  85. =============================================
  86. [..]
  87. Below the list of most used macros in USART HAL driver.
  88. (+) __HAL_USART_ENABLE: Enable the USART peripheral
  89. (+) __HAL_USART_DISABLE: Disable the USART peripheral
  90. (+) __HAL_USART_GET_FLAG : Check whether the specified USART flag is set or not
  91. (+) __HAL_USART_CLEAR_FLAG : Clear the specified USART pending flag
  92. (+) __HAL_USART_ENABLE_IT: Enable the specified USART interrupt
  93. (+) __HAL_USART_DISABLE_IT: Disable the specified USART interrupt
  94. [..]
  95. (@) You can refer to the USART HAL driver header file for more useful macros
  96. @endverbatim
  97. [..]
  98. (@) Additionnal remark: If the parity is enabled, then the MSB bit of the data written
  99. in the data register is transmitted but is changed by the parity bit.
  100. Depending on the frame length defined by the M bit (8-bits or 9-bits),
  101. the possible USART frame formats are as listed in the following table:
  102. +-------------------------------------------------------------+
  103. | M bit | PCE bit | USART frame |
  104. |---------------------|---------------------------------------|
  105. | 0 | 0 | | SB | 8 bit data | STB | |
  106. |---------|-----------|---------------------------------------|
  107. | 0 | 1 | | SB | 7 bit data | PB | STB | |
  108. |---------|-----------|---------------------------------------|
  109. | 1 | 0 | | SB | 9 bit data | STB | |
  110. |---------|-----------|---------------------------------------|
  111. | 1 | 1 | | SB | 8 bit data | PB | STB | |
  112. +-------------------------------------------------------------+
  113. ******************************************************************************
  114. * @attention
  115. *
  116. * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
  117. *
  118. * Redistribution and use in source and binary forms, with or without modification,
  119. * are permitted provided that the following conditions are met:
  120. * 1. Redistributions of source code must retain the above copyright notice,
  121. * this list of conditions and the following disclaimer.
  122. * 2. Redistributions in binary form must reproduce the above copyright notice,
  123. * this list of conditions and the following disclaimer in the documentation
  124. * and/or other materials provided with the distribution.
  125. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  126. * may be used to endorse or promote products derived from this software
  127. * without specific prior written permission.
  128. *
  129. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  130. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  131. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  132. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  133. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  134. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  135. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  136. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  137. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  138. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  139. *
  140. ******************************************************************************
  141. */
  142. /* Includes ------------------------------------------------------------------*/
  143. #include "stm32f1xx_hal.h"
  144. /** @addtogroup STM32F1xx_HAL_Driver
  145. * @{
  146. */
  147. /** @defgroup USART USART
  148. * @brief HAL USART Synchronous module driver
  149. * @{
  150. */
  151. #ifdef HAL_USART_MODULE_ENABLED
  152. /* Private typedef -----------------------------------------------------------*/
  153. /* Private define ------------------------------------------------------------*/
  154. /** @addtogroup USART_Private_Constants
  155. * @{
  156. */
  157. #define DUMMY_DATA 0xFFFFU
  158. #define USART_TIMEOUT_VALUE 22000U
  159. /**
  160. * @}
  161. */
  162. /* Private macro -------------------------------------------------------------*/
  163. /* Private variables ---------------------------------------------------------*/
  164. /* Private function prototypes -----------------------------------------------*/
  165. /* Private functions ---------------------------------------------------------*/
  166. /** @addtogroup USART_Private_Functions
  167. * @{
  168. */
  169. static void USART_EndTxTransfer(USART_HandleTypeDef *husart);
  170. static void USART_EndRxTransfer(USART_HandleTypeDef *husart);
  171. static HAL_StatusTypeDef USART_Transmit_IT(USART_HandleTypeDef *husart);
  172. static HAL_StatusTypeDef USART_EndTransmit_IT(USART_HandleTypeDef *husart);
  173. static HAL_StatusTypeDef USART_Receive_IT(USART_HandleTypeDef *husart);
  174. static HAL_StatusTypeDef USART_TransmitReceive_IT(USART_HandleTypeDef *husart);
  175. static void USART_SetConfig (USART_HandleTypeDef *husart);
  176. static void USART_DMATransmitCplt(DMA_HandleTypeDef *hdma);
  177. static void USART_DMATxHalfCplt(DMA_HandleTypeDef *hdma);
  178. static void USART_DMAReceiveCplt(DMA_HandleTypeDef *hdma);
  179. static void USART_DMARxHalfCplt(DMA_HandleTypeDef *hdma);
  180. static void USART_DMAError(DMA_HandleTypeDef *hdma);
  181. static void USART_DMAAbortOnError(DMA_HandleTypeDef *hdma);
  182. static void USART_DMATxAbortCallback(DMA_HandleTypeDef *hdma);
  183. static void USART_DMARxAbortCallback(DMA_HandleTypeDef *hdma);
  184. static HAL_StatusTypeDef USART_WaitOnFlagUntilTimeout(USART_HandleTypeDef *husart, uint32_t Flag, FlagStatus Status, uint32_t Tickstart, uint32_t Timeout);
  185. /**
  186. * @}
  187. */
  188. /* Exported functions --------------------------------------------------------*/
  189. /** @defgroup USART_Exported_Functions USART Exported Functions
  190. * @{
  191. */
  192. /** @defgroup USART_Exported_Functions_Group1 USART Initialization and de-initialization functions
  193. * @brief Initialization and Configuration functions
  194. *
  195. @verbatim
  196. ==============================================================================
  197. ##### Initialization and Configuration functions #####
  198. ==============================================================================
  199. [..]
  200. This subsection provides a set of functions allowing to initialize the USART
  201. in asynchronous and in synchronous modes.
  202. (+) For the asynchronous mode only these parameters can be configured:
  203. (++) Baud Rate
  204. (++) Word Length
  205. (++) Stop Bit
  206. (++) Parity: If the parity is enabled, then the MSB bit of the data written
  207. in the data register is transmitted but is changed by the parity bit.
  208. Depending on the frame length defined by the M bit (8-bits or 9-bits),
  209. please refer to Reference manual for possible USART frame formats.
  210. (++) USART polarity
  211. (++) USART phase
  212. (++) USART LastBit
  213. (++) Receiver/transmitter modes
  214. [..]
  215. The HAL_USART_Init() function follows the USART synchronous configuration
  216. procedure (details for the procedure are available in reference manuals
  217. (RM0008 for STM32F10Xxx MCUs and RM0041 for STM32F100xx MCUs)).
  218. @endverbatim
  219. * @{
  220. */
  221. /**
  222. * @brief Initializes the USART mode according to the specified
  223. * parameters in the USART_InitTypeDef and create the associated handle.
  224. * @param husart: pointer to a USART_HandleTypeDef structure that contains
  225. * the configuration information for the specified USART module.
  226. * @retval HAL status
  227. */
  228. HAL_StatusTypeDef HAL_USART_Init(USART_HandleTypeDef *husart)
  229. {
  230. /* Check the USART handle allocation */
  231. if(husart == NULL)
  232. {
  233. return HAL_ERROR;
  234. }
  235. /* Check the parameters */
  236. assert_param(IS_USART_INSTANCE(husart->Instance));
  237. if(husart->State == HAL_USART_STATE_RESET)
  238. {
  239. /* Allocate lock resource and initialize it */
  240. husart->Lock = HAL_UNLOCKED;
  241. /* Init the low level hardware */
  242. HAL_USART_MspInit(husart);
  243. }
  244. husart->State = HAL_USART_STATE_BUSY;
  245. /* Set the USART Communication parameters */
  246. USART_SetConfig(husart);
  247. /* In USART mode, the following bits must be kept cleared:
  248. - LINEN bit in the USART_CR2 register
  249. - HDSEL, SCEN and IREN bits in the USART_CR3 register */
  250. CLEAR_BIT(husart->Instance->CR2, USART_CR2_LINEN);
  251. CLEAR_BIT(husart->Instance->CR3, (USART_CR3_SCEN | USART_CR3_HDSEL | USART_CR3_IREN));
  252. /* Enable the Peripheral */
  253. __HAL_USART_ENABLE(husart);
  254. /* Initialize the USART state */
  255. husart->ErrorCode = HAL_USART_ERROR_NONE;
  256. husart->State= HAL_USART_STATE_READY;
  257. return HAL_OK;
  258. }
  259. /**
  260. * @brief DeInitializes the USART peripheral.
  261. * @param husart: pointer to a USART_HandleTypeDef structure that contains
  262. * the configuration information for the specified USART module.
  263. * @retval HAL status
  264. */
  265. HAL_StatusTypeDef HAL_USART_DeInit(USART_HandleTypeDef *husart)
  266. {
  267. /* Check the USART handle allocation */
  268. if(husart == NULL)
  269. {
  270. return HAL_ERROR;
  271. }
  272. /* Check the parameters */
  273. assert_param(IS_USART_INSTANCE(husart->Instance));
  274. husart->State = HAL_USART_STATE_BUSY;
  275. /* DeInit the low level hardware */
  276. HAL_USART_MspDeInit(husart);
  277. husart->ErrorCode = HAL_USART_ERROR_NONE;
  278. husart->State = HAL_USART_STATE_RESET;
  279. /* Release Lock */
  280. __HAL_UNLOCK(husart);
  281. return HAL_OK;
  282. }
  283. /**
  284. * @brief USART MSP Init.
  285. * @param husart: pointer to a USART_HandleTypeDef structure that contains
  286. * the configuration information for the specified USART module.
  287. * @retval None
  288. */
  289. __weak void HAL_USART_MspInit(USART_HandleTypeDef *husart)
  290. {
  291. /* Prevent unused argument(s) compilation warning */
  292. UNUSED(husart);
  293. /* NOTE: This function should not be modified, when the callback is needed,
  294. the HAL_USART_MspInit could be implemented in the user file
  295. */
  296. }
  297. /**
  298. * @brief USART MSP DeInit.
  299. * @param husart: pointer to a USART_HandleTypeDef structure that contains
  300. * the configuration information for the specified USART module.
  301. * @retval None
  302. */
  303. __weak void HAL_USART_MspDeInit(USART_HandleTypeDef *husart)
  304. {
  305. /* Prevent unused argument(s) compilation warning */
  306. UNUSED(husart);
  307. /* NOTE: This function should not be modified, when the callback is needed,
  308. the HAL_USART_MspDeInit could be implemented in the user file
  309. */
  310. }
  311. /**
  312. * @}
  313. */
  314. /** @defgroup USART_Exported_Functions_Group2 IO operation functions
  315. * @brief USART Transmit and Receive functions
  316. *
  317. @verbatim
  318. ==============================================================================
  319. ##### IO operation functions #####
  320. ==============================================================================
  321. [..]
  322. This subsection provides a set of functions allowing to manage the USART synchronous
  323. data transfers.
  324. [..]
  325. The USART supports master mode only: it cannot receive or send data related to an input
  326. clock (SCLK is always an output).
  327. (#) There are two modes of transfer:
  328. (++) Blocking mode: The communication is performed in polling mode.
  329. The HAL status of all data processing is returned by the same function
  330. after finishing transfer.
  331. (++) No-Blocking mode: The communication is performed using Interrupts
  332. or DMA, These API's return the HAL status.
  333. The end of the data processing will be indicated through the
  334. dedicated USART IRQ when using Interrupt mode or the DMA IRQ when
  335. using DMA mode.
  336. The HAL_USART_TxCpltCallback(), HAL_USART_RxCpltCallback() and HAL_USART_TxRxCpltCallback()
  337. user callbacks
  338. will be executed respectively at the end of the transmit or Receive process
  339. The HAL_USART_ErrorCallback() user callback will be executed when a communication
  340. error is detected
  341. (#) Blocking mode APIs are :
  342. (++) HAL_USART_Transmit() in simplex mode
  343. (++) HAL_USART_Receive() in full duplex receive only
  344. (++) HAL_USART_TransmitReceive() in full duplex mode
  345. (#) Non Blocking mode APIs with Interrupt are :
  346. (++) HAL_USART_Transmit_IT()in simplex mode
  347. (++) HAL_USART_Receive_IT() in full duplex receive only
  348. (++) HAL_USART_TransmitReceive_IT() in full duplex mode
  349. (++) HAL_USART_IRQHandler()
  350. (#) Non Blocking mode functions with DMA are :
  351. (++) HAL_USART_Transmit_DMA()in simplex mode
  352. (++) HAL_USART_Receive_DMA() in full duplex receive only
  353. (++) HAL_USART_TransmitReceive_DMA() in full duplex mode
  354. (++) HAL_USART_DMAPause()
  355. (++) HAL_USART_DMAResume()
  356. (++) HAL_USART_DMAStop()
  357. (#) A set of Transfer Complete Callbacks are provided in non Blocking mode:
  358. (++) HAL_USART_TxHalfCpltCallback()
  359. (++) HAL_USART_TxCpltCallback()
  360. (++) HAL_USART_RxHalfCpltCallback()
  361. (++) HAL_USART_RxCpltCallback()
  362. (++) HAL_USART_ErrorCallback()
  363. (++) HAL_USART_TxRxCpltCallback()
  364. @endverbatim
  365. * @{
  366. */
  367. /**
  368. * @brief Simplex Send an amount of data in blocking mode.
  369. * @param husart: pointer to a USART_HandleTypeDef structure that contains
  370. * the configuration information for the specified USART module.
  371. * @param pTxData: Pointer to data buffer
  372. * @param Size: Amount of data to be sent
  373. * @param Timeout: Timeout duration
  374. * @retval HAL status
  375. */
  376. HAL_StatusTypeDef HAL_USART_Transmit(USART_HandleTypeDef *husart, uint8_t *pTxData, uint16_t Size, uint32_t Timeout)
  377. {
  378. uint16_t* tmp;
  379. uint32_t tickstart = 0U;
  380. if(husart->State == HAL_USART_STATE_READY)
  381. {
  382. if((pTxData == NULL) || (Size == 0U))
  383. {
  384. return HAL_ERROR;
  385. }
  386. /* Process Locked */
  387. __HAL_LOCK(husart);
  388. husart->ErrorCode = HAL_USART_ERROR_NONE;
  389. husart->State = HAL_USART_STATE_BUSY_TX;
  390. /* Init tickstart for timeout managment */
  391. tickstart = HAL_GetTick();
  392. husart->TxXferSize = Size;
  393. husart->TxXferCount = Size;
  394. while(husart->TxXferCount > 0U)
  395. {
  396. husart->TxXferCount--;
  397. if(husart->Init.WordLength == USART_WORDLENGTH_9B)
  398. {
  399. /* Wait for TC flag in order to write data in DR */
  400. if(USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_TXE, RESET, tickstart, Timeout) != HAL_OK)
  401. {
  402. return HAL_TIMEOUT;
  403. }
  404. tmp = (uint16_t*) pTxData;
  405. WRITE_REG(husart->Instance->DR, (*tmp & (uint16_t)0x01FF));
  406. if(husart->Init.Parity == USART_PARITY_NONE)
  407. {
  408. pTxData += 2U;
  409. }
  410. else
  411. {
  412. pTxData += 1U;
  413. }
  414. }
  415. else
  416. {
  417. if(USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_TXE, RESET, tickstart, Timeout) != HAL_OK)
  418. {
  419. return HAL_TIMEOUT;
  420. }
  421. WRITE_REG(husart->Instance->DR, (*pTxData++ & (uint8_t)0xFF));
  422. }
  423. }
  424. if(USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_TC, RESET, tickstart, Timeout) != HAL_OK)
  425. {
  426. return HAL_TIMEOUT;
  427. }
  428. husart->State = HAL_USART_STATE_READY;
  429. /* Process Unlocked */
  430. __HAL_UNLOCK(husart);
  431. return HAL_OK;
  432. }
  433. else
  434. {
  435. return HAL_BUSY;
  436. }
  437. }
  438. /**
  439. * @brief Full-Duplex Receive an amount of data in blocking mode.
  440. * @param husart: pointer to a USART_HandleTypeDef structure that contains
  441. * the configuration information for the specified USART module.
  442. * @param pRxData: Pointer to data buffer
  443. * @param Size: Amount of data to be received
  444. * @param Timeout: Timeout duration
  445. * @retval HAL status
  446. */
  447. HAL_StatusTypeDef HAL_USART_Receive(USART_HandleTypeDef *husart, uint8_t *pRxData, uint16_t Size, uint32_t Timeout)
  448. {
  449. uint16_t* tmp;
  450. uint32_t tickstart = 0U;
  451. if(husart->State == HAL_USART_STATE_READY)
  452. {
  453. if((pRxData == NULL) || (Size == 0U))
  454. {
  455. return HAL_ERROR;
  456. }
  457. /* Process Locked */
  458. __HAL_LOCK(husart);
  459. husart->ErrorCode = HAL_USART_ERROR_NONE;
  460. husart->State = HAL_USART_STATE_BUSY_RX;
  461. /* Init tickstart for timeout managment */
  462. tickstart = HAL_GetTick();
  463. husart->RxXferSize = Size;
  464. husart->RxXferCount = Size;
  465. /* Check the remain data to be received */
  466. while(husart->RxXferCount > 0U)
  467. {
  468. husart->RxXferCount--;
  469. if(husart->Init.WordLength == USART_WORDLENGTH_9B)
  470. {
  471. /* Wait until TXE flag is set to send dummy byte in order to generate the clock for the slave to send data */
  472. if(USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_TXE, RESET, tickstart, Timeout) != HAL_OK)
  473. {
  474. return HAL_TIMEOUT;
  475. }
  476. /* Send dummy byte in order to generate clock */
  477. WRITE_REG(husart->Instance->DR, (DUMMY_DATA & (uint16_t)0x01FF));
  478. /* Wait for RXNE Flag */
  479. if(USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_RXNE, RESET, tickstart, Timeout) != HAL_OK)
  480. {
  481. return HAL_TIMEOUT;
  482. }
  483. tmp = (uint16_t*) pRxData ;
  484. if(husart->Init.Parity == USART_PARITY_NONE)
  485. {
  486. *tmp = (uint16_t)(husart->Instance->DR & (uint16_t)0x01FF);
  487. pRxData +=2U;
  488. }
  489. else
  490. {
  491. *tmp = (uint16_t)(husart->Instance->DR & (uint16_t)0x00FF);
  492. pRxData +=1U;
  493. }
  494. }
  495. else
  496. {
  497. /* Wait until TXE flag is set to send dummy byte in order to generate the clock for the slave to send data */
  498. if(USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_TXE, RESET, tickstart, Timeout) != HAL_OK)
  499. {
  500. return HAL_TIMEOUT;
  501. }
  502. /* Send Dummy Byte in order to generate clock */
  503. WRITE_REG(husart->Instance->DR, (DUMMY_DATA & (uint16_t)0x00FF));
  504. /* Wait until RXNE flag is set to receive the byte */
  505. if(USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_RXNE, RESET, tickstart, Timeout) != HAL_OK)
  506. {
  507. return HAL_TIMEOUT;
  508. }
  509. if(husart->Init.Parity == USART_PARITY_NONE)
  510. {
  511. /* Receive data */
  512. *pRxData++ = (uint8_t)(husart->Instance->DR & (uint8_t)0x00FF);
  513. }
  514. else
  515. {
  516. /* Receive data */
  517. *pRxData++ = (uint8_t)(husart->Instance->DR & (uint8_t)0x007F);
  518. }
  519. }
  520. }
  521. husart->State = HAL_USART_STATE_READY;
  522. /* Process Unlocked */
  523. __HAL_UNLOCK(husart);
  524. return HAL_OK;
  525. }
  526. else
  527. {
  528. return HAL_BUSY;
  529. }
  530. }
  531. /**
  532. * @brief Full-Duplex Send receive an amount of data in full-duplex mode (blocking mode).
  533. * @param husart: pointer to a USART_HandleTypeDef structure that contains
  534. * the configuration information for the specified USART module.
  535. * @param pTxData: Pointer to data transmitted buffer
  536. * @param pRxData: Pointer to data received buffer
  537. * @param Size: Amount of data to be sent
  538. * @param Timeout: Timeout duration
  539. * @retval HAL status
  540. */
  541. HAL_StatusTypeDef HAL_USART_TransmitReceive(USART_HandleTypeDef *husart, uint8_t *pTxData, uint8_t *pRxData, uint16_t Size, uint32_t Timeout)
  542. {
  543. uint16_t* tmp;
  544. uint32_t tickstart = 0U;
  545. if(husart->State == HAL_USART_STATE_READY)
  546. {
  547. if((pTxData == NULL) || (pRxData == NULL) || (Size == 0U))
  548. {
  549. return HAL_ERROR;
  550. }
  551. /* Process Locked */
  552. __HAL_LOCK(husart);
  553. husart->ErrorCode = HAL_USART_ERROR_NONE;
  554. husart->State = HAL_USART_STATE_BUSY_RX;
  555. /* Init tickstart for timeout managment */
  556. tickstart = HAL_GetTick();
  557. husart->RxXferSize = Size;
  558. husart->TxXferSize = Size;
  559. husart->TxXferCount = Size;
  560. husart->RxXferCount = Size;
  561. /* Check the remain data to be received */
  562. while(husart->TxXferCount > 0U)
  563. {
  564. husart->TxXferCount--;
  565. husart->RxXferCount--;
  566. if(husart->Init.WordLength == USART_WORDLENGTH_9B)
  567. {
  568. /* Wait for TC flag in order to write data in DR */
  569. if(USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_TXE, RESET, tickstart, Timeout) != HAL_OK)
  570. {
  571. return HAL_TIMEOUT;
  572. }
  573. tmp = (uint16_t*) pTxData;
  574. WRITE_REG(husart->Instance->DR, (*tmp & (uint16_t)0x01FF));
  575. if(husart->Init.Parity == USART_PARITY_NONE)
  576. {
  577. pTxData += 2U;
  578. }
  579. else
  580. {
  581. pTxData += 1U;
  582. }
  583. /* Wait for RXNE Flag */
  584. if(USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_RXNE, RESET, tickstart, Timeout) != HAL_OK)
  585. {
  586. return HAL_TIMEOUT;
  587. }
  588. tmp = (uint16_t*) pRxData ;
  589. if(husart->Init.Parity == USART_PARITY_NONE)
  590. {
  591. *tmp = (uint16_t)(husart->Instance->DR & (uint16_t)0x01FF);
  592. pRxData += 2U;
  593. }
  594. else
  595. {
  596. *tmp = (uint16_t)(husart->Instance->DR & (uint16_t)0x00FF);
  597. pRxData += 1U;
  598. }
  599. }
  600. else
  601. {
  602. /* Wait for TC flag in order to write data in DR */
  603. if(USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_TXE, RESET, tickstart, Timeout) != HAL_OK)
  604. {
  605. return HAL_TIMEOUT;
  606. }
  607. WRITE_REG(husart->Instance->DR, (*pTxData++ & (uint8_t)0x00FF));
  608. /* Wait for RXNE Flag */
  609. if(USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_RXNE, RESET, tickstart, Timeout) != HAL_OK)
  610. {
  611. return HAL_TIMEOUT;
  612. }
  613. if(husart->Init.Parity == USART_PARITY_NONE)
  614. {
  615. /* Receive data */
  616. *pRxData++ = (uint8_t)(husart->Instance->DR & (uint8_t)0x00FF);
  617. }
  618. else
  619. {
  620. /* Receive data */
  621. *pRxData++ = (uint8_t)(husart->Instance->DR & (uint8_t)0x007F);
  622. }
  623. }
  624. }
  625. husart->State = HAL_USART_STATE_READY;
  626. /* Process Unlocked */
  627. __HAL_UNLOCK(husart);
  628. return HAL_OK;
  629. }
  630. else
  631. {
  632. return HAL_BUSY;
  633. }
  634. }
  635. /**
  636. * @brief Simplex Send an amount of data in non-blocking mode.
  637. * @param husart: pointer to a USART_HandleTypeDef structure that contains
  638. * the configuration information for the specified USART module.
  639. * @param pTxData: Pointer to data buffer
  640. * @param Size: Amount of data to be sent
  641. * @retval HAL status
  642. * @note The USART errors are not managed to avoid the overrun error.
  643. */
  644. HAL_StatusTypeDef HAL_USART_Transmit_IT(USART_HandleTypeDef *husart, uint8_t *pTxData, uint16_t Size)
  645. {
  646. /* Check that a Tx process is not already ongoing */
  647. if(husart->State == HAL_USART_STATE_READY)
  648. {
  649. if((pTxData == NULL) || (Size == 0U))
  650. {
  651. return HAL_ERROR;
  652. }
  653. /* Process Locked */
  654. __HAL_LOCK(husart);
  655. husart->pTxBuffPtr = pTxData;
  656. husart->TxXferSize = Size;
  657. husart->TxXferCount = Size;
  658. husart->ErrorCode = HAL_USART_ERROR_NONE;
  659. husart->State = HAL_USART_STATE_BUSY_TX;
  660. /* The USART Error Interrupts: (Frame error, Noise error, Overrun error)
  661. are not managed by the USART transmit process to avoid the overrun interrupt
  662. when the USART mode is configured for transmit and receive "USART_MODE_TX_RX"
  663. to benefit for the frame error and noise interrupts the USART mode should be
  664. configured only for transmit "USART_MODE_TX"
  665. The __HAL_USART_ENABLE_IT(husart, USART_IT_ERR) can be used to enable the Frame error,
  666. Noise error interrupt */
  667. /* Process Unlocked */
  668. __HAL_UNLOCK(husart);
  669. /* Enable the USART Transmit Data Register Empty Interrupt */
  670. __HAL_USART_ENABLE_IT(husart, USART_IT_TXE);
  671. return HAL_OK;
  672. }
  673. else
  674. {
  675. return HAL_BUSY;
  676. }
  677. }
  678. /**
  679. * @brief Simplex Receive an amount of data in non-blocking mode.
  680. * @param husart: pointer to a USART_HandleTypeDef structure that contains
  681. * the configuration information for the specified USART module.
  682. * @param pRxData: Pointer to data buffer
  683. * @param Size: Amount of data to be received
  684. * @retval HAL status
  685. */
  686. HAL_StatusTypeDef HAL_USART_Receive_IT(USART_HandleTypeDef *husart, uint8_t *pRxData, uint16_t Size)
  687. {
  688. if(husart->State == HAL_USART_STATE_READY)
  689. {
  690. if((pRxData == NULL) || (Size == 0U))
  691. {
  692. return HAL_ERROR;
  693. }
  694. /* Process Locked */
  695. __HAL_LOCK(husart);
  696. husart->pRxBuffPtr = pRxData;
  697. husart->RxXferSize = Size;
  698. husart->RxXferCount = Size;
  699. husart->ErrorCode = HAL_USART_ERROR_NONE;
  700. husart->State = HAL_USART_STATE_BUSY_RX;
  701. /* Process Unlocked */
  702. __HAL_UNLOCK(husart);
  703. /* Enable the USART Data Register not empty Interrupt */
  704. __HAL_USART_ENABLE_IT(husart, USART_IT_RXNE);
  705. /* Enable the USART Parity Error Interrupt */
  706. __HAL_USART_ENABLE_IT(husart, USART_IT_PE);
  707. /* Enable the USART Error Interrupt: (Frame error, noise error, overrun error) */
  708. __HAL_USART_ENABLE_IT(husart, USART_IT_ERR);
  709. /* Send dummy byte in order to generate the clock for the slave to send data */
  710. WRITE_REG(husart->Instance->DR, (DUMMY_DATA & (uint16_t)0x01FF));
  711. return HAL_OK;
  712. }
  713. else
  714. {
  715. return HAL_BUSY;
  716. }
  717. }
  718. /**
  719. * @brief Full-Duplex Send receive an amount of data in full-duplex mode (non-blocking).
  720. * @param husart: pointer to a USART_HandleTypeDef structure that contains
  721. * the configuration information for the specified USART module.
  722. * @param pTxData: Pointer to data transmitted buffer
  723. * @param pRxData: Pointer to data received buffer
  724. * @param Size: Amount of data to be received
  725. * @retval HAL status
  726. */
  727. HAL_StatusTypeDef HAL_USART_TransmitReceive_IT(USART_HandleTypeDef *husart, uint8_t *pTxData, uint8_t *pRxData, uint16_t Size)
  728. {
  729. if(husart->State == HAL_USART_STATE_READY)
  730. {
  731. if((pTxData == NULL) || (pRxData == NULL) || (Size == 0U))
  732. {
  733. return HAL_ERROR;
  734. }
  735. /* Process Locked */
  736. __HAL_LOCK(husart);
  737. husart->pRxBuffPtr = pRxData;
  738. husart->RxXferSize = Size;
  739. husart->RxXferCount = Size;
  740. husart->pTxBuffPtr = pTxData;
  741. husart->TxXferSize = Size;
  742. husart->TxXferCount = Size;
  743. husart->ErrorCode = HAL_USART_ERROR_NONE;
  744. husart->State = HAL_USART_STATE_BUSY_TX_RX;
  745. /* Process Unlocked */
  746. __HAL_UNLOCK(husart);
  747. /* Enable the USART Data Register not empty Interrupt */
  748. __HAL_USART_ENABLE_IT(husart, USART_IT_RXNE);
  749. /* Enable the USART Parity Error Interrupt */
  750. __HAL_USART_ENABLE_IT(husart, USART_IT_PE);
  751. /* Enable the USART Error Interrupt: (Frame error, noise error, overrun error) */
  752. __HAL_USART_ENABLE_IT(husart, USART_IT_ERR);
  753. /* Enable the USART Transmit Data Register Empty Interrupt */
  754. __HAL_USART_ENABLE_IT(husart, USART_IT_TXE);
  755. return HAL_OK;
  756. }
  757. else
  758. {
  759. return HAL_BUSY;
  760. }
  761. }
  762. /**
  763. * @brief Simplex Send an amount of data in non-blocking mode.
  764. * @param husart: pointer to a USART_HandleTypeDef structure that contains
  765. * the configuration information for the specified USART module.
  766. * @param pTxData: Pointer to data buffer
  767. * @param Size: Amount of data to be sent
  768. * @retval HAL status
  769. */
  770. HAL_StatusTypeDef HAL_USART_Transmit_DMA(USART_HandleTypeDef *husart, uint8_t *pTxData, uint16_t Size)
  771. {
  772. uint32_t *tmp;
  773. if(husart->State == HAL_USART_STATE_READY)
  774. {
  775. if((pTxData == NULL) || (Size == 0U))
  776. {
  777. return HAL_ERROR;
  778. }
  779. /* Process Locked */
  780. __HAL_LOCK(husart);
  781. husart->pTxBuffPtr = pTxData;
  782. husart->TxXferSize = Size;
  783. husart->TxXferCount = Size;
  784. husart->ErrorCode = HAL_USART_ERROR_NONE;
  785. husart->State = HAL_USART_STATE_BUSY_TX;
  786. /* Set the USART DMA transfer complete callback */
  787. husart->hdmatx->XferCpltCallback = USART_DMATransmitCplt;
  788. /* Set the USART DMA Half transfer complete callback */
  789. husart->hdmatx->XferHalfCpltCallback = USART_DMATxHalfCplt;
  790. /* Set the DMA error callback */
  791. husart->hdmatx->XferErrorCallback = USART_DMAError;
  792. /* Set the DMA abort callback */
  793. husart->hdmatx->XferAbortCallback = NULL;
  794. /* Enable the USART transmit DMA channel */
  795. tmp = (uint32_t*)&pTxData;
  796. HAL_DMA_Start_IT(husart->hdmatx, *(uint32_t*)tmp, (uint32_t)&husart->Instance->DR, Size);
  797. /* Clear the TC flag in the SR register by writing 0 to it */
  798. __HAL_USART_CLEAR_FLAG(husart, USART_FLAG_TC);
  799. /* Process Unlocked */
  800. __HAL_UNLOCK(husart);
  801. /* Enable the DMA transfer for transmit request by setting the DMAT bit
  802. in the USART CR3 register */
  803. SET_BIT(husart->Instance->CR3, USART_CR3_DMAT);
  804. return HAL_OK;
  805. }
  806. else
  807. {
  808. return HAL_BUSY;
  809. }
  810. }
  811. /**
  812. * @brief Full-Duplex Receive an amount of data in non-blocking mode.
  813. * @param husart: pointer to a USART_HandleTypeDef structure that contains
  814. * the configuration information for the specified USART module.
  815. * @param pRxData: Pointer to data buffer
  816. * @param Size: Amount of data to be received
  817. * @retval HAL status
  818. * @note The USART DMA transmit channel must be configured in order to generate the clock for the slave.
  819. * @note When the USART parity is enabled (PCE = 1) the data received contain the parity bit.
  820. */
  821. HAL_StatusTypeDef HAL_USART_Receive_DMA(USART_HandleTypeDef *husart, uint8_t *pRxData, uint16_t Size)
  822. {
  823. uint32_t *tmp;
  824. if(husart->State == HAL_USART_STATE_READY)
  825. {
  826. if((pRxData == NULL) || (Size == 0U))
  827. {
  828. return HAL_ERROR;
  829. }
  830. /* Process Locked */
  831. __HAL_LOCK(husart);
  832. husart->pRxBuffPtr = pRxData;
  833. husart->RxXferSize = Size;
  834. husart->pTxBuffPtr = pRxData;
  835. husart->TxXferSize = Size;
  836. husart->ErrorCode = HAL_USART_ERROR_NONE;
  837. husart->State = HAL_USART_STATE_BUSY_RX;
  838. /* Set the USART DMA Rx transfer complete callback */
  839. husart->hdmarx->XferCpltCallback = USART_DMAReceiveCplt;
  840. /* Set the USART DMA Half transfer complete callback */
  841. husart->hdmarx->XferHalfCpltCallback = USART_DMARxHalfCplt;
  842. /* Set the USART DMA Rx transfer error callback */
  843. husart->hdmarx->XferErrorCallback = USART_DMAError;
  844. /* Set the DMA abort callback */
  845. husart->hdmarx->XferAbortCallback = NULL;
  846. /* Set the USART Tx DMA transfer complete callback as NULL because the communication closing
  847. is performed in DMA reception complete callback */
  848. husart->hdmatx->XferHalfCpltCallback = NULL;
  849. husart->hdmatx->XferCpltCallback = NULL;
  850. /* Set the DMA error callback */
  851. husart->hdmatx->XferErrorCallback = USART_DMAError;
  852. /* Set the DMA AbortCpltCallback */
  853. husart->hdmatx->XferAbortCallback = NULL;
  854. /* Enable the USART receive DMA channel */
  855. tmp = (uint32_t*)&pRxData;
  856. HAL_DMA_Start_IT(husart->hdmarx, (uint32_t)&husart->Instance->DR, *(uint32_t*)tmp, Size);
  857. /* Enable the USART transmit DMA channel: the transmit channel is used in order
  858. to generate in the non-blocking mode the clock to the slave device,
  859. this mode isn't a simplex receive mode but a full-duplex receive one */
  860. HAL_DMA_Start_IT(husart->hdmatx, *(uint32_t*)tmp, (uint32_t)&husart->Instance->DR, Size);
  861. /* Clear the Overrun flag just before enabling the DMA Rx request: mandatory for the second transfer */
  862. __HAL_USART_CLEAR_OREFLAG(husart);
  863. /* Process Unlocked */
  864. __HAL_UNLOCK(husart);
  865. /* Enable the USART Parity Error Interrupt */
  866. SET_BIT(husart->Instance->CR1, USART_CR1_PEIE);
  867. /* Enable the USART Error Interrupt: (Frame error, noise error, overrun error) */
  868. SET_BIT(husart->Instance->CR3, USART_CR3_EIE);
  869. /* Enable the DMA transfer for the receiver request by setting the DMAR bit
  870. in the USART CR3 register */
  871. SET_BIT(husart->Instance->CR3, USART_CR3_DMAR);
  872. /* Enable the DMA transfer for transmit request by setting the DMAT bit
  873. in the USART CR3 register */
  874. SET_BIT(husart->Instance->CR3, USART_CR3_DMAT);
  875. return HAL_OK;
  876. }
  877. else
  878. {
  879. return HAL_BUSY;
  880. }
  881. }
  882. /**
  883. * @brief Full-Duplex Transmit Receive an amount of data in non-blocking mode.
  884. * @param husart: pointer to a USART_HandleTypeDef structure that contains
  885. * the configuration information for the specified USART module.
  886. * @param pTxData: Pointer to data transmitted buffer
  887. * @param pRxData: Pointer to data received buffer
  888. * @param Size: Amount of data to be received
  889. * @note When the USART parity is enabled (PCE = 1) the data received contain the parity bit.
  890. * @retval HAL status
  891. */
  892. HAL_StatusTypeDef HAL_USART_TransmitReceive_DMA(USART_HandleTypeDef *husart, uint8_t *pTxData, uint8_t *pRxData, uint16_t Size)
  893. {
  894. uint32_t *tmp;
  895. if(husart->State == HAL_USART_STATE_READY)
  896. {
  897. if((pTxData == NULL) || (pRxData == NULL) || (Size == 0U))
  898. {
  899. return HAL_ERROR;
  900. }
  901. /* Process Locked */
  902. __HAL_LOCK(husart);
  903. husart->pRxBuffPtr = pRxData;
  904. husart->RxXferSize = Size;
  905. husart->pTxBuffPtr = pTxData;
  906. husart->TxXferSize = Size;
  907. husart->ErrorCode = HAL_USART_ERROR_NONE;
  908. husart->State = HAL_USART_STATE_BUSY_TX_RX;
  909. /* Set the USART DMA Rx transfer complete callback */
  910. husart->hdmarx->XferCpltCallback = USART_DMAReceiveCplt;
  911. /* Set the USART DMA Half transfer complete callback */
  912. husart->hdmarx->XferHalfCpltCallback = USART_DMARxHalfCplt;
  913. /* Set the USART DMA Tx transfer complete callback */
  914. husart->hdmatx->XferCpltCallback = USART_DMATransmitCplt;
  915. /* Set the USART DMA Half transfer complete callback */
  916. husart->hdmatx->XferHalfCpltCallback = USART_DMATxHalfCplt;
  917. /* Set the USART DMA Tx transfer error callback */
  918. husart->hdmatx->XferErrorCallback = USART_DMAError;
  919. /* Set the USART DMA Rx transfer error callback */
  920. husart->hdmarx->XferErrorCallback = USART_DMAError;
  921. /* Set the DMA abort callback */
  922. husart->hdmarx->XferAbortCallback = NULL;
  923. /* Enable the USART receive DMA channel */
  924. tmp = (uint32_t*)&pRxData;
  925. HAL_DMA_Start_IT(husart->hdmarx, (uint32_t)&husart->Instance->DR, *(uint32_t*)tmp, Size);
  926. /* Enable the USART transmit DMA channel */
  927. tmp = (uint32_t*)&pTxData;
  928. HAL_DMA_Start_IT(husart->hdmatx, *(uint32_t*)tmp, (uint32_t)&husart->Instance->DR, Size);
  929. /* Clear the TC flag in the SR register by writing 0 to it */
  930. __HAL_USART_CLEAR_FLAG(husart, USART_FLAG_TC);
  931. /* Clear the Overrun flag: mandatory for the second transfer in circular mode */
  932. __HAL_USART_CLEAR_OREFLAG(husart);
  933. /* Process Unlocked */
  934. __HAL_UNLOCK(husart);
  935. /* Enable the USART Parity Error Interrupt */
  936. SET_BIT(husart->Instance->CR1, USART_CR1_PEIE);
  937. /* Enable the USART Error Interrupt: (Frame error, noise error, overrun error) */
  938. SET_BIT(husart->Instance->CR3, USART_CR3_EIE);
  939. /* Enable the DMA transfer for the receiver request by setting the DMAR bit
  940. in the USART CR3 register */
  941. SET_BIT(husart->Instance->CR3, USART_CR3_DMAR);
  942. /* Enable the DMA transfer for transmit request by setting the DMAT bit
  943. in the USART CR3 register */
  944. SET_BIT(husart->Instance->CR3, USART_CR3_DMAT);
  945. return HAL_OK;
  946. }
  947. else
  948. {
  949. return HAL_BUSY;
  950. }
  951. }
  952. /**
  953. * @brief Pauses the DMA Transfer.
  954. * @param husart: pointer to a USART_HandleTypeDef structure that contains
  955. * the configuration information for the specified USART module.
  956. * @retval HAL status
  957. */
  958. HAL_StatusTypeDef HAL_USART_DMAPause(USART_HandleTypeDef *husart)
  959. {
  960. /* Process Locked */
  961. __HAL_LOCK(husart);
  962. /* Disable the USART DMA Tx request */
  963. CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAT);
  964. /* Process Unlocked */
  965. __HAL_UNLOCK(husart);
  966. return HAL_OK;
  967. }
  968. /**
  969. * @brief Resumes the DMA Transfer.
  970. * @param husart: pointer to a USART_HandleTypeDef structure that contains
  971. * the configuration information for the specified USART module.
  972. * @retval HAL status
  973. */
  974. HAL_StatusTypeDef HAL_USART_DMAResume(USART_HandleTypeDef *husart)
  975. {
  976. /* Process Locked */
  977. __HAL_LOCK(husart);
  978. /* Enable the USART DMA Tx request */
  979. SET_BIT(husart->Instance->CR3, USART_CR3_DMAT);
  980. /* Process Unlocked */
  981. __HAL_UNLOCK(husart);
  982. return HAL_OK;
  983. }
  984. /**
  985. * @brief Stops the DMA Transfer.
  986. * @param husart: pointer to a USART_HandleTypeDef structure that contains
  987. * the configuration information for the specified USART module.
  988. * @retval HAL status
  989. */
  990. HAL_StatusTypeDef HAL_USART_DMAStop(USART_HandleTypeDef *husart)
  991. {
  992. uint32_t dmarequest = 0x00U;
  993. /* The Lock is not implemented on this API to allow the user application
  994. to call the HAL USART API under callbacks HAL_USART_TxCpltCallback() / HAL_USART_RxCpltCallback():
  995. when calling HAL_DMA_Abort() API the DMA TX/RX Transfer complete interrupt is generated
  996. and the correspond call back is executed HAL_USART_TxCpltCallback() / HAL_USART_RxCpltCallback()
  997. */
  998. /* Stop USART DMA Tx request if ongoing */
  999. dmarequest = HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAT);
  1000. if((husart->State == HAL_USART_STATE_BUSY_TX) && dmarequest)
  1001. {
  1002. USART_EndTxTransfer(husart);
  1003. /* Abort the USART DMA Tx channel */
  1004. if(husart->hdmatx != NULL)
  1005. {
  1006. HAL_DMA_Abort(husart->hdmatx);
  1007. }
  1008. /* Disable the USART Tx DMA request */
  1009. CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAT);
  1010. }
  1011. /* Stop USART DMA Rx request if ongoing */
  1012. dmarequest = HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAR);
  1013. if((husart->State == HAL_USART_STATE_BUSY_RX) && dmarequest)
  1014. {
  1015. USART_EndRxTransfer(husart);
  1016. /* Abort the USART DMA Rx channel */
  1017. if(husart->hdmarx != NULL)
  1018. {
  1019. HAL_DMA_Abort(husart->hdmarx);
  1020. }
  1021. /* Disable the USART Rx DMA request */
  1022. CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAR);
  1023. }
  1024. return HAL_OK;
  1025. }
  1026. /**
  1027. * @brief Abort ongoing transfer (blocking mode).
  1028. * @param husart USART handle.
  1029. * @note This procedure could be used for aborting any ongoing transfer (either Tx or Rx,
  1030. * as described by TransferType parameter) started in Interrupt or DMA mode.
  1031. * This procedure performs following operations :
  1032. * - Disable PPP Interrupts (depending of transfer direction)
  1033. * - Disable the DMA transfer in the peripheral register (if enabled)
  1034. * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
  1035. * - Set handle State to READY
  1036. * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
  1037. * @retval HAL status
  1038. */
  1039. HAL_StatusTypeDef HAL_USART_Abort(USART_HandleTypeDef *husart)
  1040. {
  1041. /* Disable TXEIE, TCIE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
  1042. CLEAR_BIT(husart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE | USART_CR1_TXEIE | USART_CR1_TCIE));
  1043. CLEAR_BIT(husart->Instance->CR3, USART_CR3_EIE);
  1044. /* Disable the USART DMA Tx request if enabled */
  1045. if(HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAT))
  1046. {
  1047. CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAT);
  1048. /* Abort the USART DMA Tx channel : use blocking DMA Abort API (no callback) */
  1049. if(husart->hdmatx != NULL)
  1050. {
  1051. /* Set the USART DMA Abort callback to Null.
  1052. No call back execution at end of DMA abort procedure */
  1053. husart->hdmatx->XferAbortCallback = NULL;
  1054. HAL_DMA_Abort(husart->hdmatx);
  1055. }
  1056. }
  1057. /* Disable the USART DMA Rx request if enabled */
  1058. if(HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAR))
  1059. {
  1060. CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAR);
  1061. /* Abort the USART DMA Rx channel : use blocking DMA Abort API (no callback) */
  1062. if(husart->hdmarx != NULL)
  1063. {
  1064. /* Set the USART DMA Abort callback to Null.
  1065. No call back execution at end of DMA abort procedure */
  1066. husart->hdmarx->XferAbortCallback = NULL;
  1067. HAL_DMA_Abort(husart->hdmarx);
  1068. }
  1069. }
  1070. /* Reset Tx and Rx transfer counters */
  1071. husart->TxXferCount = 0x00U;
  1072. husart->RxXferCount = 0x00U;
  1073. /* Restore husart->State to Ready */
  1074. husart->State = HAL_USART_STATE_READY;
  1075. /* Reset Handle ErrorCode to No Error */
  1076. husart->ErrorCode = HAL_USART_ERROR_NONE;
  1077. return HAL_OK;
  1078. }
  1079. /**
  1080. * @brief Abort ongoing transfer (Interrupt mode).
  1081. * @param husart USART handle.
  1082. * @note This procedure could be used for aborting any ongoing transfer (either Tx or Rx,
  1083. * as described by TransferType parameter) started in Interrupt or DMA mode.
  1084. * This procedure performs following operations :
  1085. * - Disable PPP Interrupts (depending of transfer direction)
  1086. * - Disable the DMA transfer in the peripheral register (if enabled)
  1087. * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
  1088. * - Set handle State to READY
  1089. * - At abort completion, call user abort complete callback
  1090. * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be
  1091. * considered as completed only when user abort complete callback is executed (not when exiting function).
  1092. * @retval HAL status
  1093. */
  1094. HAL_StatusTypeDef HAL_USART_Abort_IT(USART_HandleTypeDef *husart)
  1095. {
  1096. uint32_t AbortCplt = 0x01U;
  1097. /* Disable TXEIE, TCIE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
  1098. CLEAR_BIT(husart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE | USART_CR1_TXEIE | USART_CR1_TCIE));
  1099. CLEAR_BIT(husart->Instance->CR3, USART_CR3_EIE);
  1100. /* If DMA Tx and/or DMA Rx Handles are associated to USART Handle, DMA Abort complete callbacks should be initialised
  1101. before any call to DMA Abort functions */
  1102. /* DMA Tx Handle is valid */
  1103. if(husart->hdmatx != NULL)
  1104. {
  1105. /* Set DMA Abort Complete callback if USART DMA Tx request if enabled.
  1106. Otherwise, set it to NULL */
  1107. if(HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAT))
  1108. {
  1109. husart->hdmatx->XferAbortCallback = USART_DMATxAbortCallback;
  1110. }
  1111. else
  1112. {
  1113. husart->hdmatx->XferAbortCallback = NULL;
  1114. }
  1115. }
  1116. /* DMA Rx Handle is valid */
  1117. if(husart->hdmarx != NULL)
  1118. {
  1119. /* Set DMA Abort Complete callback if USART DMA Rx request if enabled.
  1120. Otherwise, set it to NULL */
  1121. if(HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAR))
  1122. {
  1123. husart->hdmarx->XferAbortCallback = USART_DMARxAbortCallback;
  1124. }
  1125. else
  1126. {
  1127. husart->hdmarx->XferAbortCallback = NULL;
  1128. }
  1129. }
  1130. /* Disable the USART DMA Tx request if enabled */
  1131. if(HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAT))
  1132. {
  1133. /* Disable DMA Tx at USART level */
  1134. CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAT);
  1135. /* Abort the USART DMA Tx channel : use non blocking DMA Abort API (callback) */
  1136. if(husart->hdmatx != NULL)
  1137. {
  1138. /* USART Tx DMA Abort callback has already been initialised :
  1139. will lead to call HAL_USART_AbortCpltCallback() at end of DMA abort procedure */
  1140. /* Abort DMA TX */
  1141. if(HAL_DMA_Abort_IT(husart->hdmatx) != HAL_OK)
  1142. {
  1143. husart->hdmatx->XferAbortCallback = NULL;
  1144. }
  1145. else
  1146. {
  1147. AbortCplt = 0x00U;
  1148. }
  1149. }
  1150. }
  1151. /* Disable the USART DMA Rx request if enabled */
  1152. if(HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAR))
  1153. {
  1154. CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAR);
  1155. /* Abort the USART DMA Rx channel : use non blocking DMA Abort API (callback) */
  1156. if(husart->hdmarx != NULL)
  1157. {
  1158. /* USART Rx DMA Abort callback has already been initialised :
  1159. will lead to call HAL_USART_AbortCpltCallback() at end of DMA abort procedure */
  1160. /* Abort DMA RX */
  1161. if(HAL_DMA_Abort_IT(husart->hdmarx) != HAL_OK)
  1162. {
  1163. husart->hdmarx->XferAbortCallback = NULL;
  1164. AbortCplt = 0x01U;
  1165. }
  1166. else
  1167. {
  1168. AbortCplt = 0x00U;
  1169. }
  1170. }
  1171. }
  1172. /* if no DMA abort complete callback execution is required => call user Abort Complete callback */
  1173. if(AbortCplt == 0x01U)
  1174. {
  1175. /* Reset Tx and Rx transfer counters */
  1176. husart->TxXferCount = 0x00U;
  1177. husart->RxXferCount = 0x00U;
  1178. /* Reset errorCode */
  1179. husart->ErrorCode = HAL_USART_ERROR_NONE;
  1180. /* Restore husart->State to Ready */
  1181. husart->State = HAL_USART_STATE_READY;
  1182. /* As no DMA to be aborted, call directly user Abort complete callback */
  1183. HAL_USART_AbortCpltCallback(husart);
  1184. }
  1185. return HAL_OK;
  1186. }
  1187. /**
  1188. * @brief This function handles USART interrupt request.
  1189. * @param husart: pointer to a USART_HandleTypeDef structure that contains
  1190. * the configuration information for the specified USART module.
  1191. * @retval None
  1192. */
  1193. void HAL_USART_IRQHandler(USART_HandleTypeDef *husart)
  1194. {
  1195. uint32_t isrflags = READ_REG(husart->Instance->SR);
  1196. uint32_t cr1its = READ_REG(husart->Instance->CR1);
  1197. uint32_t cr3its = READ_REG(husart->Instance->CR3);
  1198. uint32_t errorflags = 0x00U;
  1199. uint32_t dmarequest = 0x00U;
  1200. /* If no error occurs */
  1201. errorflags = (isrflags & (uint32_t)(USART_SR_PE | USART_SR_FE | USART_SR_ORE | USART_SR_NE));
  1202. if(errorflags == RESET)
  1203. {
  1204. /* USART in mode Receiver -------------------------------------------------*/
  1205. if(((isrflags & USART_SR_RXNE) != RESET) && ((cr1its & USART_CR1_RXNEIE) != RESET))
  1206. {
  1207. if(husart->State == HAL_USART_STATE_BUSY_RX)
  1208. {
  1209. USART_Receive_IT(husart);
  1210. }
  1211. else
  1212. {
  1213. USART_TransmitReceive_IT(husart);
  1214. }
  1215. return;
  1216. }
  1217. }
  1218. /* If some errors occur */
  1219. if((errorflags != RESET) && (((cr3its & USART_CR3_EIE) != RESET) || ((cr1its & (USART_CR1_RXNEIE | USART_CR1_PEIE)) != RESET)))
  1220. {
  1221. /* USART parity error interrupt occurred ----------------------------------*/
  1222. if(((isrflags & USART_SR_PE) != RESET) && ((cr1its & USART_CR1_PEIE) != RESET))
  1223. {
  1224. husart->ErrorCode |= HAL_USART_ERROR_PE;
  1225. }
  1226. /* USART noise error interrupt occurred --------------------------------*/
  1227. if(((isrflags & USART_SR_NE) != RESET) && ((cr3its & USART_CR3_EIE) != RESET))
  1228. {
  1229. husart->ErrorCode |= HAL_USART_ERROR_NE;
  1230. }
  1231. /* USART frame error interrupt occurred --------------------------------*/
  1232. if(((isrflags & USART_SR_FE) != RESET) && ((cr3its & USART_CR3_EIE) != RESET))
  1233. {
  1234. husart->ErrorCode |= HAL_USART_ERROR_FE;
  1235. }
  1236. /* USART Over-Run interrupt occurred -----------------------------------*/
  1237. if(((isrflags & USART_SR_ORE) != RESET) && ((cr3its & USART_CR3_EIE) != RESET))
  1238. {
  1239. husart->ErrorCode |= HAL_USART_ERROR_ORE;
  1240. }
  1241. if(husart->ErrorCode != HAL_USART_ERROR_NONE)
  1242. {
  1243. /* USART in mode Receiver -----------------------------------------------*/
  1244. if(((isrflags & USART_SR_RXNE) != RESET) && ((cr1its & USART_CR1_RXNEIE) != RESET))
  1245. {
  1246. if(husart->State == HAL_USART_STATE_BUSY_RX)
  1247. {
  1248. USART_Receive_IT(husart);
  1249. }
  1250. else
  1251. {
  1252. USART_TransmitReceive_IT(husart);
  1253. }
  1254. }
  1255. /* If Overrun error occurs, or if any error occurs in DMA mode reception,
  1256. consider error as blocking */
  1257. dmarequest = HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAR);
  1258. if(((husart->ErrorCode & HAL_USART_ERROR_ORE) != RESET) || dmarequest)
  1259. {
  1260. /* Set the USART state ready to be able to start again the process,
  1261. Disable Rx Interrupts, and disable Rx DMA request, if ongoing */
  1262. USART_EndRxTransfer(husart);
  1263. /* Disable the USART DMA Rx request if enabled */
  1264. if (HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAR))
  1265. {
  1266. CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAR);
  1267. /* Abort the USART DMA Rx channel */
  1268. if(husart->hdmarx != NULL)
  1269. {
  1270. /* Set the USART DMA Abort callback :
  1271. will lead to call HAL_USART_ErrorCallback() at end of DMA abort procedure */
  1272. husart->hdmarx->XferAbortCallback = USART_DMAAbortOnError;
  1273. if(HAL_DMA_Abort_IT(husart->hdmarx) != HAL_OK)
  1274. {
  1275. /* Call Directly XferAbortCallback function in case of error */
  1276. husart->hdmarx->XferAbortCallback(husart->hdmarx);
  1277. }
  1278. }
  1279. else
  1280. {
  1281. /* Call user error callback */
  1282. HAL_USART_ErrorCallback(husart);
  1283. }
  1284. }
  1285. else
  1286. {
  1287. /* Call user error callback */
  1288. HAL_USART_ErrorCallback(husart);
  1289. }
  1290. }
  1291. else
  1292. {
  1293. /* Call user error callback */
  1294. HAL_USART_ErrorCallback(husart);
  1295. husart->ErrorCode = HAL_USART_ERROR_NONE;
  1296. }
  1297. }
  1298. return;
  1299. }
  1300. /* USART in mode Transmitter -----------------------------------------------*/
  1301. if(((isrflags & USART_SR_TXE) != RESET) && ((cr1its & USART_CR1_TXEIE) != RESET))
  1302. {
  1303. if(husart->State == HAL_USART_STATE_BUSY_TX)
  1304. {
  1305. USART_Transmit_IT(husart);
  1306. }
  1307. else
  1308. {
  1309. USART_TransmitReceive_IT(husart);
  1310. }
  1311. return;
  1312. }
  1313. /* USART in mode Transmitter (transmission end) ----------------------------*/
  1314. if(((isrflags & USART_SR_TC) != RESET) && ((cr1its & USART_CR1_TCIE) != RESET))
  1315. {
  1316. USART_EndTransmit_IT(husart);
  1317. return;
  1318. }
  1319. }
  1320. /**
  1321. * @brief Tx Transfer completed callbacks.
  1322. * @param husart: pointer to a USART_HandleTypeDef structure that contains
  1323. * the configuration information for the specified USART module.
  1324. * @retval None
  1325. */
  1326. __weak void HAL_USART_TxCpltCallback(USART_HandleTypeDef *husart)
  1327. {
  1328. /* Prevent unused argument(s) compilation warning */
  1329. UNUSED(husart);
  1330. /* NOTE: This function should not be modified, when the callback is needed,
  1331. the HAL_USART_TxCpltCallback could be implemented in the user file
  1332. */
  1333. }
  1334. /**
  1335. * @brief Tx Half Transfer completed callbacks.
  1336. * @param husart: pointer to a USART_HandleTypeDef structure that contains
  1337. * the configuration information for the specified USART module.
  1338. * @retval None
  1339. */
  1340. __weak void HAL_USART_TxHalfCpltCallback(USART_HandleTypeDef *husart)
  1341. {
  1342. /* Prevent unused argument(s) compilation warning */
  1343. UNUSED(husart);
  1344. /* NOTE: This function should not be modified, when the callback is needed,
  1345. the HAL_USART_TxHalfCpltCallback could be implemented in the user file
  1346. */
  1347. }
  1348. /**
  1349. * @brief Rx Transfer completed callbacks.
  1350. * @param husart: pointer to a USART_HandleTypeDef structure that contains
  1351. * the configuration information for the specified USART module.
  1352. * @retval None
  1353. */
  1354. __weak void HAL_USART_RxCpltCallback(USART_HandleTypeDef *husart)
  1355. {
  1356. /* Prevent unused argument(s) compilation warning */
  1357. UNUSED(husart);
  1358. /* NOTE: This function should not be modified, when the callback is needed,
  1359. the HAL_USART_RxCpltCallback could be implemented in the user file
  1360. */
  1361. }
  1362. /**
  1363. * @brief Rx Half Transfer completed callbacks.
  1364. * @param husart: pointer to a USART_HandleTypeDef structure that contains
  1365. * the configuration information for the specified USART module.
  1366. * @retval None
  1367. */
  1368. __weak void HAL_USART_RxHalfCpltCallback(USART_HandleTypeDef *husart)
  1369. {
  1370. /* Prevent unused argument(s) compilation warning */
  1371. UNUSED(husart);
  1372. /* NOTE: This function should not be modified, when the callback is needed,
  1373. the HAL_USART_RxHalfCpltCallback could be implemented in the user file
  1374. */
  1375. }
  1376. /**
  1377. * @brief Tx/Rx Transfers completed callback for the non-blocking process.
  1378. * @param husart: pointer to a USART_HandleTypeDef structure that contains
  1379. * the configuration information for the specified USART module.
  1380. * @retval None
  1381. */
  1382. __weak void HAL_USART_TxRxCpltCallback(USART_HandleTypeDef *husart)
  1383. {
  1384. /* Prevent unused argument(s) compilation warning */
  1385. UNUSED(husart);
  1386. /* NOTE: This function should not be modified, when the callback is needed,
  1387. the HAL_USART_TxRxCpltCallback could be implemented in the user file
  1388. */
  1389. }
  1390. /**
  1391. * @brief USART error callbacks.
  1392. * @param husart: pointer to a USART_HandleTypeDef structure that contains
  1393. * the configuration information for the specified USART module.
  1394. * @retval None
  1395. */
  1396. __weak void HAL_USART_ErrorCallback(USART_HandleTypeDef *husart)
  1397. {
  1398. /* Prevent unused argument(s) compilation warning */
  1399. UNUSED(husart);
  1400. /* NOTE: This function should not be modified, when the callback is needed,
  1401. the HAL_USART_ErrorCallback could be implemented in the user file
  1402. */
  1403. }
  1404. /**
  1405. * @brief USART Abort Complete callback.
  1406. * @param husart USART handle.
  1407. * @retval None
  1408. */
  1409. __weak void HAL_USART_AbortCpltCallback (USART_HandleTypeDef *husart)
  1410. {
  1411. /* Prevent unused argument(s) compilation warning */
  1412. UNUSED(husart);
  1413. /* NOTE : This function should not be modified, when the callback is needed,
  1414. the HAL_USART_AbortCpltCallback can be implemented in the user file.
  1415. */
  1416. }
  1417. /**
  1418. * @}
  1419. */
  1420. /** @defgroup USART_Exported_Functions_Group3 Peripheral State and Errors functions
  1421. * @brief USART State and Errors functions
  1422. *
  1423. @verbatim
  1424. ==============================================================================
  1425. ##### Peripheral State and Errors functions #####
  1426. ==============================================================================
  1427. [..]
  1428. This subsection provides a set of functions allowing to return the State of
  1429. USART communication
  1430. process, return Peripheral Errors occurred during communication process
  1431. (+) HAL_USART_GetState() API can be helpful to check in run-time the state
  1432. of the USART peripheral.
  1433. (+) HAL_USART_GetError() check in run-time errors that could be occurred during
  1434. communication.
  1435. @endverbatim
  1436. * @{
  1437. */
  1438. /**
  1439. * @brief Returns the USART state.
  1440. * @param husart: pointer to a USART_HandleTypeDef structure that contains
  1441. * the configuration information for the specified USART module.
  1442. * @retval HAL state
  1443. */
  1444. HAL_USART_StateTypeDef HAL_USART_GetState(USART_HandleTypeDef *husart)
  1445. {
  1446. return husart->State;
  1447. }
  1448. /**
  1449. * @brief Return the USART error code
  1450. * @param husart : pointer to a USART_HandleTypeDef structure that contains
  1451. * the configuration information for the specified USART.
  1452. * @retval USART Error Code
  1453. */
  1454. uint32_t HAL_USART_GetError(USART_HandleTypeDef *husart)
  1455. {
  1456. return husart->ErrorCode;
  1457. }
  1458. /**
  1459. * @}
  1460. */
  1461. /**
  1462. * @}
  1463. */
  1464. /** @defgroup USART_Private_Functions USART Private Functions
  1465. * @brief USART Private functions
  1466. * @{
  1467. */
  1468. /**
  1469. * @brief DMA USART transmit process complete callback.
  1470. * @param hdma: DMA handle
  1471. * @param hdma: Pointer to a DMA_HandleTypeDef structure that contains
  1472. * the configuration information for the specified DMA module.
  1473. * @retval None
  1474. */
  1475. static void USART_DMATransmitCplt(DMA_HandleTypeDef *hdma)
  1476. {
  1477. USART_HandleTypeDef* husart = ( USART_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1478. /* DMA Normal mode */
  1479. if(HAL_IS_BIT_CLR(hdma->Instance->CCR, DMA_CCR_CIRC))
  1480. {
  1481. husart->TxXferCount = 0U;
  1482. if(husart->State == HAL_USART_STATE_BUSY_TX)
  1483. {
  1484. /* Disable the DMA transfer for transmit request by resetting the DMAT bit
  1485. in the USART CR3 register */
  1486. CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAT);
  1487. /* Enable the USART Transmit Complete Interrupt */
  1488. __HAL_USART_ENABLE_IT(husart, USART_IT_TC);
  1489. }
  1490. }
  1491. /* DMA Circular mode */
  1492. else
  1493. {
  1494. if(husart->State == HAL_USART_STATE_BUSY_TX)
  1495. {
  1496. HAL_USART_TxCpltCallback(husart);
  1497. }
  1498. }
  1499. }
  1500. /**
  1501. * @brief DMA USART transmit process half complete callback
  1502. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  1503. * the configuration information for the specified DMA module.
  1504. * @retval None
  1505. */
  1506. static void USART_DMATxHalfCplt(DMA_HandleTypeDef *hdma)
  1507. {
  1508. USART_HandleTypeDef* husart = (USART_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
  1509. HAL_USART_TxHalfCpltCallback(husart);
  1510. }
  1511. /**
  1512. * @brief DMA USART receive process complete callback.
  1513. * @param hdma: DMA handle
  1514. * @retval None
  1515. */
  1516. static void USART_DMAReceiveCplt(DMA_HandleTypeDef *hdma)
  1517. {
  1518. USART_HandleTypeDef* husart = ( USART_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1519. /* DMA Normal mode */
  1520. if(HAL_IS_BIT_CLR(hdma->Instance->CCR, DMA_CCR_CIRC))
  1521. {
  1522. husart->RxXferCount = 0x00U;
  1523. /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
  1524. CLEAR_BIT(husart->Instance->CR1, USART_CR1_PEIE);
  1525. CLEAR_BIT(husart->Instance->CR3, USART_CR3_EIE);
  1526. if(husart->State == HAL_USART_STATE_BUSY_RX)
  1527. {
  1528. /* Disable the DMA transfer for the Transmit/Receiver requests by setting the DMAT/DMAR bit
  1529. in the USART CR3 register */
  1530. CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAR);
  1531. husart->State= HAL_USART_STATE_READY;
  1532. HAL_USART_RxCpltCallback(husart);
  1533. }
  1534. /* The USART state is HAL_USART_STATE_BUSY_TX_RX */
  1535. else
  1536. {
  1537. /* Disable the DMA transfer for the Transmit/receiver requests by setting the DMAT/DMAR bit
  1538. in the USART CR3 register */
  1539. CLEAR_BIT(husart->Instance->CR3, (USART_CR3_DMAT | USART_CR3_DMAR));
  1540. husart->State= HAL_USART_STATE_READY;
  1541. HAL_USART_TxRxCpltCallback(husart);
  1542. }
  1543. }
  1544. /* DMA circular mode */
  1545. else
  1546. {
  1547. if(husart->State == HAL_USART_STATE_BUSY_RX)
  1548. {
  1549. HAL_USART_RxCpltCallback(husart);
  1550. }
  1551. /* The USART state is HAL_USART_STATE_BUSY_TX_RX */
  1552. else
  1553. {
  1554. HAL_USART_TxRxCpltCallback(husart);
  1555. }
  1556. }
  1557. }
  1558. /**
  1559. * @brief DMA USART receive process half complete callback
  1560. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  1561. * the configuration information for the specified DMA module.
  1562. * @retval None
  1563. */
  1564. static void USART_DMARxHalfCplt(DMA_HandleTypeDef *hdma)
  1565. {
  1566. USART_HandleTypeDef* husart = (USART_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
  1567. HAL_USART_RxHalfCpltCallback(husart);
  1568. }
  1569. /**
  1570. * @brief DMA USART communication error callback.
  1571. * @param hdma: DMA handle
  1572. * @retval None
  1573. */
  1574. static void USART_DMAError(DMA_HandleTypeDef *hdma)
  1575. {
  1576. uint32_t dmarequest = 0x00U;
  1577. USART_HandleTypeDef* husart = ( USART_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1578. husart->RxXferCount = 0x00U;
  1579. husart->TxXferCount = 0x00U;
  1580. /* Stop USART DMA Tx request if ongoing */
  1581. dmarequest = HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAT);
  1582. if((husart->State == HAL_USART_STATE_BUSY_TX) && dmarequest)
  1583. {
  1584. USART_EndTxTransfer(husart);
  1585. }
  1586. /* Stop USART DMA Rx request if ongoing */
  1587. dmarequest = HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAR);
  1588. if((husart->State == HAL_USART_STATE_BUSY_RX) && dmarequest)
  1589. {
  1590. USART_EndRxTransfer(husart);
  1591. }
  1592. husart->ErrorCode |= HAL_USART_ERROR_DMA;
  1593. husart->State= HAL_USART_STATE_READY;
  1594. HAL_USART_ErrorCallback(husart);
  1595. }
  1596. /**
  1597. * @brief This function handles USART Communication Timeout.
  1598. * @param husart: pointer to a USART_HandleTypeDef structure that contains
  1599. * the configuration information for the specified USART module.
  1600. * @param Flag: specifies the USART flag to check.
  1601. * @param Status: The new Flag status (SET or RESET).
  1602. * @param Tickstart: Tick start value.
  1603. * @param Timeout: Timeout duration.
  1604. * @retval HAL status
  1605. */
  1606. static HAL_StatusTypeDef USART_WaitOnFlagUntilTimeout(USART_HandleTypeDef *husart, uint32_t Flag, FlagStatus Status, uint32_t Tickstart, uint32_t Timeout)
  1607. {
  1608. /* Wait until flag is set */
  1609. while((__HAL_USART_GET_FLAG(husart, Flag) ? SET : RESET) == Status)
  1610. {
  1611. /* Check for the Timeout */
  1612. if(Timeout != HAL_MAX_DELAY)
  1613. {
  1614. if((Timeout == 0U)||((HAL_GetTick() - Tickstart ) > Timeout))
  1615. {
  1616. /* Disable TXE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts for the interrupt process */
  1617. __HAL_USART_DISABLE_IT(husart, USART_IT_TXE);
  1618. __HAL_USART_DISABLE_IT(husart, USART_IT_RXNE);
  1619. __HAL_USART_DISABLE_IT(husart, USART_IT_PE);
  1620. __HAL_USART_DISABLE_IT(husart, USART_IT_ERR);
  1621. husart->State= HAL_USART_STATE_READY;
  1622. /* Process Unlocked */
  1623. __HAL_UNLOCK(husart);
  1624. return HAL_TIMEOUT;
  1625. }
  1626. }
  1627. }
  1628. return HAL_OK;
  1629. }
  1630. /**
  1631. * @brief End ongoing Tx transfer on USART peripheral (following error detection or Transmit completion).
  1632. * @param husart: USART handle.
  1633. * @retval None
  1634. */
  1635. static void USART_EndTxTransfer(USART_HandleTypeDef *husart)
  1636. {
  1637. /* Disable TXEIE and TCIE interrupts */
  1638. CLEAR_BIT(husart->Instance->CR1, (USART_CR1_TXEIE | USART_CR1_TCIE));
  1639. /* At end of Tx process, restore husart->State to Ready */
  1640. husart->State = HAL_USART_STATE_READY;
  1641. }
  1642. /**
  1643. * @brief End ongoing Rx transfer on USART peripheral (following error detection or Reception completion).
  1644. * @param husart: USART handle.
  1645. * @retval None
  1646. */
  1647. static void USART_EndRxTransfer(USART_HandleTypeDef *husart)
  1648. {
  1649. /* Disable RXNE, PE and ERR interrupts */
  1650. CLEAR_BIT(husart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE));
  1651. CLEAR_BIT(husart->Instance->CR3, USART_CR3_EIE);
  1652. /* At end of Rx process, restore husart->State to Ready */
  1653. husart->State = HAL_USART_STATE_READY;
  1654. }
  1655. /**
  1656. * @brief DMA USART communication abort callback, when initiated by HAL services on Error
  1657. * (To be called at end of DMA Abort procedure following error occurrence).
  1658. * @param hdma DMA handle.
  1659. * @retval None
  1660. */
  1661. static void USART_DMAAbortOnError(DMA_HandleTypeDef *hdma)
  1662. {
  1663. USART_HandleTypeDef* husart = ( USART_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1664. husart->RxXferCount = 0x00U;
  1665. husart->TxXferCount = 0x00U;
  1666. HAL_USART_ErrorCallback(husart);
  1667. }
  1668. /**
  1669. * @brief DMA USART Tx communication abort callback, when initiated by user
  1670. * (To be called at end of DMA Tx Abort procedure following user abort request).
  1671. * @note When this callback is executed, User Abort complete call back is called only if no
  1672. * Abort still ongoing for Rx DMA Handle.
  1673. * @param hdma DMA handle.
  1674. * @retval None
  1675. */
  1676. static void USART_DMATxAbortCallback(DMA_HandleTypeDef *hdma)
  1677. {
  1678. USART_HandleTypeDef* husart = ( USART_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1679. husart->hdmatx->XferAbortCallback = NULL;
  1680. /* Check if an Abort process is still ongoing */
  1681. if(husart->hdmarx != NULL)
  1682. {
  1683. if(husart->hdmarx->XferAbortCallback != NULL)
  1684. {
  1685. return;
  1686. }
  1687. }
  1688. /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */
  1689. husart->TxXferCount = 0x00U;
  1690. husart->RxXferCount = 0x00U;
  1691. /* Reset errorCode */
  1692. husart->ErrorCode = HAL_USART_ERROR_NONE;
  1693. /* Restore husart->State to Ready */
  1694. husart->State = HAL_USART_STATE_READY;
  1695. /* Call user Abort complete callback */
  1696. HAL_USART_AbortCpltCallback(husart);
  1697. }
  1698. /**
  1699. * @brief DMA USART Rx communication abort callback, when initiated by user
  1700. * (To be called at end of DMA Rx Abort procedure following user abort request).
  1701. * @note When this callback is executed, User Abort complete call back is called only if no
  1702. * Abort still ongoing for Tx DMA Handle.
  1703. * @param hdma DMA handle.
  1704. * @retval None
  1705. */
  1706. static void USART_DMARxAbortCallback(DMA_HandleTypeDef *hdma)
  1707. {
  1708. USART_HandleTypeDef* husart = ( USART_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1709. husart->hdmarx->XferAbortCallback = NULL;
  1710. /* Check if an Abort process is still ongoing */
  1711. if(husart->hdmatx != NULL)
  1712. {
  1713. if(husart->hdmatx->XferAbortCallback != NULL)
  1714. {
  1715. return;
  1716. }
  1717. }
  1718. /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */
  1719. husart->TxXferCount = 0x00U;
  1720. husart->RxXferCount = 0x00U;
  1721. /* Reset errorCode */
  1722. husart->ErrorCode = HAL_USART_ERROR_NONE;
  1723. /* Restore husart->State to Ready */
  1724. husart->State = HAL_USART_STATE_READY;
  1725. /* Call user Abort complete callback */
  1726. HAL_USART_AbortCpltCallback(husart);
  1727. }
  1728. /**
  1729. * @brief Simplex Send an amount of data in non-blocking mode.
  1730. * @param husart: pointer to a USART_HandleTypeDef structure that contains
  1731. * the configuration information for the specified USART module.
  1732. * @retval HAL status
  1733. * @note The USART errors are not managed to avoid the overrun error.
  1734. */
  1735. static HAL_StatusTypeDef USART_Transmit_IT(USART_HandleTypeDef *husart)
  1736. {
  1737. uint16_t* tmp;
  1738. if(husart->State == HAL_USART_STATE_BUSY_TX)
  1739. {
  1740. if(husart->Init.WordLength == USART_WORDLENGTH_9B)
  1741. {
  1742. tmp = (uint16_t*) husart->pTxBuffPtr;
  1743. WRITE_REG(husart->Instance->DR, (uint16_t)(*tmp & (uint16_t)0x01FF));
  1744. if(husart->Init.Parity == USART_PARITY_NONE)
  1745. {
  1746. husart->pTxBuffPtr += 2U;
  1747. }
  1748. else
  1749. {
  1750. husart->pTxBuffPtr += 1U;
  1751. }
  1752. }
  1753. else
  1754. {
  1755. WRITE_REG(husart->Instance->DR, (uint8_t)(*husart->pTxBuffPtr++ & (uint8_t)0x00FF));
  1756. }
  1757. if(--husart->TxXferCount == 0U)
  1758. {
  1759. /* Disable the USART Transmit data register empty Interrupt */
  1760. __HAL_USART_DISABLE_IT(husart, USART_IT_TXE);
  1761. /* Enable the USART Transmit Complete Interrupt */
  1762. __HAL_USART_ENABLE_IT(husart, USART_IT_TC);
  1763. }
  1764. return HAL_OK;
  1765. }
  1766. else
  1767. {
  1768. return HAL_BUSY;
  1769. }
  1770. }
  1771. /**
  1772. * @brief Wraps up transmission in non blocking mode.
  1773. * @param husart: pointer to a USART_HandleTypeDef structure that contains
  1774. * the configuration information for the specified USART module.
  1775. * @retval HAL status
  1776. */
  1777. static HAL_StatusTypeDef USART_EndTransmit_IT(USART_HandleTypeDef *husart)
  1778. {
  1779. /* Disable the USART Transmit Complete Interrupt */
  1780. __HAL_USART_DISABLE_IT(husart, USART_IT_TC);
  1781. /* Disable the USART Error Interrupt: (Frame error, noise error, overrun error) */
  1782. __HAL_USART_DISABLE_IT(husart, USART_IT_ERR);
  1783. husart->State = HAL_USART_STATE_READY;
  1784. HAL_USART_TxCpltCallback(husart);
  1785. return HAL_OK;
  1786. }
  1787. /**
  1788. * @brief Simplex Receive an amount of data in non-blocking mode.
  1789. * @param husart: pointer to a USART_HandleTypeDef structure that contains
  1790. * the configuration information for the specified USART module.
  1791. * @retval HAL status
  1792. */
  1793. static HAL_StatusTypeDef USART_Receive_IT(USART_HandleTypeDef *husart)
  1794. {
  1795. uint16_t* tmp;
  1796. if(husart->State == HAL_USART_STATE_BUSY_RX)
  1797. {
  1798. if(husart->Init.WordLength == USART_WORDLENGTH_9B)
  1799. {
  1800. tmp = (uint16_t*) husart->pRxBuffPtr;
  1801. if(husart->Init.Parity == USART_PARITY_NONE)
  1802. {
  1803. *tmp = (uint16_t)(husart->Instance->DR & (uint16_t)0x01FF);
  1804. husart->pRxBuffPtr += 2U;
  1805. }
  1806. else
  1807. {
  1808. *tmp = (uint16_t)(husart->Instance->DR & (uint16_t)0x00FF);
  1809. husart->pRxBuffPtr += 1U;
  1810. }
  1811. if(--husart->RxXferCount != 0x00U)
  1812. {
  1813. /* Send dummy byte in order to generate the clock for the slave to send the next data */
  1814. WRITE_REG(husart->Instance->DR, (DUMMY_DATA & (uint16_t)0x01FF));
  1815. }
  1816. }
  1817. else
  1818. {
  1819. if(husart->Init.Parity == USART_PARITY_NONE)
  1820. {
  1821. *husart->pRxBuffPtr++ = (uint8_t)(husart->Instance->DR & (uint8_t)0x00FF);
  1822. }
  1823. else
  1824. {
  1825. *husart->pRxBuffPtr++ = (uint8_t)(husart->Instance->DR & (uint8_t)0x007F);
  1826. }
  1827. if(--husart->RxXferCount != 0x00U)
  1828. {
  1829. /* Send dummy byte in order to generate the clock for the slave to send the next data */
  1830. WRITE_REG(husart->Instance->DR, (DUMMY_DATA & (uint16_t)0x00FF));
  1831. }
  1832. }
  1833. if(husart->RxXferCount == 0U)
  1834. {
  1835. /* Disable the USART RXNE Interrupt */
  1836. CLEAR_BIT(husart->Instance->CR1, USART_CR1_RXNEIE);
  1837. /* Disable the USART Parity Error Interrupt */
  1838. CLEAR_BIT(husart->Instance->CR1, USART_CR1_PEIE);
  1839. /* Disable the USART Error Interrupt: (Frame error, noise error, overrun error) */
  1840. CLEAR_BIT(husart->Instance->CR3, USART_CR3_EIE);
  1841. husart->State = HAL_USART_STATE_READY;
  1842. HAL_USART_RxCpltCallback(husart);
  1843. return HAL_OK;
  1844. }
  1845. return HAL_OK;
  1846. }
  1847. else
  1848. {
  1849. return HAL_BUSY;
  1850. }
  1851. }
  1852. /**
  1853. * @brief Full-Duplex Send receive an amount of data in full-duplex mode (non-blocking).
  1854. * @param husart: pointer to a USART_HandleTypeDef structure that contains
  1855. * the configuration information for the specified USART module.
  1856. * @retval HAL status
  1857. */
  1858. static HAL_StatusTypeDef USART_TransmitReceive_IT(USART_HandleTypeDef *husart)
  1859. {
  1860. uint16_t* tmp;
  1861. if(husart->State == HAL_USART_STATE_BUSY_TX_RX)
  1862. {
  1863. if(husart->TxXferCount != 0x00U)
  1864. {
  1865. if(__HAL_USART_GET_FLAG(husart, USART_FLAG_TXE) != RESET)
  1866. {
  1867. if(husart->Init.WordLength == USART_WORDLENGTH_9B)
  1868. {
  1869. tmp = (uint16_t*) husart->pTxBuffPtr;
  1870. WRITE_REG(husart->Instance->DR, (uint16_t)(*tmp & (uint16_t)0x01FF));
  1871. if(husart->Init.Parity == USART_PARITY_NONE)
  1872. {
  1873. husart->pTxBuffPtr += 2U;
  1874. }
  1875. else
  1876. {
  1877. husart->pTxBuffPtr += 1U;
  1878. }
  1879. }
  1880. else
  1881. {
  1882. WRITE_REG(husart->Instance->DR, (uint8_t)(*husart->pTxBuffPtr++ & (uint8_t)0x00FF));
  1883. }
  1884. husart->TxXferCount--;
  1885. /* Check the latest data transmitted */
  1886. if(husart->TxXferCount == 0U)
  1887. {
  1888. CLEAR_BIT(husart->Instance->CR1, USART_CR1_TXEIE);
  1889. }
  1890. }
  1891. }
  1892. if(husart->RxXferCount != 0x00U)
  1893. {
  1894. if(__HAL_USART_GET_FLAG(husart, USART_FLAG_RXNE) != RESET)
  1895. {
  1896. if(husart->Init.WordLength == USART_WORDLENGTH_9B)
  1897. {
  1898. tmp = (uint16_t*) husart->pRxBuffPtr;
  1899. if(husart->Init.Parity == USART_PARITY_NONE)
  1900. {
  1901. *tmp = (uint16_t)(husart->Instance->DR & (uint16_t)0x01FF);
  1902. husart->pRxBuffPtr += 2U;
  1903. }
  1904. else
  1905. {
  1906. *tmp = (uint16_t)(husart->Instance->DR & (uint16_t)0x00FF);
  1907. husart->pRxBuffPtr += 1U;
  1908. }
  1909. }
  1910. else
  1911. {
  1912. if(husart->Init.Parity == USART_PARITY_NONE)
  1913. {
  1914. *husart->pRxBuffPtr++ = (uint8_t)(husart->Instance->DR & (uint8_t)0x00FF);
  1915. }
  1916. else
  1917. {
  1918. *husart->pRxBuffPtr++ = (uint8_t)(husart->Instance->DR & (uint8_t)0x007F);
  1919. }
  1920. }
  1921. husart->RxXferCount--;
  1922. }
  1923. }
  1924. /* Check the latest data received */
  1925. if(husart->RxXferCount == 0U)
  1926. {
  1927. /* Disable the USART RXNE Interrupt */
  1928. CLEAR_BIT(husart->Instance->CR1, USART_CR1_RXNEIE);
  1929. /* Disable the USART Parity Error Interrupt */
  1930. CLEAR_BIT(husart->Instance->CR1, USART_CR1_PEIE);
  1931. /* Disable the USART Error Interrupt: (Frame error, noise error, overrun error) */
  1932. CLEAR_BIT(husart->Instance->CR3, USART_CR3_EIE);
  1933. husart->State = HAL_USART_STATE_READY;
  1934. HAL_USART_TxRxCpltCallback(husart);
  1935. return HAL_OK;
  1936. }
  1937. return HAL_OK;
  1938. }
  1939. else
  1940. {
  1941. return HAL_BUSY;
  1942. }
  1943. }
  1944. /**
  1945. * @brief Configures the USART pferipheral.
  1946. * @param husart: pointer to a USART_HandleTypeDef structure that contains
  1947. * the configuration information for the specified USART module.
  1948. * @retval None
  1949. */
  1950. static void USART_SetConfig(USART_HandleTypeDef *husart)
  1951. {
  1952. /* Check the parameters */
  1953. assert_param(IS_USART_POLARITY(husart->Init.CLKPolarity));
  1954. assert_param(IS_USART_PHASE(husart->Init.CLKPhase));
  1955. assert_param(IS_USART_LASTBIT(husart->Init.CLKLastBit));
  1956. assert_param(IS_USART_BAUDRATE(husart->Init.BaudRate));
  1957. assert_param(IS_USART_WORD_LENGTH(husart->Init.WordLength));
  1958. assert_param(IS_USART_STOPBITS(husart->Init.StopBits));
  1959. assert_param(IS_USART_PARITY(husart->Init.Parity));
  1960. assert_param(IS_USART_MODE(husart->Init.Mode));
  1961. /* The LBCL, CPOL and CPHA bits have to be selected when both the transmitter and the
  1962. receiver are disabled (TE=RE=0) to ensure that the clock pulses function correctly. */
  1963. CLEAR_BIT(husart->Instance->CR1, ((uint32_t)(USART_CR1_TE | USART_CR1_RE)));
  1964. /*---------------------------- USART CR2 Configuration ---------------------*/
  1965. /* Configure the USART Clock, CPOL, CPHA and LastBit -----------------------*/
  1966. /* Set CPOL bit according to husart->Init.CLKPolarity value */
  1967. /* Set CPHA bit according to husart->Init.CLKPhase value */
  1968. /* Set LBCL bit according to husart->Init.CLKLastBit value */
  1969. /* Set Stop Bits: Set STOP[13:12] bits according to husart->Init.StopBits value */
  1970. /* Write to USART CR2 */
  1971. MODIFY_REG(husart->Instance->CR2,
  1972. (uint32_t)(USART_CR2_CPHA | USART_CR2_CPOL | USART_CR2_CLKEN | USART_CR2_LBCL | USART_CR2_STOP),
  1973. ((uint32_t)(USART_CLOCK_ENABLE| husart->Init.CLKPolarity | husart->Init.CLKPhase| husart->Init.CLKLastBit | husart->Init.StopBits)));
  1974. /*-------------------------- USART CR1 Configuration -----------------------*/
  1975. /* Configure the USART Word Length, Parity and mode:
  1976. Set the M bits according to husart->Init.WordLength value
  1977. Set PCE and PS bits according to husart->Init.Parity value
  1978. Set TE and RE bits according to husart->Init.Mode value */
  1979. MODIFY_REG(husart->Instance->CR1,
  1980. (uint32_t)(USART_CR1_M | USART_CR1_PCE | USART_CR1_PS | USART_CR1_TE | USART_CR1_RE),
  1981. (uint32_t)husart->Init.WordLength | husart->Init.Parity | husart->Init.Mode);
  1982. /*-------------------------- USART CR3 Configuration -----------------------*/
  1983. /* Clear CTSE and RTSE bits */
  1984. CLEAR_BIT(husart->Instance->CR3, (uint32_t)(USART_CR3_RTSE | USART_CR3_CTSE));
  1985. /*-------------------------- USART BRR Configuration -----------------------*/
  1986. if((husart->Instance == USART1))
  1987. {
  1988. husart->Instance->BRR = USART_BRR(HAL_RCC_GetPCLK2Freq(), husart->Init.BaudRate);
  1989. }
  1990. else
  1991. {
  1992. husart->Instance->BRR = USART_BRR(HAL_RCC_GetPCLK1Freq(), husart->Init.BaudRate);
  1993. }
  1994. }
  1995. /**
  1996. * @}
  1997. */
  1998. #endif /* HAL_USART_MODULE_ENABLED */
  1999. /**
  2000. * @}
  2001. */
  2002. /**
  2003. * @}
  2004. */
  2005. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/