65654731aeaeaccc9f45477e156ffa1419d13c0b
[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 #define AO_CHECK_STACK  0
32
33 #if AO_CHECK_STACK
34 static uint8_t  in_yield;
35
36 static inline void ao_check_stack(void) {
37         uint8_t q;
38         if (!in_yield && ao_cur_task && &q < &ao_cur_task->stack[0])
39                 ao_panic(AO_PANIC_STACK);
40 }
41 #else
42 #define ao_check_stack()
43 #endif
44
45 void
46 ao_add_task(__xdata struct ao_task * task, void (*start)(void), __code char *name) __reentrant
47 {
48         uint8_t task_id;
49         uint8_t t;
50         if (ao_num_tasks == AO_NUM_TASKS)
51                 ao_panic(AO_PANIC_NO_TASK);
52         for (task_id = 1; task_id != 0; task_id++) {
53                 for (t = 0; t < ao_num_tasks; t++)
54                         if (ao_tasks[t]->task_id == task_id)
55                                 break;
56                 if (t == ao_num_tasks)
57                         break;
58         }
59         ao_tasks[ao_num_tasks++] = task;
60         task->task_id = task_id;
61         task->name = name;
62         task->wchan = NULL;
63         /*
64          * Construct a stack frame so that it will 'return'
65          * to the start of the task
66          */
67         ao_arch_init_stack(task, start);
68 }
69
70 /* Task switching function. This must not use any stack variables */
71 void
72 ao_yield(void) ao_arch_naked_define
73 {
74         ao_arch_save_regs();
75
76         if (ao_cur_task_index == AO_NO_TASK_INDEX)
77                 ao_cur_task_index = ao_num_tasks-1;
78         else
79         {
80                 ao_arch_save_stack();
81         }
82
83         ao_arch_isr_stack();
84
85 #if AO_CHECK_STACK
86         in_yield = 1;
87 #endif
88         /* Find a task to run. If there isn't any runnable task,
89          * this loop will run forever, which is just fine
90          */
91         {
92                 __pdata uint8_t ao_last_task_index = ao_cur_task_index;
93                 for (;;) {
94                         ++ao_cur_task_index;
95                         if (ao_cur_task_index == ao_num_tasks)
96                                 ao_cur_task_index = 0;
97
98                         ao_cur_task = ao_tasks[ao_cur_task_index];
99
100                         /* Check for ready task */
101                         if (ao_cur_task->wchan == NULL)
102                                 break;
103
104                         /* Check if the alarm is set for a time which has passed */
105                         if (ao_cur_task->alarm &&
106                             (int16_t) (ao_time() - ao_cur_task->alarm) >= 0)
107                                 break;
108
109                         /* Enter lower power mode when there isn't anything to do */
110                         if (ao_cur_task_index == ao_last_task_index)
111                                 ao_arch_cpu_idle();
112                 }
113         }
114 #if AO_CHECK_STACK
115         cli();
116         in_yield = 0;
117 #endif
118         ao_arch_restore_stack();
119 }
120
121 uint8_t
122 ao_sleep(__xdata void *wchan)
123 {
124         ao_cur_task->wchan = wchan;
125         ao_yield();
126         if (ao_cur_task->wchan) {
127                 ao_cur_task->wchan = NULL;
128                 ao_cur_task->alarm = 0;
129                 return 1;
130         }
131         return 0;
132 }
133
134 void
135 ao_wakeup(__xdata void *wchan)
136 {
137         uint8_t i;
138
139         ao_check_stack();
140         for (i = 0; i < ao_num_tasks; i++)
141                 if (ao_tasks[i]->wchan == wchan)
142                         ao_tasks[i]->wchan = NULL;
143 }
144
145 void
146 ao_alarm(uint16_t delay)
147 {
148         /* Make sure we sleep *at least* delay ticks, which means adding
149          * one to account for the fact that we may be close to the next tick
150          */
151         if (!(ao_cur_task->alarm = ao_time() + delay + 1))
152                 ao_cur_task->alarm = 1;
153 }
154
155 void
156 ao_clear_alarm(void)
157 {
158         ao_cur_task->alarm = 0;
159 }
160
161 static __xdata uint8_t ao_forever;
162
163 void
164 ao_delay(uint16_t ticks)
165 {
166         ao_alarm(ticks);
167         ao_sleep(&ao_forever);
168         ao_clear_alarm();
169 }
170
171 void
172 ao_exit(void)
173 {
174         ao_arch_critical(
175                 uint8_t i;
176                 ao_num_tasks--;
177                 for (i = ao_cur_task_index; i < ao_num_tasks; i++)
178                         ao_tasks[i] = ao_tasks[i+1];
179                 ao_cur_task_index = AO_NO_TASK_INDEX;
180                 ao_yield();
181                 );
182         /* we'll never get back here */
183 }
184
185 void
186 ao_task_info(void)
187 {
188         uint8_t         i;
189         __xdata struct ao_task *task;
190
191         for (i = 0; i < ao_num_tasks; i++) {
192                 task = ao_tasks[i];
193                 printf("%12s: wchan %04x\n",
194                        task->name,
195                        (int) task->wchan);
196         }
197 }
198
199 void
200 ao_start_scheduler(void)
201 {
202         ao_cur_task_index = AO_NO_TASK_INDEX;
203         ao_cur_task = NULL;
204         ao_yield();
205 }