air32f10x_usart.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035
  1. /* Includes ------------------------------------------------------------------*/
  2. #include "air32f10x_usart.h"
  3. #include "air32f10x_rcc.h"
  4. /** @addtogroup air32f10x_StdPeriph_Driver
  5. * @{
  6. */
  7. /** @defgroup USART
  8. * @brief USART driver modules
  9. * @{
  10. */
  11. /** @defgroup USART_Private_TypesDefinitions
  12. * @{
  13. */
  14. /**
  15. * @}
  16. */
  17. /** @defgroup USART_Private_Defines
  18. * @{
  19. */
  20. #define CR1_UE_Set ((uint16_t)0x2000) /*!< USART Enable Mask */
  21. #define CR1_UE_Reset ((uint16_t)0xDFFF) /*!< USART Disable Mask */
  22. #define CR1_WAKE_Mask ((uint16_t)0xF7FF) /*!< USART WakeUp Method Mask */
  23. #define CR1_RWU_Set ((uint16_t)0x0002) /*!< USART mute mode Enable Mask */
  24. #define CR1_RWU_Reset ((uint16_t)0xFFFD) /*!< USART mute mode Enable Mask */
  25. #define CR1_SBK_Set ((uint16_t)0x0001) /*!< USART Break Character send Mask */
  26. #define CR1_CLEAR_Mask ((uint16_t)0xE9F3) /*!< USART CR1 Mask */
  27. #define CR2_Address_Mask ((uint16_t)0xFFF0) /*!< USART address Mask */
  28. #define CR2_LINEN_Set ((uint16_t)0x4000) /*!< USART LIN Enable Mask */
  29. #define CR2_LINEN_Reset ((uint16_t)0xBFFF) /*!< USART LIN Disable Mask */
  30. #define CR2_LBDL_Mask ((uint16_t)0xFFDF) /*!< USART LIN Break detection Mask */
  31. #define CR2_STOP_CLEAR_Mask ((uint16_t)0xCFFF) /*!< USART CR2 STOP Bits Mask */
  32. #define CR2_CLOCK_CLEAR_Mask ((uint16_t)0xF0FF) /*!< USART CR2 Clock Mask */
  33. #define CR3_SCEN_Set ((uint16_t)0x0020) /*!< USART SC Enable Mask */
  34. #define CR3_SCEN_Reset ((uint16_t)0xFFDF) /*!< USART SC Disable Mask */
  35. #define CR3_NACK_Set ((uint16_t)0x0010) /*!< USART SC NACK Enable Mask */
  36. #define CR3_NACK_Reset ((uint16_t)0xFFEF) /*!< USART SC NACK Disable Mask */
  37. #define CR3_HDSEL_Set ((uint16_t)0x0008) /*!< USART Half-Duplex Enable Mask */
  38. #define CR3_HDSEL_Reset ((uint16_t)0xFFF7) /*!< USART Half-Duplex Disable Mask */
  39. #define CR3_IRLP_Mask ((uint16_t)0xFFFB) /*!< USART IrDA LowPower mode Mask */
  40. #define CR3_CLEAR_Mask ((uint16_t)0xFCFF) /*!< USART CR3 Mask */
  41. #define CR3_IREN_Set ((uint16_t)0x0002) /*!< USART IrDA Enable Mask */
  42. #define CR3_IREN_Reset ((uint16_t)0xFFFD) /*!< USART IrDA Disable Mask */
  43. #define GTPR_LSB_Mask ((uint16_t)0x00FF) /*!< Guard Time Register LSB Mask */
  44. #define GTPR_MSB_Mask ((uint16_t)0xFF00) /*!< Guard Time Register MSB Mask */
  45. #define IT_Mask ((uint16_t)0x001F) /*!< USART Interrupt Mask */
  46. /* USART OverSampling-8 Mask */
  47. #define CR1_OVER8_Set ((u16)0x8000) /* USART OVER8 mode Enable Mask */
  48. #define CR1_OVER8_Reset ((u16)0x7FFF) /* USART OVER8 mode Disable Mask */
  49. /* USART One Bit Sampling Mask */
  50. #define CR3_ONEBITE_Set ((u16)0x0800) /* USART ONEBITE mode Enable Mask */
  51. #define CR3_ONEBITE_Reset ((u16)0xF7FF) /* USART ONEBITE mode Disable Mask */
  52. /**
  53. * @}
  54. */
  55. /** @defgroup USART_Private_Macros
  56. * @{
  57. */
  58. /**
  59. * @}
  60. */
  61. /** @defgroup USART_Private_Variables
  62. * @{
  63. */
  64. /**
  65. * @}
  66. */
  67. /** @defgroup USART_Private_FunctionPrototypes
  68. * @{
  69. */
  70. /**
  71. * @}
  72. */
  73. /** @defgroup USART_Private_Functions
  74. * @{
  75. */
  76. /**
  77. * @brief Deinitializes the USARTx peripheral registers to their default reset values.
  78. * @param USARTx: Select the USART or the UART peripheral.
  79. * This parameter can be one of the following values:
  80. * USART1, USART2, USART3, UART4 or UART5.
  81. * @retval None
  82. */
  83. void USART_DeInit(USART_TypeDef* USARTx)
  84. {
  85. /* Check the parameters */
  86. assert_param(IS_USART_ALL_PERIPH(USARTx));
  87. if (USARTx == USART1)
  88. {
  89. RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1, ENABLE);
  90. RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1, DISABLE);
  91. }
  92. else if (USARTx == USART2)
  93. {
  94. RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART2, ENABLE);
  95. RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART2, DISABLE);
  96. }
  97. else if (USARTx == USART3)
  98. {
  99. RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART3, ENABLE);
  100. RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART3, DISABLE);
  101. }
  102. else if (USARTx == UART4)
  103. {
  104. RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART4, ENABLE);
  105. RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART4, DISABLE);
  106. }
  107. else
  108. {
  109. if (USARTx == UART5)
  110. {
  111. RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART5, ENABLE);
  112. RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART5, DISABLE);
  113. }
  114. }
  115. }
  116. /**
  117. * @brief Initializes the USARTx peripheral according to the specified
  118. * parameters in the USART_InitStruct .
  119. * @param USARTx: Select the USART or the UART peripheral.
  120. * This parameter can be one of the following values:
  121. * USART1, USART2, USART3, UART4 or UART5.
  122. * @param USART_InitStruct: pointer to a USART_InitTypeDef structure
  123. * that contains the configuration information for the specified USART
  124. * peripheral.
  125. * @retval None
  126. */
  127. void USART_Init(USART_TypeDef* USARTx, USART_InitTypeDef* USART_InitStruct)
  128. {
  129. uint32_t tmpreg = 0x00, apbclock = 0x00;
  130. uint32_t integerdivider = 0x00;
  131. uint32_t fractionaldivider = 0x00;
  132. uint32_t usartxbase = 0;
  133. RCC_ClocksTypeDef RCC_ClocksStatus;
  134. /* Check the parameters */
  135. assert_param(IS_USART_ALL_PERIPH(USARTx));
  136. assert_param(IS_USART_BAUDRATE(USART_InitStruct->USART_BaudRate));
  137. assert_param(IS_USART_WORD_LENGTH(USART_InitStruct->USART_WordLength));
  138. assert_param(IS_USART_STOPBITS(USART_InitStruct->USART_StopBits));
  139. assert_param(IS_USART_PARITY(USART_InitStruct->USART_Parity));
  140. assert_param(IS_USART_MODE(USART_InitStruct->USART_Mode));
  141. assert_param(IS_USART_HARDWARE_FLOW_CONTROL(USART_InitStruct->USART_HardwareFlowControl));
  142. /* The hardware flow control is available only for USART1, USART2 and USART3 */
  143. if (USART_InitStruct->USART_HardwareFlowControl != USART_HardwareFlowControl_None)
  144. {
  145. assert_param(IS_USART_123_PERIPH(USARTx));
  146. }
  147. usartxbase = (uint32_t)USARTx;
  148. /*---------------------------- USART CR2 Configuration -----------------------*/
  149. tmpreg = USARTx->CR2;
  150. /* Clear STOP[13:12] bits */
  151. tmpreg &= CR2_STOP_CLEAR_Mask;
  152. /* Configure the USART Stop Bits, Clock, CPOL, CPHA and LastBit ------------*/
  153. /* Set STOP[13:12] bits according to USART_StopBits value */
  154. tmpreg |= (uint32_t)USART_InitStruct->USART_StopBits;
  155. /* Write to USART CR2 */
  156. USARTx->CR2 = (uint16_t)tmpreg;
  157. /*---------------------------- USART CR1 Configuration -----------------------*/
  158. tmpreg = USARTx->CR1;
  159. /* Clear M, PCE, PS, TE and RE bits */
  160. tmpreg &= CR1_CLEAR_Mask;
  161. /* Configure the USART Word Length, Parity and mode ----------------------- */
  162. /* Set the M bits according to USART_WordLength value */
  163. /* Set PCE and PS bits according to USART_Parity value */
  164. /* Set TE and RE bits according to USART_Mode value */
  165. tmpreg |= (uint32_t)USART_InitStruct->USART_WordLength | USART_InitStruct->USART_Parity |
  166. USART_InitStruct->USART_Mode;
  167. /* Write to USART CR1 */
  168. USARTx->CR1 = (uint16_t)tmpreg;
  169. /*---------------------------- USART CR3 Configuration -----------------------*/
  170. tmpreg = USARTx->CR3;
  171. /* Clear CTSE and RTSE bits */
  172. tmpreg &= CR3_CLEAR_Mask;
  173. /* Configure the USART HFC -------------------------------------------------*/
  174. /* Set CTSE and RTSE bits according to USART_HardwareFlowControl value */
  175. tmpreg |= USART_InitStruct->USART_HardwareFlowControl;
  176. /* Write to USART CR3 */
  177. USARTx->CR3 = (uint16_t)tmpreg;
  178. /*---------------------------- USART BRR Configuration -----------------------*/
  179. /* Configure the USART Baud Rate -------------------------------------------*/
  180. RCC_GetClocksFreq(&RCC_ClocksStatus);
  181. if (usartxbase == USART1_BASE)
  182. {
  183. apbclock = RCC_ClocksStatus.PCLK2_Frequency/100;
  184. }
  185. else
  186. {
  187. apbclock = RCC_ClocksStatus.PCLK1_Frequency/100;
  188. }
  189. /* Determine the integer part */
  190. if ((USARTx->CR1 & CR1_OVER8_Set) != 0)
  191. {
  192. /* Integer part computing in case Oversampling mode is 8 Samples */
  193. integerdivider = ( (25 * apbclock)/ (2 * (USART_InitStruct->USART_BaudRate/100)));
  194. }
  195. else /* if ((USARTx->CR1 & CR1_OVER8_Set) == 0) */
  196. {
  197. /* Integer part computing in case Oversampling mode is 16 Samples */
  198. integerdivider = ( (25 * apbclock) / (4 * (USART_InitStruct->USART_BaudRate/100)));
  199. }
  200. tmpreg = (integerdivider / 100) << 4;
  201. /* Determine the fractional part */
  202. fractionaldivider = integerdivider - (100 * (tmpreg >> 4));
  203. /* Implement the fractional part in the register */
  204. if ((USARTx->CR1 & CR1_OVER8_Set) != 0)
  205. {
  206. tmpreg |= ((((fractionaldivider * 8) + 50) / 100)) & ((uint8_t)0x07);
  207. }
  208. else /* if ((USARTx->CR1 & CR1_OVER8_Set) == 0) */
  209. {
  210. tmpreg |= ((((fractionaldivider * 16) + 50) / 100)) & ((uint8_t)0x0F);
  211. }
  212. /* Write to USART BRR */
  213. USARTx->BRR = (uint16_t)tmpreg;
  214. }
  215. /**
  216. * @brief Fills each USART_InitStruct member with its default value.
  217. * @param USART_InitStruct: pointer to a USART_InitTypeDef structure
  218. * which will be initialized.
  219. * @retval None
  220. */
  221. void USART_StructInit(USART_InitTypeDef* USART_InitStruct)
  222. {
  223. /* USART_InitStruct members default value */
  224. USART_InitStruct->USART_BaudRate = 9600;
  225. USART_InitStruct->USART_WordLength = USART_WordLength_8b;
  226. USART_InitStruct->USART_StopBits = USART_StopBits_1;
  227. USART_InitStruct->USART_Parity = USART_Parity_No ;
  228. USART_InitStruct->USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  229. USART_InitStruct->USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  230. }
  231. /**
  232. * @brief Initializes the USARTx peripheral Clock according to the
  233. * specified parameters in the USART_ClockInitStruct .
  234. * @param USARTx: where x can be 1, 2, 3 to select the USART peripheral.
  235. * @param USART_ClockInitStruct: pointer to a USART_ClockInitTypeDef
  236. * structure that contains the configuration information for the specified
  237. * USART peripheral.
  238. * @note The Smart Card and Synchronous modes are not available for UART4 and UART5.
  239. * @retval None
  240. */
  241. void USART_ClockInit(USART_TypeDef* USARTx, USART_ClockInitTypeDef* USART_ClockInitStruct)
  242. {
  243. uint32_t tmpreg = 0x00;
  244. /* Check the parameters */
  245. assert_param(IS_USART_123_PERIPH(USARTx));
  246. assert_param(IS_USART_CLOCK(USART_ClockInitStruct->USART_Clock));
  247. assert_param(IS_USART_CPOL(USART_ClockInitStruct->USART_CPOL));
  248. assert_param(IS_USART_CPHA(USART_ClockInitStruct->USART_CPHA));
  249. assert_param(IS_USART_LASTBIT(USART_ClockInitStruct->USART_LastBit));
  250. /*---------------------------- USART CR2 Configuration -----------------------*/
  251. tmpreg = USARTx->CR2;
  252. /* Clear CLKEN, CPOL, CPHA and LBCL bits */
  253. tmpreg &= CR2_CLOCK_CLEAR_Mask;
  254. /* Configure the USART Clock, CPOL, CPHA and LastBit ------------*/
  255. /* Set CLKEN bit according to USART_Clock value */
  256. /* Set CPOL bit according to USART_CPOL value */
  257. /* Set CPHA bit according to USART_CPHA value */
  258. /* Set LBCL bit according to USART_LastBit value */
  259. tmpreg |= (uint32_t)USART_ClockInitStruct->USART_Clock | USART_ClockInitStruct->USART_CPOL |
  260. USART_ClockInitStruct->USART_CPHA | USART_ClockInitStruct->USART_LastBit;
  261. /* Write to USART CR2 */
  262. USARTx->CR2 = (uint16_t)tmpreg;
  263. }
  264. /**
  265. * @brief Fills each USART_ClockInitStruct member with its default value.
  266. * @param USART_ClockInitStruct: pointer to a USART_ClockInitTypeDef
  267. * structure which will be initialized.
  268. * @retval None
  269. */
  270. void USART_ClockStructInit(USART_ClockInitTypeDef* USART_ClockInitStruct)
  271. {
  272. /* USART_ClockInitStruct members default value */
  273. USART_ClockInitStruct->USART_Clock = USART_Clock_Disable;
  274. USART_ClockInitStruct->USART_CPOL = USART_CPOL_Low;
  275. USART_ClockInitStruct->USART_CPHA = USART_CPHA_1Edge;
  276. USART_ClockInitStruct->USART_LastBit = USART_LastBit_Disable;
  277. }
  278. /**
  279. * @brief Enables or disables the specified USART peripheral.
  280. * @param USARTx: Select the USART or the UART peripheral.
  281. * This parameter can be one of the following values:
  282. * USART1, USART2, USART3, UART4 or UART5.
  283. * @param NewState: new state of the USARTx peripheral.
  284. * This parameter can be: ENABLE or DISABLE.
  285. * @retval None
  286. */
  287. void USART_Cmd(USART_TypeDef* USARTx, FunctionalState NewState)
  288. {
  289. /* Check the parameters */
  290. assert_param(IS_USART_ALL_PERIPH(USARTx));
  291. assert_param(IS_FUNCTIONAL_STATE(NewState));
  292. if (NewState != DISABLE)
  293. {
  294. /* Enable the selected USART by setting the UE bit in the CR1 register */
  295. USARTx->CR1 |= CR1_UE_Set;
  296. }
  297. else
  298. {
  299. /* Disable the selected USART by clearing the UE bit in the CR1 register */
  300. USARTx->CR1 &= CR1_UE_Reset;
  301. }
  302. }
  303. /**
  304. * @brief Enables or disables the specified USART interrupts.
  305. * @param USARTx: Select the USART or the UART peripheral.
  306. * This parameter can be one of the following values:
  307. * USART1, USART2, USART3, UART4 or UART5.
  308. * @param USART_IT: specifies the USART interrupt sources to be enabled or disabled.
  309. * This parameter can be one of the following values:
  310. * @arg USART_IT_CTS: CTS change interrupt (not available for UART4 and UART5)
  311. * @arg USART_IT_LBD: LIN Break detection interrupt
  312. * @arg USART_IT_TXE: Transmit Data Register empty interrupt
  313. * @arg USART_IT_TC: Transmission complete interrupt
  314. * @arg USART_IT_RXNE: Receive Data register not empty interrupt
  315. * @arg USART_IT_IDLE: Idle line detection interrupt
  316. * @arg USART_IT_PE: Parity Error interrupt
  317. * @arg USART_IT_ERR: Error interrupt(Frame error, noise error, overrun error)
  318. * @param NewState: new state of the specified USARTx interrupts.
  319. * This parameter can be: ENABLE or DISABLE.
  320. * @retval None
  321. */
  322. void USART_ITConfig(USART_TypeDef* USARTx, uint16_t USART_IT, FunctionalState NewState)
  323. {
  324. uint32_t usartreg = 0x00, itpos = 0x00, itmask = 0x00;
  325. uint32_t usartxbase = 0x00;
  326. /* Check the parameters */
  327. assert_param(IS_USART_ALL_PERIPH(USARTx));
  328. assert_param(IS_USART_CONFIG_IT(USART_IT));
  329. assert_param(IS_FUNCTIONAL_STATE(NewState));
  330. /* The CTS interrupt is not available for UART4 and UART5 */
  331. if (USART_IT == USART_IT_CTS)
  332. {
  333. assert_param(IS_USART_123_PERIPH(USARTx));
  334. }
  335. usartxbase = (uint32_t)USARTx;
  336. /* Get the USART register index */
  337. usartreg = (((uint8_t)USART_IT) >> 0x05);
  338. /* Get the interrupt position */
  339. itpos = USART_IT & IT_Mask;
  340. itmask = (((uint32_t)0x01) << itpos);
  341. if (usartreg == 0x01) /* The IT is in CR1 register */
  342. {
  343. usartxbase += 0x0C;
  344. }
  345. else if (usartreg == 0x02) /* The IT is in CR2 register */
  346. {
  347. usartxbase += 0x10;
  348. }
  349. else /* The IT is in CR3 register */
  350. {
  351. usartxbase += 0x14;
  352. }
  353. if (NewState != DISABLE)
  354. {
  355. *(__IO uint32_t*)usartxbase |= itmask;
  356. }
  357. else
  358. {
  359. *(__IO uint32_t*)usartxbase &= ~itmask;
  360. }
  361. }
  362. /**
  363. * @brief Enables or disables the USART’s DMA interface.
  364. * @param USARTx: Select the USART or the UART peripheral.
  365. * This parameter can be one of the following values:
  366. * USART1, USART2, USART3, UART4 or UART5.
  367. * @param USART_DMAReq: specifies the DMA request.
  368. * This parameter can be any combination of the following values:
  369. * @arg USART_DMAReq_Tx: USART DMA transmit request
  370. * @arg USART_DMAReq_Rx: USART DMA receive request
  371. * @param NewState: new state of the DMA Request sources.
  372. * This parameter can be: ENABLE or DISABLE.
  373. * @retval None
  374. */
  375. void USART_DMACmd(USART_TypeDef* USARTx, uint16_t USART_DMAReq, FunctionalState NewState)
  376. {
  377. /* Check the parameters */
  378. assert_param(IS_USART_ALL_PERIPH(USARTx));
  379. assert_param(IS_USART_DMAREQ(USART_DMAReq));
  380. assert_param(IS_FUNCTIONAL_STATE(NewState));
  381. if (NewState != DISABLE)
  382. {
  383. /* Enable the DMA transfer for selected requests by setting the DMAT and/or
  384. DMAR bits in the USART CR3 register */
  385. USARTx->CR3 |= USART_DMAReq;
  386. }
  387. else
  388. {
  389. /* Disable the DMA transfer for selected requests by clearing the DMAT and/or
  390. DMAR bits in the USART CR3 register */
  391. USARTx->CR3 &= (uint16_t)~USART_DMAReq;
  392. }
  393. }
  394. /**
  395. * @brief Sets the address of the USART node.
  396. * @param USARTx: Select the USART or the UART peripheral.
  397. * This parameter can be one of the following values:
  398. * USART1, USART2, USART3, UART4 or UART5.
  399. * @param USART_Address: Indicates the address of the USART node.
  400. * @retval None
  401. */
  402. void USART_SetAddress(USART_TypeDef* USARTx, uint8_t USART_Address)
  403. {
  404. /* Check the parameters */
  405. assert_param(IS_USART_ALL_PERIPH(USARTx));
  406. assert_param(IS_USART_ADDRESS(USART_Address));
  407. /* Clear the USART address */
  408. USARTx->CR2 &= CR2_Address_Mask;
  409. /* Set the USART address node */
  410. USARTx->CR2 |= USART_Address;
  411. }
  412. /**
  413. * @brief Selects the USART WakeUp method.
  414. * @param USARTx: Select the USART or the UART peripheral.
  415. * This parameter can be one of the following values:
  416. * USART1, USART2, USART3, UART4 or UART5.
  417. * @param USART_WakeUp: specifies the USART wakeup method.
  418. * This parameter can be one of the following values:
  419. * @arg USART_WakeUp_IdleLine: WakeUp by an idle line detection
  420. * @arg USART_WakeUp_AddressMark: WakeUp by an address mark
  421. * @retval None
  422. */
  423. void USART_WakeUpConfig(USART_TypeDef* USARTx, uint16_t USART_WakeUp)
  424. {
  425. /* Check the parameters */
  426. assert_param(IS_USART_ALL_PERIPH(USARTx));
  427. assert_param(IS_USART_WAKEUP(USART_WakeUp));
  428. USARTx->CR1 &= CR1_WAKE_Mask;
  429. USARTx->CR1 |= USART_WakeUp;
  430. }
  431. /**
  432. * @brief Determines if the USART is in mute mode or not.
  433. * @param USARTx: Select the USART or the UART peripheral.
  434. * This parameter can be one of the following values:
  435. * USART1, USART2, USART3, UART4 or UART5.
  436. * @param NewState: new state of the USART mute mode.
  437. * This parameter can be: ENABLE or DISABLE.
  438. * @retval None
  439. */
  440. void USART_ReceiverWakeUpCmd(USART_TypeDef* USARTx, FunctionalState NewState)
  441. {
  442. /* Check the parameters */
  443. assert_param(IS_USART_ALL_PERIPH(USARTx));
  444. assert_param(IS_FUNCTIONAL_STATE(NewState));
  445. if (NewState != DISABLE)
  446. {
  447. /* Enable the USART mute mode by setting the RWU bit in the CR1 register */
  448. USARTx->CR1 |= CR1_RWU_Set;
  449. }
  450. else
  451. {
  452. /* Disable the USART mute mode by clearing the RWU bit in the CR1 register */
  453. USARTx->CR1 &= CR1_RWU_Reset;
  454. }
  455. }
  456. /**
  457. * @brief Sets the USART LIN Break detection length.
  458. * @param USARTx: Select the USART or the UART peripheral.
  459. * This parameter can be one of the following values:
  460. * USART1, USART2, USART3, UART4 or UART5.
  461. * @param USART_LINBreakDetectLength: specifies the LIN break detection length.
  462. * This parameter can be one of the following values:
  463. * @arg USART_LINBreakDetectLength_10b: 10-bit break detection
  464. * @arg USART_LINBreakDetectLength_11b: 11-bit break detection
  465. * @retval None
  466. */
  467. void USART_LINBreakDetectLengthConfig(USART_TypeDef* USARTx, uint16_t USART_LINBreakDetectLength)
  468. {
  469. /* Check the parameters */
  470. assert_param(IS_USART_ALL_PERIPH(USARTx));
  471. assert_param(IS_USART_LIN_BREAK_DETECT_LENGTH(USART_LINBreakDetectLength));
  472. USARTx->CR2 &= CR2_LBDL_Mask;
  473. USARTx->CR2 |= USART_LINBreakDetectLength;
  474. }
  475. /**
  476. * @brief Enables or disables the USART’s LIN mode.
  477. * @param USARTx: Select the USART or the UART peripheral.
  478. * This parameter can be one of the following values:
  479. * USART1, USART2, USART3, UART4 or UART5.
  480. * @param NewState: new state of the USART LIN mode.
  481. * This parameter can be: ENABLE or DISABLE.
  482. * @retval None
  483. */
  484. void USART_LINCmd(USART_TypeDef* USARTx, FunctionalState NewState)
  485. {
  486. /* Check the parameters */
  487. assert_param(IS_USART_ALL_PERIPH(USARTx));
  488. assert_param(IS_FUNCTIONAL_STATE(NewState));
  489. if (NewState != DISABLE)
  490. {
  491. /* Enable the LIN mode by setting the LINEN bit in the CR2 register */
  492. USARTx->CR2 |= CR2_LINEN_Set;
  493. }
  494. else
  495. {
  496. /* Disable the LIN mode by clearing the LINEN bit in the CR2 register */
  497. USARTx->CR2 &= CR2_LINEN_Reset;
  498. }
  499. }
  500. /**
  501. * @brief Transmits single data through the USARTx peripheral.
  502. * @param USARTx: Select the USART or the UART peripheral.
  503. * This parameter can be one of the following values:
  504. * USART1, USART2, USART3, UART4 or UART5.
  505. * @param Data: the data to transmit.
  506. * @retval None
  507. */
  508. void USART_SendData(USART_TypeDef* USARTx, uint16_t Data)
  509. {
  510. /* Check the parameters */
  511. assert_param(IS_USART_ALL_PERIPH(USARTx));
  512. assert_param(IS_USART_DATA(Data));
  513. /* Transmit Data */
  514. USARTx->DR = (Data & (uint16_t)0x01FF);
  515. }
  516. /**
  517. * @brief Returns the most recent received data by the USARTx peripheral.
  518. * @param USARTx: Select the USART or the UART peripheral.
  519. * This parameter can be one of the following values:
  520. * USART1, USART2, USART3, UART4 or UART5.
  521. * @retval The received data.
  522. */
  523. uint16_t USART_ReceiveData(USART_TypeDef* USARTx)
  524. {
  525. /* Check the parameters */
  526. assert_param(IS_USART_ALL_PERIPH(USARTx));
  527. /* Receive Data */
  528. return (uint16_t)(USARTx->DR & (uint16_t)0x01FF);
  529. }
  530. /**
  531. * @brief Transmits break characters.
  532. * @param USARTx: Select the USART or the UART peripheral.
  533. * This parameter can be one of the following values:
  534. * USART1, USART2, USART3, UART4 or UART5.
  535. * @retval None
  536. */
  537. void USART_SendBreak(USART_TypeDef* USARTx)
  538. {
  539. /* Check the parameters */
  540. assert_param(IS_USART_ALL_PERIPH(USARTx));
  541. /* Send break characters */
  542. USARTx->CR1 |= CR1_SBK_Set;
  543. }
  544. /**
  545. * @brief Sets the specified USART guard time.
  546. * @param USARTx: where x can be 1, 2 or 3 to select the USART peripheral.
  547. * @param USART_GuardTime: specifies the guard time.
  548. * @note The guard time bits are not available for UART4 and UART5.
  549. * @retval None
  550. */
  551. void USART_SetGuardTime(USART_TypeDef* USARTx, uint8_t USART_GuardTime)
  552. {
  553. /* Check the parameters */
  554. assert_param(IS_USART_123_PERIPH(USARTx));
  555. /* Clear the USART Guard time */
  556. USARTx->GTPR &= GTPR_LSB_Mask;
  557. /* Set the USART guard time */
  558. USARTx->GTPR |= (uint16_t)((uint16_t)USART_GuardTime << 0x08);
  559. }
  560. /**
  561. * @brief Sets the system clock prescaler.
  562. * @param USARTx: Select the USART or the UART peripheral.
  563. * This parameter can be one of the following values:
  564. * USART1, USART2, USART3, UART4 or UART5.
  565. * @param USART_Prescaler: specifies the prescaler clock.
  566. * @note The function is used for IrDA mode with UART4 and UART5.
  567. * @retval None
  568. */
  569. void USART_SetPrescaler(USART_TypeDef* USARTx, uint8_t USART_Prescaler)
  570. {
  571. /* Check the parameters */
  572. assert_param(IS_USART_ALL_PERIPH(USARTx));
  573. /* Clear the USART prescaler */
  574. USARTx->GTPR &= GTPR_MSB_Mask;
  575. /* Set the USART prescaler */
  576. USARTx->GTPR |= USART_Prescaler;
  577. }
  578. /**
  579. * @brief Enables or disables the USART’s Smart Card mode.
  580. * @param USARTx: where x can be 1, 2 or 3 to select the USART peripheral.
  581. * @param NewState: new state of the Smart Card mode.
  582. * This parameter can be: ENABLE or DISABLE.
  583. * @note The Smart Card mode is not available for UART4 and UART5.
  584. * @retval None
  585. */
  586. void USART_SmartCardCmd(USART_TypeDef* USARTx, FunctionalState NewState)
  587. {
  588. /* Check the parameters */
  589. assert_param(IS_USART_123_PERIPH(USARTx));
  590. assert_param(IS_FUNCTIONAL_STATE(NewState));
  591. if (NewState != DISABLE)
  592. {
  593. /* Enable the SC mode by setting the SCEN bit in the CR3 register */
  594. USARTx->CR3 |= CR3_SCEN_Set;
  595. }
  596. else
  597. {
  598. /* Disable the SC mode by clearing the SCEN bit in the CR3 register */
  599. USARTx->CR3 &= CR3_SCEN_Reset;
  600. }
  601. }
  602. /**
  603. * @brief Enables or disables NACK transmission.
  604. * @param USARTx: where x can be 1, 2 or 3 to select the USART peripheral.
  605. * @param NewState: new state of the NACK transmission.
  606. * This parameter can be: ENABLE or DISABLE.
  607. * @note The Smart Card mode is not available for UART4 and UART5.
  608. * @retval None
  609. */
  610. void USART_SmartCardNACKCmd(USART_TypeDef* USARTx, FunctionalState NewState)
  611. {
  612. /* Check the parameters */
  613. assert_param(IS_USART_123_PERIPH(USARTx));
  614. assert_param(IS_FUNCTIONAL_STATE(NewState));
  615. if (NewState != DISABLE)
  616. {
  617. /* Enable the NACK transmission by setting the NACK bit in the CR3 register */
  618. USARTx->CR3 |= CR3_NACK_Set;
  619. }
  620. else
  621. {
  622. /* Disable the NACK transmission by clearing the NACK bit in the CR3 register */
  623. USARTx->CR3 &= CR3_NACK_Reset;
  624. }
  625. }
  626. /**
  627. * @brief Enables or disables the USART’s Half Duplex communication.
  628. * @param USARTx: Select the USART or the UART peripheral.
  629. * This parameter can be one of the following values:
  630. * USART1, USART2, USART3, UART4 or UART5.
  631. * @param NewState: new state of the USART Communication.
  632. * This parameter can be: ENABLE or DISABLE.
  633. * @retval None
  634. */
  635. void USART_HalfDuplexCmd(USART_TypeDef* USARTx, FunctionalState NewState)
  636. {
  637. /* Check the parameters */
  638. assert_param(IS_USART_ALL_PERIPH(USARTx));
  639. assert_param(IS_FUNCTIONAL_STATE(NewState));
  640. if (NewState != DISABLE)
  641. {
  642. /* Enable the Half-Duplex mode by setting the HDSEL bit in the CR3 register */
  643. USARTx->CR3 |= CR3_HDSEL_Set;
  644. }
  645. else
  646. {
  647. /* Disable the Half-Duplex mode by clearing the HDSEL bit in the CR3 register */
  648. USARTx->CR3 &= CR3_HDSEL_Reset;
  649. }
  650. }
  651. /**
  652. * @brief Enables or disables the USART's 8x oversampling mode.
  653. * @param USARTx: Select the USART or the UART peripheral.
  654. * This parameter can be one of the following values:
  655. * USART1, USART2, USART3, UART4 or UART5.
  656. * @param NewState: new state of the USART one bit sampling method.
  657. * This parameter can be: ENABLE or DISABLE.
  658. * @note
  659. * This function has to be called before calling USART_Init()
  660. * function in order to have correct baudrate Divider value.
  661. * @retval None
  662. */
  663. void USART_OverSampling8Cmd(USART_TypeDef* USARTx, FunctionalState NewState)
  664. {
  665. /* Check the parameters */
  666. assert_param(IS_USART_ALL_PERIPH(USARTx));
  667. assert_param(IS_FUNCTIONAL_STATE(NewState));
  668. if (NewState != DISABLE)
  669. {
  670. /* Enable the 8x Oversampling mode by setting the OVER8 bit in the CR1 register */
  671. USARTx->CR1 |= CR1_OVER8_Set;
  672. }
  673. else
  674. {
  675. /* Disable the 8x Oversampling mode by clearing the OVER8 bit in the CR1 register */
  676. USARTx->CR1 &= CR1_OVER8_Reset;
  677. }
  678. }
  679. /**
  680. * @brief Enables or disables the USART's one bit sampling method.
  681. * @param USARTx: Select the USART or the UART peripheral.
  682. * This parameter can be one of the following values:
  683. * USART1, USART2, USART3, UART4 or UART5.
  684. * @param NewState: new state of the USART one bit sampling method.
  685. * This parameter can be: ENABLE or DISABLE.
  686. * @retval None
  687. */
  688. void USART_OneBitMethodCmd(USART_TypeDef* USARTx, FunctionalState NewState)
  689. {
  690. /* Check the parameters */
  691. assert_param(IS_USART_ALL_PERIPH(USARTx));
  692. assert_param(IS_FUNCTIONAL_STATE(NewState));
  693. if (NewState != DISABLE)
  694. {
  695. /* Enable the one bit method by setting the ONEBITE bit in the CR3 register */
  696. USARTx->CR3 |= CR3_ONEBITE_Set;
  697. }
  698. else
  699. {
  700. /* Disable tthe one bit method by clearing the ONEBITE bit in the CR3 register */
  701. USARTx->CR3 &= CR3_ONEBITE_Reset;
  702. }
  703. }
  704. /**
  705. * @brief Configures the USART's IrDA interface.
  706. * @param USARTx: Select the USART or the UART peripheral.
  707. * This parameter can be one of the following values:
  708. * USART1, USART2, USART3, UART4 or UART5.
  709. * @param USART_IrDAMode: specifies the IrDA mode.
  710. * This parameter can be one of the following values:
  711. * @arg USART_IrDAMode_LowPower
  712. * @arg USART_IrDAMode_Normal
  713. * @retval None
  714. */
  715. void USART_IrDAConfig(USART_TypeDef* USARTx, uint16_t USART_IrDAMode)
  716. {
  717. /* Check the parameters */
  718. assert_param(IS_USART_ALL_PERIPH(USARTx));
  719. assert_param(IS_USART_IRDA_MODE(USART_IrDAMode));
  720. USARTx->CR3 &= CR3_IRLP_Mask;
  721. USARTx->CR3 |= USART_IrDAMode;
  722. }
  723. /**
  724. * @brief Enables or disables the USART's IrDA interface.
  725. * @param USARTx: Select the USART or the UART peripheral.
  726. * This parameter can be one of the following values:
  727. * USART1, USART2, USART3, UART4 or UART5.
  728. * @param NewState: new state of the IrDA mode.
  729. * This parameter can be: ENABLE or DISABLE.
  730. * @retval None
  731. */
  732. void USART_IrDACmd(USART_TypeDef* USARTx, FunctionalState NewState)
  733. {
  734. /* Check the parameters */
  735. assert_param(IS_USART_ALL_PERIPH(USARTx));
  736. assert_param(IS_FUNCTIONAL_STATE(NewState));
  737. if (NewState != DISABLE)
  738. {
  739. /* Enable the IrDA mode by setting the IREN bit in the CR3 register */
  740. USARTx->CR3 |= CR3_IREN_Set;
  741. }
  742. else
  743. {
  744. /* Disable the IrDA mode by clearing the IREN bit in the CR3 register */
  745. USARTx->CR3 &= CR3_IREN_Reset;
  746. }
  747. }
  748. /**
  749. * @brief Checks whether the specified USART flag is set or not.
  750. * @param USARTx: Select the USART or the UART peripheral.
  751. * This parameter can be one of the following values:
  752. * USART1, USART2, USART3, UART4 or UART5.
  753. * @param USART_FLAG: specifies the flag to check.
  754. * This parameter can be one of the following values:
  755. * @arg USART_FLAG_CTS: CTS Change flag (not available for UART4 and UART5)
  756. * @arg USART_FLAG_LBD: LIN Break detection flag
  757. * @arg USART_FLAG_TXE: Transmit data register empty flag
  758. * @arg USART_FLAG_TC: Transmission Complete flag
  759. * @arg USART_FLAG_RXNE: Receive data register not empty flag
  760. * @arg USART_FLAG_IDLE: Idle Line detection flag
  761. * @arg USART_FLAG_ORE: OverRun Error flag
  762. * @arg USART_FLAG_NE: Noise Error flag
  763. * @arg USART_FLAG_FE: Framing Error flag
  764. * @arg USART_FLAG_PE: Parity Error flag
  765. * @retval The new state of USART_FLAG (SET or RESET).
  766. */
  767. FlagStatus USART_GetFlagStatus(USART_TypeDef* USARTx, uint16_t USART_FLAG)
  768. {
  769. FlagStatus bitstatus = RESET;
  770. /* Check the parameters */
  771. assert_param(IS_USART_ALL_PERIPH(USARTx));
  772. assert_param(IS_USART_FLAG(USART_FLAG));
  773. /* The CTS flag is not available for UART4 and UART5 */
  774. if (USART_FLAG == USART_FLAG_CTS)
  775. {
  776. assert_param(IS_USART_123_PERIPH(USARTx));
  777. }
  778. if ((USARTx->SR & USART_FLAG) != (uint16_t)RESET)
  779. {
  780. bitstatus = SET;
  781. }
  782. else
  783. {
  784. bitstatus = RESET;
  785. }
  786. return bitstatus;
  787. }
  788. /**
  789. * @brief Clears the USARTx's pending flags.
  790. * @param USARTx: Select the USART or the UART peripheral.
  791. * This parameter can be one of the following values:
  792. * USART1, USART2, USART3, UART4 or UART5.
  793. * @param USART_FLAG: specifies the flag to clear.
  794. * This parameter can be any combination of the following values:
  795. * @arg USART_FLAG_CTS: CTS Change flag (not available for UART4 and UART5).
  796. * @arg USART_FLAG_LBD: LIN Break detection flag.
  797. * @arg USART_FLAG_TC: Transmission Complete flag.
  798. * @arg USART_FLAG_RXNE: Receive data register not empty flag.
  799. *
  800. * @note
  801. * - PE (Parity error), FE (Framing error), NE (Noise error), ORE (OverRun
  802. * error) and IDLE (Idle line detected) flags are cleared by software
  803. * sequence: a read operation to USART_SR register (USART_GetFlagStatus())
  804. * followed by a read operation to USART_DR register (USART_ReceiveData()).
  805. * - RXNE flag can be also cleared by a read to the USART_DR register
  806. * (USART_ReceiveData()).
  807. * - TC flag can be also cleared by software sequence: a read operation to
  808. * USART_SR register (USART_GetFlagStatus()) followed by a write operation
  809. * to USART_DR register (USART_SendData()).
  810. * - TXE flag is cleared only by a write to the USART_DR register
  811. * (USART_SendData()).
  812. * @retval None
  813. */
  814. void USART_ClearFlag(USART_TypeDef* USARTx, uint16_t USART_FLAG)
  815. {
  816. /* Check the parameters */
  817. assert_param(IS_USART_ALL_PERIPH(USARTx));
  818. assert_param(IS_USART_CLEAR_FLAG(USART_FLAG));
  819. /* The CTS flag is not available for UART4 and UART5 */
  820. if ((USART_FLAG & USART_FLAG_CTS) == USART_FLAG_CTS)
  821. {
  822. assert_param(IS_USART_123_PERIPH(USARTx));
  823. }
  824. USARTx->SR = (uint16_t)~USART_FLAG;
  825. }
  826. /**
  827. * @brief Checks whether the specified USART interrupt has occurred or not.
  828. * @param USARTx: Select the USART or the UART peripheral.
  829. * This parameter can be one of the following values:
  830. * USART1, USART2, USART3, UART4 or UART5.
  831. * @param USART_IT: specifies the USART interrupt source to check.
  832. * This parameter can be one of the following values:
  833. * @arg USART_IT_CTS: CTS change interrupt (not available for UART4 and UART5)
  834. * @arg USART_IT_LBD: LIN Break detection interrupt
  835. * @arg USART_IT_TXE: Tansmit Data Register empty interrupt
  836. * @arg USART_IT_TC: Transmission complete interrupt
  837. * @arg USART_IT_RXNE: Receive Data register not empty interrupt
  838. * @arg USART_IT_IDLE: Idle line detection interrupt
  839. * @arg USART_IT_ORE: OverRun Error interrupt
  840. * @arg USART_IT_NE: Noise Error interrupt
  841. * @arg USART_IT_FE: Framing Error interrupt
  842. * @arg USART_IT_PE: Parity Error interrupt
  843. * @retval The new state of USART_IT (SET or RESET).
  844. */
  845. ITStatus USART_GetITStatus(USART_TypeDef* USARTx, uint16_t USART_IT)
  846. {
  847. uint32_t bitpos = 0x00, itmask = 0x00, usartreg = 0x00;
  848. ITStatus bitstatus = RESET;
  849. /* Check the parameters */
  850. assert_param(IS_USART_ALL_PERIPH(USARTx));
  851. assert_param(IS_USART_GET_IT(USART_IT));
  852. /* The CTS interrupt is not available for UART4 and UART5 */
  853. if (USART_IT == USART_IT_CTS)
  854. {
  855. assert_param(IS_USART_123_PERIPH(USARTx));
  856. }
  857. /* Get the USART register index */
  858. usartreg = (((uint8_t)USART_IT) >> 0x05);
  859. /* Get the interrupt position */
  860. itmask = USART_IT & IT_Mask;
  861. itmask = (uint32_t)0x01 << itmask;
  862. if (usartreg == 0x01) /* The IT is in CR1 register */
  863. {
  864. itmask &= USARTx->CR1;
  865. }
  866. else if (usartreg == 0x02) /* The IT is in CR2 register */
  867. {
  868. itmask &= USARTx->CR2;
  869. }
  870. else /* The IT is in CR3 register */
  871. {
  872. itmask &= USARTx->CR3;
  873. }
  874. bitpos = USART_IT >> 0x08;
  875. bitpos = (uint32_t)0x01 << bitpos;
  876. bitpos &= USARTx->SR;
  877. if ((itmask != (uint16_t)RESET)&&(bitpos != (uint16_t)RESET))
  878. {
  879. bitstatus = SET;
  880. }
  881. else
  882. {
  883. bitstatus = RESET;
  884. }
  885. return bitstatus;
  886. }
  887. /**
  888. * @brief Clears the USARTx's interrupt pending bits.
  889. * @param USARTx: Select the USART or the UART peripheral.
  890. * This parameter can be one of the following values:
  891. * USART1, USART2, USART3, UART4 or UART5.
  892. * @param USART_IT: specifies the interrupt pending bit to clear.
  893. * This parameter can be one of the following values:
  894. * @arg USART_IT_CTS: CTS change interrupt (not available for UART4 and UART5)
  895. * @arg USART_IT_LBD: LIN Break detection interrupt
  896. * @arg USART_IT_TC: Transmission complete interrupt.
  897. * @arg USART_IT_RXNE: Receive Data register not empty interrupt.
  898. *
  899. * @note
  900. * - PE (Parity error), FE (Framing error), NE (Noise error), ORE (OverRun
  901. * error) and IDLE (Idle line detected) pending bits are cleared by
  902. * software sequence: a read operation to USART_SR register
  903. * (USART_GetITStatus()) followed by a read operation to USART_DR register
  904. * (USART_ReceiveData()).
  905. * - RXNE pending bit can be also cleared by a read to the USART_DR register
  906. * (USART_ReceiveData()).
  907. * - TC pending bit can be also cleared by software sequence: a read
  908. * operation to USART_SR register (USART_GetITStatus()) followed by a write
  909. * operation to USART_DR register (USART_SendData()).
  910. * - TXE pending bit is cleared only by a write to the USART_DR register
  911. * (USART_SendData()).
  912. * @retval None
  913. */
  914. void USART_ClearITPendingBit(USART_TypeDef* USARTx, uint16_t USART_IT)
  915. {
  916. uint16_t bitpos = 0x00, itmask = 0x00;
  917. /* Check the parameters */
  918. assert_param(IS_USART_ALL_PERIPH(USARTx));
  919. assert_param(IS_USART_CLEAR_IT(USART_IT));
  920. /* The CTS interrupt is not available for UART4 and UART5 */
  921. if (USART_IT == USART_IT_CTS)
  922. {
  923. assert_param(IS_USART_123_PERIPH(USARTx));
  924. }
  925. bitpos = USART_IT >> 0x08;
  926. itmask = ((uint16_t)0x01 << (uint16_t)bitpos);
  927. USARTx->SR = (uint16_t)~itmask;
  928. }
  929. /**
  930. * @}
  931. */
  932. /**
  933. * @}
  934. */
  935. /**
  936. * @}
  937. */