Skip to content

Commit

Permalink
Rework entire firmware optimizing ram usage. Inmense amount of work!
Browse files Browse the repository at this point in the history
The whole profile is no longer stored in RAM, only the current tip and
the common profile settings.
Now the entire system/profile settings in ram use only ~250 bytes.
This saves about 1KB, but also removes a lot of other limitations.
The system saving functions where completely redesigned, unifying most
functionality in saveSettings.

10KB devices have 1.5KB free ram now!
  • Loading branch information
David authored and David committed Dec 31, 2023
1 parent 42a02b5 commit 235ece6
Show file tree
Hide file tree
Showing 15 changed files with 1,117 additions and 572 deletions.
35 changes: 35 additions & 0 deletions Core/Inc/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ extern "C" {
#endif

/* Includes ------------------------------------------------------------------*/
#include "stm32f1xx_hal.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
Expand Down Expand Up @@ -65,6 +66,8 @@ extern "C" {

/* USER CODE END EM */

void HAL_TIM_MspPostInit(TIM_HandleTypeDef *htim);

/* Exported functions prototypes ---------------------------------------------*/
void Error_Handler(void);

Expand All @@ -73,6 +76,38 @@ void Error_Handler(void);
/* USER CODE END EFP */

/* Private defines -----------------------------------------------------------*/
#define DISPLAY_CS_Pin GPIO_PIN_13
#define DISPLAY_CS_GPIO_Port GPIOC
#define VREF_Pin GPIO_PIN_1
#define VREF_GPIO_Port GPIOA
#define NTC_Pin GPIO_PIN_2
#define NTC_GPIO_Port GPIOA
#define VIN_Pin GPIO_PIN_3
#define VIN_GPIO_Port GPIOA
#define TIP_Pin GPIO_PIN_5
#define TIP_GPIO_Port GPIOA
#define DISPLAY_DC_Pin GPIO_PIN_11
#define DISPLAY_DC_GPIO_Port GPIOB
#define DISPLAY_RST_Pin GPIO_PIN_12
#define DISPLAY_RST_GPIO_Port GPIOB
#define HW_SCL_Pin GPIO_PIN_13
#define HW_SCL_GPIO_Port GPIOB
#define HW_SDA_Pin GPIO_PIN_15
#define HW_SDA_GPIO_Port GPIOB
#define ENC_R_Pin GPIO_PIN_8
#define ENC_R_GPIO_Port GPIOA
#define ENC_L_Pin GPIO_PIN_9
#define ENC_L_GPIO_Port GPIOA
#define WAKE_Pin GPIO_PIN_10
#define WAKE_GPIO_Port GPIOA
#define ENC_SW_Pin GPIO_PIN_11
#define ENC_SW_GPIO_Port GPIOA
#define BUZZER_Pin GPIO_PIN_12
#define BUZZER_GPIO_Port GPIOA
#define PWM_DBG_Pin GPIO_PIN_3
#define PWM_DBG_GPIO_Port GPIOB
#define PWM_Pin GPIO_PIN_7
#define PWM_GPIO_Port GPIOB

/* USER CODE BEGIN Private defines */

Expand Down
74 changes: 56 additions & 18 deletions Core/Inc/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
#include "board.h"

#define SWSTRING "SW: "__DATE__ // Software version reported in settings screen
#define SYSTEM_SETTINGS_VERSION 26 // Change this if you change the system settings struct to prevent getting out of sync
#define PROFILE_SETTINGS_VERSION 1 // Same, but for profile settings struct
#define SYSTEM_SETTINGS_VERSION 27 // Change this if you change the system settings struct to prevent getting out of sync
#define PROFILE_SETTINGS_VERSION 2 // Same, but for profile settings struct

#define LANGUAGE_COUNT 7 // Number of languages
#define NUM_PROFILES 3 // Number of profiles
Expand Down Expand Up @@ -90,15 +90,21 @@ typedef enum{

save_Settings = 1,
save_Profile = 2,
//save_Profiles = 4, // This is never used, only one profile is used at a time
save_Tip = 4,
save_Addons = 8,
save_All = save_Settings | save_Profile | save_Addons,
save_All = save_Settings | save_Profile | save_Addons,

reset_Settings = 0x10,
reset_Profile = 0x20,
reset_Profiles = 0x40,
reset_Addons = 0x80,
reset_All = reset_Settings | reset_Profile | reset_Addons,
reset_All = reset_Settings | reset_Profiles | reset_Addons,

mode_SaveTip = 1,
mode_AddTip = 2,
mode_DeleteTip = 3,

perform_scanFix = 0, // Perform check over existing flash, reset any wrong section

no_reboot = 0,
do_reboot = 1,
Expand Down Expand Up @@ -211,7 +217,6 @@ __attribute__((aligned(4))) typedef struct{
uint16_t : 16;
uint16_t : 16;
uint16_t : 16;
tipData_t tip[NUM_TIPS];
uint32_t errorTimeout;
uint32_t boostTimeout;
uint32_t sleepTimeout;
Expand All @@ -220,6 +225,11 @@ __attribute__((aligned(4))) typedef struct{
uint32_t : 32;
uint32_t : 32;
uint32_t : 32;
}profile_settings_t;

__attribute__((aligned(4))) typedef struct{
tipData_t tip[NUM_TIPS];
profile_settings_t settings;
}profile_t;

__attribute__((aligned(4))) typedef struct{
Expand All @@ -238,12 +248,7 @@ __attribute__((aligned(4))) typedef struct{
uint8_t displayResRatio : 4;
#endif
};
#if (SYSTEM_SETTINGS_VERSION == 27) // Future version
#warning Clean this up!
#endif
uint8_t :8; // TODO: old rememberLastProfile (Delete when settings version change)
uint8_t hasBattery;
uint8_t :8; // TODO: old rememberLastTip (Delete when settings version change)
uint8_t dim_inSleep;
uint8_t EncoderMode;
uint8_t debugEnabled;
Expand Down Expand Up @@ -309,21 +314,54 @@ __attribute__((aligned(4))) typedef struct {

__attribute__((aligned(4))) typedef struct{
settings_t settings;
uint32_t settingsChecksum;
profile_t Profile;
uint32_t ProfileChecksum;
profile_settings_t Profile;
tipData_t currentTipData;
#ifdef ENABLE_ADDONS
addonSettings_t addonSettings;
uint32_t addonSettingsChecksum;
#endif
uint8_t setupMode;
uint8_t isSaving;
uint8_t currentProfile;
uint8_t currentTip;
uint8_t tipUpdateMode;
uint8_t tipUpdateIndex;
}systemSettings_t;


__attribute__((aligned(4))) typedef struct{
settings_t settings;
uint32_t settingsChecksum;
} flashSettingsSettings_t;

__attribute__((aligned(4))) typedef struct{
profile_t Profile[NUM_PROFILES];
uint32_t ProfileSettingsChecksum[NUM_PROFILES]; // Only for small profile settings block (Excluding tips)
uint32_t ProfileChecksum[NUM_PROFILES]; // For the entire profile (Including tips)
} flashSettingsProfiles_t;

__attribute__((aligned(4))) typedef struct{
addonSettings_t addonSettings;
uint32_t addonSettingsChecksum;
} flashSettingsAddons_t;

typedef struct{
uint16_t profile[128];
uint16_t tip[NUM_PROFILES][128];
uint16_t temperature[512];
}temp_settings_t;


extern flashSettingsSettings_t flashGlobalSettings;
extern flashSettingsProfiles_t flashProfilesSettings;
extern temp_settings_t temp_settings;
#ifdef ENABLE_ADDONS
extern flashSettingsAddons_t flashAddonSettings;
#endif


extern systemSettings_t systemSettings;
extern const settings_t defaultSettings;
extern const settings_t defaultSystemSettings;
extern const profile_settings_t defaultProfileSettings;

/** Cyclic task to save the current temperature/tip/profile if needed. */
void updateTempData(bool force);
Expand All @@ -332,9 +370,9 @@ void saveSettings(uint8_t save_mode, uint8_t reboot_mode);
/** Reads the save flag. */
uint8_t getSaveFlag(void);
/** Loads the settings from flash on boot. */
void restoreSettings();
void restoreSettings(void);
/** Load/change to the profile with the given index */
void loadProfile(uint8_t profile);
ErrorStatus loadProfile(uint8_t profile);
void setCurrentTip(uint8_t tip);
tipData_t *getCurrentTip(void);

Expand Down

0 comments on commit 235ece6

Please sign in to comment.