read/write first attempt
This commit is contained in:
parent
6006fd39d7
commit
ed59135498
4 changed files with 37 additions and 10 deletions
|
@ -25,7 +25,10 @@ pico_sdk_init()
|
||||||
|
|
||||||
# Add executable. Default name is the project name, version 0.1
|
# Add executable. Default name is the project name, version 0.1
|
||||||
|
|
||||||
add_executable(pico_keyer pico_keyer.cpp )
|
add_executable(pico_keyer
|
||||||
|
pico_keyer.cpp
|
||||||
|
settings.cpp
|
||||||
|
)
|
||||||
|
|
||||||
pico_set_program_name(pico_keyer "pico_keyer")
|
pico_set_program_name(pico_keyer "pico_keyer")
|
||||||
pico_set_program_version(pico_keyer "0.1")
|
pico_set_program_version(pico_keyer "0.1")
|
||||||
|
@ -48,6 +51,7 @@ target_link_libraries(pico_keyer
|
||||||
hardware_flash
|
hardware_flash
|
||||||
hardware_exception
|
hardware_exception
|
||||||
pico_flash
|
pico_flash
|
||||||
|
pico_mem_ops
|
||||||
)
|
)
|
||||||
|
|
||||||
pico_add_extra_outputs(pico_keyer)
|
pico_add_extra_outputs(pico_keyer)
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include "pico/stdlib.h"
|
#include <pico/stdlib.h>
|
||||||
|
|
||||||
#include "settings.h"
|
#include "settings.h"
|
||||||
|
|
||||||
|
@ -16,5 +16,17 @@ int main()
|
||||||
|
|
||||||
puts("Hello, world!");
|
puts("Hello, world!");
|
||||||
|
|
||||||
|
Settings settings;
|
||||||
|
settings.mode = Mode::IAMBIC_A;
|
||||||
|
store_settings(settings);
|
||||||
|
printf("Iambic mode (stored): %d", static_cast<int>(settings.mode));
|
||||||
|
sleep_ms(1000);
|
||||||
|
Settings newSettings = read_settings();
|
||||||
|
printf("Iambic mode (loaded): %d", static_cast<int>(newSettings.mode));
|
||||||
|
|
||||||
|
while(true) {
|
||||||
|
sleep_ms(1000);
|
||||||
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
23
settings.cpp
23
settings.cpp
|
@ -1,19 +1,30 @@
|
||||||
|
#include <cstring>
|
||||||
|
|
||||||
#include "settings.h"
|
#include "settings.h"
|
||||||
|
|
||||||
namespace
|
namespace
|
||||||
{
|
{
|
||||||
const size_t PAGE_SIZE{256};
|
constexpr const uint32_t FLASH_TARGET_OFFSET = PICO_FLASH_SIZE_BYTES - FLASH_SECTOR_SIZE;
|
||||||
const size_t NVS_SIZE{4096};
|
const uint8_t *flash_target_contents = (const uint8_t *) (XIP_BASE + FLASH_TARGET_OFFSET);
|
||||||
const uint32_t FLASH_WRITE_START = PICO_FLASH_SIZE_BYTES - NVS_SIZE;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void flash_store_callback(void *settings)
|
void flash_store_callback(void *settings)
|
||||||
{
|
{
|
||||||
flash_range_erase(FLASH_WRITE_START, NVS_SIZE);
|
int writeSize = (sizeof(struct Settings) / FLASH_PAGE_SIZE) + 1;
|
||||||
flash_range_program(FLASH_WRITE_START, (const uint8_t *)settings, PAGE_SIZE);
|
|
||||||
|
flash_range_erase(FLASH_TARGET_OFFSET, FLASH_SECTOR_SIZE);
|
||||||
|
flash_range_program(FLASH_TARGET_OFFSET, reinterpret_cast<uint8_t *>(settings), FLASH_PAGE_SIZE * writeSize);
|
||||||
}
|
}
|
||||||
|
|
||||||
void store_settings(Settings &settings)
|
void store_settings(Settings &settings)
|
||||||
{
|
{
|
||||||
flash_safe_execute(flash_store_callback, (void *)&settings, 1000);
|
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;
|
||||||
}
|
}
|
|
@ -1,7 +1,7 @@
|
||||||
#ifndef SETTINGS_H
|
#ifndef SETTINGS_H
|
||||||
#define SETTINGS_H
|
#define SETTINGS_H
|
||||||
|
|
||||||
#include "pico/flash.h"
|
#include <pico/flash.h>
|
||||||
|
|
||||||
enum class Mode
|
enum class Mode
|
||||||
{
|
{
|
||||||
|
@ -16,7 +16,7 @@ struct Settings
|
||||||
};
|
};
|
||||||
|
|
||||||
void store_settings(Settings &settings);
|
void store_settings(Settings &settings);
|
||||||
|
Settings read_settings();
|
||||||
|
|
||||||
|
|
||||||
#endif
|
#endif
|
Loading…
Reference in a new issue