Simple utility functions for backup and restore of external flash memory content

This commit is contained in:
Silvano Seva 2022-03-22 13:39:08 +01:00
parent 19c1b8eff9
commit a1c26b2898
2 changed files with 120 additions and 0 deletions

View File

@ -0,0 +1,47 @@
/***************************************************************************
* Copyright (C) 2022 by Federico Amedeo Izzo IU2NUO, *
* Niccolò Izzo IU2KIN *
* Frederik Saraci IU2NRO *
* Silvano Seva IU2KWO *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 3 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, see <http://www.gnu.org/licenses/> *
***************************************************************************/
#ifndef BACKUP_H
#define BACKUP_H
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
/**
* Start a dump of the external flash memory content via xmodem transfer,
* blocking function.
*/
void eflash_dump();
/**
* Start a restore of the external flash memory content via xmodem transfer,
* blocking function.
*/
void eflash_restore();
#ifdef __cplusplus
}
#endif
#endif /* BACKUP_H */

73
openrtx/src/core/backup.c Normal file
View File

@ -0,0 +1,73 @@
/***************************************************************************
* Copyright (C) 2022 by Federico Amedeo Izzo IU2NUO, *
* Niccolò Izzo IU2KIN *
* Frederik Saraci IU2NRO *
* Silvano Seva IU2KWO *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 3 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, see <http://www.gnu.org/licenses/> *
***************************************************************************/
#include <backup.h>
#include <xmodem.h>
#include <string.h>
#include "W25Qx.h"
#if defined(PLATFORM_GD77) || defined(PLATFORM_DM1801)
static const size_t EFLASH_SIZE = 1024*1024; // 1 MB
#else
static const size_t EFLASH_SIZE = 16*1024*1024; // 16 MB
#endif
size_t memAddr = 0;
static int getDataCallback(uint8_t *ptr, size_t size)
{
if((memAddr + size) > EFLASH_SIZE) return -1;
W25Qx_readData(memAddr, ptr, size);
memAddr += size;
return 0;
}
static void writeDataCallback(uint8_t *ptr, size_t size)
{
// Trigger sector erase on each 4kB address boundary
if((memAddr % 0x1000) == 0)
{
W25Qx_eraseSector(memAddr);
}
for(size_t written = 0; written < size; )
{
size_t toWrite = size - written;
if(toWrite > 256) toWrite = 256;
W25Qx_writePage(memAddr, ptr, toWrite);
written += toWrite;
memAddr += toWrite;
ptr += toWrite;
}
}
void eflash_dump()
{
memAddr = 0;
W25Qx_wakeup();
xmodem_sendData(EFLASH_SIZE, getDataCallback);
}
void eflash_restore()
{
memAddr = 0;
W25Qx_wakeup();
xmodem_receiveData(EFLASH_SIZE, writeDataCallback);
}