Path: blob/master/libraries/AP_Filesystem/AP_Filesystem_FlashMemory_LittleFS.h
4232 views
/*1This program is free software: you can redistribute it and/or modify2it under the terms of the GNU General Public License as published by3the Free Software Foundation, either version 3 of the License, or4(at your option) any later version.56This program is distributed in the hope that it will be useful,7but WITHOUT ANY WARRANTY; without even the implied warranty of8MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the9GNU General Public License for more details.1011You should have received a copy of the GNU General Public License12along with this program. If not, see <http://www.gnu.org/licenses/>.13*/14#pragma once1516#include "AP_Filesystem_backend.h"1718#if AP_FILESYSTEM_LITTLEFS_ENABLED1920#include <AP_HAL/AP_HAL.h>21#include <AP_HAL/Semaphores.h>22#include "lfs.h"2324class AP_Filesystem_FlashMemory_LittleFS : public AP_Filesystem_Backend25{26public:27// functions that closely match the equivalent posix calls28int open(const char *fname, int flags, bool allow_absolute_paths = false) override;29int close(int fd) override;30int32_t read(int fd, void *buf, uint32_t count) override;31int32_t write(int fd, const void *buf, uint32_t count) override;32int fsync(int fd) override;33int32_t lseek(int fd, int32_t offset, int whence) override;34int stat(const char *pathname, struct stat *stbuf) override;3536int unlink(const char *pathname) override;37int mkdir(const char *pathname) override;3839void *opendir(const char *pathname) override;40struct dirent *readdir(void *dirp) override;41int closedir(void *dirp) override;4243uint32_t bytes_until_fsync(int fd) override;4445int64_t disk_free(const char *path) override;46int64_t disk_space(const char *path) override;4748// set modification time on a file49bool set_mtime(const char *filename, const uint32_t mtime_sec) override;5051bool retry_mount(void) override;52void unmount(void) override;53// format flash. This is async, monitor get_format_status for progress54bool format(void) override;55AP_Filesystem_Backend::FormatStatus get_format_status() const override;5657int _flashmem_read(lfs_block_t block, lfs_off_t off, void* buffer, lfs_size_t size);58int _flashmem_prog(lfs_block_t block, lfs_off_t off, const void* buffer, lfs_size_t size);59int _flashmem_erase(lfs_block_t block);60int _flashmem_sync();6162private:63// Semaphore to protect against concurrent accesses to fs64HAL_Semaphore fs_sem;6566// The filesystem object67lfs_t fs;6869// The configuration of the filesystem70struct lfs_config fs_cfg;7172// Maximum number of files that may be open at the same time73static constexpr int MAX_OPEN_FILES = 16;7475// Stores whether the filesystem is mounted76bool mounted;7778// Stores whether the filesystem has been marked as dead79bool dead;8081// Array of currently open file descriptors82struct FileDescriptor {83lfs_file_t file;84lfs_file_config cfg;85lfs_attr attrs[1];86uint32_t mtime;87char* filename;88};8990FileDescriptor* open_files[MAX_OPEN_FILES];9192// SPI device that handles the raw flash memory93AP_HAL::OwnPtr<AP_HAL::SPIDevice> dev;9495// Semaphore to protect access to the SPI device96AP_HAL::Semaphore *dev_sem;9798// Flag to denote that the underlying flash chip uses 32-bit addresses99bool use_32bit_address;100FormatStatus format_status;101102int allocate_fd();103int free_fd(int fd);104void free_all_fds();105FileDescriptor* lfs_file_from_fd(int fd) const;106107uint32_t find_block_size_and_count();108bool init_flash() WARN_IF_UNUSED;109bool write_enable() WARN_IF_UNUSED;110bool is_busy();111bool mount_filesystem();112void send_command_addr(uint8_t command, uint32_t addr);113void send_command_page(uint8_t command, uint32_t page);114bool wait_until_device_is_ready() WARN_IF_UNUSED;115void write_status_register(uint8_t reg, uint8_t bits);116void format_handler(void);117void mark_dead();118};119120#endif // #if AP_FILESYSTEM_LITTLEFS_ENABLED121122123