Developing with ZBOSS for Zigbee
|
Typedefs | |
typedef zb_uint32_t | zb_time_t |
ZBOSS time subsystem is based on hardware timer. There is a special zb_time_t type defined in ZBOSS API for working with time variables in ZBOSS framework. The zb_time_t type size may be limited depending on a platform. ZBOSS handles the overflow.
The basic time measurement unit is "beacon interval" (tick) equal to 15.36 microseconds (see Zigbee specification revision 22 section 3.6.4.1 Scheduling Method).
#define ZB_BEACON_INTERVAL_USEC (ZB_SYMBOL_DURATION_USEC * ZB_ABASE_SUPERFRAME_DURATION) |
Beacon interval in microseconds
1 beacon interval = aBaseSuperframeDuration * symbol duration
1 symbol = 16e-6 sec (mac spec 6.5.3.2 Symbol rate) for 2.4GHz ZB. 1 beacon interval = 15.360 ms.
#define ZB_HALF_MAX_TIME_VAL (ZB_MAX_TIME_VAL / 2U) |
A half of defined maximum timer value.
#define ZB_MAX_TIME_VAL ZB_UINT32_MAX |
Maximum timer value, if 32-bit timer is used.
#define ZB_MILLISECONDS_TO_BEACON_INTERVAL | ( | ms | ) | ZB_MILLISECONDS_TO_BEACON_INTERVAL_CEIL(ms) |
Convert time from milliseconds to beacon intervals.
#define ZB_MILLISECONDS_TO_BEACON_INTERVAL_CEIL | ( | ms | ) | (((zb_time_t)(ms) * 1000U + (ZB_BEACON_INTERVAL_USEC - 1U)) / ZB_BEACON_INTERVAL_USEC) |
Convert time from milliseconds to beacon intervals (32-bit platforms). Round the result up.
#define ZB_MILLISECONDS_TO_BEACON_INTERVAL_FLOOR | ( | ms | ) | ((zb_time_t)(ms) * 1000U / ZB_BEACON_INTERVAL_USEC) |
Convert time from milliseconds to beacon intervals (32-bit platforms). Round the result down.
#define ZB_MILLISECONDS_TO_USEC | ( | ms | ) | ((ms) * (1000u)) |
Convert from milliseconds to microseconds
#define ZB_MIN_TIME_VAL ZB_UINT32_MIN |
Minimum timer value, if 32-bit timer is used.
#define ZB_MSEC_TO_QUARTERECONDS | ( | n | ) | ((n) / 250U) |
Convert from msec to quarterseconds
#define ZB_QUARTERECONDS_TO_BEACON_INTERVAL | ( | qsec | ) | ZB_MILLISECONDS_TO_BEACON_INTERVAL(250U * (qsec)) |
Convert from quarterseconds to beacon
#define ZB_QUARTERECONDS_TO_MSEC | ( | n | ) | 250UL*(n) |
Convert from quarterseconds to msec
#define ZB_SECONDS_TO_BEACON_INTERVAL | ( | _s | ) | ZB_MILLISECONDS_TO_BEACON_INTERVAL(1000UL * (_s)) |
Convert from seconds to beacon
This macro works correctly on 32-bit platforms for intervals smaller than 71 minutes. The calculation was not tested on 16-bit platforms.
#define ZB_SECONDS_TO_MILLISECONDS | ( | _s | ) | (1000ul*(_s)) |
Convert from seconds to milliseconds
#define ZB_TIME_ADD | ( | a, | |
b | |||
) | (zb_time_t)((a) + (b)) |
Time add: add 'a' to 'b'
Overflow is possible, but this is ok - it handled by subtraction and compare macros.
a | - time to add to |
b | - value to add |
#define ZB_TIME_BEACON_INTERVAL_TO_MSEC | ( | t | ) | ((ZB_BEACON_INTERVAL_USEC * (zb_time_t)(t)) / 1000U) |
Convert time from beacon intervals to milliseconds (32-bit platform).
#define ZB_TIME_BEACON_INTERVAL_TO_USEC | ( | t | ) | ((zb_uint64_t)ZB_BEACON_INTERVAL_USEC * (t)) |
Convert time from beacon intervals to microseconds
#define ZB_TIME_GE | ( | a, | |
b | |||
) | ((zb_time_t)((a) - (b)) < ZB_HALF_MAX_TIME_VAL) |
Compare times a and b - check that a >= b
Taking into account overflow and unsigned values arithmetic and supposing difference between a and b can't be > 1/2 of the overall time values diapason, a >= b only if a - b < values_diapason/2
a | - first time value to compare |
b | - second time value to compare |
#define ZB_TIME_ONE_SECOND ZB_MILLISECONDS_TO_BEACON_INTERVAL(1000U) |
One second timeout
#define ZB_TIME_QUARTERECONDS | ( | n | ) | (ZB_TIME_BEACON_INTERVAL_TO_MSEC((n)) / 250U) |
Quarterseconds timeout
#define ZB_TIME_SUBTRACT | ( | a, | |
b | |||
) | ((zb_time_t)((a) - (b)) < ZB_HALF_MAX_TIME_VAL ? (zb_time_t)((a) - (b)) : (zb_time_t)((b) - (a))) |
Time subtraction: subtract 'b' from 'a'
Take overflow into account: change sign (subtraction order) if result > values_diapason/2. Suppose a always >= b, so result is never negative. This macro will be used to calculate, for example, amount of time to sleep
a | - time to subtract from |
b | - time to subtract |
#define ZB_TIMER_GET | ( | ) | (zb_timer_get()) |
#define ZB_USECS_TO_MILLISECONDS | ( | usec | ) | ((usec) / (1000u)) |
Convert from microseconds to milliseconds
typedef zb_uint32_t zb_time_t |
The idea is: platform has some timer which can be stopped or run. When run, it increments (or decrements - depends on platform) some counter until counter overflow (underflow), then issues interrupt - wakeups main loop if it sleeping. Time stored in ticks; time resolution is platform dependent, its usual value is 15.36 usec - 1 beacon interval.
All that timer macros will not be used directly by the application code - it is scheduler internals. The only API for timer is ZB_SCHEDULE_ALARM() call. Timer type.