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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [packages/] [hal/] [arm/] [at91/] [var/] [current/] [src/] [hal_diag.c] - Blame information for rev 786

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):   jskov
43
// Contributors:jskov, gthomas
44
// Date:        2001-07-12
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 CYGBLD_HAL_PLATFORM_H
54
 
55
#include <cyg/infra/cyg_type.h>         // base types
56
 
57
#include <cyg/hal/hal_arch.h>           // SAVE/RESTORE GP macros
58
#include <cyg/hal/hal_io.h>             // IO macros
59
#include <cyg/hal/hal_if.h>             // interface API
60
#include <cyg/hal/hal_intr.h>           // HAL_ENABLE/MASK/UNMASK_INTERRUPTS
61
#include <cyg/hal/hal_misc.h>           // Helper functions
62
#include <cyg/hal/drv_api.h>            // CYG_ISR_HANDLED
63
#include <cyg/hal/hal_diag.h>
64
 
65
#include <cyg/hal/var_io.h>             // USART registers
66
 
67
#include "hal_diag_dcc.h"               // DCC initialization file
68
//-----------------------------------------------------------------------------
69
typedef struct {
70
    cyg_uint8* base;
71
    cyg_int32 msec_timeout;
72
    int isr_vector;
73
    int baud_rate;
74
} channel_data_t;
75
 
76
//-----------------------------------------------------------------------------
77
 
78
void
79
cyg_hal_plf_serial_putc(void *__ch_data, char c);
80
 
81
static void
82
cyg_hal_plf_serial_init_channel(void* __ch_data)
83
{
84
    channel_data_t* chan = (channel_data_t*)__ch_data;
85
    cyg_uint8* base = chan->base;
86
 
87
    // Reset device
88
    HAL_WRITE_UINT32(base+AT91_US_CR, AT91_US_CR_RxRESET | AT91_US_CR_TxRESET);
89
 
90
    // 8-1-no parity.
91
    HAL_WRITE_UINT32(base+AT91_US_MR,
92
                     AT91_US_MR_CLOCK_MCK | AT91_US_MR_LENGTH_8 |
93
                     AT91_US_MR_PARITY_NONE | AT91_US_MR_STOP_1);
94
 
95
    HAL_WRITE_UINT32(base+AT91_US_BRG, AT91_US_BAUD(chan->baud_rate));
96
 
97
    // Enable RX and TX
98
    HAL_WRITE_UINT32(base+AT91_US_CR, AT91_US_CR_RxENAB | AT91_US_CR_TxENAB);
99
 
100
}
101
 
102
void
103
cyg_hal_plf_serial_putc(void *__ch_data, char c)
104
{
105
    cyg_uint8* base = ((channel_data_t*)__ch_data)->base;
106
    cyg_uint32 status, ch;
107
    CYGARC_HAL_SAVE_GP();
108
 
109
    do {
110
        HAL_READ_UINT32(base+AT91_US_CSR, status);
111
    } while ((status & AT91_US_CSR_TxRDY) == 0);
112
 
113
    ch = (cyg_uint32)c;
114
    HAL_WRITE_UINT32(base+AT91_US_THR, ch);
115
 
116
    CYGARC_HAL_RESTORE_GP();
117
}
118
 
119
static cyg_bool
120
cyg_hal_plf_serial_getc_nonblock(void* __ch_data, cyg_uint8* ch)
121
{
122
    channel_data_t* chan = (channel_data_t*)__ch_data;
123
    cyg_uint8* base = chan->base;
124
    cyg_uint32 stat;
125
    cyg_uint32 c;
126
 
127
    HAL_READ_UINT32(base+AT91_US_CSR, stat);
128
    if ((stat & AT91_US_CSR_RxRDY) == 0)
129
        return false;
130
 
131
    HAL_READ_UINT32(base+AT91_US_RHR, c);
132
    *ch = (cyg_uint8)(c & 0xff);
133
 
134
    return true;
135
}
136
 
