Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sd card map pins, drive index & fix events #3008

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/HAL/Include/nanoHAL_System_IO_FileSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ typedef enum StorageEventType
// removable device removed
StorageEventType_RemovableDeviceRemoval = 0x02,

// Card detect changed
StorageEventType_CardDetectChanged = 0x03

} StorageEventType;

// constants to be used throughout the code
Expand Down
21 changes: 19 additions & 2 deletions src/System.IO.FileSystem/nf_sys_io_filesystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,20 @@

static const CLR_RT_MethodHandler method_lookup[] =
{
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
Expand Down Expand Up @@ -75,6 +89,9 @@ static const CLR_RT_MethodHandler method_lookup[] =
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
Library_nf_sys_io_filesystem_System_IO_Directory::NativeGetChildren___STATIC__SZARRAY_STRING__STRING__BOOLEAN,
NULL,
NULL,
Expand Down Expand Up @@ -222,9 +239,9 @@ static const CLR_RT_MethodHandler method_lookup[] =
const CLR_RT_NativeAssemblyData g_CLR_AssemblyNative_System_IO_FileSystem =
{
"System.IO.FileSystem",
0xB0025409,
0x1777E2FE,
method_lookup,
{ 1, 1, 0, 3 }
{ 1, 1, 0, 4 }
};

// clang-format on
63 changes: 41 additions & 22 deletions src/System.IO.FileSystem/nf_sys_io_filesystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@
FileAttributes::FileAttributes_System | FileAttributes::FileAttributes_Directory | \
FileAttributes::FileAttributes_Archive)

typedef enum __nfpack CardDetectState
{
CardDetectState_Inserted = 0,
CardDetectState_Removed = 1,
} CardDetectState;

typedef enum __nfpack SDCard_SDDataWidth
{
SDCard_SDDataWidth__1_bit = 1,
Expand All @@ -45,6 +51,7 @@ typedef enum __nfpack StorageEventManager_StorageEventType
StorageEventManager_StorageEventType_Invalid = 0,
StorageEventManager_StorageEventType_RemovableDeviceInsertion = 1,
StorageEventManager_StorageEventType_RemovableDeviceRemoval = 2,
StorageEventManager_StorageEventType_CardDetectChanged = 3,
} StorageEventManager_StorageEventType;

typedef enum __nfpack DriveType
Expand Down Expand Up @@ -91,16 +98,28 @@ typedef enum __nfpack FileShare
FileShare_ReadWrite = 3,
} FileShare;

struct Library_nf_sys_io_filesystem_nanoFramework_System_IO_CardDetectChangedEventArgs
{
static const int FIELD___cardState = 1;
static const int FIELD___slotIndex = 2;

//--//
};

struct Library_nf_sys_io_filesystem_nanoFramework_System_IO_FileSystem_SDCard
{
static const int FIELD___mounted = 1;
static const int FIELD___disposed = 2;
static const int FIELD___sdCardType = 3;
static const int FIELD___enableCardDetectPin = 4;
static const int FIELD___cardDetectPin = 5;
static const int FIELD___dataWidth = 6;
static const int FIELD___spiBus = 7;
static const int FIELD___chipSelectPin = 8;
static const int FIELD___cardDetectedState = 5;
static const int FIELD___cardDetectPin = 6;
static const int FIELD___slotIndex = 7;
static const int FIELD___autoMount = 8;
static const int FIELD___dataWidth = 9;
static const int FIELD___spiBus = 10;
static const int FIELD___chipSelectPin = 11;
static const int FIELD__CardDetectChanged = 12;

NANOCLR_NATIVE_DECLARE(InitNative___VOID);
NANOCLR_NATIVE_DECLARE(NativeDispose___VOID);
Expand All @@ -111,21 +130,19 @@ struct Library_nf_sys_io_filesystem_nanoFramework_System_IO_FileSystem_SDCard
//--//
};

struct Library_nf_sys_io_filesystem_nanoFramework_System_IO_FileSystem_SDCard__SDCardMmcParameters
struct Library_nf_sys_io_filesystem_nanoFramework_System_IO_FileSystem_SDCardMmcParameters
{
static const int FIELD__dataWidth = 1;
static const int FIELD__enableCardDetectPin = 2;
static const int FIELD__cardDetectPin = 3;
static const int FIELD__slotIndex = 1;
static const int FIELD__dataWidth = 2;

//--//
};

