Skip to content

Commit 538d5bc

Browse files
authored
Sd card map pins, drive index & fix events (#3008)
1 parent 13d98b2 commit 538d5bc

File tree

10 files changed

+212
-91
lines changed

10 files changed

+212
-91
lines changed

src/HAL/Include/nanoHAL_System_IO_FileSystem.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ typedef enum StorageEventType
3939
// removable device removed
4040
StorageEventType_RemovableDeviceRemoval = 0x02,
4141

42+
// Card detect changed
43+
StorageEventType_CardDetectChanged = 0x03
44+
4245
} StorageEventType;
4346

4447
// constants to be used throughout the code

src/System.IO.FileSystem/nf_sys_io_filesystem.cpp

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,20 @@
99

1010
static const CLR_RT_MethodHandler method_lookup[] =
1111
{
12+
NULL,
13+
NULL,
14+
NULL,
15+
NULL,
16+
NULL,
17+
NULL,
18+
NULL,
19+
NULL,
20+
NULL,
21+
NULL,
22+
NULL,
23+
NULL,
24+
NULL,
25+
NULL,
1226
NULL,
1327
NULL,
1428
NULL,
@@ -75,6 +89,9 @@ static const CLR_RT_MethodHandler method_lookup[] =
7589
NULL,
7690
NULL,
7791
NULL,
92+
NULL,
93+
NULL,
94+
NULL,
7895
Library_nf_sys_io_filesystem_System_IO_Directory::NativeGetChildren___STATIC__SZARRAY_STRING__STRING__BOOLEAN,
7996
NULL,
8097
NULL,
@@ -222,9 +239,9 @@ static const CLR_RT_MethodHandler method_lookup[] =
222239
const CLR_RT_NativeAssemblyData g_CLR_AssemblyNative_System_IO_FileSystem =
223240
{
224241
"System.IO.FileSystem",
225-
0xB0025409,
242+
0x1777E2FE,
226243
method_lookup,
227-
{ 1, 1, 0, 3 }
244+
{ 1, 1, 0, 4 }
228245
};
229246

230247
// clang-format on

src/System.IO.FileSystem/nf_sys_io_filesystem.h

Lines changed: 41 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@
2121
FileAttributes::FileAttributes_System | FileAttributes::FileAttributes_Directory | \
2222
FileAttributes::FileAttributes_Archive)
2323

24+
typedef enum __nfpack CardDetectState
25+
{
26+
CardDetectState_Inserted = 0,
27+
CardDetectState_Removed = 1,
28+
} CardDetectState;
29+
2430
typedef enum __nfpack SDCard_SDDataWidth
2531
{
2632
SDCard_SDDataWidth__1_bit = 1,
@@ -45,6 +51,7 @@ typedef enum __nfpack StorageEventManager_StorageEventType
4551
StorageEventManager_StorageEventType_Invalid = 0,
4652
StorageEventManager_StorageEventType_RemovableDeviceInsertion = 1,
4753
StorageEventManager_StorageEventType_RemovableDeviceRemoval = 2,
54+
StorageEventManager_StorageEventType_CardDetectChanged = 3,
4855
} StorageEventManager_StorageEventType;
4956

5057
typedef enum __nfpack DriveType
@@ -91,16 +98,28 @@ typedef enum __nfpack FileShare
9198
FileShare_ReadWrite = 3,
9299
} FileShare;
93100

