]> Zhao Yanbai Git Server - acecode.git/commitdiff
...
authorAceVest <zhaoyanbai@126.com>
Mon, 18 Jun 2018 04:24:02 +0000 (12:24 +0800)
committerAceVest <zhaoyanbai@126.com>
Mon, 18 Jun 2018 04:24:02 +0000 (12:24 +0800)
arduino/hardware/ace/avr/cores/avr/a.sh [deleted file]
arduino/hardware/ace/avr/cores/avr/kernel.cpp [new file with mode: 0644]
arduino/hardware/ace/avr/cores/avr/kernel.h [new file with mode: 0644]
arduino/hardware/ace/avr/cores/avr/main [deleted file]
arduino/hardware/ace/avr/cores/avr/main.cpp
arduino/hardware/ace/avr/cores/avr/main.cpp.d [deleted file]
arduino/hardware/ace/avr/cores/avr/main.d [deleted file]
arduino/hardware/ace/avr/cores/avr/timer.cpp

diff --git a/arduino/hardware/ace/avr/cores/avr/a.sh b/arduino/hardware/ace/avr/cores/avr/a.sh
deleted file mode 100755 (executable)
index 1b4c2d8..0000000
+++ /dev/null
@@ -1,2 +0,0 @@
-avr-gcc -g -O0 -Wall -Wextra -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -MMD -flto -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10804 -DARDUINO_ACE_AVR -DARDUINO_ARCH_AVR   "-I/Users/ace/workspace/Arduino/hardware/ace/avr/cores/avr" "-I/Users/ace/workspace/Arduino/hardware/ace/avr/variants/avr" "/Users/ace/workspace/Arduino/hardware/ace/avr/cores/avr/main.cpp" -o "main"
-avr-gcc -c -g -O0 -Wall -Wextra -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -MMD -flto -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10804 -DARDUINO_ACE_AVR -DARDUINO_ARCH_AVR   "-I/Users/ace/workspace/Arduino/hardware/ace/avr/cores/avr" "-I/Users/ace/workspace/Arduino/hardware/ace/avr/variants/avr" "/Users/ace/workspace/Arduino/hardware/ace/avr/cores/avr/main.cpp" -o "main.cpp.o"
diff --git a/arduino/hardware/ace/avr/cores/avr/kernel.cpp b/arduino/hardware/ace/avr/cores/avr/kernel.cpp
new file mode 100644 (file)
index 0000000..8488fe5
--- /dev/null
@@ -0,0 +1,284 @@
+/*
+ * ------------------------------------------------------------------------
+ *   File Name: kernel.cpp
+ *      Author: Zhao Yanbai
+ *              2018-06-17 16:42:56 Sunday CST
+ * Description: none
+ * ------------------------------------------------------------------------
+ */
+#include <AceAvr.h>
+#include <kernel.h>
+
+void delay(unsigned long ms);
+
+struct task *current_task = 0;
+const uint8_t max_task_cnt = 8;
+struct task tasks[max_task_cnt];
+uint8_t idle_task_stack[TASK_STACK_SIZE];
+uint32_t ticks = 0;
+void task_scheduler();
+
+#define IDLE_TASK (tasks+max_task_cnt-1)
+#define LED_TASK (task+0);
+#define DEBUG_TASK (task+1);
+
+uint8_t debug_task_stack[TASK_STACK_SIZE];
+void debug_task() {
+    uint8_t pin = 12;
+    set_digital_pin_mode(pin, OUTPUT);
+
+    while(1) {
+        digital_write(pin, HIGH);
+        task_delay(20);
+        digital_write(pin, LOW);
+        task_delay(20);
+    }
+}
+
+uint8_t led_task_stack[TASK_STACK_SIZE];
+void led_task() {
+    while(1) task_delay(1000);
+    uint8_t pin = 13;
+    set_digital_pin_mode(pin, OUTPUT);
+
+    while(1) {
+        digital_write(pin, HIGH);
+        task_delay(30);
+        digital_write(pin, LOW);
+        task_delay(30);
+    }
+}
+
+
+// idle_task 在没有进程READY的情况下都会调度运行
+// 所以task_delay不能在此进程生效
+void idle_task() {
+    sei();
+    static uint32_t idle_cnt = 0;
+    uint8_t pin = 13;
+    set_digital_pin_mode(pin, OUTPUT);
+    uint8_t state = LOW;
+    while(1) {
+        idle_cnt++;
+        state = state == LOW ? HIGH : LOW;
+        digital_write(pin, state);
+        delay(1000);
+    }
+}
+
+void task_delay(uint16_t ticks)
+{
+    cli();
+    current_task->state = TASK_STATE_SLEEP;
+    current_task->delay_ticks = ticks;
+    sei();
+    task_scheduler();
+}
+
+
+void create_task(void (*handler)(), uint8_t *stack, uint8_t priority)
+{
+    if(priority >= max_task_cnt) {
+        return;
+    }
+
+    struct task *t = tasks + priority;
+    t->handler = handler;
+    t->state = TASK_STATE_READY;
+#if 0
+    t->stack = stack + TASK_STACK_SIZE - 3;
+    uint8_t *p = t->stack;
+    *(p+0) = 0x00;
+    *(p+1) = (((uint16_t)handler) >> 8);
+    *(p+2) = (((uint16_t)handler) >> 0);
+#else
+    t->stack = stack + TASK_STACK_SIZE - 2;
+    uint8_t *p = t->stack;
+    *(p+0) = (((uint16_t)handler) >> 0);
+    *(p+1) = (((uint16_t)handler) >> 8);
+#endif
+
+    *(--(t->stack)) = 0x00;   // SREG
+    *(--(t->stack)) = 0x00;   // R0
+    *(--(t->stack)) = 0x00;
+    *(--(t->stack)) = 0x00;
+    *(--(t->stack)) = 0x00;
+    *(--(t->stack)) = 0x00;
+    *(--(t->stack)) = 0x00;
+    *(--(t->stack)) = 0x00;
+    *(--(t->stack)) = 0x00;
+    *(--(t->stack)) = 0x00;
+    *(--(t->stack)) = 0x00;
+    *(--(t->stack)) = 0x00;   // R10
+    *(--(t->stack)) = 0x00;
+    *(--(t->stack)) = 0x00;
+    *(--(t->stack)) = 0x00;
+    *(--(t->stack)) = 0x00;
+    *(--(t->stack)) = 0x00;
+    *(--(t->stack)) = 0x00;
+    *(--(t->stack)) = 0x00;
+    *(--(t->stack)) = 0x00;
+    *(--(t->stack)) = 0x00;
+    *(--(t->stack)) = 0x00;   // 20
+    *(--(t->stack)) = 0x00;
+    *(--(t->stack)) = 0x00;
+    *(--(t->stack)) = 0x00;
+    *(--(t->stack)) = 0x00;
+    *(--(t->stack)) = 0x00;
+    *(--(t->stack)) = 0x00;
+    *(--(t->stack)) = 0x00;
+    *(--(t->stack)) = 0x00;
+    *(--(t->stack)) = 0x00;
+    *(--(t->stack)) = 0x00;   // 30
+    *(--(t->stack)) = 0x00;
+
+    --(t->stack);
+    // AVR 的PUSH是先存值再SP-1
+    // AVR 的POP 是先SP+1再取值
+}
+
+void task_switch(struct task *prev, struct task *next) {
+    if(prev == 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"
+    );
+
+    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"
+    "reti\n"
+    );
+}
+
+void init_tasks() {
+    cli();
+    for(uint8_t i=0; i<max_task_cnt; i++) {
+        struct task *t = tasks+i;
+        t->pid          = i;
+        t->handler      = 0;
+        t->stack        = 0;
+        t->state        = TASK_STATE_EMPTY;
+        t->delay_ticks  = 0;
+    }
+
+
+    current_task = IDLE_TASK;
+
+    create_task(idle_task, idle_task_stack, max_task_cnt-1);
+//    create_task(led_task, led_task_stack, 0);
+//    create_task(debug_task, debug_task_stack, 1);
+    IDLE_TASK->stack = idle_task_stack+TASK_STACK_SIZE-2-2;
+    SP = (uint16_t)(IDLE_TASK->stack);
+    asm("ret;");
+}
+
+void task_scheduler() {
+    return;
+    struct task *next = IDLE_TASK;
+    for(uint8_t i=0; i<max_task_cnt; i++) {
+        struct task *t = tasks+i;
+        if(TASK_STATE_READY == t->state) {
+            next = t;
+            break;
+        }
+    }
+
+    task_switch(current_task, next);
+}
+
+extern "C" void TIMER1_COMPA_vect() __attribute__ ((signal,used, externally_visible));
+void TIMER1_COMPA_vect()
+{
+    ticks++;
+
+    return;
+
+    for(uint8_t i=0; i<max_task_cnt; i++) {
+        struct task *t = tasks+i;
+        if(TASK_STATE_SLEEP != t->state) {
+            continue;
+        }
+
+        if(t->delay_ticks > 0) {
+            t->delay_ticks--;
+        }
+
+        if(t->delay_ticks == 0) {
+            t->state =  TASK_STATE_READY;
+        }
+    }
+
+    task_scheduler();
+}
diff --git a/arduino/hardware/ace/avr/cores/avr/kernel.h b/arduino/hardware/ace/avr/cores/avr/kernel.h
new file mode 100644 (file)
index 0000000..63606e4
--- /dev/null
@@ -0,0 +1,32 @@
+/*
+ * ------------------------------------------------------------------------
+ *   File Name: kernel.h
+ *      Author: Zhao Yanbai
+ *              2018-06-17 16:42:52 Sunday CST
+ * Description: none
+ * ------------------------------------------------------------------------
+ */
+
+#pragma once
+#include <stdint.h>
+
+#define TASK_STACK_SIZE 64
+
+enum task_state {
+    TASK_STATE_EMPTY  = 0,
+    TASK_STATE_READY  = 1,
+    TASK_STATE_SLEEP  = 2,
+};
+
+struct task {
+    uint8_t pid;
+    enum task_state state;
+    void (*handler)();
+    uint8_t* stack;
+    uint16_t delay_ticks;
+};
+
+void init_tasks();
+void create_task(void (*handler)(), char *stack, uint8_t priority);
+void task_switch(struct task *prev, struct task *next);
+void task_delay(uint16_t ticks);
diff --git a/arduino/hardware/ace/avr/cores/avr/main b/arduino/hardware/ace/avr/cores/avr/main
deleted file mode 100755 (executable)
index 0327266..0000000
Binary files a/arduino/hardware/ace/avr/cores/avr/main and /dev/null differ
index 6892650e6f4c29f639d05dc1445fc9d5a6403f53..8995be4b089c050fb541735f34f15b9d88c93272 100644 (file)
@@ -6,38 +6,90 @@
  * Description: none
  * ------------------------------------------------------------------------
  */
