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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-2.0/] [packages/] [hal/] [calmrisc16/] [ceb/] [v2_0/] [src/] [ser.c] - Blame information for rev 773

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

Line No. Rev Author Line
1 27 unneback
//=============================================================================
2
//
3
//      ser.c
4
//
5
//      Simple driver for the MDSChip serial port
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):   msalter
44
// Contributors:msalter
45
// Date:        2001-02-12
46
// Description: Simple driver for the MDSChip serial port
47
//
48
//####DESCRIPTIONEND####
49
//
50
//=============================================================================
51
 
52
#include <pkgconf/hal.h>
53
#include <pkgconf/system.h>
54
#include CYGBLD_HAL_PLATFORM_H
55
 
56
#include <cyg/hal/hal_arch.h>           // SAVE/RESTORE GP macros
57
#include <cyg/hal/hal_io.h>             // IO macros
58
#include <cyg/hal/hal_if.h>             // interface API
59
#include <cyg/hal/hal_intr.h>           // HAL_ENABLE/MASK/UNMASK_INTERRUPTS
60
#include <cyg/hal/hal_misc.h>           // Helper functions
61
#include <cyg/hal/drv_api.h>            // CYG_ISR_HANDLED
62
 
63
// We have no control over baud rate
64
#if CYGNUM_HAL_VIRTUAL_VECTOR_CONSOLE_CHANNEL_BAUD==57600
65
#define CYG_DEV_SERIAL_BAUD_DIVISOR    BAUD_57600
66
#endif
67
 
68
#ifndef CYG_DEV_SERIAL_BAUD_DIVISOR
69
#error Missing/incorrect serial baud rate defined - CDL error?
70
#endif
71
 
72
//-----------------------------------------------------------------------------
73
typedef struct {
74
    cyg_uint8* base;
75
    cyg_int32 msec_timeout;
76
    int isr_vector;
77
} channel_data_t;
78
 
79
static channel_data_t channels[1] = {
80
    { (cyg_uint8*)0, 1000, 0}
81
};
82
 
83
//-----------------------------------------------------------------------------
84
// The minimal init, get and put functions. All by polling.
85
 
86
void
87
cyg_hal_plf_serial_init_channel(void* __ch_data)
88
{
89
    cyg_hal_plf_write_sr_rbr(0);
90
    cyg_hal_plf_write_sr_tbr(0);
91
    cyg_hal_plf_write_tbr(0);
92
    cyg_hal_plf_write_rbr(0);
93
}
94
 
95
void
96
cyg_hal_plf_serial_putc(void* __ch_data, cyg_uint8 __ch)
97
{
98
    // wait for tx rdy
99
    while (cyg_hal_plf_read_sr_tbr() != 0) ;
100
 
101
    // Now, write it
102
    cyg_hal_plf_write_tbr(__ch);
103
 
104
    // and set TBR
105
    cyg_hal_plf_write_sr_tbr(1);
106
}
107
 
108
static cyg_bool
109
cyg_hal_plf_serial_getc_nonblock(void* __ch_data, cyg_uint8* ch)
110
{
111
    if (cyg_hal_plf_read_sr_rbr() == 0)
112
        return false;
113
 
114
    *ch = cyg_hal_plf_read_rbr();
115
 
116
    cyg_hal_plf_write_sr_rbr(0);
117
 
118
    return true;
119
}
120
 
121
cyg_uint8
122
cyg_hal_plf_serial_getc(void* __ch_data)
123
{
124
    cyg_uint8 ch;
125
 
126
    while(!cyg_hal_plf_serial_getc_nonblock(__ch_data, &ch));
127
 
128
    return ch;
129
}
130
 
131
static void
132
cyg_hal_plf_serial_write(void* __ch_data, const cyg_uint8* __buf,
133
                         cyg_uint32 __len)
134
{
135
    while(__len-- > 0)
136
        cyg_hal_plf_serial_putc(__ch_data, *__buf++);
137
}
138
 
139
static void
140
cyg_hal_plf_serial_read(void* __ch_data, cyg_uint8* __buf, cyg_uint32 __len)
141
{
142
    while(__len-- > 0)
143
        *__buf++ = cyg_hal_plf_serial_getc(__ch_data);
144
}
145
 
146
 
147
cyg_bool
148
cyg_hal_plf_serial_getc_timeout(void* __ch_data, cyg_uint8* ch)
149
{
150
    int delay_count;
151
    channel_data_t* chan;
152
    cyg_bool res;
153
 
154
    // Some of the diagnostic print code calls through here with no idea what the ch_data is.
155
    // Go ahead and assume it is channels[0].
156
    if (__ch_data == 0)
157
      __ch_data = (void*)&channels[0];
158
 
159
    chan = (channel_data_t*)__ch_data;
160
 
161
    delay_count = chan->msec_timeout * 10; // delay in .1 ms steps
162
 
163
    for(;;) {
164
        res = cyg_hal_plf_serial_getc_nonblock(__ch_data, ch);
165
        if (res || 0 == delay_count--)
166
            break;
167
        CYGACC_CALL_IF_DELAY_US(100);
168
    }
169
 
170
    return res;
171
}
172
 
