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 #2868

Closed
wants to merge 25 commits into from
Closed
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
96facb1
Improve storage device defines.
networkfusion Dec 20, 2023
1ee0245
Define drive letter only
networkfusion Dec 20, 2023
446a84d
Rename file
networkfusion Dec 20, 2023
bbb45f2
Remove duplicate include
networkfusion Dec 20, 2023
19fb329
Code style fixes
nfbot Dec 21, 2023
8cfdae0
Merge pull request #12 from nanoframework/nfbot/clang-format-fix/ec79…
networkfusion Dec 21, 2023
b4c46d3
Merge branch 'main' into improve-fs
networkfusion Dec 21, 2023
05f3706
Update nf_sys_io_filesystem_System_IO_Directory.cpp
networkfusion Dec 21, 2023
e42675a
Merge branch 'nanoframework:main' into improve-fs
networkfusion Dec 21, 2023
7296b0a
Merge remote-tracking branch 'upstream/main' into improve-fs
networkfusion Dec 21, 2023
d0f3457
Revert changes I am too unsure of
networkfusion Dec 21, 2023
0a2775f
Merge branch 'main' into improve-fs
networkfusion Dec 27, 2023
5433bf5
Revert drive index changes
networkfusion Jan 15, 2024
39575ca
Align internal drive names
networkfusion Jan 15, 2024
b4a9b38
Code style fixes
nfbot Jan 15, 2024
04b8a1b
Merge pull request #13 from nanoframework/nfbot/clang-format-fix/feaf…
networkfusion Jan 15, 2024
25ff9ac
Fix dir enumeration
networkfusion Jan 15, 2024
9398f00
Add extra improvements
networkfusion Jan 15, 2024
49ca583
Revert stuff
networkfusion Jan 15, 2024
9e027ac
Code style fixes
nfbot Jan 15, 2024
091308b
Merge pull request #15 from nanoframework/nfbot/clang-format-fix/235e…
networkfusion Jan 15, 2024
67a2e36
Merge branch 'main' into improve-fs-1
networkfusion Jan 15, 2024
ce10a89
Merge branch 'main' into improve-fs-1
networkfusion Jan 17, 2024
e71a245
Merge branch 'main' into improve-fs-1
networkfusion Mar 3, 2024
63ad3e2
Merge branch 'main' into improve-fs-1
networkfusion Mar 3, 2024
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,40 @@
// 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 DRIVE_PATH_STR "\\"

// SD Card
#define INDEX0_DRIVE_LETTER "D:"
#define INDEX0_DRIVE_PATH INDEX0_DRIVE_LETTER "\\"
// drive letter for USB mass storage device
#define INDEX0_DRIVE_PATH INDEX0_DRIVE_LETTER DRIVE_PATH_STR

// USB mass storage device
#define INDEX1_DRIVE_LETTER "E:"
#define INDEX1_DRIVE_PATH INDEX1_DRIVE_LETTER "\\"
// spare drive letter
#define INDEX1_DRIVE_PATH INDEX1_DRIVE_LETTER DRIVE_PATH_STR

// USB mass storage device 2
#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 "\\"
#define INDEX2_DRIVE_PATH INDEX2_DRIVE_LETTER DRIVE_PATH_STR

// Internal drive 0 (SPIFFS)
#define INDEX3_DRIVE_LETTER "I:"
#define INDEX3_DRIVE_PATH INDEX3_DRIVE_LETTER DRIVE_PATH_STR

// Internal drive 1 (SPIFFS)
#define INDEX4_DRIVE_LETTER "J:"
#define INDEX4_DRIVE_PATH INDEX4_DRIVE_LETTER DRIVE_PATH_STR

//////////////////////////////////////////////////////////////////////////
// Keep in sync with StorageEventManager.StorageEventType in managed code
Expand All @@ -47,8 +56,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 (2)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why changing for 2?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My reasoning was that we know the length, so why base it off of a static field that should never change (as a compiler optimization. Happy to revert it though.

#define DRIVE_PATH_LENGTH (DRIVE_LETTER_LENGTH + sizeof(DRIVE_PATH_STR))

/////////////////////////////////////////////////////////////////////////////////////////
// !!! KEEP IN SYNC WITH Windows.Storage.CreationCollisionOption (in managed code) !!! //
Expand All @@ -73,4 +82,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 @@ -398,7 +398,9 @@ HRESULT EnumerateDrives(CLR_RT_HeapBlock *array, int &count)
}

