stm32f7xx_hal_uart.c 69 KB

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