d79775e8d523fe1b8a408624b81719aecfa53197
[bertos.git] / boards / sam3n-ek / examples / display / main.c
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2010 Develer S.r.l. (http://www.develer.com/)
30  *
31  * -->
32  *
33  * \brief Atmel SAM3N-EK testcase
34  *
35  * \author Stefano Fedrigo <aleph@develer.com>
36  */
37
38 #include "bitmaps.h"
39
40 #include "hw/hw_led.h"
41 #include "hw/hw_lcd.h"
42
43 #include <cfg/debug.h>
44
45 #include <cpu/irq.h>
46
47 #include <drv/timer.h>
48 #include <drv/kbd.h>
49 #include <drv/lcd_ili9225.h>
50
51 #include <cpu/arm/drv/spi_dma_at91.h>
52
53 #include <gfx/gfx.h>
54 #include <gfx/font.h>
55 #include <gfx/text.h>
56
57 #include <gui/menu.h>
58
59 #include <icons/logo.h>
60
61 #include <io/kfile.h>
62
63 #include <kern/signal.h>
64 #include <kern/proc.h>
65
66 // Keyboard
67 #define KEY_MASK (K_LEFT | K_RIGHT)
68
69 // Kernel
70 #define PROC_STACK_SIZE KERN_MINSTACKSIZE * 2
71
72 #if CONFIG_KERN_HEAP
73         #define hp_stack NULL
74         #define lp_stack NULL
75         #define led_stack NULL
76 #else
77         static PROC_DEFINE_STACK(hp_stack, PROC_STACK_SIZE);
78         static PROC_DEFINE_STACK(lp_stack, PROC_STACK_SIZE);
79         static PROC_DEFINE_STACK(led_stack, PROC_STACK_SIZE);
80 #endif
81
82 struct SpiDmaAt91 spi;
83 static uint8_t raster[RAST_SIZE(LCD_WIDTH, LCD_HEIGHT)];
84 static Bitmap lcd_bitmap;
85 extern Font font_gohu;
86 static int lcd_brightness = LCD_BACKLIGHT_MAX;
87 static Process *hp_proc, *lp_proc, *led_proc;
88 static hptime_t start, end;
89
90
91 INLINE void led_on(int idx)
92 {
93         if (idx == 0)
94                 LED_BLUE_ON();
95         else if (idx == 1)
96                 LED_GREEN_ON();
97         else if (idx == 2)
98                 LED_AMBER_ON();
99 }
100
101 INLINE void led_off(int idx)
102 {
103         if (idx == 0)
104                 LED_BLUE_OFF();
105         else if (idx == 1)
106                 LED_GREEN_OFF();
107         else if (idx == 2)
108                 LED_AMBER_OFF();
109 }
110
111 static bool led_blinking;
112
113 static void NORETURN led_process(void)
114 {
115         unsigned i;
116
117         for (i = 0; ; i++)
118         {
119                 if (!led_blinking)
120                 {
121                         led_off(0);
122                         led_off(1);
123                         led_off(2);
124                         sig_wait(SIG_USER0);
125                 }
126                 led_on(i % 3);
127                 led_off((i-1) % 3);
128                 timer_delay(100);
129         }
130 }
131
132 static void led_test(UNUSED_ARG(Bitmap *, bm))
133 {
134         led_blinking = !led_blinking;
135         sig_send(led_proc, SIG_USER0);
136 }
137
138 static void bouncing_logo(Bitmap *bm)
139 {
140         const long SPEED_SCALE = 1000;
141         const long GRAVITY_ACCEL = 100;
142         const long BOUNCE_ELASTICITY = 1;
143         long h = (long)(-bertos_logo.height) * SPEED_SCALE;
144         long speed = 0, i;
145
146         for (i = 0; ; i++)
147         {
148                 /* Move */
149                 h += speed;
150
151                 /* Gravity acceleration */
152                 speed += GRAVITY_ACCEL;
153
154                 if (h > 0 && speed > 0)
155                 {
156                         /* Bounce */
157                         speed = -(speed / BOUNCE_ELASTICITY);
158
159                 }
160                 /* Update graphics */
161                 gfx_bitmapClear(bm);
162                 gfx_blitImage(bm,
163                         (LCD_WIDTH - bertos_logo.width) / 2,
164                         (LCD_HEIGHT - bertos_logo.height) / 2 + h / SPEED_SCALE,
165                         &bertos_logo);
166                 text_xprintf(bm, 13, 0, TEXT_FILL | TEXT_CENTER, "Press any key to quit");
167                 lcd_ili9225_blitBitmap(bm);
168                 timer_delay(15);
169                 if (kbd_peek() & KEY_MASK)
170                         break;
171         }
172 }
173
174 static void screen_saver(Bitmap *bm)
175 {
176         int x1, y1, x2, y2;
177         int i;
178
179         for (i = 0; ; i++)
180         {
181                 x1 = i % LCD_WIDTH;
182                 y1 = i % LCD_HEIGHT;
183
184                 x2 = LCD_WIDTH - i % LCD_WIDTH;
185                 y2 = LCD_HEIGHT - i % LCD_HEIGHT;
186
187                 gfx_bitmapClear(bm);
188                 gfx_rectDraw(bm, x1, y1, x2, y2);
189                 lcd_ili9225_blitBitmap(bm);
190                 if (kbd_peek() & KEY_MASK)
191                         break;
192         }
193 }
194
195 static void robot_logo(UNUSED_ARG(Bitmap *, bm))
196 {
197         lcd_ili9225_blitBitmap24(0, 0, BMP_THINKING_WIDTH, BMP_THINKING_HEIGHT, bmp_thinking);
198
199         while (!(kbd_peek() & KEY_MASK))
200                 timer_delay(50);
201 }
202
203
204 INLINE hptime_t get_hp_ticks(void)
205 {
206         return (timer_clock_unlocked() * TIMER_HW_CNT) + timer_hw_hpread();
207 }
208
209 static void NORETURN hp_process(void)
210 {
211         while (1)
212         {
213                 sig_wait(SIG_USER0);
214                 end = get_hp_ticks();
215                 timer_delay(100);
216                 sig_send(lp_proc, SIG_USER0);
217         }
218 }
219
220 static void NORETURN lp_process(void)
221 {
222         while (1)
223         {
224                 start = get_hp_ticks();
225                 sig_send(hp_proc, SIG_USER0);
226                 sig_wait(SIG_USER0);
227         }
228 }
229
230 static void res_process(void)
231 {
232         const char spinner[] = {'/', '-', '\\', '|'};
233         int i;
234         char c;
235
236         for (i = 0; ; i++)
237         {
238                 /* Show context switch (in clock cycles) */
239                 c = spinner[i % countof(spinner)];
240                 text_xprintf(&lcd_bitmap, 3, 0, TEXT_CENTER | TEXT_FILL, "%c Context switch %c", c, c);
241                 text_xprintf(&lcd_bitmap, 5, 0, TEXT_FILL, " %lu clock cycles", end - start);
242                 /* Show context switch (in usec) */
243                 text_xprintf(&lcd_bitmap, 6, 0, TEXT_FILL,
244                         " %lu.%lu usec",
245                                 ((end - start) * 1000000) / CPU_FREQ,
246                                 ((end - start) * (100000000 / CPU_FREQ)) % 100);
247                 lcd_ili9225_blitBitmap(&lcd_bitmap);
248                 timer_delay(5);
249                 if (kbd_peek() & KEY_MASK)
250                         break;
251         }
252 }
253
254 static void context_switch_test(Bitmap *bm)
255 {
256         const Font *old_font = bm->font;
257         gfx_setFont(&lcd_bitmap, &font_gohu);
258
259         gfx_bitmapClear(bm);
260         text_xprintf(bm, 0, 0, TEXT_FILL,
261                         "CPU: Cortex-M3 %luMHz", CPU_FREQ / 1000000);
262         text_xprintf(bm, 1, 0, TEXT_FILL, "Board: SAM3N-EK EVB");
263
264         res_process();
265
266         gfx_setFont(&lcd_bitmap, old_font);
267 }
268
269 static void uptime(Bitmap *bm)
270 {
271         gfx_bitmapClear(bm);
272         text_xprintf(bm, 0, 0, TEXT_FILL | TEXT_CENTER, "Uptime");
273         while (1)
274         {
275                 ticks_t clock = ticks_to_ms(timer_clock_unlocked());
276
277                 /* Display uptime (in ticks) */
278                 text_xprintf(&lcd_bitmap, 2, 0, TEXT_FILL | TEXT_CENTER,
279                                 "seconds: %lu", clock / 1000);
280                 lcd_ili9225_blitBitmap(bm);
281                 timer_delay(5);
282                 if (kbd_peek() & KEY_MASK)
283                         break;
284         }
285 }
286
287 /*
288  * Lcd
289  */
290 static void setBrightness(Bitmap *bm)
291 {
292         while (1)
293         {
294                 gfx_bitmapClear(bm);
295                 text_xprintf(bm, 1, 0, TEXT_FILL | TEXT_CENTER, "Brightness: %d", lcd_brightness);
296                 text_xprintf(bm, 3, 0, TEXT_FILL | TEXT_CENTER, "RIGHT key: change");
297                 text_xprintf(bm, 4, 0, TEXT_FILL | TEXT_CENTER, "LEFT  key: back  ");
298                 lcd_ili9225_blitBitmap(bm);
299
300                 keymask_t mask = kbd_get();
301
302                 if (mask & K_LEFT)
303                         break;
304                 else if (mask & K_RIGHT)
305                 {
306                         if (++lcd_brightness > LCD_BACKLIGHT_MAX)
307                                 lcd_brightness = 0;
308                         lcd_setBacklight(lcd_brightness);
309                 }
310         }
311 }
312
313
314 static void NORETURN soft_reset(Bitmap * bm)
315 {
316         int i;
317
318         gfx_bitmapClear(bm);
319         for (i = 5; i; --i)
320         {
321                 text_xprintf(bm, 2, 0, TEXT_FILL | TEXT_CENTER, "%d", i);
322                 lcd_ili9225_blitBitmap(bm);
323                 timer_delay(1000);
324         }
325         text_xprintf(bm, 2, 0, TEXT_FILL | TEXT_CENTER, "REBOOT");
326         lcd_ili9225_blitBitmap(bm);
327         timer_delay(1000);
328
329         /* Perform a software reset request */
330         HWREG(NVIC_APINT) = NVIC_APINT_VECTKEY | NVIC_APINT_SYSRESETREQ;
331         UNREACHABLE();
332 }
333
334
335 static struct MenuItem main_items[] =
336 {
337         { (const_iptr_t)"LED blinking", 0, (MenuHook)led_test, (iptr_t)&lcd_bitmap },
338         { (const_iptr_t)"Graphics demo", 0, (MenuHook)robot_logo, (iptr_t)&lcd_bitmap },
339         { (const_iptr_t)"Bouncing logo", 0, (MenuHook)bouncing_logo, (iptr_t)&lcd_bitmap },
340         { (const_iptr_t)"Screen saver demo", 0, (MenuHook)screen_saver, (iptr_t)&lcd_bitmap },
341         { (const_iptr_t)"Scheduling test", 0, (MenuHook)context_switch_test, (iptr_t)&lcd_bitmap },
342         { (const_iptr_t)"Show uptime", 0, (MenuHook)uptime, (iptr_t)&lcd_bitmap },
343         { (const_iptr_t)"Display brightness", 0, (MenuHook)setBrightness, (iptr_t)&lcd_bitmap },
344         { (const_iptr_t)"Reboot", 0, (MenuHook)soft_reset, (iptr_t)&lcd_bitmap },
345         { (const_iptr_t)0, 0, NULL, (iptr_t)0 }
346 };
347 static struct Menu main_menu = { main_items, "BeRTOS", MF_STICKY | MF_SAVESEL, &lcd_bitmap, 0, lcd_ili9225_blitBitmap };
348
349
350 int main(void)
351 {
352         IRQ_ENABLE;
353
354         kdbg_init();
355         LED_INIT();
356         timer_init();
357         proc_init();
358
359         spi_dma_init(&spi);
360         spi_dma_setclock(LCD_SPICLOCK);
361         lcd_ili9225_init(&spi.fd);
362         LCD_BACKLIGHT_INIT();
363         lcd_setBacklight(lcd_brightness);
364
365         gfx_bitmapInit(&lcd_bitmap, raster, LCD_WIDTH, LCD_HEIGHT);
366         gfx_setFont(&lcd_bitmap, &font_luBS14);
367         lcd_ili9225_blitBitmap(&lcd_bitmap);
368
369         kbd_init();
370
371         hp_proc = proc_new(hp_process, NULL, PROC_STACK_SIZE, hp_stack);
372         lp_proc = proc_new(lp_process, NULL, PROC_STACK_SIZE, lp_stack);
373         led_proc = proc_new(led_process, NULL, PROC_STACK_SIZE, led_stack);
374
375         proc_setPri(hp_proc, 2);
376         proc_setPri(lp_proc, 1);
377
378         lcd_ili9225_blitBitmap24(0, 50, BMP_LOGO_WIDTH, BMP_LOGO_HEIGHT, bmp_logo);
379         timer_delay(3000);
380
381         while (1)
382         {
383                 menu_handle(&main_menu);
384                 cpu_relax();
385         }
386 }