Skip to content

Commit 390ee79

Browse files
committed
Add config options to enable onboard i2c pull-ups
* CIRCUITPY_BUSIO_I2C_INTERNAL_PULLUPS config option to enable/use internal i2c pull-ups. * CIRCUITPY_BUSIO_I2C_INTERNAL_PULLUPS_AVAILABLE to indicate availability of internal pull-up resistors. * Add a check to ensure onboard pullups are available when requested. * Enable onboard pullups for pimoroni_keybow2040 * Indicate onboard pullups are available on Pico * NB: Onboard pullups are weak and not ideal
1 parent b8d4f96 commit 390ee79

File tree

6 files changed

+28
-1
lines changed

6 files changed

+28
-1
lines changed

ports/raspberrypi/boards/pimoroni_keybow2040/mpconfigboard.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,4 @@
3939
#define IGNORE_PIN_GPIO27 1
4040
#define IGNORE_PIN_GPIO28 1
4141
#define IGNORE_PIN_GPIO29 1
42+

ports/raspberrypi/boards/pimoroni_keybow2040/mpconfigboard.mk

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,6 @@ CHIP_FAMILY = rp2
99
INTERNAL_FLASH_FILESYSTEM = 1
1010

1111
CIRCUITPY__EVE = 1
12+
13+
CIRCUITPY_REQUIRE_I2C_PULLUPS ?= 0
14+
CIRCUITPY_BUSIO_I2C_INTERNAL_PULLUPS ?= 1

ports/raspberrypi/common-hal/busio/I2C.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,10 @@ void common_hal_busio_i2c_construct(busio_i2c_obj_t *self,
108108
// set up as GPIO by the bitbangio.I2C object.
109109
//
110110
// Sets pins to open drain, high, and input.
111+
#ifndef CIRCUITPY_BUSIO_I2C_INTERNAL_PULLUPS
111112
shared_module_bitbangio_i2c_construct(&self->bitbangio_i2c, scl, sda,
112-
frequency, timeout);
113+
frequency, timeout);
114+
#endif
113115

114116
self->baudrate = i2c_init(self->peripheral, frequency);
115117

@@ -120,6 +122,11 @@ void common_hal_busio_i2c_construct(busio_i2c_obj_t *self,
120122

121123
gpio_set_function(self->scl_pin, GPIO_FUNC_I2C);
122124
gpio_set_function(self->sda_pin, GPIO_FUNC_I2C);
125+
126+
#if CIRCUITPY_BUSIO_I2C_INTERNAL_PULLUPS
127+
gpio_set_pulls(self->scl_pin, true, false);
128+
gpio_set_pulls(self->sda_pin, true, false);
129+
#endif
123130
}
124131

125132
bool common_hal_busio_i2c_deinited(busio_i2c_obj_t *self) {
@@ -163,6 +170,8 @@ void common_hal_busio_i2c_unlock(busio_i2c_obj_t *self) {
163170

164171
uint8_t common_hal_busio_i2c_write(busio_i2c_obj_t *self, uint16_t addr,
165172
const uint8_t *data, size_t len, bool transmit_stop_bit) {
173+
174+
#ifndef CIRCUITPY_BUSIO_I2C_INTERNAL_PULLUPS
166175
if (len == 0) {
167176
// The RP2040 I2C peripheral will not perform 0 byte writes.
168177
// So use bitbangio.I2C to do the write.
@@ -184,6 +193,7 @@ uint8_t common_hal_busio_i2c_write(busio_i2c_obj_t *self, uint16_t addr,
184193

185194
return status;
186195
}
196+
#endif
187197

188198
int result = i2c_write_timeout_us(self->peripheral, addr, data, len, !transmit_stop_bit, BUS_TIMEOUT_US);
189199
if (result == len) {

ports/raspberrypi/mpconfigport.mk

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ else
2323
CIRCUITPY_NEOPIXEL_WRITE = 0
2424
endif
2525

26+
CIRCUITPY_BUSIO_I2C_INTERNAL_PULLUPS_AVAILABLE ?= 1
27+
2628
CIRCUITPY_FRAMEBUFFERIO = 1
2729
CIRCUITPY_FULL_BUILD = 1
2830
CIRCUITPY_AUDIOMP3 ?= 1

py/circuitpy_mpconfig.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,11 @@ extern const struct _mp_obj_module_t bitmaptools_module;
314314
#define BITMAPTOOLS_MODULE
315315
#endif
316316

317+
#if CIRCUITPY_BUSIO_I2C_INTERNAL_PULLUPS && !CIRCUITPY_BUSIO_I2C_INTERNAL_PULLUPS_AVAILABLE
318+
#error "CIRCUITPY_BUSIO_I2C_INTERNAL_PULLUPS requires onboard pullups." \
319+
"Ensure board supports these and set CIRCUITPY_BUSIO_I2C_INTERNAL_PULLUPS_AVAILABLE = 1"
320+
#endif
321+
317322
#if CIRCUITPY_BITOPS
318323
extern const struct _mp_obj_module_t bitops_module;
319324
#define BITOPS_MODULE { MP_OBJ_NEW_QSTR(MP_QSTR_bitops),(mp_obj_t)&bitops_module },

py/circuitpy_mpconfig.mk

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,12 @@ CFLAGS += -DCIRCUITPY_BUILTINS_POW3=$(CIRCUITPY_BUILTINS_POW3)
121121
CIRCUITPY_BUSIO ?= 1
122122
CFLAGS += -DCIRCUITPY_BUSIO=$(CIRCUITPY_BUSIO)
123123

124+
CIRCUITPY_BUSIO_I2C_INTERNAL_PULLUPS ?= 0
125+
CFLAGS += -DCIRCUITPY_BUSIO_I2C_INTERNAL_PULLUPS=$(CIRCUITPY_BUSIO_I2C_INTERNAL_PULLUPS)
126+
127+
CIRCUITPY_BUSIO_I2C_INTERNAL_PULLUPS_AVAILABLE ?= 0
128+
CFLAGS += -DCIRCUITPY_BUSIO_I2C_INTERNAL_PULLUPS_AVAILABLE=$(CIRCUITPY_BUSIO_I2C_INTERNAL_PULLUPS_AVAILABLE)
129+
124130
CIRCUITPY_CAMERA ?= 0
125131
CFLAGS += -DCIRCUITPY_CAMERA=$(CIRCUITPY_CAMERA)
126132

0 commit comments

Comments
 (0)