I needed a timer to count every second and even on sleep mode. Therefore I set a kernel timer and handler as below.
int app_ble_timer_handler(xxx)
{
gBleTimer++;
app_timer_set(SPS_TIMER, TASK_APP, 100); //1 sec
return (KE_MSG_CONSUMED);
}
In the main while loop, I printed the variable gBleTimer by UART and the accuracy of result was acceptable. After adding a delay into the main while loop, let say 3 seconds, I expected that during those 3 seconds delay the variable gBleTimer should be updated, however it wasn't the case. What I tried to do was to simulate there was a program loading (read and write RFID tag, measure the battery voltage) that would affect the kernel timer.
Device:
Hi jackiechau,
The kernel timers are handled by the kernel, that means that when its time for the timer_handler function to execute the scheduler has to check the if there is a timer in the queue in order to execute the corresponding callback function. The scheduler runs in the main loop (is the rwip_schedule() function) if you stall the main loop, the scheduler will delay in scheduling the messages, therefore the execution of your timer callback is going to be delayed.
Thanks MT_dialog
Hi MT_dialog,
Understood but I needed to count per second no matter how heavy the program loading was, so please advise where I should put "gBleTimer++" into in order to get an accurate counting value, thanks.
Hi jackiechau,
Its not advisable to stall for a long time the main while of the system, since even the BLE events needs to be schedueled as well, if there are long delays in the loop you will probably also miss crucial BLE events (perhaps you are not going to be able to keep a link). Its advisable if the code is time consuming to brake the code, if possible, to smaller chunks in order for the rwip_schedule() to be executed periodically (return from the app_asynch_trm true in order to force the 580 to stay awake, run the rwip_schedule() again and continue with executing your code).
In your case you can try to use the timers of the 580 and count (they are shut down though when the device is asleep) or you can try to use both of them kernel timers when you device falls asleep and hardware timers when you are awake and there is the possibillity that the code in the while will stall the system.
Thanks MT_dialog