1 //=============================================================================
5 // Synthetic target diagnostic output code
7 //=============================================================================
8 //####ECOSGPLCOPYRIGHTBEGIN####
9 // -------------------------------------------
10 // This file is part of eCos, the Embedded Configurable Operating System.
11 // Copyright (C) 2002 Bart Veer
12 // Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
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.
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
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.
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.
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.
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####
45 // Contributors:proven, bartv
47 // Purpose: HAL diagnostic output
48 // Description: Implementations of HAL diagnostic output support.
50 // There are two possible ways of performing I/O. The first
51 // involves simply writing to stdout. This is robust, but
52 // has some disadvantages such as the output getting mixed up
53 // with gdb output. The second involves sending the data on
54 // to the auxiliary, less robust but much more flexible.
56 // Similarly, input can be handled by reading from stdin or
57 // by a suitable device in the auxiliary.
59 //####DESCRIPTIONEND####
61 //=============================================================================
63 #include <cyg/infra/cyg_type.h>
64 #include <cyg/hal/hal_diag.h>
65 #include <cyg/hal/hal_io.h>
66 #include <cyg/infra/cyg_ass.h>
68 //-----------------------------------------------------------------------------
69 // If the auxiliary exists, hal_diag_init() will try to contact it and
70 // instantiate a console device. Subsequent console writes will be
71 // redirected to that device, as long as the auxiliary is up and running.
72 // If the auxiliary is not being used or has exited, console writes
73 // will instead go to stdout.
75 // This code also contains an implementation of hal_diag_read_char()
76 // which is probably not very useful. Currently it works by reading
77 // from stdin, but no attempt is made to set the tty into raw mode
78 // or anything like that.
80 static int auxiliary_console_id = -1;
82 void hal_diag_init( void )
84 if (synth_auxiliary_running) {
85 auxiliary_console_id = synth_auxiliary_instantiate("hal/synth/arch", SYNTH_MAKESTRING(CYGPKG_HAL_SYNTH), "console",
86 (const char*) 0, (const char*) 0);
90 // Output a single character.
92 // The calling code will output one character at a time. Output
93 // involves at least one system call, and this is expensive for
94 // a single character (especially when used in conjunction with
95 // I/O intensive facilities like unbuffered tracing). Therefore
96 // this code will buffer lines up to 128 characters before
99 // NOTE: one problem is that there is no support for flushing buffers
100 // at this level. Therefore if say C library stdio ends up mapped to
101 // HAL diagnostics I/O then functions like fflush() and setvbuf() will
102 // not behave the way they should. There is no simple workaround at
103 // this level, the required information is not available.
105 void hal_diag_write_char(char c)
107 static int diag_index = 0;
108 static unsigned char diag_buffer[128];
110 CYG_ASSERT(diag_index < 128, "Diagnostic buffer overflow");
112 diag_buffer[diag_index++] = (unsigned char) c;
113 if (('\n' == c) || (128 == diag_index)) {
114 if ((-1 != auxiliary_console_id) && synth_auxiliary_running) {
115 synth_auxiliary_xchgmsg(auxiliary_console_id, 0, 0, 0, diag_buffer, diag_index, (int *) 0, (unsigned char*) 0, (int *)0, 0);
119 unsigned char* next = diag_buffer;
121 while (diag_index > 0) {
122 written = cyg_hal_sys_write(1, next, diag_index);
124 diag_index -= written;
126 } else if ((-CYG_HAL_SYS_EINTR != written) && (-CYG_HAL_SYS_EAGAIN != written)) {
127 CYG_FAIL("Unexpected error writing to stdout.");
132 CYG_ASSERT(0 == diag_index, "All data should have been written out");
134 cyg_hal_sys_fdatasync(1);
139 // Diagnostic input. It is not clear that this is actually useful,
140 // input would normally go to gdb rather than to the application. If
141 // keyboard input really is required then that should be handled via a
142 // suitable device driver interacting with the auxiliary, not at the
143 // HAL level. The read syscall will get woken up by the itimer alarm,
144 // but we don't want to stop reading if that's the case
146 void hal_diag_read_char(char *c)
150 rc = cyg_hal_sys_read(0, c, 1);
151 } while ((-CYG_HAL_SYS_EINTR == rc) || (-CYG_HAL_SYS_EAGAIN == rc));
154 //-----------------------------------------------------------------------------