From a10cc9b86af56c8f9383ef0b2e1d8ee2e71a1759 Mon Sep 17 00:00:00 2001 From: Adrian Soundy Date: Sat, 3 Aug 2024 15:31:42 +1200 Subject: [PATCH 1/5] Sd card map pins, drive index & fix events --- .../Include/nanoHAL_System_IO_FileSystem.h | 3 + .../nf_sys_io_filesystem.cpp | 21 +++- .../nf_sys_io_filesystem.h | 63 +++++++---- .../ESP32/_common/DeviceMapping_common.cpp | 29 ++++- targets/ESP32/_common/ESP32_DeviceMapping.cpp | 8 ++ .../ESP32/_common/ESP32_S3_DeviceMapping.cpp | 8 ++ .../_common/Target_System_IO_FileSystem.c | 34 +++--- targets/ESP32/_include/Esp32_DeviceMapping.h | 23 +++- .../_nanoCLR/System.Device.Gpio/cpu_gpio.cpp | 10 +- ...oFramework_System_IO_FileSystem_SDCard.cpp | 100 ++++++++++-------- 10 files changed, 209 insertions(+), 90 deletions(-) diff --git a/src/HAL/Include/nanoHAL_System_IO_FileSystem.h b/src/HAL/Include/nanoHAL_System_IO_FileSystem.h index d33584f27a..7d060a2d19 100644 --- a/src/HAL/Include/nanoHAL_System_IO_FileSystem.h +++ b/src/HAL/Include/nanoHAL_System_IO_FileSystem.h @@ -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 diff --git a/src/System.IO.FileSystem/nf_sys_io_filesystem.cpp b/src/System.IO.FileSystem/nf_sys_io_filesystem.cpp index 85d9164708..20a482a12c 100644 --- a/src/System.IO.FileSystem/nf_sys_io_filesystem.cpp +++ b/src/System.IO.FileSystem/nf_sys_io_filesystem.cpp @@ -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, @@ -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, @@ -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 diff --git a/src/System.IO.FileSystem/nf_sys_io_filesystem.h b/src/System.IO.FileSystem/nf_sys_io_filesystem.h index 2d5bd90829..bc12f92c32 100644 --- a/src/System.IO.FileSystem/nf_sys_io_filesystem.h +++ b/src/System.IO.FileSystem/nf_sys_io_filesystem.h @@ -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, @@ -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 @@ -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); @@ -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; //--// }; @@ -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; //--// }; @@ -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; //--// }; @@ -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; //--// }; @@ -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; //--// }; @@ -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; //--// }; diff --git a/targets/ESP32/_common/DeviceMapping_common.cpp b/targets/ESP32/_common/DeviceMapping_common.cpp index 85e4625b77..5d3a1ca8ab 100644 --- a/targets/ESP32/_common/DeviceMapping_common.cpp +++ b/targets/ESP32/_common/DeviceMapping_common.cpp @@ -6,6 +6,7 @@ #include #include + void Esp32_DecodeAlternateFunction( uint32_t alternateFunction, Esp32_MapDeviceType &deviceType, @@ -57,9 +58,10 @@ 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; }; @@ -67,6 +69,12 @@ int Esp32_GetMappedDevicePins(Esp32_MapDeviceType deviceType, int busIndex, int 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) @@ -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; }; @@ -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; diff --git a/targets/ESP32/_common/ESP32_DeviceMapping.cpp b/targets/ESP32/_common/ESP32_DeviceMapping.cpp index 40609d9eed..2200375524 100644 --- a/targets/ESP32/_common/ESP32_DeviceMapping.cpp +++ b/targets/ESP32/_common/ESP32_DeviceMapping.cpp @@ -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}}; diff --git a/targets/ESP32/_common/ESP32_S3_DeviceMapping.cpp b/targets/ESP32/_common/ESP32_S3_DeviceMapping.cpp index fb3c467730..d1322122f9 100644 --- a/targets/ESP32/_common/ESP32_S3_DeviceMapping.cpp +++ b/targets/ESP32/_common/ESP32_S3_DeviceMapping.cpp @@ -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}}; diff --git a/targets/ESP32/_common/Target_System_IO_FileSystem.c b/targets/ESP32/_common/Target_System_IO_FileSystem.c index 1d82762774..c0a00af20a 100644 --- a/targets/ESP32/_common/Target_System_IO_FileSystem.c +++ b/targets/ESP32/_common/Target_System_IO_FileSystem.c @@ -36,10 +36,10 @@ #include -// #include #include +#include -#if !defined(CONFIG_IDF_TARGET_ESP32C3) && (HAL_USE_SDC == TRUE) +#if (HAL_USE_SDC == TRUE) static const char *TAG = "SDCard"; @@ -88,18 +88,18 @@ 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(); @@ -107,7 +107,13 @@ bool Storage_MountMMC(bool bit1Mode, int driveIndex) // 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; @@ -115,6 +121,11 @@ bool Storage_MountMMC(bool bit1Mode, int driveIndex) 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 @@ -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 @@ -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 diff --git a/targets/ESP32/_include/Esp32_DeviceMapping.h b/targets/ESP32/_include/Esp32_DeviceMapping.h index 83956393b7..ba1b9426f9 100644 --- a/targets/ESP32/_include/Esp32_DeviceMapping.h +++ b/targets/ESP32/_include/Esp32_DeviceMapping.h @@ -41,6 +41,17 @@ enum Esp32SpiPin Esp32SpiPin_Max, }; +enum Esp32SdmmcPin +{ + Esp32SdmmcPin_Clock, + Esp32SdmmcPin_Command, + Esp32SdmmcPin_D0, + Esp32SdmmcPin_D1, + Esp32SdmmcPin_D2, + Esp32SdmmcPin_D3, + Esp32SdmmcPin_Max, +}; + enum Esp32_MapDeviceType { DEV_TYPE_GPIO, @@ -51,9 +62,12 @@ enum Esp32_MapDeviceType DEV_TYPE_ADC, DEV_TYPE_I2S, DEV_TYPE_DAC, + DEV_TYPE_SDMMC, DEV_TYPE_MAX, }; +#ifdef __cplusplus + extern int8_t Esp32_SPI_DevicePinMap[MAX_SPI_DEVICES][Esp32SpiPin_Max]; extern int8_t Esp32_I2C_DevicePinMap[I2C_NUM_MAX][2]; extern int8_t Esp32_I2S_DevicePinMap[I2S_NUM_MAX][5]; @@ -79,7 +93,9 @@ extern int8_t Esp32_DAC_DevicePinMap[2]; extern int8_t Esp32_ADC_DevicePinMap[TARGET_ADC_NUM_PINS]; extern int8_t Esp32_LED_DevicePinMap[TARGET_LED_NUM_PINS]; - +#if (defined(CONFIG_IDF_TARGET_ESP32) || defined(CONFIG_IDF_TARGET_ESP32S3) || defined(CONFIG_IDF_TARGET_ESP32P4)) +extern int8_t Esp32_SDMMC_DevicePinMap[CONFIG_SOC_SDMMC_NUM_SLOTS][Esp32SdmmcPin_Max]; +#endif void Esp32_DecodeAlternateFunction( uint32_t alternateFunction, @@ -95,4 +111,9 @@ void Esp32_SetMappedDevicePins(Esp32_MapDeviceType deviceType, int busIndex, int int Esp32_ValidateMappedDevicePin(Esp32_MapDeviceType deviceType, int ioPinNumber); +#else +// For C code access +int Esp32_GetSDmmcDevicePins_C(int busIndex, int pinIndex); +#endif + #endif // ESP32_DEVICEMAPPING_H diff --git a/targets/ESP32/_nanoCLR/System.Device.Gpio/cpu_gpio.cpp b/targets/ESP32/_nanoCLR/System.Device.Gpio/cpu_gpio.cpp index bef4304c7c..4dd38a241e 100644 --- a/targets/ESP32/_nanoCLR/System.Device.Gpio/cpu_gpio.cpp +++ b/targets/ESP32/_nanoCLR/System.Device.Gpio/cpu_gpio.cpp @@ -58,6 +58,7 @@ struct gpio_input_state : public HAL_DblLinkedNode uint32_t debounceMs; // debounce Millsecs, no debonce=0 uint8_t mode; // Interrupt mode void *param; // Param to user isr call + bool current; // Current state bool expected; // Expected state for debounce handler bool waitingDebounce; // True if waiting for debounce timer to complete }; @@ -144,6 +145,7 @@ static void Esp_Gpio_DebounceHandler(TimerHandle_t timer) if (actual == state->expected) { + state->current = actual; state->isrPtr(state->pinNumber, actual, state->param); if (state->mode == GPIO_INT_EDGE_BOTH) @@ -323,8 +325,8 @@ static void gpio_isr(void *arg) // flag waiting for debounce timeout state->waitingDebounce = true; - // store current state - state->expected = actual; + // store expected new state + state->expected = !state->current; // start timer xTimerStartFromISR(state->debounceTimer, &xHigherPriorityTaskWoken); @@ -336,6 +338,7 @@ static void gpio_isr(void *arg) } else { + state->current = actual; // just fire event state->isrPtr(state->pinNumber, actual, state->param); } @@ -397,6 +400,7 @@ bool CPU_GPIO_EnableInputPin( state->mode = intEdge; state->param = (void *)isr_Param; state->debounceMs = debounceTimeMilliseconds; + state->current = CPU_GPIO_GetPinState(pinNumber); switch (intEdge) { @@ -411,7 +415,7 @@ bool CPU_GPIO_EnableInputPin( break; case GPIO_INT_EDGE_BOTH: - state->expected = !CPU_GPIO_GetPinState(pinNumber); // Use not current state + state->expected = !state->current; // Use not current state break; default: diff --git a/targets/ESP32/_nanoCLR/System.IO.FileSystem/nf_sys_io_filesystem_nanoFramework_System_IO_FileSystem_SDCard.cpp b/targets/ESP32/_nanoCLR/System.IO.FileSystem/nf_sys_io_filesystem_nanoFramework_System_IO_FileSystem_SDCard.cpp index 104cb0700c..34034ebc09 100644 --- a/targets/ESP32/_nanoCLR/System.IO.FileSystem/nf_sys_io_filesystem_nanoFramework_System_IO_FileSystem_SDCard.cpp +++ b/targets/ESP32/_nanoCLR/System.IO.FileSystem/nf_sys_io_filesystem_nanoFramework_System_IO_FileSystem_SDCard.cpp @@ -4,45 +4,30 @@ // #include "nf_sys_io_filesystem.h" +#include "Esp32_DeviceMapping.h" -StorageEventType GetStorageEventType(bool pinState) +void postManagedStorageEvent(bool pinState, uint32_t slotIndex) { - // Low means card inserted - return pinState ? StorageEventType_RemovableDeviceRemoval : StorageEventType_RemovableDeviceInsertion; -} - -void postManagedStorageEvent(bool pinState, uint32_t driveIndex) -{ - PostManagedEvent(EVENT_STORAGE, 0, GetStorageEventType(pinState), driveIndex); + ets_printf("post pin %d slot %d\n", pinState, slotIndex); + PostManagedEvent(EVENT_STORAGE, StorageEventType_CardDetectChanged, pinState, slotIndex); } void cardDetect_interrupt(GPIO_PIN Pin, bool pinState, void *pArg) { (void)Pin; - postManagedStorageEvent(pinState, (uint32_t)pArg); + // Decode pArg + int slotIndex = (uint32_t)(0xff && pArg); + + postManagedStorageEvent(pinState, slotIndex); } #if SOC_SDMMC_HOST_SUPPORTED -// Reserve all MMC pins -// CMD, Data0, Data1, Data2, Data3 -int8_t pins4bit[] = {15, 2, 4, 12, 13}; -// CMD, Data0 -int8_t pins1bit[] = {15, 2}; - -void GetMMCPins(bool _1bit, int *count, int8_t **pPins) +void GetMMCPins(int slotIndex, bool _1bit, int *count, int8_t **pPins) { - if (_1bit) - { - *pPins = pins1bit; - *count = sizeof(pins1bit); - } - else - { - *pPins = pins4bit; - *count = sizeof(pins4bit); - } + *pPins = &Esp32_SDMMC_DevicePinMap[slotIndex][Esp32SdmmcPin_Clock]; + *count = _1bit ? 3 : 6; } #endif @@ -85,15 +70,19 @@ HRESULT Library_nf_sys_io_filesystem_nanoFramework_System_IO_FileSystem_SDCard:: NANOCLR_HEADER(); SDCard_SDInterfaceType cardType; + int slotIndex; bool useCardDetect = false; + bool cardDetectedState = false; int cardDetectPin; - bool cardDetectpinState; + bool cdPinState; // get a pointer to the managed object instance and check that it's not NULL CLR_RT_HeapBlock *pThis = stack.This(); FAULT_ON_NULL(pThis); + slotIndex = (pThis[FIELD___slotIndex].NumericByRef().u4); useCardDetect = (bool)(pThis[FIELD___enableCardDetectPin].NumericByRef().s4); + cardDetectedState = (bool)(pThis[FIELD___cardDetectedState].NumericByRef().s4); cardDetectPin = (int)(pThis[FIELD___cardDetectPin].NumericByRef().s4); cardType = (SDCard_SDInterfaceType)pThis[FIELD___sdCardType].NumericByRef().s4; @@ -102,12 +91,13 @@ HRESULT Library_nf_sys_io_filesystem_nanoFramework_System_IO_FileSystem_SDCard:: case SDCard_SDInterfaceType::SDCard_SDInterfaceType_Mmc: { #if SOC_SDMMC_HOST_SUPPORTED + bool bit1Mode = pThis[FIELD___dataWidth].NumericByRef().s4 == SDCard_SDDataWidth::SDCard_SDDataWidth__1_bit; int count; int8_t *pPins; - GetMMCPins(bit1Mode, &count, &pPins); + GetMMCPins(slotIndex, bit1Mode, &count, &pPins); if (!TryReservePins(count, pPins)) { @@ -121,7 +111,6 @@ HRESULT Library_nf_sys_io_filesystem_nanoFramework_System_IO_FileSystem_SDCard:: case SDCard_SDInterfaceType::SDCard_SDInterfaceType_Spi: // TODO reserve pins ? - // or init SPI break; // No predefined system SD card or other @@ -138,24 +127,27 @@ HRESULT Library_nf_sys_io_filesystem_nanoFramework_System_IO_FileSystem_SDCard:: } // Set interrupt for change on Card Detect GPIO pin + PinMode pullups = cardDetectedState? PinMode::PinMode_InputPullDown : PinMode::PinMode_InputPullUp; + if (!CPU_GPIO_EnableInputPin( cardDetectPin, - 100, + 200, cardDetect_interrupt, - 0, + (void*)slotIndex, GPIO_INT_EDGE::GPIO_INT_EDGE_BOTH, - PinMode::PinMode_InputPullUp)) + pullups)) { CPU_GPIO_ReservePin(cardDetectPin, false); NANOCLR_SET_AND_LEAVE(CLR_E_INVALID_PARAMETER); } - cardDetectpinState = CPU_GPIO_GetPinState(cardDetectPin); - if (GetStorageEventType(cardDetectpinState) == StorageEventType_RemovableDeviceInsertion) + // Check if we need to fire an event on start + cdPinState = CPU_GPIO_GetPinState(cardDetectPin); + if (cdPinState == cardDetectedState) { // Fire event for initial card detect state if card inserted // This is so mount can be done solely in event handler - postManagedStorageEvent(cardDetectpinState, 0); + postManagedStorageEvent(cdPinState, slotIndex); } } @@ -168,12 +160,15 @@ HRESULT Library_nf_sys_io_filesystem_nanoFramework_System_IO_FileSystem_SDCard:: NANOCLR_HEADER(); SDCard_SDInterfaceType cardType; + int slotIndex; int cardDetectPin; // get a pointer to the managed object instance and check that it's not NULL CLR_RT_HeapBlock *pThis = stack.This(); FAULT_ON_NULL(pThis); + slotIndex = (pThis[FIELD___slotIndex].NumericByRef().u4); + if ((bool)(pThis[FIELD___enableCardDetectPin].NumericByRef().s4)) { // Stop card detect interupt @@ -194,7 +189,7 @@ HRESULT Library_nf_sys_io_filesystem_nanoFramework_System_IO_FileSystem_SDCard:: int count; int8_t *pPins; - GetMMCPins(bit1Mode, &count, &pPins); + GetMMCPins(slotIndex, bit1Mode, &count, &pPins); UnReservePins(count, pPins); #else @@ -218,6 +213,8 @@ HRESULT Library_nf_sys_io_filesystem_nanoFramework_System_IO_FileSystem_SDCard:: #if (HAL_USE_SDC == TRUE) SDCard_SDInterfaceType cardType; + int slotIndex; + char mountPoint[] = INDEX0_DRIVE_LETTER; // get a pointer to the managed object instance and check that it's not NULL CLR_RT_HeapBlock *pThis = stack.This(); @@ -228,6 +225,8 @@ HRESULT Library_nf_sys_io_filesystem_nanoFramework_System_IO_FileSystem_SDCard:: NANOCLR_SET_AND_LEAVE(CLR_E_OBJECT_DISPOSED); } + slotIndex = (pThis[FIELD___slotIndex].NumericByRef().u4); + pThis[FIELD___mounted].NumericByRef().s4 = 0; // Get Card type so we know what parameters to use @@ -237,9 +236,14 @@ HRESULT Library_nf_sys_io_filesystem_nanoFramework_System_IO_FileSystem_SDCard:: case SDCard_SDInterfaceType::SDCard_SDInterfaceType_Mmc: { #if SOC_SDMMC_HOST_SUPPORTED + if (slotIndex < 0 || slotIndex > (CONFIG_SOC_SDMMC_NUM_SLOTS - 1)) + { + NANOCLR_SET_AND_LEAVE(CLR_E_INVALID_PARAMETER); + } + bool bit1Mode = pThis[FIELD___dataWidth].NumericByRef().s4 == SDCard_SDDataWidth::SDCard_SDDataWidth__1_bit; - if (!Storage_MountMMC(bit1Mode, 0)) + if (!Storage_MountMMC(bit1Mode, slotIndex)) { NANOCLR_SET_AND_LEAVE(CLR_E_VOLUME_NOT_FOUND); } @@ -255,6 +259,12 @@ HRESULT Library_nf_sys_io_filesystem_nanoFramework_System_IO_FileSystem_SDCard:: int chipSelectPin; SPI_DEVICE_CONFIGURATION spiConfig; + // Allow drive index 0-3 for SPI + if (slotIndex < 0 || slotIndex >= 4) + { + NANOCLR_SET_AND_LEAVE(CLR_E_INVALID_PARAMETER); + } + // internally SPI bus ID is zero based busIndex = (int)(pThis[FIELD___spiBus].NumericByRef().s4 - 1); chipSelectPin = (int)(pThis[FIELD___chipSelectPin].NumericByRef().s4); @@ -267,9 +277,9 @@ HRESULT Library_nf_sys_io_filesystem_nanoFramework_System_IO_FileSystem_SDCard:: CPU_SPI_Initialize(busIndex, spiConfig); // Try mount twice - if (!Storage_MountSpi(busIndex, chipSelectPin, 0)) + if (!Storage_MountSpi(busIndex, chipSelectPin, slotIndex)) { - if (!Storage_MountSpi(busIndex, chipSelectPin, 0)) + if (!Storage_MountSpi(busIndex, chipSelectPin, slotIndex)) { NANOCLR_SET_AND_LEAVE(CLR_E_VOLUME_NOT_FOUND); } @@ -284,7 +294,8 @@ HRESULT Library_nf_sys_io_filesystem_nanoFramework_System_IO_FileSystem_SDCard:: } // switch // add volume to the file system - FS_MountVolume(INDEX0_DRIVE_LETTER, 0, "FATFS"); + mountPoint[0] += slotIndex; + FS_MountVolume(mountPoint, 0, "FATFS"); pThis[FIELD___mounted].NumericByRef().s4 = 1; @@ -306,6 +317,7 @@ HRESULT Library_nf_sys_io_filesystem_nanoFramework_System_IO_FileSystem_SDCard:: #if (HAL_USE_SDC == TRUE) SDCard_SDInterfaceType cardType; + int slotIndex; // get a pointer to the managed object instance and check that it's not NULL CLR_RT_HeapBlock *pThis = stack.This(); @@ -316,6 +328,8 @@ HRESULT Library_nf_sys_io_filesystem_nanoFramework_System_IO_FileSystem_SDCard:: NANOCLR_SET_AND_LEAVE(CLR_E_OBJECT_DISPOSED); } + slotIndex = (pThis[FIELD___slotIndex].NumericByRef().u4); + cardType = (SDCard_SDInterfaceType)pThis[FIELD___sdCardType].NumericByRef().s4; switch (cardType) { @@ -323,7 +337,7 @@ HRESULT Library_nf_sys_io_filesystem_nanoFramework_System_IO_FileSystem_SDCard:: case SDCard_SDInterfaceType_Spi: // Unmount SPI device - if (!Storage_UnMountSDCard(0)) + if (!Storage_UnMountSDCard(slotIndex)) { // SDcard not mounted NANOCLR_SET_AND_LEAVE(CLR_E_INVALID_OPERATION); @@ -358,6 +372,7 @@ HRESULT Library_nf_sys_io_filesystem_nanoFramework_System_IO_FileSystem_SDCard:: #if (HAL_USE_SDC == TRUE) int cardDetectPin; + GpioPinValue detectedState; CLR_RT_HeapBlock *pThis = stack.This(); FAULT_ON_NULL(pThis); @@ -373,7 +388,8 @@ HRESULT Library_nf_sys_io_filesystem_nanoFramework_System_IO_FileSystem_SDCard:: // Poll Card Detect pin cardDetectPin = (int)pThis[FIELD___cardDetectPin].NumericByRef().s4; - stack.SetResult_Boolean(CPU_GPIO_GetPinState(cardDetectPin) == GpioPinValue_Low); + detectedState = (bool)pThis[FIELD___cardDetectedState].NumericByRef().s4 ? GpioPinValue_High : GpioPinValue_Low; + stack.SetResult_Boolean(CPU_GPIO_GetPinState(cardDetectPin) == detectedState); #else From 9ace5dfe96b24fbfc0577401248b508373acd044 Mon Sep 17 00:00:00 2001 From: Adrian Soundy Date: Tue, 13 Aug 2024 17:54:49 +1200 Subject: [PATCH 2/5] Removed test code --- ...s_io_filesystem_nanoFramework_System_IO_FileSystem_SDCard.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/targets/ESP32/_nanoCLR/System.IO.FileSystem/nf_sys_io_filesystem_nanoFramework_System_IO_FileSystem_SDCard.cpp b/targets/ESP32/_nanoCLR/System.IO.FileSystem/nf_sys_io_filesystem_nanoFramework_System_IO_FileSystem_SDCard.cpp index 34034ebc09..3470fd67b0 100644 --- a/targets/ESP32/_nanoCLR/System.IO.FileSystem/nf_sys_io_filesystem_nanoFramework_System_IO_FileSystem_SDCard.cpp +++ b/targets/ESP32/_nanoCLR/System.IO.FileSystem/nf_sys_io_filesystem_nanoFramework_System_IO_FileSystem_SDCard.cpp @@ -8,7 +8,6 @@ void postManagedStorageEvent(bool pinState, uint32_t slotIndex) { - ets_printf("post pin %d slot %d\n", pinState, slotIndex); PostManagedEvent(EVENT_STORAGE, StorageEventType_CardDetectChanged, pinState, slotIndex); } From 668f86fcc663ab8b96d735bb2af7b6a22471f85e Mon Sep 17 00:00:00 2001 From: nfbot Date: Tue, 13 Aug 2024 05:57:16 +0000 Subject: [PATCH 3/5] Code style fixes Automated fixes for code style. --- targets/ESP32/_common/DeviceMapping_common.cpp | 15 +++++++-------- targets/ESP32/_include/Esp32_DeviceMapping.h | 2 +- ..._nanoFramework_System_IO_FileSystem_SDCard.cpp | 6 +++--- 3 files changed, 11 insertions(+), 12 deletions(-) diff --git a/targets/ESP32/_common/DeviceMapping_common.cpp b/targets/ESP32/_common/DeviceMapping_common.cpp index 5d3a1ca8ab..a8d5fed9fc 100644 --- a/targets/ESP32/_common/DeviceMapping_common.cpp +++ b/targets/ESP32/_common/DeviceMapping_common.cpp @@ -6,7 +6,6 @@ #include #include - void Esp32_DecodeAlternateFunction( uint32_t alternateFunction, Esp32_MapDeviceType &deviceType, @@ -58,7 +57,7 @@ int Esp32_GetMappedDevicePins(Esp32_MapDeviceType deviceType, int busIndex, int return (int)Esp32_DAC_DevicePinMap[pinIndex]; #endif -#if (defined(CONFIG_IDF_TARGET_ESP32)|| defined(CONFIG_IDF_TARGET_ESP32S3)) +#if (defined(CONFIG_IDF_TARGET_ESP32) || defined(CONFIG_IDF_TARGET_ESP32S3)) case DEV_TYPE_SDMMC: return (int)Esp32_SDMMC_DevicePinMap[busIndex][pinIndex]; #endif @@ -210,12 +209,12 @@ int Esp32_SetMappedDevicePins(uint8_t pin, int32_t alternateFunction) #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; + if (busIndex <= 1) + { + Esp32_SDMMC_DevicePinMap[busIndex][gpioMapping] = pin; + return true; + } + break; #endif default: diff --git a/targets/ESP32/_include/Esp32_DeviceMapping.h b/targets/ESP32/_include/Esp32_DeviceMapping.h index ba1b9426f9..6a3e7c50f5 100644 --- a/targets/ESP32/_include/Esp32_DeviceMapping.h +++ b/targets/ESP32/_include/Esp32_DeviceMapping.h @@ -13,7 +13,7 @@ #include "TargetHAL_Spi.h" // Note - I2S_NUM_MAX renamed/changed as of IDF 5.1.3 and I2S_PIN_NO_CHANGE missing -#define I2S_NUM_MAX I2S_NUM_AUTO +#define I2S_NUM_MAX I2S_NUM_AUTO #define I2S_PIN_NO_CHANGE (-1) // Default I2C gpio pins diff --git a/targets/ESP32/_nanoCLR/System.IO.FileSystem/nf_sys_io_filesystem_nanoFramework_System_IO_FileSystem_SDCard.cpp b/targets/ESP32/_nanoCLR/System.IO.FileSystem/nf_sys_io_filesystem_nanoFramework_System_IO_FileSystem_SDCard.cpp index 3470fd67b0..5125145fd5 100644 --- a/targets/ESP32/_nanoCLR/System.IO.FileSystem/nf_sys_io_filesystem_nanoFramework_System_IO_FileSystem_SDCard.cpp +++ b/targets/ESP32/_nanoCLR/System.IO.FileSystem/nf_sys_io_filesystem_nanoFramework_System_IO_FileSystem_SDCard.cpp @@ -25,7 +25,7 @@ void cardDetect_interrupt(GPIO_PIN Pin, bool pinState, void *pArg) void GetMMCPins(int slotIndex, bool _1bit, int *count, int8_t **pPins) { - *pPins = &Esp32_SDMMC_DevicePinMap[slotIndex][Esp32SdmmcPin_Clock]; + *pPins = &Esp32_SDMMC_DevicePinMap[slotIndex][Esp32SdmmcPin_Clock]; *count = _1bit ? 3 : 6; } @@ -126,13 +126,13 @@ HRESULT Library_nf_sys_io_filesystem_nanoFramework_System_IO_FileSystem_SDCard:: } // Set interrupt for change on Card Detect GPIO pin - PinMode pullups = cardDetectedState? PinMode::PinMode_InputPullDown : PinMode::PinMode_InputPullUp; + PinMode pullups = cardDetectedState ? PinMode::PinMode_InputPullDown : PinMode::PinMode_InputPullUp; if (!CPU_GPIO_EnableInputPin( cardDetectPin, 200, cardDetect_interrupt, - (void*)slotIndex, + (void *)slotIndex, GPIO_INT_EDGE::GPIO_INT_EDGE_BOTH, pullups)) { From b833e6b258759f92dc1dadffb7c8b533fedf7c41 Mon Sep 17 00:00:00 2001 From: Adrian Soundy Date: Tue, 13 Aug 2024 18:10:23 +1200 Subject: [PATCH 4/5] Update nf_sys_io_filesystem_nanoFramework_System_IO_FileSystem_SDCard.cpp Suggested coderabit change --- ..._io_filesystem_nanoFramework_System_IO_FileSystem_SDCard.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/targets/ESP32/_nanoCLR/System.IO.FileSystem/nf_sys_io_filesystem_nanoFramework_System_IO_FileSystem_SDCard.cpp b/targets/ESP32/_nanoCLR/System.IO.FileSystem/nf_sys_io_filesystem_nanoFramework_System_IO_FileSystem_SDCard.cpp index 5125145fd5..fc344a019a 100644 --- a/targets/ESP32/_nanoCLR/System.IO.FileSystem/nf_sys_io_filesystem_nanoFramework_System_IO_FileSystem_SDCard.cpp +++ b/targets/ESP32/_nanoCLR/System.IO.FileSystem/nf_sys_io_filesystem_nanoFramework_System_IO_FileSystem_SDCard.cpp @@ -16,7 +16,7 @@ void cardDetect_interrupt(GPIO_PIN Pin, bool pinState, void *pArg) (void)Pin; // Decode pArg - int slotIndex = (uint32_t)(0xff && pArg); + int slotIndex = (uint32_t)0xff && pArg; postManagedStorageEvent(pinState, slotIndex); } From 3fd06c18b2774e1b6a67aa2600046aea67ae2cbd Mon Sep 17 00:00:00 2001 From: Adrian Soundy Date: Wed, 14 Aug 2024 17:24:46 +1200 Subject: [PATCH 5/5] Fix build error with risv targets --- ...filesystem_nanoFramework_System_IO_FileSystem_SDCard.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/targets/ESP32/_nanoCLR/System.IO.FileSystem/nf_sys_io_filesystem_nanoFramework_System_IO_FileSystem_SDCard.cpp b/targets/ESP32/_nanoCLR/System.IO.FileSystem/nf_sys_io_filesystem_nanoFramework_System_IO_FileSystem_SDCard.cpp index fc344a019a..e0d3f61602 100644 --- a/targets/ESP32/_nanoCLR/System.IO.FileSystem/nf_sys_io_filesystem_nanoFramework_System_IO_FileSystem_SDCard.cpp +++ b/targets/ESP32/_nanoCLR/System.IO.FileSystem/nf_sys_io_filesystem_nanoFramework_System_IO_FileSystem_SDCard.cpp @@ -159,14 +159,18 @@ HRESULT Library_nf_sys_io_filesystem_nanoFramework_System_IO_FileSystem_SDCard:: NANOCLR_HEADER(); SDCard_SDInterfaceType cardType; - int slotIndex; int cardDetectPin; +#if SOC_SDMMC_HOST_SUPPORTED + int slotIndex; +#endif // get a pointer to the managed object instance and check that it's not NULL CLR_RT_HeapBlock *pThis = stack.This(); FAULT_ON_NULL(pThis); +#if SOC_SDMMC_HOST_SUPPORTED slotIndex = (pThis[FIELD___slotIndex].NumericByRef().u4); +#endif if ((bool)(pThis[FIELD___enableCardDetectPin].NumericByRef().s4)) {