altos: Implement remote launch protocol
[fw/altos] / src / core / ao_task.c
1 /*
2  * Copyright © 2009 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17
18 #include "ao.h"
19
20 #define AO_NO_TASK_INDEX        0xff
21
22 __xdata struct ao_task * __xdata ao_tasks[AO_NUM_TASKS];
23 __data uint8_t ao_num_tasks;
24 __data uint8_t ao_cur_task_index;
25 __xdata struct ao_task *__data ao_cur_task;
26
27 #ifdef ao_arch_task_globals
28 ao_arch_task_globals
29 #endif
30
31 void
32 ao_add_task(__xdata struct ao_task * task, void (*start)(void), __code char *name) __reentrant
33 {
34         uint8_t task_id;
35         uint8_t t;
36         if (ao_num_tasks == AO_NUM_TASKS)
37                 ao_panic(AO_PANIC_NO_TASK);
38         for (task_id = 1; task_id != 0; task_id++) {
39                 for (t = 0; t < ao_num_tasks; t++)
40                         if (ao_tasks[t]->task_id == task_id)
41                                 break;
42                 if (t == ao_num_tasks)
43                         break;
44         }
45         ao_tasks[ao_num_tasks++] = task;
46         task->task_id = task_id;
47         task->name = name;
48         task->wchan = NULL;
49         /*
50          * Construct a stack frame so that it will 'return'
51          * to the start of the task
52          */
53         ao_arch_init_stack(task, start);
54 }
55
56 /* Task switching function. This must not use any stack variables */
57 void
58 ao_yield(void) ao_arch_naked_define
59 {
60         ao_arch_save_regs();
61
62         if (ao_cur_task_index == AO_NO_TASK_INDEX)
63                 ao_cur_task_index = ao_num_tasks-1;
64         else
65         {
66                 ao_arch_save_stack();
67         }
68
69         ao_arch_isr_stack();
70
71         /* Find a task to run. If there isn't any runnable task,
72          * this loop will run forever, which is just fine
73          */
74         {
75                 __pdata uint8_t ao_next_task_index = ao_cur_task_index;
76                 for (;;) {
77                         ++ao_next_task_index;
78                         if (ao_next_task_index == ao_num_tasks)
79                                 ao_next_task_index = 0;
80
81                         ao_cur_task = ao_tasks[ao_next_task_index];
82                         if (ao_cur_task->wchan == NULL) {
83                                 ao_cur_task_index = ao_next_task_index;
84                                 break;
85                         }
86
87                         /* Check if the alarm is set for a time which has passed */
88                         if (ao_cur_task->alarm &&
89                             (int16_t) (ao_time() - ao_cur_task->alarm) >= 0) {
90                                 ao_cur_task_index = ao_next_task_index;
91                                 break;
92                         }
93
94                         /* Enter lower power mode when there isn't anything to do */
95                         if (ao_next_task_index == ao_cur_task_index) {
96                                 ao_arch_cpu_idle();
97                         }
98                 }
99         }
100         ao_arch_restore_stack();
101 }
102
103 uint8_t
104 ao_sleep(__xdata void *wchan)
105 {
106         ao_arch_critical(
107                 ao_cur_task->wchan = wchan;
108                 );
109         ao_yield();
110         if (ao_cur_task->wchan) {
111                 ao_cur_task->wchan = NULL;
112                 return 1;
113         }
114         return 0;
115 }
116
117 void
118 ao_wakeup(__xdata void *wchan)
119 {
120         uint8_t i;
121
122         for (i = 0; i < ao_num_tasks; i++)
123                 if (ao_tasks[i]->wchan == wchan)
124                         ao_tasks[i]->wchan = NULL;
125 }
126
127 void
128 ao_alarm(uint16_t delay)
129 {
130         /* Make sure we sleep *at least* delay ticks, which means adding
131          * one to account for the fact that we may be close to the next tick
132          */
133         if (!(ao_cur_task->alarm = ao_time() + delay + 1))
134                 ao_cur_task->alarm = 1;
135 }
136
137 void
138 ao_clear_alarm(void)
139 {
140         ao_cur_task->alarm = 0;
141 }
142
143 void
144 ao_exit(void)
145 {
146         ao_arch_critical(
147                 uint8_t i;
148                 ao_num_tasks--;
149                 for (i = ao_cur_task_index; i < ao_num_tasks; i++)
150                         ao_tasks[i] = ao_tasks[i+1];
151                 ao_cur_task_index = AO_NO_TASK_INDEX;
152                 ao_yield();
153                 );
154         /* we'll never get back here */
155 }
156
157 void
158 ao_task_info(void)
159 {
160         uint8_t i;
161         __xdata struct ao_task *task;
162
163         for (i = 0; i < ao_num_tasks; i++) {
164                 task = ao_tasks[i];
165                 printf("%12s: wchan %04x\n",
166                        task->name,
167                        (int16_t) task->wchan);
168         }
169 }
170
171 void
172 ao_start_scheduler(void)
173 {
174         ao_cur_task_index = AO_NO_TASK_INDEX;
175         ao_cur_task = NULL;
176         ao_yield();
177 }