AVR Sleep Mode Clarification

avravr-gccsleep

I have been learning about the AVR sleep modes as I would like to start using them in my future projects. I think I have understood the most of it except some specific details below

  1. Under Power Reduction Register (PRR), it mentions that using this to turn off the clock to a peripheral freezes the peripheral in it's current state with the I/O registers becoming inaccessible. Also the resources used by this peripheral would remain unoccupied. So its suggested to disable to peripheral before stopping the clock to it.

  2. Under AVR-gcc documention of , they have provided the atomic instructions (sleep_enable, sleep_cpu and sleep_disable) besides the all automatic sleep_mode macro. The reason given is that sleep_mode might case race condition in some cases.

Can someone please explain the above 2 points in more detail.

Thanks!

Best Answer

  1. When you disable a peripheral, you can turn it on with one instruction and resume it - meanwhile all resources used by this particular peripheral are freed and you are free to use them. When you turn the clock off you put the peripheral to wait - its resources can't be freed, since it requires a clock pulse to execute clearing instruction. Only thing you could do is turn the clock signal back on and resume using it.

  2. Race condition mentioned concerns pipeline stages in your CPU. To keep things short - your CPU processes more than a one instruction, and using correct variable values is based on when the instruction was fetched and decoded. If you modify one register/variable/bitfield sequentially with different instructions, there is a chance, that one of the instructions in given sequence would fetch a value before its processed by previous instructions, changing the output of operation, and therefore state of the circuit as a whole.