machine module: handling of unsupported args
This showed up using pin IRQ's but the problem prompted contemplating the general issue. The docs state that the hard arg for Pin.irq() is not supported by all ports. Accordingly the line
xirq = pin_x.irq(trigger=Pin.IRQ_RISING | Pin.IRQ_FALLING, handler=self._x_cb, hard=True)
fails on ESP32 with "TypeError: extra keyword arguments given". The obvious (ugly) way to make this portable is to trap the exception and try again without the offending arg. This could become unmanageable if there were multiple args with platform-dependent support.
The best solution is moot. One option would be to accept and silently ignore unsupported args, but this isn't very user friendly. Another would be to accept and ignore the arg, but throw (say) ValueError. Done in such a way as to show which arg is problematic, and in such a way that the handler can be null.
Given that the purpose of machine is to facilitate portability, I think this issue is worth review.
Crash on RP2 with pin IRQ's
The problem has appeared on nightly builds since 20th Dec. This build fails: rp2-pico-20230120-unstable-v1.19.1-831-g4f3780a15.uf2. The following is the smallest repro I've come up with so far:
import uasyncio as asyncio
from machine import Pin
class Encoder:
def __init__(self):
pin_x = Pin(20, Pin.IN, Pin.PULL_UP)
pin_y = Pin(17, Pin.IN, Pin.PULL_UP)
self._x = 0
self._y = 0
self._v = 0
trig = Pin.IRQ_RISING | Pin.IRQ_FALLING
pin_x.irq(trigger=trig, handler=self._x_cb, hard=False)
pin_y.irq(trigger=trig, handler=self._y_cb, hard=False)
def _x_cb(self, pin_x):
print('x')
if (x := pin_x.value()) != self._x: # Crash seems to occur here
pass
def _y_cb(self, pin_y):
print('y')
if (y := pin_y.value()) != self._y: # or here
pass
async def main():
e = Encoder()
while True:
await asyncio.sleep(1)
print(e._v)
asyncio.run(main())
When either pin changes state the ISR prints then the board crashes. If I reduce the ISR's to just a print statement the code runs. Note hard and soft IRQ's behave similarly.