stm32f0xx_hal_uart.c 98 KB

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