Fix touch vs SD VSPI clash; SoftSPI + BOOT/serial REC.

Recording verified via serial r; 2.8 touch no longer shares VSPI with microSD.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-10-27 21:42:37 +08:00
parent 76ca6189d5
commit 994d21c7fb
11 changed files with 192 additions and 98 deletions

View File

@@ -40,6 +40,8 @@ ESP32 Cheap Yellow Display + INMP441(I2S)→ microSD **WAV 录音**,支持
- **PLAY / STOP**:用板载喇叭(GPIO26)回放最近一条录音
- 短按顶部标题栏切换背光 4 档
- 需插入 **FAT32 microSD**
- 触控备用:板载 **BOOT** 键切换 REC;串口发送 `r` / `p` 切换录音 / 播放
- 2.8" 触控用 SoftSPI(与 SD 的 VSPI 隔离;勿再用会抢总线的 XPT2046+VSPI)
## 构建与烧录

View File

@@ -1,5 +1,5 @@
name: CYD inmp441recondtest (cyd_28r)
git: ddff48d
git: 76ca618
date: 2026-10-27
board: ESP32-2432S028R (ILI9341)
inmp441: WS=GPIO22 SCK=GPIO27 SD=GPIO35
@@ -14,5 +14,5 @@ offsets:
sha256:
bootloader.bin 3D234A7471F67B013686DABD4DEE7C1FA915C9928463616A94BC9297ACF1ABF8
boot_app0.bin F94C5D786A7A8FAB06AC5D10E33BF37711A6697636DC037559EA19CC410A17F0
firmware.bin 8BDD28C452BF7406295B7E76FDC387917FE6BF223BE0FD18B6988BABD4EFF12E
firmware.bin 201E74138012880FDEA3AA46187BA3353B65788D2689DD076A7456C11EF5DD04
partitions.bin 0A8B5720E7B77FF11F1462458C3A509DEE79224E5279898F26D6A2E3AE0517B7

Binary file not shown.

View File

@@ -8,20 +8,25 @@
#define I2S_WS_PIN 22
#define I2S_SCK_PIN 21
#define I2S_SD_PIN 35
#define SCREEN_WIDTH 480
#define SCREEN_HEIGHT 320
#define SCREEN_WIDTH 320
#define SCREEN_HEIGHT 480
#define UI_HOR_RES 480
#define UI_VER_RES 320
#define CYD_GT911_SDA 33
#define CYD_GT911_SCL 32
#define CYD_GT911_INT 21
#define CYD_GT911_RST 25
#else
/* 2.8 ESP32-2432S028R: BL=21; free peripheral pair is 22+27; SD on P3 IO35 */
/* 2.8 ESP32-2432S028R: BL=21; free peripheral pair is 22+27; SD on P3 IO35
* SCREEN_* = native panel; UI_* = landscape after LVGL ROTATION_270 */
#define TFT_BL_PIN 21
#define I2S_WS_PIN 22
#define I2S_SCK_PIN 27
#define I2S_SD_PIN 35
#define SCREEN_WIDTH 320
#define SCREEN_HEIGHT 240
#define SCREEN_WIDTH 240
#define SCREEN_HEIGHT 320
#define UI_HOR_RES 320
#define UI_VER_RES 240
#define CYD_TOUCH_IRQ 36
#define CYD_TOUCH_MOSI 32
#define CYD_TOUCH_MISO 39

View File

@@ -7,3 +7,6 @@ void sd_deinit(void);
bool sd_ready(void);
uint64_t sd_free_bytes(void);
uint64_t sd_total_bytes(void);
/* VSPI is shared with 2.8" XPT2046 — reclaim before SD I/O after touch. */
void sd_reclaim_spi(void);

View File

@@ -14,7 +14,6 @@ board_build.partitions = min_spiffs.csv
lib_deps =
lvgl/lvgl@9.2.2
bodmer/TFT_eSPI@2.5.43
https://github.com/PaulStoffregen/XPT2046_Touchscreen.git
extra_scripts = pre:scripts/exclude_lvgl_asm.py
build_flags =
-D CORE_DEBUG_LEVEL=0

