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