101+
struct Library_nf_sys_io_filesystem_nanoFramework_System_IO_CardDetectChangedEventArgs
102+
{
103+
static const int FIELD___cardState = 1;
104+
static const int FIELD___slotIndex = 2;
105+
106+
//--//
107+
};
108+
94109
struct Library_nf_sys_io_filesystem_nanoFramework_System_IO_FileSystem_SDCard
95110
{
96111
static const int FIELD___mounted = 1;
97112
static const int FIELD___disposed = 2;
98113
static const int FIELD___sdCardType = 3;
99114
static const int FIELD___enableCardDetectPin = 4;
100-
static const int FIELD___cardDetectPin = 5;
101-
static const int FIELD___dataWidth = 6;
102-
static const int FIELD___spiBus = 7;
103-
static const int FIELD___chipSelectPin = 8;
115+
static const int FIELD___cardDetectedState = 5;
116+
static const int FIELD___cardDetectPin = 6;
117+
static const int FIELD___slotIndex = 7;
118+
static const int FIELD___autoMount = 8;
119+
static const int FIELD___dataWidth = 9;
120+
static const int FIELD___spiBus = 10;
121+
static const int FIELD___chipSelectPin = 11;
122+
static const int FIELD__CardDetectChanged = 12;
104123

105124
NANOCLR_NATIVE_DECLARE(InitNative___VOID);
106125
NANOCLR_NATIVE_DECLARE(NativeDispose___VOID);
@@ -111,21 +130,19 @@ struct Library_nf_sys_io_filesystem_nanoFramework_System_IO_FileSystem_SDCard
111130
//--//
112131
};
113132

114-
struct Library_nf_sys_io_filesystem_nanoFramework_System_IO_FileSystem_SDCard__SDCardMmcParameters
133+
struct Library_nf_sys_io_filesystem_nanoFramework_System_IO_FileSystem_SDCardMmcParameters
115134
{
116-
static const int FIELD__dataWidth = 1;
117-
static const int FIELD__enableCardDetectPin = 2;
118-
static const int FIELD__cardDetectPin = 3;
135+
static const int FIELD__slotIndex = 1;
136+
static const int FIELD__dataWidth = 2;
119137

120138
//--//
121139
};
122140

123-
struct Library_nf_sys_io_filesystem_nanoFramework_System_IO_FileSystem_SDCard__SDCardSpiParameters
141+
struct Library_nf_sys_io_filesystem_nanoFramework_System_IO_FileSystem_SDCardSpiParameters
124142
{
125-
static const int FIELD__spiBus = 1;
126-
static const int FIELD__chipSelectPin = 2;
127-
static const int FIELD__enableCardDetectPin = 3;
128-
static const int FIELD__cardDetectPin = 4;
143+
static const int FIELD__slotIndex = 1;
144+
static const int FIELD__spiBus = 2;
145+
static const int FIELD__chipSelectPin = 3;
129146

130147
//--//
131148
};
@@ -161,6 +178,7 @@ struct Library_nf_sys_io_filesystem_nanoFramework_System_IO_StorageEventManager
161178
static const int FIELD_STATIC__RemovableDeviceInserted = 0;
162179
static const int FIELD_STATIC__RemovableDeviceRemoved = 1;
163180
static const int FIELD_STATIC___drives = 2;
181+
static const int FIELD_STATIC___sdCardList = 3;
164182

165183
//--//
166184
};
@@ -170,6 +188,7 @@ struct Library_nf_sys_io_filesystem_nanoFramework_System_IO_StorageEventManager_
170188
static const int FIELD__EventType = 3;
171189
static const int FIELD__VolumeIndex = 4;
172190
static const int FIELD__Time = 5;
191+
static const int FIELD__state = 6;
173192

174193
//--//
175194
};
@@ -194,10 +213,10 @@ struct Library_nf_sys_io_filesystem_System_IO_FileSystemManager__FileRecord
194213

195214
struct Library_nf_sys_io_filesystem_System_IO_FileSystemManager
196215
{
197-
static const int FIELD_STATIC___openFiles = 3;
198-
static const int FIELD_STATIC___lockedDirs = 4;
199-
static const int FIELD_STATIC___currentDirectoryRecord = 5;
200-
static const int FIELD_STATIC__CurrentDirectory = 6;
216+
static const int FIELD_STATIC___openFiles = 4;
217+
static const int FIELD_STATIC___lockedDirs = 5;
218+
static const int FIELD_STATIC___currentDirectoryRecord = 6;
219+
static const int FIELD_STATIC__CurrentDirectory = 7;
201220

202221
//--//
203222
};
@@ -218,7 +237,7 @@ struct Library_nf_sys_io_filesystem_System_IO_FileStream
218237