-
 #include <Arduino.h>
+
 void init_timer0();
 void init_timer1();
+void init_tasks();
 
+#if 1
 void init() {
-    sei();
+    cli();
     init_timer0();
-    init_timer1();
+    //init_timer1();
+    //init_tasks();
+    sei();
 }
 
-extern uint8_t tick;
 int main()
 {
     init();
 
-    uint8_t pin = 12;
+    uint8_t pin = 13;
     set_digital_pin_mode(pin, OUTPUT);
+    digital_write(pin, LOW);
 
-    int n = 0;
     uint8_t state = LOW;
     while(1) {
-        n++;
+        state = state == LOW ? HIGH : LOW;
+        digital_write(pin, state);
+        delay(1000);
+    }
+
+
+    return 0;
+}
+#else
+
+uint8_t stack[128];
+
+void handler() {
+    uint8_t pin = 12;
+    set_digital_pin_mode(pin, OUTPUT);
+    digital_write(pin, LOW);
 
-        if(tick > 100) {
-            state = state == LOW ? HIGH : LOW;
-            tick = 0;
-        }
 
+    uint8_t state = LOW;
+    while(1) {
+        state = state == LOW ? HIGH : LOW;
         digital_write(pin, state);
-        delay(10);
+        delay(1000);
     }
+}
+
+
+uint8_t * volatile p = 0;
+int main()
+{
+    sei();
+    init_timer0();
+
+    p = stack+64;
+
+    *(p+0) = 0;
+    *(p+1) = 0; //(((uint16_t)handler) >> 8);
+    *(p+2) = (((uint16_t)handler) >> 0);
+    *(p+3) = (((uint16_t)handler) >> 8);
+
+
+    asm("nop");
+    asm("nop");
+    #if 1
+    SP = (uint16_t) p;
+    #else
+    asm volatile (
+        "lds r26, p\n"
+        "lds r27, p+1\n"
+        "out __SP_L__, r26\n"
+        "out __SP_H__, r27\n"
+        );
+    #endif
+    asm("nop");
+    asm("nop");
+
+    asm("ret");
 
     return 0;
 }
