/* RCS revision control
$Header: c:/cvsroot/bart/rt_task.h,v 1.5 2004/03/09 00:45:20 mjames Exp $
RCS Log file
$Log: rt_task.h,v $
Revision 1.5 2004/03/09 00:45:20 mjames
Corrected mistakes, made task numbers visible
Revision 1.4 2004/03/08 22:45:45 mjames
Updated some useful macros
Revision 1.3 2004/03/06 12:17:48 mjames
Moved headers around, made it clearer that there are no configurable
parts to the OS unless it is rebuilt
Revision 1.2 2004/03/04 21:53:02 mjames
Made the files work with a demo project
Revision 1.1.1.1 2004/03/03 22:54:33 mjames
no message
*/
#if !defined RT_TASK_H
#define RT_TASK_H
/******************************************************************************/
/* Task settings */
/** this means n tasks + virtual idle task */
#define TASKS 3
#define STACK0SIZE 41 /**< stack 0 overlaps with real stack . take care in memory map */
#define STACK1SIZE 46
#define STACK2SIZE 46
#define STACK3SIZE 46
/******************************************************************************/
/* Interrupt vectors */
#define T0_INTVEC 1
#define T1_INTVEC 3
#define SIO_INTVEC 4
#define T2_INTVEC 5
#define SIO_INTERRUPT_BANK 1 /**< defined for serial use */
#define T0_INTERRUPT_BANK 2
#define T2_INTERRUPT_BANK 3
/** Interrupt declaration so that main() containing function has correct interface */
extern void T0Interrupt(void) interrupt T0_INTVEC using T0_INTERRUPT_BANK;
/* Exported for main() to see in scope */
extern void T2Interrupt(void) interrupt T2_INTVEC using T2_INTERRUPT_BANK;
extern void SIOInterrupt(void) interrupt SIO_INTVEC using SIO_INTERRUPT_BANK;
/******************************************************************************/
#define TASK_BIT(taskno) (1<<(taskno))
#define RUNNING(task) (ready & (TASK_BIT(task)))
/** Used in functions which may or may not have EA on on entry but which contain critical sections.
* Each usage in a function uses another bit variable
* do not use in an ISR as it modifies the carry bit in END_CRITICAL and the running task gets hit with the C bit change */
#define USE_CRITICAL static Bool EA_local /**< declaration section: using a bit flag to copy old EA status */
#define BEGIN_CRITICAL { if(EA) {EA =0; EA_local =1; } else {EA_local = 0;};} /**< this code encourages SDCC to use JBC atomic operation, so EA will be 0 inside section, and old state is stored in local flag. */
#define END_CRITICAL { EA=EA_local; } /**< EA is restored */
#define RESCHEDULE reschedule(); /**< do not use sleep() as this now waits 100ms */
/* need all of these in scope in main() */
extern volatile STACK_PTR_TYPE stack_save[TASKS]; /**< Idata pointers to task stacks */
extern volatile SIGNAL_TYPE task_signals[TASKS]; /**< Bytes representing the task structures */
extern volatile SIGNAL_TYPE task_masks[TASKS]; /**< if a signal bit set then it is masked with this */
extern volatile TIMER_TYPE task_timer[TASKS]; /**< Counters decrementing at 112.5Hz */
extern volatile TASKID_TYPE ready ; /**< bits 5,4 are schedule table index, bits 3 2 1 0 are task ready to run bits */
extern volatile TASKID_TYPE run ; /**< currently running task */
extern code char const priotab[];
typedef enum { QUEUED,FAILED,STARTED } start_rc;
typedef void (*task_p)(void );
/** the stack for the task is built and then its run flag is set. Not necessary to call this
for task 0 as will be effectively started in call to tasks_init() */
extern start_rc start_task(task_p f, /**< address of function */
char tasknum); /**< task number to assign function to */
extern void end_run_task(void);
/* look at task flags and run the appropriate task. Repeated calls will eventully try and run all the other tasks */
extern void reschedule(void);
/** schedules future timer signal as well as earlier timeouts of signals */
extern char wait_timed(char signal,/**< A byte made out of all the signals which are to be acknowledged.
TIMER_SIG is implicit in the use of this call */
char ticks);/**< The number of 100ms periods before timeout */
/** Add the signals to the set which will set the ready flag for this task.*/
#define enable_signal(pattern) task_masks[run] |= (pattern)/**< A byte made out of all the signals which are to be enabled */
/** Remove the signals from the set which will set the ready flag for this task. Does
not clear the signal itself. */
#define disable_signal(pattern) task_masks[run]&= ~(pattern)/**< A byte made out of all the signals which are to be disabled */
/** the list of all of the signals currently active on this task before masking */
#define curr_signal() task_signals[run]
/** the current running task acknowledges the signal bits in the argument */
extern void clear_signal(SIGNAL_TYPE pattern);/**< A byte made out of all the signals which are to be acknowledged */
/** Sends a signal to the task referred to. Does not actually cause rescheduling
until either a T0 interrupt, or a sleep, reschedule or wait_timed call made by this task */
extern void signal(char task, /**< Task number (0 to 3) */
SIGNAL_TYPE pattern); /**< A byte made out of all the signals which are to be sent */
/** Signal sending acro for interrupt context
* Sends a signal to the task referred to. Does not actually cause rescheduling
* until either a T0 interrupt, or a sleep or wait_timed call made by this task */
#define INT_SIGNAL(task,pattern) \
task_signals[(task)]|=(pattern);\
if(task_signals[(task)] & task_masks[(task)])\
{\
ready |= TASK_BIT(task);\
}
/** Function to cooperatively reschedule : sleep = 0 or wait for up to ticks before
return. Although this uses the timer signal it is acknowledged and cleared internally.
Use wait_timed() to obtain a timer signal : Will poll for timer signal if the scheduler
returns this task as next to run even if the timer is not expired (happens on task 0) */
extern void sleep(TIMER_TYPE ticks);
/** Function to configure Hardware to run RT */
extern void rt_system_init(void);
/** setup the scheduler workspace. Use with EA off. Initially sets Task 0 (IDLE_TASK) as
running and ready to run */
extern void rt_tasks_init(void);
/*************************************************************************/
/** Timebase counting
*
* The MAXT100ms
* wrap value should be divisible by 4 because of
* the fractional N counting in T0 Interrupt code*/
#define MAXT100ms 100
#define TIMER T100ms
#define TIMER10sec T10sec
/** can use the 100ms counter as an extra timer up to 10 seconds
in the future by using this macro to determine the final value */
#define WRAP_TIME(x) if(x>=MAXT100ms) x-= MAXT100ms
/** definitions of timer rates */
#define MS_PER_TICK 100
/** a macro to get the count necessary for a certain delay */
#define CONV_MS(x) (((x)+(MS_PER_TICK-1))/MS_PER_TICK)
#define CLOCKS_PER_SEC (1000/MS_PER_TICK)
/** timer registers used by T0 IRQ : T0 interrupts at 112.5 Hz */
extern volatile unsigned char T0ctr;/**< counts from 11 down to 1 or from 12 down to 1 (25% of time). Prescales to give exact 1 second timing from this timer */
extern volatile unsigned char T100ms;/**< counts modulo 200, increment once every about 100ms .
BUT 10 counts is exactly one second */
extern volatile unsigned char T10sec;/**< counts modulo 10 seconds off T0 interrupt */
#endif