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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [packages/] [hal/] [mips/] [vrc437x/] [current/] [src/] [plf_serial.c] - Blame information for rev 868

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

Line No. Rev Author Line
1 786 skrzyp
//=============================================================================
2
//
3
//      plf_stub.c
4
//
5
//      Platform specific code for GDB stub support.
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):   nickg, jskov (based on the old tx39 hal_stub.c)
43
// Contributors:nickg, jskov
44
// Date:        1999-02-12
45
// Purpose:     Platform specific code for GDB stub support.
46
//              
47
//####DESCRIPTIONEND####
48
//
49
//=============================================================================
50
 
51
#include <pkgconf/hal.h>
52
 
53
#include <cyg/hal/hal_io.h>             // HAL IO macros
54
#include <cyg/hal/hal_diag.h>           // diag output. FIXME
55
 
56
#include <cyg/hal/hal_arch.h>
57
#include <cyg/hal/hal_intr.h>
58
#include <cyg/hal/hal_if.h>
59
#include <cyg/hal/hal_misc.h>
60
#include <cyg/hal/drv_api.h>            // CYG_ISR_HANDLED
61
 
62
#include <cyg/hal/plf_z8530.h>
63
 
64
/*---------------------------------------------------------------------------*/
65
 
66
static unsigned char _diag_init[] = {
67
    0x00, /* Register 0 */
68
    0x00, /* Register 1 - no interrupts */
69
    0x00, /* Register 2 */
70
    0xC1, /* Register 3 - Rx enable, 8 data */
71
    0x44, /* Register 4 - x16 clock, 1 stop, no parity */
72
    0x68, /* Register 5 - Tx enable, 8 data */
73
    0x00, /* Register 6 */
74
    0x00, /* Register 7 */
75
    0x00, /* Register 8 */
76
    0x00, /* Register 9 */
77
    0x00, /* Register 10 */
78
    0x56, /* Register 11 - Rx, Tx clocks from baud rate generator */
79
    0x00, /* Register 12 - baud rate LSB */
80
    0x00, /* Register 13 - baud rate MSB */
81
    0x03, /* Register 14 - enable baud rate generator */
82
    0x00  /* Register 15 */
83
};
84
 
85
#define BRTC(brate) (( ((unsigned) DUART_CLOCK) / (2*(brate)*SCC_CLKMODE_TC)) - 2)
86
#define DUART_CLOCK          4915200         /* Z8530 duart */
87
#define SCC_CLKMODE_TC       16              /* Always run x16 clock for async modes */
88
 
89
//-----------------------------------------------------------------------------
90
 
91
typedef struct {
92
    cyg_uint32 base;
93
    cyg_uint32 msec_timeout;
94
    int isr_vector;
95
} channel_data_t;
96
 
97
static channel_data_t channels[2] = {
98
    { DUART_A, 1000, CYGNUM_HAL_INTERRUPT_DUART},
99
    { DUART_B, 1000, CYGNUM_HAL_INTERRUPT_DUART}
100
};
101
 
102
//-----------------------------------------------------------------------------
103
// Set the baud rate
104
 
105
static void
106
cyg_hal_plf_serial_set_baud(cyg_uint32 duart, cyg_uint16 baud_rate)
107
{
108
    unsigned short brg = BRTC(baud_rate);
109
    HAL_DUART_WRITE_CR(duart, 12, brg&0xFF);
110
    HAL_DUART_WRITE_CR(duart, 13, brg>>8);
111
}
112
 
113
//-----------------------------------------------------------------------------
114
// The minimal init, get and put functions. All by polling.
115
 
