altos: Remove old AO_SEND_ALL_BARO bits
[fw/altos] / src / kernel / ao_stdio.c
1 /*
2  * Copyright © 2009 Keith Packard <keithp@keithp.com>
3  *
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.
7  *
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.
12  *
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.
16  */
17
18 #include "ao.h"
19
20 /*
21  * Basic I/O functions to support SDCC stdio package
22  */
23
24 #ifndef USE_SERIAL_0_STDIN
25 #define USE_SERIAL_0_STDIN      0
26 #endif
27 #ifndef USE_SERIAL_1_STDIN
28 #define USE_SERIAL_1_STDIN      0
29 #endif
30 #ifndef USE_SERIAL_2_STDIN
31 #define USE_SERIAL_2_STDIN      0
32 #endif
33 #ifndef USE_SERIAL_3_STDIN
34 #define USE_SERIAL_3_STDIN      0
35 #endif
36 #ifndef USE_SERIAL_4_STDIN
37 #define USE_SERIAL_4_STDIN      0
38 #endif
39 #ifndef USE_SERIAL_5_STDIN
40 #define USE_SERIAL_5_STDIN      0
41 #endif
42 #ifndef USE_SERIAL_6_STDIN
43 #define USE_SERIAL_6_STDIN      0
44 #endif
45 #ifndef USE_SERIAL_7_STDIN
46 #define USE_SERIAL_7_STDIN      0
47 #endif
48 #ifndef USE_SERIAL_8_STDIN
49 #define USE_SERIAL_8_STDIN      0
50 #endif
51 #ifndef USE_SERIAL_9_STDIN
52 #define USE_SERIAL_9_STDIN      0
53 #endif
54 #ifndef PACKET_HAS_SLAVE
55 #define PACKET_HAS_SLAVE        0
56 #endif
57
58 #define USE_SERIAL_STDIN (USE_SERIAL_0_STDIN +  \
59                           USE_SERIAL_1_STDIN +  \
60                           USE_SERIAL_2_STDIN +  \
61                           USE_SERIAL_3_STDIN +  \
62                           USE_SERIAL_4_STDIN +  \
63                           USE_SERIAL_5_STDIN +  \
64                           USE_SERIAL_6_STDIN +  \
65                           USE_SERIAL_7_STDIN +  \
66                           USE_SERIAL_8_STDIN +  \
67                           USE_SERIAL_9_STDIN)
68
69 #define AO_NUM_STDIOS   (HAS_USB + PACKET_HAS_SLAVE + USE_SERIAL_STDIN)
70
71 __xdata struct ao_stdio ao_stdios[AO_NUM_STDIOS];
72
73 #if AO_NUM_STDIOS > 1
74 __pdata int8_t ao_cur_stdio;
75 __pdata int8_t ao_num_stdios;
76 #else
77 __pdata int8_t ao_cur_stdio;
78 #define ao_cur_stdio    0
79 #define ao_num_stdios   0
80 #endif
81
82 void
83 putchar(char c)
84 {
85 #if LOW_LEVEL_DEBUG
86         if (!ao_cur_task) {
87                 extern void ao_debug_out(char c);
88                 if (c == '\n')
89                         ao_debug_out('\r');
90                 ao_debug_out(c);
91                 return;
92         }
93 #endif
94         if (c == '\n')
95                 (*ao_stdios[ao_cur_stdio].putchar)('\r');
96         (*ao_stdios[ao_cur_stdio].putchar)(c);
97 }
98
99 void
100 flush(void)
101 {
102         if (ao_stdios[ao_cur_stdio].flush)
103                 ao_stdios[ao_cur_stdio].flush();
104 }
105
106 __xdata uint8_t ao_stdin_ready;
107
108 char
109 getchar(void) __reentrant
110 {
111         int c;
112         int8_t stdio;
113
114         ao_arch_block_interrupts();
115         stdio = ao_cur_stdio;
116         for (;;) {
117                 c = ao_stdios[stdio]._pollchar();
118                 if (c != AO_READ_AGAIN)
119                         break;
120 #if AO_NUM_STDIOS > 1
121                 if (++stdio == ao_num_stdios)
122                         stdio = 0;
123                 if (stdio == ao_cur_stdio)
124 #endif
125                         ao_sleep(&ao_stdin_ready);
126         }
127 #if AO_NUM_STDIOS > 1
128         ao_cur_stdio = stdio;
129 #endif
130         ao_arch_release_interrupts();
131         return c;
132 }
133
134 uint8_t
135 ao_echo(void)
136 {
137         return ao_stdios[ao_cur_stdio].echo;
138 }
139
140 int8_t
141 ao_add_stdio(int (*_pollchar)(void),
142              void (*putchar)(char),
143              void (*flush)(void)) __reentrant
144 {
145 #if AO_NUM_STDIOS > 1
146         if (ao_num_stdios == AO_NUM_STDIOS)
147                 ao_panic(AO_PANIC_STDIO);
148 #endif
149         ao_stdios[ao_num_stdios]._pollchar = _pollchar;
150         ao_stdios[ao_num_stdios].putchar = putchar;
151         ao_stdios[ao_num_stdios].flush = flush;
152         ao_stdios[ao_num_stdios].echo = 1;
153 #if AO_NUM_STDIOS > 1
154         return ao_num_stdios++;
155 #else
156         return 0;
157 #endif
158 }