Change startup vector after flashing a new image

Discussion around product based on ARM Cortex M3 core.

Moderators: nferre, ncollot

ian@suzo
Posts: 2
Joined: Tue Jun 16, 2015 1:07 am

Change startup vector after flashing a new image

Wed Jul 01, 2015 5:16 am

Hi,
I'm new to arm and couldn't find help on the forum about my code issue.
I am using the SAM3S4A and I'm trying to load a new image to flash. My code starts at 0x0040000 when loaded from the programmer, once I receive new firmware, I load the new image to flash at address 0x00420000. I'm not sure its even being flashed there as I can't debug it. What I think is the problem is that I need to change my start address to 0x00420000, but I'm not sure if it is right. Please see my code below:

Code: Select all

void flashUnlockPage(uint16_t page)
{
    uint32_t cmd;
    cmd = FLASH_WRITE_ENABLE_MAGIC | FLASH_CMD_CLB | ((uint32_t)page << 8);
    IAP_Function = (uint32_t (*)(uint32_t, uint32_t)) *((uint32_t *)IAP_ADDRESS);
    IAP_Function(0, cmd);
}

void flashEraseWritePage(uint16_t pageAddress)
{
    // Calculate actual start address of the page.
    uint32_t tableAddress = (uint32_t)pageAddress * (uint32_t)FLASH_PAGE_SIZE;
    uint16_t itr;
    uint32_t *ptr = (uint32_t *)temporaryPageBuffer;
    for (itr = 0; itr < FLASH_PAGE_SIZE; itr += sizeof(uint32_t), ptr++)
    {
        if ((FIRST_PAGE == pageAddress) && (sizeof(uint32_t) == (tableAddress + itr)))
        {
            appEntryPoint = *ptr;
            appEntryPoint += 0x20000;
            MMIO(tableAddress + itr) = appEntryPoint;
        }
        else
        {
            MMIO(tableAddress + itr) = *ptr;
        }
    }
    // Perform page erase.
    flashWritePage(pageAddress, false);
}

void flashPacketToPage(uint8_t* data, uint16_t data_size)
{
    uint8_t *dataPointer = data;
    uint16_t packetPage, tempPgBuff_index;
    uint32_t fmrBackup, data_address = 0x00020000;
    
    AT91C_BASE_NVIC->NVIC_ICER[0] = 0xFFFFFFFF;
    AT91C_BASE_NVIC->NVIC_ICER[1] = 0xFFFFFFFF;
    
    currentPage = (data_address & 0x3FFFF) >> PAGE_NUMBER_SHIFT;
    
    while (data_size)
    {
        packetPage = (data_address & 0x3FFFF) >> PAGE_NUMBER_SHIFT;
        
        if (currentPage != packetPage)
        {
             
            fmrBackup = EFC->EEFC_FMR;
            EFC->EEFC_FMR = (FWS_FOR_WRITE << 8);
            flashUnlockPage(currentPage);                                       // Do a Unlock page
            flashEraseWritePage(currentPage);                                   // Do a Erase-Write of the page.
            EFC->EEFC_FMR = fmrBackup;
            currentPage = packetPage;
        }
        
        tempPgBuff_index = data_address & PAGE_POSITION_OFFSET_MASK;
        temporaryPageBuffer[tempPgBuff_index] = *dataPointer++;
        data_address++;
        data_size--;
    }

    void(*application_code_entry)(void);
    application_code_entry = (void (*)(void))(unsigned *)(*(unsigned *) (appEntryPoint));
    __set_MSP(*(uint32_t *)appEntryPoint);
    application_code_entry();
}
The above code is just to test the flash function, from programing via the ULINK2 it will start at address 0x00400000 and once flashed via serial, it must start from 0x00420000 even if it has be reset.

From what I've read, the processor starts at 0x00000000 and at 0x00000004 is the app start address, however in the hex file it doesn't show 0x00400000 it looks like the end of the code address. can someone help.

Thanks

Return to “SAM3 Cortex-M3 MCU”

Who is online

Users browsing this forum: No registered users and 2 guests