116
void
117
cyg_hal_plf_serial_init_channel(void* __ch_data)
118
{
119
    cyg_uint32 duart;
120
    unsigned short brg = BRTC(CYGNUM_HAL_VIRTUAL_VECTOR_CONSOLE_CHANNEL_BAUD);
121
    int i;
122
    channel_data_t *chan;
123
 
124
    // Some of the diagnostic print code calls through here with no idea what the ch_data is.
125
    // Go ahead and assume it is channels[0].
126
    if (__ch_data == 0)
127
        __ch_data = (void*)&channels[0];
128
 
129
    chan = (channel_data_t*)__ch_data;
130
 
131
#ifdef CYGPRI_HAL_VIRTUAL_VECTOR_DEBUG_CHANNEL_CONFIGURABLE    
132
    if( (chan-&channels[0]) == CYGNUM_HAL_VIRTUAL_VECTOR_DEBUG_CHANNEL )
133
        brg = BRTC(CYGNUM_HAL_VIRTUAL_VECTOR_DEBUG_CHANNEL_BAUD);
134
#endif    
135
#ifdef CYGPRI_HAL_VIRTUAL_VECTOR_CONSOLE_CHANNEL_CONFIGURABLE
136
    if( (chan-&channels[0]) == CYGNUM_HAL_VIRTUAL_VECTOR_CONSOLE_CHANNEL )
137
        brg = BRTC(CYGNUM_HAL_VIRTUAL_VECTOR_CONSOLE_CHANNEL_BAUD);
138
#endif
139
 
140
    duart = chan->base;
141
 
142
    _diag_init[12] = brg & 0xFF;
143
    _diag_init[13] = brg >> 8;
144
    for (i = 1;  i < 16;  i++) {
145
        HAL_DUART_WRITE_CR(duart, i, _diag_init[i]);
146
    }
147
 
148
}
149
 
150
void
151
cyg_hal_plf_serial_putc(void* __ch_data, cyg_uint8 __ch)
152
{
153
    cyg_uint32 duart;
154
    cyg_uint8 rr0;
155
 
156
    // Some of the diagnostic print code calls through here with no idea what the ch_data is.
157
    // Go ahead and assume it is channels[0].
158
    if (__ch_data == 0)
159
      __ch_data = (void*)&channels[0];
160
 
161
    duart = ((channel_data_t*)__ch_data)->base;
162
 
163
    CYGARC_HAL_SAVE_GP();
164
 
165
    do
166
    {
167
        HAL_DUART_READ_CR(duart, 0, rr0 );
168
    } while( (rr0 & 0x04) == 0 );
169
 
170
    HAL_DUART_WRITE_TR( duart, __ch );
171
 
172
    HAL_INTERRUPT_ACKNOWLEDGE( CYGNUM_HAL_INTERRUPT_DUART );
173
 
174
    CYGARC_HAL_RESTORE_GP();
175
}
176
 
177
static cyg_bool
178
cyg_hal_plf_serial_getc_nonblock(void* __ch_data, cyg_uint8* ch)
179
{
180
    cyg_uint32 duart;
181
    cyg_uint8 rr0;
182
 
183
    // Some of the diagnostic print code calls through here with no idea what the ch_data is.
184
    // Go ahead and assume it is channels[0].
185
    if (__ch_data == 0)
186
      __ch_data = (void*)&channels[0];
187
 
188
    duart = ((channel_data_t*)__ch_data)->base;
189
 
190
    HAL_DUART_READ_CR(duart, 0, rr0 );
191
 
192
    if( (rr0 & 0x01) == 0 )
193
        return false;
194
 
195
    HAL_DUART_READ_RR( duart, *ch );
196
 
197
    HAL_INTERRUPT_ACKNOWLEDGE( CYGNUM_HAL_INTERRUPT_DUART );
198
 
199
    return true;
200
}
201
 
202
cyg_uint8
203
cyg_hal_plf_serial_getc(void* __ch_data)
204
{
205
    cyg_uint8 ch;
206
    CYGARC_HAL_SAVE_GP();
207
 
208
    // Some of the diagnostic print code calls through here with no idea what the ch_data is.
209
    // Go ahead and assume it is channels[0].
210
    if (__ch_data == 0)
211
      __ch_data = (void*)&channels[0];
212
 
213
    while(!cyg_hal_plf_serial_getc_nonblock(__ch_data, &ch));
214
 
215
    CYGARC_HAL_RESTORE_GP();
216
    return ch;
217
}
218
 
219
#ifdef CYGSEM_HAL_VIRTUAL_VECTOR_SUPPORT
220
 
221
static void
222
cyg_hal_plf_serial_write(void* __ch_data, const cyg_uint8* __buf,
223
                         cyg_uint32 __len)