137
cyg_uint8
138
cyg_hal_plf_serial_getc(void* __ch_data)
139
{
140
    cyg_uint8 ch;
141
    CYGARC_HAL_SAVE_GP();
142
 
143
    while(!cyg_hal_plf_serial_getc_nonblock(__ch_data, &ch));
144
 
145
    CYGARC_HAL_RESTORE_GP();
146
    return ch;
147
}
148
 
149
static void
150
cyg_hal_plf_serial_write(void* __ch_data, const cyg_uint8* __buf,
151
                         cyg_uint32 __len)
152
{
153
    CYGARC_HAL_SAVE_GP();
154
 
155
    while(__len-- > 0)
156
        cyg_hal_plf_serial_putc(__ch_data, *__buf++);
157
 
158
    CYGARC_HAL_RESTORE_GP();
159
}
160
 
161
static void
162
cyg_hal_plf_serial_read(void* __ch_data, cyg_uint8* __buf, cyg_uint32 __len)
163
{
164
    CYGARC_HAL_SAVE_GP();
165
 
166
    while(__len-- > 0)
167
        *__buf++ = cyg_hal_plf_serial_getc(__ch_data);
168
 
169
    CYGARC_HAL_RESTORE_GP();
170
}
171
 
172
cyg_bool
173
cyg_hal_plf_serial_getc_timeout(void* __ch_data, cyg_uint8* ch)
174
{
175
    int delay_count;
176
    channel_data_t* chan = (channel_data_t*)__ch_data;
177
    cyg_bool res;
178
    CYGARC_HAL_SAVE_GP();
179
 
180
    delay_count = chan->msec_timeout * 10; // delay in .1 ms steps
181
 
182
    for(;;) {
183
        res = cyg_hal_plf_serial_getc_nonblock(__ch_data, ch);
184
        if (res || 0 == delay_count--)
185
            break;
186
 
187
        CYGACC_CALL_IF_DELAY_US(100);
188
    }
189
 
190
    CYGARC_HAL_RESTORE_GP();
191
    return res;
192
}
193
 
194
static int
195
cyg_hal_plf_serial_control(void *__ch_data, __comm_control_cmd_t __func, ...)
196
{
197
    static int irq_state = 0;
198
    channel_data_t* chan = (channel_data_t*)__ch_data;
199
    cyg_uint8* base = ((channel_data_t*)__ch_data)->base;
200
    int ret = 0;
201
    va_list ap;
202
 
203
    CYGARC_HAL_SAVE_GP();
204
    va_start(ap, __func);
205
 
206
    switch (__func) {
207
    case __COMMCTL_GETBAUD:
208
        ret = chan->baud_rate;
209
        break;
210
    case __COMMCTL_SETBAUD:
211
        chan->baud_rate = va_arg(ap, cyg_int32);
212
        // Should we verify this value here?
213
        cyg_hal_plf_serial_init_channel(chan);
214
        ret = 0;
215
        break;
216
    case __COMMCTL_IRQ_ENABLE:
217
        irq_state = 1;
218
        HAL_INTERRUPT_ACKNOWLEDGE(chan->isr_vector);
219
        HAL_INTERRUPT_UNMASK(chan->isr_vector);
220
        HAL_WRITE_UINT32(base+AT91_US_IER, AT91_US_IER_RxRDY);
221
        break;
222
    case __COMMCTL_IRQ_DISABLE:
223
        ret = irq_state;
224
        irq_state = 0;
225
        HAL_INTERRUPT_MASK(chan->isr_vector);
226
        HAL_WRITE_UINT32(base+AT91_US_IDR, AT91_US_IER_RxRDY);
227
        break;
228
    case __COMMCTL_DBG_ISR_VECTOR:
229
        ret = chan->isr_vector;
230
        break;
231
    case __COMMCTL_SET_TIMEOUT:
232
        ret = chan->msec_timeout;
233
        chan->msec_timeout = va_arg(ap, cyg_uint32);
234
    default:
235
        break;
236
    }
237
 
238
    va_end(ap);
239
    CYGARC_HAL_RESTORE_GP();
240
    return ret;
241
}
242
 
