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