The CCS C Compiler supports C++ like function overloading. Function overloading allows the user to develop two functions with the same name but have different parameters or return types. Recently the CCS C Compiler updated the function overloading to distinguish between RAM and ROM pointers as well. This is useful on the PIC® MCU which uses the Harvard architecture, meaning accessing the RAM and ROM is different. Since accessing RAM and ROM is different, the compiler needs to know which one the user is accessing so it can use the proper method.
Here is a simple example of function overloading using ROM and RAM pointers:
int1 Checksum(rom char* pRom, int hashCompare) { int hash = HashCalculateRom(pRom); if(hash == hashCompare) return 1; else return 0; }
int1 Checksum(char* pRam, int hashCompare) { int hash = HashCalculate(pRam); if(hash == hashCompare) return 1; else return 0; }
SetSavedString((rom char*) "Hello World");
SetSavedString((char*) "foo bar");
Checksum() has two functions, one for handling ROM pointers and one for handling RAM pointers. In the CCS C Compiler the 'rom' keyword specifies the ROM flash memory, if this is not specified the CCS C Compiler will use RAM. When Checksum() is called with the "Hello World" as a 'rom char*' It passes the pointer to the HashCalculateRom() method, that calculates the hash value for the pointer and compares it to the hashCompare value passed in by the user. The same is done when "foo bar" is passed in as a 'char*' but it passes the pointer into the HashCalculate() method that is used for RAM locations. If the calculated hash matches the user input hashCompare, then Checksum() returns '1', otherwise it returns '0'.
Here is a more complex example of function overloading based on ROM and RAM pointers:
struct { union { rom char* pRom; char* pRam; }; int1 isRom; } savedString;
void SetSavedString(rom char* pRom) { savedString.pRom = pRom; savedString.isRom = TRUE; }
void SetSavedString(char* pRam) { savedString.pRam = pRam; savedString.isRom = FALSE; }
void PrintSavedString(void) { if(savedString.isRom) printf("%s", savedString.pRom); else printf("%s", savedString.pRam); }
SetSavedString((rom char*)"Hello World");
SetSavedString((char*) "foo bar");
SetSavedString() saves a (char*) or (rom char*) depending on what was passed in by the user to memory, and sets a flag denoting the type of pointer that is being used. The PrintSavedString() checks the flag to select the proper pointer, and prints the string.
Like us on Facebook. Follow us on Twitter.
About CCS:
CCS is a leading worldwide supplier of embedded software development tools that enable companies to develop premium products based on Microchip PIC® MCU and dsPIC® DSC devices. Complete proven tool chains from CCS include a code optimizing C compiler, application specific hardware platforms and software development kits. CCS' products accelerate development of energy saving industrial automation, wireless and wired communication, automotive, medical device and consumer product applications. Established in 1992, CCS is a Microchip Premier 3rd Party Partner. For more information, please visit http://www.ccsinfo.com.
PIC® MCU, MPLAB® IDE, MPLAB® ICD2, MPLAB® ICD3 and dsPIC® are registered trademarks of Microchip Technology Inc. in the U.S. and other countries.
|