stm32f7xx_hal_usart.c 62 KB

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