224
{
225
    CYGARC_HAL_SAVE_GP();
226
 
227
    // Some of the diagnostic print code calls through here with no idea what the ch_data is.
228
    // Go ahead and assume it is channels[0].
229
    if (__ch_data == 0)
230
      __ch_data = (void*)&channels[0];
231
 
232
    while(__len-- > 0)
233
        cyg_hal_plf_serial_putc(__ch_data, *__buf++);
234
 
235
    CYGARC_HAL_RESTORE_GP();
236
}
237
 
238
static void
239
cyg_hal_plf_serial_read(void* __ch_data, cyg_uint8* __buf, cyg_uint32 __len)
240
{
241
    CYGARC_HAL_SAVE_GP();
242
 
243
    // Some of the diagnostic print code calls through here with no idea what the ch_data is.
244
    // Go ahead and assume it is channels[0].
245
    if (__ch_data == 0)
246
      __ch_data = (void*)&channels[0];
247
 
248
    while(__len-- > 0)
249
        *__buf++ = cyg_hal_plf_serial_getc(__ch_data);
250
 
251
    CYGARC_HAL_RESTORE_GP();
252
}
253
 
254
 
255
cyg_bool
256
cyg_hal_plf_serial_getc_timeout(void* __ch_data, cyg_uint8* ch)
257
{
258
    int delay_count;
259
    channel_data_t* chan;
260
    cyg_bool res;
261
    CYGARC_HAL_SAVE_GP();
262
 
263
    // Some of the diagnostic print code calls through here with no idea what the ch_data is.
264
    // Go ahead and assume it is channels[0].
265
    if (__ch_data == 0)
266
      __ch_data = (void*)&channels[0];
267
 
268
    chan = (channel_data_t*)__ch_data;
269
 
270
    delay_count = chan->msec_timeout * 10; // delay in .1 ms steps
271
 
272
    for(;;) {
273
        res = cyg_hal_plf_serial_getc_nonblock(__ch_data, ch);
274
        if (res || 0 == delay_count--)
275
            break;
276
        CYGACC_CALL_IF_DELAY_US(100);
277
    }
278
 
279
    CYGARC_HAL_RESTORE_GP();
280
    return res;
281
}
282
 
283
 
284
static int
285
cyg_hal_plf_serial_control(void *__ch_data, __comm_control_cmd_t __func, ...)
286
{
287
    static int irq_state = 0;
288
    channel_data_t* chan;
289
    int ret = 0;
290
    CYGARC_HAL_SAVE_GP();
291
 
292
    // Some of the diagnostic print code calls through here with no idea what the ch_data is.
293
    // Go ahead and assume it is channels[0].
294
    if (__ch_data == 0)
295
      __ch_data = (void*)&channels[0];
296
 
297
    chan = (channel_data_t*)__ch_data;
298
 
299
    switch (__func) {
300
    case __COMMCTL_IRQ_ENABLE:
301
        irq_state = 1;
302
 
303
        HAL_DUART_WRITE_CR( chan->base, 1, 0x10 );
304
        HAL_DUART_WRITE_CR( chan->base, 9, 0x0a );
305
 
306
        HAL_INTERRUPT_SET_LEVEL(chan->isr_vector, 0);
307
        HAL_INTERRUPT_UNMASK(chan->isr_vector);
308
        break;
309
    case __COMMCTL_IRQ_DISABLE:
310
        ret = irq_state;
311
        irq_state = 0;
312
 
313
        HAL_DUART_WRITE_CR( chan->base, 1, 0x00 );
314
        HAL_DUART_WRITE_CR( chan->base, 9, 0x00 );
315
 
316
        HAL_INTERRUPT_MASK(chan->isr_vector);
317
        break;
318
    case __COMMCTL_DBG_ISR_VECTOR:
319
        ret = chan->isr_vector;
320
        break;
321
    case __COMMCTL_SET_TIMEOUT:
322
    {
323
        va_list ap;
324
 
325
        va_start(ap, __func);
326
 
327
        ret = chan->msec_timeout;
328
        chan->msec_timeout = va_arg(ap, cyg_uint32);
329
 
330
        va_end(ap);
331
    }
332
    break;
333
    case __COMMCTL_SETBAUD:
334
    {
335
        cyg_uint32 baud_rate;
336
        cyg_uint32 duart = chan->base;
337
        va_list ap;
338
 
339
        va_start(ap, __func);
340
        baud_rate = va_arg(ap, cyg_uint32);
341
        va_end(ap);
342
 
343
        // Set baud rate.
344
        cyg_hal_plf_serial_set_baud(duart, baud_rate);
345
 
346
    }
347
    break;
348
 
349
    case __COMMCTL_GETBAUD:
350
        break;
351
    default:
352
        break;
353
    }
354
    CYGARC_HAL_RESTORE_GP();
355
    return ret;
356
}
357
 
