src-avr: Control log with pin on companion connector
[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 (ao_cur_stdio >= ao_num_stdios || !ao_stdios[ao_cur_stdio].putchar)
41                 return;
42         if (c == '\n')
43                 (*ao_stdios[ao_cur_stdio].putchar)('\r');
44         (*ao_stdios[ao_cur_stdio].putchar)(c);
45 }
46
47 void
48 flush(void)
49 {
50         if (ao_stdios[ao_cur_stdio].flush)
51                 ao_stdios[ao_cur_stdio].flush();
52 }
53
54 __xdata uint8_t ao_stdin_ready;
55
56 char
57 ao_getchar(void) __reentrant __critical
58 {
59         char c;
60         int8_t stdio = ao_cur_stdio;
61
62         for (;;) {
63                 if (stdio < ao_num_stdios) {
64                         c = ao_stdios[stdio].pollchar();
65                         if (c != AO_READ_AGAIN)
66                                 break;
67                 }
68                 if (++stdio >= ao_num_stdios)
69                         stdio = 0;
70                 if (stdio == ao_cur_stdio)
71                         ao_sleep(&ao_stdin_ready);
72         }
73         ao_cur_stdio = stdio;
74         return c;
75 }
76
77 uint8_t
78 ao_echo(void)
79 {
80         return ao_stdios[ao_cur_stdio].echo;
81 }
82
83 int8_t
84 ao_add_stdio(char (*pollchar)(void),
85              void (*putchar)(char),
86              void (*flush)(void)) __reentrant
87 {
88         if (ao_num_stdios == AO_NUM_STDIOS)
89                 ao_panic(AO_PANIC_STDIO);
90         ao_stdios[ao_num_stdios].pollchar = pollchar;
91         ao_stdios[ao_num_stdios].putchar = putchar;
92         ao_stdios[ao_num_stdios].flush = flush;
93         ao_stdios[ao_num_stdios].echo = 1;
94         ao_wakeup(&ao_stdin_ready);
95         return ao_num_stdios++;
96 }
97
98 #ifdef AVR
99 int
100 stdio_put(char c, FILE *stream)
101 {
102 #if 1
103         if (ao_cur_task && ao_num_stdios)
104                 putchar(c);
105         else
106 #endif
107         {
108                 if (c == '\n')
109                         stdio_put('\r', stream);
110                 loop_until_bit_is_set(UCSR1A, UDRE1);
111                 UDR1 = c;
112         }
113
114         return 0;
115 }
116
117 int
118 stdio_get(FILE *stream)
119 {
120         return (int) getchar() & 0xff;
121 }
122
123 static FILE mystdout = FDEV_SETUP_STREAM(stdio_put, NULL, _FDEV_SETUP_WRITE);
124
125 static FILE mystdin = FDEV_SETUP_STREAM(NULL, stdio_get, _FDEV_SETUP_READ);
126
127 void
128 ao_stdio_init(void)
129 {
130         stdout = &mystdout;
131         stdin = &mystdin;
132 }
133 #endif