Forum Discussion
How to set Idle state ?
Hello, I'm a new user so be gentle :)
I need to create an Idle state, I have ISR called by the clock every 1 ms, and I need to get rid of the for(;;) loop in the main function this is my main : int main() { // ISR registration ... for(;;); } I don't want to use the for(;;) ... btw - what is the meanning of "Program never exits" ? thank you ...16 Replies
- Altera_Forum
Honored Contributor
Your program has to be doing something while it's not executing the interrupt code, might as well be a for(...) loop. Otherwise you could have it doing something else useful, or un-useful, like calculating the last digit of pi.
If you really don't have something else to do, and have some aversion to for(...), you might try this, it does the same thing.
"Program never exits" is a signal to the compiler that you have either a for() or a while() loop in main. In this case, the compiler does not have to insert cleanup code after the end of main, like fclose(stdin/out/err), and a variety of other housekeeping chores that are not needed if you have used a for() or while() loop in the main program to prevent it from exiting. It is not an automatic way to make it not exit. I don't think there is a 'sleep' or 'idle' mode on Nios, but you would wrap that in a for() or while() loop anyway.main() { while(1) { // blah, blah..., whatever code, or completely blank } } - Altera_Forum
Honored Contributor
There actually is a sleep mode in Nios 2. It's called usleep (). You can find it in the Nios II Software Developer’s Handbook.
int usleep (unsigned int us) and it can be found in <unistd.h>. But the best way to not let your program end is indeed to just put in a while(1) loop. - Altera_Forum
Honored Contributor
I think that by "sleep" mode, donq meant a low power mode where the CPU would shut down until the next interrupt. This doesn't exist on Nios II.
The usleep function keeps the CPU busy by executing the same instruction again and again until the required delay is achieved. It should be renamed uwake ;) - Altera_Forum
Honored Contributor
thank you all ...
I don't want to use while(1). something odd happend ... as the main function (and the alt_main) has endded , the ISR i wrote was still active be the timer interrupt. can someone explain what the CPU is doing in this state (only the ISR is active for 200 uSec, and 800 uSec nothing is running). - Altera_Forum
Honored Contributor
Why don't you want to use for(;;) or while(1) ?
I'm not sure about what the CPU is doing after exiting main. It should either be stuck in an infinite loop, if it exists, or start to execute random code, which could be a bad thing. The ISRs still run though, as long as they aren't disabled. - Altera_Forum
Honored Contributor
There is no idle state for NIOS by default.
But I think you can write your own small slave component which stalls the NIOS by holding the WAITREQUEST and therefore blocks the bus. You can wake it up again, in case of an interrupt (e.g. Timer). My colleague told me about it. I think there must be some older threads on this subject in the forum. I didn't do it on my own. I just know that it is possible and that it's saving power in fact. - Altera_Forum
Honored Contributor
Asserting WAITREQUEST is unlikely to save power (it will still be sampled every clock).
Probably the only way to save power is to stop the cpu clock - and I suspect that is difficult. There is the other question about what you are actually trying to achieve. For a simple embedded system it is not unusual to not use any interupts, but just have the main loop check each hardware device in turn. Interrupts are only needed for hardware that have strong timing requirements. An ethernet interface will usually process a buffer ring, so there is no real need to take any interupts. Similarly even a serial port with a large enough FIFO can be polled. If you don't use interrupts, you save all the cpu cycles used saving and restoring registers, and those vectoring the interrupt itself. Of course, you could go the other way and leave the main processing loop being for (;;);, do all your work from ISRs, only allow one level of ISR, and then not bother saving and restoring any registers! - Altera_Forum
Honored Contributor
@dsl: I must object...... It does save power
Maybe I didn't explain it good enough. Well I am only a softi....;-) Take a look at this thread http://www.alteraforum.com/forum/showthread.php?t=19136&highlight=reduce+power+consumption Even if it's not much but actually this approach is saving power..... - Altera_Forum
Honored Contributor
Perhaps I meant 'save much power' ...
You could also stall cpu execution in a multi-cycle custom instruction. That gives you two 32bit values to play with as well, and a result. One could be a bit-mask of IRQ bits (or similar), the other a maximum clock count. The result could be the bit(s) that woke the system up. Quite possibly both these stalls cause the clock to almost all of the nios cpu to get suspended. Removing the instruction fetch and all the associated signal changes. (hmmm... if you disable dynamic branch prediction and execute 'foo: bxx foo' from tightly coupled instruction memory then almost nothing is likely to change!) - Altera_Forum
Honored Contributor
--- Quote Start --- @dsl: I must object...... It does save power Maybe I didn't explain it good enough. Well I am only a softi....;-) Take a look at this thread http://www.alteraforum.com/forum/showthread.php?t=19136&highlight=reduce+power+consumption Even if it's not much but actually this approach is saving power..... --- Quote End --- How much power? Did you take a measurement? If you post "this approach is saving power" - then please post how much savings so user's can benefit from this approach and know if it is worthwhile to implement. Bill