Skip to content

Commit da8c505

Browse files
committed
Merge branch 'main' of https://github.com/nanoframework/nf-interpreter into develop
2 parents fb2e915 + e9ba810 commit da8c505

8 files changed

Lines changed: 112 additions & 11 deletions

File tree

src/CLR/Core/Execution.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -640,9 +640,7 @@ HRESULT CLR_RT_ExecutionEngine::Execute(wchar_t *entryPointArgs, int maxContextS
640640

641641
NANOCLR_CHECK_HRESULT(WaitForDebugger());
642642

643-
#if defined(NANOCLR_ENABLE_SOURCELEVELDEBUGGING)
644643
CLR_EE_DBG_SET_MASK(StateProgramRunning, StateMask);
645-
#endif // #if defined(NANOCLR_ENABLE_SOURCELEVELDEBUGGING)
646644

647645
NANOCLR_CHECK_HRESULT(CLR_RT_HeapBlock_Delegate::CreateInstance(ref, g_CLR_RT_TypeSystem.m_entryPoint, nullptr));
648646

src/CLR/Startup/CLRStartup.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -425,8 +425,8 @@ void ClrStartup(CLR_SETTINGS params)
425425

426426
if (CLR_EE_DBG_IS_NOT(RebootPending))
427427
{
428-
#if defined(NANOCLR_ENABLE_SOURCELEVELDEBUGGING)
429428
CLR_EE_DBG_SET_MASK(StateProgramExited, StateMask);
429+
#if defined(NANOCLR_ENABLE_SOURCELEVELDEBUGGING)
430430
CLR_EE_DBG_EVENT_BROADCAST(CLR_DBG_Commands_c_Monitor_ProgramExit, 0, nullptr, WP_Flags_c_NonCritical);
431431
#endif // #if defined(NANOCLR_ENABLE_SOURCELEVELDEBUGGING)
432432

src/HAL/nanoHAL_StorageOperation.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ uint32_t HAL_StorageOperation(uint8_t operation, uint32_t dataLength, uint32_t o
5353
char dirPath[FS_MAX_DIRECTORY_LENGTH];
5454
char *lastSeparator;
5555
int bytesWritten = 0;
56+
HRESULT deleteResult;
5657

5758
// extract parent directory from relative path and create it if needed
5859
snprintf(dirPath, sizeof(dirPath), "%s", relativePath);
@@ -74,6 +75,17 @@ uint32_t HAL_StorageOperation(uint8_t operation, uint32_t dataLength, uint32_t o
7475
}
7576
}
7677

78+
// Open() below doesn't truncate, so remove an existing file to start the write from an empty one
79+
deleteResult = volume->Delete(relativePath, false);
80+
81+
// a missing file, or a volume without Delete, is the normal case; any other failure would leave
82+
// the previous content in place and the write would produce a file with a stale tail
83+
if (FAILED(deleteResult) && deleteResult != CLR_E_FILE_NOT_FOUND && deleteResult != CLR_E_NOT_SUPPORTED)
84+
{
85+
errorCode = StorageOperationErrorCode::WriteError;
86+
goto done;
87+
}
88+
7789
// open the file (creates it, if it doesn't exist)
7890
if (FAILED(volume->Open(relativePath, fileHandle)))
7991
{

targets/ChibiOS/_FatFs/fatfs_FS_Driver.cpp

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,13 +166,11 @@ HRESULT FATFS_FS_Driver::Format(const VOLUME_ID *volume, const char *volumeLabel
166166

167167
HRESULT FATFS_FS_Driver::GetSizeInfo(const VOLUME_ID *volume, int64_t *totalSize, int64_t *totalFreeSpace)
168168
{
169-
(void)totalSize;
170-
171169
// FATFS *fsPtr = &fs;
172170
// char buffer[3];
173171
// DWORD freeClusters, freeSectors, totalSectors;
174172

175-
// FATFS *fs = GetFatFsByVolumeId(volume, false);
173+
FATFS *fs = GetFatFsByVolumeId(volume, false);
176174

177175
FileSystemVolume *currentVolume = FileSystemVolumeList::FindVolume(volume->volumeId);
178176

@@ -194,9 +192,22 @@ HRESULT FATFS_FS_Driver::GetSizeInfo(const VOLUME_ID *volume, int64_t *totalSize
194192
// *totalFreeSpace = (int64_t)freeSectors * FF_MAX_SS;
195193
// #endif
196194

195+
// -1 means "unknown" to the caller
197196
*totalSize = -1;
197+
198+
// free space would need f_getfree(), see above
198199
*totalFreeSpace = -1;
199200

201+
if (fs != NULL)
202+
{
203+
// capacity of the file system, from the mounted FATFS object, without walking the FAT
204+
#if FF_MAX_SS != FF_MIN_SS
205+
*totalSize = (int64_t)(fs->n_fatent - 2) * fs->csize * fs->ssize;
206+
#else
207+
*totalSize = (int64_t)(fs->n_fatent - 2) * fs->csize * FF_MAX_SS;
208+
#endif
209+
}
210+
200211
return S_OK;
201212
}
202213

targets/ESP32/_common/Target_System_IO_FileSystem.c

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ static const char *TAG = "SDCard";
4545

4646
sdmmc_card_t *card;
4747

48+
// drive letter the card above is mounted under, 0 when it is not mounted
49+
char cardDriveLetter;
50+
4851
//
4952
// Unmount SD card ( MMC/SDIO or SPI)
5053
//
@@ -62,6 +65,7 @@ bool Storage_UnMountSDCard(int driveIndex)
6265
}
6366

6467
card = NULL;
68+
cardDriveLetter = 0;
6569

6670
return true;
6771
}
@@ -183,7 +187,15 @@ bool Storage_MountMMC(bool bit1Mode, int driveIndex)
183187
errCode = esp_vfs_fat_sdmmc_mount(mountPoint, &host, &slot_config, &mount_config, &card);
184188
}
185189

186-
return LogMountResult(errCode);
190+
if (!LogMountResult(errCode))
191+
{
192+
return false;
193+
}
194+
195+
// only stored on success, a failed mount leaves no card to bind the volume to
196+
cardDriveLetter = INDEX0_DRIVE_LETTER[0] + driveIndex;
197+
198+
return true;
187199
}
188200
#endif
189201

@@ -237,7 +249,15 @@ bool Storage_MountSpi(int spiBus, uint32_t csPin, int driveIndex)
237249
errCode = esp_vfs_fat_sdspi_mount(mountPoint, &host, &slot_config, &mount_config, &card);
238250
}
239251

