diff --git a/Makefile b/Makefile index 0210e959..695e3557 100644 --- a/Makefile +++ b/Makefile @@ -29,6 +29,9 @@ endif INCLUDES += \ -I./ \ -I./tinyusb/src \ + -I./littlefs \ + -I./filesystem \ + -I./shell \ -I./watch-library/shared/watch \ -I./watch-library/hardware/watch \ -I./watch-faces/clock \ @@ -36,6 +39,11 @@ INCLUDES += \ # Add your source files here. SRCS += \ + ./littlefs/lfs.c \ + ./littlefs/lfs_util.c \ + ./filesystem/filesystem.c \ + ./shell/shell.c \ + ./shell/shell_cmd_list.c \ ./watch-library/shared/watch/watch_common_buzzer.c \ ./watch-library/shared/watch/watch_common_display.c \ ./watch-library/shared/watch/watch_utility.c \ diff --git a/movement/filesystem.c b/filesystem/filesystem.c similarity index 67% rename from movement/filesystem.c rename to filesystem/filesystem.c index 326db74c..e265a864 100644 --- a/movement/filesystem.c +++ b/filesystem/filesystem.c @@ -1,11 +1,37 @@ +/* + * MIT License + * + * Copyright (c) 2022-2024 Joey Castillo + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + #include #include #include -#include #include "filesystem.h" #include "watch.h" #include "lfs.h" -#include "hpl_flash.h" + +#ifndef min +#define min(x, y) ((x) > (y) ? (y) : (x)) +#endif int lfs_storage_read(const struct lfs_config *cfg, lfs_block_t block, lfs_off_t off, void *buffer, lfs_size_t size); int lfs_storage_prog(const struct lfs_config *cfg, lfs_block_t block, lfs_off_t off, const void *buffer, lfs_size_t size); @@ -32,7 +58,7 @@ int lfs_storage_sync(const struct lfs_config *cfg) { return !watch_storage_sync(); } -const struct lfs_config cfg = { +const struct lfs_config watch_lfs_cfg = { // block device operations .read = lfs_storage_read, .prog = lfs_storage_prog, @@ -49,7 +75,7 @@ const struct lfs_config cfg = { .block_cycles = 100, }; -static lfs_t lfs; +lfs_t eeprom_filesystem; static lfs_file_t file; static struct lfs_info info; @@ -64,12 +90,12 @@ int32_t filesystem_get_free_space(void) { int err; uint32_t free_blocks = 0; - err = lfs_fs_traverse(&lfs, _traverse_df_cb, &free_blocks); + err = lfs_fs_traverse(&eeprom_filesystem, _traverse_df_cb, &free_blocks); if(err < 0){ return err; } - uint32_t available = cfg.block_count * cfg.block_size - free_blocks * cfg.block_size; + uint32_t available = watch_lfs_cfg.block_count * watch_lfs_cfg.block_size - free_blocks * watch_lfs_cfg.block_size; return (int32_t)available; } @@ -112,15 +138,15 @@ static int filesystem_ls(lfs_t *lfs, const char *path) { } bool filesystem_init(void) { - int err = lfs_mount(&lfs, &cfg); + int err = lfs_mount(&eeprom_filesystem, &watch_lfs_cfg); // reformat if we can't mount the filesystem // this should only happen on the first boot - if (err < 0) { + if (1) { printf("Ignore that error! Formatting filesystem...\r\n"); - err = lfs_format(&lfs, &cfg); + err = lfs_format(&eeprom_filesystem, &watch_lfs_cfg); if (err < 0) return false; - err = lfs_mount(&lfs, &cfg); + err = lfs_mount(&eeprom_filesystem, &watch_lfs_cfg) == LFS_ERR_OK; printf("Filesystem mounted with %ld bytes free.\r\n", filesystem_get_free_space()); } @@ -129,15 +155,15 @@ bool filesystem_init(void) { int _filesystem_format(void); int _filesystem_format(void) { - int err = lfs_unmount(&lfs); + int err = lfs_unmount(&eeprom_filesystem); if (err < 0) { printf("Couldn't unmount - continuing to format, but you should reboot afterwards!\r\n"); } - err = lfs_format(&lfs, &cfg); + err = lfs_format(&eeprom_filesystem, &watch_lfs_cfg); if (err < 0) return err; - err = lfs_mount(&lfs, &cfg); + err = lfs_mount(&eeprom_filesystem, &watch_lfs_cfg); if (err < 0) return err; printf("Filesystem re-mounted with %ld bytes free.\r\n", filesystem_get_free_space()); return 0; @@ -145,15 +171,15 @@ int _filesystem_format(void) { bool filesystem_file_exists(char *filename) { info.type = 0; - lfs_stat(&lfs, filename, &info); + lfs_stat(&eeprom_filesystem, filename, &info); return info.type == LFS_TYPE_REG; } bool filesystem_rm(char *filename) { info.type = 0; - lfs_stat(&lfs, filename, &info); + lfs_stat(&eeprom_filesystem, filename, &info); if (filesystem_file_exists(filename)) { - return lfs_remove(&lfs, filename) == LFS_ERR_OK; + return lfs_remove(&eeprom_filesystem, filename) == LFS_ERR_OK; } else { printf("rm: %s: No such file\r\n", filename); return false; @@ -172,11 +198,11 @@ bool filesystem_read_file(char *filename, char *buf, int32_t length) { memset(buf, 0, length); int32_t file_size = filesystem_get_file_size(filename); if (file_size > 0) { - int err = lfs_file_open(&lfs, &file, filename, LFS_O_RDONLY); + int err = lfs_file_open(&eeprom_filesystem, &file, filename, LFS_O_RDONLY); if (err < 0) return false; - err = lfs_file_read(&lfs, &file, buf, min(length, file_size)); + err = lfs_file_read(&eeprom_filesystem, &file, buf, min(length, file_size)); if (err < 0) return false; - return lfs_file_close(&lfs, &file) == LFS_ERR_OK; + return lfs_file_close(&eeprom_filesystem, &file) == LFS_ERR_OK; } return false; @@ -186,11 +212,11 @@ bool filesystem_read_line(char *filename, char *buf, int32_t *offset, int32_t le memset(buf, 0, length + 1); int32_t file_size = filesystem_get_file_size(filename); if (file_size > 0) { - int err = lfs_file_open(&lfs, &file, filename, LFS_O_RDONLY); + int err = lfs_file_open(&eeprom_filesystem, &file, filename, LFS_O_RDONLY); if (err < 0) return false; - err = lfs_file_seek(&lfs, &file, *offset, LFS_SEEK_SET); + err = lfs_file_seek(&eeprom_filesystem, &file, *offset, LFS_SEEK_SET); if (err < 0) return false; - err = lfs_file_read(&lfs, &file, buf, min(length - 1, file_size - *offset)); + err = lfs_file_read(&eeprom_filesystem, &file, buf, min(length - 1, file_size - *offset)); if (err < 0) return false; for(int i = 0; i < length; i++) { (*offset)++; @@ -199,7 +225,7 @@ bool filesystem_read_line(char *filename, char *buf, int32_t *offset, int32_t le break; } } - return lfs_file_close(&lfs, &file) == LFS_ERR_OK; + return lfs_file_close(&eeprom_filesystem, &file) == LFS_ERR_OK; } return false; @@ -207,7 +233,7 @@ bool filesystem_read_line(char *filename, char *buf, int32_t *offset, int32_t le static void filesystem_cat(char *filename) { info.type = 0; - lfs_stat(&lfs, filename, &info); + lfs_stat(&eeprom_filesystem, filename, &info); if (filesystem_file_exists(filename)) { if (info.size > 0) { char *buf = malloc(info.size + 1); @@ -224,26 +250,26 @@ static void filesystem_cat(char *filename) { } bool filesystem_write_file(char *filename, char *text, int32_t length) { - int err = lfs_file_open(&lfs, &file, filename, LFS_O_RDWR | LFS_O_CREAT | LFS_O_TRUNC); + int err = lfs_file_open(&eeprom_filesystem, &file, filename, LFS_O_RDWR | LFS_O_CREAT | LFS_O_TRUNC); if (err < 0) return false; - err = lfs_file_write(&lfs, &file, text, length); + err = lfs_file_write(&eeprom_filesystem, &file, text, length); if (err < 0) return false; - return lfs_file_close(&lfs, &file) == LFS_ERR_OK; + return lfs_file_close(&eeprom_filesystem, &file) == LFS_ERR_OK; } bool filesystem_append_file(char *filename, char *text, int32_t length) { - int err = lfs_file_open(&lfs, &file, filename, LFS_O_WRONLY | LFS_O_CREAT | LFS_O_APPEND); + int err = lfs_file_open(&eeprom_filesystem, &file, filename, LFS_O_WRONLY | LFS_O_CREAT | LFS_O_APPEND); if (err < 0) return false; - err = lfs_file_write(&lfs, &file, text, length); + err = lfs_file_write(&eeprom_filesystem, &file, text, length); if (err < 0) return false; - return lfs_file_close(&lfs, &file) == LFS_ERR_OK; + return lfs_file_close(&eeprom_filesystem, &file) == LFS_ERR_OK; } int filesystem_cmd_ls(int argc, char *argv[]) { if (argc >= 2) { - filesystem_ls(&lfs, argv[1]); + filesystem_ls(&eeprom_filesystem, argv[1]); } else { - filesystem_ls(&lfs, "/"); + filesystem_ls(&eeprom_filesystem, "/"); } return 0; } diff --git a/movement/filesystem.h b/filesystem/filesystem.h similarity index 97% rename from movement/filesystem.h rename to filesystem/filesystem.h index 472f2812..a54650a0 100644 --- a/movement/filesystem.h +++ b/filesystem/filesystem.h @@ -1,7 +1,7 @@ /* * MIT License * - * Copyright (c) 2022 Joey Castillo + * Copyright (c) 2022-2024 Joey Castillo * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -22,8 +22,8 @@ * SOFTWARE. */ -#ifndef FILESYSTEM_H_ -#define FILESYSTEM_H_ +#pragma once + #include #include #include "watch.h" @@ -102,5 +102,3 @@ int filesystem_cmd_df(int argc, char *argv[]); int filesystem_cmd_rm(int argc, char *argv[]); int filesystem_cmd_format(int argc, char *argv[]); int filesystem_cmd_echo(int argc, char *argv[]); - -#endif // FILESYSTEM_H_ diff --git a/movement.c b/movement.c index 96e30c56..58af7932 100644 --- a/movement.c +++ b/movement.c @@ -36,10 +36,10 @@ #include "watch_usb_cdc.h" #include "watch_private.h" #include "movement.h" +#include "filesystem.h" +#include "shell.h" -/// FIXMME: #SecondMovement needs to bring back the following includes (and remove the default signal_tune) -// #include "filesystem.h" -// #include "shell.h" +/// FIXME: #SecondMovement needs to bring back the following include (and remove the default signal_tune) // #include "movement_custom_signal_tunes.h" int8_t signal_tune[] = { BUZZER_NOTE_C8, 5, @@ -429,8 +429,7 @@ void app_init(void) { movement_state.next_available_backup_register = 4; _movement_reset_inactivity_countdown(); -/// FIXME: #SecondMovement needs filesystem support - // filesystem_init(); + filesystem_init(); #if __EMSCRIPTEN__ int32_t time_zone_offset = EM_ASM_INT({ @@ -636,8 +635,7 @@ bool app_loop(void) { // if we are plugged into USB, handle the serial shell if (usb_is_enabled()) { - /// FIXME: #SecondMovement needs to bring back the shell - // shell_task(); + shell_task(); } event.subsecond = 0; diff --git a/movement/shell.c b/shell/shell.c similarity index 99% rename from movement/shell.c rename to shell/shell.c index 37820680..c028ed23 100644 --- a/movement/shell.c +++ b/shell/shell.c @@ -30,6 +30,7 @@ #include #include #include +#include #include #if __EMSCRIPTEN__ diff --git a/movement/shell.h b/shell/shell.h similarity index 100% rename from movement/shell.h rename to shell/shell.h diff --git a/movement/shell_cmd_list.c b/shell/shell_cmd_list.c similarity index 99% rename from movement/shell_cmd_list.c rename to shell/shell_cmd_list.c index e2d700b1..1e49539e 100644 --- a/movement/shell_cmd_list.c +++ b/shell/shell_cmd_list.c @@ -30,6 +30,7 @@ #include "filesystem.h" #include "watch.h" +#include "delay.h" static int help_cmd(int argc, char *argv[]); static int flash_cmd(int argc, char *argv[]); diff --git a/movement/shell_cmd_list.h b/shell/shell_cmd_list.h similarity index 100% rename from movement/shell_cmd_list.h rename to shell/shell_cmd_list.h