stm32f3xx_hal_uart.c 95 KB

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