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