\section{aversive\_\-10-03-12/modules/base/scheduler/scheduler.h File Reference}
\label{scheduler_8h}\index{aversive\_\-10-03-12/modules/base/scheduler/scheduler.h@{aversive\_\-10-03-12/modules/base/scheduler/scheduler.h}}
{\tt \#include $<$aversive.h$>$}\par
{\tt \#include $<$scheduler\_\-config.h$>$}\par
\subsection*{Defines}
\begin{CompactItemize}
\item 
\#define {\bf TIMER\_\-UNIT\_\-FLOAT}~( 65536000000.0 / (double)(CONFIG\_\-QUARTZ) )
\item 
\#define {\bf SCHEDULER\_\-UNIT\_\-FLOAT}~( TIMER\_\-UNIT\_\-FLOAT $\ast$ (double)SCHEDULER\_\-CLOCK\_\-PRESCALER )
\item 
\#define {\bf SCHEDULER\_\-UNIT}~( (unsigned long) SCHEDULER\_\-UNIT\_\-FLOAT )
\item 
\#define {\bf SCHEDULER\_\-PERIODICAL}~0
\item 
\#define {\bf SCHEDULER\_\-SINGLE}~1
\item 
\#define {\bf SCHEDULER\_\-DEFAULT\_\-PRIORITY}~128
\end{CompactItemize}
\subsection*{Functions}
\begin{CompactItemize}
\item 
void {\bf scheduler\_\-init} (void)
\item 
int8\_\-t {\bf scheduler\_\-add\_\-event} (uint8\_\-t unicity, void($\ast$f)(void $\ast$), void $\ast$data, uint16\_\-t period, uint8\_\-t priority)
\item 
int8\_\-t {\bf scheduler\_\-del\_\-event} (int8\_\-t num)
\item 
void {\bf scheduler\_\-interrupt} (void)
\item 
uint8\_\-t {\bf scheduler\_\-disable\_\-save} (void)
\item 
void {\bf scheduler\_\-enable\_\-restore} (uint8\_\-t old\_\-prio)
\end{CompactItemize}


\subsection{Detailed Description}
This module provides a function scheduler. You can call scheduler\_\-add\_\-event for adding a function to the scheduler, and specifying what interval between each call. During the execution of the function, interrupts are masked !! So use this module with caution (small functions) for avoiding problems.

Functions with a high priority value will be call before others (default is 128).

This module uses Timer 0 

Definition in file {\bf scheduler.h}.

\subsection{Define Documentation}
\index{scheduler.h@{scheduler.h}!SCHEDULER\_\-DEFAULT\_\-PRIORITY@{SCHEDULER\_\-DEFAULT\_\-PRIORITY}}
\index{SCHEDULER\_\-DEFAULT\_\-PRIORITY@{SCHEDULER\_\-DEFAULT\_\-PRIORITY}!scheduler.h@{scheduler.h}}
\subsubsection[SCHEDULER\_\-DEFAULT\_\-PRIORITY]{\setlength{\rightskip}{0pt plus 5cm}\#define SCHEDULER\_\-DEFAULT\_\-PRIORITY~128}\label{scheduler_8h_5fa9a070a0e52c2d12b3085baca9fa72}




Definition at line 124 of file scheduler.h.\index{scheduler.h@{scheduler.h}!SCHEDULER\_\-PERIODICAL@{SCHEDULER\_\-PERIODICAL}}
\index{SCHEDULER\_\-PERIODICAL@{SCHEDULER\_\-PERIODICAL}!scheduler.h@{scheduler.h}}
\subsubsection[SCHEDULER\_\-PERIODICAL]{\setlength{\rightskip}{0pt plus 5cm}\#define SCHEDULER\_\-PERIODICAL~0}\label{scheduler_8h_14dcacf25ada1df60d4970ecaca557ee}




Definition at line 121 of file scheduler.h.\index{scheduler.h@{scheduler.h}!SCHEDULER\_\-SINGLE@{SCHEDULER\_\-SINGLE}}
\index{SCHEDULER\_\-SINGLE@{SCHEDULER\_\-SINGLE}!scheduler.h@{scheduler.h}}
\subsubsection[SCHEDULER\_\-SINGLE]{\setlength{\rightskip}{0pt plus 5cm}\#define SCHEDULER\_\-SINGLE~1}\label{scheduler_8h_7402a33d4a10874e03b4874db148261f}




Definition at line 122 of file scheduler.h.\index{scheduler.h@{scheduler.h}!SCHEDULER\_\-UNIT@{SCHEDULER\_\-UNIT}}
\index{SCHEDULER\_\-UNIT@{SCHEDULER\_\-UNIT}!scheduler.h@{scheduler.h}}
\subsubsection[SCHEDULER\_\-UNIT]{\setlength{\rightskip}{0pt plus 5cm}\#define SCHEDULER\_\-UNIT~( (unsigned long) SCHEDULER\_\-UNIT\_\-FLOAT )}\label{scheduler_8h_4ca1400bcf4141df4b451f0a92d2f8b1}




Definition at line 115 of file scheduler.h.

Referenced by main(), mf2\_\-client\_\-send(), SIGNAL(), and time\_\-increment().\index{scheduler.h@{scheduler.h}!SCHEDULER\_\-UNIT\_\-FLOAT@{SCHEDULER\_\-UNIT\_\-FLOAT}}
\index{SCHEDULER\_\-UNIT\_\-FLOAT@{SCHEDULER\_\-UNIT\_\-FLOAT}!scheduler.h@{scheduler.h}}
\subsubsection[SCHEDULER\_\-UNIT\_\-FLOAT]{\setlength{\rightskip}{0pt plus 5cm}\#define SCHEDULER\_\-UNIT\_\-FLOAT~( TIMER\_\-UNIT\_\-FLOAT $\ast$ (double)SCHEDULER\_\-CLOCK\_\-PRESCALER )}\label{scheduler_8h_a8a4e075e06a37c157f161a47b77606a}


SCHEDULER\_\-UNIT is the number of microseconds between each scheduler interruption. We can use it like this : scheduler\_\-add\_\-periodical\_\-event(f, 1000L/SCHEDULER\_\-UNIT); The function f will be called every ms. 

Definition at line 114 of file scheduler.h.\index{scheduler.h@{scheduler.h}!TIMER\_\-UNIT\_\-FLOAT@{TIMER\_\-UNIT\_\-FLOAT}}
\index{TIMER\_\-UNIT\_\-FLOAT@{TIMER\_\-UNIT\_\-FLOAT}!scheduler.h@{scheduler.h}}
\subsubsection[TIMER\_\-UNIT\_\-FLOAT]{\setlength{\rightskip}{0pt plus 5cm}\#define TIMER\_\-UNIT\_\-FLOAT~( 65536000000.0 / (double)(CONFIG\_\-QUARTZ) )}\label{scheduler_8h_b5a96bd11a8d5353898a7039e91a82de}


TIME\_\-UNIT is the number of microseconds between each interruption if the prescaler equals 1 

Definition at line 106 of file scheduler.h.

\subsection{Function Documentation}
\index{scheduler.h@{scheduler.h}!scheduler\_\-add\_\-event@{scheduler\_\-add\_\-event}}
\index{scheduler\_\-add\_\-event@{scheduler\_\-add\_\-event}!scheduler.h@{scheduler.h}}
\subsubsection[scheduler\_\-add\_\-event]{\setlength{\rightskip}{0pt plus 5cm}int8\_\-t scheduler\_\-add\_\-event (uint8\_\-t {\em unicity}, \/  void($\ast$)(void $\ast$) {\em f}, \/  void $\ast$ {\em data}, \/  uint16\_\-t {\em period}, \/  uint8\_\-t {\em priority})}\label{scheduler_8h_360d73e3218aa385300aeb2d74b79f2a}


Add an event to the event table. Return the id of the event on succes and -1 on \doxyref{error}{p.}{structerror} You can use static inline funcs below for simpler use.

Alloc an event, and fill its field, then mark it as active. return its index in the table, or -1 if no evt is available 

Definition at line 50 of file scheduler\_\-add.c.

References event\_\-t::current\_\-time, event\_\-t::data, parse\_\-symbols::f, event\_\-t::f, g\_\-tab\_\-event, IRQ\_\-LOCK, IRQ\_\-UNLOCK, event\_\-t::period, event\_\-t::priority, SCHEDULER\_\-EVENT\_\-ACTIVE, and event\_\-t::state.\index{scheduler.h@{scheduler.h}!scheduler\_\-del\_\-event@{scheduler\_\-del\_\-event}}
\index{scheduler\_\-del\_\-event@{scheduler\_\-del\_\-event}!scheduler.h@{scheduler.h}}
\subsubsection[scheduler\_\-del\_\-event]{\setlength{\rightskip}{0pt plus 5cm}int8\_\-t scheduler\_\-del\_\-event (int8\_\-t {\em num})}\label{scheduler_8h_412fd50c270ae8ddae41e410f0c4f668}


Dels an event from the table by its ID. If there is no event, nothing is done. 

Definition at line 26 of file scheduler\_\-del.c.

References g\_\-tab\_\-event, IRQ\_\-LOCK, IRQ\_\-UNLOCK, SCHEDULER\_\-EVENT\_\-ACTIVE, SCHEDULER\_\-EVENT\_\-DELETING, SCHEDULER\_\-EVENT\_\-FREE, SCHEDULER\_\-EVENT\_\-SCHEDULED, and event\_\-t::state.

Referenced by SIGNAL().\index{scheduler.h@{scheduler.h}!scheduler\_\-disable\_\-save@{scheduler\_\-disable\_\-save}}
\index{scheduler\_\-disable\_\-save@{scheduler\_\-disable\_\-save}!scheduler.h@{scheduler.h}}
\subsubsection[scheduler\_\-disable\_\-save]{\setlength{\rightskip}{0pt plus 5cm}uint8\_\-t scheduler\_\-disable\_\-save (void)}\label{scheduler_8h_ce0425932a743d3fab7b1dee1b1f4eba}


Temporarily disable scheduler events. You may loose precision in events schedule. It returns the current priority of the scheduler. 

Definition at line 34 of file scheduler\_\-interrupt.c.

Referenced by AX12\_\-send().\index{scheduler.h@{scheduler.h}!scheduler\_\-enable\_\-restore@{scheduler\_\-enable\_\-restore}}
\index{scheduler\_\-enable\_\-restore@{scheduler\_\-enable\_\-restore}!scheduler.h@{scheduler.h}}
\subsubsection[scheduler\_\-enable\_\-restore]{\setlength{\rightskip}{0pt plus 5cm}void scheduler\_\-enable\_\-restore (uint8\_\-t {\em old\_\-prio})}\label{scheduler_8h_b377e2aac32c7ffaa5705276dbed698a}


Re-enable scheduler after a call to \doxyref{scheduler\_\-disable\_\-save()}{p.}{scheduler_8h_ce0425932a743d3fab7b1dee1b1f4eba}. 

Definition at line 42 of file scheduler\_\-interrupt.c.

Referenced by AX12\_\-send().\index{scheduler.h@{scheduler.h}!scheduler\_\-init@{scheduler\_\-init}}
\index{scheduler\_\-init@{scheduler\_\-init}!scheduler.h@{scheduler.h}}
\subsubsection[scheduler\_\-init]{\setlength{\rightskip}{0pt plus 5cm}void scheduler\_\-init (void)}\label{scheduler_8h_b258cc2856e5f6bb125907cc23358fbd}


Initialisation of the module

init all global data 

Definition at line 38 of file scheduler.c.

References CS00\_\-REG, DUMP\_\-EVENTS, g\_\-tab\_\-event, and TOIE0\_\-REG.

Referenced by main().\index{scheduler.h@{scheduler.h}!scheduler\_\-interrupt@{scheduler\_\-interrupt}}
\index{scheduler\_\-interrupt@{scheduler\_\-interrupt}!scheduler.h@{scheduler.h}}
\subsubsection[scheduler\_\-interrupt]{\setlength{\rightskip}{0pt plus 5cm}void scheduler\_\-interrupt (void)}\label{scheduler_8h_7253eb381f3f978df771b01964ec6fcd}


Function called by the interruption. It is public in case of host version, because you have to call it by hand. In AVR version, you don't have to do anything with this function, it is called automatilcally by the timer interruption.

this function is called from a timer interruption. If an event has to be scheduled, it will execute the fonction (IRQ are allowed during the execution of the function). This interruption can be interrupted by itself too, in this case only events with a higher priority can be scheduled.

We assume that this function is called from a \doxyref{SIGNAL()}{p.}{i2c_8c_76216358046f104b52928cb3841a144e}, with global interrupt flag disabled --$>$ that's why we can use \doxyref{cli()}{p.}{aversive_8h_68c330e94fe121eba993e5a5973c3162} and \doxyref{sei()}{p.}{aversive_8h_ad5ebd34cb344c26ac87594f79b06b73} instead of \doxyref{IRQ\_\-LOCK(flags)}{p.}{irq__lock_8h_56988e8d5a77d00c601fa2ec150da1b4}. 

Definition at line 59 of file scheduler\_\-interrupt.c.

References cli, event\_\-t::current\_\-time, event\_\-t::data, DUMP\_\-EVENTS, event\_\-t::f, g\_\-tab\_\-event, i, event\_\-t::period, event\_\-t::priority, SCHEDULER\_\-EVENT\_\-ACTIVE, SCHEDULER\_\-EVENT\_\-DELETING, SCHEDULER\_\-EVENT\_\-FREE, SCHEDULER\_\-EVENT\_\-SCHEDULED, sei, SLIST\_\-FIRST, SLIST\_\-FOREACH, SLIST\_\-HEAD, SLIST\_\-INIT, SLIST\_\-INSERT\_\-AFTER, SLIST\_\-INSERT\_\-HEAD, SLIST\_\-NEXT, and event\_\-t::state.