Skip to content

Commit cd48c5e

Browse files
authored
Merge pull request #4315 from dhalbert/rp2040-i2c-short-writes
RP2040: Implement short I2C writes (2 bytes or less) using bitbangio
2 parents 514b73b + fb7a0f7 commit cd48c5e

File tree

16 files changed

+154
-44
lines changed

16 files changed

+154
-44
lines changed

ports/raspberrypi/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ SRC_SDK := \
157157
src/common/pico_sync/lock_core.c \
158158
src/common/pico_sync/mutex.c \
159159
src/common/pico_time/time.c \
160+
src/common/pico_time/timeout_helper.c \
160161
src/common/pico_util/pheap.c \
161162
src/rp2_common/hardware_adc/adc.c \
162163
src/rp2_common/hardware_claim/claim.c \

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

Lines changed: 59 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,23 @@
2424
* THE SOFTWARE.
2525
*/
2626

27-
#include "shared-bindings/busio/I2C.h"
2827
#include "py/mperrno.h"
28+
#include "py/mphal.h"
29+
#include "shared-bindings/busio/I2C.h"
2930
#include "py/runtime.h"
3031

3132
#include "shared-bindings/microcontroller/__init__.h"
32-
#include "supervisor/shared/translate.h"
33+
#include "shared-bindings/bitbangio/I2C.h"
3334

3435
#include "src/rp2_common/hardware_gpio/include/hardware/gpio.h"
3536

3637
// Synopsys DW_apb_i2c (v2.01) IP
3738

3839
#define NO_PIN 0xff
3940

41+
// One second
42+
#define BUS_TIMEOUT_US 1000000
43+
4044
STATIC bool never_reset_i2c[2];
4145
STATIC i2c_inst_t* i2c[2] = {i2c0, i2c1};
4246

@@ -94,15 +98,23 @@ void common_hal_busio_i2c_construct(busio_i2c_obj_t *self,
9498
}
9599
#endif
96100

97-
gpio_set_function(sda->number, GPIO_FUNC_I2C);
98-
gpio_set_function(scl->number, GPIO_FUNC_I2C);
101+
// Create a bitbangio.I2C object to do short writes.
102+
// Must be done before setting up the I2C pins, since they will be
103+
// set up as GPIO by the bitbangio.I2C object.
104+
//
105+
// Sets pins to open drain, high, and input.
106+
shared_module_bitbangio_i2c_construct(&self->bitbangio_i2c, scl, sda,
107+
frequency, timeout);
99108

100109
self->baudrate = i2c_init(self->peripheral, frequency);
101110

102-
self->sda_pin = sda->number;
103111
self->scl_pin = scl->number;
104-
claim_pin(sda);
112+
self->sda_pin = sda->number;
105113
claim_pin(scl);
114+
claim_pin(sda);
115+
116+
gpio_set_function(self->scl_pin, GPIO_FUNC_I2C);
117+
gpio_set_function(self->sda_pin, GPIO_FUNC_I2C);
106118
}
107119

