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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-2.0/] [packages/] [hal/] [sh/] [sh3/] [v2_0/] [src/] [sh3_scif.c] - Blame information for rev 174

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 27 unneback
//=============================================================================
2
//
3
//      sh3_scif.c
4
//
5
//      Simple driver for the SH3 Serial Communication Interface with FIFO
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
//
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 version.
16
//
17
// eCos is distributed in the hope that it will be useful, but WITHOUT ANY
18
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
19
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
20
// for more details.
21
//
22
// You should have received a copy of the GNU General Public License along
23
// with eCos; if not, write to the Free Software Foundation, Inc.,
24
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
25
//
26
// As a special exception, if other files instantiate templates or use macros
27
// or inline functions from this file, or you compile this file and link it
28
// with other works to produce a work based on this file, this file does not
29
// by itself cause the resulting work to be covered by the GNU General Public
30
// License. However the source code for this file must still be made available
31
// in accordance with section (3) of the GNU General Public License.
32
//
33
// This exception does not invalidate any other reasons why a work based on
34
// this file might be covered by the GNU General Public License.
35
//
36
// Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
37
// at http://sources.redhat.com/ecos/ecos-license/
38
// -------------------------------------------
39
//####ECOSGPLCOPYRIGHTEND####
40
//=============================================================================
41
//#####DESCRIPTIONBEGIN####
42
//
43
// Author(s):   jskov
44
// Contributors:jskov
45
// Date:        2000-03-30
46
// Description: Simple driver for the SH Serial Communication Interface
47
//              The driver can be used for either the SCIF or the IRDA
48
//              modules (the latter can act as the former).
49
//              Clients of this file can configure the behavior with:
50
//              CYGNUM_SCIF_PORTS: number of SCI ports
51
//
52
// Note:        It should be possible to configure a channel to IRDA mode.
53
//              Worry about that when some board needs it.
54
//
55
//####DESCRIPTIONEND####
56
//
57
//=============================================================================
58
 
59
#include <pkgconf/hal.h>
60
 
61
#ifdef CYGNUM_HAL_SH_SH3_SCIF_PORTS
62
 
63
#include <cyg/hal/hal_io.h>             // IO macros
64
#include <cyg/hal/drv_api.h>            // CYG_ISR_HANDLED
65
#include <cyg/hal/hal_misc.h>           // Helper functions
66
#include <cyg/hal/hal_intr.h>           // HAL_ENABLE/MASK/UNMASK_INTERRUPTS
67
#include <cyg/hal/hal_arch.h>           // SAVE/RESTORE GP
68
#include <cyg/hal/hal_if.h>             // Calling-if API
69
#include <cyg/hal/sh_regs.h>            // serial register definitions
70
#include <cyg/hal/sh_stub.h>            // target_register_t
71
 
72
#define CYGPRI_HAL_SH_SH3_SCIF_PRIVATE
73
#include <cyg/hal/sh3_scif.h>           // our header
74
 
75
//--------------------------------------------------------------------------
76
 