struct Library_nf_sys_io_filesystem_nanoFramework_System_IO_FileSystem_SDCard__SDCardSpiParameters
struct Library_nf_sys_io_filesystem_nanoFramework_System_IO_FileSystem_SDCardSpiParameters
{
static const int FIELD__spiBus = 1;
static const int FIELD__chipSelectPin = 2;
static const int FIELD__enableCardDetectPin = 3;
static const int FIELD__cardDetectPin = 4;
static const int FIELD__slotIndex = 1;
static const int FIELD__spiBus = 2;
static const int FIELD__chipSelectPin = 3;

//--//
};
Expand Down Expand Up @@ -161,6 +178,7 @@ struct Library_nf_sys_io_filesystem_nanoFramework_System_IO_StorageEventManager
static const int FIELD_STATIC__RemovableDeviceInserted = 0;
static const int FIELD_STATIC__RemovableDeviceRemoved = 1;
static const int FIELD_STATIC___drives = 2;
static const int FIELD_STATIC___sdCardList = 3;

//--//
};
Expand All @@ -170,6 +188,7 @@ struct Library_nf_sys_io_filesystem_nanoFramework_System_IO_StorageEventManager_
static const int FIELD__EventType = 3;
static const int FIELD__VolumeIndex = 4;
static const int FIELD__Time = 5;
static const int FIELD__state = 6;

//--//
};
Expand All @@ -194,10 +213,10 @@ struct Library_nf_sys_io_filesystem_System_IO_FileSystemManager__FileRecord

struct Library_nf_sys_io_filesystem_System_IO_FileSystemManager
{
static const int FIELD_STATIC___openFiles = 3;
static const int FIELD_STATIC___lockedDirs = 4;
static const int FIELD_STATIC___currentDirectoryRecord = 5;
static const int FIELD_STATIC__CurrentDirectory = 6;
static const int FIELD_STATIC___openFiles = 4;
static const int FIELD_STATIC___lockedDirs = 5;
static const int FIELD_STATIC___currentDirectoryRecord = 6;
static const int FIELD_STATIC__CurrentDirectory = 7;

//--//
};
Expand All @@ -218,7 +237,7 @@ struct Library_nf_sys_io_filesystem_System_IO_FileStream

struct Library_nf_sys_io_filesystem_System_IO_File
{
static const int FIELD_STATIC__EmptyBytes = 7;
static const int FIELD_STATIC__EmptyBytes = 8;

//--//
};
Expand Down Expand Up @@ -282,10 +301,10 @@ struct Library_nf_sys_io_filesystem_System_IO_NativeIO

struct Library_nf_sys_io_filesystem_System_IO_Path
{
static const int FIELD_STATIC__DirectorySeparatorChar = 8;
static const int FIELD_STATIC__AltDirectorySeparatorChar = 9;
static const int FIELD_STATIC__VolumeSeparatorChar = 10;
static const int FIELD_STATIC__PathSeparator = 11;
static const int FIELD_STATIC__DirectorySeparatorChar = 9;
static const int FIELD_STATIC__AltDirectorySeparatorChar = 10;
static const int FIELD_STATIC__VolumeSeparatorChar = 11;
static const int FIELD_STATIC__PathSeparator = 12;

//--//
};
Expand Down
29 changes: 26 additions & 3 deletions targets/ESP32/_common/DeviceMapping_common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <esp32_idf.h>
#include <Esp32_DeviceMapping.h>


void Esp32_DecodeAlternateFunction(
uint32_t alternateFunction,
Esp32_MapDeviceType &deviceType,
Expand Down Expand Up @@ -57,16 +58,23 @@ int Esp32_GetMappedDevicePins(Esp32_MapDeviceType deviceType, int busIndex, int
return (int)Esp32_DAC_DevicePinMap[pinIndex];
#endif

case DEV_TYPE_I2S:
return (int)Esp32_I2S_DevicePinMap[busIndex][pinIndex];

#if (defined(CONFIG_IDF_TARGET_ESP32)|| defined(CONFIG_IDF_TARGET_ESP32S3))
case DEV_TYPE_SDMMC:
return (int)Esp32_SDMMC_DevicePinMap[busIndex][pinIndex];
#endif
default:
break;
};
}
return -1;
}

