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

Improve Storage part 2 #2849

Closed
wants to merge 21 commits into from
Closed
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,43 @@
// Copyright (c) .NET Foundation and Contributors
// See LICENSE file in the project root for full license information.
//
#ifndef NANOHAL_WINDOWS_STORAGE_H
#define NANOHAL_WINDOWS_STORAGE_H
#ifndef NANOHAL_PERSISTENT_STORAGE_H
#define NANOHAL_PERSISTENT_STORAGE_H

#include <Target_Windows_Storage.h>

// FatFs define for size of file name members
// ANSI/OEM at DBCS
#ifndef FF_LFN_BUF
#define FF_LFN_BUF 255
#endif

// driver letters and paths
// drive letter for SD Card
#define INDEX0_DRIVE_LETTER "D:"
#define INDEX0_DRIVE_PATH INDEX0_DRIVE_LETTER "\\"
// drive letter for USB mass storage device
#define INDEX1_DRIVE_LETTER "E:"
#define INDEX1_DRIVE_PATH INDEX1_DRIVE_LETTER "\\"
// spare drive letter
#define INDEX2_DRIVE_LETTER "F:"
#define INDEX2_DRIVE_PATH INDEX2_DRIVE_LETTER "\\"
// drive letter for internal drive (SPIFFS)
#define INTERNAL_DRIVE0_LETTER "I:"
#define INTERNAL_DRIVE0_PATH INTERNAL_DRIVE0_LETTER "\\"
// drive letter for second internal drive (SPIFFS)
#define INTERNAL_DRIVE1_LETTER "J:"
#define INTERNAL_DRIVE1_PATH INTERNAL_DRIVE1_LETTER "\\"

// SD Card
// #define SDCARD_DRIVE_NAME "SD"
#define SDCARD_DRIVE_LETTER "D"
#define SDCARD_DRIVE_PATH SDCARD_DRIVE_LETTER ":\\"

// USB mass storage device
// #define USB_DRIVE_NAME "USB"
#define USB_DRIVE_LETTER "E"
#define USB_DRIVE_PATH USB_DRIVE_LETTER ":\\"

// USB mass storage device 2
// #define USB2_DRIVE_NAME "USB2"
#define USB2_DRIVE_LETTER "F"
#define USB2_DRIVE_PATH USB2_DRIVE_LETTER ":\\"

// Internal drive 0 (SPIFFS)
// #define INTERNAL_DRIVE0_NAME "SPI"
#define INTERNAL_DRIVE0_LETTER "I"
#define INTERNAL_DRIVE0_PATH INTERNAL_DRIVE0_LETTER ":\\"

// Internal drive 1 (SPIFFS)
// #define INTERNAL_DRIVE1_NAME "SPI2"
#define INTERNAL_DRIVE1_LETTER "J"
#define INTERNAL_DRIVE1_PATH INTERNAL_DRIVE1_LETTER ":\\"

//////////////////////////////////////////////////////////////////////////
// Keep in sync with StorageEventManager.StorageEventType in managed code
Expand All @@ -47,8 +59,8 @@ typedef enum StorageEventType

// constants to be used throughout the code
#define SUPPORTED_DRIVES_COUNT (2)
#define DRIVE_LETTER_LENGTH sizeof(INDEX0_DRIVE_LETTER)
#define DRIVE_PATH_LENGTH sizeof(INDEX0_DRIVE_PATH)
#define DRIVE_LETTER_LENGTH sizeof(SDCARD_DRIVE_LETTER)
#define DRIVE_PATH_LENGTH sizeof(SDCARD_DRIVE_PATH)

/////////////////////////////////////////////////////////////////////////////////////////
// !!! KEEP IN SYNC WITH Windows.Storage.CreationCollisionOption (in managed code) !!! //
Expand All @@ -73,4 +85,4 @@ extern "C"
}
#endif

#endif // NANOHAL_WINDOWS_STORAGE_H
#endif // NANOHAL_PERSISTENT_STORAGE_H
2 changes: 1 addition & 1 deletion src/HAL/Include/nanoHAL_v2.h
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,6 @@ extern "C"
#include <nanoHAL_Watchdog.h>
#include <nanoHAL_Rtos.h>

