lego-esp32s3-gameboy/main/include/hardware_config.h

244 lines
8.7 KiB
C

/**
* @file hardware_config.h
* @brief Hardware pin configuration for Waveshare ESP32-S3-Touch-LCD-2
*
* CORRECTED with proper pin assignments!
*/
#pragma once
#ifdef __cplusplus
extern "C" {
#endif
// ============================================
// DISPLAY PINS (ST7789) - VERIFIED WORKING
// ============================================
#define LCD_SPI_HOST SPI2_HOST
#define LCD_PIXEL_CLOCK_HZ (80 * 1000 * 1000) // ST7789 supports up to 80MHz
#define LCD_PIN_MOSI 38
#define LCD_PIN_MISO 40
#define LCD_PIN_SCLK 39
#define LCD_PIN_CS 45
#define LCD_PIN_DC 42
#define LCD_PIN_RST -1
#define LCD_PIN_BCKL 1
#define LCD_WIDTH 320
#define LCD_HEIGHT 240
#define LCD_ROTATION 3
#define LCD_BCKL_DEFAULT 80
#define LCD_BCKL_MIN 10
#define LCD_BCKL_MAX 100
// ============================================
// GAMEBOY DISPLAY SCALING OPTIONS
// ============================================
// Set to 1 for scaled display with black borders
// Set to 0 for full screen stretch (320x240)
#define GB_PIXEL_PERFECT_SCALING 1
// Scaling factor (float multiplier for GameBoy resolution)
// Valid range: 1.0 to 2.0
// Examples:
// 1.4 = 160x144 -> 224x202 (smaller, faster performance)
// 1.5 = 160x144 -> 240x216 (balanced)
// 1.6 = 160x144 -> 256x230 (current, excellent balance)
// 1.67 = 160x144 -> 267x240 (full height, max size)
// 2.0 = 160x144 -> 320x288 (exceeds screen height, will be clipped)
//
// Performance tip: Smaller scaling = fewer pixels = higher FPS
// Can be changed on-the-fly per game (future menu feature)
#define GB_SCALE_FACTOR 1.5
#if GB_PIXEL_PERFECT_SCALING
// Calculate scaled dimensions dynamically from GB_SCALE_FACTOR
// GameBoy native resolution: 160x144
#define GB_RENDER_WIDTH ((int)(160 * GB_SCALE_FACTOR))
#define GB_RENDER_HEIGHT ((int)(144 * GB_SCALE_FACTOR))
// Center the GameBoy screen with black borders
#define GB_OFFSET_X ((320 - GB_RENDER_WIDTH) / 2)
#define GB_OFFSET_Y ((240 - GB_RENDER_HEIGHT) / 2)
// Frame buffer is full screen to hold black borders
#define GB_SCREEN_WIDTH 320
#define GB_SCREEN_HEIGHT 240
#else
// Full screen: 160x144 -> 320x240 (stretch mode)
#define GB_RENDER_WIDTH 320
#define GB_RENDER_HEIGHT 240
#define GB_OFFSET_X 0
#define GB_OFFSET_Y 0
#define GB_SCREEN_WIDTH 320
#define GB_SCREEN_HEIGHT 240
#endif
// ============================================
// SD CARD PINS - VERIFIED WORKING
// ============================================
#define SD_SPI_HOST LCD_SPI_HOST
#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 41
// ============================================
// TOUCH CONTROLLER PINS (CST816S)
// ============================================
#define TOUCH_I2C_NUM I2C_NUM_0
#define TOUCH_I2C_SCL 6
#define TOUCH_I2C_SDA 5
#define TOUCH_I2C_INT 7
#define TOUCH_I2C_RST -1
#define TOUCH_I2C_ADDR 0x15
#define TOUCH_I2C_FREQ_HZ 400000
// ============================================
// AUDIO PINS (I2S - MAX98357A)
// ============================================
#define I2S_NUM I2S_NUM_0
#define I2S_SAMPLE_RATE 32768 // GameBoy native sample rate
#define I2S_BITS_PER_SAMPLE 16
#define I2S_PIN_BCLK 48 // Available on right header pin 6
#define I2S_PIN_LRC 47 // Available on right header pin 5
#define I2S_PIN_DIN 16 // Your original wiring (conflicts resolved by NFC move)
#define I2S_DMA_BUF_COUNT 8
#define I2S_DMA_BUF_LEN 1024
// ============================================
// GAMEBOY BUTTON PINS - REMAPPED TO FREE GPIOS!
// ============================================
#define BTN_UP 8 // D-Pad Up
#define BTN_DOWN 9 // D-Pad Down
#define BTN_LEFT 10 // D-Pad Left
#define BTN_RIGHT 11 // D-Pad Right
#define BTN_A 12 // Action Button A
#define BTN_B 13 // Action Button B
#define BTN_START 14 // Start Button
#define BTN_SELECT 21 // Select Button
#define BTN_ACTIVE_LEVEL 0 // Active LOW (buttons pull to GND)
#define BTN_DEBOUNCE_MS 50 // Debounce time
// ============================================
// POWER SWITCH (Hardware Toggle Switch)
// ============================================
#define POWER_SWITCH_PIN 0 // GPIO0 - Hardware power switch
#define POWER_SWITCH_ON 0 // Switch closed = GND = Device ON
#define POWER_SWITCH_OFF 1 // Switch open = Pull-up = Device goes to sleep
// Power switch behavior:
// - When switch is CLOSED (pin reads LOW): Device runs normally
// - When switch is OPENED (pin reads HIGH): Device enters deep-sleep
// - When switch is CLOSED again: ESP32 wakes up from deep-sleep
//
// Implementation:
// - Configure GPIO0 with internal pull-up
// - Monitor pin state in main loop
// - On transition LOW->HIGH: Enter esp_deep_sleep_start()
// - ESP32 will wake on GPIO LOW (RTC_GPIO wakeup)
#define POWER_SWITCH_CHECK_MS 100 // Check switch state every 100ms
// ============================================
// POTENTIOMETER PINS (ADC)
// ============================================
#define POTI_VOLUME_PIN 3 // ADC1_CH2 - Volume control
#define POTI_BRIGHT_PIN 4 // ADC1_CH3 - Brightness control
#define POTI_ADC_WIDTH ADC_WIDTH_BIT_12
#define POTI_ADC_ATTEN ADC_ATTEN_DB_11
// ============================================
// NFC READER PINS (PN532) - Shared I2C Bus with Touch
// ============================================
#define NFC_I2C_NUM I2C_NUM_0 // SHARED with Touch Controller!
#define NFC_I2C_SCL TOUCH_I2C_SCL // GPIO 6 (shared)
#define NFC_I2C_SDA TOUCH_I2C_SDA // GPIO 5 (shared)
#define NFC_I2C_INT -1 // Optional interrupt
#define NFC_I2C_RST -1 // Optional reset
#define NFC_I2C_ADDR 0x24 // Different I2C address than Touch (0x15)
#define NFC_I2C_FREQ_HZ 100000
// ============================================
// LINK CABLE PINS (GPIO for 2-Player) - Optional
// ============================================
#define LINK_GPIO_SCLK 15 // Free GPIO (NFC moved to I2C)
#define LINK_GPIO_SOUT 2 // Free GPIO on left header
#define LINK_GPIO_SIN 17 // Free GPIO
#define LINK_CLOCK_FREQ 8192
#define LINK_BYTE_TIME_US 122
#define LINK_BIT_TIME_US 15
// ============================================
// STATUS LED - Optional
// ============================================
#define LED_STATUS_PIN 18 // Changed from 19 (USB conflict!)
#define LED_ACTIVE_LEVEL 1
// ============================================
// DEBUG/UART
// ============================================
#define UART_NUM UART_NUM_0
#define UART_TX_PIN 43
#define UART_RX_PIN 44
#define UART_BAUD_RATE 115200
// ============================================
// PIN SUMMARY (for reference)
// ============================================
/*
USED PINS - OPTIMIZED LAYOUT:
- GPIO 0: POWER_SWITCH (Hardware toggle switch)
- GPIO 1: LCD Backlight PWM
- GPIO 3: Volume Potentiometer (ADC1_CH2)
- GPIO 4: Brightness Potentiometer (ADC1_CH3)
- GPIO 5: I2C SDA (Touch 0x15 + NFC 0x24 SHARED BUS)
- GPIO 6: I2C SCL (Touch + NFC SHARED BUS)
- GPIO 7: Touch Interrupt
- GPIO 8: BTN_UP (native GPIO)
- GPIO 9: BTN_DOWN (native GPIO)
- GPIO 10: BTN_LEFT (native GPIO)
- GPIO 11: BTN_RIGHT (native GPIO)
- GPIO 12: BTN_A (native GPIO)
- GPIO 13: BTN_B (native GPIO)
- GPIO 14: BTN_START (native GPIO)
- GPIO 2: Link Cable SOUT
- GPIO 15: Link Cable SCLK (freed from NFC!)
- GPIO 16: I2S DIN (now conflict-free, NFC moved to shared I2C!)
- GPIO 17: Link Cable SIN
- GPIO 18: Status LED (was 19, USB conflict fixed!)
- GPIO 19: RESERVED (USB D-)
- GPIO 20: RESERVED (USB D+)
- GPIO 21: BTN_SELECT (native GPIO)
- GPIO 26-37: RESERVED (Flash/PSRAM - DO NOT USE!)
- GPIO 38: SPI MOSI (LCD + SD shared)
- GPIO 39: SPI SCLK (LCD + SD shared)
- GPIO 40: SPI MISO (LCD + SD shared)
- GPIO 41: SD Card CS
- GPIO 42: LCD DC
- GPIO 43: UART TX
- GPIO 44: UART RX
- GPIO 45: LCD CS
- GPIO 47: I2S LRC (right header pin 5)
- GPIO 48: I2S BCLK (right header pin 6)
- GPIO 16: I2S DIN (now conflict-free, NFC moved to shared I2C!)
POWER SWITCH WIRING:
┌─────────┐
│ ESP32 │
│ GPIO 0 ├──────┬──────── Switch ──────┐
│ │ │ │
│ (Pull-up) [Switch] GND
│ │ │
└─────────┘ │
└─ When closed: GPIO0 = LOW (ON)
When open: GPIO0 = HIGH (SLEEP)
*/