cortexelf-v1: Use new memory map to access all flash and ram. Add fat.
[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 #include <ao_fat.h>
30
31 struct ao_task ball_task;
32
33 #define BALL_WIDTH      5
34 #define BALL_HEIGHT     5
35
36 static int      ball_x;
37 static int      ball_y;
38 static int      ball_dx, ball_dy;
39
40 uint8_t         ball_enable;
41
42 void
43 ao_ball(void)
44 {
45         ball_dx = 1;
46         ball_dy = 1;
47         ball_x = 0;
48         ball_y = 0;
49         for (;;) {
50                 while (!ball_enable)
51                         ao_sleep(&ball_enable);
52                 for (;;) {
53                         ao_line(&ao_vga_bitmap,
54                                 -100, -100, ball_x*2, ball_y*2,
55                                 1, AO_XOR);
56                         ao_text(&ao_vga_bitmap,
57                                 ball_x, ball_y - 10,
58                                 "Hello, Bdale!",
59                                 1, AO_XOR);
60                         ao_rect(&ao_vga_bitmap,
61                                 ball_x, ball_y,
62                                 BALL_WIDTH,
63                                 BALL_HEIGHT,
64                                 1,
65                                 AO_XOR);
66                         ao_delay(AO_MS_TO_TICKS(10));
67                         ao_rect(&ao_vga_bitmap,
68                                 ball_x, ball_y,
69                                 BALL_WIDTH,
70                                 BALL_HEIGHT,
71                                 1,
72                                 AO_XOR);
73                         ao_text(&ao_vga_bitmap,
74                                 ball_x, ball_y - 10,
75                                 "Hello, Bdale!",
76                                 1, AO_XOR);
77                         ao_line(&ao_vga_bitmap,
78                                 -100, -100, ball_x*2, ball_y*2,
79                                 1, AO_XOR);
80                         if (!ball_enable)
81                                 break;
82                         ball_x += ball_dx;
83                         ball_y += ball_dy;
84                         if (ball_x + BALL_WIDTH > AO_VGA_WIDTH) {
85                                 ball_x = AO_VGA_WIDTH - BALL_WIDTH;
86                                 ball_dx = -ball_dx;
87                         }
88                         if (ball_x < 0) {
89                                 ball_x = -ball_x;
90                                 ball_dx = -ball_dx;
91                         }
92                         if (ball_y + BALL_HEIGHT > AO_VGA_HEIGHT) {
93                                 ball_y = AO_VGA_HEIGHT - BALL_HEIGHT;
94                                 ball_dy = -ball_dy;
95                         }
96                         if (ball_y < 0) {
97                                 ball_y = -ball_y;
98                                 ball_dy = -ball_dy;
99                         }
100                 }
101         }
102 }
103
104 static void
105 ao_fb_init(void)
106 {
107         ao_rect(&ao_vga_bitmap,
108                 0, 0, AO_VGA_WIDTH, AO_VGA_HEIGHT,
109                 1, AO_COPY);
110
111         ao_rect(&ao_vga_bitmap,
112                 10, 10, 10, 10,
113                 0, AO_COPY);
114
115         ao_rect(&ao_vga_bitmap,
116                 AO_VGA_WIDTH - 20, 10, 10, 10,
117                 0, AO_COPY);
118
119         ao_rect(&ao_vga_bitmap,
120                 10, AO_VGA_HEIGHT - 20, 10, 10,
121                 0, AO_COPY);
122
123         ao_rect(&ao_vga_bitmap,
124                 AO_VGA_WIDTH - 20, AO_VGA_HEIGHT - 20, 10, 10,
125                 0, AO_COPY);
126
127         ao_text(&ao_vga_bitmap,
128                 20, 100,
129                 "Hello, Bdale!",
130                 0, AO_COPY);
131
132         ao_text(&ao_vga_bitmap,
133                 1, ao_font.ascent,
134                 "UL",
135                 0, AO_COPY);
136
137         ao_text(&ao_vga_bitmap,
138                 1, AO_VGA_HEIGHT - ao_font.descent,
139                 "BL",
140                 0, AO_COPY);
141 }
142
143 static void
144 ao_video_toggle(void)
145 {
146         ao_cmd_decimal();
147         if (ao_cmd_lex_i)
148                 ao_fb_init();
149         ao_vga_enable(ao_cmd_lex_i);
150 }
151
152 static void
153 ao_ball_toggle(void)
154 {
155         ao_cmd_decimal();
156         ball_enable = ao_cmd_lex_i;
157         ao_wakeup(&ball_enable);
158 }
159
160 static void
161 ao_ps2_read_keys(void)
162 {
163         char    c;
164
165         for (;;) {
166                 c = ao_ps2_getchar();
167                 printf("%02x %c\n", c, ' ' <= c && c < 0x7f ? c : '.');
168                 flush();
169                 if (c == ' ')
170                         break;
171         }
172 }
173
174 static void
175 ao_console_send(void)
176 {
177         char    c;
178
179         while ((c = getchar()) != '~') {
180                 ao_console_putchar(c);
181                 flush();
182         }
183 }
184
185 __code struct ao_cmds ao_demo_cmds[] = {
186         { ao_video_toggle, "V\0Toggle video" },
187         { ao_ball_toggle, "B\0Toggle ball" },
188         { ao_ps2_read_keys, "K\0Read keys from keyboard" },
189         { ao_console_send, "C\0Send data to console, end with ~" },
190         { 0, NULL }
191 };
192
193 int
194 main(void)
195 {
196         ao_clock_init();
197
198 #if HAS_STACK_GUARD
199         ao_mpu_init();
200 #endif
201
202         ao_task_init();
203         ao_serial_init();
204         ao_timer_init();
205
206         ao_spi_init();
207         ao_dma_init();
208         ao_exti_init();
209
210         ao_sdcard_init();
211         ao_fat_init();
212
213         ao_ps2_init();
214         ao_vga_init();
215         ao_console_init();
216
217         ao_cmd_init();
218
219         ao_usb_init();
220
221         ao_config_init();
222
223         ao_add_task(&ball_task, ao_ball, "ball");
224         ao_cmd_register(&ao_demo_cmds[0]);
225
226         ao_start_scheduler();
227         return 0;
228 }