2018-08-14 01:19:34 -05:00
|
|
|
/**
|
|
|
|
* Marlin 3D Printer Firmware
|
2020-02-03 08:00:57 -06:00
|
|
|
* Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
|
2018-08-14 01:19:34 -05:00
|
|
|
*
|
|
|
|
* Based on Sprinter and grbl.
|
2019-06-27 23:57:50 -05:00
|
|
|
* Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
|
2018-08-14 01:19:34 -05:00
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
#ifdef TARGET_LPC1768
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Emulate EEPROM storage using Flash Memory
|
|
|
|
*
|
|
|
|
* Use a single 32K flash sector to store EEPROM data. To reduce the
|
|
|
|
* number of erase operations a simple "levelling" scheme is used that
|
|
|
|
* maintains a number of EEPROM "slots" within the larger flash sector.
|
|
|
|
* Each slot is used in turn and the entire sector is only erased when all
|
|
|
|
* slots have been used.
|
|
|
|
*
|
|
|
|
* A simple RAM image is used to hold the EEPROM data during I/O operations
|
|
|
|
* and this is flushed to the next available slot when an update is complete.
|
|
|
|
* If RAM usage becomes an issue we could store this image in one of the two
|
|
|
|
* 16Kb I/O buffers (intended to hold DMA USB and Ethernet data, but currently
|
|
|
|
* unused).
|
|
|
|
*/
|
2020-03-16 15:59:05 -05:00
|
|
|
#include "../../inc/MarlinConfig.h"
|
2018-08-14 01:19:34 -05:00
|
|
|
|
2020-03-11 16:41:18 -05:00
|
|
|
#if ENABLED(FLASH_EEPROM_EMULATION)
|
2018-08-14 01:19:34 -05:00
|
|
|
|
2020-04-21 18:51:28 -05:00
|
|
|
#include "../shared/eeprom_api.h"
|
2018-08-14 01:19:34 -05:00
|
|
|
|
|
|
|
extern "C" {
|
2019-11-12 22:16:54 -06:00
|
|
|
#include <lpc17xx_iap.h>
|
2018-08-14 01:19:34 -05:00
|
|
|
}
|
|
|
|
|
2018-08-24 17:34:15 -05:00
|
|
|
#define SECTOR_START(sector) ((sector < 16) ? (sector * 0x1000) : ((sector - 14) * 0x8000))
|
2018-08-14 01:19:34 -05:00
|
|
|
#define EEPROM_SECTOR 29
|
2018-08-14 17:54:12 -05:00
|
|
|
#define EEPROM_SIZE (4096)
|
2018-08-14 01:19:34 -05:00
|
|
|
#define SECTOR_SIZE (32768)
|
|
|
|
#define EEPROM_SLOTS (SECTOR_SIZE/EEPROM_SIZE)
|
2019-02-20 04:35:58 -06:00
|
|
|
#define EEPROM_ERASE (0xFF)
|
2018-08-14 01:19:34 -05:00
|
|
|
#define SLOT_ADDRESS(sector, slot) (((uint8_t *)SECTOR_START(sector)) + slot * EEPROM_SIZE)
|
|
|
|
|
2018-11-22 01:03:32 -06:00
|
|
|
static uint8_t ram_eeprom[EEPROM_SIZE] __attribute__((aligned(4))) = {0};
|
2018-08-14 01:19:34 -05:00
|
|
|
static bool eeprom_dirty = false;
|
|
|
|
static int current_slot = 0;
|
|
|
|
|
2020-04-29 14:46:33 -05:00
|
|
|
size_t PersistentStore::capacity() { return EEPROM_SIZE; }
|
|
|
|
|
2018-08-14 01:19:34 -05:00
|
|
|
bool PersistentStore::access_start() {
|
|
|
|
uint32_t first_nblank_loc, first_nblank_val;
|
|
|
|
IAP_STATUS_CODE status;
|
|
|
|
|
|
|
|
// discover which slot we are currently using.
|
|
|
|
__disable_irq();
|
|
|
|
status = BlankCheckSector(EEPROM_SECTOR, EEPROM_SECTOR, &first_nblank_loc, &first_nblank_val);
|
|
|
|
__enable_irq();
|
2018-10-13 14:05:03 -05:00
|
|
|
|
2018-08-14 01:19:34 -05:00
|
|
|
if (status == CMD_SUCCESS) {
|
|
|
|
// sector is blank so nothing stored yet
|
|
|
|
for (int i = 0; i < EEPROM_SIZE; i++) ram_eeprom[i] = EEPROM_ERASE;
|
|
|
|
current_slot = EEPROM_SLOTS;
|
2019-03-24 19:10:33 -05:00
|
|
|
}
|
|
|
|
else {
|
2018-08-14 01:19:34 -05:00
|
|
|
// current slot is the first non blank one
|
|
|
|
current_slot = first_nblank_loc / EEPROM_SIZE;
|
|
|
|
uint8_t *eeprom_data = SLOT_ADDRESS(EEPROM_SECTOR, current_slot);
|
|
|
|
// load current settings
|
|
|
|
for (int i = 0; i < EEPROM_SIZE; i++) ram_eeprom[i] = eeprom_data[i];
|
|
|
|
}
|
|
|
|
eeprom_dirty = false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PersistentStore::access_finish() {
|
|
|
|
if (eeprom_dirty) {
|
|
|
|
IAP_STATUS_CODE status;
|
|
|
|
if (--current_slot < 0) {
|
|
|
|
// all slots have been used, erase everything and start again
|
|
|
|
__disable_irq();
|
|
|
|
status = EraseSector(EEPROM_SECTOR, EEPROM_SECTOR);
|
|
|
|
__enable_irq();
|
2018-10-13 14:05:03 -05:00
|
|
|
|
2018-08-14 01:19:34 -05:00
|
|
|
current_slot = EEPROM_SLOTS - 1;
|
|
|
|
}
|
2018-10-13 14:05:03 -05:00
|
|
|
|
2018-08-14 01:19:34 -05:00
|
|
|
__disable_irq();
|
|
|
|
status = CopyRAM2Flash(SLOT_ADDRESS(EEPROM_SECTOR, current_slot), ram_eeprom, IAP_WRITE_4096);
|
|
|
|
__enable_irq();
|
2018-10-13 14:05:03 -05:00
|
|
|
|
2018-08-14 01:19:34 -05:00
|
|
|
if (status != CMD_SUCCESS) return false;
|
|
|
|
eeprom_dirty = false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-09-28 23:51:04 -05:00
|
|
|
bool PersistentStore::write_data(int &pos, const uint8_t *value, size_t size, uint16_t *crc) {
|
2018-09-19 12:33:20 -05:00
|
|
|
for (size_t i = 0; i < size; i++) ram_eeprom[pos + i] = value[i];
|
2018-08-14 01:19:34 -05:00
|
|
|
eeprom_dirty = true;
|
|
|
|
crc16(crc, value, size);
|
|
|
|
pos += size;
|
|
|
|
return false; // return true for any error
|
|
|
|
}
|
|
|
|
|
2018-08-14 17:54:12 -05:00
|
|
|
bool PersistentStore::read_data(int &pos, uint8_t* value, size_t size, uint16_t *crc, const bool writing/*=true*/) {
|
2018-08-14 01:19:34 -05:00
|
|
|
const uint8_t * const buff = writing ? &value[0] : &ram_eeprom[pos];
|
2018-09-19 12:33:20 -05:00
|
|
|
if (writing) for (size_t i = 0; i < size; i++) value[i] = ram_eeprom[pos + i];
|
2018-08-14 01:19:34 -05:00
|
|
|
crc16(crc, buff, size);
|
|
|
|
pos += size;
|
|
|
|
return false; // return true for any error
|
|
|
|
}
|
|
|
|
|
2019-02-20 04:35:58 -06:00
|
|
|
#endif // FLASH_EEPROM_EMULATION
|
2018-08-14 01:19:34 -05:00
|
|
|
#endif // TARGET_LPC1768
|