-
Notifications
You must be signed in to change notification settings - Fork 2
/
littlefs_driver.c
74 lines (64 loc) · 2 KB
/
littlefs_driver.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*
* Driver for Raspberry Pi Pico on-board flash with littlefs file system
*
* Copyright 2024, Hiroyuki OYAMA. All rights reserved.
* SPDX-License-Identifier: BSD-3-Clause
*/
#include <hardware/flash.h>
#include <hardware/sync.h>
#include <hardware/regs/addressmap.h>
#include <lfs.h>
#define FS_SIZE (1.8 * 1024 * 1024)
static uint32_t fs_base(const struct lfs_config *c) {
uint32_t storage_size = c->block_count * c->block_size;
return PICO_FLASH_SIZE_BYTES - storage_size;
}
static int pico_read(const struct lfs_config* c,
lfs_block_t block,
lfs_off_t off,
void* buffer,
lfs_size_t size)
{
(void)c;
uint8_t* p = (uint8_t*)(XIP_NOCACHE_NOALLOC_BASE + fs_base(c) + (block * FLASH_SECTOR_SIZE) + off);
memcpy(buffer, p, size);
return 0;
}
static int pico_prog(const struct lfs_config* c,
lfs_block_t block,
lfs_off_t off,
const void* buffer,
lfs_size_t size)
{
(void)c;
uint32_t p = (block * FLASH_SECTOR_SIZE) + off;
uint32_t ints = save_and_disable_interrupts();
flash_range_program(fs_base(c) + p, buffer, size);
restore_interrupts(ints);
return 0;
}
static int pico_erase(const struct lfs_config* c, lfs_block_t block) {
(void)c;
uint32_t off = block * FLASH_SECTOR_SIZE;
uint32_t ints = save_and_disable_interrupts();
flash_range_erase(fs_base(c) + off, FLASH_SECTOR_SIZE);
restore_interrupts(ints);
return 0;
}
static int pico_sync(const struct lfs_config* c) {
(void)c;
return 0;
}
const struct lfs_config lfs_pico_flash_config = {
.read = pico_read,
.prog = pico_prog,
.erase = pico_erase,
.sync = pico_sync,
.read_size = 1,
.prog_size = FLASH_PAGE_SIZE,
.block_size = FLASH_SECTOR_SIZE,
.block_count = FS_SIZE / FLASH_SECTOR_SIZE,
.cache_size = FLASH_SECTOR_SIZE,
.lookahead_size = 16,
.block_cycles = 500,
};