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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-2.0/] [packages/] [hal/] [arm/] [xscale/] [iop310/] [v2_0/] [src/] [hal_diag.c] - Blame information for rev 565

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

Line No. Rev Author Line
1 27 unneback
/*=============================================================================
2
//
3
//      hal_diag.c
4
//
5
//      HAL diagnostic output code
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
// Copyright (C) 2002 Gary Thomas
13
//
14
// eCos is free software; you can redistribute it and/or modify it under
15
// the terms of the GNU General Public License as published by the Free
16
// Software Foundation; either version 2 or (at your option) any later version.
17
//
18
// eCos is distributed in the hope that it will be useful, but WITHOUT ANY
19
// 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 along
24
// with eCos; if not, write to the Free Software Foundation, Inc.,
25
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
26
//
27
// As a special exception, if other files instantiate templates or use macros
28
// or inline functions from this file, or you compile this file and link it
29
// with other works to produce a work based on this file, this file does not
30
// by itself cause the resulting work to be covered by the GNU General Public
31
// License. However the source code for this file must still be made available
32
// in accordance with section (3) of the GNU General Public License.
33
//
34
// This exception does not invalidate any other reasons why a work based on
35
// this file might be covered by the GNU General Public License.
36
//
37
// Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
38
// at http://sources.redhat.com/ecos/ecos-license/
39
// -------------------------------------------
40
//####ECOSGPLCOPYRIGHTEND####
41
//=============================================================================
42
//#####DESCRIPTIONBEGIN####
43
//
44
// Author(s):   msalter
45
// Contributors:msalter, gthomas
46
// Date:        2000-10-10
47
// Purpose:     HAL diagnostic output
48
// Description: Implementations of HAL diagnostic output support.
49
//
50
//####DESCRIPTIONEND####
51
//
52
//===========================================================================*/
53
 
54
#include <pkgconf/hal.h>
55
#include <pkgconf/system.h>
56
#include CYGBLD_HAL_PLATFORM_H
57
 
58
#include <cyg/infra/cyg_type.h>         // base types
59
#include <cyg/infra/cyg_trac.h>         // tracing macros
60
#include <cyg/infra/cyg_ass.h>          // assertion macros
61
 
62
#include <cyg/hal/hal_arch.h>           // basic machine info
63
#include <cyg/hal/hal_intr.h>           // interrupt macros
64
#include <cyg/hal/hal_io.h>             // IO macros
65
#include <cyg/hal/hal_if.h>             // calling interface API
66
#include <cyg/hal/hal_misc.h>           // helper functions
67
#include <cyg/hal/hal_diag.h>
68
#include <cyg/hal/hal_iop310.h>         // Hardware definitions
69
#include <cyg/hal/drv_api.h>            // cyg_drv_interrupt_acknowledge
70
 
71
 
72
static void cyg_hal_plf_serial_init(void);
73
 
74
void
75
cyg_hal_plf_comms_init(void)
76
{
77
    static int initialized = 0;
78
 
79
    if (initialized)
80
        return;
81
 
82
    initialized = 1;
83
 
84
    cyg_hal_plf_serial_init();
85
}
86
 
87
//=============================================================================
88
// Serial driver
89
//=============================================================================
90
 
91
//-----------------------------------------------------------------------------
92
// Based on 3.6864 MHz xtal
93
struct baud_config {
94
    cyg_int32 baud_rate;
95
    cyg_uint8 msb;
96
    cyg_uint8 lsb;
97
};
98
 
99
struct baud_config baud_conf[] = {
100
    {9600,   0x00, 0x0c},
101
    {19200,  0x00, 0x06},
102
    {38400,  0x00, 0x03},
103
    {57600,  0x00, 0x02},
104
    {115200, 0x00, 0x01}};
105
 
