Loader routine:
Code: Select all
void copy_ram_to_flash(void) __attribute__((section("ldr"))); //0x20001000
void copy_ram_to_flash(void){
register unsigned d = LOADER_PG_BGN; //destination page number
register unsigned s = LOADER_PAYLOAD; //source (0x20001500)
register unsigned i; //inner loop
REG_PIOA_PER = PIO_PA16; //indication LED init
REG_PIOA_OER = PIO_PA16;
REG_PIOA_OWER = PIO_PA16;
while(d <= LOADER_PG_END){ //copy payload from page to page int[color=#FF0000]o flash
REG_PIOA_CODR = PIO_PA16; //indication pro update process
for(i = 1000000; i > 0; i--){;}
REG_PIOA_SODR = PIO_PA16;
for(i = 1000[/color]000; i > 0; i--){;}
while (0 == (LOADER_EFC_STA & 0x1)); //wait for efc status ready
for(i = 0; i < LOADER_PG_SZ; i += 4) //fill efc latch buffet
*(unsigned *)(LOADER_FLASH + i) = *(unsigned *)(s + i);
LOADER_EFC_CMD = 0x5A000003 | (d << 8); //write & erase efc command
d += 1;
s += LOADER_PG_SZ;
}
while (0 == (LOADER_EFC_STA & 0x1)); //wait for efc status ready
LOADER_RST_CNT = 0xA500000F; //reset proc. to bet back to SAM-BA
}