Created
May 28, 2015 16:02
-
-
Save benpicco/c42cc73b893af41876be to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "printf.h" | |
#include "flash.h" | |
#include "spiffs.h" | |
#define LOG_PAGE_SIZE 128 // can't read more than this in one go | |
static u8_t spiffs_work_buf[LOG_PAGE_SIZE*2]; | |
static u8_t spiffs_fds[32*4]; | |
static u8_t spiffs_cache_buf[(LOG_PAGE_SIZE+32)*4]; | |
static spiffs fs; | |
static s32_t my_spiffs_read(u32_t addr, u32_t size, u8_t *dst) { | |
// printf("read(%x, %d, %p)\n", addr, size, dst); | |
read_page(addr, dst, size); | |
return SPIFFS_OK; | |
} | |
static s32_t my_spiffs_write(u32_t addr, u32_t size, u8_t *src) { | |
// printf("write(%x, %d, %p)\n", addr, size, src); | |
write_page(addr, src, size); | |
return SPIFFS_OK; | |
} | |
static s32_t my_spiffs_erase(u32_t addr, u32_t size) { | |
// printf("erase(%x, %d)\n", addr, size); | |
erase_block(addr, size); | |
return SPIFFS_OK; | |
} | |
void my_spiffs_mount(void) { | |
spiffs_config cfg; | |
cfg.phys_size = FLASH_SIZE; // use all spi flash | |
cfg.phys_addr = 0; // start spiffs at start of spi flash | |
cfg.phys_erase_block = BLOCK_4K; // according to datasheet | |
cfg.log_block_size = BLOCK_4K; // let us not complicate things | |
cfg.log_page_size = LOG_PAGE_SIZE; // as we said | |
cfg.hal_read_f = my_spiffs_read; | |
cfg.hal_write_f = my_spiffs_write; | |
cfg.hal_erase_f = my_spiffs_erase; | |
int res = SPIFFS_mount(&fs, | |
&cfg, | |
spiffs_work_buf, | |
spiffs_fds, | |
sizeof(spiffs_fds), | |
spiffs_cache_buf, | |
sizeof(spiffs_cache_buf), | |
0); | |
printf("mount res: %i\n", res); | |
if (res < 0) { | |
printf("SPIFFS_format\n"); | |
SPIFFS_format(&fs); | |
my_spiffs_mount(); | |
} | |
} | |
static void test_spiffs(void) { | |
char buf[12]; | |
// Surely, I've mounted spiffs before entering here | |
spiffs_file fd = SPIFFS_open(&fs, "my_file", SPIFFS_CREAT | SPIFFS_TRUNC | SPIFFS_RDWR, 0); | |
if (SPIFFS_write(&fs, fd, (u8_t *)"Hello world", 12) < 0) printf("errno %i\n", SPIFFS_errno(&fs)); | |
SPIFFS_close(&fs, fd); | |
fd = SPIFFS_open(&fs, "my_file", SPIFFS_RDWR, 0); | |
if (SPIFFS_read(&fs, fd, (u8_t *)buf, 12) < 0) printf("errno %i\n", SPIFFS_errno(&fs)); | |
SPIFFS_close(&fs, fd); | |
printf("--> %s <--\n", buf); | |
} | |
int main(void) { | |
printf("erasing flash…\n"); | |
erase_block(0, FLASH_SIZE); | |
printf("mounting…\n"); | |
my_spiffs_mount(); | |
printf("testing…\n"); | |
test_spiffs(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment