altos: Declare task stack as union of uint8_t and uint32_t
[fw/altos] / src / kernel / 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; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 #ifndef _AO_TASK_H_
20 #define _AO_TASK_H_
21 #if HAS_TASK_QUEUE
22 #include <ao_list.h>
23 #endif
24
25 #ifndef HAS_TASK_INFO
26 #define HAS_TASK_INFO 1
27 #endif
28
29 /* arm stacks must be 32-bit aligned */
30 #ifndef AO_STACK_ALIGNMENT
31 #ifdef __arm__
32 #define AO_STACK_ALIGNMENT __attribute__ ((aligned(4)))
33 #else
34 #define AO_STACK_ALIGNMENT
35 #endif
36 #endif
37
38 /* An AltOS task */
39 struct ao_task {
40         void *wchan;                    /* current wait channel (NULL if running) */
41         uint16_t alarm;                 /* abort ao_sleep time */
42         uint16_t task_id;               /* unique id */
43         /* Saved stack pointer */
44         union {
45                 uint32_t        *sp32;
46                 uint8_t         *sp8;
47         };
48         const char *name;               /* task name */
49 #if HAS_TASK_QUEUE
50         struct ao_list  queue;
51         struct ao_list  alarm_queue;
52 #endif
53         /* Provide both 32-bit and 8-bit stacks, always 32-bit aligned */
54         union {
55                 uint32_t stack32[AO_STACK_SIZE>>2];
56                 uint8_t stack8[AO_STACK_SIZE];
57         };
58 #if HAS_SAMPLE_PROFILE
59         uint32_t ticks;
60         uint32_t yields;
61         uint16_t start;
62         uint16_t max_run;
63 #endif
64 };
65
66 #ifndef AO_NUM_TASKS
67 #define AO_NUM_TASKS            16      /* maximum number of tasks */
68 #endif
69
70 #define AO_NO_TASK              0       /* no task id */
71
72 extern struct ao_task * ao_tasks[AO_NUM_TASKS];
73 extern uint8_t ao_num_tasks;
74 extern struct ao_task *ao_cur_task;
75 extern uint8_t ao_task_minimize_latency;        /* Reduce IRQ latency */
76
77 #ifndef HAS_ARCH_VALIDATE_CUR_STACK
78 #define ao_validate_cur_stack()
79 #endif
80
81 /*
82  ao_task.c
83  */
84
85 /* Suspend the current task until wchan is awoken.
86  * returns:
87  *  0 on normal wake
88  *  1 on alarm
89  */
90 uint8_t
91 ao_sleep(void *wchan);
92
93 /* Suspend the current task until wchan is awoken or the timeout
94  * expires. returns:
95  *  0 on normal wake
96  *  1 on alarm
97  */
98 uint8_t
99 ao_sleep_for(void *wchan, uint16_t timeout);
100
101 /* Wake all tasks sleeping on wchan */
102 void
103 ao_wakeup(void *wchan);
104
105 #if 0
106 /* set an alarm to go off in 'delay' ticks */
107 void
108 ao_alarm(uint16_t delay);
109
110 /* Clear any pending alarm */
111 void
112 ao_clear_alarm(void);
113 #endif
114
115 /* Yield the processor to another task */
116 void
117 ao_yield(void) ao_arch_naked_declare;
118
119 /* Add a task to the run queue */
120 void
121 ao_add_task(struct ao_task * task, void (*start)(void), const char *name);
122
123 #if HAS_TASK_QUEUE
124 /* Called on timer interrupt to check alarms */
125 extern uint16_t ao_task_alarm_tick;
126 void
127 ao_task_check_alarm(uint16_t tick);
128 #endif
129
130 /* Terminate the current task */
131 void
132 ao_exit(void);
133
134 /* Dump task info to console */
135 void
136 ao_task_info(void);
137
138 /* Start the scheduler. This will not return */
139 void
140 ao_start_scheduler(void) __attribute__((noreturn));
141
142 #if HAS_TASK_QUEUE
143 void
144 ao_task_init(void);
145 #else
146 #define ao_task_init()
147 #endif
148
149 #endif