microbit-foundation / micropython-microbit-v2

Temporary home for MicroPython for micro:bit v2 as we stablise it before pushing upstream
MIT License
41 stars 22 forks source link

I2C & SPI pins are not released when modules are initialised with different pins #167

Closed microbit-carlos closed 3 months ago

microbit-carlos commented 4 months ago

With the update from https://github.com/microbit-foundation/micropython-microbit-v2/issues/112 and https://github.com/microbit-foundation/micropython-microbit-v2/issues/111 we can change the I2C and SPI pins to other pins than 19 and 20. However, the pin modes are not updated.

Using a build from the latest commit in main: https://github.com/microbit-foundation/micropython-microbit-v2/actions/runs/7565598370

>>> pin19.get_mode()
'i2c'
>>> pin0.get_mode()
'unused'
>>> i2c.init(sda=pin0, scl=pin1)
>>> i2c.scan()
[]
>>> pin0.get_mode()
'unused'
>>> pin0.write_digital(1)
>>> pin19.get_mode()
'i2c'
>>> pin19.write_digital(1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: Pin 19 in i2c mode
>>> pin13.get_mode()
'unused'
>>> spi.init(sclk=pin13, mosi=pin15, miso=pin14)
>>> pin13.get_mode()
'spi'
>>> spi.init(sclk=pin8, mosi=pin12, miso=pin16)
>>> pin13.get_mode()
'spi'
>>> pin8.get_mode()
'spi'
>>> pin8.write_digital(1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: function takes 2 positional arguments but 1 were given
>>> pin13.write_digital(1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: Pin 8 in spi mode
>>> 
microbit-carlos commented 4 months ago

@dpgeorge is it possible that this issue is caused by this? Or is it something else?

dpgeorge commented 3 months ago

The problem here is MicroPython specific, it's because previous I2C and SPI pins are not being released, and new ones not being acquired.

dpgeorge commented 3 months ago

Fixed by af721c760d8663195e749c7e2b4d97d4f632c258