stm32f1xx_hal_usart.c 77 KB

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