Author Topic: [BitCloud] End device's power consumption  (Read 23545 times)

0 Members and 1 Guest are viewing this topic.

Offline ataradov

  • Super Contributor
  • ***
  • Posts: 11228
  • Country: us
    • Personal site
Re: [BitCloud] End device's power consumption
« Reply #75 on: January 23, 2017, 08:47:07 pm »
But if you use SYS_Sleep(), then it will overwrite stack's track of time, so you will have to do manual sleep management at this point.
Alex
 
The following users thanked this post: danergo

Offline danergoTopic starter

  • Regular Contributor
  • *
  • Posts: 111
  • Country: hu
Re: [BitCloud] End device's power consumption
« Reply #76 on: January 23, 2017, 09:07:55 pm »
That's exactly what I was thinking about in the past few minutes.

I'd need to call it with a HAL_Sleep_t structure, which needs a few fields.
Could you guide me on how shall I setup these fields?
-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?

Unfortunately I couldn't find anything in the opened sources.
 

Offline ataradov

  • Super Contributor
  • ***
  • Posts: 11228
  • Country: us
    • Personal site
Re: [BitCloud] End device's power consumption
« Reply #77 on: January 23, 2017, 09:12:55 pm »
-sleepTime - I need to calculate how much time spent already from the CS_SLEEP_PERIOD, and set it accordingly.
Correct.

-startPoll - true or false?
This value will be passed as an argument of the callback. HAL itself does not care what the value is.

-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);"
Alex
 

Offline danergoTopic starter

  • Regular Contributor
  • *
  • Posts: 111
  • Country: hu
Re: [BitCloud] End device's power consumption
« Reply #78 on: January 23, 2017, 09:26:22 pm »
Thanks!  :-+

-sleepTime - I need to calculate how much time spent already from the CS_SLEEP_PERIOD, and set it accordingly.
Correct.
Hm. I'm thinking on using halTimerControl.remainder for this. Is this a good approach? It's defined as global and seems to hold the remaining time for the sleeptimer to CS_SLEEP_PERIOD.

-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);"
In this function I would call ZDO_WakeUpReq to behave exactly the same as if CS_SLEEP_PERIOD ellapsed without any HW INT. Right?  :)
 

Offline ataradov

  • Super Contributor
  • ***
  • Posts: 11228
  • Country: us
    • Personal site
Re: [BitCloud] End device's power consumption
« Reply #79 on: January 23, 2017, 09:28:59 pm »
Is this a good approach?
No. It only holds this value until you start requesting manual sleeps. You need to fully manage sleep yourself. don't rely in any gloabal variables in the stack.

In this function I would call ZDO_WakeUpReq to behave exactly the same as if CS_SLEEP_PERIOD ellapsed without any HW INT. Right?  :)
I would post a task from this callback and do the real work in the task, but the general idea is correct.
Alex
 

Offline danergoTopic starter

  • Regular Contributor
  • *
  • Posts: 111
  • Country: hu
Re: [BitCloud] End device's power consumption
« Reply #80 on: January 24, 2017, 01:25:41 pm »
You need to fully manage sleep yourself. don't rely in any gloabal variables in the stack.

Shall I avoid ZDO_SleepReq at all? In this case I'd prefer to manually manage the sleeping part.

My idea without ZDO_SleepReq (CS_SLEEP_PERIOD set to 5 mins):
1.) SYS_Sleep(1 minute)
2.) callback in every minute, and decide
  • do the 1 minute tasks (e.g. increment minute counter)
  • if radio is not needed and checkin is also not needed, SYS_SLEEP(1 minute)
  • if radio or checkin is needed (checkin if minute_counter >= 5), do ZDO_WakeUpReq, and then SYS_Sleep(1 minute)
3.) On HW INT, MCU wakeup and decide
  • do the ISR part, and increment minute counter
  • if radio is not needed and checkin is also not needed, SYS_SLEEP(1 minute)
  • if radio or checkin is needed (checkin if minute_counter >= 5), do ZDO_WakeUpReq, and then SYS_Sleep(1 minute)

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?

Note: I know that I also have to take into account the durations in ISR and in callback. I'd set CS_SLEEP_PERIOD to 10 minutes to avoid any problems. In this case, ISRs and callbacks can leak 5 minutes of time which is impossible. Thus, this error is not cumulating because minute_counter shall be resetted after checkins.
 

Offline ataradov

  • Super Contributor
  • ***
  • Posts: 11228
  • Country: us
    • Personal site
Re: [BitCloud] End device's power consumption
« Reply #81 on: January 24, 2017, 05:18:50 pm »
Shall I avoid ZDO_SleepReq at all?
I don't think you can. It prepares actual stack infrastructure for work.

My idea without ZDO_SleepReq (CS_SLEEP_PERIOD set to 5 mins):
This is kind of a hack, so I don't know if it will work.

Q1.) Can I use SYS_Sleep without any previous ZDO_SleepReq?
Yes.

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?
I'm pretty sure that invalid status will be returned.

Q3.) If I use ZDO_WakeUpReq, will it do the checkin to Coordinator automatically, or something else is needed as well?
If your sleep was due to ZDO_SleepReq() noting else is needed.

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.

I was trying to point this out many years ago, but had no luck doing this. This is why I created LwMesh, which does things in a way people can actually use.
Alex
 
The following users thanked this post: danergo

Offline danergoTopic starter

  • Regular Contributor
  • *
  • Posts: 111
  • Country: hu
Re: [BitCloud] End device's power consumption
« Reply #82 on: January 24, 2017, 10:43:52 pm »
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.

Yepp, but anyway, I was thinking on this a lot in the past few days, and I still believe, that I'd need the MCU without radio for many cases.
So, One ultimate question remains: after ZDO_SleepReq, and HW INT arrival, how can I detect the current ellapsed time slept between SleepReq and HW INT? (Maybe with halGetTimeOfSleepTimer?)
Or halGetTimeOfSleepTimer is counting all the time, and I can save it's value before sleep, and check after HW INT?

Thanks!
 

Offline ataradov

  • Super Contributor
  • ***
  • Posts: 11228
  • Country: us
    • Personal site
Re: [BitCloud] End device's power consumption
« Reply #83 on: January 24, 2017, 10:54:17 pm »
Or halGetTimeOfSleepTimer is counting all the time, and I can save it's value before sleep, and check after HW INT?
That's how standard sleep routines do this, so it should work.

If you really want to make this work, you can probably do quite a bit by modifying HAL layer. It is not trivial, but doable.
Alex
 

Offline danergoTopic starter

  • Regular Contributor
  • *
  • Posts: 111
  • Country: hu
Re: [BitCloud] End device's power consumption
« Reply #84 on: January 25, 2017, 06:31:41 am »
Hm, why should I modify HAL?

halGetTimeOfSleepTimer is accessable from user space, so it's easy to save its value right before sleep, and upon wakeup I can calculate sleep time by a simple math.
Or you mean adding a new function to it like halGetTimeSlept, and handle all the calculations inside HAL?
 

Offline ataradov

  • Super Contributor
  • ***
  • Posts: 11228
  • Country: us
    • Personal site
Re: [BitCloud] End device's power consumption
« Reply #85 on: January 25, 2017, 06:33:41 am »
Hm, why should I modify HAL?
I feel like all the other HAL sleep stuff will be in a way. If you can make it work without modifying HAL, then good.
Alex
 

Offline danergoTopic starter

  • Regular Contributor
  • *
  • Posts: 111
  • Country: hu