243
static int
244
cyg_hal_plf_serial_isr(void *__ch_data, int* __ctrlc,
245
                       CYG_ADDRWORD __vector, CYG_ADDRWORD __data)
246
{
247
    int res = 0;
248
    channel_data_t* chan = (channel_data_t*)__ch_data;
249
    cyg_uint32 c;
250
    cyg_uint8 ch;
251
    cyg_uint32 stat;
252
    CYGARC_HAL_SAVE_GP();
253
 
254
    *__ctrlc = 0;
255
    HAL_READ_UINT32(chan->base+AT91_US_CSR, stat);
256
    if ( (stat & AT91_US_CSR_RxRDY) != 0 ) {
257
 
258
        HAL_READ_UINT32(chan->base+AT91_US_RHR, c);
259
        ch = (cyg_uint8)(c & 0xff);
260
        if( cyg_hal_is_break( &ch , 1 ) )
261
            *__ctrlc = 1;
262
 
263
        res = CYG_ISR_HANDLED;
264
    }
265
 
266
    HAL_INTERRUPT_ACKNOWLEDGE(chan->isr_vector);
267
 
268
    CYGARC_HAL_RESTORE_GP();
269
    return res;
270
}
271
 
272
static channel_data_t at91_ser_channels[CYGNUM_HAL_VIRTUAL_VECTOR_COMM_CHANNELS] = {
273
#if CYGNUM_HAL_VIRTUAL_VECTOR_COMM_CHANNELS > 0
274
    { (cyg_uint8*)AT91_USART0, 1000, CYGNUM_HAL_INTERRUPT_USART0, CYGNUM_HAL_VIRTUAL_VECTOR_CONSOLE_CHANNEL_BAUD},
275
#if CYGNUM_HAL_VIRTUAL_VECTOR_COMM_CHANNELS > 1
276
    { (cyg_uint8*)AT91_USART1, 1000, CYGNUM_HAL_INTERRUPT_USART1, CYGNUM_HAL_VIRTUAL_VECTOR_CONSOLE_CHANNEL_BAUD},
277
#if CYGNUM_HAL_VIRTUAL_VECTOR_COMM_CHANNELS > 2
278
    { (cyg_uint8*)AT91_USART2, 1000, CYGNUM_HAL_INTERRUPT_USART2, CYGNUM_HAL_VIRTUAL_VECTOR_CONSOLE_CHANNEL_BAUD}
279
#endif
280
#endif
281
#endif
282
};
283
 
