chipKIT® Development Platform

Inspired by Arduino™

Arduino code using PROGMEM ported to chipKIT

Created Wed, 25 May 2011 10:34:32 +0000 by nkcelectronics


nkcelectronics

Wed, 25 May 2011 10:34:32 +0000

AVR based Arduino boards have limited RAM space, compared to the new chipKIT boards, but it is still a very scarse resource. A technique commonly used to save RAM space by storing constant strings and arrays into FLASH (program) memory is by including avr/progmem.h in the code and using additional artifacts to access FLASH memory correctly.

There is a very good post that explains how PROGMEM works (and why just defining const is not enough to avoid RAM space limitation): [url]http://www.avrfreaks.net/index.php?name=PNphpBB2&file=viewtopic&t=38003[/url]

With chipKIT, I needed to run the RGB LCD shield example, adding 3 bitmaps that was taking too much RAM space. By doing some research, I was able to find that by using const and const_cast, I could include all the additional bitmap definitions and still have my code compile and run correctly.

Here is an example:

const unsigned short nkc_logo [8192] = { 0xFFFF, 0xBEEF, ... , 0x0000};

void setup() {
...
}

void loop() {
    unsigned short nkc_logo_p*;

    ...

    nkc_logo_p = const_cast<unsigned short *>(nkc_logo);
    myGLCD.drawBitmap(4,50,120,27,nkc_logo_p,1);   // Draw bitmap to the RGB LCD
...

Using this method I was able to fit a lot of constant data into the FLASH area and save precious RAM space (in my case, it was not even compiling because all 3 bitmaps were taking more than all the RAM space available).

I found the const_cast information in an IBM page, and it was described for the AIX C compiler. It must be pretty standard... but I am not sure why it is not used with the AVR platform. Probably it was not implemented in the avr-gcc yet or nobody cared to test, but for some reason people spent a lot of time and effort by writing the avr/progmem.h stuff... I tried using const_cast with the Arduino board and it compiles correctly, but for larger array sizes, the compiler reports an error for the Uno, but not for the MEGA 2560. So further research is needed in this area, but this could be one way of standardizing Arduino library and sketch code for multi-platform use.

I invite you to test and post comments and improvements, as this was a quick test / fix subject to many improvements and verifications.


Mark

Wed, 25 May 2011 11:38:17 +0000

I have the prog_char and PROGMEM stuff defined in the PIC32 implementation so that AVR code will automatically work.

I think PROGMEM was left out in the first release but its there now. All it is is

#define PROGMEM

which defines it as nothing

prog_char is defined as const char

Mark


nkcelectronics

Wed, 25 May 2011 13:19:45 +0000

What happens with casting to existing functions and method invocations? If they don't expect const, const_cast is still needed, right?


davec

Sat, 02 Jul 2011 00:20:05 +0000

I have the prog_char and PROGMEM stuff defined in the PIC32 implementation so that AVR code will automatically work. I think PROGMEM was left out in the first release but its there now. All it is is #define PROGMEM which defines it as nothing prog_char is defined as const char Mark

Which include file should PROGMEM and prog_char be defined in? In the mpide-0022-chipkit-win-20110619 distribution I can only see them in wiring.h.