85
85
#define PICO_DEFAULT_SPI 0
86
86
#endif
87
87
88
+ // Assume we won't get an RP2-compatible with 255 GPIO pins
89
+ #define MICROPY_HW_SPI_PIN_UNUSED UINT8_MAX
90
+
88
91
// SPI0 can be GP{0..7,16..23}, SPI1 can be GP{8..15,24..29}.
89
92
#define IS_VALID_PERIPH (spi , pin ) ((((pin) & 8) >> 3) == (spi))
90
93
// GP{2,6,10,14,...}
@@ -125,9 +128,14 @@ static machine_spi_obj_t machine_spi_obj[] = {
125
128
126
129
static void machine_spi_print (const mp_print_t * print , mp_obj_t self_in , mp_print_kind_t kind ) {
127
130
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=" ,
129
132
self -> spi_id , self -> baudrate , self -> polarity , self -> phase , self -> bits ,
130
133
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
+ }
131
139
}
132
140
133
141
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
145
153
{ MP_QSTR_firstbit , MP_ARG_KW_ONLY | MP_ARG_INT , {.u_int = DEFAULT_SPI_FIRSTBIT } },
146
154
{ MP_QSTR_sck , MICROPY_SPI_PINS_ARG_OPTS | MP_ARG_KW_ONLY | MP_ARG_OBJ , {.u_rom_obj = MP_ROM_NONE } },
147
155
{ 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 ) } },
149
157
};
150
158
151
159
// 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
176
184
}
177
185
self -> mosi = mosi ;
178
186
}
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 {
180
191
int miso = mp_hal_get_pin_obj (args [ARG_miso ].u_obj );
181
192
if (!IS_VALID_MISO (self -> spi_id , miso )) {
182
193
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
199
210
self -> baudrate = spi_set_baudrate (self -> spi_inst , self -> baudrate );
200
211
spi_set_format (self -> spi_inst , self -> bits , self -> polarity , self -> phase , self -> firstbit );
201
212
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
+ }
203
216
gpio_set_function (self -> mosi , GPIO_FUNC_SPI );
204
217
}
205
218
0 commit comments