stm32l4xx_hal_uart.c 115 KB

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