Merge pull request #3448 from hierophect/esp32-sd-fix

ESP32S2 - Fix SPI's SD card issue, add pin protections
This commit is contained in:
Scott Shawcroft 2020-09-23 12:30:01 -07:00 committed by GitHub
commit dd86cb00ad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 39 additions and 14 deletions

View File

@ -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;
@ -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) {
@ -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,
@ -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;

View File

@ -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];

View File

@ -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);
}