diff --git a/misc/stm32f1_test_project/Makefile b/misc/stm32f1_test_project/Makefile index 77f59cb64b..408e7a778e 100644 --- a/misc/stm32f1_test_project/Makefile +++ b/misc/stm32f1_test_project/Makefile @@ -116,6 +116,7 @@ include $(CHIBIOS)/os/common/ports/ARMCMx/compilers/GCC/mk/port_v7m.mk # Auto-build files in ./source recursively. include $(CHIBIOS)/tools/mk/autobuild.mk # Other files (optional). +include $(CHIBIOS)/os/hal/lib/complex/mfs/hal_mfs.mk include $(CHIBIOS)/os/hal/lib/streams/streams.mk # Define linker script file here @@ -131,6 +132,7 @@ CPPSRC = $(ALLCPPSRC) \ main.cpp \ spi.cpp \ can.cpp \ + persistence.cpp \ uart.cpp # List ASM source files here. diff --git a/misc/stm32f1_test_project/STM32F103xB.ld b/misc/stm32f1_test_project/STM32F103xB.ld index 02f98166c4..920036334f 100644 --- a/misc/stm32f1_test_project/STM32F103xB.ld +++ b/misc/stm32f1_test_project/STM32F103xB.ld @@ -19,7 +19,7 @@ */ MEMORY { - flash0 (rx) : org = 0x08000000, len = 128k + flash0 (rx) : org = 0x08000000, len = 60k flash1 (rx) : org = 0x00000000, len = 0 flash2 (rx) : org = 0x00000000, len = 0 flash3 (rx) : org = 0x00000000, len = 0 diff --git a/misc/stm32f1_test_project/halconf.h b/misc/stm32f1_test_project/halconf.h index 3fe11e23ef..9e41a0034e 100644 --- a/misc/stm32f1_test_project/halconf.h +++ b/misc/stm32f1_test_project/halconf.h @@ -72,7 +72,7 @@ * @brief Enables the EFlash subsystem. */ #if !defined(HAL_USE_EFL) || defined(__DOXYGEN__) -#define HAL_USE_EFL FALSE +#define HAL_USE_EFL TRUE #endif /** diff --git a/misc/stm32f1_test_project/main.cpp b/misc/stm32f1_test_project/main.cpp index aaf759f042..27df9c016f 100644 --- a/misc/stm32f1_test_project/main.cpp +++ b/misc/stm32f1_test_project/main.cpp @@ -19,6 +19,7 @@ #include "uart.h" #include "can.h" #include "spi.h" +#include "persistence.h" #define BL_PORT GPIOC #define BL_PIN 13 @@ -62,6 +63,7 @@ int main(void) { */ chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL); + InitConfiguration(); InitUart(); InitCan(); InitSpi(); diff --git a/misc/stm32f1_test_project/persistence.cpp b/misc/stm32f1_test_project/persistence.cpp new file mode 100644 index 0000000000..5ddd0d6940 --- /dev/null +++ b/misc/stm32f1_test_project/persistence.cpp @@ -0,0 +1,84 @@ +#include "hal.h" +#include "persistence.h" + +static const MFSConfig mfscfg_1k = { + .flashp = (BaseFlash *)&EFLD1, + .erased = 0xFFFFFFFFU, + /* 128K flash device with 1K pages + * use last 8 pages for settings + * one bank is 4K */ + .bank_size = 4096U, + .bank0_start = 120U, + .bank0_sectors = 4U, + .bank1_start = 124U, + .bank1_sectors = 4U +#ifdef STM32F103xE + /* 256K flash device with 2K pages + * use last 8 pages for settings + * one bank is 8K */ + .bank_size = 8096U, + .bank0_start = 120U, + .bank0_sectors = 4U, + .bank1_start = 124U, + .bank1_sectors = 4U +#endif +}; + +static const MFSConfig mfscfg_2k = { + .flashp = (BaseFlash *)&EFLD1, + .erased = 0xFFFFFFFFU, + /* 256K flash device with 2K pages + * use last 8 pages for settings + * one bank is 8K */ + .bank_size = 8096U, + .bank0_start = 120U, + .bank0_sectors = 4U, + .bank1_start = 124U, + .bank1_sectors = 4U +}; + + +static MFSDriver mfs1; +TestConfiguration configuration; + +static uint8_t *GetConfigurationPtr() { + return (uint8_t *)&configuration; +} + +static size_t GetConfigurationSize() { + return sizeof(TestConfiguration); +} + +int InitConfiguration() { + size_t size = GetConfigurationSize(); + + /* Starting EFL driver.*/ + eflStart(&EFLD1, NULL); + + mfsObjectInit(&mfs1); + mfs_error_t err; + +#define FLASH_SIZE_IN_K_ADDRESS 0x1FFFF7E0 + int flashSize = (*(uint16_t*)FLASH_SIZE_IN_K_ADDRESS); + if (flashSize > 128) { + err = mfsStart(&mfs1, &mfscfg_1k); + } else { + err = mfsStart(&mfs1, &mfscfg_2k); + } + if (err != MFS_NO_ERROR) { + return -1; + } + + err = mfsReadRecord(&mfs1, MFS_CONFIGURATION_RECORD_ID, &size, GetConfigurationPtr()); + if ((err != MFS_NO_ERROR) || (size != GetConfigurationSize() || !configuration.IsValid())) { + /* load defaults */ + configuration.resetToDefaults(); + } + + return 0; +} + +void pokeConfiguration() { + configuration.version++; +// mfsWriteRecord(&mfs1, MFS_CONFIGURATION_RECORD_ID, GetConfigurationSize(), GetConfigurationPtr()); +} \ No newline at end of file diff --git a/misc/stm32f1_test_project/persistence.h b/misc/stm32f1_test_project/persistence.h new file mode 100644 index 0000000000..08ff80a8ab --- /dev/null +++ b/misc/stm32f1_test_project/persistence.h @@ -0,0 +1,24 @@ +#pragma once + +#include "hal_mfs.h" + +#define PERSISTENCE_VERSION 4 + +#define MFS_CONFIGURATION_RECORD_ID 1 + +struct TestConfiguration { + void resetToDefaults() { + version = PERSISTENCE_VERSION; + updateCounter = 20; + } + bool IsValid() const + { + return version == PERSISTENCE_VERSION; + } + + int version; + int updateCounter; +}; + +int InitConfiguration(); +void pokeConfiguration(); diff --git a/misc/stm32f1_test_project/uart.cpp b/misc/stm32f1_test_project/uart.cpp index f9884a81a4..2d64534c3d 100644 --- a/misc/stm32f1_test_project/uart.cpp +++ b/misc/stm32f1_test_project/uart.cpp @@ -3,6 +3,7 @@ #include "chprintf.h" #include "uart.h" +#include "persistence.h" static const UARTConfig uartCfg = { @@ -26,16 +27,20 @@ static const UARTConfig uartCfg = static char printBuffer[200]; +extern TestConfiguration configuration; + static THD_WORKING_AREA(waUartThread, 256); static void UartThread(void*) { while(true) { - size_t writeCount = chsnprintf(printBuffer, 200, "%d.%03d\t%d\t%d\r\n", 0, 0, 0, 100); + size_t writeCount = chsnprintf(printBuffer, 200, "%d.%03d\t%d\t%d\r\n", 0, 0, configuration.version, 100); uartStartSend(&UARTD1, writeCount, printBuffer); - chThdSleepMilliseconds(20); + pokeConfiguration(); + + chThdSleepMilliseconds(200); } }