77
void
78
cyg_hal_plf_scif_init_channel(channel_data_t* chan)
79
{
80
    cyg_uint8* base = chan->base;
81
    cyg_uint8 tmp;
82
    cyg_uint16 sr;
83
 
84
    // Disable everything.
85
    HAL_WRITE_UINT8(base+_REG_SCSCR, 0);
86
 
87
    // Reset FIFO.
88
    HAL_WRITE_UINT8(base+_REG_SCFCR,
89
                    CYGARC_REG_SCIF_SCFCR_TFRST|CYGARC_REG_SCIF_SCFCR_RFRST);
90
 
91
    // 8-1-no parity.
92
    HAL_WRITE_UINT8(base+_REG_SCSMR, 0);
93
 
94
    // Set speed to CYGNUM_HAL_SH_SH3_SCIF_DEFAULT_BAUD_RATE
95
    HAL_READ_UINT8(base+_REG_SCSMR, tmp);
96
    tmp &= ~CYGARC_REG_SCIF_SCSMR_CKSx_MASK;
97
    tmp |= CYGARC_SCBRR_CKSx(CYGNUM_HAL_SH_SH3_SCIF_BAUD_RATE);
98
    HAL_WRITE_UINT8(base+_REG_SCSMR, tmp);
99
    HAL_WRITE_UINT8(base+_REG_SCBRR, CYGARC_SCBRR_N(CYGNUM_HAL_SH_SH3_SCIF_BAUD_RATE));
100
 
101
    // Let things settle: Here we should should wait the equivalent of
102
    // one bit interval,
103
    // i.e. 1/CYGNUM_HAL_SH_SH3_SCIF_DEFAULT_BAUD_RATE second, but
104
    // until we have something like the Linux delay loop, it's hard to
105
    // do reliably. So just move on and hope for the best (this is
106
    // unlikely to cause problems since the CPU has just come out of
107
    // reset anyway).
108
 
109
    // Clear status register (read back first).
110
    HAL_READ_UINT16(base+_REG_SCSSR, sr);
111
    HAL_WRITE_UINT16(base+_REG_SCSSR, 0);
112
 
113
    // Bring FIFO out of reset and set to trigger on every char in
114
    // FIFO (or C-c input would not be processed).
115
    HAL_WRITE_UINT8(base+_REG_SCFCR,
116
                    CYGARC_REG_SCIF_SCFCR_RTRG_1|CYGARC_REG_SCIF_SCFCR_TTRG_1);
117
 
118
    // Leave Tx/Rx interrupts disabled, but enable Tx/Rx
119
    HAL_WRITE_UINT8(base+_REG_SCSCR,
120
                    CYGARC_REG_SCIF_SCSCR_TE|CYGARC_REG_SCIF_SCSCR_RE);
121
}
122
 
123
//static 
124
cyg_bool
125
cyg_hal_plf_scif_getc_nonblock(void* __ch_data, cyg_uint8* ch)
126
{
127
    cyg_uint8* base = ((channel_data_t*)__ch_data)->base;
128
    cyg_uint16 fdr, sr;
129
 
130
    HAL_READ_UINT16(base+_REG_SCFDR, fdr);
131
    if (0 == (fdr & CYGARC_REG_SCIF_SCFDR_RCOUNT_MASK))
132
        return false;
133
 
134
    HAL_READ_UINT8(base+_REG_SCFRDR, *ch);
135
 
136
    // Clear DR/RDF flags
137
    HAL_READ_UINT16(base+_REG_SCSSR, sr);
138
    HAL_WRITE_UINT16(base+_REG_SCSSR,
139
                     CYGARC_REG_SCIF_SCSSR_CLEARMASK & ~(CYGARC_REG_SCIF_SCSSR_RDF | CYGARC_REG_SCIF_SCSSR_DR));
140
 
141
    return true;
142
}
143
 
144
cyg_uint8
145
cyg_hal_plf_scif_getc(void* __ch_data)
146
{
147
    cyg_uint8 ch;
148
    CYGARC_HAL_SAVE_GP();
149
 
150
    while(!cyg_hal_plf_scif_getc_nonblock(__ch_data, &ch));
151
 
152
    CYGARC_HAL_RESTORE_GP();
153
    return ch;
154
}
155
 
156
void
157
cyg_hal_plf_scif_putc(void* __ch_data, cyg_uint8 c)
158
{
159
    cyg_uint8* base = ((channel_data_t*)__ch_data)->base;
160
    cyg_uint16 fdr, sr;
161
    CYGARC_HAL_SAVE_GP();
162
 
163
    do {
164
        HAL_READ_UINT16(base+_REG_SCFDR, fdr);
165
    } while (((fdr & CYGARC_REG_SCIF_SCFDR_TCOUNT_MASK) >> CYGARC_REG_SCIF_SCFDR_TCOUNT_shift) == 16);
166
 
167
    HAL_WRITE_UINT8(base+_REG_SCFTDR, c);
168
 
169
    // Clear FIFO-empty/transmit end flags (read back SR first)
170
    HAL_READ_UINT16(base+_REG_SCSSR, sr);
171
    HAL_WRITE_UINT16(base+_REG_SCSSR, CYGARC_REG_SCIF_SCSSR_CLEARMASK
172
                     & ~(CYGARC_REG_SCIF_SCSSR_TDFE | CYGARC_REG_SCIF_SCSSR_TEND ));
173
 
174
    // Hang around until the character has been safely sent.
175
    do {
176
        HAL_READ_UINT16(base+_REG_SCFDR, fdr);
177
    } while ((fdr & CYGARC_REG_SCIF_SCFDR_TCOUNT_MASK) != 0);
178
 
179
    CYGARC_HAL_RESTORE_GP();
180
}
181
 
