uart_console.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. */
  9. #include <stdio.h>
  10. #include <rthw.h>
  11. #include <rtdevice.h>
  12. #include <rtthread.h>
  13. #include <rtdevice.h>
  14. /* uart driver */
  15. struct console_uart
  16. {
  17. int rx_ready;
  18. struct rt_ringbuffer rb;
  19. rt_uint8_t rx_buffer[256];
  20. } _console_uart;
  21. static struct rt_serial_device _serial;
  22. #define SAVEKEY(key) do { char ch = key; rt_ringbuffer_put_force(&(_console_uart.rb), &ch, 1); } while (0)
  23. #ifdef _WIN32
  24. #include <windows.h>
  25. #include <mmsystem.h>
  26. #include <conio.h>
  27. extern int getch(void);
  28. /*
  29. * Handler for OSKey Thread
  30. */
  31. static HANDLE OSKey_Thread;
  32. static DWORD OSKey_ThreadID;
  33. static DWORD WINAPI ThreadforKeyGet(LPVOID lpParam);
  34. void console_lowlevel_init(void)
  35. {
  36. /*
  37. * create serial thread that receive key input from keyboard
  38. */
  39. OSKey_Thread = CreateThread(NULL,
  40. 0,
  41. (LPTHREAD_START_ROUTINE)ThreadforKeyGet,
  42. 0,
  43. CREATE_SUSPENDED,
  44. &OSKey_ThreadID);
  45. if (OSKey_Thread == NULL)
  46. {
  47. //Display Error Message
  48. return;
  49. }
  50. SetThreadPriority(OSKey_Thread,
  51. THREAD_PRIORITY_NORMAL);
  52. SetThreadPriorityBoost(OSKey_Thread,
  53. TRUE);
  54. SetThreadAffinityMask(OSKey_Thread,
  55. 0x01);
  56. /*
  57. * Start OS get key Thread
  58. */
  59. ResumeThread(OSKey_Thread);
  60. }
  61. static DWORD WINAPI ThreadforKeyGet(LPVOID lpParam)
  62. #else /* POSIX version */
  63. #include <pthread.h>
  64. #include <semaphore.h>
  65. #include <stdlib.h>
  66. #include <signal.h>
  67. #include <termios.h> /* for tcxxxattr, ECHO, etc */
  68. #include <unistd.h> /* for STDIN_FILENO */
  69. static void * ThreadforKeyGet(void * lpParam);
  70. static pthread_t OSKey_Thread;
  71. void console_lowlevel_init(void)
  72. {
  73. int res;
  74. res = pthread_create(&OSKey_Thread, NULL, &ThreadforKeyGet, NULL);
  75. if (res)
  76. {
  77. printf("pthread create faild, <%d>\n", res);
  78. exit(EXIT_FAILURE);
  79. }
  80. }
  81. static struct termios oldt, newt;
  82. /*simulate windows' getch(), it works!!*/
  83. static void set_stty(void)
  84. {
  85. /* get terminal input's attribute */
  86. tcgetattr(STDIN_FILENO, &oldt);
  87. newt = oldt;
  88. /* set termios' local mode */
  89. newt.c_lflag &= ~(ECHO|ICANON);
  90. tcsetattr(STDIN_FILENO, TCSANOW, &newt);
  91. }
  92. void restore_stty(void)
  93. {
  94. /* recover terminal's attribute */
  95. tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
  96. }
  97. #define getch getchar
  98. static void * ThreadforKeyGet(void * lpParam)
  99. #endif /* not _WIN32*/
  100. {
  101. /*
  102. * left key(左): 0xe04b
  103. * up key(上): 0xe048
  104. * right key(右): 0xe04d
  105. * down key(下): 0xe050
  106. */
  107. unsigned char key;
  108. #ifndef _WIN32
  109. sigset_t sigmask, oldmask;
  110. /* set the getchar without buffer */
  111. sigfillset(&sigmask);
  112. pthread_sigmask(SIG_BLOCK, &sigmask, &oldmask);
  113. set_stty();
  114. #endif
  115. (void)lpParam; //prevent compiler warnings
  116. for (;;)
  117. {
  118. key = getch();
  119. #ifdef _WIN32
  120. if (key == 0xE0)
  121. {
  122. key = getch();
  123. if (key == 0x48) //up key , 0x1b 0x5b 0x41
  124. {
  125. SAVEKEY(0x1b);
  126. SAVEKEY(0x5b);
  127. SAVEKEY(0x41);
  128. }
  129. else if (key == 0x50)//0x1b 0x5b 0x42
  130. {
  131. SAVEKEY(0x1b);
  132. SAVEKEY(0x5b);
  133. SAVEKEY(0x42);
  134. }
  135. else if (key == 0x4b)//<- 0x1b 0x5b 0x44
  136. {
  137. SAVEKEY(0x1b);
  138. SAVEKEY(0x5b);
  139. SAVEKEY(0x44);
  140. }
  141. else if (key == 0x4d)//<- 0x1b 0x5b 0x43
  142. {
  143. SAVEKEY(0x1b);
  144. SAVEKEY(0x5b);
  145. SAVEKEY(0x43);
  146. }
  147. continue;
  148. }
  149. #endif
  150. SAVEKEY(key);
  151. /* Notfiy serial ISR */
  152. rt_hw_serial_isr(&_serial, RT_SERIAL_EVENT_RX_IND);
  153. }
  154. return RT_NULL;
  155. } /*** ThreadforKeyGet ***/
  156. static rt_err_t console_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
  157. {
  158. /* no baudrate, nothing */
  159. return RT_EOK;
  160. }
  161. static rt_err_t console_control(struct rt_serial_device *serial, int cmd, void *arg)
  162. {
  163. struct console_uart* uart;
  164. RT_ASSERT(serial != RT_NULL);
  165. uart = (struct console_uart *)serial->parent.user_data;
  166. switch (cmd)
  167. {
  168. case RT_DEVICE_CTRL_CLR_INT:
  169. uart->rx_ready = 0;
  170. break;
  171. case RT_DEVICE_CTRL_SET_INT:
  172. uart->rx_ready = 1;
  173. break;
  174. }
  175. return RT_EOK;
  176. }
  177. static int console_putc(struct rt_serial_device *serial, char c)
  178. {
  179. int level;
  180. RT_ASSERT(serial != RT_NULL);
  181. #if 0 /* Enable it if you want to save the console log */
  182. {
  183. static FILE* fp = NULL;
  184. if (fp == NULL)
  185. fp = fopen("log.txt", "wb+");
  186. if (fp != NULL)
  187. fwrite(buffer, size, 1, fp);
  188. }
  189. #endif
  190. level = rt_hw_interrupt_disable();
  191. fwrite(&c, 1, 1, stdout);
  192. fflush(stdout);
  193. rt_hw_interrupt_enable(level);
  194. return 1;
  195. }
  196. static int console_getc(struct rt_serial_device *serial)
  197. {
  198. char ch;
  199. struct console_uart* uart;
  200. RT_ASSERT(serial != RT_NULL);
  201. uart = (struct console_uart *)serial->parent.user_data;
  202. if (rt_ringbuffer_getchar(&(uart->rb), &ch)) return ch;
  203. return -1;
  204. }
  205. static const struct rt_uart_ops console_uart_ops =
  206. {
  207. console_configure,
  208. console_control,
  209. console_putc,
  210. console_getc,
  211. };
  212. int uart_console_init(void)
  213. {
  214. struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
  215. struct console_uart* uart;
  216. struct rt_serial_device* serial;
  217. uart = &_console_uart;
  218. serial = &_serial;
  219. uart->rx_ready = 0;
  220. serial->ops = &console_uart_ops;
  221. serial->config = config;
  222. /* initialize ring buffer */
  223. rt_ringbuffer_init(&uart->rb, uart->rx_buffer, sizeof(uart->rx_buffer));
  224. /* register UART device */
  225. rt_hw_serial_register(serial, "console",
  226. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  227. uart);
  228. console_lowlevel_init();
  229. return 0;
  230. }