Skip to content

Commit

Permalink
save conf. on sd card in survive mode
Browse files Browse the repository at this point in the history
  • Loading branch information
mvladic committed Jan 20, 2021
1 parent f508d8c commit 390f41e
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 67 deletions.
4 changes: 2 additions & 2 deletions src/eez/firmware.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,14 +111,14 @@ void boot() {
psu::rtc::init();
psu::datetime::init();

psu::sd_card::init();

mcu::eeprom::init();
mcu::eeprom::test();

bp3c::eeprom::init();
bp3c::eeprom::test();

psu::sd_card::init();

bp3c::io_exp::init();

psu::ontime::g_mcuCounter.init();
Expand Down
127 changes: 63 additions & 64 deletions src/eez/modules/mcu/eeprom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,20 @@

#include <memory.h>

#include <eez/system.h>
#include <eez/modules/psu/psu.h>
#include <eez/modules/mcu/eeprom.h>

#define USE_EEPROM (defined(EEZ_PLATFORM_STM32) && !CONF_SURVIVE_MODE)

#if defined(EEZ_PLATFORM_STM32)
#include <i2c.h>
#endif

#if defined(EEZ_PLATFORM_SIMULATOR)
#include <stdio.h>
#if !USE_EEPROM
#include <eez/libs/sd_fat/sd_fat.h>
#endif

#include <eez/system.h>
#include <eez/modules/psu/psu.h>
#include <eez/modules/mcu/eeprom.h>

#include <scpi/scpi.h>

namespace eez {
Expand All @@ -41,7 +43,6 @@ namespace eeprom {
// I2C-Compatible (2-Wire) Serial EEPROM
// 256-Kbit (32,768 x 8)
// http://ww1.microchip.com/downloads/en/devicedoc/atmel-8568-seeprom-at24c256c-datasheet.pdf

static const uint16_t EEPROM_ADDRESS = 0b10100000;
#endif

Expand All @@ -50,12 +51,11 @@ TestResult g_testResult = TEST_FAILED;
////////////////////////////////////////////////////////////////////////////////

#if defined(EEZ_PLATFORM_STM32)
const int MAX_READ_CHUNK_SIZE = 16;
#endif

bool read(uint8_t *buffer, uint16_t bufferSize, uint16_t address) {
const int MAX_READ_CHUNK_SIZE = 16;
const int MAX_WRITE_CHUNK_SIZE = 16;

#if defined(EEZ_PLATFORM_STM32)
bool readFromEEPROM(uint8_t *buffer, uint16_t bufferSize, uint16_t address) {
for (uint16_t i = 0; i < bufferSize; i += MAX_READ_CHUNK_SIZE) {
uint16_t chunkAddress = address + i;

Expand All @@ -82,38 +82,9 @@ bool read(uint8_t *buffer, uint16_t bufferSize, uint16_t address) {
}

return true;
#endif

#if defined(EEZ_PLATFORM_SIMULATOR)
char *file_path = getConfFilePath("EEPROM.state");
FILE *fp = fopen(file_path, "r+b");
if (fp == NULL) {
fp = fopen(file_path, "w+b");
}
if (fp == NULL) {
return false;
}

fseek(fp, address, SEEK_SET);
size_t readBytes = fread(buffer, 1, bufferSize, fp);
fclose(fp);

if (readBytes < bufferSize) {
memset(buffer + readBytes, 0xFF, bufferSize - readBytes);
}

return true;
#endif

}

#if defined(EEZ_PLATFORM_STM32)
const int MAX_WRITE_CHUNK_SIZE = 16;
#endif

bool write(const uint8_t *buffer, uint16_t bufferSize, uint16_t address) {

#if defined(EEZ_PLATFORM_STM32)
bool writeToEEPROM(const uint8_t *buffer, uint16_t bufferSize, uint16_t address) {
for (uint16_t i = 0; i < bufferSize; i += MAX_WRITE_CHUNK_SIZE) {
uint16_t chunkAddress = address + i;

Expand Down Expand Up @@ -159,43 +130,71 @@ bool write(const uint8_t *buffer, uint16_t bufferSize, uint16_t address) {
}

return true;
}

#endif

#if defined(EEZ_PLATFORM_SIMULATOR)
char *file_path = getConfFilePath("EEPROM.state");
FILE *fp = fopen(file_path, "r+b");
if (fp == NULL) {
fp = fopen(file_path, "w+b");
}
if (fp == NULL) {
#if !USE_EEPROM
const char *EEPROM_FILE_PATH = "/EEPROM.state";
#endif

bool read(uint8_t *buffer, uint16_t bufferSize, uint16_t address) {
#if USE_EEPROM
return readFromEEPROM(buffer, bufferSize, address);
#else
File file;
if (!file.open(EEPROM_FILE_PATH, FILE_READ)) {
return false;
}

fseek(fp, address, SEEK_SET);
fwrite(buffer, 1, bufferSize, fp);
fclose(fp);

file.seek(address);
size_t readBytes = file.read(buffer, bufferSize);
if (readBytes < bufferSize) {
memset(buffer + readBytes, 0xFF, bufferSize - readBytes);
}
file.close();
return true;
#endif
}

bool write(const uint8_t *buffer, uint16_t bufferSize, uint16_t address) {
#if USE_EEPROM
return writeToEEPROM(buffer, bufferSize, address);
#else
File file;
if (!file.open(EEPROM_FILE_PATH, FILE_OPEN_ALWAYS | FILE_WRITE)) {
return false;
}
file.seek(address);
file.write(buffer, bufferSize);
file.close();
return true;
#endif
}

void init() {
#if !USE_EEPROM
File file;
if (!file.open(EEPROM_FILE_PATH, FILE_READ)) {
if (file.open(EEPROM_FILE_PATH, FILE_CREATE_ALWAYS | FILE_WRITE)) {
#if defined(EEZ_PLATFORM_STM32)
uint8_t bufferTemp[MAX_WRITE_CHUNK_SIZE];
for (size_t address = 0; address < EEPROM_SIZE; address += MAX_WRITE_CHUNK_SIZE) {
readFromEEPROM(bufferTemp, MAX_WRITE_CHUNK_SIZE, address);
file.write(bufferTemp, MAX_WRITE_CHUNK_SIZE);
WATCHDOG_RESET(WATCHDOG_LONG_OPERATION);
}
#endif
file.close();
}
}
#endif

g_testResult = TEST_OK;
}

bool test() {
#if OPTION_EXT_EEPROM
// TODO add test
g_testResult = TEST_OK;
#else
g_testResult = TEST_SKIPPED;
#endif

if (g_testResult == TEST_FAILED) {
generateError(SCPI_ERROR_MCU_EEPROM_TEST_FAILED);
}

return g_testResult != TEST_FAILED;
return g_testResult == TEST_OK;
}

void resetAllExceptOnTimeCounters() {
Expand Down
2 changes: 1 addition & 1 deletion src/third_party/stm32_cubeide/bb3.launch
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
<stringAttribute key="com.st.stm32cube.ide.mcu.debug.stlink.max_halt_delay" value="2"/>
<stringAttribute key="com.st.stm32cube.ide.mcu.debug.stlink.reset_strategy" value="no_reset"/>
<booleanAttribute key="com.st.stm32cube.ide.mcu.debug.stlink.stlink_check_serial_number" value="true"/>
<stringAttribute key="com.st.stm32cube.ide.mcu.debug.stlink.stlink_txt_serial_number" value="066EFF535353897167061432"/>
<stringAttribute key="com.st.stm32cube.ide.mcu.debug.stlink.stlink_txt_serial_number" value="066CFF485754727567022108"/>
<stringAttribute key="com.st.stm32cube.ide.mcu.debug.stlink.watchdog_config" value="none"/>
<stringAttribute key="com.st.stm32cube.ide.mcu.debug.stlinkrestart_configurations" value="{&quot;fItems&quot;:[{&quot;fDisplayName&quot;:&quot;Reset&quot;,&quot;fIsSuppressible&quot;:false,&quot;fResetAttribute&quot;:&quot;Reset&quot;,&quot;fResetStrategies&quot;:[{&quot;fDisplayName&quot;:&quot;Reset&quot;,&quot;fLaunchAttribute&quot;:&quot;monitor reset&quot;,&quot;fGdbCommands&quot;:[&quot;monitor reset&quot;],&quot;fCmdOptions&quot;:[]},{&quot;fDisplayName&quot;:&quot;None&quot;,&quot;fLaunchAttribute&quot;:&quot;no_reset&quot;,&quot;fGdbCommands&quot;:[],&quot;fCmdOptions&quot;:[]}],&quot;fGdbCommandGroup&quot;:{&quot;name&quot;:&quot;Additional commands&quot;,&quot;commands&quot;:[]}}]}"/>
<booleanAttribute key="com.st.stm32cube.ide.mcu.debug.swv.swv_wait_for_sync" value="true"/>
Expand Down

0 comments on commit 390f41e

Please sign in to comment.