11.07.2015 Views

Imagecraft c compiler and development environment for the atmel avr

Imagecraft c compiler and development environment for the atmel avr

Imagecraft c compiler and development environment for the atmel avr

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

ICCV8 <strong>for</strong> AVR – C Compiler <strong>for</strong> Atmel AVRIf you access a global variable inside an interrupt h<strong>and</strong>ler, be sure that anymodifications of <strong>the</strong> global variable in <strong>the</strong> main application cannot be interrupted.Non-atomic access (i.e., access that may require multiple machine instructions)includes access to 16- or 32-bit variables, bit operations <strong>and</strong> non-basic C types(i.e., array).Spurious or unexpected interrupt behaviors can crash your program: You should always set up a h<strong>and</strong>ler <strong>for</strong> “unused” interrupts. An unexpectedinterrupt can cause problems. Beware that accesses to variables larger than <strong>the</strong> natural data size of <strong>the</strong>CPU require multiple accesses. For example, writing a 16-bit value on an 8-bitCPU probably requires at least two instructions. There<strong>for</strong>e, accessing <strong>the</strong>variable in both <strong>the</strong> main application <strong>and</strong> interrupt h<strong>and</strong>lers must be done withcare. For example, <strong>the</strong> main program writing to <strong>the</strong> 16-bit variable may getinterrupted in <strong>the</strong> middle of <strong>the</strong> 2-instruction sequence. If <strong>the</strong> interrupt h<strong>and</strong>lerexamines <strong>the</strong> variable value, it would be in an inconsistent state. Most CPU architectures do not allow nested interrupts by default. If youbypass <strong>the</strong> CPU mechanism <strong>and</strong> do use nested interrupts, be careful not tohave unbound nested interrupts. On most systems, it is best to set your interrupt h<strong>and</strong>lers to execute as fast aspossible <strong>and</strong> to use as few resources as possible. You should be carefulabout calling functions (your own or a library) inside an interrupt h<strong>and</strong>ler. Forexample, it is almost never a good idea to call such a heavy-duty library functionas printf inside an interrupt h<strong>and</strong>ler. With few exceptions, our <strong>compiler</strong>s generate reentrant code. That is, yourfunction may be interrupted <strong>and</strong> called again as long as you are careful withhow you use global variables. Most library functions are also reentrant, withprintf <strong>and</strong> related functions being <strong>the</strong> main exceptions.Test your external memory interface carefully. For example, do not just walk <strong>the</strong>entire external RAM range <strong>and</strong> verify write a few patterns in a single loop, as itmight not detect <strong>the</strong> case where <strong>the</strong> high address bits are not working correctly.The <strong>compiler</strong> may be doing something unexpected, even though it is correct. Forexample, <strong>for</strong> RISC-like targets such as <strong>the</strong> Atmel AVR, TI MSP430 <strong>and</strong> <strong>the</strong> ARMCPU, <strong>the</strong> <strong>compiler</strong>s may put multiple local variables in <strong>the</strong> same machine registeras long as <strong>the</strong> usage of <strong>the</strong> local variables does not overlap. This greatly improves<strong>the</strong> generated code, even though it may be surprising when debugging. Forexample, if you put a watch window on two variables that happen to be allocatedto <strong>the</strong> same register by <strong>the</strong> <strong>compiler</strong>, both variables would appear to be changing,even though your program is modifying only one of <strong>the</strong>m.39

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!