Page 1 of 1

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

Posted: Fri Feb 15, 2019 5:35 am
by XIVN1987

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?

Not on a Pyboard 1.x

Posted: Fri Feb 15, 2019 2:49 pm
by pythoncoder
The Pyboard 1.x has physical 4.7KΩ resistors to 3.3V on both I2C ports so there is no need for special coding.

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

Posted: Fri Feb 15, 2019 4:58 pm
by dhylands
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.

Beware of internal pull-up for I2C.

Posted: Sat Feb 16, 2019 9:46 am
by pythoncoder
Quite. But the internal pullup value is probably too large for reliable I2C operation. This is relevant for anyone using soft I2C.