stm32f1xx_hal_uart.c 61 KB

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