opening internal pull-up for I2C is so complex!!

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
User avatar
XIVN1987
Posts: 8
Joined: Wed Jan 04, 2017 1:18 am

opening internal pull-up for I2C is so complex!!

Post by XIVN1987 » Fri Feb 15, 2019 5:35 am

Code: Select all

import pyb, stm, time

i2c = pyb.I2C(1, pyb.I2C.MASTER, baudrate=20000)

''' opening internal pull-up for I2C '''
PUPDR = stm.mem32[stm.GPIOB + stm.GPIO_PUPDR]
PUPDR &= ~((3 << 6*2) | (3 << 7*2))
PUPDR |= ((1 << 6*2) | (0 << 7*2))
stm.mem32[stm.GPIOB + stm.GPIO_PUPDR] = PUPDR
time.sleep(1)
PUPDR |= ((1 << 6*2) | (1 << 7*2))
stm.mem32[stm.GPIOB + stm.GPIO_PUPDR] = PUPDR

while(1): i2c.scan()
note 1: init i2c first, and then open internal pull-pu. otherwise, i2c init will close internal pull-up
note 2: when open internal pull-up, you need make a I2C STOP signal. otherwise, the busy flag of I2C will set, and I2C will not work

is there an esay way to open the internal pull-up for I2C?

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Not on a Pyboard 1.x

Post by pythoncoder » Fri Feb 15, 2019 2:49 pm

The Pyboard 1.x has physical 4.7KΩ resistors to 3.3V on both I2C ports so there is no need for special coding.
Peter Hinch
Index to my micropython libraries.

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

Re: opening internal pull-up for I2C is so complex!!

Post by dhylands » Fri Feb 15, 2019 4:58 pm

And if you did want to set the internal pullups, you could just do:

Code: Select all

b2 = pyb.Pin('B2', pyb.Pin.IN, pull=pyb.Pin.PULL_UP)
b3 = pyb.Pin('B3', pyb.Pin.IN, pull=pyb.Pin.PULL_UP)
before initializing the i2c device.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Beware of internal pull-up for I2C.

Post by pythoncoder » Sat Feb 16, 2019 9:46 am

Quite. But the internal pullup value is probably too large for reliable I2C operation. This is relevant for anyone using soft I2C.
Peter Hinch
Index to my micropython libraries.

Post Reply