events: do not inline event_select()
[bertos.git] / boards / sam3x-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 2011 Develer S.r.l. (http://www.develer.com/)
30  *
31  * -->
32  *
33  * \brief Atmel SAM3X-EK simple demo
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 #include <cpu/irq.h>
45 #include <drv/timer.h>
46 #include <drv/kbd.h>
47 #include <drv/lcd_hx8347.h>
48 #include <gfx/gfx.h>
49 #include <gfx/font.h>
50 #include <gfx/text.h>
51 #include <gui/menu.h>
52 #include <icons/logo.h>
53 #include <io/kfile.h>
54 #include <kern/signal.h>
55 #include <kern/proc.h>
56
57 #warning FIX PMC
58
59 // Keyboard
60 #define KEY_MASK (K_LEFT | K_RIGHT)
61
62 // Kernel
63 #define PROC_STACK_SIZE KERN_MINSTACKSIZE * 2
64
65 #if CONFIG_KERN_HEAP
66         #define hp_stack NULL
67         #define lp_stack NULL
68         #define led_stack NULL
69 #else
70         static PROC_DEFINE_STACK(hp_stack, PROC_STACK_SIZE);
71         static PROC_DEFINE_STACK(lp_stack, PROC_STACK_SIZE);
72         static PROC_DEFINE_STACK(led_stack, PROC_STACK_SIZE);
73 #endif
74
75 static uint8_t raster[RAST_SIZE(LCD_WIDTH, LCD_HEIGHT)];
76 static Bitmap lcd_bitmap;
77 extern Font font_gohu;
78 static int lcd_brightness = LCD_BACKLIGHT_MAX;
79 static Process *hp_proc, *lp_proc, *led_proc;
80 static hptime_t start, end;
81
82
83 static bool led_blinking;
84
85 static void NORETURN led_process(void)
86 {
87         unsigned i;
88
89         for (i = 0; ; i++)
90         {
91                 if (!led_blinking)
92                 {
93                         LED_OFF(0);
94                         LED_OFF(1);
95                         LED_OFF(2);
96                         sig_wait(SIG_USER0);
97                 }
98                 LED_ON(i % 3);
99                 LED_OFF((i-1) % 3);
100                 timer_delay(200);
101         }
102 }
103
104 static void led_test(UNUSED_ARG(Bitmap *, bm))
105 {
106         led_blinking = !led_blinking;
107         sig_send(led_proc, SIG_USER0);
108 }
109
110 static void bouncing_logo(Bitmap *bm)
111 {
112         const long SPEED_SCALE = 1000;
113         const long GRAVITY_ACCEL = 100;
114         const long BOUNCE_ELASTICITY = 1;
115         long h = (long)(-bertos_logo.height) * SPEED_SCALE;
116         long speed = 0, i;
117
118         for (i = 0; ; i++)
119         {
120                 /* Move */
121                 h += speed;
122
123                 /* Gravity acceleration */
124                 speed += GRAVITY_ACCEL;
125
126                 if (h > 0 && speed > 0)
127                 {
128                         /* Bounce */
129                         speed = -(speed / BOUNCE_ELASTICITY);
130
131                 }
132                 /* Update graphics */
133                 gfx_bitmapClear(bm);
134                 gfx_blitImage(bm,
135                         (LCD_WIDTH - bertos_logo.width) / 2,
136                         (LCD_HEIGHT - bertos_logo.height) / 2 + h / SPEED_SCALE,
137                         &bertos_logo);
138                 text_xprintf(bm, 13, 0, TEXT_FILL | TEXT_CENTER, "Press any key to quit");
139                 lcd_hx8347_blitBitmap(bm);
140                 timer_delay(15);
141                 if (kbd_peek() & KEY_MASK)
142                         break;
143         }
144 }
145
146 static void screen_saver(Bitmap *bm)
147 {
148         int x1, y1, x2, y2;
149         int i;
150
151         for (i = 0; ; i++)
152         {
153                 x1 = i % LCD_WIDTH;
154                 y1 = i % LCD_HEIGHT;
155
156                 x2 = LCD_WIDTH - i % LCD_WIDTH;
157                 y2 = LCD_HEIGHT - i % LCD_HEIGHT;
158
159                 gfx_bitmapClear(bm);
160                 gfx_rectDraw(bm, x1, y1, x2, y2);
161                 lcd_hx8347_blitBitmap(bm);
162                 if (kbd_peek() & KEY_MASK)
163                         break;
164         }
165 }
166
167 static void show_logo(UNUSED_ARG(Bitmap *, bm))
168 {
169         lcd_hx8347_blitBitmap24(10, 52, BMP_LOGO_WIDTH, BMP_LOGO_HEIGHT, bmp_logo);
170
171         while (!(kbd_peek() & KEY_MASK))
172                 timer_delay(50);
173 }
174
175
176 INLINE hptime_t get_hp_ticks(void)
177 {
178         return (timer_clock_unlocked() * TIMER_HW_CNT) + timer_hw_hpread();
179 }
180
181 static void NORETURN hp_process(void)
182 {
183         while (1)
184         {
185                 sig_wait(SIG_USER0);
186                 end = get_hp_ticks();
187                 timer_delay(100);
188                 sig_send(lp_proc, SIG_USER0);
189         }
190 }
191
192 static void NORETURN lp_process(void)
193 {
194         while (1)
195         {
196                 start = get_hp_ticks();
197                 sig_send(hp_proc, SIG_USER0);
198                 sig_wait(SIG_USER0);
199         }
200 }
201
202 static void res_process(void)
203 {
204         const char spinner[] = {'/', '-', '\\', '|'};
205         int i;
206         char c;
207
208         for (i = 0; ; i++)
209         {
210                 /* Show context switch (in clock cycles) */
211                 c = spinner[i % countof(spinner)];
212                 text_xprintf(&lcd_bitmap, 3, 0, TEXT_CENTER | TEXT_FILL, "%c Context switch %c", c, c);
213                 text_xprintf(&lcd_bitmap, 5, 0, TEXT_FILL, " %lu clock cycles", end - start);
214                 /* Show context switch (in usec) */
215                 text_xprintf(&lcd_bitmap, 6, 0, TEXT_FILL,
216                         " %lu.%lu usec",
217                                 ((end - start) * 1000000) / CPU_FREQ,
218                                 ((end - start) * (100000000 / CPU_FREQ)) % 100);
219                 lcd_hx8347_blitBitmap(&lcd_bitmap);
220                 timer_delay(5);
221                 if (kbd_peek() & KEY_MASK)
222                         break;
223         }
224 }
225
226 static void context_switch_test(Bitmap *bm)
227 {
228         const Font *old_font = bm->font;
229         gfx_setFont(&lcd_bitmap, &font_gohu);
230
231         gfx_bitmapClear(bm);
232         text_xprintf(bm, 0, 0, TEXT_FILL,
233                         "CPU: Cortex-M3 %luMHz", CPU_FREQ / 1000000);
234         text_xprintf(bm, 1, 0, TEXT_FILL, "Board: SAM3X-EK EVB");
235
236         res_process();
237
238         gfx_setFont(&lcd_bitmap, old_font);
239 }
240
241 static void uptime(Bitmap *bm)
242 {
243         gfx_bitmapClear(bm);
244         text_xprintf(bm, 0, 0, TEXT_FILL | TEXT_CENTER, "Uptime");
245         while (1)
246         {
247                 ticks_t clock = ticks_to_ms(timer_clock_unlocked());
248
249                 /* Display uptime (in ticks) */
250                 text_xprintf(&lcd_bitmap, 2, 0, TEXT_FILL | TEXT_CENTER,
251                                 "seconds: %lu", clock / 1000);
252                 lcd_hx8347_blitBitmap(bm);
253                 timer_delay(5);
254                 if (kbd_peek() & KEY_MASK)
255                         break;
256         }
257 }
258
259 /*
260  * Lcd
261  */
262 static void setBrightness(Bitmap *bm)
263 {
264         while (1)
265         {
266                 gfx_bitmapClear(bm);
267                 text_xprintf(bm, 1, 0, TEXT_FILL | TEXT_CENTER, "Brightness: %d", lcd_brightness);
268                 text_xprintf(bm, 3, 0, TEXT_FILL | TEXT_CENTER, "RIGHT key: change");
269                 text_xprintf(bm, 4, 0, TEXT_FILL | TEXT_CENTER, "LEFT  key: back  ");
270                 lcd_hx8347_blitBitmap(bm);
271
272                 keymask_t mask = kbd_get();
273
274                 if (mask & K_LEFT)
275                         break;
276                 else if (mask & K_RIGHT)
277                 {
278                         if (++lcd_brightness > LCD_BACKLIGHT_MAX)
279                                 lcd_brightness = 0;
280                         lcd_setBacklight(lcd_brightness);
281                 }
282         }
283 }
284
285
286 static void NORETURN soft_reset(Bitmap * bm)
287 {
288         int i;
289
290         gfx_bitmapClear(bm);
291         for (i = 5; i; --i)
292         {
293                 text_xprintf(bm, 2, 0, TEXT_FILL | TEXT_CENTER, "%d", i);
294                 lcd_hx8347_blitBitmap(bm);
295                 timer_delay(1000);
296         }
297         text_xprintf(bm, 2, 0, TEXT_FILL | TEXT_CENTER, "REBOOT");
298         lcd_hx8347_blitBitmap(bm);
299         timer_delay(1000);
300
301         /* Perform a software reset request */
302         HWREG(NVIC_APINT) = NVIC_APINT_VECTKEY | NVIC_APINT_SYSRESETREQ;
303         UNREACHABLE();
304 }
305
306
307 static struct MenuItem main_items[] =
308 {
309         { (const_iptr_t)"LED blinking", 0, (MenuHook)led_test, (iptr_t)&lcd_bitmap },
310         { (const_iptr_t)"Graphics demo", 0, (MenuHook)show_logo, (iptr_t)&lcd_bitmap },
311         { (const_iptr_t)"Bouncing logo", 0, (MenuHook)bouncing_logo, (iptr_t)&lcd_bitmap },
312         { (const_iptr_t)"Screen saver demo", 0, (MenuHook)screen_saver, (iptr_t)&lcd_bitmap },
313         { (const_iptr_t)"Scheduling test", 0, (MenuHook)context_switch_test, (iptr_t)&lcd_bitmap },
314         { (const_iptr_t)"Show uptime", 0, (MenuHook)uptime, (iptr_t)&lcd_bitmap },
315         { (const_iptr_t)"Display brightness", 0, (MenuHook)setBrightness, (iptr_t)&lcd_bitmap },
316         { (const_iptr_t)"Reboot", 0, (MenuHook)soft_reset, (iptr_t)&lcd_bitmap },
317         { (const_iptr_t)0, 0, NULL, (iptr_t)0 }
318 };
319 static struct Menu main_menu = { main_items, "BeRTOS", MF_STICKY | MF_SAVESEL, &lcd_bitmap, 0, lcd_hx8347_blitBitmap };
320
321
322 int main(void)
323 {
324         IRQ_ENABLE;
325
326         kdbg_init();
327         LED_INIT();
328         timer_init();
329         proc_init();
330
331         lcd_hx8347_init();
332         lcd_setBacklight(lcd_brightness);
333
334         gfx_bitmapInit(&lcd_bitmap, raster, LCD_WIDTH, LCD_HEIGHT);
335         gfx_setFont(&lcd_bitmap, &font_luBS14);
336         lcd_hx8347_blitBitmap(&lcd_bitmap);
337
338         kbd_init();
339
340         hp_proc = proc_new(hp_process, NULL, PROC_STACK_SIZE, hp_stack);
341         lp_proc = proc_new(lp_process, NULL, PROC_STACK_SIZE, lp_stack);
342         led_proc = proc_new(led_process, NULL, PROC_STACK_SIZE, led_stack);
343
344         proc_setPri(hp_proc, 2);
345         proc_setPri(lp_proc, 1);
346
347         lcd_hx8347_blitBitmap24(10, 52, BMP_LOGO_WIDTH, BMP_LOGO_HEIGHT, bmp_logo);
348         timer_delay(3000);
349
350         while (1)
351         {
352                 menu_handle(&main_menu);
353                 cpu_relax();
354         }
355 }