Skip to content

Commit 8975d9e

Browse files
committed
rp2/machine_spi: Allow MISO to be unspecified.
It's common with write-only SPI displays for MISO to be repurposed as a register select or data/command pin. While that was possible by setting up the pin after a call to `machine.SPI()` this change makes `machine.SPI(miso=None)` explicit. Signed-off-by: Phil Howard <[email protected]>
1 parent 28eb57b commit 8975d9e

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

Diff for: ports/rp2/machine_spi.c

+17-4
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@
8585
#define PICO_DEFAULT_SPI 0
8686
#endif
8787

88+
// Assume we won't get an RP2-compatible with 255 GPIO pins
89+
#define MICROPY_HW_SPI_PIN_UNUSED UINT8_MAX
90+
8891
// SPI0 can be GP{0..7,16..23}, SPI1 can be GP{8..15,24..29}.
8992
#define IS_VALID_PERIPH(spi, pin) ((((pin) & 8) >> 3) == (spi))
9093
// GP{2,6,10,14,...}
@@ -125,9 +128,14 @@ static machine_spi_obj_t machine_spi_obj[] = {
125128

126129
static void machine_spi_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
127130
machine_spi_obj_t *self = MP_OBJ_TO_PTR(self_in);
128-
mp_printf(print, "SPI(%u, baudrate=%u, polarity=%u, phase=%u, bits=%u, sck=%u, mosi=%u, miso=%u)",
131+
mp_printf(print, "SPI(%u, baudrate=%u, polarity=%u, phase=%u, bits=%u, sck=%u, mosi=%u, miso=",
129132
self->spi_id, self->baudrate, self->polarity, self->phase, self->bits,
130133
self->sck, self->mosi, self->miso);
134+
if (self->miso == MICROPY_HW_SPI_PIN_UNUSED) {
135+
mp_printf(print, "None)");
136+
} else {
137+
mp_printf(print, "%u)", self->miso);
138+
}
131139
}
132140

133141
mp_obj_t machine_spi_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
@@ -145,7 +153,7 @@ mp_obj_t machine_spi_make_new(const mp_obj_type_t *type, size_t n_args, size_t n
145153
{ MP_QSTR_firstbit, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = DEFAULT_SPI_FIRSTBIT} },
146154
{ MP_QSTR_sck, MICROPY_SPI_PINS_ARG_OPTS | MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} },
147155
{ MP_QSTR_mosi, MICROPY_SPI_PINS_ARG_OPTS | MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} },
148-
{ MP_QSTR_miso, MICROPY_SPI_PINS_ARG_OPTS | MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} },
156+
{ MP_QSTR_miso, MICROPY_SPI_PINS_ARG_OPTS | MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_INT(-1)} },
149157
};
150158

151159
// Parse the arguments.
@@ -176,7 +184,10 @@ mp_obj_t machine_spi_make_new(const mp_obj_type_t *type, size_t n_args, size_t n
176184
}
177185
self->mosi = mosi;
178186
}
179-
if (args[ARG_miso].u_obj != mp_const_none) {
187+
188+
if (args[ARG_miso].u_obj == mp_const_none) {
189+
self->miso = MICROPY_HW_SPI_PIN_UNUSED;
190+
} else {
180191
int miso = mp_hal_get_pin_obj(args[ARG_miso].u_obj);
181192
if (!IS_VALID_MISO(self->spi_id, miso)) {
182193
mp_raise_ValueError(MP_ERROR_TEXT("bad MISO pin"));
@@ -199,7 +210,9 @@ mp_obj_t machine_spi_make_new(const mp_obj_type_t *type, size_t n_args, size_t n
199210
self->baudrate = spi_set_baudrate(self->spi_inst, self->baudrate);
200211
spi_set_format(self->spi_inst, self->bits, self->polarity, self->phase, self->firstbit);
201212
gpio_set_function(self->sck, GPIO_FUNC_SPI);
202-
gpio_set_function(self->miso, GPIO_FUNC_SPI);
213+
if (self->miso != MICROPY_HW_SPI_PIN_UNUSED) {
214+
gpio_set_function(self->miso, GPIO_FUNC_SPI);
215+
}
203216
gpio_set_function(self->mosi, GPIO_FUNC_SPI);
204217
}
205218

0 commit comments

Comments
 (0)