stm32f4xx_hal_usart.c 61 KB

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