View File

@@ -20,9 +20,9 @@
#include "player.h"
#if defined(CYD_BOARD_3248S035)
#define DRAW_BUF_SIZE (SCREEN_WIDTH * SCREEN_HEIGHT / 20 * (LV_COLOR_DEPTH / 8))
#define DRAW_BUF_SIZE (UI_HOR_RES * UI_VER_RES / 20 * (LV_COLOR_DEPTH / 8))
#else
#define DRAW_BUF_SIZE (SCREEN_WIDTH * SCREEN_HEIGHT / 10 * (LV_COLOR_DEPTH / 8))
#define DRAW_BUF_SIZE (UI_HOR_RES * UI_VER_RES / 10 * (LV_COLOR_DEPTH / 8))
#endif
#if defined(CYD_BOARD_3248S035)
@@ -130,6 +130,7 @@ static lv_obj_t *make_panel(lv_obj_t *parent, int x, int y, int w, int h) {
lv_obj_set_style_radius(p, 12, 0);
lv_obj_set_style_pad_all(p, 8, 0);
lv_obj_clear_flag(p, LV_OBJ_FLAG_SCROLLABLE);
lv_obj_clear_flag(p, LV_OBJ_FLAG_CLICKABLE);
return p;
}
@@ -142,8 +143,8 @@ static void style_btn(lv_obj_t *btn, lv_color_t bg) {
lv_obj_set_ext_click_area(btn, 8);
}
static void on_rec_clicked(lv_event_t *e) {
(void)e;
static void do_rec_toggle(void) {
Serial.println("UI: REC toggle");
if (player_is_playing()) {
player_stop();
show_hint("stopped play", 800);
@@ -166,8 +167,8 @@ static void on_rec_clicked(lv_event_t *e) {
}
}
static void on_play_clicked(lv_event_t *e) {
(void)e;
static void do_play_toggle(void) {
Serial.println("UI: PLAY toggle");
if (recorder_is_active()) {
show_hint("stop REC first", 1200);
return;
@@ -189,6 +190,16 @@ static void on_play_clicked(lv_event_t *e) {
show_hint("playing", 800);
}
static void on_rec_clicked(lv_event_t *e) {
(void)e;
do_rec_toggle();
}
static void on_play_clicked(lv_event_t *e) {
(void)e;
do_play_toggle();
}
static void create_ui(void) {
lv_obj_t *scr = lv_screen_active();
lv_obj_set_style_bg_color(scr, COL_BG, 0);
@@ -229,11 +240,11 @@ static void create_ui(void) {
lv_obj_align(lbl_hint, LV_ALIGN_TOP_MID, 0, 4);
lv_obj_add_flag(lbl_hint, LV_OBJ_FLAG_HIDDEN);
const int content_w = SCREEN_WIDTH - 2 * UI_M;
const int content_w = UI_HOR_RES - 2 * UI_M;
const int y0 = 28;
lv_obj_t *p_main = make_panel(scr, UI_M, y0, content_w,
SCREEN_HEIGHT - y0 - UI_M);
UI_VER_RES - y0 - UI_M);
lbl_time = lv_label_create(p_main);
lv_label_set_text(lbl_time, "00:00");
@@ -282,6 +293,7 @@ static void create_ui(void) {
lv_label_set_text(lbl_rec, "REC");
lv_obj_set_style_text_font(lbl_rec, &lv_font_montserrat_16, 0);
lv_obj_set_style_text_color(lbl_rec, COL_TEXT, 0);
lv_obj_clear_flag(lbl_rec, LV_OBJ_FLAG_CLICKABLE);
lv_obj_center(lbl_rec);
btn_play = lv_button_create(p_main);
@@ -293,6 +305,7 @@ static void create_ui(void) {
lv_label_set_text(lbl_play, "PLAY");
lv_obj_set_style_text_font(lbl_play, &lv_font_montserrat_16, 0);
lv_obj_set_style_text_color(lbl_play, COL_TEXT, 0);
lv_obj_clear_flag(lbl_play, LV_OBJ_FLAG_CLICKABLE);
lv_obj_center(lbl_play);
lv_obj_t *hint2 = lv_label_create(p_main);
@@ -406,8 +419,11 @@ void setup() {
mic_ok = recorder_mic_init();
player_init();
pinMode(0, INPUT_PULLUP); /* BOOT button → REC toggle fallback */
Serial.println(mic_ok ? "I2S mic OK" : "I2S mic FAIL — check wiring");
Serial.println(card_ok ? "SD OK" : "SD FAIL — insert FAT32 microSD");
Serial.println("Hint: BOOT key or serial 'r' toggles REC; 'p' toggles PLAY");
#if defined(CYD_BOARD_3248S035)
Serial.println("3.5C: SCK=GPIO21 (shared GT911 INT; touch is poll-only)");
#endif
@@ -439,6 +455,32 @@ void loop() {
recorder_tick();
player_tick();
/* BOOT (GPIO0) short press = REC — works even if touch is flaky. */
static bool boot_was = false;
static uint32_t boot_ms = 0;
bool boot_now = (digitalRead(0) == LOW);
if (boot_now && !boot_was) {
boot_was = true;
boot_ms = millis();
} else if (!boot_now && boot_was) {
boot_was = false;
if (millis() - boot_ms < 800) {
Serial.println("BOOT → REC");
do_rec_toggle();
}
}
while (Serial.available() > 0) {
char c = (char)Serial.read();
if (c == 'r' || c == 'R') {
Serial.println("Serial → REC");
do_rec_toggle();
} else if (c == 'p' || c == 'P') {
Serial.println("Serial → PLAY");
do_play_toggle();
}
}
uint32_t now = millis();
if (now - last_ui_ms >= 100) {
last_ui_ms = now;

View File

@@ -55,6 +55,7 @@ static uint8_t scale_u8(int16_t s16) {
static bool open_and_parse(const char *path, uint16_t *channels, uint16_t *bits,
uint32_t *sample_rate, uint32_t *data_left) {
sd_reclaim_spi();
play_file = SD.open(path, FILE_READ);
if (!play_file) {
status = "open fail";

View File

@@ -303,6 +303,7 @@ void recorder_tick(void) {
}
if (out_n > 0) {
sd_reclaim_spi();
size_t want = (size_t)out_n * sizeof(int16_t);
size_t wr = rec_file.write((const uint8_t *)pcm_out, want);
if (wr != want) {

View File

@@ -9,13 +9,23 @@
static SPIClass sd_spi(VSPI);
static bool g_ready = false;
void sd_reclaim_spi(void) {
#if !defined(CYD_BOARD_3248S035)
digitalWrite(CYD_TOUCH_CS, HIGH);
#endif
digitalWrite(SD_CS, HIGH);
if (g_ready) {
/* SoftSPI touch does not use VSPI — just keep CS high. */
sd_spi.begin(SD_SCK, SD_MISO, SD_MOSI, SD_CS);
}
}
bool sd_init(void) {
if (g_ready) return true;
pinMode(SD_CS, OUTPUT);
digitalWrite(SD_CS, HIGH);
#if !defined(CYD_BOARD_3248S035)
/* Keep XPT2046 CS high so SD SPI does not steal the bus. */
pinMode(CYD_TOUCH_CS, OUTPUT);
digitalWrite(CYD_TOUCH_CS, HIGH);
#endif

View File

@@ -1,16 +1,8 @@
#include "touch.h"
#include "pins.h"
#include <Arduino.h>
/**
* 2.8": XPT2046 resistive SoftSPI bus (CS33).
* 3.5C (3248S035C): GT911 capacitive on Wire1 SDA33/SCL32/INT21/RST25.
* Feed coords are always native panel 320x480 for LVGL ROTATION_270.
* Wire1 keeps GT911 off the INMP441 pins (WS22 / SCK21). After touch_init(),
* main claims GPIO21 as I2S BCK — touch stays poll-only (no INT IRQ).
* TFT_BL=27 on 3.5C.
*/
static int16_t clampi(int32_t v, int32_t lo, int32_t hi) {
if (v < lo) return (int16_t)lo;
if (v > hi) return (int16_t)hi;
@@ -21,16 +13,9 @@ static int16_t clampi(int32_t v, int32_t lo, int32_t hi) {
#include <Wire.h>
/* Native panel size — not UI landscape SCREEN_* (480x320). */
static constexpr int16_t PANEL_W = 320;
static constexpr int16_t PANEL_H = 480;
#define CYD_GT911_SDA 33
#define CYD_GT911_SCL 32
#define CYD_GT911_INT 21
#define CYD_GT911_RST 25
/* Minimal GT911 — no config rewrite (TAMC setResolution can hang Wire). */
static uint8_t s_gt_addr = 0x5D;
static constexpr uint16_t GT_POINT_INFO = 0x814E;
static constexpr uint16_t GT_POINT_1 = 0x814F;
@@ -47,16 +32,10 @@ static bool gt_read(uint16_t reg, uint8_t *buf, uint8_t len) {
Wire1.beginTransmission(s_gt_addr);
Wire1.write((uint8_t)(reg >> 8));
Wire1.write((uint8_t)(reg & 0xFF));
if (Wire1.endTransmission(false) != 0) {
return false;
}
if (Wire1.endTransmission(false) != 0) return false;
const uint8_t n = Wire1.requestFrom(s_gt_addr, len);
if (n != len) {
return false;
}
for (uint8_t i = 0; i < len; i++) {
buf[i] = Wire1.read();
}
if (n != len) return false;
for (uint8_t i = 0; i < len; i++) buf[i] = Wire1.read();
return true;
}
@@ -65,7 +44,6 @@ static bool gt_probe(uint8_t addr) {
return Wire1.endTransmission() == 0;
}
/* TAMC-compatible address select: INT low => 0x5D, INT high => 0x14 */
static void gt_hw_reset(bool addr_0x14) {
pinMode(CYD_GT911_INT, OUTPUT);
pinMode(CYD_GT911_RST, OUTPUT);
@@ -78,7 +56,6 @@ static void gt_hw_reset(bool addr_0x14) {
delay(5);
digitalWrite(CYD_GT911_INT, LOW);
delay(50);
/* BL=27. Release INT(21) before main reuses it as INMP441 I2S SCK. */
pinMode(CYD_GT911_INT, INPUT);
delay(50);
}
@@ -94,49 +71,34 @@ static bool gt_find_addr() {
}
void touch_init() {
Serial.println("Touch: GT911 I2C capacitive (3.5C) minimal Wire1");
Serial.println("Touch: GT911 Wire1");
Wire1.begin(CYD_GT911_SDA, CYD_GT911_SCL);
Wire1.setClock(100000);
#if defined(WIRE_HAS_TIMEOUT) || defined(ESP32)
Wire1.setTimeOut(50);
#endif
if (!gt_find_addr()) {
Serial.println("GT911 not found — hw reset 0x5D");
gt_hw_reset(false);
if (!gt_find_addr()) {
Serial.println("GT911 not found — hw reset 0x14");
gt_hw_reset(true);
gt_find_addr();
}
}
Serial.printf("GT911 probe 0x5D=%s 0x14=%s using=0x%02X\n",
gt_probe(0x5D) ? "OK" : "no", gt_probe(0x14) ? "OK" : "no",
s_gt_addr);
Serial.printf("GT911 SDA=%d SCL=%d INT=%d RST=%d\n", CYD_GT911_SDA,
CYD_GT911_SCL, CYD_GT911_INT, CYD_GT911_RST);
}
void touch_read_cb(lv_indev_t * /*indev*/, lv_indev_data_t *data) {
uint8_t info = 0;
if (!gt_read(GT_POINT_INFO, &info, 1)) {
data->state = LV_INDEV_STATE_RELEASED;
return;
}
const uint8_t touches = info & 0x0F;
const bool buffered = (info & 0x80) != 0;
if (!buffered || touches == 0) {
if (buffered) {
gt_write8(GT_POINT_INFO, 0);
}
if (buffered) gt_write8(GT_POINT_INFO, 0);
data->state = LV_INDEV_STATE_RELEASED;
return;
}
uint8_t pt[7];
if (!gt_read(GT_POINT_1, pt, 7)) {
gt_write8(GT_POINT_INFO, 0);
@@ -144,13 +106,8 @@ void touch_read_cb(lv_indev_t * /*indev*/, lv_indev_data_t *data) {
return;
}
gt_write8(GT_POINT_INFO, 0);
const int16_t raw_x = (int16_t)(pt[1] | (pt[2] << 8));
const int16_t raw_y = (int16_t)(pt[3] | (pt[4] << 8));
/* Native 320x480 → LVGL 270. UI ≈ (H-1-raw_y, raw_x). */
const int16_t nx = clampi(raw_x, 0, PANEL_W - 1);
const int16_t ny = clampi(raw_y, 0, PANEL_H - 1);
const int16_t nx = clampi((int16_t)(pt[1] | (pt[2] << 8)), 0, PANEL_W - 1);
const int16_t ny = clampi((int16_t)(pt[3] | (pt[4] << 8)), 0, PANEL_H - 1);
data->point.x = (int16_t)((PANEL_W - 1) - nx);
data->point.y = (int16_t)((PANEL_H - 1) - ny);
data->state = LV_INDEV_STATE_PRESSED;
@@ -158,38 +115,112 @@ void touch_read_cb(lv_indev_t * /*indev*/, lv_indev_data_t *data) {
#else
#include <SPI.h>
#include <XPT2046_Touchscreen.h>
/* Bit-bang SoftSPI — does not share VSPI with microSD (tftest path). */
#define XPT2046_IRQ 36
#define XPT2046_MOSI 32
#define XPT2046_MISO 39
#define XPT2046_CLK 25
#define XPT2046_CS 33
/* 2.8 UI landscape feed (unchanged from prior main.cpp). */
static constexpr int16_t UI_W = 320;
static constexpr int16_t UI_H = 240;
static SPIClass touchscreenSPI = SPIClass(VSPI);
static XPT2046_Touchscreen touchscreen(XPT2046_CS, XPT2046_IRQ);
void touch_init() {
touchscreenSPI.begin(XPT2046_CLK, XPT2046_MISO, XPT2046_MOSI, XPT2046_CS);
touchscreen.begin(touchscreenSPI);
touchscreen.setRotation(2);
Serial.println("Touch: XPT2046 resistive (2.8)");
}
void touch_read_cb(lv_indev_t * /*indev*/, lv_indev_data_t *data) {
if (touchscreen.tirqTouched() && touchscreen.touched()) {
TS_Point p = touchscreen.getPoint();
data->point.x = map(p.y, 240, 3800, 1, UI_W);
data->point.y = map(p.x, 200, 3700, UI_H, 1);
data->state = LV_INDEV_STATE_PRESSED;
} else {
data->state = LV_INDEV_STATE_RELEASED;
static void soft_write(uint8_t data) {
for (int i = 7; i >= 0; --i) {
digitalWrite(CYD_TOUCH_MOSI, (data >> i) & 1);
digitalWrite(CYD_TOUCH_CLK, HIGH);
delayMicroseconds(10);
digitalWrite(CYD_TOUCH_CLK, LOW);
delayMicroseconds(10);
}
}
static uint16_t soft_read16(void) {
uint16_t v = 0;
for (int i = 0; i < 16; ++i) {
digitalWrite(CYD_TOUCH_CLK, HIGH);
delayMicroseconds(10);
v = (uint16_t)((v << 1) | (digitalRead(CYD_TOUCH_MISO) ? 1u : 0u));
digitalWrite(CYD_TOUCH_CLK, LOW);
delayMicroseconds(10);
}
return v;
}
/* One CS transaction: Z, X, Y (closer to XPT2046 lib sequence). */
static void xpt_sample(uint16_t *x, uint16_t *y, uint16_t *z) {
digitalWrite(SD_CS, HIGH);
digitalWrite(CYD_TOUCH_CS, LOW);
delayMicroseconds(10);
soft_write(0xB1); /* Z1 */
uint16_t z1 = soft_read16() >> 3;
soft_write(0xC1); /* Z2 */
uint16_t z2 = soft_read16() >> 3;
*z = (uint16_t)((z1 + 4095) - z2);
soft_write(0x91); /* dummy X */
(void)soft_read16();
soft_write(0xD1); /* Y */
uint16_t y0 = soft_read16() >> 3;
soft_write(0x91); /* X */
uint16_t x0 = soft_read16() >> 3;
soft_write(0xD0); /* Y power-down */
uint16_t y1 = soft_read16() >> 3;
soft_write(0x00);
uint16_t x1 = soft_read16() >> 3;
digitalWrite(CYD_TOUCH_CS, HIGH);
*x = (uint16_t)((x0 + x1) / 2);
*y = (uint16_t)((y0 + y1) / 2);
}
void touch_init() {
pinMode(SD_CS, OUTPUT);
digitalWrite(SD_CS, HIGH);
pinMode(CYD_TOUCH_CS, OUTPUT);
pinMode(CYD_TOUCH_CLK, OUTPUT);
pinMode(CYD_TOUCH_MOSI, OUTPUT);
pinMode(CYD_TOUCH_MISO, INPUT);
pinMode(CYD_TOUCH_IRQ, INPUT);
digitalWrite(CYD_TOUCH_CS, HIGH);
digitalWrite(CYD_TOUCH_CLK, LOW);
Serial.println("Touch: SoftSPI Z/X/Y (SD-safe, no VSPI)");
}
void touch_read_cb(lv_indev_t * /*indev*/, lv_indev_data_t *data) {
uint16_t adc_x = 0, adc_y = 0, adc_z = 0;
xpt_sample(&adc_x, &adc_y, &adc_z);
/* SoftSPI Z on this board is noisy (~4095 idle); use X/Y window only.
* Idle typically x≈50; a real press lands both axes in mid-range. */
(void)adc_z;
const bool down =
(adc_x > 200 && adc_x < 3900 && adc_y > 200 && adc_y < 3900);
static uint32_t last_log = 0;
if (down && millis() - last_log > 200) {
last_log = millis();
Serial.printf("Touch soft x=%u y=%u\n", (unsigned)adc_x, (unsigned)adc_y);
}
if (!down) {
data->state = LV_INDEV_STATE_RELEASED;
return;
}
/* tftest SoftSPI → native feed for LVGL ROTATION_270 */
const int16_t p_x = (int16_t)adc_x;
const int16_t p_y = (int16_t)(4095 - adc_y);
int16_t x = map(p_x, 200, 3700, 1, SCREEN_WIDTH);
int16_t y = map(p_y, 240, 3800, 1, SCREEN_HEIGHT);
x = clampi(x, 0, SCREEN_WIDTH - 1);
y = clampi(y, 0, SCREEN_HEIGHT - 1);
const int32_t old_ux = y;
const int32_t old_uy = (SCREEN_WIDTH - 1) - x;
int16_t ui_x = clampi((old_uy * (UI_HOR_RES - 1)) / (UI_VER_RES - 1), 0,
UI_HOR_RES - 1);
int16_t ui_y = clampi((old_ux * (UI_VER_RES - 1)) / (UI_HOR_RES - 1), 0,
UI_VER_RES - 1);
data->point.x = (int16_t)((SCREEN_WIDTH - 1) - ui_y);
data->point.y = ui_x;
data->state = LV_INDEV_STATE_PRESSED;
}
#endif