uart_console.c 6.0 KB

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