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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [packages/] [hal/] [sh/] [sh4/] [current/] [src/] [sh4_scif.c] - Blame information for rev 786

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 786 skrzyp
//=============================================================================
2
//
3
//      sh4_scif.c
4
//
5
//      Simple driver for the SH4 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 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:Haruki Kashiwaya
44
// Date:        2000-08-09
45
// Description: Simple driver for the SH Serial Communication Interface
46
//              The driver can be used for the SCIF modules.
47
//              Clients of this file can configure the behavior with:
48
//              CYGNUM_SCIF_PORTS: number of SCI ports
49
//
50
//####DESCRIPTIONEND####
51
//
52
//=============================================================================
53
 
54
#include <pkgconf/hal.h>
55
 
56
#ifdef CYGNUM_HAL_SH_SH4_SCIF_PORTS
57
 
58
#include <cyg/hal/hal_io.h>             // IO macros
59
#include <cyg/hal/drv_api.h>            // CYG_ISR_HANDLED
60
#include <cyg/hal/hal_misc.h>           // Helper functions
61
#include <cyg/hal/hal_intr.h>           // HAL_ENABLE/MASK/UNMASK_INTERRUPTS
62
#include <cyg/hal/hal_arch.h>           // SAVE/RESTORE GP
63
#include <cyg/hal/hal_if.h>             // Calling-if API
64
#include <cyg/hal/sh_regs.h>            // serial register definitions
65
#include <cyg/hal/sh_stub.h>            // target_register_t
66
 
67
#define CYGPRI_HAL_SH_SH4_SCIF_PRIVATE
68
#include <cyg/hal/sh4_scif.h>           // our header
69
 
70
//--------------------------------------------------------------------------
71
 
72
static void
73
cyg_hal_plf_scif_set_baud(cyg_uint8 *base, cyg_uint32 baud)
74
{
75
    cyg_uint16 tmp;
76
 
77
    // Set desired baudrate
78
    HAL_READ_UINT16(base+_REG_SCSMR, tmp);
79
    tmp &= ~CYGARC_REG_SCIF_SCSMR_CKSx_MASK;
80
    tmp |= CYGARC_SCBRR_CKSx(baud);
81
    HAL_WRITE_UINT16(base+_REG_SCSMR, tmp);
82
    HAL_WRITE_UINT8(base+_REG_SCBRR,
83
                    CYGARC_SCBRR_N(baud));
84
 
85
    // Let things settle: Here we should wait the equivalent of
86
    // one bit interval, i.e. 1/<baudrate> second, but we'll wait twice
87
    // that to be sure.
88
    CYGACC_CALL_IF_DELAY_US(2000000/baud);
89
}
90
 
91
void
92
cyg_hal_plf_scif_init_channel(channel_data_t* chan)
93
{
94
    cyg_uint8* base = chan->base;
95
    cyg_uint16 sr;
96
 
97
    // Disable everything.
98
    HAL_WRITE_UINT16(base+_REG_SCSCR, 0);
99
 
100
    // Reset FIFO.
101
    HAL_WRITE_UINT16(base+_REG_SCFCR,
102
                    CYGARC_REG_SCIF_SCFCR_TFRST|CYGARC_REG_SCIF_SCFCR_RFRST);
103
 
104
    // 8-1-no parity.
105
    HAL_WRITE_UINT16(base+_REG_SCSMR, 0);
106
 
107
    chan->baud_rate = CYGNUM_HAL_SH_SH4_SCIF_BAUD_RATE;
108
    cyg_hal_plf_scif_set_baud(base, CYGNUM_HAL_SH_SH4_SCIF_BAUD_RATE);
109
 
110
    // Clear status register (read back first).
111
    HAL_READ_UINT16(base+_REG_SCFSR, sr);
112
    HAL_WRITE_UINT16(base+_REG_SCFSR, 0);
113
 
114
    // Bring FIFO out of reset and set to trigger on every char in
115
    // FIFO (or C-c input would not be processed).
116
    HAL_WRITE_UINT16(base+_REG_SCFCR,
117
                    CYGARC_REG_SCIF_SCFCR_RTRG_1|CYGARC_REG_SCIF_SCFCR_TTRG_1);
118
 
119
    // Leave Tx/Rx interrupts disabled, but enable Tx/Rx
120
    HAL_WRITE_UINT16(base+_REG_SCSCR,
121
                    CYGARC_REG_SCIF_SCSCR_TE|CYGARC_REG_SCIF_SCSCR_RE);
122
}
123
 
124
static 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 ((fdr & CYGARC_REG_SCIF_SCFDR_RCOUNT_MASK) == 0)
132
        return false;
133
 
134
    HAL_READ_UINT8(base+_REG_SCFRDR, *ch);
135
 
136
    // Clear FIFO full flag (read before clearing)
137
    HAL_READ_UINT16(base+_REG_SCFSR, sr);
