Send computed accel/vel/pres values over the radio
[fw/altos] / ao_task.c
index 0af5043cd67738ea33c1976ce8b569f761c3f1de..06c279e9faa7eb9119cbb53eda77195842f8b680 100644 (file)
--- a/ao_task.c
+++ b/ao_task.c
 
 #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
-ao_add_task(__xdata struct ao_task * task, void (*start)(void))
+ao_add_task(__xdata struct ao_task * task, void (*start)(void), __code char *name) __reentrant
 {
        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;
+       task->name = name;
        /*
         * Construct a stack frame so that it will 'return'
         * to the start of the task
@@ -64,9 +66,6 @@ ao_add_task(__xdata struct ao_task * task, void (*start)(void))
 void
 ao_yield(void) _naked
 {
-       static uint8_t __data stack_len;
-       static __data uint8_t * __data stack_ptr;
-       static __xdata uint8_t * __data save_ptr;
 
        /* Save current context */
        _asm
@@ -94,15 +93,19 @@ ao_yield(void) _naked
                push    _bp
        _endasm;
        
-       if (ao_cur_task_id != AO_NO_TASK)
+       if (ao_cur_task_index != AO_NO_TASK_INDEX)
        {
+               uint8_t stack_len;
+               __data uint8_t *stack_ptr;
+               __xdata uint8_t *save_ptr;
                /* Save the current stack */
                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;
-               while (stack_len--)
+               do
                        *save_ptr++ = *stack_ptr++;
+               while (--stack_len);
        }
        
        /* Empty the stack; might as well let interrupts have the whole thing */
@@ -111,23 +114,40 @@ ao_yield(void) _naked
        /* 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];
-               if (ao_cur_task->wchan == NULL)
-                       break;
+       {
+               __pdata uint8_t ao_next_task_index = ao_cur_task_index;
+               for (;;) {
+                       ++ao_next_task_index;
+                       if (ao_next_task_index == ao_num_tasks)
+                               ao_next_task_index = 0;
+
+                       ao_cur_task = ao_tasks[ao_next_task_index];
+                       if (ao_cur_task->wchan == NULL) {
+                               ao_cur_task_index = ao_next_task_index;
+                               break;
+                       }
+
+                       /* Enter lower power mode when there isn't anything to do */
+                       if (ao_next_task_index == ao_cur_task_index)
+                               PCON = PCON_IDLE;
+               }
        }
 
-       /* Restore the old stack */
-       stack_len = ao_cur_task->stack_count;
-       SP = AO_STACK_START - 1 + stack_len;
+       {
+               uint8_t stack_len;
+               __data uint8_t *stack_ptr;
+               __xdata uint8_t *save_ptr;
 
-       stack_ptr = (uint8_t __data *) AO_STACK_START;
-       save_ptr = (uint8_t __xdata *) ao_cur_task->stack;
-       while (stack_len--)
-               *stack_ptr++ = *save_ptr++;
+               /* 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;
+               do
+                       *stack_ptr++ = *save_ptr++;
+               while (--stack_len);
+       }
 
        _asm
                pop             _bp
@@ -159,14 +179,16 @@ ao_yield(void) _naked
        _endasm;
 }
 
-int
+void
 ao_sleep(__xdata void *wchan)
 {
-       ao_cur_task->wchan = wchan;
+       __critical {
+               ao_cur_task->wchan = wchan;
+       }
        ao_yield();
 }
 
-int
+void
 ao_wakeup(__xdata void *wchan)
 {
        uint8_t i;
@@ -176,10 +198,27 @@ ao_wakeup(__xdata void *wchan)
                        ao_tasks[i]->wchan = NULL;
 }
 
+void
+ao_task_info(void)
+{
+       uint8_t i;
+       uint8_t pc_loc;
+       __xdata struct ao_task *task;
+
+       for (i = 0; i < ao_num_tasks; i++) {
+               task = ao_tasks[i];
+               pc_loc = task->stack_count - 17;
+               printf("%12s: wchan %04x pc %04x\n",
+                      task->name,
+                      (int16_t) task->wchan,
+                      (task->stack[pc_loc]) | (task->stack[pc_loc+1] << 8));
+       }
+}
+
 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();
 }