Add serial upload tool for rapid Lua game iteration

Implements a complete serial upload workflow that allows uploading and
immediately testing Lua games via USB serial connection.

New Components:
- SerialUploader: Receives files via serial, writes to SD card
- upload_game.py: Python tool for sending files from host computer
- Protocol: Text-based with base64 encoding for reliability

Key Features:
- Uploads file to /games folder on SD card
- Overwrites existing files (FA_CREATE_ALWAYS)
- Auto-launches uploaded game immediately
- Proper memory cleanup (prevents Lua state conflicts)

SD Card Fixes:
- Fixed SPI speed management (12.5MHz for SD, 32MHz for display)
- Fixed SD write protocol (poll for data response token)
- Added speed switching wrappers around all FatFS operations
- Cleaned up excessive debug output

Game Launcher Improvements:
- Added clear_games() to prevent duplicate registrations
- Added cleanup in select_game_by_name() to delete old instances
- Added exact match priority in game selection
- LuaGameLoader now has clear_factory_data() for memory cleanup

Integration:
- Added serial_uploader to CMakeLists.txt
- Integrated into main loop in basic1.cpp
- Re-scans games after upload to pick up new files

Documentation:
- UPLOAD_TOOL.md: Usage instructions
- sd_card_best_practices.md: Critical lessons learned

Known Issues:
- Game launch after upload occasionally causes freeze (needs investigation)
- Display may not refresh properly after upload

Usage:
  python upload_game.py games/lua_examples/2048.lua /dev/tty.usbmodem101

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Adolfo Reyna
2026-02-12 22:52:57 -05:00
parent b26f3bf775
commit 84b009c33e
14 changed files with 1222 additions and 34 deletions

View File

@@ -6,6 +6,8 @@
#include "diskio.h"
#include "sd_card.h"
#include <string.h>
#include <stdio.h>
#include <stdbool.h>
/* Definitions of physical drive number for each drive */
#define DEV_SD 0 /* SD card */
@@ -19,9 +21,9 @@ DSTATUS disk_status (
)
{
if (pdrv != DEV_SD) return STA_NOINIT;
// Assume SD card is always initialized after sd_card_init() is called
return 0; // OK
return 0; // OK - not write protected, not removed
}
/*-----------------------------------------------------------------------*/
@@ -74,13 +76,16 @@ DRESULT disk_write (
)
{
if (pdrv != DEV_SD) return RES_PARERR;
for (UINT i = 0; i < count; i++) {
if (!sd_card_write_block(sector + i, (uint8_t*)(buff + (i * 512)))) {
bool result = sd_card_write_block(sector + i, (uint8_t*)(buff + (i * 512)));
if (!result) {
printf("ERROR disk_write failed at sector %lu\n", (unsigned long)(sector + i));
return RES_ERROR;
}
}
return RES_OK;
}
@@ -97,15 +102,15 @@ DRESULT disk_ioctl (
)
{
if (pdrv != DEV_SD) return RES_PARERR;
DRESULT res = RES_ERROR;
switch (cmd) {
case CTRL_SYNC:
// Complete pending write process (if needed)
res = RES_OK;
break;
case GET_SECTOR_COUNT:
// Get number of sectors on the disk (DWORD)
{
@@ -118,22 +123,27 @@ DRESULT disk_ioctl (
}
}
break;
case GET_SECTOR_SIZE:
// Get sector size (WORD)
*(WORD*)buff = 512;
res = RES_OK;
break;
case GET_BLOCK_SIZE:
// Get erase block size in unit of sector (DWORD)
*(DWORD*)buff = 1; // Single sector erase
res = RES_OK;
break;
case CTRL_TRIM:
// Inform device that data on the block of sectors is no longer used (optional)
res = RES_OK;
break;
default:
res = RES_PARERR;
}
return res;
}