yc_uart.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /*
  2. File Name : yc_uart.h
  3. Author : Yichip
  4. Version : V1.0
  5. Date : 2019/12/4
  6. Description : UART encapsulation.
  7. */
  8. #ifndef __YC_UART_H__
  9. #define __YC_UART_H__
  10. #include "yc3121.h"
  11. /** @def time of UART receive data time out intterrupt. real time = regvalue*48
  12. * @{
  13. */
  14. #define TIME_IT_TIMEOUT (uint16_t)0x01
  15. /** @defgroup USART_Mode
  16. * @{
  17. */
  18. #define Mode_Single_Line (1<<6)
  19. #define Mode_duplex (0<<6)
  20. #define IS_MODE(MODE) (((MODE) == Mode_Single_Line) ||\
  21. ((MODE) == Mode_duplex))
  22. /**
  23. * @}
  24. */
  25. /** @
  26. * @defgroup USART_DataBits
  27. */
  28. #define Databits_8b (0<<2)
  29. #define Databits_9b (1<<2)
  30. #define IS_USART_WORD_LENGTH(LENGTH) (((LENGTH) == Databits_8b) || \
  31. ((LENGTH) == Databits_9b))
  32. /**
  33. * @}
  34. */
  35. /** @defgroup USART_Stop_Bits
  36. * @{
  37. */
  38. #define StopBits_1 (0<<3)
  39. #define StopBits_2 (1<<3)
  40. #define IS_USART_STOPBITS(STOPBITS) (((STOPBITS) == StopBits_1) || \
  41. ((STOPBITS) == StopBits_2) )
  42. /**
  43. * @}
  44. */
  45. /** @defgroup USART_Hardware_Flow_Control
  46. * @{
  47. */
  48. #define FlowCtrl_None (0<<4)
  49. #define FlowCtrl_Enable (1<<4)
  50. #define IS_FlowCtrl(CONTROL) (((CONTROL) == FlowCtrl_None) || \
  51. ((CONTROL) == FlowCtrl_Enable))
  52. /**
  53. * @}
  54. */
  55. /** @defgroup UART_Interrupt_Type_definition
  56. * @{
  57. */
  58. #define UART_IT_TX 0x01
  59. #define UART_IT_RX 0x02
  60. #define IS_UART_IT(x) (x == UART_IT_TX)||(x == UART_IT_RX)
  61. /**
  62. * @}
  63. */
  64. /** @defgroup USART_Parity
  65. * @{
  66. */
  67. #define Parity_None (0<<1)
  68. #define Parity_Even (0<<1)
  69. #define Parity_Odd (1<<1)
  70. #define IS_PARITY(PARITY) (((PARITY) == Parity_Even) ||\
  71. ((PARITY) == Parity_None) ||\
  72. ((PARITY) == Parity_Odd))
  73. /**
  74. * @}
  75. */
  76. /** @defgroup USART_BaudRate
  77. * @{
  78. */
  79. #define IS_BAUDRATE(BAUDRATE) (((BAUDRATE) > 0x5B8) && ((BAUDRATE) < 0x0044AA21))
  80. /**
  81. * @}
  82. */
  83. typedef struct
  84. {
  85. uint8_t Mode; /*!< Specifies wether the Receive or Transmit mode is enabled or disabled.
  86. This parameter can be a value of @ref USART_Mode */
  87. uint32_t BaudRate; /*!< This member configures the USART communication baud rate. */
  88. uint8_t DataBits; /*!< Specifies the number of data bits transmitted or received in a frame.
  89. This parameter can be a value of @ref USART_DataBits */
  90. uint8_t StopBits; /*!< Specifies the number of stop bits transmitted.
  91. This parameter can be a value of @ref USART_Stop_Bits */
  92. uint8_t Parity; /*!< Specifies the parity mode.
  93. This parameter can be a value of @ref USART_Parity
  94. @note When parity is enabled, the computed parity is inserted
  95. at the MSB position of the transmitted data (9th bit when
  96. the word length is set to 9 data bits; 8th bit when the
  97. word length is set to 8 data bits). */
  98. uint8_t FlowCtrl; /*!< Specifies wether the hardware flow control mode is enabled or disabled.
  99. This parameter can be a value of @ref USART_Hardware_Flow_Control */
  100. int RxBufLen; /*!< Specifies uart DMA Rx buff length */
  101. } UART_InitTypeDef;
  102. /** @defgroup UART_TypeDef
  103. * @{
  104. */
  105. typedef enum
  106. {
  107. UART0 = 0,
  108. UART1,
  109. } UART_TypeDef;
  110. #define IS_UART(UARTx) (UARTx == UART0 ||UARTx == UART1)
  111. /**
  112. * @}
  113. */
  114. /**
  115. * @brief ENABLE or DISABLE UARTx auto flow control
  116. * @param USARTx: Select the USART or the UART peripheral.
  117. * This parameter can be one of the following values:
  118. * UART0, UART1.
  119. * @param NewState:ENABLE or DISABLE auto flow control
  120. * @retval None
  121. */
  122. void UART_AutoFlowCtrlCmd(UART_TypeDef UARTx, FunctionalState NewState);
  123. /**
  124. * @brief Clear IT
  125. * @param USARTx: Select the USART or the UART peripheral.
  126. * This parameter can be one of the following values:
  127. * UART0, UART1.
  128. * @retval None
  129. */
  130. void UART_ClearIT(UART_TypeDef UARTx);
  131. /**
  132. * @brief DeInit UART
  133. * @param UARTx: Select the UART peripheral.
  134. * This parameter can be one of the following values:
  135. * UART0, UART1.
  136. * @retval None
  137. */
  138. void UART_DeInit(UART_TypeDef UARTx);
  139. /**
  140. * @brief Transmits datas via UART DMA .
  141. * @param USARTx: Select the USART or the UART peripheral.
  142. * This parameter can be one of the following values:
  143. * UART0, UART1.
  144. * @param buf: pointer to a buf that contains the data you want transmit.
  145. * @param len: the buf length
  146. * @retval None
  147. */
  148. void UART_DMASendBuf(UART_TypeDef UARTx, uint8_t *buf, int len);
  149. /**
  150. * @brief Get IT Identity
  151. * @param UARTx: Select the UART peripheral.
  152. * @retval IT Identity
  153. */
  154. uint8_t UART_GetITIdentity(UART_TypeDef UARTx);
  155. /**
  156. * @brief Initializes the USARTx peripheral according to the specified
  157. * parameters in the USART_InitStruct .
  158. * @param UARTx: Select the UART peripheral.
  159. * This parameter can be one of the following values:
  160. * UART0, UART1.
  161. * @param USART_InitStruct: pointer to a USART_InitTypeDef structure
  162. * that contains the configuration information for the specified USART
  163. * peripheral.
  164. * @retval None
  165. */
  166. void UART_Init(UART_TypeDef UARTx, UART_InitTypeDef *UART_InitStruct);
  167. /**
  168. * @brief Judge Rx fifo full is or not.
  169. * @param UARTx: Select the UART peripheral.
  170. * @retval TRUE:Rx fifo is full.
  171. * FALSE:Rx fifo is not full
  172. */
  173. Boolean UART_IsRXFIFOFull(UART_TypeDef UARTx);
  174. /**
  175. * @brief Judge Rx fifo empty is or not.
  176. * @param UARTx: Select the UART peripheral.
  177. * @retval TRUE:Rx fifo is not empty.
  178. * FALSE:Rx fifo is empty;
  179. */
  180. Boolean UART_IsRXFIFONotEmpty(UART_TypeDef UARTx);
  181. /**
  182. * @brief Judge UART is Busy or not
  183. * @param UARTx: Select the UART peripheral.
  184. * @retval None
  185. */
  186. Boolean UART_IsUARTBusy(UART_TypeDef UARTx);
  187. /**
  188. * @brief Config Interrupt trigger mode
  189. * @param USARTx: Select the USART or the UART peripheral.
  190. * This parameter can be one of the following values:
  191. * UART0, UART1.
  192. * @param UART_IT: Interrupt trigger mode ,this param will the following values,
  193. * UART_IT_TX:interrupt trigger after send data completed.
  194. * UART_IT_RX:interrupt trigger when received data.
  195. * @param NewState:
  196. * @retval None
  197. */
  198. void UART_ITConfig(UART_TypeDef UARTx, uint32_t UART_IT, FunctionalState NewState);
  199. /**
  200. * @brief Receive single data through the USARTx peripheral.
  201. * @param USARTx: Select the USART or the UART peripheral.
  202. * This parameter can be one of the following values:
  203. * UART0, UART1.
  204. * @retval None
  205. */
  206. uint8_t UART_ReceiveData(UART_TypeDef UARTx);
  207. /**
  208. * @brief Receives datas through the UART DMA.
  209. * @param USARTx: Select the USART or the UART peripheral.
  210. * This parameter can be one of the following values:
  211. * UART0, UART1.
  212. * @param buf: pointer to a buf that contains the data you want receive.
  213. * @param len: the buf length
  214. * @retval None
  215. */
  216. int UART_RecvBuf(UART_TypeDef UARTx, uint8_t *buf, int len);
  217. /**
  218. * @brief T ransmits datas via UART DMA,the function will return after datas is sent.
  219. * @param USARTx: Select the USART or the UART peripheral.
  220. * This parameter can be one of the following values:
  221. * UART0, UART1.
  222. * @param buf: pointer to a buf that contains the data you want transmit.
  223. * @param len: the buf length
  224. * @retval None
  225. */
  226. void UART_SendBuf(UART_TypeDef UARTx, uint8_t *buf, int len);
  227. /**
  228. * @brief UART Send One Data
  229. * @param UARTx: Select the UART peripheral.
  230. * @retval None
  231. */
  232. void UART_SendData(UART_TypeDef UARTx, uint8_t Data);
  233. /**
  234. * @brief UART_SetITTimeout
  235. * @param USARTx: Select the USART or the UART peripheral.
  236. * This parameter can be one of the following values:
  237. * UART0, UART1.
  238. * timeout: 0x0000~0xffff
  239. * @retval None
  240. */
  241. void UART_SetITTimeout(UART_TypeDef UARTx, uint16_t timeout);
  242. /**
  243. * @brief Set the number of uart receive data intterupt trigger
  244. * @param UARTx: Select the UART peripheral.
  245. * This parameter can be one of the following values:
  246. * UART0, UART1.
  247. * @param Bcnt: if the number of receive datas greater than Bcnt,interrupt trigger
  248. * @retval None
  249. */
  250. void UART_SetRxITNum(UART_TypeDef UARTx, uint8_t Bcnt);
  251. /**
  252. * @brief Fills each USART_InitStruct member with its default value.
  253. * @param USART_InitStruct: pointer to a USART_InitTypeDef structure
  254. * which will be initialized.
  255. * @retval None
  256. */
  257. void UART_StructInit(UART_InitTypeDef *UART_InitStruct);
  258. /**
  259. * @brief UART_ReceiveDataLen
  260. * @param UARTx: UART0 or UART1
  261. * @retval Data len
  262. */
  263. uint16_t UART_ReceiveDataLen(UART_TypeDef UARTx);
  264. #endif /*__YC_UART_H__*/