Fix SD card integration and Lua game loading

- Fix SD card MISO pin (was using display MISO instead of SD MISO)
- Add FatFS mounting after SD card initialization
- Restore SPI baudrate on all SD init failure paths
- Add case-insensitive .lua file extension check
- Filter out hidden 8.3 filename entries
- Add SPI speed management functions for shared SPI bus
- Wrap all FatFS operations with SPI speed switching
- Restore display SPI speed (32 MHz) after SD operations
- Add debug output to Lua game loader

This fixes slow display refresh when SD card is present and enables
reliable Lua game loading from SD card /games directory.
This commit is contained in:
Adolfo Reyna
2026-02-07 19:31:38 -05:00
parent 2a472fc29f
commit b16211f148
109 changed files with 38964 additions and 20 deletions

View File

@@ -40,6 +40,9 @@
#include "pico/multicore.h"
#include "board_config.h" // Board-specific pin configuration
#include "sd_card.h"
extern "C" {
#include "ff.h" // FatFS
}
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
@@ -364,10 +367,33 @@ int main()
return new DemoGame(w, h, r, g, im);
});
// Register Lua games from SD card /games directory
printf("\nScanning for Lua games on SD card...\n");
int lua_games_found = LuaGameLoader::register_all_games(&launcher);
printf("Found %d Lua game(s)\n", lua_games_found);
// Initialize SD card and mount filesystem
printf("\nInitializing SD card...\n");
bool sd_available = sd_card_init_with_board_config();
if (sd_available) {
printf("SD card initialized successfully\n");
// Mount FatFS filesystem
static FATFS fs;
FRESULT res = f_mount(&fs, "0:", 1); // Mount drive 0 immediately
if (res == FR_OK) {
printf("FatFS mounted successfully\n");
// Register Lua games from SD card /games directory
printf("Scanning for Lua games on SD card...\n");
int lua_games_found = LuaGameLoader::register_all_games(&launcher);
printf("Found %d Lua game(s)\n", lua_games_found);
} else {
printf("FatFS mount failed (error %d) - SD card may not be formatted\n", res);
}
// Restore display SPI speed (SD card shares same SPI bus)
// SD card init sets SPI to 12.5 MHz, but display needs 32 MHz for fast refresh
spi_set_baudrate(DISPLAY_SPI_PORT, SPI_BAUDRATE);
printf("Display SPI speed restored to %d MHz\n", SPI_BAUDRATE / 1000000);
} else {
printf("SD card not available - skipping Lua game scan\n");
}
// Draw launcher menu
launcher.draw();
@@ -406,13 +432,6 @@ int main()
printf("Button interrupts enabled (falling edge = press)\n");
#endif
// Test SD card and FatFS
// if (sd_card_init_with_board_config()) {
// sd_card_test_fatfs();
// } else {
// printf("SD Card initialization failed or no card present\n");
// }
// ========================================================================
// REACTIVE GAME LOOP WITH DUAL-CORE REFRESH
// ========================================================================
@@ -491,7 +510,7 @@ int main()
}
} else if (input.type == INPUT_TOUCH_UP) {
uint32_t now = to_ms_since_boot(get_absolute_time());
if (game_start_time > 0 && (now - game_start_time) > 2000) {
if (game_start_time > 0 && (now - game_start_time) > 10000) {
// Long press detected - return to menu
printf("Long press detected - returning to launcher\n");
launcher.reset();