240-
return LogMountResult(errCode);
252+
if (!LogMountResult(errCode))
253+
{
254+
return false;
255+
}
256+
257+
// only stored on success, a failed mount leaves no card to bind the volume to
258+
cardDriveLetter = INDEX0_DRIVE_LETTER[0] + driveIndex;
259+
260+
return true;
241261
}
242262

243263
#endif

targets/ESP32/_common/WireProtocol_HAL_Interface.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ static uint8_t WP_TransmitMessageTinyUsb(WP_Message *message)
161161

162162
#include "driver/usb_serial_jtag.h"
163163

164-
#define USB_JTAG_BUFFER_SIZE 256
164+
#define USB_JTAG_BUFFER_SIZE (sizeof(WP_Packet) + WP_PACKET_SIZE)
165165

166166
static size_t UsbSerialWrite(const uint8_t *data, size_t dataSize, TickType_t xTicksToWait);
167167
static size_t UsbSerialRead(uint8_t *data, size_t dataSize, TickType_t xTicksToWait);

targets/ESP32/_littlefs/littlefs_FS_Driver.cpp

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@
77
#include "littlefs_FS_Driver.h"
88
#include <stdlib.h>
99

10+
#if (HAL_USE_SDC == TRUE)
11+
#include <sdmmc_cmd.h>
12+
13+
// the mounted card and the volume it belongs to, from Target_System_IO_FileSystem.c
14+
extern "C" sdmmc_card_t *card;
15+
extern "C" char cardDriveLetter;
16+
#endif
17+
1018
extern FileSystemVolume *g_FS_Volumes;
1119

