24
24
#include " sdmmc_cmd.h"
25
25
#include " soc/sdmmc_pins.h"
26
26
#include " ff.h"
27
+ #include " esp32-hal-periman.h"
27
28
28
29
using namespace fs ;
29
30
30
-
31
31
SDMMCFS::SDMMCFS (FSImplPtr impl)
32
32
: FS(impl), _card(nullptr )
33
33
{
@@ -40,7 +40,25 @@ SDMMCFS::SDMMCFS(FSImplPtr impl)
40
40
_pin_d2 = SDMMC_D2;
41
41
_pin_d3 = SDMMC_D3;
42
42
#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 ;
44
62
}
45
63
46
64
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)
68
86
// Since SDMMCFS::begin hardcodes the usage of slot 1, only check if
69
87
// the pins match slot 1 pins.
70
88
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)));
77
95
if (!pins_ok) {
78
96
log_e (" SDMMCFS: specified pins are not supported by this chip." );
79
97
return false ;
@@ -89,11 +107,13 @@ bool SDMMCFS::begin(const char * mountpoint, bool mode1bit, bool format_if_mount
89
107
if (_card) {
90
108
return true ;
91
109
}
110
+ perimanSetBusDeinit (ESP32_BUS_TYPE_SDMMC, SDMMCFS::sdmmcDetachBus);
111
+
92
112
// mount
93
113
sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT ();
94
114
#ifdef SOC_SDMMC_USE_GPIO_MATRIX
95
115
// 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.
97
117
if (_pin_cmd == -1 || _pin_clk == -1 || _pin_d0 == -1
98
118
|| (!mode1bit && (_pin_d1 == -1 || _pin_d2 == -1 || _pin_d3 == -1 ))) {
99
119
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
108
128
slot_config.d3 = (gpio_num_t ) _pin_d3;
109
129
slot_config.width = 4 ;
110
130
#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
+
111
141
sdmmc_host_t host = SDMMC_HOST_DEFAULT ();
112
142
host.flags = SDMMC_HOST_FLAG_4BIT;
113
143
host.slot = SDMMC_HOST_SLOT_1;
@@ -119,6 +149,7 @@ bool SDMMCFS::begin(const char * mountpoint, bool mode1bit, bool format_if_mount
119
149
host.flags = SDMMC_HOST_FLAG_1BIT; // use 1-line SD mode
120
150
slot_config.width = 1 ;
121
151
}
152
+ _mode1bit = mode1bit;
122
153
123
154
esp_vfs_fat_sdmmc_mount_config_t mount_config = {
124
155
.format_if_mount_failed = format_if_mount_failed,
@@ -142,7 +173,21 @@ bool SDMMCFS::begin(const char * mountpoint, bool mode1bit, bool format_if_mount
142
173
return false ;
143
174
}
144
175
_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
+ }
145
185
return true ;
186
+
187
+ err:
188
+ log_e (" Failed to set all pins bus to SDMMC" );
189
+ SDMMCFS::sdmmcDetachBus ((void *)(this ));
190
+ return false ;
146
191
}
147
192
148
193
void SDMMCFS::end ()
@@ -151,6 +196,14 @@ void SDMMCFS::end()
151
196
esp_vfs_fat_sdcard_unmount (_impl->mountpoint (), _card);
152
197
_impl->mountpoint (NULL );
153
198
_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
+ }
154
207
}
155
208
}
156
209
0 commit comments