Skip to content

Commit 9a353a4

Browse files
authored
Merge pull request #5422 from Neradoc/nera-secondary-I2C
Allow multiple board buses
2 parents f2713af + 3970aa5 commit 9a353a4

File tree

14 files changed

+247
-193
lines changed

14 files changed

+247
-193
lines changed

main.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,10 +290,10 @@ STATIC void cleanup_after_vm(supervisor_allocation *heap, mp_obj_t exception) {
290290
keypad_reset();
291291
#endif
292292

293-
// reset_board_busses() first because it may release pins from the never_reset state, so that
293+
// reset_board_buses() first because it may release pins from the never_reset state, so that
294294
// reset_port() can reset them.
295295
#if CIRCUITPY_BOARD
296-
reset_board_busses();
296+
reset_board_buses();
297297
#endif
298298
reset_port();
299299
reset_board();

ports/atmel-samd/boards/hallowing_m0_express/board.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ uint8_t display_init_sequence[] = {
7171
void board_init(void) {
7272
displayio_fourwire_obj_t *bus = &displays[0].fourwire_bus;
7373
bus->base.type = &displayio_fourwire_type;
74-
busio_spi_obj_t *spi = common_hal_board_create_spi();
74+
busio_spi_obj_t *spi = common_hal_board_create_spi(0);
7575
common_hal_busio_spi_never_reset(spi);
7676
common_hal_displayio_fourwire_construct(bus,
7777
spi,

ports/atmel-samd/boards/ugame10/board.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ uint8_t display_init_sequence[] = {
7272
void board_init(void) {
7373
displayio_fourwire_obj_t *bus = &displays[0].fourwire_bus;
7474
bus->base.type = &displayio_fourwire_type;
75-
busio_spi_obj_t *spi = common_hal_board_create_spi();
75+
busio_spi_obj_t *spi = common_hal_board_create_spi(0);
7676
common_hal_displayio_fourwire_construct(bus,
7777
spi,
7878
&pin_PA09, // Command or data

ports/espressif/boards/adafruit_esp32s2_camera/board.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ uint8_t display_init_sequence[] = {
5151
};
5252

5353
void board_init(void) {
54-
busio_spi_obj_t *spi = common_hal_board_create_spi();
54+
busio_spi_obj_t *spi = common_hal_board_create_spi(0);
5555
displayio_fourwire_obj_t *bus = &displays[0].fourwire_bus;
5656
bus->base.type = &displayio_fourwire_type;
5757
common_hal_displayio_fourwire_construct(bus,

ports/espressif/boards/adafruit_feather_esp32s2_tft/board.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ void board_init(void) {
7878
gpio_set_direction(21, GPIO_MODE_DEF_OUTPUT);
7979
gpio_set_level(21, true);
8080

81-
busio_spi_obj_t *spi = common_hal_board_create_spi();
81+
busio_spi_obj_t *spi = common_hal_board_create_spi(0);
8282
displayio_fourwire_obj_t *bus = &displays[0].fourwire_bus;
8383
bus->base.type = &displayio_fourwire_type;
8484

ports/raspberrypi/boards/adafruit_qtpy_rp2040/mpconfigboard.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
#define MICROPY_HW_NEOPIXEL (&pin_GPIO12)
55
#define CIRCUITPY_STATUS_LED_POWER (&pin_GPIO11)
66

7-
#define DEFAULT_I2C_BUS_SCL (&pin_GPIO25)
8-
#define DEFAULT_I2C_BUS_SDA (&pin_GPIO24)
7+
#define CIRCUITPY_BOARD_I2C (2)
8+
#define CIRCUITPY_BOARD_I2C_PIN {{.scl = &pin_GPIO25, .sda = &pin_GPIO24}, \
9+
{.scl = &pin_GPIO23, .sda = &pin_GPIO22}}
910

10-
#define DEFAULT_SPI_BUS_SCK (&pin_GPIO6)
11-
#define DEFAULT_SPI_BUS_MOSI (&pin_GPIO3)
12-
#define DEFAULT_SPI_BUS_MISO (&pin_GPIO4)
11+
#define CIRCUITPY_BOARD_SPI (1)
12+
#define CIRCUITPY_BOARD_SPI_PIN {{.clock = &pin_GPIO6, .mosi = &pin_GPIO3, .miso = &pin_GPIO4}}
1313

14-
#define DEFAULT_UART_BUS_RX (&pin_GPIO5)
15-
#define DEFAULT_UART_BUS_TX (&pin_GPIO20)
14+
#define CIRCUITPY_BOARD_UART (1)
15+
#define CIRCUITPY_BOARD_UART_PIN {{.tx = &pin_GPIO20, .rx = &pin_GPIO5}}

ports/raspberrypi/boards/adafruit_qtpy_rp2040/pins.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#include "shared-bindings/board/__init__.h"
22

3+
CIRCUITPY_BOARD_BUS_SINGLETON(stemma_i2c, i2c, 1)
4+
35
STATIC const mp_rom_map_elem_t board_module_globals_table[] = {
46
CIRCUITPYTHON_BOARD_DICT_STANDARD_ITEMS
57

@@ -47,5 +49,7 @@ STATIC const mp_rom_map_elem_t board_module_globals_table[] = {
4749
{ MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&board_i2c_obj) },
4850
{ MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&board_spi_obj) },
4951
{ MP_ROM_QSTR(MP_QSTR_UART), MP_ROM_PTR(&board_uart_obj) },
52+
53+
{ MP_ROM_QSTR(MP_QSTR_STEMMA_I2C), MP_ROM_PTR(&board_stemma_i2c_obj) },
5054
};
5155
MP_DEFINE_CONST_DICT(board_module_globals, board_module_globals_table);

py/circuitpy_mpconfig.h

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -259,22 +259,41 @@ typedef long mp_off_t;
259259
#error No *_FLASH_FILESYSTEM set!
260260
#endif
261261

262-
// These CIRCUITPY_xxx values should all be defined in the *.mk files as being on or off.
263-
// So if any are not defined in *.mk, they'll throw an error here.
262+
// Default board buses.
264263

265-
#if CIRCUITPY_BOARD
266-
#define BOARD_I2C (defined(DEFAULT_I2C_BUS_SDA) && defined(DEFAULT_I2C_BUS_SCL))
267-
#define BOARD_SPI (defined(DEFAULT_SPI_BUS_SCK) && defined(DEFAULT_SPI_BUS_MISO) && defined(DEFAULT_SPI_BUS_MOSI))
268-
#define BOARD_UART (defined(DEFAULT_UART_BUS_RX) && defined(DEFAULT_UART_BUS_TX))
269-
// I2C and SPI are always allocated off the heap.
270-
#if BOARD_UART
271-
#define BOARD_UART_ROOT_POINTER mp_obj_t shared_uart_bus;
264+
#ifndef CIRCUITPY_BOARD_I2C
265+
#if defined(DEFAULT_I2C_BUS_SCL) && defined(DEFAULT_I2C_BUS_SDA)
266+
#define CIRCUITPY_BOARD_I2C (1)
267+
#define CIRCUITPY_BOARD_I2C_PIN {{.scl = DEFAULT_I2C_BUS_SCL, .sda = DEFAULT_I2C_BUS_SDA}}
272268
#else
273-
#define BOARD_UART_ROOT_POINTER
269+
#define CIRCUITPY_BOARD_I2C (0)
270+
#endif
271+
#endif
272+
273+
#ifndef CIRCUITPY_BOARD_SPI
274+
#if defined(DEFAULT_SPI_BUS_SCK) && defined(DEFAULT_SPI_BUS_MOSI) && defined(DEFAULT_SPI_BUS_MISO)
275+
#define CIRCUITPY_BOARD_SPI (1)
276+
#define CIRCUITPY_BOARD_SPI_PIN {{.clock = DEFAULT_SPI_BUS_SCK, .mosi = DEFAULT_SPI_BUS_MOSI, .miso = DEFAULT_SPI_BUS_MISO}}
277+
#else
278+
#define CIRCUITPY_BOARD_SPI (0)
274279
#endif
280+
#endif
281+
282+
#ifndef CIRCUITPY_BOARD_UART
283+
#if defined(DEFAULT_UART_BUS_TX) && defined(DEFAULT_UART_BUS_RX)
284+
#define CIRCUITPY_BOARD_UART (1)
285+
#define CIRCUITPY_BOARD_UART_PIN {{.tx = DEFAULT_UART_BUS_TX, .rx = DEFAULT_UART_BUS_RX}}
286+
#define BOARD_UART_ROOT_POINTER mp_obj_t board_uart_bus;
275287
#else
288+
#define CIRCUITPY_BOARD_UART (0)
276289
#define BOARD_UART_ROOT_POINTER
277290
#endif
291+
#else
292+
#define BOARD_UART_ROOT_POINTER mp_obj_t board_uart_bus;
293+
#endif
294+
295+
// These CIRCUITPY_xxx values should all be defined in the *.mk files as being on or off.
296+
// So if any are not defined in *.mk, they'll throw an error here.
278297

279298
#if CIRCUITPY_DISPLAYIO
280299
#ifndef CIRCUITPY_DISPLAY_LIMIT

shared-bindings/board/__init__.c

Lines changed: 26 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,15 @@
2828
#include "py/runtime.h"
2929

3030
#include "shared-bindings/board/__init__.h"
31-
#if BOARD_I2C
31+
#if CIRCUITPY_BOARD_I2C
3232
#include "shared-bindings/busio/I2C.h"
3333
#endif
34-
#if BOARD_SPI
34+
#if CIRCUITPY_BOARD_SPI
3535
#include "shared-bindings/busio/SPI.h"
3636
#endif
37+
#if CIRCUITPY_BOARD_UART
38+
#include "shared-bindings/busio/UART.h"
39+
#endif
3740

3841
//| """Board specific pin names
3942
//|
@@ -47,84 +50,57 @@
4750
//| """Board ID string. The unique identifier for the board model in
4851
//| circuitpython, as well as on circuitpython.org.
4952
//| Example: "hallowing_m0_express"."""
50-
//|
5153

5254
//| def I2C() -> busio.I2C:
53-
//| """Returns the `busio.I2C` object for the board designated SDA and SCL pins. It is a singleton."""
55+
//| """Returns the `busio.I2C` object for the board's designated I2C bus(es).
56+
//| The object created is a singleton, and uses the default parameter values for `busio.I2C`."""
5457
//| ...
5558
//|
56-
57-
#if BOARD_I2C
58-
mp_obj_t board_i2c(void) {
59-
mp_obj_t singleton = common_hal_board_get_i2c();
60-
if (singleton != NULL && !common_hal_busio_i2c_deinited(singleton)) {
61-
return singleton;
62-
}
63-
assert_pin_free(DEFAULT_I2C_BUS_SDA);
64-
assert_pin_free(DEFAULT_I2C_BUS_SCL);
65-
return common_hal_board_create_i2c();
59+
#if CIRCUITPY_BOARD_I2C
60+
STATIC mp_obj_t board_i2c_0(void) {
61+
return common_hal_board_create_i2c(0);
6662
}
6763
#else
68-
mp_obj_t board_i2c(void) {
64+
STATIC mp_obj_t board_i2c_0(void) {
6965
mp_raise_NotImplementedError_varg(translate("No default %q bus"), MP_QSTR_I2C);
7066
return MP_ROM_NONE;
7167
}
7268
#endif
73-
MP_DEFINE_CONST_FUN_OBJ_0(board_i2c_obj, board_i2c);
74-
69+
MP_DEFINE_CONST_FUN_OBJ_0(board_i2c_obj, board_i2c_0);
7570

7671
//| def SPI() -> busio.SPI:
77-
//| """Returns the `busio.SPI` object for the board designated SCK, MOSI and MISO pins. It is a
78-
//| singleton."""
72+
//| """Returns the `busio.SPI` object for the board's designated SPI bus(es).
73+
//| The object created is a singleton, and uses the default parameter values for `busio.SPI`."""
7974
//| ...
8075
//|
81-
#if BOARD_SPI
82-
mp_obj_t board_spi(void) {
83-
mp_obj_t singleton = common_hal_board_get_spi();
84-
if (singleton != NULL && !common_hal_busio_spi_deinited(singleton)) {
85-
return singleton;
86-
}
87-
assert_pin_free(DEFAULT_SPI_BUS_SCK);
88-
assert_pin_free(DEFAULT_SPI_BUS_MOSI);
89-
assert_pin_free(DEFAULT_SPI_BUS_MISO);
90-
return common_hal_board_create_spi();
76+
#if CIRCUITPY_BOARD_SPI
77+
STATIC mp_obj_t board_spi_0(void) {
78+
return common_hal_board_create_spi(0);
9179
}
9280
#else
93-
mp_obj_t board_spi(void) {
81+
STATIC mp_obj_t board_spi_0(void) {
9482
mp_raise_NotImplementedError_varg(translate("No default %q bus"), MP_QSTR_SPI);
9583
return MP_ROM_NONE;
9684
}
9785
#endif
98-
MP_DEFINE_CONST_FUN_OBJ_0(board_spi_obj, board_spi);
86+
MP_DEFINE_CONST_FUN_OBJ_0(board_spi_obj, board_spi_0);
9987

10088
//| def UART() -> busio.UART:
101-
//| """Returns the `busio.UART` object for the board designated TX and RX pins. It is a singleton.
102-
//|
103-
//| The object created uses the default parameter values for `busio.UART`. If you need to set
104-
//| parameters that are not changeable after creation, such as ``receiver_buffer_size``,
105-
//| do not use `board.UART()`; instead create a `busio.UART` object explicitly with the
106-
//| desired parameters."""
89+
//| """Returns the `busio.UART` object for the board's designated UART bus(es).
90+
//| The object created is a singleton, and uses the default parameter values for `busio.UART`."""
10791
//| ...
10892
//|
109-
#if BOARD_UART
110-
mp_obj_t board_uart(void) {
111-
mp_obj_t singleton = common_hal_board_get_uart();
112-
if (singleton != NULL) {
113-
return singleton;
114-
}
115-
116-
assert_pin_free(DEFAULT_UART_BUS_RX);
117-
assert_pin_free(DEFAULT_UART_BUS_TX);
118-
119-
return common_hal_board_create_uart();
93+
#if CIRCUITPY_BOARD_UART
94+
STATIC mp_obj_t board_uart_0(void) {
95+
return common_hal_board_create_uart(0);
12096
}
12197
#else
122-
mp_obj_t board_uart(void) {
98+
STATIC mp_obj_t board_uart_0(void) {
12399
mp_raise_NotImplementedError_varg(translate("No default %q bus"), MP_QSTR_UART);
124100
return MP_ROM_NONE;
125101
}
126102
#endif
127-
MP_DEFINE_CONST_FUN_OBJ_0(board_uart_obj, board_uart);
103+
MP_DEFINE_CONST_FUN_OBJ_0(board_uart_obj, board_uart_0);
128104

129105
const mp_obj_module_t board_module = {
130106
.base = { &mp_type_module },

shared-bindings/board/__init__.h

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,21 +35,29 @@
3535
extern const mp_obj_dict_t board_module_globals;
3636
STATIC const MP_DEFINE_STR_OBJ(board_module_id_obj, CIRCUITPY_BOARD_ID);
3737

38-
mp_obj_t common_hal_board_get_i2c(void);
39-
mp_obj_t common_hal_board_create_i2c(void);
38+
bool common_hal_board_is_i2c(mp_obj_t obj);
39+
mp_obj_t common_hal_board_get_i2c(const mp_int_t instance);
40+
mp_obj_t common_hal_board_create_i2c(const mp_int_t instance);
41+
mp_obj_t board_i2c(size_t n_args, const mp_obj_t *args);
4042
MP_DECLARE_CONST_FUN_OBJ_0(board_i2c_obj);
4143

42-
mp_obj_t common_hal_board_get_spi(void);
43-
mp_obj_t common_hal_board_create_spi(void);
44+
bool common_hal_board_is_spi(mp_obj_t obj);
45+
mp_obj_t common_hal_board_get_spi(const mp_int_t instance);
46+
mp_obj_t common_hal_board_create_spi(const mp_int_t instance);
47+
mp_obj_t board_spi(size_t n_args, const mp_obj_t *args);
4448
MP_DECLARE_CONST_FUN_OBJ_0(board_spi_obj);
4549

46-
mp_obj_t common_hal_board_get_uart(void);
47-
mp_obj_t common_hal_board_create_uart(void);
50+
bool common_hal_board_is_uart(mp_obj_t obj);
51+
mp_obj_t common_hal_board_get_uart(const mp_int_t instance);
52+
mp_obj_t common_hal_board_create_uart(const mp_int_t instance);
53+
mp_obj_t board_uart(size_t n_args, const mp_obj_t *args);
4854
MP_DECLARE_CONST_FUN_OBJ_0(board_uart_obj);
4955

50-
mp_obj_t board_i2c(void);
51-
mp_obj_t board_spi(void);
52-
mp_obj_t board_uart(void);
56+
#define CIRCUITPY_BOARD_BUS_SINGLETON(name, bus, instance) \
57+
STATIC mp_obj_t board_##name(void) { \
58+
return common_hal_board_create_##bus(instance); \
59+
} \
60+
MP_DEFINE_CONST_FUN_OBJ_0(board_##name##_obj, board_##name);
5361

5462
#define CIRCUITPYTHON_BOARD_DICT_STANDARD_ITEMS \
5563
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_board) }, \

0 commit comments

Comments
 (0)