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
+36 -16
View File
@@ -2,6 +2,8 @@
#include "wifi_manager.h"
#include "pico/stdlib.h"
#include "pico/multicore.h"
#include "pico/flash.h"
#include "pico/util/queue.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
@@ -31,6 +33,10 @@ static bool g_force_full_refresh = false; // Flag to force full refresh when li
// Synchronization flag for multicore initialization
static volatile bool g_display_ready = false;
// Queue for display messages (replacing raw FIFO to avoid conflict with flash_safe_execute)
static queue_t g_display_queue;
#define QUEUE_LENGTH 8
// Display update message structure for inter-core communication
typedef struct {
EntryList entries;
@@ -84,9 +90,18 @@ static void init_epaper_display() {
EPD_7IN5B_V2_Display_Partial(g_epd_image, 0, 0, EPD_7IN5B_V2_WIDTH, EPD_7IN5B_V2_HEIGHT);
EPD_7IN5B_V2_Display_Partial(g_epd_image, 0, 0, EPD_7IN5B_V2_WIDTH, EPD_7IN5B_V2_HEIGHT);
printf("Trying to connect to wifi\r\n");
bool connected = wifi_try_auto_connect();
Paint_SelectImage(g_epd_red);
printf("Drawing header\r\n");
Paint_DrawString_EN(10, 10, "What's new today:", &Font24, WHITE, RED);
if (connected) {
Paint_DrawString_EN(10, 10, "What's new today: ...", &Font24, WHITE, RED);
} else {
Paint_DrawString_EN(10, 10, "What's new today:", &Font24, WHITE, RED);
}
// Display the image with both black and red buffers
EPD_7IN5B_V2_Display_Partial(g_epd_red, 0, 0, EPD_7IN5B_V2_WIDTH, EPD_7IN5B_V2_HEIGHT);
@@ -98,6 +113,9 @@ static void init_epaper_display() {
* Core 1 function: Runs display initialization and update handling
*/
static void core1_display_init() {
// Initialize flash safe execute support for this core
flash_safe_execute_core_init();
// Initialize WiFi on Core 1 so IRQs are handled here
wifi_init();
@@ -109,20 +127,18 @@ static void core1_display_init() {
// Initialize for partial refresh on second call
EPD_7IN5B_V2_Init_Part();
// Core 1 main loop: handle display updates via FIFO
// Core 1 main loop: handle display updates via Queue
while (true) {
// Check if there's a message from core 0
if (multicore_fifo_rvalid()) {
// Read the message pointer
uint32_t msg_addr = multicore_fifo_pop_blocking();
DisplayMessage *msg = (DisplayMessage *)msg_addr;
// Drain FIFO to get the latest message
while (multicore_fifo_rvalid()) {
DisplayMessage *msg;
// Check if there's a message in the queue
if (queue_try_remove(&g_display_queue, &msg)) {
// Drain Queue to get the latest message
DisplayMessage *next_msg;
while (queue_try_remove(&g_display_queue, &next_msg)) {
printf("[Core 1] Skipping intermediate update\n");
free(msg); // Free the stale message
msg_addr = multicore_fifo_pop_blocking();
msg = (DisplayMessage *)msg_addr;
msg = next_msg;
}
printf("[Core 1] Updating display with %d entries\n", msg->entries.count);
@@ -173,12 +189,16 @@ static void core1_display_init() {
}
void epaper_start_background_thread() {
// Initialize the queue before starting the thread
queue_init(&g_display_queue, sizeof(DisplayMessage*), QUEUE_LENGTH);
printf("Launching e-Paper display init on core 1...\n");
multicore_launch_core1(core1_display_init);
}
void epaper_send_update(const char *entry, bool finish_line) {
if (!g_display_ready) return; // Don't send if display isn't ready
// printf("[Core 0] Preparing display update: '%s' (finish_line=%d)\n", entry, finish_line);
// Check if list is at capacity
if (g_entry_list.count >= MAX_ENTRIES) {
@@ -224,11 +244,11 @@ void epaper_send_update(const char *entry, bool finish_line) {
msg->yend = msg->ystart + 25;
}
// Send message pointer to core 1 if FIFO has space
if (multicore_fifo_wready()) {
multicore_fifo_push_blocking((uint32_t)msg);
// Send message pointer to core 1 via Queue
if (queue_try_add(&g_display_queue, &msg)) {
// printf("[Core 0] Display update sent to core 1\n");
} else {
printf("[Core 0] FIFO full, skipping display update\n");
printf("[Core 0] Queue full, skipping display update\n");
free(msg);
}