358
static int
359
cyg_hal_plf_serial_isr(void *__ch_data, int* __ctrlc,
360
                       CYG_ADDRWORD __vector, CYG_ADDRWORD __data)
361
{
362
    int res = 0;
363
    channel_data_t* chan;
364
    char c;
365
 
366
    CYGARC_HAL_SAVE_GP();
367
 
368
    // Some of the diagnostic print code calls through here with no idea what the ch_data is.
369
    // Go ahead and assume it is channels[0].
370
    if (__ch_data == 0)
371
      __ch_data = (void*)&channels[0];
372
 
373
    chan = (channel_data_t*)__ch_data;
374
 
375
    HAL_INTERRUPT_ACKNOWLEDGE(chan->isr_vector);
376
 
377
    *__ctrlc = 0;
378
 
379
    if ( cyg_hal_plf_serial_getc_nonblock(__ch_data, &c) )
380
    {
381
        if( cyg_hal_is_break( &c , 1 ) )
382
            *__ctrlc = 1;
383
 
384
        res = CYG_ISR_HANDLED;
385
    }
386
 
387
    CYGARC_HAL_RESTORE_GP();
388
 
389
    return res;
390
}
391
 
392
#endif
393
 
394
static void
395
cyg_hal_plf_serial_init(void)
396
{
397
#ifdef CYGSEM_HAL_VIRTUAL_VECTOR_SUPPORT    
398
    hal_virtual_comm_table_t* comm;
399
    int cur = CYGACC_CALL_IF_SET_CONSOLE_COMM(CYGNUM_CALL_IF_SET_COMM_ID_QUERY_CURRENT);
400
#endif
401
    // Disable interrupts.
402
    HAL_INTERRUPT_MASK(channels[0].isr_vector);
403
 
404
    // Init channels
405
    cyg_hal_plf_serial_init_channel((void*)&channels[0]);
406
 
407
#ifdef CYGSEM_HAL_VIRTUAL_VECTOR_SUPPORT    
408
    // Setup procs in the vector table
409
 
410
    // Set channel 0
411
    CYGACC_CALL_IF_SET_CONSOLE_COMM(0);
412
    comm = CYGACC_CALL_IF_CONSOLE_PROCS();
413
    CYGACC_COMM_IF_CH_DATA_SET(*comm, &channels[0]);
414
    CYGACC_COMM_IF_WRITE_SET(*comm, cyg_hal_plf_serial_write);
415
    CYGACC_COMM_IF_READ_SET(*comm, cyg_hal_plf_serial_read);
416
    CYGACC_COMM_IF_PUTC_SET(*comm, cyg_hal_plf_serial_putc);
417
    CYGACC_COMM_IF_GETC_SET(*comm, cyg_hal_plf_serial_getc);
418
    CYGACC_COMM_IF_CONTROL_SET(*comm, cyg_hal_plf_serial_control);
419
    CYGACC_COMM_IF_DBG_ISR_SET(*comm, cyg_hal_plf_serial_isr);
420
    CYGACC_COMM_IF_GETC_TIMEOUT_SET(*comm, cyg_hal_plf_serial_getc_timeout);
421
 
422
    // Restore original console
423
    CYGACC_CALL_IF_SET_CONSOLE_COMM(cur);
424
#endif    
425
}
426
 
427
 
428
void
429
cyg_hal_plf_comms_init(void)
430
{
431
    static int initialized = 0;
432
 
433
    if (initialized)
434
        return;
435
 
436
    initialized = 1;
437
 
438
    cyg_hal_plf_serial_init();
439
}
440
 
441
//-----------------------------------------------------------------------------
442
// End of plf_serial.c

powered by: WebSVN 2.1.0

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