219238
struct Library_nf_sys_io_filesystem_System_IO_File
220239
{
221-
static const int FIELD_STATIC__EmptyBytes = 7;
240+
static const int FIELD_STATIC__EmptyBytes = 8;
222241

223242
//--//
224243
};
@@ -282,10 +301,10 @@ struct Library_nf_sys_io_filesystem_System_IO_NativeIO
282301

283302
struct Library_nf_sys_io_filesystem_System_IO_Path
284303
{
285-
static const int FIELD_STATIC__DirectorySeparatorChar = 8;
286-
static const int FIELD_STATIC__AltDirectorySeparatorChar = 9;
287-
static const int FIELD_STATIC__VolumeSeparatorChar = 10;
288-
static const int FIELD_STATIC__PathSeparator = 11;
304+
static const int FIELD_STATIC__DirectorySeparatorChar = 9;
305+
static const int FIELD_STATIC__AltDirectorySeparatorChar = 10;
306+
static const int FIELD_STATIC__VolumeSeparatorChar = 11;
307+
static const int FIELD_STATIC__PathSeparator = 12;
289308

290309
//--//
291310
};

targets/ESP32/_common/DeviceMapping_common.cpp

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,23 @@ int Esp32_GetMappedDevicePins(Esp32_MapDeviceType deviceType, int busIndex, int
5757
return (int)Esp32_DAC_DevicePinMap[pinIndex];
5858
#endif
5959

60-
case DEV_TYPE_I2S:
61-
return (int)Esp32_I2S_DevicePinMap[busIndex][pinIndex];
62-
60+
#if (defined(CONFIG_IDF_TARGET_ESP32) || defined(CONFIG_IDF_TARGET_ESP32S3))
61+
case DEV_TYPE_SDMMC:
62+
return (int)Esp32_SDMMC_DevicePinMap[busIndex][pinIndex];
63+
#endif
6364
default:
6465
break;
6566
};
6667
}
6768
return -1;
6869
}
6970

71+
// Function to map SdMmc from C to C++ code
72+
extern "C" int Esp32_GetSDmmcDevicePins_C(int busIndex, int pinIndex)
73+
{
74+
return Esp32_GetMappedDevicePins(DEV_TYPE_SDMMC, busIndex, pinIndex);
75+
}
76+
7077
void Esp32_SetMappedDevicePins(Esp32_MapDeviceType deviceType, int busIndex, int8_t pinIndex, int ioPinNumber)
7178
{
7279
if (busIndex >= 0)
@@ -106,6 +113,11 @@ void Esp32_SetMappedDevicePins(Esp32_MapDeviceType deviceType, int busIndex, int
106113
Esp32_I2S_DevicePinMap[busIndex][pinIndex] = ioPinNumber;
107114
break;
108115

116+
#if (defined(CONFIG_IDF_TARGET_ESP32S3) || defined(CONFIG_IDF_TARGET_ESP32P4))
117+
case DEV_TYPE_SDMMC:
118+
Esp32_SDMMC_DevicePinMap[busIndex][pinIndex] = ioPinNumber;
119+
#endif
120+
109121
default:
110122
break;
111123
};
@@ -195,6 +207,16 @@ int Esp32_SetMappedDevicePins(uint8_t pin, int32_t alternateFunction)
195207
}
196208
break;
197209

210+
#if (defined(CONFIG_IDF_TARGET_ESP32S3) || defined(CONFIG_IDF_TARGET_ESP32P4))
211+
case DEV_TYPE_SDMMC:
212+
if (busIndex <= 1)
213+
{
214+
Esp32_SDMMC_DevicePinMap[busIndex][gpioMapping] = pin;
215+
return true;
216+
}
217+
break;
218+
#endif
219+
198220
default:
199221
// invalid device type
200222
return false;

