Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: modem_ref_helper DebouncedInterrupt
cup.cpp
- Committer:
- Jeej
- Date:
- 2016-11-10
- Revision:
- 7:5b8648784381
- Parent:
- 5:ac38f09fd179
- Child:
- 9:bd53ca2aece5
File content as of revision 7:5b8648784381:
#include "mbed.h"
#include "cup.h"
#include "bin.h"
#include "crc.h"
#include "d7a.h"
#include "dbg.h"
#define MODEM_FLASH_PAGE_SIZE (256)
#define TU_LOCAL (220)
const uint8_t const cup_data[CUP_DATA_SIZE] = CUP_DATA;
void cup_start_update(uint32_t src_offset, uint32_t max_size)
{
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;
// get archive address
uint32_t addr = *((uint32_t*)&cup_data[4]);
// End address
uint32_t eaddr = addr + CUP_CODE_SIZE;
uint32_t offset = 0;
if (eaddr > src_offset)
{
// Calculate offset if needed
PRINT("/!\\ CUP process will overwrite Archive: 0x%08X + %d > 0x%08X /!\\\r\n", addr, CUP_CODE_SIZE, src_offset);
eaddr = ((eaddr / MODEM_FLASH_PAGE_SIZE) + 1) * MODEM_FLASH_PAGE_SIZE;
offset = eaddr - src_offset;
PRINT("/!\\ CUP Shifting archive storage to 0x%08X (Offset 0x%X) /!\\\r\n", eaddr, offset);
}
max_size -= offset;
int32_t slack = max_size - CUP_DATA_SIZE;
ASSERT(slack >= 0, "Not enough space to store archive! (%d bytes short)\r\n", -slack);
D7A_WRITE((uint8_t*)&cfg, CUP_CFG_FID, 0, 4, root_key);
// Upload file
PRINT("Uploading %d bytes at address 0x%08X (%d bytes slack)\r\n", CUP_DATA_SIZE, src_offset + offset, slack);
tim.start();
while (rem > 0)
{
D7A_WRITE(&(cup_data[fof]), CUP_CODE_FID, fof + offset, TU_LOCAL, NULL);
rem -= TU_LOCAL;
fof += TU_LOCAL;
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();
}
}