raspikeyer/settings.cpp

30 lines
870 B
C++
Raw Normal View History

2024-02-07 09:32:11 +01:00
#include <cstring>
2024-02-06 22:11:58 +01:00
#include "settings.h"
namespace
{
2024-02-07 09:32:11 +01:00
constexpr const uint32_t FLASH_TARGET_OFFSET = PICO_FLASH_SIZE_BYTES - FLASH_SECTOR_SIZE;
const uint8_t *flash_target_contents = (const uint8_t *) (XIP_BASE + FLASH_TARGET_OFFSET);
2024-02-06 22:11:58 +01:00
}
void flash_store_callback(void *settings)
{
2024-02-07 09:32:11 +01:00
int writeSize = (sizeof(struct Settings) / FLASH_PAGE_SIZE) + 1;
flash_range_erase(FLASH_TARGET_OFFSET, FLASH_SECTOR_SIZE);
flash_range_program(FLASH_TARGET_OFFSET, reinterpret_cast<uint8_t *>(settings), FLASH_PAGE_SIZE * writeSize);
2024-02-06 22:11:58 +01:00
}
void store_settings(Settings &settings)
{
2024-02-07 09:32:11 +01:00
uint8_t *settingsAsBytes = reinterpret_cast<uint8_t *>(&settings);
flash_safe_execute(flash_store_callback, settingsAsBytes, 1000);
}
Settings read_settings() {
Settings settings;
memcpy(&settings, flash_target_contents, sizeof(struct Settings));
return settings;
2024-02-06 22:11:58 +01:00
}