Slow bounging logo.
[bertos.git] / examples / lm3s8962 / lm3s8962.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 LM3S8962 Cortex-M3 testcase
34  *
35  * \author Andrea Righi <arighi@develer.com>
36  */
37
38 #include <cpu/irq.h>
39 #include <drv/timer.h>
40 #include <drv/ser.h>
41 #include <drv/kbd.h>
42 #include <drv/lcd_rit128x96.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 <stdio.h>
49
50 #include "cfg/compiler.h"
51 #include "cfg/cfg_gfx.h"
52
53 #include "hw/hw_rit128x96.h"
54
55 #define PROC_STACK_SIZE KERN_MINSTACKSIZE * 2
56
57 #if CONFIG_KERN_HEAP
58 #define hp_stack NULL
59 #define lp_stack NULL
60 #define ser_stack NULL
61 #define led_stack NULL
62 #else
63 static PROC_DEFINE_STACK(hp_stack, PROC_STACK_SIZE);
64 static PROC_DEFINE_STACK(lp_stack, PROC_STACK_SIZE);
65 static PROC_DEFINE_STACK(ser_stack, PROC_STACK_SIZE);
66 static PROC_DEFINE_STACK(led_stack, PROC_STACK_SIZE);
67 #endif
68
69 extern Font font_gohu;
70 static uint8_t raster[RAST_SIZE(LCD_WIDTH, LCD_HEIGHT)];
71 static Bitmap lcd_bitmap;
72
73 #define KEY_MASK (K_UP | K_DOWN | K_LEFT | K_RIGHT | K_OK)
74
75 static Process *hp_proc, *lp_proc, *led_proc;
76 static hptime_t start, end;
77
78 static Serial ser_port;
79
80 static void led_init(void)
81 {
82         /* Enable the GPIO port that is used for the on-board LED */
83         SYSCTL_RCGC2_R |= SYSCTL_RCGC2_GPIOF;
84         /*
85          * Perform a dummy read to insert a few cycles delay before enabling
86          * the peripheral.
87          */
88         (void)SYSCTL_RCGC2_R;
89         /* Enable the GPIO pin for the LED */
90         GPIO_PORTF_DIR_R = 0x01;
91         GPIO_PORTF_DEN_R = 0x01;
92 }
93
94 INLINE void led_on(void)
95 {
96         GPIO_PORTF_DATA_R |= 0x01;
97 }
98
99 INLINE void led_off(void)
100 {
101         GPIO_PORTF_DATA_R &= ~0x01;
102 }
103
104 static bool led_blinking;
105
106 static void NORETURN led_process(void)
107 {
108         int i;
109
110         for (i = 0; ; i++)
111         {
112                 if (!led_blinking)
113                 {
114                         led_off();
115                         sig_wait(SIG_USER0);
116                 }
117                 if (i & 1)
118                         led_on();
119                 else
120                         led_off();
121                 timer_delay(50);
122         }
123 }
124
125 static void led_test(UNUSED_ARG(Bitmap *, bm))
126 {
127         led_blinking = !led_blinking;
128         sig_send(led_proc, SIG_USER0);
129 }
130
131 static void bouncing_logo(Bitmap *bm)
132 {
133         const long SPEED_SCALE = 1000;
134         const long GRAVITY_ACCEL = 100;
135         const long BOUNCE_ELASTICITY = 1;
136         long h = (long)(-bertos_logo.height) * SPEED_SCALE;
137         long speed = 0, i;
138
139         for (i = 0; ; i++)
140         {
141                 /* Move */
142                 h += speed;
143
144                 /* Gravity acceleration */
145                 speed += GRAVITY_ACCEL;
146
147                 if (h > 0 && speed > 0)
148                 {
149                         /* Bounce */
150                         speed = -(speed / BOUNCE_ELASTICITY);
151
152                 }
153                 /* Update graphics */
154                 gfx_bitmapClear(bm);
155                 gfx_blitImage(bm,
156                         (LCD_WIDTH - bertos_logo.width) / 2,
157                         (LCD_HEIGHT - bertos_logo.height) / 2 + h / SPEED_SCALE,
158                         &bertos_logo);
159                 text_xprintf(bm, 7, 0, TEXT_FILL | TEXT_CENTER, "Press SELECT to quit");
160                 rit128x96_blitBitmap(bm);
161                 timer_delay(15);
162                 if (kbd_peek() & KEY_MASK)
163                         break;
164         }
165 }
166
167 static void screen_saver(Bitmap *bm)
168 {
169         int x1, y1, x2, y2;
170         int i;
171
172         for (i = 0; ; i++)
173         {
174                 x1 = i % LCD_WIDTH;
175                 y1 = i % LCD_HEIGHT;
176
177                 x2 = LCD_WIDTH - i % LCD_WIDTH;
178                 y2 = LCD_HEIGHT - i % LCD_HEIGHT;
179
180                 gfx_bitmapClear(bm);
181                 gfx_rectDraw(bm, x1, y1, x2, y2);
182                 rit128x96_blitBitmap(bm);
183                 if (kbd_peek() & KEY_MASK)
184                         break;
185         }
186 }
187
188 INLINE hptime_t get_hp_ticks(void)
189 {
190         return (TIMER_HW_CNT - timer_hw_hpread()) +
191                         timer_clock_unlocked() * TIMER_HW_CNT;
192 }
193
194 static void NORETURN hp_process(void)
195 {
196         while (1)
197         {
198                 sig_wait(SIG_USER0);
199                 end = get_hp_ticks();
200                 timer_delay(100);
201                 sig_send(lp_proc, SIG_USER0);
202         }
203 }
204
205 static void NORETURN lp_process(void)
206 {
207         while (1)
208         {
209                 start = get_hp_ticks();
210                 sig_send(hp_proc, SIG_USER0);
211                 sig_wait(SIG_USER0);
212         }
213 }
214
215 static void res_process(void)
216 {
217         const char spinner[] = {'/', '-', '\\', '|'};
218         int i;
219         char c;
220
221         for (i = 0; ; i++)
222         {
223                 /* Show context switch (in clock cycles) */
224                 c = spinner[i % countof(spinner)];
225                 text_xprintf(&lcd_bitmap, 3, 0, TEXT_CENTER | TEXT_FILL, "%c Context switch %c", c, c);
226                 text_xprintf(&lcd_bitmap, 5, 0, TEXT_FILL, " %lu clock cycles", end - start);
227                 /* Show context switch (in usec) */
228                 text_xprintf(&lcd_bitmap, 6, 0, TEXT_FILL,
229                         " %lu.%lu usec",
230                                 ((end - start) * 1000000) / CPU_FREQ,
231                                 ((end - start) * (100000000 / CPU_FREQ)) % 100);
232                 rit128x96_blitBitmap(&lcd_bitmap);
233                 timer_delay(5);
234                 if (kbd_peek() & KEY_MASK)
235                         break;
236         }
237 }
238
239 static void context_switch_test(Bitmap *bm)
240 {
241         gfx_bitmapClear(bm);
242         text_xprintf(bm, 0, 0, TEXT_FILL,
243                         "CPU: Cortex-M3 %luMHz", CPU_FREQ / 1000000);
244         rit128x96_blitBitmap(bm);
245         text_xprintf(bm, 1, 0, TEXT_FILL, "Board: LM3S8962 EVB");
246         rit128x96_blitBitmap(bm);
247
248         res_process();
249 }
250
251 static void uptime(Bitmap *bm)
252 {
253         extern const Font font_luBS14;
254         const Font *old_font;
255
256         old_font = bm->font;
257
258         /* Set big font */
259         gfx_bitmapClear(bm);
260         gfx_setFont(bm, &font_luBS14);
261         text_xprintf(bm, 0, 0, TEXT_FILL | TEXT_CENTER, "Uptime");
262         while (1)
263         {
264                 ticks_t clock = ticks_to_ms(timer_clock_unlocked());
265
266                 /* Display uptime (in ticks) */
267                 text_xprintf(&lcd_bitmap, 2, 0, TEXT_FILL | TEXT_CENTER,
268                                 "%lu", clock / 1000);
269                 rit128x96_blitBitmap(bm);
270                 timer_delay(5);
271                 if (kbd_peek() & KEY_MASK)
272                         break;
273         }
274         gfx_setFont(bm, old_font);
275 }
276
277 static void NORETURN soft_reset(Bitmap * bm)
278 {
279         extern const Font font_luBS14;
280         int i;
281
282         /* Set big font */
283         gfx_bitmapClear(bm);
284         gfx_setFont(bm, &font_luBS14);
285         for (i = 5; i; --i)
286         {
287                 text_xprintf(bm, 2, 0, TEXT_FILL | TEXT_CENTER, "%d", i);
288                 rit128x96_blitBitmap(bm);
289                 timer_delay(1000);
290         }
291         text_xprintf(bm, 2, 0, TEXT_FILL | TEXT_CENTER, "REBOOT");
292         rit128x96_blitBitmap(bm);
293         timer_delay(1000);
294
295         /* Perform a software reset request */
296         HWREG(NVIC_APINT) = NVIC_APINT_VECTKEY | NVIC_APINT_SYSRESETREQ;
297         UNREACHABLE();
298 }
299
300 static void NORETURN ser_process(void)
301 {
302         char buf[32];
303         int i;
304
305         ser_init(&ser_port, SER_UART0);
306         ser_setbaudrate(&ser_port, 115200);
307
308         /* BeRTOS terminal */
309         for (i = 0; ; i++)
310         {
311                 kfile_printf(&ser_port.fd, "\n\r[%03d] BeRTOS:~$ ", i);
312                 kfile_gets_echo(&ser_port.fd, buf, sizeof(buf), true);
313                 kfile_printf(&ser_port.fd, "%s", buf);
314         }
315 }
316
317 static struct MenuItem main_items[] =
318 {
319         { (const_iptr_t)"LED blinking", 0, (MenuHook)led_test, (iptr_t)&lcd_bitmap },
320         { (const_iptr_t)"Bouncing logo", 0, (MenuHook)bouncing_logo, (iptr_t)&lcd_bitmap },
321         { (const_iptr_t)"Screen saver demo", 0, (MenuHook)screen_saver, (iptr_t)&lcd_bitmap },
322         { (const_iptr_t)"Scheduling test", 0, (MenuHook)context_switch_test, (iptr_t)&lcd_bitmap },
323         { (const_iptr_t)"Show uptime", 0, (MenuHook)uptime, (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 }
326 };
327 static struct Menu main_menu = { main_items, "BeRTOS", MF_STICKY | MF_SAVESEL, &lcd_bitmap, 0, rit128x96_blitBitmap };
328
329 int main(void)
330 {
331         IRQ_ENABLE;
332         kdbg_init();
333
334         kputs("Init LED..");
335         led_init();
336         kputs("Done.\n");
337         kputs("Init Timer..");
338         timer_init();
339         kputs("Done.\n");
340         kputs("Init Process..");
341         proc_init();
342         kputs("Done.\n");
343
344         kputs("Init OLED display..");
345         rit128x96_init();
346         gfx_bitmapInit(&lcd_bitmap, raster, LCD_WIDTH, LCD_HEIGHT);
347         gfx_setFont(&lcd_bitmap, &font_gohu);
348         rit128x96_blitBitmap(&lcd_bitmap);
349         kputs("Done.\n");
350         kputs("Init Keypad..");
351         kbd_init();
352         kputs("Done.\n");
353         hp_proc = proc_new(hp_process, NULL, PROC_STACK_SIZE, hp_stack);
354         lp_proc = proc_new(lp_process, NULL, PROC_STACK_SIZE, lp_stack);
355         led_proc = proc_new(led_process, NULL, PROC_STACK_SIZE, led_stack);
356         /* Open a dummy echo terminal on UART0 */
357         proc_new(ser_process, NULL, PROC_STACK_SIZE, ser_stack);
358
359         proc_setPri(hp_proc, 2);
360         proc_setPri(lp_proc, 1);
361         while (1)
362         {
363                 menu_handle(&main_menu);
364                 cpu_relax();
365         }
366 }