Skip to content

ESP32S2 - Fix SPI's SD card issue, add pin protections #3448

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 35 additions & 5 deletions ports/esp32s2/common-hal/busio/SPI.c
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ static void spi_bus_intr_disable(void *self)
void common_hal_busio_spi_construct(busio_spi_obj_t *self,
const mcu_pin_obj_t * clock, const mcu_pin_obj_t * mosi,
const mcu_pin_obj_t * miso) {

spi_bus_config_t bus_config;
bus_config.mosi_io_num = mosi != NULL ? mosi->number : -1;
bus_config.miso_io_num = miso != NULL ? miso->number : -1;
Expand Down Expand Up @@ -212,8 +213,12 @@ void common_hal_busio_spi_never_reset(busio_spi_obj_t *self) {
spi_never_reset[self->host_id] = true;

common_hal_never_reset_pin(self->clock_pin);
common_hal_never_reset_pin(self->MOSI_pin);
common_hal_never_reset_pin(self->MISO_pin);
if (self->MOSI_pin != NULL) {
common_hal_never_reset_pin(self->MOSI_pin);
}
if (self->MISO_pin != NULL) {
common_hal_never_reset_pin(self->MISO_pin);
}
}

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

common_hal_reset_pin(self->clock_pin);
common_hal_reset_pin(self->MOSI_pin);
common_hal_reset_pin(self->MISO_pin);
if (self->MOSI_pin != NULL) {
common_hal_reset_pin(self->MOSI_pin);
}
if (self->MISO_pin != NULL) {
common_hal_reset_pin(self->MISO_pin);
}
self->clock_pin = NULL;
self->MISO_pin = NULL;
self->MOSI_pin = NULL;
}

bool common_hal_busio_spi_configure(busio_spi_obj_t *self,
Expand Down Expand Up @@ -293,18 +304,37 @@ void common_hal_busio_spi_unlock(busio_spi_obj_t *self) {

bool common_hal_busio_spi_write(busio_spi_obj_t *self,
const uint8_t *data, size_t len) {
if (self->MOSI_pin == NULL) {
mp_raise_ValueError(translate("No MOSI Pin"));
}
return common_hal_busio_spi_transfer(self, data, NULL, len);
}

bool common_hal_busio_spi_read(busio_spi_obj_t *self,
uint8_t *data, size_t len, uint8_t write_value) {
return common_hal_busio_spi_transfer(self, NULL, data, len);

if (self->MISO_pin == NULL) {
mp_raise_ValueError(translate("No MISO Pin"));
}
if (self->MOSI_pin == NULL) {
return common_hal_busio_spi_transfer(self, NULL, data, len);
} else {
memset(data, write_value, len);
return common_hal_busio_spi_transfer(self, data, data, len);
}
}

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

spi_hal_context_t* hal = &self->hal_context;
hal->send_buffer = NULL;
Expand Down
9 changes: 0 additions & 9 deletions ports/stm/common-hal/busio/SPI.c
Original file line number Diff line number Diff line change
Expand Up @@ -127,15 +127,6 @@ STATIC int check_pins(busio_spi_obj_t *self,
uint8_t mosi_len = MP_ARRAY_SIZE(mcu_spi_mosi_list);
uint8_t miso_len = MP_ARRAY_SIZE(mcu_spi_miso_list);

//SCK is not optional. MOSI and MISO are
if (!sck) {
mp_raise_ValueError(translate("Must provide SCK pin"));
}

if (!miso && !mosi) {
mp_raise_ValueError(translate("Must provide MISO or MOSI pin"));
}

// Loop over each possibility for SCK. Check whether MISO and/or MOSI can be used on the same peripheral
for (uint i = 0; i < sck_len; i++) {
const mcu_periph_obj_t *mcu_spi_sck = &mcu_spi_sck_list[i];
Expand Down
4 changes: 4 additions & 0 deletions shared-bindings/busio/SPI.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ STATIC mp_obj_t busio_spi_make_new(const mp_obj_type_t *type, size_t n_args, con
const mcu_pin_obj_t* mosi = validate_obj_is_free_pin_or_none(args[ARG_MOSI].u_obj);
const mcu_pin_obj_t* miso = validate_obj_is_free_pin_or_none(args[ARG_MISO].u_obj);

if (!miso && !mosi) {
mp_raise_ValueError(translate("Must provide MISO or MOSI pin"));
}

common_hal_busio_spi_construct(self, clock, mosi, miso);
return MP_OBJ_FROM_PTR(self);
}
Expand Down