AHI Functions and Explanations for WakeTimer
WakeTimer
Implementation of vAHI_WakeTimer
-related compatible functions.
- This implementation is not intended to maintain full compatibility.
- Notes and considerations known at the time of writing are provided.
- The internal implementation may change without notice.
- Only parameters requiring attention are documented; for others, refer to the original AHI library manual.
The Wake Timer has two channels, both running continuously regardless of sleep state.
The counter operates in a decrementing manner, generating an interrupt when it reaches zero.
Additionally, since the wake timer keeps running, it can be used as a clock to measure elapsed time since startup.
For this purpose, a set of FRWT (Free Running WTimer) functions is also provided.
- Timer width:
E_AHI_WAKE_TIMER_0
= 41-bit,E_AHI_WAKE_TIMER_1
= 28-bit.
The latter differs from BLUE/RED (JN516x); note that the maximum count value is0x0FFFFFFF
when calculating time differences.
For example, to compute elapsed counts betweenct1
andct2
, use(((ct2 << 4) - (ct1 << 4)) >> 4)
. - This library does not provide functions handling timer values exceeding 32 bits.
- In the mwx library and some applications, one timer (
E_AHI_WAKE_TIMER_0
) is used continuously for time measurement through the Free Running WTimer (FRWT
) API.
vAHI_WakeTimerEnable()
void vAHI_WakeTimerEnable(uint8 u8Timer,
bool_t bIntEnable)
Initializes the timer.
vAHI_WakeTimerStart()
void vAHI_WakeTimerStart(uint8 u8Timer, uint32 u32Count)
Starts the timer.u32Count
specifies the counter value.
For TWELITE GOLD, to trigger an interrupt after 1 second, specify 32768
(the frequency of the 32KHz crystal oscillator).
vAHI_WakeTimerStop()
void vAHI_WakeTimerStop(uint8 u8Timer)
Stops the timer.
u32AHI_WakeTimerCalibrate()
uint32 u32AHI_WakeTimerCalibrate(void);
Returns the wake timer calibration value 10000
.
- Normally, with a low-precision RC timer, this value would vary by about ±30%, but since TWELITE GOLD uses a 32KHz crystal-based timer circuit, this calibration is unnecessary, and the function always returns
10000
.
u32AHI_WakeTimerRead()
uint32 u32AHI_WakeTimerRead(uint8 dev)
Returns the counter value. The wake-up interrupt occurs when the counter reaches zero. Afterward, the counter continues decrementing (after 0, it rolls over to the maximum value).
u8AHI_WakeTimerStatus()
uint8 u8AHI_WakeTimerStatus(void)
Returns the bitmap indicating the timer status.E_AHI_WAKE_TIMER_0
→ E_AHI_WAKE_TIMER_MASK_0
(1)
E_AHI_WAKE_TIMER_1
→ E_AHI_WAKE_TIMER_MASK_1
(2)
u8AHI_WakeTimerFiredStatus()
uint8 u8AHI_WakeTimerFiredStatus()
Used immediately after wake-up.
If the wake-up source is WTIMER, a non-zero value is returned.
- For
E_AHI_WAKE_TIMER_0
, returnsE_AHI_WAKE_TIMER_MASK_0
(1)
. - For
E_AHI_WAKE_TIMER_1
, returnsE_AHI_WAKE_TIMER_MASK_1
(2)
.
Free Running WTimer (FRWT)
Uses one channel of the WAKE TIMER (typically E_AHI_WAKE_TIMER_0
)
as a continuously running real-time counter.
This allows time measurement regardless of the sleep state.
Global Variables
G_TWENET_FREE_RUNNING_WTIMER_ENABLED()
uint8_t G_TWENET_FREE_RUNNING_WTIMER_ENABLED() // MACRO
This variable is set in the cbAppColdStart(FALSE)
call.
Specify 0x80
to operate E_AHI_WAKE_TIMER_0
as FRWT.
While the WAKE TIMER counter decrements over time, FRWT increments.
- In the mwx library, FRWT with
E_AHI_WAKE_TIMER_0
is automatically configured.
g_twenet_free_running_wtimer_boot_tick
uint32_t g_twenet_free_running_wtimer_boot_tick
Stores the counter value immediately after waking from sleep (with RAM retention).
This is set when vAHI_FRWTSetBootCount_MW()
is called from within the TWENET library.
vAHI_FRWTStart_MW()
void vAHI_FRWTStart_MW(uint8_t u8Timer)
This function is primarily used within the TWENET library and
should not be used in user programs.
FRWT is started based on the setting of the global variable G_TWENET_FREE_RUNNING_WTIMER_ENABLED()
.
u32AHI_FRWTGetCurrentCount_MW()
uint32 u32AHI_FRWTGetCurrentCount_MW()
Returns the FRWT counter value (32000 counts/second).
u32AHI_FRWTGetCurrentCount_msec_MW()
uint32 u32AHI_FRWTGetCurrentCount_msec_MW(uint32* dec_part)
Returns the current FRWT count in milliseconds.
dec_part
specifies eitherNULL
or a pointer to auint32
variable.
If provided, it returns the value in 1/10 millisecond units (0..9).
u32AHI_FRWTConvertCount_msec_MW()
static inline uint32 u32AHI_FRWTConvertCount_msec_MW(uint32 ct, uint32* dec_part) {
// note: see wtimer::freerun_ct_convert_msec()
uint64_t v = 1000ull * ct;
if(dec_part) *dec_part = (10*(v & 32767)) >> 15;
return (uint32)(v >> 15);
}
Converts the FRWT counter value to milliseconds.
If dec_part
is specified, it returns the value in 1/10 millisecond units (0..9).
i32AHI_FRWTCompareCount_MW()
int32 i32AHI_FRWTCompareCount_MW(uint32 val_past, uint32 val_now)
Compares FRWT counter values.
Specify val_now
as the more recent value and val_past
as the older value.
In this case, the function returns a positive count value.
i32AHI_FRWTCompareCount_msec_MW()
int32 i32AHI_FRWTCompareCount_msec_MW(uint32 val_past, uint32 val_now)
Compares FRWT counter values.
Specify val_now
as the more recent value and val_past
as the older value.
In this case, the function returns the positive value in milliseconds.
vAHI_FRWTSetBootCount_MW()
void vAHI_FRWTSetBootCount_MW()
This function is primarily used within the TWENET library and should not be used in user programs.
It is used to save the FRWT counter value when waking up from sleep (with RAM retention, warm boot).
uint32 u32AHI_FRWTGetBootCount_MW()
uint32 u32AHI_FRWTGetBootCount_MW()
Returns the FRWT counter value when waking up from sleep (with RAM retention, warm boot).
u32AHI_FRWTGetBootCount_msec_MW()
uint32 u32AHI_FRWTGetBootCount_msec_MW()
Converts and returns the FRWT counter value in milliseconds when waking up from sleep (with RAM retention, warm boot).