138
    HAL_WRITE_UINT16(base+_REG_SCFSR,
139
                    CYGARC_REG_SCIF_SCSSR_CLEARMASK & ~CYGARC_REG_SCIF_SCSSR_RDF);
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_SCFSR, sr);
171
    HAL_WRITE_UINT16(base+_REG_SCFSR, 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_SH4_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_UINT16(chan->base+_REG_SCSCR, scr);
244
        scr |= CYGARC_REG_SCIF_SCSCR_RIE;
245
        HAL_WRITE_UINT16(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_UINT16(chan->base+_REG_SCSCR, scr);
252
        scr &= ~CYGARC_REG_SCIF_SCSCR_RIE;
253
        HAL_WRITE_UINT16(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
    break;
270
    case __COMMCTL_SETBAUD:
271
    {
272
        cyg_uint8* base = chan->base;
273
        va_list ap;
274
 
275
        va_start(ap, __func);
276
        chan->baud_rate = va_arg(ap, cyg_uint32);
277
        va_end(ap);
278
 
279
        // Set desired baudrate
280
        cyg_hal_plf_scif_set_baud(base, chan->baud_rate);
281
    }
282
    break;
283
    case __COMMCTL_GETBAUD:
284
        ret = chan->baud_rate;
285
        break;
286
    default:
287
        break;
288
    }
289
    CYGARC_HAL_RESTORE_GP();
290
    return ret;
291
}
292
 
293
static int
294
cyg_hal_plf_scif_isr(void *__ch_data, int* __ctrlc,
295
                     CYG_ADDRWORD __vector, CYG_ADDRWORD __data)
296
{
297
    cyg_uint8 c;
298
    cyg_uint16 fdr, sr;
299
    cyg_uint8* base = ((channel_data_t*)__ch_data)->base;
300
    int res = 0;
301
    CYGARC_HAL_SAVE_GP();
302
 
303
    *__ctrlc = 0;
304
    HAL_READ_UINT16(base+_REG_SCFDR, fdr);
305
    if ((fdr & CYGARC_REG_SCIF_SCFDR_RCOUNT_MASK) != 0) {
306
        HAL_READ_UINT8(base+_REG_SCFRDR, c);
307
 
308
        // Clear buffer full flag (read back first).
309
        HAL_READ_UINT16(base+_REG_SCFSR, sr);
310
        HAL_WRITE_UINT16(base+_REG_SCFSR,
311
                         CYGARC_REG_SCIF_SCSSR_CLEARMASK & ~CYGARC_REG_SCIF_SCSSR_RDF);
312
 
313
        if( cyg_hal_is_break( &c , 1 ) )
314
            *__ctrlc = 1;
315
 
316
        res = CYG_ISR_HANDLED;
317
    }
318
 
319
    CYGARC_HAL_RESTORE_GP();
320
    return res;
321
}
322
 
323
void
324
cyg_hal_plf_scif_init(int scif_index, int comm_index,
325
                      int rcv_vect, cyg_uint8* base)
326
{
327
    channel_data_t* chan = &channels[scif_index];
328
    hal_virtual_comm_table_t* comm;
329
    int cur = CYGACC_CALL_IF_SET_CONSOLE_COMM(CYGNUM_CALL_IF_SET_COMM_ID_QUERY_CURRENT);
330
 
331
    // Initialize channel table
332
    chan->base = base;
333
    chan->isr_vector = rcv_vect;
334
    chan->msec_timeout = 1000;
335
 
336
    // Disable interrupts.
337
    HAL_INTERRUPT_MASK(chan->isr_vector);
338
 
339
    // Init channel
340
    cyg_hal_plf_scif_init_channel(chan);
341
 
342
    // Setup procs in the vector table
343
 
344
    // Initialize channel procs
345
    CYGACC_CALL_IF_SET_CONSOLE_COMM(comm_index);
346
    comm = CYGACC_CALL_IF_CONSOLE_PROCS();
347
    CYGACC_COMM_IF_CH_DATA_SET(*comm, chan);
348
    CYGACC_COMM_IF_WRITE_SET(*comm, cyg_hal_plf_scif_write);
349
    CYGACC_COMM_IF_READ_SET(*comm, cyg_hal_plf_scif_read);
350
    CYGACC_COMM_IF_PUTC_SET(*comm, cyg_hal_plf_scif_putc);
351
    CYGACC_COMM_IF_GETC_SET(*comm, cyg_hal_plf_scif_getc);
352
    CYGACC_COMM_IF_CONTROL_SET(*comm, cyg_hal_plf_scif_control);
353
    CYGACC_COMM_IF_DBG_ISR_SET(*comm, cyg_hal_plf_scif_isr);
354
    CYGACC_COMM_IF_GETC_TIMEOUT_SET(*comm, cyg_hal_plf_scif_getc_timeout);
355
 
356
    // Restore original console
357
    CYGACC_CALL_IF_SET_CONSOLE_COMM(cur);
358
}
359
 
360
#endif // CYGNUM_HAL_SH_SH4_SCIF_PORTS
361
 
362
//-----------------------------------------------------------------------------
363
// end of sh4_scif.c

powered by: WebSVN 2.1.0

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