Some warning cleanups

This commit is contained in:
whowechina 2024-06-18 11:48:39 +08:00
parent 8c84a99134
commit 31f981f421
8 changed files with 406 additions and 414 deletions

8
.gitignore vendored
View File

@ -1,7 +1,5 @@
PCB/Keyboard/iidx_teeny-backups/
Production/PCB/Turntable
Production/PCB/teeny_iidx
Production/PCB/*/
*-backups
.editorconfig
.vscode
firmware/mai2/mai2hook
PCB/IO/mai_io-backups

Binary file not shown.

View File

@ -1,8 +1,9 @@
set(BTSTACK_ROOT ${PICO_SDK_PATH}/lib/btstack)
set(LWIP_ROOT ${PICO_SDK_PATH}/lib/lwip)
pico_sdk_init()
include_directories(${CMAKE_CURRENT_LIST_DIR})
add_compile_options(-Wall -Werror -Wfatal-errors -O3)
function(make_firmware board board_def)
pico_sdk_init()
add_executable(${board}
main.c touch.c button.c rgb.c save.c config.c cli.c commands.c io.c hid.c
mpr121.c usb_descriptors.c)
@ -11,12 +12,7 @@ function(make_firmware board board_def)
pico_enable_stdio_uart(${board} 0)
pico_generate_pio_header(${board} ${CMAKE_CURRENT_LIST_DIR}/ws2812.pio)
target_compile_options(${board} PRIVATE -Wfatal-errors -O3)
target_include_directories(${board} PRIVATE ${CMAKE_CURRENT_LIST_DIR})
target_include_directories(${board} PRIVATE
${BTSTACK_ROOT}/src
${LWIP_ROOT}/src/include)
target_link_libraries(${board} PRIVATE
aic

View File

@ -27,11 +27,11 @@ static void disp_rgb()
printf("[RGB]\n");
printf(" Number per button: %d, number per aux: %d\n",
mai_cfg->rgb.per_button, mai_cfg->rgb.per_aux);
printf(" Key on: %06x, off: %06x\n Level: %d\n",
printf(" Key on: %06lx, off: %06lx\n Level: %d\n",
mai_cfg->color.key_on, mai_cfg->color.key_off, mai_cfg->color.level);
}
static void print_sense_zone(const char *title, const uint8_t *zones, int num)
static void print_sense_zone(const char *title, const int8_t *zones, int num)
{
printf(" %s |", title);
for (int i = 0; i < num; i++) {
@ -276,7 +276,7 @@ static void handle_filter(int argc, char *argv[])
disp_sense();
}
static uint8_t *extract_key(const char *param)
static int8_t *extract_key(const char *param)
{
if (strlen(param) != 2) {
return NULL;
@ -319,7 +319,7 @@ static void handle_sense(int argc, char *argv[])
" >sense +\n"
" >sense -\n"
" >sense A3 +\n"
" >sense C1 -\n";
" >sense C1 -\n"
" >sense * 0\n";
if ((argc < 1) || (argc > 2)) {
printf(usage);
@ -340,7 +340,7 @@ static void handle_sense(int argc, char *argv[])
sense_do_op(&mai_cfg->sense.zones[i], op[0]);
}
} else {
uint8_t *key = extract_key(argv[0]);
int8_t *key = extract_key(argv[0]);
if (!key) {
printf(usage);
return;

View File

@ -47,7 +47,7 @@ static void report_usb_hid()
uint16_t buttons = button_read();
hid_joy.buttons[0] = native_to_io4(buttons);
hid_joy.buttons[1] = native_to_io4(0);
if (last_buttons ^ buttons & (1 << 11)) {
if ((last_buttons ^ buttons) & (1 << 11)) {
if (buttons & (1 << 11)) {
// just pressed coin button
hid_joy.chutes[0] += 0x100;

View File

@ -39,7 +39,6 @@
static void run_lights()
{
uint64_t now = time_us_64();
if (io_is_active() || aime_is_active()) {
return;
}

View File

@ -1,215 +1,215 @@
/*
* MP121 Captive Touch Sensor
* WHowe <github.com/whowechina>
*
*/
#include <stdint.h>
#include "hardware/i2c.h"
#include "mpr121.h"
#include "board_defs.h"
#define IO_TIMEOUT_US 1000
#define TOUCH_THRESHOLD_BASE 22
#define RELEASE_THRESHOLD_BASE 15
#define MPR121_TOUCH_STATUS_REG 0x00
#define MPR121_OUT_OF_RANGE_STATUS_0_REG 0x02
#define MPR121_OUT_OF_RANGE_STATUS_1_REG 0x03
#define MPR121_ELECTRODE_FILTERED_DATA_REG 0x04
#define MPR121_BASELINE_VALUE_REG 0x1E
#define MPR121_MAX_HALF_DELTA_RISING_REG 0x2B
#define MPR121_NOISE_HALF_DELTA_RISING_REG 0x2C
#define MPR121_NOISE_COUNT_LIMIT_RISING_REG 0x2D
#define MPR121_FILTER_DELAY_COUNT_RISING_REG 0x2E
#define MPR121_MAX_HALF_DELTA_FALLING_REG 0x2F
#define MPR121_NOISE_HALF_DELTA_FALLING_REG 0x30
#define MPR121_NOISE_COUNT_LIMIT_FALLING_REG 0x31
#define MPR121_FILTER_DELAY_COUNT_FALLING_REG 0x32
#define MPR121_NOISE_HALF_DELTA_TOUCHED_REG 0x33
#define MPR121_NOISE_COUNT_LIMIT_TOUCHED_REG 0x34
#define MPR121_FILTER_DELAY_COUNT_TOUCHED_REG 0x35
#define MPR121_TOUCH_THRESHOLD_REG 0x41
#define MPR121_RELEASE_THRESHOLD_REG 0x42
#define MPR121_DEBOUNCE_REG 0x5B
#define MPR121_AFE_CONFIG_REG 0x5C
#define MPR121_FILTER_CONFIG_REG 0x5D
#define MPR121_ELECTRODE_CONFIG_REG 0x5E
#define MPR121_ELECTRODE_CURRENT_REG 0x5F
#define MPR121_ELECTRODE_CHARGE_TIME_REG 0x6C
#define MPR121_GPIO_CTRL_0_REG 0x73
#define MPR121_GPIO_CTRL_1_REG 0x74
#define MPR121_GPIO_DATA_REG 0x75
#define MPR121_GPIO_DIRECTION_REG 0x76
#define MPR121_GPIO_ENABLE_REG 0x77
#define MPR121_GPIO_DATA_SET_REG 0x78
#define MPR121_GPIO_DATA_CLEAR_REG 0x79
#define MPR121_GPIO_DATA_TOGGLE_REG 0x7A
#define MPR121_AUTOCONFIG_CONTROL_0_REG 0x7B
#define MPR121_AUTOCONFIG_CONTROL_1_REG 0x7C
#define MPR121_AUTOCONFIG_USL_REG 0x7D
#define MPR121_AUTOCONFIG_LSL_REG 0x7E
#define MPR121_AUTOCONFIG_TARGET_REG 0x7F
#define MPR121_SOFT_RESET_REG 0x80
static void write_reg(uint8_t addr, uint8_t reg, uint8_t val)
{
uint8_t buf[] = {reg, val};
i2c_write_blocking_until(I2C_PORT, addr, buf, 2, false,
time_us_64() + IO_TIMEOUT_US);
}
static uint8_t read_reg(uint8_t addr, uint8_t reg)
{
uint8_t value;
i2c_write_blocking_until(I2C_PORT, addr, &reg, 1, true,
time_us_64() + IO_TIMEOUT_US);
i2c_read_blocking_until(I2C_PORT, addr, &value, 1, false,
time_us_64() + IO_TIMEOUT_US);
return value;
}
void mpr121_init(uint8_t i2c_addr)
{
write_reg(i2c_addr, 0x80, 0x63); // Soft reset MPR121 if not reset correctly
//touch pad baseline filter
//rising: baseline quick rising
write_reg(i2c_addr, 0x2B, 1); // Max half delta Rising
write_reg(i2c_addr, 0x2C, 1); // Noise half delta Rising
write_reg(i2c_addr, 0x2D, 1); // Noise count limit Rising
write_reg(i2c_addr, 0x2E, 1); // Delay limit Rising
//falling: baseline slow falling
write_reg(i2c_addr, 0x2F, 1); // Max half delta Falling
write_reg(i2c_addr, 0x30, 1); // Noise half delta Falling
write_reg(i2c_addr, 0x31, 6); // Noise count limit Falling
write_reg(i2c_addr, 0x32, 12); // Delay limit Falling
//touched: baseline very slow falling
write_reg(i2c_addr, 0x33, 1); // Noise half delta Touched
write_reg(i2c_addr, 0x34, 8); // Noise count Touched
write_reg(i2c_addr, 0x35, 30); // Delay limit Touched
//Touch pad threshold
for (int i = 0; i < 12; i++) {
write_reg(i2c_addr, 0x41 + i * 2, TOUCH_THRESHOLD_BASE);
write_reg(i2c_addr, 0x42 + i * 2, RELEASE_THRESHOLD_BASE);
}
//touch and release debounce
write_reg(i2c_addr, 0x5B, 0x00);
//AFE and filter configuration
write_reg(i2c_addr, 0x5C, 0b00010000); // AFES=6 samples, same as AFES in 0x7B, Global CDC=16uA
write_reg(i2c_addr, 0x5D, 0b00101000); // CT=0.5us, TDS=4samples, TDI=16ms
write_reg(i2c_addr, 0x5E, 0x80); // Set baseline calibration enabled, baseline loading 5MSB
//Auto Configuration
write_reg(i2c_addr, 0x7B, 0b00001011); // AFES=6 samples, same as AFES in 0x5C
// retry=2b00, no retry,
// BVA=2b10, load 5MSB after AC,
// ARE/ACE=2b11, auto configuration enabled
//write_reg(i2c_addr, 0x7C,0x80); // Skip charge time search, use setting in 0x5D,
// OOR, AR, AC IE disabled
// Not used. Possible Proximity CDC shall over 63uA
// if only use 0.5uS CDT, the TGL for proximity cannot meet
// Possible if manually set Register0x72=0x03
// (Auto configure result) alone.
// I want to max out sensitivity, I don't care linearity
const uint8_t usl = 255; //(3.3 - 0.0) / 3.3 * 256;
write_reg(i2c_addr, 0x7D, usl),
write_reg(i2c_addr, 0x7E, usl * 0.65),
write_reg(i2c_addr, 0x7F, usl * 0.9);
write_reg(i2c_addr, 0x5E, 0x8C); // Run 12 touch, load 5MSB to baseline
}
#define ABS(x) ((x) < 0 ? -(x) : (x))
static bool mpr121_read_many(uint8_t addr, uint8_t reg, uint8_t *buf, size_t n)
{
i2c_write_blocking_until(I2C_PORT, addr, &reg, 1, true,
time_us_64() + IO_TIMEOUT_US);
int bytes = i2c_read_blocking_until(I2C_PORT, addr, buf, n, false,
time_us_64() + IO_TIMEOUT_US * n / 2);
return bytes == n;
}
static bool mpr121_read_many16(uint8_t addr, uint8_t reg, uint16_t *buf, size_t n)
{
uint8_t vals[n * 2];
if (!mpr121_read_many(addr, reg, vals, n * 2)){
return false;
}
for (int i = 0; i < n; i++) {
buf[i] = (vals[i * 2 + 1] << 8) | vals[i * 2];
}
return true;
}
uint16_t mpr121_touched(uint8_t addr)
{
uint16_t touched;
mpr121_read_many16(addr, MPR121_TOUCH_STATUS_REG, &touched, 2);
return touched;
}
bool mpr121_raw(uint8_t addr, uint16_t *raw, int num)
{
return mpr121_read_many16(addr, MPR121_ELECTRODE_FILTERED_DATA_REG, raw, num);
}
static uint8_t mpr121_stop(uint8_t addr)
{
uint8_t ecr = read_reg(addr, MPR121_ELECTRODE_CONFIG_REG);
write_reg(addr, MPR121_ELECTRODE_CONFIG_REG, ecr & 0xC0);
return ecr;
}
static uint8_t mpr121_resume(uint8_t addr, uint8_t ecr)
{
write_reg(addr, MPR121_ELECTRODE_CONFIG_REG, ecr);
}
void mpr121_filter(uint8_t addr, uint8_t ffi, uint8_t sfi, uint8_t esi)
{
uint8_t ecr = mpr121_stop(addr);
uint8_t afe = read_reg(addr, MPR121_AFE_CONFIG_REG);
write_reg(addr, MPR121_AFE_CONFIG_REG, (afe & 0x3f) | ffi << 6);
uint8_t acc = read_reg(addr, MPR121_AUTOCONFIG_CONTROL_0_REG);
write_reg(addr, MPR121_AUTOCONFIG_CONTROL_0_REG, (acc & 0x3f) | ffi << 6);
uint8_t fcr = read_reg(addr, MPR121_FILTER_CONFIG_REG);
write_reg(addr, MPR121_FILTER_CONFIG_REG,
(fcr & 0xe0) | ((sfi & 3) << 3) | esi);
mpr121_resume(addr, ecr);
}
void mpr121_sense(uint8_t addr, int8_t sense, int8_t *sense_keys, int num)
{
uint8_t ecr = mpr121_stop(addr);
for (int i = 0; i < num; i++) {
int8_t delta = sense + sense_keys[i];
write_reg(addr, MPR121_TOUCH_THRESHOLD_REG + i * 2,
TOUCH_THRESHOLD_BASE - delta);
write_reg(addr, MPR121_RELEASE_THRESHOLD_REG + i * 2,
RELEASE_THRESHOLD_BASE - delta / 2);
}
mpr121_resume(addr, ecr);
}
void mpr121_debounce(uint8_t addr, uint8_t touch, uint8_t release)
{
uint8_t ecr = mpr121_stop(addr);
write_reg(addr, 0x5B, (release & 0x07) << 4 | (touch & 0x07));
mpr121_resume(addr, ecr);
}
/*
* MP121 Captive Touch Sensor
* WHowe <github.com/whowechina>
*
*/
#include <stdint.h>
#include "hardware/i2c.h"
#include "mpr121.h"
#include "board_defs.h"
#define IO_TIMEOUT_US 1000
#define TOUCH_THRESHOLD_BASE 22
#define RELEASE_THRESHOLD_BASE 15
#define MPR121_TOUCH_STATUS_REG 0x00
#define MPR121_OUT_OF_RANGE_STATUS_0_REG 0x02
#define MPR121_OUT_OF_RANGE_STATUS_1_REG 0x03
#define MPR121_ELECTRODE_FILTERED_DATA_REG 0x04
#define MPR121_BASELINE_VALUE_REG 0x1E
#define MPR121_MAX_HALF_DELTA_RISING_REG 0x2B
#define MPR121_NOISE_HALF_DELTA_RISING_REG 0x2C
#define MPR121_NOISE_COUNT_LIMIT_RISING_REG 0x2D
#define MPR121_FILTER_DELAY_COUNT_RISING_REG 0x2E
#define MPR121_MAX_HALF_DELTA_FALLING_REG 0x2F
#define MPR121_NOISE_HALF_DELTA_FALLING_REG 0x30
#define MPR121_NOISE_COUNT_LIMIT_FALLING_REG 0x31
#define MPR121_FILTER_DELAY_COUNT_FALLING_REG 0x32
#define MPR121_NOISE_HALF_DELTA_TOUCHED_REG 0x33
#define MPR121_NOISE_COUNT_LIMIT_TOUCHED_REG 0x34
#define MPR121_FILTER_DELAY_COUNT_TOUCHED_REG 0x35
#define MPR121_TOUCH_THRESHOLD_REG 0x41
#define MPR121_RELEASE_THRESHOLD_REG 0x42
#define MPR121_DEBOUNCE_REG 0x5B
#define MPR121_AFE_CONFIG_REG 0x5C
#define MPR121_FILTER_CONFIG_REG 0x5D
#define MPR121_ELECTRODE_CONFIG_REG 0x5E
#define MPR121_ELECTRODE_CURRENT_REG 0x5F
#define MPR121_ELECTRODE_CHARGE_TIME_REG 0x6C
#define MPR121_GPIO_CTRL_0_REG 0x73
#define MPR121_GPIO_CTRL_1_REG 0x74
#define MPR121_GPIO_DATA_REG 0x75
#define MPR121_GPIO_DIRECTION_REG 0x76
#define MPR121_GPIO_ENABLE_REG 0x77
#define MPR121_GPIO_DATA_SET_REG 0x78
#define MPR121_GPIO_DATA_CLEAR_REG 0x79
#define MPR121_GPIO_DATA_TOGGLE_REG 0x7A
#define MPR121_AUTOCONFIG_CONTROL_0_REG 0x7B
#define MPR121_AUTOCONFIG_CONTROL_1_REG 0x7C
#define MPR121_AUTOCONFIG_USL_REG 0x7D
#define MPR121_AUTOCONFIG_LSL_REG 0x7E
#define MPR121_AUTOCONFIG_TARGET_REG 0x7F
#define MPR121_SOFT_RESET_REG 0x80
static void write_reg(uint8_t addr, uint8_t reg, uint8_t val)
{
uint8_t buf[] = {reg, val};
i2c_write_blocking_until(I2C_PORT, addr, buf, 2, false,
time_us_64() + IO_TIMEOUT_US);
}
static uint8_t read_reg(uint8_t addr, uint8_t reg)
{
uint8_t value;
i2c_write_blocking_until(I2C_PORT, addr, &reg, 1, true,
time_us_64() + IO_TIMEOUT_US);
i2c_read_blocking_until(I2C_PORT, addr, &value, 1, false,
time_us_64() + IO_TIMEOUT_US);
return value;
}
void mpr121_init(uint8_t i2c_addr)
{
write_reg(i2c_addr, 0x80, 0x63); // Soft reset MPR121 if not reset correctly
//touch pad baseline filter
//rising: baseline quick rising
write_reg(i2c_addr, 0x2B, 1); // Max half delta Rising
write_reg(i2c_addr, 0x2C, 1); // Noise half delta Rising
write_reg(i2c_addr, 0x2D, 1); // Noise count limit Rising
write_reg(i2c_addr, 0x2E, 1); // Delay limit Rising
//falling: baseline slow falling
write_reg(i2c_addr, 0x2F, 1); // Max half delta Falling
write_reg(i2c_addr, 0x30, 1); // Noise half delta Falling
write_reg(i2c_addr, 0x31, 6); // Noise count limit Falling
write_reg(i2c_addr, 0x32, 12); // Delay limit Falling
//touched: baseline very slow falling
write_reg(i2c_addr, 0x33, 1); // Noise half delta Touched
write_reg(i2c_addr, 0x34, 8); // Noise count Touched
write_reg(i2c_addr, 0x35, 30); // Delay limit Touched
//Touch pad threshold
for (int i = 0; i < 12; i++) {
write_reg(i2c_addr, 0x41 + i * 2, TOUCH_THRESHOLD_BASE);
write_reg(i2c_addr, 0x42 + i * 2, RELEASE_THRESHOLD_BASE);
}
//touch and release debounce
write_reg(i2c_addr, 0x5B, 0x00);
//AFE and filter configuration
write_reg(i2c_addr, 0x5C, 0b00010000); // AFES=6 samples, same as AFES in 0x7B, Global CDC=16uA
write_reg(i2c_addr, 0x5D, 0b00101000); // CT=0.5us, TDS=4samples, TDI=16ms
write_reg(i2c_addr, 0x5E, 0x80); // Set baseline calibration enabled, baseline loading 5MSB
//Auto Configuration
write_reg(i2c_addr, 0x7B, 0b00001011); // AFES=6 samples, same as AFES in 0x5C
// retry=2b00, no retry,
// BVA=2b10, load 5MSB after AC,
// ARE/ACE=2b11, auto configuration enabled
//write_reg(i2c_addr, 0x7C,0x80); // Skip charge time search, use setting in 0x5D,
// OOR, AR, AC IE disabled
// Not used. Possible Proximity CDC shall over 63uA
// if only use 0.5uS CDT, the TGL for proximity cannot meet
// Possible if manually set Register0x72=0x03
// (Auto configure result) alone.
// I want to max out sensitivity, I don't care linearity
const uint8_t usl = 255; //(3.3 - 0.0) / 3.3 * 256;
write_reg(i2c_addr, 0x7D, usl),
write_reg(i2c_addr, 0x7E, usl * 0.65),
write_reg(i2c_addr, 0x7F, usl * 0.9);
write_reg(i2c_addr, 0x5E, 0x8C); // Run 12 touch, load 5MSB to baseline
}
#define ABS(x) ((x) < 0 ? -(x) : (x))
static bool mpr121_read_many(uint8_t addr, uint8_t reg, uint8_t *buf, size_t n)
{
i2c_write_blocking_until(I2C_PORT, addr, &reg, 1, true,
time_us_64() + IO_TIMEOUT_US);
int bytes = i2c_read_blocking_until(I2C_PORT, addr, buf, n, false,
time_us_64() + IO_TIMEOUT_US * n / 2);
return bytes == n;
}
static bool mpr121_read_many16(uint8_t addr, uint8_t reg, uint16_t *buf, size_t n)
{
uint8_t vals[n * 2];
if (!mpr121_read_many(addr, reg, vals, n * 2)){
return false;
}
for (int i = 0; i < n; i++) {
buf[i] = (vals[i * 2 + 1] << 8) | vals[i * 2];
}
return true;
}
uint16_t mpr121_touched(uint8_t addr)
{
uint16_t touched = 0;
mpr121_read_many16(addr, MPR121_TOUCH_STATUS_REG, &touched, 1);
return touched;
}
bool mpr121_raw(uint8_t addr, uint16_t *raw, int num)
{
return mpr121_read_many16(addr, MPR121_ELECTRODE_FILTERED_DATA_REG, raw, num);
}
static uint8_t mpr121_stop(uint8_t addr)
{
uint8_t ecr = read_reg(addr, MPR121_ELECTRODE_CONFIG_REG);
write_reg(addr, MPR121_ELECTRODE_CONFIG_REG, ecr & 0xC0);
return ecr;
}
static void mpr121_resume(uint8_t addr, uint8_t ecr)
{
write_reg(addr, MPR121_ELECTRODE_CONFIG_REG, ecr);
}
void mpr121_filter(uint8_t addr, uint8_t ffi, uint8_t sfi, uint8_t esi)
{
uint8_t ecr = mpr121_stop(addr);
uint8_t afe = read_reg(addr, MPR121_AFE_CONFIG_REG);
write_reg(addr, MPR121_AFE_CONFIG_REG, (afe & 0x3f) | ffi << 6);
uint8_t acc = read_reg(addr, MPR121_AUTOCONFIG_CONTROL_0_REG);
write_reg(addr, MPR121_AUTOCONFIG_CONTROL_0_REG, (acc & 0x3f) | ffi << 6);
uint8_t fcr = read_reg(addr, MPR121_FILTER_CONFIG_REG);
write_reg(addr, MPR121_FILTER_CONFIG_REG,
(fcr & 0xe0) | ((sfi & 3) << 3) | esi);
mpr121_resume(addr, ecr);
}
void mpr121_sense(uint8_t addr, int8_t sense, int8_t *sense_keys, int num)
{
uint8_t ecr = mpr121_stop(addr);
for (int i = 0; i < num; i++) {
int8_t delta = sense + sense_keys[i];
write_reg(addr, MPR121_TOUCH_THRESHOLD_REG + i * 2,
TOUCH_THRESHOLD_BASE - delta);
write_reg(addr, MPR121_RELEASE_THRESHOLD_REG + i * 2,
RELEASE_THRESHOLD_BASE - delta / 2);
}
mpr121_resume(addr, ecr);
}
void mpr121_debounce(uint8_t addr, uint8_t touch, uint8_t release)
{
uint8_t ecr = mpr121_stop(addr);
write_reg(addr, 0x5B, (release & 0x07) << 4 | (touch & 0x07));
mpr121_resume(addr, ecr);
}

