stm32f4xx_hal_usart.c 62 KB

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