altos: Note that Lithium battery may be included with MicroPeak
[fw/altos] / src / core / ao_task.h
1 /*
2  * Copyright © 2012 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 #ifndef _AO_TASK_H_
19 #define _AO_TASK_H_
20 #if HAS_TASK_QUEUE
21 #include <ao_list.h>
22 #endif
23
24 /* An AltOS task */
25 struct ao_task {
26         __xdata void *wchan;            /* current wait channel (NULL if running) */
27         uint16_t alarm;                 /* abort ao_sleep time */
28         ao_arch_task_members            /* any architecture-specific fields */
29         uint8_t task_id;                /* unique id */
30         __code char *name;              /* task name */
31 #if HAS_TASK_QUEUE
32         struct ao_list  queue;
33         struct ao_list  alarm_queue;
34 #endif
35         uint8_t stack[AO_STACK_SIZE];   /* saved stack */
36 #if HAS_SAMPLE_PROFILE
37         uint32_t ticks;
38         uint32_t yields;
39         uint16_t start;
40         uint16_t max_run;
41 #endif
42 };
43
44 #define AO_NUM_TASKS            16      /* maximum number of tasks */
45 #define AO_NO_TASK              0       /* no task id */
46
47 extern __xdata struct ao_task * __xdata ao_tasks[AO_NUM_TASKS];
48 extern __data uint8_t ao_num_tasks;
49 extern __xdata struct ao_task *__data ao_cur_task;
50
51 /*
52  ao_task.c
53  */
54
55 /* Suspend the current task until wchan is awoken.
56  * returns:
57  *  0 on normal wake
58  *  1 on alarm
59  */
60 uint8_t
61 ao_sleep(__xdata void *wchan);
62
63 /* Wake all tasks sleeping on wchan */
64 void
65 ao_wakeup(__xdata void *wchan);
66
67 /* set an alarm to go off in 'delay' ticks */
68 void
69 ao_alarm(uint16_t delay);
70
71 /* Clear any pending alarm */
72 void
73 ao_clear_alarm(void);
74
75 /* Yield the processor to another task */
76 void
77 ao_yield(void) ao_arch_naked_declare;
78
79 /* Add a task to the run queue */
80 void
81 ao_add_task(__xdata struct ao_task * task, void (*start)(void), __code char *name) __reentrant;
82
83 #if HAS_TASK_QUEUE
84 /* Called on timer interrupt to check alarms */
85 extern uint16_t ao_task_alarm_tick;
86 void
87 ao_task_check_alarm(uint16_t tick);
88 #endif
89
90 /* Terminate the current task */
91 void
92 ao_exit(void);
93
94 /* Dump task info to console */
95 void
96 ao_task_info(void);
97
98 /* Start the scheduler. This will not return */
99 void
100 ao_start_scheduler(void);
101
102 #if HAS_TASK_QUEUE
103 void
104 ao_task_init(void);
105 #else
106 #define ao_task_init()
107 #endif
108
109 #endif