|
发表于 2008-2-28 14:48:13
|
显示全部楼层
icc的HELP中有说明
The AVR is a Harvard architecture machine, separating program memory from data memory. There are several advantages to such a design. For example, the separate address space allows an AVR device to access more total memory than a conventional architecture. For example, the ATmega series allows up to 64K words of program memory and 64K bytes of data memory, and future devices with possibly even greater amounts of program memory may be available later. However, the program counter still remains at 16 bits.
Unfortunately, C was not invented on such a machine. In particular, C pointers are either data pointers or function pointers, and C rules already specify that you cannot assume data and function pointers can be converted back and forth. However, with a Harvard architecture machine like the AVR, even a data pointer may point to either data memory or to program memory.
There are no standard C rules for how to handle this. The ImageCraft AVR compiler uses the "const" qualifier to signify that an item is in the program memory. Note that for a pointer declaration, the const qualifier may appear in different places, depending whether it is qualifying the pointer variable itself or the items that it points to. For example:
const int table[] = { 1, 2, 3 };
const char *ptr1;
char * const ptr2;
const char * const ptr3;
"table" is a table allocated in the program memory. "ptr1" is an item in the data memory that points to data in the program memory. "ptr2" is an item in the program memory that points to data in the data memory. Finally, "ptr3" is an item in the program memory that points to data in the program memory. In most cases, items such as "table" and "ptr1" are probably the most typical. The C compiler generates the LPM instruction to access the program memory.
Note that the C Standard does not require "const" data to be put in the read-only memory, and in a conventional architecture, this would not matter except for access rights. So, this use of the const qualifier is unconventional, but within the allowable parameters of the C standard. However, this does introduce conflicts with some of the standard C function definitions.
For example, the standard prototype for "strcpy" is strcpy(char *dst, const char *src), with the const qualifier of the second argument signifying that the function does not modify the argument. However, under ICCAVR, the const qualifier would indicate that the second argument points to the program memory which is likely not the case. Thus these functions are defined without the const qualifiers.
Finally, note that only const variables with file storage class will be put into FLASH. For example, variables that are defined outside of a function body or variables that have the static storage class have file storage class. If you declare local variables with the const qualifier, they will not be put into FLASH and undefined behaviors may result. The compiler emits a warning when it detects this situation.
ELPM and RAMPZ
To access Flash memory beyond 64K bytes (e.g. on a M103 or M128), the ELPM instruction must be used in conjunction of setting the RAMPZ bit to 1. A check box ("Use ELPM") has been added to the Compiler Target Options page for this purpose. This is useful for bootloader that are in the high memory. Unfortunately, in the general case, the compiler cannot determine whether a pointer to the flash memory is pointing to the high or low memory. You must enable this check box manually if you are putting constant data in the high memory and you must set the RAMPZ bit yourself. |
|