EVE 1.0
msec work scheduler
Collaboration diagram for msec work scheduler:

Data Structures

struct  mwork_t
 

Macros

#define MWORK_COUNTER_WIDTH   (24)
 
#define MWORK_GUARD_WIDTH   (2)
 
#define MWORK_EFFECTIVE_WIDTH   (MWORK_COUNTER_WIDTH - MWORK_GUARD_WIDTH)
 
#define MWORK_TIME_MASK   ((1 << MWORK_EFFECTIVE_WIDTH) - 1)
 
#define MWORK_ROUND_TIME(tick)   ((tick) & MWORK_TIME_MASK)
 
#define MWORK_TIME_SPAN_IS_NEGATIVE(span)   (((span) & (1 << (MWORK_EFFECTIVE_WIDTH - 1))) != 0)
 
#define MWORK_MSEC(ms)    MS_TO_TICKS(ms)
 
#define MWORK_INIT(x, callback)
 
#define MWORK_INIT_TYPED(x, callback)   (struct mwork_t) MWORK_INIT(x, callback)
 
#define DECLARE_MWORK(x, callback)   struct mwork_t x = MWORK_INIT(x, callback)
 

Typedefs

typedef uint32_t mwork_time_t
 
typedef void(* mwork_cb_t) (struct mwork_t *work)
 

Functions

static mwork_time_t mwork_now ()
 
void mwork_schedule (struct mwork_t *work)
 
void mwork_cancel (struct mwork_t *work)
 
static bool mwork_pending (struct mwork_t *work)
 
void mwork_timer (void)
 

Detailed Description

Macro Definition Documentation

#define MWORK_COUNTER_WIDTH   (24)

Counter register width, bits.

Definition at line 58 of file mwork.h.

#define MWORK_GUARD_WIDTH   (2)

Amount of counter register LSB bits, reserved as synchronization quard

Definition at line 64 of file mwork.h.

Referenced by mwork_now().

#define MWORK_EFFECTIVE_WIDTH   (MWORK_COUNTER_WIDTH - MWORK_GUARD_WIDTH)

Effective width if counter register width, bits.

Definition at line 70 of file mwork.h.

#define MWORK_TIME_MASK   ((1 << MWORK_EFFECTIVE_WIDTH) - 1)

Effective time mask.

Definition at line 76 of file mwork.h.

#define MWORK_ROUND_TIME (   tick)    ((tick) & MWORK_TIME_MASK)

Round time by the effective width of the counter register.

Definition at line 82 of file mwork.h.

#define MWORK_TIME_SPAN_IS_NEGATIVE (   span)    (((span) & (1 << (MWORK_EFFECTIVE_WIDTH - 1))) != 0)

Checks if mwork time span is negative.

Definition at line 88 of file mwork.h.

#define MWORK_MSEC (   ms)    MS_TO_TICKS(ms)

Converts a time interval, given in number of milliseconds, to the number of ticks.

Parameters
msduration of the time interval, milliseconds
Returns
number of ticks in the time interval

Definition at line 98 of file mwork.h.

#define MWORK_INIT (   x,
  callback 
)
Value:
{ \
.work.link.next = &(x).work.link, \
.work.link.prev = &(x).work.link, \
.work.cb = (work_cb_t) (callback), \
}
void(* work_cb_t)(struct work_t *work)
Definition: work.h:126

Initializes a milliwork structure.

The macro is used to initialize a millisecond-scale work (milliwork) structure instance.

Parameters
xmilliwork structure
callbackmilliwork callback

Usage:

1 static void MyWorkCallback(struct mwork_t *Work);
2 struct mwork_t MyWork = MWORK_INIT(MyWork, MyWorkCallback);

Definition at line 115 of file mwork.h.

#define MWORK_INIT_TYPED (   x,
  callback 
)    (struct mwork_t) MWORK_INIT(x, callback)

Initializes a milliwork structure. Typed version of MWORK_INIT

Definition at line 126 of file mwork.h.

#define DECLARE_MWORK (   x,
  callback 
)    struct mwork_t x = MWORK_INIT(x, callback)

Instantiates a milliwork structure.

The macro is used to instantiate a work structure.

Parameters
xname of the milliwork structure
callbackthe milliwork callback

Usage:

1 static void MyWorkCallback(struct mwork_t *Work);
2 DECLARE_MWORK(MyWork, MyWorkCallback);

Definition at line 144 of file mwork.h.

Typedef Documentation

typedef uint32_t mwork_time_t

Milliwork time type.

Only MWORK_EFFECTIVE_WIDTH LSB bits are used, while MSB of them becomes a sign bit, and the rest of the uint32_t is ignored by the framework. So, the max interval for the milliwork is 2^(24-2-1)-1 = 2097151 ticks, or ~34 min. Please use OS etimer for longer intervals.

Definition at line 148 of file mwork.h.

typedef void(* mwork_cb_t) (struct mwork_t *work)

Milliwork callback type.

Parameters
workpointer to the milliwork structure

Definition at line 167 of file mwork.h.

Function Documentation

static mwork_time_t mwork_now ( )
inlinestatic

Returns the current timestamp, which can be used as a time reference in struct mwork_t::at

Usage:

1 MyWork.at = mwork_now() + MWORK_MSEC(100);
2 mwork_schedule(&MyWork);
3 // The work is scheduled and will be executed 100 ms later.
Returns
current timestamp in system ticks

Definition at line 200 of file mwork.h.

References mwork_cancel(), MWORK_GUARD_WIDTH, mwork_schedule(), and mwork_t::work.

Referenced by app_timer_start().

void mwork_schedule ( struct mwork_t work)

Schedules a milliwork.

The function schedules a milliwork for execution at the specified point in time. The milliwork will be executed at the software interrupt level, like a work item. It is allowed to schedule a milliwork in the past. In this case it is triggered for execution immediately.

If two or more milliworks are scheduled for the same tick, they will be executed in the same order as thay were scheduled. Due to non-preemptive execution of work items and milliworks at the software interrupt level, the actual time when the milliwork execution starts can vary.

If the milliwork structure, pointed by the work parameter is already scheduled for execution, then it is re-scheduled at the new time.

A scheduled milliwork holds system at PM_LEVEL_LOWPWR power level.

Parameters
workpointer to the milliwork structure

Referenced by app_timer_start(), and mwork_now().

void mwork_cancel ( struct mwork_t work)

Cancels a scheduled milliwork.

The function cancels a previously scheduled work.

Parameters
workpointer to the milliwork structure

Referenced by app_timer_stop(), and mwork_now().

static bool mwork_pending ( struct mwork_t work)
inlinestatic

Checks if the milliwork is scheduled and pending execution.

The function checks if the milliwork is scheduled for execution.

Parameters
workpointer to the milliwork structure
Returns
true if the milliwork is scheduled for execution
false if the milliwork is not scheduled
false if the milliwork is being executed

Definition at line 249 of file mwork.h.

References dlist_is_empty(), work_t::link, mwork_timer(), and mwork_t::work.

void mwork_timer ( void  )

Milliwork timer

Referenced by mwork_pending().