stm32f1xx_hal_uart.c 85 KB

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