stm8s_uart3.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737
  1. /**
  2. ********************************************************************************
  3. * @file stm8s_uart3.c
  4. * @author MCD Application Team
  5. * @version V2.3.0
  6. * @date 16-June-2017
  7. * @brief This file contains all the functions for the uart3 peripheral.
  8. ******************************************************************************
  9. * @attention
  10. *
  11. * <h2><center>&copy; COPYRIGHT 2014 STMicroelectronics</center></h2>
  12. *
  13. * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
  14. * You may not use this file except in compliance with the License.
  15. * You may obtain a copy of the License at:
  16. *
  17. * http://www.st.com/software_license_agreement_liberty_v2
  18. *
  19. * Unless required by applicable law or agreed to in writing, software
  20. * distributed under the License is distributed on an "AS IS" BASIS,
  21. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  22. * See the License for the specific language governing permissions and
  23. * limitations under the License.
  24. *
  25. ******************************************************************************
  26. */
  27. /* Includes ------------------------------------------------------------------*/
  28. #include "stm8s_uart3.h"
  29. /** @addtogroup STM8S_StdPeriph_Driver
  30. * @{
  31. */
  32. /* Private typedef -----------------------------------------------------------*/
  33. /* Private define ------------------------------------------------------------*/
  34. /* Private macro -------------------------------------------------------------*/
  35. /* Private variables ---------------------------------------------------------*/
  36. /* Private function prototypes -----------------------------------------------*/
  37. /* Private functions ---------------------------------------------------------*/
  38. /* Public functions ----------------------------------------------------------*/
  39. /**
  40. * @addtogroup UART3_Public_Functions
  41. * @{
  42. */
  43. /**
  44. * @brief Deinitializes the UART peripheral.
  45. * @param None
  46. * @retval None
  47. */
  48. void UART3_DeInit(void)
  49. {
  50. /* Clear the Idle Line Detected bit in the status rerister by a read
  51. to the UART3_SR register followed by a Read to the UART3_DR register */
  52. (void) UART3->SR;
  53. (void) UART3->DR;
  54. UART3->BRR2 = UART3_BRR2_RESET_VALUE; /*Set UART3_BRR2 to reset value 0x00 */
  55. UART3->BRR1 = UART3_BRR1_RESET_VALUE; /*Set UART3_BRR1 to reset value 0x00 */
  56. UART3->CR1 = UART3_CR1_RESET_VALUE; /*Set UART3_CR1 to reset value 0x00 */
  57. UART3->CR2 = UART3_CR2_RESET_VALUE; /*Set UART3_CR2 to reset value 0x00 */
  58. UART3->CR3 = UART3_CR3_RESET_VALUE; /*Set UART3_CR3 to reset value 0x00 */
  59. UART3->CR4 = UART3_CR4_RESET_VALUE; /*Set UART3_CR4 to reset value 0x00 */
  60. UART3->CR6 = UART3_CR6_RESET_VALUE; /*Set UART3_CR6 to reset value 0x00 */
  61. }
  62. /**
  63. * @brief Initializes the UART3 according to the specified parameters.
  64. * @param BaudRate: The baudrate.
  65. * @param WordLength : This parameter can be any of
  66. * the @ref UART3_WordLength_TypeDef enumeration.
  67. * @param StopBits: This parameter can be any of the
  68. * @ref UART3_StopBits_TypeDef enumeration.
  69. * @param Parity: This parameter can be any of the
  70. * @ref UART3_Parity_TypeDef enumeration.
  71. * @param Mode: This parameter can be any of the @ref UART3_Mode_TypeDef values
  72. * @retval None
  73. */
  74. void UART3_Init(uint32_t BaudRate, UART3_WordLength_TypeDef WordLength,
  75. UART3_StopBits_TypeDef StopBits, UART3_Parity_TypeDef Parity,
  76. UART3_Mode_TypeDef Mode)
  77. {
  78. uint8_t BRR2_1 = 0, BRR2_2 = 0;
  79. uint32_t BaudRate_Mantissa = 0, BaudRate_Mantissa100 = 0;
  80. /* Check the parameters */
  81. assert_param(IS_UART3_WORDLENGTH_OK(WordLength));
  82. assert_param(IS_UART3_STOPBITS_OK(StopBits));
  83. assert_param(IS_UART3_PARITY_OK(Parity));
  84. assert_param(IS_UART3_BAUDRATE_OK(BaudRate));
  85. assert_param(IS_UART3_MODE_OK((uint8_t)Mode));
  86. /* Clear the word length bit */
  87. UART3->CR1 &= (uint8_t)(~UART3_CR1_M);
  88. /* Set the word length bit according to UART3_WordLength value */
  89. UART3->CR1 |= (uint8_t)WordLength;
  90. /* Clear the STOP bits */
  91. UART3->CR3 &= (uint8_t)(~UART3_CR3_STOP);
  92. /* Set the STOP bits number according to UART3_StopBits value */
  93. UART3->CR3 |= (uint8_t)StopBits;
  94. /* Clear the Parity Control bit */
  95. UART3->CR1 &= (uint8_t)(~(UART3_CR1_PCEN | UART3_CR1_PS));
  96. /* Set the Parity Control bit to UART3_Parity value */
  97. UART3->CR1 |= (uint8_t)Parity;
  98. /* Clear the LSB mantissa of UART3DIV */
  99. UART3->BRR1 &= (uint8_t)(~UART3_BRR1_DIVM);
  100. /* Clear the MSB mantissa of UART3DIV */
  101. UART3->BRR2 &= (uint8_t)(~UART3_BRR2_DIVM);
  102. /* Clear the Fraction bits of UART3DIV */
  103. UART3->BRR2 &= (uint8_t)(~UART3_BRR2_DIVF);
  104. /* Set the UART3 BaudRates in BRR1 and BRR2 registers according to UART3_BaudRate value */
  105. BaudRate_Mantissa = ((uint32_t)CLK_GetClockFreq() / (BaudRate << 4));
  106. BaudRate_Mantissa100 = (((uint32_t)CLK_GetClockFreq() * 100) / (BaudRate << 4));
  107. /* The fraction and MSB mantissa should be loaded in one step in the BRR2 register */
  108. /* Set the fraction of UART3DIV */
  109. BRR2_1 = (uint8_t)((uint8_t)(((BaudRate_Mantissa100 - (BaudRate_Mantissa * 100))
  110. << 4) / 100) & (uint8_t)0x0F);
  111. BRR2_2 = (uint8_t)((BaudRate_Mantissa >> 4) & (uint8_t)0xF0);
  112. UART3->BRR2 = (uint8_t)(BRR2_1 | BRR2_2);
  113. /* Set the LSB mantissa of UART3DIV */
  114. UART3->BRR1 = (uint8_t)BaudRate_Mantissa;
  115. if ((uint8_t)(Mode & UART3_MODE_TX_ENABLE))
  116. {
  117. /* Set the Transmitter Enable bit */
  118. UART3->CR2 |= UART3_CR2_TEN;
  119. }
  120. else
  121. {
  122. /* Clear the Transmitter Disable bit */
  123. UART3->CR2 &= (uint8_t)(~UART3_CR2_TEN);
  124. }
  125. if ((uint8_t)(Mode & UART3_MODE_RX_ENABLE))
  126. {
  127. /* Set the Receiver Enable bit */
  128. UART3->CR2 |= UART3_CR2_REN;
  129. }
  130. else
  131. {
  132. /* Clear the Receiver Disable bit */
  133. UART3->CR2 &= (uint8_t)(~UART3_CR2_REN);
  134. }
  135. }
  136. /**
  137. * @brief Enable the UART1 peripheral.
  138. * @param NewState : The new state of the UART Communication.
  139. * This parameter can be any of the @ref FunctionalState enumeration.
  140. * @retval None
  141. */
  142. void UART3_Cmd(FunctionalState NewState)
  143. {
  144. if (NewState != DISABLE)
  145. {
  146. /* UART3 Enable */
  147. UART3->CR1 &= (uint8_t)(~UART3_CR1_UARTD);
  148. }
  149. else
  150. {
  151. /* UART3 Disable */
  152. UART3->CR1 |= UART3_CR1_UARTD;
  153. }
  154. }
  155. /**
  156. * @brief Enables or disables the specified UART3 interrupts.
  157. * @param UART3_IT specifies the UART3 interrupt sources to be enabled or disabled.
  158. * This parameter can be one of the following values:
  159. * - UART3_IT_LBDF: LIN Break detection interrupt
  160. * - UART3_IT_LHDF: LIN Break detection interrupt
  161. * - UART3_IT_TXE: Transmit Data Register empty interrupt
  162. * - UART3_IT_TC: Transmission complete interrupt
  163. * - UART3_IT_RXNE_OR: Receive Data register not empty/Over run error interrupt
  164. * - UART3_IT_IDLE: Idle line detection interrupt
  165. * - UART3_IT_PE: Parity Error interrupt
  166. * @param NewState new state of the specified UART3 interrupts.
  167. * This parameter can be: ENABLE or DISABLE.
  168. * @retval None
  169. */
  170. void UART3_ITConfig(UART3_IT_TypeDef UART3_IT, FunctionalState NewState)
  171. {
  172. uint8_t uartreg = 0, itpos = 0x00;
  173. /* Check the parameters */
  174. assert_param(IS_UART3_CONFIG_IT_OK(UART3_IT));
  175. assert_param(IS_FUNCTIONALSTATE_OK(NewState));
  176. /* Get the UART3 register index */
  177. uartreg = (uint8_t)((uint16_t)UART3_IT >> 0x08);
  178. /* Get the UART3 IT index */
  179. itpos = (uint8_t)((uint8_t)1 << (uint8_t)((uint8_t)UART3_IT & (uint8_t)0x0F));
  180. if (NewState != DISABLE)
  181. {
  182. /* Enable the Interrupt bits according to UART3_IT mask */
  183. if (uartreg == 0x01)
  184. {
  185. UART3->CR1 |= itpos;
  186. }
  187. else if (uartreg == 0x02)
  188. {
  189. UART3->CR2 |= itpos;
  190. }
  191. else if (uartreg == 0x03)
  192. {
  193. UART3->CR4 |= itpos;
  194. }
  195. else
  196. {
  197. UART3->CR6 |= itpos;
  198. }
  199. }
  200. else
  201. {
  202. /* Disable the interrupt bits according to UART3_IT mask */
  203. if (uartreg == 0x01)
  204. {
  205. UART3->CR1 &= (uint8_t)(~itpos);
  206. }
  207. else if (uartreg == 0x02)
  208. {
  209. UART3->CR2 &= (uint8_t)(~itpos);
  210. }
  211. else if (uartreg == 0x03)
  212. {
  213. UART3->CR4 &= (uint8_t)(~itpos);
  214. }
  215. else
  216. {
  217. UART3->CR6 &= (uint8_t)(~itpos);
  218. }
  219. }
  220. }
  221. /**
  222. * @brief Sets the UART3 LIN Break detection length.
  223. * @param UART3_LINBreakDetectionLength specifies the LIN break detection length.
  224. * This parameter can be any of the
  225. * @ref UART3_LINBreakDetectionLength_TypeDef values.
  226. * @retval None
  227. */
  228. void UART3_LINBreakDetectionConfig(UART3_LINBreakDetectionLength_TypeDef UART3_LINBreakDetectionLength)
  229. {
  230. /* Check the parameters */
  231. assert_param(IS_UART3_LINBREAKDETECTIONLENGTH_OK(UART3_LINBreakDetectionLength));
  232. if (UART3_LINBreakDetectionLength != UART3_LINBREAKDETECTIONLENGTH_10BITS)
  233. {
  234. UART3->CR4 |= UART3_CR4_LBDL;
  235. }
  236. else
  237. {
  238. UART3->CR4 &= ((uint8_t)~UART3_CR4_LBDL);
  239. }
  240. }
  241. /**
  242. * @brief Configure the UART3 peripheral.
  243. * @param UART3_Mode specifies the LIN mode.
  244. * This parameter can be any of the @ref UART3_LinMode_TypeDef values.
  245. * @param UART3_Autosync specifies the LIN automatic resynchronization mode.
  246. * This parameter can be any of the @ref UART3_LinAutosync_TypeDef values.
  247. * @param UART3_DivUp specifies the LIN divider update method.
  248. * This parameter can be any of the @ref UART3_LinDivUp_TypeDef values.
  249. * @retval None
  250. */
  251. void UART3_LINConfig(UART3_LinMode_TypeDef UART3_Mode,
  252. UART3_LinAutosync_TypeDef UART3_Autosync,
  253. UART3_LinDivUp_TypeDef UART3_DivUp)
  254. {
  255. /* Check the parameters */
  256. assert_param(IS_UART3_SLAVE_OK(UART3_Mode));
  257. assert_param(IS_UART3_AUTOSYNC_OK(UART3_Autosync));
  258. assert_param(IS_UART3_DIVUP_OK(UART3_DivUp));
  259. if (UART3_Mode != UART3_LIN_MODE_MASTER)
  260. {
  261. UART3->CR6 |= UART3_CR6_LSLV;
  262. }
  263. else
  264. {
  265. UART3->CR6 &= ((uint8_t)~UART3_CR6_LSLV);
  266. }
  267. if (UART3_Autosync != UART3_LIN_AUTOSYNC_DISABLE)
  268. {
  269. UART3->CR6 |= UART3_CR6_LASE ;
  270. }
  271. else
  272. {
  273. UART3->CR6 &= ((uint8_t)~ UART3_CR6_LASE );
  274. }
  275. if (UART3_DivUp != UART3_LIN_DIVUP_LBRR1)
  276. {
  277. UART3->CR6 |= UART3_CR6_LDUM;
  278. }
  279. else
  280. {
  281. UART3->CR6 &= ((uint8_t)~ UART3_CR6_LDUM);
  282. }
  283. }
  284. /**
  285. * @brief Enables or disables the UART3 LIN mode.
  286. * @param NewState is new state of the UART3 LIN mode.
  287. * This parameter can be ENABLE or DISABLE
  288. * @retval None
  289. */
  290. void UART3_LINCmd(FunctionalState NewState)
  291. {
  292. /* Check the parameters */
  293. assert_param(IS_FUNCTIONALSTATE_OK(NewState));
  294. if (NewState != DISABLE)
  295. {
  296. /* Enable the LIN mode by setting the LINE bit in the CR2 register */
  297. UART3->CR3 |= UART3_CR3_LINEN;
  298. }
  299. else
  300. {
  301. /* Disable the LIN mode by clearing the LINE bit in the CR2 register */
  302. UART3->CR3 &= ((uint8_t)~UART3_CR3_LINEN);
  303. }
  304. }
  305. /**
  306. * @brief Selects the UART3 WakeUp method.
  307. * @param UART3_WakeUp: specifies the UART3 wakeup method.
  308. * This parameter can be any of the @ref UART3_WakeUp_TypeDef values.
  309. * @retval None
  310. */
  311. void UART3_WakeUpConfig(UART3_WakeUp_TypeDef UART3_WakeUp)
  312. {
  313. /* Check the parameters */
  314. assert_param(IS_UART3_WAKEUP_OK(UART3_WakeUp));
  315. UART3->CR1 &= ((uint8_t)~UART3_CR1_WAKE);
  316. UART3->CR1 |= (uint8_t)UART3_WakeUp;
  317. }
  318. /**
  319. * @brief Determines if the UART3 is in mute mode or not.
  320. * @param NewState: new state of the UART3 mode.
  321. * This parameter can be ENABLE or DISABLE
  322. * @retval None
  323. */
  324. void UART3_ReceiverWakeUpCmd(FunctionalState NewState)
  325. {
  326. /* Check the parameters */
  327. assert_param(IS_FUNCTIONALSTATE_OK(NewState));
  328. if (NewState != DISABLE)
  329. {
  330. /* Enable the mute mode UART3 by setting the RWU bit in the CR2 register */
  331. UART3->CR2 |= UART3_CR2_RWU;
  332. }
  333. else
  334. {
  335. /* Disable the mute mode UART3 by clearing the RWU bit in the CR1 register */
  336. UART3->CR2 &= ((uint8_t)~UART3_CR2_RWU);
  337. }
  338. }
  339. /**
  340. * @brief Returns the most recent received data by the UART3 peripheral.
  341. * @param None
  342. * @retval Received Data
  343. */
  344. uint8_t UART3_ReceiveData8(void)
  345. {
  346. return ((uint8_t)UART3->DR);
  347. }
  348. /**
  349. * @brief Returns the most recent received data by the UART3 peripheral.
  350. * @param None
  351. * @retval Received Data
  352. */
  353. uint16_t UART3_ReceiveData9(void)
  354. {
  355. uint16_t temp = 0;
  356. temp = (uint16_t)(((uint16_t)((uint16_t)UART3->CR1 & (uint16_t)UART3_CR1_R8)) << 1);
  357. return (uint16_t)((((uint16_t)UART3->DR) | temp) & ((uint16_t)0x01FF));
  358. }
  359. /**
  360. * @brief Transmits 8 bit data through the UART3 peripheral.
  361. * @param Data the data to transmit.
  362. * @retval None
  363. */
  364. void UART3_SendData8(uint8_t Data)
  365. {
  366. /* Transmit Data */
  367. UART3->DR = Data;
  368. }
  369. /**
  370. * @brief Transmits 9 bit data through the UART3 peripheral.
  371. * @param Data: the data to transmit.
  372. * @retval None
  373. */
  374. void UART3_SendData9(uint16_t Data)
  375. {
  376. /* Clear the transmit data bit 8 */
  377. UART3->CR1 &= ((uint8_t)~UART3_CR1_T8);
  378. /* Write the transmit data bit [8] */
  379. UART3->CR1 |= (uint8_t)(((uint8_t)(Data >> 2)) & UART3_CR1_T8);
  380. /* Write the transmit data bit [0:7] */
  381. UART3->DR = (uint8_t)(Data);
  382. }
  383. /**
  384. * @brief Transmits break characters.
  385. * @param None
  386. * @retval None
  387. */
  388. void UART3_SendBreak(void)
  389. {
  390. UART3->CR2 |= UART3_CR2_SBK;
  391. }
  392. /**
  393. * @brief Sets the address of the UART3 node.
  394. * @param UART3_Address: Indicates the address of the UART3 node.
  395. * @retval None
  396. */
  397. void UART3_SetAddress(uint8_t UART3_Address)
  398. {
  399. /* Check the parameters */
  400. assert_param(IS_UART3_ADDRESS_OK(UART3_Address));
  401. /* Clear the UART3 address */
  402. UART3->CR4 &= ((uint8_t)~UART3_CR4_ADD);
  403. /* Set the UART3 address node */
  404. UART3->CR4 |= UART3_Address;
  405. }
  406. /**
  407. * @brief Checks whether the specified UART3 flag is set or not.
  408. * @param UART3_FLAG specifies the flag to check.
  409. * This parameter can be any of the @ref UART3_Flag_TypeDef enumeration.
  410. * @retval FlagStatus (SET or RESET)
  411. */
  412. FlagStatus UART3_GetFlagStatus(UART3_Flag_TypeDef UART3_FLAG)
  413. {
  414. FlagStatus status = RESET;
  415. /* Check parameters */
  416. assert_param(IS_UART3_FLAG_OK(UART3_FLAG));
  417. /* Check the status of the specified UART3 flag*/
  418. if (UART3_FLAG == UART3_FLAG_LBDF)
  419. {
  420. if ((UART3->CR4 & (uint8_t)UART3_FLAG) != (uint8_t)0x00)
  421. {
  422. /* UART3_FLAG is set*/
  423. status = SET;
  424. }
  425. else
  426. {
  427. /* UART3_FLAG is reset*/
  428. status = RESET;
  429. }
  430. }
  431. else if (UART3_FLAG == UART3_FLAG_SBK)
  432. {
  433. if ((UART3->CR2 & (uint8_t)UART3_FLAG) != (uint8_t)0x00)
  434. {
  435. /* UART3_FLAG is set*/
  436. status = SET;
  437. }
  438. else
  439. {
  440. /* UART3_FLAG is reset*/
  441. status = RESET;
  442. }
  443. }
  444. else if ((UART3_FLAG == UART3_FLAG_LHDF) || (UART3_FLAG == UART3_FLAG_LSF))
  445. {
  446. if ((UART3->CR6 & (uint8_t)UART3_FLAG) != (uint8_t)0x00)
  447. {
  448. /* UART3_FLAG is set*/
  449. status = SET;
  450. }
  451. else
  452. {
  453. /* UART3_FLAG is reset*/
  454. status = RESET;
  455. }
  456. }
  457. else
  458. {
  459. if ((UART3->SR & (uint8_t)UART3_FLAG) != (uint8_t)0x00)
  460. {
  461. /* UART3_FLAG is set*/
  462. status = SET;
  463. }
  464. else
  465. {
  466. /* UART3_FLAG is reset*/
  467. status = RESET;
  468. }
  469. }
  470. /* Return the UART3_FLAG status*/
  471. return status;
  472. }
  473. /**
  474. * @brief Clears the UART3 flags.
  475. * @param UART3_FLAG specifies the flag to clear
  476. * This parameter can be any combination of the following values:
  477. * - UART3_FLAG_LBDF: LIN Break detection flag.
  478. * - UART3_FLAG_LHDF: LIN Header detection flag.
  479. * - UART3_FLAG_LSF: LIN synchrone field flag.
  480. * - UART3_FLAG_RXNE: Receive data register not empty flag.
  481. * @note
  482. * - PE (Parity error), FE (Framing error), NF (Noise error),
  483. * OR (OverRun error) and IDLE (Idle line detected) flags are cleared
  484. * by software sequence: a read operation to UART3_SR register
  485. * (UART3_GetFlagStatus())followed by a read operation to UART3_DR
  486. * register(UART3_ReceiveData8() or UART3_ReceiveData9()).
  487. *
  488. * - RXNE flag can be also cleared by a read to the UART3_DR register
  489. * (UART3_ReceiveData8()or UART3_ReceiveData9()).
  490. *
  491. * - TC flag can be also cleared by software sequence: a read operation
  492. * to UART3_SR register (UART3_GetFlagStatus()) followed by a write
  493. * operation to UART3_DR register (UART3_SendData8() or UART3_SendData9()).
  494. *
  495. * - TXE flag is cleared only by a write to the UART3_DR register
  496. * (UART3_SendData8() or UART3_SendData9()).
  497. *
  498. * - SBK flag is cleared during the stop bit of break.
  499. * @retval None
  500. */
  501. void UART3_ClearFlag(UART3_Flag_TypeDef UART3_FLAG)
  502. {
  503. /* Check the parameters */
  504. assert_param(IS_UART3_CLEAR_FLAG_OK(UART3_FLAG));
  505. /*Clear the Receive Register Not Empty flag */
  506. if (UART3_FLAG == UART3_FLAG_RXNE)
  507. {
  508. UART3->SR = (uint8_t)~(UART3_SR_RXNE);
  509. }
  510. /*Clear the LIN Break Detection flag */
  511. else if (UART3_FLAG == UART3_FLAG_LBDF)
  512. {
  513. UART3->CR4 &= (uint8_t)(~UART3_CR4_LBDF);
  514. }
  515. /*Clear the LIN Header Detection Flag */
  516. else if (UART3_FLAG == UART3_FLAG_LHDF)
  517. {
  518. UART3->CR6 &= (uint8_t)(~UART3_CR6_LHDF);
  519. }
  520. /*Clear the LIN Synch Field flag */
  521. else
  522. {
  523. UART3->CR6 &= (uint8_t)(~UART3_CR6_LSF);
  524. }
  525. }
  526. /**
  527. * @brief Checks whether the specified UART3 interrupt has occurred or not.
  528. * @param UART3_IT: Specifies the UART3 interrupt pending bit to check.
  529. * This parameter can be one of the following values:
  530. * - UART3_IT_LBDF: LIN Break detection interrupt
  531. * - UART3_IT_TXE: Transmit Data Register empty interrupt
  532. * - UART3_IT_TC: Transmission complete interrupt
  533. * - UART3_IT_RXNE: Receive Data register not empty interrupt
  534. * - UART3_IT_IDLE: Idle line detection interrupt
  535. * - UART3_IT_OR: OverRun Error interrupt
  536. * - UART3_IT_PE: Parity Error interrupt
  537. * @retval The state of UART3_IT (SET or RESET).
  538. */
  539. ITStatus UART3_GetITStatus(UART3_IT_TypeDef UART3_IT)
  540. {
  541. ITStatus pendingbitstatus = RESET;
  542. uint8_t itpos = 0;
  543. uint8_t itmask1 = 0;
  544. uint8_t itmask2 = 0;
  545. uint8_t enablestatus = 0;
  546. /* Check parameters */
  547. assert_param(IS_UART3_GET_IT_OK(UART3_IT));
  548. /* Get the UART3 IT index*/
  549. itpos = (uint8_t)((uint8_t)1 << (uint8_t)((uint8_t)UART3_IT & (uint8_t)0x0F));
  550. /* Get the UART3 IT index*/
  551. itmask1 = (uint8_t)((uint8_t)UART3_IT >> (uint8_t)4);
  552. /* Set the IT mask*/
  553. itmask2 = (uint8_t)((uint8_t)1 << itmask1);
  554. /* Check the status of the specified UART3 pending bit*/
  555. if (UART3_IT == UART3_IT_PE)
  556. {
  557. /* Get the UART3_ITPENDINGBIT enable bit status*/
  558. enablestatus = (uint8_t)((uint8_t)UART3->CR1 & itmask2);
  559. /* Check the status of the specified UART3 interrupt*/
  560. if (((UART3->SR & itpos) != (uint8_t)0x00) && enablestatus)
  561. {
  562. /* Interrupt occurred*/
  563. pendingbitstatus = SET;
  564. }
  565. else
  566. {
  567. /* Interrupt not occurred*/
  568. pendingbitstatus = RESET;
  569. }
  570. }
  571. else if (UART3_IT == UART3_IT_LBDF)
  572. {
  573. /* Get the UART3_IT enable bit status*/
  574. enablestatus = (uint8_t)((uint8_t)UART3->CR4 & itmask2);
  575. /* Check the status of the specified UART3 interrupt*/
  576. if (((UART3->CR4 & itpos) != (uint8_t)0x00) && enablestatus)
  577. {
  578. /* Interrupt occurred*/
  579. pendingbitstatus = SET;
  580. }
  581. else
  582. {
  583. /* Interrupt not occurred*/
  584. pendingbitstatus = RESET;
  585. }
  586. }
  587. else if (UART3_IT == UART3_IT_LHDF)
  588. {
  589. /* Get the UART3_IT enable bit status*/
  590. enablestatus = (uint8_t)((uint8_t)UART3->CR6 & itmask2);
  591. /* Check the status of the specified UART3 interrupt*/
  592. if (((UART3->CR6 & itpos) != (uint8_t)0x00) && enablestatus)
  593. {
  594. /* Interrupt occurred*/
  595. pendingbitstatus = SET;
  596. }
  597. else
  598. {
  599. /* Interrupt not occurred*/
  600. pendingbitstatus = RESET;
  601. }
  602. }
  603. else
  604. {
  605. /* Get the UART3_IT enable bit status*/
  606. enablestatus = (uint8_t)((uint8_t)UART3->CR2 & itmask2);
  607. /* Check the status of the specified UART3 interrupt*/
  608. if (((UART3->SR & itpos) != (uint8_t)0x00) && enablestatus)
  609. {
  610. /* Interrupt occurred*/
  611. pendingbitstatus = SET;
  612. }
  613. else
  614. {
  615. /* Interrupt not occurred*/
  616. pendingbitstatus = RESET;
  617. }
  618. }
  619. /* Return the UART3_IT status*/
  620. return pendingbitstatus;
  621. }
  622. /**
  623. * @brief Clears the UART3 pending flags.
  624. * @param UART3_IT specifies the pending bit to clear
  625. * This parameter can be one of the following values:
  626. * - UART3_IT_LBDF: LIN Break detection interrupt
  627. * - UART3_IT_LHDF: LIN Header detection interrupt
  628. * - UART3_IT_RXNE: Receive Data register not empty interrupt.
  629. *
  630. * @note
  631. * - PE (Parity error), FE (Framing error), NF (Noise error),
  632. * OR (OverRun error) and IDLE (Idle line detected) pending bits are
  633. * cleared by software sequence: a read operation to UART3_SR register
  634. * (UART3_GetITStatus()) followed by a read operation to UART3_DR register
  635. * (UART3_ReceiveData8() or UART3_ReceiveData9()).
  636. *
  637. * - RXNE pending bit can be also cleared by a read to the UART3_DR register
  638. * (UART3_ReceiveData8() or UART3_ReceiveData9() ).
  639. *
  640. * - TC (Transmit complete) pending bit can be cleared by software
  641. * sequence: a read operation to UART3_SR register (UART3_GetITStatus())
  642. * followed by a write operation to UART3_DR register
  643. * (UART3_SendData8()or UART3_SendData9()).
  644. *
  645. * - TXE pending bit is cleared only by a write to the UART3_DR register
  646. * (UART3_SendData8() or UART3_SendData9()).
  647. * @retval None
  648. */
  649. void UART3_ClearITPendingBit(UART3_IT_TypeDef UART3_IT)
  650. {
  651. /* Check the parameters */
  652. assert_param(IS_UART3_CLEAR_IT_OK(UART3_IT));
  653. /*Clear the Receive Register Not Empty pending bit */
  654. if (UART3_IT == UART3_IT_RXNE)
  655. {
  656. UART3->SR = (uint8_t)~(UART3_SR_RXNE);
  657. }
  658. /*Clear the LIN Break Detection pending bit */
  659. else if (UART3_IT == UART3_IT_LBDF)
  660. {
  661. UART3->CR4 &= (uint8_t)~(UART3_CR4_LBDF);
  662. }
  663. /*Clear the LIN Header Detection pending bit */
  664. else
  665. {
  666. UART3->CR6 &= (uint8_t)(~UART3_CR6_LHDF);
  667. }
  668. }
  669. /**
  670. * @}
  671. */
  672. /**
  673. * @}
  674. */
  675. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/