Skip to content

Commit

Permalink
USB Improvements
Browse files Browse the repository at this point in the history
* Introduce shell module for basic serial shell with argument parsing
* Introduce shell_cmd_list module for basic compile-time command
  registration
* Harden USB handling to hang less and drop fewer inputs
  - Service tud_task() with periodic TC0 timer interrupt
  - Service cdc_task() with periodic TC1 timer interrupt
  - Handle shell servicing in main app loop
  - Add a circular buffering layer for reads/writes
* Change newline prints to also send carriage return
* Refactor filesystem commands for shell subsystem
* Introduce new shell commands:
  - 'help' command
  - 'flash' command to reset into bootloader
  - 'stress' command to stress CDC writes

Testing:
* Shell validated on Sensor Watch Blue w/ Linux host
* Shell validated in emscripten emulator
* Tuned by spamming inputs during `stress` cmd until stack didn't crash
  • Loading branch information
Hylian committed Jan 7, 2024
1 parent 63d6bc6 commit 5b762d0
Show file tree
Hide file tree
Showing 15 changed files with 820 additions and 160 deletions.
1 change: 1 addition & 0 deletions make.mk
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ SRCS += \
$(TOP)/watch-library/hardware/watch/watch_storage.c \
$(TOP)/watch-library/hardware/watch/watch_deepsleep.c \
$(TOP)/watch-library/hardware/watch/watch_private.c \
$(TOP)/watch-library/hardware/watch/watch_private_cdc.c \
$(TOP)/watch-library/hardware/watch/watch.c \
$(TOP)/watch-library/hardware/hal/src/hal_atomic.c \
$(TOP)/watch-library/hardware/hal/src/hal_delay.c \
Expand Down
121 changes: 61 additions & 60 deletions movement/filesystem.c
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ static int filesystem_ls(lfs_t *lfs, const char *path) {

printf("%4ld bytes ", info.size);

printf("%s\n", info.name);
printf("%s\r\n", info.name);
}

err = lfs_dir_close(lfs, &dir);
Expand All @@ -117,11 +117,11 @@ bool filesystem_init(void) {
// reformat if we can't mount the filesystem
// this should only happen on the first boot
if (err < 0) {
printf("Ignore that error! Formatting filesystem...\n");
printf("Ignore that error! Formatting filesystem...\r\n");
err = lfs_format(&lfs, &cfg);
if (err < 0) return false;
err = lfs_mount(&lfs, &cfg) == LFS_ERR_OK;
printf("Filesystem mounted with %ld bytes free.\n", filesystem_get_free_space());
printf("Filesystem mounted with %ld bytes free.\r\n", filesystem_get_free_space());
}

return err == LFS_ERR_OK;
Expand All @@ -139,7 +139,7 @@ bool filesystem_rm(char *filename) {
if (filesystem_file_exists(filename)) {
return lfs_remove(&lfs, filename) == LFS_ERR_OK;
} else {
printf("rm: %s: No such file\n", filename);
printf("rm: %s: No such file\r\n", filename);
return false;
}
}
Expand Down Expand Up @@ -197,13 +197,13 @@ static void filesystem_cat(char *filename) {
char *buf = malloc(info.size + 1);
filesystem_read_file(filename, buf, info.size);
buf[info.size] = '\0';
printf("%s\n", buf);
printf("%s\r\n", buf);
free(buf);
} else {
printf("\n");
printf("\r\n");
}
} else {
printf("cat: %s: No such file\n", filename);
printf("cat: %s: No such file\r\n", filename);
}
}

Expand All @@ -223,59 +223,60 @@ bool filesystem_append_file(char *filename, char *text, int32_t length) {
return lfs_file_close(&lfs, &file) == LFS_ERR_OK;
}

