diff --git a/firmware/boards/f1_common/f1_port.cpp b/firmware/boards/f1_common/f1_port.cpp new file mode 100644 index 0000000..a6ec047 --- /dev/null +++ b/firmware/boards/f1_common/f1_port.cpp @@ -0,0 +1,87 @@ +#include "port.h" + +#include "wideband_config.h" + +#include "hal.h" +#include "hal_mfs.h" + +// Storage + +static const MFSConfig mfscfg1 = { + .flashp = (BaseFlash *)&EFLD1, + .erased = 0xFFFFFFFFU, + .bank_size = 4096U, + .bank0_start = 120U, + .bank0_sectors = 4U, + .bank1_start = 124U, + .bank1_sectors = 4U +}; + +static MFSDriver mfs1; + +// Settings +static Configuration cfg; +#define MFS_CONFIGURATION_RECORD_ID 1 + +// Configuration defaults +void Configuration::LoadDefaults() +{ + CanIndexOffset = 0; + + /* Finaly */ + Tag = ExpectedTag; +} + +int InitConfiguration() +{ + size_t size = GetConfiguratiuonSize(); + + /* Starting EFL driver.*/ + eflStart(&EFLD1, NULL); + + mfsObjectInit(&mfs1); + + mfsStart(&mfs1, &mfscfg1); + + mfs_error_t err = mfsReadRecord(&mfs1, MFS_CONFIGURATION_RECORD_ID, &size, GetConfiguratiuonPtr()); + if ((err != MFS_NO_ERROR) || (size != GetConfiguratiuonSize() || !cfg.IsValid())) { + /* load defaults */ + cfg.LoadDefaults(); + } + + return 0; +} + +static Configuration c; + +Configuration& GetConfiguration() +{ + return cfg; +} + +void SetConfiguration(const Configuration& newConfig) +{ + cfg = newConfig; + + SaveConfiguration(); +} + +/* TS stuff */ +void SaveConfiguration() { + /* TODO: handle error */ + mfsWriteRecord(&mfs1, MFS_CONFIGURATION_RECORD_ID, GetConfiguratiuonSize(), GetConfiguratiuonPtr()); +} + +uint8_t *GetConfiguratiuonPtr() +{ + return (uint8_t *)&cfg; +} + +size_t GetConfiguratiuonSize() +{ + return sizeof(cfg); +} + +const char *getTsSignature() { + return TS_SIGNATURE; +} diff --git a/firmware/boards/f1_dual/board.mk b/firmware/boards/f1_dual/board.mk index add0892..5757664 100644 --- a/firmware/boards/f1_dual/board.mk +++ b/firmware/boards/f1_dual/board.mk @@ -2,6 +2,8 @@ USE_BOOTLOADER = no MCU = cortex-m3 +ALLCPPSRC += $(BOARDDIR)/../f1_common/f1_port.cpp + include $(CHIBIOS)/os/common/startup/ARMCMx/compilers/GCC/mk/startup_stm32f1xx.mk include $(CHIBIOS)/os/hal/ports/STM32/STM32F1xx/platform.mk include $(CHIBIOS)/os/common/ports/ARMv7-M/compilers/GCC/mk/port.mk diff --git a/firmware/boards/f1_dual/halconf.h b/firmware/boards/f1_dual/halconf.h index ad71475..0bf3f31 100644 --- a/firmware/boards/f1_dual/halconf.h +++ b/firmware/boards/f1_dual/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/firmware/boards/f1_dual/port.cpp b/firmware/boards/f1_dual/port.cpp index 51262b8..98bc9ea 100644 --- a/firmware/boards/f1_dual/port.cpp +++ b/firmware/boards/f1_dual/port.cpp @@ -112,41 +112,3 @@ AnalogResult AnalogSample() .VirtualGroundVoltageInt = HALF_VCC, }; } - -static struct { - uint8_t pad[128]; -} config; - -static Configuration c; - -Configuration& GetConfiguration() -{ - // TODO: implement me! - return c; -} - -void SetConfiguration(const Configuration& newConfig) -{ - // TODO: implement me! -} - -/* TS stuff */ -void SaveConfiguration() { - // TODO: implement me! -} - -uint8_t *GetConfiguratiuonPtr() -{ - return (uint8_t *)&config; -} - -size_t GetConfiguratiuonSize() -{ - return sizeof(config); -} - -#define TS_SIGNATURE "rusEFI 2022.05.29.wideband_dual" - -const char *getTsSignature() { - return TS_SIGNATURE; -} \ No newline at end of file diff --git a/firmware/boards/f1_dual/wideband_board_config.h b/firmware/boards/f1_dual/wideband_board_config.h index e33405a..9f1f48a 100644 --- a/firmware/boards/f1_dual/wideband_board_config.h +++ b/firmware/boards/f1_dual/wideband_board_config.h @@ -1,5 +1,8 @@ #pragma once +// TS settings +#define TS_SIGNATURE "rusEFI 2022.05.29.wideband_dual" + // This board implements two channels #define AFR_CHANNELS 2 #define EGT_CHANNELS 2 diff --git a/firmware/boards/f1_rev2/board.mk b/firmware/boards/f1_rev2/board.mk index add0892..5757664 100644 --- a/firmware/boards/f1_rev2/board.mk +++ b/firmware/boards/f1_rev2/board.mk @@ -2,6 +2,8 @@ USE_BOOTLOADER = no MCU = cortex-m3 +ALLCPPSRC += $(BOARDDIR)/../f1_common/f1_port.cpp + include $(CHIBIOS)/os/common/startup/ARMCMx/compilers/GCC/mk/startup_stm32f1xx.mk include $(CHIBIOS)/os/hal/ports/STM32/STM32F1xx/platform.mk include $(CHIBIOS)/os/common/ports/ARMv7-M/compilers/GCC/mk/port.mk diff --git a/firmware/boards/f1_rev2/halconf.h b/firmware/boards/f1_rev2/halconf.h index 8c2372c..c2abaf9 100644 --- a/firmware/boards/f1_rev2/halconf.h +++ b/firmware/boards/f1_rev2/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/firmware/boards/f1_rev2/port.cpp b/firmware/boards/f1_rev2/port.cpp index d898184..28a3608 100644 --- a/firmware/boards/f1_rev2/port.cpp +++ b/firmware/boards/f1_rev2/port.cpp @@ -76,41 +76,3 @@ AnalogResult AnalogSample() .VirtualGroundVoltageInt = HALF_VCC, }; } - -static struct { - uint8_t pad[128]; -} config; - -static Configuration c; - -Configuration& GetConfiguration() -{ - // TODO: implement me! - return c; -} - -void SetConfiguration(const Configuration& newConfig) -{ - // TODO: implement me! -} - -/* TS stuff */ -void SaveConfiguration() { - // TODO: implement me! -} - -uint8_t *GetConfiguratiuonPtr() -{ - return (uint8_t *)&config; -} - -size_t GetConfiguratiuonSize() -{ - return sizeof(config); -} - -#define TS_SIGNATURE "rusEFI 2022.05.29.wideband_f1" - -const char *getTsSignature() { - return TS_SIGNATURE; -} diff --git a/firmware/boards/f1_rev2/wideband_board_config.h b/firmware/boards/f1_rev2/wideband_board_config.h index b6f2319..faa7a10 100644 --- a/firmware/boards/f1_rev2/wideband_board_config.h +++ b/firmware/boards/f1_rev2/wideband_board_config.h @@ -1,5 +1,8 @@ #pragma once +// TS settings +#define TS_SIGNATURE "rusEFI 2022.05.29.wideband_f1" + // Fundamental board constants #define VCC_VOLTS (3.3f) #define HALF_VCC (VCC_VOLTS / 2) diff --git a/firmware/boards/f1_rev3/board.mk b/firmware/boards/f1_rev3/board.mk index add0892..5757664 100644 --- a/firmware/boards/f1_rev3/board.mk +++ b/firmware/boards/f1_rev3/board.mk @@ -2,6 +2,8 @@ USE_BOOTLOADER = no MCU = cortex-m3 +ALLCPPSRC += $(BOARDDIR)/../f1_common/f1_port.cpp + include $(CHIBIOS)/os/common/startup/ARMCMx/compilers/GCC/mk/startup_stm32f1xx.mk include $(CHIBIOS)/os/hal/ports/STM32/STM32F1xx/platform.mk include $(CHIBIOS)/os/common/ports/ARMv7-M/compilers/GCC/mk/port.mk diff --git a/firmware/boards/f1_rev3/port.cpp b/firmware/boards/f1_rev3/port.cpp index d9e3fdd..bc6286c 100644 --- a/firmware/boards/f1_rev3/port.cpp +++ b/firmware/boards/f1_rev3/port.cpp @@ -1,11 +1,7 @@ #include "port.h" - #include "wideband_config.h" -#include "hal.h" -#include "hal_mfs.h" - #define ADC_CHANNEL_COUNT 5 #define ADC_SAMPLE ADC_SAMPLE_7P5 @@ -77,67 +73,3 @@ AnalogResult AnalogSample() .VirtualGroundVoltageInt = HALF_VCC, }; } - -// Settings - -static struct { - uint8_t pad[128]; -} config; - -const MFSConfig mfscfg1 = { - .flashp = (BaseFlash *)&EFLD1, - .erased = 0xFFFFFFFFU, - .bank_size = 4096U, - .bank0_start = 120U, - .bank0_sectors = 4U, - .bank1_start = 124U, - .bank1_sectors = 4U -}; - -MFSDriver mfs1; - -int InitConfiguration() -{ - /* Starting EFL driver.*/ - eflStart(&EFLD1, NULL); - - mfsObjectInit(&mfs1); - - mfsStart(&mfs1, &mfscfg1); - - return 0; -} - -static Configuration c; - -Configuration& GetConfiguration() -{ - // TODO: implement me! - return c; -} - -void SetConfiguration(const Configuration& newConfig) -{ - // TODO: implement me! -} - -/* TS stuff */ -void SaveConfiguration() { - // TODO: implement me! -} - -uint8_t *GetConfiguratiuonPtr() -{ - return (uint8_t *)&config; -} - -size_t GetConfiguratiuonSize() -{ - return sizeof(config); -} - -#define TS_SIGNATURE "rusEFI 2022.05.29.wideband_f1" - -const char *getTsSignature() { - return TS_SIGNATURE; -} diff --git a/firmware/boards/f1_rev3/wideband_board_config.h b/firmware/boards/f1_rev3/wideband_board_config.h index 7ec01bc..688f2dd 100644 --- a/firmware/boards/f1_rev3/wideband_board_config.h +++ b/firmware/boards/f1_rev3/wideband_board_config.h @@ -1,5 +1,8 @@ #pragma once +// TS settings +#define TS_SIGNATURE "rusEFI 2022.05.29.wideband_f1" + // Fundamental board constants #define VCC_VOLTS (3.3f) #define HALF_VCC (VCC_VOLTS / 2) diff --git a/firmware/boards/port.h b/firmware/boards/port.h index 05211cf..3a75b4d 100644 --- a/firmware/boards/port.h +++ b/firmware/boards/port.h @@ -30,6 +30,7 @@ public: { return this->Tag == ExpectedTag; } + void LoadDefaults(); // Actual configuration data uint8_t CanIndexOffset = 0; @@ -38,6 +39,7 @@ public: uint8_t pad[128 - 1 - 4]; }; +int InitConfiguration(); Configuration& GetConfiguration(); void SetConfiguration(const Configuration& newConfig); diff --git a/firmware/main.cpp b/firmware/main.cpp index dbd955f..b37f97e 100644 --- a/firmware/main.cpp +++ b/firmware/main.cpp @@ -11,6 +11,7 @@ #include "io_pins.h" #include "auxout.h" #include "max31855.h" +#include "port.h" #include "wideband_config.h" @@ -23,6 +24,9 @@ int main() { halInit(); chSysInit(); + // Load configuration + InitConfiguration(); + // Fire up all of our threads StartSampling(); InitPumpDac();