Index: rtos_arduino/trunk/lib/r2ca_lib.cpp
===================================================================
--- rtos_arduino/trunk/lib/rca_lib.cpp	(revision 251)
+++ rtos_arduino/trunk/lib/r2ca_lib.cpp	(revision 260)
@@ -1,44 +1,44 @@
-#include "rca.h"
-#include "rca_lib.h"
+#include "r2ca.h"
+#include "r2ca_lib.h"
 
 extern "C" {
 extern void yield(void);
-extern void rca_ena_int(uint_t intno);
-extern void rca_dis_int(uint_t intno);
-}
-
-#ifdef RCA_ENABLE_PROFILING
-volatile uint32_t rca_idle_result;
-volatile uint32_t rca_isr_result;
-volatile uint32_t rca_dispatch_result;
-volatile uint32_t rca_timer_isr_result;
-volatile uint32_t rca_usb_isr_result;
-volatile uint32_t rca_sercom0_isr_result;
-volatile uint32_t rca_sercom2_isr_result;
-volatile uint32_t rca_sercom3_isr_result;
-volatile uint32_t rca_sercom4_isr_result;
-volatile uint32_t rca_sercom5_isr_result;
-volatile uint32_t rca_eic_isr_result;
-volatile uint32_t rca_tc5_isr_result;
-volatile uint32_t rca_rtc_isr_result;
-
-static volatile uint32_t rca_idle_cnt;
-static volatile uint32_t rca_isr_cnt;
-static volatile uint32_t rca_dispatch_cnt;
-static volatile uint32_t rca_timer_isr_cnt;
-static volatile uint32_t rca_usb_isr_cnt;
-static volatile uint32_t rca_sercom0_isr_cnt;
-static volatile uint32_t rca_sercom2_isr_cnt;
-static volatile uint32_t rca_sercom3_isr_cnt;
-static volatile uint32_t rca_sercom4_isr_cnt;
-static volatile uint32_t rca_sercom5_isr_cnt;
-static volatile uint32_t rca_eic_isr_cnt;
-static volatile uint32_t rca_tc5_isr_cnt;
-static volatile uint32_t rca_rtc_isr_cnt;     
-static volatile uint32_t rca_profiling_cyccnt;
-#endif /* RCA_ENABLE_PROFILING */
+extern void r2ca_ena_int(uint_t intno);
+extern void r2ca_dis_int(uint_t intno);
+}
+
+#ifdef R2CA_ENABLE_PROFILING
+volatile uint32_t r2ca_idle_result;
+volatile uint32_t r2ca_isr_result;
+volatile uint32_t r2ca_dispatch_result;
+volatile uint32_t r2ca_timer_isr_result;
+volatile uint32_t r2ca_usb_isr_result;
+volatile uint32_t r2ca_sercom0_isr_result;
+volatile uint32_t r2ca_sercom2_isr_result;
+volatile uint32_t r2ca_sercom3_isr_result;
+volatile uint32_t r2ca_sercom4_isr_result;
+volatile uint32_t r2ca_sercom5_isr_result;
+volatile uint32_t r2ca_eic_isr_result;
+volatile uint32_t r2ca_tc5_isr_result;
+volatile uint32_t r2ca_rtc_isr_result;
+
+static volatile uint32_t r2ca_idle_cnt;
+static volatile uint32_t r2ca_isr_cnt;
+static volatile uint32_t r2ca_dispatch_cnt;
+static volatile uint32_t r2ca_timer_isr_cnt;
+static volatile uint32_t r2ca_usb_isr_cnt;
+static volatile uint32_t r2ca_sercom0_isr_cnt;
+static volatile uint32_t r2ca_sercom2_isr_cnt;
+static volatile uint32_t r2ca_sercom3_isr_cnt;
+static volatile uint32_t r2ca_sercom4_isr_cnt;
+static volatile uint32_t r2ca_sercom5_isr_cnt;
+static volatile uint32_t r2ca_eic_isr_cnt;
+static volatile uint32_t r2ca_tc5_isr_cnt;
+static volatile uint32_t r2ca_rtc_isr_cnt;     
+static volatile uint32_t r2ca_profiling_cyccnt;
+#endif /* R2CA_ENABLE_PROFILING */
 
 Inline void