106
//-----------------------------------------------------------------------------
107
// Define the serial registers. The Cogent board is equipped with a 16552
108
// serial chip.
109
#define CYG_DEV_SERIAL_RBR   0x00  // receiver buffer register, read, dlab = 0
110
#define CYG_DEV_SERIAL_THR   0x00 // transmitter holding register, write, dlab = 0
111
#define CYG_DEV_SERIAL_DLL   0x00 // divisor latch (LS), read/write, dlab = 1
112
#define CYG_DEV_SERIAL_IER   0x01 // interrupt enable register, read/write, dlab = 0
113
#define CYG_DEV_SERIAL_DLM   0x01 // divisor latch (MS), read/write, dlab = 1
114
#define CYG_DEV_SERIAL_IIR   0x02 // interrupt identification register, read, dlab = 0
115
#define CYG_DEV_SERIAL_FCR   0x02 // fifo control register, write, dlab = 0
116
#define CYG_DEV_SERIAL_LCR   0x03 // line control register, write
117
#define CYG_DEV_SERIAL_MCR   0x04 // modem control register, write
118
#define CYG_DEV_SERIAL_LSR   0x05 // line status register, read
119
#define CYG_DEV_SERIAL_MSR   0x06 // modem status register, read
120
#define CYG_DEV_SERIAL_SCR   0x07 // scratch pad register
121
 
122
// The interrupt enable register bits.
123
#define SIO_IER_ERDAI   0x01            // enable received data available irq
124
#define SIO_IER_ETHREI  0x02            // enable THR empty interrupt
125
#define SIO_IER_ELSI    0x04            // enable receiver line status irq
126
#define SIO_IER_EMSI    0x08            // enable modem status interrupt
127
 
128
// The interrupt identification register bits.
129
#define SIO_IIR_IP      0x01            // 0 if interrupt pending
130
#define SIO_IIR_ID_MASK 0x0e            // mask for interrupt ID bits
131
#define ISR_Tx  0x02
132
#define ISR_Rx  0x04
133
 
134
// The line status register bits.
135
#define SIO_LSR_DR      0x01            // data ready
136
#define SIO_LSR_OE      0x02            // overrun error
137
#define SIO_LSR_PE      0x04            // parity error
138
#define SIO_LSR_FE      0x08            // framing error
139
#define SIO_LSR_BI      0x10            // break interrupt
140
#define SIO_LSR_THRE    0x20            // transmitter holding register empty
141
#define SIO_LSR_TEMT    0x40            // transmitter register empty
142
#define SIO_LSR_ERR     0x80            // any error condition
143
 
144
// The modem status register bits.
145
#define SIO_MSR_DCTS  0x01              // delta clear to send
146
#define SIO_MSR_DDSR  0x02              // delta data set ready
147
#define SIO_MSR_TERI  0x04              // trailing edge ring indicator
148
#define SIO_MSR_DDCD  0x08              // delta data carrier detect
149
#define SIO_MSR_CTS   0x10              // clear to send
150
#define SIO_MSR_DSR   0x20              // data set ready
151
#define SIO_MSR_RI    0x40              // ring indicator
152
#define SIO_MSR_DCD   0x80              // data carrier detect
153
 
154
// The line control register bits.
155
#define SIO_LCR_WLS0   0x01             // word length select bit 0
156
#define SIO_LCR_WLS1   0x02             // word length select bit 1
157
#define SIO_LCR_STB    0x04             // number of stop bits
158
#define SIO_LCR_PEN    0x08             // parity enable
159
#define SIO_LCR_EPS    0x10             // even parity select
160
#define SIO_LCR_SP     0x20             // stick parity
161
#define SIO_LCR_SB     0x40             // set break
162
#define SIO_LCR_DLAB   0x80             // divisor latch access bit
163
 
164
// The FIFO control register
165
#define SIO_FCR_FCR0   0x01             // enable xmit and rcvr fifos
166
#define SIO_FCR_FCR1   0x02             // clear RCVR FIFO
167
#define SIO_FCR_FCR2   0x04             // clear XMIT FIFO
168
 
169
 
170
//-----------------------------------------------------------------------------
171
typedef struct {
172
    cyg_uint8* base;
173
    cyg_int32 msec_timeout;
174
    int isr_vector;
175
    cyg_int32 baud_rate;
176
} channel_data_t;
177
 
