All comments in the library, bootloaders, demos and projects have now been spell...
[pub/USBasp.git] / LUFA / Scheduler / Scheduler.h
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2009.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
7 */
8
9 /*
10 Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
12 Permission to use, copy, modify, and distribute this software
13 and its documentation for any purpose and without fee is hereby
14 granted, provided that the above copyright notice appear in all
15 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 * For a task to yield it must return, thus each task should have persistent data marked with the static attribute.
37 *
38 * Usage Example:
39 * \code
40 * #include <LUFA/Scheduler/Scheduler.h>
41 *
42 * TASK(MyTask1);
43 * TASK(MyTask2);
44 *
45 * TASK_LIST
46 * {
47 * { Task: MyTask1, TaskStatus: TASK_RUN, GroupID: 1 },
48 * { Task: MyTask2, TaskStatus: TASK_RUN, GroupID: 1 },
49 * }
50 *
51 * int main(void)
52 * {
53 * Scheduler_Start();
54 * }
55 *
56 * TASK(MyTask1)
57 * {
58 * // Implementation Here
59 * }
60 *
61 * TASK(MyTask2)
62 * {
63 * // Implementation Here
64 * }
65 * \endcode
66 */
67
68 #ifndef __SCHEDULER_H__
69 #define __SCHEDULER_H__
70
71 /* Includes: */
72 #include <avr/io.h>
73 #include <stdbool.h>
74
75 #include <util/atomic.h>
76
77 #include "../Common/Common.h"
78
79 /* Enable C linkage for C++ Compilers: */
80 #if defined(__cplusplus)
81 extern "C" {
82 #endif
83
84 /* Public Interface - May be used in end-application: */
85 /* Macros: */
86 /** Creates a new scheduler task body or prototype. Should be used in the form:
87 * \code
88 * TASK(TaskName); // Prototype
89 *
90 * TASK(TaskName)
91 * {
92 * // Task body
93 * }
94 * \endcode
95 */
96 #define TASK(name) void name (void)
97
98 /** Defines a task list array, containing one or more task entries of the type TaskEntry_t. Each task list
99 * should be encased in curly braces and ended with a comma.
100 *
101 * Usage Example:
102 * \code
103 * TASK_LIST
104 * {
105 * { Task: MyTask1, TaskStatus: TASK_RUN, GroupID: 1 },
106 * // More task entries here
107 * }
108 * \endcode
109 */
110 #define TASK_LIST extern TaskEntry_t Scheduler_TaskList[]; TaskEntry_t Scheduler_TaskList[] =
111
112 /** Constant, giving the maximum delay in scheduler ticks which can be stored in a variable of type
113 * SchedulerDelayCounter_t.
114 */
115 #define TASK_MAX_DELAY (MAX_DELAYCTR_COUNT - 1)
116
117 /** Task status mode constant, for passing to Scheduler_SetTaskMode() or Scheduler_SetGroupTaskMode(). */
118 #define TASK_RUN true
119
120 /** Task status mode constant, for passing to Scheduler_SetTaskMode() or Scheduler_SetGroupTaskMode(). */
121 #define TASK_STOP false
122
123 /** Starts the scheduler in its infinite loop, executing running tasks. This should be placed at the end
124 * of the user application's main() function, as it can never return to the calling function.
125 */
126 #define Scheduler_Start() Scheduler_GoSchedule(TOTAL_TASKS);
127
128 /** Initializes the scheduler so that the scheduler functions can be called before the scheduler itself
129 * is started. This must be executed before any scheduler function calls other than Scheduler_Start(),
130 * and can be omitted if no such functions could be called before the scheduler is started.
131 */
132 #define Scheduler_Init() Scheduler_InitScheduler(TOTAL_TASKS);
133
134 /* Type Defines: */
135 /** Type define for a pointer to a scheduler task. */
136 typedef void (*TaskPtr_t)(void);
137
138 /** Type define for a variable which can hold a tick delay value for the scheduler up to the maximum delay
139 * possible.
140 */
141 typedef uint16_t SchedulerDelayCounter_t;
142
143 /** Structure for holding a single task's information in the scheduler task list. */
144 typedef struct
145 {
146 TaskPtr_t Task; /**< Pointer to the task to execute. */
147 bool TaskStatus; /**< Status of the task (either TASK_RUN or TASK_STOP). */
148 uint8_t GroupID; /**< Group ID of the task so that its status can be changed as a group. */
149 } TaskEntry_t;
150
151 /* Global Variables: */
152 /** Task entry list, containing the scheduler tasks, task statuses and group IDs. Each entry is of type
153 * TaskEntry_t and can be manipulated as desired, although it is preferential that the proper Scheduler
154 * functions should be used instead of direct manipulation.
155 */
156 extern TaskEntry_t Scheduler_TaskList[];
157
158 /** Contains the total number of tasks in the task list, irrespective of if the task's status is set to
159 * TASK_RUN or TASK_STOP.
160 *
161 * \note This value should be treated as read-only, and never altered in user-code.
162 */
163 extern volatile uint8_t Scheduler_TotalTasks;
164
165 /** Contains the current scheduler tick count, for use with the delay functions. If the delay functions
166 * are used in the user code, this should be incremented each tick period so that the delays can be
167 * calculated.
168 */
169 extern volatile SchedulerDelayCounter_t Scheduler_TickCounter;
170
171 /* Inline Functions: */
172 /** Resets the delay counter value to the current tick count. This should be called to reset the period
173 * for a delay in a task which is dependant on the current tick value.
174 *
175 * \param DelayCounter Counter which is storing the starting tick count for a given delay.
176 */
177 static inline void Scheduler_ResetDelay(SchedulerDelayCounter_t* const DelayCounter)
178 ATTR_NON_NULL_PTR_ARG(1) ATTR_ALWAYS_INLINE;
179 static inline void Scheduler_ResetDelay(SchedulerDelayCounter_t* const DelayCounter)
180 {
181 ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
182 {
183 *DelayCounter = Scheduler_TickCounter;
184 }
185 }
186
187 /* Function Prototypes: */
188 /** Determines if the given tick delay has elapsed, based on the given .
189 *
190 * \param Delay The delay to test for, measured in ticks
191 * \param DelayCounter The counter which is storing the starting tick value for the delay
192 *
193 * \return Boolean true if the delay has elapsed, false otherwise
194 *
195 * Usage Example:
196 * \code
197 * static SchedulerDelayCounter_t DelayCounter = 10000; // Force immediate run on start-up
198 *
199 * // Task runs every 10000 ticks, 10 seconds for this demo
200 * if (Scheduler_HasDelayElapsed(10000, &DelayCounter))
201 * {
202 * // Code to execute after delay interval elapsed here
203 * }
204 * \endcode
205 */
206 bool Scheduler_HasDelayElapsed(const uint16_t Delay,
207 SchedulerDelayCounter_t* const DelayCounter)
208 ATTR_WARN_UNUSED_RESULT ATTR_NON_NULL_PTR_ARG(2);
209
210 /** Sets the task mode for a given task.
211 *
212 * \param Task Name of the task whose status is to be changed
213 * \param TaskStatus New task status for the task (TASK_RUN or TASK_STOP)
214 */
215 void Scheduler_SetTaskMode(const TaskPtr_t Task, const bool TaskStatus);
216
217 /** Sets the task mode for a given task group ID, allowing for an entire group of tasks to have their
218 * statuses changed at once.
219 *
220 * \param GroupID Value of the task group ID whose status is to be changed
221 * \param TaskStatus New task status for tasks in the specified group (TASK_RUN or TASK_STOP)
222 */
223 void Scheduler_SetGroupTaskMode(const uint8_t GroupID, const bool TaskStatus);
224
225 /* Private Interface - For use in library only: */
226 #if !defined(__DOXYGEN__)
227 /* Macros: */
228 #define TOTAL_TASKS (sizeof(Scheduler_TaskList) / sizeof(TaskEntry_t))
229 #define MAX_DELAYCTR_COUNT 0xFFFF
230
231 /* Inline Functions: */
232 static inline void Scheduler_InitScheduler(const uint8_t TotalTasks) ATTR_ALWAYS_INLINE;
233 static inline void Scheduler_InitScheduler(const uint8_t TotalTasks)
234 {
235 Scheduler_TotalTasks = TotalTasks;
236 }
237
238 static inline void Scheduler_GoSchedule(const uint8_t TotalTasks) ATTR_NO_RETURN ATTR_ALWAYS_INLINE;
239 static inline void Scheduler_GoSchedule(const uint8_t TotalTasks)
240 {
241 Scheduler_InitScheduler(TotalTasks);
242
243 for (;;)
244 {
245 TaskEntry_t* CurrTask = &Scheduler_TaskList[0];
246
247 while (CurrTask != &Scheduler_TaskList[TotalTasks])
248 {
249 if (CurrTask->TaskStatus == TASK_RUN)
250 CurrTask->Task();
251
252 CurrTask++;
253 }
254 }
255 }
256 #endif
257
258 /* Disable C linkage for C++ Compilers: */
259 #if defined(__cplusplus)
260 }
261 #endif
262
263 #endif