LPC11u24 EEPROM Storage. Help please

10 Feb 2013

Hi all...

First, C is not my native language.

There is a very helpful "access the onboard eeprom" library out there, and this is it below. Now.. all I want to do is hold an integer in the EEPROM, so I can read it after a reboot and use it in my program. I'd like to be able to update the integer now and then while my program is running.

Surely not too hard, if I could make head or tail of the code below, I am pretty sur eI could pop one in and pull it out using the read and write to the EEPROM.

Can anyone who has a better grasp of C than me, please give me a hand ?

thanks in advance.. (hopefully)

Dave.

*    IAP demo : demo code for internal EEPROM memory access
 *
 *    LPC11U24FBD64/401 : EEPROM 4KB
 *    Remark: The top 64 bytes of the EEPROM memory are reserved and cannot be written to.
 *
 *     by Tedd OKANO http://mbed.org/users/okano/notebook/iap-in-application-programming-internal-flash-eras/
 *     modified by Suga (supported to LPC11U24)
 */

#include    "mbed.h"
#include    "IAP.h"

#define     MEM_SIZE        256
#define     TARGET_ADDRESS   64

void    memdump( char *p, char *b, int n );
int     isprint( int c );

IAP     iap;


int main() {
    char    mem[ MEM_SIZE ];    //  memory, it should be aligned to word boundary
    char    mem2[ MEM_SIZE ];
    int     r;

    printf( "IAP: EEPROM writing test\n" );
    printf( "  device-ID = 0x%08X, serial# = 0x%08X, CPU running %dkHz\n", iap.read_ID(), iap.read_serial(), SystemCoreClock / 1000 );

    for ( int i = 0; i < MEM_SIZE; i++ )
        mem[ i ]    = i & 0xFF;

    r   = iap.write_eeprom( mem, (char*)TARGET_ADDRESS, MEM_SIZE );
    printf( "copied: SRAM(0x%08X)->EEPROM(0x%08X) for %d bytes. (result=0x%08X)\n", mem, TARGET_ADDRESS, MEM_SIZE, r );

    r   = iap.read_eeprom( (char*)TARGET_ADDRESS, mem2, MEM_SIZE );
    printf( "copied: EEPROM(0x%08X)->SRAM(0x%08X) for %d bytes. (result=0x%08X)\n", TARGET_ADDRESS, mem, MEM_SIZE, r );

    // compare
    r = memcmp(mem, mem2, MEM_SIZE);
    printf( "compare result     = \"%s\"\n", r ? "FAILED" : "OK" );

    printf( "showing the EEPROM contents...\n" );
    memdump( (char*)TARGET_ADDRESS, mem2, MEM_SIZE );
}


void memdump( char *base, char *buf, int n ) {
    unsigned int    *p;

    printf( "  memdump from 0x%08X for %d bytes", (unsigned long)base, n );

    p   = (unsigned int *)((unsigned int)buf & ~(unsigned int)0x3);

    for ( int i = 0; i < (n >> 2); i++, p++ ) {
        if ( !(i % 4) )
            printf( "\n  0x%08X :", (unsigned int)base + i * 4 );

        printf( " 0x%08X", *p );
    }

    printf( "\n" );
}

10 Feb 2013

Actually... the test program above fails to store the bytes on my mbed. That may be a problem with my mBed though. Would still be good to know how to change the above to just put an int into/out of EEPROM.

cheers

Dave.

10 Feb 2013

I get this.

IAP: EEPROM writing test
  device-ID = 0x0980002B, serial# = 0x17170333, CPU running 48000kHz
copied: SRAM(0x10000DF0)->EEPROM(0x00000040) for 256 bytes. (result=0x00000001)
copied: EEPROM(0x00000040)->SRAM(0x10000DF0) for 256 bytes. (result=0x00000001)
compare result     = "FAILED"
showing the EEPROM contents...
  memdump from 0x00000040 for 256 bytes
  0x00000040 : 0x00000000 0x00000000 0x00000000 0x00000000
  0x00000050 : 0x00000227 0x10000F38 0x00000000 0x00000B85
  0x00000060 : 0x00000016 0x00000000 0x6667C000 0x00000272
  0x00000070 : 0x00000000 0x00000B85 0x00000016 0x00000000
  0x00000080 : 0x6667C000 0x00000272 0x00000000 0x00000000
  0x00000090 : 0x00000000 0x00000000 0x00000258 0x10000F80
  0x000000A0 : 0x00000000 0x000006A1 0x00000000 0x00000000
  0x000000B0 : 0x00000416 0x00000001 0x00000000 0x00000000
  0x000000C0 : 0x00000000 0x00000791 0x0000040B 0x00000001
  0x000000D0 : 0x00000000 0x00800000 0x00000000 0x0000000F
  0x000000E0 : 0x0000000F 0x0000000E 0x000000A2 0x00002327
  0x000000F0 : 0x000000A2 0x0000000D 0x0000000E 0x396FAE14
  0x00000100 : 0x46160000 0x02DC6C00 0x00000001 0x00000138
  0x00000110 : 0x40B39333 0x40008000 0x00002580 0x40008000
  0x00000120 : 0x000008A8 0x00000000 0x400483C0 0x00001097
  0x00000130 : 0x00002950 0x00001CF9 0x000003E8 0x00002950
26 Sep 2014

I worked it out in the end.. I've just done this little program to show how it works.

http://mbed.org/users/kstech/code/EepromTest/

I've not got an mbed to hand to run it, but this is cut from a fully working large program, and compiles,