stm32f4xx_hal_uart.c 61 KB

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