Add eeprom driver and command loop
[fw/altos] / ao_task.c
index 4f7387e310f595fe921b6a3b3c604d2ebd934ce3..f79d6e1f184aa530917f59050856a00256b61860 100644 (file)
--- a/ao_task.c
+++ b/ao_task.c
@@ -3,8 +3,7 @@
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
+ * the Free Software Foundation; version 2 of the License.
  *
  * This program is distributed in the hope that it will be useful, but
  * WITHOUT ANY WARRANTY; without even the implied warranty of
 
 #include "ao.h"
 
-#define AO_NO_TASK     0xff
+#define AO_NO_TASK_INDEX       0xff
 
 __xdata struct ao_task * __xdata ao_tasks[AO_NUM_TASKS];
 __data uint8_t ao_num_tasks;
-__data uint8_t ao_cur_task_id;
+__data uint8_t ao_cur_task_index;
 __xdata struct ao_task *__data ao_cur_task;
 
 void
@@ -30,8 +29,9 @@ ao_add_task(__xdata struct ao_task * task, void (*start)(void))
 {
        uint8_t __xdata *stack;
        if (ao_num_tasks == AO_NUM_TASKS)
-               ao_panic(AO_ERROR_NO_TASK);
+               ao_panic(AO_PANIC_NO_TASK);
        ao_tasks[ao_num_tasks++] = task;
+       task->task_id = ao_num_tasks;
        /*
         * Construct a stack frame so that it will 'return'
         * to the start of the task
@@ -74,9 +74,9 @@ ao_yield(void) _naked
                /* Push ACC first, as when restoring the context it must be restored
                 * last (it is used to set the IE register). */
                push    ACC
-               /* Store the IE register then disable interrupts. */
+               /* Store the IE register then enable interrupts. */
                push    _IEN0
-               clr     _EA
+               setb    _EA
                push    DPL
                push    DPH
                push    b
@@ -95,10 +95,10 @@ ao_yield(void) _naked
                push    _bp
        _endasm;
        
-       if (ao_cur_task_id != AO_NO_TASK)
+       if (ao_cur_task_index != AO_NO_TASK_INDEX)
        {
                /* Save the current stack */
-               stack_len = SP - AO_STACK_START;
+               stack_len = SP - (AO_STACK_START - 1);
                ao_cur_task->stack_count = stack_len;
                stack_ptr = (uint8_t __data *) AO_STACK_START;
                save_ptr = (uint8_t __xdata *) ao_cur_task->stack;
@@ -107,27 +107,28 @@ ao_yield(void) _naked
        }
        
        /* Empty the stack; might as well let interrupts have the whole thing */
-       SP = AO_STACK_START;
+       SP = AO_STACK_START - 1;
 
        /* Find a task to run. If there isn't any runnable task,
         * this loop will run forever, which is just fine
         */
        for (;;) {
-               ++ao_cur_task_id;
-               if (ao_cur_task_id == ao_num_tasks)
-                       ao_cur_task_id = 0;
-               ao_cur_task = ao_tasks[ao_cur_task_id];
+               ++ao_cur_task_index;
+               if (ao_cur_task_index == ao_num_tasks)
+                       ao_cur_task_index = 0;
+               ao_cur_task = ao_tasks[ao_cur_task_index];
                if (ao_cur_task->wchan == NULL)
                        break;
        }
 
        /* Restore the old stack */
        stack_len = ao_cur_task->stack_count;
+       SP = AO_STACK_START - 1 + stack_len;
+
        stack_ptr = (uint8_t __data *) AO_STACK_START;
        save_ptr = (uint8_t __xdata *) ao_cur_task->stack;
        while (stack_len--)
                *stack_ptr++ = *save_ptr++;
-       SP = (uint8_t) (stack_ptr - 1);
 
        _asm
                pop             _bp
@@ -162,7 +163,9 @@ ao_yield(void) _naked
 int
 ao_sleep(__xdata void *wchan)
 {
+       __critical {
        ao_cur_task->wchan = wchan;
+       }
        ao_yield();
 }
 
@@ -179,7 +182,8 @@ ao_wakeup(__xdata void *wchan)
 void
 ao_start_scheduler(void)
 {
-       ao_cur_task_id = AO_NO_TASK;
+
+       ao_cur_task_index = AO_NO_TASK_INDEX;
        ao_cur_task = NULL;
        ao_yield();
 }