ch32v30x_usart.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /********************************** (C) COPYRIGHT *******************************
  2. * File Name : ch32v30x_usart.h
  3. * Author : WCH
  4. * Version : V1.0.0
  5. * Date : 2024/03/06
  6. * Description : This file contains all the functions prototypes for the
  7. * USART firmware library.
  8. *********************************************************************************
  9. * Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
  10. * Attention: This software (modified or not) and binary are used for
  11. * microcontroller manufactured by Nanjing Qinheng Microelectronics.
  12. *******************************************************************************/
  13. #ifndef __CH32V30x_USART_H
  14. #define __CH32V30x_USART_H
  15. #ifdef __cplusplus
  16. extern "C" {
  17. #endif
  18. #include "ch32v30x.h"
  19. /* USART Init Structure definition */
  20. typedef struct
  21. {
  22. uint32_t USART_BaudRate; /* This member configures the USART communication baud rate.
  23. The baud rate is computed using the following formula:
  24. - IntegerDivider = ((PCLKx) / (16 * (USART_InitStruct->USART_BaudRate)))
  25. - FractionalDivider = ((IntegerDivider - ((u32) IntegerDivider)) * 16) + 0.5 */
  26. uint16_t USART_WordLength; /* Specifies the number of data bits transmitted or received in a frame.
  27. This parameter can be a value of @ref USART_Word_Length */
  28. uint16_t USART_StopBits; /* Specifies the number of stop bits transmitted.
  29. This parameter can be a value of @ref USART_Stop_Bits */
  30. uint16_t USART_Parity; /* Specifies the parity mode.
  31. This parameter can be a value of @ref USART_Parity
  32. @note When parity is enabled, the computed parity is inserted
  33. at the MSB position of the transmitted data (9th bit when
  34. the word length is set to 9 data bits; 8th bit when the
  35. word length is set to 8 data bits). */
  36. uint16_t USART_Mode; /* Specifies wether the Receive or Transmit mode is enabled or disabled.
  37. This parameter can be a value of @ref USART_Mode */
  38. uint16_t USART_HardwareFlowControl; /* Specifies wether the hardware flow control mode is enabled
  39. or disabled.
  40. This parameter can be a value of @ref USART_Hardware_Flow_Control */
  41. } USART_InitTypeDef;
  42. /* USART Clock Init Structure definition */
  43. typedef struct
  44. {
  45. uint16_t USART_Clock; /* Specifies whether the USART clock is enabled or disabled.
  46. This parameter can be a value of @ref USART_Clock */
  47. uint16_t USART_CPOL; /* Specifies the steady state value of the serial clock.
  48. This parameter can be a value of @ref USART_Clock_Polarity */
  49. uint16_t USART_CPHA; /* Specifies the clock transition on which the bit capture is made.
  50. This parameter can be a value of @ref USART_Clock_Phase */
  51. uint16_t USART_LastBit; /* Specifies whether the clock pulse corresponding to the last transmitted
  52. data bit (MSB) has to be output on the SCLK pin in synchronous mode.
  53. This parameter can be a value of @ref USART_Last_Bit */
  54. } USART_ClockInitTypeDef;
  55. /* USART_Word_Length */
  56. #define USART_WordLength_8b ((uint16_t)0x0000)
  57. #define USART_WordLength_9b ((uint16_t)0x1000)
  58. /* USART_Stop_Bits */
  59. #define USART_StopBits_1 ((uint16_t)0x0000)
  60. #define USART_StopBits_0_5 ((uint16_t)0x1000)
  61. #define USART_StopBits_2 ((uint16_t)0x2000)
  62. #define USART_StopBits_1_5 ((uint16_t)0x3000)
  63. /* USART_Parity */
  64. #define USART_Parity_No ((uint16_t)0x0000)
  65. #define USART_Parity_Even ((uint16_t)0x0400)
  66. #define USART_Parity_Odd ((uint16_t)0x0600)
  67. /* USART_Mode */
  68. #define USART_Mode_Rx ((uint16_t)0x0004)
  69. #define USART_Mode_Tx ((uint16_t)0x0008)
  70. /* USART_Hardware_Flow_Control */
  71. #define USART_HardwareFlowControl_None ((uint16_t)0x0000)
  72. #define USART_HardwareFlowControl_RTS ((uint16_t)0x0100)
  73. #define USART_HardwareFlowControl_CTS ((uint16_t)0x0200)
  74. #define USART_HardwareFlowControl_RTS_CTS ((uint16_t)0x0300)
  75. /* USART_Clock */
  76. #define USART_Clock_Disable ((uint16_t)0x0000)
  77. #define USART_Clock_Enable ((uint16_t)0x0800)
  78. /* USART_Clock_Polarity */
  79. #define USART_CPOL_Low ((uint16_t)0x0000)
  80. #define USART_CPOL_High ((uint16_t)0x0400)
  81. /* USART_Clock_Phase */
  82. #define USART_CPHA_1Edge ((uint16_t)0x0000)
  83. #define USART_CPHA_2Edge ((uint16_t)0x0200)
  84. /* USART_Last_Bit */
  85. #define USART_LastBit_Disable ((uint16_t)0x0000)
  86. #define USART_LastBit_Enable ((uint16_t)0x0100)
  87. /* USART_Interrupt_definition */
  88. #define USART_IT_PE ((uint16_t)0x0028)
  89. #define USART_IT_TXE ((uint16_t)0x0727)
  90. #define USART_IT_TC ((uint16_t)0x0626)
  91. #define USART_IT_RXNE ((uint16_t)0x0525)
  92. #define USART_IT_ORE_RX ((uint16_t)0x0325)
  93. #define USART_IT_IDLE ((uint16_t)0x0424)
  94. #define USART_IT_LBD ((uint16_t)0x0846)
  95. #define USART_IT_CTS ((uint16_t)0x096A)
  96. #define USART_IT_ERR ((uint16_t)0x0060)
  97. #define USART_IT_ORE_ER ((uint16_t)0x0360)
  98. #define USART_IT_NE ((uint16_t)0x0260)
  99. #define USART_IT_FE ((uint16_t)0x0160)
  100. #define USART_IT_ORE USART_IT_ORE_ER
  101. /* USART_DMA_Requests */
  102. #define USART_DMAReq_Tx ((uint16_t)0x0080)
  103. #define USART_DMAReq_Rx ((uint16_t)0x0040)
  104. /* USART_WakeUp_methods */
  105. #define USART_WakeUp_IdleLine ((uint16_t)0x0000)
  106. #define USART_WakeUp_AddressMark ((uint16_t)0x0800)
  107. /* USART_LIN_Break_Detection_Length */
  108. #define USART_LINBreakDetectLength_10b ((uint16_t)0x0000)
  109. #define USART_LINBreakDetectLength_11b ((uint16_t)0x0020)
  110. /* USART_IrDA_Low_Power */
  111. #define USART_IrDAMode_LowPower ((uint16_t)0x0004)
  112. #define USART_IrDAMode_Normal ((uint16_t)0x0000)
  113. /* USART_Flags */
  114. #define USART_FLAG_CTS ((uint16_t)0x0200)
  115. #define USART_FLAG_LBD ((uint16_t)0x0100)
  116. #define USART_FLAG_TXE ((uint16_t)0x0080)
  117. #define USART_FLAG_TC ((uint16_t)0x0040)
  118. #define USART_FLAG_RXNE ((uint16_t)0x0020)
  119. #define USART_FLAG_IDLE ((uint16_t)0x0010)
  120. #define USART_FLAG_ORE ((uint16_t)0x0008)
  121. #define USART_FLAG_NE ((uint16_t)0x0004)
  122. #define USART_FLAG_FE ((uint16_t)0x0002)
  123. #define USART_FLAG_PE ((uint16_t)0x0001)
  124. void USART_DeInit(USART_TypeDef* USARTx);
  125. void USART_Init(USART_TypeDef* USARTx, USART_InitTypeDef* USART_InitStruct);
  126. void USART_StructInit(USART_InitTypeDef* USART_InitStruct);
  127. void USART_ClockInit(USART_TypeDef* USARTx, USART_ClockInitTypeDef* USART_ClockInitStruct);
  128. void USART_ClockStructInit(USART_ClockInitTypeDef* USART_ClockInitStruct);
  129. void USART_Cmd(USART_TypeDef* USARTx, FunctionalState NewState);
  130. void USART_ITConfig(USART_TypeDef* USARTx, uint16_t USART_IT, FunctionalState NewState);
  131. void USART_DMACmd(USART_TypeDef* USARTx, uint16_t USART_DMAReq, FunctionalState NewState);
  132. void USART_SetAddress(USART_TypeDef* USARTx, uint8_t USART_Address);
  133. void USART_WakeUpConfig(USART_TypeDef* USARTx, uint16_t USART_WakeUp);
  134. void USART_ReceiverWakeUpCmd(USART_TypeDef* USARTx, FunctionalState NewState);
  135. void USART_LINBreakDetectLengthConfig(USART_TypeDef* USARTx, uint16_t USART_LINBreakDetectLength);
  136. void USART_LINCmd(USART_TypeDef* USARTx, FunctionalState NewState);
  137. void USART_SendData(USART_TypeDef* USARTx, uint16_t Data);
  138. uint16_t USART_ReceiveData(USART_TypeDef* USARTx);
  139. void USART_SendBreak(USART_TypeDef* USARTx);
  140. void USART_SetGuardTime(USART_TypeDef* USARTx, uint8_t USART_GuardTime);
  141. void USART_SetPrescaler(USART_TypeDef* USARTx, uint8_t USART_Prescaler);
  142. void USART_SmartCardCmd(USART_TypeDef* USARTx, FunctionalState NewState);
  143. void USART_SmartCardNACKCmd(USART_TypeDef* USARTx, FunctionalState NewState);
  144. void USART_HalfDuplexCmd(USART_TypeDef* USARTx, FunctionalState NewState);
  145. void USART_IrDAConfig(USART_TypeDef* USARTx, uint16_t USART_IrDAMode);
  146. void USART_IrDACmd(USART_TypeDef* USARTx, FunctionalState NewState);
  147. FlagStatus USART_GetFlagStatus(USART_TypeDef* USARTx, uint16_t USART_FLAG);
  148. void USART_ClearFlag(USART_TypeDef* USARTx, uint16_t USART_FLAG);
  149. ITStatus USART_GetITStatus(USART_TypeDef* USARTx, uint16_t USART_IT);
  150. void USART_ClearITPendingBit(USART_TypeDef* USARTx, uint16_t USART_IT);
  151. #ifdef __cplusplus
  152. }
  153. #endif
  154. #endif