Sleeping mode Advice needed (Atmega 324P)

Go To Last Post
8 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi Freaks :lol:

I have been designing a certain electronic Device and i am nearly finished
but i still need some bits n' bolts tidy up plus your ideas here would be very much appreciated.

My project has about 9 Tasks, in my Cooperative Real time Scheduling (Thanks to Kartman's for his Tutorial, I really made a use of it).
My Task 6 deals with switching some relays & Actuators based on if certain buttons were pressed. (it does this by keeping on pooling and read inputs to see if any button was pressed).

I have a Watchdog Resetting every 20 ms and is placed right next to dispatch () within the main () since each task was assigned to use about 15 ms when dispatch ever took longer than 15 ms until 20 ms then Watchdog will kick in an reset the MCU. this was fine as far as i could see.

The dilemma i had was on some sleeping mode. i really wanted to use power save mode but. to me based on the application of this project it seems like i can never have some periodic Sleeping time as Tasks ran independent. and i would like each task to go back to sleep as soon as it's done with its stuffs

Ex. Task 6 is reading about some pressed buttons and if a button is pressed Task 6 will execute its code and wait about 3 mins (during time in which it must remain active without Sleeping) and after these 3 mins task 6 will then read the status (either ON or OFF) of all relays & actuators and save them before it can allow the MCU to go back to sleep mode.
Other tasks also do something different but the issue here is that they all are active in completely different manner & time so if i was to create some periodic sleep time. this would surely create some violations.

My approach was to create a function "Go_to_Sleep()" and i would call Go_to_Sleep() everytime each task finishes its stuff. ofcourse we would wake up on next time the interrupt fires. this looks fine to me as far as i can see, But Though is this the right way of doing it? my thought was that since each task has its own Interrupt associated to it, every task would allow itself to run whenever its interrupt fires and then hand over to MCU to sleep and the other task etc...
So the Original question probably was "How do we put MCU to sleep having Tasks (Scheduled at different times obviously) given that we would like to maximise the sleeping time but preferably putting mcu to sleep evrytime there isn't any active task
Another Alternative i once though of was something like "Kicking Go_to_Sleep()" Thus Kicking MCU from Sleeping just like we do on a Watchdog but is this even possible (I am sorry if this sounds funny ':)' in some ways)
Thus I could just set MCU to Sleep and when every single active task is running it would be stopping CMU from sleeping and when it finishes the MCU would get to sleep?

Last issue was the facts that even when MCU is sleeping I am pretty sure convinced that the Watchdog would probably still be firing (since main() which was stopping it from reseting but main() itself is inaccessible during sleep ()
and this means some unnecessary MCU resets during sleep () unfortunately.
any standard Cleaver way on how to deal with this issue?
if this issue is not dealt, is this bad for MCU's life cycle?

PS: All of my Interrupts are very small, in facts the only thing the do is to only enable some boolean variables.

I am sorry for writing so much but i am sure someone will give me
a good insight in this.

Many Thanks & Merry Xmas to all.

Regards.
Kase

Last Edited: Sun. Dec 16, 2012 - 12:50 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Anyone to guide me on How the sleeping mode works into a real time Cooperative multitasking? Here the facts is that cooperatively multitasked system relies on each process regularly giving up time to other processes on the system, thinking about non-preemptive cases like fibers or evented IO, How would one acquire that Sleeping time? I really want to know the usual professional / standard way of dealing with this cases. a format or a pseudo-code here would be very much appreciated.

Cheers.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
The dilemma i had was on some sleeping mode. i really wanted to use power save mode but. to me based on the application of this project it seems like i can never have some periodic Sleeping time as Tasks ran independent. and i would like each task to go back to sleep as soon as it's done with its stuffs

I think you are confused about how this works.
There is only a single core, if you put it to sleep then nothing executes until it wakes up so I don't understand "each task to go back to sleep as soon as it's done with its stuffs".
You can't sleep any task because this will sleep the cpu which needs to execute other tasks, you can only delay the next call to that task for a specified amount of time.

Download this free ebbok http://www.tte-systems.com/books...
it explains the operation of a cooperative scheduler and has some samples with a scheduler written by the author.
There is also a download of the scheduler for AVR in that website.

Alex

"For every effect there is a root cause. Find and address the root cause rather than try to fix the effect, as there is no end to the latter."
Author Unknown

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I guess it depends on your OS but do the tasks have the concept of a "read to run" flag? If so then when a task yields the scheduler presumably runs down the list of all tasks and if any are set as "ready to run" it then executes that task. If, however it goes right through the entire list and does not find a task to run then at that point it can set a timer then go to sleep. Presumably either that timer interrupt or some other interrupt that one of the "not ready" tasks was waiting for, will occur at which point the cheduler is re-awoken and either it now finds a task that is "ready to run" or if they are all still waiting it just sets the timer and sleeps again and so on.

Sleeping is possibly easier to handle with a pre-emptive OS as you just have an "idle" task that absorbs all the CPU that the other tasks don't need and all it does is sleep().

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi Alex,

You are right, putting each task to sleep would def cause CPU to sleep, I too came to realize this and wouldn't make sense as far as a cooperative multitasking is concerned. i have been analyzing all different possible ways i could use to maximize the sleep time. because i really don't want MCU to keep on running while there aren't any actives tasks as it would be wasting precious power (I know i am using one of the pico-power processor based) but i am still frugal here. Anyways,

My current approach was to create the 10th Task and this task will only be designed to check troughout all 9 remaining tasks and see if there aren't any active peripherals due to be serviced and then it would put MCU to sleep , and MCU would wake up on the next interrupt and it would never sleep again until Task 10 confirms.
I do not know if this is the right way of doing it, but at least it's what i could think of at the moment.

Thx for your Comment and suggestion Alex.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Our posts crossed. Your "10th task" sounds quite a lot like the "idle task" I envisaged in a pre-emptive system or otherwise it's doing the job of the scheduler and the "ready to run" flags in a co-operative system.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thx too for the comment Cliff,
you are right preemptive multi-tasking would probably be the best in this as a certain given task can be suspended at anytime as far as order of priority is concerned. But again Well... i am only learning how these OS Multitaskings works hehe... as far as i can see cooperative Scheduling is easier to adapt to but again the worst case scenario would be having too many more tasks as all tasks fairy use the same time_chunk.
But i wouldn't take this further here being a learner in this already,
so i will have to find another way out. i am pretty sure although preemptive might be better but also i am convinced cooperative MMTSKing could do fairy well if built effectively. it's probably me who is crap in this here hehe.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I cannot help thinking that this is probably yet another reason to think very long and hard about any design and whether an OS actually really delivers more benefits than drawbacks. A well designed main() with a handful of accompanying ISR()s could well be a much more manageable solution than trying to shoe-horn in an OS and make it fit with what you are trying to do.

Of course there could well be some OS that have been specifically designed with the idea of power-saving and sleeping in mind so it could simply be that you chose the wrong OS in the first place.