console.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. /*
  2. * File : console.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006, RT-Thread Development Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://openlab.rt-thread.com/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2006-09-15 QiuYi the first version
  13. */
  14. #include <rtthread.h>
  15. #include <rthw.h>
  16. #include <bsp.h>
  17. static unsigned addr_6845;
  18. static rt_uint16_t *crt_buf;
  19. static rt_int16_t crt_pos;
  20. extern void init_keyboard();
  21. extern void rt_keyboard_isr(void);
  22. extern rt_bool_t rt_keyboard_getc(char* c);
  23. extern void rt_serial_init(void);
  24. extern char rt_serial_getc(void);
  25. extern void rt_serial_putc(const char c);
  26. void rt_console_putc(int c);
  27. /**
  28. * @addtogroup QEMU
  29. */
  30. /*@{*/
  31. /**
  32. * This function initializes cga
  33. *
  34. */
  35. void rt_cga_init(void)
  36. {
  37. rt_uint16_t volatile *cp;
  38. rt_uint16_t was;
  39. rt_uint32_t pos;
  40. cp = (rt_uint16_t *) (CGA_BUF);
  41. was = *cp;
  42. *cp = (rt_uint16_t) 0xA55A;
  43. if (*cp != 0xA55A)
  44. {
  45. cp = (rt_uint16_t *) (MONO_BUF);
  46. addr_6845 = MONO_BASE;
  47. }
  48. else
  49. {
  50. *cp = was;
  51. addr_6845 = CGA_BASE;
  52. }
  53. /* Extract cursor location */
  54. outb(addr_6845, 14);
  55. pos = inb(addr_6845+1) << 8;
  56. outb(addr_6845, 15);
  57. pos |= inb(addr_6845+1);
  58. crt_buf = (rt_uint16_t *)cp;
  59. crt_pos = pos;
  60. }
  61. /**
  62. * This function will write a character to cga
  63. *
  64. * @param c the char to write
  65. */
  66. static void rt_cga_putc(int c)
  67. {
  68. /* if no attribute given, then use black on white */
  69. if (!(c & ~0xff)) c |= 0x0700;
  70. switch (c & 0xff)
  71. {
  72. case '\b':
  73. if (crt_pos > 0)
  74. {
  75. crt_pos--;
  76. crt_buf[crt_pos] = (c&~0xff) | ' ';
  77. }
  78. break;
  79. case '\n':
  80. crt_pos += CRT_COLS;
  81. /* cascade */
  82. case '\r':
  83. crt_pos -= (crt_pos % CRT_COLS);
  84. break;
  85. case '\t':
  86. rt_console_putc(' ');
  87. rt_console_putc(' ');
  88. rt_console_putc(' ');
  89. rt_console_putc(' ');
  90. rt_console_putc(' ');
  91. break;
  92. default:
  93. crt_buf[crt_pos++] = c; /* write the character */
  94. break;
  95. }
  96. if (crt_pos >= CRT_SIZE)
  97. {
  98. rt_int32_t i;
  99. rt_memcpy(crt_buf, crt_buf + CRT_COLS, (CRT_SIZE - CRT_COLS) << 1);
  100. for (i = CRT_SIZE - CRT_COLS; i < CRT_SIZE; i++)
  101. crt_buf[i] = 0x0700 | ' ';
  102. crt_pos -= CRT_COLS;
  103. }
  104. outb(addr_6845, 14);
  105. outb(addr_6845+1, crt_pos >> 8);
  106. outb(addr_6845, 15);
  107. outb(addr_6845+1, crt_pos);
  108. }
  109. /**
  110. * This function will write a character to serial an cga
  111. *
  112. * @param c the char to write
  113. */
  114. void rt_console_putc(int c)
  115. {
  116. rt_cga_putc(c);
  117. rt_serial_putc(c);
  118. }
  119. /* RT-Thread Device Interface */
  120. #define CONSOLE_RX_BUFFER_SIZE 64
  121. static struct rt_device console_device;
  122. static rt_uint8_t rx_buffer[CONSOLE_RX_BUFFER_SIZE];
  123. static rt_uint32_t read_index, save_index;
  124. static rt_err_t rt_console_init (rt_device_t dev)
  125. {
  126. return RT_EOK;
  127. }
  128. static rt_err_t rt_console_open(rt_device_t dev, rt_uint16_t oflag)
  129. {
  130. return RT_EOK;
  131. }
  132. static rt_err_t rt_console_close(rt_device_t dev)
  133. {
  134. return RT_EOK;
  135. }
  136. static rt_err_t rt_console_control(rt_device_t dev, int cmd, void *args)
  137. {
  138. return RT_EOK;
  139. }
  140. static rt_size_t rt_console_write(rt_device_t dev, rt_off_t pos, const void * buffer, rt_size_t size)
  141. {
  142. rt_size_t i = size;
  143. const char* str = buffer;
  144. while(i--)
  145. {
  146. rt_console_putc(*str++);
  147. }
  148. return size;
  149. }
  150. static rt_size_t rt_console_read(rt_device_t dev, rt_off_t pos, void* buffer, rt_size_t size)
  151. {
  152. rt_uint8_t* ptr = buffer;
  153. rt_err_t err_code = RT_EOK;
  154. /* interrupt mode Rx */
  155. while (size)
  156. {
  157. rt_base_t level;
  158. /* disable interrupt */
  159. level = rt_hw_interrupt_disable();
  160. if (read_index != save_index)
  161. {
  162. /* read a character */
  163. *ptr++ = rx_buffer[read_index];
  164. size--;
  165. /* move to next position */
  166. read_index ++;
  167. if (read_index >= CONSOLE_RX_BUFFER_SIZE)
  168. read_index = 0;
  169. }
  170. else
  171. {
  172. /* set error code */
  173. err_code = -RT_EEMPTY;
  174. /* enable interrupt */
  175. rt_hw_interrupt_enable(level);
  176. break;
  177. }
  178. /* enable interrupt */
  179. rt_hw_interrupt_enable(level);
  180. }
  181. /* set error code */
  182. rt_set_errno(err_code);
  183. return (rt_uint32_t)ptr - (rt_uint32_t)buffer;
  184. }
  185. static void rt_console_isr(int vector, void* param)
  186. {
  187. char c;
  188. rt_bool_t ret;
  189. rt_base_t level;
  190. if(INTUART0_RX == vector)
  191. {
  192. c = rt_serial_getc();
  193. ret = RT_TRUE;
  194. }
  195. else
  196. {
  197. rt_keyboard_isr();
  198. ret = rt_keyboard_getc(&c);
  199. }
  200. if(ret == RT_FALSE)
  201. {
  202. /* do nothing */
  203. }
  204. else
  205. {
  206. /* disable interrupt */
  207. level = rt_hw_interrupt_disable();
  208. /* save character */
  209. rx_buffer[save_index] = c;
  210. save_index ++;
  211. if (save_index >= CONSOLE_RX_BUFFER_SIZE)
  212. save_index = 0;
  213. /* if the next position is read index, discard this 'read char' */
  214. if (save_index == read_index)
  215. {
  216. read_index ++;
  217. if (read_index >= CONSOLE_RX_BUFFER_SIZE)
  218. read_index = 0;
  219. }
  220. /* enable interrupt */
  221. rt_hw_interrupt_enable(level);
  222. }
  223. /* invoke callback */
  224. if (console_device.rx_indicate != RT_NULL)
  225. {
  226. rt_size_t rx_length;
  227. /* get rx length */
  228. rx_length = read_index > save_index ?
  229. CONSOLE_RX_BUFFER_SIZE - read_index + save_index :
  230. save_index - read_index;
  231. if(rx_length > 0)
  232. {
  233. console_device.rx_indicate(&console_device, rx_length);
  234. }
  235. }
  236. else
  237. {
  238. }
  239. }
  240. /**
  241. * This function initializes console
  242. *
  243. */
  244. void rt_hw_console_init(void)
  245. {
  246. rt_cga_init();
  247. rt_serial_init();
  248. init_keyboard();
  249. /* install keyboard isr */
  250. rt_hw_interrupt_install(INTKEYBOARD, rt_console_isr, RT_NULL, "kbd");
  251. rt_hw_interrupt_umask(INTKEYBOARD);
  252. rt_hw_interrupt_install(INTUART0_RX, rt_console_isr, RT_NULL, "COM1");
  253. rt_hw_interrupt_umask(INTUART0_RX);
  254. console_device.type = RT_Device_Class_Char;
  255. console_device.rx_indicate = RT_NULL;
  256. console_device.tx_complete = RT_NULL;
  257. console_device.init = rt_console_init;
  258. console_device.open = rt_console_open;
  259. console_device.close = rt_console_close;
  260. console_device.read = rt_console_read;
  261. console_device.write = rt_console_write;
  262. console_device.control = rt_console_control;
  263. console_device.user_data = RT_NULL;
  264. /* register a character device */
  265. rt_device_register(&console_device,
  266. "console",
  267. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_STREAM);
  268. }
  269. /**
  270. * This function is used to display a string on console, normally, it's
  271. * invoked by rt_kprintf
  272. *
  273. * @param str the displayed string
  274. *
  275. * Modified:
  276. * caoxl 2009-10-14
  277. * the name is change to rt_hw_console_output in the v0.3.0
  278. *
  279. */
  280. void rt_hw_console_output(const char* str)
  281. {
  282. while (*str)
  283. {
  284. rt_console_putc (*str++);
  285. }
  286. }
  287. /*@}*/