LPC1837 IAP usage to update flash

Discussion created by lpcware Employee on Jun 15, 2016
Latest reply on Jun 15, 2016 by lpcware
Content originally posted in LPCWare by romanleonov on Thu Nov 26 03:27:10 MST 2015
Hi all!
Before explanation of my problem i want to tell my point of view of the working process with IAP flash update (by using command Copy_RAM_to_Flash, UM10430, p.74)

I want to write to sector 7, for example, of Flash bank A (i.e. start addres is 0x1A000000) 512 bytes from buffer, placed in ram. 512 - because of the Param2 command: number of bytes to written. It should be 512 | 1024 | 4096.
To do this, i perform next steps:

1) Create the entry to IAP (like in UM10430, p.72):

#define IAP_LOCATION *(volatile unsigned int *)(0x10400100);
typedef void (*IAP)(uint32_t[5], uint32_t[5]);
IAP iap_enrty = (IAP)IAP_LOCATION;

2) Next i perform three steps:

// First step - flash init

    uint32_t command[5], reply[5];
    iapCmd[0] = iap_Init;                        // iap_Init = 49
    iap(iapCmd, result);                        // call the IAP in the ROM

*here i had in result[0] something strange value which not equal CMD_SUCCESS (i.e. 0x00). This strange value looks like trash in memory.

// Second step - prepare flash to operation
    command[0] = iap_Prepare_sector_for_write_operation;
    command[1] = 7;                               //  Start sector 7 - want to write in sector 7 of bank A - 0x1A007000
    command[2] = 7;         //  End Sector the same
    command[3] = 0;                  //  Bank A
    iap(command, result);      // call the IAP in the ROM
** here i had the correct reply CMD_SUCCESS and it really seems like IAP does perform FLASH preparation.

//  Third step - write data to flash

    uint8_t data_to_write[512]; // Create the buffer in ram, addrees may differs, but always in address space of ram, for example: 0x10001F70
    memset(data_to_write, 0xAA, 512); // fill the buffer with 0xAA bytes

    command[0] = iap_Copy_RAM_to_Flash;
    command[1] = 0x1A007000;         //  Destination flash address where data bytes are to be written. This address should be a 512 byte boundary.
    command[2] = 0x10001F70;          //  Source RAM address from which data bytes are to be read. This address should be a word boundary.
    command[3] = 512;               //  Number of bytes to be written. Should be 512 | 1024 | 4096.
    command[4] = 180000;             //  CPU Clock Frequency (CCLK) in kHz, in my case i work on 180 MHz
    iap(command, result);     // call the IAP in the ROM

***here i got the result[0] value 0x09, which means SECTOR_NOT_PREPARED_FOR_WRITE_OPERATION.

Here are my questions:
Is my point of view correct?
1) Should i perform this three command one by one: flash init, prepare for operation, write flash?
2) I use the sector number like in LPC11Uxx uCs, i.e. 0x00000000 is 0 sector, 0x00001000 is 1 sector, and so on. Thus 0x00007000 is 7 sector. Am I right?
3) I use distination flash address like 0x1A007000, but if i use address like 0x00007000 i got returned value 0x05 - DST_ADDR_NOT_MAPPED. What the right one?

I reservated 2 kB on the start of SRAM to stack of IAP (because i had seen in the forum information about that IAP command select flash bank uses ~2000 bytes, so i think "maybe command copy is does the same"). And my (program's) ram address started from 0x10000800. So, i suppose that program and ram stacks do not clashed.
I tryed to make my CPU frequency not so high and on CPU 48 MHz result the same.

Could someone help me please?

Thank for attention and apologize for my long description and possible mistakes in grammatics.
Thank again.