uart_console.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. /*
  28. * Handler for OSKey Thread
  29. */
  30. static HANDLE OSKey_Thread;
  31. static DWORD OSKey_ThreadID;
  32. static DWORD WINAPI ThreadforKeyGet(LPVOID lpParam);
  33. void console_lowlevel_init(void)
  34. {
  35. /*
  36. * create serial thread that receive key input from keyboard
  37. */
  38. OSKey_Thread = CreateThread(NULL,
  39. 0,
  40. (LPTHREAD_START_ROUTINE)ThreadforKeyGet,
  41. 0,
  42. CREATE_SUSPENDED,
  43. &OSKey_ThreadID);
  44. if (OSKey_Thread == NULL)
  45. {
  46. //Display Error Message
  47. return;
  48. }
  49. SetThreadPriority(OSKey_Thread,
  50. THREAD_PRIORITY_NORMAL);
  51. SetThreadPriorityBoost(OSKey_Thread,
  52. TRUE);
  53. SetThreadAffinityMask(OSKey_Thread,
  54. 0x01);
  55. /*
  56. * Start OS get key Thread
  57. */
  58. ResumeThread(OSKey_Thread);
  59. }
  60. static DWORD WINAPI ThreadforKeyGet(LPVOID lpParam)
  61. #else /* POSIX version */
  62. #include <pthread.h>
  63. #include <semaphore.h>
  64. #include <stdlib.h>
  65. #include <signal.h>
  66. #include <termios.h> /* for tcxxxattr, ECHO, etc */
  67. #include <unistd.h> /* for STDIN_FILENO */
  68. static void * ThreadforKeyGet(void * lpParam);
  69. static pthread_t OSKey_Thread;
  70. void console_lowlevel_init(void)
  71. {
  72. int res;
  73. res = pthread_create(&OSKey_Thread, NULL, &ThreadforKeyGet, NULL);
  74. if (res)
  75. {
  76. printf("pthread create faild, <%d>\n", res);
  77. exit(EXIT_FAILURE);
  78. }
  79. }
  80. static struct termios oldt, newt;
  81. /*simulate windows' getch(), it works!!*/
  82. static void set_stty(void)
  83. {
  84. /* get terminal input's attribute */
  85. tcgetattr(STDIN_FILENO, &oldt);
  86. newt = oldt;
  87. /* set termios' local mode */
  88. newt.c_lflag &= ~(ECHO|ICANON);
  89. tcsetattr(STDIN_FILENO, TCSANOW, &newt);
  90. }
  91. void restore_stty(void)
  92. {
  93. /* recover terminal's attribute */
  94. tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
  95. }
  96. #define getch getchar
  97. static void * ThreadforKeyGet(void * lpParam)
  98. #endif /* not _WIN32*/
  99. {
  100. /*
  101. * left key(��)�� 0xe04b
  102. * up key(��)�� 0xe048
  103. * right key(��)�� 0xe04d
  104. * down key(��)�� 0xe050
  105. */
  106. unsigned char key;
  107. #ifndef _WIN32
  108. sigset_t sigmask, oldmask;
  109. /* set the getchar without buffer */
  110. sigfillset(&sigmask);
  111. pthread_sigmask(SIG_BLOCK, &sigmask, &oldmask);
  112. set_stty();
  113. #endif
  114. (void)lpParam; //prevent compiler warnings
  115. for (;;)
  116. {
  117. key = getch();
  118. #ifdef _WIN32
  119. if (key == 0xE0)
  120. {
  121. key = getch();
  122. if (key == 0x48) //up key , 0x1b 0x5b 0x41
  123. {
  124. SAVEKEY(0x1b);
  125. SAVEKEY(0x5b);
  126. SAVEKEY(0x41);
  127. }
  128. else if (key == 0x50)//0x1b 0x5b 0x42
  129. {
  130. SAVEKEY(0x1b);
  131. SAVEKEY(0x5b);
  132. SAVEKEY(0x42);
  133. }
  134. else if (key == 0x4b)//<- 0x1b 0x5b 0x44
  135. {
  136. SAVEKEY(0x1b);
  137. SAVEKEY(0x5b);
  138. SAVEKEY(0x44);
  139. }
  140. else if (key == 0x4d)//<- 0x1b 0x5b 0x43
  141. {
  142. SAVEKEY(0x1b);
  143. SAVEKEY(0x5b);
  144. SAVEKEY(0x43);
  145. }
  146. continue;
  147. }
  148. #endif
  149. SAVEKEY(key);
  150. /* Notfiy serial ISR */
  151. rt_hw_serial_isr(&_serial, RT_SERIAL_EVENT_RX_IND);
  152. }
  153. } /*** ThreadforKeyGet ***/
  154. static rt_err_t console_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
  155. {
  156. /* no baudrate, nothing */
  157. return RT_EOK;
  158. }
  159. static rt_err_t console_control(struct rt_serial_device *serial, int cmd, void *arg)
  160. {
  161. struct console_uart* uart;
  162. RT_ASSERT(serial != RT_NULL);
  163. uart = (struct console_uart *)serial->parent.user_data;
  164. switch (cmd)
  165. {
  166. case RT_DEVICE_CTRL_CLR_INT:
  167. uart->rx_ready = 0;
  168. break;
  169. case RT_DEVICE_CTRL_SET_INT:
  170. uart->rx_ready = 1;
  171. break;
  172. }
  173. return RT_EOK;
  174. }
  175. static int console_putc(struct rt_serial_device *serial, char c)
  176. {
  177. int level;
  178. struct console_uart* uart;
  179. RT_ASSERT(serial != RT_NULL);
  180. uart = (struct console_uart *)serial->parent.user_data;
  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. }