+
+#endif
diff --git a/arduino/hardware/ace/avr/cores/avr/main.cpp.d b/arduino/hardware/ace/avr/cores/avr/main.cpp.d
deleted file mode 100644 (file)
index ded7b7e..0000000
+++ /dev/null
@@ -1,4 +0,0 @@
-main.cpp.o: \
- /Users/ace/workspace/Arduino/hardware/ace/avr/cores/avr/main.cpp \
- /Users/ace/workspace/Arduino/hardware/ace/avr/cores/avr/Arduino.h \
- /Users/ace/workspace/Arduino/hardware/ace/avr/cores/avr/AceAvr.h
diff --git a/arduino/hardware/ace/avr/cores/avr/main.d b/arduino/hardware/ace/avr/cores/avr/main.d
deleted file mode 100644 (file)
index cade3be..0000000
+++ /dev/null
@@ -1,3 +0,0 @@
-main: /Users/ace/workspace/Arduino/hardware/ace/avr/cores/avr/main.cpp \
- /Users/ace/workspace/Arduino/hardware/ace/avr/cores/avr/Arduino.h \
- /Users/ace/workspace/Arduino/hardware/ace/avr/cores/avr/AceAvr.h
index 9a3092ccbe3d4d8435e858c2003b10f4c9905ff3..5ebc6e2a9206f7162febbfafd7e58506ed5fea7f 100644 (file)
@@ -65,22 +65,20 @@ void init_timer0() {
     sbi(TIMSK0, TOIE0);
 }
 
