Skip to content

Commit a4dd38d

Browse files
committed
machine/esp32s3: use edge-triggered CPU interrupt for GPIO pin interrupts
When SPI is configured via the GPIO Matrix, SPI signal transitions set GPIO.STATUS bits on the routed pins. With a level-triggered CPU interrupt (line 8), the ISR re-enters continuously as long as any STATUS bit is asserted — causing user GPIO callbacks to fire spuriously. Switch cpuInterruptFromPin to CPU interrupt 10, which is edge-triggered (level 1) on the Xtensa LX7. This ensures the ISR fires once per GPIO event rather than looping while SPI is active. Also move STATUS_W1TC clears to before callback dispatch so that new GPIO events arriving during handler execution generate a fresh edge, and add writeINTCLEAR(active) in handleInterrupt to properly acknowledge edge-triggered CPU interrupt pending bits via the INTCLEAR register. Fixes GPIO interrupts firing constantly when SPI and pin interrupts are used together. Signed-off-by: deadprogram <ron@hybridgroup.com>
1 parent 486ee37 commit a4dd38d

File tree

3 files changed

+33
-7
lines changed

3 files changed

+33
-7
lines changed

src/examples/pininterrupt/xiao-esp32s3.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ import "machine"
66

77
const (
88
button = machine.D1
9-
buttonMode = machine.PinInput
9+
buttonMode = machine.PinInputPullup
1010
buttonPinChange = machine.PinFalling
1111
)

src/machine/machine_esp32s3.go

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,12 @@ func (p Pin) pinReg() *volatile.Register32 {
306306
}
307307

308308
const maxPin = 49
309-
const cpuInterruptFromPin = 8
309+
310+
// cpuInterruptFromPin selects an edge-triggered CPU interrupt line for GPIO.
311+
// CPU interrupt 10 is edge-triggered level-1 on the Xtensa LX7, which prevents
312+
// the ISR from re-entering continuously when other peripherals (e.g. SPI via
313+
// the GPIO Matrix) keep GPIO.STATUS bits asserted.
314+
const cpuInterruptFromPin = 10
310315

311316
type PinChange uint8
312317

@@ -370,23 +375,28 @@ var (
370375
func setupPinInterrupt() error {
371376
esp.INTERRUPT_CORE0.SetGPIO_INTERRUPT_PRO_MAP(cpuInterruptFromPin)
372377
return interrupt.New(cpuInterruptFromPin, func(interrupt.Interrupt) {
373-
// Check status for GPIO0-31
378+
// Read and immediately clear interrupt status bits.
379+
// Clearing before processing is critical for edge-triggered CPU
380+
// interrupts: any new GPIO events that arrive during callback
381+
// execution will set fresh STATUS bits, generating a new edge
382+
// on the CPU interrupt line so they are not lost.
374383
status := esp.GPIO.STATUS.Get()
384+
status1 := esp.GPIO.STATUS1.Get()
385+
esp.GPIO.STATUS_W1TC.Set(status)
386+
esp.GPIO.STATUS1_W1TC.Set(status1)
387+
388+
// Check status for GPIO0-31
375389
for i, mask := 0, uint32(1); i < 32; i, mask = i+1, mask<<1 {
376390
if (status&mask) != 0 && pinCallbacks[i] != nil {
377391
pinCallbacks[i](Pin(i))
378392
}
379393
}
380394
// Check status for GPIO32-48
381-
status1 := esp.GPIO.STATUS1.Get()
382395
for i, mask := 32, uint32(1); i < maxPin; i, mask = i+1, mask<<1 {
383396
if (status1&mask) != 0 && pinCallbacks[i] != nil {
384397
pinCallbacks[i](Pin(i))
385398
}
386399
}
387-
// Clear interrupt bits
388-
esp.GPIO.STATUS_W1TC.SetBits(status)
389-
esp.GPIO.STATUS1_W1TC.SetBits(status1)
390400
}).Enable()
391401
}
392402

src/runtime/interrupt/interrupt_esp32s3.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,12 @@ func handleInterrupt() {
107107
enabled := readINTENABLE()
108108
active := pending & enabled
109109

110+
// Clear edge-triggered pending bits before dispatching handlers so that
111+
// new edges arriving during handler execution are not lost. Writing to
112+
// INTCLEAR is a no-op for level-triggered lines, so this is safe for all
113+
// interrupt types.
114+
writeINTCLEAR(active)
115+
110116
for i := firstCPUInt; i <= lastCPUInt; i++ {
111117
if active&(1<<uint(i)) != 0 {
112118
// callHandlers requires a compile-time constant, so we
@@ -212,6 +218,16 @@ func readINTERRUPT() uint32 {
212218
return uint32(device.AsmFull("rsr {}, INTERRUPT", nil))
213219
}
214220

221+
// writeINTCLEAR writes the INTCLEAR special register (SR 227).
222+
// Setting bit N clears CPU interrupt N if it is edge-triggered or
223+
// software-triggered. Bits corresponding to level-triggered interrupts
224+
// are ignored by hardware.
225+
func writeINTCLEAR(val uint32) {
226+
device.AsmFull("wsr {val}, INTCLEAR", map[string]interface{}{
227+
"val": val,
228+
})
229+
}
230+
215231
// -- Interrupt matrix helpers -----------------------------------------------
216232
// The ESP32-S3 interrupt matrix has one mapping register per peripheral
217233
// source. These are memory-mapped in the INTERRUPT_CORE0 peripheral.

0 commit comments

Comments
 (0)