284
static void
285
cyg_hal_plf_serial_init(void)
286
{
287
    hal_virtual_comm_table_t* comm;
288
    int cur;
289
 
290
    cur = CYGACC_CALL_IF_SET_CONSOLE_COMM(CYGNUM_CALL_IF_SET_COMM_ID_QUERY_CURRENT);
291
 
292
    // Init channels
293
#if CYGNUM_HAL_VIRTUAL_VECTOR_COMM_CHANNELS > 0
294
    cyg_hal_plf_serial_init_channel(&at91_ser_channels[0]);
295
#if CYGNUM_HAL_VIRTUAL_VECTOR_COMM_CHANNELS > 1
296
    cyg_hal_plf_serial_init_channel(&at91_ser_channels[1]);
297
#if CYGNUM_HAL_VIRTUAL_VECTOR_COMM_CHANNELS > 2
298
    cyg_hal_plf_serial_init_channel(&at91_ser_channels[2]);
299
#endif
300
#endif
301
#endif
302
    // Setup procs in the vector table
303
 
304
    // Set channel 0
305
#if CYGNUM_HAL_VIRTUAL_VECTOR_COMM_CHANNELS > 0
306
    CYGACC_CALL_IF_SET_CONSOLE_COMM(0);
307
    comm = CYGACC_CALL_IF_CONSOLE_PROCS();
308
    CYGACC_COMM_IF_CH_DATA_SET(*comm, &at91_ser_channels[0]);
309
    CYGACC_COMM_IF_WRITE_SET(*comm, cyg_hal_plf_serial_write);
310
    CYGACC_COMM_IF_READ_SET(*comm, cyg_hal_plf_serial_read);
311
    CYGACC_COMM_IF_PUTC_SET(*comm, cyg_hal_plf_serial_putc);
312
    CYGACC_COMM_IF_GETC_SET(*comm, cyg_hal_plf_serial_getc);
313
    CYGACC_COMM_IF_CONTROL_SET(*comm, cyg_hal_plf_serial_control);
314
    CYGACC_COMM_IF_DBG_ISR_SET(*comm, cyg_hal_plf_serial_isr);
315
    CYGACC_COMM_IF_GETC_TIMEOUT_SET(*comm, cyg_hal_plf_serial_getc_timeout);
316
 
317
#if CYGNUM_HAL_VIRTUAL_VECTOR_COMM_CHANNELS > 1
318
    // Set channel 1
319
    CYGACC_CALL_IF_SET_CONSOLE_COMM(1);
320
    comm = CYGACC_CALL_IF_CONSOLE_PROCS();
321
    CYGACC_COMM_IF_CH_DATA_SET(*comm, &at91_ser_channels[1]);
322
    CYGACC_COMM_IF_WRITE_SET(*comm, cyg_hal_plf_serial_write);
323
    CYGACC_COMM_IF_READ_SET(*comm, cyg_hal_plf_serial_read);
324
    CYGACC_COMM_IF_PUTC_SET(*comm, cyg_hal_plf_serial_putc);
325
    CYGACC_COMM_IF_GETC_SET(*comm, cyg_hal_plf_serial_getc);
326
    CYGACC_COMM_IF_CONTROL_SET(*comm, cyg_hal_plf_serial_control);
327
    CYGACC_COMM_IF_DBG_ISR_SET(*comm, cyg_hal_plf_serial_isr);
328
    CYGACC_COMM_IF_GETC_TIMEOUT_SET(*comm, cyg_hal_plf_serial_getc_timeout);
329
 
330
#if CYGNUM_HAL_VIRTUAL_VECTOR_COMM_CHANNELS > 2    
331
    CYGACC_CALL_IF_SET_CONSOLE_COMM(2);
332
    comm = CYGACC_CALL_IF_CONSOLE_PROCS();
333
    CYGACC_COMM_IF_CH_DATA_SET(*comm, &at91_ser_channels[2]);
334
    CYGACC_COMM_IF_WRITE_SET(*comm, cyg_hal_plf_serial_write);
335
    CYGACC_COMM_IF_READ_SET(*comm, cyg_hal_plf_serial_read);
336
    CYGACC_COMM_IF_PUTC_SET(*comm, cyg_hal_plf_serial_putc);
337
    CYGACC_COMM_IF_GETC_SET(*comm, cyg_hal_plf_serial_getc);
338
    CYGACC_COMM_IF_CONTROL_SET(*comm, cyg_hal_plf_serial_control);
339
    CYGACC_COMM_IF_DBG_ISR_SET(*comm, cyg_hal_plf_serial_isr);
340
    CYGACC_COMM_IF_GETC_TIMEOUT_SET(*comm, cyg_hal_plf_serial_getc_timeout);
341
#endif
342
#endif
343
#endif
344
 
345
    // Restore original console
346
    CYGACC_CALL_IF_SET_CONSOLE_COMM(cur);
347
}
348
 
349
void
350
cyg_hal_plf_comms_init(void)
351
{
352
    static int initialized = 0;
353
 
354
    if (initialized)
355
        return;
356
 
357
    initialized = 1;
358
 
359
    cyg_hal_plf_serial_init();
360
 
361
#ifdef CYGBLD_HAL_ARM_AT91_DCC
362
    cyg_hal_plf_dcc_init(CYGBLD_HAL_ARM_AT91_DCC_CHANNEL);
363
#endif
364
}
365
 
366
void
367
hal_diag_led(int mask)
368
{
369
    hal_at91_set_leds(mask);
370
}
371
 
372
//-----------------------------------------------------------------------------
373
// End of hal_diag.c

powered by: WebSVN 2.1.0

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