2 * Copyright © 2009 Keith Packard <keithp@keithp.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
21 * Basic I/O functions to support SDCC stdio package
24 #ifndef USE_SERIAL_0_STDIN
25 #define USE_SERIAL_0_STDIN 0
27 #ifndef USE_SERIAL_1_STDIN
28 #define USE_SERIAL_1_STDIN 0
30 #ifndef USE_SERIAL_2_STDIN
31 #define USE_SERIAL_2_STDIN 0
33 #ifndef USE_SERIAL_3_STDIN
34 #define USE_SERIAL_3_STDIN 0
36 #ifndef USE_SERIAL_4_STDIN
37 #define USE_SERIAL_4_STDIN 0
39 #ifndef USE_SERIAL_5_STDIN
40 #define USE_SERIAL_5_STDIN 0
42 #ifndef USE_SERIAL_6_STDIN
43 #define USE_SERIAL_6_STDIN 0
45 #ifndef USE_SERIAL_7_STDIN
46 #define USE_SERIAL_7_STDIN 0
48 #ifndef USE_SERIAL_8_STDIN
49 #define USE_SERIAL_8_STDIN 0
51 #ifndef USE_SERIAL_9_STDIN
52 #define USE_SERIAL_9_STDIN 0
55 #define USE_SERIAL_STDIN (USE_SERIAL_0_STDIN + \
56 USE_SERIAL_1_STDIN + \
57 USE_SERIAL_2_STDIN + \
58 USE_SERIAL_3_STDIN + \
59 USE_SERIAL_4_STDIN + \
60 USE_SERIAL_5_STDIN + \
61 USE_SERIAL_6_STDIN + \
62 USE_SERIAL_7_STDIN + \
63 USE_SERIAL_8_STDIN + \
66 #define AO_NUM_STDIOS (HAS_USB + PACKET_HAS_SLAVE + USE_SERIAL_STDIN)
68 __xdata struct ao_stdio ao_stdios[AO_NUM_STDIOS];
71 __pdata int8_t ao_cur_stdio;
72 __pdata int8_t ao_num_stdios;
74 __pdata int8_t ao_cur_stdio;
75 #define ao_cur_stdio 0
76 #define ao_num_stdios 0
84 extern void ao_debug_out(char c);
92 (*ao_stdios[ao_cur_stdio].putchar)('\r');
93 (*ao_stdios[ao_cur_stdio].putchar)(c);
99 if (ao_stdios[ao_cur_stdio].flush)
100 ao_stdios[ao_cur_stdio].flush();
103 __xdata uint8_t ao_stdin_ready;
106 getchar(void) __reentrant
111 ao_arch_block_interrupts();
112 stdio = ao_cur_stdio;
114 c = ao_stdios[stdio]._pollchar();
115 if (c != AO_READ_AGAIN)
117 #if AO_NUM_STDIOS > 1
118 if (++stdio == ao_num_stdios)
120 if (stdio == ao_cur_stdio)
122 ao_sleep(&ao_stdin_ready);
124 #if AO_NUM_STDIOS > 1
125 ao_cur_stdio = stdio;
127 ao_arch_release_interrupts();
134 return ao_stdios[ao_cur_stdio].echo;
138 ao_add_stdio(int (*_pollchar)(void),
139 void (*putchar)(char),
140 void (*flush)(void)) __reentrant
142 #if AO_NUM_STDIOS > 1
143 if (ao_num_stdios == AO_NUM_STDIOS)
144 ao_panic(AO_PANIC_STDIO);
146 ao_stdios[ao_num_stdios]._pollchar = _pollchar;
147 ao_stdios[ao_num_stdios].putchar = putchar;
148 ao_stdios[ao_num_stdios].flush = flush;
149 ao_stdios[ao_num_stdios].echo = 1;
150 #if AO_NUM_STDIOS > 1
151 return ao_num_stdios++;