BeRTOS
context_switch.c
Go to the documentation of this file.
00001 
00039 #include "context_switch.h"
00040 
00041 #include "hw/hw_led.h"
00042 
00043 #include "cfg/cfg_context_switch.h"
00044 #include <cfg/debug.h>
00045 
00046 #include <cpu/irq.h>
00047 #include <cpu/power.h>
00048 
00049 #include <drv/timer.h>
00050 #if CONFIG_USE_HP_TIMER
00051 #include <drv/ser.h>
00052 static Serial out;
00053 #endif
00054 
00055 #include <kern/proc.h>
00056 
00057 #define PROC_STACK_SIZE    KERN_MINSTACKSIZE
00058 
00059 static PROC_DEFINE_STACK(hp_stack, PROC_STACK_SIZE);
00060 static PROC_DEFINE_STACK(lp_stack, PROC_STACK_SIZE);
00061 
00062 static Process *hp_proc, *lp_proc, *main_proc;
00063 #if CONFIG_USE_HP_TIMER
00064 static hptime_t start, end;
00065 #endif
00066 
00067 static void NORETURN hp_process(void)
00068 {
00069     while (1)
00070     {
00071         sig_wait(SIG_USER0);
00072         #if CONFIG_USE_LED
00073             LED_ON();
00074         #endif
00075         #if CONFIG_USE_HP_TIMER
00076             end = timer_hw_hpread();
00077         #endif
00078         sig_send(main_proc, SIG_USER0);
00079     }
00080 }
00081 
00082 static void NORETURN lp_process(void)
00083 {
00084     while (1)
00085     {
00086         sig_wait(SIG_USER0);
00087         #if CONFIG_USE_LED
00088             LED_ON();
00089             LED_OFF();
00090         #endif
00091         #if CONFIG_USE_HP_TIMER
00092             start = timer_hw_hpread();
00093         #endif
00094         sig_send(hp_proc, SIG_USER0);
00095     }
00096 }
00097 
00098 
00099 void NORETURN context_switch(void)
00100 {
00101     IRQ_ENABLE;
00102     timer_init();
00103     proc_init();
00104 
00105     #if CONFIG_USE_HP_TIMER
00106         ser_init(&out, CONFIG_CTX_DEBUG_PORT);
00107         ser_setbaudrate(&out, CONFIG_CTX_DEBUG_BAUDRATE);
00108     #endif
00109 
00110     #if CONFIG_USE_LED
00111         LED_INIT();
00112     #endif
00113 
00114     proc_forbid();
00115     hp_proc = proc_new(hp_process, NULL, PROC_STACK_SIZE, hp_stack);
00116     lp_proc = proc_new(lp_process, NULL, PROC_STACK_SIZE, lp_stack);
00117     main_proc = proc_current();
00118     proc_setPri(hp_proc, 2);
00119     proc_setPri(lp_proc, 1);
00120     proc_permit();
00121 
00122     while (1)
00123     {
00124         timer_delay(100);
00125 
00126         sig_send(lp_proc, SIG_USER0);
00127         sig_wait(SIG_USER0);
00128 
00129         #if CONFIG_USE_HP_TIMER
00130             kfile_printf(&out.fd,
00131                 "Switch: %lu.%lu usec\n\r",
00132                 hptime_to_us((end - start)),
00133                 hptime_to_us((end - start) * 1000) % 1000);
00134         #endif
00135     }
00136 }