lpcware

storing data in flash - lpc1114

Discussion created by lpcware Employee on Jun 15, 2016
Content originally posted in LPCWare by Badman on Thu Jan 30 05:09:02 MST 2014
Hello,

Modeled on the code from the http://www.lpcware.com/content/blog/application-programming-iap-code-example

My code:

#include "iap_driver.h"
#include "driver_config.h"
#include "uart.h"

/* Flash offset where the configuration is stored */
#define CONFIG_FLASH_OFFSET            0x1000
#define CONFIG_FLASH_SECTOR            (CONFIG_FLASH_OFFSET >> 12)
#define CONFIG_FLASH_SECTOR_SIZE    1

#define CONFIG_FLASH_OFFSET2            0x2000
#define CONFIG_FLASH_SECTOR2            (CONFIG_FLASH_OFFSET >> 12)
#define CONFIG_FLASH_SECTOR_SIZE2    1


static uint8_t demo_messg[] = "Dane zapamietane przez IAP";
static uint8_t dana = 9;

int main(void)
{
    __e_iap_status iap_status;
    uint8_t *wsk;
    uint8_t liczba = 0;
    char *tmp_tekst;
    int8_t tmp[2];

    UARTInit(115200);

    /* Init the IAP driver */
    iap_init();

    // --- Zapamietanie demo_messg --- //

    /* Prepare the sector for erase */
    iap_status = (__e_iap_status) iap_prepare_sector(CONFIG_FLASH_SECTOR,
            (CONFIG_FLASH_SECTOR + CONFIG_FLASH_SECTOR_SIZE));
    if (iap_status != CMD_SUCCESS)
    UARTSend( "Prepre error\n\r", sizeof("Prepre error\n\r") );


    /* Erase the sector */
    iap_status = (__e_iap_status) iap_erase_sector(CONFIG_FLASH_SECTOR,
            (CONFIG_FLASH_SECTOR + CONFIG_FLASH_SECTOR_SIZE));
    if (iap_status != CMD_SUCCESS)
    UARTSend( "Erase sector\n\r", sizeof("Erase sector\r\n") );


    /* Prepare the sector for writing */
    iap_status = (__e_iap_status) iap_prepare_sector(CONFIG_FLASH_SECTOR,
            (CONFIG_FLASH_SECTOR + CONFIG_FLASH_SECTOR_SIZE));
    if (iap_status != CMD_SUCCESS)
    UARTSend( "Prepre sector write error\n\r", sizeof("Prepre sector write error\n\r") );

    /* Write data to flash */
    iap_status = (__e_iap_status) iap_copy_ram_to_flash(&demo_messg,
            (void *)CONFIG_FLASH_OFFSET, 256);
    if (iap_status != CMD_SUCCESS)
    UARTSend( "Write error\n\r", sizeof("Write error\n\r") );


    // --- Zapamietanie dana --- //
    /* Prepare the sector for erase */
    iap_status = (__e_iap_status) iap_prepare_sector(CONFIG_FLASH_SECTOR2,
                (CONFIG_FLASH_SECTOR2 + CONFIG_FLASH_SECTOR_SIZE2));
    if (iap_status != CMD_SUCCESS)
    UARTSend( "Prepre error\n\r", sizeof("Prepre error\n\r") );


    /* Erase the sector */
    iap_status = (__e_iap_status) iap_erase_sector(CONFIG_FLASH_SECTOR2,
                (CONFIG_FLASH_SECTOR2 + CONFIG_FLASH_SECTOR_SIZE2));
    if (iap_status != CMD_SUCCESS)
    UARTSend( "Erase sector\n\r", sizeof("Erase sector\r\n") );

    /* Prepare the sector for writing */
    iap_status = (__e_iap_status) iap_prepare_sector(CONFIG_FLASH_SECTOR2,
            (CONFIG_FLASH_SECTOR2 + CONFIG_FLASH_SECTOR_SIZE2));
    if (iap_status != CMD_SUCCESS)
    UARTSend( "Prepre sector write error\n\r", sizeof("Prepre sector write error\n\r") );


    iap_status = (__e_iap_status) iap_copy_ram_to_flash(&dana,
                (void *)CONFIG_FLASH_OFFSET2, 256);
        if (iap_status != CMD_SUCCESS)
        UARTSend( "Write error\n\r", sizeof("Write error\n\r") );


    // --- Wyswietlenie tekstu zapamietanego --- ///
    wsk = (uint8_t *) CONFIG_FLASH_OFFSET;
    UARTSend( "Zapamietany tekst: ", sizeof("Zapamietany tekst: ") );
    tmp_tekst = wsk;
    UARTSend( tmp_tekst, sizeof("Dane zapamietane przez IAP") );
    UARTSend( "\n\r", sizeof("\n\r") );

    // --- Wyswietlenie liczby zapamietanej --- ///
    wsk = (uint8_t *) CONFIG_FLASH_OFFSET2;
    liczba = *wsk;

    // zamiana liczby na tekst

    tmp[0] = '0' + liczba;
    tmp[1] ='\0';

    UARTSend( "Zapamietana liczba: ", sizeof("Zapamietana liczba: ") );
    UARTSend( tmp, 2 );

    if ( *wsk == 9)
    UARTSend( "dziewiec\n\r", sizeof("dziewiec\n\r") );
    else
    UARTSend( "Nie dziewiec\n\r", sizeof("Nie dziewiec\n\r") );



    while(1)
    {

    }

    return 0 ;
}


Why is the second record of the dana not write succeeds:

iap_status = (__e_iap_status) iap_copy_ram_to_flash(&dana,
                (void *)CONFIG_FLASH_OFFSET2, 256);
        if (iap_status != CMD_SUCCESS)
        UARTSend( "Write error\n\r", sizeof("Write error\n\r") )

Evry time iap_status != CMD_SUCCESS -  WHY ?

When I comment out the code responsible for recording the demo_messg, record variable dana succeeds.

Outcomes