fix wifi & flash + eink screen on core 1, by using queue

This commit is contained in:
Adolfo Reyna
2025-12-10 13:38:04 -05:00
parent 65650a7b57
commit f197b9c1f4
5 changed files with 80 additions and 36 deletions

View File

@@ -1,5 +1,7 @@
#include "wifi_manager.h"
#include "pico/cyw43_arch.h"
#include "pico/multicore.h"
#include "pico/flash.h"
#include "hardware/flash.h"
#include "hardware/sync.h"
#include <stdio.h>
@@ -60,7 +62,20 @@ void wifi_scan() {
}
}
void wifi_save_credentials(const char* ssid, const char* password) {
// Helper struct for flash operations
struct FlashWriteParams {
const uint8_t* data;
size_t size;
};
// Actual flash operation to be executed safely
static void do_flash_write(void *param) {
FlashWriteParams *p = (FlashWriteParams*)param;
flash_range_erase(FLASH_TARGET_OFFSET, FLASH_SECTOR_SIZE);
flash_range_program(FLASH_TARGET_OFFSET, p->data, p->size);
}
int wifi_save_credentials(const char* ssid, const char* password) {
WifiCreds creds;
creds.magic = WIFI_CREDS_MAGIC;
strncpy(creds.ssid, ssid, sizeof(creds.ssid) - 1);
@@ -68,17 +83,25 @@ void wifi_save_credentials(const char* ssid, const char* password) {
strncpy(creds.password, password, sizeof(creds.password) - 1);
creds.password[sizeof(creds.password) - 1] = '\0';
uint32_t ints = save_and_disable_interrupts();
flash_range_erase(FLASH_TARGET_OFFSET, FLASH_SECTOR_SIZE);
flash_range_program(FLASH_TARGET_OFFSET, (const uint8_t*)&creds, sizeof(creds));
restore_interrupts(ints);
FlashWriteParams params = { (const uint8_t*)&creds, sizeof(creds) };
int rc = flash_safe_execute(do_flash_write, &params, 1000);
printf("WiFi credentials saved to flash.\n");
if (rc == PICO_OK) {
printf("WiFi credentials saved to flash.\n");
} else {
printf("Failed to save WiFi credentials: %d\n", rc);
}
return rc;
}
bool wifi_try_auto_connect() {
// On RP2040/RP2350, flash is memory-mapped at XIP_BASE (0x10000000).
// We can read from it directly like a normal pointer without special API calls.
// The hardware XIP controller handles fetching data from the flash chip.
const WifiCreds* creds = (const WifiCreds*)(XIP_BASE + FLASH_TARGET_OFFSET);
// Check for the magic number to verify valid data exists.
// Erased flash reads as 0xFFFFFFFF, so this check fails if no data was saved.
if (creds->magic != WIFI_CREDS_MAGIC) {
printf("No saved WiFi credentials found.\n");
return false;