// Function to map SdMmc from C to C++ code
extern "C" int Esp32_GetSDmmcDevicePins_C(int busIndex, int pinIndex)
{
return Esp32_GetMappedDevicePins(DEV_TYPE_SDMMC, busIndex, pinIndex);
}

void Esp32_SetMappedDevicePins(Esp32_MapDeviceType deviceType, int busIndex, int8_t pinIndex, int ioPinNumber)
{
if (busIndex >= 0)
Expand Down Expand Up @@ -106,6 +114,11 @@ void Esp32_SetMappedDevicePins(Esp32_MapDeviceType deviceType, int busIndex, int
Esp32_I2S_DevicePinMap[busIndex][pinIndex] = ioPinNumber;
break;

#if (defined(CONFIG_IDF_TARGET_ESP32S3) || defined(CONFIG_IDF_TARGET_ESP32P4))
case DEV_TYPE_SDMMC:
Esp32_SDMMC_DevicePinMap[busIndex][pinIndex] = ioPinNumber;
#endif

default:
break;
};
Expand Down Expand Up @@ -195,6 +208,16 @@ int Esp32_SetMappedDevicePins(uint8_t pin, int32_t alternateFunction)
}
break;

#if (defined(CONFIG_IDF_TARGET_ESP32S3) || defined(CONFIG_IDF_TARGET_ESP32P4))
case DEV_TYPE_SDMMC:
if (busIndex <= 1)
{
Esp32_SDMMC_DevicePinMap[busIndex][gpioMapping] = pin;
return true;
}
break;
#endif

default:
// invalid device type
return false;
Expand Down
8 changes: 8 additions & 0 deletions targets/ESP32/_common/ESP32_DeviceMapping.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,11 @@ int8_t Esp32_I2S_DevicePinMap[I2S_NUM_MAX][5] = {
{I2S_PIN_NO_CHANGE, I2S_PIN_NO_CHANGE, I2S_PIN_NO_CHANGE, I2S_PIN_NO_CHANGE, I2S_PIN_NO_CHANGE},
// No pin pre configured
{I2S_PIN_NO_CHANGE, I2S_PIN_NO_CHANGE, I2S_PIN_NO_CHANGE, I2S_PIN_NO_CHANGE, I2S_PIN_NO_CHANGE}};

