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 AVRStringsAs explained in Program Data <strong>and</strong> Constant Memory, <strong>the</strong> separation of program <strong>and</strong>data memory in <strong>the</strong> AVR’s Harvard architecture introduces some complexity. Thissection explains this complexity as it relates to literal strings.The <strong>compiler</strong> places switch tables <strong>and</strong> items declared as __flash into programmemory. The last thorny issue is <strong>the</strong> allocation of literal strings. The problem is that, inC, strings are converted to char pointers. If strings are allocated in <strong>the</strong> programmemory, ei<strong>the</strong>r all <strong>the</strong> string library functions must be duplicated to h<strong>and</strong>le differentpointer flavors, or <strong>the</strong> strings must also be allocated in <strong>the</strong> data memory. TheImageCraft <strong>compiler</strong> offers two options <strong>for</strong> dealing with this.Default String AllocationThe default is to allocate <strong>the</strong> strings in both <strong>the</strong> data memory <strong>and</strong> <strong>the</strong> programmemory. All references to <strong>the</strong> strings are to <strong>the</strong> copies in data memory. To ensure <strong>the</strong>irvalues are correct, at program startup <strong>the</strong> strings are copied from <strong>the</strong> programmemory to <strong>the</strong> data memory. Thus, only a single copy of <strong>the</strong> string functions isneeded. This is also exactly how <strong>the</strong> <strong>compiler</strong> implements initialized global variables.If you wish to conserve space, you can allocate strings only in <strong>the</strong> program memory byusing __flash character arrays. For example:__flash char hello[] = “Hello World”;In this example, hello can be used in all contexts where a literal string can be used,except as an argument to <strong>the</strong> st<strong>and</strong>ard C library string functions as previouslyexplained.The printf function has been extended with <strong>the</strong> %S <strong>for</strong>mat character <strong>for</strong> printing outFLASH-only strings. In addition, new string functions have been added to supportFLASH-only strings. (See String Functions.)Allocating All Literal Strings to FLASH OnlyYou can direct <strong>the</strong> <strong>compiler</strong> to place all literal strings in FLASH only by selecting <strong>the</strong>Build Options - Target “Strings In FLASH Only” checkbox. Again, be aware that youmust be careful when calling library functions. When this option is checked, effectively<strong>the</strong> type <strong>for</strong> a literal string is __flash char *, <strong>and</strong> you must ensure <strong>the</strong> functiontakes <strong>the</strong> appropriate argument type. Besides new __flash char * related StringFunctions, <strong>the</strong> cprintf <strong>and</strong> csprint functions accept __flash char * as <strong>the</strong><strong>for</strong>mat string type. See St<strong>and</strong>ard IO Functions.118

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

Saved successfully!

Ooh no, something went wrong!