@@ -17,8 +17,8 @@ type io struct {
17
17
18
18
type irqCtrl struct {
19
19
intE [4 ]volatile.Register32
20
- intS [4 ]volatile.Register32
21
20
intF [4 ]volatile.Register32
21
+ intS [4 ]volatile.Register32
22
22
}
23
23
24
24
type ioBank0Type struct {
@@ -237,8 +237,8 @@ const (
237
237
238
238
// Callbacks to be called for pins configured with SetInterrupt.
239
239
var (
240
- pinCallbacks [2 ]func (Pin )
241
- setInt [2 ]bool
240
+ pinCallbacks [2 ][ _NUMBANK0_GPIOS ] func (Pin )
241
+ setInt [2 ][ _NUMBANK0_GPIOS ] bool
242
242
)
243
243
244
244
// SetInterrupt sets an interrupt to be executed when a particular pin changes
@@ -256,20 +256,19 @@ func (p Pin) SetInterrupt(change PinChange, callback func(Pin)) error {
256
256
if callback == nil {
257
257
// disable current interrupt
258
258
p .setInterrupt (change , false )
259
- pinCallbacks [core ] = nil
259
+ pinCallbacks [core ][ p ] = nil
260
260
return nil
261
261
}
262
262
263
- if pinCallbacks [core ] != nil {
263
+ if pinCallbacks [core ][ p ] != nil {
264
264
// Callback already configured. Should disable callback by passing a nil callback first.
265
265
return ErrNoPinChangeChannel
266
266
}
267
267
p .setInterrupt (change , true )
268
- pinCallbacks [core ] = callback
268
+ pinCallbacks [core ][ p ] = callback
269
269
270
- if setInt [core ] {
270
+ if setInt [core ][ p ] {
271
271
// interrupt has already been set. Exit.
272
- println ("core set" )
273
272
return nil
274
273
}
275
274
interrupt .New (rp .IRQ_IO_IRQ_BANK0 , gpioHandleInterrupt ).Enable ()
@@ -280,31 +279,27 @@ func (p Pin) SetInterrupt(change PinChange, callback func(Pin)) error {
280
279
// gpioHandleInterrupt finds the corresponding pin for the interrupt.
281
280
// C SDK equivalent of gpio_irq_handler
282
281
func gpioHandleInterrupt (intr interrupt.Interrupt ) {
283
- // panic("END") // if program is not ended here rp2040 will call interrupt again when finished, a vicious spin cycle.
282
+
284
283
core := CurrentCore ()
285
- callback := pinCallbacks [core ]
286
- if callback != nil {
287
- // TODO fix gpio acquisition (see below)
288
- // For now all callbacks get pin 255 (nonexistent).
289
- callback (0xff )
290
- }
291
284
var gpio Pin
292
285
for gpio = 0 ; gpio < _NUMBANK0_GPIOS ; gpio ++ {
293
- // Acknowledge all GPIO interrupts for now
294
- // since we are yet unable to acquire interrupt status
295
- gpio .acknowledgeInterrupt (0xff ) // TODO fix status get. For now we acknowledge all pending interrupts.
296
- // Commented code below from C SDK not working.
297
- // statreg := base.intS[gpio>>3]
298
- // change := getIntChange(gpio, statreg.Get())
299
- // if change != 0 {
300
- // gpio.acknowledgeInterrupt(change)
301
- // if callback != nil {
302
- // callback(gpio)
303
- // return
304
- // } else {
305
- // panic("unset callback in handler")
306
- // }
307
- // }
286
+ var base * irqCtrl
287
+ switch core {
288
+ case 0 :
289
+ base = & ioBank0 .proc0IRQctrl
290
+ case 1 :
291
+ base = & ioBank0 .proc1IRQctrl
292
+ }
293
+
294
+ statreg := base .intS [gpio >> 3 ]
295
+ change := getIntChange (gpio , statreg .Get ())
296
+ if change != 0 {
297
+ gpio .acknowledgeInterrupt (change )
298
+ callback := pinCallbacks [core ][gpio ]
299
+ if callback != nil {
300
+ callback (gpio )
301
+ }
302
+ }
308
303
}
309
304
}
310
305
0 commit comments