Skip to content

Commit dd86cb0

Browse files
authored
Merge pull request #3448 from hierophect/esp32-sd-fix
ESP32S2 - Fix SPI's SD card issue, add pin protections
2 parents a8558a4 + 00517b2 commit dd86cb0

File tree

3 files changed

+39
-14
lines changed
  • ports
    • esp32s2/common-hal/busio
    • stm/common-hal/busio
  • shared-bindings/busio

3 files changed

+39
-14
lines changed

ports/esp32s2/common-hal/busio/SPI.c

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ static void spi_bus_intr_disable(void *self)
115115
void common_hal_busio_spi_construct(busio_spi_obj_t *self,
116116
const mcu_pin_obj_t * clock, const mcu_pin_obj_t * mosi,
117117
const mcu_pin_obj_t * miso) {
118+
118119
spi_bus_config_t bus_config;
119120
bus_config.mosi_io_num = mosi != NULL ? mosi->number : -1;
120121
bus_config.miso_io_num = miso != NULL ? miso->number : -1;
@@ -212,8 +213,12 @@ void common_hal_busio_spi_never_reset(busio_spi_obj_t *self) {
212213
spi_never_reset[self->host_id] = true;
213214

214215
common_hal_never_reset_pin(self->clock_pin);
215-
common_hal_never_reset_pin(self->MOSI_pin);
216-
common_hal_never_reset_pin(self->MISO_pin);
216+
if (self->MOSI_pin != NULL) {
217+
common_hal_never_reset_pin(self->MOSI_pin);
218+
}
219+
if (self->MISO_pin != NULL) {
220+
common_hal_never_reset_pin(self->MISO_pin);
221+
}
217222
}
218223

219224
bool common_hal_busio_spi_deinited(busio_spi_obj_t *self) {
@@ -236,9 +241,15 @@ void common_hal_busio_spi_deinit(busio_spi_obj_t *self) {
236241
spi_bus_free(self->host_id);
237242

238243
common_hal_reset_pin(self->clock_pin);
239-
common_hal_reset_pin(self->MOSI_pin);
240-
common_hal_reset_pin(self->MISO_pin);
244+
if (self->MOSI_pin != NULL) {
245+
common_hal_reset_pin(self->MOSI_pin);
246+
}
247+
if (self->MISO_pin != NULL) {
248+
common_hal_reset_pin(self->MISO_pin);
249+
}
241250
self->clock_pin = NULL;
251+
self->MISO_pin = NULL;
252+
self->MOSI_pin = NULL;
242253
}
243254

244255
bool common_hal_busio_spi_configure(busio_spi_obj_t *self,
@@ -293,18 +304,37 @@ void common_hal_busio_spi_unlock(busio_spi_obj_t *self) {
293304

294305
bool common_hal_busio_spi_write(busio_spi_obj_t *self,
295306
const uint8_t *data, size_t len) {
307+
if (self->MOSI_pin == NULL) {
308+
mp_raise_ValueError(translate("No MOSI Pin"));
309+
}
296310
return common_hal_busio_spi_transfer(self, data, NULL, len);
297311
}
298312

299313
bool common_hal_busio_spi_read(busio_spi_obj_t *self,
300314
uint8_t *data, size_t len, uint8_t write_value) {
301-
return common_hal_busio_spi_transfer(self, NULL, data, len);
315+
316+
if (self->MISO_pin == NULL) {
317+
mp_raise_ValueError(translate("No MISO Pin"));
318+
}
319+
if (self->MOSI_pin == NULL) {
320+
return common_hal_busio_spi_transfer(self, NULL, data, len);
321+
} else {
322+
memset(data, write_value, len);
323+
return common_hal_busio_spi_transfer(self, data, data, len);
324+
}
302325
}
303326

304327
bool common_hal_busio_spi_transfer(busio_spi_obj_t *self, const uint8_t *data_out, uint8_t *data_in, size_t len) {
305328
if (len == 0) {
306329
return true;
307330
}
331+
// Other than the read special case, stop transfers that don't have a pin/array match
332+
if (!self->MOSI_pin && (data_out != data_in)) {
333+
mp_raise_ValueError(translate("No MOSI Pin"));
334+
}
335+
if (!self->MISO_pin && data_in) {
336+
mp_raise_ValueError(translate("No MISO Pin"));
337+
}
308338

309339
spi_hal_context_t* hal = &self->hal_context;
310340
hal->send_buffer = NULL;

ports/stm/common-hal/busio/SPI.c

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -127,15 +127,6 @@ STATIC int check_pins(busio_spi_obj_t *self,
127127
uint8_t mosi_len = MP_ARRAY_SIZE(mcu_spi_mosi_list);
128128
uint8_t miso_len = MP_ARRAY_SIZE(mcu_spi_miso_list);
129129

130-
//SCK is not optional. MOSI and MISO are
131-
if (!sck) {
132-
mp_raise_ValueError(translate("Must provide SCK pin"));
133-
}
134-
135-
if (!miso && !mosi) {
136-
mp_raise_ValueError(translate("Must provide MISO or MOSI pin"));
137-
}
138-
139130
// Loop over each possibility for SCK. Check whether MISO and/or MOSI can be used on the same peripheral
140131
for (uint i = 0; i < sck_len; i++) {
141132
const mcu_periph_obj_t *mcu_spi_sck = &mcu_spi_sck_list[i];

shared-bindings/busio/SPI.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,10 @@ STATIC mp_obj_t busio_spi_make_new(const mp_obj_type_t *type, size_t n_args, con
9696
const mcu_pin_obj_t* mosi = validate_obj_is_free_pin_or_none(args[ARG_MOSI].u_obj);
9797
const mcu_pin_obj_t* miso = validate_obj_is_free_pin_or_none(args[ARG_MISO].u_obj);
9898

99+
if (!miso && !mosi) {
100+
mp_raise_ValueError(translate("Must provide MISO or MOSI pin"));
101+
}
102+
99103
common_hal_busio_spi_construct(self, clock, mosi, miso);
100104
return MP_OBJ_FROM_PTR(self);
101105
}

0 commit comments

Comments
 (0)