uart_console.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. } /*** ThreadforKeyGet ***/
  155. static rt_err_t console_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
  156. {
  157. /* no baudrate, nothing */
  158. return RT_EOK;
  159. }
  160. static rt_err_t console_control(struct rt_serial_device *serial, int cmd, void *arg)
  161. {
  162. struct console_uart* uart;
  163. RT_ASSERT(serial != RT_NULL);
  164. uart = (struct console_uart *)serial->parent.user_data;
  165. switch (cmd)
  166. {
  167. case RT_DEVICE_CTRL_CLR_INT:
  168. uart->rx_ready = 0;
  169. break;
  170. case RT_DEVICE_CTRL_SET_INT:
  171. uart->rx_ready = 1;
  172. break;
  173. }
  174. return RT_EOK;
  175. }
  176. static int console_putc(struct rt_serial_device *serial, char c)
  177. {
  178. int level;
  179. struct console_uart* uart;
  180. RT_ASSERT(serial != RT_NULL);
  181. uart = (struct console_uart *)serial->parent.user_data;
  182. #if 0 /* Enable it if you want to save the console log */
  183. {
  184. static FILE* fp = NULL;
  185. if (fp == NULL)
  186. fp = fopen("log.txt", "wb+");
  187. if (fp != NULL)
  188. fwrite(buffer, size, 1, fp);
  189. }
  190. #endif
  191. level = rt_hw_interrupt_disable();
  192. fwrite(&c, 1, 1, stdout);
  193. fflush(stdout);
  194. rt_hw_interrupt_enable(level);
  195. return 1;
  196. }
  197. static int console_getc(struct rt_serial_device *serial)
  198. {
  199. char ch;
  200. struct console_uart* uart;
  201. RT_ASSERT(serial != RT_NULL);
  202. uart = (struct console_uart *)serial->parent.user_data;
  203. if (rt_ringbuffer_getchar(&(uart->rb), &ch)) return ch;
  204. return -1;
  205. }
  206. static const struct rt_uart_ops console_uart_ops =
  207. {
  208. console_configure,
  209. console_control,
  210. console_putc,
  211. console_getc,
  212. };
  213. int uart_console_init(void)
  214. {
  215. struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
  216. struct console_uart* uart;
  217. struct rt_serial_device* serial;
  218. uart = &_console_uart;
  219. serial = &_serial;
  220. uart->rx_ready = 0;
  221. serial->ops = &console_uart_ops;
  222. serial->config = config;
  223. /* initialize ring buffer */
  224. rt_ringbuffer_init(&uart->rb, uart->rx_buffer, sizeof(uart->rx_buffer));
  225. /* register UART device */
  226. rt_hw_serial_register(serial, "console",
  227. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  228. uart);
  229. console_lowlevel_init();
  230. return 0;
  231. }