-
-
-
 void init_timer1() {
-  //set timer1 interrupt at 100Hz
-  TCCR1A = 0;// set entire TCCR1A register to 0
-  TCCR1B = 0;// same for TCCR1B
-  TCNT1  = 0;//initialize counter value to 0
-  // set compare match register for 100Hz increments
-  OCR1A = 155; // = (16*10^6) / (1024*100Hz) - 1 (must be <65536)
-  // turn on CTC mode
-  TCCR1B |= (1 << WGM12);
-  // Set CS10 and CS12 bits for 1024 prescaler
-  TCCR1B |= (1 << CS12) | (1 << CS10);  
-  // enable timer compare interrupt
-  TIMSK1 |= (1 << OCIE1A);
+    //set timer1 interrupt at 100Hz
+    TCCR1A = 0;// set entire TCCR1A register to 0
+    TCCR1B = 0;// same for TCCR1B
+    TCNT1  = 0;//initialize counter value to 0
+    // set compare match register for 100Hz increments
+    //OCR1A = 155; // = (16*10^6) / (1024*100Hz) - 1 (must be <65536)
+    OCR1A = 15500; // = (16*10^6) / (1024*100Hz) - 1 (must be <65536)
+    // turn on CTC mode
+    TCCR1B |= (1 << WGM12);
+    // Set CS10 and CS12 bits for 1024 prescaler
+    TCCR1B |= (1 << CS12) | (1 << CS10);
+    // enable timer compare interrupt
+    TIMSK1 |= (1 << OCIE1A);
 }
 
 extern "C" void TIMER0_OVF_vect() __attribute__ ((signal,used, externally_visible));
@@ -88,11 +86,3 @@ void TIMER0_OVF_vect()
 {
     timer0_overflow_count++;
 }
-
-
-extern "C" void TIMER1_COMPA_vect() __attribute__ ((signal,used, externally_visible));
-uint8_t tick = 0;
-void TIMER1_COMPA_vect()
-{
-    tick++;
-}