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.h"
#include "lua_bindings.h"
#include "sd_card.h"
#include <stdio.h>
#include <string.h>
@@ -54,9 +55,13 @@ bool LuaGame::load_script() {
FIL fil;
FRESULT fr;
// Set SPI speed for SD card operations
uint prev_speed = sd_card_set_spi_speed();
// Open Lua script from SD card
fr = f_open(&fil, script_path.c_str(), FA_READ);
if (fr != FR_OK) {
sd_card_restore_spi_speed(prev_speed);
error_message = "Failed to open file (FatFS error: ";
error_message += std::to_string((int)fr);
error_message += ")";
@@ -67,6 +72,7 @@ bool LuaGame::load_script() {
FSIZE_t file_size = f_size(&fil);
if (file_size == 0 || file_size > 64 * 1024) { // Limit to 64KB
f_close(&fil);
sd_card_restore_spi_speed(prev_speed);
error_message = "Script file size invalid (0 or > 64KB)";
return false;
}
@@ -75,6 +81,7 @@ bool LuaGame::load_script() {
char* script_buffer = new char[file_size + 1];
if (!script_buffer) {
f_close(&fil);
sd_card_restore_spi_speed(prev_speed);
error_message = "Failed to allocate memory for script";
return false;
}
@@ -84,6 +91,9 @@ bool LuaGame::load_script() {
fr = f_read(&fil, script_buffer, file_size, &bytes_read);
f_close(&fil);
// Restore SPI speed immediately after file operations
sd_card_restore_spi_speed(prev_speed);
if (fr != FR_OK || bytes_read != file_size) {
delete[] script_buffer;
error_message = "Failed to read script file";