comparison packages/io/serial/current/misc/serial.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 d376b777e2ce
comparison
equal deleted inserted replaced
1:72f549f0d891 2:443894e2e912
1 //==========================================================================
2 //
3 // serial.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 "serial" I/O
36 //####DESCRIPTIONEND####
37
38 #include <pkgconf/kernel.h>
39 #include <pkgconf/io.h>
40 #include <cyg/io/io.h>
41 #include <cyg/io/ttyio.h>
42 #include <cyg/infra/diag.h>
43 #include <cyg/infra/testcase.h>
44 #ifdef CYGFUN_KERNEL_API_C
45 #include <cyg/kernel/kapi.h>
46 #include <cyg/hal/hal_arch.h>
47 #define STACK_SIZE CYGNUM_HAL_STACK_SIZE_TYPICAL
48 unsigned char stack[STACK_SIZE];
49 cyg_thread thread_data;
50 cyg_handle_t thread_handle;
51
52 // This routine will be called if the read "times out"
53 static void
54 do_abort(void *handle)
55 {
56 cyg_io_handle_t io_handle = (cyg_io_handle_t)handle;
57 cyg_int32 len = 1; // Need something here
58 cyg_io_get_config(io_handle, CYG_IO_GET_CONFIG_SERIAL_ABORT, 0, &len);
59 }
60
61 #include "timeout.inl"
62 #endif
63
64 static void
65 dump_buf_with_offset(unsigned char *p,
66 int s,
67 unsigned char *base)
68 {
69 int i, c;
70 if ((unsigned int)s > (unsigned int)p) {
71 s = (unsigned int)s - (unsigned int)p;
72 }
73 while (s > 0) {
74 if (base) {
75 diag_printf("%08X: ", (int)p - (int)base);
76 } else {
77 diag_printf("%08X: ", p);
78 }
79 for (i = 0; i < 16; i++) {
80 if (i < s) {
81 diag_printf("%02X", p[i] & 0xFF);
82 } else {
83 diag_printf(" ");
84 }
85 if ((i % 2) == 1) diag_printf(" ");
86 if ((i % 8) == 7) diag_printf(" ");
87 }
88 diag_printf(" |");
89 for (i = 0; i < 16; i++) {
90 if (i < s) {
91 c = p[i] & 0xFF;
92 if ((c < 0x20) || (c >= 0x7F)) c = '.';
93 } else {
94 c = ' ';
95 }
96 diag_printf("%c", c);
97 }
98 diag_printf("|\n");
99 s -= 16;
100 p += 16;
101 }
102 }
103
104 static void
105 dump_buf(unsigned char *p, int s)
106 {
107 dump_buf_with_offset(p, s, 0);
108 }
109
110 void
111 hang(void)
112 {
113 while (true) ;
114 }
115
116 static int
117 strlen(char *c)
118 {
119 int l = 0;
120 while (*c++) l++;
121 return l;
122 }
123
124 void
125 serial_test( void )
126 {
127 Cyg_ErrNo res;
128 cyg_io_handle_t handle;
129 char msg[] = "This is a test\n";
130 int msglen = sizeof(msg)-1;
131 char in_msg[80];
132 int in_msglen = sizeof(in_msg)-1;
133 cyg_serial_info_t serial_info;
134 cyg_tty_info_t tty_info;
135 char short_msg[] = "This is a short message\n";
136 char long_msg[] = "This is a longer message 0123456789abcdefghijklmnopqrstuvwxyz\n";
137 char filler[] = " ";
138 char prompt[] = "\nPlease enter some data: ";
139 int i, len;
140 #ifdef CYGFUN_KERNEL_API_C
141 cyg_uint32 stamp;
142 #endif
143
144 res = cyg_io_lookup("/dev/tty1", &handle);
145 if (res != ENOERR) {
146 diag_printf("Can't lookup - DEVIO error: %d\n", res);
147 return;
148 }
149 len = sizeof(serial_info);
150 res = cyg_io_get_config(handle, CYG_IO_GET_CONFIG_SERIAL_INFO, &serial_info, &len);
151 if (res != ENOERR) {
152 diag_printf("Can't get serial config - DEVIO error: %d\n", res);
153 hang();
154 return;
155 }
156 len = sizeof(tty_info);
157 res = cyg_io_get_config(handle, CYG_IO_GET_CONFIG_TTY_INFO, &tty_info, &len);
158 if (res != ENOERR) {
159 diag_printf("Can't get tty config - DEVIO error: %d\n", res);
160 hang();
161 return;
162 }
163 diag_printf("Config - baud: %d, stop: %d, parity: %d, out flags: %x, in flags: %x\n",
164 serial_info.baud, serial_info.stop, serial_info.parity,
165 tty_info.tty_out_flags, tty_info.tty_in_flags);
166 len = sizeof(serial_info);
167 res = cyg_io_set_config(handle, CYG_IO_SET_CONFIG_SERIAL_INFO, &serial_info, &len);
168 if (res != ENOERR) {
169 diag_printf("Can't set serial config - DEVIO error: %d\n", res);
170 hang();
171 return;
172 }
173 len = sizeof(tty_info);
174 res = cyg_io_set_config(handle, CYG_IO_SET_CONFIG_TTY_INFO, &tty_info, &len);
175 if (res != ENOERR) {
176 diag_printf("Can't set tty config - DEVIO error: %d\n", res);
177 hang();
178 return;
179 }
180 msglen = strlen(msg);
181 res = cyg_io_write(handle, msg, &msglen);
182 if (res != ENOERR) {
183 diag_printf("Can't write data - DEVIO error: %d\n", res);
184 hang();
185 return;
186 }
187 for (i = 0; i < 10; i++) {
188 len = strlen(short_msg);
189 res = cyg_io_write(handle, short_msg, &len);
190 if (res != ENOERR) {
191 diag_printf("Can't write [short] data - DEVIO error: %d\n", res);
192 hang();
193 return;
194 }
195 }
196 for (i = 0; i < 100; i++) {
197 len = (i % 10) + 1;
198 cyg_io_write(handle, filler, &len);
199 len = strlen(long_msg);
200 res = cyg_io_write(handle, long_msg, &len);
201 if (res != ENOERR) {
202 diag_printf("Can't write [long] data - DEVIO error: %d\n", res);
203 hang();
204 return;
205 }
206 }
207 len = strlen(prompt);
208 cyg_io_write(handle, prompt, &len);
209 #ifdef CYGFUN_KERNEL_API_C
210 stamp = timeout(1000, do_abort, handle);
211 #endif
212 res = cyg_io_read(handle, in_msg, &in_msglen);
213 if (res != ENOERR) {
214 diag_printf("Can't read data - DEVIO error: %d\n", res);
215 hang();
216 return;
217 }
218 #ifdef CYGFUN_KERNEL_API_C
219 untimeout(stamp);
220 #endif
221 diag_printf("Read %d bytes\n", in_msglen);
222 dump_buf(in_msg, in_msglen);
223 CYG_TEST_PASS_FINISH("Serial I/O test OK");
224 }
225
226 void
227 cyg_start(void)
228 {
229 CYG_TEST_INIT();
230 #ifdef CYGFUN_KERNEL_API_C
231 cyg_thread_create(10, // Priority - just a number
232 (cyg_thread_entry_t*)serial_test, // entry
233 (cyg_addrword_t) 0, // entry data
234 "serial_thread", // Name
235 &stack[0], // Stack
236 STACK_SIZE, // Size
237 &thread_handle, // Handle
238 &thread_data // Thread data structure
239 );
240 cyg_thread_resume(thread_handle);
241 cyg_scheduler_start();
242 #else
243 serial_test();
244 #endif
245 }
246 // EOF serial.c