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

Subversion Repositories openrisc

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

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 27 unneback
//=============================================================================
2
//
3
//      sh2_sci.c
4
//
5
//      Simple driver for the SH Serial Communication Interface (SCI)
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:        1999-05-17
46
// Description: Simple driver for the SH Serial Communication Interface
47
//              Clients of this file can configure the behavior with:
48
//              CYGNUM_SCI_PORTS:  number of SCI ports
49
//
50
//####DESCRIPTIONEND####
51
//
52
//=============================================================================
53
 
54
#include <pkgconf/hal.h>
55
 
56
#ifdef CYGNUM_HAL_SH_SH2_SCI_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
 
66
#define CYGPRI_HAL_SH_SH2_SCI_PRIVATE
67
#include <cyg/hal/sh2_sci.h>            // our header
68
 
69
//--------------------------------------------------------------------------
70
 
71
void
72
cyg_hal_plf_sci_init_channel(channel_data_t* chan)
73
{
74
    cyg_uint8 tmp;
75
    cyg_uint8* base = chan->base;
76
 
77
    // Disable Tx/Rx interrupts, but enable Tx/Rx
78
    HAL_WRITE_UINT8(base+_REG_SCSCR,
79
                    CYGARC_REG_SCI_SCSCR_TE|CYGARC_REG_SCI_SCSCR_RE);
80
 
81
    // 8-1-no parity.
82
    HAL_WRITE_UINT8(base+_REG_SCSMR, 0);
83
 
84
    // Set speed to CYGNUM_HAL_SH_SH2_SCI_DEFAULT_BAUD_RATE
85
    HAL_READ_UINT8(base+_REG_SCSMR, tmp);
86
    tmp &= ~CYGARC_REG_SCI_SCSMR_CKSx_MASK;
87
    tmp |= CYGARC_SCBRR_CKSx(CYGNUM_HAL_SH_SH2_SCI_BAUD_RATE);
88
    HAL_WRITE_UINT8(base+_REG_SCSMR, tmp);
89
    HAL_WRITE_UINT8(base+_REG_SCBRR, CYGARC_SCBRR_N(CYGNUM_HAL_SH_SH2_SCI_BAUD_RATE));
90
}
91
 
92
static cyg_bool
93
cyg_hal_plf_sci_getc_nonblock(void* __ch_data, cyg_uint8* ch)
94
{
95
    cyg_uint8* base = ((channel_data_t*)__ch_data)->base;
96
    cyg_uint8 sr;
97
 
98
    HAL_READ_UINT8(base+_REG_SCSSR, sr);
99
    if (sr & CYGARC_REG_SCI_SCSSR_ORER) {
100
        // Serial RX overrun. Clear error and let caller try again.
101
        HAL_WRITE_UINT8(base+_REG_SCSSR,
102
                        CYGARC_REG_SCI_SCSSR_CLEARMASK & ~CYGARC_REG_SCI_SCSSR_ORER);
103
        return false;
104
    }
105
 
106
    if ((sr & CYGARC_REG_SCI_SCSSR_RDRF) == 0)
107
        return false;
108
 
109
    HAL_READ_UINT8(base+_REG_SCRDR, *ch);
110
 
111
    // Clear buffer full flag.
112
    HAL_WRITE_UINT8(base+_REG_SCSSR, sr & ~CYGARC_REG_SCI_SCSSR_RDRF);
113
 
114
    return true;
115
}
116
 
117
cyg_uint8
118
cyg_hal_plf_sci_getc(void* __ch_data)
119
{
120
    cyg_uint8 ch;
121
    CYGARC_HAL_SAVE_GP();
122
 
123
    while(!cyg_hal_plf_sci_getc_nonblock(__ch_data, &ch));
124
 
125
    CYGARC_HAL_RESTORE_GP();
126
    return ch;
127
}
128
 
