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

@@ -5,6 +5,7 @@
#include "lua_game_loader.h"
#include "lua_game.h"
#include "sd_card.h"
#include <stdio.h>
#include <string.h>
#include <vector>
@@ -51,10 +52,14 @@ bool LuaGameLoader::parse_metadata(const char* script_path, char* name, char* de
// Default empty description
description[0] = '\0';
// Set SPI speed for SD card operations
uint prev_speed = sd_card_set_spi_speed();
// Try to open file and parse metadata comments
fr = f_open(&fil, script_path, FA_READ);
if (fr != FR_OK) {
printf("LuaGameLoader: Warning - could not open %s for metadata\n", script_path);
sd_card_restore_spi_speed(prev_speed);
return false;
}
@@ -64,6 +69,9 @@ bool LuaGameLoader::parse_metadata(const char* script_path, char* name, char* de
fr = f_read(&fil, buffer, sizeof(buffer) - 1, &bytes_read);
f_close(&fil);
// Restore SPI speed
sd_card_restore_spi_speed(prev_speed);
if (fr != FR_OK) {
return false;
}
@@ -109,25 +117,60 @@ int LuaGameLoader::register_all_games(GameLauncher* launcher) {
printf("LuaGameLoader: Scanning /games directory for .lua scripts...\n");
// Set SPI speed for SD card operations
uint prev_speed = sd_card_set_spi_speed();
// Open /games directory
fr = f_opendir(&dir, "/games");
if (fr != FR_OK) {
printf("LuaGameLoader: Could not open /games directory (error %d)\n", fr);
printf("LuaGameLoader: Make sure SD card is mounted and /games exists\n");
sd_card_restore_spi_speed(prev_speed);
return 0;
}
// Scan for .lua files
while (true) {
fr = f_readdir(&dir, &fno);
if (fr != FR_OK || fno.fname[0] == 0) break; // End of directory
if (fr != FR_OK) {
printf("LuaGameLoader: Error reading directory (error %d)\n", fr);
break;
}
if (fno.fname[0] == 0) {
printf("LuaGameLoader: End of directory reached\n");
break; // End of directory
}
printf("LuaGameLoader: Found entry: %s (attrib=0x%02X)\n", fno.fname, fno.fattrib);
// Skip directories
if (fno.fattrib & AM_DIR) continue;
if (fno.fattrib & AM_DIR) {
printf("LuaGameLoader: Skipping directory\n");
continue;
}
// Check for .lua extension
// Skip hidden files (these are short 8.3 filename entries)
if (fno.fattrib & AM_HID) {
printf("LuaGameLoader: Skipping hidden file (short filename entry)\n");
continue;
}
// Check for .lua extension (case-insensitive)
size_t len = strlen(fno.fname);
if (len < 5 || strcmp(fno.fname + len - 4, ".lua") != 0) continue;
printf("LuaGameLoader: Filename length: %d\n", len);
if (len < 5) {
printf("LuaGameLoader: Filename too short\n");
continue;
}
// Case-insensitive check for .lua or .LUA
const char* ext = fno.fname + len - 4;
if (strcasecmp(ext, ".lua") != 0) {
printf("LuaGameLoader: Not a .lua file (ext=%s)\n", ext);
continue;
}
printf("LuaGameLoader: Valid .lua file!\n");
// Build full path
char script_path[256];
@@ -160,6 +203,9 @@ int LuaGameLoader::register_all_games(GameLauncher* launcher) {
f_closedir(&dir);
// Restore SPI speed
sd_card_restore_spi_speed(prev_speed);
printf("LuaGameLoader: Registered %d Lua games\n", count);
return count;
}