1 |
27 |
unneback |
//=============================================================================
|
2 |
|
|
//
|
3 |
|
|
// hal_diag.c
|
4 |
|
|
//
|
5 |
|
|
// Simple polling driver for the 16c550c serial controller(s) in the ORP,
|
6 |
|
|
// to be used for diagnostic I/O and gdb remote debugging.
|
7 |
|
|
//
|
8 |
|
|
//=============================================================================
|
9 |
|
|
//####ECOSGPLCOPYRIGHTBEGIN####
|
10 |
|
|
// -------------------------------------------
|
11 |
|
|
// This file is part of eCos, the Embedded Configurable Operating System.
|
12 |
|
|
// Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
|
13 |
|
|
//
|
14 |
|
|
// eCos is free software; you can redistribute it and/or modify it under
|
15 |
|
|
// the terms of the GNU General Public License as published by the Free
|
16 |
|
|
// Software Foundation; either version 2 or (at your option) any later version.
|
17 |
|
|
//
|
18 |
|
|
// eCos is distributed in the hope that it will be useful, but WITHOUT ANY
|
19 |
|
|
// 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 along
|
24 |
|
|
// with eCos; if not, write to the Free Software Foundation, Inc.,
|
25 |
|
|
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
|
26 |
|
|
//
|
27 |
|
|
// As a special exception, if other files instantiate templates or use macros
|
28 |
|
|
// or inline functions from this file, or you compile this file and link it
|
29 |
|
|
// with other works to produce a work based on this file, this file does not
|
30 |
|
|
// by itself cause the resulting work to be covered by the GNU General Public
|
31 |
|
|
// License. However the source code for this file must still be made available
|
32 |
|
|
// in accordance with section (3) of the GNU General Public License.
|
33 |
|
|
//
|
34 |
|
|
// This exception does not invalidate any other reasons why a work based on
|
35 |
|
|
// this file might be covered by the GNU General Public License.
|
36 |
|
|
//
|
37 |
|
|
// Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
|
38 |
|
|
// at http://sources.redhat.com/ecos/ecos-license/
|
39 |
|
|
// -------------------------------------------
|
40 |
|
|
//####ECOSGPLCOPYRIGHTEND####
|
41 |
|
|
//=============================================================================
|
42 |
|
|
//#####DESCRIPTIONBEGIN####
|
43 |
|
|
//
|
44 |
|
|
// Author(s): sfurman
|
45 |
|
|
// Contributors:dmoseley
|
46 |
|
|
// Date: 2003-02-28
|
47 |
|
|
// Description: Simple polling driver for the 16c550c serial controller(s) in the ORP,
|
48 |
|
|
// to be used for diagnostic I/O and gdb remote debugging.
|
49 |
|
|
//
|
50 |
|
|
//
|
51 |
|
|
//####DESCRIPTIONEND####
|
52 |
|
|
//
|
53 |
|
|
//=============================================================================
|
54 |
|
|
|
55 |
|
|
#include <pkgconf/hal.h>
|
56 |
|
|
#include <pkgconf/system.h>
|
57 |
|
|
#include CYGBLD_HAL_PLATFORM_H
|
58 |
|
|
|
59 |
|
|
#include <cyg/hal/hal_arch.h> // SAVE/RESTORE GP macros
|
60 |
|
|
#include <cyg/hal/hal_io.h> // IO macros
|
61 |
|
|
#include <cyg/hal/hal_if.h> // interface API
|
62 |
|
|
#include <cyg/hal/hal_intr.h> // HAL_ENABLE/MASK/UNMASK_INTERRUPTS
|
63 |
|
|
#include <cyg/hal/hal_misc.h> // Helper functions
|
64 |
|
|
#include <cyg/hal/drv_api.h> // CYG_ISR_HANDLED
|
65 |
|
|
#include <cyg/infra/cyg_ass.h> // assertion macros
|
66 |
|
|
|
67 |
|
|
//-----------------------------------------------------------------------------
|
68 |
|
|
// Base addresses for each 16550 UART in the system
|
69 |
|
|
#define SERIAL_16550_CONSOLE_BASE_ADDR 0x90000000
|
70 |
|
|
#define SERIAL_16550_DEBUGGER_BASE_ADDR 0x90000008
|
71 |
|
|
|
72 |
|
|
//-----------------------------------------------------------------------------
|
73 |
|
|
// Define the 16550C serial registers.
|
74 |
|
|
#define SER_16550_RBR 0x00 // receiver buffer register, read, dlab = 0
|
75 |
|
|
#define SER_16550_THR 0x00 // transmitter holding register, write, dlab = 0
|
76 |
|
|
#define SER_16550_DLL 0x00 // divisor latch (LS), read/write, dlab = 1
|
77 |
|
|
#define SER_16550_IER 0x01 // interrupt enable register, read/write, dlab = 0
|
78 |
|
|
#define SER_16550_DLM 0x01 // divisor latch (MS), read/write, dlab = 1
|
79 |
|
|
#define SER_16550_IIR 0x02 // interrupt identification reg, read, dlab = 0
|
80 |
|
|
#define SER_16550_FCR 0x02 // fifo control register, write, dlab = 0
|
81 |
|
|
#define SER_16550_AFR 0x02 // alternate function reg, read/write, dlab = 1
|
82 |
|
|
#define SER_16550_LCR 0x03 // line control register, read/write
|
83 |
|
|
#define SER_16550_MCR 0x04 // modem control register, read/write
|
84 |
|
|
#define SER_16550_LSR 0x05 // line status register, read
|
85 |
|
|
#define SER_16550_MSR 0x06 // modem status register, read
|
86 |
|
|
#define SER_16550_SCR 0x07 // scratch pad register
|
87 |
|
|
|
88 |
|
|
// The interrupt enable register bits.
|
89 |
|
|
#define SIO_IER_ERDAI 0x01 // enable received data available irq
|
90 |
|
|
#define SIO_IER_ETHREI 0x02 // enable THR empty interrupt
|
91 |
|
|
#define SIO_IER_ELSI 0x04 // enable receiver line status irq
|
92 |
|
|
#define SIO_IER_EMSI 0x08 // enable modem status interrupt
|
93 |
|
|
|
94 |
|
|
// The interrupt identification register bits.
|
95 |
|
|
#define SIO_IIR_IP 0x01 // 0 if interrupt pending
|
96 |
|
|
#define SIO_IIR_ID_MASK 0x0e // mask for interrupt ID bits
|
97 |
|
|
|
98 |
|
|
// The line status register bits.
|
99 |
|
|
#define SIO_LSR_DR 0x01 // data ready
|
100 |
|
|
#define SIO_LSR_OE 0x02 // overrun error
|
101 |
|
|
#define SIO_LSR_PE 0x04 // parity error
|
102 |
|
|
#define SIO_LSR_FE 0x08 // framing error
|
103 |
|
|
#define SIO_LSR_BI 0x10 // break interrupt
|
104 |
|
|
#define SIO_LSR_THRE 0x20 // transmitter holding register empty
|
105 |
|
|
#define SIO_LSR_TEMT 0x40 // transmitter register empty
|
106 |
|
|
#define SIO_LSR_ERR 0x80 // any error condition
|
107 |
|
|
|
108 |
|
|
// The modem status register bits.
|
109 |
|
|
#define SIO_MSR_DCTS 0x01 // delta clear to send
|
110 |
|
|
#define SIO_MSR_DDSR 0x02 // delta data set ready
|
111 |
|
|
#define SIO_MSR_TERI 0x04 // trailing edge ring indicator
|
112 |
|
|
#define SIO_MSR_DDCD 0x08 // delta data carrier detect
|
113 |
|
|
#define SIO_MSR_CTS 0x10 // clear to send
|
114 |
|
|
#define SIO_MSR_DSR 0x20 // data set ready
|
115 |
|
|
#define SIO_MSR_RI 0x40 // ring indicator
|
116 |
|
|
#define SIO_MSR_DCD 0x80 // data carrier detect
|
117 |
|
|
|
118 |
|
|
// The line control register bits.
|
119 |
|
|
#define SIO_LCR_WLS0 0x01 // word length select bit 0
|
120 |
|
|
#define SIO_LCR_WLS1 0x02 // word length select bit 1
|
121 |
|
|
#define SIO_LCR_STB 0x04 // number of stop bits
|
122 |
|
|
#define SIO_LCR_PEN 0x08 // parity enable
|
123 |
|
|
#define SIO_LCR_EPS 0x10 // even parity select
|
124 |
|
|
#define SIO_LCR_SP 0x20 // stick parity
|
125 |
|
|
#define SIO_LCR_SB 0x40 // set break
|
126 |
|
|
#define SIO_LCR_DLAB 0x80 // divisor latch access bit
|
127 |
|
|
|
128 |
|
|
// The FIFO control register
|
129 |
|
|
#define SIO_FCR_FCR0 0x01 // enable xmit and rcvr fifos
|
130 |
|
|
#define SIO_FCR_FCR1 0x02 // clear RCVR FIFO
|
131 |
|
|
#define SIO_FCR_FCR2 0x04 // clear XMIT FIFO
|
132 |
|
|
|
133 |
|
|
/////////////////////////////////////////
|
134 |
|
|
// Interrupt Enable Register
|
135 |
|
|
#define IER_RCV 0x01
|
136 |
|
|
#define IER_XMT 0x02
|
137 |
|
|
#define IER_LS 0x04
|
138 |
|
|
#define IER_MS 0x08
|
139 |
|
|
|
140 |
|
|
// Line Control Register
|
141 |
|
|
#define LCR_WL5 0x00 // Word length
|
142 |
|
|
#define LCR_WL6 0x01
|
143 |
|
|
#define LCR_WL7 0x02
|
144 |
|
|
#define LCR_WL8 0x03
|
145 |
|
|
#define LCR_SB1 0x00 // Number of stop bits
|
146 |
|
|
#define LCR_SB1_5 0x04 // 1.5 -> only valid with 5 bit words
|
147 |
|
|
#define LCR_SB2 0x04
|
148 |
|
|
#define LCR_PN 0x00 // Parity mode - none
|
149 |
|
|
#define LCR_PE 0x0C // Parity mode - even
|
150 |
|
|
#define LCR_PO 0x08 // Parity mode - odd
|
151 |
|
|
#define LCR_PM 0x28 // Forced "mark" parity
|
152 |
|
|
#define LCR_PS 0x38 // Forced "space" parity
|
153 |
|
|
#define LCR_DL 0x80 // Enable baud rate latch
|
154 |
|
|
|
155 |
|
|
// Line Status Register
|
156 |
|
|
#define LSR_RSR 0x01
|
157 |
|
|
#define LSR_THE 0x20
|
158 |
|
|
|
159 |
|
|
// Modem Control Register
|
160 |
|
|
#define MCR_DTR 0x01
|
161 |
|
|
#define MCR_RTS 0x02
|
162 |
|
|
#define MCR_INT 0x08 // Enable interrupts
|
163 |
|
|
|
164 |
|
|
// Interrupt status register
|
165 |
|
|
#define ISR_None 0x01
|
166 |
|
|
#define ISR_Rx_Line_Status 0x06
|
167 |
|
|
#define ISR_Rx_Avail 0x04
|
168 |
|
|
#define ISR_Rx_Char_Timeout 0x0C
|
169 |
|
|
#define ISR_Tx_Empty 0x02
|
170 |
|
|
#define ISR_Modem_Status 0x00
|
171 |
|
|
|
172 |
|
|
// FIFO control register
|
173 |
|
|
#define FCR_ENABLE 0x01
|
174 |
|
|
#define FCR_CLEAR_RCVR 0x02
|
175 |
|
|
#define FCR_CLEAR_XMIT 0x04
|
176 |
|
|
|
177 |
|
|
// Assume the UART is driven 1/16 CPU frequency
|
178 |
|
|
#define UART_CLOCK ((CYGHWR_HAL_OPENRISC_CPU_FREQ)*1.0e6/16.0)
|
179 |
|
|
|
180 |
|
|
#define DIVISOR(baud) ((int)((UART_CLOCK)/baud))
|
181 |
|
|
|
182 |
|
|
#ifdef CYGNUM_HAL_VIRTUAL_VECTOR_CONSOLE_CHANNEL_BAUD
|
183 |
|
|
#define CYG_DEV_SERIAL_BAUD_DIVISOR \
|
184 |
|
|
DIVISOR(CYGNUM_HAL_VIRTUAL_VECTOR_CONSOLE_CHANNEL_BAUD)
|
185 |
|
|
#else
|
186 |
|
|
#error Missing/incorrect serial baud rate defined - CDL error?
|
187 |
|
|
#endif
|
188 |
|
|
|
189 |
|
|
|
190 |
|
|
//-----------------------------------------------------------------------------
|
191 |
|
|
typedef struct {
|
192 |
|
|
cyg_uint8* base;
|
193 |
|
|
cyg_int32 msec_timeout;
|
194 |
|
|
int isr_vector;
|
195 |
|
|
} channel_data_t;
|
196 |
|
|
|
197 |
|
|
static channel_data_t channels[] = {
|
198 |
|
|
{ (cyg_uint8*)SERIAL_16550_CONSOLE_BASE_ADDR,
|
199 |
|
|
1000,
|
200 |
|
|
CYGNUM_HAL_INTERRUPT_SERIAL_CONSOLE
|
201 |
|
|
},
|
202 |
|
|
{ (cyg_uint8*)SERIAL_16550_DEBUGGER_BASE_ADDR,
|
203 |
|
|
1000,
|
204 |
|
|
CYGNUM_HAL_INTERRUPT_SERIAL_DEBUGGER
|
205 |
|
|
}
|
206 |
|
|
};
|
207 |
|
|
|
208 |
|
|
//-----------------------------------------------------------------------------
|
209 |
|
|
// Set the baud rate
|
210 |
|
|
|
211 |
|
|
static void
|
212 |
|
|
cyg_hal_plf_serial_set_baud(cyg_uint8* port, cyg_uint16 baud_divisor)
|
213 |
|
|
{
|
214 |
|
|
cyg_uint8 _lcr;
|
215 |
|
|
|
216 |
|
|
HAL_READ_UINT8(port+SER_16550_LCR, _lcr);
|
217 |
|
|
_lcr |= LCR_DL;
|
218 |
|
|
HAL_WRITE_UINT8(port+SER_16550_LCR, _lcr);
|
219 |
|
|
|
220 |
|
|
HAL_WRITE_UINT8(port+SER_16550_DLM, baud_divisor >> 8);
|
221 |
|
|
HAL_WRITE_UINT8(port+SER_16550_DLL, baud_divisor & 0xff);
|
222 |
|
|
|
223 |
|
|
_lcr &= ~LCR_DL;
|
224 |
|
|
HAL_WRITE_UINT8(port+SER_16550_LCR, _lcr);
|
225 |
|
|
}
|
226 |
|
|
|
227 |
|
|
//-----------------------------------------------------------------------------
|
228 |
|
|
// The minimal init, get and put functions. All by polling.
|
229 |
|
|
|
230 |
|
|
void
|
231 |
|
|
cyg_hal_plf_serial_init_channel(void* __ch_data)
|
232 |
|
|
{
|
233 |
|
|
cyg_uint8* port;
|
234 |
|
|
cyg_uint8 _lcr;
|
235 |
|
|
|
236 |
|
|
// Some of the diagnostic print code calls through here with no idea what the ch_data is.
|
237 |
|
|
// Go ahead and assume it is channels[0].
|
238 |
|
|
if (__ch_data == 0)
|
239 |
|
|
__ch_data = (void*)&channels[0];
|
240 |
|
|
|
241 |
|
|
port = ((channel_data_t*)__ch_data)->base;
|
242 |
|
|
|
243 |
|
|
// Disable port interrupts while changing hardware
|
244 |
|
|
HAL_WRITE_UINT8(port+SER_16550_IER, 0);
|
245 |
|
|
|
246 |
|
|
// Set databits, stopbits and parity.
|
247 |
|
|
_lcr = LCR_WL8 | LCR_SB1 | LCR_PN;
|
248 |
|
|
HAL_WRITE_UINT8(port+SER_16550_LCR, _lcr);
|
249 |
|
|
|
250 |
|
|
// Set baud rate.
|
251 |
|
|
cyg_hal_plf_serial_set_baud(port, CYG_DEV_SERIAL_BAUD_DIVISOR);
|
252 |
|
|
|
253 |
|
|
// Enable and clear FIFO
|
254 |
|
|
HAL_WRITE_UINT8(port+SER_16550_FCR, (FCR_ENABLE | FCR_CLEAR_RCVR | FCR_CLEAR_XMIT));
|
255 |
|
|
|
256 |
|
|
// enable RTS to keep host side happy
|
257 |
|
|
HAL_WRITE_UINT8( port+SER_16550_MCR, MCR_RTS );
|
258 |
|
|
|
259 |
|
|
// Don't allow interrupts.
|
260 |
|
|
HAL_WRITE_UINT8(port+SER_16550_IER, 0);
|
261 |
|
|
}
|
262 |
|
|
|
263 |
|
|
void
|
264 |
|
|
cyg_hal_plf_serial_putc(void* __ch_data, cyg_uint8 __ch)
|
265 |
|
|
{
|
266 |
|
|
cyg_uint8* port;
|
267 |
|
|
cyg_uint8 _lsr;
|
268 |
|
|
|
269 |
|
|
// Some of the diagnostic print code calls through here with no idea what the ch_data is.
|
270 |
|
|
// Go ahead and assume it is channels[0].
|
271 |
|
|
if (__ch_data == 0)
|
272 |
|
|
__ch_data = (void*)&channels[0];
|
273 |
|
|
|
274 |
|
|
port = ((channel_data_t*)__ch_data)->base;
|
275 |
|
|
|
276 |
|
|
CYGARC_HAL_SAVE_GP();
|
277 |
|
|
|
278 |
|
|
do {
|
279 |
|
|
HAL_READ_UINT8(port+SER_16550_LSR, _lsr);
|
280 |
|
|
} while ((_lsr & SIO_LSR_THRE) == 0);
|
281 |
|
|
|
282 |
|
|
// Now, the transmit buffer is empty
|
283 |
|
|
HAL_WRITE_UINT8(port+SER_16550_THR, __ch);
|
284 |
|
|
|
285 |
|
|
// Hang around until the character has been safely sent.
|
286 |
|
|
do {
|
287 |
|
|
HAL_READ_UINT8(port+SER_16550_LSR, _lsr);
|
288 |
|
|
} while ((_lsr & SIO_LSR_THRE) == 0);
|
289 |
|
|
|
290 |
|
|
CYGARC_HAL_RESTORE_GP();
|
291 |
|
|
}
|
292 |
|
|
|
293 |
|
|
static int lsr_global;
|
294 |
|
|
|
295 |
|
|
static cyg_bool
|
296 |
|
|
cyg_hal_plf_serial_getc_nonblock(void* __ch_data, cyg_uint8* ch)
|
297 |
|
|
{
|
298 |
|
|
cyg_uint8* port;
|
299 |
|
|
cyg_uint8 _lsr;
|
300 |
|
|
|
301 |
|
|
// Some of the diagnostic print code calls through here with no idea what the ch_data is.
|
302 |
|
|
// Go ahead and assume it is channels[0].
|
303 |
|
|
if (__ch_data == 0)
|
304 |
|
|
__ch_data = (void*)&channels[0];
|
305 |
|
|
|
306 |
|
|
port = ((channel_data_t*)__ch_data)->base;
|
307 |
|
|
|
308 |
|
|
HAL_READ_UINT8(port+SER_16550_LSR, _lsr);
|
309 |
|
|
if ((_lsr & SIO_LSR_DR) == 0)
|
310 |
|
|
return false;
|
311 |
|
|
lsr_global = _lsr;
|
312 |
|
|
CYG_ASSERT((_lsr & SIO_LSR_OE) == 0 , "UART receiver overrun error");
|
313 |
|
|
HAL_READ_UINT8(port+SER_16550_RBR, *ch);
|
314 |
|
|
|
315 |
|
|
return true;
|
316 |
|
|
}
|
317 |
|
|
|
318 |
|
|
cyg_uint8
|
319 |
|
|
cyg_hal_plf_serial_getc(void* __ch_data)
|
320 |
|
|
{
|
321 |
|
|
cyg_uint8 ch;
|
322 |
|
|
CYGARC_HAL_SAVE_GP();
|
323 |
|
|
|
324 |
|
|
// Some of the diagnostic print code calls through here with no idea what the ch_data is.
|
325 |
|
|
// Go ahead and assume it is channels[0].
|
326 |
|
|
if (__ch_data == 0)
|
327 |
|
|
__ch_data = (void*)&channels[0];
|
328 |
|
|
|
329 |
|
|
while(!cyg_hal_plf_serial_getc_nonblock(__ch_data, &ch));
|
330 |
|
|
|
331 |
|
|
CYGARC_HAL_RESTORE_GP();
|
332 |
|
|
return ch;
|
333 |
|
|
}
|
334 |
|
|
|
335 |
|
|
static void
|
336 |
|
|
cyg_hal_plf_serial_write(void* __ch_data, const cyg_uint8* __buf,
|
337 |
|
|
cyg_uint32 __len)
|
338 |
|
|
{
|
339 |
|
|
CYGARC_HAL_SAVE_GP();
|
340 |
|
|
|
341 |
|
|
// Some of the diagnostic print code calls through here with no idea what the ch_data is.
|
342 |
|
|
// Go ahead and assume it is channels[0].
|
343 |
|
|
if (__ch_data == 0)
|
344 |
|
|
__ch_data = (void*)&channels[0];
|
345 |
|
|
|
346 |
|
|
while(__len-- > 0)
|
347 |
|
|
cyg_hal_plf_serial_putc(__ch_data, *__buf++);
|
348 |
|
|
|
349 |
|
|
CYGARC_HAL_RESTORE_GP();
|
350 |
|
|
}
|
351 |
|
|
|
352 |
|
|
static void
|
353 |
|
|
cyg_hal_plf_serial_read(void* __ch_data, cyg_uint8* __buf, cyg_uint32 __len)
|
354 |
|
|
{
|
355 |
|
|
CYGARC_HAL_SAVE_GP();
|
356 |
|
|
|
357 |
|
|
// Some of the diagnostic print code calls through here with no idea what the ch_data is.
|
358 |
|
|
// Go ahead and assume it is channels[0].
|
359 |
|
|
if (__ch_data == 0)
|
360 |
|
|
__ch_data = (void*)&channels[0];
|
361 |
|
|
|
362 |
|
|
while(__len-- > 0)
|
363 |
|
|
*__buf++ = cyg_hal_plf_serial_getc(__ch_data);
|
364 |
|
|
|
365 |
|
|
CYGARC_HAL_RESTORE_GP();
|
366 |
|
|
}
|
367 |
|
|
|
368 |
|
|
|
369 |
|
|
cyg_bool
|
370 |
|
|
cyg_hal_plf_serial_getc_timeout(void* __ch_data, cyg_uint8* ch)
|
371 |
|
|
{
|
372 |
|
|
int delay_count;
|
373 |
|
|
channel_data_t* chan;
|
374 |
|
|
cyg_bool res;
|
375 |
|
|
CYGARC_HAL_SAVE_GP();
|
376 |
|
|
|
377 |
|
|
// Some of the diagnostic print code calls through here with no idea what the ch_data is.
|
378 |
|
|
// Go ahead and assume it is channels[0].
|
379 |
|
|
if (__ch_data == 0)
|
380 |
|
|
__ch_data = (void*)&channels[0];
|
381 |
|
|
|
382 |
|
|
chan = (channel_data_t*)__ch_data;
|
383 |
|
|
|
384 |
|
|
delay_count = chan->msec_timeout; // delay in 1000 us steps
|
385 |
|
|
|
386 |
|
|
for(;;) {
|
387 |
|
|
res = cyg_hal_plf_serial_getc_nonblock(__ch_data, ch);
|
388 |
|
|
if (res || 0 == delay_count--)
|
389 |
|
|
break;
|
390 |
|
|
CYGACC_CALL_IF_DELAY_US(1000);
|
391 |
|
|
}
|
392 |
|
|
|
393 |
|
|
CYGARC_HAL_RESTORE_GP();
|
394 |
|
|
return res;
|
395 |
|
|
}
|
396 |
|
|
|
397 |
|
|
static int
|
398 |
|
|
cyg_hal_plf_serial_control(void *__ch_data, __comm_control_cmd_t __func, ...)
|
399 |
|
|
{
|
400 |
|
|
static int irq_state = 0;
|
401 |
|
|
channel_data_t* chan;
|
402 |
|
|
cyg_uint8 ier;
|
403 |
|
|
int ret = 0;
|
404 |
|
|
CYGARC_HAL_SAVE_GP();
|
405 |
|
|
|
406 |
|
|
// Some of the diagnostic print code calls through here with no idea what the ch_data is.
|
407 |
|
|
// Go ahead and assume it is channels[0].
|
408 |
|
|
if (__ch_data == 0)
|
409 |
|
|
__ch_data = (void*)&channels[0];
|
410 |
|
|
|
411 |
|
|
chan = (channel_data_t*)__ch_data;
|
412 |
|
|
|
413 |
|
|
switch (__func) {
|
414 |
|
|
case __COMMCTL_IRQ_ENABLE:
|
415 |
|
|
irq_state = 1;
|
416 |
|
|
|
417 |
|
|
HAL_READ_UINT8(chan->base + SER_16550_IER, ier);
|
418 |
|
|
ier |= SIO_IER_ERDAI;
|
419 |
|
|
HAL_WRITE_UINT8(chan->base + SER_16550_IER, ier);
|
420 |
|
|
|
421 |
|
|
HAL_INTERRUPT_SET_LEVEL(chan->isr_vector, 1);
|
422 |
|
|
HAL_INTERRUPT_UNMASK(chan->isr_vector);
|
423 |
|
|
break;
|
424 |
|
|
case __COMMCTL_IRQ_DISABLE:
|
425 |
|
|
ret = irq_state;
|
426 |
|
|
irq_state = 0;
|
427 |
|
|
|
428 |
|
|
HAL_READ_UINT8(chan->base + SER_16550_IER, ier);
|
429 |
|
|
ier &= ~SIO_IER_ERDAI;
|
430 |
|
|
HAL_WRITE_UINT8(chan->base + SER_16550_IER, ier);
|
431 |
|
|
|
432 |
|
|
HAL_INTERRUPT_MASK(chan->isr_vector);
|
433 |
|
|
break;
|
434 |
|
|
case __COMMCTL_DBG_ISR_VECTOR:
|
435 |
|
|
ret = chan->isr_vector;
|
436 |
|
|
break;
|
437 |
|
|
case __COMMCTL_SET_TIMEOUT:
|
438 |
|
|
{
|
439 |
|
|
va_list ap;
|
440 |
|
|
|
441 |
|
|
va_start(ap, __func);
|
442 |
|
|
|
443 |
|
|
ret = chan->msec_timeout;
|
444 |
|
|
chan->msec_timeout = va_arg(ap, cyg_uint32);
|
445 |
|
|
|
446 |
|
|
va_end(ap);
|
447 |
|
|
}
|
448 |
|
|
break;
|
449 |
|
|
case __COMMCTL_SETBAUD:
|
450 |
|
|
{
|
451 |
|
|
cyg_uint32 baud_rate;
|
452 |
|
|
cyg_uint16 baud_divisor;
|
453 |
|
|
cyg_uint8* port = chan->base;
|
454 |
|
|
va_list ap;
|
455 |
|
|
|
456 |
|
|
va_start(ap, __func);
|
457 |
|
|
baud_rate = va_arg(ap, cyg_uint32);
|
458 |
|
|
va_end(ap);
|
459 |
|
|
|
460 |
|
|
switch (baud_rate)
|
461 |
|
|
{
|
462 |
|
|
case 110: baud_divisor = DIVISOR(110); break;
|
463 |
|
|
case 150: baud_divisor = DIVISOR(150); break;
|
464 |
|
|
case 300: baud_divisor = DIVISOR(300); break;
|
465 |
|
|
case 600: baud_divisor = DIVISOR(600); break;
|
466 |
|
|
case 1200: baud_divisor = DIVISOR(1200); break;
|
467 |
|
|
case 2400: baud_divisor = DIVISOR(2400); break;
|
468 |
|
|
case 4800: baud_divisor = DIVISOR(4800); break;
|
469 |
|
|
case 7200: baud_divisor = DIVISOR(7200); break;
|
470 |
|
|
case 9600: baud_divisor = DIVISOR(9600); break;
|
471 |
|
|
case 14400: baud_divisor = DIVISOR(14400); break;
|
472 |
|
|
case 19200: baud_divisor = DIVISOR(19200); break;
|
473 |
|
|
case 38400: baud_divisor = DIVISOR(38400); break;
|
474 |
|
|
case 57600: baud_divisor = DIVISOR(57600); break;
|
475 |
|
|
case 115200: baud_divisor = DIVISOR(115200); break;
|
476 |
|
|
case 230400: baud_divisor = DIVISOR(230400); break;
|
477 |
|
|
default: return -1; break; // Invalid baud rate selected
|
478 |
|
|
}
|
479 |
|
|
|
480 |
|
|
// Disable port interrupts while changing hardware
|
481 |
|
|
HAL_READ_UINT8(port+SER_16550_IER, ier);
|
482 |
|
|
HAL_WRITE_UINT8(port+SER_16550_IER, 0);
|
483 |
|
|
|
484 |
|
|
// Set baud rate.
|
485 |
|
|
cyg_hal_plf_serial_set_baud(port, baud_divisor);
|
486 |
|
|
|
487 |
|
|
// Reenable interrupts if necessary
|
488 |
|
|
HAL_WRITE_UINT8(port+SER_16550_IER, ier);
|
489 |
|
|
}
|
490 |
|
|
break;
|
491 |
|
|
|
492 |
|
|
case __COMMCTL_GETBAUD:
|
493 |
|
|
break;
|
494 |
|
|
default:
|
495 |
|
|
break;
|
496 |
|
|
}
|
497 |
|
|
CYGARC_HAL_RESTORE_GP();
|
498 |
|
|
return ret;
|
499 |
|
|
}
|
500 |
|
|
|
501 |
|
|
static int
|
502 |
|
|
cyg_hal_plf_serial_isr(void *__ch_data, int* __ctrlc,
|
503 |
|
|
CYG_ADDRWORD __vector, CYG_ADDRWORD __data)
|
504 |
|
|
{
|
505 |
|
|
int res = 0;
|
506 |
|
|
cyg_uint8 _iir, c;
|
507 |
|
|
channel_data_t* chan;
|
508 |
|
|
CYGARC_HAL_SAVE_GP();
|
509 |
|
|
|
510 |
|
|
// Some of the diagnostic print code calls through here with no idea what the ch_data is.
|
511 |
|
|
// Go ahead and assume it is channels[0].
|
512 |
|
|
if (__ch_data == 0)
|
513 |
|
|
__ch_data = (void*)&channels[0];
|
514 |
|
|
|
515 |
|
|
chan = (channel_data_t*)__ch_data;
|
516 |
|
|
|
517 |
|
|
HAL_INTERRUPT_ACKNOWLEDGE(chan->isr_vector);
|
518 |
|
|
|
519 |
|
|
HAL_READ_UINT8(chan->base + SER_16550_IIR, _iir);
|
520 |
|
|
_iir &= SIO_IIR_ID_MASK;
|
521 |
|
|
|
522 |
|
|
*__ctrlc = 0;
|
523 |
|
|
if ((_iir == ISR_Rx_Avail) || (_iir == ISR_Rx_Char_Timeout)) {
|
524 |
|
|
|
525 |
|
|
HAL_READ_UINT8(chan->base + SER_16550_RBR, c);
|
526 |
|
|
|
527 |
|
|
if( cyg_hal_is_break( &c , 1 ) )
|
528 |
|
|
*__ctrlc = 1;
|
529 |
|
|
|
530 |
|
|
res = CYG_ISR_HANDLED;
|
531 |
|
|
}
|
532 |
|
|
|
533 |
|
|
/* sfurman - Hmmm. Under or1ksim, we sometimes receive interrupts
|
534 |
|
|
when no characters are in the FIFO. I think this is a SW bug
|
535 |
|
|
and not a problem w/ or1ksim, but until the problem is solved,
|
536 |
|
|
we always consume the interrupt */
|
537 |
|
|
res = CYG_ISR_HANDLED;
|
538 |
|
|
|
539 |
|
|
CYGARC_HAL_RESTORE_GP();
|
540 |
|
|
return res;
|
541 |
|
|
}
|
542 |
|
|
|
543 |
|
|
static void
|
544 |
|
|
cyg_hal_plf_serial_init(void)
|
545 |
|
|
{
|
546 |
|
|
int i;
|
547 |
|
|
hal_virtual_comm_table_t* comm;
|
548 |
|
|
int cur = CYGACC_CALL_IF_SET_CONSOLE_COMM(CYGNUM_CALL_IF_SET_COMM_ID_QUERY_CURRENT);
|
549 |
|
|
|
550 |
|
|
//#define NUM_CHANNELS (sizeof(channels)/sizeof(channels[0]))
|
551 |
|
|
#define NUM_CHANNELS CYGNUM_HAL_VIRTUAL_VECTOR_COMM_CHANNELS
|
552 |
|
|
for (i = 0; i < NUM_CHANNELS; i++) {
|
553 |
|
|
|
554 |
|
|
// Disable interrupts.
|
555 |
|
|
HAL_INTERRUPT_MASK(channels[i].isr_vector);
|
556 |
|
|
|
557 |
|
|
// Init channels
|
558 |
|
|
cyg_hal_plf_serial_init_channel((void*)&channels[i]);
|
559 |
|
|
|
560 |
|
|
// Setup procs in the vector table
|
561 |
|
|
|
562 |
|
|
// Set COMM callbacks for channel
|
563 |
|
|
CYGACC_CALL_IF_SET_CONSOLE_COMM(i);
|
564 |
|
|
comm = CYGACC_CALL_IF_CONSOLE_PROCS();
|
565 |
|
|
CYGACC_COMM_IF_CH_DATA_SET(*comm, &channels[i]);
|
566 |
|
|
CYGACC_COMM_IF_WRITE_SET(*comm, cyg_hal_plf_serial_write);
|
567 |
|
|
CYGACC_COMM_IF_READ_SET(*comm, cyg_hal_plf_serial_read);
|
568 |
|
|
CYGACC_COMM_IF_PUTC_SET(*comm, cyg_hal_plf_serial_putc);
|
569 |
|
|
CYGACC_COMM_IF_GETC_SET(*comm, cyg_hal_plf_serial_getc);
|
570 |
|
|
CYGACC_COMM_IF_CONTROL_SET(*comm, cyg_hal_plf_serial_control);
|
571 |
|
|
CYGACC_COMM_IF_DBG_ISR_SET(*comm, cyg_hal_plf_serial_isr);
|
572 |
|
|
CYGACC_COMM_IF_GETC_TIMEOUT_SET(*comm, cyg_hal_plf_serial_getc_timeout);
|
573 |
|
|
}
|
574 |
|
|
|
575 |
|
|
// Restore original console
|
576 |
|
|
CYGACC_CALL_IF_SET_CONSOLE_COMM(cur);
|
577 |
|
|
}
|
578 |
|
|
|
579 |
|
|
void
|
580 |
|
|
cyg_hal_plf_comms_init(void)
|
581 |
|
|
{
|
582 |
|
|
static int initialized = 0;
|
583 |
|
|
|
584 |
|
|
if (initialized)
|
585 |
|
|
return;
|
586 |
|
|
|
587 |
|
|
initialized = 1;
|
588 |
|
|
|
589 |
|
|
cyg_hal_plf_serial_init();
|
590 |
|
|
}
|
591 |
|
|
|
592 |
|
|
//-----------------------------------------------------------------------------
|
593 |
|
|
// end of ser16c550c.c
|
594 |
|
|
|