178
//-----------------------------------------------------------------------------
179
static int
180
set_baud( channel_data_t *chan )
181
{
182
    cyg_uint8* base = chan->base;
183
    cyg_uint8 i;
184
 
185
    for (i=0; i<(sizeof(baud_conf)/sizeof(baud_conf[0])); i++)
186
    {
187
        if (chan->baud_rate == baud_conf[i].baud_rate) {
188
            cyg_uint8 lcr;
189
            HAL_READ_UINT8(base+CYG_DEV_SERIAL_LCR, lcr);
190
            HAL_WRITE_UINT8(base+CYG_DEV_SERIAL_LCR, lcr|SIO_LCR_DLAB);
191
            HAL_WRITE_UINT8(base+CYG_DEV_SERIAL_DLL, baud_conf[i].lsb);
192
            HAL_WRITE_UINT8(base+CYG_DEV_SERIAL_DLM, baud_conf[i].msb);
193
            HAL_WRITE_UINT8(base+CYG_DEV_SERIAL_LCR, lcr);
194
            return 1;
195
        }
196
    }
197
    return -1;
198
}
199
 
200
static void
201
init_serial_channel(channel_data_t* __ch_data)
202
{
203
    cyg_uint8* base = __ch_data->base;
204
    channel_data_t* chan = (channel_data_t*)__ch_data;
205
 
206
    // 8-1-no parity.
207
    HAL_WRITE_UINT8(base+CYG_DEV_SERIAL_LCR, SIO_LCR_WLS0 | SIO_LCR_WLS1);
208
    chan->baud_rate = CYGNUM_HAL_VIRTUAL_VECTOR_CONSOLE_CHANNEL_BAUD;
209
    set_baud( chan );
210
    HAL_WRITE_UINT8(base+CYG_DEV_SERIAL_FCR, 0x07);  // Enable & clear FIFO
211
}
212
 
213
static cyg_bool
214
cyg_hal_plf_serial_getc_nonblock(void* __ch_data, cyg_uint8* ch)
215
{
216
    cyg_uint8* base = ((channel_data_t*)__ch_data)->base;
217
    cyg_uint8 lsr;
218
 
219
    HAL_READ_UINT8(base+CYG_DEV_SERIAL_LSR, lsr);
220
    if ((lsr & SIO_LSR_DR) == 0)
221
        return false;
222
 
223
    HAL_READ_UINT8(base+CYG_DEV_SERIAL_RBR, *ch);
224
 
225
    return true;
226
}
227
 
228
 
229
cyg_uint8
230
cyg_hal_plf_serial_getc(void* __ch_data)
231
{
232
    cyg_uint8 ch;
233
    CYGARC_HAL_SAVE_GP();
234
 
235
    while(!cyg_hal_plf_serial_getc_nonblock(__ch_data, &ch));
236
 
237
    CYGARC_HAL_RESTORE_GP();
238
    return ch;
239
}
240
 
241
void
242
cyg_hal_plf_serial_putc(void* __ch_data, cyg_uint8 c)
243
{
244
    cyg_uint8* base = ((channel_data_t*)__ch_data)->base;
245
    cyg_uint8 lsr;
246
    CYGARC_HAL_SAVE_GP();
247
 
248
    do {
249
        HAL_READ_UINT8(base+CYG_DEV_SERIAL_LSR, lsr);
250
    } while ((lsr & SIO_LSR_THRE) == 0);
251
 
252
    HAL_WRITE_UINT8(base+CYG_DEV_SERIAL_THR, c);
253
 
254
    // Hang around until the character has been safely sent.
255
    do {
256
        HAL_READ_UINT8(base+CYG_DEV_SERIAL_LSR, lsr);
257
    } while ((lsr & SIO_LSR_THRE) == 0);
258
 
259
    CYGARC_HAL_RESTORE_GP();
260
}
261
 
262
static channel_data_t channels[2] = {
263
#if CYGHWR_HAL_ARM_IOP310_SERIAL_PORTA != 0
264
    { (cyg_uint8*)IOP310_SERIAL_PORT_A, 1000, CYGNUM_HAL_INTERRUPT_SERIAL_A},
265
#endif
266
#if CYGHWR_HAL_ARM_IOP310_SERIAL_PORTB != 0
267
    { (cyg_uint8*)IOP310_SERIAL_PORT_B, 1000, CYGNUM_HAL_INTERRUPT_SERIAL_B},
268
#endif
269
};
270
 
271
static void
272
cyg_hal_plf_serial_write(void* __ch_data, const cyg_uint8* __buf,
273
                         cyg_uint32 __len)
274
{
275
    CYGARC_HAL_SAVE_GP();
276
 
277
    while(__len-- > 0)
278
        cyg_hal_plf_serial_putc(__ch_data, *__buf++);
279
 
280
    CYGARC_HAL_RESTORE_GP();
281
}
282
 
