Skip to content

Commit 7a2ecbe

Browse files
phoddiemkellner
authored andcommitted
add spiffs partition, fix xs_stage partition length, support arbitrary reads using modSPIRead
1 parent 3f67bb7 commit 7a2ecbe

File tree

1 file changed

+32
-9
lines changed

1 file changed

+32
-9
lines changed

modules/files/flash/esp/flash.c

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,9 @@ typedef struct modFlashRecord *modFlash;
3939
void xs_flash(xsMachine *the)
4040
{
4141
modFlashRecord flash;
42+
int kind = xsmcTypeOf(xsArg(0));
4243

43-
if (xsStringType == xsmcTypeOf(xsArg(0))) {
44+
if ((xsStringType == kind) || (xsStringXType == kind)) {
4445
char *partition = xsmcToString(xsArg(0));
4546
if (0 == c_strcmp(partition, "xs")) {
4647
flash.partitionStart = (uintptr_t)kModulesStart - (uintptr_t)kFlashStart;
@@ -51,7 +52,14 @@ void xs_flash(xsMachine *the)
5152
extern uint8_t _XSMOD_end;
5253

5354
flash.partitionStart = (uintptr_t)&_XSMOD_start - (uintptr_t)kFlashStart;
54-
flash.partitionByteLength = _XSMOD_end - _XSMOD_end;
55+
flash.partitionByteLength = &_XSMOD_end - &_XSMOD_start; //@@
56+
}
57+
else if (0 == c_strcmp(partition, "spiffs")) {
58+
extern uint8_t _SPIFFS_start;
59+
extern uint8_t _SPIFFS_end;
60+
61+
flash.partitionStart = (uintptr_t)&_SPIFFS_start - (uintptr_t)kFlashStart;
62+
flash.partitionByteLength = &_SPIFFS_end - &_SPIFFS_start;
5563
}
5664
else
5765
xsUnknownError("unknown partition");
@@ -88,23 +96,37 @@ void xs_flash_erase(xsMachine *the)
8896
void xs_flash_read(xsMachine *the)
8997
{
9098
modFlash flash = xsmcGetHostChunk(xsThis);
91-
SpiFlashOpResult result;
9299
int offset = xsmcToInteger(xsArg(0));
93100
int byteLength = xsmcToInteger(xsArg(1));
94-
void *buffer;
101+
uint8_t *buffer;
95102

96103
if ((offset < 0) || (offset >= flash->partitionByteLength))
97104
xsUnknownError("invalid offset");
98105

99106
xsResult = xsArrayBuffer(NULL, byteLength);
100107
buffer = xsmcToArrayBuffer(xsResult);
101108

102-
ets_isr_mask(FLASH_INT_MASK);
103-
result = spi_flash_read(offset + flash->partitionStart, buffer, byteLength);
104-
ets_isr_unmask(FLASH_INT_MASK);
109+
offset += flash->partitionStart;
110+
while (byteLength) {
111+
int use = byteLength;
105112

106-
if (SPI_FLASH_RESULT_OK != result)
107-
modLog("read fail");
113+
if (offset & 0x1FF) {
114+
use = 512 - (offset & 0x1FF);
115+
if (use > byteLength)
116+
use = byteLength;
117+
}
118+
else if (use >= 512)
119+
use = 512;
120+
121+
if (0 == modSPIRead(offset, use, buffer)) {
122+
modLog("read fail");
123+
break;
124+
}
125+
126+
offset += use;
127+
buffer += use;
128+
byteLength -= use;
129+
}
108130
}
109131

110132
void xs_flash_write(xsMachine *the)
@@ -120,6 +142,7 @@ void xs_flash_write(xsMachine *the)
120142

121143
buffer = xsmcToArrayBuffer(xsArg(2));
122144

145+
//@@ modSPIWrite
123146
ets_isr_mask(FLASH_INT_MASK);
124147
result = spi_flash_write(offset + flash->partitionStart, buffer, byteLength);
125148
ets_isr_unmask(FLASH_INT_MASK);

0 commit comments

Comments
 (0)