23
23
#include < algorithm>
24
24
#include " spiffs/spiffs.h"
25
25
#include " debug.h"
26
- #include " interrupts.h"
27
26
28
27
extern " C" {
29
28
#include " c_types.h"
30
29
#include " spi_flash.h"
31
30
}
32
-
33
- static int spi_flash_read_locked (uint32_t addr, uint32_t * dst, uint32_t size) {
34
- optimistic_yield (10000 );
35
- AutoInterruptLock (5 );
36
- return spi_flash_read (addr, dst, size);
37
- }
38
-
39
- static int spi_flash_write_locked (uint32_t addr, const uint32_t * src, uint32_t size) {
40
- optimistic_yield (10000 );
41
- AutoInterruptLock (5 );
42
- return spi_flash_write (addr, (uint32_t *) src, size);
43
- }
44
-
45
- static int spi_flash_erase_sector_locked (uint32_t sector) {
46
- optimistic_yield (10000 );
47
- AutoInterruptLock (5 );
48
- return spi_flash_erase_sector (sector);
49
- }
50
-
51
-
52
31
/*
53
32
spi_flash_read function requires flash address to be aligned on word boundary.
54
33
We take care of this by reading first and last words separately and memcpy
@@ -64,14 +43,16 @@ alignedEnd: ^
64
43
*/
65
44
66
45
int32_t spiffs_hal_read (uint32_t addr, uint32_t size, uint8_t *dst) {
46
+ optimistic_yield (10000 );
47
+
67
48
uint32_t result = SPIFFS_OK;
68
49
uint32_t alignedBegin = (addr + 3 ) & (~3 );
69
50
uint32_t alignedEnd = (addr + size) & (~3 );
70
51
71
52
if (addr < alignedBegin) {
72
53
uint32_t nb = alignedBegin - addr;
73
54
uint32_t tmp;
74
- if (spi_flash_read_locked (alignedBegin - 4 , &tmp, 4 ) != SPI_FLASH_RESULT_OK ) {
55
+ if (!ESP. flashRead (alignedBegin - 4 , &tmp, 4 )) {
75
56
DEBUGV (" _spif_read(%d) addr=%x size=%x ab=%x ae=%x\r\n " ,
76
57
__LINE__, addr, size, alignedBegin, alignedEnd);
77
58
return SPIFFS_ERR_INTERNAL;
@@ -80,8 +61,8 @@ int32_t spiffs_hal_read(uint32_t addr, uint32_t size, uint8_t *dst) {
80
61
}
81
62
82
63
if (alignedEnd != alignedBegin) {
83
- if (spi_flash_read_locked (alignedBegin, (uint32_t *) (dst + alignedBegin - addr),
84
- alignedEnd - alignedBegin) != SPI_FLASH_RESULT_OK ) {
64
+ if (!ESP. flashRead (alignedBegin, (uint32_t *) (dst + alignedBegin - addr),
65
+ alignedEnd - alignedBegin)) {
85
66
DEBUGV (" _spif_read(%d) addr=%x size=%x ab=%x ae=%x\r\n " ,
86
67
__LINE__, addr, size, alignedBegin, alignedEnd);
87
68
return SPIFFS_ERR_INTERNAL;
@@ -91,7 +72,7 @@ int32_t spiffs_hal_read(uint32_t addr, uint32_t size, uint8_t *dst) {
91
72
if (addr + size > alignedEnd) {
92
73
uint32_t nb = addr + size - alignedEnd;
93
74
uint32_t tmp;
94
- if (spi_flash_read_locked (alignedEnd, &tmp, 4 ) != SPI_FLASH_RESULT_OK ) {
75
+ if (!ESP. flashRead (alignedEnd, &tmp, 4 )) {
95
76
DEBUGV (" _spif_read(%d) addr=%x size=%x ab=%x ae=%x\r\n " ,
96
77
__LINE__, addr, size, alignedBegin, alignedEnd);
97
78
return SPIFFS_ERR_INTERNAL;
@@ -116,14 +97,16 @@ int32_t spiffs_hal_read(uint32_t addr, uint32_t size, uint8_t *dst) {
116
97
static const int UNALIGNED_WRITE_BUFFER_SIZE = 512 ;
117
98
118
99
int32_t spiffs_hal_write (uint32_t addr, uint32_t size, uint8_t *src) {
100
+ optimistic_yield (10000 );
101
+
119
102
uint32_t alignedBegin = (addr + 3 ) & (~3 );
120
103
uint32_t alignedEnd = (addr + size) & (~3 );
121
104
122
105
if (addr < alignedBegin) {
123
106
uint32_t nb = alignedBegin - addr;
124
107
uint32_t tmp = 0xffffffff ;
125
108
memcpy (((uint8_t * )&tmp) + 4 - nb, src, nb);
126
- if (spi_flash_write_locked (alignedBegin - 4 , &tmp, 4 ) != SPI_FLASH_RESULT_OK ) {
109
+ if (!ESP. flashWrite (alignedBegin - 4 , &tmp, 4 )) {
127
110
DEBUGV (" _spif_write(%d) addr=%x size=%x ab=%x ae=%x\r\n " ,
128
111
__LINE__, addr, size, alignedBegin, alignedEnd);
129
112
return SPIFFS_ERR_INTERNAL;
@@ -134,8 +117,8 @@ int32_t spiffs_hal_write(uint32_t addr, uint32_t size, uint8_t *src) {
134
117
uint32_t * srcLeftover = (uint32_t *) (src + alignedBegin - addr);
135
118
uint32_t srcAlign = ((uint32_t ) srcLeftover) & 3 ;
136
119
if (!srcAlign) {
137
- if (spi_flash_write_locked (alignedBegin, (uint32_t *) srcLeftover,
138
- alignedEnd - alignedBegin) != SPI_FLASH_RESULT_OK ) {
120
+ if (!ESP. flashWrite (alignedBegin, (uint32_t *) srcLeftover,
121
+ alignedEnd - alignedBegin)) {
139
122
DEBUGV (" _spif_write(%d) addr=%x size=%x ab=%x ae=%x\r\n " ,
140
123
__LINE__, addr, size, alignedBegin, alignedEnd);
141
124
return SPIFFS_ERR_INTERNAL;
@@ -147,8 +130,7 @@ int32_t spiffs_hal_write(uint32_t addr, uint32_t size, uint8_t *src) {
147
130
size_t willCopy = std::min (sizeLeft, sizeof (buf));
148
131
memcpy (buf, srcLeftover, willCopy);
149
132
150
- if (spi_flash_write_locked (alignedBegin, (uint32_t *) buf,
151
- willCopy) != SPI_FLASH_RESULT_OK) {
133
+ if (!ESP.flashWrite (alignedBegin, (uint32_t *) buf, willCopy)) {
152
134
DEBUGV (" _spif_write(%d) addr=%x size=%x ab=%x ae=%x\r\n " ,
153
135
__LINE__, addr, size, alignedBegin, alignedEnd);
154
136
return SPIFFS_ERR_INTERNAL;
@@ -166,7 +148,7 @@ int32_t spiffs_hal_write(uint32_t addr, uint32_t size, uint8_t *src) {
166
148
uint32_t tmp = 0xffffffff ;
167
149
memcpy (&tmp, src + size - nb, nb);
168
150
169
- if (spi_flash_write_locked (alignedEnd, &tmp, 4 ) != SPI_FLASH_RESULT_OK ) {
151
+ if (!ESP. flashWrite (alignedEnd, &tmp, 4 )) {
170
152
DEBUGV (" _spif_write(%d) addr=%x size=%x ab=%x ae=%x\r\n " ,
171
153
__LINE__, addr, size, alignedBegin, alignedEnd);
172
154
return SPIFFS_ERR_INTERNAL;
@@ -185,7 +167,8 @@ int32_t spiffs_hal_erase(uint32_t addr, uint32_t size) {
185
167
const uint32_t sector = addr / SPI_FLASH_SEC_SIZE;
186
168
const uint32_t sectorCount = size / SPI_FLASH_SEC_SIZE;
187
169
for (uint32_t i = 0 ; i < sectorCount; ++i) {
188
- if (spi_flash_erase_sector_locked (sector + i) != 0 ) {
170
+ optimistic_yield (10000 );
171
+ if (!ESP.flashEraseSector (sector + i)) {
189
172
DEBUGV (" _spif_erase addr=%x size=%d i=%d\r\n " , addr, size, i);
190
173
return SPIFFS_ERR_INTERNAL;
191
174
}
0 commit comments