Re: [BitCloud] End device's power consumption
« Reply #86 on: January 25, 2017, 05:59:24 pm »
I thought that it won't break anything:
Possible flows:
1.) ZDO_SleepReq->CS_SLEEP_PERIOD->fully awake(checkin)->ZDO_SleepReq
2.) ZDO_SleepReq->HWINT->calculate sleep timer->SYS_Sleep->CS_SLEEP_PERIOD-calculated_time->fully awake(checkin)->ZDO_SleepReq
3.) ZDO_SleepReq->HWINT->ZDO_WakeUpReq->fully awake(checkin)->ZDO_SleepReq
4.) ZDO_SleepReq->HWINT->calculate sleep timer->SYS_Sleep->HWINT->calculate sleep timer->SYS_Sleep->CS_SLEEP_PERIOD-calculated_time->fully awake(checkin)->ZDO_SleepReq

In the above cases I believe the HAL doesn't need to be modified.

Maybe, if HWINT is not subscribed by the app layer, it can cause problems. ?
How would the line continues:
5.) ZDO_SleepReq->HWINT(not published to app)->...?


Thanks very much!
 

Offline ataradov

  • Super Contributor
  • ***
  • Posts: 11228
  • Country: us
    • Personal site
Re: [BitCloud] End device's power consumption
« Reply #87 on: January 25, 2017, 06:04:25 pm »
2.) ZDO_SleepReq->HWINT->calculate sleep timer->SYS_Sleep->CS_SLEEP_PERIOD-calculated_time->fully awake(checkin)->ZDO_SleepReq

You will also have to restore wake up callback to the stack one before last wake up (which you don't know). ZDO sets "halSleep.callback = wakeupCallback;" where wakeupCallback() is a static function inside ZDO. So basically you would need to save the value of this pointer before setting your own, and then call it manually  when needed.
Alex
 

Offline danergoTopic starter

  • Regular Contributor
  • *
  • Posts: 111
  • Country: hu
Re: [BitCloud] End device's power consumption
« Reply #88 on: January 25, 2017, 10:13:46 pm »
Yes, I'm getting your point.

It is easy, I can give it directly to SYS_Sleep, right? (because SYS_Sleep happens only after HWINT)
Then after the timeout, it will call ZDO's wakeup fn.
If that is interrupted again by a HWINT, after processing the INT, I can still give that pointer to SYS_Sleep. (I dont need a custom callback for SYS_Sleep)

ZDO will think that nothing happened, and CS_SLEEP_PERIOD has expired -> will checkin the ED.
Am I right here? :)
 

Offline ataradov

  • Super Contributor
  • ***
  • Posts: 11228
  • Country: us
    • Personal site
Re: [BitCloud] End device's power consumption
« Reply #89 on: January 25, 2017, 10:17:02 pm »
It is easy, I can give it directly to SYS_Sleep, right? (because SYS_Sleep happens only after HWINT)
Give what? wakeupCallback is static inside the closed part of the stack, linker will let you use that name.

You can get the whole sleep structure saved in HAL and give that to SYS_Sleep() again.
Alex
 

Offline danergoTopic starter

  • Regular Contributor
  • *
  • Posts: 111
  • Country: hu
Re: [BitCloud] End device's power consumption
« Reply #90 on: January 26, 2017, 08:47:19 am »
What I wanted to mark here, is that with this mod, I won't need to create my own callback for SYS_Sleep, because I'm giving 'wakeupCallback' to it.
If I can count the right time to sleep, 'wakeupCallback' will be called exactly the same time like when no INTs arrived:
This is CS_SLEEP_PERIOD ms after the very first ZDO_SleepReq. Afterwards, wakeupCallback will be called and it will take care of checking in and making the ball rolling again in the StateMachine.

Thank you very much Alex for your kind help.  :-+

 

Offline ataradov

  • Super Contributor
  • ***
  • Posts: 11228
  • Country: us
    • Personal site
Re: [BitCloud] End device's power consumption
« Reply #91 on: January 26, 2017, 08:50:03 am »
because I'm giving 'wakeupCallback' to it.
And I was saying that you can't give wakeupCallback to it, it is an internal static function that you have no access to, even to take its address.

You can extract this address from the request already created by ZDO and stored in a global variable inside HAL.
Alex
 
The following users thanked this post: danergo

Offline danergoTopic starter

  • Regular Contributor
  • *
  • Posts: 111
  • Country: hu
