aec08cecef22dbe9a6e1fa4d35826ae080131f69
[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 <cfg/compiler.h>
39 #include <cfg/cfg_gfx.h>
40 #include <cpu/irq.h>
41 #include <drv/timer.h>
42 #include <drv/lcd_lm3s.h>
43 #include <io/lm3s.h>
44 #include <gfx/gfx.h>
45 #include <gfx/font.h>
46 #include <gfx/text.h>
47 #include <icons/logo.h>
48 #include <stdio.h>
49
50 #define PROC_STACK_SIZE KERN_MINSTACKSIZE * 2
51
52 #if CONFIG_KERN_HEAP
53 #define hp_stack NULL
54 #define lp_stack NULL
55 #else
56 static PROC_DEFINE_STACK(hp_stack, PROC_STACK_SIZE);
57 static PROC_DEFINE_STACK(lp_stack, PROC_STACK_SIZE);
58 #endif
59
60 static Process *hp_proc, *lp_proc, *res_proc;
61
62 static hptime_t start, end;
63
64 static uint8_t raster[RAST_SIZE(128, 96)];
65 static Bitmap bm;
66
67 extern Font font_helvB10;
68
69 static void led_init(void)
70 {
71         /* Enable the GPIO port that is used for the on-board LED */
72         SYSCTL_RCGC2_R = SYSCTL_RCGC2_GPIOG;
73         /*
74          * Perform a dummy read to insert a few cycles delay before enabling
75          * the peripheral.
76          */
77         (void)SYSCTL_RCGC2_R;
78         /* Enable the GPIO pin for the LED */
79         GPIO_PORTG_DIR_R = 0x04;
80         GPIO_PORTG_DEN_R = 0x04;
81 }
82
83 INLINE void led_on(void)
84 {
85         GPIO_PORTG_DATA_R |= 0x04;
86 }
87
88 INLINE void led_off(void)
89 {
90         GPIO_PORTG_DATA_R &= ~0x04;
91 }
92
93 INLINE hptime_t get_hp_ticks(void)
94 {
95         return (TIMER_HW_CNT - timer_hw_hpread()) +
96                         timer_clock_unlocked() * TIMER_HW_CNT;
97 }
98
99 static void NORETURN res_process(void)
100 {
101         const char spinner[] = {'/', '-', '\\', '|'};
102         char buffer[256], c;
103         int i;
104
105         for (i = 0; ; i++)
106         {
107                 ticks_t clock;
108
109                 sig_wait(SIG_USER0);
110                 clock = timer_clock_unlocked();
111
112                 /* Display uptime (in ticks) */
113                 buffer[sizeof(buffer) - 1] = '\0';
114                 snprintf(buffer, sizeof(buffer) - 1,
115                         "uptime: %lu sec", clock / 1000);
116                 text_xprintf(&bm, 2, 0, TEXT_FILL, buffer);
117
118                 /* Show context switch (in clock cycles) */
119                 c = spinner[i % countof(spinner)];
120                 buffer[sizeof(buffer) - 1] = '\0';
121                 snprintf(buffer, sizeof(buffer) - 1,
122                         "%c Context switch %c", c, c);
123                 text_xprintf(&bm, 4, 0, TEXT_CENTER | TEXT_FILL, buffer);
124                 buffer[sizeof(buffer) - 1] = '\0';
125                 snprintf(buffer, sizeof(buffer) - 1,
126                         " %lu clock cycles", end - start);
127                 text_xprintf(&bm, 6, 0, TEXT_FILL, buffer);
128
129                 /* Show context switch (in usec) */
130                 buffer[sizeof(buffer) - 1] = '\0';
131                 snprintf(buffer, sizeof(buffer) - 1,
132                         " %lu.%lu usec",
133                                 ((end - start) * 1000000) / CPU_FREQ,
134                                 ((end - start) * (100000000 / CPU_FREQ)) % 100);
135                 text_xprintf(&bm, 7, 0, TEXT_FILL, buffer);
136                 lm3s_lcd_blitBitmap(&bm);
137
138                 /* Blink the status LED and restart the test */
139                 led_off();
140                 timer_delay(100);
141                 led_on();
142                 sig_send(lp_proc, SIG_USER0);
143         }
144 }
145
146 static void NORETURN hp_process(void)
147 {
148         while (1)
149         {
150                 sig_wait(SIG_USER0);
151                 end = get_hp_ticks();
152                 sig_send(res_proc, SIG_USER0);
153         }
154 }
155
156 static void NORETURN lp_process(void)
157 {
158         while (1)
159         {
160                 start = get_hp_ticks();
161                 sig_send(hp_proc, SIG_USER0);
162                 sig_wait(SIG_USER0);
163         }
164 }
165
166 /**
167  * Show the splash screen
168  */
169 static void bouncing_logo(Bitmap *bm)
170 {
171         const long SPEED_SCALE = 1000;
172         const long GRAVITY_ACCEL = 100;
173         const long BOUNCE_ELASTICITY = 2;
174         const long TOT_FRAMES = 100;
175         long h = (long)(-bertos_logo.height) * SPEED_SCALE;
176         long speed = 0, i;
177
178         for (i = 0; i < TOT_FRAMES; i++)
179         {
180                 /* Move */
181                 h += speed;
182
183                 /* Gravity acceleration */
184                 speed += GRAVITY_ACCEL;
185
186                 if (h > 0 && speed > 0)
187                 {
188                         /* Bounce */
189                         speed = -(speed / BOUNCE_ELASTICITY);
190
191                 }
192                 /* Update graphics */
193                 gfx_blitImage(bm,
194                         (LCD_WIDTH - bertos_logo.width) / 2,
195                         (LCD_HEIGHT - bertos_logo.height) / 2 + h / SPEED_SCALE,
196                         &bertos_logo);
197                 lm3s_lcd_blitBitmap(bm);
198                 timer_delay(5);
199         }
200 }
201
202 int main(void)
203 {
204         char buffer[32];
205
206         IRQ_ENABLE;
207         kdbg_init();
208
209         kputs("Init LED..");
210         led_init();
211         kputs("Done.\n");
212         kputs("Init Timer..");
213         timer_init();
214         kputs("Done.\n");
215         kputs("Init Process..");
216         proc_init();
217         kputs("Done.\n");
218         kputs("Init OLED display..");
219         lm3s_lcd_init(CPU_FREQ / 2);
220         gfx_bitmapInit(&bm, raster, LCD_WIDTH, LCD_HEIGHT);
221         gfx_setFont(&bm, &font_helvB10);
222         kputs("Done.\n");
223
224         bouncing_logo(&bm);
225
226         gfx_bitmapClear(&bm);
227 #ifdef _DEBUG
228         text_xprintf(&bm, 4, 0, TEXT_CENTER | TEXT_FILL, "BeRTOS up & running!");
229         lm3s_lcd_blitBitmap(&bm);
230         proc_testRun();
231 #endif
232         snprintf(buffer, sizeof(buffer),
233                         "CPU: Cortex-M3 %luMHz", CPU_FREQ / 1000000);
234         text_xprintf(&bm, 0, 0, TEXT_FILL, buffer);
235         lm3s_lcd_blitBitmap(&bm);
236         text_xprintf(&bm, 1, 0, TEXT_FILL, "Board: LM3S1968 EVB");
237         lm3s_lcd_blitBitmap(&bm);
238
239         hp_proc = proc_new(hp_process, NULL, PROC_STACK_SIZE, hp_stack);
240         lp_proc = proc_new(lp_process, NULL, PROC_STACK_SIZE, lp_stack);
241
242         res_proc = proc_current();
243
244         proc_setPri(hp_proc, 2);
245         proc_setPri(lp_proc, 1);
246
247         res_process();
248 }