1220
static int32_t RemoveAllFiles(const char *path);
@@ -81,8 +89,6 @@ HRESULT LITTLEFS_FS_Driver::Format(const VOLUME_ID *volume, const char *volumeLa
8189

8290
HRESULT LITTLEFS_FS_Driver::GetSizeInfo(const VOLUME_ID *volume, int64_t *totalSize, int64_t *totalFreeSpace)
8391
{
84-
(void)totalSize;
85-
8692
// FATFS *fsPtr = &fs;
8793
// char buffer[3];
8894
// DWORD freeClusters, freeSectors, totalSectors;
@@ -109,9 +115,30 @@ HRESULT LITTLEFS_FS_Driver::GetSizeInfo(const VOLUME_ID *volume, int64_t *totalS
109115
// // *totalFreeSpace = (int64_t)freeSectors * FF_MAX_SS;
110116
// // #endif
111117

118+
// -1 means "unknown" to the caller
112119
*totalSize = -1;
120+
121+
// free space would need f_getfree(), which walks the FAT and trips the watchdog on large cards
113122
*totalFreeSpace = -1;
114123

124+
#if (HAL_USE_SDC == TRUE)
125+
126+
// the driver also serves the internal flash, which has no card behind it
127+
FileSystemVolume *currentVolume = FileSystemVolumeList::FindVolume(volume->volumeId);
128+
129+
if (currentVolume != NULL && card != NULL && cardDriveLetter != 0 &&
130+
currentVolume->m_rootName[0] == cardDriveLetter)
131+
{
132+
// physical capacity of the card, not of the file system on it
133+
*totalSize = (int64_t)card->csd.capacity * card->csd.sector_size;
134+
}
135+
136+
#else
137+
138+
(void)volume;
139+
140+
#endif
141+
115142
return S_OK;
116143
}
117144

@@ -282,6 +309,13 @@ HRESULT LITTLEFS_FS_Driver::Read(void *handle, uint8_t *buffer, int size, int *b
282309

283310
fileHandle = (LITTLEFS_FileHandle *)handle;
284311

312+
if (fileHandle->lastOp == LITTLEFS_LastOperation_Write && fseek(fileHandle->file, 0, SEEK_CUR) != 0)
313+
{
314+
NANOCLR_SET_AND_LEAVE(CLR_E_FILE_IO);
315+
}
316+
317+
fileHandle->lastOp = LITTLEFS_LastOperation_Read;
318+
285319
// read from the file
286320
readCount = fread(buffer, 1, size, fileHandle->file);
287321

@@ -324,6 +358,13 @@ HRESULT LITTLEFS_FS_Driver::Write(void *handle, uint8_t *buffer, int size, int *
324358

325359
fileHandle = (LITTLEFS_FileHandle *)handle;
326360

361+
if (fileHandle->lastOp == LITTLEFS_LastOperation_Read && fseek(fileHandle->file, 0, SEEK_CUR) != 0)
362+
{
363+
NANOCLR_SET_AND_LEAVE(CLR_E_FILE_IO);
364+
}
365+
366+
fileHandle->lastOp = LITTLEFS_LastOperation_Write;
367+
327368
// write to the file
328369
writeCount = fwrite(buffer, 1, size, fileHandle->file);
329370

@@ -397,6 +438,8 @@ HRESULT LITTLEFS_FS_Driver::Seek(void *handle, int64_t offset, uint32_t origin,
397438
return CLR_E_FILE_IO;
398439
}
399440

441+
fileHandle->lastOp = LITTLEFS_LastOperation_None;
442+
400443
// get the current position
401444
*position = ftell(fileHandle->file);
402445

@@ -445,6 +488,8 @@ HRESULT LITTLEFS_FS_Driver::GetLength(void *handle, int64_t *length)
445488
return CLR_E_FILE_IO;
446489
}
447490

491+
fileHandle->lastOp = LITTLEFS_LastOperation_None;
492+
448493
return S_OK;
449494
}
450495

@@ -489,6 +534,8 @@ HRESULT LITTLEFS_FS_Driver::SetLength(void *handle, int64_t length)
489534
return CLR_E_FILE_IO;
490535
}
491536

537+
fileHandle->lastOp = LITTLEFS_LastOperation_None;
538+
492539
// Synchronize the file state
493540
fflush(fileHandle->file);
494541

targets/ESP32/_littlefs/littlefs_FS_Driver.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,22 @@ extern "C"
1616
{
1717
#endif
1818

19+
// direction of the last operation on a file, files are opened in update mode and ANSI C
20+
// requires a positioning call between a read and a write on such a stream
21+
enum LITTLEFS_LastOperation
22+
{
23+
// no operation yet, or the file was just positioned: both directions are legal
24+
LITTLEFS_LastOperation_None = 0,
25+
// last operation was a read: a write needs a positioning call first
26+
LITTLEFS_LastOperation_Read = 1,
27+
// last operation was a write: a read needs a positioning call first
28+
LITTLEFS_LastOperation_Write = 2,
29+
};
30+
1931
struct LITTLEFS_FileHandle
2032
{
2133
FILE *file;
34+
LITTLEFS_LastOperation lastOp;
2235
};
2336

2437
struct LITTLEFS_FindFileHandle

0 commit comments

Comments
 (0)