1
0

console.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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.12.07 linzhenxing first version
  9. */
  10. #include <rtthread.h>
  11. #include <dfs_file.h>
  12. #include <dfs_fs.h>
  13. #include <tty.h>
  14. #define DBG_TAG "CONSOLE"
  15. #ifdef RT_TTY_DEBUG
  16. #define DBG_LVL DBG_LOG
  17. #else
  18. #define DBG_LVL DBG_INFO
  19. #endif /* RT_TTY_DEBUG */
  20. #include <rtdbg.h>
  21. #include <ipc/waitqueue.h>
  22. #include <ipc/ringbuffer.h>
  23. static struct tty_struct console_dev;
  24. static struct rt_ringbuffer console_rx_ringbuffer;
  25. static struct rt_wqueue console_rx_wqueue;
  26. static rt_thread_t console_rx_thread;
  27. static const size_t rb_bufsz = 0x1000;
  28. static void console_rx_work(void *parameter)
  29. {
  30. int len;
  31. char ch;
  32. int lens;
  33. static char buf[0x1000];
  34. struct tty_struct *console;
  35. console = &console_dev;
  36. while (1)
  37. {
  38. rt_wqueue_wait(&console_rx_wqueue, 0, RT_WAITING_FOREVER);
  39. lens = 0;
  40. while (lens < sizeof(buf))
  41. {
  42. len = rt_ringbuffer_get(&console_rx_ringbuffer, (void *)&ch, sizeof(ch));
  43. if (len == 0)
  44. {
  45. break;
  46. }
  47. lens += len;
  48. buf[lens-1] = ch;
  49. }
  50. if (lens && console->ldisc->ops->receive_buf)
  51. {
  52. console->ldisc->ops->receive_buf((struct tty_struct *)console, buf, lens);
  53. }
  54. }
  55. }
  56. static int rx_thread_init(void)
  57. {
  58. void *rb_buffer;
  59. rt_thread_t thread;
  60. rb_buffer = rt_malloc(rb_bufsz);
  61. rt_ringbuffer_init(&console_rx_ringbuffer, rb_buffer, rb_bufsz);
  62. rt_wqueue_init(&console_rx_wqueue);
  63. thread = rt_thread_create("console_rx", console_rx_work, &console_dev, rb_bufsz, 10, 10);
  64. if (thread != RT_NULL)
  65. {
  66. rt_thread_startup(thread);
  67. console_rx_thread = thread;
  68. }
  69. return 0;
  70. }
  71. INIT_COMPONENT_EXPORT(rx_thread_init);
  72. static void console_rx_notify(struct rt_device *dev)
  73. {
  74. struct tty_struct *console = NULL;
  75. int len = 0;
  76. int lens = 0;
  77. char ch = 0;
  78. console = (struct tty_struct *)dev;
  79. RT_ASSERT(console != RT_NULL);
  80. while (1)
  81. {
  82. len = rt_device_read(console->io_dev, -1, &ch, 1);
  83. if (len == 0)
  84. {
  85. break;
  86. }
  87. lens += len;
  88. rt_ringbuffer_put(&console_rx_ringbuffer, (void *)&ch, sizeof(ch));
  89. if (lens > rb_bufsz)
  90. {
  91. break;
  92. }
  93. }
  94. if (console_rx_thread)
  95. rt_wqueue_wakeup(&console_rx_wqueue, 0);
  96. }
  97. struct tty_struct *console_tty_get(void)
  98. {
  99. return &console_dev;
  100. }
  101. static void iodev_close(struct tty_struct *console)
  102. {
  103. struct rt_device_notify rx_notify;
  104. rx_notify.notify = RT_NULL;
  105. rx_notify.dev = RT_NULL;
  106. /* clear notify */
  107. rt_device_control(console->io_dev, RT_DEVICE_CTRL_NOTIFY_SET, &rx_notify);
  108. rt_device_close(console->io_dev);
  109. }
  110. static rt_err_t iodev_open(struct tty_struct *console)
  111. {
  112. rt_err_t ret = RT_EOK;
  113. struct rt_device_notify rx_notify;
  114. rt_uint16_t oflags = 0;
  115. rt_device_control(console->io_dev, RT_DEVICE_CTRL_CONSOLE_OFLAG, &oflags);
  116. ret = rt_device_open(console->io_dev, oflags);
  117. if (ret != RT_EOK)
  118. {
  119. return -RT_ERROR;
  120. }
  121. rx_notify.notify = console_rx_notify;
  122. rx_notify.dev = (struct rt_device *)console;
  123. rt_device_control(console->io_dev, RT_DEVICE_CTRL_NOTIFY_SET, &rx_notify);
  124. return RT_EOK;
  125. }
  126. struct rt_device *console_get_iodev(void)
  127. {
  128. rt_base_t level = 0;
  129. struct rt_device *iodev = RT_NULL;
  130. level = rt_hw_interrupt_disable();
  131. iodev = console_dev.io_dev;
  132. rt_hw_interrupt_enable(level);
  133. return iodev;
  134. }
  135. struct rt_device *console_set_iodev(struct rt_device *iodev)
  136. {
  137. rt_base_t level = 0;
  138. struct rt_device *io_before = RT_NULL;
  139. struct tty_struct *console = RT_NULL;
  140. RT_ASSERT(iodev != RT_NULL);
  141. console = &console_dev;
  142. level = rt_hw_interrupt_disable();
  143. RT_ASSERT(console->init_flag >= TTY_INIT_FLAG_REGED);
  144. io_before = console->io_dev;
  145. if (iodev == io_before)
  146. {
  147. goto exit;
  148. }
  149. if (console->init_flag >= TTY_INIT_FLAG_INITED)
  150. {
  151. /* close old device */
  152. iodev_close(console);
  153. }
  154. console->io_dev = iodev;
  155. if (console->init_flag >= TTY_INIT_FLAG_INITED)
  156. {
  157. rt_err_t ret;
  158. /* open new device */
  159. ret = iodev_open(console);
  160. RT_ASSERT(ret == RT_EOK);
  161. }
  162. exit:
  163. rt_hw_interrupt_enable(level);
  164. return io_before;
  165. }
  166. /* RT-Thread Device Interface */
  167. /*
  168. * This function initializes console device.
  169. */
  170. static rt_err_t rt_console_init(struct rt_device *dev)
  171. {
  172. rt_base_t level = 0;
  173. rt_err_t result = RT_EOK;
  174. struct tty_struct *console = RT_NULL;
  175. RT_ASSERT(dev != RT_NULL);
  176. console = (struct tty_struct *)dev;
  177. level = rt_hw_interrupt_disable();
  178. RT_ASSERT(console->init_flag == TTY_INIT_FLAG_REGED);
  179. result = iodev_open(console);
  180. if (result != RT_EOK)
  181. {
  182. goto exit;
  183. }
  184. console->init_flag = TTY_INIT_FLAG_INITED;
  185. exit:
  186. rt_hw_interrupt_enable(level);
  187. return result;
  188. }
  189. static rt_err_t rt_console_open(struct rt_device *dev, rt_uint16_t oflag)
  190. {
  191. rt_err_t result = RT_EOK;
  192. struct tty_struct *console = RT_NULL;
  193. RT_ASSERT(dev != RT_NULL);
  194. console = (struct tty_struct *)dev;
  195. RT_ASSERT(console != RT_NULL);
  196. RT_ASSERT(console->init_flag == TTY_INIT_FLAG_INITED);
  197. return result;
  198. }
  199. static rt_err_t rt_console_close(struct rt_device *dev)
  200. {
  201. rt_err_t result = RT_EOK;
  202. struct tty_struct *console = RT_NULL;
  203. console = (struct tty_struct *)dev;
  204. RT_ASSERT(console != RT_NULL);
  205. RT_ASSERT(console->init_flag == TTY_INIT_FLAG_INITED);
  206. return result;
  207. }
  208. static rt_ssize_t rt_console_read(struct rt_device *dev,
  209. rt_off_t pos,
  210. void *buffer,
  211. rt_size_t size)
  212. {
  213. rt_size_t len = 0;
  214. return len;
  215. }
  216. static rt_ssize_t rt_console_write(struct rt_device *dev,
  217. rt_off_t pos,
  218. const void *buffer,
  219. rt_size_t size)
  220. {
  221. rt_size_t len = 0;
  222. struct tty_struct *console = RT_NULL;
  223. console = (struct tty_struct *)dev;
  224. RT_ASSERT(console != RT_NULL);
  225. RT_ASSERT(console->init_flag == TTY_INIT_FLAG_INITED);
  226. len = rt_device_write((struct rt_device *)console->io_dev, -1, buffer, size);
  227. return len;
  228. }
  229. static rt_err_t rt_console_control(rt_device_t dev, int cmd, void *args)
  230. {
  231. rt_size_t len = 0;
  232. struct tty_struct *console = RT_NULL;
  233. console = (struct tty_struct *)dev;
  234. RT_ASSERT(console != RT_NULL);
  235. RT_ASSERT(console->init_flag == TTY_INIT_FLAG_INITED);
  236. len = rt_device_control((struct rt_device *)console->io_dev, cmd, args);
  237. return len;
  238. }
  239. #ifdef RT_USING_DEVICE_OPS
  240. const static struct rt_device_ops console_ops =
  241. {
  242. rt_console_init,
  243. rt_console_open,
  244. rt_console_close,
  245. rt_console_read,
  246. rt_console_write,
  247. rt_console_control,
  248. };
  249. #endif
  250. /*
  251. * console register
  252. */
  253. static struct dfs_file_ops con_fops;
  254. rt_err_t console_register(const char *name, struct rt_device *iodev)
  255. {
  256. rt_err_t ret = RT_EOK;
  257. struct rt_device *device = RT_NULL;
  258. struct tty_struct *console = &console_dev;
  259. RT_ASSERT(iodev != RT_NULL);
  260. RT_ASSERT(console->init_flag == TTY_INIT_FLAG_NONE);
  261. tty_init(console, TTY_DRIVER_TYPE_CONSOLE, SERIAL_TYPE_NORMAL, iodev);
  262. console_ldata_init(console);
  263. device = &(console->parent);
  264. device->type = RT_Device_Class_Char;
  265. #ifdef RT_USING_DEVICE_OPS
  266. device->ops = &console_ops;
  267. #else
  268. device->init = rt_console_init;
  269. device->open = rt_console_open;
  270. device->close = rt_console_close;
  271. device->read = rt_console_read;
  272. device->write = rt_console_write;
  273. device->control = rt_console_control;
  274. #endif
  275. /* register a character device */
  276. ret = rt_device_register(device, name, 0);
  277. if (ret != RT_EOK)
  278. {
  279. LOG_E("console driver register fail\n");
  280. }
  281. else
  282. {
  283. #ifdef RT_USING_POSIX_DEVIO
  284. /* set fops */
  285. memcpy(&con_fops, tty_get_fops(), sizeof(struct dfs_file_ops));
  286. device->fops = &con_fops;
  287. #endif
  288. }
  289. return ret;
  290. }