8080
8181#endif
8282
83+ // Assume we won't get an RP2-compatible with 255 GPIO pins
84+ #define MICROPY_HW_SPI_PIN_UNUSED UINT8_MAX
85+
8386// SPI0 can be GP{0..7,16..23}, SPI1 can be GP{8..15,24..29}.
8487#define IS_VALID_PERIPH (spi , pin ) ((((pin) & 8) >> 3) == (spi))
8588// GP{2,6,10,14,...}
@@ -120,9 +123,14 @@ static machine_spi_obj_t machine_spi_obj[] = {
120123
121124static void machine_spi_print (const mp_print_t * print , mp_obj_t self_in , mp_print_kind_t kind ) {
122125 machine_spi_obj_t * self = MP_OBJ_TO_PTR (self_in );
123- mp_printf (print , "SPI(%u, baudrate=%u, polarity=%u, phase=%u, bits=%u, sck=%u, mosi=%u, miso=%u) " ,
126+ mp_printf (print , "SPI(%u, baudrate=%u, polarity=%u, phase=%u, bits=%u, sck=%u, mosi=%u, miso=" ,
124127 self -> spi_id , self -> baudrate , self -> polarity , self -> phase , self -> bits ,
125- self -> sck , self -> mosi , self -> miso );
128+ self -> sck , self -> mosi );
129+ if (self -> miso == MICROPY_HW_SPI_PIN_UNUSED ) {
130+ mp_printf (print , "None)" );
131+ } else {
132+ mp_printf (print , "%u)" , self -> miso );
133+ }
126134}
127135
128136mp_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 ) {
@@ -140,7 +148,7 @@ mp_obj_t machine_spi_make_new(const mp_obj_type_t *type, size_t n_args, size_t n
140148 { MP_QSTR_firstbit , MP_ARG_KW_ONLY | MP_ARG_INT , {.u_int = DEFAULT_SPI_FIRSTBIT } },
141149 { MP_QSTR_sck , MICROPY_SPI_PINS_ARG_OPTS | MP_ARG_KW_ONLY | MP_ARG_OBJ , {.u_rom_obj = MP_ROM_NONE } },
142150 { MP_QSTR_mosi , MICROPY_SPI_PINS_ARG_OPTS | MP_ARG_KW_ONLY | MP_ARG_OBJ , {.u_rom_obj = MP_ROM_NONE } },
143- { MP_QSTR_miso , MICROPY_SPI_PINS_ARG_OPTS | MP_ARG_KW_ONLY | MP_ARG_OBJ , {.u_rom_obj = MP_ROM_NONE } },
151+ { MP_QSTR_miso , MICROPY_SPI_PINS_ARG_OPTS | MP_ARG_KW_ONLY | MP_ARG_OBJ , {.u_rom_obj = MP_ROM_INT ( -1 ) } },
144152 };
145153
146154 // Parse the arguments.
@@ -171,7 +179,10 @@ mp_obj_t machine_spi_make_new(const mp_obj_type_t *type, size_t n_args, size_t n
171179 }
172180 self -> mosi = mosi ;
173181 }
174- if (args [ARG_miso ].u_obj != mp_const_none ) {
182+
183+ if (args [ARG_miso ].u_obj == mp_const_none ) {
184+ self -> miso = MICROPY_HW_SPI_PIN_UNUSED ;
185+ } else if (args [ARG_miso ].u_obj != MP_OBJ_NEW_SMALL_INT (-1 )) {
175186 int miso = mp_hal_get_pin_obj (args [ARG_miso ].u_obj );
176187 if (!IS_VALID_MISO (self -> spi_id , miso )) {
177188 mp_raise_ValueError (MP_ERROR_TEXT ("bad MISO pin" ));
@@ -194,7 +205,9 @@ mp_obj_t machine_spi_make_new(const mp_obj_type_t *type, size_t n_args, size_t n
194205 self -> baudrate = spi_set_baudrate (self -> spi_inst , self -> baudrate );
195206 spi_set_format (self -> spi_inst , self -> bits , self -> polarity , self -> phase , self -> firstbit );
196207 gpio_set_function (self -> sck , GPIO_FUNC_SPI );
197- gpio_set_function (self -> miso , GPIO_FUNC_SPI );
208+ if (self -> miso != MICROPY_HW_SPI_PIN_UNUSED ) {
209+ gpio_set_function (self -> miso , GPIO_FUNC_SPI );
210+ }
198211 gpio_set_function (self -> mosi , GPIO_FUNC_SPI );
199212 }
200213
0 commit comments