Skip to content

Commit f798eb3

Browse files
authored
SDMMC - Peripheral manager implementation (#8289)
* sdmmc periman implemented * save pins when SOC_SDMMC_USE_IOMUX
1 parent 92cf003 commit f798eb3

File tree

2 files changed

+66
-12
lines changed

2 files changed

+66
-12
lines changed

Diff for: libraries/SD_MMC/src/SD_MMC.cpp

+62-9
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@
2424
#include "sdmmc_cmd.h"
2525
#include "soc/sdmmc_pins.h"
2626
#include "ff.h"
27+
#include "esp32-hal-periman.h"
2728

2829
using namespace fs;
2930

30-
3131
SDMMCFS::SDMMCFS(FSImplPtr impl)
3232
: FS(impl), _card(nullptr)
3333
{
@@ -40,7 +40,25 @@ SDMMCFS::SDMMCFS(FSImplPtr impl)
4040
_pin_d2 = SDMMC_D2;
4141
_pin_d3 = SDMMC_D3;
4242
#endif // BOARD_HAS_1BIT_SDMMC
43-
#endif // defined(SOC_SDMMC_USE_GPIO_MATRIX) && defined(BOARD_HAS_SDMMC)
43+
44+
#elif SOC_SDMMC_USE_IOMUX
45+
_pin_clk = SDMMC_SLOT1_IOMUX_PIN_NUM_CLK;
46+
_pin_cmd = SDMMC_SLOT1_IOMUX_PIN_NUM_CMD;
47+
_pin_d0 = SDMMC_SLOT1_IOMUX_PIN_NUM_D0;
48+
#ifndef BOARD_HAS_1BIT_SDMMC
49+
_pin_d1 = SDMMC_SLOT1_IOMUX_PIN_NUM_D1;
50+
_pin_d2 = SDMMC_SLOT1_IOMUX_PIN_NUM_D2;
51+
_pin_d3 = SDMMC_SLOT1_IOMUX_PIN_NUM_D3;
52+
#endif // BOARD_HAS_1BIT_SDMMC
53+
#endif
54+
}
55+
56+
bool SDMMCFS::sdmmcDetachBus(void * bus_pointer){
57+
SDMMCFS *bus = (SDMMCFS *) bus_pointer;
58+
if(bus->_card) {
59+
bus->end();
60+
}
61+
return true;
4462
}
4563

4664
bool SDMMCFS::setPins(int clk, int cmd, int d0)
@@ -68,12 +86,12 @@ bool SDMMCFS::setPins(int clk, int cmd, int d0, int d1, int d2, int d3)
6886
// Since SDMMCFS::begin hardcodes the usage of slot 1, only check if
6987
// the pins match slot 1 pins.
7088
bool pins_ok = (clk == (int)SDMMC_SLOT1_IOMUX_PIN_NUM_CLK) &&
71-
(cmd == (int)SDMMC_SLOT1_IOMUX_PIN_NUM_CMD) &&
72-
(d0 == (int)SDMMC_SLOT1_IOMUX_PIN_NUM_D0) &&
73-
(((d1 == -1) && (d2 == -1) && (d3 == -1)) ||
74-
((d1 == (int)SDMMC_SLOT1_IOMUX_PIN_NUM_D1) &&
75-
(d2 == (int)SDMMC_SLOT1_IOMUX_PIN_NUM_D2) &&
76-
(d3 == (int)SDMMC_SLOT1_IOMUX_PIN_NUM_D3)));
89+
(cmd == (int)SDMMC_SLOT1_IOMUX_PIN_NUM_CMD) &&
90+
(d0 == (int)SDMMC_SLOT1_IOMUX_PIN_NUM_D0) &&
91+
(((d1 == -1) && (d2 == -1) && (d3 == -1)) ||
92+
((d1 == (int)SDMMC_SLOT1_IOMUX_PIN_NUM_D1) &&
93+
(d2 == (int)SDMMC_SLOT1_IOMUX_PIN_NUM_D2) &&
94+
(d3 == (int)SDMMC_SLOT1_IOMUX_PIN_NUM_D3)));
7795
if (!pins_ok) {
7896
log_e("SDMMCFS: specified pins are not supported by this chip.");
7997
return false;
@@ -89,11 +107,13 @@ bool SDMMCFS::begin(const char * mountpoint, bool mode1bit, bool format_if_mount
89107
if(_card) {
90108
return true;
91109
}
110+
perimanSetBusDeinit(ESP32_BUS_TYPE_SDMMC, SDMMCFS::sdmmcDetachBus);
111+
92112
//mount
93113
sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT();
94114
#ifdef SOC_SDMMC_USE_GPIO_MATRIX
95115
// SoC supports SDMMC pin configuration via GPIO matrix.
96-
// Chech that the pins have been set either in the constructor or setPins function.
116+
// Check that the pins have been set either in the constructor or setPins function.
97117
if (_pin_cmd == -1 || _pin_clk == -1 || _pin_d0 == -1
98118
|| (!mode1bit && (_pin_d1 == -1 || _pin_d2 == -1 || _pin_d3 == -1))) {
99119
log_e("SDMMCFS: some SD pins are not set");
@@ -108,6 +128,16 @@ bool SDMMCFS::begin(const char * mountpoint, bool mode1bit, bool format_if_mount
108128
slot_config.d3 = (gpio_num_t) _pin_d3;
109129
slot_config.width = 4;
110130
#endif // SOC_SDMMC_USE_GPIO_MATRIX
131+
132+
if(!perimanSetPinBus(_pin_cmd, ESP32_BUS_TYPE_INIT, NULL)){ return false; }
133+
if(!perimanSetPinBus(_pin_clk, ESP32_BUS_TYPE_INIT, NULL)){ return false; }
134+
if(!perimanSetPinBus(_pin_d0, ESP32_BUS_TYPE_INIT, NULL)){ return false; }
135+
if(!mode1bit) {
136+
if(!perimanSetPinBus(_pin_d1, ESP32_BUS_TYPE_INIT, NULL)){ return false; }
137+
if(!perimanSetPinBus(_pin_d2, ESP32_BUS_TYPE_INIT, NULL)){ return false; }
138+
if(!perimanSetPinBus(_pin_d3, ESP32_BUS_TYPE_INIT, NULL)){ return false; }
139+
}
140+
111141
sdmmc_host_t host = SDMMC_HOST_DEFAULT();
112142
host.flags = SDMMC_HOST_FLAG_4BIT;
113143
host.slot = SDMMC_HOST_SLOT_1;
@@ -119,6 +149,7 @@ bool SDMMCFS::begin(const char * mountpoint, bool mode1bit, bool format_if_mount
119149
host.flags = SDMMC_HOST_FLAG_1BIT; //use 1-line SD mode
120150
slot_config.width = 1;
121151
}
152+
_mode1bit = mode1bit;
122153

123154
esp_vfs_fat_sdmmc_mount_config_t mount_config = {
124155
.format_if_mount_failed = format_if_mount_failed,
@@ -142,7 +173,21 @@ bool SDMMCFS::begin(const char * mountpoint, bool mode1bit, bool format_if_mount
142173
return false;
143174
}
144175
_impl->mountpoint(mountpoint);
176+
177+
if(!perimanSetPinBus(_pin_cmd, ESP32_BUS_TYPE_SDMMC, (void *)(this))){ goto err; }
178+
if(!perimanSetPinBus(_pin_clk, ESP32_BUS_TYPE_SDMMC, (void *)(this))){ goto err; }
179+
if(!perimanSetPinBus(_pin_d0, ESP32_BUS_TYPE_SDMMC, (void *)(this))){ goto err; }
180+
if(!mode1bit) {
181+
if(!perimanSetPinBus(_pin_d1, ESP32_BUS_TYPE_SDMMC, (void *)(this))){ goto err; }
182+
if(!perimanSetPinBus(_pin_d2, ESP32_BUS_TYPE_SDMMC, (void *)(this))){ goto err; }
183+
if(!perimanSetPinBus(_pin_d3, ESP32_BUS_TYPE_SDMMC, (void *)(this))){ goto err; }
184+
}
145185
return true;
186+
187+
err:
188+
log_e("Failed to set all pins bus to SDMMC");
189+
SDMMCFS::sdmmcDetachBus((void *)(this));
190+
return false;
146191
}
147192

148193
void SDMMCFS::end()
@@ -151,6 +196,14 @@ void SDMMCFS::end()
151196
esp_vfs_fat_sdcard_unmount(_impl->mountpoint(), _card);
152197
_impl->mountpoint(NULL);
153198
_card = NULL;
199+
perimanSetPinBus(_pin_cmd, ESP32_BUS_TYPE_INIT, NULL);
200+
perimanSetPinBus(_pin_clk, ESP32_BUS_TYPE_INIT, NULL);
201+
perimanSetPinBus(_pin_d0, ESP32_BUS_TYPE_INIT, NULL);
202+
if(!_mode1bit) {
203+
perimanSetPinBus(_pin_d1, ESP32_BUS_TYPE_INIT, NULL);
204+
perimanSetPinBus(_pin_d2, ESP32_BUS_TYPE_INIT, NULL);
205+
perimanSetPinBus(_pin_d3, ESP32_BUS_TYPE_INIT, NULL);
206+
}
154207
}
155208
}
156209

Diff for: libraries/SD_MMC/src/SD_MMC.h

+4-3
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,13 @@ class SDMMCFS : public FS
3636
{
3737
protected:
3838
sdmmc_card_t* _card;
39-
40-
#ifdef SOC_SDMMC_USE_GPIO_MATRIX
4139
int8_t _pin_clk = -1;
4240
int8_t _pin_cmd = -1;
4341
int8_t _pin_d0 = -1;
4442
int8_t _pin_d1 = -1;
4543
int8_t _pin_d2 = -1;
4644
int8_t _pin_d3 = -1;
47-
#endif
45+
bool _mode1bit = false;
4846

4947
public:
5048
SDMMCFS(FSImplPtr impl);
@@ -56,6 +54,9 @@ class SDMMCFS : public FS
5654
uint64_t cardSize();
5755
uint64_t totalBytes();
5856
uint64_t usedBytes();
57+
58+
private:
59+
static bool sdmmcDetachBus(void * bus_pointer);
5960
};
6061

6162
}

0 commit comments

Comments
 (0)