Skip to content

Commit b78ca32

Browse files
committed
drivers/memory/spiflash: Add functions for direct erase/read/write.
These new API functions do not use the cache.
1 parent cc5a940 commit b78ca32

File tree

2 files changed

+55
-8
lines changed

2 files changed

+55
-8
lines changed

drivers/memory/spiflash.c

Lines changed: 49 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ void mp_spiflash_init(mp_spiflash_t *self) {
187187
mp_spiflash_release_bus(self);
188188
}
189189

190-
STATIC int mp_spiflash_erase_sector(mp_spiflash_t *self, uint32_t addr) {
190+
STATIC int mp_spiflash_erase_block_internal(mp_spiflash_t *self, uint32_t addr) {
191191
// enable writes
192192
mp_spiflash_write_cmd(self, CMD_WREN);
193193

@@ -204,7 +204,7 @@ STATIC int mp_spiflash_erase_sector(mp_spiflash_t *self, uint32_t addr) {
204204
return mp_spiflash_wait_wip0(self);
205205
}
206206

207-
STATIC int mp_spiflash_write_page(mp_spiflash_t *self, uint32_t addr, const uint8_t *src) {
207+
STATIC int mp_spiflash_write_page(mp_spiflash_t *self, uint32_t addr, size_t len, const uint8_t *src) {
208208
// enable writes
209209
mp_spiflash_write_cmd(self, CMD_WREN);
210210

@@ -215,12 +215,53 @@ STATIC int mp_spiflash_write_page(mp_spiflash_t *self, uint32_t addr, const uint
215215
}
216216

217217
// write the page
218-
mp_spiflash_write_cmd_addr_data(self, CMD_WRITE, addr, PAGE_SIZE, src);
218+
mp_spiflash_write_cmd_addr_data(self, CMD_WRITE, addr, len, src);
219219

220220
// wait WIP=0
221221
return mp_spiflash_wait_wip0(self);
222222
}
223223

224+
/******************************************************************************/
225+
// Interface functions that go direct to the SPI flash device
226+
227+
int mp_spiflash_erase_block(mp_spiflash_t *self, uint32_t addr) {
228+
mp_spiflash_acquire_bus(self);
229+
int ret = mp_spiflash_erase_block_internal(self, addr);
230+
mp_spiflash_release_bus(self);
231+
return ret;
232+
}
233+
234+
void mp_spiflash_read(mp_spiflash_t *self, uint32_t addr, size_t len, uint8_t *dest) {
235+
if (len == 0) {
236+
return;
237+
}
238+
mp_spiflash_acquire_bus(self);
239+
mp_spiflash_read_data(self, addr, len, dest);
240+
mp_spiflash_release_bus(self);
241+
}
242+
243+
int mp_spiflash_write(mp_spiflash_t *self, uint32_t addr, size_t len, const uint8_t *src) {
244+
mp_spiflash_acquire_bus(self);
245+
int ret = 0;
246+
uint32_t offset = addr & (PAGE_SIZE - 1);
247+
while (len) {
248+
size_t rest = PAGE_SIZE - offset;
249+
if (rest > len) {
250+
rest = len;
251+
}
252+
ret = mp_spiflash_write_page(self, addr, rest, src);
253+
if (ret != 0) {
254+
break;
255+
}
256+
len -= rest;
257+
addr += rest;
258+
src += rest;
259+
offset = 0;
260+
}
261+
mp_spiflash_release_bus(self);
262+
return ret;
263+
}
264+
224265
/******************************************************************************/
225266
// Interface functions that use the cache
226267

@@ -275,14 +316,15 @@ STATIC void mp_spiflash_cache_flush_internal(mp_spiflash_t *self) {
275316
mp_spiflash_cache_t *cache = self->config->cache;
276317

277318
// Erase sector
278-
int ret = mp_spiflash_erase_sector(self, cache->block * SECTOR_SIZE);
319+
int ret = mp_spiflash_erase_block_internal(self, cache->block * SECTOR_SIZE);
279320
if (ret != 0) {
280321
return;
281322
}
282323

283324
// Write
284325
for (int i = 0; i < 16; i += 1) {
285-
int ret = mp_spiflash_write_page(self, cache->block * SECTOR_SIZE + i * PAGE_SIZE, cache->buf + i * PAGE_SIZE);
326+
uint32_t addr = cache->block * SECTOR_SIZE + i * PAGE_SIZE;
327+
int ret = mp_spiflash_write_page(self, addr, PAGE_SIZE, cache->buf + i * PAGE_SIZE);
286328
if (ret != 0) {
287329
return;
288330
}
@@ -344,7 +386,7 @@ STATIC int mp_spiflash_cached_write_part(mp_spiflash_t *self, uint32_t addr, siz
344386
if (cache->buf[offset + i] != src[i]) {
345387
if (cache->buf[offset + i] != 0xff) {
346388
// Erase sector
347-
int ret = mp_spiflash_erase_sector(self, addr);
389+
int ret = mp_spiflash_erase_block_internal(self, addr);
348390
if (ret != 0) {
349391
return ret;
350392
}
@@ -363,7 +405,7 @@ STATIC int mp_spiflash_cached_write_part(mp_spiflash_t *self, uint32_t addr, siz
363405
// Write sector in pages of 256 bytes
364406
for (size_t i = 0; i < 16; ++i) {
365407
if (dirty & (1 << i)) {
366-
int ret = mp_spiflash_write_page(self, addr + i * PAGE_SIZE, cache->buf + i * PAGE_SIZE);
408+
int ret = mp_spiflash_write_page(self, addr + i * PAGE_SIZE, PAGE_SIZE, cache->buf + i * PAGE_SIZE);
367409
if (ret != 0) {
368410
return ret;
369411
}

drivers/memory/spiflash.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ typedef struct _mp_spiflash_config_t {
5959
const mp_qspi_proto_t *proto;
6060
} u_qspi;
6161
} bus;
62-
mp_spiflash_cache_t *cache;
62+
mp_spiflash_cache_t *cache; // can be NULL if cache functions not used
6363
} mp_spiflash_config_t;
6464

6565
typedef struct _mp_spiflash_t {
@@ -69,6 +69,11 @@ typedef struct _mp_spiflash_t {
6969

7070
void mp_spiflash_init(mp_spiflash_t *self);
7171

72+
// These functions go direct to the SPI flash device
73+
int mp_spiflash_erase_block(mp_spiflash_t *self, uint32_t addr);
74+
void mp_spiflash_read(mp_spiflash_t *self, uint32_t addr, size_t len, uint8_t *dest);
75+
int mp_spiflash_write(mp_spiflash_t *self, uint32_t addr, size_t len, const uint8_t *src);
76+
7277
// These functions use the cache (which must already be configured)
7378
void mp_spiflash_cache_flush(mp_spiflash_t *self);
7479
void mp_spiflash_cached_read(mp_spiflash_t *self, uint32_t addr, size_t len, uint8_t *dest);

0 commit comments

Comments
 (0)