-sleepTime - I need to calculate how much time spent already from the CS_SLEEP_PERIOD, and set it accordingly.
-startPoll - true or false?
-callback - shall I use my own wakeup callback, or it needs to be set to some internal function?
-sleepTime - I need to calculate how much time spent already from the CS_SLEEP_PERIOD, and set it accordingly.Correct.
-callback - shall I use my own wakeup callback, or it needs to be set to some internal function?Your own function with the following prototype: "typedef void (* HAL_WakeUpCallback_t)(bool);"
Is this a good approach?
In this function I would call ZDO_WakeUpReq to behave exactly the same as if CS_SLEEP_PERIOD ellapsed without any HW INT. Right?
You need to fully manage sleep yourself. don't rely in any gloabal variables in the stack.
Shall I avoid ZDO_SleepReq at all?
My idea without ZDO_SleepReq (CS_SLEEP_PERIOD set to 5 mins):
Q1.) Can I use SYS_Sleep without any previous ZDO_SleepReq?
Q2.) After waking up from SYS_Sleep, can I use ZDO_WakeUpReq, or it will return with ZDO_INVALID_REQUEST_STATUS if I didn't do any ZDO_SleepReq before?
Q3.) If I use ZDO_WakeUpReq, will it do the checkin to Coordinator automatically, or something else is needed as well?
It look like the best thing to do is to actually properly wake up every time. This sucks, but the rest of it really depends on experimentation.
Or halGetTimeOfSleepTimer is counting all the time, and I can save it's value before sleep, and check after HW INT?
Hm, why should I modify HAL?
2.) ZDO_SleepReq->HWINT->calculate sleep timer->SYS_Sleep->CS_SLEEP_PERIOD-calculated_time->fully awake(checkin)->ZDO_SleepReq
It is easy, I can give it directly to SYS_Sleep, right? (because SYS_Sleep happens only after HWINT)
because I'm giving 'wakeupCallback' to it.
I've done some research in the HAL's code, and realized that HAL already stores halSleep.callback and halSleep.startPoll in a global variable named 'halSleepControl'.
I'm using this now, and works as I thought, without modifying HAL's code.
However, what happens, if the IRQ arrives right before CS_SLEEP_PERIOD expired? Let's imagine this situation. In this case, the next calculated time_to_sleep shall be mathematically negative.
So it's quite sure that Coordinator will handle this ED as disconnected (I don't know the right expression here).
Is it a good approach that before calling SYS_Sleep, I'd check time_to_sleep. If it would be negative, I call halSleep.callback immediately, instead of SYS_Sleep.
So, to reverse this question, is it valid to call ZDO's 'wakeupCallback' in case the MCU itself is not sleeping anymore?
Is it okay, to set CS_SLEEP_PERIOD to 10mins in the Coordinator, and 5mins in the ED? In this case ED will checkin before C would judge it as disconnected.
uint32_t a = 21625 - 26250;
uint32_t b = a % 60000U;
uint32_t c = 60000U - b;
printf("data: a=%d, b=%d, c=%d\n\r", (int)a,(int)b,(int)c);
//prints "data: a=-4625, b=-22865, c=17329"
A feel confused of b's current value. Any explanation would be very helpful, I guess.
Unfortunately it still has some defect, because now b is 42671. Seems very mistique, but there is a reason for sure.
#define mod(M, N) (M>=0 ? M%N : (N-abs(M%N))%N)
or
#define mod2(M, N) (M%N>=0 ? M%N : M%N + N)