apm32e10x_usart.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833
  1. /*!
  2. * @file apm32e10x_usart.c
  3. *
  4. * @brief This file provides all the USART firmware functions
  5. *
  6. * @version V1.0.2
  7. *
  8. * @date 2022-12-31
  9. *
  10. * @attention
  11. *
  12. * Copyright (C) 2021-2023 Geehy Semiconductor
  13. *
  14. * You may not use this file except in compliance with the
  15. * GEEHY COPYRIGHT NOTICE (GEEHY SOFTWARE PACKAGE LICENSE).
  16. *
  17. * The program is only for reference, which is distributed in the hope
  18. * that it will be useful and instructional for customers to develop
  19. * their software. Unless required by applicable law or agreed to in
  20. * writing, the program is distributed on an "AS IS" BASIS, WITHOUT
  21. * ANY WARRANTY OR CONDITIONS OF ANY KIND, either express or implied.
  22. * See the GEEHY SOFTWARE PACKAGE LICENSE for the governing permissions
  23. * and limitations under the License.
  24. */
  25. #include "apm32e10x_usart.h"
  26. #include "apm32e10x_rcm.h"
  27. /** @addtogroup APM32E10x_StdPeriphDriver
  28. @{
  29. */
  30. /** @addtogroup USART_Driver
  31. * @brief USART driver modules
  32. @{
  33. */
  34. /** @defgroup USART_Functions Functions
  35. @{
  36. */
  37. /*!
  38. * @brief Reset usart peripheral registers to their default reset values
  39. *
  40. * @param usart: Select the USART or the UART peripheral
  41. *
  42. * @retval None
  43. *
  44. * @note The usart can be USART1, USART2, USART3, UART4 and UART5
  45. */
  46. void USART_Reset(USART_T* usart)
  47. {
  48. if (USART1 == usart)
  49. {
  50. RCM_EnableAPB2PeriphReset(RCM_APB2_PERIPH_USART1);
  51. RCM_DisableAPB2PeriphReset(RCM_APB2_PERIPH_USART1);
  52. }
  53. else if (USART2 == usart)
  54. {
  55. RCM_EnableAPB1PeriphReset(RCM_APB1_PERIPH_USART2);
  56. RCM_DisableAPB1PeriphReset(RCM_APB1_PERIPH_USART2);
  57. }
  58. else if (USART3 == usart)
  59. {
  60. RCM_EnableAPB1PeriphReset(RCM_APB1_PERIPH_USART3);
  61. RCM_DisableAPB1PeriphReset(RCM_APB1_PERIPH_USART3);
  62. }
  63. else if (UART4 == usart)
  64. {
  65. RCM_EnableAPB1PeriphReset(RCM_APB1_PERIPH_UART4);
  66. RCM_DisableAPB1PeriphReset(RCM_APB1_PERIPH_UART4);
  67. }
  68. else if (UART5 == usart)
  69. {
  70. RCM_EnableAPB1PeriphReset(RCM_APB1_PERIPH_UART5);
  71. RCM_DisableAPB1PeriphReset(RCM_APB1_PERIPH_UART5);
  72. }
  73. }
  74. /*!
  75. * @brief Config the USART peripheral according to the specified parameters in the usartConfig
  76. *
  77. * @param uart: Select the USART or the UART peripheral
  78. *
  79. * @param usartConfig: pointer to a USART_Config_T structure
  80. *
  81. * @retval None
  82. *
  83. * @note The usart can be USART1, USART2, USART3, UART4 and UART5
  84. */
  85. void USART_Config(USART_T* uart, USART_Config_T* usartConfig)
  86. {
  87. uint32_t temp, fCLK, intDiv, fractionalDiv;
  88. temp = uart->CTRL1;
  89. temp &= 0xE9F3;
  90. temp |= (uint32_t)usartConfig->mode | \
  91. (uint32_t)usartConfig->parity | \
  92. (uint32_t)usartConfig->wordLength;
  93. uart->CTRL1 = temp;
  94. temp = uart->CTRL2;
  95. temp &= 0xCFFF;
  96. temp |= usartConfig->stopBits;
  97. uart->CTRL2 = temp;
  98. temp = uart->CTRL3;
  99. temp &= 0xFCFF;
  100. temp |= (uint32_t)usartConfig->hardwareFlow;
  101. uart->CTRL3 = temp;
  102. if (uart == USART1)
  103. {
  104. RCM_ReadPCLKFreq(NULL, &fCLK);
  105. }
  106. else
  107. {
  108. RCM_ReadPCLKFreq(&fCLK, NULL);
  109. }
  110. intDiv = ((25 * fCLK) / (4 * (usartConfig->baudRate)));
  111. temp = (intDiv / 100) << 4;
  112. fractionalDiv = intDiv - (100 * (temp >> 4));
  113. temp |= ((((fractionalDiv * 16) + 50) / 100)) & ((uint8_t)0x0F);
  114. uart->BR = temp;
  115. }
  116. /*!
  117. * @brief Fills each USART_InitStruct member with its default value
  118. *
  119. * @param usartConfig: pointer to a USART_Config_T structure which will be initialized
  120. *
  121. * @retval None
  122. */
  123. void USART_ConfigStructInit(USART_Config_T* usartConfig)
  124. {
  125. usartConfig->baudRate = 9600;
  126. usartConfig->wordLength = USART_WORD_LEN_8B;
  127. usartConfig->stopBits = USART_STOP_BIT_1;
  128. usartConfig->parity = USART_PARITY_NONE ;
  129. usartConfig->mode = USART_MODE_TX_RX;
  130. usartConfig->hardwareFlow = USART_HARDWARE_FLOW_NONE;
  131. }
  132. /*!
  133. * @brief Configuration communication clock
  134. *
  135. * @param usart: Select the USART or the UART peripheral
  136. *
  137. * @param clockConfig: Pointer to a USART_clockConfig_T structure
  138. *
  139. * @retval None
  140. *
  141. * @note The usart can be USART1, USART2, USART3
  142. */
  143. void USART_ConfigClock(USART_T* usart, USART_ClockConfig_T* clockConfig)
  144. {
  145. usart->CTRL2_B.CLKEN = clockConfig->clock;
  146. usart->CTRL2_B.CPHA = clockConfig->phase;
  147. usart->CTRL2_B.CPOL = clockConfig->polarity;
  148. usart->CTRL2_B.LBCPOEN = clockConfig->lastBit;
  149. }
  150. /*!
  151. * @brief Fills each clockConfig member with its default value
  152. *
  153. * @param clockConfig: Pointer to a USART_clockConfig_T structure
  154. *
  155. * @retval None
  156. *
  157. * @note
  158. */
  159. void USART_ConfigClockStructInit(USART_ClockConfig_T* clockConfig)
  160. {
  161. clockConfig->clock = USART_CLKEN_DISABLE;
  162. clockConfig->phase = USART_CLKPHA_1EDGE;
  163. clockConfig->polarity = USART_CLKPOL_LOW;
  164. clockConfig->lastBit = USART_LBCP_DISABLE;
  165. }
  166. /*!
  167. * @brief Enables the specified USART peripheral
  168. *
  169. * @param usart: Select the USART or the UART peripheral
  170. *
  171. * @retval None
  172. *
  173. * @note The usart can be USART1, USART2, USART3, UART4 and UART5
  174. */
  175. void USART_Enable(USART_T* usart)
  176. {
  177. usart->CTRL1_B.UEN = BIT_SET;
  178. }
  179. /*!
  180. * @brief Disable the specified USART peripheral
  181. *
  182. * @param usart: Select the USART or the UART peripheral
  183. *
  184. * @retval None
  185. *
  186. * @note The usart can be USART1, USART2, USART3, UART4 and UART5
  187. */
  188. void USART_Disable(USART_T* usart)
  189. {
  190. usart->CTRL1_B.UEN = BIT_RESET;
  191. }
  192. /*!
  193. * @brief Enables the USART DMA interface
  194. *
  195. * @param usart: Select the USART or the UART peripheral
  196. *
  197. * @param dmaReq: Specifies the DMA request
  198. * This parameter can be one of the following values:
  199. * @arg USART_DMA_TX: USART DMA receive request
  200. * @arg USART_DMA_RX: USART DMA transmit request
  201. * @arg USART_DMA_TX_RX: USART DMA transmit/receive request
  202. *
  203. * @retval None
  204. *
  205. * @note The usart can be USART1, USART2, USART3, UART4 and UART5
  206. */
  207. void USART_EnableDMA(USART_T* usart, USART_DMA_T dmaReq)
  208. {
  209. usart->CTRL3 |= dmaReq;
  210. }
  211. /*!
  212. * @brief Disable the USART DMA interface
  213. *
  214. * @param usart: Select the USART or the UART peripheral
  215. *
  216. * @param dmaReq: Specifies the DMA request
  217. * This parameter can be one of the following values:
  218. * @arg USART_DMA_TX: USART DMA receive request
  219. * @arg USART_DMA_RX: USART DMA transmit request
  220. * @arg USART_DMA_TX_RX: USART DMA transmit/receive request
  221. *
  222. * @retval None
  223. *
  224. * @note The usart can be USART1, USART2, USART3, UART4 and UART5
  225. */
  226. void USART_DisableDMA(USART_T* usart, USART_DMA_T dmaReq)
  227. {
  228. usart->CTRL3 &= (uint32_t)~dmaReq;
  229. }
  230. /*!
  231. * @brief Configures the address of the USART node
  232. *
  233. * @param usart: Select the USART or the UART peripheral
  234. *
  235. * @param address: Indicates the address of the USART node
  236. *
  237. * @retval None
  238. *
  239. * @note The usart can be USART1, USART2, USART3, UART4 and UART5
  240. */
  241. void USART_Address(USART_T* usart, uint8_t address)
  242. {
  243. usart->CTRL2_B.ADDR = address;
  244. }
  245. /*!
  246. * @brief Selects the USART WakeUp method.
  247. *
  248. * @param usart: Select the USART or the UART peripheral
  249. *
  250. * @param wakeup: Specifies the selected USART auto baud rate method
  251. * This parameter can be one of the following values:
  252. * @arg USART_WAKEUP_IDLE_LINE: WakeUp by an idle line detection
  253. * @arg USART_WAKEUP_ADDRESS_MARK: WakeUp by an address mark
  254. *
  255. * @retval None
  256. *
  257. * @note The usart can be USART1, USART2, USART3, UART4 and UART5
  258. */
  259. void USART_ConfigWakeUp(USART_T* usart, USART_WAKEUP_T wakeup)
  260. {
  261. usart->CTRL1_B.WUPMCFG = wakeup;
  262. }
  263. /*!
  264. * @brief Enable USART Receiver in mute mode
  265. *
  266. * @param usart: Select the USART or the UART peripheral
  267. *
  268. * @retval None
  269. *
  270. * @note The usart can be USART1, USART2, USART3, UART4 and UART5
  271. */
  272. void USART_EnableMuteMode(USART_T* usart)
  273. {
  274. usart->CTRL1_B.RXMUTEEN = BIT_SET;
  275. }
  276. /*!
  277. * @brief Disable USART Receiver in active mode
  278. *
  279. * @param usart: Select the USART or the UART peripheral
  280. *
  281. * @retval None
  282. *
  283. * @note The usart can be USART1, USART2, USART3, UART4 and UART5
  284. */
  285. void USART_DisableMuteMode(USART_T* usart)
  286. {
  287. usart->CTRL1_B.RXMUTEEN = BIT_RESET;
  288. }
  289. /*!
  290. * @brief Sets the USART LIN Break detection length
  291. *
  292. * @param usart: Select the USART or the UART peripheral
  293. *
  294. * @param length: Specifies the LIN break detection length
  295. * This parameter can be one of the following values:
  296. * @arg USART_LBDL_10B: 10-bit break detection
  297. * @arg USART_LBDL_10B: 11-bit break detection
  298. *
  299. * @retval None
  300. *
  301. * @note The usart can be USART1, USART2, USART3, UART4 and UART5
  302. */
  303. void USART_ConfigLINBreakDetectLength(USART_T* usart, USART_LBDL_T length)
  304. {
  305. usart->CTRL2_B.LBDLCFG = length;
  306. }
  307. /*!
  308. * @brief Enables the USART LIN MODE
  309. *
  310. * @param usart: Select the USART or the UART peripheral
  311. *
  312. * @retval None
  313. *
  314. * @note The usart can be USART1, USART2, USART3, UART4 and UART5
  315. */
  316. void USART_EnableLIN(USART_T* usart)
  317. {
  318. usart->CTRL2_B.LINMEN = BIT_SET;
  319. }
  320. /*!
  321. * @brief Disable the USART LIN MODE
  322. *
  323. * @param usart: Select the USART or the UART peripheral
  324. *
  325. * @retval None
  326. *
  327. * @note The usart can be USART1, USART2, USART3, UART4 and UART5
  328. */
  329. void USART_DisableLIN(USART_T* usart)
  330. {
  331. usart->CTRL2_B.LINMEN = BIT_RESET;
  332. }
  333. /*!
  334. * @brief Transmitter Enable
  335. *
  336. * @param usart: Select the USART or the UART peripheral
  337. *
  338. * @retval None
  339. *
  340. * @note The usart can be USART1, USART2, USART3, UART4 and UART5
  341. */
  342. void USART_EnableTx(USART_T* usart)
  343. {
  344. usart->CTRL1_B.TXEN = BIT_SET;
  345. }
  346. /*!
  347. * @brief Transmitter Disable
  348. *
  349. * @param usart: Select the USART or the UART peripheral
  350. *
  351. * @retval None
  352. *
  353. * @note The usart can be USART1, USART2, USART3, UART4 and UART5
  354. */
  355. void USART_DisableTx(USART_T* usart)
  356. {
  357. usart->CTRL1_B.TXEN = BIT_RESET;
  358. }
  359. /*!
  360. * @brief Receiver enable
  361. *
  362. * @param usart: Select the USART or the UART peripheral
  363. *
  364. * @retval None
  365. *
  366. * @note The usart can be USART1, USART2, USART3, UART4 and UART5
  367. */
  368. void USART_EnableRx(USART_T* usart)
  369. {
  370. usart->CTRL1_B.RXEN = BIT_SET;
  371. }
  372. /*!
  373. * @brief Receiver disable
  374. *
  375. * @param usart: Select the USART or the UART peripheral
  376. *
  377. * @retval None
  378. *
  379. * @note The usart can be USART1, USART2, USART3, UART4 and UART5
  380. */
  381. void USART_DisableRx(USART_T* usart)
  382. {
  383. usart->CTRL1_B.RXEN = BIT_RESET;
  384. }
  385. /*!
  386. * @brief Transmits single data
  387. *
  388. * @param usart: Select the USART or the UART peripheral
  389. *
  390. * @param data: the data to transmit
  391. *
  392. * @retval None
  393. *
  394. * @note The usart can be USART1, USART2, USART3, UART4 and UART5
  395. */
  396. void USART_TxData(USART_T* usart, uint16_t data)
  397. {
  398. usart->DATA_B.DATA = data;
  399. }
  400. /*!
  401. * @brief Returns the most recent received data
  402. *
  403. * @param usart: Select the USART or the UART peripheral
  404. *
  405. * @retval None
  406. *
  407. * @note The usart can be USART1, USART2, USART3, UART4 and UART5
  408. */
  409. uint16_t USART_RxData(USART_T* usart)
  410. {
  411. return (uint16_t)(usart->DATA_B.DATA);
  412. }
  413. /*!
  414. * @brief Transmits break characters
  415. *
  416. * @param usart: Select the USART or the UART peripheral
  417. *
  418. * @retval None
  419. *
  420. * @note The usart can be USART1, USART2, USART3, UART4 and UART5
  421. */
  422. void USART_TxBreak(USART_T* usart)
  423. {
  424. usart->CTRL1_B.TXBF = BIT_SET;
  425. }
  426. /*!
  427. * @brief Sets the specified USART guard time
  428. *
  429. * @param usart: Select the USART or the UART peripheral
  430. *
  431. * @param guardTime: Specifies the guard time
  432. *
  433. * @retval None
  434. *
  435. * @note The usart can be USART1, USART2, USART3
  436. */
  437. void USART_ConfigGuardTime(USART_T* usart, uint8_t guardTime)
  438. {
  439. usart->GTPSC_B.GRDT = guardTime;
  440. }
  441. /*!
  442. * @brief Sets the system clock divider number
  443. *
  444. * @param usart: Select the USART or the UART peripheral
  445. *
  446. * @param div: specifies the divider number
  447. *
  448. * @retval None
  449. *
  450. * @note The usart can be USART1, USART2, USART3
  451. */
  452. void USART_ConfigPrescaler(USART_T* usart, uint8_t div)
  453. {
  454. usart->GTPSC_B.PSC = div;
  455. }
  456. /*!
  457. * @brief Enables the USART Smart Card mode
  458. *
  459. * @param usart: Select the USART or the UART peripheral
  460. *
  461. * @retval None
  462. *
  463. * @note The Smart Card mode is not available for UART4 and UART5
  464. */
  465. void USART_EnableSmartCard(USART_T* usart)
  466. {
  467. usart->CTRL3_B.SCEN = BIT_SET;
  468. }
  469. /*!
  470. * @brief Disable the USART Smart Card mode
  471. *
  472. * @param usart: Select the USART or the UART peripheral
  473. *
  474. * @retval None
  475. *
  476. * @note The Smart Card mode is not available for UART4 and UART5
  477. */
  478. void USART_DisableSmartCard(USART_T* usart)
  479. {
  480. usart->CTRL3_B.SCEN = BIT_RESET;
  481. }
  482. /*!
  483. * @brief Enables NACK transmission
  484. *
  485. * @param usart: Select the USART or the UART peripheral
  486. *
  487. * @retval None
  488. *
  489. * @note The Smart Card mode is not available for UART4 and UART5
  490. */
  491. void USART_EnableSmartCardNACK(USART_T* usart)
  492. {
  493. usart->CTRL3_B.SCNACKEN = BIT_SET;
  494. }
  495. /*!
  496. * @brief Disable NACK transmission
  497. *
  498. * @param usart: Select the USART or the UART peripheral
  499. *
  500. * @retval None
  501. *
  502. * @note The Smart Card mode is not available for UART4 and UART5
  503. */
  504. void USART_DisableSmartCardNACK(USART_T* usart)
  505. {
  506. usart->CTRL3_B.SCNACKEN = BIT_RESET;
  507. }
  508. /*!
  509. * @brief Enables USART Half Duplex communication
  510. *
  511. * @param usart: Select the USART or the UART peripheral
  512. *
  513. * @retval None
  514. *
  515. * @note The usart can be USART1, USART2, USART3, UART4 and UART5
  516. */
  517. void USART_EnableHalfDuplex(USART_T* usart)
  518. {
  519. usart->CTRL3_B.HDEN = BIT_SET;
  520. }
  521. /*!
  522. * @brief Disable USART Half Duplex communication
  523. *
  524. * @param usart: Select the USART or the UART peripheral
  525. *
  526. * @retval None
  527. *
  528. * @note The usart can be USART1, USART2, USART3, UART4 and UART5
  529. */
  530. void USART_DisableHalfDuplex(USART_T* usart)
  531. {
  532. usart->CTRL3_B.HDEN = BIT_RESET;
  533. }
  534. /*!
  535. * @brief Configures the USART's IrDA interface
  536. *
  537. * @param usart: Select the USART or the UART peripheral
  538. *
  539. * @param IrDAMode: Specifies the IrDA mode
  540. * This parameter can be one of the following values:
  541. * @arg USART_IRDALP_NORMAL: Normal
  542. * @arg USART_IRDALP_LOWPOWER: Low-Power
  543. * @retval None
  544. *
  545. * @note The usart can be USART1, USART2, USART3, UART4 and UART5
  546. */
  547. void USART_ConfigIrDA(USART_T* usart, USART_IRDALP_T IrDAMode)
  548. {
  549. usart->CTRL3_B.IRLPEN = IrDAMode;
  550. }
  551. /*!
  552. * @brief Enables the USART's IrDA interface
  553. *
  554. * @param usart: Select the USART or the UART peripheral
  555. *
  556. * @retval None
  557. *
  558. * @note The usart can be USART1, USART2, USART3, UART4 and UART5
  559. */
  560. void USART_EnableIrDA(USART_T* usart)
  561. {
  562. usart->CTRL3_B.IREN = BIT_SET;
  563. }
  564. /*!
  565. * @brief Disable the USART's IrDA interface
  566. *
  567. * @param usart: Select the USART or the UART peripheral
  568. *
  569. * @retval None
  570. *
  571. * @note The usart can be USART1, USART2, USART3, UART4 and UART5
  572. */
  573. void USART_DisableIrDA(USART_T* usart)
  574. {
  575. usart->CTRL3_B.IREN = BIT_RESET;
  576. }
  577. /*!
  578. * @brief Enable the specified USART interrupts
  579. *
  580. * @param usart: Select the USART or the UART peripheral
  581. *
  582. * @param interrupt: Specifies the USART interrupts sources
  583. * The parameter can be one of following values:
  584. * @arg USART_INT_PE: Parity error interrupt
  585. * @arg USART_INT_TXBE: Tansmit data buffer empty interrupt
  586. * @arg USART_INT_TXC: Transmission complete interrupt
  587. * @arg USART_INT_RXBNE: Receive data buffer not empty interrupt
  588. * @arg USART_INT_IDLE: Idle line detection interrupt
  589. * @arg USART_INT_LBD: LIN break detection interrupt
  590. * @arg USART_INT_CTS: CTS change interrupt
  591. * @arg USART_INT_ERR: Error interrupt(Frame error, noise error, overrun error)
  592. *
  593. * @retval None
  594. *
  595. * @note The usart can be USART1, USART2, USART3, UART4 and UART5
  596. */
  597. void USART_EnableInterrupt(USART_T* usart, USART_INT_T interrupt)
  598. {
  599. uint32_t temp;
  600. temp = (uint32_t)(interrupt & 0xffff);
  601. if (interrupt & 0X10000)
  602. {
  603. usart->CTRL1 |= temp;
  604. }
  605. if (interrupt & 0X20000)
  606. {
  607. usart->CTRL2 |= temp;
  608. }
  609. if (interrupt & 0X40000)
  610. {
  611. usart->CTRL3 |= temp;
  612. }
  613. }
  614. /*!
  615. * @brief Disables the specified USART interrupts
  616. *
  617. * @param usart: Select the USART or the UART peripheral
  618. *
  619. * @param interrupt: Specifies the USART interrupts sources
  620. * The parameter can be one of following values:
  621. * @arg USART_INT_PE: Parity error interrupt
  622. * @arg USART_INT_TXBE: Tansmit data buffer empty interrupt
  623. * @arg USART_INT_TXC: Transmission complete interrupt
  624. * @arg USART_INT_RXBNE: Receive data buffer not empty interrupt
  625. * @arg USART_INT_IDLE: Idle line detection interrupt
  626. * @arg USART_INT_LBD: LIN break detection interrupt
  627. * @arg USART_INT_CTS: CTS change interrupt
  628. * @arg USART_INT_ERR: Error interrupt(Frame error, noise error, overrun error)
  629. *
  630. * @retval None
  631. *
  632. * @note The usart can be USART1, USART2, USART3, UART4 and UART5
  633. */
  634. void USART_DisableInterrupt(USART_T* usart, USART_INT_T interrupt)
  635. {
  636. uint32_t temp;
  637. temp = (uint32_t)~(interrupt & 0xffff);
  638. if (interrupt & 0X10000)
  639. {
  640. usart->CTRL1 &= temp;
  641. }
  642. if (interrupt & 0X20000)
  643. {
  644. usart->CTRL2 &= temp;
  645. }
  646. if (interrupt & 0X40000)
  647. {
  648. usart->CTRL3 &= temp;
  649. }
  650. }
  651. /*!
  652. * @brief Read the specified USART flag
  653. *
  654. * @param usart: Select the USART or the UART peripheral
  655. *
  656. * @param flag: Specifies the flag to check
  657. * The parameter can be one of following values:
  658. * @arg USART_FLAG_CTS: CTS Change flag (not available for UART4 and UART5)
  659. * @arg USART_FLAG_LBD: LIN Break detection flag
  660. * @arg USART_FLAG_TXBE: Transmit data buffer empty flag
  661. * @arg USART_FLAG_TXC: Transmission Complete flag
  662. * @arg USART_FLAG_RXBNE: Receive data buffer not empty flag
  663. * @arg USART_FLAG_IDLE: Idle Line detection flag
  664. * @arg USART_FLAG_OVRE: OverRun Error flag
  665. * @arg USART_FLAG_NE: Noise Error flag
  666. * @arg USART_FLAG_FE: Framing Error flag
  667. * @arg USART_FLAG_PE: Parity Error flag
  668. *
  669. * @retval The new state of flag (SET or RESET)
  670. *
  671. * @note The usart can be USART1, USART2, USART3, UART4 and UART5
  672. */
  673. uint8_t USART_ReadStatusFlag(USART_T* usart, USART_FLAG_T flag)
  674. {
  675. return (usart->STS & flag) ? SET : RESET;
  676. }
  677. /*!
  678. * @brief Clears the USARTx's pending flags
  679. *
  680. * @param usart: Select the USART or the UART peripheral
  681. *
  682. * @param flag: Specifies the flag to clear
  683. * The parameter can be one of following values:
  684. * @arg USART_FLAG_CTS: CTS Change flag (not available for UART4 and UART5)
  685. * @arg USART_FLAG_LBD: LIN Break detection flag
  686. * @arg USART_FLAG_TXC: Transmission Complete flag
  687. * @arg USART_FLAG_RXBNE: Receive data buffer not empty flag
  688. *
  689. * @retval None
  690. *
  691. * @note The usart can be USART1, USART2, USART3, UART4 and UART5
  692. */
  693. void USART_ClearStatusFlag(USART_T* usart, USART_FLAG_T flag)
  694. {
  695. usart->STS &= (uint32_t)~flag;
  696. }
  697. /*!
  698. * @brief Read the specified USART interrupt flag
  699. *
  700. * @param usart: Select the USART or the UART peripheral
  701. *
  702. * @param flag: Specifies the USART interrupt source to check
  703. * The parameter can be one of following values:
  704. * @arg USART_INT_TXBE: Tansmit data buffer empty interrupt
  705. * @arg USART_INT_TXC: Transmission complete interrupt
  706. * @arg USART_INT_RXBNE: Receive data buffer not empty interrupt
  707. * @arg USART_INT_IDLE: Idle line detection interrupt
  708. * @arg USART_INT_LBD: LIN break detection interrupt
  709. * @arg USART_INT_CTS: CTS change interrupt
  710. * @arg USART_INT_OVRE: OverRun Error interruptpt
  711. * @arg USART_INT_NE: Noise Error interrupt
  712. * @arg USART_INT_FE: Framing Error interrupt
  713. * @arg USART_INT_PE: Parity error interrupt
  714. *
  715. * @retval The new state of flag (SET or RESET)
  716. *
  717. * @note The usart can be USART1, USART2, USART3, UART4 and UART5
  718. */
  719. uint8_t USART_ReadIntFlag(USART_T* usart, USART_INT_T flag)
  720. {
  721. uint32_t itFlag, srFlag;
  722. if (flag & 0x10000)
  723. {
  724. itFlag = usart->CTRL1 & flag & 0xffff;
  725. }
  726. else if (flag & 0x20000)
  727. {
  728. itFlag = usart->CTRL2 & flag & 0xffff;
  729. }
  730. else
  731. {
  732. itFlag = usart->CTRL3 & flag & 0xffff;
  733. }
  734. srFlag = flag >> 24;
  735. srFlag = (uint32_t)(1 << srFlag);
  736. srFlag = usart->STS & srFlag;
  737. if (srFlag && itFlag)
  738. {
  739. return SET;
  740. }
  741. return RESET;
  742. }
  743. /*!
  744. * @brief Clears the USART interrupt pending bits
  745. *
  746. * @param usart: Select the USART or the UART peripheral
  747. *
  748. * @param flag: Specifies the interrupt pending bit to clear
  749. * The parameter can be one of following values:
  750. * @arg USART_INT_RXBNE: Receive data buffer not empty interrupt
  751. * @arg USART_INT_TXC: Transmission complete interrupt
  752. * @arg USART_INT_LBD: LIN break detection interrupt
  753. * @arg USART_INT_CTS: CTS change interrupt
  754. *
  755. * @retval None
  756. *
  757. * @note The usart can be USART1, USART2, USART3, UART4 and UART5
  758. */
  759. void USART_ClearIntFlag(USART_T* usart, USART_INT_T flag)
  760. {
  761. uint32_t srFlag;
  762. srFlag = flag >> 24;
  763. srFlag = (uint32_t)(1 << srFlag);
  764. usart->STS &= (uint32_t)~srFlag;
  765. }
  766. /**@} end of group USART_Functions */
  767. /**@} end of group USART_Driver */
  768. /**@} end of group APM32E10x_StdPeriphDriver */