-rca_di(void){
+r2ca_di(void){
 	Asm("cpsid f":::"memory");
 }
@@ -48,34 +48,31 @@
  */
 Inline void
-rca_ei(void){
+r2ca_ei(void){
 	Asm("cpsie f":::"memory");
 }
 
 void
-rca_init(intptr_t exinf)
-{
-    init(); //wiring.c
-}
-
-void
-rca_ena_int(uint_t intno) {
-    if (ena_int(intno + 16) != E_OK) {
-        while(1); //ToDo
-    }
-
-}
-
-void
-rca_dis_int(uint_t intno) {
-    if (dis_int(intno + 16) != E_OK) {
-        while(1); //ToDo
-    }
+r2ca_init(intptr_t exinf)
+{
+	init(); //wiring.c
+}
+
+void
+r2ca_ena_int(uint_t intno) {
+	if (ena_int(intno + 16) != E_OK) {
+		while(1); //ToDo
+	}
+}
+
+void
+r2ca_dis_int(uint_t intno) {
+	if (dis_int(intno + 16) != E_OK) {
+		while(1); //ToDo
+	}
 }
 
 extern const DeviceVectors exception_table;
 
-#ifdef RCA_RR_SCHEDULE
 static uint32_t time_slice = 0;
-#endif /* RCA_RR_SCHEDULE */
 
 /*
@@ -83,54 +80,52 @@
  */
 void
-rca_CycHandler(intptr_t exinf)
+r2ca_CycHandler(intptr_t exinf)
 {    
-#ifdef RCA_RR_SCHEDULE
-    int i;
-    if (++time_slice == RCA_RR_SCHEDULE_CYCLE) {
-        time_slice = 0;
-        for(i = 1; i < 16; i++){
-            if (RCA_RR_SCHEDULE & (1 << i)) {
-                irot_rdq(i);
-            }
-        }
-    }
-#endif /* RCA_RR_SCHEDULE */
-#ifdef RCA_ENABLE_PROFILING    
-    rca_di();
-    rca_isr_cnt++;
-    rca_timer_isr_cnt++;
-    rca_profiling_cyccnt++;
-    if(rca_profiling_cyccnt == RCA_PROFILING_CYC_MS){
-        rca_idle_result=        rca_idle_cnt;
-        rca_isr_result=         rca_isr_cnt;
-        rca_dispatch_result=    rca_dispatch_cnt;
-        rca_timer_isr_result=   rca_timer_isr_cnt;
-        rca_usb_isr_result=     rca_usb_isr_cnt;
-        rca_sercom0_isr_result= rca_sercom0_isr_cnt;
-        rca_sercom2_isr_result= rca_sercom2_isr_cnt;        
-        rca_sercom3_isr_result= rca_sercom3_isr_cnt;
-        rca_sercom4_isr_result= rca_sercom4_isr_cnt;
-        rca_sercom5_isr_result= rca_sercom5_isr_cnt;
-        rca_eic_isr_result=     rca_eic_isr_cnt;
-        rca_tc5_isr_result=     rca_tc5_isr_cnt;
-        rca_rtc_isr_result=     rca_rtc_isr_cnt;             
-        rca_idle_cnt = 0;
-        rca_isr_cnt = 0;
-        rca_dispatch_cnt = 0;
-        rca_timer_isr_cnt = 0;
-        rca_usb_isr_cnt = 0;
-        rca_sercom0_isr_cnt = 0;
-        rca_sercom2_isr_cnt = 0;        
-        rca_sercom3_isr_cnt = 0;
-        rca_sercom4_isr_cnt = 0;
-        rca_sercom5_isr_cnt = 0;
-        rca_eic_isr_cnt = 0;
-        rca_tc5_isr_cnt = 0;
-        rca_rtc_isr_cnt = 0;
-        rca_profiling_cyccnt = 0;
-    }
-    rca_ei();
-#endif /* RCA_ENABLE_PROFILING */    
-    ((void(*)(void))(exception_table.pfnSysTick_Handler))();        
+	int i;
+	if (++time_slice == R2CA_RR_SCHEDULE_CYCLE) {
+		time_slice = 0;
+		for(i = 1; i < 16; i++){
+			if (R2CA_RR_SCHEDULE_PRI & (1 << i)) {
+				irot_rdq(i);
+			}
+		}
+	}
+#ifdef R2CA_ENABLE_PROFILING    
+	r2ca_di();
+	r2ca_isr_cnt++;
+	r2ca_timer_isr_cnt++;
+	r2ca_profiling_cyccnt++;
+	if(r2ca_profiling_cyccnt == R2CA_PROFILING_CYC_MS){
+		r2ca_idle_result=        r2ca_idle_cnt;
+		r2ca_isr_result=         r2ca_isr_cnt;
+		r2ca_dispatch_result=    r2ca_dispatch_cnt;
+		r2ca_timer_isr_result=   r2ca_timer_isr_cnt;
+		r2ca_usb_isr_result=     r2ca_usb_isr_cnt;
+		r2ca_sercom0_isr_result= r2ca_sercom0_isr_cnt;
+		r2ca_sercom2_isr_result= r2ca_sercom2_isr_cnt;        
+		r2ca_sercom3_isr_result= r2ca_sercom3_isr_cnt;
+		r2ca_sercom4_isr_result= r2ca_sercom4_isr_cnt;
+		r2ca_sercom5_isr_result= r2ca_sercom5_isr_cnt;
+		r2ca_eic_isr_result=     r2ca_eic_isr_cnt;
+		r2ca_tc5_isr_result=     r2ca_tc5_isr_cnt;
+		r2ca_rtc_isr_result=     r2ca_rtc_isr_cnt;             
+		r2ca_idle_cnt = 0;
+		r2ca_isr_cnt = 0;
+		r2ca_dispatch_cnt = 0;
+		r2ca_timer_isr_cnt = 0;
+		r2ca_usb_isr_cnt = 0;
+		r2ca_sercom0_isr_cnt = 0;
+		r2ca_sercom2_isr_cnt = 0;        
+		r2ca_sercom3_isr_cnt = 0;
+		r2ca_sercom4_isr_cnt = 0;
+		r2ca_sercom5_isr_cnt = 0;
+		r2ca_eic_isr_cnt = 0;
+		r2ca_tc5_isr_cnt = 0;
+		r2ca_rtc_isr_cnt = 0;
+		r2ca_profiling_cyccnt = 0;
+	}
+	r2ca_ei();
+#endif /* R2CA_ENABLE_PROFILING */    
+	((void(*)(void))(exception_table.pfnSysTick_Handler))();        
 }
 
@@ -139,28 +134,28 @@
  */
 void
-rca_USB_Handler(void)
-{
-#ifdef RCA_ENABLE_PROFILING
-    rca_di();
-    rca_usb_isr_cnt++;
-    rca_isr_cnt++;
-    rca_ei();
-#endif /* RCA_ENABLE_PROFILING */    
-    ((void(*)(void))(exception_table.pfnUSB_Handler))();
-}
-
-void
-rca_SERCOM0_Handler(void)
-{
-#ifdef RCA_ENABLE_PROFILING
-    rca_di();
-    rca_sercom0_isr_cnt++;
-    rca_isr_cnt++;
-    rca_ei();
-#endif /* RCA_ENABLE_PROFILING */    
-    ((void(*)(void))(exception_table.pfnSERCOM0_Handler))();
-}
-
-#ifdef RCA_USE_SERIAL3
+r2ca_USB_Handler(void)
+{
+#ifdef R2CA_ENABLE_PROFILING
+	r2ca_di();
+	r2ca_usb_isr_cnt++;
+	r2ca_isr_cnt++;
+	r2ca_ei();
+#endif /* R2CA_ENABLE_PROFILING */    
+	((void(*)(void))(exception_table.pfnUSB_Handler))();
+}
+
+void
+r2ca_SERCOM0_Handler(void)
+{
+#ifdef R2CA_ENABLE_PROFILING
+	r2ca_di();
+	r2ca_sercom0_isr_cnt++;
+	r2ca_isr_cnt++;
+	r2ca_ei();
+#endif /* R2CA_ENABLE_PROFILING */    
+	((void(*)(void))(exception_table.pfnSERCOM0_Handler))();
+}
+
+#ifdef R2CA_USE_SERIAL3
 #define PIN_SERIAL3_RX 49  /* D5 */
 #define PIN_SERIAL3_TX 48  /* D4 */
@@ -169,88 +164,88 @@
 
 void
-rca_SERCOM2_Handler(void)
-{
-#ifdef RCA_ENABLE_PROFILING
-    rca_di();
-    rca_sercom2_isr_cnt++;
-    rca_isr_cnt++;
-    rca_ei();
-#endif /* RCA_ENABLE_PROFILING */
-    Serial3.IrqHandler();
-}
-#endif /* RCA_USE_SERIAL3 */
-
-void
-rca_SERCOM3_Handler(void)
-{
-#ifdef RCA_ENABLE_PROFILING
-    rca_di();
-    rca_sercom3_isr_cnt++;
-    rca_isr_cnt++;
-    rca_ei();
-#endif /* RCA_ENABLE_PROFILING */    
-    ((void(*)(void))(exception_table.pfnSERCOM3_Handler))();
-}
-
-void
-rca_SERCOM4_Handler(void)
-{
-#ifdef RCA_ENABLE_PROFILING
-    rca_di();
-    rca_sercom4_isr_cnt++;
-    rca_isr_cnt++;
-    rca_ei();
-#endif /* RCA_ENABLE_PROFILING */    
-    ((void(*)(void))(exception_table.pfnSERCOM4_Handler))();
+r2ca_SERCOM2_Handler(void)
+{
+#ifdef R2CA_ENABLE_PROFILING
+	r2ca_di();
+	r2ca_sercom2_isr_cnt++;
+	r2ca_isr_cnt++;
+	r2ca_ei();
+#endif /* R2CA_ENABLE_PROFILING */
+	Serial3.IrqHandler();
+}
+#endif /* R2CA_USE_SERIAL3 */
+
+void
+r2ca_SERCOM3_Handler(void)
+{
+#ifdef R2CA_ENABLE_PROFILING
+	r2ca_di();
+	r2ca_sercom3_isr_cnt++;
+	r2ca_isr_cnt++;
+	r2ca_ei();
+#endif /* R2CA_ENABLE_PROFILING */    
+	((void(*)(void))(exception_table.pfnSERCOM3_Handler))();
+}
+
+void
+r2ca_SERCOM4_Handler(void)
+{
+#ifdef R2CA_ENABLE_PROFILING
+	r2ca_di();
+	r2ca_sercom4_isr_cnt++;
+	r2ca_isr_cnt++;
+	r2ca_ei();
+#endif /* R2CA_ENABLE_PROFILING */    
+	((void(*)(void))(exception_table.pfnSERCOM4_Handler))();
 }
 
 #ifdef TOPPERS_USE_ARDUINO_SERIAL
 void
-rca_SERCOM5_Handler(void)
-{
-#ifdef RCA_ENABLE_PROFILING
-    rca_di();
-    rca_sercom5_isr_cnt++;
-    rca_isr_cnt++;
-    rca_ei();
-#endif /* RCA_ENABLE_PROFILING */    
-    ((void(*)(void))(exception_table.pfnSERCOM5_Handler))();
+r2ca_SERCOM5_Handler(void)
+{
+#ifdef R2CA_ENABLE_PROFILING
+	r2ca_di();
+	r2ca_sercom5_isr_cnt++;
+	r2ca_isr_cnt++;
+	r2ca_ei();
+#endif /* R2CA_ENABLE_PROFILING */    
+	((void(*)(void))(exception_table.pfnSERCOM5_Handler))();
 }
 #endif /* TOPPERS_USE_ARDUINO_SERIAL */
 
 void
-rca_EIC_Handler(void)
-{
-#ifdef RCA_ENABLE_PROFILING
-    rca_di();
-    rca_eic_isr_cnt++;
-    rca_isr_cnt++;
-    rca_ei();
-#endif /* RCA_ENABLE_PROFILING */    
-    ((void(*)(void))(exception_table.pfnEIC_Handler))();
-}
-
-void
-rca_TC5_Handler(void)
-{
-#ifdef RCA_ENABLE_PROFILING
-    rca_di();
-    rca_tc5_isr_cnt++;
-    rca_isr_cnt++;
-    rca_ei();
-#endif /* RCA_ENABLE_PROFILING */    
-    ((void(*)(void))(exception_table.pfnTC5_Handler))();
-}
-
-void
-rca_RTC_Handler(void)
-{
-#ifdef RCA_ENABLE_PROFILING
-    rca_di();
-    rca_rtc_isr_cnt++;
-    rca_isr_cnt++;
-    rca_ei();
-#endif /* RCA_ENABLE_PROFILING */
-    ((void(*)(void))(exception_table.pfnRTC_Handler))();
+r2ca_EIC_Handler(void)
+{
+#ifdef R2CA_ENABLE_PROFILING
+	r2ca_di();
+	r2ca_eic_isr_cnt++;
+	r2ca_isr_cnt++;
+	r2ca_ei();
+#endif /* R2CA_ENABLE_PROFILING */    
+	((void(*)(void))(exception_table.pfnEIC_Handler))();
+}
+
+void
+r2ca_TC5_Handler(void)
+{
+#ifdef R2CA_ENABLE_PROFILING
+	r2ca_di();
+	r2ca_tc5_isr_cnt++;
+	r2ca_isr_cnt++;
+	r2ca_ei();
+#endif /* R2CA_ENABLE_PROFILING */    
+	((void(*)(void))(exception_table.pfnTC5_Handler))();
+}
+
+void
+r2ca_RTC_Handler(void)
+{
+#ifdef R2CA_ENABLE_PROFILING
+	r2ca_di();
+	r2ca_rtc_isr_cnt++;
+	r2ca_isr_cnt++;
+	r2ca_ei();
+#endif /* R2CA_ENABLE_PROFILING */
+	((void(*)(void))(exception_table.pfnRTC_Handler))();
 }
 
@@ -260,13 +255,11 @@
 void
 yield(void){
-#ifdef RCA_RR_SCHEDULE    
-    time_slice = 0;
-#endif /* RCA_RR_SCHEDULE */
-#ifdef RCA_ENABLE_PROFILING
-    rca_di();
-    rca_dispatch_cnt++;
-    rca_ei();
-#endif /* RCA_ENABLE_PROFILING */    
-    dly_tsk(0);
+	time_slice = 0;
+#ifdef R2CA_ENABLE_PROFILING
+	r2ca_di();
+	r2ca_dispatch_cnt++;
+	r2ca_ei();
+#endif /* R2CA_ENABLE_PROFILING */    
+	dly_tsk(0);
 }
 
@@ -274,73 +267,88 @@
 extern void loop(void);
 
-void
-rca_maintask(intptr_t exinf)
-{
-    syslog(LOG_NOTICE, "Arduino Main Task start!");
-    dly_tsk(1);
+
+const ID task_id[] = {
+#if R2CA_NUM_TASK > 0
+	R2CA_TASK1,
+#endif /* R2CA_NUM_TASK > 0 */
+#if R2CA_NUM_TASK > 1
+	R2CA_TASK2,
+#endif /* R2CA_NUM_TASK > 1 */
+#if R2CA_NUM_TASK > 2
+	R2CA_TASK3,
+#endif /* R2CA_NUM_TASK > 2 */
+#if R2CA_NUM_TASK > 3
+	R2CA_TASK4,
+#endif /* R2CA_NUM_TASK > 3 */
+#if R2CA_NUM_TASK > 4
+	R2CA_TASK5,
+#endif /* R2CA_NUM_TASK > 4 */
+};
+
+void
+r2ca_maintask(intptr_t exinf)
+{
+	int i;
+
+	syslog(LOG_NOTICE, "Arduino Main Task start!");
+
 #ifdef USBCON
-    USBDevice.init();
-    USBDevice.attach();
+	USBDevice.init();
+	USBDevice.attach();
 #endif /* USBCON */
     
-    analogReference(AR_DEFAULT);
+	analogReference(AR_DEFAULT);
     
-    setup();
+	setup();
     
-    sig_sem(SETUP_SEM);
+	for(i = 0; i < R2CA_NUM_TASK; i++) {
+		act_tsk(task_id[i]);
+	}
     
-    chg_pri(0, RCA_MAINTASK_LOOP_PRI);
-    
-    while(1){
-        loop();
-        if (serialEventRun) serialEventRun();
-    }    
-}
-
-#define RCA_TASK_BODY(NUM) \
-extern void task##NUM##_setup(void); \
-extern void task##NUM##_loop(void); \
+	while(1){
+		loop();
+		if (serialEventRun) serialEventRun();
+	}    
+}
+
+#define R2CA_TASK_BODY(NUM) \
+extern void loop##NUM(void); \
  \
 void \
-rca_task##NUM(intptr_t exinf) \
+r2ca_task##NUM(intptr_t exinf) \
 { \
-    wai_sem(SETUP_SEM);\
-    task##NUM##_setup(); \
-    sig_sem(SETUP_SEM);\
-    chg_pri(0, RCA_TASK##NUM##_LOOP_PRI); \
-         \
     while(1){ \
-        task##NUM##_loop(); \
+        loop##NUM(); \
     }     \
 }
 
-#if RCA_NUM_TASK > 0
-RCA_TASK_BODY(1)
-#endif /* RCA_NUM_TASK > 0 */
-
-#if RCA_NUM_TASK > 1
-RCA_TASK_BODY(2)
-#endif /* RCA_NUM_TASK > 1 */
-
-#if RCA_NUM_TASK > 2
-RCA_TASK_BODY(3)
-#endif /* RCA_NUM_TASK > 2 */
-
-#if RCA_NUM_TASK > 3
-RCA_TASK_BODY(4)
-#endif /* RCA_NUM_TASK > 3 */
-
-#if RCA_NUM_TASK > 4
-RCA_TASK_BODY(5)
-#endif /* RCA_NUM_TASK > 4 */
-
-#ifdef RCA_ENABLE_PROFILING
-void
-rca_idle_task(intptr_t exinf) {
-    while(1){
-        rca_di();
-        rca_idle_cnt++;
-        rca_ei();
-    }
-}
-#endif /* RCA_ENABLE_PROFILING */
+#if R2CA_NUM_TASK > 0
+R2CA_TASK_BODY(1)
+#endif /* R2CA_NUM_TASK > 0 */
+
+#if R2CA_NUM_TASK > 1
+R2CA_TASK_BODY(2)
+#endif /* R2CA_NUM_TASK > 1 */
+
+#if R2CA_NUM_TASK > 2
+R2CA_TASK_BODY(3)
+#endif /* R2CA_NUM_TASK > 2 */
+
+#if R2CA_NUM_TASK > 3
+R2CA_TASK_BODY(4)
+#endif /* R2CA_NUM_TASK > 3 */
+
+#if R2CA_NUM_TASK > 4
+R2CA_TASK_BODY(5)
+#endif /* R2CA_NUM_TASK > 4 */
+
+#ifdef R2CA_ENABLE_PROFILING
+void
+r2ca_idle_task(intptr_t exinf) {
+	while(1){
+		r2ca_di();
+		r2ca_idle_cnt++;
+		r2ca_ei();
+	}
+}
+#endif /* R2CA_ENABLE_PROFILING */
