OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-2.0/] [packages/] [io/] [serial/] [v2_0/] [misc/] [serial.c] - Blame information for rev 174

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.