283
static void
284
cyg_hal_plf_serial_read(void* __ch_data, cyg_uint8* __buf, cyg_uint32 __len)
285
{
286
    CYGARC_HAL_SAVE_GP();
287
 
288
    while(__len-- > 0)
289
        *__buf++ = cyg_hal_plf_serial_getc(__ch_data);
290
 
291
    CYGARC_HAL_RESTORE_GP();
292
}
293
 
294
cyg_bool
295
cyg_hal_plf_serial_getc_timeout(void* __ch_data, cyg_uint8* ch)
296
{
297
    int delay_count;
298
    channel_data_t* chan = (channel_data_t*)__ch_data;
299
    cyg_bool res;
300
    CYGARC_HAL_SAVE_GP();
301
 
302
    delay_count = chan->msec_timeout * 10; // delay in .1 ms steps
303
    for(;;) {
304
        res = cyg_hal_plf_serial_getc_nonblock(__ch_data, ch);
305
        if (res || 0 == delay_count--)
306
            break;
307
 
308
        CYGACC_CALL_IF_DELAY_US(100);
309
    }
310
 
311
    CYGARC_HAL_RESTORE_GP();
312
    return res;
313
}
314
 
315
static int
316
cyg_hal_plf_serial_control(void *__ch_data, __comm_control_cmd_t __func, ...)
317
{
318
    static int irq_state = 0;
319
    channel_data_t* chan = (channel_data_t*)__ch_data;
320
    cyg_uint8 ier;
321
    int ret = 0;
322
    CYGARC_HAL_SAVE_GP();
323
 
324
    switch (__func) {
325
    case __COMMCTL_IRQ_ENABLE:
326
        HAL_INTERRUPT_UNMASK(chan->isr_vector);
327
        HAL_INTERRUPT_SET_LEVEL(chan->isr_vector, 1);
328
        HAL_READ_UINT8(chan->base+CYG_DEV_SERIAL_IER, ier);
329
        ier |= SIO_IER_ERDAI;
330
        HAL_WRITE_UINT8(chan->base+CYG_DEV_SERIAL_IER, ier);
331
        irq_state = 1;
332
        break;
333
    case __COMMCTL_IRQ_DISABLE:
334
        ret = irq_state;
335
        irq_state = 0;
336
        HAL_INTERRUPT_MASK(chan->isr_vector);
337
        HAL_READ_UINT8(chan->base+CYG_DEV_SERIAL_IER, ier);
338
        ier &= ~SIO_IER_ERDAI;
339
        HAL_WRITE_UINT8(chan->base+CYG_DEV_SERIAL_IER, ier);
340
        break;
341
    case __COMMCTL_DBG_ISR_VECTOR:
342
        ret = chan->isr_vector;
343
        break;
344
    case __COMMCTL_SET_TIMEOUT:
345
    {
346
        va_list ap;
347
 
348
        va_start(ap, __func);
349
 
350
        ret = chan->msec_timeout;
351
        chan->msec_timeout = va_arg(ap, cyg_uint32);
352
 
353
        va_end(ap);
354
    }
355
    case __COMMCTL_GETBAUD:
356
        ret = chan->baud_rate;
357
        break;
358
    case __COMMCTL_SETBAUD:
359
    {
360
        va_list ap;
361
        va_start(ap, __func);
362
        chan->baud_rate = va_arg(ap, cyg_int32);
363
        va_end(ap);
364
        ret = set_baud(chan);
365
        break;
366
    }
367
    default:
368
        break;
369
    }
370
    CYGARC_HAL_RESTORE_GP();
371
    return ret;
372
}
373
 
374
static int
375
cyg_hal_plf_serial_isr(void *__ch_data, int* __ctrlc,
376
                       CYG_ADDRWORD __vector, CYG_ADDRWORD __data)
