Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Ardupilot
GitHub Repository: Ardupilot/ardupilot
Path: blob/master/libraries/AP_Filesystem/AP_Filesystem_FlashMemory_LittleFS.h
4232 views
1
/*
2
This program is free software: you can redistribute it and/or modify
3
it under the terms of the GNU General Public License as published by
4
the Free Software Foundation, either version 3 of the License, or
5
(at your option) any later version.
6
7
This program is distributed in the hope that it will be useful,
8
but WITHOUT ANY WARRANTY; without even the implied warranty of
9
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
GNU General Public License for more details.
11
12
You should have received a copy of the GNU General Public License
13
along with this program. If not, see <http://www.gnu.org/licenses/>.
14
*/
15
#pragma once
16
17
#include "AP_Filesystem_backend.h"
18
19
#if AP_FILESYSTEM_LITTLEFS_ENABLED
20
21
#include <AP_HAL/AP_HAL.h>
22
#include <AP_HAL/Semaphores.h>
23
#include "lfs.h"
24
25
class AP_Filesystem_FlashMemory_LittleFS : public AP_Filesystem_Backend
26
{
27
public:
28
// functions that closely match the equivalent posix calls
29
int open(const char *fname, int flags, bool allow_absolute_paths = false) override;
30
int close(int fd) override;
31
int32_t read(int fd, void *buf, uint32_t count) override;
32
int32_t write(int fd, const void *buf, uint32_t count) override;
33
int fsync(int fd) override;
34
int32_t lseek(int fd, int32_t offset, int whence) override;
35
int stat(const char *pathname, struct stat *stbuf) override;
36
37
int unlink(const char *pathname) override;
38
int mkdir(const char *pathname) override;
39
40
void *opendir(const char *pathname) override;
41
struct dirent *readdir(void *dirp) override;
42
int closedir(void *dirp) override;
43
44
uint32_t bytes_until_fsync(int fd) override;
45
46
int64_t disk_free(const char *path) override;
47
int64_t disk_space(const char *path) override;
48
49
// set modification time on a file
50
bool set_mtime(const char *filename, const uint32_t mtime_sec) override;
51
52
bool retry_mount(void) override;
53
void unmount(void) override;
54
// format flash. This is async, monitor get_format_status for progress
55
bool format(void) override;
56
AP_Filesystem_Backend::FormatStatus get_format_status() const override;
57
58
int _flashmem_read(lfs_block_t block, lfs_off_t off, void* buffer, lfs_size_t size);
59
int _flashmem_prog(lfs_block_t block, lfs_off_t off, const void* buffer, lfs_size_t size);
60
int _flashmem_erase(lfs_block_t block);
61
int _flashmem_sync();
62
63
private:
64
// Semaphore to protect against concurrent accesses to fs
65
HAL_Semaphore fs_sem;
66
67
// The filesystem object
68
lfs_t fs;
69
70
// The configuration of the filesystem
71
struct lfs_config fs_cfg;
72
73
// Maximum number of files that may be open at the same time
74
static constexpr int MAX_OPEN_FILES = 16;
75
76
// Stores whether the filesystem is mounted
77
bool mounted;
78
79
// Stores whether the filesystem has been marked as dead
80
bool dead;
81
82
// Array of currently open file descriptors
83
struct FileDescriptor {
84
lfs_file_t file;
85
lfs_file_config cfg;
86
lfs_attr attrs[1];
87
uint32_t mtime;
88
char* filename;
89
};
90
91
FileDescriptor* open_files[MAX_OPEN_FILES];
92
93
// SPI device that handles the raw flash memory
94
AP_HAL::OwnPtr<AP_HAL::SPIDevice> dev;
95
96
// Semaphore to protect access to the SPI device
97
AP_HAL::Semaphore *dev_sem;
98
99
// Flag to denote that the underlying flash chip uses 32-bit addresses
100
bool use_32bit_address;
101
FormatStatus format_status;
102
103
int allocate_fd();
104
int free_fd(int fd);
105
void free_all_fds();
106
FileDescriptor* lfs_file_from_fd(int fd) const;
107
108
uint32_t find_block_size_and_count();
109
bool init_flash() WARN_IF_UNUSED;
110
bool write_enable() WARN_IF_UNUSED;
111
bool is_busy();
112
bool mount_filesystem();
113
void send_command_addr(uint8_t command, uint32_t addr);
114
void send_command_page(uint8_t command, uint32_t page);
115
bool wait_until_device_is_ready() WARN_IF_UNUSED;
116
void write_status_register(uint8_t reg, uint8_t bits);
117
void format_handler(void);
118
void mark_dead();
119
};
120
121
#endif // #if AP_FILESYSTEM_LITTLEFS_ENABLED
122
123