Skip to content

Commit 67feebe

Browse files
committed
working on being able to share the SPI Bus
1 parent eaa6c27 commit 67feebe

File tree

1 file changed

+12
-34
lines changed

1 file changed

+12
-34
lines changed

micropy_updates/esp32/machine_sdcard.c

Lines changed: 12 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ typedef struct _sdcard_obj_t {
8181
// The card structure duplicates the host. It's not clear if we
8282
// can avoid this given the way that it is copied.
8383
sdmmc_card_t card;
84+
sdspi_device_config_t dev_config;
85+
sdspi_dev_handle_t sdspi_handle;
8486
} sdcard_card_obj_t;
8587

8688

@@ -90,23 +92,6 @@ typedef struct _sdcard_obj_t {
9092
#define _SECTOR_SIZE(self) (self->card.csd.sector_size)
9193

9294

93-
static const sdspi_device_config_t spi_dev_defaults[2] = {
94-
{
95-
#if CONFIG_IDF_TARGET_ESP32
96-
.host_id = VSPI_HOST,
97-
.gpio_cs = GPIO_NUM_5,
98-
#else
99-
.host_id = SPI3_HOST,
100-
.gpio_cs = GPIO_NUM_34,
101-
#endif
102-
.gpio_cd = SDSPI_SLOT_NO_CD,
103-
.gpio_wp = SDSPI_SLOT_NO_WP,
104-
.gpio_int = SDSPI_SLOT_NO_INT,
105-
},
106-
SDSPI_DEVICE_CONFIG_DEFAULT(), // HSPI (ESP32) / SPI2 (ESP32S3)
107-
};
108-
109-
11095
static esp_err_t sdcard_ensure_card_init(sdcard_card_obj_t *self, bool force) {
11196
if (force || !(self->flags & SDCARD_CARD_FLAGS_CARD_INIT_DONE)) {
11297

@@ -178,7 +163,7 @@ static mp_obj_t machine_sdcard_make_new(const mp_obj_type_t *type, size_t n_args
178163
is_spi = true;
179164
} else {
180165
slot_num = args[ARG_slot].u_int;
181-
is_spi = false
166+
is_spi = false;
182167

183168
if (slot_num < 0 || slot_num > 3) {
184169
mp_raise_ValueError(MP_ERROR_TEXT("slot number must be between 0 and 3 inclusive"));
@@ -202,30 +187,23 @@ static mp_obj_t machine_sdcard_make_new(const mp_obj_type_t *type, size_t n_args
202187

203188
if (is_spi) {
204189
// Needs to match spi_dev_defaults above.
205-
#if CONFIG_IDF_TARGET_ESP32
206-
self->host.slot = slot_num ? HSPI_HOST : VSPI_HOST;
207-
#else
208-
self->host.slot = slot_num ? SPI2_HOST : SPI3_HOST;
209-
#endif
190+
self->host.slot = slot_num;
210191
}
211192

212193
check_esp_err(self->host.init());
213194
self->flags |= SDCARD_CARD_FLAGS_HOST_INIT_DONE;
214195

215196
if (is_spi) {
216197
// SPI interface
217-
spi_host_device_t spi_host_id = self->host.slot;
218-
sdspi_device_config_t dev_config = spi_dev_defaults[slot_num];
219-
220-
dev_config.gpio_cs = (int)args[ARG_cs].u_int;
221-
dev_config.gpio_cd = (int)args[ARG_cd].u_int;
222-
dev_config.gpio_wp = (int)args[ARG_wp].u_int;
223-
224-
sdspi_dev_handle_t sdspi_handle;
225-
esp_err_t ret = sdspi_host_init_device(&dev_config, &sdspi_handle);
226-
227-
check_esp_err(ret);
198+
self->dev_config = (sdspi_device_config_t){
199+
.host_id = (spi_host_device_t)self->host.slot,
200+
.gpio_cs = (int)args[ARG_cs].u_int
201+
.gpio_cd = (int)args[ARG_cd].u_int,
202+
.gpio_wp = (int)args[ARG_wp].u_int,
203+
.gpio_int = SDSPI_SLOT_NO_INT,
204+
}
228205

206+
check_esp_err(sdspi_host_init_device(&self->dev_config, &self->sdspi_handle));
229207
} else {
230208
// SD/MMC interface
231209
sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT();

0 commit comments

Comments
 (0)