altos/test: Adjust CRC error rate after FEC fix
[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
22 #include <ao_list.h>
23
24 #ifndef HAS_TASK_INFO
25 #define HAS_TASK_INFO 1
26 #endif
27
28 /* arm stacks must be 64-bit aligned */
29 #ifndef AO_STACK_ALIGNMENT
30 #ifdef __arm__
31 #define AO_STACK_ALIGNMENT __attribute__ ((aligned(8)))
32 #else
33 #define AO_STACK_ALIGNMENT
34 #endif
35 #endif
36
37 /* An AltOS task */
38 struct ao_task {
39         void *wchan;                    /* current wait channel (NULL if running) */
40         AO_TICK_TYPE alarm;             /* abort ao_sleep time */
41         uint8_t task_id;                /* unique id */
42         /* Saved stack pointer */
43         union {
44                 uint32_t        *sp32;
45                 uint8_t         *sp8;
46         };
47         const char *name;               /* task name */
48         struct ao_list  queue;
49         struct ao_list  alarm_queue;
50         /* Provide both 32-bit and 8-bit stacks */
51 #ifdef AO_STACK_CANARY
52         uint32_t        bottom_canary;
53 #endif
54         union {
55                 uint32_t stack32[AO_STACK_SIZE>>2];
56                 uint8_t stack8[AO_STACK_SIZE];
57         } AO_STACK_ALIGNMENT;
58 #ifdef AO_STACK_CANARY
59         uint32_t top_canary;
60 #endif
61 #if HAS_SAMPLE_PROFILE
62         uint32_t ticks;
63         uint32_t yields;
64         uint16_t start;
65         uint16_t max_run;
66 #endif
67 };
68
69 #ifndef AO_NUM_TASKS
70 #define AO_NUM_TASKS            16      /* maximum number of tasks */
71 #endif
72
73 extern struct ao_task * ao_tasks[AO_NUM_TASKS];
74 extern uint8_t ao_num_tasks;
75 extern struct ao_task *ao_cur_task;
76 extern uint8_t ao_task_minimize_latency;        /* Reduce IRQ latency */
77
78 #ifndef HAS_ARCH_VALIDATE_CUR_STACK
79 #define ao_validate_cur_stack()
80 #endif
81
82 /*
83  ao_task.c
84  */
85
86 /* Suspend the current task until wchan is awoken.
87  * returns:
88  *  0 on normal wake
89  *  1 on alarm
90  */
91 uint8_t
92 ao_sleep(void *wchan);
93
94 /* Suspend the current task until wchan is awoken or the timeout
95  * expires. returns:
96  *  0 on normal wake
97  *  1 on alarm
98  */
99 uint8_t
100 ao_sleep_for(void *wchan, AO_TICK_TYPE timeout);
101
102 /* Wake all tasks sleeping on wchan */
103 void
104 ao_wakeup(void *wchan);
105
106 #if 0
107 /* set an alarm to go off in 'delay' ticks */
108 void
109 ao_alarm(AO_TICK_TYPE delay);
110
111 /* Clear any pending alarm */
112 void
113 ao_clear_alarm(void);
114 #endif
115
116 /* Yield the processor to another task */
117 void
118 ao_yield(void) ao_arch_naked_declare;
119
120 /* Add a task to the run queue */
121 void
122 ao_add_task(struct ao_task * task, void (*start)(void), const char *name);
123
124 /* Called on timer interrupt to check alarms */
125 extern AO_TICK_TYPE             ao_task_alarm_tick;
126 extern volatile AO_TICK_TYPE    ao_tick_count;
127
128 void
129 ao_task_alarm(AO_TICK_TYPE tick);
130
131 static inline void
132 ao_task_check_alarm(void) {
133 #if HAS_TASK
134         if ((AO_TICK_SIGNED) (ao_tick_count - ao_task_alarm_tick) >= 0)
135                 ao_task_alarm(ao_tick_count);
136 #endif
137 }
138
139 /* Terminate the current task */
140 void
141 ao_exit(void) __attribute__ ((noreturn));
142
143 /* Dump task info to console */
144 void
145 ao_task_info(void);
146
147 /* Start the scheduler. This will not return */
148 void
149 ao_start_scheduler(void) __attribute__((noreturn));
150
151 void
152 ao_task_init(void);
153
154 #endif