View File

@ -1,178 +1,177 @@
/*
* Controller Config Save and Load
* WHowe <github.com/whowechina>
*
* Config is stored in last sector of flash
*/
#include "save.h"
#include <stdint.h>
#include <stdlib.h>
#include <stdbool.h>
#include <memory.h>
#include "bsp/board.h"
#include "pico/bootrom.h"
#include "pico/stdio.h"
#include "hardware/flash.h"
#include "pico/multicore.h"
#include "pico/unique_id.h"
static struct {
size_t size;
size_t offset;
void (*after_load)();
} modules[8] = {0};
static int module_num = 0;
static uint32_t my_magic = 0xcafecafe;
#define SAVE_TIMEOUT_US 5000000
#define SAVE_SECTOR_OFFSET (PICO_FLASH_SIZE_BYTES - FLASH_SECTOR_SIZE)
typedef struct __attribute ((packed)) {
uint32_t magic;
uint8_t data[FLASH_PAGE_SIZE - 4];
} page_t;
static page_t old_data = {0};
static page_t new_data = {0};
static page_t default_data = {0};
static int data_page = -1;
static bool requesting_save = false;
static uint64_t requesting_time = 0;
static mutex_t *io_lock;
static void save_program()
{
old_data = new_data;
data_page = (data_page + 1) % (FLASH_SECTOR_SIZE / FLASH_PAGE_SIZE);
printf("\nProgram Flash %d %8lx\n", data_page, old_data.magic);
if (mutex_enter_timeout_us(io_lock, 100000)) {
sleep_ms(10); /* wait for all io operations to finish */
uint32_t ints = save_and_disable_interrupts();
if (data_page == 0) {
flash_range_erase(SAVE_SECTOR_OFFSET, FLASH_SECTOR_SIZE);
}
flash_range_program(SAVE_SECTOR_OFFSET + data_page * FLASH_PAGE_SIZE,
(uint8_t *)&old_data, FLASH_PAGE_SIZE);
restore_interrupts(ints);
mutex_exit(io_lock);
} else {
printf("Program Flash Failed.\n");
}
}
static void load_default()
{
printf("Load Default\n");
new_data = default_data;
new_data.magic = my_magic;
}
static const page_t *get_page(int id)
{
int addr = XIP_BASE + SAVE_SECTOR_OFFSET;
return (page_t *)(addr + FLASH_PAGE_SIZE * id);
}
static void save_load()
{
for (int i = 0; i < FLASH_SECTOR_SIZE / FLASH_PAGE_SIZE; i++) {
if (get_page(i)->magic != my_magic) {
break;
}
data_page = i;
}
if (data_page < 0) {
load_default();
save_request(false);
return;
}
old_data = *get_page(data_page);
new_data = old_data;
printf("Page Loaded %d %8lx\n", data_page, new_data.magic);
}
static void save_loaded()
{
for (int i = 0; i < module_num; i++) {
modules[i].after_load();
}
}
static union __attribute__((packed)) {
pico_unique_board_id_t id;
struct {
uint32_t id32h;
uint32_t id32l;
};
uint64_t id64;
} board_id;
uint32_t board_id_32()
{
pico_get_unique_board_id(&board_id.id);
return board_id.id32h ^ board_id.id32l;
}
uint64_t board_id_64()
{
pico_get_unique_board_id(&board_id.id);
return board_id.id64;
}
void save_init(uint32_t magic, mutex_t *locker)
{
my_magic = magic;
io_lock = locker;
save_load();
save_loop();
save_loaded();
}
void save_loop()
{
if (requesting_save && (time_us_64() - requesting_time > SAVE_TIMEOUT_US)) {
requesting_save = false;
/* only when data is actually changed */
if (memcmp(&old_data, &new_data, sizeof(old_data)) == 0) {
return;
}
save_program();
}
}
void *save_alloc(size_t size, void *def, void (*after_load)())
{
modules[module_num].size = size;
size_t offset = module_num > 0 ? modules[module_num - 1].offset + size : 0;
modules[module_num].offset = offset;
modules[module_num].after_load = after_load;
module_num++;
memcpy(default_data.data + offset, def, size); // backup the default
return new_data.data + offset;
}
void save_request(bool immediately)
{
if (!requesting_save) {
printf("Save requested.\n");
requesting_save = true;
new_data.magic = my_magic;
requesting_time = time_us_64();
}
if (immediately) {
requesting_time = 0;
save_loop();
}
}
/*
* Controller Config Save and Load
* WHowe <github.com/whowechina>
*
* Config is stored in last sector of flash
*/
#include "save.h"
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <memory.h>
#include "pico/bootrom.h"
#include "pico/stdio.h"
#include "hardware/flash.h"
#include "pico/multicore.h"
#include "pico/unique_id.h"
static struct {
size_t size;
size_t offset;
void (*after_load)();
} modules[8] = {0};
static int module_num = 0;
static uint32_t my_magic = 0xcafecafe;
#define SAVE_TIMEOUT_US 5000000
#define SAVE_SECTOR_OFFSET (PICO_FLASH_SIZE_BYTES - FLASH_SECTOR_SIZE)
typedef struct __attribute ((packed)) {
uint32_t magic;
uint8_t data[FLASH_PAGE_SIZE - 4];
} page_t;
static page_t old_data = {0};
static page_t new_data = {0};
static page_t default_data = {0};
static int data_page = -1;
static bool requesting_save = false;
static uint64_t requesting_time = 0;
static mutex_t *io_lock;
static void save_program()
{
old_data = new_data;
data_page = (data_page + 1) % (FLASH_SECTOR_SIZE / FLASH_PAGE_SIZE);
printf("\nProgram Flash %d %8lx\n", data_page, old_data.magic);
if (mutex_enter_timeout_us(io_lock, 100000)) {
sleep_ms(10); /* wait for all io operations to finish */
uint32_t ints = save_and_disable_interrupts();
if (data_page == 0) {
flash_range_erase(SAVE_SECTOR_OFFSET, FLASH_SECTOR_SIZE);
}
flash_range_program(SAVE_SECTOR_OFFSET + data_page * FLASH_PAGE_SIZE,
(uint8_t *)&old_data, FLASH_PAGE_SIZE);
restore_interrupts(ints);
mutex_exit(io_lock);
} else {
printf("Program Flash Failed.\n");
}
}
static void load_default()
{
printf("Load Default\n");
new_data = default_data;
new_data.magic = my_magic;
}
static const page_t *get_page(int id)
{
int addr = XIP_BASE + SAVE_SECTOR_OFFSET;
return (page_t *)(addr + FLASH_PAGE_SIZE * id);
}
static void save_load()
{
for (int i = 0; i < FLASH_SECTOR_SIZE / FLASH_PAGE_SIZE; i++) {
if (get_page(i)->magic != my_magic) {
break;
}
data_page = i;
}
if (data_page < 0) {
load_default();
save_request(false);
return;
}
old_data = *get_page(data_page);
new_data = old_data;
printf("Page Loaded %d %8lx\n", data_page, new_data.magic);
}
static void save_loaded()
{
for (int i = 0; i < module_num; i++) {
modules[i].after_load();
}
}
static union __attribute__((packed)) {
pico_unique_board_id_t id;
struct {
uint32_t id32h;
uint32_t id32l;
};
uint64_t id64;
} board_id;
uint32_t board_id_32()
{
pico_get_unique_board_id(&board_id.id);
return board_id.id32h ^ board_id.id32l;
}
uint64_t board_id_64()
{
pico_get_unique_board_id(&board_id.id);
return board_id.id64;
}
void save_init(uint32_t magic, mutex_t *locker)
{
my_magic = magic;
io_lock = locker;
save_load();
save_loop();
save_loaded();
}
void save_loop()
{
if (requesting_save && (time_us_64() - requesting_time > SAVE_TIMEOUT_US)) {
requesting_save = false;
/* only when data is actually changed */
if (memcmp(&old_data, &new_data, sizeof(old_data)) == 0) {
return;
}
save_program();
}
}
void *save_alloc(size_t size, void *def, void (*after_load)())
{
modules[module_num].size = size;
size_t offset = module_num > 0 ? modules[module_num - 1].offset + size : 0;
modules[module_num].offset = offset;
modules[module_num].after_load = after_load;
module_num++;
memcpy(default_data.data + offset, def, size); // backup the default
return new_data.data + offset;
}
void save_request(bool immediately)
{
if (!requesting_save) {
printf("Save requested.\n");
requesting_save = true;
new_data.magic = my_magic;
requesting_time = time_us_64();
}
if (immediately) {
requesting_time = 0;
save_loop();
}
}