stm32f1xx_hal_usart.c 63 KB

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