stm32l1xx_hal_usart.c 63 KB

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