gd32f10x_usart.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784
  1. /**
  2. ******************************************************************************
  3. * @brief USART functions of the firmware library.
  4. ******************************************************************************
  5. */
  6. /* Includes ------------------------------------------------------------------*/
  7. #include "gd32f10x_usart.h"
  8. #include "gd32f10x_rcc.h"
  9. /** @addtogroup GD32F10x_Firmware
  10. * @{
  11. */
  12. /** @defgroup USART
  13. * @brief USART driver modules
  14. * @{
  15. */
  16. /** @defgroup USART_Private_Defines
  17. * @{
  18. */
  19. /* USART Interrupts mask */
  20. #define INT_MASK ((uint16_t)(0x001F))
  21. /* USART CTLR1 initializes bit mask */
  22. #define CTLR1_CLEAR_MASK ((uint16_t)(0x160C))
  23. /* USART CTLR2 CLOCK initializes bit mask */
  24. #define CTLR2_CLOCK_CLEAR_MASK ((uint16_t)(0x0F00))
  25. /* USART CTLR3 initializes bit mask */
  26. #define CTLR3_CLEAR_MASK ((uint16_t)(0x0300))
  27. /**
  28. * @}
  29. */
  30. /** @defgroup USART_Private_Functions
  31. * @{
  32. */
  33. /**
  34. * @brief Reset the USART peripheral.
  35. * @param USARTx: the USART peripheral.
  36. * This parameter can be one of the following values
  37. * @arg USART1
  38. * @arg USART2
  39. * @arg USART3
  40. * @arg UART4
  41. * @arg UART5
  42. * @param NewValue: new value of the USARTx peripheral.
  43. * This parameter can be: ENABLE or DISABLE.
  44. * @retval None
  45. */
  46. void USART_DeInit(USART_TypeDef *USARTx)
  47. {
  48. if (USARTx == USART0) {
  49. RCC_APB2PeriphReset_Enable(RCC_APB2PERIPH_USART0RST, ENABLE);
  50. RCC_APB2PeriphReset_Enable(RCC_APB2PERIPH_USART0RST, DISABLE);
  51. } else if (USARTx == USART1) {
  52. RCC_APB1PeriphReset_Enable(RCC_APB1PERIPH_USART1RST, ENABLE);
  53. RCC_APB1PeriphReset_Enable(RCC_APB1PERIPH_USART1RST, DISABLE);
  54. } else if (USARTx == USART2) {
  55. RCC_APB1PeriphReset_Enable(RCC_APB1PERIPH_USART2RST, ENABLE);
  56. RCC_APB1PeriphReset_Enable(RCC_APB1PERIPH_USART2RST, DISABLE);
  57. } else if (USARTx == UART3) {
  58. RCC_APB1PeriphReset_Enable(RCC_APB1PERIPH_UART3RST, ENABLE);
  59. RCC_APB1PeriphReset_Enable(RCC_APB1PERIPH_UART3RST, DISABLE);
  60. } else {
  61. if (USARTx == UART4) {
  62. RCC_APB1PeriphReset_Enable(RCC_APB1PERIPH_UART4RST, ENABLE);
  63. RCC_APB1PeriphReset_Enable(RCC_APB1PERIPH_UART4RST, DISABLE);
  64. }
  65. }
  66. }
  67. /**
  68. * @brief Initialize the USART peripheral interface parameters.
  69. * @param USARTx: the USART peripheral.
  70. * This parameter can be one of the following values
  71. * @arg USART1
  72. * @arg USART2
  73. * @arg USART3
  74. * @arg UART4
  75. * @arg UART5
  76. * @param USART_InitParaStruct: the struct USART_InitPara pointer.
  77. * @retval None
  78. */
  79. void USART_Init(USART_TypeDef *USARTx, USART_InitPara *USART_InitParaStruct)
  80. {
  81. uint32_t divider = 0, apbclock = 0, temp = 0;
  82. RCC_ClocksPara RCC_ClocksState;
  83. USARTx->CTLR1 &= ~((uint32_t)USART_CTLR1_UEN);
  84. /* Initialize CTLR2 */
  85. temp = USARTx->CTLR2;
  86. /* Reset stop bits then set it use USART_STBits */
  87. temp &= ~((uint32_t)USART_CTLR2_STB);
  88. temp |= (uint32_t)USART_InitParaStruct->USART_STBits;
  89. USARTx->CTLR2 = temp;
  90. /*Initialize CTLR1*/
  91. temp = USARTx->CTLR1;
  92. /* Reset WL, PCEN, PM, TEN and REN bits */
  93. temp &= ~((uint32_t)CTLR1_CLEAR_MASK);
  94. /* According to USART_WL,USART_Parity,USART_RxorTx to configure the CTLR1 */
  95. temp |= (uint32_t)USART_InitParaStruct->USART_WL | USART_InitParaStruct->USART_Parity |
  96. USART_InitParaStruct->USART_RxorTx;
  97. USARTx->CTLR1 = temp;
  98. /*Initialize CTLR3*/
  99. temp = USARTx->CTLR3;
  100. /* Reset CTSEN and RTSEN bits */
  101. temp &= ~((uint32_t)CTLR3_CLEAR_MASK);
  102. /* According to USART_HardwareFlowControl to configure the CTLR3 */
  103. temp |= USART_InitParaStruct->USART_HardwareFlowControl;
  104. USARTx->CTLR3 = temp;
  105. /*Initialize USART BRR*/
  106. RCC_GetClocksFreq(&RCC_ClocksState);
  107. if (USARTx == USART0) {
  108. apbclock = RCC_ClocksState.APB2_Frequency;
  109. } else {
  110. apbclock = RCC_ClocksState.APB1_Frequency;
  111. }
  112. /* Get integer of baud-rate divide and raction of baud-rate divider */
  113. divider = (uint32_t)((apbclock) / ((USART_InitParaStruct->USART_BRR)));
  114. temp = (uint32_t)((apbclock) % ((USART_InitParaStruct->USART_BRR)));
  115. /* Round the divider : if fractional part i greater than 0.5 increment divider */
  116. if (temp >= (USART_InitParaStruct->USART_BRR) / 2) {
  117. divider++;
  118. }
  119. USARTx->BRR = (uint16_t)divider;
  120. }
  121. /**
  122. * @brief Initial the struct USART_InitPara
  123. * @param USART_InitParaStruct: the struct USART_InitPara pointer
  124. * @retval None
  125. */
  126. void USART_ParaInit(USART_InitPara *USART_InitParaStruct)
  127. {
  128. /* USART_InitStruct members default value */
  129. USART_InitParaStruct->USART_BRR = 9600;
  130. USART_InitParaStruct->USART_WL = USART_WL_8B;
  131. USART_InitParaStruct->USART_STBits = USART_STBITS_1;
  132. USART_InitParaStruct->USART_Parity = USART_PARITY_RESET;
  133. USART_InitParaStruct->USART_RxorTx = USART_RXORTX_RX | USART_RXORTX_TX;
  134. USART_InitParaStruct->USART_HardwareFlowControl = USART_HARDWAREFLOWCONTROL_NONE;
  135. }
  136. /**
  137. * @brief Initialize the USARTx peripheral Clock according to the
  138. * specified parameters in the USART_ClockInitStruct.
  139. * @param USARTx: the USART peripheral.
  140. * This parameter can be one of the following values
  141. * @arg USART1
  142. * @arg USART2
  143. * @arg USART3
  144. * @param USART_ClockInitStruct: pointer to a USART_ClockInitTypeDef structure that contains
  145. * the configuration information for the specified USART peripheral.
  146. * @retval None
  147. */
  148. void USART_ClockInit(USART_TypeDef *USARTx, USART_ClockInitPara *USART_ClockInitStruct)
  149. {
  150. uint32_t temp = 0;
  151. temp = USARTx->CTLR2;
  152. /* Clear CKEN, CPL, CPH, LBCP and SSM bits */
  153. temp &= ~(CTLR2_CLOCK_CLEAR_MASK);
  154. /* Reset hen set it usethe USART Clock, CPL, CPH, LBCP */
  155. temp |= (uint32_t)(USART_ClockInitStruct->USART_CKEN | USART_ClockInitStruct->USART_CPL |
  156. USART_ClockInitStruct->USART_CPH | USART_ClockInitStruct->USART_LBCP);
  157. /* Write to USART CTLR2 */
  158. USARTx->CTLR2 = (uint16_t)temp;
  159. }
  160. /**
  161. * @brief Initial the struct USART_ClockInitPara.
  162. * @param USART_ClockInitParaStruct: the struct USART_ClockInitPara pointer
  163. * @retval None
  164. */
  165. void USART_ClockStructInit(USART_ClockInitPara *USART_ClockInitParaStruct)
  166. {
  167. /*Reset USART_ClockInitStruct members default value */
  168. USART_ClockInitParaStruct->USART_CKEN = USART_CKEN_RESET;
  169. USART_ClockInitParaStruct->USART_CPL = USART_CPL_LOW;
  170. USART_ClockInitParaStruct->USART_CPH = USART_CPH_1EDGE;
  171. USART_ClockInitParaStruct->USART_LBCP = USART_LBCP_DISABLE;
  172. }
  173. /**
  174. * @brief Enable or disable the specified USART peripheral.
  175. * @param USARTx: the USART peripheral.
  176. * This parameter can be one of the following values
  177. * @arg USART1
  178. * @arg USART2
  179. * @arg USART3
  180. * @arg UART4
  181. * @arg UART5
  182. * @param NewValue: new value of the USARTx peripheral.
  183. * This parameter can be: ENABLE or DISABLE.
  184. * @retval None
  185. */
  186. void USART_Enable(USART_TypeDef *USARTx, TypeState NewValue)
  187. {
  188. /* Enable or disable the specified USART peripheral by setting the UEN bit in the CTLR1 register */
  189. if (NewValue != DISABLE) {
  190. USARTx->CTLR1 |= USART_CTLR1_UEN;
  191. } else {
  192. USARTx->CTLR1 &= ~((uint16_t)USART_CTLR1_UEN);
  193. }
  194. }
  195. /**
  196. * @brief Set the system clock prescaler.
  197. * @param USARTx: the USART peripheral.
  198. * This parameter can be one of the following values
  199. * @arg USART1
  200. * @arg USART2
  201. * @arg USART3
  202. * @arg UART4
  203. * @arg UART5
  204. * @param USART_Prescaler: specifies the prescaler clock.
  205. * @note This function has to be called before calling USART_Enable() function.
  206. * @retval None
  207. */
  208. void USART_SetPrescaler(USART_TypeDef *USARTx, uint8_t USART_Prescaler)
  209. {
  210. /* Clear and set the USART prescaler */
  211. USARTx->GTPR &= ~((uint16_t)USART_GTPR_PSC);
  212. USARTx->GTPR |= USART_Prescaler;
  213. }
  214. /**
  215. * @brief Send data by the USART peripheral.
  216. * @param USARTx: the USART peripheral.
  217. * This parameter can be one of the following values
  218. * @arg USART1
  219. * @arg USART2
  220. * @arg USART3
  221. * @arg UART4
  222. * @arg UART5
  223. * @param Data: the data will be sent.
  224. * @retval None
  225. */
  226. void USART_DataSend(USART_TypeDef *USARTx, uint16_t Data)
  227. {
  228. USARTx->DR = (Data & (uint16_t)0x01FF);
  229. }
  230. /**
  231. * @brief Receive data by the USART peripheral.
  232. * @param USARTx: the USART peripheral.
  233. * This parameter can be one of the following values
  234. * @arg USART1
  235. * @arg USART2
  236. * @arg USART3
  237. * @arg UART4
  238. * @arg UART5
  239. * @retval The received data.
  240. */
  241. uint16_t USART_DataReceive(USART_TypeDef *USARTx)
  242. {
  243. return (uint16_t)(USARTx->DR & (uint16_t)0x01FF);
  244. }
  245. /**
  246. * @brief Set the address of the USART terminal.
  247. * @param USARTx: the USART peripheral.
  248. * This parameter can be one of the following values
  249. * @arg USART1
  250. * @arg USART2
  251. * @arg USART3
  252. * @arg UART4
  253. * @arg UART5
  254. * @param USART_Address: Indicates the address of the USART node.
  255. * @retval None
  256. */
  257. void USART_Address(USART_TypeDef *USARTx, uint8_t USART_Address)
  258. {
  259. /* Clear the USART address and Set the USART terminal*/
  260. USARTx->CTLR2 &= ~USART_CTLR2_ADD;
  261. USARTx->CTLR2 |= USART_Address;
  262. }
  263. /**
  264. * @brief Enable or disable the USART's mute mode.
  265. * @param USARTx: the USART peripheral.
  266. * This parameter can be one of the following values
  267. * @arg USART1
  268. * @arg USART2
  269. * @arg USART3
  270. * @arg UART4
  271. * @arg UART5
  272. * @param NewValue: the USART mute mode.
  273. * This parameter can be: ENABLE or DISABLE.
  274. * @retval None
  275. */
  276. void USART_MuteMode_Enable(USART_TypeDef *USARTx, TypeState NewValue)
  277. {
  278. /* By setting the MEN bit in the CTLR1 register enable or disable the USART's mute mode*/
  279. if (NewValue != DISABLE) {
  280. USARTx->CTLR1 |= USART_CTLR1_RWU;
  281. } else {
  282. USARTx->CTLR1 &= ~USART_CTLR1_RWU;
  283. }
  284. }
  285. /**
  286. * @brief Set the USART WakeUp method from mute mode.
  287. * @param USARTx: the USART peripheral.
  288. * This parameter can be one of the following values
  289. * @arg USART1
  290. * @arg USART2
  291. * @arg USART3
  292. * @arg UART4
  293. * @arg UART5
  294. * @param USART_WakeUp
  295. * This parameter can be one of the following values
  296. * @arg USART_WAKEUP_IDLELINE
  297. * @arg USART_WAKEUP_ADDRESSMARK
  298. * @retval None
  299. */
  300. void USART_MuteModeWakeUp_Set(USART_TypeDef *USARTx, uint16_t USART_WakeUp)
  301. {
  302. USARTx->CTLR1 &= ~((uint16_t)USART_CTLR1_WM);
  303. USARTx->CTLR1 |= USART_WakeUp;
  304. }
  305. /**
  306. * @brief Set the USART LIN Break detection length.
  307. * @param USARTx: the USART peripheral.
  308. * This parameter can be one of the following values
  309. * @arg USART1
  310. * @arg USART2
  311. * @arg USART3
  312. * @arg UART4
  313. * @arg UART5
  314. * @param USART_LINBreakDetectLength
  315. * This parameter can be one of the following values
  316. * @arg USART_LINBREAKDETECTLENGTH_10B
  317. * @arg USART_LINBREAKDETECTLENGTH_11B
  318. * @retval None
  319. */
  320. void USART_SetLINBDLength(USART_TypeDef *USARTx, uint16_t USART_LINBreakDetectLength)
  321. {
  322. USARTx->CTLR2 &= ~((uint16_t)USART_CTLR2_LBDL);
  323. USARTx->CTLR2 |= USART_LINBreakDetectLength;
  324. }
  325. /**
  326. * @brief Enable or disable the USART's LIN mode.
  327. * @param USARTx: the USART peripheral.
  328. * This parameter can be one of the following values
  329. * @arg USART1
  330. * @arg USART2
  331. * @arg USART3
  332. * @arg UART4
  333. * @arg UART5
  334. * @param NewValue: the USART LIN mode value.
  335. * This parameter can be: ENABLE or DISABLE.
  336. * @retval None
  337. */
  338. void USART_LIN_Enable(USART_TypeDef *USARTx, TypeState NewValue)
  339. {
  340. /* By setting the LINEN bit in the CTLR2 register enable or disable the USART's LIN mode */
  341. if (NewValue != DISABLE) {
  342. USARTx->CTLR2 |= USART_CTLR2_LMEN;
  343. } else {
  344. USARTx->CTLR2 &= ~((uint16_t)USART_CTLR2_LMEN);
  345. }
  346. }
  347. /**
  348. * @brief Enable or disable the Half-duplex mode.
  349. * @param USARTx: the USART peripheral.
  350. * This parameter can be one of the following values
  351. * @arg USART1
  352. * @arg USART2
  353. * @arg USART3
  354. * @arg UART4
  355. * @arg UART5
  356. * @param NewValue: the USART Half-duplex mode value.
  357. * This parameter can be: ENABLE or DISABLE.
  358. * @retval None
  359. */
  360. void USART_HalfDuplex_Enable(USART_TypeDef *USARTx, TypeState NewValue)
  361. {
  362. /* By setting the HDEN bit in the CTLR3 register enable or disable the Half-duplex mode */
  363. if (NewValue != DISABLE) {
  364. USARTx->CTLR3 |= USART_CTLR3_HDEN;
  365. } else {
  366. USARTx->CTLR3 &= ~USART_CTLR3_HDEN;
  367. }
  368. }
  369. /**
  370. * @brief Set the the USART guard time.
  371. * @param USARTx: the USART peripheral.
  372. * This parameter can be one of the following values
  373. * @arg USART1
  374. * @arg USART2
  375. * @arg USART3
  376. * @param USART_GuardTime: the USART guard time.
  377. * @retval None
  378. */
  379. void USART_GuardTime_Set(USART_TypeDef *USARTx, uint8_t USART_GuardTime)
  380. {
  381. /* Set the USART guard time */
  382. USARTx->GTPR &= ~((uint16_t)USART_GTPR_GT);
  383. USARTx->GTPR |= (uint16_t)((uint16_t)USART_GuardTime << 0x08);
  384. }
  385. /**
  386. * @brief Enable or disable the USART's Smart Card mode.
  387. * @param USARTx: the USART peripheral.
  388. * This parameter can be one of the following values
  389. * @arg USART1
  390. * @arg USART2
  391. * @arg USART3
  392. * @param NewValue: the Smart Card mode value.
  393. * This parameter can be: ENABLE or DISABLE.
  394. * @retval None
  395. */
  396. void USART_SmartCard_Enable(USART_TypeDef *USARTx, TypeState NewValue)
  397. {
  398. /* By setting the SCEN bit in the CTLR3 register enable or disable the USART's Smart Card mode */
  399. if (NewValue != DISABLE) {
  400. USARTx->CTLR3 |= USART_CTLR3_SCEN;
  401. } else {
  402. USARTx->CTLR3 &= ~((uint16_t)USART_CTLR3_SCEN);
  403. }
  404. }
  405. /**
  406. * @brief Enable or disable NACK transmission.
  407. * @param USARTx: the USART peripheral.
  408. * This parameter can be one of the following values
  409. * @arg USART1
  410. * @arg USART2
  411. * @arg USART3
  412. * @arg UART4
  413. * @arg UART5
  414. * @param NewValue: the NACK transmission state.
  415. * This parameter can be: ENABLE or DISABLE.
  416. * @retval None
  417. */
  418. void USART_SmartCardNACK_Enable(USART_TypeDef *USARTx, TypeState NewValue)
  419. {
  420. /* By setting the NACK bit in the CTLR3 register Enable or disable NACK transmission */
  421. if (NewValue != DISABLE) {
  422. USARTx->CTLR3 |= USART_CTLR3_NACK;
  423. } else {
  424. USARTx->CTLR3 &= ~((uint32_t)USART_CTLR3_NACK);
  425. }
  426. }
  427. /**
  428. * @brief DMA enable for transmission or reception.
  429. * @param USARTx: the USART peripheral.
  430. * This parameter can be one of the following values
  431. * @arg USART1
  432. * @arg USART2
  433. * @arg USART3
  434. * @arg UART4
  435. * @arg UART5
  436. * @param USART_DMAEnable: the DMA request.
  437. * This parameter can be one of the following values
  438. * @arg USART_DMAREQ_RX
  439. * @arg USART_DMAREQ_TX
  440. * @param NewValue: the DMA Request sources state.
  441. * This parameter can be: ENABLE or DISABLE.
  442. * @retval None
  443. */
  444. void USART_DMA_Enable(USART_TypeDef *USARTx, uint16_t USART_DMAEnable, TypeState NewValue)
  445. {
  446. /* Enable or disable the DMA transfer for transmission or reception by setting the DENT and/or
  447. DENR bits in the USART CTLR3 register */
  448. if (NewValue != DISABLE) {
  449. USARTx->CTLR3 |= USART_DMAEnable;
  450. } else {
  451. USARTx->CTLR3 &= ~USART_DMAEnable;
  452. }
  453. }
  454. /**
  455. * @brief Set the USARTX IrDA low-power.
  456. * @param USARTx: the USART peripheral.
  457. * This parameter can be one of the following values
  458. * @arg USART1
  459. * @arg USART2
  460. * @arg USART3
  461. * @arg UART4
  462. * @arg UART5
  463. * @param USART_IrDAMode
  464. * This parameter can be one of the following values
  465. * @arg USART_IRDAMODE_LOWPOWER
  466. * @arg USART_IRDAMODE_NORMAL.
  467. * @retval None
  468. */
  469. void USART_IrDA_Set(USART_TypeDef *USARTx, uint16_t USART_IrDAMode)
  470. {
  471. USARTx->CTLR3 &= ~((uint16_t)USART_CTLR3_IRLP);
  472. USARTx->CTLR3 |= USART_IrDAMode;
  473. }
  474. /**
  475. * @brief Enable or disable the USART's IrDA interface.
  476. * @param USARTx: the USART peripheral.
  477. * This parameter can be one of the following values
  478. * @arg USART1
  479. * @arg USART2
  480. * @arg USART3
  481. * @arg UART4
  482. * @arg UART5
  483. * @param NewValue: the IrDA mode value.
  484. * This parameter can be: ENABLE or DISABLE.
  485. * @retval None
  486. */
  487. void USART_IrDA_Enable(USART_TypeDef *USARTx, TypeState NewValue)
  488. {
  489. /* By setting the IREN bit in the CTLR3 register enable or disable the USART's IrDA interface */
  490. if (NewValue != DISABLE) {
  491. USARTx->CTLR3 |= USART_CTLR3_IREN;
  492. } else {
  493. USARTx->CTLR3 &= ~((uint16_t)USART_CTLR3_IREN);
  494. }
  495. }
  496. /**
  497. * @brief Detect the bit flag of STR register.
  498. * @param USARTx: the USART peripheral.
  499. * This parameter can be one of the following values
  500. * @arg USART1
  501. * @arg USART2
  502. * @arg USART3
  503. * @arg UART4
  504. * @arg UART5
  505. * @param USART_FLAG: the flag of STR register.
  506. * This parameter can be one of the following values:
  507. * @arg USART_FLAG_CTSF: CTS Change flag.(not available for UART4 and UART5)
  508. * @arg USART_FLAG_LBDF: LIN Break detection flag
  509. * @arg USART_FLAG_TBE: Transmit data register empty flag.
  510. * @arg USART_FLAG_TC: Transmission Complete flag.
  511. * @arg USART_FLAG_RBNE: Receive data register not empty flag.
  512. * @arg USART_FLAG_IDLEF:Idle Line detection flag.
  513. * @arg USART_FLAG_ORE: OverRun Error flag.
  514. * @arg USART_FLAG_NE: Noise Error flag.
  515. * @arg USART_FLAG_FE: Framing Error flag.
  516. * @arg USART_FLAG_PE: Parity Error flag.
  517. * @retval The new state of USART_FLAG (SET or RESET).
  518. */
  519. TypeState USART_GetBitState(USART_TypeDef *USARTx, uint16_t USART_FLAG)
  520. {
  521. if ((USARTx->STR & USART_FLAG) != (uint16_t)RESET) {
  522. return SET;
  523. } else {
  524. return RESET;
  525. }
  526. }
  527. /**
  528. * @brief Enable or disable USART interrupt.
  529. * @param USARTx: the USART peripheral.
  530. * This parameter can be one of the following values
  531. * @arg USART1
  532. * @arg USART2
  533. * @arg USART3
  534. * @arg UART4
  535. * @arg UART5
  536. * @param USART_INT: the USART interrupt
  537. * This parameter can be one of the following values:
  538. * @arg USART_INT_PE
  539. * @arg USART_INT_TBE
  540. * @arg USART_INT_TC
  541. * @arg USART_INT_RBNE
  542. * @arg USART_INT_IDLEF
  543. * @arg USART_INT_LBDF
  544. * @arg USART_INT_CTSF (not available for UART4 and UART5)
  545. * @arg USART_INT_ERIE
  546. * @param NewValue: the USART interrupt State.
  547. * This parameter can be: ENABLE or DISABLE.
  548. * @retval None
  549. */
  550. void USART_INT_Set(USART_TypeDef *USARTx, uint16_t USART_INT, TypeState NewValue)
  551. {
  552. uint32_t intpos = 0, usartreg = 0;
  553. uint32_t usartxbase = 0;
  554. usartxbase = (uint32_t)USARTx;
  555. /* Get the USART register index and the interrupt position */
  556. usartreg = ((uint8_t)(USART_INT) >> 0x05);
  557. intpos = USART_INT & INT_MASK;
  558. /* Get the interrupt from which register: CTLR1,CTLR2 OR CTLR3 */
  559. if (usartreg == 0x01) {
  560. usartxbase += 0x0C;
  561. } else if (usartreg == 0x02) {
  562. usartxbase += 0x10;
  563. } else {
  564. usartxbase += 0x14;
  565. }
  566. if (NewValue != DISABLE) {
  567. *(__IO uint32_t *)usartxbase |= (((uint32_t)0x01) << intpos);
  568. } else {
  569. *(__IO uint32_t *)usartxbase &= ~(((uint32_t)0x01) << intpos);
  570. }
  571. }
  572. /**
  573. * @brief Clear the bit flag.
  574. * @param USARTx: the USART peripheral.
  575. * This parameter can be one of the following values
  576. * @arg USART1
  577. * @arg USART2
  578. * @arg USART3
  579. * @arg UART4
  580. * @arg UART5
  581. * @param USART_FLAG: the bit flag usart .
  582. * This parameter can be one of the following values:
  583. * @arg USART_FLAG_CTSF: CTS Change flag.(not available for UART4 and UART5)
  584. * @arg USART_FLAG_LBDF: LIN Break detection flag
  585. * @arg USART_FLAG_TC: Transmission Complete flag.
  586. * @arg USART_FLAG_RBNE: Receive data register not empty flag.
  587. * @note
  588. * - PE (Parity error), FE (Framing error), NE (Noise error), ORE (OverRun
  589. * error) and IDLEF (Idle line detected) flag bits are cleared by
  590. * software sequence: a read operation to USART_STR register
  591. * (USART_GetBitState()) followed by a read operation to USART_DR register
  592. * (USART_DataReceive()).
  593. * - RBNE flag bit can be also cleared by a read to the USART_DR register
  594. * (USART_DataReceive()).
  595. * - TC flag bit can be also cleared by software sequence: a read
  596. * operation to USART_STR register (USART_GetBitState()) followed by a write
  597. * operation to USART_DR register (USART_DataSend()).
  598. * - TBE flag bit is cleared only by a write to the USART_DR register
  599. * (USART_DataSend()).
  600. * @retval None
  601. */
  602. void USART_ClearBitState(USART_TypeDef *USARTx, uint16_t USART_FLAG)
  603. {
  604. USARTx->STR = (uint16_t)~ USART_FLAG;
  605. }
  606. /**
  607. * @brief Detect the interrupt bit flag.
  608. * @param USARTx: the USART peripheral.
  609. * This parameter can be one of the following values
  610. * @arg USART1
  611. * @arg USART2
  612. * @arg USART3
  613. * @arg UART4
  614. * @arg UART5
  615. * @param USART_INT: the USART interrupt bit flag.
  616. * This parameter can be one of the following values:
  617. * @arg USART_INT_PE
  618. * @arg USART_INT_TBE
  619. * @arg USART_INT_TC
  620. * @arg USART_INT_RBNE
  621. * @arg USART_INT_IDLEF
  622. * @arg USART_INT_LBDF
  623. * @arg USART_INT_CTSF: CTS flag interrupt (not available for UART4 and UART5)
  624. * @arg USART_INT_ORE
  625. * @arg USART_INT_NE
  626. * @arg USART_INT_FE
  627. * @retval The new state of USART_INT (SET or RESET).
  628. */
  629. TypeState USART_GetIntBitState(USART_TypeDef *USARTx, uint16_t USART_INT)
  630. {
  631. uint16_t bitpos = 0, itmask = 0, usartreg = 0;
  632. /* Get the USART register index and the interrupt position */
  633. usartreg = (((uint8_t)USART_INT) >> 0x05);
  634. itmask = (USART_INT) & (INT_MASK);
  635. itmask = (uint16_t)0x01 << itmask;
  636. if (usartreg == 0x01) {
  637. itmask &= USARTx->CTLR1;
  638. } else {
  639. if (usartreg == 0x02) {
  640. itmask &= USARTx->CTLR2;
  641. } else {
  642. itmask &= USARTx->CTLR3;
  643. }
  644. }
  645. bitpos = USART_INT >> 0x08;
  646. bitpos = (uint32_t)0x01 << bitpos;
  647. bitpos &= USARTx->STR;
  648. if ((itmask != (uint16_t)RESET) && (bitpos != (uint16_t)RESET)) {
  649. return SET;
  650. } else {
  651. return RESET;
  652. }
  653. }
  654. /**
  655. * @brief Clear the interrupt bit flag.
  656. * @param USARTx: the USART peripheral.
  657. * This parameter can be one of the following values
  658. * @arg USART1
  659. * @arg USART2
  660. * @arg USART3
  661. * @arg UART4
  662. * @arg UART5
  663. * @param USART_INT: the interrupt bit flag.
  664. * This parameter can be one of the following values: * @arg USART_INT_TC
  665. * @arg USART_INT_LBDF
  666. * @arg USART_INT_CTSF: CTS Flag interrupt (not available for UART4 and UART5)
  667. * @arg USART_INT_RBNE
  668. * @arg USART_INT_TC
  669. * @note
  670. * - PE (Parity error), FE (Framing error), NE (Noise error), ORE (OverRun
  671. * error) and IDLEF (Idle line detected flag) interrupt bits are cleared by
  672. * software sequence: a read operation to USART_STR register
  673. * (USART_GetINTState()) followed by a read operation to USART_DR register
  674. * (USART_DataReceive()).
  675. * - RBNE interrupt bit can be also cleared by a read to the USART_DR register
  676. * (USART_DataReceive()).
  677. * - TC interrupt bit can be also cleared by software sequence: a read
  678. * operation to USART_STR register (USART_GetINTState()) followed by a write
  679. * operation to USART_DR register (USART_DataSend()).
  680. * - TBE interrupt bit is cleared only by a write to the USART_DR register
  681. * (USART_DataSend()).
  682. * @retval None
  683. */
  684. void USART_ClearIntBitState(USART_TypeDef *USARTx, uint16_t USART_INT)
  685. {
  686. uint16_t bitpos = 0, itmask = 0;
  687. bitpos = USART_INT >> 0x08;
  688. itmask = ((uint16_t)0x01 << (uint16_t)bitpos);
  689. USARTx->STR = (uint16_t)~itmask;
  690. }
  691. /**
  692. * @brief Send break command.
  693. * @param USARTx: the USART peripheral
  694. * This parameter can be one of the following values
  695. * @arg USART1
  696. * @arg USART2
  697. * @arg USART3
  698. * @arg UART4
  699. * @arg UART5
  700. * @retval None
  701. */
  702. void USART_SendBreak(USART_TypeDef *USARTx)
  703. {
  704. /* Send break command */
  705. USARTx->CTLR1 |= USART_CTLR1_SBKCMD;
  706. }
  707. /**
  708. * @}
  709. */
  710. /**
  711. * @}
  712. */
  713. /**
  714. * @}
  715. */