stm32f1xx_hal_uart.c 85 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558
  1. /**
  2. ******************************************************************************
  3. * @file stm32f1xx_hal_uart.c
  4. * @author MCD Application Team
  5. * @version V1.1.1
  6. * @date 12-May-2017
  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 UART 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 customized
  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. (@) Additionnal remark: If the parity is enabled, then the MSB bit of the data written
  112. in the data register is transmitted but is changed by the parity bit.
  113. Depending on the frame length defined by the M bit (8-bits or 9-bits),
  114. the possible UART frame formats are as listed in the following table:
  115. +-------------------------------------------------------------+
  116. | M bit | PCE bit | UART frame |
  117. |---------------------|---------------------------------------|
  118. | 0 | 0 | | SB | 8 bit data | STB | |
  119. |---------|-----------|---------------------------------------|
  120. | 0 | 1 | | SB | 7 bit data | PB | STB | |
  121. |---------|-----------|---------------------------------------|
  122. | 1 | 0 | | SB | 9 bit data | STB | |
  123. |---------|-----------|---------------------------------------|
  124. | 1 | 1 | | SB | 8 bit data | PB | STB | |
  125. +-------------------------------------------------------------+
  126. ******************************************************************************
  127. * @attention
  128. *
  129. * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
  130. *
  131. * Redistribution and use in source and binary forms, with or without modification,
  132. * are permitted provided that the following conditions are met:
  133. * 1. Redistributions of source code must retain the above copyright notice,
  134. * this list of conditions and the following disclaimer.
  135. * 2. Redistributions in binary form must reproduce the above copyright notice,
  136. * this list of conditions and the following disclaimer in the documentation
  137. * and/or other materials provided with the distribution.
  138. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  139. * may be used to endorse or promote products derived from this software
  140. * without specific prior written permission.
  141. *
  142. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  143. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  144. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  145. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  146. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  147. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  148. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  149. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  150. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  151. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  152. *
  153. ******************************************************************************
  154. */
  155. /* Includes ------------------------------------------------------------------*/
  156. #include "stm32f1xx_hal.h"
  157. /** @addtogroup STM32F1xx_HAL_Driver
  158. * @{
  159. */
  160. /** @defgroup UART UART
  161. * @brief HAL UART module driver
  162. * @{
  163. */
  164. #ifdef HAL_UART_MODULE_ENABLED
  165. /* Private typedef -----------------------------------------------------------*/
  166. /* Private define ------------------------------------------------------------*/
  167. /** @addtogroup UART_Private_Constants
  168. * @{
  169. */
  170. /**
  171. * @}
  172. */
  173. /* Private macro -------------------------------------------------------------*/
  174. /* Private variables ---------------------------------------------------------*/
  175. /* Private function prototypes -----------------------------------------------*/
  176. /** @addtogroup UART_Private_Functions
  177. * @{
  178. */
  179. static void UART_EndTxTransfer(UART_HandleTypeDef *huart);
  180. static void UART_EndRxTransfer(UART_HandleTypeDef *huart);
  181. static void UART_DMATransmitCplt(DMA_HandleTypeDef *hdma);
  182. static void UART_DMAReceiveCplt(DMA_HandleTypeDef *hdma);
  183. static void UART_DMATxHalfCplt(DMA_HandleTypeDef *hdma);
  184. static void UART_DMARxHalfCplt(DMA_HandleTypeDef *hdma);
  185. static void UART_DMAError(DMA_HandleTypeDef *hdma);
  186. static void UART_DMAAbortOnError(DMA_HandleTypeDef *hdma);
  187. static void UART_DMATxAbortCallback(DMA_HandleTypeDef *hdma);
  188. static void UART_DMARxAbortCallback(DMA_HandleTypeDef *hdma);
  189. static void UART_DMATxOnlyAbortCallback(DMA_HandleTypeDef *hdma);
  190. static void UART_DMARxOnlyAbortCallback(DMA_HandleTypeDef *hdma);
  191. static HAL_StatusTypeDef UART_Transmit_IT(UART_HandleTypeDef *huart);
  192. static HAL_StatusTypeDef UART_EndTransmit_IT(UART_HandleTypeDef *huart);
  193. static HAL_StatusTypeDef UART_Receive_IT(UART_HandleTypeDef *huart);
  194. static HAL_StatusTypeDef UART_WaitOnFlagUntilTimeout(UART_HandleTypeDef *huart, uint32_t Flag, FlagStatus Status, uint32_t Tickstart, uint32_t Timeout);
  195. static void UART_SetConfig (UART_HandleTypeDef *huart);
  196. /**
  197. * @}
  198. */
  199. /* Exported functions ---------------------------------------------------------*/
  200. /** @defgroup UART_Exported_Functions UART Exported Functions
  201. * @{
  202. */
  203. /** @defgroup UART_Exported_Functions_Group1 Initialization and de-initialization functions
  204. * @brief Initialization and Configuration functions
  205. *
  206. @verbatim
  207. ==============================================================================
  208. ##### Initialization and Configuration functions #####
  209. ==============================================================================
  210. [..]
  211. This subsection provides a set of functions allowing to initialize the USARTx or the UARTy
  212. in asynchronous mode.
  213. (+) For the asynchronous mode only these parameters can be configured:
  214. (++) Baud Rate
  215. (++) Word Length
  216. (++) Stop Bit
  217. (++) Parity: If the parity is enabled, then the MSB bit of the data written
  218. in the data register is transmitted but is changed by the parity bit.
  219. Depending on the frame length defined by the M bit (8-bits or 9-bits),
  220. please refer to Reference manual for possible UART frame formats.
  221. (++) Hardware flow control
  222. (++) Receiver/transmitter modes
  223. (++) Over Sampling Method
  224. [..]
  225. The HAL_UART_Init(), HAL_HalfDuplex_Init(), HAL_LIN_Init() and HAL_MultiProcessor_Init() APIs
  226. follow respectively the UART asynchronous, UART Half duplex, LIN and Multi-Processor
  227. configuration procedures (details for the procedures are available in reference manuals
  228. (RM0008 for STM32F10Xxx MCUs and RM0041 for STM32F100xx MCUs)).
  229. @endverbatim
  230. * @{
  231. */
  232. /**
  233. * @brief Initializes the UART mode according to the specified parameters in
  234. * the UART_InitTypeDef and create the associated handle.
  235. * @param huart: pointer to a UART_HandleTypeDef structure that contains
  236. * the configuration information for the specified UART module.
  237. * @retval HAL status
  238. */
  239. HAL_StatusTypeDef HAL_UART_Init(UART_HandleTypeDef *huart)
  240. {
  241. /* Check the UART handle allocation */
  242. if(huart == NULL)
  243. {
  244. return HAL_ERROR;
  245. }
  246. /* Check the parameters */
  247. if(huart->Init.HwFlowCtl != UART_HWCONTROL_NONE)
  248. {
  249. /* The hardware flow control is available only for USART1, USART2, USART3 */
  250. assert_param(IS_UART_HWFLOW_INSTANCE(huart->Instance));
  251. assert_param(IS_UART_HARDWARE_FLOW_CONTROL(huart->Init.HwFlowCtl));
  252. }
  253. else
  254. {
  255. assert_param(IS_UART_INSTANCE(huart->Instance));
  256. }
  257. assert_param(IS_UART_WORD_LENGTH(huart->Init.WordLength));
  258. #if defined(USART_CR1_OVER8)
  259. assert_param(IS_UART_OVERSAMPLING(huart->Init.OverSampling));
  260. #endif /* USART_CR1_OVER8 */
  261. if(huart->gState == HAL_UART_STATE_RESET)
  262. {
  263. /* Allocate lock resource and initialize it */
  264. huart->Lock = HAL_UNLOCKED;
  265. /* Init the low level hardware */
  266. HAL_UART_MspInit(huart);
  267. }
  268. huart->gState = HAL_UART_STATE_BUSY;
  269. /* Disable the peripheral */
  270. __HAL_UART_DISABLE(huart);
  271. /* Set the UART Communication parameters */
  272. UART_SetConfig(huart);
  273. /* In asynchronous mode, the following bits must be kept cleared:
  274. - LINEN and CLKEN bits in the USART_CR2 register,
  275. - SCEN, HDSEL and IREN bits in the USART_CR3 register.*/
  276. CLEAR_BIT(huart->Instance->CR2, (USART_CR2_LINEN | USART_CR2_CLKEN));
  277. CLEAR_BIT(huart->Instance->CR3, (USART_CR3_SCEN | USART_CR3_HDSEL | USART_CR3_IREN));
  278. /* Enable the peripheral */
  279. __HAL_UART_ENABLE(huart);
  280. /* Initialize the UART state */
  281. huart->ErrorCode = HAL_UART_ERROR_NONE;
  282. huart->gState= HAL_UART_STATE_READY;
  283. huart->RxState= HAL_UART_STATE_READY;
  284. return HAL_OK;
  285. }
  286. /**
  287. * @brief Initializes the half-duplex mode according to the specified
  288. * parameters in the UART_InitTypeDef and create the associated handle.
  289. * @param huart: pointer to a UART_HandleTypeDef structure that contains
  290. * the configuration information for the specified UART module.
  291. * @retval HAL status
  292. */
  293. HAL_StatusTypeDef HAL_HalfDuplex_Init(UART_HandleTypeDef *huart)
  294. {
  295. /* Check the UART handle allocation */
  296. if(huart == NULL)
  297. {
  298. return HAL_ERROR;
  299. }
  300. /* Check the parameters */
  301. assert_param(IS_UART_HALFDUPLEX_INSTANCE(huart->Instance));
  302. assert_param(IS_UART_WORD_LENGTH(huart->Init.WordLength));
  303. #if defined(USART_CR1_OVER8)
  304. assert_param(IS_UART_OVERSAMPLING(huart->Init.OverSampling));
  305. #endif /* USART_CR1_OVER8 */
  306. if(huart->gState == HAL_UART_STATE_RESET)
  307. {
  308. /* Allocate lock resource and initialize it */
  309. huart->Lock = HAL_UNLOCKED;
  310. /* Init the low level hardware */
  311. HAL_UART_MspInit(huart);
  312. }
  313. huart->gState = HAL_UART_STATE_BUSY;
  314. /* Disable the peripheral */
  315. __HAL_UART_DISABLE(huart);
  316. /* Set the UART Communication parameters */
  317. UART_SetConfig(huart);
  318. /* In half-duplex mode, the following bits must be kept cleared:
  319. - LINEN and CLKEN bits in the USART_CR2 register,
  320. - SCEN and IREN bits in the USART_CR3 register.*/
  321. CLEAR_BIT(huart->Instance->CR2, (USART_CR2_LINEN | USART_CR2_CLKEN));
  322. CLEAR_BIT(huart->Instance->CR3, (USART_CR3_IREN | USART_CR3_SCEN));
  323. /* Enable the Half-Duplex mode by setting the HDSEL bit in the CR3 register */
  324. SET_BIT(huart->Instance->CR3, USART_CR3_HDSEL);
  325. /* Enable the peripheral */
  326. __HAL_UART_ENABLE(huart);
  327. /* Initialize the UART state*/
  328. huart->ErrorCode = HAL_UART_ERROR_NONE;
  329. huart->gState= HAL_UART_STATE_READY;
  330. huart->RxState= HAL_UART_STATE_READY;
  331. return HAL_OK;
  332. }
  333. /**
  334. * @brief Initializes the LIN mode according to the specified
  335. * parameters in the UART_InitTypeDef and create the associated handle.
  336. * @param huart: pointer to a UART_HandleTypeDef structure that contains
  337. * the configuration information for the specified UART module.
  338. * @param BreakDetectLength: Specifies the LIN break detection length.
  339. * This parameter can be one of the following values:
  340. * @arg UART_LINBREAKDETECTLENGTH_10B: 10-bit break detection
  341. * @arg UART_LINBREAKDETECTLENGTH_11B: 11-bit break detection
  342. * @retval HAL status
  343. */
  344. HAL_StatusTypeDef HAL_LIN_Init(UART_HandleTypeDef *huart, uint32_t BreakDetectLength)
  345. {
  346. /* Check the UART handle allocation */
  347. if(huart == NULL)
  348. {
  349. return HAL_ERROR;
  350. }
  351. /* Check the LIN UART instance */
  352. assert_param(IS_UART_LIN_INSTANCE(huart->Instance));
  353. /* Check the Break detection length parameter */
  354. assert_param(IS_UART_LIN_BREAK_DETECT_LENGTH(BreakDetectLength));
  355. assert_param(IS_UART_LIN_WORD_LENGTH(huart->Init.WordLength));
  356. #if defined(USART_CR1_OVER8)
  357. assert_param(IS_UART_LIN_OVERSAMPLING(huart->Init.OverSampling));
  358. #endif /* USART_CR1_OVER8 */
  359. if(huart->gState == HAL_UART_STATE_RESET)
  360. {
  361. /* Allocate lock resource and initialize it */
  362. huart->Lock = HAL_UNLOCKED;
  363. /* Init the low level hardware */
  364. HAL_UART_MspInit(huart);
  365. }
  366. huart->gState = HAL_UART_STATE_BUSY;
  367. /* Disable the peripheral */
  368. __HAL_UART_DISABLE(huart);
  369. /* Set the UART Communication parameters */
  370. UART_SetConfig(huart);
  371. /* In LIN mode, the following bits must be kept cleared:
  372. - CLKEN bits in the USART_CR2 register,
  373. - SCEN and IREN bits in the USART_CR3 register.*/
  374. CLEAR_BIT(huart->Instance->CR2, USART_CR2_CLKEN);
  375. CLEAR_BIT(huart->Instance->CR3, (USART_CR3_HDSEL | USART_CR3_IREN | USART_CR3_SCEN));
  376. /* Enable the LIN mode by setting the LINEN bit in the CR2 register */
  377. SET_BIT(huart->Instance->CR2, USART_CR2_LINEN);
  378. /* Set the USART LIN Break detection length. */
  379. MODIFY_REG(huart->Instance->CR2, USART_CR2_LBDL, BreakDetectLength);
  380. /* Enable the peripheral */
  381. __HAL_UART_ENABLE(huart);
  382. /* Initialize the UART state*/
  383. huart->ErrorCode = HAL_UART_ERROR_NONE;
  384. huart->gState= HAL_UART_STATE_READY;
  385. huart->RxState= HAL_UART_STATE_READY;
  386. return HAL_OK;
  387. }
  388. /**
  389. * @brief Initializes the Multi-Processor mode according to the specified
  390. * parameters in the UART_InitTypeDef and create the associated handle.
  391. * @param huart: pointer to a UART_HandleTypeDef structure that contains
  392. * the configuration information for the specified UART module.
  393. * @param Address: USART address
  394. * @param WakeUpMethod: specifies the USART wake-up method.
  395. * This parameter can be one of the following values:
  396. * @arg UART_WAKEUPMETHOD_IDLELINE: Wake-up by an idle line detection
  397. * @arg UART_WAKEUPMETHOD_ADDRESSMARK: Wake-up by an address mark
  398. * @retval HAL status
  399. */
  400. HAL_StatusTypeDef HAL_MultiProcessor_Init(UART_HandleTypeDef *huart, uint8_t Address, uint32_t WakeUpMethod)
  401. {
  402. /* Check the UART handle allocation */
  403. if(huart == NULL)
  404. {
  405. return HAL_ERROR;
  406. }
  407. /* Check UART instance capabilities */
  408. assert_param(IS_UART_MULTIPROCESSOR_INSTANCE(huart->Instance));
  409. /* Check the Address & wake up method parameters */
  410. assert_param(IS_UART_WAKEUPMETHOD(WakeUpMethod));
  411. assert_param(IS_UART_ADDRESS(Address));
  412. assert_param(IS_UART_WORD_LENGTH(huart->Init.WordLength));
  413. #if defined(USART_CR1_OVER8)
  414. assert_param(IS_UART_OVERSAMPLING(huart->Init.OverSampling));
  415. #endif /* USART_CR1_OVER8 */
  416. if(huart->gState == HAL_UART_STATE_RESET)
  417. {
  418. /* Allocate lock resource and initialize it */
  419. huart->Lock = HAL_UNLOCKED;
  420. /* Init the low level hardware */
  421. HAL_UART_MspInit(huart);
  422. }
  423. huart->gState = HAL_UART_STATE_BUSY;
  424. /* Disable the peripheral */
  425. __HAL_UART_DISABLE(huart);
  426. /* Set the UART Communication parameters */
  427. UART_SetConfig(huart);
  428. /* In Multi-Processor mode, the following bits must be kept cleared:
  429. - LINEN and CLKEN bits in the USART_CR2 register,
  430. - SCEN, HDSEL and IREN bits in the USART_CR3 register */
  431. CLEAR_BIT(huart->Instance->CR2, (USART_CR2_LINEN | USART_CR2_CLKEN));
  432. CLEAR_BIT(huart->Instance->CR3, (USART_CR3_SCEN | USART_CR3_HDSEL | USART_CR3_IREN));
  433. /* Set the USART address node */
  434. MODIFY_REG(huart->Instance->CR2, USART_CR2_ADD, Address);
  435. /* Set the wake up method by setting the WAKE bit in the CR1 register */
  436. MODIFY_REG(huart->Instance->CR1, USART_CR1_WAKE, WakeUpMethod);
  437. /* Enable the peripheral */
  438. __HAL_UART_ENABLE(huart);
  439. /* Initialize the UART state */
  440. huart->ErrorCode = HAL_UART_ERROR_NONE;
  441. huart->gState = HAL_UART_STATE_READY;
  442. huart->RxState = HAL_UART_STATE_READY;
  443. return HAL_OK;
  444. }
  445. /**
  446. * @brief DeInitializes the UART peripheral.
  447. * @param huart: pointer to a UART_HandleTypeDef structure that contains
  448. * the configuration information for the specified UART module.
  449. * @retval HAL status
  450. */
  451. HAL_StatusTypeDef HAL_UART_DeInit(UART_HandleTypeDef *huart)
  452. {
  453. /* Check the UART handle allocation */
  454. if(huart == NULL)
  455. {
  456. return HAL_ERROR;
  457. }
  458. /* Check the parameters */
  459. assert_param(IS_UART_INSTANCE(huart->Instance));
  460. huart->gState = HAL_UART_STATE_BUSY;
  461. /* DeInit the low level hardware */
  462. HAL_UART_MspDeInit(huart);
  463. huart->ErrorCode = HAL_UART_ERROR_NONE;
  464. huart->gState = HAL_UART_STATE_RESET;
  465. huart->RxState = HAL_UART_STATE_RESET;
  466. /* Process Unlock */
  467. __HAL_UNLOCK(huart);
  468. return HAL_OK;
  469. }
  470. /**
  471. * @brief UART MSP Init.
  472. * @param huart: pointer to a UART_HandleTypeDef structure that contains
  473. * the configuration information for the specified UART module.
  474. * @retval None
  475. */
  476. __weak void HAL_UART_MspInit(UART_HandleTypeDef *huart)
  477. {
  478. /* Prevent unused argument(s) compilation warning */
  479. UNUSED(huart);
  480. /* NOTE: This function should not be modified, when the callback is needed,
  481. the HAL_UART_MspInit could be implemented in the user file
  482. */
  483. }
  484. /**
  485. * @brief UART MSP DeInit.
  486. * @param huart: pointer to a UART_HandleTypeDef structure that contains
  487. * the configuration information for the specified UART module.
  488. * @retval None
  489. */
  490. __weak void HAL_UART_MspDeInit(UART_HandleTypeDef *huart)
  491. {
  492. /* Prevent unused argument(s) compilation warning */
  493. UNUSED(huart);
  494. /* NOTE: This function should not be modified, when the callback is needed,
  495. the HAL_UART_MspDeInit could be implemented in the user file
  496. */
  497. }
  498. /**
  499. * @}
  500. */
  501. /** @defgroup UART_Exported_Functions_Group2 IO operation functions
  502. * @brief UART Transmit and Receive functions
  503. *
  504. @verbatim
  505. ==============================================================================
  506. ##### IO operation functions #####
  507. ==============================================================================
  508. [..]
  509. This subsection provides a set of functions allowing to manage the UART asynchronous
  510. and Half duplex data transfers.
  511. (#) There are two modes of transfer:
  512. (++) Blocking mode: The communication is performed in polling mode.
  513. The HAL status of all data processing is returned by the same function
  514. after finishing transfer.
  515. (++) Non blocking mode: The communication is performed using Interrupts
  516. or DMA, these APIs return the HAL status.
  517. The end of the data processing will be indicated through the
  518. dedicated UART IRQ when using Interrupt mode or the DMA IRQ when
  519. using DMA mode.
  520. The HAL_UART_TxCpltCallback(), HAL_UART_RxCpltCallback() user callbacks
  521. will be executed respectively at the end of the transmit or receive process.
  522. The HAL_UART_ErrorCallback() user callback will be executed when
  523. a communication error is detected.
  524. (#) Blocking mode APIs are:
  525. (++) HAL_UART_Transmit()
  526. (++) HAL_UART_Receive()
  527. (#) Non Blocking mode APIs with Interrupt are:
  528. (++) HAL_UART_Transmit_IT()
  529. (++) HAL_UART_Receive_IT()
  530. (++) HAL_UART_IRQHandler()
  531. (#) Non Blocking mode functions with DMA are:
  532. (++) HAL_UART_Transmit_DMA()
  533. (++) HAL_UART_Receive_DMA()
  534. (++) HAL_UART_DMAPause()
  535. (++) HAL_UART_DMAResume()
  536. (++) HAL_UART_DMAStop()
  537. (#) A set of Transfer Complete Callbacks are provided in non blocking mode:
  538. (++) HAL_UART_TxHalfCpltCallback()
  539. (++) HAL_UART_TxCpltCallback()
  540. (++) HAL_UART_RxHalfCpltCallback()
  541. (++) HAL_UART_RxCpltCallback()
  542. (++) HAL_UART_ErrorCallback()
  543. [..]
  544. (@) In the Half duplex communication, it is forbidden to run the transmit
  545. and receive process in parallel, the UART state HAL_UART_STATE_BUSY_TX_RX
  546. can't be useful.
  547. @endverbatim
  548. * @{
  549. */
  550. /**
  551. * @brief Sends an amount of data in blocking mode.
  552. * @param huart: pointer to a UART_HandleTypeDef structure that contains
  553. * the configuration information for the specified UART module.
  554. * @param pData: Pointer to data buffer
  555. * @param Size: Amount of data to be sent
  556. * @param Timeout: Timeout duration
  557. * @retval HAL status
  558. */
  559. HAL_StatusTypeDef HAL_UART_Transmit(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint32_t Timeout)
  560. {
  561. uint16_t* tmp;
  562. uint32_t tickstart = 0U;
  563. /* Check that a Tx process is not already ongoing */
  564. if(huart->gState == HAL_UART_STATE_READY)
  565. {
  566. if((pData == NULL) || (Size == 0U))
  567. {
  568. return HAL_ERROR;
  569. }
  570. /* Process Locked */
  571. __HAL_LOCK(huart);
  572. huart->ErrorCode = HAL_UART_ERROR_NONE;
  573. huart->gState = HAL_UART_STATE_BUSY_TX;
  574. /* Init tickstart for timeout managment */
  575. tickstart = HAL_GetTick();
  576. huart->TxXferSize = Size;
  577. huart->TxXferCount = Size;
  578. while(huart->TxXferCount > 0U)
  579. {
  580. huart->TxXferCount--;
  581. if(huart->Init.WordLength == UART_WORDLENGTH_9B)
  582. {
  583. if(UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_TXE, RESET, tickstart, Timeout) != HAL_OK)
  584. {
  585. return HAL_TIMEOUT;
  586. }
  587. tmp = (uint16_t*) pData;
  588. huart->Instance->DR = (*tmp & (uint16_t)0x01FF);
  589. if(huart->Init.Parity == UART_PARITY_NONE)
  590. {
  591. pData +=2U;
  592. }
  593. else
  594. {
  595. pData +=1U;
  596. }
  597. }
  598. else
  599. {
  600. if(UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_TXE, RESET, tickstart, Timeout) != HAL_OK)
  601. {
  602. return HAL_TIMEOUT;
  603. }
  604. huart->Instance->DR = (*pData++ & (uint8_t)0xFF);
  605. }
  606. }
  607. if(UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_TC, RESET, tickstart, Timeout) != HAL_OK)
  608. {
  609. return HAL_TIMEOUT;
  610. }
  611. /* At end of Tx process, restore huart->gState to Ready */
  612. huart->gState = HAL_UART_STATE_READY;
  613. /* Process Unlocked */
  614. __HAL_UNLOCK(huart);
  615. return HAL_OK;
  616. }
  617. else
  618. {
  619. return HAL_BUSY;
  620. }
  621. }
  622. /**
  623. * @brief Receive an amount of data in blocking mode.
  624. * @param huart: pointer to a UART_HandleTypeDef structure that contains
  625. * the configuration information for the specified UART module.
  626. * @param pData: Pointer to data buffer
  627. * @param Size: Amount of data to be received
  628. * @param Timeout: Timeout duration
  629. * @retval HAL status
  630. */
  631. HAL_StatusTypeDef HAL_UART_Receive(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint32_t Timeout)
  632. {
  633. uint16_t* tmp;
  634. uint32_t tickstart = 0U;
  635. /* Check that a Rx process is not already ongoing */
  636. if(huart->RxState == HAL_UART_STATE_READY)
  637. {
  638. if((pData == NULL) || (Size == 0U))
  639. {
  640. return HAL_ERROR;
  641. }
  642. /* Process Locked */
  643. __HAL_LOCK(huart);
  644. huart->ErrorCode = HAL_UART_ERROR_NONE;
  645. huart->RxState = HAL_UART_STATE_BUSY_RX;
  646. /* Init tickstart for timeout managment */
  647. tickstart = HAL_GetTick();
  648. huart->RxXferSize = Size;
  649. huart->RxXferCount = Size;
  650. /* Check the remain data to be received */
  651. while(huart->RxXferCount > 0U)
  652. {
  653. huart->RxXferCount--;
  654. if(huart->Init.WordLength == UART_WORDLENGTH_9B)
  655. {
  656. if(UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_RXNE, RESET, tickstart, Timeout) != HAL_OK)
  657. {
  658. return HAL_TIMEOUT;
  659. }
  660. tmp = (uint16_t*)pData;
  661. if(huart->Init.Parity == UART_PARITY_NONE)
  662. {
  663. *tmp = (uint16_t)(huart->Instance->DR & (uint16_t)0x01FF);
  664. pData +=2U;
  665. }
  666. else
  667. {
  668. *tmp = (uint16_t)(huart->Instance->DR & (uint16_t)0x00FF);
  669. pData +=1U;
  670. }
  671. }
  672. else
  673. {
  674. if(UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_RXNE, RESET, tickstart, Timeout) != HAL_OK)
  675. {
  676. return HAL_TIMEOUT;
  677. }
  678. if(huart->Init.Parity == UART_PARITY_NONE)
  679. {
  680. *pData++ = (uint8_t)(huart->Instance->DR & (uint8_t)0x00FF);
  681. }
  682. else
  683. {
  684. *pData++ = (uint8_t)(huart->Instance->DR & (uint8_t)0x007F);
  685. }
  686. }
  687. }
  688. /* At end of Rx process, restore huart->RxState to Ready */
  689. huart->RxState = HAL_UART_STATE_READY;
  690. /* Process Unlocked */
  691. __HAL_UNLOCK(huart);
  692. return HAL_OK;
  693. }
  694. else
  695. {
  696. return HAL_BUSY;
  697. }
  698. }
  699. /**
  700. * @brief Sends an amount of data in non blocking mode.
  701. * @param huart: pointer to a UART_HandleTypeDef structure that contains
  702. * the configuration information for the specified UART module.
  703. * @param pData: Pointer to data buffer
  704. * @param Size: Amount of data to be sent
  705. * @retval HAL status
  706. */
  707. HAL_StatusTypeDef HAL_UART_Transmit_IT(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
  708. {
  709. /* Check that a Tx process is not already ongoing */
  710. if(huart->gState == HAL_UART_STATE_READY)
  711. {
  712. if((pData == NULL) || (Size == 0U))
  713. {
  714. return HAL_ERROR;
  715. }
  716. /* Process Locked */
  717. __HAL_LOCK(huart);
  718. huart->pTxBuffPtr = pData;
  719. huart->TxXferSize = Size;
  720. huart->TxXferCount = Size;
  721. huart->ErrorCode = HAL_UART_ERROR_NONE;
  722. huart->gState = HAL_UART_STATE_BUSY_TX;
  723. /* Process Unlocked */
  724. __HAL_UNLOCK(huart);
  725. /* Enable the UART Transmit data register empty Interrupt */
  726. __HAL_UART_ENABLE_IT(huart, UART_IT_TXE);
  727. return HAL_OK;
  728. }
  729. else
  730. {
  731. return HAL_BUSY;
  732. }
  733. }
  734. /**
  735. * @brief Receives an amount of data in non blocking mode.
  736. * @param huart: pointer to a UART_HandleTypeDef structure that contains
  737. * the configuration information for the specified UART module.
  738. * @param pData: Pointer to data buffer
  739. * @param Size: Amount of data to be received
  740. * @retval HAL status
  741. */
  742. HAL_StatusTypeDef HAL_UART_Receive_IT(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
  743. {
  744. /* Check that a Rx process is not already ongoing */
  745. if(huart->RxState == HAL_UART_STATE_READY)
  746. {
  747. if((pData == NULL) || (Size == 0U))
  748. {
  749. return HAL_ERROR;
  750. }
  751. /* Process Locked */
  752. __HAL_LOCK(huart);
  753. huart->pRxBuffPtr = pData;
  754. huart->RxXferSize = Size;
  755. huart->RxXferCount = Size;
  756. huart->ErrorCode = HAL_UART_ERROR_NONE;
  757. huart->RxState = HAL_UART_STATE_BUSY_RX;
  758. /* Process Unlocked */
  759. __HAL_UNLOCK(huart);
  760. /* Enable the UART Parity Error Interrupt */
  761. __HAL_UART_ENABLE_IT(huart, UART_IT_PE);
  762. /* Enable the UART Error Interrupt: (Frame error, noise error, overrun error) */
  763. __HAL_UART_ENABLE_IT(huart, UART_IT_ERR);
  764. /* Enable the UART Data Register not empty Interrupt */
  765. __HAL_UART_ENABLE_IT(huart, UART_IT_RXNE);
  766. return HAL_OK;
  767. }
  768. else
  769. {
  770. return HAL_BUSY;
  771. }
  772. }
  773. /**
  774. * @brief Sends an amount of data in non blocking mode.
  775. * @param huart: pointer to a UART_HandleTypeDef structure that contains
  776. * the configuration information for the specified UART module.
  777. * @param pData: Pointer to data buffer
  778. * @param Size: Amount of data to be sent
  779. * @retval HAL status
  780. */
  781. HAL_StatusTypeDef HAL_UART_Transmit_DMA(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
  782. {
  783. uint32_t *tmp;
  784. /* Check that a Tx process is not already ongoing */
  785. if(huart->gState == HAL_UART_STATE_READY)
  786. {
  787. if((pData == NULL) || (Size == 0U))
  788. {
  789. return HAL_ERROR;
  790. }
  791. /* Process Locked */
  792. __HAL_LOCK(huart);
  793. huart->pTxBuffPtr = pData;
  794. huart->TxXferSize = Size;
  795. huart->TxXferCount = Size;
  796. huart->ErrorCode = HAL_UART_ERROR_NONE;
  797. huart->gState = HAL_UART_STATE_BUSY_TX;
  798. /* Set the UART DMA transfer complete callback */
  799. huart->hdmatx->XferCpltCallback = UART_DMATransmitCplt;
  800. /* Set the UART DMA Half transfer complete callback */
  801. huart->hdmatx->XferHalfCpltCallback = UART_DMATxHalfCplt;
  802. /* Set the DMA error callback */
  803. huart->hdmatx->XferErrorCallback = UART_DMAError;
  804. /* Set the DMA abort callback */
  805. huart->hdmatx->XferAbortCallback = NULL;
  806. /* Enable the UART transmit DMA channel */
  807. tmp = (uint32_t*)&pData;
  808. HAL_DMA_Start_IT(huart->hdmatx, *(uint32_t*)tmp, (uint32_t)&huart->Instance->DR, Size);
  809. /* Clear the TC flag in the SR register by writing 0 to it */
  810. __HAL_UART_CLEAR_FLAG(huart, UART_FLAG_TC);
  811. /* Process Unlocked */
  812. __HAL_UNLOCK(huart);
  813. /* Enable the DMA transfer for transmit request by setting the DMAT bit
  814. in the UART CR3 register */
  815. SET_BIT(huart->Instance->CR3, USART_CR3_DMAT);
  816. return HAL_OK;
  817. }
  818. else
  819. {
  820. return HAL_BUSY;
  821. }
  822. }
  823. /**
  824. * @brief Receives an amount of data in non blocking mode.
  825. * @param huart: pointer to a UART_HandleTypeDef structure that contains
  826. * the configuration information for the specified UART module.
  827. * @param pData: Pointer to data buffer
  828. * @param Size: Amount of data to be received
  829. * @note When the UART parity is enabled (PCE = 1) the data received contain the parity bit.
  830. * @retval HAL status
  831. */
  832. HAL_StatusTypeDef HAL_UART_Receive_DMA(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
  833. {
  834. uint32_t *tmp;
  835. /* Check that a Rx process is not already ongoing */
  836. if(huart->RxState == HAL_UART_STATE_READY)
  837. {
  838. if((pData == NULL) || (Size == 0U))
  839. {
  840. return HAL_ERROR;
  841. }
  842. /* Process Locked */
  843. __HAL_LOCK(huart);
  844. huart->pRxBuffPtr = pData;
  845. huart->RxXferSize = Size;
  846. huart->ErrorCode = HAL_UART_ERROR_NONE;
  847. huart->RxState = HAL_UART_STATE_BUSY_RX;
  848. /* Set the UART DMA transfer complete callback */
  849. huart->hdmarx->XferCpltCallback = UART_DMAReceiveCplt;
  850. /* Set the UART DMA Half transfer complete callback */
  851. huart->hdmarx->XferHalfCpltCallback = UART_DMARxHalfCplt;
  852. /* Set the DMA error callback */
  853. huart->hdmarx->XferErrorCallback = UART_DMAError;
  854. /* Set the DMA abort callback */
  855. huart->hdmarx->XferAbortCallback = NULL;
  856. /* Enable the DMA channel */
  857. tmp = (uint32_t*)&pData;
  858. HAL_DMA_Start_IT(huart->hdmarx, (uint32_t)&huart->Instance->DR, *(uint32_t*)tmp, Size);
  859. /* Clear the Overrun flag just before enabling the DMA Rx request: can be mandatory for the second transfer */
  860. __HAL_UART_CLEAR_OREFLAG(huart);
  861. /* Process Unlocked */
  862. __HAL_UNLOCK(huart);
  863. /* Enable the UART Parity Error Interrupt */
  864. SET_BIT(huart->Instance->CR1, USART_CR1_PEIE);
  865. /* Enable the UART Error Interrupt: (Frame error, noise error, overrun error) */
  866. SET_BIT(huart->Instance->CR3, USART_CR3_EIE);
  867. /* Enable the DMA transfer for the receiver request by setting the DMAR bit
  868. in the UART CR3 register */
  869. SET_BIT(huart->Instance->CR3, USART_CR3_DMAR);
  870. return HAL_OK;
  871. }
  872. else
  873. {
  874. return HAL_BUSY;
  875. }
  876. }
  877. /**
  878. * @brief Pauses the DMA Transfer.
  879. * @param huart: pointer to a UART_HandleTypeDef structure that contains
  880. * the configuration information for the specified UART module.
  881. * @retval HAL status
  882. */
  883. HAL_StatusTypeDef HAL_UART_DMAPause(UART_HandleTypeDef *huart)
  884. {
  885. uint32_t dmarequest = 0x00U;
  886. /* Process Locked */
  887. __HAL_LOCK(huart);
  888. dmarequest = HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT);
  889. if((huart->gState == HAL_UART_STATE_BUSY_TX) && dmarequest)
  890. {
  891. /* Disable the UART DMA Tx request */
  892. CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAT);
  893. }
  894. dmarequest = HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR);
  895. if((huart->RxState == HAL_UART_STATE_BUSY_RX) && dmarequest)
  896. {
  897. /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
  898. CLEAR_BIT(huart->Instance->CR1, USART_CR1_PEIE);
  899. CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
  900. /* Disable the UART DMA Rx request */
  901. CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR);
  902. }
  903. /* Process Unlocked */
  904. __HAL_UNLOCK(huart);
  905. return HAL_OK;
  906. }
  907. /**
  908. * @brief Resumes the DMA Transfer.
  909. * @param huart: pointer to a UART_HandleTypeDef structure that contains
  910. * the configuration information for the specified UART module.
  911. * @retval HAL status
  912. */
  913. HAL_StatusTypeDef HAL_UART_DMAResume(UART_HandleTypeDef *huart)
  914. {
  915. /* Process Locked */
  916. __HAL_LOCK(huart);
  917. if(huart->gState == HAL_UART_STATE_BUSY_TX)
  918. {
  919. /* Enable the UART DMA Tx request */
  920. SET_BIT(huart->Instance->CR3, USART_CR3_DMAT);
  921. }
  922. if(huart->RxState == HAL_UART_STATE_BUSY_RX)
  923. {
  924. /* Clear the Overrun flag before resuming the Rx transfer*/
  925. __HAL_UART_CLEAR_OREFLAG(huart);
  926. /* Reenable PE and ERR (Frame error, noise error, overrun error) interrupts */
  927. SET_BIT(huart->Instance->CR1, USART_CR1_PEIE);
  928. SET_BIT(huart->Instance->CR3, USART_CR3_EIE);
  929. /* Enable the UART DMA Rx request */
  930. SET_BIT(huart->Instance->CR3, USART_CR3_DMAR);
  931. }
  932. /* Process Unlocked */
  933. __HAL_UNLOCK(huart);
  934. return HAL_OK;
  935. }
  936. /**
  937. * @brief Stops the DMA Transfer.
  938. * @param huart: pointer to a UART_HandleTypeDef structure that contains
  939. * the configuration information for the specified UART module.
  940. * @retval HAL status
  941. */
  942. HAL_StatusTypeDef HAL_UART_DMAStop(UART_HandleTypeDef *huart)
  943. {
  944. uint32_t dmarequest = 0x00U;
  945. /* The Lock is not implemented on this API to allow the user application
  946. to call the HAL UART API under callbacks HAL_UART_TxCpltCallback() / HAL_UART_RxCpltCallback():
  947. when calling HAL_DMA_Abort() API the DMA TX/RX Transfer complete interrupt is generated
  948. and the correspond call back is executed HAL_UART_TxCpltCallback() / HAL_UART_RxCpltCallback()
  949. */
  950. /* Stop UART DMA Tx request if ongoing */
  951. dmarequest = HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT);
  952. if((huart->gState == HAL_UART_STATE_BUSY_TX) && dmarequest)
  953. {
  954. CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAT);
  955. /* Abort the UART DMA Tx channel */
  956. if(huart->hdmatx != NULL)
  957. {
  958. HAL_DMA_Abort(huart->hdmatx);
  959. }
  960. UART_EndTxTransfer(huart);
  961. }
  962. /* Stop UART DMA Rx request if ongoing */
  963. dmarequest = HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR);
  964. if((huart->RxState == HAL_UART_STATE_BUSY_RX) && dmarequest)
  965. {
  966. CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR);
  967. /* Abort the UART DMA Rx channel */
  968. if(huart->hdmarx != NULL)
  969. {
  970. HAL_DMA_Abort(huart->hdmarx);
  971. }
  972. UART_EndRxTransfer(huart);
  973. }
  974. return HAL_OK;
  975. }
  976. /**
  977. * @brief Abort ongoing transfers (blocking mode).
  978. * @param huart UART handle.
  979. * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
  980. * This procedure performs following operations :
  981. * - Disable PPP Interrupts
  982. * - Disable the DMA transfer in the peripheral register (if enabled)
  983. * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
  984. * - Set handle State to READY
  985. * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
  986. * @retval HAL status
  987. */
  988. HAL_StatusTypeDef HAL_UART_Abort(UART_HandleTypeDef *huart)
  989. {
  990. /* Disable TXEIE, TCIE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
  991. CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE | USART_CR1_TXEIE | USART_CR1_TCIE));
  992. CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
  993. /* Disable the UART DMA Tx request if enabled */
  994. if(HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT))
  995. {
  996. CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAT);
  997. /* Abort the UART DMA Tx channel: use blocking DMA Abort API (no callback) */
  998. if(huart->hdmatx != NULL)
  999. {
  1000. /* Set the UART DMA Abort callback to Null.
  1001. No call back execution at end of DMA abort procedure */
  1002. huart->hdmatx->XferAbortCallback = NULL;
  1003. HAL_DMA_Abort(huart->hdmatx);
  1004. }
  1005. }
  1006. /* Disable the UART DMA Rx request if enabled */
  1007. if(HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR))
  1008. {
  1009. CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR);
  1010. /* Abort the UART DMA Rx channel: use blocking DMA Abort API (no callback) */
  1011. if(huart->hdmarx != NULL)
  1012. {
  1013. /* Set the UART DMA Abort callback to Null.
  1014. No call back execution at end of DMA abort procedure */
  1015. huart->hdmarx->XferAbortCallback = NULL;
  1016. HAL_DMA_Abort(huart->hdmarx);
  1017. }
  1018. }
  1019. /* Reset Tx and Rx transfer counters */
  1020. huart->TxXferCount = 0x00U;
  1021. huart->RxXferCount = 0x00U;
  1022. /* Reset ErrorCode */
  1023. huart->ErrorCode = HAL_UART_ERROR_NONE;
  1024. /* Restore huart->RxState and huart->gState to Ready */
  1025. huart->RxState = HAL_UART_STATE_READY;
  1026. huart->gState = HAL_UART_STATE_READY;
  1027. return HAL_OK;
  1028. }
  1029. /**
  1030. * @brief Abort ongoing Transmit transfer (blocking mode).
  1031. * @param huart UART handle.
  1032. * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
  1033. * This procedure performs following operations :
  1034. * - Disable PPP Interrupts
  1035. * - Disable the DMA transfer in the peripheral register (if enabled)
  1036. * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
  1037. * - Set handle State to READY
  1038. * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
  1039. * @retval HAL status
  1040. */
  1041. HAL_StatusTypeDef HAL_UART_AbortTransmit(UART_HandleTypeDef *huart)
  1042. {
  1043. /* Disable TXEIE and TCIE interrupts */
  1044. CLEAR_BIT(huart->Instance->CR1, (USART_CR1_TXEIE | USART_CR1_TCIE));
  1045. /* Disable the UART DMA Tx request if enabled */
  1046. if(HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT))
  1047. {
  1048. CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAT);
  1049. /* Abort the UART DMA Tx channel : use blocking DMA Abort API (no callback) */
  1050. if(huart->hdmatx != NULL)
  1051. {
  1052. /* Set the UART DMA Abort callback to Null.
  1053. No call back execution at end of DMA abort procedure */
  1054. huart->hdmatx->XferAbortCallback = NULL;
  1055. HAL_DMA_Abort(huart->hdmatx);
  1056. }
  1057. }
  1058. /* Reset Tx transfer counter */
  1059. huart->TxXferCount = 0x00U;
  1060. /* Restore huart->gState to Ready */
  1061. huart->gState = HAL_UART_STATE_READY;
  1062. return HAL_OK;
  1063. }
  1064. /**
  1065. * @brief Abort ongoing Receive transfer (blocking mode).
  1066. * @param huart UART handle.
  1067. * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
  1068. * This procedure performs following operations :
  1069. * - Disable PPP Interrupts
  1070. * - Disable the DMA transfer in the peripheral register (if enabled)
  1071. * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
  1072. * - Set handle State to READY
  1073. * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
  1074. * @retval HAL status
  1075. */
  1076. HAL_StatusTypeDef HAL_UART_AbortReceive(UART_HandleTypeDef *huart)
  1077. {
  1078. /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
  1079. CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE));
  1080. CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
  1081. /* Disable the UART DMA Rx request if enabled */
  1082. if(HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR))
  1083. {
  1084. CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR);
  1085. /* Abort the UART DMA Rx channel : use blocking DMA Abort API (no callback) */
  1086. if(huart->hdmarx != NULL)
  1087. {
  1088. /* Set the UART DMA Abort callback to Null.
  1089. No call back execution at end of DMA abort procedure */
  1090. huart->hdmarx->XferAbortCallback = NULL;
  1091. HAL_DMA_Abort(huart->hdmarx);
  1092. }
  1093. }
  1094. /* Reset Rx transfer counter */
  1095. huart->RxXferCount = 0x00U;
  1096. /* Restore huart->RxState to Ready */
  1097. huart->RxState = HAL_UART_STATE_READY;
  1098. return HAL_OK;
  1099. }
  1100. /**
  1101. * @brief Abort ongoing transfers (Interrupt mode).
  1102. * @param huart UART handle.
  1103. * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
  1104. * This procedure performs following operations :
  1105. * - Disable PPP Interrupts
  1106. * - Disable the DMA transfer in the peripheral register (if enabled)
  1107. * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
  1108. * - Set handle State to READY
  1109. * - At abort completion, call user abort complete callback
  1110. * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be
  1111. * considered as completed only when user abort complete callback is executed (not when exiting function).
  1112. * @retval HAL status
  1113. */
  1114. HAL_StatusTypeDef HAL_UART_Abort_IT(UART_HandleTypeDef *huart)
  1115. {
  1116. uint32_t AbortCplt = 0x01U;
  1117. /* Disable TXEIE, TCIE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
  1118. CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE | USART_CR1_TXEIE | USART_CR1_TCIE));
  1119. CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
  1120. /* If DMA Tx and/or DMA Rx Handles are associated to UART Handle, DMA Abort complete callbacks should be initialised
  1121. before any call to DMA Abort functions */
  1122. /* DMA Tx Handle is valid */
  1123. if(huart->hdmatx != NULL)
  1124. {
  1125. /* Set DMA Abort Complete callback if UART DMA Tx request if enabled.
  1126. Otherwise, set it to NULL */
  1127. if(HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT))
  1128. {
  1129. huart->hdmatx->XferAbortCallback = UART_DMATxAbortCallback;
  1130. }
  1131. else
  1132. {
  1133. huart->hdmatx->XferAbortCallback = NULL;
  1134. }
  1135. }
  1136. /* DMA Rx Handle is valid */
  1137. if(huart->hdmarx != NULL)
  1138. {
  1139. /* Set DMA Abort Complete callback if UART DMA Rx request if enabled.
  1140. Otherwise, set it to NULL */
  1141. if(HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR))
  1142. {
  1143. huart->hdmarx->XferAbortCallback = UART_DMARxAbortCallback;
  1144. }
  1145. else
  1146. {
  1147. huart->hdmarx->XferAbortCallback = NULL;
  1148. }
  1149. }
  1150. /* Disable the UART DMA Tx request if enabled */
  1151. if(HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT))
  1152. {
  1153. /* Disable DMA Tx at UART level */
  1154. CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAT);
  1155. /* Abort the UART DMA Tx channel : use non blocking DMA Abort API (callback) */
  1156. if(huart->hdmatx != NULL)
  1157. {
  1158. /* UART Tx DMA Abort callback has already been initialised :
  1159. will lead to call HAL_UART_AbortCpltCallback() at end of DMA abort procedure */
  1160. /* Abort DMA TX */
  1161. if(HAL_DMA_Abort_IT(huart->hdmatx) != HAL_OK)
  1162. {
  1163. huart->hdmatx->XferAbortCallback = NULL;
  1164. }
  1165. else
  1166. {
  1167. AbortCplt = 0x00U;
  1168. }
  1169. }
  1170. }
  1171. /* Disable the UART DMA Rx request if enabled */
  1172. if(HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR))
  1173. {
  1174. CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR);
  1175. /* Abort the UART DMA Rx channel : use non blocking DMA Abort API (callback) */
  1176. if(huart->hdmarx != NULL)
  1177. {
  1178. /* UART Rx DMA Abort callback has already been initialised :
  1179. will lead to call HAL_UART_AbortCpltCallback() at end of DMA abort procedure */
  1180. /* Abort DMA RX */
  1181. if(HAL_DMA_Abort_IT(huart->hdmarx) != HAL_OK)
  1182. {
  1183. huart->hdmarx->XferAbortCallback = NULL;
  1184. AbortCplt = 0x01U;
  1185. }
  1186. else
  1187. {
  1188. AbortCplt = 0x00U;
  1189. }
  1190. }
  1191. }
  1192. /* if no DMA abort complete callback execution is required => call user Abort Complete callback */
  1193. if(AbortCplt == 0x01U)
  1194. {
  1195. /* Reset Tx and Rx transfer counters */
  1196. huart->TxXferCount = 0x00U;
  1197. huart->RxXferCount = 0x00U;
  1198. /* Reset ErrorCode */
  1199. huart->ErrorCode = HAL_UART_ERROR_NONE;
  1200. /* Restore huart->gState and huart->RxState to Ready */
  1201. huart->gState = HAL_UART_STATE_READY;
  1202. huart->RxState = HAL_UART_STATE_READY;
  1203. /* As no DMA to be aborted, call directly user Abort complete callback */
  1204. HAL_UART_AbortCpltCallback(huart);
  1205. }
  1206. return HAL_OK;
  1207. }
  1208. /**
  1209. * @brief Abort ongoing Transmit transfer (Interrupt mode).
  1210. * @param huart UART handle.
  1211. * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
  1212. * This procedure performs following operations :
  1213. * - Disable PPP Interrupts
  1214. * - Disable the DMA transfer in the peripheral register (if enabled)
  1215. * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
  1216. * - Set handle State to READY
  1217. * - At abort completion, call user abort complete callback
  1218. * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be
  1219. * considered as completed only when user abort complete callback is executed (not when exiting function).
  1220. * @retval HAL status
  1221. */
  1222. HAL_StatusTypeDef HAL_UART_AbortTransmit_IT(UART_HandleTypeDef *huart)
  1223. {
  1224. /* Disable TXEIE and TCIE interrupts */
  1225. CLEAR_BIT(huart->Instance->CR1, (USART_CR1_TXEIE | USART_CR1_TCIE));
  1226. /* Disable the UART DMA Tx request if enabled */
  1227. if(HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT))
  1228. {
  1229. CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAT);
  1230. /* Abort the UART DMA Tx channel : use blocking DMA Abort API (no callback) */
  1231. if(huart->hdmatx != NULL)
  1232. {
  1233. /* Set the UART DMA Abort callback :
  1234. will lead to call HAL_UART_AbortCpltCallback() at end of DMA abort procedure */
  1235. huart->hdmatx->XferAbortCallback = UART_DMATxOnlyAbortCallback;
  1236. /* Abort DMA TX */
  1237. if(HAL_DMA_Abort_IT(huart->hdmatx) != HAL_OK)
  1238. {
  1239. /* Call Directly huart->hdmatx->XferAbortCallback function in case of error */
  1240. huart->hdmatx->XferAbortCallback(huart->hdmatx);
  1241. }
  1242. }
  1243. else
  1244. {
  1245. /* Reset Tx transfer counter */
  1246. huart->TxXferCount = 0x00U;
  1247. /* Restore huart->gState to Ready */
  1248. huart->gState = HAL_UART_STATE_READY;
  1249. /* As no DMA to be aborted, call directly user Abort complete callback */
  1250. HAL_UART_AbortTransmitCpltCallback(huart);
  1251. }
  1252. }
  1253. else
  1254. {
  1255. /* Reset Tx transfer counter */
  1256. huart->TxXferCount = 0x00U;
  1257. /* Restore huart->gState to Ready */
  1258. huart->gState = HAL_UART_STATE_READY;
  1259. /* As no DMA to be aborted, call directly user Abort complete callback */
  1260. HAL_UART_AbortTransmitCpltCallback(huart);
  1261. }
  1262. return HAL_OK;
  1263. }
  1264. /**
  1265. * @brief Abort ongoing Receive transfer (Interrupt mode).
  1266. * @param huart UART handle.
  1267. * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
  1268. * This procedure performs following operations :
  1269. * - Disable PPP Interrupts
  1270. * - Disable the DMA transfer in the peripheral register (if enabled)
  1271. * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
  1272. * - Set handle State to READY
  1273. * - At abort completion, call user abort complete callback
  1274. * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be
  1275. * considered as completed only when user abort complete callback is executed (not when exiting function).
  1276. * @retval HAL status
  1277. */
  1278. HAL_StatusTypeDef HAL_UART_AbortReceive_IT(UART_HandleTypeDef *huart)
  1279. {
  1280. /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
  1281. CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE));
  1282. CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
  1283. /* Disable the UART DMA Rx request if enabled */
  1284. if(HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR))
  1285. {
  1286. CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR);
  1287. /* Abort the UART DMA Rx channel : use blocking DMA Abort API (no callback) */
  1288. if(huart->hdmarx != NULL)
  1289. {
  1290. /* Set the UART DMA Abort callback :
  1291. will lead to call HAL_UART_AbortCpltCallback() at end of DMA abort procedure */
  1292. huart->hdmarx->XferAbortCallback = UART_DMARxOnlyAbortCallback;
  1293. /* Abort DMA RX */
  1294. if(HAL_DMA_Abort_IT(huart->hdmarx) != HAL_OK)
  1295. {
  1296. /* Call Directly huart->hdmarx->XferAbortCallback function in case of error */
  1297. huart->hdmarx->XferAbortCallback(huart->hdmarx);
  1298. }
  1299. }
  1300. else
  1301. {
  1302. /* Reset Rx transfer counter */
  1303. huart->RxXferCount = 0x00U;
  1304. /* Restore huart->RxState to Ready */
  1305. huart->RxState = HAL_UART_STATE_READY;
  1306. /* As no DMA to be aborted, call directly user Abort complete callback */
  1307. HAL_UART_AbortReceiveCpltCallback(huart);
  1308. }
  1309. }
  1310. else
  1311. {
  1312. /* Reset Rx transfer counter */
  1313. huart->RxXferCount = 0x00U;
  1314. /* Restore huart->RxState to Ready */
  1315. huart->RxState = HAL_UART_STATE_READY;
  1316. /* As no DMA to be aborted, call directly user Abort complete callback */
  1317. HAL_UART_AbortReceiveCpltCallback(huart);
  1318. }
  1319. return HAL_OK;
  1320. }
  1321. /**
  1322. * @brief This function handles UART interrupt request.
  1323. * @param huart: pointer to a UART_HandleTypeDef structure that contains
  1324. * the configuration information for the specified UART module.
  1325. * @retval None
  1326. */
  1327. void HAL_UART_IRQHandler(UART_HandleTypeDef *huart)
  1328. {
  1329. uint32_t isrflags = READ_REG(huart->Instance->SR);
  1330. uint32_t cr1its = READ_REG(huart->Instance->CR1);
  1331. uint32_t cr3its = READ_REG(huart->Instance->CR3);
  1332. uint32_t errorflags = 0x00U;
  1333. uint32_t dmarequest = 0x00U;
  1334. /* If no error occurs */
  1335. errorflags = (isrflags & (uint32_t)(USART_SR_PE | USART_SR_FE | USART_SR_ORE | USART_SR_NE));
  1336. if(errorflags == RESET)
  1337. {
  1338. /* UART in mode Receiver -------------------------------------------------*/
  1339. if(((isrflags & USART_SR_RXNE) != RESET) && ((cr1its & USART_CR1_RXNEIE) != RESET))
  1340. {
  1341. UART_Receive_IT(huart);
  1342. return;
  1343. }
  1344. }
  1345. /* If some errors occur */
  1346. if((errorflags != RESET) && (((cr3its & USART_CR3_EIE) != RESET) || ((cr1its & (USART_CR1_RXNEIE | USART_CR1_PEIE)) != RESET)))
  1347. {
  1348. /* UART parity error interrupt occurred ----------------------------------*/
  1349. if(((isrflags & USART_SR_PE) != RESET) && ((cr1its & USART_CR1_PEIE) != RESET))
  1350. {
  1351. huart->ErrorCode |= HAL_UART_ERROR_PE;
  1352. }
  1353. /* UART noise error interrupt occurred -----------------------------------*/
  1354. if(((isrflags & USART_SR_NE) != RESET) && ((cr3its & USART_CR3_EIE) != RESET))
  1355. {
  1356. huart->ErrorCode |= HAL_UART_ERROR_NE;
  1357. }
  1358. /* UART frame error interrupt occurred -----------------------------------*/
  1359. if(((isrflags & USART_SR_FE) != RESET) && ((cr3its & USART_CR3_EIE) != RESET))
  1360. {
  1361. huart->ErrorCode |= HAL_UART_ERROR_FE;
  1362. }
  1363. /* UART Over-Run interrupt occurred --------------------------------------*/
  1364. if(((isrflags & USART_SR_ORE) != RESET) && ((cr3its & USART_CR3_EIE) != RESET))
  1365. {
  1366. huart->ErrorCode |= HAL_UART_ERROR_ORE;
  1367. }
  1368. /* Call UART Error Call back function if need be --------------------------*/
  1369. if(huart->ErrorCode != HAL_UART_ERROR_NONE)
  1370. {
  1371. /* UART in mode Receiver -----------------------------------------------*/
  1372. if(((isrflags & USART_SR_RXNE) != RESET) && ((cr1its & USART_CR1_RXNEIE) != RESET))
  1373. {
  1374. UART_Receive_IT(huart);
  1375. }
  1376. /* If Overrun error occurs, or if any error occurs in DMA mode reception,
  1377. consider error as blocking */
  1378. dmarequest = HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR);
  1379. if(((huart->ErrorCode & HAL_UART_ERROR_ORE) != RESET) || dmarequest)
  1380. {
  1381. /* Blocking error : transfer is aborted
  1382. Set the UART state ready to be able to start again the process,
  1383. Disable Rx Interrupts, and disable Rx DMA request, if ongoing */
  1384. UART_EndRxTransfer(huart);
  1385. /* Disable the UART DMA Rx request if enabled */
  1386. if(HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR))
  1387. {
  1388. CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR);
  1389. /* Abort the UART DMA Rx channel */
  1390. if(huart->hdmarx != NULL)
  1391. {
  1392. /* Set the UART DMA Abort callback :
  1393. will lead to call HAL_UART_ErrorCallback() at end of DMA abort procedure */
  1394. huart->hdmarx->XferAbortCallback = UART_DMAAbortOnError;
  1395. if(HAL_DMA_Abort_IT(huart->hdmarx) != HAL_OK)
  1396. {
  1397. /* Call Directly XferAbortCallback function in case of error */
  1398. huart->hdmarx->XferAbortCallback(huart->hdmarx);
  1399. }
  1400. }
  1401. else
  1402. {
  1403. /* Call user error callback */
  1404. HAL_UART_ErrorCallback(huart);
  1405. }
  1406. }
  1407. else
  1408. {
  1409. /* Call user error callback */
  1410. HAL_UART_ErrorCallback(huart);
  1411. }
  1412. }
  1413. else
  1414. {
  1415. /* Non Blocking error : transfer could go on.
  1416. Error is notified to user through user error callback */
  1417. HAL_UART_ErrorCallback(huart);
  1418. huart->ErrorCode = HAL_UART_ERROR_NONE;
  1419. }
  1420. }
  1421. return;
  1422. } /* End if some error occurs */
  1423. /* UART in mode Transmitter ------------------------------------------------*/
  1424. if(((isrflags & USART_SR_TXE) != RESET) && ((cr1its & USART_CR1_TXEIE) != RESET))
  1425. {
  1426. UART_Transmit_IT(huart);
  1427. return;
  1428. }
  1429. /* UART in mode Transmitter end --------------------------------------------*/
  1430. if(((isrflags & USART_SR_TC) != RESET) && ((cr1its & USART_CR1_TCIE) != RESET))
  1431. {
  1432. UART_EndTransmit_IT(huart);
  1433. return;
  1434. }
  1435. }
  1436. /**
  1437. * @brief Tx Transfer completed callbacks.
  1438. * @param huart: pointer to a UART_HandleTypeDef structure that contains
  1439. * the configuration information for the specified UART module.
  1440. * @retval None
  1441. */
  1442. __weak void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
  1443. {
  1444. /* Prevent unused argument(s) compilation warning */
  1445. UNUSED(huart);
  1446. /* NOTE: This function Should not be modified, when the callback is needed,
  1447. the HAL_UART_TxCpltCallback could be implemented in the user file
  1448. */
  1449. }
  1450. /**
  1451. * @brief Tx Half Transfer completed callbacks.
  1452. * @param huart: pointer to a UART_HandleTypeDef structure that contains
  1453. * the configuration information for the specified UART module.
  1454. * @retval None
  1455. */
  1456. __weak void HAL_UART_TxHalfCpltCallback(UART_HandleTypeDef *huart)
  1457. {
  1458. /* Prevent unused argument(s) compilation warning */
  1459. UNUSED(huart);
  1460. /* NOTE: This function Should not be modified, when the callback is needed,
  1461. the HAL_UART_TxHalfCpltCallback could be implemented in the user file
  1462. */
  1463. }
  1464. /**
  1465. * @brief Rx Transfer completed callbacks.
  1466. * @param huart: pointer to a UART_HandleTypeDef structure that contains
  1467. * the configuration information for the specified UART module.
  1468. * @retval None
  1469. */
  1470. __weak void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
  1471. {
  1472. /* Prevent unused argument(s) compilation warning */
  1473. UNUSED(huart);
  1474. /* NOTE: This function Should not be modified, when the callback is needed,
  1475. the HAL_UART_RxCpltCallback could be implemented in the user file
  1476. */
  1477. }
  1478. /**
  1479. * @brief Rx Half Transfer completed callbacks.
  1480. * @param huart: pointer to a UART_HandleTypeDef structure that contains
  1481. * the configuration information for the specified UART module.
  1482. * @retval None
  1483. */
  1484. __weak void HAL_UART_RxHalfCpltCallback(UART_HandleTypeDef *huart)
  1485. {
  1486. /* Prevent unused argument(s) compilation warning */
  1487. UNUSED(huart);
  1488. /* NOTE: This function Should not be modified, when the callback is needed,
  1489. the HAL_UART_RxHalfCpltCallback could be implemented in the user file
  1490. */
  1491. }
  1492. /**
  1493. * @brief UART error callbacks.
  1494. * @param huart: pointer to a UART_HandleTypeDef structure that contains
  1495. * the configuration information for the specified UART module.
  1496. * @retval None
  1497. */
  1498. __weak void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart)
  1499. {
  1500. /* Prevent unused argument(s) compilation warning */
  1501. UNUSED(huart);
  1502. /* NOTE: This function Should not be modified, when the callback is needed,
  1503. the HAL_UART_ErrorCallback could be implemented in the user file
  1504. */
  1505. }
  1506. /**
  1507. * @brief UART Abort Complete callback.
  1508. * @param huart UART handle.
  1509. * @retval None
  1510. */
  1511. __weak void HAL_UART_AbortCpltCallback (UART_HandleTypeDef *huart)
  1512. {
  1513. /* Prevent unused argument(s) compilation warning */
  1514. UNUSED(huart);
  1515. /* NOTE : This function should not be modified, when the callback is needed,
  1516. the HAL_UART_AbortCpltCallback can be implemented in the user file.
  1517. */
  1518. }
  1519. /**
  1520. * @brief UART Abort Complete callback.
  1521. * @param huart UART handle.
  1522. * @retval None
  1523. */
  1524. __weak void HAL_UART_AbortTransmitCpltCallback (UART_HandleTypeDef *huart)
  1525. {
  1526. /* Prevent unused argument(s) compilation warning */
  1527. UNUSED(huart);
  1528. /* NOTE : This function should not be modified, when the callback is needed,
  1529. the HAL_UART_AbortTransmitCpltCallback can be implemented in the user file.
  1530. */
  1531. }
  1532. /**
  1533. * @brief UART Abort Receive Complete callback.
  1534. * @param huart UART handle.
  1535. * @retval None
  1536. */
  1537. __weak void HAL_UART_AbortReceiveCpltCallback (UART_HandleTypeDef *huart)
  1538. {
  1539. /* Prevent unused argument(s) compilation warning */
  1540. UNUSED(huart);
  1541. /* NOTE : This function should not be modified, when the callback is needed,
  1542. the HAL_UART_AbortReceiveCpltCallback can be implemented in the user file.
  1543. */
  1544. }
  1545. /**
  1546. * @}
  1547. */
  1548. /** @defgroup UART_Exported_Functions_Group3 Peripheral Control functions
  1549. * @brief UART control functions
  1550. *
  1551. @verbatim
  1552. ==============================================================================
  1553. ##### Peripheral Control functions #####
  1554. ==============================================================================
  1555. [..]
  1556. This subsection provides a set of functions allowing to control the UART:
  1557. (+) HAL_LIN_SendBreak() API can be helpful to transmit the break character.
  1558. (+) HAL_MultiProcessor_EnterMuteMode() API can be helpful to enter the UART in mute mode.
  1559. (+) HAL_MultiProcessor_ExitMuteMode() API can be helpful to exit the UART mute mode by software.
  1560. (+) HAL_HalfDuplex_EnableTransmitter() API to enable the UART transmitter and disables the UART receiver in Half Duplex mode
  1561. (+) HAL_HalfDuplex_EnableReceiver() API to enable the UART receiver and disables the UART transmitter in Half Duplex mode
  1562. @endverbatim
  1563. * @{
  1564. */
  1565. /**
  1566. * @brief Transmits break characters.
  1567. * @param huart: pointer to a UART_HandleTypeDef structure that contains
  1568. * the configuration information for the specified UART module.
  1569. * @retval HAL status
  1570. */
  1571. HAL_StatusTypeDef HAL_LIN_SendBreak(UART_HandleTypeDef *huart)
  1572. {
  1573. /* Check the parameters */
  1574. assert_param(IS_UART_INSTANCE(huart->Instance));
  1575. /* Process Locked */
  1576. __HAL_LOCK(huart);
  1577. huart->gState = HAL_UART_STATE_BUSY;
  1578. /* Send break characters */
  1579. SET_BIT(huart->Instance->CR1, USART_CR1_SBK);
  1580. huart->gState = HAL_UART_STATE_READY;
  1581. /* Process Unlocked */
  1582. __HAL_UNLOCK(huart);
  1583. return HAL_OK;
  1584. }
  1585. /**
  1586. * @brief Enters the UART in mute mode.
  1587. * @param huart: pointer to a UART_HandleTypeDef structure that contains
  1588. * the configuration information for the specified UART module.
  1589. * @retval HAL status
  1590. */
  1591. HAL_StatusTypeDef HAL_MultiProcessor_EnterMuteMode(UART_HandleTypeDef *huart)
  1592. {
  1593. /* Check the parameters */
  1594. assert_param(IS_UART_INSTANCE(huart->Instance));
  1595. /* Process Locked */
  1596. __HAL_LOCK(huart);
  1597. huart->gState = HAL_UART_STATE_BUSY;
  1598. /* Enable the USART mute mode by setting the RWU bit in the CR1 register */
  1599. SET_BIT(huart->Instance->CR1, USART_CR1_RWU);
  1600. huart->gState = HAL_UART_STATE_READY;
  1601. /* Process Unlocked */
  1602. __HAL_UNLOCK(huart);
  1603. return HAL_OK;
  1604. }
  1605. /**
  1606. * @brief Exits the UART mute mode: wake up software.
  1607. * @param huart: pointer to a UART_HandleTypeDef structure that contains
  1608. * the configuration information for the specified UART module.
  1609. * @retval HAL status
  1610. */
  1611. HAL_StatusTypeDef HAL_MultiProcessor_ExitMuteMode(UART_HandleTypeDef *huart)
  1612. {
  1613. /* Check the parameters */
  1614. assert_param(IS_UART_INSTANCE(huart->Instance));
  1615. /* Process Locked */
  1616. __HAL_LOCK(huart);
  1617. huart->gState = HAL_UART_STATE_BUSY;
  1618. /* Disable the USART mute mode by clearing the RWU bit in the CR1 register */
  1619. CLEAR_BIT(huart->Instance->CR1, USART_CR1_RWU);
  1620. huart->gState = HAL_UART_STATE_READY;
  1621. /* Process Unlocked */
  1622. __HAL_UNLOCK(huart);
  1623. return HAL_OK;
  1624. }
  1625. /**
  1626. * @brief Enables the UART transmitter and disables the UART receiver.
  1627. * @param huart: pointer to a UART_HandleTypeDef structure that contains
  1628. * the configuration information for the specified UART module.
  1629. * @retval HAL status
  1630. */
  1631. HAL_StatusTypeDef HAL_HalfDuplex_EnableTransmitter(UART_HandleTypeDef *huart)
  1632. {
  1633. uint32_t tmpreg = 0x00U;
  1634. /* Process Locked */
  1635. __HAL_LOCK(huart);
  1636. huart->gState = HAL_UART_STATE_BUSY;
  1637. /*-------------------------- USART CR1 Configuration -----------------------*/
  1638. tmpreg = huart->Instance->CR1;
  1639. /* Clear TE and RE bits */
  1640. tmpreg &= (uint32_t)~((uint32_t)(USART_CR1_TE | USART_CR1_RE));
  1641. /* Enable the USART's transmit interface by setting the TE bit in the USART CR1 register */
  1642. tmpreg |= (uint32_t)USART_CR1_TE;
  1643. /* Write to USART CR1 */
  1644. WRITE_REG(huart->Instance->CR1, (uint32_t)tmpreg);
  1645. huart->gState = HAL_UART_STATE_READY;
  1646. /* Process Unlocked */
  1647. __HAL_UNLOCK(huart);
  1648. return HAL_OK;
  1649. }
  1650. /**
  1651. * @brief Enables the UART receiver and disables the UART transmitter.
  1652. * @param huart: pointer to a UART_HandleTypeDef structure that contains
  1653. * the configuration information for the specified UART module.
  1654. * @retval HAL status
  1655. */
  1656. HAL_StatusTypeDef HAL_HalfDuplex_EnableReceiver(UART_HandleTypeDef *huart)
  1657. {
  1658. uint32_t tmpreg = 0x00U;
  1659. /* Process Locked */
  1660. __HAL_LOCK(huart);
  1661. huart->gState = HAL_UART_STATE_BUSY;
  1662. /*-------------------------- USART CR1 Configuration -----------------------*/
  1663. tmpreg = huart->Instance->CR1;
  1664. /* Clear TE and RE bits */
  1665. tmpreg &= (uint32_t)~((uint32_t)(USART_CR1_TE | USART_CR1_RE));
  1666. /* Enable the USART's receive interface by setting the RE bit in the USART CR1 register */
  1667. tmpreg |= (uint32_t)USART_CR1_RE;
  1668. /* Write to USART CR1 */
  1669. WRITE_REG(huart->Instance->CR1, (uint32_t)tmpreg);
  1670. huart->gState = HAL_UART_STATE_READY;
  1671. /* Process Unlocked */
  1672. __HAL_UNLOCK(huart);
  1673. return HAL_OK;
  1674. }
  1675. /**
  1676. * @}
  1677. */
  1678. /** @defgroup UART_Exported_Functions_Group4 Peripheral State and Errors functions
  1679. * @brief UART State and Errors functions
  1680. *
  1681. @verbatim
  1682. ==============================================================================
  1683. ##### Peripheral State and Errors functions #####
  1684. ==============================================================================
  1685. [..]
  1686. This subsection provides a set of functions allowing to return the State of
  1687. UART communication process, return Peripheral Errors occurred during communication
  1688. process
  1689. (+) HAL_UART_GetState() API can be helpful to check in run-time the state of the UART peripheral.
  1690. (+) HAL_UART_GetError() check in run-time errors that could be occurred during communication.
  1691. @endverbatim
  1692. * @{
  1693. */
  1694. /**
  1695. * @brief Returns the UART state.
  1696. * @param huart: pointer to a UART_HandleTypeDef structure that contains
  1697. * the configuration information for the specified UART module.
  1698. * @retval HAL state
  1699. */
  1700. HAL_UART_StateTypeDef HAL_UART_GetState(UART_HandleTypeDef *huart)
  1701. {
  1702. uint32_t temp1= 0x00U, temp2 = 0x00U;
  1703. temp1 = huart->gState;
  1704. temp2 = huart->RxState;
  1705. return (HAL_UART_StateTypeDef)(temp1 | temp2);
  1706. }
  1707. /**
  1708. * @brief Return the UART error code
  1709. * @param huart : pointer to a UART_HandleTypeDef structure that contains
  1710. * the configuration information for the specified UART.
  1711. * @retval UART Error Code
  1712. */
  1713. uint32_t HAL_UART_GetError(UART_HandleTypeDef *huart)
  1714. {
  1715. return huart->ErrorCode;
  1716. }
  1717. /**
  1718. * @}
  1719. */
  1720. /**
  1721. * @brief DMA UART transmit process complete callback.
  1722. * @param hdma: DMA handle
  1723. * @retval None
  1724. */
  1725. static void UART_DMATransmitCplt(DMA_HandleTypeDef *hdma)
  1726. {
  1727. UART_HandleTypeDef* huart = ( UART_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1728. /* DMA Normal mode*/
  1729. if((hdma->Instance->CCR & DMA_CCR_CIRC) == 0U)
  1730. {
  1731. huart->TxXferCount = 0U;
  1732. /* Disable the DMA transfer for transmit request by setting the DMAT bit
  1733. in the UART CR3 register */
  1734. CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAT);
  1735. /* Enable the UART Transmit Complete Interrupt */
  1736. SET_BIT(huart->Instance->CR1, USART_CR1_TCIE);
  1737. }
  1738. /* DMA Circular mode */
  1739. else
  1740. {
  1741. HAL_UART_TxCpltCallback(huart);
  1742. }
  1743. }
  1744. /**
  1745. * @brief DMA UART transmit process half complete callback
  1746. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  1747. * the configuration information for the specified DMA module.
  1748. * @retval None
  1749. */
  1750. static void UART_DMATxHalfCplt(DMA_HandleTypeDef *hdma)
  1751. {
  1752. UART_HandleTypeDef* huart = (UART_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
  1753. HAL_UART_TxHalfCpltCallback(huart);
  1754. }
  1755. /**
  1756. * @brief DMA UART receive process complete callback.
  1757. * @param hdma: DMA handle
  1758. * @retval None
  1759. */
  1760. static void UART_DMAReceiveCplt(DMA_HandleTypeDef *hdma)
  1761. {
  1762. UART_HandleTypeDef* huart = ( UART_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1763. /* DMA Normal mode*/
  1764. if((hdma->Instance->CCR & DMA_CCR_CIRC) == 0U)
  1765. {
  1766. huart->RxXferCount = 0U;
  1767. /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
  1768. CLEAR_BIT(huart->Instance->CR1, USART_CR1_PEIE);
  1769. CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
  1770. /* Disable the DMA transfer for the receiver request by setting the DMAR bit
  1771. in the UART CR3 register */
  1772. CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR);
  1773. /* At end of Rx process, restore huart->RxState to Ready */
  1774. huart->RxState = HAL_UART_STATE_READY;
  1775. }
  1776. HAL_UART_RxCpltCallback(huart);
  1777. }
  1778. /**
  1779. * @brief DMA UART receive process half complete callback
  1780. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  1781. * the configuration information for the specified DMA module.
  1782. * @retval None
  1783. */
  1784. static void UART_DMARxHalfCplt(DMA_HandleTypeDef *hdma)
  1785. {
  1786. UART_HandleTypeDef* huart = (UART_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
  1787. HAL_UART_RxHalfCpltCallback(huart);
  1788. }
  1789. /**
  1790. * @brief DMA UART communication error callback.
  1791. * @param hdma: DMA handle
  1792. * @retval None
  1793. */
  1794. static void UART_DMAError(DMA_HandleTypeDef *hdma)
  1795. {
  1796. uint32_t dmarequest = 0x00U;
  1797. UART_HandleTypeDef* huart = ( UART_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1798. /* Stop UART DMA Tx request if ongoing */
  1799. dmarequest = HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT);
  1800. if((huart->gState == HAL_UART_STATE_BUSY_TX) && dmarequest)
  1801. {
  1802. huart->TxXferCount = 0U;
  1803. UART_EndTxTransfer(huart);
  1804. }
  1805. /* Stop UART DMA Rx request if ongoing */
  1806. dmarequest = HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR);
  1807. if((huart->RxState == HAL_UART_STATE_BUSY_RX) && dmarequest)
  1808. {
  1809. huart->RxXferCount = 0U;
  1810. UART_EndRxTransfer(huart);
  1811. }
  1812. huart->ErrorCode |= HAL_UART_ERROR_DMA;
  1813. HAL_UART_ErrorCallback(huart);
  1814. }
  1815. /**
  1816. * @brief This function handles UART Communication Timeout.
  1817. * @param huart: pointer to a UART_HandleTypeDef structure that contains
  1818. * the configuration information for the specified UART module.
  1819. * @param Flag: specifies the UART flag to check.
  1820. * @param Status: The new Flag status (SET or RESET).
  1821. * @param Tickstart Tick start value
  1822. * @param Timeout: Timeout duration
  1823. * @retval HAL status
  1824. */
  1825. static HAL_StatusTypeDef UART_WaitOnFlagUntilTimeout(UART_HandleTypeDef *huart, uint32_t Flag, FlagStatus Status, uint32_t Tickstart, uint32_t Timeout)
  1826. {
  1827. /* Wait until flag is set */
  1828. while((__HAL_UART_GET_FLAG(huart, Flag) ? SET : RESET) == Status)
  1829. {
  1830. /* Check for the Timeout */
  1831. if(Timeout != HAL_MAX_DELAY)
  1832. {
  1833. if((Timeout == 0U)||((HAL_GetTick() - Tickstart ) > Timeout))
  1834. {
  1835. /* Disable TXE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts for the interrupt process */
  1836. CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE | USART_CR1_TXEIE));
  1837. CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
  1838. huart->gState = HAL_UART_STATE_READY;
  1839. huart->RxState = HAL_UART_STATE_READY;
  1840. /* Process Unlocked */
  1841. __HAL_UNLOCK(huart);
  1842. return HAL_TIMEOUT;
  1843. }
  1844. }
  1845. }
  1846. return HAL_OK;
  1847. }
  1848. /**
  1849. * @brief End ongoing Tx transfer on UART peripheral (following error detection or Transmit completion).
  1850. * @param huart: UART handle.
  1851. * @retval None
  1852. */
  1853. static void UART_EndTxTransfer(UART_HandleTypeDef *huart)
  1854. {
  1855. /* Disable TXEIE and TCIE interrupts */
  1856. CLEAR_BIT(huart->Instance->CR1, (USART_CR1_TXEIE | USART_CR1_TCIE));
  1857. /* At end of Tx process, restore huart->gState to Ready */
  1858. huart->gState = HAL_UART_STATE_READY;
  1859. }
  1860. /**
  1861. * @brief End ongoing Rx transfer on UART peripheral (following error detection or Reception completion).
  1862. * @param huart: UART handle.
  1863. * @retval None
  1864. */
  1865. static void UART_EndRxTransfer(UART_HandleTypeDef *huart)
  1866. {
  1867. /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
  1868. CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE));
  1869. CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
  1870. /* At end of Rx process, restore huart->RxState to Ready */
  1871. huart->RxState = HAL_UART_STATE_READY;
  1872. }
  1873. /**
  1874. * @brief DMA UART communication abort callback, when initiated by HAL services on Error
  1875. * (To be called at end of DMA Abort procedure following error occurrence).
  1876. * @param hdma DMA handle.
  1877. * @retval None
  1878. */
  1879. static void UART_DMAAbortOnError(DMA_HandleTypeDef *hdma)
  1880. {
  1881. UART_HandleTypeDef* huart = ( UART_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1882. huart->RxXferCount = 0x00U;
  1883. huart->TxXferCount = 0x00U;
  1884. HAL_UART_ErrorCallback(huart);
  1885. }
  1886. /**
  1887. * @brief DMA UART Tx communication abort callback, when initiated by user
  1888. * (To be called at end of DMA Tx Abort procedure following user abort request).
  1889. * @note When this callback is executed, User Abort complete call back is called only if no
  1890. * Abort still ongoing for Rx DMA Handle.
  1891. * @param hdma DMA handle.
  1892. * @retval None
  1893. */
  1894. static void UART_DMATxAbortCallback(DMA_HandleTypeDef *hdma)
  1895. {
  1896. UART_HandleTypeDef* huart = ( UART_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1897. huart->hdmatx->XferAbortCallback = NULL;
  1898. /* Check if an Abort process is still ongoing */
  1899. if(huart->hdmarx != NULL)
  1900. {
  1901. if(huart->hdmarx->XferAbortCallback != NULL)
  1902. {
  1903. return;
  1904. }
  1905. }
  1906. /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */
  1907. huart->TxXferCount = 0x00U;
  1908. huart->RxXferCount = 0x00U;
  1909. /* Reset ErrorCode */
  1910. huart->ErrorCode = HAL_UART_ERROR_NONE;
  1911. /* Restore huart->gState and huart->RxState to Ready */
  1912. huart->gState = HAL_UART_STATE_READY;
  1913. huart->RxState = HAL_UART_STATE_READY;
  1914. /* Call user Abort complete callback */
  1915. HAL_UART_AbortCpltCallback(huart);
  1916. }
  1917. /**
  1918. * @brief DMA UART Rx communication abort callback, when initiated by user
  1919. * (To be called at end of DMA Rx Abort procedure following user abort request).
  1920. * @note When this callback is executed, User Abort complete call back is called only if no
  1921. * Abort still ongoing for Tx DMA Handle.
  1922. * @param hdma DMA handle.
  1923. * @retval None
  1924. */
  1925. static void UART_DMARxAbortCallback(DMA_HandleTypeDef *hdma)
  1926. {
  1927. UART_HandleTypeDef* huart = ( UART_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1928. huart->hdmarx->XferAbortCallback = NULL;
  1929. /* Check if an Abort process is still ongoing */
  1930. if(huart->hdmatx != NULL)
  1931. {
  1932. if(huart->hdmatx->XferAbortCallback != NULL)
  1933. {
  1934. return;
  1935. }
  1936. }
  1937. /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */
  1938. huart->TxXferCount = 0x00U;
  1939. huart->RxXferCount = 0x00U;
  1940. /* Reset ErrorCode */
  1941. huart->ErrorCode = HAL_UART_ERROR_NONE;
  1942. /* Restore huart->gState and huart->RxState to Ready */
  1943. huart->gState = HAL_UART_STATE_READY;
  1944. huart->RxState = HAL_UART_STATE_READY;
  1945. /* Call user Abort complete callback */
  1946. HAL_UART_AbortCpltCallback(huart);
  1947. }
  1948. /**
  1949. * @brief DMA UART Tx communication abort callback, when initiated by user by a call to
  1950. * HAL_UART_AbortTransmit_IT API (Abort only Tx transfer)
  1951. * (This callback is executed at end of DMA Tx Abort procedure following user abort request,
  1952. * and leads to user Tx Abort Complete callback execution).
  1953. * @param hdma DMA handle.
  1954. * @retval None
  1955. */
  1956. static void UART_DMATxOnlyAbortCallback(DMA_HandleTypeDef *hdma)
  1957. {
  1958. UART_HandleTypeDef* huart = ( UART_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1959. huart->TxXferCount = 0x00U;
  1960. /* Restore huart->gState to Ready */
  1961. huart->gState = HAL_UART_STATE_READY;
  1962. /* Call user Abort complete callback */
  1963. HAL_UART_AbortTransmitCpltCallback(huart);
  1964. }
  1965. /**
  1966. * @brief DMA UART Rx communication abort callback, when initiated by user by a call to
  1967. * HAL_UART_AbortReceive_IT API (Abort only Rx transfer)
  1968. * (This callback is executed at end of DMA Rx Abort procedure following user abort request,
  1969. * and leads to user Rx Abort Complete callback execution).
  1970. * @param hdma DMA handle.
  1971. * @retval None
  1972. */
  1973. static void UART_DMARxOnlyAbortCallback(DMA_HandleTypeDef *hdma)
  1974. {
  1975. UART_HandleTypeDef* huart = ( UART_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1976. huart->RxXferCount = 0x00U;
  1977. /* Restore huart->RxState to Ready */
  1978. huart->RxState = HAL_UART_STATE_READY;
  1979. /* Call user Abort complete callback */
  1980. HAL_UART_AbortReceiveCpltCallback(huart);
  1981. }
  1982. /**
  1983. * @brief Sends an amount of data in non blocking mode.
  1984. * @param huart: Pointer to a UART_HandleTypeDef structure that contains
  1985. * the configuration information for the specified UART module.
  1986. * @retval HAL status
  1987. */
  1988. static HAL_StatusTypeDef UART_Transmit_IT(UART_HandleTypeDef *huart)
  1989. {
  1990. uint16_t* tmp;
  1991. /* Check that a Tx process is ongoing */
  1992. if(huart->gState == HAL_UART_STATE_BUSY_TX)
  1993. {
  1994. if(huart->Init.WordLength == UART_WORDLENGTH_9B)
  1995. {
  1996. tmp = (uint16_t*) huart->pTxBuffPtr;
  1997. huart->Instance->DR = (uint16_t)(*tmp & (uint16_t)0x01FF);
  1998. if(huart->Init.Parity == UART_PARITY_NONE)
  1999. {
  2000. huart->pTxBuffPtr += 2U;
  2001. }
  2002. else
  2003. {
  2004. huart->pTxBuffPtr += 1U;
  2005. }
  2006. }
  2007. else
  2008. {
  2009. huart->Instance->DR = (uint8_t)(*huart->pTxBuffPtr++ & (uint8_t)0x00FF);
  2010. }
  2011. if(--huart->TxXferCount == 0U)
  2012. {
  2013. /* Disable the UART Transmit Complete Interrupt */
  2014. __HAL_UART_DISABLE_IT(huart, UART_IT_TXE);
  2015. /* Enable the UART Transmit Complete Interrupt */
  2016. __HAL_UART_ENABLE_IT(huart, UART_IT_TC);
  2017. }
  2018. return HAL_OK;
  2019. }
  2020. else
  2021. {
  2022. return HAL_BUSY;
  2023. }
  2024. }
  2025. /**
  2026. * @brief Wraps up transmission in non blocking mode.
  2027. * @param huart: pointer to a UART_HandleTypeDef structure that contains
  2028. * the configuration information for the specified UART module.
  2029. * @retval HAL status
  2030. */
  2031. static HAL_StatusTypeDef UART_EndTransmit_IT(UART_HandleTypeDef *huart)
  2032. {
  2033. /* Disable the UART Transmit Complete Interrupt */
  2034. __HAL_UART_DISABLE_IT(huart, UART_IT_TC);
  2035. /* Tx process is ended, restore huart->gState to Ready */
  2036. huart->gState = HAL_UART_STATE_READY;
  2037. HAL_UART_TxCpltCallback(huart);
  2038. return HAL_OK;
  2039. }
  2040. /**
  2041. * @brief Receives an amount of data in non blocking mode
  2042. * @param huart: pointer to a UART_HandleTypeDef structure that contains
  2043. * the configuration information for the specified UART module.
  2044. * @retval HAL status
  2045. */
  2046. static HAL_StatusTypeDef UART_Receive_IT(UART_HandleTypeDef *huart)
  2047. {
  2048. uint16_t* tmp;
  2049. /* Check that a Rx process is ongoing */
  2050. if(huart->RxState == HAL_UART_STATE_BUSY_RX)
  2051. {
  2052. if(huart->Init.WordLength == UART_WORDLENGTH_9B)
  2053. {
  2054. tmp = (uint16_t*) huart->pRxBuffPtr;
  2055. if(huart->Init.Parity == UART_PARITY_NONE)
  2056. {
  2057. *tmp = (uint16_t)(huart->Instance->DR & (uint16_t)0x01FF);
  2058. huart->pRxBuffPtr += 2U;
  2059. }
  2060. else
  2061. {
  2062. *tmp = (uint16_t)(huart->Instance->DR & (uint16_t)0x00FF);
  2063. huart->pRxBuffPtr += 1U;
  2064. }
  2065. }
  2066. else
  2067. {
  2068. if(huart->Init.Parity == UART_PARITY_NONE)
  2069. {
  2070. *huart->pRxBuffPtr++ = (uint8_t)(huart->Instance->DR & (uint8_t)0x00FF);
  2071. }
  2072. else
  2073. {
  2074. *huart->pRxBuffPtr++ = (uint8_t)(huart->Instance->DR & (uint8_t)0x007F);
  2075. }
  2076. }
  2077. if(--huart->RxXferCount == 0U)
  2078. {
  2079. /* Disable the IRDA Data Register not empty Interrupt */
  2080. __HAL_UART_DISABLE_IT(huart, UART_IT_RXNE);
  2081. /* Disable the UART Parity Error Interrupt */
  2082. __HAL_UART_DISABLE_IT(huart, UART_IT_PE);
  2083. /* Disable the UART Error Interrupt: (Frame error, noise error, overrun error) */
  2084. __HAL_UART_DISABLE_IT(huart, UART_IT_ERR);
  2085. /* Rx process is completed, restore huart->RxState to Ready */
  2086. huart->RxState = HAL_UART_STATE_READY;
  2087. HAL_UART_RxCpltCallback(huart);
  2088. return HAL_OK;
  2089. }
  2090. return HAL_OK;
  2091. }
  2092. else
  2093. {
  2094. return HAL_BUSY;
  2095. }
  2096. }
  2097. /**
  2098. * @brief Configures the UART peripheral.
  2099. * @param huart: pointer to a UART_HandleTypeDef structure that contains
  2100. * the configuration information for the specified UART module.
  2101. * @retval None
  2102. */
  2103. static void UART_SetConfig(UART_HandleTypeDef *huart)
  2104. {
  2105. uint32_t tmpreg = 0x00U;
  2106. /* Check the parameters */
  2107. assert_param(IS_UART_BAUDRATE(huart->Init.BaudRate));
  2108. assert_param(IS_UART_STOPBITS(huart->Init.StopBits));
  2109. assert_param(IS_UART_PARITY(huart->Init.Parity));
  2110. assert_param(IS_UART_MODE(huart->Init.Mode));
  2111. /*------- UART-associated USART registers setting : CR2 Configuration ------*/
  2112. /* Configure the UART Stop Bits: Set STOP[13:12] bits according
  2113. * to huart->Init.StopBits value */
  2114. MODIFY_REG(huart->Instance->CR2, USART_CR2_STOP, huart->Init.StopBits);
  2115. /*------- UART-associated USART registers setting : CR1 Configuration ------*/
  2116. /* Configure the UART Word Length, Parity and mode:
  2117. Set the M bits according to huart->Init.WordLength value
  2118. Set PCE and PS bits according to huart->Init.Parity value
  2119. Set TE and RE bits according to huart->Init.Mode value
  2120. Set OVER8 bit according to huart->Init.OverSampling value */
  2121. #if defined(USART_CR1_OVER8)
  2122. tmpreg |= (uint32_t)huart->Init.WordLength | huart->Init.Parity | huart->Init.Mode | huart->Init.OverSampling;
  2123. MODIFY_REG(huart->Instance->CR1,
  2124. (uint32_t)(USART_CR1_M | USART_CR1_PCE | USART_CR1_PS | USART_CR1_TE | USART_CR1_RE | USART_CR1_OVER8),
  2125. tmpreg);
  2126. #else
  2127. tmpreg |= (uint32_t)huart->Init.WordLength | huart->Init.Parity | huart->Init.Mode;
  2128. MODIFY_REG(huart->Instance->CR1,
  2129. (uint32_t)(USART_CR1_M | USART_CR1_PCE | USART_CR1_PS | USART_CR1_TE | USART_CR1_RE),
  2130. tmpreg);
  2131. #endif /* USART_CR1_OVER8 */
  2132. /*------- UART-associated USART registers setting : CR3 Configuration ------*/
  2133. /* Configure the UART HFC: Set CTSE and RTSE bits according to huart->Init.HwFlowCtl value */
  2134. MODIFY_REG(huart->Instance->CR3, (USART_CR3_RTSE | USART_CR3_CTSE), huart->Init.HwFlowCtl);
  2135. #if defined(USART_CR1_OVER8)
  2136. /* Check the Over Sampling */
  2137. if(huart->Init.OverSampling == UART_OVERSAMPLING_8)
  2138. {
  2139. /*-------------------------- USART BRR Configuration ---------------------*/
  2140. if(huart->Instance == USART1)
  2141. {
  2142. huart->Instance->BRR = UART_BRR_SAMPLING8(HAL_RCC_GetPCLK2Freq(), huart->Init.BaudRate);
  2143. }
  2144. else
  2145. {
  2146. huart->Instance->BRR = UART_BRR_SAMPLING8(HAL_RCC_GetPCLK1Freq(), huart->Init.BaudRate);
  2147. }
  2148. }
  2149. else
  2150. {
  2151. /*-------------------------- USART BRR Configuration ---------------------*/
  2152. if(huart->Instance == USART1)
  2153. {
  2154. huart->Instance->BRR = UART_BRR_SAMPLING16(HAL_RCC_GetPCLK2Freq(), huart->Init.BaudRate);
  2155. }
  2156. else
  2157. {
  2158. huart->Instance->BRR = UART_BRR_SAMPLING16(HAL_RCC_GetPCLK1Freq(), huart->Init.BaudRate);
  2159. }
  2160. }
  2161. #else
  2162. /*-------------------------- USART BRR Configuration ---------------------*/
  2163. if(huart->Instance == USART1)
  2164. {
  2165. huart->Instance->BRR = UART_BRR_SAMPLING16(HAL_RCC_GetPCLK2Freq(), huart->Init.BaudRate);
  2166. }
  2167. else
  2168. {
  2169. huart->Instance->BRR = UART_BRR_SAMPLING16(HAL_RCC_GetPCLK1Freq(), huart->Init.BaudRate);
  2170. }
  2171. #endif /* USART_CR1_OVER8 */
  2172. }
  2173. /**
  2174. * @}
  2175. */
  2176. #endif /* HAL_UART_MODULE_ENABLED */
  2177. /**
  2178. * @}
  2179. */
  2180. /**
  2181. * @}
  2182. */
  2183. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/