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

@ -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