129
void
130
cyg_hal_plf_sci_putc(void* __ch_data, cyg_uint8 c)
131
{
132
    cyg_uint8* base = ((channel_data_t*)__ch_data)->base;
133
    cyg_uint8 sr;
134
    CYGARC_HAL_SAVE_GP();
135
 
136
    do {
137
        HAL_READ_UINT8(base+_REG_SCSSR, sr);
138
    } while ((sr & CYGARC_REG_SCI_SCSSR_TDRE) == 0);
139
 
140
    HAL_WRITE_UINT8(base+_REG_SCTDR, c);
141
 
142
    // Clear empty flag.
143
    HAL_WRITE_UINT8(base+_REG_SCSSR, sr & ~CYGARC_REG_SCI_SCSSR_TDRE);
144
 
145
    // Hang around until the character has been safely sent.
146
    do {
147
        HAL_READ_UINT8(base+_REG_SCSSR, sr);
148
    } while ((sr & CYGARC_REG_SCI_SCSSR_TDRE) == 0);
149
 
150
    CYGARC_HAL_RESTORE_GP();
151
}
152
 
153
 
154
static channel_data_t channels[CYGNUM_HAL_SH_SH2_SCI_PORTS];
155
 
156
static void
157
cyg_hal_plf_sci_write(void* __ch_data, const cyg_uint8* __buf,
158
                         cyg_uint32 __len)
159
{
160
    CYGARC_HAL_SAVE_GP();
161
 
162
    while(__len-- > 0)
163
        cyg_hal_plf_sci_putc(__ch_data, *__buf++);
164
 
165
    CYGARC_HAL_RESTORE_GP();
166
}
167
 
168
static void
169
cyg_hal_plf_sci_read(void* __ch_data, cyg_uint8* __buf, cyg_uint32 __len)
170
{
171
    CYGARC_HAL_SAVE_GP();
172
 
173
    while(__len-- > 0)
174
        *__buf++ = cyg_hal_plf_sci_getc(__ch_data);
175
 
176
    CYGARC_HAL_RESTORE_GP();
177
}
178
 
179
cyg_bool
180
cyg_hal_plf_sci_getc_timeout(void* __ch_data, cyg_uint8* ch)
181
{
182
    channel_data_t* chan = (channel_data_t*)__ch_data;
183
    int delay_count;
184
    cyg_bool res;
185
    CYGARC_HAL_SAVE_GP();
186
 
187
    delay_count = chan->msec_timeout * 10; // delay in .1 ms steps
188
 
189
    for(;;) {
190
        res = cyg_hal_plf_sci_getc_nonblock(__ch_data, ch);
191
        if (res || 0 == delay_count--)
192
            break;
193
 
194
        CYGACC_CALL_IF_DELAY_US(100);
195
    }
196
 
197
    CYGARC_HAL_RESTORE_GP();
198
    return res;
199
}
200
 
201
static int
202
cyg_hal_plf_sci_control(void *__ch_data, __comm_control_cmd_t __func, ...)
203
{
204
    static int irq_state = 0;
205
    channel_data_t* chan = (channel_data_t*)__ch_data;
206
    cyg_uint8 scr;
207
    int ret = 0;
208
    CYGARC_HAL_SAVE_GP();
209
 
210
    switch (__func) {
211
    case __COMMCTL_IRQ_ENABLE:
212
        irq_state = 1;
213
        HAL_INTERRUPT_UNMASK(chan->isr_vector);
214
        HAL_READ_UINT8(chan->base+_REG_SCSCR, scr);
215
        scr |= CYGARC_REG_SCI_SCSCR_RIE;
216
        HAL_WRITE_UINT8(chan->base+_REG_SCSCR, scr);
217
        break;
218
    case __COMMCTL_IRQ_DISABLE:
219
        ret = irq_state;
220
        irq_state = 0;
221
        HAL_INTERRUPT_UNMASK(chan->isr_vector);
222
        HAL_READ_UINT8(chan->base+_REG_SCSCR, scr);
223
        scr &= ~CYGARC_REG_SCI_SCSCR_RIE;
224
        HAL_WRITE_UINT8(chan->base+_REG_SCSCR, scr);
225
        break;
226
    case __COMMCTL_DBG_ISR_VECTOR:
227
        ret = chan->isr_vector;
228
        break;
229
    case __COMMCTL_SET_TIMEOUT:
230
    {
231
        va_list ap;
232
 
233
        va_start(ap, __func);
234
 
235
        ret = chan->msec_timeout;
236
        chan->msec_timeout = va_arg(ap, cyg_uint32);
237
 
238
        va_end(ap);
239
    }
240
    default:
241
        break;
242
    }
243
    CYGARC_HAL_RESTORE_GP();
244
    return ret;
245
}
246
 
