3      Copyright (C) Dean Camera, 2011. 
   5   dean [at] fourwalledcubicle [dot] com 
  10   Copyright 2011  Dean Camera (dean [at] fourwalledcubicle [dot] com) 
  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. 
  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 
  32  *  \brief Simple round-robbin pseudo-task scheduler. 
  34  *  Simple round-robbin cooperative scheduler for use in basic projects where non real-time tasks need 
  35  *  to be executed. Each task is executed in sequence, and can be enabled or disabled individually or as a group. 
  37  *  \deprecated This module is deprecated and will be removed in a future library release. 
  40 /** @defgroup Group_Scheduler Simple Task Scheduler - LUFA/Scheduler/Scheduler.h 
  42  *  \deprecated This module is deprecated and will be removed in a future library release. 
  44  *  \section Sec_Dependencies Module Source Dependencies 
  45  *  The following files must be built with any user project that uses this module: 
  46  *    - LUFA/Scheduler/Scheduler.c <i>(Makefile source module name: LUFA_SRC_SCHEDULER)</i> 
  48  *  \section Sec_ModDescription Module Description 
  49  *  Simple round-robbin cooperative scheduler for use in basic projects where non real-time tasks need 
  50  *  to be executed. Each task is executed in sequence, and can be enabled or disabled individually or as a group. 
  52  *  For a task to yield it must \c return, thus each task should have persistent data marked with the \c static keyword. 
  54  *  Each LUFA scheduler task should be written similar to an ISR; it should execute quickly (so that no one task 
  55  *  hogs the processor, preventing another from running before some sort of timeout is exceeded). Unlike normal RTOS 
  56  *  tasks, each LUFA scheduler task is a regular function, and thus must be designed to be called, and designed to 
  57  *  return to the calling scheduler function repeatedly. Data which must be preserved between task calls should be 
  58  *  declared as global or (preferably) as a \c static local variable inside the task. 
  60  *  The scheduler consists of a task list, listing all the tasks which can be executed by the scheduler. Once started, 
  61  *  each task is then called one after another, unless the task is stopped by another running task or interrupt. 
  65  *      #include <LUFA/Scheduler/Scheduler.h> 
  67  *      TASK(MyTask1); // Task prototype 
  68  *      TASK(MyTask2); // Task prototype 
  72  *          { .Task = MyTask1, .TaskStatus = TASK_RUN, .GroupID = 1  }, 
  73  *          { .Task = MyTask2, .TaskStatus = TASK_RUN, .GroupID = 1  }, 
  80  *          // Other initialisation here 
  87  *          // Task implementation here 
  92  *          // Task implementation here 
  96  *  If desired, the LUFA scheduler <b>does not need to be used</b> in a LUFA powered application. A more conventional 
  97  *  approach to application design can be used, or a proper scheduling RTOS inserted in the place of the LUFA scheduler. 
  98  *  In the case of the former the USB task must be run manually repeatedly to maintain USB communications, and in the 
  99  *  case of the latter a proper RTOS task must be set up to do the same. 
 104 #ifndef __SCHEDULER_H__ 
 105 #define __SCHEDULER_H__ 
 111                 #include <util/atomic.h> 
 113                 #include "../Common/Common.h" 
 115         /* Enable C linkage for C++ Compilers: */ 
 116                 #if defined(__cplusplus) 
 120         /* Public Interface - May be used in end-application: */ 
 122                         /** Creates a new scheduler task body or prototype. Should be used in the form: 
 124                          *      TASK(TaskName); // Prototype 
 132                         #define TASK(name)              void name (void) 
 134                         /** Defines a task list array, containing one or more task entries of the type \ref TaskEntry_t. Each task list 
 135                          *  should be encased in curly braces and ended with a comma. 
 141                          *           { .Task = MyTask1, .TaskStatus = TASK_RUN, .GroupID = 1 }, 
 142                          *           // More task entries here 
 146                         #define TASK_LIST               TaskEntry_t Scheduler_TaskList[] = 
 148                         /** Constant, giving the maximum delay in scheduler ticks which can be stored in a variable of type 
 149                          *  \ref SchedulerDelayCounter_t. 
 151                         #define TASK_MAX_DELAY          (MAX_DELAYCTR_COUNT - 1) 
 153                         /** Task status mode constant, for passing to \ref Scheduler_SetTaskMode() or \ref Scheduler_SetGroupTaskMode(). */ 
 154                         #define TASK_RUN                true 
 156                         /** Task status mode constant, for passing to \ref Scheduler_SetTaskMode() or \ref Scheduler_SetGroupTaskMode(). */ 
 157                         #define TASK_STOP               false 
 159                 /* Pseudo-Function Macros: */ 
 160                         #if defined(__DOXYGEN__) 
 161                                 /** Starts the scheduler in its infinite loop, executing running tasks. This should be placed at the end 
 162                                  *  of the user application's \c main() function, as it can never return to the calling function. 
 164                                 void Scheduler_Start(void); 
 166                                 /** Initialises the scheduler so that the scheduler functions can be called before the scheduler itself 
 167                                  *  is started. This must be executed before any scheduler function calls other than \ref Scheduler_Start(), 
 168                                  *  and can be omitted if no such functions could be called before the scheduler is started. 
 170                                 void Scheduler_Init(void); 
 172                                 #define Scheduler_Start()    Scheduler_GoSchedule(TOTAL_TASKS); 
 173                                 #define Scheduler_Init()     Scheduler_InitScheduler(TOTAL_TASKS); 
 177                         /** Type define for a pointer to a scheduler task. */ 
 178                         typedef void (*TaskPtr_t
)(void); 
 180                         /** Type define for a variable which can hold a tick delay value for the scheduler up to the maximum delay 
 183                         typedef uint16_t SchedulerDelayCounter_t
; 
 185                         /** \brief Scheduler Task List Entry Structure. 
 187                          *  Structure for holding a single task's information in the scheduler task list. 
 191                                 TaskPtr_t Task
;       /**< Pointer to the task to execute. */ 
 192                                 bool      TaskStatus
