stm32l1xx_hal_uart.c 62 KB

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