targets/ESP32/_common/ESP32_DeviceMapping.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,11 @@ int8_t Esp32_I2S_DevicePinMap[I2S_NUM_MAX][5] = {
9494
{I2S_PIN_NO_CHANGE, I2S_PIN_NO_CHANGE, I2S_PIN_NO_CHANGE, I2S_PIN_NO_CHANGE, I2S_PIN_NO_CHANGE},
9595
// No pin pre configured
9696
{I2S_PIN_NO_CHANGE, I2S_PIN_NO_CHANGE, I2S_PIN_NO_CHANGE, I2S_PIN_NO_CHANGE, I2S_PIN_NO_CHANGE}};
97+
98+
// SDMMC
99+
// ESP32 allows 2 sdmmc devices, allow for 4 data pins
100+
// Set SDMMC1 to default pins and SDMMC2 to not configured
101+
int8_t Esp32_SDMMC_DevicePinMap[CONFIG_SOC_SDMMC_NUM_SLOTS][6] = {
102+
// Clock, Command, D0, D1, D2, D3
103+
{GPIO_NUM_14, GPIO_NUM_15, GPIO_NUM_2, GPIO_NUM_4, GPIO_NUM_12, GPIO_NUM_13},
104+
{GPIO_NUM_NC, GPIO_NUM_NC, GPIO_NUM_NC, GPIO_NUM_NC, GPIO_NUM_NC, GPIO_NUM_NC}};

