MegaThread
Macros | Typedefs | Functions
thread.h File Reference
#include <stdint.h>
#include <avr/io.h>

Go to the source code of this file.

Macros

#define THREAD_MAX   2
 defines the maximum number of threads More...
 
#define THREAD_MS_PER_TICK   10
 
#define THREAD_STATUS_DEFAULT   1
 defines a default status which should returned by the thread function if no state machine is used. More...
 

Typedefs

typedef int8_t threadStatus_t
 defines the integer type used for the thread state. More...
 
typedef struct threadTCBData_t * pThread_t
 
typedef threadStatus_t(* threadFunc_t) (threadStatus_t state, void *pArgument)
 

Functions

pThread_t threadAdd (threadFunc_t f, void *pArgument, uint16_t periodTicks)
 
void threadStart ()
 
void threadBeginCriticalBlock ()
 
void threadEndCriticalBlock ()
 

Macro Definition Documentation

#define THREAD_MAX   2

defines the maximum number of threads

#define THREAD_MS_PER_TICK   10

Defines the milliseconds per timeslice and must be a positive integer literal. It should be the greatest common divisor of all periods, but must not exceed the value of 261/[CPU-clock in MHz]

#define THREAD_STATUS_DEFAULT   1

defines a default status which should returned by the thread function if no state machine is used.

Typedef Documentation

typedef struct threadTCBData_t* pThread_t
typedef threadStatus_t(* threadFunc_t) (threadStatus_t state, void *pArgument)

Prototype for thread function. The function must return a state value, which is passed back on the next call. This value can be used for a state machine and is not used outside the function, but the return value should never be zero, because this marks the first call of the function. You should use the macro 'THREAD_STATUS_DEFAULT' if you do not use a state machine.

Parameters
stateactual state of the state machine, if a state machine is used. This value was returned by the last call and is set to zero at the first call of the function.
pArgumentthis parameter is defined in threadAdd and is provided to the thread function at every call.
See also
threadAdd
typedef int8_t threadStatus_t

defines the integer type used for the thread state.

Function Documentation

pThread_t threadAdd ( threadFunc_t  f,
void *  pArgument,
uint16_t  periodTicks 
)

Adds a new thread to the list. This function must not be called after starting the scheduling. The priority of the task depends on the order of the adding of the threads. The first function has the highest, the last the lowest priority.

See also
threadStart
Parameters
fpointer to the thread function
pArgumentthis parameter is delivered to the thread function at every call. Typically it would be a pointer to struct with parameters and/or a pointer to a struct holding the static data of the function. (You should avoid using static variables in function used for a thread)
periodTicksnumber of ticks between to calls. You should use ' [period in ms]/THREAD_MS_PER_TICK' here.
Returns
NULL if the list was already full, not NULL if the task was successfully added.
void threadBeginCriticalBlock ( )
inline

Marks the beginning of instructions, which must not interrupted by another thread. This function only affects the timetick of the scheduler, all another interrupts remain active. This block must be closed by 'threadEndCriticalBlock'.

See also
threadEndCriticalBlock
void threadEndCriticalBlock ( )
inline

Marks the end of instructions, which must not interrupted by another thread.

See also
threadBeginCriticalBlock
void threadStart ( )

Configures the timer, enables global interrupts and starts executing of the tasks.