Hello,
my goal is to write a bootloader for SAM3S4C, which will sit on RS232 line, and write incoming data to flash. When finished, it should jump to newly written code. Problem is in flash writing section, all the functions return zero codes (meaning ok), but the flash remains untouched (0xFFFF.....).
I am using FLASHD_ functions from SAM3S ek, along with IAP, so all the code is run from FLASH (no scatter file alternations), excetp the hard coded IAP routines, which are in ROM. Mclk frequency is set to 64MHz, voltage 1.8V, flash wait cycles FWS = 2 (so 3 cycles).
Constants for flash access
Code:
#define AT91C_IFLASH (0x00400000) /**< Internal Flash base address */
#define IFLASH_SIZE 0x40000
#define IFLASH_PAGE_SIZE (256) /* Internal FLASH 0 Page Size: 256 bytes */
#define IFLASH_LOCK_REGION_SIZE (16384) /* Internal FLASH 0 Lock Region Size: 16 Kbytes */
#define IFLASH_NB_OF_PAGES (1024) /* Internal FLASH 0 Number of Pages: 1024 */
#define IFLASH_NB_OF_LOCK_BITS (16) /* Internal FLASH 0 Number of Lock Bits: 16 */
#define IRAM_SIZE 0xC000
#define AT91C_IFLASH_PROG (0x00404000)
The size of the compiled code is ~12kb, so it comfortably fits into the first lock region of flash - 16kB, 64 pages, address 0x00400000 - 0x00403FFF.
Code of FLASH write function
Code:
uint8_t err = 0;
uint16_t i;
uint32_t addr = AT91C_IFLASH_PROG + args->address*IFLASH_PAGE_SIZE;
volatile uint8_t* cpdata = (volatile uint8_t*) addr;
err = FLASHD_IsSecurityBitSet();
//unlock
err += FLASHD_Unlock(addr,addr + IFLASH_PAGE_SIZE,0,0);
//write
err += FLASHD_Write(addr,args->data,IFLASH_PAGE_SIZE);
//verify
for ( i = 0; i < IFLASH_PAGE_SIZE; i++ )
{
err += (cpdata[i] != args->data[i]);
if ( err )
{
break;
}
}
//lock
err += FLASHD_Lock(addr,addr + IFLASH_PAGE_SIZE,0,0);
So to sum it up, page address is 0, so I am trying to write to address AT91C_IFLASH_PROG = 0x00404000, 256bytes.
FLASHD_IsSecurityBitSet returns 0
FLASHD_Unlock and FLASHD_Write returns 0
cpdata[i] = 0xFF != args->data[i] for i <0,255>
I would appreciate any ideas about what I am doing wrong.
Regards
Entik