Update PersistentStore api (#11538)

- Clean up the API to use a `static` class instance to adhere to Marlin convention
- Add `const` position data access for read/write
- Add Storage capacity to the interface
This commit is contained in:
Chris Pepper 2018-08-13 23:30:26 +01:00 committed by Scott Lahteine
parent 60f1376798
commit 66d2b48b59
13 changed files with 199 additions and 117 deletions

View file

@ -1,18 +1,15 @@
#ifdef __AVR__
#include "../persistent_store_api.h"
#include "../../inc/MarlinConfig.h"
#if ENABLED(EEPROM_SETTINGS)
namespace HAL {
namespace PersistentStore {
#include "../persistent_store_api.h"
bool access_start() { return true; }
bool access_finish() { return true; }
bool PersistentStore::access_start() { return true; }
bool PersistentStore::access_finish() { return true; }
bool write_data(int &pos, const uint8_t *value, uint16_t size, uint16_t *crc) {
bool PersistentStore::write_data(int &pos, const uint8_t *value, size_t size, uint16_t *crc) {
while (size--) {
uint8_t * const p = (uint8_t * const)pos;
uint8_t v = *value;
@ -33,7 +30,7 @@ bool write_data(int &pos, const uint8_t *value, uint16_t size, uint16_t *crc) {
return false;
}
bool read_data(int &pos, uint8_t* value, uint16_t size, uint16_t *crc, const bool writing/*=true*/) {
bool PersistentStore::read_data(int &pos, uint8_t* value, size_t size, uint16_t *crc, const bool writing/*=true*/) {
do {
uint8_t c = eeprom_read_byte((unsigned char*)pos);
if (writing) *value = c;
@ -44,7 +41,20 @@ bool read_data(int &pos, uint8_t* value, uint16_t size, uint16_t *crc, const boo
return false; // always assume success for AVR's
}
bool PersistentStore::write_data(const int pos, uint8_t* value, size_t size) {
int data_pos = pos;
uint16_t crc = 0;
return write_data(data_pos, value, size, &crc);
}
bool PersistentStore::read_data(const int pos, uint8_t* value, size_t size) {
int data_pos = pos;
uint16_t crc = 0;
return read_data(data_pos, value, size, &crc);
}
const size_t PersistentStore::capacity() {
return E2END + 1;
}
#endif // EEPROM_SETTINGS

View file

@ -8,19 +8,16 @@
extern void eeprom_flush(void);
namespace HAL {
namespace PersistentStore {
bool PersistentStore::access_start() { return true; }
bool access_start() { return true; }
bool access_finish() {
bool PersistentStore::access_finish() {
#if DISABLED(I2C_EEPROM) && DISABLED(SPI_EEPROM)
eeprom_flush();
#endif
return true;
}
bool write_data(int &pos, const uint8_t *value, uint16_t size, uint16_t *crc) {
bool PersistentStore::write_data(int &pos, const uint8_t *value, size_t size, uint16_t *crc) {
while (size--) {
uint8_t * const p = (uint8_t * const)pos;
uint8_t v = *value;
@ -41,7 +38,7 @@ bool write_data(int &pos, const uint8_t *value, uint16_t size, uint16_t *crc) {
return false;
}
bool read_data(int &pos, uint8_t* value, uint16_t size, uint16_t *crc, const bool writing/*=true*/) {
bool PersistentStore::read_data(int &pos, uint8_t* value, size_t size, uint16_t *crc, const bool writing/*=true*/) {
do {
uint8_t c = eeprom_read_byte((unsigned char*)pos);
if (writing) *value = c;
@ -52,8 +49,21 @@ bool read_data(int &pos, uint8_t* value, uint16_t size, uint16_t *crc, const boo
return false;
}
bool PersistentStore::write_data(const int pos, uint8_t* value, size_t size) {
int data_pos = pos;
uint16_t crc = 0;
return write_data(data_pos, value, size, &crc);
}
bool PersistentStore::read_data(const int pos, uint8_t* value, size_t size) {
int data_pos = pos;
uint16_t crc = 0;
return read_data(data_pos, value, size, &crc);
}
const size_t PersistentStore::capacity() {
return E2END + 1;
}
#endif // EEPROM_SETTINGS
#endif // __AVR__
#endif // ARDUINO_ARCH_SAM

View file

@ -43,8 +43,6 @@
#define FALLING 0x03
#define RISING 0x04
#define E2END 0xFFF // EEPROM end address
typedef uint8_t byte;
#define PROGMEM
#define PSTR(v) (v)

View file

@ -27,19 +27,17 @@
#include "../persistent_store_api.h"
#include "chanfs/diskio.h"
#include "chanfs/ff.h"
#include <chanfs/diskio.h>
#include <chanfs/ff.h>
extern uint32_t MSC_Aquire_Lock();
extern uint32_t MSC_Release_Lock();
namespace HAL {
namespace PersistentStore {
FATFS fat_fs;
FIL eeprom_file;
bool eeprom_file_open = false;
bool access_start() {
bool PersistentStore::access_start() {
const char eeprom_erase_value = 0xFF;
MSC_Aquire_Lock();
if (f_mount(&fat_fs, "", 1)) {
@ -53,7 +51,7 @@ bool access_start() {
UINT bytes_written;
FSIZE_t file_size = f_size(&eeprom_file);
f_lseek(&eeprom_file, file_size);
while (file_size <= E2END && res == FR_OK) {
while (file_size < capacity() && res == FR_OK) {
res = f_write(&eeprom_file, &eeprom_erase_value, 1, &bytes_written);
file_size++;
}
@ -61,14 +59,16 @@ bool access_start() {
if (res == FR_OK) {
f_lseek(&eeprom_file, 0);
f_sync(&eeprom_file);
eeprom_file_open = true;
}
return res == FR_OK;
}
bool access_finish() {
bool PersistentStore::access_finish() {
f_close(&eeprom_file);
f_unmount("");
MSC_Release_Lock();
eeprom_file_open = false;
return true;
}
@ -98,7 +98,8 @@ bool access_finish() {
// FR_INVALID_PARAMETER /* (19) Given parameter is invalid */
// } FRESULT;
bool write_data(int &pos, const uint8_t *value, uint16_t size, uint16_t *crc) {
bool PersistentStore::write_data(int &pos, const uint8_t *value, size_t size, uint16_t *crc) {
if(!eeprom_file_open) return true;
FRESULT s;
UINT bytes_written = 0;
@ -128,7 +129,8 @@ bool write_data(int &pos, const uint8_t *value, uint16_t size, uint16_t *crc) {
return (bytes_written != size); // return true for any error
}
bool read_data(int &pos, uint8_t* value, uint16_t size, uint16_t *crc, const bool writing/*=true*/) {
bool PersistentStore::read_data(int &pos, uint8_t* value, size_t size, uint16_t *crc, const bool writing/*=true*/) {
if(!eeprom_file_open) return true;
UINT bytes_read = 0;
FRESULT s;
s = f_lseek(&eeprom_file, pos);
@ -163,8 +165,21 @@ bool read_data(int &pos, uint8_t* value, uint16_t size, uint16_t *crc, const boo
return bytes_read != size; // return true for any error
}
} // PersistentStore
} // HAL
bool PersistentStore::write_data(const int pos, uint8_t* value, size_t size) {
int data_pos = pos;
uint16_t crc = 0;
return write_data(data_pos, value, size, &crc);
}
bool PersistentStore::read_data(const int pos, uint8_t* value, size_t size) {
int data_pos = pos;
uint16_t crc = 0;
return read_data(data_pos, value, size, &crc);
}
const size_t PersistentStore::capacity() {
return 4096; //4KiB of Emulated EEPROM
}
#endif // EEPROM_SETTINGS
#endif // TARGET_LPC1768

View file

@ -39,28 +39,23 @@
#include <flash_stm32.h>
#include <EEPROM.h>
namespace HAL {
namespace PersistentStore {
// Store settings in the last two pages
// Flash pages must be erased before writing, so keep track.
bool firstWrite = false;
uint32_t pageBase = EEPROM_START_ADDRESS;
namespace {
// Store settings in the last two pages
// Flash pages must be erased before writing, so keep track.
bool firstWrite = false;
uint32_t pageBase = EEPROM_START_ADDRESS;
}
bool access_start() {
bool PersistentStore::access_start() {
firstWrite = true;
return true;
}
bool access_finish() {
bool PersistentStore::access_finish() {
FLASH_Lock();
firstWrite = false;
return true;
}
bool write_data(int &pos, const uint8_t *value, uint16_t size, uint16_t *crc) {
bool PersistentStore::write_data(int &pos, const uint8_t *value, size_t size, uint16_t *crc) {
FLASH_Status status;
if (firstWrite) {
@ -95,7 +90,7 @@ bool write_data(int &pos, const uint8_t *value, uint16_t size, uint16_t *crc) {
return false;
}
bool read_data(int &pos, uint8_t* value, uint16_t size, uint16_t *crc, const bool writing/*=true*/) {
bool PersistentStore::read_data(int &pos, uint8_t* value, size_t size, uint16_t *crc, const bool writing/*=true*/) {
for (uint16_t i = 0; i < size; i++) {
byte* accessPoint = (byte*)(pageBase + pos + i);
uint8_t c = *accessPoint;
@ -106,8 +101,22 @@ bool read_data(int &pos, uint8_t* value, uint16_t size, uint16_t *crc, const boo
return false;
}
} // PersistentStore
} // HAL
bool PersistentStore::write_data(const int pos, uint8_t* value, size_t size) {
int data_pos = pos;
uint16_t crc = 0;
return write_data(data_pos, value, size, &crc);
}
bool PersistentStore::read_data(const int pos, uint8_t* value, size_t size) {
int data_pos = pos;
uint16_t crc = 0;
return read_data(data_pos, value, size, &crc);
}
const size_t PersistentStore::capacity() {
return E2END + 1;
}
#endif // EEPROM_SETTINGS && EEPROM FLASH
#endif // __STM32F1__

View file

@ -32,24 +32,14 @@
#if ENABLED(EEPROM_SETTINGS) && DISABLED(FLASH_EEPROM_EMULATION)
#include "../persistent_store_api.h"
//#include "../../core/types.h"
//#include "../../core/language.h"
//#include "../../core/serial.h"
//#include "../../core/utility.h"
#include "../../sd/cardreader.h"
namespace HAL {
namespace PersistentStore {
#define HAL_STM32F1_EEPROM_SIZE 4096
char HAL_STM32F1_eeprom_content[HAL_STM32F1_EEPROM_SIZE];
char eeprom_filename[] = "eeprom.dat";
bool access_start() {
bool PersistentStore::access_start() {
if (!card.cardOK) return false;
int16_t bytes_read = 0;
constexpr char eeprom_zero = 0xFF;
@ -62,7 +52,7 @@ bool access_start() {
return true;
}
bool access_finish() {
bool PersistentStore::access_finish() {
if (!card.cardOK) return false;
card.openFile(eeprom_filename, true);
int16_t bytes_written = card.write(HAL_STM32F1_eeprom_content, HAL_STM32F1_EEPROM_SIZE);
@ -70,7 +60,7 @@ bool access_finish() {
return (bytes_written == HAL_STM32F1_EEPROM_SIZE);
}
bool write_data(int &pos, const uint8_t *value, uint16_t size, uint16_t *crc) {
bool PersistentStore::write_data(int &pos, const uint8_t *value, size_t size, uint16_t *crc) {
for (int i = 0; i < size; i++)
HAL_STM32F1_eeprom_content[pos + i] = value[i];
crc16(crc, value, size);
@ -78,7 +68,7 @@ bool write_data(int &pos, const uint8_t *value, uint16_t size, uint16_t *crc) {
return false;
}
bool read_data(int &pos, uint8_t* value, uint16_t size, uint16_t *crc, const bool writing/*=true*/) {
bool PersistentStore::read_data(int &pos, uint8_t* value, size_t size, uint16_t *crc, const bool writing/*=true*/) {
for (int i = 0; i < size; i++) {
uint8_t c = HAL_STM32F1_eeprom_content[pos + i];
if (writing) value[i] = c;
@ -88,10 +78,22 @@ bool read_data(int &pos, uint8_t* value, uint16_t size, uint16_t *crc, const boo
return false;
}
} // PersistentStore::
} // HAL::
bool PersistentStore::write_data(const int pos, uint8_t* value, size_t size) {
int data_pos = pos;
uint16_t crc = 0;
return write_data(data_pos, value, size, &crc);
}
bool PersistentStore::read_data(const int pos, uint8_t* value, size_t size) {
int data_pos = pos;
uint16_t crc = 0;
return read_data(data_pos, value, size, &crc);
}
const size_t PersistentStore::capacity() {
return HAL_STM32F1_EEPROM_SIZE;
}
#endif // EEPROM_SETTINGS
#endif // __STM32F1__

View file

@ -29,13 +29,10 @@
#if ENABLED(EEPROM_SETTINGS)
namespace HAL {
namespace PersistentStore {
bool PersistentStore::access_start() { return true; }
bool PersistentStore::access_finish() { return true; }
bool access_start() { return true; }
bool access_finish() { return true; }
bool write_data(int &pos, const uint8_t *value, uint16_t size, uint16_t *crc) {
bool PersistentStore::write_data(int &pos, const uint8_t *value, size_t size, uint16_t *crc) {
while (size--) {
uint8_t * const p = (uint8_t * const)pos;
uint8_t v = *value;
@ -56,7 +53,7 @@ bool write_data(int &pos, const uint8_t *value, uint16_t size, uint16_t *crc) {
return false;
}
bool read_data(int &pos, uint8_t* value, uint16_t size, uint16_t *crc, const bool writing) {
bool PersistentStore::read_data(int &pos, uint8_t* value, size_t size, uint16_t *crc, const bool writing) {
do {
uint8_t c = eeprom_read_byte((unsigned char*)pos);
if (writing) *value = c;
@ -67,8 +64,21 @@ bool read_data(int &pos, uint8_t* value, uint16_t size, uint16_t *crc, const boo
return false;
}
} // PersistentStore
} // HAL
bool PersistentStore::write_data(const int pos, uint8_t* value, size_t size) {
int data_pos = pos;
uint16_t crc = 0;
return write_data(data_pos, value, size, &crc);
}
bool PersistentStore::read_data(const int pos, uint8_t* value, size_t size) {
int data_pos = pos;
uint16_t crc = 0;
return read_data(data_pos, value, size, &crc);
}
const size_t PersistentStore::capacity() {
return E2END + 1;
}
#endif // EEPROM_SETTINGS
#endif // STM32F4 || STM32F4xx

View file

@ -24,19 +24,16 @@
#ifdef STM32F7
#include "../persistent_store_api.h"
#include "../../inc/MarlinConfig.h"
#if ENABLED(EEPROM_SETTINGS)
namespace HAL {
namespace PersistentStore {
#include "../persistent_store_api.h"
bool access_start() { return true; }
bool access_finish() { return true; }
bool PersistentStore::access_start() { return true; }
bool PersistentStore::access_finish() { return true; }
bool write_data(int &pos, const uint8_t *value, uint16_t size, uint16_t *crc) {
bool PersistentStore::write_data(int &pos, const uint8_t *value, size_t size, uint16_t *crc) {
while (size--) {
uint8_t * const p = (uint8_t * const)pos;
uint8_t v = *value;
@ -57,7 +54,7 @@ bool write_data(int &pos, const uint8_t *value, uint16_t size, uint16_t *crc) {
return false;
}
bool read_data(int &pos, uint8_t* value, uint16_t size, uint16_t *crc) {
bool PersistentStore::read_data(int &pos, uint8_t* value, size_t size, uint16_t *crc) {
do {
uint8_t c = eeprom_read_byte((unsigned char*)pos);
*value = c;
@ -68,10 +65,21 @@ bool read_data(int &pos, uint8_t* value, uint16_t size, uint16_t *crc) {
return false;
}
bool PersistentStore::write_data(const int pos, uint8_t* value, size_t size) {
int data_pos = pos;
uint16_t crc = 0;
return write_data(data_pos, value, size, &crc);
}
bool PersistentStore::read_data(const int pos, uint8_t* value, size_t size) {
int data_pos = pos;
uint16_t crc = 0;
return read_data(data_pos, value, size, &crc);
}
const size_t PersistentStore::capacity() {
return E2END + 1;
}
#endif // EEPROM_SETTINGS
#endif // STM32F7

View file

@ -5,14 +5,12 @@
#if ENABLED(EEPROM_SETTINGS)
#include "../persistent_store_api.h"
#include <avr/eeprom.h>
namespace HAL {
namespace PersistentStore {
bool PersistentStore::access_start() { return true; }
bool PersistentStore::access_finish() { return true; }
bool access_start() { return true; }
bool access_finish() { return true; }
bool write_data(int &pos, const uint8_t *value, uint16_t size, uint16_t *crc) {
bool PersistentStore::write_data(int &pos, const uint8_t *value, size_t size, uint16_t *crc) {
while (size--) {
uint8_t * const p = (uint8_t * const)pos;
uint8_t v = *value;
@ -33,7 +31,7 @@ bool write_data(int &pos, const uint8_t *value, uint16_t size, uint16_t *crc) {
return false;
}
bool read_data(int &pos, uint8_t* value, uint16_t size, uint16_t *crc, const bool writing/*=true*/) {
bool PersistentStore::read_data(int &pos, uint8_t* value, size_t size, uint16_t *crc, const bool writing/*=true*/) {
do {
uint8_t c = eeprom_read_byte((unsigned char*)pos);
if (writing) *value = c;
@ -44,8 +42,21 @@ bool read_data(int &pos, uint8_t* value, uint16_t size, uint16_t *crc, const boo
return false;
}
} // PersistentStore
} // HAL
bool PersistentStore::write_data(const int pos, uint8_t* value, size_t size) {
int data_pos = pos;
uint16_t crc = 0;
return write_data(data_pos, value, size, &crc);
}
bool PersistentStore::read_data(const int pos, uint8_t* value, size_t size) {
int data_pos = pos;
uint16_t crc = 0;
return read_data(data_pos, value, size, &crc);
}
const size_t PersistentStore::capacity() {
return E2END + 1;
}
#endif // EEPROM_SETTINGS
#endif // __MK64FX512__ || __MK66FX1M0__

View file

@ -4,15 +4,17 @@
#include <stddef.h>
#include <stdint.h>
namespace HAL {
namespace PersistentStore {
class PersistentStore {
public:
static bool access_start();
static bool access_finish();
static bool write_data(int &pos, const uint8_t *value, size_t size, uint16_t *crc);
static bool read_data(int &pos, uint8_t* value, size_t size, uint16_t *crc, const bool writing=true);
static bool write_data(const int pos, uint8_t* value, size_t size);
static bool read_data(const int pos, uint8_t* value, size_t size);
static const size_t capacity();
};
bool access_start();
bool access_finish();
bool write_data(int &pos, const uint8_t *value, uint16_t size, uint16_t *crc);
bool read_data(int &pos, uint8_t* value, uint16_t size, uint16_t *crc, const bool writing=true);
} // PersistentStore
} // HAL
extern PersistentStore persistentStore;
#endif // _PERSISTENT_STORE_H_

View file

@ -1172,7 +1172,7 @@
SERIAL_ECHO_START();
SERIAL_ECHOLNPGM("EEPROM Dump:");
for (uint16_t i = 0; i <= E2END; i += 16) {
for (uint16_t i = 0; i < persistentStore.capacity(); i += 16) {
if (!(i & 0x3)) idle();
print_hex_word(i);
SERIAL_ECHOPGM(": ");

View file

@ -345,14 +345,15 @@ void MarlinSettings::postprocess() {
#if ENABLED(EEPROM_SETTINGS)
#include "../HAL/persistent_store_api.h"
PersistentStore persistentStore;
#define DUMMY_PID_VALUE 3000.0f
#define EEPROM_START() int eeprom_index = EEPROM_OFFSET; HAL::PersistentStore::access_start()
#define EEPROM_FINISH() HAL::PersistentStore::access_finish()
#define EEPROM_START() int eeprom_index = EEPROM_OFFSET; persistentStore.access_start()
#define EEPROM_FINISH() persistentStore.access_finish()
#define EEPROM_SKIP(VAR) eeprom_index += sizeof(VAR)
#define EEPROM_WRITE(VAR) HAL::PersistentStore::write_data(eeprom_index, (uint8_t*)&VAR, sizeof(VAR), &working_crc)
#define EEPROM_READ(VAR) HAL::PersistentStore::read_data(eeprom_index, (uint8_t*)&VAR, sizeof(VAR), &working_crc, !validating)
#define EEPROM_READ_ALWAYS(VAR) HAL::PersistentStore::read_data(eeprom_index, (uint8_t*)&VAR, sizeof(VAR), &working_crc)
#define EEPROM_WRITE(VAR) persistentStore.write_data(eeprom_index, (uint8_t*)&VAR, sizeof(VAR), &working_crc)
#define EEPROM_READ(VAR) persistentStore.read_data(eeprom_index, (uint8_t*)&VAR, sizeof(VAR), &working_crc, !validating)
#define EEPROM_READ_ALWAYS(VAR) persistentStore.read_data(eeprom_index, (uint8_t*)&VAR, sizeof(VAR), &working_crc)
#define EEPROM_ASSERT(TST,ERR) if (!(TST)) do{ SERIAL_ERROR_START_P(port); SERIAL_ERRORLNPGM_P(port, ERR); eeprom_error = true; }while(0)
#if ENABLED(DEBUG_EEPROM_READWRITE)
@ -1607,6 +1608,10 @@ void MarlinSettings::postprocess() {
}
#endif
const uint16_t MarlinSettings::meshes_end = persistentStore.capacity() - 129; // 128 (+1 because of the change to capacity rather than last valid address)
// is a placeholder for the size of the MAT; the MAT will always
// live at the very end of the eeprom
uint16_t MarlinSettings::meshes_start_index() {
return (datasize() + EEPROM_OFFSET + 32) & 0xFFF8; // Pad the end of configuration data so it can float up
// or down a little bit without disrupting the mesh data
@ -1627,7 +1632,7 @@ void MarlinSettings::postprocess() {
if (!WITHIN(slot, 0, a - 1)) {
#if ENABLED(EEPROM_CHITCHAT)
ubl_invalid_slot(a);
SERIAL_PROTOCOLPAIR("E2END=", E2END);
SERIAL_PROTOCOLPAIR("E2END=", persistentStore.capacity() - 1);
SERIAL_PROTOCOLPAIR(" meshes_end=", meshes_end);
SERIAL_PROTOCOLLNPAIR(" slot=", slot);
SERIAL_EOL();
@ -1638,9 +1643,9 @@ void MarlinSettings::postprocess() {
int pos = mesh_slot_offset(slot);
uint16_t crc = 0;
HAL::PersistentStore::access_start();
const bool status = HAL::PersistentStore::write_data(pos, (uint8_t *)&ubl.z_values, sizeof(ubl.z_values), &crc);
HAL::PersistentStore::access_finish();
persistentStore.access_start();
const bool status = persistentStore.write_data(pos, (uint8_t *)&ubl.z_values, sizeof(ubl.z_values), &crc);
persistentStore.access_finish();
if (status)
SERIAL_PROTOCOLPGM("?Unable to save mesh data.\n");
@ -1676,9 +1681,9 @@ void MarlinSettings::postprocess() {
uint16_t crc = 0;
uint8_t * const dest = into ? (uint8_t*)into : (uint8_t*)&ubl.z_values;
HAL::PersistentStore::access_start();
const uint16_t status = HAL::PersistentStore::read_data(pos, dest, sizeof(ubl.z_values), &crc);
HAL::PersistentStore::access_finish();
persistentStore.access_start();
const uint16_t status = persistentStore.read_data(pos, dest, sizeof(ubl.z_values), &crc);
persistentStore.access_finish();
if (status)
SERIAL_PROTOCOLPGM("?Unable to load mesh data.\n");

View file

@ -24,6 +24,9 @@
#define CONFIGURATION_STORE_H
#include "../inc/MarlinConfig.h"
#if ENABLED(EEPROM_SETTINGS)
#include "../HAL/persistent_store_api.h"
#endif
#define ADD_PORT_ARG ENABLED(EEPROM_CHITCHAT) && NUM_SERIAL > 1
@ -98,9 +101,8 @@ class MarlinSettings {
#if ENABLED(AUTO_BED_LEVELING_UBL) // Eventually make these available if any leveling system
// That can store is enabled
static constexpr uint16_t meshes_end = E2END - 128; // 128 is a placeholder for the size of the MAT; the MAT will always
static const uint16_t meshes_end; // 128 is a placeholder for the size of the MAT; the MAT will always
// live at the very end of the eeprom
#endif
static bool _load(PORTINIT_SOLO);