1 /*=============================================================================
5 // HAL diagnostic output code
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.
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.
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
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.
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.
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.
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####
43 // Author(s): hmt, nickg
44 // Contributors: nickg
46 // Purpose: HAL diagnostic output
47 // Description: Implementations of HAL diagnostic output support.
49 //####DESCRIPTIONEND####
51 //===========================================================================*/
53 #include <pkgconf/hal.h>
55 #include <cyg/infra/cyg_type.h> // base types
56 #include <cyg/infra/cyg_trac.h> // tracing macros
57 #include <cyg/infra/cyg_ass.h> // assertion macros
59 #include <cyg/hal/hal_arch.h> // which includes var_arch => UART.
60 #include <cyg/hal/hal_diag.h>
61 #include <cyg/hal/hal_intr.h>
62 #include <cyg/hal/hal_io.h>
63 #include <cyg/hal/drv_api.h>
64 #include <cyg/hal/hal_if.h> // interface API
65 #include <cyg/hal/hal_misc.h> // Helper functions
67 /*---------------------------------------------------------------------------*/
68 //#define CYG_KERNEL_DIAG_GDB
70 #if defined(CYGSEM_HAL_USE_ROM_MONITOR_GDB_stubs)
72 #define CYG_KERNEL_DIAG_GDB
76 /*---------------------------------------------------------------------------*/
78 void hal_uart_setbaud( int baud )
80 // now set the baud rate
81 *UARTLCR |= UARTLCR_DLAB;
82 *UARTDLM = UARTDLM_VAL( baud );
83 *UARTDLL = UARTDLL_VAL( baud );
84 *UARTLCR &=~UARTLCR_DLAB;
87 void hal_uart_init(void)
89 // Ensure that we use the internal clock
90 *S_GMR &=~S_GMR_UCSEL;
92 *UARTFCR = UARTFCR_16550_MODE;
93 *UARTLCR = UARTLCR_8N1;
94 *UARTIER = UARTIER_ERBFI; // rx interrupts enabled for CTRL-C
96 hal_uart_setbaud( CYGHWR_HAL_MIPS_UPD985XX_DIAG_BAUD );
99 void hal_uart_write_char(char c)
101 while ( 0 == (UARTLSR_THRE & *UARTLSR) )
104 *UARTTHR = (unsigned int)c;
106 // Ensure that this write does not provoke a spurious interrupt.
107 HAL_INTERRUPT_ACKNOWLEDGE( CYGHWR_HAL_GDB_PORT_VECTOR );
110 void hal_uart_read_char(char *c)
112 while ( 0 == (UARTLSR_DR & *UARTLSR) )
117 // Ensure that this read does not provoke a spurious interrupt.
118 HAL_INTERRUPT_ACKNOWLEDGE( CYGHWR_HAL_GDB_PORT_VECTOR );
121 int hal_uart_read_char_nonblock(char *c)
123 if ( 0 == (UARTLSR_DR & *UARTLSR) )
128 // Ensure that this read does not provoke a spurious interrupt.
129 HAL_INTERRUPT_ACKNOWLEDGE( CYGHWR_HAL_GDB_PORT_VECTOR );
135 /*---------------------------------------------------------------------------*/
137 #ifdef CYGSEM_HAL_VIRTUAL_VECTOR_DIAG
139 #include <cyg/hal/hal_if.h>
141 // This is lame, duplicating all these wrappers with slightly different details.
142 // All this should be in hal_if.c
144 cyg_hal_plf_serial_putc(void* __ch_data, cyg_uint8 c)
146 CYGARC_HAL_SAVE_GP();
147 hal_uart_write_char( (char)c );
148 CYGARC_HAL_RESTORE_GP();
152 cyg_hal_plf_serial_getc(void* __ch_data)
155 CYGARC_HAL_SAVE_GP();
156 hal_uart_read_char( (char *)&result );
157 CYGARC_HAL_RESTORE_GP();
162 cyg_hal_plf_serial_getc_nonblock(void* __ch_data, cyg_uint8 *pc)
164 return hal_uart_read_char_nonblock( (char *)pc );
168 cyg_hal_plf_serial_write(void* __ch_data, const cyg_uint8* __buf,
171 CYGARC_HAL_SAVE_GP();
174 cyg_hal_plf_serial_putc(__ch_data, *__buf++);
176 CYGARC_HAL_RESTORE_GP();
180 cyg_hal_plf_serial_read(void* __ch_data, cyg_uint8* __buf, cyg_uint32 __len)
182 CYGARC_HAL_SAVE_GP();
185 *__buf++ = cyg_hal_plf_serial_getc(__ch_data);
187 CYGARC_HAL_RESTORE_GP();
190 static int chan__msec_timeout = 0;
191 static int chan__irq_state = 0;
192 static int chan__baud_rate = CYGHWR_HAL_MIPS_UPD985XX_DIAG_BAUD;
195 cyg_hal_plf_serial_getc_timeout(void* __ch_data, cyg_uint8* ch)
199 CYGARC_HAL_SAVE_GP();
201 delay_count = chan__msec_timeout * 10; // delay in .1 ms steps
204 res = cyg_hal_plf_serial_getc_nonblock(__ch_data, ch);
205 if (res || 0 >= delay_count--)
208 CYGACC_CALL_IF_DELAY_US(100);
211 CYGARC_HAL_RESTORE_GP();
216 cyg_hal_plf_serial_control(void *__ch_data, __comm_control_cmd_t __func, ...)
221 CYGARC_HAL_SAVE_GP();
222 va_start(ap, __func);
225 case __COMMCTL_GETBAUD:
226 ret = chan__baud_rate;
228 case __COMMCTL_SETBAUD:
230 chan__baud_rate = va_arg(ap, cyg_int32);
231 hal_uart_setbaud( chan__baud_rate );
233 case __COMMCTL_IRQ_ENABLE:
234 ret = chan__irq_state;
236 HAL_INTERRUPT_UNMASK( CYGHWR_HAL_GDB_PORT_VECTOR );
238 case __COMMCTL_IRQ_DISABLE:
239 ret = chan__irq_state;
241 HAL_INTERRUPT_MASK( CYGHWR_HAL_GDB_PORT_VECTOR );
243 case __COMMCTL_DBG_ISR_VECTOR:
244 ret = CYGHWR_HAL_GDB_PORT_VECTOR;
246 case __COMMCTL_SET_TIMEOUT:
247 ret = chan__msec_timeout;
248 chan__msec_timeout = va_arg(ap, cyg_uint32);
254 CYGARC_HAL_RESTORE_GP();
259 cyg_hal_plf_serial_isr(void *__ch_data, int* __ctrlc,
260 CYG_ADDRWORD __vector, CYG_ADDRWORD __data)
267 if ( hal_uart_read_char_nonblock( &c ) ) {
268 if ( cyg_hal_is_break( &c , 1 ) )
270 ret = CYG_ISR_HANDLED;
277 cyg_hal_plf_serial_init(void)
279 hal_virtual_comm_table_t* comm;
280 int cur = CYGACC_CALL_IF_SET_CONSOLE_COMM(CYGNUM_CALL_IF_SET_COMM_ID_QUERY_CURRENT);
283 CYGACC_CALL_IF_SET_CONSOLE_COMM(0);
284 comm = CYGACC_CALL_IF_CONSOLE_PROCS();
285 CYGACC_COMM_IF_CH_DATA_SET(*comm, comm);
286 CYGACC_COMM_IF_WRITE_SET(*comm, cyg_hal_plf_serial_write);
287 CYGACC_COMM_IF_READ_SET(*comm, cyg_hal_plf_serial_read);
288 CYGACC_COMM_IF_PUTC_SET(*comm, cyg_hal_plf_serial_putc);
289 CYGACC_COMM_IF_GETC_SET(*comm, cyg_hal_plf_serial_getc);
290 CYGACC_COMM_IF_CONTROL_SET(*comm, cyg_hal_plf_serial_control);
291 CYGACC_COMM_IF_DBG_ISR_SET(*comm, cyg_hal_plf_serial_isr);
292 CYGACC_COMM_IF_GETC_TIMEOUT_SET(*comm, cyg_hal_plf_serial_getc_timeout);
294 // Restore original console
295 CYGACC_CALL_IF_SET_CONSOLE_COMM(cur);
299 cyg_hal_plf_comms_init(void)
301 static int initialized = 0;
309 cyg_hal_plf_serial_init();
312 #endif // CYGSEM_HAL_VIRTUAL_VECTOR_DIAG
314 // ------------------------------------------------------------------------
316 void hal_diag_init(void)
321 void hal_diag_read_char(char *c)
323 hal_uart_read_char(c);
326 extern cyg_bool cyg_hal_is_break(char *buf, int size);
327 extern void cyg_hal_user_break(CYG_ADDRWORD *regs);
329 void hal_diag_write_char(char c)
331 #ifdef CYG_KERNEL_DIAG_GDB
332 static char line[100];
335 // No need to send CRs
336 if( c == '\r' ) return;
340 if( c == '\n' || pos == sizeof(line) )
343 // Disable interrupts. This prevents GDB trying to interrupt us
344 // while we are in the middle of sending a packet. The serial
345 // receive interrupt will be seen when we re-enable interrupts
347 CYG_INTERRUPT_STATE oldstate;
348 HAL_DISABLE_INTERRUPTS(oldstate);
352 static char hex[] = "0123456789ABCDEF";
357 hal_uart_write_char('$');
358 hal_uart_write_char('O');
360 for( i = 0; i < pos; i++ )
363 char h = hex[(ch>>4)&0xF];
364 char l = hex[ch&0xF];
365 hal_uart_write_char(h);
366 hal_uart_write_char(l);
370 hal_uart_write_char('#');
371 hal_uart_write_char(hex[(csum>>4)&0xF]);
372 hal_uart_write_char(hex[csum&0xF]);
374 hal_uart_read_char( &c1 );
376 if( c1 == '+' ) break;
378 if( cyg_hal_is_break( &c1 , 1 ) )
379 cyg_hal_user_break( NULL );
385 // Disabling the interrupts for an extended period of time
386 // can provoke a spurious interrupt.
388 // Ensure that this write does not provoke a spurious interrupt.
389 HAL_INTERRUPT_ACKNOWLEDGE( CYGHWR_HAL_GDB_PORT_VECTOR );
391 // And re-enable interrupts
392 HAL_RESTORE_INTERRUPTS( oldstate );
396 hal_uart_write_char(c);
400 /*---------------------------------------------------------------------------*/
401 /* End of hal_diag.c */