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_