void filesystem_process_command(char *line) {
printf("$ %s", line);
char *command = strtok(line, " \n");

if (strcmp(command, "ls") == 0) {
char *directory = strtok(NULL, " \n");
if (directory == NULL) {
filesystem_ls(&lfs, "/");
} else {
printf("usage: ls\n");
}
} else if (strcmp(command, "cat") == 0) {
char *filename = strtok(NULL, " \n");
if (filename == NULL) {
printf("usage: cat file\n");
} else {
filesystem_cat(filename);
}
} else if (strcmp(command, "df") == 0) {
printf("free space: %ld bytes\n", filesystem_get_free_space());
} else if (strcmp(command, "rm") == 0) {
char *filename = strtok(NULL, " \n");
if (filename == NULL) {
printf("usage: rm file\n");
} else {
filesystem_rm(filename);
}
} else if (strcmp(command, "echo") == 0) {
char *text = malloc(248);
memset(text, 0, 248);
size_t pos = 0;
char *word = strtok(NULL, " \n");
while (strcmp(word, ">") && strcmp(word, ">>")) {
sprintf(text + pos, "%s ", word);
pos += strlen(word) + 1;
word = strtok(NULL, " \n");
if (word == NULL) break;
}
text[strlen(text) - 1] = 0;
char *filename = strtok(NULL, " \n");
if (filename == NULL) {
printf("usage: echo text > file\n");
} else if (strchr(filename, '/') || strchr(filename, '\\')) {
printf("subdirectories are not supported\n");
} else if (!strcmp(word, ">")) {
filesystem_write_file(filename, text, strlen(text));
filesystem_append_file(filename, "\n", 1);
} else if (!strcmp(word, ">>")) {
filesystem_append_file(filename, text, strlen(text));
filesystem_append_file(filename, "\n", 1);
}
free(text);
int filesystem_cmd_ls(int argc, char *argv[]) {
if (argc >= 2) {
filesystem_ls(&lfs, argv[1]);
} else {
printf("%s: command not found\n", command);
filesystem_ls(&lfs, "/");
}
return 0;
}

int filesystem_cmd_cat(int argc, char *argv[]) {
(void) argc;
filesystem_cat(argv[1]);
return 0;
}

int filesystem_cmd_df(int argc, char *argv[]) {
(void) argc;
(void) argv;
printf("free space: %ld bytes\r\n", filesystem_get_free_space());
return 0;
}

int filesystem_cmd_rm(int argc, char *argv[]) {
(void) argc;
filesystem_rm(argv[1]);
return 0;
}

int filesystem_cmd_echo(int argc, char *argv[]) {
(void) argc;

char *line = argv[1];
size_t line_len = strlen(line);
if (line[0] == '"' || line[0] == '\'') {
line++;
line_len -= 2;
line[line_len] = '\0';
}

if (strchr(argv[3], '/')) {
printf("subdirectories are not supported\r\n");
return -2;
}

if (!strcmp(argv[2], ">")) {
filesystem_write_file(argv[3], line, line_len);
filesystem_append_file(argv[3], "\n", 1);
} else if (!strcmp(argv[2], ">>")) {
filesystem_append_file(argv[3], line, line_len);
filesystem_append_file(argv[3], "\n", 1);
} else {
return -2;
}

return 0;
}

9 changes: 5 additions & 4 deletions movement/filesystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,10 @@ bool filesystem_write_file(char *filename, char *text, int32_t length);
*/
bool filesystem_append_file(char *filename, char *text, int32_t length);

/** @brief Handles the interactive file browser when Movement is plugged in to USB.
* @param line The command that the user typed into the serial console.
*/
void filesystem_process_command(char *line);
int filesystem_cmd_ls(int argc, char *argv[]);
int filesystem_cmd_cat(int argc, char *argv[]);
int filesystem_cmd_df(int argc, char *argv[]);
int filesystem_cmd_rm(int argc, char *argv[]);
int filesystem_cmd_echo(int argc, char *argv[]);

#endif // FILESYSTEM_H_
2 changes: 2 additions & 0 deletions movement/make/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ SRCS += \
../../littlefs/lfs_util.c \
../movement.c \
../filesystem.c \
../shell.c \
../shell_cmd_list.c \
../watch_faces/clock/simple_clock_face.c \
../watch_faces/clock/world_clock_face.c \
../watch_faces/clock/beats_face.c \
Expand Down
32 changes: 6 additions & 26 deletions movement/movement.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "watch.h"
#include "filesystem.h"
#include "movement.h"
#include "shell.h"

#ifndef MOVEMENT_FIRMWARE
#include "movement_config.h"
Expand Down Expand Up @@ -538,30 +539,9 @@ bool app_loop(void) {
}
}

// if we are plugged into USB, handle the file browser tasks
// if we are plugged into USB, handle the serial shell
if (watch_is_usb_enabled()) {
char line[256] = {0};
#if __EMSCRIPTEN__
// This is a terrible hack; ideally this should be handled deeper in the watch library.
// Alas, emscripten treats read() as something that should pop up an input box, so I
// wasn't able to implement this over there. I sense that this relates to read() being
// the wrong way to read data from USB (like we should be using fgets or something), but
// until I untangle that, this will have to do.
char *received_data = (char*)EM_ASM_INT({
var len = lengthBytesUTF8(tx) + 1;
var s = _malloc(len);
stringToUTF8(tx, s, len);
return s;
});
memcpy(line, received_data, min(255, strlen(received_data)));
free(received_data);
EM_ASM({
tx = "";
});
#else
read(0, line, 256);
#endif
if (strlen(line)) filesystem_process_command(line);
shell_task();
}

event.subsecond = 0;
Expand Down Expand Up @@ -633,13 +613,13 @@ void cb_fast_tick(void) {
// Notice: is it possible that two or more buttons have an identical timestamp? In this case
// only one of these buttons would receive the long press event. Don't bother for now...
if (movement_state.light_down_timestamp > 0)
if (movement_state.fast_ticks - movement_state.light_down_timestamp == MOVEMENT_LONG_PRESS_TICKS + 1)
if (movement_state.fast_ticks - movement_state.light_down_timestamp == MOVEMENT_LONG_PRESS_TICKS + 1)
event.event_type = EVENT_LIGHT_LONG_PRESS;
if (movement_state.mode_down_timestamp > 0)
if (movement_state.fast_ticks - movement_state.mode_down_timestamp == MOVEMENT_LONG_PRESS_TICKS + 1)
if (movement_state.fast_ticks - movement_state.mode_down_timestamp == MOVEMENT_LONG_PRESS_TICKS + 1)
event.event_type = EVENT_MODE_LONG_PRESS;
if (movement_state.alarm_down_timestamp > 0)
if (movement_state.fast_ticks - movement_state.alarm_down_timestamp == MOVEMENT_LONG_PRESS_TICKS + 1)
if (movement_state.fast_ticks - movement_state.alarm_down_timestamp == MOVEMENT_LONG_PRESS_TICKS + 1)
event.event_type = EVENT_ALARM_LONG_PRESS;
// this is just a fail-safe; fast tick should be disabled as soon as the button is up, the LED times out, and/or the alarm finishes.
// but if for whatever reason it isn't, this forces the fast tick off after 20 seconds.
Expand Down
Loading

0 comments on commit 5b762d0

Please sign in to comment.