bring in filesysten and shell, rename LFS globals for clarity
This commit is contained in:
parent
e1fa85faf7
commit
e29134bf9f
8
Makefile
8
Makefile
@ -29,6 +29,9 @@ endif
|
|||||||
INCLUDES += \
|
INCLUDES += \
|
||||||
-I./ \
|
-I./ \
|
||||||
-I./tinyusb/src \
|
-I./tinyusb/src \
|
||||||
|
-I./littlefs \
|
||||||
|
-I./filesystem \
|
||||||
|
-I./shell \
|
||||||
-I./watch-library/shared/watch \
|
-I./watch-library/shared/watch \
|
||||||
-I./watch-library/hardware/watch \
|
-I./watch-library/hardware/watch \
|
||||||
-I./watch-faces/clock \
|
-I./watch-faces/clock \
|
||||||
@ -36,6 +39,11 @@ INCLUDES += \
|
|||||||
|
|
||||||
# Add your source files here.
|
# Add your source files here.
|
||||||
SRCS += \
|
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_buzzer.c \
|
||||||
./watch-library/shared/watch/watch_common_display.c \
|
./watch-library/shared/watch/watch_common_display.c \
|
||||||
./watch-library/shared/watch/watch_utility.c \
|
./watch-library/shared/watch/watch_utility.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 <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <peripheral_clk_config.h>
|
|
||||||
#include "filesystem.h"
|
#include "filesystem.h"
|
||||||
#include "watch.h"
|
#include "watch.h"
|
||||||
#include "lfs.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_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);
|
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();
|
return !watch_storage_sync();
|
||||||
}
|
}
|
||||||
|
|
||||||
const struct lfs_config cfg = {
|
const struct lfs_config watch_lfs_cfg = {
|
||||||
// block device operations
|
// block device operations
|
||||||
.read = lfs_storage_read,
|
.read = lfs_storage_read,
|
||||||
.prog = lfs_storage_prog,
|
.prog = lfs_storage_prog,
|
||||||
@ -49,7 +75,7 @@ const struct lfs_config cfg = {
|
|||||||
.block_cycles = 100,
|
.block_cycles = 100,
|
||||||
};
|
};
|
||||||
|
|
||||||
static lfs_t lfs;
|
lfs_t eeprom_filesystem;
|
||||||
static lfs_file_t file;
|
static lfs_file_t file;
|
||||||
static struct lfs_info info;
|
static struct lfs_info info;
|
||||||
|
|
||||||
@ -64,12 +90,12 @@ int32_t filesystem_get_free_space(void) {
|
|||||||
int err;
|
int err;
|
||||||
|
|
||||||
uint32_t free_blocks = 0;
|
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){
|
if(err < 0){
|
||||||
return err;
|
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;
|
return (int32_t)available;
|
||||||
}
|
}
|
||||||
@ -112,15 +138,15 @@ static int filesystem_ls(lfs_t *lfs, const char *path) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool filesystem_init(void) {
|
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
|
// reformat if we can't mount the filesystem
|
||||||
// this should only happen on the first boot
|
// this should only happen on the first boot
|
||||||
if (err < 0) {
|
if (1) {
|
||||||
printf("Ignore that error! Formatting filesystem...\r\n");
|
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;
|
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());
|
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 _filesystem_format(void) {
|
int _filesystem_format(void) {
|
||||||
int err = lfs_unmount(&lfs);
|
int err = lfs_unmount(&eeprom_filesystem);
|
||||||
if (err < 0) {
|
if (err < 0) {
|
||||||
printf("Couldn't unmount - continuing to format, but you should reboot afterwards!\r\n");
|
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;
|
if (err < 0) return err;
|
||||||
|
|
||||||
err = lfs_mount(&lfs, &cfg);
|
err = lfs_mount(&eeprom_filesystem, &watch_lfs_cfg);
|
||||||
if (err < 0) return err;
|
if (err < 0) return err;
|
||||||
printf("Filesystem re-mounted with %ld bytes free.\r\n", filesystem_get_free_space());
|
printf("Filesystem re-mounted with %ld bytes free.\r\n", filesystem_get_free_space());
|
||||||
return 0;
|
return 0;
|
||||||
@ -145,15 +171,15 @@ int _filesystem_format(void) {
|
|||||||
|
|
||||||
bool filesystem_file_exists(char *filename) {
|
bool filesystem_file_exists(char *filename) {
|
||||||
info.type = 0;
|
info.type = 0;
|
||||||
lfs_stat(&lfs, filename, &info);
|
lfs_stat(&eeprom_filesystem, filename, &info);
|
||||||
return info.type == LFS_TYPE_REG;
|
return info.type == LFS_TYPE_REG;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool filesystem_rm(char *filename) {
|
bool filesystem_rm(char *filename) {
|
||||||
info.type = 0;
|
info.type = 0;
|
||||||
lfs_stat(&lfs, filename, &info);
|
lfs_stat(&eeprom_filesystem, filename, &info);
|
||||||
if (filesystem_file_exists(filename)) {
|
if (filesystem_file_exists(filename)) {
|
||||||
return lfs_remove(&lfs, filename) == LFS_ERR_OK;
|
return lfs_remove(&eeprom_filesystem, filename) == LFS_ERR_OK;
|
||||||
} else {
|
} else {
|
||||||
printf("rm: %s: No such file\r\n", filename);
|
printf("rm: %s: No such file\r\n", filename);
|
||||||
return false;
|
return false;
|
||||||
@ -172,11 +198,11 @@ bool filesystem_read_file(char *filename, char *buf, int32_t length) {
|
|||||||
memset(buf, 0, length);
|
memset(buf, 0, length);
|
||||||
int32_t file_size = filesystem_get_file_size(filename);
|
int32_t file_size = filesystem_get_file_size(filename);
|
||||||
if (file_size > 0) {
|
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;
|
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;
|
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;
|
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);
|
memset(buf, 0, length + 1);
|
||||||
int32_t file_size = filesystem_get_file_size(filename);
|
int32_t file_size = filesystem_get_file_size(filename);
|
||||||
if (file_size > 0) {
|
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;
|
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;
|
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;
|
if (err < 0) return false;
|
||||||
for(int i = 0; i < length; i++) {
|
for(int i = 0; i < length; i++) {
|
||||||
(*offset)++;
|
(*offset)++;
|
||||||
@ -199,7 +225,7 @@ bool filesystem_read_line(char *filename, char *buf, int32_t *offset, int32_t le
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return lfs_file_close(&lfs, &file) == LFS_ERR_OK;
|
return lfs_file_close(&eeprom_filesystem, &file) == LFS_ERR_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
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) {
|
static void filesystem_cat(char *filename) {
|
||||||
info.type = 0;
|
info.type = 0;
|
||||||
lfs_stat(&lfs, filename, &info);
|
lfs_stat(&eeprom_filesystem, filename, &info);
|
||||||
if (filesystem_file_exists(filename)) {
|
if (filesystem_file_exists(filename)) {
|
||||||
if (info.size > 0) {
|
if (info.size > 0) {
|
||||||
char *buf = malloc(info.size + 1);
|
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) {
|
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;
|
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;
|
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) {
|
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;
|
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;
|
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[]) {
|
int filesystem_cmd_ls(int argc, char *argv[]) {
|
||||||
if (argc >= 2) {
|
if (argc >= 2) {
|
||||||
filesystem_ls(&lfs, argv[1]);
|
filesystem_ls(&eeprom_filesystem, argv[1]);
|
||||||
} else {
|
} else {
|
||||||
filesystem_ls(&lfs, "/");
|
filesystem_ls(&eeprom_filesystem, "/");
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@ -1,7 +1,7 @@
|
|||||||
/*
|
/*
|
||||||
* MIT License
|
* 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
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
@ -22,8 +22,8 @@
|
|||||||
* SOFTWARE.
|
* SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifndef FILESYSTEM_H_
|
#pragma once
|
||||||
#define FILESYSTEM_H_
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include "watch.h"
|
#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_rm(int argc, char *argv[]);
|
||||||
int filesystem_cmd_format(int argc, char *argv[]);
|
int filesystem_cmd_format(int argc, char *argv[]);
|
||||||
int filesystem_cmd_echo(int argc, char *argv[]);
|
int filesystem_cmd_echo(int argc, char *argv[]);
|
||||||
|
|
||||||
#endif // FILESYSTEM_H_
|
|
||||||
12
movement.c
12
movement.c
@ -36,10 +36,10 @@
|
|||||||
#include "watch_usb_cdc.h"
|
#include "watch_usb_cdc.h"
|
||||||
#include "watch_private.h"
|
#include "watch_private.h"
|
||||||
#include "movement.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)
|
/// FIXME: #SecondMovement needs to bring back the following include (and remove the default signal_tune)
|
||||||
// #include "filesystem.h"
|
|
||||||
// #include "shell.h"
|
|
||||||
// #include "movement_custom_signal_tunes.h"
|
// #include "movement_custom_signal_tunes.h"
|
||||||
int8_t signal_tune[] = {
|
int8_t signal_tune[] = {
|
||||||
BUZZER_NOTE_C8, 5,
|
BUZZER_NOTE_C8, 5,
|
||||||
@ -429,8 +429,7 @@ void app_init(void) {
|
|||||||
movement_state.next_available_backup_register = 4;
|
movement_state.next_available_backup_register = 4;
|
||||||
_movement_reset_inactivity_countdown();
|
_movement_reset_inactivity_countdown();
|
||||||
|
|
||||||
/// FIXME: #SecondMovement needs filesystem support
|
filesystem_init();
|
||||||
// filesystem_init();
|
|
||||||
|
|
||||||
#if __EMSCRIPTEN__
|
#if __EMSCRIPTEN__
|
||||||
int32_t time_zone_offset = EM_ASM_INT({
|
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 we are plugged into USB, handle the serial shell
|
||||||
if (usb_is_enabled()) {
|
if (usb_is_enabled()) {
|
||||||
/// FIXME: #SecondMovement needs to bring back the shell
|
shell_task();
|
||||||
// shell_task();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
event.subsecond = 0;
|
event.subsecond = 0;
|
||||||
|
|||||||
@ -30,6 +30,7 @@
|
|||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <strings.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
#if __EMSCRIPTEN__
|
#if __EMSCRIPTEN__
|
||||||
@ -30,6 +30,7 @@
|
|||||||
|
|
||||||
#include "filesystem.h"
|
#include "filesystem.h"
|
||||||
#include "watch.h"
|
#include "watch.h"
|
||||||
|
#include "delay.h"
|
||||||
|
|
||||||
static int help_cmd(int argc, char *argv[]);
|
static int help_cmd(int argc, char *argv[]);
|
||||||
static int flash_cmd(int argc, char *argv[]);
|
static int flash_cmd(int argc, char *argv[]);
|
||||||
Loading…
x
Reference in New Issue
Block a user