mwf_periph_wtimer
- WWDT, FRWT
This implements the_wtimer
, a peripheral object that summarizes the procedures for using the wake-up timer.
With TWENET, this is used implicitly, so no initialization is required in the user’s program.
The wake-up timer counts down based on the 32768Hz crystal oscillator built into the module. For example, if the timer starts from 32768, the counter will reach 0 and an interrupt will occur after 1 second. It is typically used for waking up from sleep. Since the counter continues to count down even after waking up, you can calculate the elapsed time since waking up by reading the counter value.
There are two wake-up timer channels: channel 0 uses a 40-bit counter (used as a 32-bit counter in this library), and channel 1 uses a 28-bit counter.
The FRWT (Free Running Wake Timer) procedure, which uses the wake-up timer, is also included. By taking advantage of its ability to operate with low current consumption during sleep, one of the two wake-up timer channels can be kept running constantly, allowing it to be used as a counter for real-time. In TWENET, FRWT functions with a specific setting, which allows for efficient waiting for ADC initialization, etc., while FRWT is running.
TWENET uses channel 0 as the FRWT and channel 1 as the regular wake-up timer.
Code example
- include
#include "mwf_periph_wtimer.hpp"
class mwf::periph::wtimer
global_init_wtimer_manager()
, global_deinit_wtimer_manager()
static void global_init_wtimer_manager();
static void global_deinit_wtimer_manager();
These functions create and destroy the the_wtimer
class object.
init()
, deinit()
void init();
void deinit();
These functions initialize or terminate the wake-up timer.
- The
u32ms
parameter during initialization specifies the timeout in milliseconds (ms). If it is0
or omitted, the timeout will be 4000ms.
start()
, stop()
void start(uint8_t dev, uint32_t u32ct)
void stop(uint8_t dev)
These functions start or stop the wake-up timer.
- For
dev
, specify the device number of the wake-up timer to be used (WTIMER0_DEVICE
orWTIMER1_DEVICE
). - For
u32ct
, specify the initial count value. - If you specify a wake-up timer channel that is currently running as an FRWT, the function will do nothing.
read()
uint32_t read(uint8_t dev)
This function reads the count value of the wake-up timer.
- For
dev
, specify the device number of the wake-up timer to be used (WTIMER0_DEVICE
orWTIMER1_DEVICE
). - The value of a wake-up timer channel that is currently running as an FRWT cannot be read.
is_running()
bool is_running(uint8_t dev)
This function determines whether the wake-up timer is currently running.
- For
dev
, specify the device number of the wake-up timer to be used (WTIMER0_DEVICE
orWTIMER1_DEVICE
). - If you specify a wake-up timer channel that is currently running as an FRWT, it returns
false
.
set_interrupt()
void set_interrupt(uint8_t dev, bool b_enabled)
This function specifies whether the wake-up timer should generate an interrupt.
- For
dev
, specify the device number of the wake-up timer to be used (WTIMER0_DEVICE
orWTIMER1_DEVICE
). - Setting
b_enabled
totrue
enables interrupts. If this is not specified, an interrupt will not occur even when waking up from sleep. Note that oncetrue
has been specified for a wake-up timer, you cannot disable the interrupt by specifyingfalse
. - If you specify a wake-up timer channel that is currently running as an FRWT, the function will do nothing.
get_fired_status_on_wakeup()
uint8_t get_fired_status_on_wakeup()
This function is called after waking up from sleep. If the wake-up cause was a wake-up timer, the corresponding bit (WTIMER0_DEVICE_MASK
or WTIMER1_DEVICE_MASK
) will be set.
class mwf::periph::wtimer (sys_ev_handler)
on_sleep()
There are no special procedures.
on_wakeup()
This function confirms the wake-up cause, saves the information internally, and clears the interrupt status. The timer also does not stop.
FRWT related functions
freerun_start()
, freerun_stop()
void freerun_start(uint8_t dev)
void freerun_stop()
These functions start and stop the FRWT. The count value is incremented at 32768Hz, starting from 0 at the beginning.
- For
dev
, specify the device number of the wake-up timer (WTIMER0_DEVICE
orWTIMER1_DEVICE
).
freerun_is_running()
bool freerun_is_running()
Returns true
if the FRWT is running.
freerun_is_device()
bool freerun_is_device(uint8_t dev)
Returns true
if the specified device is the one running as an FRWT.
- For
dev
, specify the device number of the wake-up timer (WTIMER0_DEVICE
orWTIMER1_DEVICE
).
freerun_ct_get()
uint32_t freerun_ct_get()
Returns the FRWT’s count value. The FRWT’s count value is not the wake-up timer’s value itself, but is converted to an incrementing value from 0 (roughly a value with its sign inverted).
Count Value Calculation Functions
These functions convert the FRWT count value to milliseconds or calculate the difference between two count values.
freerun_ct_convert_msec()
uint32_t freerun_ct_convert_msec(uint32_t ct, uint32_t* dec_part = nullptr)
This function converts the FRWT count value ct
to milliseconds. If dec_part
is specified, it sets the value of the 1/10th digit to a value from 0 to 9.
freerun_ct_diff()
int32_t freerun_ct_diff(uint32_t val_past, uint32_t val_now)
This function calculates the difference between two count values. It is essentially val_now - val_past
, but it is calculated to account for cases where the counter returns to 0 after reaching its maximum value. Time differences up to half of the maximum counter value can be calculated, and if val_past
is older, it returns a positive value.
freerun_ct_diff_msec()
int32_t freerun_ct_diff_msec(int32 vdiff)
This function converts the counter difference vdiff
obtained from freerun_ct_diff()
to milliseconds.
freerun_ct_diff_usec()
int32_t freerun_ct_diff_usec(int32 vdiff)
int32_t freerun_ct_diff_usec(uint32_t val_past, uint32_t val_now)
This function converts the counter difference vdiff
obtained from freerun_ct_diff()
or the counter difference calculated from val_past
and val_now
to microseconds.
- Due to calculation, an
int32_t
overflow can occur (e.g., if the time difference exceeds approximately 2000 seconds).