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