2019-02-23 02:09:10 +01:00
|
|
|
/**
|
|
|
|
* Marlin 3D Printer Firmware
|
2019-06-28 06:57:50 +02:00
|
|
|
* Copyright (c) 2019 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
|
2019-02-23 02:09:10 +01:00
|
|
|
*
|
|
|
|
* Based on Sprinter and grbl.
|
2019-06-28 06:57:50 +02:00
|
|
|
* Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
|
2019-02-23 02:09:10 +01: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 __PLAT_LINUX__
|
|
|
|
|
|
|
|
#include "../../inc/MarlinConfig.h"
|
|
|
|
|
|
|
|
#if ENABLED(EEPROM_SETTINGS)
|
|
|
|
|
|
|
|
#include "../shared/persistent_store_api.h"
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2019-07-10 04:26:35 +02:00
|
|
|
#define LINUX_EEPROM_SIZE (E2END + 1)
|
|
|
|
uint8_t buffer[LINUX_EEPROM_SIZE];
|
2019-02-23 02:09:10 +01:00
|
|
|
char filename[] = "eeprom.dat";
|
|
|
|
|
|
|
|
bool PersistentStore::access_start() {
|
|
|
|
const char eeprom_erase_value = 0xFF;
|
|
|
|
FILE * eeprom_file = fopen(filename, "rb");
|
2019-05-09 18:45:55 +02:00
|
|
|
if (eeprom_file == nullptr) return false;
|
2019-02-23 02:09:10 +01:00
|
|
|
|
|
|
|
fseek(eeprom_file, 0L, SEEK_END);
|
|
|
|
std::size_t file_size = ftell(eeprom_file);
|
|
|
|
|
2019-07-10 04:26:35 +02:00
|
|
|
if (file_size < LINUX_EEPROM_SIZE) {
|
|
|
|
memset(buffer + file_size, eeprom_erase_value, LINUX_EEPROM_SIZE - file_size);
|
2019-02-23 02:09:10 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
fseek(eeprom_file, 0L, SEEK_SET);
|
|
|
|
fread(buffer, sizeof(uint8_t), sizeof(buffer), eeprom_file);
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(eeprom_file);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PersistentStore::access_finish() {
|
|
|
|
FILE * eeprom_file = fopen(filename, "wb");
|
2019-05-09 18:45:55 +02:00
|
|
|
if (eeprom_file == nullptr) return false;
|
2019-02-23 02:09:10 +01:00
|
|
|
fwrite(buffer, sizeof(uint8_t), sizeof(buffer), eeprom_file);
|
|
|
|
fclose(eeprom_file);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-08-02 17:32:51 +02:00
|
|
|
bool PersistentStore::write_data(int &pos, const uint8_t *value, size_t size, uint16_t *crc) {
|
2019-02-23 02:09:10 +01:00
|
|
|
std::size_t bytes_written = 0;
|
|
|
|
|
|
|
|
for (std::size_t i = 0; i < size; i++) {
|
|
|
|
buffer[pos+i] = value[i];
|
|
|
|
bytes_written ++;
|
|
|
|
}
|
|
|
|
|
|
|
|
crc16(crc, value, size);
|
|
|
|
pos = pos + size;
|
|
|
|
return (bytes_written != size); // return true for any error
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PersistentStore::read_data(int &pos, uint8_t* value, const size_t size, uint16_t *crc, const bool writing/*=true*/) {
|
|
|
|
std::size_t bytes_read = 0;
|
|
|
|
if (writing) {
|
|
|
|
for (std::size_t i = 0; i < size; i++) {
|
|
|
|
value[i] = buffer[pos+i];
|
|
|
|
bytes_read ++;
|
|
|
|
}
|
|
|
|
crc16(crc, value, size);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
uint8_t temp[size];
|
|
|
|
for (std::size_t i = 0; i < size; i++) {
|
|
|
|
temp[i] = buffer[pos+i];
|
|
|
|
bytes_read ++;
|
|
|
|
}
|
|
|
|
crc16(crc, temp, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
pos = pos + size;
|
|
|
|
return bytes_read != size; // return true for any error
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t PersistentStore::capacity() { return 4096; } // 4KiB of Emulated EEPROM
|
|
|
|
|
|
|
|
#endif // EEPROM_SETTINGS
|
|
|
|
#endif // __PLAT_LINUX__
|