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