Program to update the D7A modem's firmware.

Dependencies:   modem_ref_helper DebouncedInterrupt

cup.cpp

Committer:
Jeej
Date:
2017-01-06
Revision:
12:beabd59e0c35
Parent:
9:bd53ca2aece5
Child:
13:c3324b26d473

File content as of revision 12:beabd59e0c35:

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


uint8_t const cup_data[CUP_DATA_SIZE] = CUP_DATA;

void cup_start_update(uint32_t offset)
{
    cup_cfg_t cfg = {
        .cmd = 0x10AD,
        .arch_nb = 20,
    };
    
    uint32_t fof = 0;
    int32_t rem = CUP_DATA_SIZE;
    uint8_t percent = 0;
    uint8_t percent_old = 255;
    Timer tim;
    
    // Put reset pin in input
    //DigitalIn rst(A3);
    
    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)
    {
        D7A_WRITE(&(cup_data[fof]), CUP_CODE_FID, fof + offset, CUP_LOCAL_MTU, NULL);
        rem -= CUP_LOCAL_MTU;
        fof += CUP_LOCAL_MTU;
        
        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();
    }
}