]> git.kernelconcepts.de Git - karo-tx-redboot.git/blob - packages/hal/arm/mx27/ads/v2_0/src/board_diag.c
Initial revision
[karo-tx-redboot.git] / packages / hal / arm / mx27 / ads / v2_0 / src / board_diag.c
1 /*=============================================================================
2 //
3 //      board_diag.c
4 //
5 //      HAL diagnostic output code
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
42 #include <pkgconf/hal.h>
43
44 #include <cyg/infra/cyg_type.h>         // base types
45 #include <cyg/infra/cyg_trac.h>         // tracing macros
46 #include <cyg/infra/cyg_ass.h>          // assertion macros
47
48 #include <cyg/hal/hal_arch.h>           // basic machine info
49 #include <cyg/hal/hal_intr.h>           // interrupt macros
50 #include <cyg/hal/hal_io.h>             // IO macros
51 #include <cyg/hal/hal_diag.h>
52 #include <cyg/hal/hal_if.h>             // Calling-if API
53 #include <cyg/hal/drv_api.h>            // driver API
54 #include <cyg/hal/hal_misc.h>           // Helper functions
55 #include <cyg/hal/hal_soc.h>            // Hardware definitions
56 #include <cyg/hal/fsl_board.h>          // Platform specifics
57
58 static void cyg_hal_plf_duart_init(void);
59 extern void cyg_hal_plf_serial_init(void);
60
61 void cyg_hal_plf_comms_init(void)
62 {
63     static int initialized = 0;
64
65     if (initialized)
66         return;
67
68     initialized = 1;
69     cyg_hal_plf_duart_init();
70     cyg_hal_plf_serial_init();
71 }
72
73 //=============================================================================
74 // ST16552 DUART driver
75 //=============================================================================
76
77 //-----------------------------------------------------------------------------
78 // Only one external UART.
79 #define CYG_DEV_SERIAL_BASE_A    (BOARD_CS_UART_BASE + 0x00)
80
81 //-----------------------------------------------------------------------------
82 // Based on 3.6864 MHz xtal
83 #if CYGNUM_HAL_VIRTUAL_VECTOR_CONSOLE_CHANNEL_BAUD==9600
84 #define CYG_DEV_SERIAL_BAUD_MSB        0x00
85 #define CYG_DEV_SERIAL_BAUD_LSB        0x18
86 #endif
87 #if CYGNUM_HAL_VIRTUAL_VECTOR_CONSOLE_CHANNEL_BAUD==19200
88 #define CYG_DEV_SERIAL_BAUD_MSB        0x00
89 #define CYG_DEV_SERIAL_BAUD_LSB        0x0C
90 #endif
91 #if CYGNUM_HAL_VIRTUAL_VECTOR_CONSOLE_CHANNEL_BAUD==38400
92 #define CYG_DEV_SERIAL_BAUD_MSB        0x00
93 #define CYG_DEV_SERIAL_BAUD_LSB        0x06
94 #endif
95 #if CYGNUM_HAL_VIRTUAL_VECTOR_CONSOLE_CHANNEL_BAUD==57600
96 #define CYG_DEV_SERIAL_BAUD_MSB        0x00
97 #define CYG_DEV_SERIAL_BAUD_LSB        0x04
98 #endif
99 #if CYGNUM_HAL_VIRTUAL_VECTOR_CONSOLE_CHANNEL_BAUD==115200
100 #define CYG_DEV_SERIAL_BAUD_MSB        0x00
101 #define CYG_DEV_SERIAL_BAUD_LSB        0x02
102 #endif
103
104 #ifndef CYG_DEV_SERIAL_BAUD_MSB
105 #error Missing/incorrect serial baud rate defined - CDL error?
106 #endif
107
108 //-----------------------------------------------------------------------------
109 // Define the serial registers. The board is equipped with a 16552
110 // serial chip.
111
112 #if defined (EXT_UART_x16)
113 #define HAL_WRITE_UINT_UART HAL_WRITE_UINT16
114 #define HAL_READ_UINT_UART HAL_READ_UINT16
115 typedef cyg_uint16 uart_width;
116 #elif defined (EXT_UART_x32)
117 #define HAL_WRITE_UINT_UART HAL_WRITE_UINT32
118 #define HAL_READ_UINT_UART HAL_READ_UINT32
119 typedef cyg_uint32 uart_width;
120 #else  //_x8
121 #define HAL_WRITE_UINT_UART HAL_WRITE_UINT8
122 #define HAL_READ_UINT_UART HAL_READ_UINT8
123 typedef cyg_uint8 uart_width;
124 #endif
125
126 #define CYG_DEV_SERIAL_RHR   0x00 // receiver buffer register, read, dlab = 0
127 #define CYG_DEV_SERIAL_THR   0x00 // transmitter holding register, write, dlab = 0
128 #define CYG_DEV_SERIAL_DLL   0x00 // divisor latch (LS), read/write, dlab = 1
129 #define CYG_DEV_SERIAL_IER   0x01 // interrupt enable register, read/write, dlab = 0
130 #define CYG_DEV_SERIAL_DLM   0x01 // divisor latch (MS), read/write, dlab = 1
131 #define CYG_DEV_SERIAL_IIR   0x02 // interrupt identification register, read, dlab = 0
132 #define CYG_DEV_SERIAL_FCR   0x02 // fifo control register, write, dlab = 0
133 #define CYG_DEV_SERIAL_AFR   0x02 // alternate function register, read/write, dlab = 1
134 #define CYG_DEV_SERIAL_LCR   0x03 // line control register, read/write
135 #define CYG_DEV_SERIAL_MCR   0x04
136 #define CYG_DEV_SERIAL_MCR_A 0x04
137 #define CYG_DEV_SERIAL_MCR_B 0x04
138 #define CYG_DEV_SERIAL_LSR   0x05 // line status register, read
139 #define CYG_DEV_SERIAL_MSR   0x06 // modem status register, read
140 #define CYG_DEV_SERIAL_SCR   0x07 // scratch pad register
141
142 // The interrupt enable register bits.
143 #define SIO_IER_ERDAI   0x01            // enable received data available irq
144 #define SIO_IER_ETHREI  0x02            // enable THR empty interrupt
145 #define SIO_IER_ELSI    0x04            // enable receiver line status irq
146 #define SIO_IER_EMSI    0x08            // enable modem status interrupt
147
148 // The interrupt identification register bits.
149 #define SIO_IIR_IP      0x01            // 0 if interrupt pending
150 #define SIO_IIR_ID_MASK 0x0e            // mask for interrupt ID bits
151 #define ISR_Tx  0x02
152 #define ISR_Rx  0x04
153
154 // The line status register bits.
155 #define SIO_LSR_DR      0x01            // data ready
156 #define SIO_LSR_OE      0x02            // overrun error
157 #define SIO_LSR_PE      0x04            // parity error
158 #define SIO_LSR_FE      0x08            // framing error
159 #define SIO_LSR_BI      0x10            // break interrupt
160 #define SIO_LSR_THRE    0x20            // transmitter holding register empty
161 #define SIO_LSR_TEMT    0x40            // transmitter register empty
162 #define SIO_LSR_ERR     0x80            // any error condition
163
164 // The modem status register bits.
165 #define SIO_MSR_DCTS    0x01            // delta clear to send
166 #define SIO_MSR_DDSR    0x02            // delta data set ready
167 #define SIO_MSR_TERI    0x04            // trailing edge ring indicator
168 #define SIO_MSR_DDCD    0x08            // delta data carrier detect
169 #define SIO_MSR_CTS     0x10            // clear to send
170 #define SIO_MSR_DSR     0x20            // data set ready
171 #define SIO_MSR_RI      0x40            // ring indicator
172 #define SIO_MSR_DCD     0x80            // data carrier detect
173
174 // The line control register bits.
175 #define SIO_LCR_WLS0   0x01             // word length select bit 0
176 #define SIO_LCR_WLS1   0x02             // word length select bit 1
177 #define SIO_LCR_STB    0x04             // number of stop bits
178 #define SIO_LCR_PEN    0x08             // parity enable
179 #define SIO_LCR_EPS    0x10             // even parity select
180 #define SIO_LCR_SP     0x20             // stick parity
181 #define SIO_LCR_SB     0x40             // set break
182 #define SIO_LCR_DLAB   0x80             // divisor latch access bit
183
184 // The FIFO control register
185 #define SIO_FCR_FCR0   0x01             // enable xmit and rcvr fifos
186 #define SIO_FCR_FCR1   0x02             // clear RCVR FIFO
187 #define SIO_FCR_FCR2   0x04             // clear XMIT FIFO
188
189 //-----------------------------------------------------------------------------
190
191 typedef struct {
192     uart_width* base;
193     cyg_int32 msec_timeout;
194     int isr_vector;
195 } channel_data_t;
196
197 static channel_data_t channels[] = {
198 #if CYGHWR_HAL_ARM_DUART_UARTA != 0
199     {(uart_width*)CYG_DEV_SERIAL_BASE_A, 1000, 0},
200 #endif
201 #if CYGHWR_HAL_ARM_DUART_UARTB != 0
202     {(uart_width*)CYG_DEV_SERIAL_BASE_B, 1000, 0}
203 #endif
204 };
205
206 //-----------------------------------------------------------------------------
207
208 static void init_duart_channel(channel_data_t* __ch_data)
209 {
210     uart_width* base = __ch_data->base;
211     uart_width lcr;
212
213     // 8-1-no parity.
214     HAL_WRITE_UINT_UART(base+CYG_DEV_SERIAL_LCR,
215                         SIO_LCR_WLS0 | SIO_LCR_WLS1);
216
217     HAL_READ_UINT_UART(base+CYG_DEV_SERIAL_LCR, lcr);
218     lcr |= SIO_LCR_DLAB;
219     HAL_WRITE_UINT_UART(base+CYG_DEV_SERIAL_LCR, lcr);
220     HAL_WRITE_UINT_UART(base+CYG_DEV_SERIAL_DLL, CYG_DEV_SERIAL_BAUD_LSB);
221     HAL_WRITE_UINT_UART(base+CYG_DEV_SERIAL_DLM, CYG_DEV_SERIAL_BAUD_MSB);
222     lcr &= ~SIO_LCR_DLAB;
223     HAL_WRITE_UINT_UART(base+CYG_DEV_SERIAL_LCR, lcr);
224     HAL_WRITE_UINT_UART(base+CYG_DEV_SERIAL_FCR, 0x07);  // Enable & clear FIFO
225 }
226
227 //#define x_debug_uart_log_buf
228 #ifdef x_debug_uart_log_buf
229 #define x_DIAG_BUFSIZE 2048
230 static char __x_log_buf[x_DIAG_BUFSIZE];
231 static int x_diag_bp = 0;
232 #endif
233
234 extern unsigned int g_board_type;
235
236 void cyg_hal_plf_duart_putc(void* __ch_data, cyg_uint8 c)
237 {
238     uart_width* base = ((channel_data_t*)__ch_data)->base;
239     uart_width lsr;
240
241     if (g_board_type == BOARD_TYPE_EVB_A) {
242         return;
243     }
244 #ifdef x_debug_uart_log_buf
245     __x_log_buf[x_diag_bp++] = c;
246 #endif
247     CYGARC_HAL_SAVE_GP();
248
249     do {
250         HAL_READ_UINT_UART(base+CYG_DEV_SERIAL_LSR, lsr);
251     } while ((lsr & SIO_LSR_THRE) == 0);
252
253     HAL_WRITE_UINT_UART(base+CYG_DEV_SERIAL_THR, c);
254
255     // Hang around until the character has been safely sent.
256     do {
257         HAL_READ_UINT_UART(base+CYG_DEV_SERIAL_LSR, lsr);
258     } while ((lsr & SIO_LSR_THRE) == 0);
259
260     CYGARC_HAL_RESTORE_GP();
261 }
262
263 static cyg_bool cyg_hal_plf_duart_getc_nonblock(void* __ch_data, cyg_uint8* ch)
264 {
265     uart_width* base = ((channel_data_t*)__ch_data)->base;
266     uart_width lsr, ch16;
267
268     if (g_board_type == BOARD_TYPE_EVB_A) {
269         return false;
270     }
271
272     HAL_READ_UINT_UART(base+CYG_DEV_SERIAL_LSR, lsr);
273     if ((lsr & SIO_LSR_DR) == 0)
274         return false;
275
276     HAL_READ_UINT_UART(base+CYG_DEV_SERIAL_RHR, ch16);
277
278     *ch = (cyg_uint8) (ch16 & 0x00FF);
279
280     return true;
281 }
282
283 cyg_uint8 cyg_hal_plf_duart_getc(void* __ch_data)
284 {
285     cyg_uint8 ch;
286
287     CYGARC_HAL_SAVE_GP();
288
289     while (!cyg_hal_plf_duart_getc_nonblock(__ch_data, &ch));
290
291     CYGARC_HAL_RESTORE_GP();
292     return ch;
293 }
294
295 static void cyg_hal_plf_duart_write(void* __ch_data, const cyg_uint8* __buf,
296                                     cyg_uint32 __len)
297 {
298     CYGARC_HAL_SAVE_GP();
299
300     while (__len-- > 0)
301         cyg_hal_plf_duart_putc(__ch_data, *__buf++);
302
303     CYGARC_HAL_RESTORE_GP();
304 }
305
306 static void cyg_hal_plf_duart_read(void* __ch_data, cyg_uint8* __buf, 
307                                    cyg_uint32 __len)
308 {
309     CYGARC_HAL_SAVE_GP();
310
311     while (__len-- > 0)
312         *__buf++ = cyg_hal_plf_duart_getc(__ch_data);
313
314     CYGARC_HAL_RESTORE_GP();
315 }
316
317 cyg_bool cyg_hal_plf_duart_getc_timeout(void* __ch_data, cyg_uint8* ch)
318 {
319     int delay_count;
320     channel_data_t* chan = (channel_data_t*)__ch_data;
321     cyg_bool res;
322
323     CYGARC_HAL_SAVE_GP();
324
325     delay_count = chan->msec_timeout * 10; // delay in .1 ms steps
326     for (;;) {
327         res = cyg_hal_plf_duart_getc_nonblock(__ch_data, ch);
328         if (res || 0 == delay_count--)
329             break;
330
331         CYGACC_CALL_IF_DELAY_US(100);
332     }
333
334     CYGARC_HAL_RESTORE_GP();
335     return res;
336 }
337
338 static int cyg_hal_plf_duart_control(void *__ch_data, 
339                                      __comm_control_cmd_t __func, ...)
340 {
341     static int irq_state = 0;
342     channel_data_t* chan = (channel_data_t*)__ch_data;
343     uart_width ier;
344     int ret = 0;
345
346     CYGARC_HAL_SAVE_GP();
347
348     switch (__func) {
349     case __COMMCTL_IRQ_ENABLE:
350         HAL_INTERRUPT_UNMASK(chan->isr_vector);
351         HAL_INTERRUPT_SET_LEVEL(chan->isr_vector, 1);
352         HAL_READ_UINT_UART(chan->base+CYG_DEV_SERIAL_IER, ier);
353         ier |= SIO_IER_ERDAI;
354         HAL_WRITE_UINT_UART(chan->base+CYG_DEV_SERIAL_IER, ier);
355         irq_state = 1;
356         break;
357     case __COMMCTL_IRQ_DISABLE:
358         ret = irq_state;
359         irq_state = 0;
360         HAL_INTERRUPT_MASK(chan->isr_vector);
361         HAL_READ_UINT_UART(chan->base+CYG_DEV_SERIAL_IER, ier);
362         ier &= ~SIO_IER_ERDAI;
363         HAL_WRITE_UINT_UART(chan->base+CYG_DEV_SERIAL_IER, ier);
364         break;
365     case __COMMCTL_DBG_ISR_VECTOR:
366         ret = chan->isr_vector;
367         break;
368     case __COMMCTL_SET_TIMEOUT:
369         {
370             va_list ap;
371
372             va_start(ap, __func);
373
374             ret = chan->msec_timeout;
375             chan->msec_timeout = va_arg(ap, cyg_uint32);
376
377             va_end(ap);
378         }
379         break;
380     default:
381         break;
382     }
383     CYGARC_HAL_RESTORE_GP();
384     return ret;
385 }
386
387 static int cyg_hal_plf_duart_isr(void *__ch_data, int* __ctrlc,
388                                  CYG_ADDRWORD __vector, CYG_ADDRWORD __data)
389 {
390     channel_data_t* chan = (channel_data_t*)__ch_data;
391     uart_width _iir;
392     int res = 0;
393     CYGARC_HAL_SAVE_GP();
394
395     HAL_READ_UINT_UART(chan->base+CYG_DEV_SERIAL_IIR, _iir);
396     _iir &= SIO_IIR_ID_MASK;
397
398     *__ctrlc = 0;
399     if ( ISR_Rx == _iir ) {
400         uart_width c, lsr;
401         cyg_uint8 c8;
402         HAL_READ_UINT_UART(chan->base+CYG_DEV_SERIAL_LSR, lsr);
403         if (lsr & SIO_LSR_DR) {
404
405             HAL_READ_UINT_UART(chan->base+CYG_DEV_SERIAL_RHR, c);
406
407             c8 = (cyg_uint8) (c & 0x00FF);
408
409             if (cyg_hal_is_break( &c8 , 1 ))
410                 *__ctrlc = 1;
411         }
412
413         // Acknowledge the interrupt
414         HAL_INTERRUPT_ACKNOWLEDGE(chan->isr_vector);
415         res = CYG_ISR_HANDLED;
416     }
417
418     CYGARC_HAL_RESTORE_GP();
419     return res;
420 }
421
422 static void cyg_hal_plf_duart_init(void)
423 {
424     hal_virtual_comm_table_t* comm;
425     int cur = CYGACC_CALL_IF_SET_CONSOLE_COMM(CYGNUM_CALL_IF_SET_COMM_ID_QUERY_CURRENT);
426     int i;
427
428     // Init channels
429 #define NUMOF(x) (sizeof(x)/sizeof(x[0]))
430     for (i = 0;  i < NUMOF(channels);  i++) {
431         HAL_INTERRUPT_MASK(channels[i].isr_vector);
432         init_duart_channel(&channels[i]);
433         CYGACC_CALL_IF_SET_CONSOLE_COMM(i);
434         comm = CYGACC_CALL_IF_CONSOLE_PROCS();
435         CYGACC_COMM_IF_CH_DATA_SET(*comm, &channels[i]);
436         CYGACC_COMM_IF_WRITE_SET(*comm, cyg_hal_plf_duart_write);
437         CYGACC_COMM_IF_READ_SET(*comm, cyg_hal_plf_duart_read);
438         CYGACC_COMM_IF_PUTC_SET(*comm, cyg_hal_plf_duart_putc);
439         CYGACC_COMM_IF_GETC_SET(*comm, cyg_hal_plf_duart_getc);
440         CYGACC_COMM_IF_CONTROL_SET(*comm, cyg_hal_plf_duart_control);
441         CYGACC_COMM_IF_DBG_ISR_SET(*comm, cyg_hal_plf_duart_isr);
442         CYGACC_COMM_IF_GETC_TIMEOUT_SET(*comm, cyg_hal_plf_duart_getc_timeout);
443     }
444
445     // Restore original console
446     CYGACC_CALL_IF_SET_CONSOLE_COMM(cur);
447 }
448
449 //=============================================================================
450 // Compatibility with older stubs
451 //=============================================================================
452
453 //=============================================================================
454 // Compatibility with older stubs
455 //=============================================================================
456
457 #ifndef CYGSEM_HAL_VIRTUAL_VECTOR_DIAG
458
459 #include <cyg/hal/hal_stub.h>           // cyg_hal_gdb_interrupt
460
461 #if (CYGNUM_HAL_VIRTUAL_VECTOR_CONSOLE_CHANNEL == 0)
462 #define __BASE   CMA101_DUARTA
463 #define _INT     CYGNUM_HAL_INTERRUPT_SERIAL_A
464 #elif (CYGNUM_HAL_VIRTUAL_VECTOR_CONSOLE_CHANNEL == 1)
465 #define __BASE   CMA101_DUARTB
466 #define _INT     CYGNUM_HAL_INTERRUPT_SERIAL_B
467 #endif
468
469 #ifdef __BASE
470
471 #ifdef CYGSEM_HAL_ROM_MONITOR
472 #define CYG_HAL_STARTUP_ROM
473 #define CYG_HAL_STARTUP_ROMRAM
474 #undef CYGDBG_HAL_DEBUG_GDB_INCLUDE_STUBS
475 #endif
476
477 #if (defined(CYG_HAL_STARTUP_ROM) || defined(CYG_HAL_STARTUP_ROMRAM)) && !defined(CYGDBG_HAL_DEBUG_GDB_INCLUDE_STUBS)
478 #define HAL_DIAG_USES_HARDWARE
479 #elif !defined(CYGDBG_HAL_DIAG_TO_DEBUG_CHAN)
480 #define HAL_DIAG_USES_HARDWARE
481 #elif CYGNUM_HAL_VIRTUAL_VECTOR_CONSOLE_CHANNEL != CYGNUM_HAL_VIRTUAL_VECTOR_DEBUG_CHANNEL
482 #define HAL_DIAG_USES_HARDWARE
483 #endif
484
485 static channel_data_t channel = {
486     (uart_width*) _BASE, 0, 0
487 };
488
489 #ifdef HAL_DIAG_USES_HARDWARE
490
491 void hal_diag_init(void)
492 {
493     static int init = 0;
494     char *msg = "\n\rARM eCos\n\r";
495     uart_width lcr;
496
497     if (init++) return;
498
499     init_duart_channel(&channel);
500
501     while (*msg) hal_diag_write_char(*msg++);
502 }
503
504 #ifdef DEBUG_DIAG
505 #if defined(CYGDBG_HAL_DEBUG_GDB_INCLUDE_STUBS)
506 #define DIAG_BUFSIZE 32
507 #else
508 #define DIAG_BUFSIZE 2048
509 #endif
510 static char diag_buffer[DIAG_BUFSIZE];
511 static int diag_bp = 0;
512 #endif
513
514 void hal_diag_write_char(char c)
515 {
516     uart_width lsr;
517
518     hal_diag_init();
519
520     cyg_hal_plf_duart_putc(&channel, c)
521
522 #ifdef DEBUG_DIAG
523     diag_buffer[diag_bp++] = c;
524     if (diag_bp == DIAG_BUFSIZE) {
525         while (1) ;
526         diag_bp = 0;
527     }
528 #endif
529 }
530
531 void hal_diag_read_char(char *c)
532 {
533     *c = cyg_hal_plf_duart_getc(&channel);
534 }
535
536 #else // HAL_DIAG relies on GDB
537
538 // Initialize diag port - assume GDB channel is already set up
539 void hal_diag_init(void)
540 {
541     if (0) init_duart_channel(&channel); // avoid warning
542 }
543
544 // Actually send character down the wire
545 static void hal_diag_write_char_serial(char c)
546 {
547     cyg_hal_plf_duart_putc(&channel, c);
548 }
549
550 static bool hal_diag_read_serial(char *c)
551 {
552     long timeout = 1000000000;  // A long time...
553
554     while (!cyg_hal_plf_duart_getc_nonblock(&channel, c))
555         if (0 == --timeout) return false;
556
557     return true;
558 }
559
560 void hal_diag_read_char(char *c)
561 {
562     while (!hal_diag_read_serial(c)) ;
563 }
564
565 void hal_diag_write_char(char c)
566 {
567     static char line[100];
568     static int pos = 0;
569
570     // No need to send CRs
571     if (c == '\r') return;
572
573     line[pos++] = c;
574
575     if (c == '\n' || pos == sizeof(line)) {
576         CYG_INTERRUPT_STATE old;
577
578         // Disable interrupts. This prevents GDB trying to interrupt us
579         // while we are in the middle of sending a packet. The serial
580         // receive interrupt will be seen when we re-enable interrupts
581         // later.
582
583 #ifdef CYGDBG_HAL_DEBUG_GDB_INCLUDE_STUBS
584         CYG_HAL_GDB_ENTER_CRITICAL_IO_REGION(old);
585 #else
586         HAL_DISABLE_INTERRUPTS(old);
587 #endif
588
589         while (1) {
590             static char hex[] = "0123456789ABCDEF";
591             cyg_uint8 csum = 0;
592             int i;
593             char c1;
594
595             hal_diag_write_char_serial('$');
596             hal_diag_write_char_serial('O');
597             csum += 'O';
598             for (i = 0; i < pos; i++) {
599                 char ch = line[i];
600                 char h = hex[(ch>>4)&0xF];
601                 char l = hex[ch&0xF];
602                 hal_diag_write_char_serial(h);
603                 hal_diag_write_char_serial(l);
604                 csum += h;
605                 csum += l;
606             }
607             hal_diag_write_char_serial('#');
608             hal_diag_write_char_serial(hex[(csum>>4)&0xF]);
609             hal_diag_write_char_serial(hex[csum&0xF]);
610
611             // Wait for the ACK character '+' from GDB here and handle
612             // receiving a ^C instead.  This is the reason for this clause
613             // being a loop.
614             if (!hal_diag_read_serial(&c1))
615                 continue;   // No response - try sending packet again
616
617             if ( c1 == '+' )
618                 break;          // a good acknowledge
619
620 #ifdef CYGDBG_HAL_DEBUG_GDB_BREAK_SUPPORT
621             cyg_drv_interrupt_acknowledge(CYG_DEV_SERIAL_INT);
622             if ( c1 == 3 ) {
623                 // Ctrl-C: breakpoint.
624                 cyg_hal_gdb_interrupt (__builtin_return_address(0));
625                 break;
626             }
627 #endif
628             // otherwise, loop round again
629         }
630
631         pos = 0;
632
633         // And re-enable interrupts
634 #ifdef CYGDBG_HAL_DEBUG_GDB_INCLUDE_STUBS
635         CYG_HAL_GDB_LEAVE_CRITICAL_IO_REGION(old);
636 #else
637         HAL_RESTORE_INTERRUPTS(old);
638 #endif
639
640     }
641 }
642 #endif
643
644 #endif // __BASE
645
646 #endif // CYGSEM_HAL_VIRTUAL_VECTOR_DIAG
647
648 /*---------------------------------------------------------------------------*/