diff --git a/README.md b/README.md index e8b1867..3db5370 100644 --- a/README.md +++ b/README.md @@ -46,18 +46,26 @@ All code is **pure ESP-IDF v5.x (C) / FreeRTOS** — no Arduino. | Peripheral | Function | ESP32 GPIO | Notes | | :--- | :--- | :---: | :--- | -| **Shared SPI** | SCK | **18** | SPI2_HOST (VSPI), 1 MHz modules | -| | MOSI | **23** | | -| | MISO | **19** | | -| **MFRC522** | SDA (CS) | **5** | dedicated chip select | -| | RST | **22** | hard reset | -| | 3.3V / GND | — | feed from board 3.3V rail | -| **ST7789** (7-pin) | CS | **15** | dedicated chip select | +> **Display and reader are on two separate SPI controllers:** the ST7789 on +> **SPI2_HOST (VSPI)**, the MFRC522 on **SPI3_HOST (HSPI)** — so the panel and +> the RFID reader never share bus timing. + +### Pin map + +| Peripheral | Function | ESP32 GPIO | Notes | +| :--- | :--- | :---: | :--- | +| **ST7789** (7-pin) — SPI2_HOST | SCL | **18** | SPI2 (VSPI) clock | +| | SDA | **23** | SPI2 MOSI (data) | +| | CS | **15** | dedicated chip select | | | DC | **2** | data/command | | | RST | **4** | panel reset | -| | SDA | **23** | shared MOSI (data) | -| | SCL | **18** | shared SCK (clock) | | | VCC / GND | 3.3V / GND | panel power (no separate LED pin) | +| **MFRC522** — SPI3_HOST | SCK | **16** | SPI3 (HSPI) clock (own bus) | +| | MOSI | **17** | SPI3 data out | +| | MISO | **13** | SPI3 data in | +| | SDA (CS) | **26** | dedicated chip select | +| | RST | **22** | hard reset | +| | 3.3V / GND | — | feed from board 3.3V rail | | **EC11** | CLK | **32** | interrupt input, pull-up | | | DT | **33** | quadrature B | | | SW | **25** | button, active low, pull-up | @@ -69,23 +77,26 @@ All code is **pure ESP-IDF v5.x (C) / FreeRTOS** — no Arduino. ``` ESP32-WROOM-32 ┌────────────────────────────┐ - │ 3V3 18 ── SCK ──┐ │ - │ GND 23 ── MOSI ─┼─────┐ │ - │ 19 ── MISO ─┘ │ │ - │ 5 ── SDA ───────┤ MFRC522 22 ── RST - │ 22 ── RST ───────┤ 3V3/GND - │ 15 ── CS ────────┤ ST7789 (7-pin) - │ 2 ── DC ────────┤ SDA <-> MOSI, SCL <-> SCK - │ 4 ── RST ───────┤ VCC = 3V3, GND = GND + │ 3V3 16 ── SCK ────────┐ │ + │ GND 17 ── MOSI ────┐ │ │ + │ 13 ── MISO ────┼──┘ │ + │ 26 ── SDA(CS) ─┘ │ + │ 22 ── RST ─┐ │ + │ 15 ── CS ──┼───────┤ ST7789 (7-pin) + │ 2 ── DC ──┤ │ SCL = 18, SDA = 23 + │ 4 ── RST ─┘ │ VCC = 3V3, GND = GND + │ 18 ── SCL ─────────┘ + │ 23 ── SDA ───────── (display = SPI2) │ 32 ── CLK ────────┤ EC11 33 ── DT 25 ── SW/GND │ 27 ── Din ────────┤ WS2812B (5V + GND) │ 14 ── SIG ────────┤ Buzzer (GND) └────────────────────────────┘ ``` -> **7-pin ST7789 wiring recap:** connect the display's **SCL → ESP32 GPIO 18**, -> **SDA → GPIO 23**, **CS → 15**, **DC → 2**, **RST → 4**, **VCC → 3.3V**, -> **GND → GND**. The MFRC522 taps the same SCL/SDA lines (its own CS is GPIO 5). +> **MFRC522 (own SPI3/HSPI bus):** SCK→16, MOSI→17, MISO→13, SDA(CS)→26, +> RST→22, VCC→3.3V, GND→GND. It shares **no lines** with the display. +> +> **ST7789 (SPI2/VSPI):** SCL→18, SDA→23, CS→15, DC→2, RST→4, VCC→3.3V, GND→GND. - Keep SPI signal runs short (< 10 cm) for reliable 20 MHz display transfers. - NTAG213/215 FPC stickers need to sit flat and close to the MFRC522 antenna. diff --git a/main/hw_pins.h b/main/hw_pins.h index d043f8f..03cf916 100644 --- a/main/hw_pins.h +++ b/main/hw_pins.h @@ -11,17 +11,23 @@ #include "hal/gpio_types.h" #include "driver/spi_master.h" -/* Shared SPI2_HOST (VSPI) bus. - * NOTE: on 7-pin ST7789 modules, the display labels these SDA (MOSI) and - * SCL (SCK) - they tap the SAME shared bus as the MFRC522. */ +/* SPI2_HOST (VSPI): dedicated to the ST7789 display. + * On 7-pin ST7789 modules the display labels these SDA (MOSI) and + * SCL (SCK). */ #define HW_SPI_HOST (SPI2_HOST) -#define HW_SPI_SCK_GPIO (GPIO_NUM_18) /* display SCL + MFRC522 SCK */ -#define HW_SPI_MOSI_GPIO (GPIO_NUM_23) /* display SDA + MFRC522 MOSI */ -#define HW_SPI_MISO_GPIO (GPIO_NUM_19) /* MFRC522 MISO only */ +#define HW_SPI_SCK_GPIO (GPIO_NUM_18) /* display SCL */ +#define HW_SPI_MOSI_GPIO (GPIO_NUM_23) /* display SDA */ +#define HW_SPI_MISO_GPIO (GPIO_NUM_19) /* unused by display; kept for bus */ -/* MFRC522 RFID reader */ -#define HW_RC522_CS_GPIO (GPIO_NUM_5) -#define HW_RC522_RST_GPIO (GPIO_NUM_22) +/* MFRC522 RFID reader - OWN SPI3_HOST (HSPI) bus, separate from the display. + * Using a independent controller avoids any bus interference between the + * panel and the reader. */ +#define HW_RFID_SPI_HOST (SPI3_HOST) +#define HW_RFID_SCK_GPIO (GPIO_NUM_16) +#define HW_RFID_MOSI_GPIO (GPIO_NUM_17) +#define HW_RFID_MISO_GPIO (GPIO_NUM_13) +#define HW_RC522_CS_GPIO (GPIO_NUM_26) +#define HW_RC522_RST_GPIO (GPIO_NUM_22) /* ST7789 display - 7-pin breakout: CS / DC / RST / SDA / SCL / GND / VCC. * SDA and SCL share the SPI bus above (SCL=HW_SPI_SCK, SDA=HW_SPI_MOSI). */ diff --git a/main/rfid_manager.c b/main/rfid_manager.c index b9ac4fc..7cdffcd 100644 --- a/main/rfid_manager.c +++ b/main/rfid_manager.c @@ -63,10 +63,18 @@ static void on_picc_state_changed(void *arg, esp_event_base_t base, int32_t even esp_err_t rfid_manager_init(void) { + /* Dedicated SPI3_HOST (HSPI) bus for the reader - independent controller + * so the display (SPI2_HOST) and the RFID never share bus timing. The + * rc522 driver will call spi_bus_initialize() itself on this host. */ + static spi_bus_config_t rfid_bus_cfg = { + .sclk_io_num = HW_RFID_SCK_GPIO, + .mosi_io_num = HW_RFID_MOSI_GPIO, + .miso_io_num = HW_RFID_MISO_GPIO, + }; + rc522_spi_config_t driver_config = { - .host_id = HW_SPI_HOST, - /* Bus already owned by spi_bus_manager: attaching as an extra device. */ - .bus_config = NULL, + .host_id = HW_RFID_SPI_HOST, + .bus_config = &rfid_bus_cfg, .dev_config = { .spics_io_num = HW_RC522_CS_GPIO, .clock_speed_hz = RC522_SPI_CLK_HZ,