Re: [BitCloud] End device's power consumption
« Reply #92 on: January 26, 2017, 08:52:21 am »
Yes!! Got your point now. :)

Thanks really much.



 

Offline danergoTopic starter

  • Regular Contributor
  • *
  • Posts: 111
  • Country: hu
Re: [BitCloud] End device's power consumption
« Reply #93 on: January 27, 2017, 08:13:15 pm »
Hello!

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?

Of course, if this is valid, I'd also need to set a bit larger CS_SLEEP_PERIOD in the Coordinator to avoid mistique behaviours.
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.

Thanks!


 

Offline ataradov

  • Super Contributor
  • ***
  • Posts: 11228
  • Country: us
    • Personal site
Re: [BitCloud] End device's power consumption
« Reply #94 on: January 27, 2017, 08:21:30 pm »
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. :)
Yes, that's exactly what I was suggesting.

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).
The chap way is to adjust it so it is not negative, but a small positive value.

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?
That may work too, I don't think it will break anything.  But keep in mind that wakeupCallback calls ZDO_WakeUpInd(). So you need to handle this properly.

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.
Sure, you can set much higher timeouts, like a whole day or something.
Alex
 
The following users thanked this post: danergo

Offline danergoTopic starter

  • Regular Contributor
  • *
  • Posts: 111
  • Country: hu
Re: [BitCloud] End device's power consumption
« Reply #95 on: January 28, 2017, 08:17:44 pm »
Hello!

Well, this might be a bit out-of-topic, but somehow relates to here.

I'm trying to deal with calculating the sleeping time to the next minute at 0 second. So this is something between 0 and 59999 ms.
Hm, that's too easy, mod60000 calculates exactly this. However I can't figure out what am I doing wrong:

Code: [Select]
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"

I know that printf uses void* pointers and casts them to the defined format.
So far, 'a' is alright.
However, for 'b', I have no idea. I'd like it to be exactly 55375. (I know about C89's truncate towards zero, but how could I overcome of that in avr-gcc?) Why is b -22865?
And well, for 'c', that's alright too.

A feel confused of b's current value. Any explanation would be very helpful, I guess.

Thanks!
 

Offline ataradov

  • Super Contributor
  • ***
  • Posts: 11228
  • Country: us
    • Personal site
Re: [BitCloud] End device's power consumption
« Reply #96 on: January 28, 2017, 08:22:05 pm »
A feel confused of b's current value. Any explanation would be very helpful, I guess.
int is 16 bits on AVR.

printf("data: a=%ld, b=%ld, c=%ld\n\r", a, b, c);
Alex
 

Offline danergoTopic starter

  • Regular Contributor
  • *
  • Posts: 111
  • Country: hu
Re: [BitCloud] End device's power consumption
« Reply #97 on: January 28, 2017, 08:35:57 pm »
Ah, first of all  |O.

Unfortunately it still has some defect, because now b is 42671. Seems very mistique, but there is a reason for sure.
 

Offline ataradov

  • Super Contributor
  • ***
  • Posts: 11228
  • Country: us
    • Personal site
Re: [BitCloud] End device's power consumption
« Reply #98 on: January 28, 2017, 08:39:19 pm »
Unfortunately it still has some defect, because now b is 42671. Seems very mistique, but there is a reason for sure.
Your a is negative, and I'm not sure how % operation is defined for negative numbers. I would try to transfer all this math to positive numbers.
Alex
 

Offline danergoTopic starter

  • Regular Contributor
  • *
  • Posts: 111
  • Country: hu
Re: [BitCloud] End device's power consumption
« Reply #99 on: January 28, 2017, 10:13:27 pm »
Wow, I just have solved this module question by this macro:
Code: [Select]
#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)

Both produces the same (correct) result, but they look really ugly to me. And besides of that I'm quite sure they will consume a lot of cycles to calculate.
If there is a faster, simplier way to calculate congruent modulo, I'd love to see. :)
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf