Program to update the D7A modem's firmware.

Dependencies:   modem_ref_helper DebouncedInterrupt

cup.cpp

Committer:
Jeej
Date:
2017-03-10
Revision:
15:24434827c575
Parent:
13:c3324b26d473
Child:
18:a8a640941018

File content as of revision 15:24434827c575:

#include "mbed.h"
#include "cup.h"
#include "bin.h"
#include "crc.h"
#include "d7a.h"
#include "dbg.h"


uint8_t const modem_data[CUP_DATA_SIZE] = CUP_DATA;
uint8_t const bootloader_data[BOOTLOADER_DATA_SIZE] = BOOTLOADER_DATA;

cup_param_t const cup_modem = {
    .data            = (uint8_t*)modem_data,
    .cfg_fid         = CUP_CFG_FID,
    .code_fid        = CUP_CODE_FID,
    .code_size       = CUP_CODE_SIZE,
    .data_size       = CUP_DATA_SIZE,
    .local_mtu       = CUP_LOCAL_MTU,
    .nb_archives     = CUP_NB_ARCHIVES,
    .signature       = CUP_SIGNATURE,
    .mfg_id          = CUP_MFG_ID,
    .dev_id          = CUP_DEV_ID,
    .hw_id           = CUP_HW_ID,
    .fw_id           = CUP_FW_ID,
    .fw_major        = CUP_FW_MAJOR,
    .fw_minor        = CUP_FW_MINOR,
    .fw_patch        = CUP_FW_PATCH,
    .fw_hash         = CUP_FW_HASH,
    .target_fw_id    = CUP_TARGET_FW_ID,
    .target_fw_major = CUP_TARGET_FW_MAJOR,
    .target_fw_minor = CUP_TARGET_FW_MINOR,
};

cup_param_t const cup_bootloader = {
    .data            = (uint8_t*)bootloader_data,
    .cfg_fid         = BOOTLOADER_CFG_FID,
    .code_fid        = BOOTLOADER_CODE_FID,
    .code_size       = BOOTLOADER_CODE_SIZE,
    .data_size       = BOOTLOADER_DATA_SIZE,
    .local_mtu       = BOOTLOADER_LOCAL_MTU,
    .nb_archives     = BOOTLOADER_NB_ARCHIVES,
    .signature       = BOOTLOADER_SIGNATURE,
    .mfg_id          = BOOTLOADER_MFG_ID,
    .dev_id          = BOOTLOADER_DEV_ID,
    .hw_id           = BOOTLOADER_HW_ID,
    .fw_id           = BOOTLOADER_FW_ID,
    .fw_major        = BOOTLOADER_FW_MAJOR,
    .fw_minor        = BOOTLOADER_FW_MINOR,
    .fw_patch        = BOOTLOADER_FW_PATCH,
    .fw_hash         = BOOTLOADER_FW_HASH,
    .target_fw_id    = BOOTLOADER_TARGET_FW_ID,
    .target_fw_major = BOOTLOADER_TARGET_FW_MAJOR,
    .target_fw_minor = BOOTLOADER_TARGET_FW_MINOR,
};

void cup_start_update(uint32_t offset, bool bootloader)
{
    cup_cfg_t cfg = {
        .cmd = 0x10AD,
        .arch_nb = 20,
    };
    
    cup_param_t* cup;
        
    uint32_t fof = 0;
    uint8_t percent = 0;
    uint8_t percent_old = 255;
    Timer tim;
    int32_t rem;
    
    if (bootloader)
    {
        PRINT("Uploading Bootloader\r\n");
        cup = (cup_param_t*)&cup_bootloader;
    }
    else
    {
        PRINT("Uploading New version\r\n");
        cup = (cup_param_t*)&cup_modem;
    }

    rem = cup->data_size;
    
    // Start CUP
    D7A_WRITE((uint8_t*)&cfg, cup->cfg_fid, 0, 4, root_key);
    
    // Upload file
    PRINT("Uploading %d bytes to CUP file. (offset %d)\r\n", cup->data_size, offset);
    
    tim.start();
    
    while (rem > 0)
    {
        int32_t chunk = (rem > cup->local_mtu)? cup->local_mtu : rem;
        D7A_WRITE(&(cup->data[fof]), cup->code_fid, fof + offset, chunk, NULL);
        rem -= chunk;
        fof += chunk;
        
        percent = (100*fof)/cup->data_size;
        if (percent != percent_old)
        {
            PRINT("UPLOADING CUP FILE %3d percent\r\n", percent);
            percent_old = percent;
        }
        
        // Wait to avoid COM faillure
        Thread::wait(1);
    }
    
    float time_s = tim.read();
    PRINT("CUP: %d bytes written in %.2f sec (%.2f kB/s)\r\n", cup->data_size, time_s, (cup->data_size/time_s)/1024.0);
    
    // Force PFLASH-cache flushing
    D7A_FLUSH(cup->code_fid, root_key);
        
    // Send Upgrade command
    cfg.cmd = 0xC0D5;
    cfg.arch_nb = cup->nb_archives;
    cfg.src_offset = offset;
    cfg.signature = cup->signature;
        
    D7A_WRITE((uint8_t*)&cfg, cup->cfg_fid, 0, 12, root_key);
    
    PRINT("Waiting self reboot...\r\n");
    
    d7a_wait_ready();
    
    /*
    D7A_READ(&cfg, cup->cfg_fid, 0, 2, root_key);
    
    if (cfg.cmd)
    {
        PRINT("/!\\ CUP Error 0x%04X /!\\\r\n", cfg.cmd);
    }
    else
    */
    {
        PRINT("CUP OK\r\nResetting...\r\n\r\n");
        FLUSH();
        NVIC_SystemReset();
    }
}