182
 
183
static channel_data_t channels[CYGNUM_HAL_SH_SH3_SCIF_PORTS];
184
 
185
static void
186
cyg_hal_plf_scif_write(void* __ch_data, const cyg_uint8* __buf,
187
                         cyg_uint32 __len)
188
{
189
    CYGARC_HAL_SAVE_GP();
190
 
191
    while(__len-- > 0)
192
        cyg_hal_plf_scif_putc(__ch_data, *__buf++);
193
 
194
    CYGARC_HAL_RESTORE_GP();
195
}
196
 
197
static void
198
cyg_hal_plf_scif_read(void* __ch_data, cyg_uint8* __buf, cyg_uint32 __len)
199
{
200
    CYGARC_HAL_SAVE_GP();
201
 
202
    while(__len-- > 0)
203
        *__buf++ = cyg_hal_plf_scif_getc(__ch_data);
204
 
205
    CYGARC_HAL_RESTORE_GP();
206
}
207
 
208
cyg_bool
209
cyg_hal_plf_scif_getc_timeout(void* __ch_data, cyg_uint8* ch)
210
{
211
    channel_data_t* chan = (channel_data_t*)__ch_data;
212
    int delay_count;
213
    cyg_bool res;
214
    CYGARC_HAL_SAVE_GP();
215
 
216
    delay_count = chan->msec_timeout * 10; // delay in .1 ms steps
217
 
218
    for(;;) {
219
        res = cyg_hal_plf_scif_getc_nonblock(__ch_data, ch);
220
        if (res || 0 == delay_count--)
221
            break;
222
 
223
        CYGACC_CALL_IF_DELAY_US(100);
224
    }
225
 
226
    CYGARC_HAL_RESTORE_GP();
227
    return res;
228
}
229
 
230
static int
231
cyg_hal_plf_scif_control(void *__ch_data, __comm_control_cmd_t __func, ...)
232
{
233
    static int irq_state = 0;
234
    channel_data_t* chan = (channel_data_t*)__ch_data;
235
    cyg_uint8 scr;
236
    int ret = 0;
237
    CYGARC_HAL_SAVE_GP();
238
 
239
    switch (__func) {
240
    case __COMMCTL_IRQ_ENABLE:
241
        irq_state = 1;
242
        HAL_INTERRUPT_UNMASK(chan->isr_vector);
243
        HAL_READ_UINT8(chan->base+_REG_SCSCR, scr);
244
        scr |= CYGARC_REG_SCIF_SCSCR_RIE;
245
        HAL_WRITE_UINT8(chan->base+_REG_SCSCR, scr);
246
        break;
247
    case __COMMCTL_IRQ_DISABLE:
248
        ret = irq_state;
249
        irq_state = 0;
250
        HAL_INTERRUPT_UNMASK(chan->isr_vector);
251
        HAL_READ_UINT8(chan->base+_REG_SCSCR, scr);
252
        scr &= ~CYGARC_REG_SCIF_SCSCR_RIE;
253
        HAL_WRITE_UINT8(chan->base+_REG_SCSCR, scr);
254
        break;
255
    case __COMMCTL_DBG_ISR_VECTOR:
256
        ret = chan->isr_vector;
257
        break;
258
    case __COMMCTL_SET_TIMEOUT:
259
    {
260
        va_list ap;
261
 
262
        va_start(ap, __func);
263
 
264
        ret = chan->msec_timeout;
265
        chan->msec_timeout = va_arg(ap, cyg_uint32);
266
 
267
        va_end(ap);
268
    }
269
    default:
270
        break;
271
    }
272
    CYGARC_HAL_RESTORE_GP();
273
    return ret;
274
}
275
 
