Hi Steve,

Steve Hosgood wrote:

On Tue, 2004-11-09 at 07:59, Dmitry wrote:
No bugs at all.
Just coding...
read_flag must be declared volatile
to reach if() iner code.
~d


while(1) {
    if(read_flag == 1)
    {
read_flag = 0; <-- can not reach TXBUF0 = *Flash_ptrA; }
    else if(flash_flag ==1)
    {
       flash_flag =0;
}
}

Are you sure about that, Dmitry?
The code looks OK to me - though it is a bit odd. I assume the original
poster only posted extracts...

First time around the while loop (assuming read_flag starts off as 1)
then set it to zero and write to TXBUF0.
              ^^^^^^^^^^^^^^

Second time around the while loop (assuming flash_flag starts off as 1)
set flash flag to zero.
                ^^^^^^^^^^^^^^^^^^^^^^

For all time after that, spin and do nothing.

I suppose I can see that if the loop gets unrolled, the both "set X to
zero" statements underlined above might get optimised out.
it certainly would be more informative to know all of the code (and the -O level) but I wouldn't assume the flags to be set to 1 in that very function or their definitions before the loop. If not so and they are not declared volatile the compiler acted completely correct considering an uninitialized bss variable as zero initialized and never been changed. In this case the complete while(1) loop could vanish.

              ----------------------------

The only explanation I can see for having to declare 'read_flag'
volatile is if something else (like an IRQ handler) might set read_flag
to 1.

That's the point ...

If that's the case, then of course the 'while' loop won't see the
change if it's not declared volatile.

But the original poster didn't say that, did he?

...and the only scenario (IRQ/TASK or anything else) where this polling loop design is usually used and makes sense to me. Indeed he didn't say anything explicitly about that but didn't deny either ;-)

And why does the
rewrite without the 'else' work? Did the compiler unroll the loop in the
first case, but not in the rewritten version?
That's what I'm still wondering about. Haven't found any explanation yet. Any ideas?

Arnd-Hendrik

Steve.




-------------------------------------------------------
This SF.Net email is sponsored by:
Sybase ASE Linux Express Edition - download now for FREE
LinuxWorld Reader's Choice Award Winner for best database on Linux.
http://ads.osdn.com/?ad_id=5588&alloc_id=12065&op=click
_______________________________________________
Mspgcc-users mailing list
Mspgcc-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/mspgcc-users



Reply via email to