173
static int
174
cyg_hal_plf_serial_control(void *__ch_data, __comm_control_cmd_t __func, ...)
175
{
176
    static int irq_state = 0;
177
    channel_data_t* chan;
178
    int ret = 0;
179
 
180
    // Some of the diagnostic print code calls through here with no idea what the ch_data is.
181
    // Go ahead and assume it is channels[0].
182
    if (__ch_data == 0)
183
      __ch_data = (void*)&channels[0];
184
 
185
    chan = (channel_data_t*)__ch_data;
186
 
187
    switch (__func) {
188
    case __COMMCTL_IRQ_ENABLE:
189
        irq_state = 1;
190
 
191
        HAL_INTERRUPT_SET_LEVEL(chan->isr_vector, 1);
192
        HAL_INTERRUPT_UNMASK(chan->isr_vector);
193
        break;
194
    case __COMMCTL_IRQ_DISABLE:
195
        ret = irq_state;
196
        irq_state = 0;
197
 
198
        HAL_INTERRUPT_MASK(chan->isr_vector);
199
        break;
200
    case __COMMCTL_DBG_ISR_VECTOR:
201
        ret = chan->isr_vector;
202
        break;
203
    case __COMMCTL_SET_TIMEOUT:
204
    {
205
        va_list ap;
206
 
207
        va_start(ap, __func);
208
 
209
        ret = chan->msec_timeout;
210
        chan->msec_timeout = va_arg(ap, cyg_uint32);
211
 
212
        va_end(ap);
213
    }
214
    break;
215
    case __COMMCTL_SETBAUD:
216
    {
217
        cyg_uint32 baud_rate;
218
        va_list ap;
219
 
220
        va_start(ap, __func);
221
        baud_rate = va_arg(ap, cyg_uint32);
222
        va_end(ap);
223
 
224
        switch (baud_rate)
225
        {
226
        case 57600:  break;
227
        default:     return -1;
228
        }
229
    }
230
    break;
231
 
232
    case __COMMCTL_GETBAUD:
233
        break;
234
    default:
235
        break;
236
    }
237
    return ret;
238
}
239
 
240
static int
241
cyg_hal_plf_serial_isr(void *__ch_data, int* __ctrlc,
242
                       CYG_ADDRWORD __vector, CYG_ADDRWORD __data)
243
{
244
    *__ctrlc = 0;
245
    return 0;
246
}
247
 
248
static void
249
cyg_hal_plf_serial_init(void)
250
{
251
    hal_virtual_comm_table_t* comm;
252
    int cur = CYGACC_CALL_IF_SET_CONSOLE_COMM(CYGNUM_CALL_IF_SET_COMM_ID_QUERY_CURRENT);
253
 
254
    // Disable interrupts.
255
    HAL_INTERRUPT_MASK(channels[0].isr_vector);
256
 
257
    // Init channels
258
    cyg_hal_plf_serial_init_channel((void*)&channels[0]);
259
 
260
    // Setup procs in the vector table
261
 
262
    // Set channel 0
263
    CYGACC_CALL_IF_SET_CONSOLE_COMM(0);
264
    comm = CYGACC_CALL_IF_CONSOLE_PROCS();
265
    CYGACC_COMM_IF_CH_DATA_SET(*comm, &channels[0]);
266
    CYGACC_COMM_IF_WRITE_SET(*comm, cyg_hal_plf_serial_write);
267
    CYGACC_COMM_IF_READ_SET(*comm, cyg_hal_plf_serial_read);
268
    CYGACC_COMM_IF_PUTC_SET(*comm, cyg_hal_plf_serial_putc);
269
    CYGACC_COMM_IF_GETC_SET(*comm, cyg_hal_plf_serial_getc);
270
    CYGACC_COMM_IF_CONTROL_SET(*comm, cyg_hal_plf_serial_control);
271
    CYGACC_COMM_IF_DBG_ISR_SET(*comm, cyg_hal_plf_serial_isr);
272
    CYGACC_COMM_IF_GETC_TIMEOUT_SET(*comm, cyg_hal_plf_serial_getc_timeout);
273
 
274
    // Restore original console
275
    CYGACC_CALL_IF_SET_CONSOLE_COMM(cur);
276
}
277
 
278
void
279
cyg_hal_plf_comms_init(void)
280
{
281
    static int initialized = 0;
282
 
283
    if (initialized)
284
        return;
285
 
286
    initialized = 1;
287
 
288
    cyg_hal_plf_serial_init();
289
}
290
 
291
//-----------------------------------------------------------------------------
292
// end of ser16c550c.c
293
 

powered by: WebSVN 2.1.0

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