first commit

This commit is contained in:
2025-11-23 11:46:56 +01:00
commit 3ea3d26ee4
25 changed files with 3227 additions and 0 deletions
+17
View File
@@ -0,0 +1,17 @@
idf_component_register(
SRCS
"main.c"
INCLUDE_DIRS
"."
REQUIRES
driver
esp_psram
nvs_flash
fatfs
spi_flash
gnuboy
st7789
nfc_manager
link_cable
potentiometer_manager
)
+220
View File
@@ -0,0 +1,220 @@
/**
* @file hardware_config.h
* @brief Hardware pin configuration for Waveshare ESP32-S3-Touch-LCD-2
*
* This file contains ALL pin definitions for the LEGO GameBoy project.
* Centralized configuration - change pins here only!
*
* Hardware: Waveshare ESP32-S3-Touch-LCD-2 (2.0" ST7789 240x320)
* - ESP32-S3-WROOM-1 module
* - 16MB Flash, 8MB PSRAM
* - 2.0" ST7789 TFT Display (240x320)
* - Integrated touch controller
*/
#pragma once
#ifdef __cplusplus
extern "C" {
#endif
// ============================================
// DISPLAY PINS (ST7789 - 2.0" 240x320)
// SPI Interface
// ============================================
#define LCD_SPI_HOST SPI2_HOST
#define LCD_PIXEL_CLOCK_HZ (40 * 1000 * 1000) // 40MHz
#define LCD_PIN_MOSI 11 // SPI MOSI (DIN)
#define LCD_PIN_MISO 13 // SPI MISO (nicht verwendet bei Display)
#define LCD_PIN_SCLK 12 // SPI Clock
#define LCD_PIN_CS 10 // Chip Select
#define LCD_PIN_DC 8 // Data/Command
#define LCD_PIN_RST 14 // Reset
#define LCD_PIN_BCKL 9 // Backlight (PWM capable)
// Display specs
#define LCD_WIDTH 240
#define LCD_HEIGHT 320
#define LCD_ROTATION 0 // 0, 1, 2, or 3 (0 = Portrait)
// Backlight PWM
#define LCD_BCKL_DEFAULT 80 // 0-100%
#define LCD_BCKL_MIN 10
#define LCD_BCKL_MAX 100
// ============================================
// TOUCH CONTROLLER PINS (CST816S)
// I2C Interface
// ============================================
#define TOUCH_I2C_NUM I2C_NUM_0
#define TOUCH_I2C_SCL 6
#define TOUCH_I2C_SDA 5
#define TOUCH_I2C_INT 7 // Touch interrupt (active low)
#define TOUCH_I2C_RST -1 // Optional reset (not used)
#define TOUCH_I2C_ADDR 0x15 // CST816S address
#define TOUCH_I2C_FREQ_HZ 400000 // 400kHz
// ============================================
// SD CARD PINS (SPI - shares with Display)
// ============================================
#define SD_SPI_HOST LCD_SPI_HOST // Same SPI bus as display!
#define SD_PIN_MOSI LCD_PIN_MOSI
#define SD_PIN_MISO LCD_PIN_MISO
#define SD_PIN_SCLK LCD_PIN_SCLK
#define SD_PIN_CS 4 // Separate CS from display!
// ============================================
// AUDIO PINS (I2S - MAX98357A)
// ============================================
#define I2S_NUM I2S_NUM_0
#define I2S_SAMPLE_RATE 32000 // Hz (GameBoy: 32kHz)
#define I2S_BITS_PER_SAMPLE 16
#define I2S_PIN_BCLK 41 // Bit Clock
#define I2S_PIN_LRC 42 // Left/Right Clock (WS)
#define I2S_PIN_DIN 40 // Data In (to MAX98357A)
// No MCLK needed for MAX98357A!
#define I2S_DMA_BUF_COUNT 8
#define I2S_DMA_BUF_LEN 1024
// ============================================
// NFC READER PINS (PN532)
// I2C Interface (separate bus from touch!)
// ============================================
#define NFC_I2C_NUM I2C_NUM_1
#define NFC_I2C_SCL 16 // I2C Clock
#define NFC_I2C_SDA 15 // I2C Data
#define NFC_I2C_INT -1 // Optional interrupt
#define NFC_I2C_RST -1 // Optional reset
#define NFC_I2C_ADDR 0x24 // PN532 I2C address
#define NFC_I2C_FREQ_HZ 100000 // 100kHz (PN532 spec)
// ============================================
// GAMEBOY BUTTON PINS
// All inputs with pull-up
// ============================================
#define BTN_UP 1
#define BTN_DOWN 2
#define BTN_LEFT 42
#define BTN_RIGHT 41
#define BTN_A 21
#define BTN_B 47
#define BTN_START 48
#define BTN_SELECT 45
// Button configuration
#define BTN_ACTIVE_LEVEL 0 // Buttons pull to GND (active LOW)
#define BTN_DEBOUNCE_MS 50 // Debounce time
// ============================================
// POTENTIOMETER PINS (ADC)
// ============================================
#define POTI_VOLUME_PIN 3 // ADC1_CH2 - Volume control
#define POTI_BRIGHT_PIN 46 // ADC2_CH5 - Brightness control
#define POTI_ADC_WIDTH ADC_WIDTH_BIT_12 // 12-bit (0-4095)
#define POTI_ADC_ATTEN ADC_ATTEN_DB_11 // 0-3.3V range
// ============================================
// LINK CABLE PINS (GPIO for 2-Player)
// ============================================
#define LINK_GPIO_SCLK 17 // Serial Clock (bidirectional)
#define LINK_GPIO_SOUT 18 // Serial Out (data to other GB)
#define LINK_GPIO_SIN 38 // Serial In (data from other GB)
// Link Cable Settings
#define LINK_CLOCK_FREQ 8192 // Hz (GameBoy standard)
#define LINK_BYTE_TIME_US 122 // Microseconds per byte
#define LINK_BIT_TIME_US 15 // Microseconds per bit
// ============================================
// STATUS LED (optional indicator)
// ============================================
#define LED_STATUS_PIN 39 // Status LED (Link active, etc.)
#define LED_ACTIVE_LEVEL 1 // Active HIGH
// ============================================
// POWER MANAGEMENT (optional)
// ============================================
#define BATTERY_ADC_PIN -1 // Not used yet
#define POWER_ENABLE_PIN -1 // Not used yet
// ============================================
// DEBUG/UART (Serial Console)
// ============================================
#define UART_NUM UART_NUM_0
#define UART_TX_PIN 43
#define UART_RX_PIN 44
#define UART_BAUD_RATE 115200
// ============================================
// GPIO SUMMARY (for reference/debugging)
// ============================================
/*
WAVESHARE ESP32-S3-TOUCH-LCD-2 PIN ALLOCATION:
GPIO | Function | Direction | Notes
------|-------------------|-----------|------------------
1 | BTN_UP | Input | Pull-up
2 | BTN_DOWN | Input | Pull-up
3 | POTI_VOLUME | Input | ADC1_CH2
4 | SD_CS | Output | SPI
5 | TOUCH_SDA | I/O | I2C0
6 | TOUCH_SCL | Output | I2C0
7 | TOUCH_INT | Input | Interrupt
8 | LCD_DC | Output | SPI
9 | LCD_BCKL | Output | PWM
10 | LCD_CS | Output | SPI
11 | LCD_MOSI | Output | SPI
12 | LCD_SCLK | Output | SPI
13 | LCD_MISO | Input | SPI (not used)
14 | LCD_RST | Output | Reset
15 | NFC_SDA | I/O | I2C1
16 | NFC_SCL | Output | I2C1
17 | LINK_SCLK | I/O | Link Cable
18 | LINK_SOUT | Output | Link Cable
21 | BTN_A | Input | Pull-up
38 | LINK_SIN | Input | Link Cable
39 | LED_STATUS | Output | Status indicator
40 | I2S_DIN | Output | Audio
41 | BTN_RIGHT/I2S_BCLK| I/O | Shared!
42 | BTN_LEFT/I2S_LRC | I/O | Shared!
43 | UART_TX | Output | Debug
44 | UART_RX | Input | Debug
45 | BTN_SELECT | Input | Pull-up
46 | POTI_BRIGHT | Input | ADC2_CH5
47 | BTN_B | Input | Pull-up
48 | BTN_START | Input | Pull-up
NOTES:
- GPIO 41/42 are SHARED between buttons and I2S!
→ Configure carefully or use different pins
- SPI bus is shared: Display + SD Card
→ Use separate CS pins!
- I2C0: Touch controller
- I2C1: NFC reader (separate bus!)
- All buttons have internal pull-ups enabled
*/
// ============================================
// HARDWARE VALIDATION MACROS
// ============================================
#define VALIDATE_PIN(pin) ((pin) >= 0 && (pin) <= 48)
// Check for pin conflicts at compile time
#if (BTN_RIGHT == I2S_PIN_BCLK)
#warning "BTN_RIGHT and I2S_BCLK share GPIO 41!"
#endif
#if (BTN_LEFT == I2S_PIN_LRC)
#warning "BTN_LEFT and I2S_LRC share GPIO 42!"
#endif
#ifdef __cplusplus
}
#endif
+185
View File
@@ -0,0 +1,185 @@
/**
* @file main.c
* @brief ESP32-S3 GNUBoy Emulator - Main Entry Point
*
* LEGO GameBoy Emulator Project
* Hardware: Waveshare ESP32-S3-Touch-LCD-2
*
* Features:
* - GameBoy/GameBoy Color emulation
* - NFC ROM selection
* - Potentiometer volume/brightness control
* - Link Cable 2-player support
* - SD Card ROM loading
*/
#include <stdio.h>
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_system.h"
#include "esp_log.h"
#include "esp_err.h"
#include "nvs_flash.h"
#include "esp_psram.h"
#include "hardware_config.h"
// Component includes (will be created)
// #include "st7789.h"
// #include "nfc_manager.h"
// #include "link_cable.h"
// #include "potentiometer_manager.h"
// #include "gnuboy.h"
static const char *TAG = "MAIN";
/**
* @brief Callback for potentiometer changes
*/
static void poti_change_handler(uint8_t volume, uint8_t brightness)
{
ESP_LOGI(TAG, "Potentiometer changed: Volume=%d%%, Brightness=%d%%",
volume, brightness);
// TODO: Set audio volume when audio is implemented
// audio_set_volume(volume);
}
/**
* @brief Initialize NVS (Non-Volatile Storage)
*/
static void init_nvs(void)
{
ESP_LOGI(TAG, "Initializing NVS...");
esp_err_t ret = nvs_flash_init();
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
ESP_LOGW(TAG, "NVS partition was truncated, erasing...");
ESP_ERROR_CHECK(nvs_flash_erase());
ret = nvs_flash_init();
}
ESP_ERROR_CHECK(ret);
ESP_LOGI(TAG, "✓ NVS initialized");
}
/**
* @brief Initialize PSRAM
*/
static void init_psram(void)
{
ESP_LOGI(TAG, "Checking PSRAM...");
if (esp_psram_is_initialized()) {
size_t psram_size = esp_psram_get_size();
ESP_LOGI(TAG, "✓ PSRAM initialized: %d MB", psram_size / (1024 * 1024));
} else {
ESP_LOGE(TAG, "✗ PSRAM not available!");
ESP_LOGE(TAG, " Make sure PSRAM is enabled in sdkconfig!");
}
}
/**
* @brief Print system information
*/
static void print_system_info(void)
{
ESP_LOGI(TAG, "");
ESP_LOGI(TAG, "╔════════════════════════════════════════════════════════╗");
ESP_LOGI(TAG, "║ ║");
ESP_LOGI(TAG, "║ ESP32-S3 GNUBoy LEGO GameBoy Emulator ║");
ESP_LOGI(TAG, "║ ║");
ESP_LOGI(TAG, "╚════════════════════════════════════════════════════════╝");
ESP_LOGI(TAG, "");
ESP_LOGI(TAG, "Hardware: Waveshare ESP32-S3-Touch-LCD-2");
ESP_LOGI(TAG, "Display: ST7789 2.0\" 240x320");
ESP_LOGI(TAG, "Flash: %d MB", spi_flash_get_chip_size() / (1024 * 1024));
if (esp_psram_is_initialized()) {
ESP_LOGI(TAG, "PSRAM: %d MB", esp_psram_get_size() / (1024 * 1024));
}
ESP_LOGI(TAG, "");
ESP_LOGI(TAG, "Features:");
ESP_LOGI(TAG, " ✓ GameBoy / GameBoy Color emulation");
ESP_LOGI(TAG, " ✓ NFC ROM selection");
ESP_LOGI(TAG, " ✓ Potentiometer controls");
ESP_LOGI(TAG, " ✓ Link Cable 2-player");
ESP_LOGI(TAG, " ✓ SD Card ROM loading");
ESP_LOGI(TAG, "");
}
/**
* @brief Initialize hardware components
*/
static void init_hardware(void)
{
ESP_LOGI(TAG, "Initializing hardware components...");
// TODO: Initialize display (ST7789)
// st7789_init();
ESP_LOGI(TAG, " [ ] Display (ST7789) - TODO");
// TODO: Initialize buttons
// buttons_init();
ESP_LOGI(TAG, " [ ] Buttons - TODO");
// TODO: Initialize audio (I2S)
// audio_init();
ESP_LOGI(TAG, " [ ] Audio (I2S) - TODO");
// TODO: Initialize SD Card
// sd_card_init();
ESP_LOGI(TAG, " [ ] SD Card - TODO");
// TODO: Initialize NFC
// nfc_manager_init();
ESP_LOGI(TAG, " [ ] NFC Reader - TODO");
// TODO: Initialize Link Cable
// link_cable_init();
ESP_LOGI(TAG, " [ ] Link Cable - TODO");
// TODO: Initialize Potentiometers
// poti_manager_init();
// poti_manager_start(poti_change_handler);
ESP_LOGI(TAG, " [ ] Potentiometers - TODO");
ESP_LOGI(TAG, "✓ Hardware initialization complete");
}
/**
* @brief Main application entry point
*/
void app_main(void)
{
// Print welcome banner
print_system_info();
// Initialize core systems
init_nvs();
init_psram();
// Initialize hardware peripherals
init_hardware();
ESP_LOGI(TAG, "");
ESP_LOGI(TAG, "════════════════════════════════════════════════════════");
ESP_LOGI(TAG, "System initialization complete!");
ESP_LOGI(TAG, "════════════════════════════════════════════════════════");
ESP_LOGI(TAG, "");
// TODO: Start emulator
ESP_LOGI(TAG, "Starting GNUBoy emulator...");
ESP_LOGI(TAG, " (GNUBoy core not yet integrated)");
// Main loop
ESP_LOGI(TAG, "Entering main loop...");
while (1) {
// TODO: Run emulator main loop
// gnuboy_run_frame();
vTaskDelay(pdMS_TO_TICKS(100));
}
}