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