247
static int
248
cyg_hal_plf_sci_isr(void *__ch_data, int* __ctrlc,
249
                    CYG_ADDRWORD __vector, CYG_ADDRWORD __data)
250
{
251
    cyg_uint8 c, sr;
252
    cyg_uint8* base = ((channel_data_t*)__ch_data)->base;
253
    int res = 0;
254
    CYGARC_HAL_SAVE_GP();
255
 
256
    *__ctrlc = 0;
257
    HAL_READ_UINT8(base+_REG_SCSSR, sr);
258
    if (sr & CYGARC_REG_SCI_SCSSR_ORER) {
259
        // Serial RX overrun. Clear error and hope protocol recovers.
260
        HAL_WRITE_UINT8(base+_REG_SCSSR,
261
                        CYGARC_REG_SCI_SCSSR_CLEARMASK & ~CYGARC_REG_SCI_SCSSR_ORER);
262
        res = CYG_ISR_HANDLED;
263
    } else if (sr & CYGARC_REG_SCI_SCSSR_RDRF) {
264
        // Received character
265
        HAL_READ_UINT8(base+_REG_SCRDR, c);
266
 
267
        // Clear buffer full flag.
268
        HAL_WRITE_UINT8(base+_REG_SCSSR,
269
                        CYGARC_REG_SCI_SCSSR_CLEARMASK & ~CYGARC_REG_SCI_SCSSR_RDRF);
270
 
271
        if( cyg_hal_is_break( &c , 1 ) )
272
            *__ctrlc = 1;
273
 
274
        res = CYG_ISR_HANDLED;
275
    }
276
 
277
    CYGARC_HAL_RESTORE_GP();
278
    return res;
279
}
280
 
281
void
282
cyg_hal_plf_sci_init(int sci_index, int comm_index,
283
                     int rcv_vect, cyg_uint8* base)
284
{
285
    channel_data_t* chan = &channels[sci_index];
286
    hal_virtual_comm_table_t* comm;
287
    int cur = CYGACC_CALL_IF_SET_CONSOLE_COMM(CYGNUM_CALL_IF_SET_COMM_ID_QUERY_CURRENT);
288
 
289
    // Initialize channel table
290
    chan->base = base;
291
    chan->isr_vector = rcv_vect;
292
    chan->msec_timeout = 1000;
293
 
294
    // Disable interrupts.
295
    HAL_INTERRUPT_MASK(chan->isr_vector);
296
 
297
    // Init channel
298
 
299
    cyg_hal_plf_sci_init_channel(chan);
300
 
301
    // Setup procs in the vector table
302
 
303
    // Initialize channel procs
304
    CYGACC_CALL_IF_SET_CONSOLE_COMM(comm_index);
305
    comm = CYGACC_CALL_IF_CONSOLE_PROCS();
306
    CYGACC_COMM_IF_CH_DATA_SET(*comm, chan);
307
    CYGACC_COMM_IF_WRITE_SET(*comm, cyg_hal_plf_sci_write);
308
    CYGACC_COMM_IF_READ_SET(*comm, cyg_hal_plf_sci_read);
309
    CYGACC_COMM_IF_PUTC_SET(*comm, cyg_hal_plf_sci_putc);
310
    CYGACC_COMM_IF_GETC_SET(*comm, cyg_hal_plf_sci_getc);
311
    CYGACC_COMM_IF_CONTROL_SET(*comm, cyg_hal_plf_sci_control);
312
    CYGACC_COMM_IF_DBG_ISR_SET(*comm, cyg_hal_plf_sci_isr);
313
    CYGACC_COMM_IF_GETC_TIMEOUT_SET(*comm, cyg_hal_plf_sci_getc_timeout);
314
 
315
    // Restore original console
316
    CYGACC_CALL_IF_SET_CONSOLE_COMM(cur);
317
}
318
 
319
#endif // CYGNUM_HAL_SH_SH2_SCI_PORTS
320
 
321
//-----------------------------------------------------------------------------
322
// end of sh_sci.c

powered by: WebSVN 2.1.0

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