Forum Discussion

Altera_Forum's avatar
Altera_Forum
Icon for Honored Contributor rankHonored Contributor
15 years ago

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's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    I didn't measure it myself....

    My colleague told me it was a few mA. Don't know how reliable it ist but I guess for mobile devices you can save at least something ....
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    --- Quote Start ---

    I don't want to use while(1).

    --- Quote End ---

    The point is, when the processor is not running the interrupt code, it will be running some code, somewhere. It would probably be better if it was your code, rather than some random bits scattered across uninitialized memory somewhere.

    --- Quote Start ---

    something odd happend ... as the main function (and the alt_main) has endded , the ISR i wrote was still active be the timer interrupt.

    --- Quote End ---

    All bets are off until you wrap your embedded run-time code in an endless loop. The moment your program 'falls off the end', your program is over and you should no longer consider yourself in charge of the hardware. The fact that the interrupt routine keeps running after your program ends is more of an example of poor programming practices than something you should be trying to exploit.

    --- Quote Start ---

    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).

    --- Quote End ---

    If it is not running your code, it is running some other code. As long as this completely unknown code does not alter the hardware, or change the stack pointer, or overwrite the stack frame, or any of a huge number of other possiblilities, your interrupt routine will keep running.

    --- Quote Start ---

    The ISRs still run though, as long as they aren't disabled.

    --- Quote End ---

    Or the code overwritten, or the stack pointer altered, or the stack data overwritten, or the interrupt pointed at another routine, or any of about a kazillion other possibilities...

    Yossi, just put some sort of loop in your code! If you don't like if() and you don't like while(), then do something else, but just do it!
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    Thank you all, I added some sort of while(1) to my code - I wrote scheduler that registered my function, and every tick run it and some other routines ...

    I think that an Idle state is necessary to the NIOS, after all, if my code is event driven, so most of the time (80%) the cpu should do NOTHING !!!!!!

    power consumption is an issue in embedded real time systems, and so is cpu temp, especially if one uses two cpu's or more .

    donq - if you are correct, so alt_main must disable all interrupts, just like any other real time operation system (I know i'm working OSless ...)

    how can I contact Altera to ask about cpu behavior after main has ended ?
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    Actually, especially if you're running it from the Quartus downloader/debugger, there is an "operating system" of sorts. And Altera already told you how it works because they gave you the source code.

    The easiest way to see what happens is to run the code in debug mode and look at the disassembly view.

    First, see what your 'while(1)' code looks like, then take it out and see where the code eventually winds up. Does it look familiar?

    Problem with depending on the _exit() routine to catch you, or lacking that, another 'bsr here' further along in the code, is that it may not be there in a final implementation of your code, running without the debugger, etc.

    Also, other environments will not have the same safety net. It is simply a bad practice to let your code run off the end. Once it does so, you are no longer in control of what happens. Even if you know exactly what happens today, you do not know what will happen in version 1.01, 1.02, 1.02b, 2.0, etc.

    An idle state might be nice, but if it doesn't exist on this processor, it's useless to try to invent it here. Even so, many processors come out of idle state on an interrupt and have to loop back to the 'Idle' instruction to re-enter the 'Idle' state. There's the while(1) loop again! Wrapped around the 'Idle' instruction.

    Curious is good... spend it on exploring the source code supplied by Altera. Do a text search for 'alt_main' and that will get you close. But leave the while(1) loop in your embedded code regardless of processor, operating system, and the availability (or not) of 'Idle' instructions.

    The point is not to see if you can make something unusual work under exactly the right conditions, it is to make it most likely to work under all conditions. There are enough problems with getting good code to run right, you don't need to stack bad coding on top of that.
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    --- Quote Start ---

    Do a text search for 'alt_main' and that will get you close. But leave the while(1) loop in your embedded code regardless of processor, operating system, and the availability (or not) of 'Idle' instructions.

    The point is not to see if you can make something unusual work under exactly the right conditions, it is to make it most likely to work under all conditions. There are enough problems with getting good code to run right, you don't need to stack bad coding on top of that.

    --- Quote End ---

    Thank you,

    where did you get your training, i'm searching for a good book/site and It seems that this forum is the only place ...

    Always happy to write better code :)