dev_serial.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /*
  2. * Copyright (c) 2006-2024 RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2012-05-15 lgnq first version.
  9. * 2012-05-28 bernard change interfaces
  10. * 2013-02-20 bernard use RT_SERIAL_RB_BUFSZ to define
  11. * the size of ring buffer.
  12. */
  13. #ifndef __DEV_SERIAL_H__
  14. #define __DEV_SERIAL_H__
  15. #include <rtthread.h>
  16. /**
  17. * @defgroup group_drivers_serial Serial
  18. * @brief Serial driver api
  19. * @ingroup group_device_driver
  20. *
  21. * <b>Example</b>
  22. * @code {.c}
  23. *
  24. * #include <rtthread.h>
  25. *
  26. * #define SAMPLE_UART_NAME "uart2"
  27. * static struct rt_semaphore rx_sem;
  28. * static rt_device_t serial;
  29. *
  30. * static rt_err_t uart_input(rt_device_t dev, rt_size_t size)
  31. * {
  32. *
  33. * rt_sem_release(&rx_sem);
  34. *
  35. * return RT_EOK;
  36. * }
  37. *
  38. * static void serial_thread_entry(void *parameter)
  39. * {
  40. * char ch;
  41. *
  42. * while (1)
  43. * {
  44. *
  45. * while (rt_device_read(serial, -1, &ch, 1) != 1)
  46. * {
  47. *
  48. * rt_sem_take(&rx_sem, RT_WAITING_FOREVER);
  49. * }
  50. *
  51. * ch = ch + 1;
  52. * rt_device_write(serial, 0, &ch, 1);
  53. * }
  54. * }
  55. *
  56. * static int uart_sample(int argc, char *argv[])
  57. * {
  58. * rt_err_t ret = RT_EOK;
  59. * char uart_name[RT_NAME_MAX];
  60. * char str[] = "hello RT-Thread!\r\n";
  61. *
  62. * if (argc == 2)
  63. * {
  64. * rt_strncpy(uart_name, argv[1], RT_NAME_MAX);
  65. * }
  66. * else
  67. * {
  68. * rt_strncpy(uart_name, SAMPLE_UART_NAME, RT_NAME_MAX);
  69. * }
  70. *
  71. *
  72. * serial = rt_device_find(uart_name);
  73. * if (!serial)
  74. * {
  75. * rt_kprintf("find %s failed!\n", uart_name);
  76. * return -RT_ERROR;
  77. * }
  78. *
  79. *
  80. * rt_sem_init(&rx_sem, "rx_sem", 0, RT_IPC_FLAG_FIFO);
  81. *
  82. * rt_device_open(serial, RT_DEVICE_FLAG_INT_RX);
  83. *
  84. * rt_device_set_rx_indicate(serial, uart_input);
  85. *
  86. * rt_device_write(serial, 0, str, (sizeof(str) - 1));
  87. *
  88. *
  89. * rt_thread_t thread = rt_thread_create("serial", serial_thread_entry, RT_NULL, 1024, 25, 10);
  90. *
  91. * if (thread != RT_NULL)
  92. * {
  93. * rt_thread_startup(thread);
  94. * }
  95. * else
  96. * {
  97. * ret = -RT_ERROR;
  98. * }
  99. *
  100. * return ret;
  101. * }
  102. *
  103. * MSH_CMD_EXPORT(uart_sample, uart device sample);
  104. * @endcode
  105. */
  106. /*!
  107. * @addtogroup group_drivers_serial
  108. * @{
  109. */
  110. #define BAUD_RATE_2400 2400
  111. #define BAUD_RATE_4800 4800
  112. #define BAUD_RATE_9600 9600
  113. #define BAUD_RATE_19200 19200
  114. #define BAUD_RATE_38400 38400
  115. #define BAUD_RATE_57600 57600
  116. #define BAUD_RATE_115200 115200
  117. #define BAUD_RATE_230400 230400
  118. #define BAUD_RATE_460800 460800
  119. #define BAUD_RATE_500000 500000
  120. #define BAUD_RATE_576000 576000
  121. #define BAUD_RATE_921600 921600
  122. #define BAUD_RATE_1000000 1000000
  123. #define BAUD_RATE_1152000 1152000
  124. #define BAUD_RATE_1500000 1500000
  125. #define BAUD_RATE_2000000 2000000
  126. #define BAUD_RATE_2500000 2500000
  127. #define BAUD_RATE_3000000 3000000
  128. #define BAUD_RATE_3500000 3500000
  129. #define BAUD_RATE_4000000 4000000
  130. #define DATA_BITS_5 5
  131. #define DATA_BITS_6 6
  132. #define DATA_BITS_7 7
  133. #define DATA_BITS_8 8
  134. #define DATA_BITS_9 9
  135. #define STOP_BITS_1 0
  136. #define STOP_BITS_2 1
  137. #define STOP_BITS_3 2
  138. #define STOP_BITS_4 3
  139. #ifdef _WIN32
  140. #include <windows.h>
  141. #else
  142. #define PARITY_NONE 0
  143. #define PARITY_ODD 1
  144. #define PARITY_EVEN 2
  145. #endif
  146. #define BIT_ORDER_LSB 0
  147. #define BIT_ORDER_MSB 1
  148. #define NRZ_NORMAL 0 /* Non Return to Zero : normal mode */
  149. #define NRZ_INVERTED 1 /* Non Return to Zero : inverted mode */
  150. #ifndef RT_SERIAL_RB_BUFSZ
  151. #define RT_SERIAL_RB_BUFSZ 64
  152. #endif
  153. #define RT_SERIAL_EVENT_RX_IND 0x01 /* Rx indication */
  154. #define RT_SERIAL_EVENT_TX_DONE 0x02 /* Tx complete */
  155. #define RT_SERIAL_EVENT_RX_DMADONE 0x03 /* Rx DMA transfer done */
  156. #define RT_SERIAL_EVENT_TX_DMADONE 0x04 /* Tx DMA transfer done */
  157. #define RT_SERIAL_EVENT_RX_TIMEOUT 0x05 /* Rx timeout */
  158. #define RT_SERIAL_DMA_RX 0x01
  159. #define RT_SERIAL_DMA_TX 0x02
  160. #define RT_SERIAL_RX_INT 0x01
  161. #define RT_SERIAL_TX_INT 0x02
  162. #define RT_SERIAL_ERR_OVERRUN 0x01
  163. #define RT_SERIAL_ERR_FRAMING 0x02
  164. #define RT_SERIAL_ERR_PARITY 0x03
  165. #define RT_SERIAL_TX_DATAQUEUE_SIZE 2048
  166. #define RT_SERIAL_TX_DATAQUEUE_LWM 30
  167. #define RT_SERIAL_FLOWCONTROL_CTSRTS 1
  168. #define RT_SERIAL_FLOWCONTROL_NONE 0
  169. /* Default config for serial_configure structure */
  170. #define RT_SERIAL_CONFIG_DEFAULT \
  171. { \
  172. BAUD_RATE_115200, /* 115200 bits/s */ \
  173. DATA_BITS_8, /* 8 databits */ \
  174. STOP_BITS_1, /* 1 stopbit */ \
  175. PARITY_NONE, /* No parity */ \
  176. BIT_ORDER_LSB, /* LSB first sent */ \
  177. NRZ_NORMAL, /* Normal mode */ \
  178. RT_SERIAL_RB_BUFSZ, /* Buffer size */ \
  179. RT_SERIAL_FLOWCONTROL_NONE, /* Off flowcontrol */ \
  180. 0 \
  181. }
  182. /**
  183. * @brief Sets a hook function when RX indicate is called
  184. *
  185. */
  186. typedef void (*rt_hw_serial_rxind_hookproto_t)(rt_device_t dev, rt_size_t size);
  187. RT_OBJECT_HOOKLIST_DECLARE(rt_hw_serial_rxind_hookproto_t, rt_hw_serial_rxind);
  188. struct serial_configure
  189. {
  190. rt_uint32_t baud_rate;
  191. rt_uint32_t data_bits :4;
  192. rt_uint32_t stop_bits :2;
  193. rt_uint32_t parity :2;
  194. rt_uint32_t bit_order :1;
  195. rt_uint32_t invert :1;
  196. rt_uint32_t bufsz :16;
  197. rt_uint32_t flowcontrol :1;
  198. rt_uint32_t reserved :5;
  199. };
  200. /*
  201. * Serial FIFO mode
  202. */
  203. struct rt_serial_rx_fifo
  204. {
  205. /* software fifo */
  206. rt_uint8_t *buffer;
  207. rt_uint16_t put_index, get_index;
  208. rt_bool_t is_full;
  209. };
  210. struct rt_serial_tx_fifo
  211. {
  212. struct rt_completion completion;
  213. };
  214. /*
  215. * Serial DMA mode
  216. */
  217. struct rt_serial_rx_dma
  218. {
  219. rt_bool_t activated;
  220. };
  221. struct rt_serial_tx_dma
  222. {
  223. rt_bool_t activated;
  224. struct rt_data_queue data_queue;
  225. };
  226. struct rt_serial_device
  227. {
  228. struct rt_device parent;
  229. const struct rt_uart_ops *ops;
  230. struct serial_configure config;
  231. void *serial_rx;
  232. void *serial_tx;
  233. struct rt_spinlock spinlock;
  234. #ifdef RT_USING_SERIAL_BYPASS
  235. struct rt_serial_bypass* bypass;
  236. #endif
  237. struct rt_device_notify rx_notify;
  238. };
  239. typedef struct rt_serial_device rt_serial_t;
  240. /**
  241. * @brief Configure the serial device
  242. */
  243. struct rt_uart_ops
  244. {
  245. rt_err_t (*configure)(struct rt_serial_device *serial, struct serial_configure *cfg);
  246. rt_err_t (*control)(struct rt_serial_device *serial, int cmd, void *arg);
  247. int (*putc)(struct rt_serial_device *serial, char c);
  248. int (*getc)(struct rt_serial_device *serial);
  249. rt_ssize_t (*dma_transmit)(struct rt_serial_device *serial, rt_uint8_t *buf, rt_size_t size, int direction);
  250. };
  251. /**
  252. * @brief Serial interrupt service routine
  253. * @param serial serial device
  254. * @param event event mask
  255. * @ingroup group_drivers_serial
  256. */
  257. void rt_hw_serial_isr(struct rt_serial_device *serial, int event);
  258. /**
  259. * @brief Register a serial device to device list
  260. *
  261. * @param serial serial device
  262. * @param name device name
  263. * @param flag device flag
  264. * @param data device private data
  265. * @return rt_err_t error code
  266. * @note This function will register a serial device to system device list,
  267. * and add a device object to system object list.
  268. * @ingroup group_drivers_serial
  269. */
  270. rt_err_t rt_hw_serial_register(struct rt_serial_device *serial,
  271. const char *name,
  272. rt_uint32_t flag,
  273. void *data);
  274. /**
  275. * @brief register a serial device to system device list and add a device object to system object list
  276. *
  277. * @param serial serial device
  278. * @return rt_err_t error code
  279. *
  280. * @ingroup group_drivers_serial
  281. */
  282. rt_err_t rt_hw_serial_register_tty(struct rt_serial_device *serial);
  283. /*! @}*/
  284. #endif