lm3s1968: enable kernel preemption in the example.
[bertos.git] / examples / lm3s1968 / lm3s1968.c
index 6316ad18373e09b0c9c1e795c35538b945f2c255..826bd3e4099d97509d7400f5ba83fd773bb4eebc 100644 (file)
  */
 
 #include <cpu/irq.h>
+#include <drv/timer.h>
 #include "io/lm3s.h"
-#include "drv/timer.h"
 
-extern unsigned long ticks;
+static Process *hp_proc, *lp_proc;
 
-int main(void)
-{
-       timer_hw_init();
+static hptime_t start, end;
 
+static void led_init(void)
+{
        /* Enable the GPIO port that is used for the on-board LED */
        SYSCTL_RCGC2_R = SYSCTL_RCGC2_GPIOG;
        /*
@@ -55,14 +55,87 @@ int main(void)
        /* Enable the GPIO pin for the LED */
        GPIO_PORTG_DIR_R = 0x04;
        GPIO_PORTG_DEN_R = 0x04;
+}
+
+INLINE void led_on(void)
+{
+       GPIO_PORTG_DATA_R |= 0x04;
+}
+
+INLINE void led_off(void)
+{
+       GPIO_PORTG_DATA_R &= ~0x04;
+}
+
+INLINE hptime_t get_hp_ticks(void)
+{
+       return (TIMER_HW_CNT - timer_hw_hpread()) +
+                       timer_clock_unlocked() * TIMER_HW_CNT;
+}
+
+#if CONFIG_KERN_HEAP
+#define hp_stack NULL
+#define HP_STACK_SIZE  KERN_MINSTACKSIZE * 2
+#else
+static PROC_DEFINE_STACK(hp_stack, KERN_MINSTACKSIZE * 2);
+#define HP_STACK_SIZE  sizeof(hp_stack)
+#endif
+
+static void NORETURN hp_process(void)
+{
+       char spinner[] = {'/', '-', '\\', '|'};
+       int i;
+
+       for(i = 0; ; i++)
+       {
+               sig_wait(SIG_USER0);
+               end = get_hp_ticks();
+               kprintf("%c context switch in %lu clock cycles (~%lu us)    \r",
+                               spinner[i % countof(spinner)],
+                               end - start,
+                               ((end - start) * 1000000 / CPU_FREQ));
+               led_off();
+               timer_delay(50);
+               sig_send(lp_proc, SIG_USER0);
+       }
+}
 
-       while(1)
+static void NORETURN lp_process(void)
+{
+       while (1)
        {
-               /* Turn on the LED */
-               if ((ticks & 0x04) == 0x04)
-                       GPIO_PORTG_DATA_R |= 0x04;
-               /* Turn off the LED */
-               else if ((ticks & 0x04) == 0)
-                       GPIO_PORTG_DATA_R &= ~0x04;
+               led_on();
+               timer_delay(50);
+               start = get_hp_ticks();
+               sig_send(hp_proc, SIG_USER0);
+               sig_wait(SIG_USER0);
        }
 }
+
+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("Check scheduling functionality\n");
+       proc_testRun();
+
+       kputs("BeRTOS is up & running\n\n");
+       hp_proc = proc_new(hp_process, NULL, HP_STACK_SIZE, hp_stack);
+       lp_proc = proc_current();
+
+       proc_setPri(hp_proc, 2);
+       proc_setPri(lp_proc, 1);
+
+       lp_process();
+}