377
{
378
    channel_data_t* chan = (channel_data_t*)__ch_data;
379
    cyg_uint8 _iir;
380
    int res = 0;
381
    CYGARC_HAL_SAVE_GP();
382
 
383
    HAL_READ_UINT8(chan->base+CYG_DEV_SERIAL_IIR, _iir);
384
    _iir &= SIO_IIR_ID_MASK;
385
 
386
    *__ctrlc = 0;
387
    if ( ISR_Rx == _iir ) {
388
        cyg_uint8 c, lsr;
389
        HAL_READ_UINT8(chan->base+CYG_DEV_SERIAL_LSR, lsr);
390
        if (lsr & SIO_LSR_DR) {
391
 
392
            HAL_READ_UINT8(chan->base+CYG_DEV_SERIAL_RBR, c);
393
 
394
            if( cyg_hal_is_break( &c , 1 ) )
395
                *__ctrlc = 1;
396
        }
397
 
398
        // Acknowledge the interrupt
399
        HAL_INTERRUPT_ACKNOWLEDGE(chan->isr_vector);
400
        res = CYG_ISR_HANDLED;
401
    }
402
 
403
    CYGARC_HAL_RESTORE_GP();
404
    return res;
405
}
406
 
407
static void
408
cyg_hal_plf_serial_init(void)
409
{
410
    hal_virtual_comm_table_t* comm;
411
    int cur = CYGACC_CALL_IF_SET_CONSOLE_COMM(CYGNUM_CALL_IF_SET_COMM_ID_QUERY_CURRENT);
412
 
413
    // Disable interrupts.
414
    HAL_INTERRUPT_MASK(channels[0].isr_vector);
415
#if CYGNUM_HAL_VIRTUAL_VECTOR_COMM_CHANNELS > 1
416
    HAL_INTERRUPT_MASK(channels[1].isr_vector);
417
#endif
418
 
419
    // Init channels
420
    init_serial_channel(&channels[0]);
421
#if CYGNUM_HAL_VIRTUAL_VECTOR_COMM_CHANNELS > 1
422
    init_serial_channel(&channels[1]);
423
#endif
424
 
425
    // Setup procs in the vector table
426
 
427
    // Set channel 0
428
    CYGACC_CALL_IF_SET_CONSOLE_COMM(0);
429
    comm = CYGACC_CALL_IF_CONSOLE_PROCS();
430
    CYGACC_COMM_IF_CH_DATA_SET(*comm, &channels[0]);
431
    CYGACC_COMM_IF_WRITE_SET(*comm, cyg_hal_plf_serial_write);
432
    CYGACC_COMM_IF_READ_SET(*comm, cyg_hal_plf_serial_read);
433
    CYGACC_COMM_IF_PUTC_SET(*comm, cyg_hal_plf_serial_putc);
434
    CYGACC_COMM_IF_GETC_SET(*comm, cyg_hal_plf_serial_getc);
435
    CYGACC_COMM_IF_CONTROL_SET(*comm, cyg_hal_plf_serial_control);
436
    CYGACC_COMM_IF_DBG_ISR_SET(*comm, cyg_hal_plf_serial_isr);
437
    CYGACC_COMM_IF_GETC_TIMEOUT_SET(*comm, cyg_hal_plf_serial_getc_timeout);
438
 
439
#if CYGNUM_HAL_VIRTUAL_VECTOR_COMM_CHANNELS > 1
440
    // Set channel 1
441
    CYGACC_CALL_IF_SET_CONSOLE_COMM(1);
442
    comm = CYGACC_CALL_IF_CONSOLE_PROCS();
443
    CYGACC_COMM_IF_CH_DATA_SET(*comm, &channels[1]);
444
    CYGACC_COMM_IF_WRITE_SET(*comm, cyg_hal_plf_serial_write);
445
    CYGACC_COMM_IF_READ_SET(*comm, cyg_hal_plf_serial_read);
446
    CYGACC_COMM_IF_PUTC_SET(*comm, cyg_hal_plf_serial_putc);
447
    CYGACC_COMM_IF_GETC_SET(*comm, cyg_hal_plf_serial_getc);
448
    CYGACC_COMM_IF_CONTROL_SET(*comm, cyg_hal_plf_serial_control);
449
    CYGACC_COMM_IF_DBG_ISR_SET(*comm, cyg_hal_plf_serial_isr);
450
    CYGACC_COMM_IF_GETC_TIMEOUT_SET(*comm, cyg_hal_plf_serial_getc_timeout);
451
#endif
452
 
453
    // Restore original console
454
    CYGACC_CALL_IF_SET_CONSOLE_COMM(cur);
455
}
456
 
457
/*---------------------------------------------------------------------------*/
458
/* End of hal_diag.c */

powered by: WebSVN 2.1.0

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