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