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