comparison packages/redboot/current/src/io.c @ 115:6ed91473a1cd ecos-sw-2000-08-21

Merge from eCos master repository on 2000-08-21-22:40:54-BST
author jlarmour
date Fri, 25 Aug 2000 17:32:38 +0000
parents
children 518f42066aba
comparison
equal deleted inserted replaced
114:5ad2b71d525e 115:6ed91473a1cd
1 //==========================================================================
2 //
3 // io.c
4 //
5 // RedBoot I/O support
6 //
7 //==========================================================================
8 //####COPYRIGHTBEGIN####
9 //
10 // -------------------------------------------
11 // The contents of this file are subject to the Red Hat eCos Public License
12 // Version 1.1 (the "License"); you may not use this file except in
13 // compliance with the License. You may obtain a copy of the License at
14 // http://www.redhat.com/
15 //
16 // Software distributed under the License is distributed on an "AS IS"
17 // basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
18 // License for the specific language governing rights and limitations under
19 // the License.
20 //
21 // The Original Code is eCos - Embedded Configurable Operating System,
22 // released September 30, 1998.
23 //
24 // The Initial Developer of the Original Code is Red Hat.
25 // Portions created by Red Hat are
26 // Copyright (C) 1998, 1999, 2000 Red Hat, Inc.
27 // All Rights Reserved.
28 // -------------------------------------------
29 //
30 //####COPYRIGHTEND####
31 //==========================================================================
32 //#####DESCRIPTIONBEGIN####
33 //
34 // Author(s): gthomas
35 // Contributors: gthomas
36 // Date: 2000-07-14
37 // Purpose:
38 // Description:
39 //
40 // This code is part of RedBoot (tm).
41 //
42 //####DESCRIPTIONEND####
43 //
44 //==========================================================================
45
46 #include "redboot.h"
47
48 // GDB interface functions
49 extern void ungetDebugChar(char c);
50
51 void
52 mon_write_char(char c)
53 {
54 hal_virtual_comm_table_t *__chan;
55
56 #ifdef CYGPKG_REDBOOT_ANY_CONSOLE
57 if (!console_selected) {
58 int cur = CYGACC_CALL_IF_SET_CONSOLE_COMM(CYGNUM_CALL_IF_SET_COMM_ID_QUERY_CURRENT);
59 int i;
60 // Send output to all channels
61 for (i = 0; i < CYGNUM_HAL_VIRTUAL_VECTOR_COMM_CHANNELS; i++) {
62 CYGACC_CALL_IF_SET_CONSOLE_COMM(i);
63 __chan = CYGACC_CALL_IF_CONSOLE_PROCS();
64 CYGACC_COMM_IF_PUTC(*__chan, c);
65 }
66 CYGACC_CALL_IF_SET_CONSOLE_COMM(cur);
67 } else
68 #endif
69 {
70 __chan = CYGACC_CALL_IF_CONSOLE_PROCS();
71 if (__chan)
72 CYGACC_COMM_IF_PUTC(*__chan, c);
73 else {
74 __chan = CYGACC_CALL_IF_DEBUG_PROCS();
75 CYGACC_COMM_IF_PUTC(*__chan, c);
76 }
77 }
78 }
79
80 static void
81 mon_read_char(char *c)
82 {
83 hal_virtual_comm_table_t* __chan = CYGACC_CALL_IF_CONSOLE_PROCS();
84
85 if (__chan)
86 *c = CYGACC_COMM_IF_GETC(*__chan);
87 else {
88 __chan = CYGACC_CALL_IF_DEBUG_PROCS();
89 *c = CYGACC_COMM_IF_GETC(*__chan);
90 }
91 }
92
93 static bool
94 mon_read_char_with_timeout(char *c)
95 {
96 bool res;
97 hal_virtual_comm_table_t *__chan;
98
99 #ifdef CYGPKG_REDBOOT_ANY_CONSOLE
100 if (!console_selected) {
101 int cur = CYGACC_CALL_IF_SET_CONSOLE_COMM(CYGNUM_CALL_IF_SET_COMM_ID_QUERY_CURRENT);
102 int i;
103 // Try input from all channels
104 for (i = 0; i < CYGNUM_HAL_VIRTUAL_VECTOR_COMM_CHANNELS; i++) {
105 CYGACC_CALL_IF_SET_CONSOLE_COMM(i);
106 __chan = CYGACC_CALL_IF_CONSOLE_PROCS();
107 res = CYGACC_COMM_IF_GETC_TIMEOUT(*__chan, c);
108 if (res) {
109 // Input available on this channel, make it be the console
110 console_selected = true;
111 CYGACC_CALL_IF_SET_DEBUG_COMM(i);
112 return res;
113 }
114 }
115 CYGACC_CALL_IF_SET_CONSOLE_COMM(cur);
116 } else
117 #endif
118 {
119 __chan = CYGACC_CALL_IF_CONSOLE_PROCS();
120 if (__chan)
121 res = CYGACC_COMM_IF_GETC_TIMEOUT(*__chan, c);
122 else {
123 __chan = CYGACC_CALL_IF_DEBUG_PROCS();
124 res = CYGACC_COMM_IF_GETC_TIMEOUT(*__chan, c);
125 }
126 }
127 return res;
128 }
129
130 static void
131 mon_set_read_char_timeout(int ms)
132 {
133 hal_virtual_comm_table_t *__chan;
134
135 #ifdef CYGPKG_REDBOOT_ANY_CONSOLE
136 if (!console_selected) {
137 int cur = CYGACC_CALL_IF_SET_CONSOLE_COMM(CYGNUM_CALL_IF_SET_COMM_ID_QUERY_CURRENT);
138 int i;
139 // Set timeout on each channel so total amounts to desired value
140 ms = ms / CYGNUM_HAL_VIRTUAL_VECTOR_COMM_CHANNELS;
141 for (i = 0; i < CYGNUM_HAL_VIRTUAL_VECTOR_COMM_CHANNELS; i++) {
142 CYGACC_CALL_IF_SET_CONSOLE_COMM(i);
143 if ((__chan = CYGACC_CALL_IF_CONSOLE_PROCS()) != 0) {
144 CYGACC_COMM_IF_CONTROL(*__chan, __COMMCTL_SET_TIMEOUT, ms);
145 }
146 }
147 CYGACC_CALL_IF_SET_CONSOLE_COMM(cur);
148 } else
149 #endif
150 if ((__chan = CYGACC_CALL_IF_CONSOLE_PROCS()) != 0) {
151 CYGACC_COMM_IF_CONTROL(*__chan, __COMMCTL_SET_TIMEOUT, ms);
152 }
153 if ((__chan = CYGACC_CALL_IF_DEBUG_PROCS()) != 0) {
154 CYGACC_COMM_IF_CONTROL(*__chan, __COMMCTL_SET_TIMEOUT, ms);
155 }
156 }
157
158 //
159 // Read a line of input from the user
160 // Return:
161 // n: 'n' valid characters received
162 // 0: '$' (GDB lead-in)
163 // -1: No input before timeout
164 // -2: ^C typed
165 //
166 int
167 gets(char *buf, int buflen, int timeout)
168 {
169 char *ptr = buf;
170 char c;
171 bool res = false;
172 static char last_ch = '\0';
173
174 while (true) {
175 #ifdef CYGSEM_REDBOOT_FLASH_CONFIG
176 if (script && *script) {
177 c = *script++;
178 } else
179 #endif
180 if ((timeout > 0) && (ptr == buf)) {
181 mon_set_read_char_timeout(50);
182 while (timeout > 0) {
183 res = mon_read_char_with_timeout(&c);
184 if (res) {
185 // Got a character
186 break;
187 }
188 timeout -= 50;
189 }
190 if (res == false) {
191 return -1; // Input timed out
192 }
193 } else {
194 mon_read_char(&c);
195 }
196 *ptr = '\0';
197 switch (c) {
198 case 0x03: // ^C
199 if (ptr == buf) {
200 printf("^C\n");
201 return -2;
202 }
203 *ptr++ = c;
204 break;
205 case '\n':
206 case '\r':
207 // If previous character was the "other" end-of-line, ignore this one
208 if (((c == '\n') && (last_ch == '\r')) ||
209 ((c == '\r') && (last_ch == '\n'))) {
210 break;
211 }
212 // End of line
213 mon_write_char('\r');
214 mon_write_char('\n');
215 last_ch = c;
216 return 1;
217 case '\b':
218 case 0x7F: // DEL
219 if (ptr != buf) {
220 mon_write_char('\b');
221 mon_write_char(' ');
222 mon_write_char('\b');
223 ptr--;
224 }
225 break;
226 case '+':
227 case '$':
228 if (ptr == buf) {
229 // Give up and try GDB protocol
230 ungetDebugChar(c); // Push back character so stubs will see it
231 return 0;
232 }
233 // Fall through - accept '$' at other than start of line
234 default:
235 mon_write_char(c);
236 *ptr++ = c;
237 }
238 last_ch = c;
239 }
240 }
241
242 void
243 dump_buf_with_offset(cyg_uint8 *p,
244 CYG_ADDRWORD s,
245 cyg_uint8 *base)
246 {
247 int i, c;
248 if ((CYG_ADDRWORD)s > (CYG_ADDRWORD)p) {
249 s = (CYG_ADDRWORD)s - (CYG_ADDRWORD)p;
250 }
251 while ((int)s > 0) {
252 if (base) {
253 printf("0x%08X: ", (CYG_ADDRWORD)p - (CYG_ADDRWORD)base);
254 } else {
255 printf("0x%08X: ", (CYG_ADDRWORD)p);
256 }
257 for (i = 0; i < 16; i++) {
258 if (i < (int)s) {
259 printf("%02X", p[i] & 0xFF);
260 } else {
261 printf(" ");
262 }
263 if ((i % 2) == 1) printf(" ");
264 if ((i % 8) == 7) printf(" ");
265 }
266 printf(" |");
267 for (i = 0; i < 16; i++) {
268 if (i < (int)s) {
269 c = p[i] & 0xFF;
270 if ((c < 0x20) || (c >= 0x7F)) c = '.';
271 } else {
272 c = ' ';
273 }
274 printf("%c", c);
275 }
276 printf("|\n");
277 s -= 16;
278 p += 16;
279 }
280 }
281
282 void
283 dump_buf(void *p, CYG_ADDRWORD s)
284 {
285 dump_buf_with_offset((cyg_uint8 *)p, s, 0);
286 }
287
288 bool
289 verify_action(char *fmt, ...)
290 {
291 va_list ap;
292 char ans[8];
293 #ifdef CYGSEM_REDBOOT_FLASH_CONFIG
294 unsigned char *hold_script = script;
295 #endif
296
297 va_start(ap, fmt);
298 vprintf(fmt, ap);
299 printf(" - are you sure (y/n)? ");
300 #ifdef CYGSEM_REDBOOT_FLASH_CONFIG
301 script = (unsigned char *)0;
302 #endif
303 gets(ans, sizeof(ans), 0);
304 #ifdef CYGSEM_REDBOOT_FLASH_CONFIG
305 script = hold_script;
306 #endif
307 return ((ans[0] == 'y') || (ans[0] == 'Y'));
308 }