+ return (TIMER_HW_CNT - timer_hw_hpread()) +
+ timer_clock_unlocked() * TIMER_HW_CNT;
+}
+
+static void NORETURN hp_process(void)
+{
+ while (1)
+ {
+ sig_wait(SIG_USER0);
+ end = get_hp_ticks();
+ timer_delay(100);
+ sig_send(lp_proc, SIG_USER0);
+ }
+}
+
+static void NORETURN lp_process(void)
+{
+ while (1)
+ {
+ start = get_hp_ticks();
+ sig_send(hp_proc, SIG_USER0);
+ sig_wait(SIG_USER0);
+ }
+}
+
+static void res_process(void)
+{
+ const char spinner[] = {'/', '-', '\\', '|'};
+ int i;
+ char c;
+
+ for (i = 0; ; i++)
+ {
+ /* Show context switch (in clock cycles) */
+ c = spinner[i % countof(spinner)];
+ text_xprintf(&lcd_bitmap, 3, 0, TEXT_CENTER | TEXT_FILL, "%c Context switch %c", c, c);
+ text_xprintf(&lcd_bitmap, 5, 0, TEXT_FILL, " %lu clock cycles", end - start);
+ /* Show context switch (in usec) */
+ text_xprintf(&lcd_bitmap, 6, 0, TEXT_FILL,
+ " %lu.%lu usec",
+ ((end - start) * 1000000) / CPU_FREQ,
+ ((end - start) * (100000000 / CPU_FREQ)) % 100);
+ rit128x96_blitBitmap(&lcd_bitmap);
+ timer_delay(5);
+ if (kbd_peek() & KEY_MASK)
+ break;
+ }
+}
+
+static void context_switch_test(Bitmap *bm)
+{
+ gfx_bitmapClear(bm);
+ text_xprintf(bm, 0, 0, TEXT_FILL,
+ "CPU: Cortex-M3 %luMHz", CPU_FREQ / 1000000);
+ rit128x96_blitBitmap(bm);
+ text_xprintf(bm, 1, 0, TEXT_FILL, "Board: LM3S1968 EVB");
+ rit128x96_blitBitmap(bm);
+
+ res_process();
+}
+
+static void uptime(Bitmap *bm)
+{
+ extern const Font font_luBS14;
+ const Font *old_font;
+
+ old_font = bm->font;
+
+ /* Set big font */
+ gfx_bitmapClear(bm);
+ gfx_setFont(bm, &font_luBS14);
+ text_xprintf(bm, 0, 0, TEXT_FILL | TEXT_CENTER, "Uptime");
+ while (1)
+ {
+ ticks_t clock = ticks_to_ms(timer_clock_unlocked());
+
+ /* Display uptime (in ticks) */
+ text_xprintf(&lcd_bitmap, 2, 0, TEXT_FILL | TEXT_CENTER,
+ "%lu", clock / 1000);
+ rit128x96_blitBitmap(bm);
+ timer_delay(5);
+ if (kbd_peek() & KEY_MASK)
+ break;
+ }
+ gfx_setFont(bm, old_font);
+}
+
+static void NORETURN soft_reset(Bitmap * bm)
+{
+ extern const Font font_luBS14;
+ int i;
+
+ /* Set big font */
+ gfx_bitmapClear(bm);
+ gfx_setFont(bm, &font_luBS14);
+ for (i = 5; i; --i)
+ {
+ text_xprintf(bm, 2, 0, TEXT_FILL | TEXT_CENTER, "%d", i);
+ rit128x96_blitBitmap(bm);
+ timer_delay(1000);
+ }
+ text_xprintf(bm, 2, 0, TEXT_FILL | TEXT_CENTER, "REBOOT");
+ rit128x96_blitBitmap(bm);
+ timer_delay(1000);
+
+ /* Perform a software reset request */
+ HWREG(NVIC_APINT) = NVIC_APINT_VECTKEY | NVIC_APINT_SYSRESETREQ;
+ UNREACHABLE();
+}
+
+static void NORETURN ser_process(void)
+{
+ char buf[32];
+ int i;
+
+ ser_init(&ser_port, SER_UART0);
+ ser_setbaudrate(&ser_port, 115200);
+
+ /* BeRTOS terminal */
+ for (i = 0; ; i++)
+ {
+ kfile_printf(&ser_port.fd, "\n\r[%03d] BeRTOS:~$ ", i);
+ kfile_gets_echo(&ser_port.fd, buf, sizeof(buf), true);
+ kfile_printf(&ser_port.fd, "%s", buf);
+ }
+}
+
+static struct MenuItem main_items[] =
+{
+ { (const_iptr_t)"LED blinking", 0, (MenuHook)led_test, (iptr_t)&lcd_bitmap },
+ { (const_iptr_t)"Bouncing logo", 0, (MenuHook)bouncing_logo, (iptr_t)&lcd_bitmap },
+ { (const_iptr_t)"Screen saver demo", 0, (MenuHook)screen_saver, (iptr_t)&lcd_bitmap },
+ { (const_iptr_t)"Scheduling test", 0, (MenuHook)context_switch_test, (iptr_t)&lcd_bitmap },
+ { (const_iptr_t)"Show uptime", 0, (MenuHook)uptime, (iptr_t)&lcd_bitmap },
+ { (const_iptr_t)"Reboot", 0, (MenuHook)soft_reset, (iptr_t)&lcd_bitmap },
+ { (const_iptr_t)0, 0, NULL, (iptr_t)0 }
+};
+static struct Menu main_menu = { main_items, "BeRTOS", MF_STICKY | MF_SAVESEL, &lcd_bitmap, 0 };
+
+int main(void)
+{
+ IRQ_ENABLE;
+ kdbg_init();
+
+ kputs("Init LED..");
+ led_init();
+ kputs("Done.\n");
+ kputs("Init Timer..");
+ timer_init();
+ kputs("Done.\n");
+ kputs("Init Process..");
+ proc_init();
+ kputs("Done.\n");
+ kputs("Init OLED display..");
+ rit128x96_init();
+ gfx_bitmapInit(&lcd_bitmap, raster, LCD_WIDTH, LCD_HEIGHT);
+ gfx_setFont(&lcd_bitmap, &font_gohu);
+ rit128x96_blitBitmap(&lcd_bitmap);
+ kputs("Done.\n");
+ kputs("Init Keypad..");
+ kbd_init();
+ kputs("Done.\n");
+
+ hp_proc = proc_new(hp_process, NULL, PROC_STACK_SIZE, hp_stack);
+ lp_proc = proc_new(lp_process, NULL, PROC_STACK_SIZE, lp_stack);
+ led_proc = proc_new(led_process, NULL, PROC_STACK_SIZE, led_stack);
+ /* Open a dummy echo terminal on UART0 */
+ proc_new(ser_process, NULL, PROC_STACK_SIZE, ser_stack);
+
+ proc_setPri(hp_proc, 2);
+ proc_setPri(lp_proc, 1);