Make SPI and USART peripheral dispatch driver headers in preparation for a set of...
[pub/USBasp.git] / LUFA / Scheduler / Scheduler.h
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2010.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
7 */
8
9 /*
10 Copyright 2010 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
12 Permission to use, copy, modify, distribute, and sell this
13 software and its documentation for any purpose is hereby granted
14 without fee, provided that the above copyright notice appear in
15 all copies and that both that the copyright notice and this
16 permission notice and warranty disclaimer appear in supporting
17 documentation, and that the name of the author not be used in
18 advertising or publicity pertaining to distribution of the
19 software without specific, written prior permission.
20
21 The author disclaim all warranties with regard to this
22 software, including all implied warranties of merchantability
23 and fitness. In no event shall the author be liable for any
24 special, indirect or consequential damages or any damages
25 whatsoever resulting from loss of use, data or profits, whether
26 in an action of contract, negligence or other tortious action,
27 arising out of or in connection with the use or performance of
28 this software.
29 */
30
31 /** \file
32 *
33 * Simple round-robbin cooperative scheduler for use in basic projects where non real-time tasks need
34 * to be executed. Each task is executed in sequence, and can be enabled or disabled individually or as a group.
35 *
36 * \deprecated This module is deprecated and will be removed in a future library release.
37 */
38
39 /** @defgroup Group_Scheduler Simple Task Scheduler - LUFA/Scheduler/Scheduler.h
40 *
41 * \deprecated This module is deprecated and will be removed in a future library release.
42 *
43 * \section Sec_Dependencies Module Source Dependencies
44 * The following files must be built with any user project that uses this module:
45 * - LUFA/Scheduler/Scheduler.c
46 *
47 * \section Module Description
48 * Simple round-robbin cooperative scheduler for use in basic projects where non real-time tasks need
49 * to be executed. Each task is executed in sequence, and can be enabled or disabled individually or as a group.
50 *
51 * For a task to yield it must return, thus each task should have persistent data marked with the static attribute.
52 *
53 * Usage Example:
54 * \code
55 * #include <LUFA/Scheduler/Scheduler.h>
56 *
57 * TASK(MyTask1);
58 * TASK(MyTask2);
59 *
60 * TASK_LIST
61 * {
62 * { .Task = MyTask1, .TaskStatus = TASK_RUN, .GroupID = 1 },
63 * { .Task = MyTask2, .TaskStatus = TASK_RUN, .GroupID = 1 },
64 * }
65 *
66 * int main(void)
67 * {
68 * Scheduler_Start();
69 * }
70 *
71 * TASK(MyTask1)
72 * {
73 * // Implementation Here
74 * }
75 *
76 * TASK(MyTask2)
77 * {
78 * // Implementation Here
79 * }
80 * \endcode
81 *
82 * @{
83 */
84
85 #ifndef __SCHEDULER_H__
86 #define __SCHEDULER_H__
87
88 /* Includes: */
89 #if defined(__AVR32__)
90 #include <avr32/io.h>
91 #include <stdbool.h>
92 #else
93 #include <avr/io.h>
94 #include <util/atomic.h>
95 #include <stdbool.h>
96 #endif
97
98 #include "../Common/Common.h"
99
100 /* Enable C linkage for C++ Compilers: */
101 #if defined(__cplusplus)
102 extern "C" {
103 #endif
104
105 /* Public Interface - May be used in end-application: */
106 /* Macros: */
107 /** Creates a new scheduler task body or prototype. Should be used in the form:
108 * \code
109 * TASK(TaskName); // Prototype
110 *
111 * TASK(TaskName)
112 * {
113 * // Task body
114 * }
115 * \endcode
116 */
117 #define TASK(name) void name (void)
118
119 /** Defines a task list array, containing one or more task entries of the type TaskEntry_t. Each task list
120 * should be encased in curly braces and ended with a comma.
121 *
122 * Usage Example:
123 * \code
124 * TASK_LIST
125 * {
126 * { .Task = MyTask1, .TaskStatus = TASK_RUN, .GroupID = 1 },
127 * // More task entries here
128 * }
129 * \endcode
130 */
131 #define TASK_LIST TaskEntry_t Scheduler_TaskList[] =
132
133 /** Constant, giving the maximum delay in scheduler ticks which can be stored in a variable of type
134 * SchedulerDelayCounter_t.
135 */
136 #define TASK_MAX_DELAY (MAX_DELAYCTR_COUNT - 1)
137
138 /** Task status mode constant, for passing to Scheduler_SetTaskMode() or Scheduler_SetGroupTaskMode(). */
139 #define TASK_RUN true
140
141 /** Task status mode constant, for passing to Scheduler_SetTaskMode() or Scheduler_SetGroupTaskMode(). */
142 #define TASK_STOP false
143
144 /* Pseudo-Function Macros: */
145 #if defined(__DOXYGEN__)
146 /** Starts the scheduler in its infinite loop, executing running tasks. This should be placed at the end
147 * of the user application's main() function, as it can never return to the calling function.
148 */
149 void Scheduler_Start(void);
150
151 /** Initializes the scheduler so that the scheduler functions can be called before the scheduler itself
152 * is started. This must be executed before any scheduler function calls other than Scheduler_Start(),
153 * and can be omitted if no such functions could be called before the scheduler is started.
154 */
155 void Scheduler_Init(void);
156 #else
157 #define Scheduler_Start() Scheduler_GoSchedule(TOTAL_TASKS);
158
159 #define Scheduler_Init() Scheduler_InitScheduler(TOTAL_TASKS);
160 #endif
161
162 /* Type Defines: */
163 /** Type define for a pointer to a scheduler task. */
164 typedef void (*TaskPtr_t)(void);
165
166 /** Type define for a variable which can hold a tick delay value for the scheduler up to the maximum delay
167 * possible.
168 */
169 typedef uint16_t SchedulerDelayCounter_t;
170
171 /** Structure for holding a single task's information in the scheduler task list. */
172 typedef struct
173 {
174 TaskPtr_t Task; /**< Pointer to the task to execute. */
175 bool TaskStatus; /**< Status of the task (either TASK_RUN or TASK_STOP). */
176 uint8_t GroupID; /**< Group ID of the task so that its status can be changed as a group. */
177 } TaskEntry_t;
178
179 /* Global Variables: */
180 /** Task entry list, containing the scheduler tasks, task statuses and group IDs. Each entry is of type
181 * TaskEntry_t and can be manipulated as desired, although it is preferential that the proper Scheduler
182 * functions should be used instead of direct manipulation.
183 */
184 extern TaskEntry_t Scheduler_TaskList[];
185
186 /** Contains the total number of tasks in the task list, irrespective of if the task's status is set to
187 * TASK_RUN or TASK_STOP.
188 *
189 * \note This value should be treated as read-only, and never altered in user-code.
190 */
191 extern volatile uint8_t Scheduler_TotalTasks;
192
193 /** Contains the current scheduler tick count, for use with the delay functions. If the delay functions
194 * are used in the user code, this should be incremented each tick period so that the delays can be
195 * calculated.
196 */
197 extern volatile SchedulerDelayCounter_t Scheduler_TickCounter;
198
199 /* Inline Functions: */
200 /** Resets the delay counter value to the current tick count. This should be called to reset the period
201 * for a delay in a task which is dependant on the current tick value.
202 *
203 * \param[out] DelayCounter Counter which is storing the starting tick count for a given delay.
204 */
205 static inline void Scheduler_ResetDelay(SchedulerDelayCounter_t* const DelayCounter)
206 ATTR_NON_NULL_PTR_ARG(1) ATTR_ALWAYS_INLINE;
207 static inline void Scheduler_ResetDelay(SchedulerDelayCounter_t* const DelayCounter)
208 {
209 ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
210 {
211 *DelayCounter = Scheduler_TickCounter;
212 }
213 }
214
215 /* Function Prototypes: */
216 /** Determines if the given tick delay has elapsed, based on the given .
217 *
218 * \param[in] Delay The delay to test for, measured in ticks
219 * \param[in] DelayCounter The counter which is storing the starting tick value for the delay
220 *
221 * \return Boolean true if the delay has elapsed, false otherwise
222 *
223 * Usage Example:
224 * \code
225 * static SchedulerDelayCounter_t DelayCounter = 10000; // Force immediate run on start-up
226 *
227 * // Task runs every 10000 ticks, 10 seconds for this demo
228 * if (Scheduler_HasDelayElapsed(10000, &DelayCounter))
229 * {
230 * // Code to execute after delay interval elapsed here
231 * }
232 * \endcode
233 */
234 bool Scheduler_HasDelayElapsed(const uint16_t Delay,
235 SchedulerDelayCounter_t* const DelayCounter)
236 ATTR_WARN_UNUSED_RESULT ATTR_NON_NULL_PTR_ARG(2);
237
238 /** Sets the task mode for a given task.
239 *
240 * \param[in] Task Name of the task whose status is to be changed
241 * \param[in] TaskStatus New task status for the task (TASK_RUN or TASK_STOP)
242 */
243 void Scheduler_SetTaskMode(const TaskPtr_t Task, const bool TaskStatus);
244
245 /** Sets the task mode for a given task group ID, allowing for an entire group of tasks to have their
246 * statuses changed at once.
247 *
248 * \param[in] GroupID Value of the task group ID whose status is to be changed
249 * \param[in] TaskStatus New task status for tasks in the specified group (TASK_RUN or TASK_STOP)
250 */
251 void Scheduler_SetGroupTaskMode(const uint8_t GroupID, const bool TaskStatus);
252
253 /* Private Interface - For use in library only: */
254 #if !defined(__DOXYGEN__)
255 /* Macros: */
256 #define TOTAL_TASKS (sizeof(Scheduler_TaskList) / sizeof(TaskEntry_t))
257 #define MAX_DELAYCTR_COUNT 0xFFFF
258
259 /* Inline Functions: */
260 static inline void Scheduler_InitScheduler(const uint8_t TotalTasks) ATTR_ALWAYS_INLINE;
261 static inline void Scheduler_InitScheduler(const uint8_t TotalTasks)
262 {
263 Scheduler_TotalTasks = TotalTasks;
264 }
265
266 static inline void Scheduler_GoSchedule(const uint8_t TotalTasks) ATTR_NO_RETURN ATTR_ALWAYS_INLINE ATTR_DEPRECATED;
267 static inline void Scheduler_GoSchedule(const uint8_t TotalTasks)
268 {
269 Scheduler_InitScheduler(TotalTasks);
270
271 for (;;)
272 {
273 TaskEntry_t* CurrTask = &Scheduler_TaskList[0];
274
275 while (CurrTask != &Scheduler_TaskList[TotalTasks])
276 {
277 if (CurrTask->TaskStatus == TASK_RUN)
278 CurrTask->Task();
279
280 CurrTask++;
281 }
282 }
283 }
284 #endif
285
286 /* Disable C linkage for C++ Compilers: */
287 #if defined(__cplusplus)
288 }
289 #endif
290
291 #endif
292
293 /** @} */