10000 added slot tracking and never_reset logic · eric321/circuitpython@36c956a · GitHub
[go: up one dir, main page]

Skip to content

Commit 36c956a

Browse files
committed
added slot tracking and never_reset logic
1 parent 0ea2dd3 commit 36c956a

File tree

3 files changed

+55
-26
lines changed

3 files changed

+55
-26
lines changed

ports/espressif/boards/makerfabs_tft7/pins.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,9 @@ static const mp_rom_map_elem_t board_module_globals_table[] = {
121121

122122
// IO10 <> SD_CS is cut at factory (non-placed resistor position R34) and pulled up.
123123
// Permanent SDIO 1-bit mode?
124+
{ MP_ROM_QSTR(MP_QSTR_SDIO_CLK), MP_ROM_PTR(&pin_GPIO12) },
124125
{ MP_ROM_QSTR(MP_QSTR_SDIO_CMD), MP_ROM_PTR(&pin_GPIO11) },
125126
{ MP_ROM_QSTR(MP_QSTR_SDIO_D0), MP 10000 _ROM_PTR(&pin_GPIO13) },
126-
{ MP_ROM_QSTR(MP_QSTR_SDIO_CLK), MP_ROM_PTR(&pin_GPIO12) },
127127

128128
{ MP_ROM_QSTR(MP_QSTR_DISPLAY), MP_ROM_PTR(&displays[0].display) },
129129

ports/espressif/common-hal/sdioio/SDCard.c

Lines changed: 52 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
#include "esp_log.h"
2020
static const char *TAG = "SDCard.c";
2121

22-
static bool sdio_host_init = false;
22+
static bool slot_in_use[2];
23+
static bool never_reset_sdio[2] = { false, false };
2324

2425
static void common_hal_sdioio_sdcard_check_for_deinit(sdioio_sdcard_obj_t *self) {
2526
if (common_hal_sdioio_sdcard_deinited(self)) {
@@ -32,8 +33,12 @@ static int check_pins(const mcu_pin_obj_t *clock, const mcu_pin_obj_t *command,
3233
// ESP32-S3 and P4 can use any pin for any SDMMC func in either slot
3334
// Default to SLOT_1 for SD cards
3435
ESP_LOGI(TAG, "Using chip with CONFIG_SOC_SDMMC_USE_GPIO_MATRIX");
35-
return SDMMC_HOST_SLOT_1;
36-
#endif
36+
if (!slot_in_use[1]) {
37+
return SDMMC_HOST_SLOT_1;
38+
} else {
39+
return SDMMC_HOST_SLOT_0;
40+
}
41+
#else
3742
if (command->number == GPIO_NUM_11 && clock->number == GPIO_NUM_6 && data[0]->number == GPIO_NUM_7) {
3843
// Might be slot 0
3944
if (num_data == 1 || (num_data == 4 && data[1]->number == GPIO_NUM_8 && data[2]->number == GPIO_NUM_9 && data[3]->number == GPIO_NUM_10)) {
@@ -46,6 +51,15 @@ static int check_pins(const mcu_pin_obj_t *clock, const mcu_pin_obj_t *command,
4651
}
4752
}
4853
return -1;
54+
#endif
55+
}
56+
57+
uint8_t get_slot_index(sdioio_sdcard_obj_t *self) {
58+
if (self->slot == SDMMC_HOST_SLOT_0) {
59+
return 0;
60+
} else {
61+
return 1;
62+
}
4963
}
5064

5165
void common_hal_sdioio_sdcard_construct(sdioio_sdcard_obj_t *self,
@@ -72,15 +86,14 @@ void common_hal_sdioio_sdcard_construct(sdioio_sdcard_obj_t *self,
7286
host.max_freq_khz = frequency / 1000;
7387

7488
sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT();
75-
slot_config.width = 1;
89+
slot_config.width = num_data;
7690
slot_config.clk = clock->number;
7791
self->clock = clock->number;
7892
slot_config.cmd = command->number;
7993
self->command = command->number;
8094
slot_config.d0 = data[0]->number;
8195
self->data[0] = data[0]->number;
8296
if (num_data == 4) {
83-
slot_config.width = 4;
8497
slot_config.d1 = data[1]->number;
8598
self->data[1] = data[1]->number;
8699
slot_config.d2 = data[2]->number;
@@ -93,13 +106,11 @@ void common_hal_sdioio_sdcard_construct(sdioio_sdcard_obj_t *self,
93106
slot_config.width, slot_config.clk, slot_config.cmd,
94107
slot_config.d0, slot_config.d1, slot_config.d2, slot_config.d3);
95108

96-
if (!sdio_host_init) {
97-
err = sdmmc_host_init();
98-
if (err != ESP_OK) {
99-
mp_raise_OSError_msg_varg(MP_ERROR_TEXT("SDIO Init Error %x"), err);
100-
}
101-
// sdio_host_init = true;
109+
err = sdmmc_host_init();
110+
if (err != ESP_OK) {
111+
mp_raise_OSError_msg_varg(MP_ERROR_TEXT("SDIO Init Error %x"), err);
102112
}
113+
103114
err = sdmmc_host_init_slot(sd_slot, &slot_config);
104115
if (err != ESP_OK) {
105116
ESP_LOGW(TAG, "Failed to initialize SDMMC slot: %x", err);
@@ -115,13 +126,12 @@ void common_hal_sdioio_sdcard_construct(sdioio_sdcard_obj_t *self,
115126

116127
common_hal_sdioio_sdcard_check_for_deinit(self);
117128

129+
slot_in_use[get_slot_index(self)] = true;
130+
118131
claim_pin(clock);
119132
claim_pin(command);
120-
claim_pin(data[0]);
121-
if (num_data == 4) {
122-
claim_pin(data[1]);
123-
claim_pin(data[2]);
124-
claim_pin(data[3]);
133+
for (size_t i = 0; i < num_data; i++) {
134+
claim_pin(data[i]);
125135
}
126136

127137
ESP_LOGI(TAG, "Initialized SD card with ID %d:%d-%s",
@@ -194,30 +204,47 @@ void common_hal_sdioio_sdcard_deinit(sdioio_sdcard_obj_t *self) {
194204
if (common_hal_sdioio_sdcard_deinited(self)) {
195205
return;
196206
}
207+
208+
never_reset_sdio[get_slot_index(self)] = false;
209+
slot_in_use[get_slot_index(self)] = false;
210+
197211
sdmmc_host_deinit();
198212
reset_pin_number(self->command);
199213
self->command = COMMON_HAL_MCU_NO_PIN;
200214
reset_pin_number(self->clock);
201215
self->clock = COMMON_HAL_MCU_NO_PIN;
202-
reset_pin_number(self->data[0]);
203-
self->data[0] = COMMON_HAL_MCU_NO_PIN;
204-
if (self->num_data == 4) {
205-
reset_pin_number(self->data[1]);
206-
self->data[1] = COMMON_HAL_MCU_NO_PIN;
207-
reset_pin_number(self->data[2]);
208-
self->data[2] = COMMON_HAL_MCU_NO_PIN;
209-
reset_pin_number(self->data[3]);
210-
self->data[3] = COMMON_HAL_MCU_NO_PIN;
216+
for (size_t i = 0; i < self->num_data; i++) {
217+
reset_pin_number(self->data[i]);
218+
self->data[i] = COMMON_HAL_MCU_NO_PIN;
211219
}
220+
212221
return;
213222
}
214223

215224
void common_hal_sdioio_sdcard_never_reset(sdioio_sdcard_obj_t *self) {
216225
if (common_hal_sdioio_sdcard_deinited(self)) {
217226
return;
218227
}
228+
229+
if (never_reset_sdio[get_slot_index(self)]) {
230+
return;
231+
}
232+
233+
never_reset_sdio[get_slot_index(self)] = true;
234+
235+
never_reset_pin_number(self->command);
236+
never_reset_pin_number(self->clock);
237+
238+
for (size_t i = 0; i < self->num_data; i++) {
239+
never_reset_pin_number(self->data[i]);
240+
}
219241
}
220242

221243
void sdioio_reset() {
244+
for (size_t i = 0; i < MP_ARRAY_SIZE(slot_in_use); i++) {
245+
if (!never_reset_sdio[i]) {
246+
slot_in_use[i] = false;
247+
}
248+
}
222249
return;
223250
}

ports/espressif/common-hal/sdioio/SDCard.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,5 @@ typedef struct {
2424
} sdioio_sdcard_obj_t;
2525

2626
void sdioio_reset(void);
27+
28+
uint8_t get_slot_index(sdioio_sdcard_obj_t *);

0 commit comments

Comments
 (0)
0