]> Zhao Yanbai Git Server - acecode.git/commitdiff
...
authorAceVest <zhaoyanbai@126.com>
Mon, 18 Jun 2018 08:30:54 +0000 (16:30 +0800)
committerAceVest <zhaoyanbai@126.com>
Mon, 18 Jun 2018 08:30:54 +0000 (16:30 +0800)
arduino/hardware/ace/avr/cores/avr/app.cpp
arduino/hardware/ace/avr/cores/avr/kernel.cpp
arduino/hardware/ace/avr/cores/avr/kernel.h
arduino/hardware/ace/avr/cores/avr/timer.cpp

index 1806ef3b770a418f444edc2ed37de9eb9f003fb7..873c2b3c0506f42ac238124260d933c3d1587ba4 100644 (file)
@@ -30,12 +30,25 @@ void led_task() {
 
 uint8_t user_task1_stack[TASK_STACK_SIZE];
 void user_task1() {
+    int cnt = 0;
     while(1) {
-        Serial.println("user task 1");
+        Serial.print("user task 1, cnt: ");
+        Serial.println(cnt++);
         task_delay(100);
     }
 }
 
+#if 1
+uint8_t user_task2_stack[TASK_STACK_SIZE];
+void user_task2() {
+    int cnt = 0;
+    while(1) {
+        Serial.print("user task 2, cnt: ");
+        Serial.println(cnt++);
+        task_delay(100);
+    }
+}
+#endif
 
 
 
@@ -43,6 +56,7 @@ void app_main() {
     Serial.begin(9600);
     create_task(led_task, led_task_stack, 0);
     create_task(user_task1, user_task1_stack, 1);
+    create_task(user_task2, user_task2_stack, 2);
     create_task(debug_task, debug_task_stack, 3);
 }
 
index 64a571fa8964e54a48379ad730707262e8af0331..17b1cd6aae77662c169726627c8132a15c70cc3b 100644 (file)
@@ -113,84 +113,15 @@ void task_switch(struct task *prev, struct task *next) {
         return;
     }
 
-    __asm__ __volatile__(
-    "PUSH R31\n"
-    "PUSH R30\n"
-    "PUSH R29\n"
-    "PUSH R28\n"
-    "PUSH R27\n"
-    "PUSH R26\n"
-    "PUSH R25\n"
-    "PUSH R24\n"
-    "PUSH R23\n"
-    "PUSH R22\n"
-    "PUSH R21\n"
-    "PUSH R20\n"
-    "PUSH R19\n"
-    "PUSH R18\n"
-    "PUSH R17\n"
-    "PUSH R16\n"
-    "PUSH R15\n"
-    "PUSH R14\n"
-    "PUSH R13\n"
-    "PUSH R12\n"
-    "PUSH R11\n"
-    "PUSH R10\n"
-    "PUSH R09\n"
-    "PUSH R08\n"
-    "PUSH R07\n"
-    "PUSH R06\n"
-    "PUSH R05\n"
-    "PUSH R04\n"
-    "PUSH R03\n"
-    "PUSH R02\n"
-    "PUSH R01\n"
-    "PUSH R00\n"
-    "IN R00, __SREG__\n"
-    "PUSH R00\n"
-    );
+    SAVE_CONTEXT;
 
     prev->stack = (uint8_t *) SP;
     SP = ((uint16_t)next->stack);
     current_task = next;
 
-    __asm__ __volatile__(
-    "POP R00\n"
-    "OUT __SREG__, R00\n"
-    "POP R00\n"
-    "POP R01\n"
-    "POP R02\n"
-    "POP R03\n"
-    "POP R04\n"
-    "POP R05\n"
-    "POP R06\n"
-    "POP R07\n"
-    "POP R08\n"
-    "POP R09\n"
-    "POP R10\n"
-    "POP R11\n"
-    "POP R12\n"
-    "POP R13\n"
-    "POP R14\n"
-    "POP R15\n"
-    "POP R16\n"
-    "POP R17\n"
-    "POP R18\n"
-    "POP R19\n"
-    "POP R20\n"
-    "POP R21\n"
-    "POP R22\n"
-    "POP R23\n"
-    "POP R24\n"
-    "POP R25\n"
-    "POP R26\n"
-    "POP R27\n"
-    "POP R28\n"
-    "POP R29\n"
-    "POP R30\n"
-    "POP R31\n"
-    "ret\n"
-    );
+    RESTORE_CONTEXT;
+    __asm__ __volatile__("ret");
+
 }
 
 void task_scheduler() {
@@ -218,6 +149,7 @@ void yield(void)
 extern "C" void TIMER1_COMPA_vect() __attribute__ ((signal,used, externally_visible));
 void TIMER1_COMPA_vect()
 {
+    SAVE_CONTEXT;
     ticks++;
 
     for(uint8_t i=0; i<task_cnt; i++) {
@@ -236,6 +168,7 @@ void TIMER1_COMPA_vect()
     }
 
     task_scheduler();
+    RESTORE_CONTEXT;
 }
 
 void init_timer1() {
index 517f19e4cdeae3265a404c8496e61096614e0932..3f0fb5037ea6392d472cdd793aaa9cc8c3c138b3 100644 (file)
@@ -11,7 +11,7 @@
 #include <stdint.h>
 
 #define MAX_TASK_CNT    8
-#define TASK_STACK_SIZE 64
+#define TASK_STACK_SIZE 256
 
 #if MAX_TASK_CNT > 32
 #error "too much tasks"
@@ -36,3 +36,80 @@ void create_task(void (*handler)(), uint8_t *stack, uint8_t priority);
 void task_switch(struct task *prev, struct task *next);
 void task_delay(uint16_t ticks);
 //void yield(void);
+
+#define SAVE_CONTEXT  \
+    __asm__ __volatile__( \
+    "PUSH R31\n"    \
+    "PUSH R30\n"    \
+    "PUSH R29\n"    \
+    "PUSH R28\n"    \
+    "PUSH R27\n"    \
+    "PUSH R26\n"    \
+    "PUSH R25\n"    \
+    "PUSH R24\n"    \
+    "PUSH R23\n"    \
+    "PUSH R22\n"    \
+    "PUSH R21\n"    \
+    "PUSH R20\n"    \
+    "PUSH R19\n"    \
+    "PUSH R18\n"    \
+    "PUSH R17\n"    \
+    "PUSH R16\n"    \
+    "PUSH R15\n"    \
+    "PUSH R14\n"    \
+    "PUSH R13\n"    \
+    "PUSH R12\n"    \
+    "PUSH R11\n"    \
+    "PUSH R10\n"    \
+    "PUSH R09\n"    \
+    "PUSH R08\n"    \
+    "PUSH R07\n"    \
+    "PUSH R06\n"    \
+    "PUSH R05\n"    \
+    "PUSH R04\n"    \
+    "PUSH R03\n"    \
+    "PUSH R02\n"    \
+    "PUSH R01\n"    \
+    "PUSH R00\n"    \
+    "IN R00, __SREG__\n"    \
+    "PUSH R00\n"    \
+    );
+
+#define RESTORE_CONTEXT    \
+    __asm__ __volatile__(    \
+    "POP R00\n"    \
+    "OUT __SREG__, R00\n"    \
+    "POP R00\n"    \
+    "POP R01\n"    \
+    "POP R02\n"    \
+    "POP R03\n"    \
+    "POP R04\n"    \
+    "POP R05\n"    \
+    "POP R06\n"    \
+    "POP R07\n"    \
+    "POP R08\n"    \
+    "POP R09\n"    \
+    "POP R10\n"    \
+    "POP R11\n"    \
+    "POP R12\n"    \
+    "POP R13\n"    \
+    "POP R14\n"    \
+    "POP R15\n"    \
+    "POP R16\n"    \
+    "POP R17\n"    \
+    "POP R18\n"    \
+    "POP R19\n"    \
+    "POP R20\n"    \
+    "POP R21\n"    \
+    "POP R22\n"    \
+    "POP R23\n"    \
+    "POP R24\n"    \
+    "POP R25\n"    \
+    "POP R26\n"    \
+    "POP R27\n"    \
+    "POP R28\n"    \
+    "POP R29\n"    \
+    "POP R30\n"    \
+    "POP R31\n"    \
+    );
+
index b101177c681225b6b01b7ee777cadb0dd864e783..7ec5d3af4b02fdb8eca8809e0f4512c3d75824d9 100644 (file)
@@ -75,6 +75,7 @@ void init_timer0() {
 extern "C" void TIMER0_OVF_vect() __attribute__ ((signal,used, externally_visible));
 void TIMER0_OVF_vect()
 {
+    SAVE_CONTEXT;
     unsigned long m = timer0_millis;
     unsigned char f = timer0_fract;
 
@@ -89,6 +90,7 @@ void TIMER0_OVF_vect()
     timer0_millis = m;
 
     timer0_overflow_count++;
+    RESTORE_CONTEXT;
 }