#include <nanoHAL_Windows_Storage.h>
#include <nanoHAL_Persistent_Storage.h>

#endif // NANOHAL_V2_H
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ HRESULT EnumerateDrives(CLR_RT_HeapBlock *array, int &count)
CLR_RT_HeapBlock *storageFolder = NULL;
FRESULT operationResult;
NANO_DIR currentDirectory;
char workingDrive[] = INDEX0_DRIVE_PATH;
char workingDrive[] = SDCARD_DRIVE_PATH;

if (array)
{
Expand All @@ -398,7 +398,7 @@ HRESULT EnumerateDrives(CLR_RT_HeapBlock *array, int &count)
}

count = 0;
for (char drive = INDEX0_DRIVE_LETTER[0]; drive <= INTERNAL_DRIVE0_LETTER[0]; drive++)
for (char drive = SDCARD_DRIVE_LETTER[0]; drive <= INTERNAL_DRIVE1_LETTER[0]; drive++)
{
workingDrive[0] = drive;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include "nf_sys_io_filesystem.h"

#include <ff.h>
#include <nanoHAL_Windows_Storage.h>
#include <nanoHAL_Persistent_Storage.h>

extern SYSTEMTIME GetDateTime(uint16_t date, uint16_t time);
extern void CombinePathAndName(char *outpath, const char *path1, const char *path2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include "nf_sys_io_filesystem.h"

#include <ff.h>
#include <nanoHAL_Windows_Storage.h>
#include <nanoHAL_Persistent_Storage.h>

void CombinePathAndName(char *outpath, const char *path1, const char *path2)
{
Expand Down
2 changes: 1 addition & 1 deletion targets/ChibiOS/_common/Target_Windows_Storage.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include <cmsis_os.h>
#include <ff.h>
#include <target_windows_storage_config.h>
#include <nanoHAL_Windows_Storage.h>
#include <nanoHAL_Persistent_Storage.h>
#include <Target_Windows_Storage.h>
#include <nanoHAL_v2.h>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@

int32_t GetInternalDriveIndex(char *drive)
{
if (memcmp(drive, INTERNAL_DRIVE0_LETTER, sizeof(INTERNAL_DRIVE0_LETTER) - 1) == 0)
if (memcmp(drive, INTERNAL_DRIVE0_LETTER, sizeof(INTERNAL_DRIVE0_LETTER)) == 0)
{
return 0;
}
if (memcmp(drive, INTERNAL_DRIVE1_LETTER, sizeof(INTERNAL_DRIVE1_LETTER) - 1) == 0)
if (memcmp(drive, INTERNAL_DRIVE1_LETTER, sizeof(INTERNAL_DRIVE1_LETTER)) == 0)
{
return 1;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ HRESULT Library_win_storage_native_Windows_Storage_StorageFolder::
// is the SD card file system ready?
if (sdCardFileSystemReady)
{
memcpy(workingDrive, INDEX0_DRIVE_PATH, DRIVE_PATH_LENGTH);
memcpy(workingDrive, SDCARD_DRIVE_PATH, DRIVE_PATH_LENGTH);
}
else
{
Expand All @@ -160,7 +160,7 @@ HRESULT Library_win_storage_native_Windows_Storage_StorageFolder::
// is the USB mass storage device file system ready?
if (usbMsdFileSystemReady)
{
memcpy(workingDrive, INDEX1_DRIVE_PATH, DRIVE_PATH_LENGTH);
memcpy(workingDrive, USB_DRIVE_PATH, DRIVE_PATH_LENGTH);
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,12 @@
#include <hal.h>
#include <win_storage_native.h>
#include <target_windows_storage_config.h>
#include <nanoHAL_Windows_Storage.h>
#include <nanoHAL_Persistent_Storage.h>
#include <Target_Windows_Storage.h>
#include <nanoHAL_Windows_Storage.h>

#define WORKING_DRIVE_IS_INTERNAL_DRIVE \
(memcmp(workingDrive, INTERNAL_DRIVE0_LETTER, sizeof(INTERNAL_DRIVE0_LETTER) - 1) == 0 || \
memcmp(workingDrive, INTERNAL_DRIVE1_LETTER, sizeof(INTERNAL_DRIVE1_LETTER) - 1) == 0)
(memcmp(workingDrive, INTERNAL_DRIVE0_LETTER, sizeof(INTERNAL_DRIVE0_LETTER)) == 0 || \
memcmp(workingDrive, INTERNAL_DRIVE1_LETTER, sizeof(INTERNAL_DRIVE1_LETTER)) == 0)

extern int32_t GetInternalDriveIndex(char *drive);

Expand Down
6 changes: 3 additions & 3 deletions targets/ESP32/_common/Target_Windows_Storage.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
#include <target_platform.h>

#include <Target_Windows_Storage.h>
#include <nanoHAL_Windows_Storage.h>
#include <nanoHAL_Persistent_Storage.h>

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

Expand Down Expand Up @@ -80,7 +80,7 @@ bool LogMountResult(esp_err_t errCode)
bool Storage_MountMMC(bool bit1Mode, int driveIndex)
{
esp_err_t errCode;
char mountPoint[] = INDEX0_DRIVE_LETTER;
char mountPoint[] = SDCARD_DRIVE_LETTER;

// Change fatfs drive letter to mount point D: -> /D for ESP32 VFS
mountPoint[1] = mountPoint[0] + driveIndex;
Expand Down Expand Up @@ -146,7 +146,7 @@ bool Storage_MountMMC(bool bit1Mode, int driveIndex)
bool Storage_MountSpi(int spiBus, uint32_t csPin, int driveIndex)
{
esp_err_t errCode;
char mountPoint[] = INDEX0_DRIVE_LETTER;
char mountPoint[] = SDCARD_DRIVE_LETTER;

// Change fatfs drive letter to mount point D: -> /D for ESP32 VFS
mountPoint[1] = mountPoint[0] + driveIndex;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ static HRESULT EnumerateDrives(CLR_RT_HeapBlock *array, int &count)
{
CLR_RT_HeapBlock *storageFolder = NULL;
DIR *currentDirectory;
char outputDrive[] = INDEX0_DRIVE_PATH;
char outputDrive[] = SDCARD_DRIVE_PATH;
char workingDrive[] = "/D/";

if (array)
Expand All @@ -399,7 +399,7 @@ static HRESULT EnumerateDrives(CLR_RT_HeapBlock *array, int &count)
}

count = 0;
for (char drive = INDEX0_DRIVE_LETTER[0]; drive <= INTERNAL_DRIVE0_LETTER[0]; drive++)
for (char drive = SDCARD_DRIVE_LETTER[0]; drive <= INTERNAL_DRIVE1_LETTER[0]; drive++)
{
workingDrive[1] = drive;
currentDirectory = opendir(workingDrive);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include "nf_sys_io_filesystem.h"

#include <ff.h>
#include <nanoHAL_Windows_Storage.h>
#include <nanoHAL_Persistent_Storage.h>

extern void CombinePathAndName(char *outpath, const char *path1, const char *path2);
extern char *ConvertToVfsPath(const char *filepath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include "nf_sys_io_filesystem.h"

#include <ff.h>
#include <nanoHAL_Windows_Storage.h>
#include <nanoHAL_Persistent_Storage.h>

void CombinePathAndName(char *outpath, const char *path1, const char *path2)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

#include "win_storage_native_target.h"
#include <target_windows_storage_config.h>
#include <nanoHAL_Windows_Storage.h>
#include <nanoHAL_Persistent_Storage.h>
#include <target_platform.h>
#include <Esp32_DeviceMapping.h>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include <ff.h>
#include "win_storage_native_target.h"
#include <target_windows_storage_config.h>
#include <nanoHAL_Windows_Storage.h>
#include <nanoHAL_Persistent_Storage.h>
#include <target_platform.h>

// defining these types here to make it shorter and improve code readability
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include "win_storage_native_target.h"
#include <target_windows_storage_config.h>
#include <Target_Windows_Storage.h>
#include <nanoHAL_Windows_Storage.h>
#include <nanoHAL_Persistent_Storage.h>
#include <target_platform.h>

//
Expand Down Expand Up @@ -202,7 +202,7 @@ HRESULT Library_win_storage_native_Windows_Storage_StorageFolder::
// fill the folder name and path
if (driveIterator == 0 && sdCardDrivePresent == true)
{
memcpy(workingDrive, INDEX0_DRIVE_PATH, DRIVE_PATH_LENGTH);
memcpy(workingDrive, SDCARD_DRIVE_PATH, DRIVE_PATH_LENGTH);
}
else
{
Expand Down
41 changes: 19 additions & 22 deletions targets/FreeRTOS/NXP/_common/Target_Windows_Storage.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,17 @@
#include "task.h"

#include "ff.h"
#include <nanoHAL_Windows_Storage.h>
#include <nanoHAL_Persistent_Storage.h>
#include <Target_Windows_Storage.h>
#include <nanoHAL_v2.h>

#include "fsl_sd.h"


// need to declare this here as extern
extern void PostManagedEvent(uint8_t category, uint8_t subCategory, uint16_t data1, uint32_t data2);

#define SD_CARD_DRIVE_INDEX "0"
#define SD_CARD_DRIVE_INDEX_NUMERIC (0)

#define SD_CARD_DRIVE_INDEX "0"
#define SD_CARD_DRIVE_INDEX_NUMERIC (0)

///////////////////////////////////////////
// code specific to SD Card
Expand All @@ -31,12 +29,12 @@ bool sdCardFileSystemReady;
static FATFS sdCard_FS;

/*!
* call back function for SD card detect.
*
* @param isInserted true, indicate the card is insert.
* false, indicate the card is remove.
* @param userData
*/
* call back function for SD card detect.
*
* @param isInserted true, indicate the card is insert.
* false, indicate the card is remove.
* @param userData
*/
static void SDCARD_DetectCallBack(bool isInserted, void *userData);

/*! Card descriptor. */
Expand Down Expand Up @@ -83,44 +81,45 @@ static void CardDetectTask(void *pvParameters)
vTaskDelay(100 / portTICK_PERIOD_MS);
if (s_cardInserted)
{
//Card inserted.
// Card inserted.
/* power on the card */
SD_PowerOnCard(g_sd.host.base, g_sd.usrParam.pwr);
/* Init card. */
if (SD_CardInit(&g_sd))
{
//SD card init failed.
// SD card init failed.
continue;
}

FRESULT err = f_mount(&sdCard_FS, "0", 1U);
if (err != FR_OK) {
if (err != FR_OK)
{
vTaskDelay(1000 / portTICK_PERIOD_MS);
SD_CardDeinit(&g_sd);
continue;
}
sdCardFileSystemReady = true;

// post event to managed app
PostManagedEvent( EVENT_STORAGE, 0, StorageEventType_RemovableDeviceInsertion, SD_CARD_DRIVE_INDEX_NUMERIC );
PostManagedEvent(EVENT_STORAGE, 0, StorageEventType_RemovableDeviceInsertion, SD_CARD_DRIVE_INDEX_NUMERIC);
}
else
{
SD_CardDeinit(&g_sd);

//Card removed
// Card removed
sdCardFileSystemReady = false;

// post event to managed app
PostManagedEvent( EVENT_STORAGE, 0, StorageEventType_RemovableDeviceRemoval, SD_CARD_DRIVE_INDEX_NUMERIC );
PostManagedEvent(EVENT_STORAGE, 0, StorageEventType_RemovableDeviceRemoval, SD_CARD_DRIVE_INDEX_NUMERIC);
}
}
}

void SdCardThread(void * argument)
void SdCardThread(void *argument)
{
(void)argument;

s_CardDetectSemaphore = xSemaphoreCreateBinary();

sdCardFileSystemReady = false;
Expand All @@ -134,7 +133,7 @@ void SdCardThread(void * argument)
/* SD host init function */
if (SD_HostInit(&g_sd) != kStatus_Success)
{
//SD host init fail
// SD host init fail
vTaskDelete(NULL);
return;
}
Expand All @@ -144,6 +143,4 @@ void SdCardThread(void * argument)
vTaskDelete(NULL);
}


///////////////////////////////////////////

Loading