; /**< Status of the task (either TASK_RUN or TASK_STOP). */ 
 193                                 uint8_t   GroupID
;    /**< Group ID of the task so that its status can be changed as a group. */ 
 196                 /* Global Variables: */ 
 197                         /** Task entry list, containing the scheduler tasks, task statuses and group IDs. Each entry is of type 
 198                          *  \ref TaskEntry_t and can be manipulated as desired, although it is preferred that the proper Scheduler 
 199                          *  functions should be used instead of direct manipulation. 
 201                         exter TaskEntry_t Scheduler_TaskList
[]; 
 203                         /** Contains the total number of tasks in the task list, irrespective of if the task's status is set to 
 204                          *  \ref TASK_RUN or \ref TASK_STOP. 
 206                          *  \note This value should be treated as read-only, and never altered in user-code. 
 208                         extern volatile uint8_t Scheduler_TotalTasks
; 
 210                         /**  Contains the current scheduler tick count, for use with the delay functions. If the delay functions 
 211                          *   are used in the user code, this should be incremented each tick period so that the delays can be 
 214                         extern volatile SchedulerDelayCounter_t Scheduler_TickCounter
; 
 216                 /* Inline Functions: */ 
 217                         /** Resets the delay counter value to the current tick count. This should be called to reset the period 
 218                          *  for a delay in a task which is dependant on the current tick value. 
 220                          *  \param[out] DelayCounter  Counter which is storing the starting tick count for a given delay. 
 222                         static inline void Scheduler_ResetDelay(SchedulerDelayCounter_t
* const DelayCounter
) 
 223                                                                 ATTR_NON_NULL_PTR_ARG(1) ATTR_ALWAYS_INLINE
; 
 224                         static inline void Scheduler_ResetDelay(SchedulerDelayCounter_t
* const DelayCounter
) 
 226                                 ATOMIC_BLOCK(ATOMIC_RESTORESTATE
) 
 228                                         *DelayCounter 
= Scheduler_TickCounter
; 
 232                 /* Function Prototypes: */ 
 233                         /** Determines if the given tick delay has elapsed, based on the given delay period and tick counter value. 
 235                          *  \param[in] Delay         The delay to test for, measured in ticks. 
 236                          *  \param[in] DelayCounter  The counter which is storing the starting tick value for the delay. 
 238                          *  \return Boolean \c true if the delay has elapsed, \c false otherwise. 
 242                          *      static SchedulerDelayCounter_t DelayCounter = 10000; // Force immediate run on start-up 
 244                          *      // Task runs every 10000 ticks, 10 seconds for this demo 
 245                          *      if (Scheduler_HasDelayElapsed(10000, &DelayCounter)) 
 247                          *           // Code to execute after delay interval elapsed here 
 251                         bool Scheduler_HasDelayElapsed(const uint16_t Delay
, 
 252                                                        SchedulerDelayCounter_t
* const DelayCounter
) 
 253                                                        ATTR_WARN_UNUSED_RESULT 
ATTR_NON_NULL_PTR_ARG(2); 
 255                         /** Sets the task mode for a given task. 
 257                          *  \param[in] Task        Name of the task whose status is to be changed. 
 258                          *  \param[in] TaskStatus  New task status for the task (\ref TASK_RUN or \ref TASK_STOP). 
 260                         void Scheduler_SetTaskMode(const TaskPtr_t Task
, 
 261                                                    const bool TaskStatus
); 
 263                         /** Sets the task mode for a given task group ID, allowing for an entire group of tasks to have their 
 264                          *  statuses changed at once. 
 266                          *  \param[in] GroupID     Value of the task group ID whose status is to be changed. 
 267                          *  \param[in] TaskStatus  New task status for tasks in the specified group (\ref TASK_RUN or \ref TASK_STOP). 
 269                         void Scheduler_SetGroupTaskMode(const uint8_t GroupID
, 
 270                                                         const bool TaskStatus
); 
 272         /* Private Interface - For use in library only: */ 
 273         #if !defined(__DOXYGEN__) 
 275                         #define TOTAL_TASKS                       (sizeof(Scheduler_TaskList) / sizeof(TaskEntry_t)) 
 276                         #define MAX_DELAYCTR_COUNT                0xFFFF 
 278                 /* Inline Functions: */ 
 279                         static inline void Scheduler_InitScheduler(const uint8_t TotalTasks
) ATTR_ALWAYS_INLINE
; 
 280                         static inline void Scheduler_InitScheduler(const uint8_t TotalTasks
) 
 282                                 Scheduler_TotalTasks 
= TotalTasks
; 
 285                         static inline void Scheduler_GoSchedule(const uint8_t TotalTasks
) ATTR_NO_RETURN ATTR_ALWAYS_INLINE ATTR_DEPRECATED
; 
 286                         static inline void Scheduler_GoSchedule(const uint8_t TotalTasks
) 
 288                                 Scheduler_InitScheduler(TotalTasks
); 
 292                                         TaskEntry_t
* CurrTask 
= &Scheduler_TaskList
[0]; 
 294                                         while (CurrTask 
!= &Scheduler_TaskList
[TotalTasks
]) 
 296                                                 if (CurrTask
->TaskStatus 
== TASK_RUN
) 
 305         /* Disable C linkage for C++ Compilers: */ 
 306                 #if defined(__cplusplus)