targets/ESP32/_common/ESP32_S3_DeviceMapping.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,11 @@ int8_t Esp32_ADC_DevicePinMap[TARGET_ADC_NUM_PINS] = {
6969
int8_t Esp32_I2S_DevicePinMap[I2S_NUM_MAX][5] = {
7070
{I2S_PIN_NO_CHANGE, I2S_PIN_NO_CHANGE, I2S_PIN_NO_CHANGE, I2S_PIN_NO_CHANGE, I2S_PIN_NO_CHANGE},
7171
{I2S_PIN_NO_CHANGE, I2S_PIN_NO_CHANGE, I2S_PIN_NO_CHANGE, I2S_PIN_NO_CHANGE, I2S_PIN_NO_CHANGE}};
72+
73+
// SDMMC
74+
// ESP32_S3 allows 2 sdmmc devices, allow for 4 data pins
75+
// Set SDMMC1 to default pins and SDMMC2 to not configured
76+
int8_t Esp32_SDMMC_DevicePinMap[CONFIG_SOC_SDMMC_NUM_SLOTS][6] = {
77+
// Clock, Command, D0, D1, D2, D3, D4, D5, D6, D7, D8
78+
{GPIO_NUM_14, GPIO_NUM_15, GPIO_NUM_2, GPIO_NUM_4, GPIO_NUM_12, GPIO_NUM_13},
79+
{GPIO_NUM_NC, GPIO_NUM_NC, GPIO_NUM_NC, GPIO_NUM_NC, GPIO_NUM_NC, GPIO_NUM_NC}};

targets/ESP32/_common/Target_System_IO_FileSystem.c

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@
3636

3737
#include <target_platform.h>
3838

39-
// #include <Target_Windows_Storage.h>
4039
#include <nanoHAL_Windows_Storage.h>
40+
#include <Esp32_DeviceMapping.h>
4141

42-
#if !defined(CONFIG_IDF_TARGET_ESP32C3) && (HAL_USE_SDC == TRUE)
42+
#if (HAL_USE_SDC == TRUE)
4343

4444
static const char *TAG = "SDCard";
4545

@@ -88,33 +88,44 @@ bool LogMountResult(esp_err_t errCode)
8888
// Mount SDcard on MMC/SDIO bus
8989
//
9090
// bit1Mode- true to use 1 bit MMC interface
91-
// driveIndex = 0 = first drive
91+
// driveIndex = 0 = first drive, 1 = 2nd drive
9292
//
9393
bool Storage_MountMMC(bool bit1Mode, int driveIndex)
9494
{
9595
esp_err_t errCode;
9696
char mountPoint[] = INDEX0_DRIVE_LETTER;
9797

98-
// Change fatfs drive letter to mount point D: -> /D for ESP32 VFS
98+
// Change fatfs drive letter to mount point D: -> /D for ESP32 VFS or /E
9999
mountPoint[1] = mountPoint[0] + driveIndex;
100100
mountPoint[0] = '/';
101101

102-
ESP_LOGI(TAG, "Initializing MMC SD card");
102+
ESP_LOGI(TAG, "Initializing SDMMC%d SD card", driveIndex + 1);
103103

104104
sdmmc_host_t host = SDMMC_HOST_DEFAULT();
105105

106106
// This initializes the slot without card detect (CD) and write protect (WP) signals.
107107
// Modify slot_config.gpio_cd and slot_config.gpio_wp if your board has these signals.
108108
sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT();
109109

110-
// Set bus width to use
110+
// Set bus width and pins to use
111+
#if (defined(CONFIG_IDF_TARGET_ESP32S3) || defined(CONFIG_IDF_TARGET_ESP32P4))
112+
slot_config.clk = (gpio_num_t)Esp32_GetSDmmcDevicePins_C(driveIndex, Esp32SdmmcPin_Clock);
113+
slot_config.cmd = (gpio_num_t)Esp32_GetSDmmcDevicePins_C(driveIndex, Esp32SdmmcPin_Command);
114+
slot_config.d0 = (gpio_num_t)Esp32_GetSDmmcDevicePins_C(driveIndex, Esp32SdmmcPin_D0);
115+
#endif
116+
111117
if (bit1Mode)
112118
{
113119
slot_config.width = 1;
114120
}
115121
else
116122
{
117123
slot_config.width = 4;
124+
#if (defined(CONFIG_IDF_TARGET_ESP32S3) || defined(CONFIG_IDF_TARGET_ESP32P4))
125+
slot_config.d1 = (gpio_num_t)Esp32_GetSDmmcDevicePins_C(driveIndex, Esp32SdmmcPin_D1);
126+
slot_config.d2 = (gpio_num_t)Esp32_GetSDmmcDevicePins_C(driveIndex, Esp32SdmmcPin_D2);
127+
slot_config.d3 = (gpio_num_t)Esp32_GetSDmmcDevicePins_C(driveIndex, Esp32SdmmcPin_D3);
128+
#endif
118129
}
119130

120131
// from IDF readme on SDMMC
@@ -125,9 +136,6 @@ bool Storage_MountMMC(bool bit1Mode, int driveIndex)
125136
// On ESP32, SDMMC peripheral is connected to specific GPIO pins using the IO MUX.
126137
// GPIO pins cannot be customized. Please see the table below for the pin connections.
127138

128-
// When using an ESP-WROVER-KIT board, this example runs without any extra modifications required.
129-
// Only an SD card needs to be inserted into the slot.
130-
131139
// ESP32 pin | SD card pin | Notes
132140
// --------------|-------------|------------
133141
// GPIO14 (MTMS) | CLK | 10k pullup in SD mode
@@ -144,16 +152,8 @@ bool Storage_MountMMC(bool bit1Mode, int driveIndex)
144152
// This allows arbitrary GPIOs to be used to connect an SD card. In this example, GPIOs can be configured in two
145153
// ways:
146154

147-
// 1. Using menuconfig: Run `idf.py menuconfig` in the project directory and open "SD/MMC Example Configuration"
148-
// menu.
149-
// 2. In the source code: See the initialization of ``sdmmc_slot_config_t slot_config`` structure in the example
150-
// code.
151-
152155
// The table below lists the default pin assignments.
153156

154-
// When using an ESP32-S3-USB-OTG board, this example runs without any extra modifications required.
155-
// Only an SD card needs to be inserted into the slot.
156-
157157
// ESP32-S3 pin | SD card pin | Notes
158158
// --------------|-------------|------------
159159
// GPIO36 | CLK | 10k pullup

0 commit comments

Comments
 (0)