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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [packages/] [hal/] [arm/] [xscale/] [iop310/] [current/] [src/] [hal_diag.c] - Blame information for rev 856

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

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