stm32l0xx_hal_uart.c 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007
  1. /**
  2. ******************************************************************************
  3. * @file stm32l0xx_hal_uart.c
  4. * @author MCD Application Team
  5. * @version V1.7.0
  6. * @date 31-May-2016
  7. * @brief UART HAL module driver.
  8. *
  9. * This file provides firmware functions to manage the following
  10. * functionalities of the Universal Asynchronous Receiver Transmitter Peripheral (UART).
  11. * + Initialization and de-initialization methods
  12. * + IO operation methods
  13. * + Peripheral Control methods
  14. *
  15. @verbatim
  16. ===============================================================================
  17. ##### How to use this driver #####
  18. ===============================================================================
  19. [..]
  20. The UART HAL driver can be used as follows:
  21. (#) Declare a UART_HandleTypeDef handle structure.
  22. (#) Initialize the UART low level resources by implementing the HAL_UART_MspInit() API:
  23. (##) Enable the USARTx interface clock.
  24. (##) UART pins configuration:
  25. (+++) Enable the clock for the UART GPIOs.
  26. (+++) Configure these UART pins as alternate function pull-up.
  27. (##) NVIC configuration if you need to use interrupt process (HAL_UART_Transmit_IT()
  28. and HAL_UART_Receive_IT() APIs):
  29. (+++) Configure the USARTx interrupt priority.
  30. (+++) Enable the NVIC USART IRQ handle.
  31. (##) DMA Configuration if you need to use DMA process (HAL_UART_Transmit_DMA()
  32. and HAL_UART_Receive_DMA() APIs):
  33. (+++) Declare a DMA handle structure for the Tx/Rx stream.
  34. (+++) Enable the DMAx interface clock.
  35. (+++) Configure the declared DMA handle structure with the required
  36. Tx/Rx parameters.
  37. (+++) Configure the DMA Tx/Rx Stream.
  38. (+++) Associate the initialized DMA handle to the UART DMA Tx/Rx handle.
  39. (+++) Configure the priority and enable the NVIC for the transfer complete
  40. interrupt on the DMA Tx/Rx Stream.
  41. (#) Program the Baud Rate, Word Length, Stop Bit, Parity, Hardware
  42. flow control and Mode(Receiver/Transmitter) in the Init structure.
  43. (#) For the UART asynchronous mode, initialize the UART registers by calling
  44. the HAL_UART_Init() API.
  45. (#) For the UART Half duplex mode, initialize the UART registers by calling
  46. the HAL_HalfDuplex_Init() API.
  47. (#) For the LIN mode, initialize the UART registers by calling the HAL_LIN_Init() API.
  48. (#) For the Multi-Processor mode, initialize the UART registers by calling
  49. the HAL_MultiProcessor_Init() API.
  50. [..]
  51. (@) The specific UART interrupts (Transmission complete interrupt,
  52. RXNE interrupt and Error Interrupts) will be managed using the macros
  53. __HAL_UART_ENABLE_IT() and __HAL_UART_DISABLE_IT() inside the transmit
  54. and receive process.
  55. [..]
  56. (@) These APIs (HAL_UART_Init() and HAL_HalfDuplex_Init()) configure also the
  57. low level Hardware GPIO, CLOCK, CORTEX...etc) by calling the customed
  58. HAL_UART_MspInit() API.
  59. [..]
  60. Three operation modes are available within this driver :
  61. *** Polling mode IO operation ***
  62. =================================
  63. [..]
  64. (+) Send an amount of data in blocking mode using HAL_UART_Transmit()
  65. (+) Receive an amount of data in blocking mode using HAL_UART_Receive()
  66. *** Interrupt mode IO operation ***
  67. ===================================
  68. [..]
  69. (+) Send an amount of data in non blocking mode using HAL_UART_Transmit_IT()
  70. (+) At transmission end of transfer HAL_UART_TxCpltCallback is executed and user can
  71. add his own code by customization of function pointer HAL_UART_TxCpltCallback
  72. (+) Receive an amount of data in non blocking mode using HAL_UART_Receive_IT()
  73. (+) At reception end of transfer HAL_UART_RxCpltCallback is executed and user can
  74. add his own code by customization of function pointer HAL_UART_RxCpltCallback
  75. (+) In case of transfer Error, HAL_UART_ErrorCallback() function is executed and user can
  76. add his own code by customization of function pointer HAL_UART_ErrorCallback
  77. *** DMA mode IO operation ***
  78. ==============================
  79. [..]
  80. (+) Send an amount of data in non blocking mode (DMA) using HAL_UART_Transmit_DMA()
  81. (+) At transmission end of half transfer HAL_UART_TxHalfCpltCallback is executed and user can
  82. add his own code by customization of function pointer HAL_UART_TxHalfCpltCallback
  83. (+) At transmission end of transfer HAL_UART_TxCpltCallback is executed and user can
  84. add his own code by customization of function pointer HAL_UART_TxCpltCallback
  85. (+) Receive an amount of data in non blocking mode (DMA) using HAL_UART_Receive_DMA()
  86. (+) At reception end of half transfer HAL_UART_RxHalfCpltCallback is executed and user can
  87. add his own code by customization of function pointer HAL_UART_RxHalfCpltCallback
  88. (+) At reception end of transfer HAL_UART_RxCpltCallback is executed and user can
  89. add his own code by customization of function pointer HAL_UART_RxCpltCallback
  90. (+) In case of transfer Error, HAL_UART_ErrorCallback() function is executed and user can
  91. add his own code by customization of function pointer HAL_UART_ErrorCallback
  92. (+) Pause the DMA Transfer using HAL_UART_DMAPause()
  93. (+) Resume the DMA Transfer using HAL_UART_DMAResume()
  94. (+) Stop the DMA Transfer using HAL_UART_DMAStop()
  95. *** UART HAL driver macros list ***
  96. =============================================
  97. [..]
  98. Below the list of most used macros in UART HAL driver.
  99. (+) __HAL_UART_ENABLE: Enable the UART peripheral
  100. (+) __HAL_UART_DISABLE: Disable the UART peripheral
  101. (+) __HAL_UART_GET_FLAG : Check whether the specified UART flag is set or not
  102. (+) __HAL_UART_CLEAR_FLAG : Clear the specified UART pending flag
  103. (+) __HAL_UART_ENABLE_IT: Enable the specified UART interrupt
  104. (+) __HAL_UART_DISABLE_IT: Disable the specified UART interrupt
  105. [..]
  106. (@) You can refer to the UART HAL driver header file for more useful macros
  107. @endverbatim
  108. ******************************************************************************
  109. * @attention
  110. *
  111. * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
  112. *
  113. * Redistribution and use in source and binary forms, with or without modification,
  114. * are permitted provided that the following conditions are met:
  115. * 1. Redistributions of source code must retain the above copyright notice,
  116. * this list of conditions and the following disclaimer.
  117. * 2. Redistributions in binary form must reproduce the above copyright notice,
  118. * this list of conditions and the following disclaimer in the documentation
  119. * and/or other materials provided with the distribution.
  120. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  121. * may be used to endorse or promote products derived from this software
  122. * without specific prior written permission.
  123. *
  124. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  125. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  126. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  127. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  128. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  129. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  130. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  131. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  132. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  133. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  134. *
  135. ******************************************************************************
  136. */
  137. /* Includes ------------------------------------------------------------------*/
  138. #include "stm32l0xx_hal.h"
  139. /** @addtogroup STM32L0xx_HAL_Driver
  140. * @{
  141. */
  142. #ifdef HAL_UART_MODULE_ENABLED
  143. /** @addtogroup UART
  144. * @brief UART module driver
  145. * @{
  146. */
  147. /** @addtogroup UART_Private
  148. * @{
  149. */
  150. /* Private typedef -----------------------------------------------------------*/
  151. /* Private define ------------------------------------------------------------*/
  152. #define UART_TIMEOUT_VALUE ((uint32_t) 22000U)
  153. #define UART_CR1_FIELDS ((uint32_t)(USART_CR1_M | USART_CR1_PCE | USART_CR1_PS | \
  154. USART_CR1_TE | USART_CR1_RE | USART_CR1_OVER8))
  155. /* Private macro -------------------------------------------------------------*/
  156. /* Private variables ---------------------------------------------------------*/
  157. /* Private function prototypes -----------------------------------------------*/
  158. static void UART_DMATransmitCplt(DMA_HandleTypeDef *hdma);
  159. static void UART_DMATxHalfCplt(DMA_HandleTypeDef *hdma);
  160. static void UART_DMAReceiveCplt(DMA_HandleTypeDef *hdma);
  161. static void UART_DMARxHalfCplt(DMA_HandleTypeDef *hdma);
  162. static void UART_DMAError(DMA_HandleTypeDef *hdma);
  163. static HAL_StatusTypeDef UART_Transmit_IT(UART_HandleTypeDef *huart);
  164. static HAL_StatusTypeDef UART_EndTransmit_IT(UART_HandleTypeDef *huart);
  165. static HAL_StatusTypeDef UART_Receive_IT(UART_HandleTypeDef *huart);
  166. /**
  167. * @}
  168. */
  169. /* Private functions ---------------------------------------------------------*/
  170. /** @addtogroup UART_Exported_Functions UART Exported Functions
  171. * @{
  172. */
  173. /** @addtogroup UART_Exported_Functions_Group1
  174. * @brief Initialization and Configuration functions
  175. *
  176. @verbatim
  177. ===============================================================================
  178. ##### Initialization and Configuration functions #####
  179. ===============================================================================
  180. [..]
  181. This subsection provides a set of functions allowing to initialize the USARTx or the UARTy
  182. in asynchronous mode.
  183. (+) For the asynchronous mode only these parameters can be configured:
  184. (++) Baud Rate
  185. (++) Word Length
  186. (++) Stop Bit
  187. (++) Parity: If the parity is enabled, then the MSB bit of the data written
  188. in the data register is transmitted but is changed by the parity bit.
  189. (++) Hardware flow control
  190. (++) Receiver/transmitter modes
  191. (++) Over Sampling Method
  192. (++) One-Bit Sampling Method
  193. (+) For the asynchronous mode, the following advanced features can be configured as well:
  194. (++) TX and/or RX pin level inversion
  195. (++) data logical level inversion
  196. (++) RX and TX pins swap
  197. (++) RX overrun detection disabling
  198. (++) DMA disabling on RX error
  199. (++) MSB first on communication line
  200. (++) auto Baud rate detection
  201. [..]
  202. The HAL_UART_Init(), HAL_HalfDuplex_Init(), HAL_LIN_Init()and HAL_MultiProcessorEx_Init()API
  203. follow respectively the UART asynchronous, UART Half duplex, UART LIN mode
  204. and UART multiprocessor mode mode configuration procedures (details for the procedures
  205. are available in reference manual).
  206. @endverbatim
  207. Depending on the frame length defined by the M1 and M0 bits (7-bit,
  208. 8-bit or 9-bit), the possible UART formats are listed in the
  209. following table.
  210. Table 1. UART frame format.
  211. +-----------------------------------------------------------------------+
  212. | M1 bit | M0 bit | PCE bit | USART frame |
  213. |---------|---------|-----------|---------------------------------------|
  214. | 0 | 0 | 0 | | SB | 8 bit data | STB | |
  215. |---------|---------|-----------|---------------------------------------|
  216. | 0 | 0 | 1 | | SB | 7 bit data | PB | STB | |
  217. |---------|---------|-----------|---------------------------------------|
  218. | 0 | 1 | 0 | | SB | 9 bit data | STB | |
  219. |---------|---------|-----------|---------------------------------------|
  220. | 0 | 1 | 1 | | SB | 8 bit data | PB | STB | |
  221. |---------|---------|-----------|---------------------------------------|
  222. | 1 | 0 | 0 | | SB | 7 bit data | STB | |
  223. |---------|---------|-----------|---------------------------------------|
  224. | 1 | 0 | 1 | | SB | 6 bit data | PB | STB | |
  225. +-----------------------------------------------------------------------+
  226. * @{
  227. */
  228. /**
  229. * @brief Initializes the UART mode according to the specified
  230. * parameters in the UART_InitTypeDef and creates the associated handle .
  231. * @param huart: uart handle
  232. * @retval HAL status
  233. */
  234. HAL_StatusTypeDef HAL_UART_Init(UART_HandleTypeDef *huart)
  235. {
  236. /* Check the UART handle allocation */
  237. if(huart == NULL)
  238. {
  239. return HAL_ERROR;
  240. }
  241. if(huart->Init.HwFlowCtl != UART_HWCONTROL_NONE)
  242. {
  243. /* Check the parameters */
  244. assert_param(IS_UART_HWFLOW_INSTANCE(huart->Instance));
  245. }
  246. else
  247. {
  248. /* Check the parameters */
  249. assert_param(IS_UART_INSTANCE(huart->Instance));
  250. }
  251. if(huart->gState == HAL_UART_STATE_RESET)
  252. {
  253. /* Allocate lock resource and initialize it */
  254. huart->Lock = HAL_UNLOCKED;
  255. /* Init the low level hardware : GPIO, CLOCK, CORTEX */
  256. HAL_UART_MspInit(huart);
  257. }
  258. huart->gState = HAL_UART_STATE_BUSY;
  259. /* Disable the Peripheral */
  260. __HAL_UART_DISABLE(huart);
  261. /* Set the UART Communication parameters */
  262. UART_SetConfig(huart);
  263. if (huart->AdvancedInit.AdvFeatureInit != UART_ADVFEATURE_NO_INIT)
  264. {
  265. UART_AdvFeatureConfig(huart);
  266. }
  267. /* In asynchronous mode, the following bits must be kept cleared:
  268. - LINEN and CLKEN bits in the USART_CR2 register,
  269. - SCEN, HDSEL and IREN bits in the USART_CR3 register.*/
  270. huart->Instance->CR2 &= ~(USART_CR2_LINEN | USART_CR2_CLKEN);
  271. huart->Instance->CR3 &= ~(USART_CR3_SCEN | USART_CR3_HDSEL | USART_CR3_IREN);
  272. /* Enable the Peripheral */
  273. __HAL_UART_ENABLE(huart);
  274. /* TEACK and/or REACK to check before moving huart->gState and huart->RxState to Ready */
  275. return (UART_CheckIdleState(huart));
  276. }
  277. /**
  278. * @brief Initializes the half-duplex mode according to the specified
  279. * parameters in the UART_InitTypeDef and creates the associated handle .
  280. * @param huart: uart handle
  281. * @retval HAL status
  282. */
  283. HAL_StatusTypeDef HAL_HalfDuplex_Init(UART_HandleTypeDef *huart)
  284. {
  285. /* Check the UART handle allocation */
  286. if(huart == NULL)
  287. {
  288. return HAL_ERROR;
  289. }
  290. /* Check UART instance */
  291. assert_param(IS_UART_HALFDUPLEX_INSTANCE(huart->Instance));
  292. if(huart->gState == HAL_UART_STATE_RESET)
  293. {
  294. /* Init the low level hardware : GPIO, CLOCK, CORTEX */
  295. HAL_UART_MspInit(huart);
  296. }
  297. huart->gState = HAL_UART_STATE_BUSY;
  298. /* Disable the Peripheral */
  299. __HAL_UART_DISABLE(huart);
  300. /* Set the UART Communication parameters */
  301. UART_SetConfig(huart);
  302. if (huart->AdvancedInit.AdvFeatureInit != UART_ADVFEATURE_NO_INIT)
  303. {
  304. UART_AdvFeatureConfig(huart);
  305. }
  306. /* In half-duplex mode, the following bits must be kept cleared:
  307. - LINEN and CLKEN bits in the USART_CR2 register,
  308. - SCEN and IREN bits in the USART_CR3 register.*/
  309. huart->Instance->CR2 &= ~(USART_CR2_LINEN | USART_CR2_CLKEN);
  310. huart->Instance->CR3 &= ~(USART_CR3_IREN | USART_CR3_SCEN);
  311. /* Enable the Half-Duplex mode by setting the HDSEL bit in the CR3 register */
  312. huart->Instance->CR3 |= USART_CR3_HDSEL;
  313. /* Enable the Peripheral */
  314. __HAL_UART_ENABLE(huart);
  315. /* TEACK and/or REACK to check before moving huart->gState and huart->RxState to Ready */
  316. return (UART_CheckIdleState(huart));
  317. }
  318. /**
  319. * @brief Initializes the LIN mode according to the specified
  320. * parameters in the UART_InitTypeDef and creates the associated handle .
  321. * @param huart: uart handle
  322. * @param BreakDetectLength: specifies the LIN break detection length.
  323. * This parameter can be one of the following values:
  324. * @arg UART_LINBREAKDETECTLENGTH_10B: 10-bit break detection
  325. * @arg UART_LINBREAKDETECTLENGTH_11B: 11-bit break detection
  326. * @retval HAL status
  327. */
  328. HAL_StatusTypeDef HAL_LIN_Init(UART_HandleTypeDef *huart, uint32_t BreakDetectLength)
  329. {
  330. /* Check the UART handle allocation */
  331. if(huart == NULL)
  332. {
  333. return HAL_ERROR;
  334. }
  335. /* Check the LIN UART instance */
  336. assert_param(IS_UART_LIN_INSTANCE(huart->Instance));
  337. /* Check the Break detection length parameter */
  338. assert_param(IS_UART_LIN_BREAK_DETECT_LENGTH(BreakDetectLength));
  339. /* LIN mode limited to 16-bit oversampling only */
  340. if(huart->Init.OverSampling == UART_OVERSAMPLING_8)
  341. {
  342. return HAL_ERROR;
  343. }
  344. /* in LIN mode limited, data length limited to 8-bit only */
  345. if(huart->Init.WordLength!= UART_WORDLENGTH_8B)
  346. {
  347. return HAL_ERROR;
  348. }
  349. if(huart->gState == HAL_UART_STATE_RESET)
  350. {
  351. /* Init the low level hardware : GPIO, CLOCK, CORTEX */
  352. HAL_UART_MspInit(huart);
  353. }
  354. huart->gState = HAL_UART_STATE_BUSY;
  355. /* Disable the Peripheral */
  356. __HAL_UART_DISABLE(huart);
  357. /* Set the UART Communication parameters */
  358. UART_SetConfig(huart);
  359. if (huart->AdvancedInit.AdvFeatureInit != UART_ADVFEATURE_NO_INIT)
  360. {
  361. UART_AdvFeatureConfig(huart);
  362. }
  363. /* In LIN mode, the following bits must be kept cleared:
  364. - LINEN and CLKEN bits in the USART_CR2 register,
  365. - SCEN and IREN bits in the USART_CR3 register.*/
  366. huart->Instance->CR2 &= ~(USART_CR2_CLKEN);
  367. huart->Instance->CR3 &= ~(USART_CR3_HDSEL | USART_CR3_IREN | USART_CR3_SCEN);
  368. /* Enable the LIN mode by setting the LINEN bit in the CR2 register */
  369. huart->Instance->CR2 |= USART_CR2_LINEN;
  370. /* Set the USART LIN Break detection length. */
  371. MODIFY_REG(huart->Instance->CR2, USART_CR2_LBDL, BreakDetectLength);
  372. /* Enable the Peripheral */
  373. __HAL_UART_ENABLE(huart);
  374. /* TEACK and/or REACK to check before moving huart->gState and huart->RxState to Ready */
  375. return (UART_CheckIdleState(huart));
  376. }
  377. /**
  378. * @brief Initializes the multiprocessor mode according to the specified
  379. * parameters in the UART_InitTypeDef and creates the associated handle.
  380. * @param huart: UART handle
  381. * @param Address: UART node address (4-, 6-, 7- or 8-bit long)
  382. * @param WakeUpMethod: specifies the UART wakeup method.
  383. * This parameter can be one of the following values:
  384. * @arg UART_WAKEUPMETHOD_IDLELINE: WakeUp by an idle line detection
  385. * @arg UART_WAKEUPMETHOD_ADDRESSMARK: WakeUp by an address mark
  386. * @note If the user resorts to idle line detection wake up, the Address parameter
  387. * is useless and ignored by the initialization function.
  388. * @note If the user resorts to address mark wake up, the address length detection
  389. * is configured by default to 4 bits only. For the UART to be able to
  390. * manage 6-, 7- or 8-bit long addresses detection, the API
  391. * HAL_MultiProcessorEx_AddressLength_Set() must be called after
  392. * HAL_MultiProcessor_Init().
  393. * @retval HAL status
  394. */
  395. HAL_StatusTypeDef HAL_MultiProcessor_Init(UART_HandleTypeDef *huart, uint8_t Address, uint32_t WakeUpMethod)
  396. {
  397. /* Check the UART handle allocation */
  398. if(huart == NULL)
  399. {
  400. return HAL_ERROR;
  401. }
  402. /* Check the wake up method parameter */
  403. assert_param(IS_UART_WAKEUPMETHOD(WakeUpMethod));
  404. if(huart->gState == HAL_UART_STATE_RESET)
  405. {
  406. /* Init the low level hardware : GPIO, CLOCK */
  407. HAL_UART_MspInit(huart);
  408. }
  409. huart->gState = HAL_UART_STATE_BUSY;
  410. /* Disable the Peripheral */
  411. __HAL_UART_DISABLE(huart);
  412. /* Set the UART Communication parameters */
  413. UART_SetConfig(huart);
  414. if (huart->AdvancedInit.AdvFeatureInit != UART_ADVFEATURE_NO_INIT)
  415. {
  416. UART_AdvFeatureConfig(huart);
  417. }
  418. /* In multiprocessor mode, the following bits must be kept cleared:
  419. - LINEN and CLKEN bits in the USART_CR2 register,
  420. - SCEN, HDSEL and IREN bits in the USART_CR3 register. */
  421. huart->Instance->CR2 &= ~(USART_CR2_LINEN | USART_CR2_CLKEN);
  422. huart->Instance->CR3 &= ~(USART_CR3_SCEN | USART_CR3_HDSEL | USART_CR3_IREN);
  423. if (WakeUpMethod == UART_WAKEUPMETHOD_ADDRESSMARK)
  424. {
  425. /* If address mark wake up method is chosen, set the USART address node */
  426. MODIFY_REG(huart->Instance->CR2, USART_CR2_ADD, ((uint32_t)Address << UART_CR2_ADDRESS_LSB_POS));
  427. }
  428. /* Set the wake up method by setting the WAKE bit in the CR1 register */
  429. MODIFY_REG(huart->Instance->CR1, USART_CR1_WAKE, WakeUpMethod);
  430. /* Enable the Peripheral */
  431. __HAL_UART_ENABLE(huart);
  432. /* TEACK and/or REACK to check before moving huart->gState and huart->RxState to Ready */
  433. return (UART_CheckIdleState(huart));
  434. }
  435. /**
  436. * @brief DeInitializes the UART peripheral
  437. * @param huart: uart handle
  438. * @retval HAL status
  439. */
  440. HAL_StatusTypeDef HAL_UART_DeInit(UART_HandleTypeDef *huart)
  441. {
  442. /* Check the UART handle allocation */
  443. if(huart == NULL)
  444. {
  445. return HAL_ERROR;
  446. }
  447. /* Check the parameters */
  448. assert_param(IS_UART_INSTANCE(huart->Instance));
  449. huart->gState = HAL_UART_STATE_BUSY;
  450. /* Disable the Peripheral */
  451. __HAL_UART_DISABLE(huart);
  452. huart->Instance->CR1 = 0x0U;
  453. huart->Instance->CR2 = 0x0U;
  454. huart->Instance->CR3 = 0x0U;
  455. /* DeInit the low level hardware */
  456. HAL_UART_MspDeInit(huart);
  457. huart->ErrorCode = HAL_UART_ERROR_NONE;
  458. huart->gState = HAL_UART_STATE_RESET;
  459. huart->RxState = HAL_UART_STATE_RESET;
  460. /* Release Lock */
  461. __HAL_UNLOCK(huart);
  462. return HAL_OK;
  463. }
  464. /**
  465. * @brief UART MSP Init
  466. * @param huart: uart handle
  467. * @retval None
  468. */
  469. __weak void HAL_UART_MspInit(UART_HandleTypeDef *huart)
  470. {
  471. /* Prevent unused argument(s) compilation warning */
  472. UNUSED(huart);
  473. /* NOTE : This function should not be modified, when the callback is needed,
  474. the HAL_UART_MspInit can be implemented in the user file
  475. */
  476. }
  477. /**
  478. * @brief UART MSP DeInit
  479. * @param huart: uart handle
  480. * @retval None
  481. */
  482. __weak void HAL_UART_MspDeInit(UART_HandleTypeDef *huart)
  483. {
  484. /* Prevent unused argument(s) compilation warning */
  485. UNUSED(huart);
  486. /* NOTE : This function should not be modified, when the callback is needed,
  487. the HAL_UART_MspDeInit can be implemented in the user file
  488. */
  489. }
  490. /**
  491. * @}
  492. */
  493. /** @addtogroup UART_Exported_Functions_Group2
  494. * @brief UART Transmit/Receive functions
  495. *
  496. @verbatim
  497. ===============================================================================
  498. ##### IO operation functions #####
  499. ===============================================================================
  500. This subsection provides a set of functions allowing to manage the UART asynchronous
  501. and Half duplex data transfers.
  502. (#) There are two mode of transfer:
  503. (+) Blocking mode: The communication is performed in polling mode.
  504. The HAL status of all data processing is returned by the same function
  505. after finishing transfer.
  506. (+) No-Blocking mode: The communication is performed using Interrupts
  507. or DMA, These API's return the HAL status.
  508. The end of the data processing will be indicated through the
  509. dedicated UART IRQ when using Interrupt mode or the DMA IRQ when
  510. using DMA mode.
  511. The HAL_UART_TxCpltCallback(), HAL_UART_RxCpltCallback() user callbacks
  512. will be executed respectivelly at the end of the transmit or Receive process
  513. The HAL_UART_ErrorCallback()user callback will be executed when a communication error is detected
  514. (#) Blocking mode API's are :
  515. (+) HAL_UART_Transmit()
  516. (+) HAL_UART_Receive()
  517. (#) Non-Blocking mode API's with Interrupt are :
  518. (+) HAL_UART_Transmit_IT()
  519. (+) HAL_UART_Receive_IT()
  520. (+) HAL_UART_IRQHandler()
  521. (+) UART_Transmit_IT()
  522. (+) UART_Receive_IT()
  523. (#) No-Blocking mode API's with DMA are :
  524. (+) HAL_UART_Transmit_DMA()
  525. (+) HAL_UART_Receive_DMA()
  526. (+) HAL_UART_DMAPause()
  527. (+) HAL_UART_DMAResume()
  528. (+) HAL_UART_DMAStop()
  529. (#) A set of Transfer Complete Callbacks are provided in No_Blocking mode:
  530. (+) HAL_UART_TxHalfCpltCallback()
  531. (+) HAL_UART_TxCpltCallback()
  532. (+) HAL_UART_RxHalfCpltCallback()
  533. (+) HAL_UART_RxCpltCallback()
  534. (+) HAL_UART_ErrorCallback()
  535. -@- In the Half duplex communication, it is forbidden to run the transmit
  536. and receive process in parallel, the UART state HAL_UART_STATE_BUSY_TX_RX can't be useful.
  537. @endverbatim
  538. * @{
  539. */
  540. /**
  541. * @brief Send an amount of data in blocking mode
  542. * @param huart: uart handle
  543. * @param pData: pointer to data buffer
  544. * @param Size: amount of data to be sent
  545. * @param Timeout : Timeout duration
  546. * @retval HAL status
  547. */
  548. HAL_StatusTypeDef HAL_UART_Transmit(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint32_t Timeout)
  549. {
  550. uint16_t* tmp;
  551. /* Check that a Tx process is not already ongoing */
  552. if(huart->gState == HAL_UART_STATE_READY)
  553. {
  554. if((pData == NULL ) || (Size == 0U))
  555. {
  556. return HAL_ERROR;
  557. }
  558. /* Process Locked */
  559. __HAL_LOCK(huart);
  560. huart->ErrorCode = HAL_UART_ERROR_NONE;
  561. huart->gState = HAL_UART_STATE_BUSY_TX;
  562. huart->TxXferSize = Size;
  563. huart->TxXferCount = Size;
  564. while(huart->TxXferCount > 0U)
  565. {
  566. huart->TxXferCount--;
  567. if(UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_TXE, RESET, Timeout) != HAL_OK)
  568. {
  569. return HAL_TIMEOUT;
  570. }
  571. if ((huart->Init.WordLength == UART_WORDLENGTH_9B) && (huart->Init.Parity == UART_PARITY_NONE))
  572. {
  573. tmp = (uint16_t*) pData;
  574. huart->Instance->TDR = (*tmp & (uint16_t)0x01FFU);
  575. pData += 2U;
  576. }
  577. else
  578. {
  579. huart->Instance->TDR = (*pData++ & (uint8_t)0xFFU);
  580. }
  581. }
  582. if(UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_TC, RESET, Timeout) != HAL_OK)
  583. {
  584. return HAL_TIMEOUT;
  585. }
  586. /* At end of Tx process, restore huart->gState to Ready */
  587. huart->gState = HAL_UART_STATE_READY;
  588. /* Process Unlocked */
  589. __HAL_UNLOCK(huart);
  590. return HAL_OK;
  591. }
  592. else
  593. {
  594. return HAL_BUSY;
  595. }
  596. }
  597. /**
  598. * @brief Receive an amount of data in blocking mode
  599. * @param huart: uart handle
  600. * @param pData: pointer to data buffer
  601. * @param Size: amount of data to be received
  602. * @param Timeout : Timeout duration
  603. * @retval HAL status
  604. */
  605. HAL_StatusTypeDef HAL_UART_Receive(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint32_t Timeout)
  606. {
  607. uint16_t* tmp;
  608. uint16_t uhMask;
  609. /* Check that a Rx process is not already ongoing */
  610. if(huart->RxState == HAL_UART_STATE_READY)
  611. {
  612. if((pData == NULL ) || (Size == 0U))
  613. {
  614. return HAL_ERROR;
  615. }
  616. /* Process Locked */
  617. __HAL_LOCK(huart);
  618. huart->ErrorCode = HAL_UART_ERROR_NONE;
  619. huart->RxState = HAL_UART_STATE_BUSY_RX;
  620. huart->RxXferSize = Size;
  621. huart->RxXferCount = Size;
  622. /* Computation of UART mask to apply to RDR register */
  623. UART_MASK_COMPUTATION(huart);
  624. uhMask = huart->Mask;
  625. /* as long as data have to be received */
  626. while(huart->RxXferCount > 0U)
  627. {
  628. huart->RxXferCount--;
  629. if(UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_RXNE, RESET, Timeout) != HAL_OK)
  630. {
  631. return HAL_TIMEOUT;
  632. }
  633. if ((huart->Init.WordLength == UART_WORDLENGTH_9B) && (huart->Init.Parity == UART_PARITY_NONE))
  634. {
  635. tmp = (uint16_t*) pData ;
  636. *tmp = (uint16_t)(huart->Instance->RDR & uhMask);
  637. pData +=2U;
  638. }
  639. else
  640. {
  641. *pData++ = (uint8_t)(huart->Instance->RDR & (uint8_t)uhMask);
  642. }
  643. }
  644. /* At end of Rx process, restore huart->RxState to Ready */
  645. huart->RxState = HAL_UART_STATE_READY;
  646. /* Process Unlocked */
  647. __HAL_UNLOCK(huart);
  648. return HAL_OK;
  649. }
  650. else
  651. {
  652. return HAL_BUSY;
  653. }
  654. }
  655. /**
  656. * @brief Send an amount of data in interrupt mode
  657. * @param huart: uart handle
  658. * @param pData: pointer to data buffer
  659. * @param Size: amount of data to be sent
  660. * @retval HAL status
  661. */
  662. HAL_StatusTypeDef HAL_UART_Transmit_IT(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
  663. {
  664. /* Check that a Tx process is not already ongoing */
  665. if(huart->gState == HAL_UART_STATE_READY)
  666. {
  667. if((pData == NULL ) || (Size == 0U))
  668. {
  669. return HAL_ERROR;
  670. }
  671. /* Process Locked */
  672. __HAL_LOCK(huart);
  673. huart->pTxBuffPtr = pData;
  674. huart->TxXferSize = Size;
  675. huart->TxXferCount = Size;
  676. huart->ErrorCode = HAL_UART_ERROR_NONE;
  677. huart->gState = HAL_UART_STATE_BUSY_TX;
  678. /* Enable the UART Parity Error Interrupt */
  679. __HAL_UART_ENABLE_IT(huart, UART_IT_PE);
  680. /* Process Unlocked */
  681. __HAL_UNLOCK(huart);
  682. /* Enable the UART Transmit data register empty Interrupt */
  683. __HAL_UART_ENABLE_IT(huart, UART_IT_TXE);
  684. return HAL_OK;
  685. }
  686. else
  687. {
  688. return HAL_BUSY;
  689. }
  690. }
  691. /**
  692. * @brief Receive an amount of data in interrupt mode
  693. * @param huart: uart handle
  694. * @param pData: pointer to data buffer
  695. * @param Size: amount of data to be received
  696. * @retval HAL status
  697. */
  698. HAL_StatusTypeDef HAL_UART_Receive_IT(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
  699. {
  700. /* Check that a Rx process is not already ongoing */
  701. if(huart->RxState == HAL_UART_STATE_READY)
  702. {
  703. if((pData == NULL ) || (Size == 0U))
  704. {
  705. return HAL_ERROR;
  706. }
  707. /* Process Locked */
  708. __HAL_LOCK(huart);
  709. huart->pRxBuffPtr = pData;
  710. huart->RxXferSize = Size;
  711. huart->RxXferCount = Size;
  712. /* Computation of UART mask to apply to RDR register */
  713. UART_MASK_COMPUTATION(huart);
  714. huart->ErrorCode = HAL_UART_ERROR_NONE;
  715. huart->RxState = HAL_UART_STATE_BUSY_RX;
  716. /* Enable the UART Parity Error Interrupt */
  717. __HAL_UART_ENABLE_IT(huart, UART_IT_PE);
  718. /* Enable the UART Error Interrupt: (Frame error, noise error, overrun error) */
  719. __HAL_UART_ENABLE_IT(huart, UART_IT_ERR);
  720. /* Process Unlocked */
  721. __HAL_UNLOCK(huart);
  722. /* Enable the UART Data Register not empty Interrupt */
  723. __HAL_UART_ENABLE_IT(huart, UART_IT_RXNE);
  724. return HAL_OK;
  725. }
  726. else
  727. {
  728. return HAL_BUSY;
  729. }
  730. }
  731. /**
  732. * @brief Send an amount of data in DMA mode
  733. * @param huart: uart handle
  734. * @param pData: pointer to data buffer
  735. * @param Size: amount of data to be sent
  736. * @retval HAL status
  737. */
  738. HAL_StatusTypeDef HAL_UART_Transmit_DMA(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
  739. {
  740. uint32_t *tmp;
  741. /* Check that a Tx process is not already ongoing */
  742. if(huart->gState == HAL_UART_STATE_READY)
  743. {
  744. if((pData == NULL ) || (Size == 0U))
  745. {
  746. return HAL_ERROR;
  747. }
  748. /* Process Locked */
  749. __HAL_LOCK(huart);
  750. huart->pTxBuffPtr = pData;
  751. huart->TxXferSize = Size;
  752. huart->TxXferCount = Size;
  753. huart->ErrorCode = HAL_UART_ERROR_NONE;
  754. huart->gState = HAL_UART_STATE_BUSY_TX;
  755. /* Set the UART DMA transfert complete callback */
  756. huart->hdmatx->XferCpltCallback = UART_DMATransmitCplt;
  757. /* Set the UART DMA Half transfer complete callback */
  758. huart->hdmatx->XferHalfCpltCallback = UART_DMATxHalfCplt;
  759. /* Set the DMA error callback */
  760. huart->hdmatx->XferErrorCallback = UART_DMAError;
  761. /* Enable the UART transmit DMA Stream */
  762. tmp = (uint32_t*)&pData;
  763. HAL_DMA_Start_IT(huart->hdmatx, *(uint32_t*)tmp, (uint32_t)&huart->Instance->TDR, Size);
  764. /* Clear the TC flag in the SR register by writing 0 to it */
  765. __HAL_UART_CLEAR_FLAG(huart, UART_FLAG_TC);
  766. /* Enable the DMA transfer for transmit request by setting the DMAT bit
  767. in the UART CR3 register */
  768. huart->Instance->CR3 |= USART_CR3_DMAT;
  769. /* Process Unlocked */
  770. __HAL_UNLOCK(huart);
  771. return HAL_OK;
  772. }
  773. else
  774. {
  775. return HAL_BUSY;
  776. }
  777. }
  778. /**
  779. * @brief Receive an amount of data in DMA mode
  780. * @param huart: uart handle
  781. * @param pData: pointer to data buffer
  782. * @param Size: amount of data to be received
  783. * @note When the UART parity is enabled (PCE = 1) the data received contain the parity bit.
  784. * @retval HAL status
  785. */
  786. HAL_StatusTypeDef HAL_UART_Receive_DMA(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
  787. {
  788. uint32_t *tmp;
  789. /* Check that a Rx process is not already ongoing */
  790. if(huart->RxState == HAL_UART_STATE_READY)
  791. {
  792. if((pData == NULL ) || (Size == 0U))
  793. {
  794. return HAL_ERROR;
  795. }
  796. /* Process Locked */
  797. __HAL_LOCK(huart);
  798. huart->pRxBuffPtr = pData;
  799. huart->RxXferSize = Size;
  800. huart->ErrorCode = HAL_UART_ERROR_NONE;
  801. huart->RxState = HAL_UART_STATE_BUSY_RX;
  802. /* Set the UART DMA transfert complete callback */
  803. huart->hdmarx->XferCpltCallback = UART_DMAReceiveCplt;
  804. /* Set the UART DMA Half transfer complete callback */
  805. huart->hdmarx->XferHalfCpltCallback = UART_DMARxHalfCplt;
  806. /* Set the DMA error callback */
  807. huart->hdmarx->XferErrorCallback = UART_DMAError;
  808. /* Enable the DMA Stream */
  809. tmp = (uint32_t*)&pData;
  810. HAL_DMA_Start_IT(huart->hdmarx, (uint32_t)&huart->Instance->RDR, *(uint32_t*)tmp, Size);
  811. /* Enable the DMA transfer for the receiver request by setting the DMAR bit
  812. in the UART CR3 register */
  813. huart->Instance->CR3 |= USART_CR3_DMAR;
  814. /* Process Unlocked */
  815. __HAL_UNLOCK(huart);
  816. return HAL_OK;
  817. }
  818. else
  819. {
  820. return HAL_BUSY;
  821. }
  822. }
  823. /**
  824. * @brief Pauses the DMA Transfer.
  825. * @param huart: UART handle
  826. * @retval None
  827. */
  828. HAL_StatusTypeDef HAL_UART_DMAPause(UART_HandleTypeDef *huart)
  829. {
  830. /* Process Locked */
  831. __HAL_LOCK(huart);
  832. if(huart->gState == HAL_UART_STATE_BUSY_TX)
  833. {
  834. /* Disable the UART DMA Tx request */
  835. huart->Instance->CR3 &= (uint32_t)(~USART_CR3_DMAT);
  836. }
  837. if(huart->RxState == HAL_UART_STATE_BUSY_RX)
  838. {
  839. /* Disable the UART DMA Rx request */
  840. huart->Instance->CR3 &= (uint32_t)(~USART_CR3_DMAR);
  841. }
  842. /* Process Unlocked */
  843. __HAL_UNLOCK(huart);
  844. return HAL_OK;
  845. }
  846. /**
  847. * @brief Resumes the DMA Transfer.
  848. * @param huart: UART handle
  849. * @retval None
  850. */
  851. HAL_StatusTypeDef HAL_UART_DMAResume(UART_HandleTypeDef *huart)
  852. {
  853. /* Process Locked */
  854. __HAL_LOCK(huart);
  855. if(huart->gState == HAL_UART_STATE_BUSY_TX)
  856. {
  857. /* Enable the UART DMA Tx request */
  858. huart->Instance->CR3 |= USART_CR3_DMAT;
  859. }
  860. else if(huart->RxState == HAL_UART_STATE_BUSY_RX)
  861. {
  862. /* Clear the Overrun flag before resumming the Rx transfer*/
  863. __HAL_UART_CLEAR_IT(huart, UART_CLEAR_OREF);
  864. /* Enable the UART DMA Rx request */
  865. huart->Instance->CR3 |= USART_CR3_DMAR;
  866. }
  867. /* Process Unlocked */
  868. __HAL_UNLOCK(huart);
  869. return HAL_OK;
  870. }
  871. /**
  872. * @brief Stops the DMA Transfer.
  873. * @param huart: UART handle
  874. * @retval None
  875. */
  876. HAL_StatusTypeDef HAL_UART_DMAStop(UART_HandleTypeDef *huart)
  877. {
  878. /* The Lock is not implemented on this API to allow the user application
  879. to call the HAL UART API under callbacks HAL_UART_TxCpltCallback() / HAL_UART_RxCpltCallback():
  880. when calling HAL_DMA_Abort() API the DMA TX/RX Transfer complete interrupt is generated
  881. and the correspond call back is executed HAL_UART_TxCpltCallback() / HAL_UART_RxCpltCallback()
  882. */
  883. /* Disable the UART Tx/Rx DMA requests */
  884. huart->Instance->CR3 &= ~USART_CR3_DMAT;
  885. huart->Instance->CR3 &= ~USART_CR3_DMAR;
  886. /* Abort the UART DMA tx channel */
  887. if(huart->hdmatx != NULL)
  888. {
  889. HAL_DMA_Abort(huart->hdmatx);
  890. }
  891. /* Abort the UART DMA rx channel */
  892. if(huart->hdmarx != NULL)
  893. {
  894. HAL_DMA_Abort(huart->hdmarx);
  895. }
  896. huart->gState = HAL_UART_STATE_READY;
  897. huart->RxState = HAL_UART_STATE_READY;
  898. return HAL_OK;
  899. }
  900. /**
  901. * @brief This function handles UART interrupt request.
  902. * @param huart: uart handle
  903. * @retval None
  904. */
  905. void HAL_UART_IRQHandler(UART_HandleTypeDef *huart)
  906. {
  907. /* UART parity error interrupt occurred ------------------------------------*/
  908. if((__HAL_UART_GET_IT(huart, UART_IT_PE) != RESET) && (__HAL_UART_GET_IT_SOURCE(huart, UART_IT_PE) != RESET))
  909. {
  910. __HAL_UART_CLEAR_IT(huart, UART_CLEAR_PEF);
  911. huart->ErrorCode |= HAL_UART_ERROR_PE;
  912. /* Set the UART state ready to be able to start again the process */
  913. huart->gState = HAL_UART_STATE_READY;
  914. huart->RxState = HAL_UART_STATE_READY;
  915. }
  916. /* UART frame error interrupt occured --------------------------------------*/
  917. if((__HAL_UART_GET_IT(huart, UART_IT_FE) != RESET) && (__HAL_UART_GET_IT_SOURCE(huart, UART_IT_ERR) != RESET))
  918. {
  919. __HAL_UART_CLEAR_IT(huart, UART_CLEAR_FEF);
  920. huart->ErrorCode |= HAL_UART_ERROR_FE;
  921. /* Set the UART state ready to be able to start again the process */
  922. huart->gState = HAL_UART_STATE_READY;
  923. huart->RxState = HAL_UART_STATE_READY;
  924. }
  925. /* UART noise error interrupt occured --------------------------------------*/
  926. if((__HAL_UART_GET_IT(huart, UART_IT_NE) != RESET) && (__HAL_UART_GET_IT_SOURCE(huart, UART_IT_ERR) != RESET))
  927. {
  928. __HAL_UART_CLEAR_IT(huart, UART_CLEAR_NEF);
  929. huart->ErrorCode |= HAL_UART_ERROR_NE;
  930. /* Set the UART state ready to be able to start again the process */
  931. huart->gState = HAL_UART_STATE_READY;
  932. huart->RxState = HAL_UART_STATE_READY;
  933. }
  934. /* UART Over-Run interrupt occurred -----------------------------------------*/
  935. if((__HAL_UART_GET_IT(huart, UART_IT_ORE) != RESET) && (__HAL_UART_GET_IT_SOURCE(huart, UART_IT_ERR) != RESET))
  936. {
  937. __HAL_UART_CLEAR_IT(huart, UART_CLEAR_OREF);
  938. huart->ErrorCode |= HAL_UART_ERROR_ORE;
  939. /* Set the UART state ready to be able to start again the process */
  940. huart->gState = HAL_UART_STATE_READY;
  941. huart->RxState = HAL_UART_STATE_READY;
  942. }
  943. /* Call UART Error Call back function if need be --------------------------*/
  944. if(huart->ErrorCode != HAL_UART_ERROR_NONE)
  945. {
  946. /* Set the UART state ready to be able to start again the process */
  947. huart->gState = HAL_UART_STATE_READY;
  948. huart->RxState = HAL_UART_STATE_READY;
  949. HAL_UART_ErrorCallback(huart);
  950. }
  951. /* UART Wake Up interrupt occured ------------------------------------------*/
  952. if((__HAL_UART_GET_IT(huart, UART_IT_WUF) != RESET) && (__HAL_UART_GET_IT_SOURCE(huart, UART_IT_WUF) != RESET))
  953. {
  954. __HAL_UART_CLEAR_IT(huart, UART_CLEAR_WUF);
  955. /* Set the UART state ready to be able to start again the process */
  956. huart->gState = HAL_UART_STATE_READY;
  957. huart->RxState = HAL_UART_STATE_READY;
  958. HAL_UARTEx_WakeupCallback(huart);
  959. }
  960. /* UART in mode Receiver ---------------------------------------------------*/
  961. if((__HAL_UART_GET_IT(huart, UART_IT_RXNE) != RESET) && (__HAL_UART_GET_IT_SOURCE(huart, UART_IT_RXNE) != RESET))
  962. {
  963. UART_Receive_IT(huart);
  964. }
  965. /* UART in mode Transmitter ------------------------------------------------*/
  966. if((__HAL_UART_GET_IT(huart, UART_IT_TXE) != RESET) &&(__HAL_UART_GET_IT_SOURCE(huart, UART_IT_TXE) != RESET))
  967. {
  968. UART_Transmit_IT(huart);
  969. }
  970. /* UART in mode Transmitter -- TC ------------------------------------------*/
  971. if((__HAL_UART_GET_IT(huart, UART_IT_TC) != RESET) &&(__HAL_UART_GET_IT_SOURCE(huart, UART_IT_TC) != RESET))
  972. {
  973. UART_EndTransmit_IT(huart);
  974. }
  975. }
  976. /**
  977. * @brief Tx Transfer completed callbacks
  978. * @param huart: uart handle
  979. * @retval None
  980. */
  981. __weak void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
  982. {
  983. /* Prevent unused argument(s) compilation warning */
  984. UNUSED(huart);
  985. /* NOTE : This function Should not be modified, when the callback is needed,
  986. the HAL_UART_TxCpltCallback could be implemented in the user file
  987. */
  988. }
  989. /**
  990. * @brief Tx Half Transfer completed callbacks.
  991. * @param huart: UART handle
  992. * @retval None
  993. */
  994. __weak void HAL_UART_TxHalfCpltCallback(UART_HandleTypeDef *huart)
  995. {
  996. /* Prevent unused argument(s) compilation warning */
  997. UNUSED(huart);
  998. /* NOTE: This function Should not be modified, when the callback is needed,
  999. the HAL_UART_TxCpltCallback could be implemented in the user file
  1000. */
  1001. }
  1002. /**
  1003. * @brief Rx Transfer completed callbacks
  1004. * @param huart: uart handle
  1005. * @retval None
  1006. */
  1007. __weak void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
  1008. {
  1009. /* Prevent unused argument(s) compilation warning */
  1010. UNUSED(huart);
  1011. /* NOTE : This function Should not be modified, when the callback is needed,
  1012. the HAL_UART_TxCpltCallback could be implemented in the user file
  1013. */
  1014. }
  1015. /**
  1016. * @brief Rx Half Transfer completed callbacks.
  1017. * @param huart: UART handle
  1018. * @retval None
  1019. */
  1020. __weak void HAL_UART_RxHalfCpltCallback(UART_HandleTypeDef *huart)
  1021. {
  1022. /* Prevent unused argument(s) compilation warning */
  1023. UNUSED(huart);
  1024. /* NOTE: This function Should not be modified, when the callback is needed,
  1025. the HAL_UART_TxCpltCallback could be implemented in the user file
  1026. */
  1027. }
  1028. /**
  1029. * @brief UART error callbacks
  1030. * @param huart: uart handle
  1031. * @retval None
  1032. */
  1033. __weak void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart)
  1034. {
  1035. /* Prevent unused argument(s) compilation warning */
  1036. UNUSED(huart);
  1037. /* NOTE : This function should not be modified, when the callback is needed,
  1038. the HAL_UART_ErrorCallback can be implemented in the user file
  1039. */
  1040. }
  1041. /**
  1042. * @}
  1043. */
  1044. /** @addtogroup UART_Exported_Functions_Group3
  1045. * @brief UART control functions
  1046. *
  1047. @verbatim
  1048. ===============================================================================
  1049. ##### Peripheral Control functions #####
  1050. ===============================================================================
  1051. [..]
  1052. This subsection provides a set of functions allowing to control the UART.
  1053. (+) HAL_MultiProcessor_EnableMuteMode() API enables mute mode
  1054. (+) HAL_MultiProcessor_DisableMuteMode() API disables mute mode
  1055. (+) HAL_MultiProcessor_EnterMuteMode() API enters mute mode
  1056. (+) HAL_HalfDuplex_EnableTransmitter() API enables the transmitter
  1057. (+) HAL_HalfDuplex_EnableReceiver() API enables the receiver
  1058. (+) HAL_UART_GetState() API is helpful to check in run-time the state of the UART peripheral
  1059. (+) HAL_UART_GetError()API is helpful to check in run-time the error state of the UART peripheral
  1060. @endverbatim
  1061. * @{
  1062. */
  1063. /**
  1064. * @brief Enable UART in mute mode (doesn't mean UART enters mute mode;
  1065. * to enter mute mode, HAL_MultiProcessor_EnterMuteMode() API must be called)
  1066. * @param huart: uart handle
  1067. * @retval HAL status
  1068. */
  1069. HAL_StatusTypeDef HAL_MultiProcessor_EnableMuteMode(UART_HandleTypeDef *huart)
  1070. {
  1071. /* Process Locked */
  1072. __HAL_LOCK(huart);
  1073. huart->gState = HAL_UART_STATE_BUSY;
  1074. /* Enable USART mute mode by setting the MME bit in the CR1 register */
  1075. huart->Instance->CR1 |= USART_CR1_MME;
  1076. huart->gState = HAL_UART_STATE_READY;
  1077. return (UART_CheckIdleState(huart));
  1078. }
  1079. /**
  1080. * @brief Disable UART mute mode (doesn't mean it actually wakes up the software,
  1081. * as it may not have been in mute mode at this very moment).
  1082. * @param huart: uart handle
  1083. * @retval HAL status
  1084. */
  1085. HAL_StatusTypeDef HAL_MultiProcessor_DisableMuteMode(UART_HandleTypeDef *huart)
  1086. {
  1087. /* Process Locked */
  1088. __HAL_LOCK(huart);
  1089. huart->gState = HAL_UART_STATE_BUSY;
  1090. /* Disable USART mute mode by clearing the MME bit in the CR1 register */
  1091. huart->Instance->CR1 &= ~(USART_CR1_MME);
  1092. huart->gState = HAL_UART_STATE_READY;
  1093. return (UART_CheckIdleState(huart));
  1094. }
  1095. /**
  1096. * @brief Enter UART mute mode (means UART actually enters mute mode).
  1097. * To exit from mute mode, HAL_MultiProcessor_DisableMuteMode() API must be called.
  1098. * @param huart: uart handle
  1099. * @retval HAL status
  1100. */
  1101. void HAL_MultiProcessor_EnterMuteMode(UART_HandleTypeDef *huart)
  1102. {
  1103. __HAL_UART_SEND_REQ(huart, UART_MUTE_MODE_REQUEST);
  1104. }
  1105. /**
  1106. * @brief Enables the UART transmitter and disables the UART receiver.
  1107. * @param huart: UART handle
  1108. * @retval HAL status
  1109. * @retval None
  1110. */
  1111. HAL_StatusTypeDef HAL_HalfDuplex_EnableTransmitter(UART_HandleTypeDef *huart)
  1112. {
  1113. /* Process Locked */
  1114. __HAL_LOCK(huart);
  1115. huart->gState = HAL_UART_STATE_BUSY;
  1116. /* Clear TE and RE bits */
  1117. CLEAR_BIT(huart->Instance->CR1, (USART_CR1_TE | USART_CR1_RE));
  1118. /* Enable the USART's transmit interface by setting the TE bit in the USART CR1 register */
  1119. SET_BIT(huart->Instance->CR1, USART_CR1_TE);
  1120. huart->gState = HAL_UART_STATE_READY;
  1121. /* Process Unlocked */
  1122. __HAL_UNLOCK(huart);
  1123. return HAL_OK;
  1124. }
  1125. /**
  1126. * @brief Enables the UART receiver and disables the UART transmitter.
  1127. * @param huart: UART handle
  1128. * @retval HAL status
  1129. */
  1130. HAL_StatusTypeDef HAL_HalfDuplex_EnableReceiver(UART_HandleTypeDef *huart)
  1131. {
  1132. /* Process Locked */
  1133. __HAL_LOCK(huart);
  1134. huart->gState = HAL_UART_STATE_BUSY;
  1135. /* Clear TE and RE bits */
  1136. CLEAR_BIT(huart->Instance->CR1, (USART_CR1_TE | USART_CR1_RE));
  1137. /* Enable the USART's receive interface by setting the RE bit in the USART CR1 register */
  1138. SET_BIT(huart->Instance->CR1, USART_CR1_RE);
  1139. huart->gState = HAL_UART_STATE_READY;
  1140. /* Process Unlocked */
  1141. __HAL_UNLOCK(huart);
  1142. return HAL_OK;
  1143. }
  1144. /**
  1145. * @brief Transmits break characters.
  1146. * @param huart: pointer to a UART_HandleTypeDef structure that contains
  1147. * the configuration information for the specified UART module.
  1148. * @retval HAL status
  1149. */
  1150. HAL_StatusTypeDef HAL_LIN_SendBreak(UART_HandleTypeDef *huart)
  1151. {
  1152. /* Check the parameters */
  1153. assert_param(IS_UART_LIN_INSTANCE(huart->Instance));
  1154. /* Process Locked */
  1155. __HAL_LOCK(huart);
  1156. huart->gState = HAL_UART_STATE_BUSY;
  1157. /* Send break characters */
  1158. huart->Instance->RQR |= USART_RQR_SBKRQ;
  1159. huart->gState = HAL_UART_STATE_READY;
  1160. /* Process Unlocked */
  1161. __HAL_UNLOCK(huart);
  1162. return HAL_OK;
  1163. }
  1164. /**
  1165. * @brief return the UART state
  1166. * @param huart: uart handle
  1167. * @retval HAL state
  1168. */
  1169. HAL_UART_StateTypeDef HAL_UART_GetState(UART_HandleTypeDef *huart)
  1170. {
  1171. uint32_t temp1= 0x00U, temp2 = 0x00U;
  1172. temp1 = huart->gState;
  1173. temp2 = huart->RxState;
  1174. return (HAL_UART_StateTypeDef)(temp1 | temp2);
  1175. }
  1176. /**
  1177. * @brief Return the UART error code
  1178. * @param huart : pointer to a UART_HandleTypeDef structure that contains
  1179. * the configuration information for the specified UART.
  1180. * @retval UART Error Code
  1181. */
  1182. uint32_t HAL_UART_GetError(UART_HandleTypeDef *huart)
  1183. {
  1184. return huart->ErrorCode;
  1185. }
  1186. /**
  1187. * @}
  1188. */
  1189. /**
  1190. * @}
  1191. */
  1192. /***************************************************************
  1193. * Private functions...
  1194. *
  1195. ***************************************************************/
  1196. /** @addtogroup UART_Private
  1197. * @{
  1198. */
  1199. /**
  1200. * @brief DMA UART transmit process complete callback
  1201. * @param hdma: DMA handle
  1202. * @retval None
  1203. */
  1204. static void UART_DMATransmitCplt(DMA_HandleTypeDef *hdma)
  1205. {
  1206. UART_HandleTypeDef* huart = ( UART_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1207. /* DMA Normal mode */
  1208. if ( HAL_IS_BIT_CLR(hdma->Instance->CCR, DMA_CCR_CIRC) )
  1209. {
  1210. huart->TxXferCount = 0U;
  1211. /* Disable the DMA transfer for transmit request by resetting the DMAT bit
  1212. in the UART CR3 register */
  1213. huart->Instance->CR3 &= (uint32_t)~((uint32_t)USART_CR3_DMAT);
  1214. /* Enable the UART Transmit Complete Interrupt */
  1215. __HAL_UART_ENABLE_IT(huart, UART_IT_TC);
  1216. }
  1217. /* DMA Circular mode */
  1218. else
  1219. {
  1220. HAL_UART_TxCpltCallback(huart);
  1221. }
  1222. }
  1223. /**
  1224. * @brief DMA UART transmit process half complete callback
  1225. * @param hdma : DMA handle
  1226. * @retval None
  1227. */
  1228. static void UART_DMATxHalfCplt(DMA_HandleTypeDef *hdma)
  1229. {
  1230. UART_HandleTypeDef* huart = (UART_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
  1231. HAL_UART_TxHalfCpltCallback(huart);
  1232. }
  1233. /**
  1234. * @brief DMA UART receive process complete callback
  1235. * @param hdma: DMA handle
  1236. * @retval None
  1237. */
  1238. static void UART_DMAReceiveCplt(DMA_HandleTypeDef *hdma)
  1239. {
  1240. UART_HandleTypeDef* huart = ( UART_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1241. /* DMA Normal mode*/
  1242. if((hdma->Instance->CCR & DMA_CCR_CIRC) == 0U)
  1243. {
  1244. huart->RxXferCount = 0U;
  1245. /* Disable the DMA transfer for the receiver request by setting the DMAR bit
  1246. in the UART CR3 register */
  1247. huart->Instance->CR3 &= (uint32_t)~((uint32_t)USART_CR3_DMAR);
  1248. /* At end of Rx process, restore huart->RxState to Ready */
  1249. huart->RxState = HAL_UART_STATE_READY;
  1250. }
  1251. HAL_UART_RxCpltCallback(huart);
  1252. }
  1253. /**
  1254. * @brief DMA UART receive process half complete callback
  1255. * @param hdma : DMA handle
  1256. * @retval None
  1257. */
  1258. static void UART_DMARxHalfCplt(DMA_HandleTypeDef *hdma)
  1259. {
  1260. UART_HandleTypeDef* huart = (UART_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
  1261. HAL_UART_RxHalfCpltCallback(huart);
  1262. }
  1263. /**
  1264. * @brief DMA UART communication error callback
  1265. * @param hdma: DMA handle
  1266. * @retval None
  1267. */
  1268. static void UART_DMAError(DMA_HandleTypeDef *hdma)
  1269. {
  1270. UART_HandleTypeDef* huart = ( UART_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1271. huart->RxXferCount = 0U;
  1272. huart->TxXferCount = 0U;
  1273. huart->gState= HAL_UART_STATE_READY;
  1274. huart->RxState= HAL_UART_STATE_READY;
  1275. huart->ErrorCode |= HAL_UART_ERROR_DMA;
  1276. HAL_UART_ErrorCallback(huart);
  1277. }
  1278. /**
  1279. * @brief Send an amount of data in interrupt mode
  1280. * Function called under interruption only, once
  1281. * interruptions have been enabled by HAL_UART_Transmit_IT()
  1282. * @param huart: UART handle
  1283. * @retval HAL status
  1284. */
  1285. static HAL_StatusTypeDef UART_Transmit_IT(UART_HandleTypeDef *huart)
  1286. {
  1287. uint16_t* tmp;
  1288. if (huart->gState == HAL_UART_STATE_BUSY_TX)
  1289. {
  1290. if(huart->TxXferCount == 0U)
  1291. {
  1292. /* Disable the UART TXE Interrupt */
  1293. __HAL_UART_DISABLE_IT(huart, UART_IT_TXE);
  1294. /* Enable the UART Transmit Complete Interrupt */
  1295. __HAL_UART_ENABLE_IT(huart, UART_IT_TC);
  1296. return HAL_OK;
  1297. }
  1298. else
  1299. {
  1300. if ((huart->Init.WordLength == UART_WORDLENGTH_9B) && (huart->Init.Parity == UART_PARITY_NONE))
  1301. {
  1302. tmp = (uint16_t*) huart->pTxBuffPtr;
  1303. huart->Instance->TDR = (*tmp & (uint16_t)0x01FFU);
  1304. huart->pTxBuffPtr += 2U;
  1305. }
  1306. else
  1307. {
  1308. huart->Instance->TDR = (uint8_t)(*huart->pTxBuffPtr++ & (uint8_t)0xFFU);
  1309. }
  1310. huart->TxXferCount--;
  1311. return HAL_OK;
  1312. }
  1313. }
  1314. else
  1315. {
  1316. return HAL_BUSY;
  1317. }
  1318. }
  1319. /**
  1320. * @brief Wraps up transmission in non blocking mode.
  1321. * @param huart: pointer to a UART_HandleTypeDef structure that contains
  1322. * the configuration information for the specified UART module.
  1323. * @retval HAL status
  1324. */
  1325. static HAL_StatusTypeDef UART_EndTransmit_IT(UART_HandleTypeDef *huart)
  1326. {
  1327. /* Disable the UART Transmit Complete Interrupt */
  1328. __HAL_UART_DISABLE_IT(huart, UART_IT_TC);
  1329. huart->gState = HAL_UART_STATE_READY;
  1330. HAL_UART_TxCpltCallback(huart);
  1331. return HAL_OK;
  1332. }
  1333. /**
  1334. * @brief Receive an amount of data in interrupt mode
  1335. * Function called under interruption only, once
  1336. * interruptions have been enabled by HAL_UART_Receive_IT()
  1337. * @param huart: UART handle
  1338. * @retval HAL status
  1339. */
  1340. static HAL_StatusTypeDef UART_Receive_IT(UART_HandleTypeDef *huart)
  1341. {
  1342. uint16_t* tmp;
  1343. uint16_t uhMask = huart->Mask;
  1344. /* Check that a Rx process is ongoing */
  1345. if(huart->RxState == HAL_UART_STATE_BUSY_RX)
  1346. {
  1347. if ((huart->Init.WordLength == UART_WORDLENGTH_9B) && (huart->Init.Parity == UART_PARITY_NONE))
  1348. {
  1349. tmp = (uint16_t*) huart->pRxBuffPtr ;
  1350. *tmp = (uint16_t)(huart->Instance->RDR & uhMask);
  1351. huart->pRxBuffPtr +=2U;
  1352. }
  1353. else
  1354. {
  1355. *huart->pRxBuffPtr++ = (uint8_t)(huart->Instance->RDR & (uint8_t)uhMask);
  1356. }
  1357. if(--huart->RxXferCount == 0U)
  1358. {
  1359. while(HAL_IS_BIT_SET(huart->Instance->ISR, UART_FLAG_RXNE))
  1360. {
  1361. }
  1362. __HAL_UART_DISABLE_IT(huart, UART_IT_RXNE);
  1363. /* Disable the UART Parity Error Interrupt */
  1364. __HAL_UART_DISABLE_IT(huart, UART_IT_PE);
  1365. /* Disable the UART Error Interrupt: (Frame error, noise error, overrun error) */
  1366. __HAL_UART_DISABLE_IT(huart, UART_IT_ERR);
  1367. /* Rx process is completed, restore huart->RxState to Ready */
  1368. huart->RxState = HAL_UART_STATE_READY;
  1369. HAL_UART_RxCpltCallback(huart);
  1370. return HAL_OK;
  1371. }
  1372. return HAL_OK;
  1373. }
  1374. else
  1375. {
  1376. /* Clear RXNE interrupt flag */
  1377. __HAL_UART_SEND_REQ(huart, UART_RXDATA_FLUSH_REQUEST);
  1378. return HAL_BUSY;
  1379. }
  1380. }
  1381. /**
  1382. * @brief Configure the UART peripheral
  1383. * @param huart: uart handle
  1384. * @retval None
  1385. */
  1386. void UART_SetConfig(UART_HandleTypeDef *huart)
  1387. {
  1388. uint32_t tmpreg = 0x00000000U;
  1389. uint32_t clocksource = 0x00000000U;
  1390. uint16_t brrtemp = 0x0000U;
  1391. uint16_t usartdiv = 0x0000U;
  1392. /* Check the parameters */
  1393. assert_param(IS_UART_BAUDRATE(huart->Init.BaudRate));
  1394. assert_param(IS_UART_WORD_LENGTH(huart->Init.WordLength));
  1395. if(UART_INSTANCE_LOWPOWER(huart))
  1396. {
  1397. assert_param(IS_LPUART_STOPBITS(huart->Init.StopBits));
  1398. }
  1399. else
  1400. {
  1401. assert_param(IS_UART_STOPBITS(huart->Init.StopBits));
  1402. assert_param(IS_UART_ONE_BIT_SAMPLE(huart->Init.OneBitSampling));
  1403. }
  1404. assert_param(IS_UART_PARITY(huart->Init.Parity));
  1405. assert_param(IS_UART_MODE(huart->Init.Mode));
  1406. assert_param(IS_UART_HARDWARE_FLOW_CONTROL(huart->Init.HwFlowCtl));
  1407. assert_param(IS_UART_OVERSAMPLING(huart->Init.OverSampling));
  1408. /*-------------------------- USART CR1 Configuration -----------------------*/
  1409. /* Clear M, PCE, PS, TE, RE and OVER8 bits and configure
  1410. * the UART Word Length, Parity, Mode and oversampling:
  1411. * set the M bits according to huart->Init.WordLength value
  1412. * set PCE and PS bits according to huart->Init.Parity value
  1413. * set TE and RE bits according to huart->Init.Mode value
  1414. * set OVER8 bit according to huart->Init.OverSampling value */
  1415. tmpreg = (uint32_t)huart->Init.WordLength | huart->Init.Parity | huart->Init.Mode | huart->Init.OverSampling ;
  1416. MODIFY_REG(huart->Instance->CR1, UART_CR1_FIELDS, tmpreg);
  1417. /*-------------------------- USART CR2 Configuration -----------------------*/
  1418. /* Configure the UART Stop Bits: Set STOP[13:12] bits according
  1419. * to huart->Init.StopBits value */
  1420. MODIFY_REG(huart->Instance->CR2, USART_CR2_STOP, huart->Init.StopBits);
  1421. /*-------------------------- USART CR3 Configuration -----------------------*/
  1422. /* Configure
  1423. * - UART HardWare Flow Control: set CTSE and RTSE bits according
  1424. * to huart->Init.HwFlowCtl value
  1425. * - one-bit sampling method versus three samples' majority rule according
  1426. * to huart->Init.OneBitSampling */
  1427. tmpreg = (uint32_t)huart->Init.HwFlowCtl;
  1428. if (!(UART_INSTANCE_LOWPOWER(huart)))
  1429. {
  1430. tmpreg |= huart->Init.OneBitSampling;
  1431. }
  1432. MODIFY_REG(huart->Instance->CR3, (USART_CR3_RTSE | USART_CR3_CTSE | USART_CR3_ONEBIT), tmpreg);
  1433. /*-------------------------- USART BRR Configuration -----------------------*/
  1434. UART_GETCLOCKSOURCE(huart, clocksource);
  1435. /* Check LPUART instace */
  1436. if(UART_INSTANCE_LOWPOWER(huart))
  1437. {
  1438. switch (clocksource)
  1439. {
  1440. case UART_CLOCKSOURCE_PCLK1:
  1441. huart->Instance->BRR = (uint32_t)(__DIV_LPUART(HAL_RCC_GetPCLK1Freq(), huart->Init.BaudRate));
  1442. break;
  1443. case UART_CLOCKSOURCE_HSI:
  1444. if (__HAL_RCC_GET_FLAG(RCC_FLAG_HSIDIV) != 0U)
  1445. {
  1446. huart->Instance->BRR = (uint32_t)(__DIV_LPUART((HSI_VALUE >> 2U), huart->Init.BaudRate));
  1447. }
  1448. else
  1449. {
  1450. huart->Instance->BRR = (uint32_t)(__DIV_LPUART(HSI_VALUE, huart->Init.BaudRate));
  1451. }
  1452. break;
  1453. case UART_CLOCKSOURCE_SYSCLK:
  1454. huart->Instance->BRR = (uint32_t)(__DIV_LPUART(HAL_RCC_GetSysClockFreq(), huart->Init.BaudRate));
  1455. break;
  1456. case UART_CLOCKSOURCE_LSE:
  1457. huart->Instance->BRR = (uint32_t)(__DIV_LPUART(LSE_VALUE, huart->Init.BaudRate));
  1458. break;
  1459. default:
  1460. break;
  1461. }
  1462. }
  1463. /* Check the UART Over Sampling 8 to set Baud Rate Register */
  1464. else if (huart->Init.OverSampling == UART_OVERSAMPLING_8)
  1465. {
  1466. switch (clocksource)
  1467. {
  1468. case UART_CLOCKSOURCE_PCLK1:
  1469. usartdiv = (uint32_t)(UART_DIV_SAMPLING8(HAL_RCC_GetPCLK1Freq(), huart->Init.BaudRate));
  1470. break;
  1471. case UART_CLOCKSOURCE_PCLK2:
  1472. usartdiv = (uint32_t)(UART_DIV_SAMPLING8(HAL_RCC_GetPCLK2Freq(), huart->Init.BaudRate));
  1473. break;
  1474. case UART_CLOCKSOURCE_HSI:
  1475. if (__HAL_RCC_GET_FLAG(RCC_FLAG_HSIDIV) != 0U)
  1476. {
  1477. usartdiv = (uint32_t)(UART_DIV_SAMPLING8((HSI_VALUE >> 2U), huart->Init.BaudRate));
  1478. }
  1479. else
  1480. {
  1481. usartdiv = (uint32_t)(UART_DIV_SAMPLING8(HSI_VALUE, huart->Init.BaudRate));
  1482. }
  1483. break;
  1484. case UART_CLOCKSOURCE_SYSCLK:
  1485. usartdiv = (uint32_t)(UART_DIV_SAMPLING8(HAL_RCC_GetSysClockFreq(), huart->Init.BaudRate));
  1486. break;
  1487. case UART_CLOCKSOURCE_LSE:
  1488. usartdiv = (uint32_t)(UART_DIV_SAMPLING8(LSE_VALUE, huart->Init.BaudRate));
  1489. break;
  1490. default:
  1491. break;
  1492. }
  1493. brrtemp = usartdiv & 0xFFF0U;
  1494. brrtemp |= (uint16_t)((uint16_t)(usartdiv & (uint16_t)0x000FU) >> (uint16_t)1U);
  1495. huart->Instance->BRR = brrtemp;
  1496. }
  1497. else
  1498. {
  1499. switch (clocksource)
  1500. {
  1501. case UART_CLOCKSOURCE_PCLK1:
  1502. huart->Instance->BRR = (uint32_t)(UART_DIV_SAMPLING16(HAL_RCC_GetPCLK1Freq(), huart->Init.BaudRate));
  1503. break;
  1504. case UART_CLOCKSOURCE_PCLK2:
  1505. huart->Instance->BRR = (uint32_t)(UART_DIV_SAMPLING16(HAL_RCC_GetPCLK2Freq(), huart->Init.BaudRate));
  1506. break;
  1507. case UART_CLOCKSOURCE_HSI:
  1508. if (__HAL_RCC_GET_FLAG(RCC_FLAG_HSIDIV) != 0U)
  1509. {
  1510. huart->Instance->BRR = (uint32_t)(UART_DIV_SAMPLING16((HSI_VALUE >> 2U), huart->Init.BaudRate));
  1511. }
  1512. else
  1513. {
  1514. huart->Instance->BRR = (uint32_t)(UART_DIV_SAMPLING16(HSI_VALUE, huart->Init.BaudRate));
  1515. }
  1516. break;
  1517. case UART_CLOCKSOURCE_SYSCLK:
  1518. huart->Instance->BRR = (uint32_t)(UART_DIV_SAMPLING16(HAL_RCC_GetSysClockFreq(), huart->Init.BaudRate));
  1519. break;
  1520. case UART_CLOCKSOURCE_LSE:
  1521. huart->Instance->BRR = (uint32_t)(UART_DIV_SAMPLING16(LSE_VALUE, huart->Init.BaudRate));
  1522. break;
  1523. default:
  1524. break;
  1525. }
  1526. }
  1527. }
  1528. /**
  1529. * @brief Check the UART Idle State
  1530. * @param huart: uart handle
  1531. * @retval HAL status
  1532. */
  1533. HAL_StatusTypeDef UART_CheckIdleState(UART_HandleTypeDef *huart)
  1534. {
  1535. /* Check if the Transmitter is enabled */
  1536. if((huart->Instance->CR1 & USART_CR1_TE) == USART_CR1_TE)
  1537. {
  1538. /* Wait until TEACK flag is set */
  1539. if(UART_WaitOnFlagUntilTimeout(huart, USART_ISR_TEACK, RESET, UART_TIMEOUT_VALUE) != HAL_OK)
  1540. {
  1541. return HAL_TIMEOUT;
  1542. }
  1543. }
  1544. /* Check if the Receiver is enabled */
  1545. if((huart->Instance->CR1 & USART_CR1_RE) == USART_CR1_RE)
  1546. {
  1547. /* Wait until REACK flag is set */
  1548. if(UART_WaitOnFlagUntilTimeout(huart, USART_ISR_REACK, RESET, UART_TIMEOUT_VALUE) != HAL_OK)
  1549. {
  1550. return HAL_TIMEOUT;
  1551. }
  1552. }
  1553. /* Process Unlocked */
  1554. __HAL_UNLOCK(huart);
  1555. /* Initialize the UART state*/
  1556. huart->ErrorCode = HAL_UART_ERROR_NONE;
  1557. huart->gState= HAL_UART_STATE_READY;
  1558. huart->RxState= HAL_UART_STATE_READY;
  1559. return HAL_OK;
  1560. }
  1561. /**
  1562. * @brief Configure the UART peripheral advanced feautures
  1563. * @param huart: uart handle
  1564. * @retval None
  1565. */
  1566. void UART_AdvFeatureConfig(UART_HandleTypeDef *huart)
  1567. {
  1568. /* Check whether the set of advanced features to configure is properly set */
  1569. assert_param(IS_UART_ADVFEATURE_INIT(huart->AdvancedInit.AdvFeatureInit));
  1570. /* if required, configure TX pin active level inversion */
  1571. if(HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_TXINVERT_INIT))
  1572. {
  1573. assert_param(IS_UART_ADVFEATURE_TXINV(huart->AdvancedInit.TxPinLevelInvert));
  1574. MODIFY_REG(huart->Instance->CR2, USART_CR2_TXINV, huart->AdvancedInit.TxPinLevelInvert);
  1575. }
  1576. /* if required, configure RX pin active level inversion */
  1577. if(HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_RXINVERT_INIT))
  1578. {
  1579. assert_param(IS_UART_ADVFEATURE_RXINV(huart->AdvancedInit.RxPinLevelInvert));
  1580. MODIFY_REG(huart->Instance->CR2, USART_CR2_RXINV, huart->AdvancedInit.RxPinLevelInvert);
  1581. }
  1582. /* if required, configure data inversion */
  1583. if(HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_DATAINVERT_INIT))
  1584. {
  1585. assert_param(IS_UART_ADVFEATURE_DATAINV(huart->AdvancedInit.DataInvert));
  1586. MODIFY_REG(huart->Instance->CR2, USART_CR2_DATAINV, huart->AdvancedInit.DataInvert);
  1587. }
  1588. /* if required, configure RX/TX pins swap */
  1589. if(HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_SWAP_INIT))
  1590. {
  1591. assert_param(IS_UART_ADVFEATURE_SWAP(huart->AdvancedInit.Swap));
  1592. MODIFY_REG(huart->Instance->CR2, USART_CR2_SWAP, huart->AdvancedInit.Swap);
  1593. }
  1594. /* if required, configure RX overrun detection disabling */
  1595. if(HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_RXOVERRUNDISABLE_INIT))
  1596. {
  1597. assert_param(IS_UART_OVERRUN(huart->AdvancedInit.OverrunDisable));
  1598. MODIFY_REG(huart->Instance->CR3, USART_CR3_OVRDIS, huart->AdvancedInit.OverrunDisable);
  1599. }
  1600. /* if required, configure DMA disabling on reception error */
  1601. if(HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_DMADISABLEONERROR_INIT))
  1602. {
  1603. assert_param(IS_UART_ADVFEATURE_DMAONRXERROR(huart->AdvancedInit.DMADisableonRxError));
  1604. MODIFY_REG(huart->Instance->CR3, USART_CR3_DDRE, huart->AdvancedInit.DMADisableonRxError);
  1605. }
  1606. /* if required, configure auto Baud rate detection scheme */
  1607. if(HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_AUTOBAUDRATE_INIT))
  1608. {
  1609. assert_param(IS_USART_AUTOBAUDRATE_DETECTION_INSTANCE(huart->Instance));
  1610. assert_param(IS_UART_ADVFEATURE_AUTOBAUDRATE(huart->AdvancedInit.AutoBaudRateEnable));
  1611. MODIFY_REG(huart->Instance->CR2, USART_CR2_ABREN, huart->AdvancedInit.AutoBaudRateEnable);
  1612. /* set auto Baudrate detection parameters if detection is enabled */
  1613. if(huart->AdvancedInit.AutoBaudRateEnable == UART_ADVFEATURE_AUTOBAUDRATE_ENABLE)
  1614. {
  1615. assert_param(IS_UART_ADVFEATURE_AUTOBAUDRATEMODE(huart->AdvancedInit.AutoBaudRateMode));
  1616. MODIFY_REG(huart->Instance->CR2, USART_CR2_ABRMODE, huart->AdvancedInit.AutoBaudRateMode);
  1617. }
  1618. }
  1619. /* if required, configure MSB first on communication line */
  1620. if(HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_MSBFIRST_INIT))
  1621. {
  1622. assert_param(IS_UART_ADVFEATURE_MSBFIRST(huart->AdvancedInit.MSBFirst));
  1623. MODIFY_REG(huart->Instance->CR2, USART_CR2_MSBFIRST, huart->AdvancedInit.MSBFirst);
  1624. }
  1625. }
  1626. /**
  1627. * @brief This function handles UART Communication Timeout.
  1628. * @param huart: UART handle
  1629. * @param Flag: specifies the UART flag to check.
  1630. * @param Status: The new Flag status (SET or RESET).
  1631. * @param Timeout: Timeout duration
  1632. * @retval HAL status
  1633. */
  1634. HAL_StatusTypeDef UART_WaitOnFlagUntilTimeout(UART_HandleTypeDef *huart, uint32_t Flag, FlagStatus Status, uint32_t Timeout)
  1635. {
  1636. uint32_t tickstart = 0x00U;
  1637. tickstart = HAL_GetTick();
  1638. /* Wait until flag is set */
  1639. if(Status == RESET)
  1640. {
  1641. while(__HAL_UART_GET_FLAG(huart, Flag) == RESET)
  1642. {
  1643. /* Check for the Timeout */
  1644. if(Timeout != HAL_MAX_DELAY)
  1645. {
  1646. if((Timeout == 0U)||((HAL_GetTick() - tickstart ) > Timeout))
  1647. {
  1648. /* Disable TXE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts for the interrupt process */
  1649. __HAL_UART_DISABLE_IT(huart, UART_IT_TXE);
  1650. __HAL_UART_DISABLE_IT(huart, UART_IT_RXNE);
  1651. __HAL_UART_DISABLE_IT(huart, UART_IT_PE);
  1652. __HAL_UART_DISABLE_IT(huart, UART_IT_ERR);
  1653. huart->gState= HAL_UART_STATE_READY;
  1654. huart->RxState= HAL_UART_STATE_READY;
  1655. /* Process Unlocked */
  1656. __HAL_UNLOCK(huart);
  1657. return HAL_TIMEOUT;
  1658. }
  1659. }
  1660. }
  1661. }
  1662. else
  1663. {
  1664. while(__HAL_UART_GET_FLAG(huart, Flag) != RESET)
  1665. {
  1666. /* Check for the Timeout */
  1667. if(Timeout != HAL_MAX_DELAY)
  1668. {
  1669. if((Timeout == 0U)||((HAL_GetTick() - tickstart ) > Timeout))
  1670. {
  1671. /* Disable TXE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts for the interrupt process */
  1672. __HAL_UART_DISABLE_IT(huart, UART_IT_TXE);
  1673. __HAL_UART_DISABLE_IT(huart, UART_IT_RXNE);
  1674. __HAL_UART_DISABLE_IT(huart, UART_IT_PE);
  1675. __HAL_UART_DISABLE_IT(huart, UART_IT_ERR);
  1676. huart->gState= HAL_UART_STATE_READY;
  1677. huart->RxState= HAL_UART_STATE_READY;
  1678. /* Process Unlocked */
  1679. __HAL_UNLOCK(huart);
  1680. return HAL_TIMEOUT;
  1681. }
  1682. }
  1683. }
  1684. }
  1685. return HAL_OK;
  1686. }
  1687. /**
  1688. * @}
  1689. */
  1690. /**
  1691. * @}
  1692. */
  1693. #endif /* HAL_UART_MODULE_ENABLED */
  1694. /**
  1695. * @}
  1696. */
  1697. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/