reassign i2c pins

The official pyboard running MicroPython.
This is the reference design and main target board for MicroPython.
You can buy one at the store.
Target audience: Users with a pyboard.
Post Reply
ttmetro
Posts: 104
Joined: Mon Jul 31, 2017 12:44 am

reassign i2c pins

Post by ttmetro » Mon Mar 08, 2021 8:00 pm

The default F405 I2C pins are PB6, PB7. How can I use PB8, PB9 instead (they I2C1 alt functions)?

The driver gives an error that pin reassignment is not supported. Presumably it's possible with a custom firmware build?
Bernhard Boser

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: reassign i2c pins

Post by dhylands » Tue Mar 09, 2021 1:17 am

On the 405 the pin assignments are hard-wired, so rather than selecting them when you create the I2C object, you reconfigure the approriate pins instead.

Rather, you'd do something like this:

Code: Select all

i2c = pyb.I2C(1,115200)
# SCL is now on B6
# SDA is now on B7
# Switch B6/B7 back to GPIO inputs
b6 = pyb.Pin('B6', mode=pyb.Pin.OUT)
b6 = pyb.Pin('B6', mode=pyb.Pin.IN)
b7 = pyb.Pin('B7', mode=pyb.Pin.OUT)
b7 = pyb.Pin('B7', mode=pyb.Pin.IN)
# Switch B8/B9 to be I2C (ALT1)
sclPin = pyb.Pin('B8', mode=pyb.Pin.ALT_OPEN_DRAIN, pull=pyb.Pin.PULL_UP, af=pyb.Pin.AF4_I2C1)
sdaPin = pyb.Pin('B9', mode=pyb.Pin.ALT_OPEN_DRAIN, pull=pyb.Pin.PULL_UP, af=pyb.Pin.AF4_I2C1)
If you have external pull-up resistors, then you can set pull=pyb.Pin.PULL_NONE rather than using PULL_UP.

In writing up my answer for this, I discovered a bug in Micropython. When you configure the pin as I2C, and then try to switch it to IN mode, it seems to stay in ALT_OPEN_DRAIN mode. If I switch it to OUT mode first, then I can change it to IN mode. I'll see if I can figure out the problem so that you can set it directly to IN mode.

ttmetro
Posts: 104
Joined: Mon Jul 31, 2017 12:44 am

Re: reassign i2c pins

Post by ttmetro » Tue Mar 09, 2021 7:46 pm

dhylands wrote:
Tue Mar 09, 2021 1:17 am
On the 405 the pin assignments are hard-wired, so rather than selecting them when you create the I2C object, you reconfigure the approriate pins instead.

...
Thanks a lot! Unlike

Code: Select all

#define MICROPY_HW_I2C1_NAME "I2C1"
#define MICROPY_HW_I2C1_SCL (pin_B6)  // SCL
#define MICROPY_HW_I2C1_SDA (pin_B7)  // SDA
in mpconfigboard.h, this does not require custom firmware.

I may need that anyway, though, as I also changed the REPL to UART6

Code: Select all

#define MICROPY_HW_UART_REPL        PYB_UART_6
This may also be possible to configure with MicroPython by placing something like this in boot.py (will try when I get my custom board back):

Code: Select all

pyb.repl_uart(uart)
Discovering ever more features about MicroPython.
Bernhard Boser

Post Reply