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