1 |
1254 |
phoenix |
//==========================================================================
|
2 |
|
|
//
|
3 |
|
|
// console.c
|
4 |
|
|
//
|
5 |
|
|
// Initial device I/O tests
|
6 |
|
|
//
|
7 |
|
|
//==========================================================================
|
8 |
|
|
//####ECOSGPLCOPYRIGHTBEGIN####
|
9 |
|
|
// -------------------------------------------
|
10 |
|
|
// This file is part of eCos, the Embedded Configurable Operating System.
|
11 |
|
|
// Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
|
12 |
|
|
//
|
13 |
|
|
// eCos is free software; you can redistribute it and/or modify it under
|
14 |
|
|
// the terms of the GNU General Public License as published by the Free
|
15 |
|
|
// Software Foundation; either version 2 or (at your option) any later version.
|
16 |
|
|
//
|
17 |
|
|
// eCos is distributed in the hope that it will be useful, but WITHOUT ANY
|
18 |
|
|
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
19 |
|
|
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
20 |
|
|
// for more details.
|
21 |
|
|
//
|
22 |
|
|
// You should have received a copy of the GNU General Public License along
|
23 |
|
|
// with eCos; if not, write to the Free Software Foundation, Inc.,
|
24 |
|
|
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
|
25 |
|
|
//
|
26 |
|
|
// As a special exception, if other files instantiate templates or use macros
|
27 |
|
|
// or inline functions from this file, or you compile this file and link it
|
28 |
|
|
// with other works to produce a work based on this file, this file does not
|
29 |
|
|
// by itself cause the resulting work to be covered by the GNU General Public
|
30 |
|
|
// License. However the source code for this file must still be made available
|
31 |
|
|
// in accordance with section (3) of the GNU General Public License.
|
32 |
|
|
//
|
33 |
|
|
// This exception does not invalidate any other reasons why a work based on
|
34 |
|
|
// this file might be covered by the GNU General Public License.
|
35 |
|
|
//
|
36 |
|
|
// Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
|
37 |
|
|
// at http://sources.redhat.com/ecos/ecos-license/
|
38 |
|
|
// -------------------------------------------
|
39 |
|
|
//####ECOSGPLCOPYRIGHTEND####
|
40 |
|
|
//==========================================================================
|
41 |
|
|
//#####DESCRIPTIONBEGIN####
|
42 |
|
|
//
|
43 |
|
|
// Author(s): gthomas
|
44 |
|
|
// Contributors: gthomas
|
45 |
|
|
// Date: 1999-02-05
|
46 |
|
|
// Description: Minimal testing of "console" I/O
|
47 |
|
|
//####DESCRIPTIONEND####
|
48 |
|
|
|
49 |
|
|
#include <pkgconf/kernel.h>
|
50 |
|
|
#include <pkgconf/io.h>
|
51 |
|
|
#include <pkgconf/io_serial.h>
|
52 |
|
|
#include <cyg/io/io.h>
|
53 |
|
|
#include <cyg/io/ttyio.h>
|
54 |
|
|
#include <cyg/infra/testcase.h>
|
55 |
|
|
#include <cyg/infra/diag.h>
|
56 |
|
|
#ifdef CYGFUN_KERNEL_API_C
|
57 |
|
|
#include <cyg/kernel/kapi.h>
|
58 |
|
|
#include <cyg/hal/hal_arch.h>
|
59 |
|
|
#define STACK_SIZE CYGNUM_HAL_STACK_SIZE_TYPICAL
|
60 |
|
|
unsigned char stack[STACK_SIZE];
|
61 |
|
|
cyg_thread thread_data;
|
62 |
|
|
cyg_handle_t thread_handle;
|
63 |
|
|
#endif
|
64 |
|
|
|
65 |
|
|
static void
|
66 |
|
|
dump_buf_with_offset(unsigned char *p,
|
67 |
|
|
int s,
|
68 |
|
|
unsigned char *base)
|
69 |
|
|
{
|
70 |
|
|
int i, c;
|
71 |
|
|
if ((unsigned int)s > (unsigned int)p) {
|
72 |
|
|
s = (unsigned int)s - (unsigned int)p;
|
73 |
|
|
}
|
74 |
|
|
while (s > 0) {
|
75 |
|
|
if (base) {
|
76 |
|
|
diag_printf("%08X: ", (int)p - (int)base);
|
77 |
|
|
} else {
|
78 |
|
|
diag_printf("%08X: ", p);
|
79 |
|
|
}
|
80 |
|
|
for (i = 0; i < 16; i++) {
|
81 |
|
|
if (i < s) {
|
82 |
|
|
diag_printf("%02X", p[i] & 0xFF);
|
83 |
|
|
} else {
|
84 |
|
|
diag_printf(" ");
|
85 |
|
|
}
|
86 |
|
|
if ((i % 2) == 1) diag_printf(" ");
|
87 |
|
|
if ((i % 8) == 7) diag_printf(" ");
|
88 |
|
|
}
|
89 |
|
|
diag_printf(" |");
|
90 |
|
|
for (i = 0; i < 16; i++) {
|
91 |
|
|
if (i < s) {
|
92 |
|
|
c = p[i] & 0xFF;
|
93 |
|
|
if ((c < 0x20) || (c >= 0x7F)) c = '.';
|
94 |
|
|
} else {
|
95 |
|
|
c = ' ';
|
96 |
|
|
}
|
97 |
|
|
diag_printf("%c", c);
|
98 |
|
|
}
|
99 |
|
|
diag_printf("|\n");
|
100 |
|
|
s -= 16;
|
101 |
|
|
p += 16;
|
102 |
|
|
}
|
103 |
|
|
}
|
104 |
|
|
|
105 |
|
|
static void
|
106 |
|
|
dump_buf(unsigned char *p, int s)
|
107 |
|
|
{
|
108 |
|
|
dump_buf_with_offset(p, s, 0);
|
109 |
|
|
}
|
110 |
|
|
|
111 |
|
|
void
|
112 |
|
|
hang(void)
|
113 |
|
|
{
|
114 |
|
|
while (true) ;
|
115 |
|
|
}
|
116 |
|
|
|
117 |
|
|
static int
|
118 |
|
|
strlen(char *c)
|
119 |
|
|
{
|
120 |
|
|
int l = 0;
|
121 |
|
|
while (*c++) l++;
|
122 |
|
|
return l;
|
123 |
|
|
}
|
124 |
|
|
|
125 |
|
|
void
|
126 |
|
|
console_test( CYG_ADDRWORD x )
|
127 |
|
|
{
|
128 |
|
|
Cyg_ErrNo res;
|
129 |
|
|
cyg_io_handle_t handle;
|
130 |
|
|
char msg[] = "This is a test\n";
|
131 |
|
|
int msglen = sizeof(msg)-1;
|
132 |
|
|
char in_msg[80];
|
133 |
|
|
int in_msglen = sizeof(in_msg)-1;
|
134 |
|
|
cyg_serial_info_t serial_info;
|
135 |
|
|
cyg_tty_info_t tty_info;
|
136 |
|
|
char short_msg[] = "This is a short message\n";
|
137 |
|
|
char long_msg[] = "This is a longer message 0123456789abcdefghijklmnopqrstuvwxyz\n";
|
138 |
|
|
char filler[] = " ";
|
139 |
|
|
char prompt[] = "\nPlease enter some data: ";
|
140 |
|
|
int i, len;
|
141 |
|
|
|
142 |
|
|
res = cyg_io_lookup(CYGDAT_IO_SERIAL_TTY_CONSOLE, &handle);
|
143 |
|
|
if (res != ENOERR) {
|
144 |
|
|
diag_printf("Can't lookup - DEVIO error: %d\n", res);
|
145 |
|
|
return;
|
146 |
|
|
}
|
147 |
|
|
len = sizeof(serial_info);
|
148 |
|
|
res = cyg_io_get_config(handle, CYG_IO_GET_CONFIG_SERIAL_INFO, &serial_info, &len);
|
149 |
|
|
if (res != ENOERR) {
|
150 |
|
|
diag_printf("Can't get serial config - DEVIO error: %d\n", res);
|
151 |
|
|
hang();
|
152 |
|
|
return;
|
153 |
|
|
}
|
154 |
|
|
len = sizeof(tty_info);
|
155 |
|
|
res = cyg_io_get_config(handle, CYG_IO_GET_CONFIG_TTY_INFO, &tty_info, &len);
|
156 |
|
|
if (res != ENOERR) {
|
157 |
|
|
diag_printf("Can't get tty config - DEVIO error: %d\n", res);
|
158 |
|
|
hang();
|
159 |
|
|
return;
|
160 |
|
|
}
|
161 |
|
|
diag_printf("Config - baud: %d, stop: %d, parity: %d, out flags: %x, in flags: %x\n",
|
162 |
|
|
serial_info.baud, serial_info.stop, serial_info.parity,
|
163 |
|
|
tty_info.tty_out_flags, tty_info.tty_in_flags);
|
164 |
|
|
len = sizeof(serial_info);
|
165 |
|
|
res = cyg_io_set_config(handle, CYG_IO_SET_CONFIG_SERIAL_INFO, &serial_info, &len);
|
166 |
|
|
if (res != ENOERR) {
|
167 |
|
|
diag_printf("Can't set serial config - DEVIO error: %d\n", res);
|
168 |
|
|
hang();
|
169 |
|
|
return;
|
170 |
|
|
}
|
171 |
|
|
len = sizeof(tty_info);
|
172 |
|
|
res = cyg_io_set_config(handle, CYG_IO_SET_CONFIG_TTY_INFO, &tty_info, &len);
|
173 |
|
|
if (res != ENOERR) {
|
174 |
|
|
diag_printf("Can't set tty config - DEVIO error: %d\n", res);
|
175 |
|
|
hang();
|
176 |
|
|
return;
|
177 |
|
|
}
|
178 |
|
|
msglen = strlen(msg);
|
179 |
|
|
res = cyg_io_write(handle, msg, &msglen);
|
180 |
|
|
if (res != ENOERR) {
|
181 |
|
|
diag_printf("Can't write data - DEVIO error: %d\n", res);
|
182 |
|
|
hang();
|
183 |
|
|
return;
|
184 |
|
|
}
|
185 |
|
|
for (i = 0; i < 10; i++) {
|
186 |
|
|
len = strlen(short_msg);
|
187 |
|
|
res = cyg_io_write(handle, short_msg, &len);
|
188 |
|
|
if (res != ENOERR) {
|
189 |
|
|
diag_printf("Can't write [short] data - DEVIO error: %d\n", res);
|
190 |
|
|
hang();
|
191 |
|
|
return;
|
192 |
|
|
}
|
193 |
|
|
}
|
194 |
|
|
for (i = 0; i < 100; i++) {
|
195 |
|
|
len = (i % 10) + 1;
|
196 |
|
|
cyg_io_write(handle, filler, &len);
|
197 |
|
|
len = strlen(long_msg);
|
198 |
|
|
res = cyg_io_write(handle, long_msg, &len);
|
199 |
|
|
if (res != ENOERR) {
|
200 |
|
|
diag_printf("Can't write [long] data - DEVIO error: %d\n", res);
|
201 |
|
|
hang();
|
202 |
|
|
return;
|
203 |
|
|
}
|
204 |
|
|
}
|
205 |
|
|
len = strlen(prompt);
|
206 |
|
|
cyg_io_write(handle, prompt, &len);
|
207 |
|
|
res = cyg_io_read(handle, in_msg, &in_msglen);
|
208 |
|
|
if (res != ENOERR) {
|
209 |
|
|
diag_printf("Can't read data - DEVIO error: %d\n", res);
|
210 |
|
|
hang();
|
211 |
|
|
return;
|
212 |
|
|
}
|
213 |
|
|
diag_printf("Read %d bytes\n", in_msglen);
|
214 |
|
|
dump_buf(in_msg, in_msglen);
|
215 |
|
|
CYG_TEST_PASS_FINISH("Console I/O test OK");
|
216 |
|
|
}
|
217 |
|
|
|
218 |
|
|
void
|
219 |
|
|
cyg_start(void)
|
220 |
|
|
{
|
221 |
|
|
CYG_TEST_INIT();
|
222 |
|
|
#ifdef CYGFUN_KERNEL_API_C
|
223 |
|
|
cyg_thread_create(10, // Priority - just a number
|
224 |
|
|
(cyg_thread_entry_t*)console_test, // entry
|
225 |
|
|
0, //
|
226 |
|
|
"console_thread", // Name
|
227 |
|
|
&stack[0], // Stack
|
228 |
|
|
STACK_SIZE, // Size
|
229 |
|
|
&thread_handle, // Handle
|
230 |
|
|
&thread_data // Thread data structure
|
231 |
|
|
);
|
232 |
|
|
cyg_thread_resume(thread_handle);
|
233 |
|
|
cyg_scheduler_start();
|
234 |
|
|
#else
|
235 |
|
|
console_test();
|
236 |
|
|
#endif
|
237 |
|
|
}
|
238 |
|
|
// EOF console.c
|