This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

IAP flash programming - put program to RAM

Hi,

I'm using KEIL uVISION4 + SEGGER J-Link for programming NXP LPC1764 device.
I have developed C code for programming flash using IAP, and I came to point when I can successful programming sectors 1 to 17 of our LPC1764. Our algorithm is:

#include <LPC17xx.H>        // LPC17xx definitions
#include <stdio.h>

#define IAP_LOCATION 0x1FFF1FF1
#define Target_sector 2      //start sector
#define End_target_sector 2   //end sector
#define f_CCLK 2000            //core frequency
#define Mem_size 4096       //how many Bytes will be writes into Flash, can be 256/512/1024/4096

#define Flash_sector_0 0x00000000
#define Flash_sector_1 0x00001000
#define Flash_sector_2 0x00002000
#define Flash_sector_3 0x00003000

static char* sector_start_adress[] = {
(char*)Flash_sector_0, (char*)Flash_sector_1, (char*)Flash_sector_2, (char*)Flash_sector_3,
};

int iap_program(int start,int end,char *target_addr,char *source_addr,long size,int f_core)
{
unsigned int IAP_command[5];
unsigned int IAP_result[5];
int Result_prepare;

typedef void(*IAP)(unsigned int[], unsigned int[]);
IAP iap_entry;
iap_entry=(IAP) IAP_LOCATION;

//prepare sector for erase
IAP_command[0] = 50;
IAP_command[1] = (unsigned int)start;  //Start sector
IAP_command[2] = (unsigned int)end;      //End sector
iap_entry(IAP_command,IAP_result);
Result_prepare = IAP_result[0];

//erase sector
IAP_command[0] = 52;
IAP_command[1] = (unsigned int)start;  //Start sector
IAP_command[2] = (unsigned int)end;      //End sector
IAP_command[3] = f_core;
iap_entry(IAP_command,IAP_result);
Result_prepare = IAP_result[0];

//prepare sector for writing
IAP_command[0] = 50;
IAP_command[1] = (unsigned int)start;  //Start sector
IAP_command[2] = (unsigned int)end;      //End sector
iap_entry(IAP_command,IAP_result);
Result_prepare = IAP_result[0];

//writing to FLASH
IAP_command[0] = 51;
IAP_command[1] = (unsigned int)target_addr;
IAP_command[2] = (unsigned int)source_addr;
IAP_command[3] = size;
IAP_command[4] = f_core;

iap_entry(IAP_command,IAP_result);
return IAP_result[0];
}

int main ()  //Main Program
{
int Result_x;
static char mem[Mem_size] = {'T','E','S','T'};   //data buffer - data array with size depends on constant Mem_size
__disable_irq(); //disable irq

//start, end addr for prepare, destination address in flash, source address in RAM, number of bytes, CPU clock
Result_x=iap_program(Target_sector,End_target_sector,sector_start_adress[Target_sector],mem,sizeof(mem),f_CCLK);
__enable_irq();   //enable irq
return Result_x;
}

As I wrote, we can successful program Flash from sector 1 to 17. We can't program sector 0 because there is stored compiled program and after run program(erase sector 0 - we have logically all's 1 => missing code instruction for executing) there isn't present expected data.

So, I have some questions:

How can be stored compiled code(shown above) into RAM and run it from this location(results in storing data to address 0x0)?
Does this option lies only in Keil IDE destination settings? Or is it needed to put some new commands into my main() structure? If yes, which one?

Thanks for your answers!
Best regards,

Milan Muller