n32g45x_usart.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974
  1. /*****************************************************************************
  2. * Copyright (c) 2019, Nations Technologies Inc.
  3. *
  4. * All rights reserved.
  5. * ****************************************************************************
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. *
  10. * - Redistributions of source code must retain the above copyright notice,
  11. * this list of conditions and the disclaimer below.
  12. *
  13. * Nations' name may not be used to endorse or promote products derived from
  14. * this software without specific prior written permission.
  15. *
  16. * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY NATIONS "AS IS" AND ANY EXPRESS OR
  17. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
  19. * DISCLAIMED. IN NO EVENT SHALL NATIONS BE LIABLE FOR ANY DIRECT, INDIRECT,
  20. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  21. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  22. * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  23. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  24. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  25. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. * ****************************************************************************/
  27. /**
  28. * @file n32g45x_usart.c
  29. * @author Nations
  30. * @version v1.0.1
  31. *
  32. * @copyright Copyright (c) 2019, Nations Technologies Inc. All rights reserved.
  33. */
  34. #include "n32g45x_usart.h"
  35. #include "n32g45x_rcc.h"
  36. /** @addtogroup N32G45X_StdPeriph_Driver
  37. * @{
  38. */
  39. /** @addtogroup USART
  40. * @brief USART driver modules
  41. * @{
  42. */
  43. /** @addtogroup USART_Private_TypesDefinitions
  44. * @{
  45. */
  46. /**
  47. * @}
  48. */
  49. /** @addtogroup USART_Private_Defines
  50. * @{
  51. */
  52. #define CTRL1_UEN_SET ((uint16_t)0x2000) /*!< USART Enable Mask */
  53. #define CTRL1_UEN_RESET ((uint16_t)0xDFFF) /*!< USART Disable Mask */
  54. #define CTRL1_WUM_MASK ((uint16_t)0xF7FF) /*!< USART WakeUp Method Mask */
  55. #define CTRL1_RCVWU_SET ((uint16_t)0x0002) /*!< USART mute mode Enable Mask */
  56. #define CTRL1_RCVWU_RESET ((uint16_t)0xFFFD) /*!< USART mute mode Enable Mask */
  57. #define CTRL1_SDBRK_SET ((uint16_t)0x0001) /*!< USART Break Character send Mask */
  58. #define CTRL1_CLR_MASK ((uint16_t)0xE9F3) /*!< USART CTRL1 Mask */
  59. #define CTRL2_ADDR_MASK ((uint16_t)0xFFF0) /*!< USART address Mask */
  60. #define CTRL2_LINMEN_SET ((uint16_t)0x4000) /*!< USART LIN Enable Mask */
  61. #define CTRL2_LINMEN_RESET ((uint16_t)0xBFFF) /*!< USART LIN Disable Mask */
  62. #define CTRL2_LINBDL_MASK ((uint16_t)0xFFDF) /*!< USART LIN Break detection Mask */
  63. #define CTRL2_STPB_CLR_MASK ((uint16_t)0xCFFF) /*!< USART CTRL2 STOP Bits Mask */
  64. #define CTRL2_CLOCK_CLR_MASK ((uint16_t)0xF0FF) /*!< USART CTRL2 Clock Mask */
  65. #define CTRL3_SCMEN_SET ((uint16_t)0x0020) /*!< USART SC Enable Mask */
  66. #define CTRL3_SCMEN_RESET ((uint16_t)0xFFDF) /*!< USART SC Disable Mask */
  67. #define CTRL3_SCNACK_SET ((uint16_t)0x0010) /*!< USART SC NACK Enable Mask */
  68. #define CTRL3_SCNACK_RESET ((uint16_t)0xFFEF) /*!< USART SC NACK Disable Mask */
  69. #define CTRL3_HDMEN_SET ((uint16_t)0x0008) /*!< USART Half-Duplex Enable Mask */
  70. #define CTRL3_HDMEN_RESET ((uint16_t)0xFFF7) /*!< USART Half-Duplex Disable Mask */
  71. #define CTRL3_IRDALP_MASK ((uint16_t)0xFFFB) /*!< USART IrDA LowPower mode Mask */
  72. #define CTRL3_CLR_MASK ((uint16_t)0xFCFF) /*!< USART CTRL3 Mask */
  73. #define CTRL3_IRDAMEN_SET ((uint16_t)0x0002) /*!< USART IrDA Enable Mask */
  74. #define CTRL3_IRDAMEN_RESET ((uint16_t)0xFFFD) /*!< USART IrDA Disable Mask */
  75. #define GTP_LSB_MASK ((uint16_t)0x00FF) /*!< Guard Time Register LSB Mask */
  76. #define GTP_MSB_MASK ((uint16_t)0xFF00) /*!< Guard Time Register MSB Mask */
  77. #define INT_MASK ((uint16_t)0x001F) /*!< USART Interrupt Mask */
  78. /**
  79. * @}
  80. */
  81. /** @addtogroup USART_Private_Macros
  82. * @{
  83. */
  84. /**
  85. * @}
  86. */
  87. /** @addtogroup USART_Private_Variables
  88. * @{
  89. */
  90. /**
  91. * @}
  92. */
  93. /** @addtogroup USART_Private_FunctionPrototypes
  94. * @{
  95. */
  96. /**
  97. * @}
  98. */
  99. /** @addtogroup USART_Private_Functions
  100. * @{
  101. */
  102. /**
  103. * @brief Deinitializes the USARTx peripheral registers to their default reset values.
  104. * @param USARTx Select the USART or the UART peripheral.
  105. * This parameter can be one of the following values:
  106. * USART1, USART2, USART3, UART4 or UART5.
  107. */
  108. void USART_DeInit(USART_Module* USARTx)
  109. {
  110. /* Check the parameters */
  111. assert_param(IS_USART_ALL_PERIPH(USARTx));
  112. if (USARTx == USART1)
  113. {
  114. RCC_EnableAPB2PeriphReset(RCC_APB2_PERIPH_USART1, ENABLE);
  115. RCC_EnableAPB2PeriphReset(RCC_APB2_PERIPH_USART1, DISABLE);
  116. }
  117. else if (USARTx == USART2)
  118. {
  119. RCC_EnableAPB1PeriphReset(RCC_APB1_PERIPH_USART2, ENABLE);
  120. RCC_EnableAPB1PeriphReset(RCC_APB1_PERIPH_USART2, DISABLE);
  121. }
  122. else if (USARTx == USART3)
  123. {
  124. RCC_EnableAPB1PeriphReset(RCC_APB1_PERIPH_USART3, ENABLE);
  125. RCC_EnableAPB1PeriphReset(RCC_APB1_PERIPH_USART3, DISABLE);
  126. }
  127. else if (USARTx == UART4)
  128. {
  129. RCC_EnableAPB1PeriphReset(RCC_APB1_PERIPH_UART4, ENABLE);
  130. RCC_EnableAPB1PeriphReset(RCC_APB1_PERIPH_UART4, DISABLE);
  131. }
  132. else if (USARTx == UART5)
  133. {
  134. RCC_EnableAPB1PeriphReset(RCC_APB1_PERIPH_UART5, ENABLE);
  135. RCC_EnableAPB1PeriphReset(RCC_APB1_PERIPH_UART5, DISABLE);
  136. }
  137. else if (USARTx == UART6)
  138. {
  139. RCC_EnableAPB2PeriphReset(RCC_APB2_PERIPH_UART6, ENABLE);
  140. RCC_EnableAPB2PeriphReset(RCC_APB2_PERIPH_UART6, DISABLE);
  141. }
  142. else
  143. {
  144. if (USARTx == UART7)
  145. {
  146. RCC_EnableAPB2PeriphReset(RCC_APB2_PERIPH_UART7, ENABLE);
  147. RCC_EnableAPB2PeriphReset(RCC_APB2_PERIPH_UART7, DISABLE);
  148. }
  149. }
  150. }
  151. /**
  152. * @brief Initializes the USARTx peripheral according to the specified
  153. * parameters in the USART_InitStruct .
  154. * @param USARTx Select the USART or the UART peripheral.
  155. * This parameter can be one of the following values:
  156. * USART1, USART2, USART3, UART4 or UART5.
  157. * @param USART_InitStruct pointer to a USART_InitType structure
  158. * that contains the configuration information for the specified USART
  159. * peripheral.
  160. */
  161. void USART_Init(USART_Module* USARTx, USART_InitType* USART_InitStruct)
  162. {
  163. uint32_t tmpregister = 0x00, apbclock = 0x00;
  164. uint32_t integerdivider = 0x00;
  165. uint32_t fractionaldivider = 0x00;
  166. uint32_t usartxbase = 0;
  167. RCC_ClocksType RCC_ClocksStatus;
  168. /* Check the parameters */
  169. assert_param(IS_USART_ALL_PERIPH(USARTx));
  170. assert_param(IS_USART_BAUDRATE(USART_InitStruct->BaudRate));
  171. assert_param(IS_USART_WORD_LENGTH(USART_InitStruct->WordLength));
  172. assert_param(IS_USART_STOPBITS(USART_InitStruct->StopBits));
  173. assert_param(IS_USART_PARITY(USART_InitStruct->Parity));
  174. assert_param(IS_USART_MODE(USART_InitStruct->Mode));
  175. assert_param(IS_USART_HARDWARE_FLOW_CONTROL(USART_InitStruct->HardwareFlowControl));
  176. /* The hardware flow control is available only for USART1, USART2 and USART3 */
  177. if (USART_InitStruct->HardwareFlowControl != USART_HFCTRL_NONE)
  178. {
  179. assert_param(IS_USART_123_PERIPH(USARTx));
  180. }
  181. usartxbase = (uint32_t)USARTx;
  182. /*---------------------------- USART CTRL2 Configuration -----------------------*/
  183. tmpregister = USARTx->CTRL2;
  184. /* Clear STOP[13:12] bits */
  185. tmpregister &= CTRL2_STPB_CLR_MASK;
  186. /* Configure the USART Stop Bits, Clock, CPOL, CPHA and LastBit ------------*/
  187. /* Set STOP[13:12] bits according to StopBits value */
  188. tmpregister |= (uint32_t)USART_InitStruct->StopBits;
  189. /* Write to USART CTRL2 */
  190. USARTx->CTRL2 = (uint16_t)tmpregister;
  191. /*---------------------------- USART CTRL1 Configuration -----------------------*/
  192. tmpregister = USARTx->CTRL1;
  193. /* Clear M, PCE, PS, TE and RE bits */
  194. tmpregister &= CTRL1_CLR_MASK;
  195. /* Configure the USART Word Length, Parity and mode ----------------------- */
  196. /* Set the M bits according to WordLength value */
  197. /* Set PCE and PS bits according to Parity value */
  198. /* Set TE and RE bits according to Mode value */
  199. tmpregister |= (uint32_t)USART_InitStruct->WordLength | USART_InitStruct->Parity | USART_InitStruct->Mode;
  200. /* Write to USART CTRL1 */
  201. USARTx->CTRL1 = (uint16_t)tmpregister;
  202. /*---------------------------- USART CTRL3 Configuration -----------------------*/
  203. tmpregister = USARTx->CTRL3;
  204. /* Clear CTSE and RTSE bits */
  205. tmpregister &= CTRL3_CLR_MASK;
  206. /* Configure the USART HFC -------------------------------------------------*/
  207. /* Set CTSE and RTSE bits according to HardwareFlowControl value */
  208. tmpregister |= USART_InitStruct->HardwareFlowControl;
  209. /* Write to USART CTRL3 */
  210. USARTx->CTRL3 = (uint16_t)tmpregister;
  211. /*---------------------------- USART PBC Configuration -----------------------*/
  212. /* Configure the USART Baud Rate -------------------------------------------*/
  213. RCC_GetClocksFreqValue(&RCC_ClocksStatus);
  214. if ((usartxbase == USART1_BASE) || (usartxbase == UART6_BASE) || (usartxbase == UART7_BASE))
  215. {
  216. apbclock = RCC_ClocksStatus.Pclk2Freq;
  217. }
  218. else
  219. {
  220. apbclock = RCC_ClocksStatus.Pclk1Freq;
  221. }
  222. /* Determine the integer part */
  223. integerdivider = ((25 * apbclock) / (4 * (USART_InitStruct->BaudRate)));
  224. tmpregister = (integerdivider / 100) << 4;
  225. /* Determine the fractional part */
  226. fractionaldivider = (((((integerdivider - (100 * (tmpregister >> 4))) * 16) + 50) / 100));
  227. /*Determine whether the fractional part needs to carried*/
  228. if((fractionaldivider >> 4) == 1){
  229. tmpregister = ((integerdivider / 100) + 1) << 4;
  230. }
  231. /* Implement the fractional part in the register */
  232. tmpregister |= fractionaldivider & ((uint8_t)0x0F);
  233. /* Write to USART PBC */
  234. USARTx->BRCF = (uint16_t)tmpregister;
  235. }
  236. /**
  237. * @brief Fills each USART_InitStruct member with its default value.
  238. * @param USART_InitStruct pointer to a USART_InitType structure
  239. * which will be initialized.
  240. */
  241. void USART_StructInit(USART_InitType* USART_InitStruct)
  242. {
  243. /* USART_InitStruct members default value */
  244. USART_InitStruct->BaudRate = 9600;
  245. USART_InitStruct->WordLength = USART_WL_8B;
  246. USART_InitStruct->StopBits = USART_STPB_1;
  247. USART_InitStruct->Parity = USART_PE_NO;
  248. USART_InitStruct->Mode = USART_MODE_RX | USART_MODE_TX;
  249. USART_InitStruct->HardwareFlowControl = USART_HFCTRL_NONE;
  250. }
  251. /**
  252. * @brief Initializes the USARTx peripheral Clock according to the
  253. * specified parameters in the USART_ClockInitStruct .
  254. * @param USARTx where x can be 1, 2, 3 to select the USART peripheral.
  255. * @param USART_ClockInitStruct pointer to a USART_ClockInitType
  256. * structure that contains the configuration information for the specified
  257. * USART peripheral.
  258. * @note The Smart Card and Synchronous modes are not available for UART4/UART5/UART6/UART7.
  259. */
  260. void USART_ClockInit(USART_Module* USARTx, USART_ClockInitType* USART_ClockInitStruct)
  261. {
  262. uint32_t tmpregister = 0x00;
  263. /* Check the parameters */
  264. assert_param(IS_USART_123_PERIPH(USARTx));
  265. assert_param(IS_USART_CLOCK(USART_ClockInitStruct->Clock));
  266. assert_param(IS_USART_CPOL(USART_ClockInitStruct->Polarity));
  267. assert_param(IS_USART_CPHA(USART_ClockInitStruct->Phase));
  268. assert_param(IS_USART_LASTBIT(USART_ClockInitStruct->LastBit));
  269. /*---------------------------- USART CTRL2 Configuration -----------------------*/
  270. tmpregister = USARTx->CTRL2;
  271. /* Clear CLKEN, CPOL, CPHA and LBCL bits */
  272. tmpregister &= CTRL2_CLOCK_CLR_MASK;
  273. /* Configure the USART Clock, CPOL, CPHA and LastBit ------------*/
  274. /* Set CLKEN bit according to Clock value */
  275. /* Set CPOL bit according to Polarity value */
  276. /* Set CPHA bit according to Phase value */
  277. /* Set LBCL bit according to LastBit value */
  278. tmpregister |= (uint32_t)USART_ClockInitStruct->Clock | USART_ClockInitStruct->Polarity
  279. | USART_ClockInitStruct->Phase | USART_ClockInitStruct->LastBit;
  280. /* Write to USART CTRL2 */
  281. USARTx->CTRL2 = (uint16_t)tmpregister;
  282. }
  283. /**
  284. * @brief Fills each USART_ClockInitStruct member with its default value.
  285. * @param USART_ClockInitStruct pointer to a USART_ClockInitType
  286. * structure which will be initialized.
  287. */
  288. void USART_ClockStructInit(USART_ClockInitType* USART_ClockInitStruct)
  289. {
  290. /* USART_ClockInitStruct members default value */
  291. USART_ClockInitStruct->Clock = USART_CLK_DISABLE;
  292. USART_ClockInitStruct->Polarity = USART_CLKPOL_LOW;
  293. USART_ClockInitStruct->Phase = USART_CLKPHA_1EDGE;
  294. USART_ClockInitStruct->LastBit = USART_CLKLB_DISABLE;
  295. }
  296. /**
  297. * @brief Enables or disables the specified USART peripheral.
  298. * @param USARTx Select the USART or the UART peripheral.
  299. * This parameter can be one of the following values:
  300. * USART1, USART2, USART3, UART4, UART5, UART6 or UART7.
  301. * @param Cmd new state of the USARTx peripheral.
  302. * This parameter can be: ENABLE or DISABLE.
  303. */
  304. void USART_Enable(USART_Module* USARTx, FunctionalState Cmd)
  305. {
  306. /* Check the parameters */
  307. assert_param(IS_USART_ALL_PERIPH(USARTx));
  308. assert_param(IS_FUNCTIONAL_STATE(Cmd));
  309. if (Cmd != DISABLE)
  310. {
  311. /* Enable the selected USART by setting the UE bit in the CTRL1 register */
  312. USARTx->CTRL1 |= CTRL1_UEN_SET;
  313. }
  314. else
  315. {
  316. /* Disable the selected USART by clearing the UE bit in the CTRL1 register */
  317. USARTx->CTRL1 &= CTRL1_UEN_RESET;
  318. }
  319. }
  320. /**
  321. * @brief Enables or disables the specified USART interrupts.
  322. * @param USARTx Select the USART or the UART peripheral.
  323. * This parameter can be one of the following values:
  324. * USART1, USART2, USART3, UART4, UART5, UART6 or UART7.
  325. * @param USART_INT specifies the USART interrupt sources to be enabled or disabled.
  326. * This parameter can be one of the following values:
  327. * @arg USART_INT_CTSF CTS change interrupt (not available for UART4 and UART5)
  328. * @arg USART_INT_LINBD LIN Break detection interrupt
  329. * @arg USART_INT_TXDE Transmit Data Register empty interrupt
  330. * @arg USART_INT_TXC Transmission complete interrupt
  331. * @arg USART_INT_RXDNE Receive Data register not empty interrupt
  332. * @arg USART_INT_IDLEF Idle line detection interrupt
  333. * @arg USART_INT_PEF Parity Error interrupt
  334. * @arg USART_INT_ERRF Error interrupt(Frame error, noise error, overrun error)
  335. * @param Cmd new state of the specified USARTx interrupts.
  336. * This parameter can be: ENABLE or DISABLE.
  337. */
  338. void USART_ConfigInt(USART_Module* USARTx, uint16_t USART_INT, FunctionalState Cmd)
  339. {
  340. uint32_t usartreg = 0x00, itpos = 0x00, itmask = 0x00;
  341. uint32_t usartxbase = 0x00;
  342. /* Check the parameters */
  343. assert_param(IS_USART_ALL_PERIPH(USARTx));
  344. assert_param(IS_USART_CFG_INT(USART_INT));
  345. assert_param(IS_FUNCTIONAL_STATE(Cmd));
  346. /* The CTS interrupt is not available for UART4/UART5/UART6/UART7 */
  347. if (USART_INT == USART_INT_CTSF)
  348. {
  349. assert_param(IS_USART_123_PERIPH(USARTx));
  350. }
  351. usartxbase = (uint32_t)USARTx;
  352. /* Get the USART register index */
  353. usartreg = (((uint8_t)USART_INT) >> 0x05);
  354. /* Get the interrupt position */
  355. itpos = USART_INT & INT_MASK;
  356. itmask = (((uint32_t)0x01) << itpos);
  357. if (usartreg == 0x01) /* The IT is in CTRL1 register */
  358. {
  359. usartxbase += 0x0C;
  360. }
  361. else if (usartreg == 0x02) /* The IT is in CTRL2 register */
  362. {
  363. usartxbase += 0x10;
  364. }
  365. else /* The IT is in CTRL3 register */
  366. {
  367. usartxbase += 0x14;
  368. }
  369. if (Cmd != DISABLE)
  370. {
  371. *(__IO uint32_t*)usartxbase |= itmask;
  372. }
  373. else
  374. {
  375. *(__IO uint32_t*)usartxbase &= ~itmask;
  376. }
  377. }
  378. /**
  379. * @brief Enables or disables the USART's DMA interface.
  380. * @param USARTx Select the USART or the UART peripheral.
  381. * This parameter can be one of the following values:
  382. * USART1, USART2, USART3, UART4, UART5, UART6 or UART7.
  383. * @param USART_DMAReq specifies the DMA request.
  384. * This parameter can be any combination of the following values:
  385. * @arg USART_DMAREQ_TX USART DMA transmit request
  386. * @arg USART_DMAREQ_RX USART DMA receive request
  387. * @param Cmd new state of the DMA Request sources.
  388. * This parameter can be: ENABLE or DISABLE.
  389. */
  390. void USART_EnableDMA(USART_Module* USARTx, uint16_t USART_DMAReq, FunctionalState Cmd)
  391. {
  392. /* Check the parameters */
  393. assert_param(IS_USART_ALL_PERIPH(USARTx));
  394. assert_param(IS_USART_DMAREQ(USART_DMAReq));
  395. assert_param(IS_FUNCTIONAL_STATE(Cmd));
  396. if (Cmd != DISABLE)
  397. {
  398. /* Enable the DMA transfer for selected requests by setting the DMAT and/or
  399. DADDR bits in the USART CTRL3 register */
  400. USARTx->CTRL3 |= USART_DMAReq;
  401. }
  402. else
  403. {
  404. /* Disable the DMA transfer for selected requests by clearing the DMAT and/or
  405. DADDR bits in the USART CTRL3 register */
  406. USARTx->CTRL3 &= (uint16_t)~USART_DMAReq;
  407. }
  408. }
  409. /**
  410. * @brief Sets the address of the USART node.
  411. * @param USARTx Select the USART or the UART peripheral.
  412. * This parameter can be one of the following values:
  413. * USART1, USART2, USART3, UART4, UART5, UART6 or UART7.
  414. * @param USART_Addr Indicates the address of the USART node.
  415. */
  416. void USART_SetAddr(USART_Module* USARTx, uint8_t USART_Addr)
  417. {
  418. /* Check the parameters */
  419. assert_param(IS_USART_ALL_PERIPH(USARTx));
  420. assert_param(IS_USART_ADDRESS(USART_Addr));
  421. /* Clear the USART address */
  422. USARTx->CTRL2 &= CTRL2_ADDR_MASK;
  423. /* Set the USART address node */
  424. USARTx->CTRL2 |= USART_Addr;
  425. }
  426. /**
  427. * @brief Selects the USART WakeUp method.
  428. * @param USARTx Select the USART or the UART peripheral.
  429. * This parameter can be one of the following values:
  430. * USART1, USART2, USART3, UART4, UART5, UART6 or UART7.
  431. * @param USART_WakeUpMode specifies the USART wakeup method.
  432. * This parameter can be one of the following values:
  433. * @arg USART_WUM_IDLELINE WakeUp by an idle line detection
  434. * @arg USART_WUM_ADDRMASK WakeUp by an address mark
  435. */
  436. void USART_ConfigWakeUpMode(USART_Module* USARTx, uint16_t USART_WakeUpMode)
  437. {
  438. /* Check the parameters */
  439. assert_param(IS_USART_ALL_PERIPH(USARTx));
  440. assert_param(IS_USART_WAKEUP(USART_WakeUpMode));
  441. USARTx->CTRL1 &= CTRL1_WUM_MASK;
  442. USARTx->CTRL1 |= USART_WakeUpMode;
  443. }
  444. /**
  445. * @brief Determines if the USART is in mute mode or not.
  446. * @param USARTx Select the USART or the UART peripheral.
  447. * This parameter can be one of the following values:
  448. * USART1, USART2, USART3, UART4, UART5, UART6 or UART7.
  449. * @param Cmd new state of the USART mute mode.
  450. * This parameter can be: ENABLE or DISABLE.
  451. */
  452. void USART_EnableRcvWakeUp(USART_Module* USARTx, FunctionalState Cmd)
  453. {
  454. /* Check the parameters */
  455. assert_param(IS_USART_ALL_PERIPH(USARTx));
  456. assert_param(IS_FUNCTIONAL_STATE(Cmd));
  457. if (Cmd != DISABLE)
  458. {
  459. /* Enable the USART mute mode by setting the RWU bit in the CTRL1 register */
  460. USARTx->CTRL1 |= CTRL1_RCVWU_SET;
  461. }
  462. else
  463. {
  464. /* Disable the USART mute mode by clearing the RWU bit in the CTRL1 register */
  465. USARTx->CTRL1 &= CTRL1_RCVWU_RESET;
  466. }
  467. }
  468. /**
  469. * @brief Sets the USART LIN Break detection length.
  470. * @param USARTx Select the USART or the UART peripheral.
  471. * This parameter can be one of the following values:
  472. * USART1, USART2, USART3, UART4, UART5, UART6 or UART7.
  473. * @param USART_LINBreakDetectLength specifies the LIN break detection length.
  474. * This parameter can be one of the following values:
  475. * @arg USART_LINBDL_10B 10-bit break detection
  476. * @arg USART_LINBDL_11B 11-bit break detection
  477. */
  478. void USART_ConfigLINBreakDetectLength(USART_Module* USARTx, uint16_t USART_LINBreakDetectLength)
  479. {
  480. /* Check the parameters */
  481. assert_param(IS_USART_ALL_PERIPH(USARTx));
  482. assert_param(IS_USART_LIN_BREAK_DETECT_LENGTH(USART_LINBreakDetectLength));
  483. USARTx->CTRL2 &= CTRL2_LINBDL_MASK;
  484. USARTx->CTRL2 |= USART_LINBreakDetectLength;
  485. }
  486. /**
  487. * @brief Enables or disables the USART's LIN mode.
  488. * @param USARTx Select the USART or the UART peripheral.
  489. * This parameter can be one of the following values:
  490. * USART1, USART2, USART3, UART4, UART5, UART6 or UART7.
  491. * @param Cmd new state of the USART LIN mode.
  492. * This parameter can be: ENABLE or DISABLE.
  493. */
  494. void USART_EnableLIN(USART_Module* USARTx, FunctionalState Cmd)
  495. {
  496. /* Check the parameters */
  497. assert_param(IS_USART_ALL_PERIPH(USARTx));
  498. assert_param(IS_FUNCTIONAL_STATE(Cmd));
  499. if (Cmd != DISABLE)
  500. {
  501. /* Enable the LIN mode by setting the LINEN bit in the CTRL2 register */
  502. USARTx->CTRL2 |= CTRL2_LINMEN_SET;
  503. }
  504. else
  505. {
  506. /* Disable the LIN mode by clearing the LINEN bit in the CTRL2 register */
  507. USARTx->CTRL2 &= CTRL2_LINMEN_RESET;
  508. }
  509. }
  510. /**
  511. * @brief Transmits single data through the USARTx peripheral.
  512. * @param USARTx Select the USART or the UART peripheral.
  513. * This parameter can be one of the following values:
  514. * USART1, USART2, USART3, UART4, UART5, UART6 or UART7.
  515. * @param Data the data to transmit.
  516. */
  517. void USART_SendData(USART_Module* USARTx, uint16_t Data)
  518. {
  519. /* Check the parameters */
  520. assert_param(IS_USART_ALL_PERIPH(USARTx));
  521. assert_param(IS_USART_DATA(Data));
  522. /* Transmit Data */
  523. USARTx->DAT = (Data & (uint16_t)0x01FF);
  524. }
  525. /**
  526. * @brief Returns the most recent received data by the USARTx peripheral.
  527. * @param USARTx Select the USART or the UART peripheral.
  528. * This parameter can be one of the following values:
  529. * USART1, USART2, USART3, UART4, UART5, UART6 or UART7.
  530. * @return The received data.
  531. */
  532. uint16_t USART_ReceiveData(USART_Module* USARTx)
  533. {
  534. /* Check the parameters */
  535. assert_param(IS_USART_ALL_PERIPH(USARTx));
  536. /* Receive Data */
  537. return (uint16_t)(USARTx->DAT & (uint16_t)0x01FF);
  538. }
  539. /**
  540. * @brief Transmits break characters.
  541. * @param USARTx Select the USART or the UART peripheral.
  542. * This parameter can be one of the following values:
  543. * USART1, USART2, USART3, UART4, UART5, UART6 or UART7.
  544. */
  545. void USART_SendBreak(USART_Module* USARTx)
  546. {
  547. /* Check the parameters */
  548. assert_param(IS_USART_ALL_PERIPH(USARTx));
  549. /* Send break characters */
  550. USARTx->CTRL1 |= CTRL1_SDBRK_SET;
  551. }
  552. /**
  553. * @brief Sets the specified USART guard time.
  554. * @param USARTx where x can be 1, 2 or 3 to select the USART peripheral.
  555. * @param USART_GuardTime specifies the guard time.
  556. * @note The guard time bits are not available for UART4/UART5/UART6/UART7.
  557. */
  558. void USART_SetGuardTime(USART_Module* USARTx, uint8_t USART_GuardTime)
  559. {
  560. /* Check the parameters */
  561. assert_param(IS_USART_123_PERIPH(USARTx));
  562. /* Clear the USART Guard time */
  563. USARTx->GTP &= GTP_LSB_MASK;
  564. /* Set the USART guard time */
  565. USARTx->GTP |= (uint16_t)((uint16_t)USART_GuardTime << 0x08);
  566. }
  567. /**
  568. * @brief Sets the system clock prescaler.
  569. * @param USARTx Select the USART or the UART peripheral.
  570. * This parameter can be one of the following values:
  571. * USART1, USART2, USART3, UART4, UART5, UART6 or UART7.
  572. * @param USART_Prescaler specifies the prescaler clock.
  573. * @note The function is used for IrDA mode with UART4 and UART5.
  574. */
  575. void USART_SetPrescaler(USART_Module* USARTx, uint8_t USART_Prescaler)
  576. {
  577. /* Check the parameters */
  578. assert_param(IS_USART_ALL_PERIPH(USARTx));
  579. /* Clear the USART prescaler */
  580. USARTx->GTP &= GTP_MSB_MASK;
  581. /* Set the USART prescaler */
  582. USARTx->GTP |= USART_Prescaler;
  583. }
  584. /**
  585. * @brief Enables or disables the USART's Smart Card mode.
  586. * @param USARTx where x can be 1, 2 or 3 to select the USART peripheral.
  587. * @param Cmd new state of the Smart Card mode.
  588. * This parameter can be: ENABLE or DISABLE.
  589. * @note The Smart Card mode is not available for UART4/UART5/UART6/UART7.
  590. */
  591. void USART_EnableSmartCard(USART_Module* USARTx, FunctionalState Cmd)
  592. {
  593. /* Check the parameters */
  594. assert_param(IS_USART_123_PERIPH(USARTx));
  595. assert_param(IS_FUNCTIONAL_STATE(Cmd));
  596. if (Cmd != DISABLE)
  597. {
  598. /* Enable the SC mode by setting the SCEN bit in the CTRL3 register */
  599. USARTx->CTRL3 |= CTRL3_SCMEN_SET;
  600. }
  601. else
  602. {
  603. /* Disable the SC mode by clearing the SCEN bit in the CTRL3 register */
  604. USARTx->CTRL3 &= CTRL3_SCMEN_RESET;
  605. }
  606. }
  607. /**
  608. * @brief Enables or disables NACK transmission.
  609. * @param USARTx where x can be 1, 2 or 3 to select the USART peripheral.
  610. * @param Cmd new state of the NACK transmission.
  611. * This parameter can be: ENABLE or DISABLE.
  612. * @note The Smart Card mode is not available for UART4/UART5/UART6/UART7.
  613. */
  614. void USART_SetSmartCardNACK(USART_Module* USARTx, FunctionalState Cmd)
  615. {
  616. /* Check the parameters */
  617. assert_param(IS_USART_123_PERIPH(USARTx));
  618. assert_param(IS_FUNCTIONAL_STATE(Cmd));
  619. if (Cmd != DISABLE)
  620. {
  621. /* Enable the NACK transmission by setting the NACK bit in the CTRL3 register */
  622. USARTx->CTRL3 |= CTRL3_SCNACK_SET;
  623. }
  624. else
  625. {
  626. /* Disable the NACK transmission by clearing the NACK bit in the CTRL3 register */
  627. USARTx->CTRL3 &= CTRL3_SCNACK_RESET;
  628. }
  629. }
  630. /**
  631. * @brief Enables or disables the USART's Half Duplex communication.
  632. * @param USARTx Select the USART or the UART peripheral.
  633. * This parameter can be one of the following values:
  634. * USART1, USART2, USART3, UART4, UART5, UART6 or UART7.
  635. * @param Cmd new state of the USART Communication.
  636. * This parameter can be: ENABLE or DISABLE.
  637. */
  638. void USART_EnableHalfDuplex(USART_Module* USARTx, FunctionalState Cmd)
  639. {
  640. /* Check the parameters */
  641. assert_param(IS_USART_ALL_PERIPH(USARTx));
  642. assert_param(IS_FUNCTIONAL_STATE(Cmd));
  643. if (Cmd != DISABLE)
  644. {
  645. /* Enable the Half-Duplex mode by setting the HDSEL bit in the CTRL3 register */
  646. USARTx->CTRL3 |= CTRL3_HDMEN_SET;
  647. }
  648. else
  649. {
  650. /* Disable the Half-Duplex mode by clearing the HDSEL bit in the CTRL3 register */
  651. USARTx->CTRL3 &= CTRL3_HDMEN_RESET;
  652. }
  653. }
  654. /**
  655. * @brief Configures the USART's IrDA interface.
  656. * @param USARTx Select the USART or the UART peripheral.
  657. * This parameter can be one of the following values:
  658. * USART1, USART2, USART3, UART4, UART5, UART6 or UART7.
  659. * @param USART_IrDAMode specifies the IrDA mode.
  660. * This parameter can be one of the following values:
  661. * @arg USART_IRDAMODE_LOWPPWER
  662. * @arg USART_IRDAMODE_NORMAL
  663. */
  664. void USART_ConfigIrDAMode(USART_Module* USARTx, uint16_t USART_IrDAMode)
  665. {
  666. /* Check the parameters */
  667. assert_param(IS_USART_ALL_PERIPH(USARTx));
  668. assert_param(IS_USART_IRDA_MODE(USART_IrDAMode));
  669. USARTx->CTRL3 &= CTRL3_IRDALP_MASK;
  670. USARTx->CTRL3 |= USART_IrDAMode;
  671. }
  672. /**
  673. * @brief Enables or disables the USART's IrDA interface.
  674. * @param USARTx Select the USART or the UART peripheral.
  675. * This parameter can be one of the following values:
  676. * USART1, USART2, USART3, UART4, UART5, UART6 or UART7.
  677. * @param Cmd new state of the IrDA mode.
  678. * This parameter can be: ENABLE or DISABLE.
  679. */
  680. void USART_EnableIrDA(USART_Module* USARTx, FunctionalState Cmd)
  681. {
  682. /* Check the parameters */
  683. assert_param(IS_USART_ALL_PERIPH(USARTx));
  684. assert_param(IS_FUNCTIONAL_STATE(Cmd));
  685. if (Cmd != DISABLE)
  686. {
  687. /* Enable the IrDA mode by setting the IREN bit in the CTRL3 register */
  688. USARTx->CTRL3 |= CTRL3_IRDAMEN_SET;
  689. }
  690. else
  691. {
  692. /* Disable the IrDA mode by clearing the IREN bit in the CTRL3 register */
  693. USARTx->CTRL3 &= CTRL3_IRDAMEN_RESET;
  694. }
  695. }
  696. /**
  697. * @brief Checks whether the specified USART flag is set or not.
  698. * @param USARTx Select the USART or the UART peripheral.
  699. * This parameter can be one of the following values:
  700. * USART1, USART2, USART3, UART4, UART5, UART6 or UART7.
  701. * @param USART_FLAG specifies the flag to check.
  702. * This parameter can be one of the following values:
  703. * @arg USART_FLAG_CTSF CTS Change flag (not available for UART4 and UART5)
  704. * @arg USART_FLAG_LINBD LIN Break detection flag
  705. * @arg USART_FLAG_TXDE Transmit data register empty flag
  706. * @arg USART_FLAG_TXC Transmission Complete flag
  707. * @arg USART_FLAG_RXDNE Receive data register not empty flag
  708. * @arg USART_FLAG_IDLEF Idle Line detection flag
  709. * @arg USART_FLAG_OREF OverRun Error flag
  710. * @arg USART_FLAG_NEF Noise Error flag
  711. * @arg USART_FLAG_FEF Framing Error flag
  712. * @arg USART_FLAG_PEF Parity Error flag
  713. * @return The new state of USART_FLAG (SET or RESET).
  714. */
  715. FlagStatus USART_GetFlagStatus(USART_Module* USARTx, uint16_t USART_FLAG)
  716. {
  717. FlagStatus bitstatus = RESET;
  718. /* Check the parameters */
  719. assert_param(IS_USART_ALL_PERIPH(USARTx));
  720. assert_param(IS_USART_FLAG(USART_FLAG));
  721. /* The CTS flag is not available for UART4/UART5/UART6/UART7 */
  722. if (USART_FLAG == USART_FLAG_CTSF)
  723. {
  724. assert_param(IS_USART_123_PERIPH(USARTx));
  725. }
  726. if ((USARTx->STS & USART_FLAG) != (uint16_t)RESET)
  727. {
  728. bitstatus = SET;
  729. }
  730. else
  731. {
  732. bitstatus = RESET;
  733. }
  734. return bitstatus;
  735. }
  736. /**
  737. * @brief Clears the USARTx's pending flags.
  738. * @param USARTx Select the USART or the UART peripheral.
  739. * This parameter can be one of the following values:
  740. * USART1, USART2, USART3, UART4, UART5, UART6 or UART7.
  741. * @param USART_FLAG specifies the flag to clear.
  742. * This parameter can be any combination of the following values:
  743. * @arg USART_FLAG_CTSF CTS Change flag (not available for UART4 and UART5).
  744. * @arg USART_FLAG_LINBD LIN Break detection flag.
  745. * @arg USART_FLAG_TXC Transmission Complete flag.
  746. * @arg USART_FLAG_RXDNE Receive data register not empty flag.
  747. *
  748. * @note
  749. * - PE (Parity error), FE (Framing error), NE (Noise error), ORE (OverRun
  750. * error) and IDLE (Idle line detected) flags are cleared by software
  751. * sequence: a read operation to USART_SR register (USART_GetFlagStatus())
  752. * followed by a read operation to USART_DR register (USART_ReceiveData()).
  753. * - RXNE flag can be also cleared by a read to the USART_DR register
  754. * (USART_ReceiveData()).
  755. * - TC flag can be also cleared by software sequence: a read operation to
  756. * USART_SR register (USART_GetFlagStatus()) followed by a write operation
  757. * to USART_DR register (USART_SendData()).
  758. * - TXE flag is cleared only by a write to the USART_DR register
  759. * (USART_SendData()).
  760. */
  761. void USART_ClrFlag(USART_Module* USARTx, uint16_t USART_FLAG)
  762. {
  763. /* Check the parameters */
  764. assert_param(IS_USART_ALL_PERIPH(USARTx));
  765. assert_param(IS_USART_CLEAR_FLAG(USART_FLAG));
  766. /* The CTS flag is not available for UART4/UART5/UART6/UART7 */
  767. if ((USART_FLAG & USART_FLAG_CTSF) == USART_FLAG_CTSF)
  768. {
  769. assert_param(IS_USART_123_PERIPH(USARTx));
  770. }
  771. USARTx->STS = (uint16_t)~USART_FLAG;
  772. }
  773. /**
  774. * @brief Checks whether the specified USART interrupt has occurred or not.
  775. * @param USARTx Select the USART or the UART peripheral.
  776. * This parameter can be one of the following values:
  777. * USART1, USART2, USART3, UART4, UART5, UART6 or UART7.
  778. * @param USART_INT specifies the USART interrupt source to check.
  779. * This parameter can be one of the following values:
  780. * @arg USART_INT_CTSF CTS change interrupt (not available for UART4 and UART5)
  781. * @arg USART_INT_LINBD LIN Break detection interrupt
  782. * @arg USART_INT_TXDE Tansmit Data Register empty interrupt
  783. * @arg USART_INT_TXC Transmission complete interrupt
  784. * @arg USART_INT_RXDNE Receive Data register not empty interrupt
  785. * @arg USART_INT_IDLEF Idle line detection interrupt
  786. * @arg USART_INT_OREF OverRun Error interrupt
  787. * @arg USART_INT_NEF Noise Error interrupt
  788. * @arg USART_INT_FEF Framing Error interrupt
  789. * @arg USART_INT_PEF Parity Error interrupt
  790. * @return The new state of USART_INT (SET or RESET).
  791. */
  792. INTStatus USART_GetIntStatus(USART_Module* USARTx, uint16_t USART_INT)
  793. {
  794. uint32_t bitpos = 0x00, itmask = 0x00, usartreg = 0x00;
  795. INTStatus bitstatus = RESET;
  796. /* Check the parameters */
  797. assert_param(IS_USART_ALL_PERIPH(USARTx));
  798. assert_param(IS_USART_GET_INT(USART_INT));
  799. /* The CTS interrupt is not available for UART4/UART5/UART6/UART7 */
  800. if (USART_INT == USART_INT_CTSF)
  801. {
  802. assert_param(IS_USART_123_PERIPH(USARTx));
  803. }
  804. /* Get the USART register index */
  805. usartreg = (((uint8_t)USART_INT) >> 0x05);
  806. /* Get the interrupt position */
  807. itmask = USART_INT & INT_MASK;
  808. itmask = (uint32_t)0x01 << itmask;
  809. if (usartreg == 0x01) /* The IT is in CTRL1 register */
  810. {
  811. itmask &= USARTx->CTRL1;
  812. }
  813. else if (usartreg == 0x02) /* The IT is in CTRL2 register */
  814. {
  815. itmask &= USARTx->CTRL2;
  816. }
  817. else /* The IT is in CTRL3 register */
  818. {
  819. itmask &= USARTx->CTRL3;
  820. }
  821. bitpos = USART_INT >> 0x08;
  822. bitpos = (uint32_t)0x01 << bitpos;
  823. bitpos &= USARTx->STS;
  824. if ((itmask != (uint16_t)RESET) && (bitpos != (uint16_t)RESET))
  825. {
  826. bitstatus = SET;
  827. }
  828. else
  829. {
  830. bitstatus = RESET;
  831. }
  832. return bitstatus;
  833. }
  834. /**
  835. * @brief Clears the USARTx's interrupt pending bits.
  836. * @param USARTx Select the USART or the UART peripheral.
  837. * This parameter can be one of the following values:
  838. * USART1, USART2, USART3, UART4, UART5, UART6 or UART7.
  839. * @param USART_INT specifies the interrupt pending bit to clear.
  840. * This parameter can be one of the following values:
  841. * @arg USART_INT_CTSF CTS change interrupt (not available for UART4 and UART5)
  842. * @arg USART_INT_LINBD LIN Break detection interrupt
  843. * @arg USART_INT_TXC Transmission complete interrupt.
  844. * @arg USART_INT_RXDNE Receive Data register not empty interrupt.
  845. *
  846. * @note
  847. * - PE (Parity error), FE (Framing error), NE (Noise error), ORE (OverRun
  848. * error) and IDLE (Idle line detected) pending bits are cleared by
  849. * software sequence: a read operation to USART_SR register
  850. * (USART_GetIntStatus()) followed by a read operation to USART_DR register
  851. * (USART_ReceiveData()).
  852. * - RXNE pending bit can be also cleared by a read to the USART_DR register
  853. * (USART_ReceiveData()).
  854. * - TC pending bit can be also cleared by software sequence: a read
  855. * operation to USART_SR register (USART_GetIntStatus()) followed by a write
  856. * operation to USART_DR register (USART_SendData()).
  857. * - TXE pending bit is cleared only by a write to the USART_DR register
  858. * (USART_SendData()).
  859. */
  860. void USART_ClrIntPendingBit(USART_Module* USARTx, uint16_t USART_INT)
  861. {
  862. uint16_t bitpos = 0x00, itmask = 0x00;
  863. /* Check the parameters */
  864. assert_param(IS_USART_ALL_PERIPH(USARTx));
  865. assert_param(IS_USART_CLR_INT(USART_INT));
  866. /* The CTS interrupt is not available for UART4/UART5/UART6/UART7 */
  867. if (USART_INT == USART_INT_CTSF)
  868. {
  869. assert_param(IS_USART_123_PERIPH(USARTx));
  870. }
  871. bitpos = USART_INT >> 0x08;
  872. itmask = ((uint16_t)0x01 << (uint16_t)bitpos);
  873. USARTx->STS = (uint16_t)~itmask;
  874. }
  875. /**
  876. * @}
  877. */
  878. /**
  879. * @}
  880. */
  881. /**
  882. * @}
  883. */