276
static int
277
cyg_hal_plf_scif_isr(void *__ch_data, int* __ctrlc,
278
                     CYG_ADDRWORD __vector, CYG_ADDRWORD __data)
279
{
280
    cyg_uint8 c;
281
    cyg_uint16 fdr, sr;
282
    cyg_uint8* base = ((channel_data_t*)__ch_data)->base;
283
    int res = 0;
284
    CYGARC_HAL_SAVE_GP();
285
 
286
    *__ctrlc = 0;
287
    HAL_READ_UINT16(base+_REG_SCFDR, fdr);
288
    if ((fdr & CYGARC_REG_SCIF_SCFDR_RCOUNT_MASK) != 0) {
289
        HAL_READ_UINT8(base+_REG_SCFRDR, c);
290
 
291
        // Clear buffer full flag (read back first).
292
        HAL_READ_UINT16(base+_REG_SCSSR, sr);
293
        HAL_WRITE_UINT16(base+_REG_SCSSR,
294
                         CYGARC_REG_SCIF_SCSSR_CLEARMASK & ~CYGARC_REG_SCIF_SCSSR_RDF);
295
 
296
        if( cyg_hal_is_break( &c , 1 ) )
297
            *__ctrlc = 1;
298
 
299
        res = CYG_ISR_HANDLED;
300
    }
301
 
302
    CYGARC_HAL_RESTORE_GP();
303
    return res;
304
}
305
 
306
void
307
cyg_hal_plf_scif_init(int scif_index, int comm_index,
308
                      int rcv_vect, cyg_uint8* base)
309
{
310
    channel_data_t* chan = &channels[scif_index];
311
    hal_virtual_comm_table_t* comm;
312
    int cur = CYGACC_CALL_IF_SET_CONSOLE_COMM(CYGNUM_CALL_IF_SET_COMM_ID_QUERY_CURRENT);
313
 
314
    // Initialize channel table
315
    chan->base = base;
316
    chan->isr_vector = rcv_vect;
317
    chan->msec_timeout = 1000;
318
 
319
    // Disable interrupts.
320
    HAL_INTERRUPT_MASK(chan->isr_vector);
321
 
322
    // Init channel
323
    cyg_hal_plf_scif_init_channel(chan);
324
 
325
    // Setup procs in the vector table
326
 
327
    // Initialize channel procs
328
    CYGACC_CALL_IF_SET_CONSOLE_COMM(comm_index);
329
    comm = CYGACC_CALL_IF_CONSOLE_PROCS();
330
    CYGACC_COMM_IF_CH_DATA_SET(*comm, chan);
331
    CYGACC_COMM_IF_WRITE_SET(*comm, cyg_hal_plf_scif_write);
332
    CYGACC_COMM_IF_READ_SET(*comm, cyg_hal_plf_scif_read);
333
    CYGACC_COMM_IF_PUTC_SET(*comm, cyg_hal_plf_scif_putc);
334
    CYGACC_COMM_IF_GETC_SET(*comm, cyg_hal_plf_scif_getc);
335
    CYGACC_COMM_IF_CONTROL_SET(*comm, cyg_hal_plf_scif_control);
336
    CYGACC_COMM_IF_DBG_ISR_SET(*comm, cyg_hal_plf_scif_isr);
337
    CYGACC_COMM_IF_GETC_TIMEOUT_SET(*comm, cyg_hal_plf_scif_getc_timeout);
338
 
339
    // Restore original console
340
    CYGACC_CALL_IF_SET_CONSOLE_COMM(cur);
341
}
342
 
343
#endif // CYGNUM_HAL_SH_SH3_SCIF_PORTS
344
 
345
//-----------------------------------------------------------------------------
346
// end of sh3_scif.c

powered by: WebSVN 2.1.0

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