4 * This file is part of BeRTOS.
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.
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.
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
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.
29 * Copyright 2010 Develer S.r.l. (http://www.develer.com/)
33 * \brief Atmel SAM3N-EK testcase
35 * \author Stefano Fedrigo <aleph@develer.com>
40 #include "hw/hw_led.h"
41 #include "hw/hw_lcd.h"
43 #include <cfg/debug.h>
47 #include <drv/timer.h>
49 #include <drv/lcd_ili9225.h>
51 #include <drv/spi_dma.h>
59 #include <icons/logo.h>
63 #include <kern/signal.h>
64 #include <kern/proc.h>
67 #define KEY_MASK (K_LEFT | K_RIGHT)
70 #define PROC_STACK_SIZE KERN_MINSTACKSIZE * 2
75 #define led_stack NULL
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);
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;
91 static bool led_blinking;
93 static void NORETURN led_process(void)
112 static void led_test(UNUSED_ARG(Bitmap *, bm))
114 led_blinking = !led_blinking;
115 sig_send(led_proc, SIG_USER0);
118 static void bouncing_logo(Bitmap *bm)
120 const long SPEED_SCALE = 1000;
121 const long GRAVITY_ACCEL = 100;
122 const long BOUNCE_ELASTICITY = 1;
123 long h = (long)(-bertos_logo.height) * SPEED_SCALE;
131 /* Gravity acceleration */
132 speed += GRAVITY_ACCEL;
134 if (h > 0 && speed > 0)
137 speed = -(speed / BOUNCE_ELASTICITY);
140 /* Update graphics */
143 (LCD_WIDTH - bertos_logo.width) / 2,
144 (LCD_HEIGHT - bertos_logo.height) / 2 + h / SPEED_SCALE,
146 text_xprintf(bm, 13, 0, TEXT_FILL | TEXT_CENTER, "Press any key to quit");
147 lcd_ili9225_blitBitmap(bm);
149 if (kbd_peek() & KEY_MASK)
154 static void screen_saver(Bitmap *bm)
164 x2 = LCD_WIDTH - i % LCD_WIDTH;
165 y2 = LCD_HEIGHT - i % LCD_HEIGHT;
168 gfx_rectDraw(bm, x1, y1, x2, y2);
169 lcd_ili9225_blitBitmap(bm);
170 if (kbd_peek() & KEY_MASK)
175 static void robot_logo(UNUSED_ARG(Bitmap *, bm))
177 lcd_ili9225_blitBitmap24(0, 0, BMP_THINKING_WIDTH, BMP_THINKING_HEIGHT, bmp_thinking);
179 while (!(kbd_peek() & KEY_MASK))
184 INLINE hptime_t get_hp_ticks(void)
186 return (timer_clock_unlocked() * TIMER_HW_CNT) + timer_hw_hpread();
189 static void NORETURN hp_process(void)
194 end = get_hp_ticks();
196 sig_send(lp_proc, SIG_USER0);
200 static void NORETURN lp_process(void)
204 start = get_hp_ticks();
205 sig_send(hp_proc, SIG_USER0);
210 static void res_process(void)
212 const char spinner[] = {'/', '-', '\\', '|'};
218 /* Show context switch (in clock cycles) */
219 c = spinner[i % countof(spinner)];
220 text_xprintf(&lcd_bitmap, 3, 0, TEXT_CENTER | TEXT_FILL, "%c Context switch %c", c, c);
221 text_xprintf(&lcd_bitmap, 5, 0, TEXT_FILL, " %lu clock cycles", end - start);
222 /* Show context switch (in usec) */
223 text_xprintf(&lcd_bitmap, 6, 0, TEXT_FILL,
225 ((end - start) * 1000000) / CPU_FREQ,
226 ((end - start) * (100000000 / CPU_FREQ)) % 100);
227 lcd_ili9225_blitBitmap(&lcd_bitmap);
229 if (kbd_peek() & KEY_MASK)
234 static void context_switch_test(Bitmap *bm)
236 const Font *old_font = bm->font;
237 gfx_setFont(&lcd_bitmap, &font_gohu);
240 text_xprintf(bm, 0, 0, TEXT_FILL,
241 "CPU: Cortex-M3 %luMHz", CPU_FREQ / 1000000);
242 text_xprintf(bm, 1, 0, TEXT_FILL, "Board: SAM3N-EK EVB");
246 gfx_setFont(&lcd_bitmap, old_font);
249 static void uptime(Bitmap *bm)
252 text_xprintf(bm, 0, 0, TEXT_FILL | TEXT_CENTER, "Uptime");
255 ticks_t clock = ticks_to_ms(timer_clock_unlocked());
257 /* Display uptime (in ticks) */
258 text_xprintf(&lcd_bitmap, 2, 0, TEXT_FILL | TEXT_CENTER,
259 "seconds: %lu", clock / 1000);
260 lcd_ili9225_blitBitmap(bm);
262 if (kbd_peek() & KEY_MASK)
270 static void setBrightness(Bitmap *bm)
275 text_xprintf(bm, 1, 0, TEXT_FILL | TEXT_CENTER, "Brightness: %d", lcd_brightness);
276 text_xprintf(bm, 3, 0, TEXT_FILL | TEXT_CENTER, "RIGHT key: change");
277 text_xprintf(bm, 4, 0, TEXT_FILL | TEXT_CENTER, "LEFT key: back ");
278 lcd_ili9225_blitBitmap(bm);
280 keymask_t mask = kbd_get();
284 else if (mask & K_RIGHT)
286 if (++lcd_brightness > LCD_BACKLIGHT_MAX)
288 lcd_setBacklight(lcd_brightness);
294 static void NORETURN soft_reset(Bitmap * bm)
301 text_xprintf(bm, 2, 0, TEXT_FILL | TEXT_CENTER, "%d", i);
302 lcd_ili9225_blitBitmap(bm);
305 text_xprintf(bm, 2, 0, TEXT_FILL | TEXT_CENTER, "REBOOT");
306 lcd_ili9225_blitBitmap(bm);
309 /* Perform a software reset request */
310 HWREG(NVIC_APINT) = NVIC_APINT_VECTKEY | NVIC_APINT_SYSRESETREQ;
315 static struct MenuItem main_items[] =
317 { (const_iptr_t)"LED blinking", 0, (MenuHook)led_test, (iptr_t)&lcd_bitmap },
318 { (const_iptr_t)"Graphics demo", 0, (MenuHook)robot_logo, (iptr_t)&lcd_bitmap },
319 { (const_iptr_t)"Bouncing logo", 0, (MenuHook)bouncing_logo, (iptr_t)&lcd_bitmap },
320 { (const_iptr_t)"Screen saver demo", 0, (MenuHook)screen_saver, (iptr_t)&lcd_bitmap },
321 { (const_iptr_t)"Scheduling test", 0, (MenuHook)context_switch_test, (iptr_t)&lcd_bitmap },
322 { (const_iptr_t)"Show uptime", 0, (MenuHook)uptime, (iptr_t)&lcd_bitmap },
323 { (const_iptr_t)"Display brightness", 0, (MenuHook)setBrightness, (iptr_t)&lcd_bitmap },
324 { (const_iptr_t)"Reboot", 0, (MenuHook)soft_reset, (iptr_t)&lcd_bitmap },
325 { (const_iptr_t)0, 0, NULL, (iptr_t)0 }
327 static struct Menu main_menu = { main_items, "BeRTOS", MF_STICKY | MF_SAVESEL, &lcd_bitmap, 0, lcd_ili9225_blitBitmap };
340 spi_dma_setclock(LCD_SPICLOCK);
341 lcd_ili9225_init(&spi.fd);
342 LCD_BACKLIGHT_INIT();
343 lcd_setBacklight(lcd_brightness);
345 gfx_bitmapInit(&lcd_bitmap, raster, LCD_WIDTH, LCD_HEIGHT);
346 gfx_setFont(&lcd_bitmap, &font_luBS14);
347 lcd_ili9225_blitBitmap(&lcd_bitmap);
351 hp_proc = proc_new(hp_process, NULL, PROC_STACK_SIZE, hp_stack);
352 lp_proc = proc_new(lp_process, NULL, PROC_STACK_SIZE, lp_stack);
353 led_proc = proc_new(led_process, NULL, PROC_STACK_SIZE, led_stack);
355 proc_setPri(hp_proc, 2);
356 proc_setPri(lp_proc, 1);
358 lcd_ili9225_blitBitmap24(0, 50, BMP_LOGO_WIDTH, BMP_LOGO_HEIGHT, bmp_logo);
363 menu_handle(&main_menu);