From 009ba0247c8acd876683c21744168e5a4bb4ff78 Mon Sep 17 00:00:00 2001 From: Nils Hasenbanck Date: Tue, 14 Aug 2018 08:32:48 +0200 Subject: [PATCH 1/3] Use the new const functions of the persistentStore api I cleaned up some functions by using the new const functions of the peristentStore api. --- Marlin/src/feature/bedlevel/ubl/ubl_G29.cpp | 5 +---- Marlin/src/module/printcounter.cpp | 19 +++++++------------ 2 files changed, 8 insertions(+), 16 deletions(-) diff --git a/Marlin/src/feature/bedlevel/ubl/ubl_G29.cpp b/Marlin/src/feature/bedlevel/ubl/ubl_G29.cpp index 73a0cfdeb386..9a926fa56d58 100644 --- a/Marlin/src/feature/bedlevel/ubl/ubl_G29.cpp +++ b/Marlin/src/feature/bedlevel/ubl/ubl_G29.cpp @@ -1169,8 +1169,6 @@ */ void unified_bed_leveling::g29_eeprom_dump() { uint8_t cccc; - int kkkk; - uint16_t crc = 0; SERIAL_ECHO_START(); SERIAL_ECHOLNPGM("EEPROM Dump:"); @@ -1180,8 +1178,7 @@ print_hex_word(i); SERIAL_ECHOPGM(": "); for (uint16_t j = 0; j < 16; j++) { - kkkk = i + j; - persistentStore.read_data(kkkk, &cccc, sizeof(uint8_t), &crc); + persistentStore.read_data(i+j, &cccc, sizeof(uint8_t)); print_hex_byte(cccc); SERIAL_ECHO(' '); } diff --git a/Marlin/src/module/printcounter.cpp b/Marlin/src/module/printcounter.cpp index 5f4d226286eb..4d89ca9d2e45 100644 --- a/Marlin/src/module/printcounter.cpp +++ b/Marlin/src/module/printcounter.cpp @@ -75,10 +75,8 @@ void PrintCounter::initStats() { saveStats(); - uint16_t crc = 0; - int a = address; persistentStore.access_start(); - persistentStore.write_data(a, (uint8_t*)0x16, sizeof(uint8_t), &crc); + persistentStore.write_data(address, (uint8_t*)0x16, sizeof(uint8_t)); persistentStore.access_finish(); } @@ -88,15 +86,13 @@ void PrintCounter::loadStats() { #endif // Check if the EEPROM block is initialized - uint16_t crc = 0; - int a = address; - uint8_t value; + uint8_t value = 0; persistentStore.access_start(); - persistentStore.read_data(a, &value, sizeof(uint8_t), &crc); + persistentStore.read_data(address, &value, sizeof(uint8_t)); if (value != 0x16) initStats(); else { - a = address + sizeof(uint8_t); - persistentStore.read_data(a, (uint8_t*)&data, sizeof(printStatistics), &crc); + persistentStore.read_data(address + sizeof(uint8_t), + (uint8_t*)&data, sizeof(printStatistics)); } persistentStore.access_finish(); loaded = true; @@ -111,10 +107,9 @@ void PrintCounter::saveStats() { if (!isLoaded()) return; // Saves the struct to EEPROM - uint16_t crc = 0; - int a = (address + sizeof(uint8_t)); persistentStore.access_start(); - persistentStore.write_data(a, (uint8_t*)&data, sizeof(printStatistics), &crc); + persistentStore.write_data(address + sizeof(uint8_t), + (uint8_t*)&data, sizeof(printStatistics)); persistentStore.access_finish(); } From 44d04702f2210bb57dda8a768a4493d20e57b172 Mon Sep 17 00:00:00 2001 From: Nils Hasenbanck Date: Tue, 14 Aug 2018 11:33:12 +0200 Subject: [PATCH 2/3] Fix invalid memory access We want to save value 0x16 and not write the value of 0x16 into the address. --- Marlin/src/module/printcounter.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Marlin/src/module/printcounter.cpp b/Marlin/src/module/printcounter.cpp index 4d89ca9d2e45..b0bfcb252b21 100644 --- a/Marlin/src/module/printcounter.cpp +++ b/Marlin/src/module/printcounter.cpp @@ -76,7 +76,7 @@ void PrintCounter::initStats() { saveStats(); persistentStore.access_start(); - persistentStore.write_data(address, (uint8_t*)0x16, sizeof(uint8_t)); + persistentStore.write_data(address, 0x16, sizeof(uint8_t)); persistentStore.access_finish(); } From ed5339d933b12428d53f3f50c51e36e2de301b60 Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Tue, 14 Aug 2018 17:18:20 -0500 Subject: [PATCH 3/3] Patch compile error and formatting --- Marlin/src/HAL/shared/persistent_store_api.h | 6 ++++-- Marlin/src/feature/bedlevel/ubl/ubl_G29.cpp | 2 +- Marlin/src/module/printcounter.cpp | 14 ++++++-------- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/Marlin/src/HAL/shared/persistent_store_api.h b/Marlin/src/HAL/shared/persistent_store_api.h index 007a8ed737dd..09db6310e039 100644 --- a/Marlin/src/HAL/shared/persistent_store_api.h +++ b/Marlin/src/HAL/shared/persistent_store_api.h @@ -33,13 +33,15 @@ class PersistentStore { static bool read_data(int &pos, uint8_t* value, size_t size, uint16_t *crc, const bool writing=true); static size_t capacity(); - static inline bool write_data(const int pos, uint8_t* value, const size_t size) { + static inline bool write_data(const int pos, const uint8_t* value, const size_t size=sizeof(uint8_t)) { int data_pos = pos; uint16_t crc = 0; return write_data(data_pos, value, size, &crc); } - static inline bool read_data(const int pos, uint8_t* value, const size_t size) { + static inline bool write_data(const int pos, const uint8_t value) { return write_data(pos, &value); } + + static inline bool read_data(const int pos, uint8_t* value, const size_t size=1) { int data_pos = pos; uint16_t crc = 0; return read_data(data_pos, value, size, &crc); diff --git a/Marlin/src/feature/bedlevel/ubl/ubl_G29.cpp b/Marlin/src/feature/bedlevel/ubl/ubl_G29.cpp index 9a926fa56d58..422513840b5a 100644 --- a/Marlin/src/feature/bedlevel/ubl/ubl_G29.cpp +++ b/Marlin/src/feature/bedlevel/ubl/ubl_G29.cpp @@ -1178,7 +1178,7 @@ print_hex_word(i); SERIAL_ECHOPGM(": "); for (uint16_t j = 0; j < 16; j++) { - persistentStore.read_data(i+j, &cccc, sizeof(uint8_t)); + persistentStore.read_data(i + j, &cccc, sizeof(uint8_t)); print_hex_byte(cccc); SERIAL_ECHO(' '); } diff --git a/Marlin/src/module/printcounter.cpp b/Marlin/src/module/printcounter.cpp index b0bfcb252b21..9bf350c052d0 100644 --- a/Marlin/src/module/printcounter.cpp +++ b/Marlin/src/module/printcounter.cpp @@ -76,7 +76,7 @@ void PrintCounter::initStats() { saveStats(); persistentStore.access_start(); - persistentStore.write_data(address, 0x16, sizeof(uint8_t)); + persistentStore.write_data(address, (uint8_t)0x16); persistentStore.access_finish(); } @@ -89,11 +89,10 @@ void PrintCounter::loadStats() { uint8_t value = 0; persistentStore.access_start(); persistentStore.read_data(address, &value, sizeof(uint8_t)); - if (value != 0x16) initStats(); - else { - persistentStore.read_data(address + sizeof(uint8_t), - (uint8_t*)&data, sizeof(printStatistics)); - } + if (value != 0x16) + initStats(); + else + persistentStore.read_data(address + sizeof(uint8_t), (uint8_t*)&data, sizeof(printStatistics)); persistentStore.access_finish(); loaded = true; } @@ -108,8 +107,7 @@ void PrintCounter::saveStats() { // Saves the struct to EEPROM persistentStore.access_start(); - persistentStore.write_data(address + sizeof(uint8_t), - (uint8_t*)&data, sizeof(printStatistics)); + persistentStore.write_data(address + sizeof(uint8_t), (uint8_t*)&data, sizeof(printStatistics)); persistentStore.access_finish(); }