Subversion Repositories shark

Rev

Rev 716 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
714 giacomo 1
/*
2
 * Project: S.Ha.R.K.
3
 *
4
 * Coordinators:
5
 *   Giorgio Buttazzo    <giorgio@sssup.it>
6
 *   Paolo Gai           <pj@gandalf.sssup.it>
7
 *
8
 * Authors     :
9
 *   Paolo Gai           <pj@gandalf.sssup.it>
10
 *   (see the web pages for full authors list)
11
 *
12
 * ReTiS Lab (Scuola Superiore S.Anna - Pisa - Italy)
13
 *
14
 * http://www.sssup.it
15
 * http://retis.sssup.it
16
 * http://shark.sssup.it
17
 */
18
 
19
/*
20
 * Copyright (C) 2000 Michele Cirinei
21
 *
22
 * This program is free software; you can redistribute it and/or modify
23
 * it under the terms of the GNU General Public License as published by
24
 * the Free Software Foundation; either version 2 of the License, or
25
 * (at your option) any later version.
26
 *
27
 * This program is distributed in the hope that it will be useful,
28
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
30
 * GNU General Public License for more details.
31
 *
32
 * You should have received a copy of the GNU General Public License
33
 * along with this program; if not, write to the Free Software
34
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
35
 */
36
 
37
#include <kernel/kern.h>
38
#include <drivers/scomirq.h>
39
 
40
/****************************
41
 Variables
42
****************************/
43
static unsigned com_irq[] = {COM1_IRQ, COM2_IRQ, COM3_IRQ, COM4_IRQ};
44
 
45
struct SCom_Access_Type SCom_Access;
46
 
47
unsigned SCom_Error;
48
 
49
struct TX_data_t{
50
    int ptr;
51
    int len;
52
    BYTE buf[100];
53
};
54
 
55
static struct TX_data_t SCom_TX_data;
56
 
57
/*****************************
58
* Functions
59
*****************************/
60
 
61
void RBRF_handler(unsigned port);
62
 
63
void THRE_handler(unsigned port);
64
 
65
void LSR_handler(unsigned port);
66
 
67
void com_send_msg(unsigned port, BYTE len, BYTE *m);
68
 
69
void com_irq_sel(int no)
70
{
71
    BYTE b;
72
    unsigned i;
73
 
74
    for(i=0;i<4;i++)
75
      if (com_irq[i] == no) break;
76
 
77
    b = DECODE(com_read(i, IIR));
78
 
79
    switch (b)
80
    {  
81
        case LS_CHANGED:
82
            LSR_handler(i);
83
        break;
84
 
85
        case RX_FULL:
86
            RBRF_handler(i);
87
        break;
88
 
89
        case TX_EMPTY:
90
            THRE_handler(i);
91
        break;
92
    }
93
}      
94
 
95
void com_irq_enable(unsigned port, unsigned irq)
96
{
97
    SYS_FLAGS f;
98
 
99
    f = kern_fsave();
100
 
101
    com_read(port, LSR);
102
    com_read(port, IIR);
103
    com_write(port, IER, com_read(port, IER) | irq); /* Enable  irq */
104
 
105
    kern_frestore(f);
106
 
107
}
108
 
109
void com_irq_disable(unsigned port, unsigned irq)
110
{
111
 
112
    SYS_FLAGS f;
113
 
114
    f = kern_fsave();
115
 
116
    com_read(port, LSR);
117
    com_read(port, IIR);
118
    com_write(port, IER, com_read(port, IER) & ~irq); /* Disable irq */
119
 
120
    kern_frestore(f);
121
 
122
}
123
 
124
void com_end(unsigned port)
125
{
126
    SYS_FLAGS f;
127
 
128
    f = kern_fsave();
129
 
130
    com_write(port, IER, 0);
131
    com_read(port, LSR);
132
    com_read(port, IIR);
133
    com_close(port);
134
 
135
    kern_frestore(f);
136
 
137
}
138
 
139
void com_init(unsigned port, DWORD speed, BYTE parity, BYTE len, BYTE stop)
140
{
141
    SYS_FLAGS f;
142
 
143
    f = kern_fsave();
144
 
145
    com_open(port, speed, parity, len, stop);    
146
    SCom_Error = 0;
147
    handler_set(com_irq[port], com_irq_sel, NIL, TRUE);
148
    com_irq_disable(port, ALL_IRQ);     /* Disable interrupts */
149
 
150
    kern_frestore(f);
151
 
152
}
153
 
154
void com_set_functions(void (*confirm)(unsigned port, BYTE msg_status),void (*indication)(unsigned port, BYTE data))
155
{
156
    SYS_FLAGS f;
157
 
158
    f = kern_fsave();
159
 
160
    SCom_Access.confirm    = confirm;
161
    SCom_Access.request    = com_send_msg;
162
    SCom_Access.indication = indication;
163
 
164
    kern_frestore(f);
165
 
166
}
167
 
168
void com_send_msg(unsigned port, BYTE len, BYTE *m)
169
{
170
    SYS_FLAGS f;
171
 
172
    f = kern_fsave();
173
 
174
    com_irq_disable(port, ALL_IRQ);     /* Disable interrupts */
175
 
176
    /* Transmit first byte of message  */
177
    SCom_TX_data.ptr = 0;
178
    SCom_TX_data.len = len;
179
    if (len < 100) {
180
      memcpy(SCom_TX_data.buf, m, len);
181
      com_write(port, THR, SCom_TX_data.buf[SCom_TX_data.ptr++]);
182
      com_irq_enable(port, THRE_IRQ);  /* Enable THRE */
183
    }
184
 
185
    kern_frestore(f);
186
 
187
}
188
 
189
void THRE_handler (unsigned port) /* Transmit Data Register Ready for next byte */
190
{
191
    if (SCom_TX_data.ptr < SCom_TX_data.len)
192
        com_write(port, THR, SCom_TX_data.buf[SCom_TX_data.ptr++]);
193
    else
194
    {
195
        com_irq_disable(port, THRE_IRQ);
196
        while((com_read(port,LSR) & bit6) == 0); /* Wait until last byte sent */
197
        SCom_Access.confirm(port, COM_OK);
198
    }
199
}
200
 
201
void RBRF_handler (unsigned port) /* Receive Data Register Full */
202
{
203
    SCom_Access.indication(port, com_read(port, RBR));
204
}
205
 
206
void LSR_handler (unsigned port)    /* break = 8, frame = 4, parity = 2, RBR overrun = 1*/
207
{
208
    SCom_Error = (com_read(port, LSR) & (bit4|bit3|bit2|bit1) ) >> 1;
209
    com_irq_disable(port, ALL_IRQ);  /* Disable all interrupts */
210
    SCom_Access.confirm(port, COM_ERROR);  
211
}
212