src-avr: Make avr-demo objects depend on ao_usb.h
[fw/altos] / src-avr / 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 #ifdef AVR
21 #undef putchar
22 #undef getchar
23 #define putchar(c)      ao_putchar(c)
24 #define getchar()       ao_getchar()
25 #endif
26
27 /*
28  * Basic I/O functions to support SDCC stdio package
29  */
30
31 #define AO_NUM_STDIOS   (HAS_USB + PACKET_HAS_SLAVE + USE_SERIAL_STDIN)
32
33 __xdata struct ao_stdio ao_stdios[AO_NUM_STDIOS];
34 __data int8_t ao_cur_stdio;
35 __data int8_t ao_num_stdios;
36
37 void
38 putchar(char c)
39 {
40         if (c == '\n')
41                 (*ao_stdios[ao_cur_stdio].putchar)('\r');
42         (*ao_stdios[ao_cur_stdio].putchar)(c);
43 }
44
45 void
46 flush(void)
47 {
48         if (ao_stdios[ao_cur_stdio].flush)
49                 ao_stdios[ao_cur_stdio].flush();
50 }
51
52 __xdata uint8_t ao_stdin_ready;
53
54 char
55 ao_getchar(void) __reentrant __critical
56 {
57         char c;
58         int8_t stdio = ao_cur_stdio;
59
60         for (;;) {
61                 c = ao_stdios[stdio].pollchar();
62                 if (c != AO_READ_AGAIN)
63                         break;
64                 if (++stdio == ao_num_stdios)
65                         stdio = 0;
66                 if (stdio == ao_cur_stdio)
67                         ao_sleep(&ao_stdin_ready);
68         }
69         ao_cur_stdio = stdio;
70         return c;
71 }
72
73 uint8_t
74 ao_echo(void)
75 {
76         return ao_stdios[ao_cur_stdio].echo;
77 }
78
79 int8_t
80 ao_add_stdio(char (*pollchar)(void),
81              void (*putchar)(char),
82              void (*flush)(void)) __reentrant
83 {
84         if (ao_num_stdios == AO_NUM_STDIOS)
85                 ao_panic(AO_PANIC_STDIO);
86         ao_stdios[ao_num_stdios].pollchar = pollchar;
87         ao_stdios[ao_num_stdios].putchar = putchar;
88         ao_stdios[ao_num_stdios].flush = flush;
89         ao_stdios[ao_num_stdios].echo = 1;
90         return ao_num_stdios++;
91 }
92
93 #ifdef AVR
94 int
95 stdio_put(char c, FILE *stream)
96 {
97 #if 0
98         if (ao_cur_task && ao_num_stdios)
99                 putchar(c);
100         else
101 #endif
102         {
103                 if (c == '\n')
104                         stdio_put('\r', stream);
105                 loop_until_bit_is_set(UCSR1A, UDRE1);
106                 UDR1 = c;
107         }
108
109         return 0;
110 }
111
112 int
113 stdio_get(FILE *stream)
114 {
115         return (int) getchar() & 0xff;
116 }
117
118 static FILE mystdout = FDEV_SETUP_STREAM(stdio_put, NULL, _FDEV_SETUP_WRITE);
119
120 static FILE mystdin = FDEV_SETUP_STREAM(NULL, stdio_get, _FDEV_SETUP_READ);
121
122 void
123 ao_stdio_init(void)
124 {
125         stdout = &mystdout;
126         stdin = &mystdin;
127         printf("%d stdios registered\n", ao_num_stdios);
128 }
129 #endif