dev_serial_v2.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2021-06-01 KyleChan first version
  9. */
  10. #ifndef __DEV_SERIAL_V2_H__
  11. #define __DEV_SERIAL_V2_H__
  12. #include <rtthread.h>
  13. /**
  14. * @addtogroup Drivers RTTHREAD Driver
  15. * @defgroup Serial_v2 Serial v2
  16. *
  17. * @brief Serial v2 driver api
  18. *
  19. * <b>Example</b>
  20. * @code {.c}
  21. *
  22. * #include <rtthread.h>
  23. * #include <rtdevice.h>
  24. *
  25. * #define SAMPLE_UART_NAME "uart1"
  26. *
  27. * struct rx_msg
  28. * {
  29. * rt_device_t dev;
  30. * rt_size_t size;
  31. * };
  32. * static rt_device_t serial;
  33. * static struct rt_messagequeue rx_mq;
  34. *
  35. * static rt_err_t uart_input(rt_device_t dev, rt_size_t size)
  36. * {
  37. * struct rx_msg msg;
  38. * rt_err_t result;
  39. * msg.dev = dev;
  40. * msg.size = size;
  41. *
  42. * result = rt_mq_send(&rx_mq, &msg, sizeof(msg));
  43. * if (result == -RT_EFULL)
  44. * {
  45. * rt_kprintf("message queue full!\n");
  46. * }
  47. * return result;
  48. * }
  49. *
  50. * static void serial_thread_entry(void *parameter)
  51. * {
  52. * struct rx_msg msg;
  53. * rt_err_t result;
  54. * rt_uint32_t rx_length;
  55. * static char rx_buffer[BSP_UART1_RX_BUFSIZE + 1];
  56. *
  57. * while (1)
  58. * {
  59. * rt_memset(&msg, 0, sizeof(msg));
  60. * result = rt_mq_recv(&rx_mq, &msg, sizeof(msg), RT_WAITING_FOREVER);
  61. * if (result > 0)
  62. * {
  63. * rx_length = rt_device_read(msg.dev, 0, rx_buffer, msg.size);
  64. * rx_buffer[rx_length] = '\0';
  65. * rt_device_write(serial, 0, rx_buffer, rx_length);
  66. * rt_kprintf("%s\n",rx_buffer);
  67. * }
  68. * }
  69. * }
  70. *
  71. * static int uart_dma_sample(int argc, char *argv[])
  72. * {
  73. * rt_err_t ret = RT_EOK;
  74. * char uart_name[RT_NAME_MAX];
  75. * static char msg_pool[256];
  76. * char str[] = "hello RT-Thread!\r\n";
  77. *
  78. * if (argc == 2)
  79. * {
  80. * rt_strncpy(uart_name, argv[1], RT_NAME_MAX);
  81. * }
  82. * else
  83. * {
  84. * rt_strncpy(uart_name, SAMPLE_UART_NAME, RT_NAME_MAX);
  85. * }
  86. *
  87. * serial = rt_device_find(uart_name);
  88. * if (!serial)
  89. * {
  90. * rt_kprintf("find %s failed!\n", uart_name);
  91. * return RT_ERROR;
  92. * }
  93. *
  94. * rt_mq_init(&rx_mq, "rx_mq",
  95. * msg_pool,
  96. * sizeof(struct rx_msg),
  97. * sizeof(msg_pool),
  98. * RT_IPC_FLAG_FIFO);
  99. *
  100. * rt_device_open(serial, RT_DEVICE_FLAG_RX_NON_BLOCKING | RT_DEVICE_FLAG_TX_BLOCKING);
  101. * rt_device_set_rx_indicate(serial, uart_input);
  102. * rt_device_write(serial, 0, str, (sizeof(str) - 1));
  103. *
  104. * rt_thread_t thread = rt_thread_create("serial", serial_thread_entry, RT_NULL, 1024, 25, 10);
  105. * if (thread != RT_NULL)
  106. * {
  107. * rt_thread_startup(thread);
  108. * }
  109. * else
  110. * {
  111. * ret = RT_ERROR;
  112. * }
  113. *
  114. * return ret;
  115. * }
  116. * MSH_CMD_EXPORT(uart_dma_sample, uart device dma sample);
  117. * @endcode
  118. *
  119. * @ingroup Drivers
  120. */
  121. /*!
  122. * @addtogroup Serial_v2
  123. * @{
  124. */
  125. #define BAUD_RATE_2400 2400
  126. #define BAUD_RATE_4800 4800
  127. #define BAUD_RATE_9600 9600
  128. #define BAUD_RATE_19200 19200
  129. #define BAUD_RATE_38400 38400
  130. #define BAUD_RATE_57600 57600
  131. #define BAUD_RATE_115200 115200
  132. #define BAUD_RATE_230400 230400
  133. #define BAUD_RATE_460800 460800
  134. #define BAUD_RATE_500000 500000
  135. #define BAUD_RATE_921600 921600
  136. #define BAUD_RATE_2000000 2000000
  137. #define BAUD_RATE_2500000 2500000
  138. #define BAUD_RATE_3000000 3000000
  139. #define DATA_BITS_5 5
  140. #define DATA_BITS_6 6
  141. #define DATA_BITS_7 7
  142. #define DATA_BITS_8 8
  143. #define DATA_BITS_9 9
  144. #define STOP_BITS_1 0
  145. #define STOP_BITS_2 1
  146. #define STOP_BITS_3 2
  147. #define STOP_BITS_4 3
  148. #ifdef _WIN32
  149. #include <windows.h>
  150. #else
  151. #define PARITY_NONE 0
  152. #define PARITY_ODD 1
  153. #define PARITY_EVEN 2
  154. #endif
  155. #define BIT_ORDER_LSB 0
  156. #define BIT_ORDER_MSB 1
  157. #define NRZ_NORMAL 0 /* Non Return to Zero : normal mode */
  158. #define NRZ_INVERTED 1 /* Non Return to Zero : inverted mode */
  159. #define RT_DEVICE_FLAG_RX_BLOCKING 0x1000
  160. #define RT_DEVICE_FLAG_RX_NON_BLOCKING 0x2000
  161. #define RT_DEVICE_FLAG_TX_BLOCKING 0x4000
  162. #define RT_DEVICE_FLAG_TX_NON_BLOCKING 0x8000
  163. #define RT_SERIAL_RX_BLOCKING RT_DEVICE_FLAG_RX_BLOCKING
  164. #define RT_SERIAL_RX_NON_BLOCKING RT_DEVICE_FLAG_RX_NON_BLOCKING
  165. #define RT_SERIAL_TX_BLOCKING RT_DEVICE_FLAG_TX_BLOCKING
  166. #define RT_SERIAL_TX_NON_BLOCKING RT_DEVICE_FLAG_TX_NON_BLOCKING
  167. #define RT_DEVICE_CHECK_OPTMODE 0x20
  168. #define RT_SERIAL_EVENT_RX_IND 0x01 /* Rx indication */
  169. #define RT_SERIAL_EVENT_TX_DONE 0x02 /* Tx complete */
  170. #define RT_SERIAL_EVENT_RX_DMADONE 0x03 /* Rx DMA transfer done */
  171. #define RT_SERIAL_EVENT_TX_DMADONE 0x04 /* Tx DMA transfer done */
  172. #define RT_SERIAL_EVENT_RX_TIMEOUT 0x05 /* Rx timeout */
  173. #define RT_SERIAL_ERR_OVERRUN 0x01
  174. #define RT_SERIAL_ERR_FRAMING 0x02
  175. #define RT_SERIAL_ERR_PARITY 0x03
  176. #define RT_SERIAL_TX_DATAQUEUE_SIZE 2048
  177. #define RT_SERIAL_TX_DATAQUEUE_LWM 30
  178. #define RT_SERIAL_RX_MINBUFSZ 64
  179. #define RT_SERIAL_TX_MINBUFSZ 64
  180. #define RT_SERIAL_TX_BLOCKING_BUFFER 1
  181. #define RT_SERIAL_TX_BLOCKING_NO_BUFFER 0
  182. #define RT_SERIAL_FLOWCONTROL_CTSRTS 1
  183. #define RT_SERIAL_FLOWCONTROL_NONE 0
  184. /* Default config for serial_configure structure */
  185. #define RT_SERIAL_CONFIG_DEFAULT \
  186. { \
  187. BAUD_RATE_115200, /* 115200 bits/s */ \
  188. DATA_BITS_8, /* 8 databits */ \
  189. STOP_BITS_1, /* 1 stopbit */ \
  190. PARITY_NONE, /* No parity */ \
  191. BIT_ORDER_LSB, /* LSB first sent */ \
  192. NRZ_NORMAL, /* Normal mode */ \
  193. RT_SERIAL_RX_MINBUFSZ, /* rxBuf size */ \
  194. RT_SERIAL_TX_MINBUFSZ, /* txBuf size */ \
  195. RT_SERIAL_FLOWCONTROL_NONE, /* Off flowcontrol */ \
  196. 0 \
  197. }
  198. /**
  199. * @brief Serial receive indicate hook function type
  200. *
  201. */
  202. typedef void (*rt_hw_serial_rxind_hookproto_t)(rt_device_t dev, rt_size_t size);
  203. RT_OBJECT_HOOKLIST_DECLARE(rt_hw_serial_rxind_hookproto_t, rt_hw_serial_rxind);
  204. struct serial_configure
  205. {
  206. rt_uint32_t baud_rate;
  207. rt_uint32_t data_bits :4;
  208. rt_uint32_t stop_bits :2;
  209. rt_uint32_t parity :2;
  210. rt_uint32_t bit_order :1;
  211. rt_uint32_t invert :1;
  212. rt_uint32_t rx_bufsz :16;
  213. rt_uint32_t tx_bufsz :16;
  214. rt_uint32_t flowcontrol :1;
  215. rt_uint32_t reserved :5;
  216. };
  217. /**
  218. * @brief Serial Receive FIFO mode
  219. */
  220. struct rt_serial_rx_fifo
  221. {
  222. struct rt_ringbuffer rb;
  223. struct rt_completion rx_cpt;
  224. rt_uint16_t rx_cpt_index;
  225. /* software fifo */
  226. rt_uint8_t buffer[];
  227. };
  228. /**
  229. * @brief Serial Transmit FIFO mode
  230. *
  231. */
  232. struct rt_serial_tx_fifo
  233. {
  234. struct rt_ringbuffer rb;
  235. rt_size_t put_size;
  236. rt_bool_t activated;
  237. struct rt_completion tx_cpt;
  238. /* software fifo */
  239. rt_uint8_t buffer[];
  240. };
  241. /**
  242. * @brief serial device structure
  243. *
  244. */
  245. struct rt_serial_device
  246. {
  247. struct rt_device parent;
  248. const struct rt_uart_ops *ops;
  249. struct serial_configure config;
  250. void *serial_rx;
  251. void *serial_tx;
  252. struct rt_device_notify rx_notify;
  253. };
  254. /**
  255. * @brief uart device operations
  256. *
  257. */
  258. struct rt_uart_ops
  259. {
  260. rt_err_t (*configure)(struct rt_serial_device *serial,
  261. struct serial_configure *cfg);
  262. rt_err_t (*control)(struct rt_serial_device *serial,
  263. int cmd,
  264. void *arg);
  265. int (*putc)(struct rt_serial_device *serial, char c);
  266. int (*getc)(struct rt_serial_device *serial);
  267. rt_ssize_t (*transmit)(struct rt_serial_device *serial,
  268. rt_uint8_t *buf,
  269. rt_size_t size,
  270. rt_uint32_t tx_flag);
  271. };
  272. /**
  273. * @brief Serial interrupt service routine
  274. * @param serial serial device
  275. * @param event event mask
  276. * @ingroup Serial_v2
  277. */
  278. void rt_hw_serial_isr(struct rt_serial_device *serial, int event);
  279. /**
  280. * @brief Register a serial device to device list
  281. *
  282. * @param serial serial device
  283. * @param name device name
  284. * @param flag device flag
  285. * @param data device private data
  286. * @return rt_err_t error code
  287. * @note This function will register a serial device to system device list,
  288. * and add a device object to system object list.
  289. * @ingroup Serial_v2
  290. */
  291. rt_err_t rt_hw_serial_register(struct rt_serial_device *serial,
  292. const char *name,
  293. rt_uint32_t flag,
  294. void *data);
  295. /**
  296. * @brief register a serial device to system device list and add a device object to system object list
  297. *
  298. * @param serial serial device
  299. * @return rt_err_t error code
  300. *
  301. * @ingroup Serial_v2
  302. */
  303. rt_err_t rt_hw_serial_register_tty(struct rt_serial_device *serial);
  304. /*! @}*/
  305. #endif