cortexelf-v1: Fix clock to drive VGA at 640/480. Add sdcard, remove others
[fw/altos] / src / cortexelf-v1 / ao_cortexelf.c
1 /*
2  * Copyright © 2011 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; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 #include <ao.h>
20 #include <ao_exti.h>
21 #include <ao_profile.h>
22 #if HAS_STACK_GUARD
23 #include <ao_mpu.h>
24 #endif
25 #include <ao_ps2.h>
26 #include <ao_vga.h>
27 #include <ao_console.h>
28 #include <ao_sdcard.h>
29
30 struct ao_task ball_task;
31
32 #define BALL_WIDTH      5
33 #define BALL_HEIGHT     5
34
35 static int      ball_x;
36 static int      ball_y;
37 static int      ball_dx, ball_dy;
38
39 uint8_t         ball_enable;
40
41 void
42 ao_ball(void)
43 {
44         ball_dx = 1;
45         ball_dy = 1;
46         ball_x = 0;
47         ball_y = 0;
48         for (;;) {
49                 while (!ball_enable)
50                         ao_sleep(&ball_enable);
51                 for (;;) {
52                         ao_line(&ao_vga_bitmap,
53                                 -100, -100, ball_x*2, ball_y*2,
54                                 1, AO_XOR);
55                         ao_text(&ao_vga_bitmap,
56                                 ball_x, ball_y - 10,
57                                 "Hello, Bdale!",
58                                 1, AO_XOR);
59                         ao_rect(&ao_vga_bitmap,
60                                 ball_x, ball_y,
61                                 BALL_WIDTH,
62                                 BALL_HEIGHT,
63                                 1,
64                                 AO_XOR);
65                         ao_delay(AO_MS_TO_TICKS(10));
66                         ao_rect(&ao_vga_bitmap,
67                                 ball_x, ball_y,
68                                 BALL_WIDTH,
69                                 BALL_HEIGHT,
70                                 1,
71                                 AO_XOR);
72                         ao_text(&ao_vga_bitmap,
73                                 ball_x, ball_y - 10,
74                                 "Hello, Bdale!",
75                                 1, AO_XOR);
76                         ao_line(&ao_vga_bitmap,
77                                 -100, -100, ball_x*2, ball_y*2,
78                                 1, AO_XOR);
79                         if (!ball_enable)
80                                 break;
81                         ball_x += ball_dx;
82                         ball_y += ball_dy;
83                         if (ball_x + BALL_WIDTH > AO_VGA_WIDTH) {
84                                 ball_x = AO_VGA_WIDTH - BALL_WIDTH;
85                                 ball_dx = -ball_dx;
86                         }
87                         if (ball_x < 0) {
88                                 ball_x = -ball_x;
89                                 ball_dx = -ball_dx;
90                         }
91                         if (ball_y + BALL_HEIGHT > AO_VGA_HEIGHT) {
92                                 ball_y = AO_VGA_HEIGHT - BALL_HEIGHT;
93                                 ball_dy = -ball_dy;
94                         }
95                         if (ball_y < 0) {
96                                 ball_y = -ball_y;
97                                 ball_dy = -ball_dy;
98                         }
99                 }
100         }
101 }
102
103 static void
104 ao_fb_init(void)
105 {
106         ao_rect(&ao_vga_bitmap,
107                 0, 0, AO_VGA_WIDTH, AO_VGA_HEIGHT,
108                 1, AO_COPY);
109
110         ao_rect(&ao_vga_bitmap,
111                 10, 10, 10, 10,
112                 0, AO_COPY);
113
114         ao_rect(&ao_vga_bitmap,
115                 AO_VGA_WIDTH - 20, 10, 10, 10,
116                 0, AO_COPY);
117
118         ao_rect(&ao_vga_bitmap,
119                 10, AO_VGA_HEIGHT - 20, 10, 10,
120                 0, AO_COPY);
121
122         ao_rect(&ao_vga_bitmap,
123                 AO_VGA_WIDTH - 20, AO_VGA_HEIGHT - 20, 10, 10,
124                 0, AO_COPY);
125
126         ao_text(&ao_vga_bitmap,
127                 20, 100,
128                 "Hello, Bdale!",
129                 0, AO_COPY);
130
131         ao_text(&ao_vga_bitmap,
132                 1, ao_font.ascent,
133                 "UL",
134                 0, AO_COPY);
135
136         ao_text(&ao_vga_bitmap,
137                 1, AO_VGA_HEIGHT - ao_font.descent,
138                 "BL",
139                 0, AO_COPY);
140 }
141
142 static void
143 ao_video_toggle(void)
144 {
145         ao_cmd_decimal();
146         if (ao_cmd_lex_i)
147                 ao_fb_init();
148         ao_vga_enable(ao_cmd_lex_i);
149 }
150
151 static void
152 ao_ball_toggle(void)
153 {
154         ao_cmd_decimal();
155         ball_enable = ao_cmd_lex_i;
156         ao_wakeup(&ball_enable);
157 }
158
159 static void
160 ao_ps2_read_keys(void)
161 {
162         char    c;
163
164         for (;;) {
165                 c = ao_ps2_getchar();
166                 printf("%02x %c\n", c, ' ' <= c && c < 0x7f ? c : '.');
167                 flush();
168                 if (c == ' ')
169                         break;
170         }
171 }
172
173 static void
174 ao_console_send(void)
175 {
176         char    c;
177
178         while ((c = getchar()) != '~') {
179                 ao_console_putchar(c);
180                 flush();
181         }
182 }
183
184 __code struct ao_cmds ao_demo_cmds[] = {
185         { ao_video_toggle, "V\0Toggle video" },
186         { ao_ball_toggle, "B\0Toggle ball" },
187         { ao_ps2_read_keys, "K\0Read keys from keyboard" },
188         { ao_console_send, "C\0Send data to console, end with ~" },
189         { 0, NULL }
190 };
191
192 int
193 main(void)
194 {
195         ao_clock_init();
196
197 #if HAS_STACK_GUARD
198         ao_mpu_init();
199 #endif
200
201         ao_task_init();
202         ao_serial_init();
203         ao_timer_init();
204
205         ao_spi_init();
206         ao_dma_init();
207         ao_exti_init();
208
209         ao_sdcard_init();
210
211         ao_ps2_init();
212         ao_vga_init();
213         ao_console_init();
214
215         ao_cmd_init();
216
217         ao_usb_init();
218
219         ao_config_init();
220
221         ao_add_task(&ball_task, ao_ball, "ball");
222         ao_cmd_register(&ao_demo_cmds[0]);
223
224         ao_start_scheduler();
225         return 0;
226 }