read/write works

This commit is contained in:
Martin Brodbeck 2024-02-07 11:15:41 +01:00
parent a40d9e0e5a
commit e848ca8f92
3 changed files with 44 additions and 19 deletions

View file

@ -5,26 +5,35 @@
namespace
{
const uint LED_PIN = PICO_DEFAULT_LED_PIN;
}
int main()
{
stdio_init_all();
sleep_ms(1000);
puts("Hello, world!");
gpio_init(LED_PIN);
gpio_set_dir(LED_PIN, GPIO_OUT);
gpio_put(LED_PIN, 0);
Settings settings;
settings.mode = Mode::IAMBIC_A;
store_settings(settings);
printf("Iambic mode (stored): %d\n", static_cast<int>(settings.mode));
sleep_ms(1000);
Settings newSettings = read_settings();
printf("Iambic mode (loaded): %d\n", static_cast<int>(newSettings.mode));
settings.wpm = 25;
while(true) {
//store_settings(settings);
Settings newSettings{read_settings()};
printf("Iambic mode (loaded): %d\n", static_cast<int>(newSettings.mode));
printf("WPM (loaded): %d\n", newSettings.wpm);
if (settings.mode == Mode::IAMBIC_A && settings.wpm == 25)
{
gpio_put(LED_PIN, 1);
}
while (true)
{
sleep_ms(1000);
}

View file

@ -1,11 +1,14 @@
#include <cstring>
#include <pico/stdlib.h>
#include "settings.h"
namespace
{
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);
const uint LED_PIN = PICO_DEFAULT_LED_PIN;
}
void flash_store_callback(void *settings)
@ -13,17 +16,29 @@ void flash_store_callback(void *settings)
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);
flash_range_program(FLASH_TARGET_OFFSET, (uint8_t *)settings, FLASH_PAGE_SIZE);
}
void store_settings(Settings &settings)
{
uint8_t *settingsAsBytes = reinterpret_cast<uint8_t *>(&settings);
uint8_t *settingsAsBytes = (uint8_t *)&settings;
flash_safe_execute(flash_store_callback, settingsAsBytes, 1000);
int result = flash_safe_execute(flash_store_callback, settingsAsBytes, 1000);
if (result == PICO_OK)
{
gpio_put(LED_PIN, 1);
sleep_ms(250);
gpio_put(LED_PIN, 0);
sleep_ms(250);
gpio_put(LED_PIN, 1);
sleep_ms(250);
gpio_put(LED_PIN, 0);
sleep_ms(250);
}
}
Settings read_settings() {
Settings read_settings()
{
Settings settings;
memcpy(&settings, flash_target_contents, sizeof(struct Settings));
return settings;

View file

@ -3,20 +3,21 @@
#include <pico/flash.h>
enum class Mode
enum class Mode : uint8_t
{
IAMBIC_A,
IAMBIC_A = 0,
IAMBIC_B,
// ULTIMATE
};
struct Settings
{
Mode mode{Mode::IAMBIC_B};
Mode mode{Mode::IAMBIC_B}; // Byte 1
uint8_t wpm{20}; // Byte 2
uint8_t dummy[FLASH_PAGE_SIZE - 2]{0}; // Fill up to next flash page size boundary
};
void store_settings(Settings &settings);
Settings read_settings();
#endif