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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [packages/] [hal/] [calmrisc32/] [ceb/] [current/] [src/] [ser.c] - Blame information for rev 786

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

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