Writing to and reading data from Flash using IAP

armcembeddedflashmicrocontroller

Here you can see functions I'm using to store data into Flash, but I'm getting data erased after power reset, so it's still in RAM not in Flash.

#define   OFFSET_ADDRESS                     0x00030000                                                                                        
#define   OFFSET_VERSION                     0x00030002
#define   OFFSET_SERIAL                      0x00030004  

    void read_from_flash (void)   {                     

        BYTE i, *pok_add, *pok_ver, *pok_serial_no;

        pok_add = (BYTE *)OFFSET_ADDRESS;
        display_address = *pok_add;
        delay_1_ms();
        if ((display_address > MAX_OUTDOOR_DISPLAY_ADDRESS) || (display_address < MIN_OUTDOOR_DISPLAY_ADDRESS))  display_address = UNI_ADDRESS;


        pok_ver = (BYTE *)OFFSET_VERSION;
        version = *pok_ver;
        delay_1_ms();
        if (version > MAX_FW_VERSION)        version = 0;                        

        for (i=0;i<8;i++)     {
            pok_serial_no = (BYTE *)OFFSET_ADDRESS;
            serial_no[i] = *pok_serial_no++;
            delay_1_ms();
        }
        for (i=0;i<8;i++)         if (serial_no[i] > 9)   serial_no[i] = 0;       

    }


    void write_to_flash (void)    {                     
    BYTE i;

        iap_copy_to_flash(&display_address, OFFSET_ADDRESS, 1);
        delay_1_ms();

        iap_copy_to_flash(&version, OFFSET_VERSION, 1);
        delay_1_ms();

        for (i=0;i<8;i++)     {
            iap_copy_to_flash(&serial_no[i], OFFSET_VERSION+i, 1);
            delay_1_ms();
            }

        }

Question is where I'm wrong? Sectors in a LPC178x/177x device http://www.nxp.com/documents/user_manual/UM10470.pdf on page 883. Maybe I don't write and read data properly so it's still located in RAM.

Best Answer

Have you seen the IAP flow mentioned in section 37.8 of the data sheet? Make sure you follow all the steps mentioned there. Also your flash can be written to only if the address does not contain any data currently. If it was previously written you need to erase the entire sector and then program it again.Read this application note for an example on IAP for LPC17xx series. You may have a certail level of write protection/security enabled by default.