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): nickg, gthomas
44 // Contributors:nickg, gthomas, jlarmour
46 // Purpose: HAL diagnostic output
47 // Description: Implementations of HAL diagnostic output support.
49 //####DESCRIPTIONEND####
51 //===========================================================================*/
53 #include <pkgconf/hal.h>
55 #if CYGINT_HAL_V850_DIAG_ONCHIP_SERIAL0
57 #include <pkgconf/system.h>
58 #include CYGBLD_HAL_PLATFORM_H
60 #include <cyg/infra/cyg_type.h> // base types
61 #include <cyg/infra/cyg_ass.h> // assertion macros
63 #include <cyg/hal/hal_arch.h> // basic machine info
64 #include <cyg/hal/hal_intr.h> // interrupt macros
65 #include <cyg/hal/hal_io.h> // IO macros
66 #include <cyg/hal/hal_diag.h>
68 #include <cyg/hal/hal_stub.h> // target_register_t
69 #include <cyg/hal/hal_if.h> // Calling interface definitions
70 #include <cyg/hal/hal_misc.h> // Helper functions
71 #include <cyg/hal/drv_api.h> // CYG_ISR_HANDLED
73 #include <cyg/hal/v850_common.h> // hardware registers, etc.
76 #define BAUD_COUNT ((CYGHWR_HAL_V85X_CPU_FREQ/2)/CYGHWR_HAL_V85X_V850_DIAG_BAUD)
77 #define BAUD_DIVISOR 1
79 /*---------------------------------------------------------------------------*/
83 init_serial_channel(void* base)
85 volatile unsigned char *mode = (volatile unsigned char *)V850_REG_ASIM0;
86 volatile unsigned char *brgc = (volatile unsigned char *)V850_REG_BRGC0;
87 volatile unsigned char *brgm0 = (volatile unsigned char *)V850_REG_BRGMC00;
88 #ifdef V850_REG_BRGMC01
89 volatile unsigned char *brgm1 = (volatile unsigned char *)V850_REG_BRGMC01;
91 volatile unsigned char *rxstat = (volatile unsigned char *)V850_REG_SRIC0;
92 volatile unsigned char *rxerr = (volatile unsigned char *)V850_REG_SERIC0;
93 volatile unsigned char *txstat = (volatile unsigned char *)V850_REG_STIC0;
94 int count = BAUD_COUNT;
95 int divisor = BAUD_DIVISOR;
97 while (count > 0xFF) {
104 *brgm0 = divisor & 0x07;
105 #ifdef V850_REG_BRGMC01
106 *brgm1 = divisor >> 3;
113 // Actually send character down the wire
115 cyg_hal_plf_serial_putc(void* __ch_data, cyg_uint8 c)
117 volatile unsigned char *TxDATA = (volatile unsigned char *)V850_REG_TXS0;
118 volatile unsigned char *TxSTAT = (volatile unsigned char *)V850_REG_STIC0;
119 int timeout = 0xFFFF;
121 CYGARC_HAL_SAVE_GP();
124 *TxDATA = (unsigned char)c;
125 // Wait for Tx not busy
126 while ((*TxSTAT & 0x80) == 0x00) {
132 CYGARC_HAL_RESTORE_GP();
136 cyg_hal_plf_serial_getc_nonblock(void* __ch_data, cyg_uint8* ch)
138 volatile unsigned char *RxDATA = (volatile unsigned char *)V850_REG_RXB0;
139 volatile unsigned char *RxSTAT = (volatile unsigned char *)V850_REG_SRIC0;
141 if ((*RxSTAT & 0x80) == 0x00)
150 cyg_hal_plf_serial_getc(void* __ch_data)
153 CYGARC_HAL_SAVE_GP();
155 while(!cyg_hal_plf_serial_getc_nonblock(__ch_data, &ch));
157 CYGARC_HAL_RESTORE_GP();
162 cyg_hal_plf_serial_write(void* __ch_data, const cyg_uint8* __buf,
165 CYGARC_HAL_SAVE_GP();
168 cyg_hal_plf_serial_putc(__ch_data, *__buf++);
170 CYGARC_HAL_RESTORE_GP();
174 cyg_hal_plf_serial_read(void* __ch_data, cyg_uint8* __buf, cyg_uint32 __len)
176 CYGARC_HAL_SAVE_GP();
179 *__buf++ = cyg_hal_plf_serial_getc(__ch_data);
181 CYGARC_HAL_RESTORE_GP();
184 static cyg_int32 msec_timeout = 1000;
187 cyg_hal_plf_serial_getc_timeout(void* __ch_data, cyg_uint8* ch)
189 int delay_count = msec_timeout * 10; // delay in .1 ms steps
191 CYGARC_HAL_SAVE_GP();
194 res = cyg_hal_plf_serial_getc_nonblock(__ch_data, ch);
195 if (res || 0 == delay_count--)
198 CYGACC_CALL_IF_DELAY_US(100);
201 CYGARC_HAL_RESTORE_GP();
206 cyg_hal_plf_serial_control(void *__ch_data, __comm_control_cmd_t __func, ...)
208 static int irq_state = 0;
210 CYGARC_HAL_SAVE_GP();
213 case __COMMCTL_IRQ_ENABLE:
214 HAL_INTERRUPT_UNMASK(CYGNUM_HAL_VECTOR_INTCSI1);
217 case __COMMCTL_IRQ_DISABLE:
220 HAL_INTERRUPT_MASK(CYGNUM_HAL_VECTOR_INTCSI1);
222 case __COMMCTL_DBG_ISR_VECTOR:
223 ret = CYGNUM_HAL_VECTOR_INTCSI1;
225 case __COMMCTL_SET_TIMEOUT:
229 va_start(ap, __func);
232 msec_timeout = va_arg(ap, cyg_uint32);
239 CYGARC_HAL_RESTORE_GP();
244 cyg_hal_plf_serial_isr(void *__ch_data, int* __ctrlc,
245 CYG_ADDRWORD __vector, CYG_ADDRWORD __data)
247 volatile unsigned char *RxDATA = (volatile unsigned char *)V850_REG_RXB0;
250 CYGARC_HAL_SAVE_GP();
254 if( cyg_hal_is_break( &c , 1 ) )
257 cyg_drv_interrupt_acknowledge(CYGNUM_HAL_VECTOR_INTCSI1);
259 res = CYG_ISR_HANDLED;
261 CYGARC_HAL_RESTORE_GP();
266 cyg_hal_plf_serial_init(void)
268 hal_virtual_comm_table_t* comm;
269 int cur = CYGACC_CALL_IF_SET_CONSOLE_COMM(CYGNUM_CALL_IF_SET_COMM_ID_QUERY_CURRENT);
271 // Disable interrupts.
272 HAL_INTERRUPT_MASK(CYGNUM_HAL_VECTOR_INTCSI1);
275 init_serial_channel((cyg_uint8*)0);
277 // Setup procs in the vector table
280 CYGACC_CALL_IF_SET_CONSOLE_COMM(0);
281 comm = CYGACC_CALL_IF_CONSOLE_PROCS();
282 CYGACC_COMM_IF_CH_DATA_SET(*comm, 0);
283 CYGACC_COMM_IF_WRITE_SET(*comm, cyg_hal_plf_serial_write);
284 CYGACC_COMM_IF_READ_SET(*comm, cyg_hal_plf_serial_read);
285 CYGACC_COMM_IF_PUTC_SET(*comm, cyg_hal_plf_serial_putc);
286 CYGACC_COMM_IF_GETC_SET(*comm, cyg_hal_plf_serial_getc);
287 CYGACC_COMM_IF_CONTROL_SET(*comm, cyg_hal_plf_serial_control);
288 CYGACC_COMM_IF_DBG_ISR_SET(*comm, cyg_hal_plf_serial_isr);
289 CYGACC_COMM_IF_GETC_TIMEOUT_SET(*comm, cyg_hal_plf_serial_getc_timeout);
291 // Restore original console
292 CYGACC_CALL_IF_SET_CONSOLE_COMM(cur);
295 __externC void cyg_hal_plf_ice_diag_init();
298 cyg_hal_plf_comms_init(void)
300 static int initialized = 0;
307 cyg_hal_plf_serial_init();
309 #ifdef CYGDBG_HAL_V85X_V850_ICE_DIAG
310 cyg_hal_plf_ice_diag_init();
314 //=============================================================================
315 // Compatibility with older stubs
316 //=============================================================================
318 #ifndef CYGSEM_HAL_VIRTUAL_VECTOR_DIAG
320 // Assumption: all diagnostic output must be GDB packetized unless this is a ROM (i.e.
321 // totally stand-alone) system.
323 //#ifdef CYGSEM_HAL_ROM_MONITOR
324 //#define CYG_HAL_STARTUP_ROM
325 //#undef CYGDBG_HAL_DEBUG_GDB_INCLUDE_STUBS
328 #if (defined(CYG_HAL_STARTUP_ROM) || defined(CYG_HAL_STARTUP_ROMRAM)) \
329 && !defined(CYGDBG_HAL_DEBUG_GDB_INCLUDE_STUBS)
330 #define HAL_DIAG_USES_HARDWARE
332 #if !defined(CYGDBG_HAL_DIAG_TO_DEBUG_CHAN)
333 #define HAL_DIAG_USES_HARDWARE
337 void hal_diag_init(void)
339 init_serial_channel(0);
342 #ifdef HAL_DIAG_USES_HARDWARE
344 void hal_diag_write_char(char c)
346 CYG_INTERRUPT_STATE old;
347 HAL_DISABLE_INTERRUPTS(old);
348 cyg_hal_plf_serial_putc(0, c);
349 HAL_RESTORE_INTERRUPTS(old);
352 void hal_diag_read_char(char *c)
354 *c = cyg_hal_plf_serial_getc(0);
357 #else // HAL_DIAG relies on GDB
360 hal_diag_read_char(char *c)
362 *c = cyg_hal_plf_serial_getc(0);
366 hal_diag_write_char(char c)
368 static char line[100];
371 // No need to send CRs
372 if( c == '\r' ) return;
376 if( c == '\n' || pos == sizeof(line) )
378 CYG_INTERRUPT_STATE old;
380 // Disable interrupts. This prevents GDB trying to interrupt us
381 // while we are in the middle of sending a packet. The serial
382 // receive interrupt will be seen when we re-enable interrupts
385 #ifdef CYGDBG_HAL_DEBUG_GDB_INCLUDE_STUBS
386 CYG_HAL_GDB_ENTER_CRITICAL_IO_REGION(old);
388 HAL_DISABLE_INTERRUPTS(old);
393 static char hex[] = "0123456789ABCDEF";
398 cyg_hal_plf_serial_putc(0, '$');
399 cyg_hal_plf_serial_putc(0, 'O');
401 for( i = 0; i < pos; i++ )
404 char h = hex[(ch>>4)&0xF];
405 char l = hex[ch&0xF];
406 cyg_hal_plf_serial_putc(0, h);
407 cyg_hal_plf_serial_putc(0, l);
411 cyg_hal_plf_serial_putc(0, '#');
412 cyg_hal_plf_serial_putc(0, hex[(csum>>4)&0xF]);
413 cyg_hal_plf_serial_putc(0, hex[csum&0xF]);
415 // Wait for the ACK character '+' from GDB here and handle
416 // receiving a ^C instead. This is the reason for this clause
418 c1 = cyg_hal_plf_serial_getc(0);
421 break; // a good acknowledge
423 #if defined(CYGDBG_HAL_DEBUG_GDB_INCLUDE_STUBS) && \
424 defined(CYGDBG_HAL_DEBUG_GDB_BREAK_SUPPORT)
425 cyg_drv_interrupt_acknowledge(CYGNUM_HAL_VECTOR_INTCSI1);
427 // Ctrl-C: breakpoint.
428 cyg_hal_gdb_interrupt (__builtin_return_address(0));
432 // otherwise, loop round again
437 // And re-enable interrupts
438 #ifdef CYGDBG_HAL_DEBUG_GDB_INCLUDE_STUBS
439 CYG_HAL_GDB_LEAVE_CRITICAL_IO_REGION(old);
441 HAL_RESTORE_INTERRUPTS(old);
445 #endif // USE HARDWARE
447 #endif // CYGSEM_HAL_VIRTUAL_VECTOR_DIAG
449 #endif // #if CYGINT_HAL_V850_DIAG_ONCHIP_SERIAL0
451 /*---------------------------------------------------------------------------*/
452 /* End of hal_diag.c */