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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [packages/] [io/] [serial/] [current/] [misc/] [serial.c] - Blame information for rev 825

Go to most recent revision | Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

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