108120
bool common_hal_busio_i2c_deinited(busio_i2c_obj_t *self) {
@@ -124,8 +136,7 @@ void common_hal_busio_i2c_deinit(busio_i2c_obj_t *self) {
124136
}
125137

126138
bool common_hal_busio_i2c_probe(busio_i2c_obj_t *self, uint8_t addr) {
127-
uint8_t fake_read = 0;
128-
return i2c_read_blocking(self->peripheral, addr, &fake_read, 1, false) != PICO_ERROR_GENERIC;
139+
return common_hal_busio_i2c_write(self, addr, NULL, 0, true) == 0;
129140
}
130141

131142
bool common_hal_busio_i2c_try_lock(busio_i2c_obj_t *self) {
@@ -147,24 +158,56 @@ void common_hal_busio_i2c_unlock(busio_i2c_obj_t *self) {
147158

148159
uint8_t common_hal_busio_i2c_write(busio_i2c_obj_t *self, uint16_t addr,
149160
const uint8_t *data, size_t len, bool transmit_stop_bit) {
150-
int result = i2c_write_blocking(self->peripheral, addr, data, len, !transmit_stop_bit);
161+
if (len <= 2) {
162+
// The RP2040 I2C peripheral will not do writes 2 bytes or less long.
163+
// So use bitbangio.I2C to do the write.
164+
165+
gpio_set_function(self->scl_pin, GPIO_FUNC_SIO);
166+
gpio_set_function(self->sda_pin, GPIO_FUNC_SIO);
167+
gpio_set_dir(self->scl_pin, GPIO_IN);
168+
gpio_set_dir(self->sda_pin, GPIO_IN);
169+
gpio_put(self->scl_pin, false);
170+
gpio_put(self->sda_pin, false);
171+
172+
uint8_t status = shared_module_bitbangio_i2c_write(&self->bitbangio_i2c,
173+
addr, data, len, transmit_stop_bit);
174+
175+
// The pins must be set back to GPIO_FUNC_I2C in the order given here,
176+
// SCL first, otherwise reads will hang.
177+
gpio_set_function(self->scl_pin, GPIO_FUNC_I2C);
178+
gpio_set_function(self->sda_pin, GPIO_FUNC_I2C);
179+
180+
return status;
181+
}
182+
183+
int result = i2c_write_timeout_us(self->peripheral, addr, data, len, !transmit_stop_bit, BUS_TIMEOUT_US);
151184
if (result == len) {
152185
return 0;
153-
} else if (result == PICO_ERROR_GENERIC) {
154-
return MP_ENODEV;
155186
}
156-
return MP_EIO;
187+
switch (result) {
188+
case PICO_ERROR_GENERIC:
189+
return MP_ENODEV;
190+
case PICO_ERROR_TIMEOUT:
191+
return MP_ETIMEDOUT;
192+
default:
193+
return MP_EIO;
194+
}
157195
}
158196

159197
uint8_t common_hal_busio_i2c_read(busio_i2c_obj_t *self, uint16_t addr,
160198
uint8_t *data, size_t len) {
161-
int result = i2c_read_blocking(self->peripheral, addr, data, len, false);
199+
int result = i2c_read_timeout_us(self->peripheral, addr, data, len, false, BUS_TIMEOUT_US);
162200
if (result == len) {
163201
return 0;
164-
} else if (result == PICO_ERROR_GENERIC) {
165-
return MP_ENODEV;
166202
}
167-
return MP_EIO;
203+
switch (result) {
204+
case PICO_ERROR_GENERIC:
205+
return MP_ENODEV;
206+
case PICO_ERROR_TIMEOUT:
207+
return MP_ETIMEDOUT;
208+
default:
209+
return MP_EIO;
210+
}
168211
}
169212

170213
void common_hal_busio_i2c_never_reset(busio_i2c_obj_t *self) {

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#define MICROPY_INCLUDED_RASPBERRYPI_COMMON_HAL_BUSIO_I2C_H
2929

3030
#include "common-hal/microcontroller/Pin.h"
31+
#include "shared-module/bitbangio/I2C.h"
3132

3233
#include "py/obj.h"
3334

@@ -36,6 +37,7 @@
3637
typedef struct {
3738
mp_obj_base_t base;
3839
i2c_inst_t * peripheral;
40+
bitbangio_i2c_obj_t bitbangio_i2c;
3941
bool has_lock;
4042
uint baudrate;
4143
uint8_t scl_pin;

shared-bindings/bitbangio/I2C.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
#include "py/obj.h"
3131

3232
#include "common-hal/microcontroller/Pin.h"
33-
#include "shared-module/bitbangio/types.h"
33+
#include "shared-module/bitbangio/I2C.h"
3434

3535
// Type object used in Python. Should be shared between ports.
3636
extern const mp_obj_type_t bitbangio_i2c_type;

shared-bindings/bitbangio/OneWire.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
#define MICROPY_INCLUDED_SHARED_BINDINGS_BITBANGIO_ONEWIRE_H
2929

3030
#include "common-hal/microcontroller/Pin.h"
31-
#include "shared-module/bitbangio/types.h"
31+
#include "shared-module/bitbangio/OneWire.h"
3232

3333
extern const mp_obj_type_t bitbangio_onewire_type;
3434

shared-bindings/bitbangio/SPI.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
#include "py/obj.h"
3131

3232
#include "common-hal/microcontroller/Pin.h"
33-
#include "shared-module/bitbangio/types.h"
33+
#include "shared-module/bitbangio/SPI.h"
3434

3535
// Type object used in Python. Should be shared between ports.
3636
extern const mp_obj_type_t bitbangio_spi_type;

shared-bindings/bitbangio/__init__.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
#include "shared-bindings/bitbangio/I2C.h"
3737
#include "shared-bindings/bitbangio/OneWire.h"
3838
#include "shared-bindings/bitbangio/SPI.h"
39-
#include "shared-module/bitbangio/types.h"
4039

4140
#include "py/runtime.h"
4241

shared-module/bitbangio/I2C.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
#include "common-hal/microcontroller/Pin.h"
3333
#include "shared-bindings/microcontroller/__init__.h"
3434
#include "shared-bindings/digitalio/DigitalInOut.h"
35-
#include "shared-module/bitbangio/types.h"
3635
#include "supervisor/shared/translate.h"
3736

3837
STATIC void delay(bitbangio_i2c_obj_t *self) {

shared-module/bitbangio/I2C.h

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2016 Scott Shawcroft
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#ifndef MICROPY_INCLUDED_SHARED_MODULE_BITBANGIO_I2C_H
28+
#define MICROPY_INCLUDED_SHARED_MODULE_BITBANGIO_I2C_H
29+
30+
#include "common-hal/digitalio/DigitalInOut.h"
31+
32+
#include "py/obj.h"
33+
34+
typedef struct {
35+
mp_obj_base_t base;
36+
digitalio_digitalinout_obj_t scl;
37+
digitalio_digitalinout_obj_t sda;
38+
uint32_t us_delay;
39+
uint32_t us_timeout;
40+
volatile bool locked;
41+
} bitbangio_i2c_obj_t;
42+
43+
#endif // MICROPY_INCLUDED_SHARED_MODULE_BITBANGIO_I2C_H

shared-module/bitbangio/OneWire.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
#include "shared-bindings/bitbangio/OneWire.h"
2929
#include "shared-bindings/microcontroller/__init__.h"
3030
#include "shared-bindings/digitalio/DigitalInOut.h"
31-
#include "shared-module/bitbangio/types.h"
3231

3332
// Durations are taken from here: https://www.maximintegrated.com/en/app-notes/index.mvp/id/126
3433

shared-module/bitbangio/OneWire.h

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2016 Scott Shawcroft
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#ifndef MICROPY_INCLUDED_SHARED_MODULE_BITBANGIO_ONEWIRE_H
28+
#define MICROPY_INCLUDED_SHARED_MODULE_BITBANGIO_ONEWIRE_H
29+
30+
#include "common-hal/digitalio/DigitalInOut.h"
31+
32+
#include "py/obj.h"
33+
34+
typedef struct {
35+
mp_obj_base_t base;
36+
digitalio_digitalinout_obj_t pin;
37+
} bitbangio_onewire_obj_t;
38+
39+
#endif // MICROPY_INCLUDED_SHARED_MODULE_BITBANGIO_ONEWIRE_H

shared-module/bitbangio/SPI.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@
2929
#include "py/runtime.h"
3030

3131
#include "common-hal/microcontroller/Pin.h"
32-
#include "shared-bindings/microcontroller/__init__.h"
32+
#include "shared-bindings/bitbangio/SPI.h"
3333
#include "shared-bindings/digitalio/DigitalInOut.h"
34-
#include "shared-module/bitbangio/types.h"
34+
#include "shared-bindings/microcontroller/__init__.h"
3535
#include "supervisor/shared/translate.h"
3636

3737
#define MAX_BAUDRATE (common_hal_mcu_get_clock_frequency() / 48)

shared-module/bitbangio/types.h renamed to shared-module/bitbangio/SPI.h

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,27 +24,13 @@
2424
* THE SOFTWARE.
2525
*/
2626

27-
#ifndef MICROPY_INCLUDED_SHARED_MODULE_BITBANGIO_TYPES_H
28-
#define MICROPY_INCLUDED_SHARED_MODULE_BITBANGIO_TYPES_H
27+
#ifndef MICROPY_INCLUDED_SHARED_MODULE_BITBANGIO_SPI_H
28+
#define MICROPY_INCLUDED_SHARED_MODULE_BITBANGIO_SPI_H
2929

3030
#include "common-hal/digitalio/DigitalInOut.h"
3131

3232
#include "py/obj.h"
3333

34-
typedef struct {
35-
mp_obj_base_t base;
36-
digitalio_digitalinout_obj_t scl;
37-
digitalio_digitalinout_obj_t sda;
38-
uint32_t us_delay;
39-
uint32_t us_timeout;
40-
volatile bool locked;
41-
} bitbangio_i2c_obj_t;
42-
43-
typedef struct {
44-
mp_obj_base_t base;
45-
digitalio_digitalinout_obj_t pin;
46-
} bitbangio_onewire_obj_t;
47-
4834
typedef struct {
4935
mp_obj_base_t base;
5036
digitalio_digitalinout_obj_t clock;
@@ -58,4 +44,4 @@ typedef struct {
5844
volatile bool locked:1;
5945
} bitbangio_spi_obj_t;
6046

61-
#endif // MICROPY_INCLUDED_SHARED_MODULE_BITBANGIO_TYPES_H
47+
#endif // MICROPY_INCLUDED_SHARED_MODULE_BITBANGIO_SPI_H

shared-module/busio/I2C.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
#ifndef MICROPY_INCLUDED_ATMEL_SAMD_SHARED_MODULE_BUSIO_I2C_H
2828
#define MICROPY_INCLUDED_ATMEL_SAMD_SHARED_MODULE_BUSIO_I2C_H
2929

30-
#include "shared-module/bitbangio/types.h"
30+
#include "shared-module/bitbangio/I2C.h"
3131

3232
#include "py/obj.h"
3333

shared-module/busio/OneWire.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
#ifndef MICROPY_INCLUDED_ATMEL_SAMD_SHARED_MODULE_BUSIO_ONEWIRE_H
2828
#define MICROPY_INCLUDED_ATMEL_SAMD_SHARED_MODULE_BUSIO_ONEWIRE_H
2929

30-
#include "shared-module/bitbangio/types.h"
30+
#include "shared-module/bitbangio/OneWire.h"
3131

3232
#include "py/obj.h"
3333

supervisor/shared/rgb_led_status.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ static uint8_t status_apa102_color[APA102_BUFFER_LENGTH] = {0, 0, 0, 0, 0xff, 0,
4747

4848
#if CIRCUITPY_BITBANG_APA102
4949
#include "shared-bindings/bitbangio/SPI.h"
50-
#include "shared-module/bitbangio/types.h"
5150
static bitbangio_spi_obj_t status_apa102 = {
5251
.base = {
5352
.type = &bitbangio_spi_type,

0 commit comments

Comments
 (0)