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