stm32l4xx_hal_uart.c 89 KB

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