uart_console.c 6.0 KB

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