count = 0;
for (char drive = INDEX0_DRIVE_LETTER[0]; drive <= INTERNAL_DRIVE0_LETTER[0]; drive++)
// SPIFFS cannot handle directory structures, so only check the valid drives.
// FIXME: return warning if the drive type cannot handle directories.
for (char drive = INDEX0_DRIVE_LETTER[0]; drive <= INDEX2_DRIVE_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, INDEX3_DRIVE_LETTER, sizeof(INDEX3_DRIVE_LETTER) - 1) == 0)
{
return 0;
}
if (memcmp(drive, INTERNAL_DRIVE1_LETTER, sizeof(INTERNAL_DRIVE1_LETTER) - 1) == 0)
if (memcmp(drive, INDEX4_DRIVE_LETTER, sizeof(INDEX4_DRIVE_LETTER) - 1) == 0)
{
return 1;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,11 +274,11 @@ HRESULT Library_win_storage_native_Windows_Storage_StorageFolder::
switch (driveIterator)
{
case 0:
memcpy(workingDrive, INTERNAL_DRIVE0_PATH, DRIVE_PATH_LENGTH);
memcpy(workingDrive, INDEX3_DRIVE_PATH, DRIVE_PATH_LENGTH);
break;

case 1:
memcpy(workingDrive, INTERNAL_DRIVE1_PATH, DRIVE_PATH_LENGTH);
memcpy(workingDrive, INDEX4_DRIVE_PATH, DRIVE_PATH_LENGTH);
break;

default:
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, INDEX3_DRIVE_LETTER, sizeof(INDEX3_DRIVE_LETTER) - 1) == 0 || \
memcmp(workingDrive, INDEX4_DRIVE_LETTER, sizeof(INDEX4_DRIVE_LETTER) - 1) == 0)

extern int32_t GetInternalDriveIndex(char *drive);

Expand Down
2 changes: 1 addition & 1 deletion 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
2 changes: 1 addition & 1 deletion targets/ESP32/_common/targetHAL_ConfigStorageSPIFFS.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include "targetHAL_ConfigStorage.h"
#include <esp_vfs.h>

// this one is following the the same "drive letter" as the HAL Windows Storage INTERNAL_DRIVE0_LETTER
// this one is following the the same "drive letter" as the HAL Windows Storage INDEX3_DRIVE_LETTER
#define NANO_SPIFFS_BASE_PATH "/I"
#define NANO_SPIFFS_CONFIG_NAME "con_"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,9 @@ static HRESULT EnumerateDrives(CLR_RT_HeapBlock *array, int &count)
}

count = 0;
for (char drive = INDEX0_DRIVE_LETTER[0]; drive <= INTERNAL_DRIVE0_LETTER[0]; drive++)
// SPIFFS cannot handle directory structures, so only check the valid drives.
// FIXME: return warning if the drive type cannot handle directories.
for (char drive = INDEX0_DRIVE_LETTER[0]; drive <= INDEX2_DRIVE_LETTER[0]; drive++)
{
workingDrive[1] = drive;
currentDirectory = opendir(workingDrive);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
#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);

bool IsInternalFilePath(const char *filePath)
{
if (filePath[0] == INTERNAL_DRIVE0_LETTER[0])
if (filePath[0] == INDEX3_DRIVE_LETTER[0])
{
return true;
}
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 @@ -302,7 +302,7 @@ HRESULT Library_win_storage_native_Windows_Storage_StorageFolder::
// dereference the object in order to reach its fields
hbObj = storageFolder->Dereference();

memcpy(workingDrive, INTERNAL_DRIVE0_PATH, DRIVE_PATH_LENGTH);
memcpy(workingDrive, INDEX3_DRIVE_PATH, DRIVE_PATH_LENGTH);

// set the managed fields
NANOCLR_CHECK_HRESULT(CLR_RT_HeapBlock_String::CreateInstance(
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);
}


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

Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@
// See LICENSE file in the project root for full license information.
//


#ifndef WIN_STORAGE_NATIVE_TARGET_H
#define WIN_STORAGE_NATIVE_TARGET_H

#include <ff.h>
#include <corlib_native.h>
#include "win_storage_native.h"
#include <nanoHAL_Windows_Storage.h>
#include <nanoHAL_Persistent_Storage.h>
#include <Target_Windows_Storage.h>

#endif // WIN_STORAGE_NATIVE_TARGET_H