comparison packages/io/serial/current/misc/console.c @ 2:443894e2e912 ecos-v1_2_1-release

Block commit of eCos version 1.2.1
author jlarmour
date Tue, 11 May 1999 12:24:34 +0000
parents
children c38311975d4f
comparison
equal deleted inserted replaced
1:72f549f0d891 2:443894e2e912
1 //==========================================================================
2 //
3 // console.c
4 //
5 // Initial device I/O tests
6 //
7 //==========================================================================
8 //####COPYRIGHTBEGIN####
9 //
10 // -------------------------------------------
11 // The contents of this file are subject to the Cygnus eCos Public License
12 // Version 1.0 (the "License"); you may not use this file except in
13 // compliance with the License. You may obtain a copy of the License at
14 // http://sourceware.cygnus.com/ecos
15 //
16 // Software distributed under the License is distributed on an "AS IS"
17 // basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
18 // License for the specific language governing rights and limitations under
19 // the License.
20 //
21 // The Original Code is eCos - Embedded Cygnus Operating System, released
22 // September 30, 1998.
23 //
24 // The Initial Developer of the Original Code is Cygnus. Portions created
25 // by Cygnus are Copyright (C) 1998,1999 Cygnus Solutions. All Rights Reserved.
26 // -------------------------------------------
27 //
28 //####COPYRIGHTEND####
29 //==========================================================================
30 //#####DESCRIPTIONBEGIN####
31 //
32 // Author(s): gthomas
33 // Contributors: gthomas
34 // Date: 1999-02-05
35 // Description: Minimal testing of "console" I/O
36 //####DESCRIPTIONEND####
37
38 #include <pkgconf/kernel.h>
39 #include <pkgconf/io.h>
40 #include <pkgconf/io_serial.h>
41 #include <cyg/io/io.h>
42 #include <cyg/io/ttyio.h>
43 #include <cyg/infra/testcase.h>
44 #include <cyg/infra/diag.h>
45 #ifdef CYGFUN_KERNEL_API_C
46 #include <cyg/kernel/kapi.h>
47 #include <cyg/hal/hal_arch.h>
48 #define STACK_SIZE CYGNUM_HAL_STACK_SIZE_TYPICAL
49 unsigned char stack[STACK_SIZE];
50 cyg_thread thread_data;
51 cyg_handle_t thread_handle;
52 #endif
53
54 static void
55 dump_buf_with_offset(unsigned char *p,
56 int s,
57 unsigned char *base)
58 {
59 int i, c;
60 if ((unsigned int)s > (unsigned int)p) {
61 s = (unsigned int)s - (unsigned int)p;
62 }
63 while (s > 0) {
64 if (base) {
65 diag_printf("%08X: ", (int)p - (int)base);
66 } else {
67 diag_printf("%08X: ", p);
68 }
69 for (i = 0; i < 16; i++) {
70 if (i < s) {
71 diag_printf("%02X", p[i] & 0xFF);
72 } else {
73 diag_printf(" ");
74 }
75 if ((i % 2) == 1) diag_printf(" ");
76 if ((i % 8) == 7) diag_printf(" ");
77 }
78 diag_printf(" |");
79 for (i = 0; i < 16; i++) {
80 if (i < s) {
81 c = p[i] & 0xFF;
82 if ((c < 0x20) || (c >= 0x7F)) c = '.';
83 } else {
84 c = ' ';
85 }
86 diag_printf("%c", c);
87 }
88 diag_printf("|\n");
89 s -= 16;
90 p += 16;
91 }
92 }
93
94 static void
95 dump_buf(unsigned char *p, int s)
96 {
97 dump_buf_with_offset(p, s, 0);
98 }
99
100 void
101 hang(void)
102 {
103 while (true) ;
104 }
105
106 static int
107 strlen(char *c)
108 {
109 int l = 0;
110 while (*c++) l++;
111 return l;
112 }
113
114 void
115 console_test( CYG_ADDRWORD x )
116 {
117 Cyg_ErrNo res;
118 cyg_io_handle_t handle;
119 char msg[] = "This is a test\n";
120 int msglen = sizeof(msg)-1;
121 char in_msg[80];
122 int in_msglen = sizeof(in_msg)-1;
123 cyg_serial_info_t serial_info;
124 cyg_tty_info_t tty_info;
125 char short_msg[] = "This is a short message\n";
126 char long_msg[] = "This is a longer message 0123456789abcdefghijklmnopqrstuvwxyz\n";
127 char filler[] = " ";
128 char prompt[] = "\nPlease enter some data: ";
129 int i, len;
130
131 res = cyg_io_lookup(CYGDAT_IO_SERIAL_TTY_CONSOLE, &handle);
132 if (res != ENOERR) {
133 diag_printf("Can't lookup - DEVIO error: %d\n", res);
134 return;
135 }
136 len = sizeof(serial_info);
137 res = cyg_io_get_config(handle, CYG_IO_GET_CONFIG_SERIAL_INFO, &serial_info, &len);
138 if (res != ENOERR) {
139 diag_printf("Can't get serial config - DEVIO error: %d\n", res);
140 hang();
141 return;
142 }
143 len = sizeof(tty_info);
144 res = cyg_io_get_config(handle, CYG_IO_GET_CONFIG_TTY_INFO, &tty_info, &len);
145 if (res != ENOERR) {
146 diag_printf("Can't get tty config - DEVIO error: %d\n", res);
147 hang();
148 return;
149 }
150 diag_printf("Config - baud: %d, stop: %d, parity: %d, out flags: %x, in flags: %x\n",
151 serial_info.baud, serial_info.stop, serial_info.parity,
152 tty_info.tty_out_flags, tty_info.tty_in_flags);
153 len = sizeof(serial_info);
154 res = cyg_io_set_config(handle, CYG_IO_SET_CONFIG_SERIAL_INFO, &serial_info, &len);
155 if (res != ENOERR) {
156 diag_printf("Can't set serial config - DEVIO error: %d\n", res);
157 hang();
158 return;
159 }
160 len = sizeof(tty_info);
161 res = cyg_io_set_config(handle, CYG_IO_SET_CONFIG_TTY_INFO, &tty_info, &len);
162 if (res != ENOERR) {
163 diag_printf("Can't set tty config - DEVIO error: %d\n", res);
164 hang();
165 return;
166 }
167 msglen = strlen(msg);
168 res = cyg_io_write(handle, msg, &msglen);
169 if (res != ENOERR) {
170 diag_printf("Can't write data - DEVIO error: %d\n", res);
171 hang();
172 return;
173 }
174 for (i = 0; i < 10; i++) {
175 len = strlen(short_msg);
176 res = cyg_io_write(handle, short_msg, &len);
177 if (res != ENOERR) {
178 diag_printf("Can't write [short] data - DEVIO error: %d\n", res);
179 hang();
180 return;
181 }
182 }
183 for (i = 0; i < 100; i++) {
184 len = (i % 10) + 1;
185 cyg_io_write(handle, filler, &len);
186 len = strlen(long_msg);
187 res = cyg_io_write(handle, long_msg, &len);
188 if (res != ENOERR) {
189 diag_printf("Can't write [long] data - DEVIO error: %d\n", res);
190 hang();
191 return;
192 }
193 }
194 len = strlen(prompt);
195 cyg_io_write(handle, prompt, &len);
196 res = cyg_io_read(handle, in_msg, &in_msglen);
197 if (res != ENOERR) {
198 diag_printf("Can't read data - DEVIO error: %d\n", res);
199 hang();
200 return;
201 }
202 diag_printf("Read %d bytes\n", in_msglen);
203 dump_buf(in_msg, in_msglen);
204 CYG_TEST_PASS_FINISH("Console I/O test OK");
205 }
206
207 void
208 cyg_start(void)
209 {
210 CYG_TEST_INIT();
211 #ifdef CYGFUN_KERNEL_API_C
212 cyg_thread_create(10, // Priority - just a number
213 (cyg_thread_entry_t*)console_test, // entry
214 0, //
215 "console_thread", // Name
216 &stack[0], // Stack
217 STACK_SIZE, // Size
218 &thread_handle, // Handle
219 &thread_data // Thread data structure
220 );
221 cyg_thread_resume(thread_handle);
222 cyg_scheduler_start();
223 #else
224 console_test();
225 #endif
226 }
227 // EOF console.c