Im reusing RAK_TIMER_0. Initially I create a oneshot timer, then make it periodic, the callback function changes but the timer still behaves as a oneshot timer.
api.system.timer.create(RAK_TIMER_0, reinterpret_cast<RAK_TIMER_HANDLER>(firstWork), RAK_TIMER_ONESHOT);
api.system.timer.start(RAK_TIMER_0, 1000);
// do some work for some time
api.system.sleep(10000);
// after a few seconds i want to reuse the timer in periodic mode
api.system.timer.stop(RAK_TIMER_0);
api.system.timer.create(RAK_TIMER_0, reinterpret_cast<RAK_TIMER_HANDLER>(secondWork), RAK_TIMER_PERIODIC);
// this way the timer triggers only once instead of periodically
api.system.timer.start(RAK_TIMER_0, 1000);
create and start return true, and it updates the callback function to secondWork but it’s still running in oneshot mode, ie secondWork only triggers once - and only if i calll api.system.timer.start(RAK_TIMER_0,1000); again
As far as I know that is not possible at the moment.
What is the return value of your second api.system.timer.create?
I am guessing it returns FALSE because the timer was already created.
I’m using the Rak11720, it doesnt seem to work for me, here’s the code im running:
{
Serial.println("first");
}
void second(void*)
{
Serial.println("second");
}
void setup()
{
// todo: Disable AT mode on production
Serial.begin(115200);
api.system.timer.create(RAK_TIMER_1, reinterpret_cast<RAK_TIMER_HANDLER>(first), RAK_TIMER_ONESHOT);
api.system.timer.start(RAK_TIMER_1, 1000, NULL);
api.system.sleep.all(10000);
// after a few seconds i want to reuse the timer in periodic mode
api.system.timer.stop(RAK_TIMER_1);
api.system.timer.create(RAK_TIMER_1, reinterpret_cast<RAK_TIMER_HANDLER>(second), RAK_TIMER_PERIODIC);
// this way the timer triggers only once instead of periodically
api.system.timer.start(RAK_TIMER_1, 1000, NULL);
}
void loop()
{
api.system.sleep.all();
}
looks like the timer id once created is never deleted, and so create is essentially a one time only and the only things you can change really is the callback function for that timer id:
if there was a way to destroy it or if the api allowed us to use vTimerSetReloadMode id be able to change the timer mode
i guess i could try a hacky fix by calling vTimerSetReloadMode directly with the timer_id, just need to figure out how to get access to it in my application code
Hey, looks like vTimerSetReloadMode is not part of the FreeRTOS base api, it’s in FreeRTOS+TCP. Looks like RAK uses base FreeRTOS so vTimerSetReloadMode cant be used. This also explains why it isn’t already included.
In the current state the timer would have to be destroyed and created again to be reused.
Adding a separate destroy method is going to propagate changes from uhal through udrv_timer_api all the way upto the RUI3 API so maybe that’s not the way to go
Destroying in stop is counter intuitive because you wont be able to run start on a destroyed timer
The best option I think, given current information, is to destroy the timer when creating if it already exists, reset apollo3_timer_id to NULL and proceed with creation of a new timer. What do you think?