// SDMMC
// ESP32 allows 2 sdmmc devices, allow for 4 data pins
// Set SDMMC1 to default pins and SDMMC2 to not configured
int8_t Esp32_SDMMC_DevicePinMap[CONFIG_SOC_SDMMC_NUM_SLOTS][6] = {
// Clock, Command, D0, D1, D2, D3
{GPIO_NUM_14, GPIO_NUM_15, GPIO_NUM_2, GPIO_NUM_4, GPIO_NUM_12, GPIO_NUM_13},
{GPIO_NUM_NC, GPIO_NUM_NC, GPIO_NUM_NC, GPIO_NUM_NC, GPIO_NUM_NC, GPIO_NUM_NC}};
8 changes: 8 additions & 0 deletions targets/ESP32/_common/ESP32_S3_DeviceMapping.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,11 @@ int8_t Esp32_ADC_DevicePinMap[TARGET_ADC_NUM_PINS] = {
int8_t Esp32_I2S_DevicePinMap[I2S_NUM_MAX][5] = {
{I2S_PIN_NO_CHANGE, I2S_PIN_NO_CHANGE, I2S_PIN_NO_CHANGE, I2S_PIN_NO_CHANGE, I2S_PIN_NO_CHANGE},
{I2S_PIN_NO_CHANGE, I2S_PIN_NO_CHANGE, I2S_PIN_NO_CHANGE, I2S_PIN_NO_CHANGE, I2S_PIN_NO_CHANGE}};

// SDMMC
// ESP32_S3 allows 2 sdmmc devices, allow for 4 data pins
// Set SDMMC1 to default pins and SDMMC2 to not configured
int8_t Esp32_SDMMC_DevicePinMap[CONFIG_SOC_SDMMC_NUM_SLOTS][6] = {
// Clock, Command, D0, D1, D2, D3, D4, D5, D6, D7, D8
{GPIO_NUM_14, GPIO_NUM_15, GPIO_NUM_2, GPIO_NUM_4, GPIO_NUM_12, GPIO_NUM_13},
{GPIO_NUM_NC, GPIO_NUM_NC, GPIO_NUM_NC, GPIO_NUM_NC, GPIO_NUM_NC, GPIO_NUM_NC}};
34 changes: 17 additions & 17 deletions targets/ESP32/_common/Target_System_IO_FileSystem.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@

#include <target_platform.h>

// #include <Target_Windows_Storage.h>
#include <nanoHAL_Windows_Storage.h>
#include <Esp32_DeviceMapping.h>

#if !defined(CONFIG_IDF_TARGET_ESP32C3) && (HAL_USE_SDC == TRUE)
#if (HAL_USE_SDC == TRUE)

static const char *TAG = "SDCard";

Expand Down Expand Up @@ -88,33 +88,44 @@ bool LogMountResult(esp_err_t errCode)
// Mount SDcard on MMC/SDIO bus
//
// bit1Mode- true to use 1 bit MMC interface
// driveIndex = 0 = first drive
// driveIndex = 0 = first drive, 1 = 2nd drive
//
bool Storage_MountMMC(bool bit1Mode, int driveIndex)
{
esp_err_t errCode;
char mountPoint[] = INDEX0_DRIVE_LETTER;

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

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

sdmmc_host_t host = SDMMC_HOST_DEFAULT();

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

// Set bus width to use
// Set bus width and pins to use
#if (defined(CONFIG_IDF_TARGET_ESP32S3) || defined(CONFIG_IDF_TARGET_ESP32P4))
slot_config.clk = (gpio_num_t)Esp32_GetSDmmcDevicePins_C(driveIndex, Esp32SdmmcPin_Clock);
slot_config.cmd = (gpio_num_t)Esp32_GetSDmmcDevicePins_C(driveIndex, Esp32SdmmcPin_Command);
slot_config.d0 = (gpio_num_t)Esp32_GetSDmmcDevicePins_C(driveIndex, Esp32SdmmcPin_D0);
#endif

if (bit1Mode)
{
slot_config.width = 1;
}
else
{
slot_config.width = 4;
#if (defined(CONFIG_IDF_TARGET_ESP32S3) || defined(CONFIG_IDF_TARGET_ESP32P4))
slot_config.d1 = (gpio_num_t)Esp32_GetSDmmcDevicePins_C(driveIndex, Esp32SdmmcPin_D1);
slot_config.d2 = (gpio_num_t)Esp32_GetSDmmcDevicePins_C(driveIndex, Esp32SdmmcPin_D2);
slot_config.d3 = (gpio_num_t)Esp32_GetSDmmcDevicePins_C(driveIndex, Esp32SdmmcPin_D3);
#endif
}

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

// When using an ESP-WROVER-KIT board, this example runs without any extra modifications required.
// Only an SD card needs to be inserted into the slot.

// ESP32 pin | SD card pin | Notes
// --------------|-------------|------------
// GPIO14 (MTMS) | CLK | 10k pullup in SD mode
Expand All @@ -144,16 +152,8 @@ bool Storage_MountMMC(bool bit1Mode, int driveIndex)
// This allows arbitrary GPIOs to be used to connect an SD card. In this example, GPIOs can be configured in two
// ways:

// 1. Using menuconfig: Run `idf.py menuconfig` in the project directory and open "SD/MMC Example Configuration"
// menu.
// 2. In the source code: See the initialization of ``sdmmc_slot_config_t slot_config`` structure in the example
// code.

// The table below lists the default pin assignments.

// When using an ESP32-S3-USB-OTG board, this example runs without any extra modifications required.
// Only an SD card needs to be inserted into the slot.

// ESP32-S3 pin | SD card pin | Notes
// --------------|-------------|------------
// GPIO36 | CLK | 10k pullup
Expand Down
Loading
Loading