2017-08-01 18:19:23 +02:00
|
|
|
#ifdef TARGET_LPC1768
|
2017-06-17 23:19:42 +02:00
|
|
|
|
2017-09-06 13:28:32 +02:00
|
|
|
#include "../../inc/MarlinConfig.h"
|
2017-06-17 23:19:42 +02:00
|
|
|
|
2017-09-06 13:28:32 +02:00
|
|
|
#if ENABLED(EEPROM_SETTINGS)
|
2017-06-17 23:19:42 +02:00
|
|
|
|
2017-09-06 13:28:32 +02:00
|
|
|
#include "../persistent_store_api.h"
|
2017-08-01 18:19:23 +02:00
|
|
|
|
2017-09-06 13:28:32 +02:00
|
|
|
#include "chanfs/diskio.h"
|
|
|
|
#include "chanfs/ff.h"
|
2017-06-17 23:19:42 +02:00
|
|
|
|
2017-10-04 22:40:54 +02:00
|
|
|
extern uint32_t MSC_Aquire_Lock();
|
|
|
|
extern uint32_t MSC_Release_Lock();
|
|
|
|
|
2017-06-17 23:19:42 +02:00
|
|
|
namespace HAL {
|
|
|
|
namespace PersistentStore {
|
|
|
|
|
|
|
|
FATFS fat_fs;
|
|
|
|
FIL eeprom_file;
|
|
|
|
|
|
|
|
bool access_start() {
|
Update LPC persistent store to initialize eeprom.dat with FF
This change initialize any data in eeprom.dat beyond the current file size to FF.
That way if eeprom.dat is deleted and created again, it doesn't take the old values or random ones, but rather starts with FF in all positions as a real brand new or erased eeprom.dat
Currently if you delete eeprom.dat and restart the board, the new file is created in the same sector with the same content, since FAT does not actually delete the data, just marks the sector as free. I tested by deleting the file, and then rebooting the board, and checking the file content.
The change can be tested in the same way, deleting, rebooting the board, and then the new content should be all FF.
If an eeprom file already exist with data on it, but smaller than E2END, it will be padded with FF on first access, so it will not have random or old content appended.
2017-09-28 03:43:43 +02:00
|
|
|
UINT file_size = 0,
|
|
|
|
bytes_written = 0;
|
|
|
|
const char eeprom_zero = 0xFF;
|
2017-10-04 22:40:54 +02:00
|
|
|
MSC_Aquire_Lock();
|
Update LPC persistent store to initialize eeprom.dat with FF
This change initialize any data in eeprom.dat beyond the current file size to FF.
That way if eeprom.dat is deleted and created again, it doesn't take the old values or random ones, but rather starts with FF in all positions as a real brand new or erased eeprom.dat
Currently if you delete eeprom.dat and restart the board, the new file is created in the same sector with the same content, since FAT does not actually delete the data, just marks the sector as free. I tested by deleting the file, and then rebooting the board, and checking the file content.
The change can be tested in the same way, deleting, rebooting the board, and then the new content should be all FF.
If an eeprom file already exist with data on it, but smaller than E2END, it will be padded with FF on first access, so it will not have random or old content appended.
2017-09-28 03:43:43 +02:00
|
|
|
if (f_mount(&fat_fs, "", 1)) {
|
2017-10-04 22:40:54 +02:00
|
|
|
MSC_Release_Lock();
|
|
|
|
return false;
|
|
|
|
}
|
2017-06-17 23:19:42 +02:00
|
|
|
FRESULT res = f_open(&eeprom_file, "eeprom.dat", FA_OPEN_ALWAYS | FA_WRITE | FA_READ);
|
Update LPC persistent store to initialize eeprom.dat with FF
This change initialize any data in eeprom.dat beyond the current file size to FF.
That way if eeprom.dat is deleted and created again, it doesn't take the old values or random ones, but rather starts with FF in all positions as a real brand new or erased eeprom.dat
Currently if you delete eeprom.dat and restart the board, the new file is created in the same sector with the same content, since FAT does not actually delete the data, just marks the sector as free. I tested by deleting the file, and then rebooting the board, and checking the file content.
The change can be tested in the same way, deleting, rebooting the board, and then the new content should be all FF.
If an eeprom file already exist with data on it, but smaller than E2END, it will be padded with FF on first access, so it will not have random or old content appended.
2017-09-28 03:43:43 +02:00
|
|
|
if (res) MSC_Release_Lock();
|
|
|
|
|
|
|
|
if (res == FR_OK) file_size = f_size(&eeprom_file);
|
|
|
|
|
|
|
|
if (res == FR_OK) {
|
|
|
|
f_lseek(&eeprom_file, file_size);
|
|
|
|
while (file_size < E2END && res == FR_OK) {
|
|
|
|
res = f_write(&eeprom_file, &eeprom_zero, 1, &bytes_written);
|
|
|
|
file_size++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (res == FR_OK) {
|
|
|
|
f_lseek(&eeprom_file, 0);
|
|
|
|
f_sync(&eeprom_file);
|
|
|
|
}
|
|
|
|
return res == FR_OK;
|
2017-06-17 23:19:42 +02:00
|
|
|
}
|
|
|
|
|
2017-09-06 13:28:32 +02:00
|
|
|
bool access_finish() {
|
2017-06-17 23:19:42 +02:00
|
|
|
f_close(&eeprom_file);
|
|
|
|
f_unmount("");
|
2017-10-04 22:40:54 +02:00
|
|
|
MSC_Release_Lock();
|
2017-06-17 23:19:42 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool write_data(int &pos, const uint8_t *value, uint16_t size, uint16_t *crc) {
|
|
|
|
UINT bytes_written = 0;
|
|
|
|
f_lseek(&eeprom_file, pos);
|
|
|
|
f_write(&eeprom_file, (void *)value, size, &bytes_written);
|
|
|
|
crc16(crc, value, size);
|
|
|
|
pos = pos + size;
|
|
|
|
return (bytes_written == size);
|
|
|
|
}
|
|
|
|
|
|
|
|
void read_data(int &pos, uint8_t* value, uint16_t size, uint16_t *crc) {
|
|
|
|
UINT bytes_read = 0;
|
|
|
|
f_lseek(&eeprom_file, pos);
|
|
|
|
f_read(&eeprom_file, (void *)value, size, &bytes_read);
|
|
|
|
crc16(crc, value, size);
|
|
|
|
pos = pos + size;
|
|
|
|
}
|
|
|
|
|
2017-09-06 13:28:32 +02:00
|
|
|
} // PersistentStore
|
|
|
|
} // HAL
|
2017-06-17 23:19:42 +02:00
|
|
|
|
|
|
|
#endif // EEPROM_SETTINGS
|
2017-09-06 13:28:32 +02:00
|
|
|
#endif // TARGET_LPC1768
|