adafruit / Adafruit_Blinka

Add CircuitPython hardware API and libraries to MicroPython & CPython devices
https://learn.adafruit.com/circuitpython-on-raspberrypi-linux
MIT License
453 stars 340 forks source link

MCP2221 pins driven low during import of board #660

Closed Rainerlan closed 1 year ago

Rainerlan commented 1 year ago

I try to control shutters. Their movement is being triggered by a LOW input signal. Hence, the shutter-input MUST stay high (unless commanded to move otherwise). I connected G0 (later configured as OUTPUT) to the shutter input pin.

Without a pullup at G0, when executing "import board" in python3, G0 pin is being pulled to ground several times. After attaching a pullup (100kOhm) at G0, still after executing "import board", G0 pin is being pulled to ground for ~12ms.

Note: This is before digitalio is being imported and before the pins are being setup as OUTPUT. How can I avoid G0 being pulled to low during startup of the script (import of board)?

caternuson commented 1 year ago

Try suppressing the software reset by setting the environment variable MCP2221_RESET_DELAY to 0. Exactly how to do that is OS dependent. But it'd be the same way you are setting BLINKA_MCP2221 to 1.

caternuson commented 1 year ago

Although the pins are also set to inputs at startup: https://github.com/adafruit/Adafruit_Blinka/blob/802e6874ffa75449f61e229e7aeb00fc5e0022ab/src/adafruit_blinka/microcontroller/mcp2221/mcp2221.py#L64-L66

Rainerlan commented 1 year ago

export BLINKA_MCP2221_RESET_DELAY=-1 does skip the reset - and hence does NOT change the pins output states, which is great!

Second problem was, that changing the direction to OUTPUT, the pins got low. This had to be changed in the code:

https://github.com/adafruit/Adafruit_Blinka/blob/802e6874ffa75449f61e229e7aeb00fc5e0022ab/src/adafruit_blinka/microcontroller/mcp2221/mcp2221.py#L160

to report = bytearray(b"\x50" + b"\x01" * 17 + b"\x00" * 46) # empty set GPIO report

Also I removed the lines 64-66 https://github.com/adafruit/Adafruit_Blinka/blob/802e6874ffa75449f61e229e7aeb00fc5e0022ab/src/adafruit_blinka/microcontroller/mcp2221/mcp2221.py#L64-L66 Don't know why the pins shall be set to GPIs when not explicitly requested...

Rainerlan commented 1 year ago

After applying the above changes, the GPIOs work as I need them to work (default set to HIGH/3.3V when changing them to outputs - also the reset is not messing around with the pins output states)

caternuson commented 1 year ago

Oops, sry, meant negative number for the reset envar.

Good hacks for the other parts! Glad you got it working.

Rainerlan commented 1 year ago

@caternuson Thanks a lot for your support!