From 4df68880f00f1e2ec65c4d7283c705c1523e2e94 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mario=20H=C3=BCttel?= Date: Sun, 26 Apr 2020 21:23:25 +0200 Subject: [PATCH] Add correct handling of sd card, add reset command, add ls command --- stm-firmware/Makefile | 2 +- .../shimatta_sdio_driver/shimatta_sdio.c | 6 +- .../shimatta_sdio_driver/shimatta_sdio.h | 2 + .../shimatta_sdio_config.h | 10 +-- .../reflow-controller/settings/settings.h | 2 + .../include/reflow-controller/shell.h | 2 + stm-firmware/main.c | 34 +++++++++- stm-firmware/shell.c | 66 ++++++++++++++++++- 8 files changed, 111 insertions(+), 13 deletions(-) diff --git a/stm-firmware/Makefile b/stm-firmware/Makefile index 337c6b3..1f93bdd 100644 --- a/stm-firmware/Makefile +++ b/stm-firmware/Makefile @@ -43,7 +43,7 @@ CFILES += rotary-encoder.c CFILES += stack-check.c CFILES += fatfs/diskio.c fatfs/ff.c fatfs/ffsystem.c fatfs/ffunicode.c fatfs/shimatta_sdio_driver/shimatta_sdio.c CFILES += pid-controller.c oven-driver.c -CFILES += settings/settings.c settings-sd-card.c +CFILES += settings/settings.c settings/settings-sd-card.c DEFINES += -DDEBUGBUILD diff --git a/stm-firmware/fatfs/shimatta_sdio_driver/shimatta_sdio.c b/stm-firmware/fatfs/shimatta_sdio_driver/shimatta_sdio.c index 4867957..d764419 100644 --- a/stm-firmware/fatfs/shimatta_sdio_driver/shimatta_sdio.c +++ b/stm-firmware/fatfs/shimatta_sdio_driver/shimatta_sdio.c @@ -32,9 +32,9 @@ static struct sd_info card_info; // = {.type = CARD_NONE}; * @brief checkNotInserted * @return return 0 if card is inserted, else 1 */ -static int sdio_check_inserted() { +int sdio_check_inserted() { #if SDIO_ENABLE_INS - return ((INS_PORT->IDR & INS_PIN) == (INS_ACTIVE_LEVEL<IDR & (1<IDR & WRITEPROT_PIN) == (WRITEPROT_ACTIVE_LEVEL<IDR & (1< #include #include +#include "fatfs/shimatta_sdio_driver/shimatta_sdio.h" #include #include #include @@ -60,6 +61,9 @@ static uint32_t rot; static volatile float pid_out; static volatile float current_temperature; +FATFS fs; +FATFS *fs_ptr = &fs; + static inline void uart_gpio_config() { #ifdef DEBUGBUILD @@ -104,11 +108,32 @@ static inline void setup_sell_uart(struct stm_uart *uart) NVIC_EnableIRQ(DMA2_Stream7_IRQn); } +static bool mount_sd_card_if_avail(bool mounted) +{ + FRESULT res; + + if (sdio_check_inserted() && mounted) { + memset(fs_ptr, 0, sizeof(FATFS)); + return false; + } + + if (!sdio_check_inserted() && !mounted) { + res = f_mount(fs_ptr, "0:/", 1); + if (res == FR_OK) { + return true; + } else { + return false; + } + } + + return mounted; +} + const char *oven_controller_hello_world = "Hello world :)\n"; int main() { - FATFS fs; + bool sd_card_mounted = false; FIL test_file; const char *uart_input; size_t uart_input_len; @@ -132,8 +157,10 @@ int main() setup_sell_uart(&shell_uart); shell_handle = shell_init(write_shell_callback); + shell_print_motd(shell_handle); - if (f_mount(&fs, "0:/", 1) == FR_OK) { + if (f_mount(fs_ptr, "0:/", 1) == FR_OK) { + sd_card_mounted = true; f_open(&test_file, "hello-world.txt", FA_OPEN_APPEND | FA_WRITE); f_write(&test_file, oven_controller_hello_world, strlen(oven_controller_hello_world), NULL); f_close(&test_file); @@ -143,6 +170,9 @@ int main() pid_zero(&pid); while (1) { + + sd_card_mounted = mount_sd_card_if_avail(sd_card_mounted); + pt1000_value_status = adc_pt1000_get_current_resistance(&pt1000_value); if (pt1000_value_status >= 0) { diff --git a/stm-firmware/shell.c b/stm-firmware/shell.c index 9c54313..254d08b 100644 --- a/stm-firmware/shell.c +++ b/stm-firmware/shell.c @@ -18,6 +18,8 @@ * If not, see . */ +#include +#include #include #include #include @@ -30,7 +32,7 @@ #include #include #include - +#include #include #include @@ -290,6 +292,43 @@ static shellmatta_retCode_t shell_cmd_pt1000_res_loop(const shellmatta_handle_t return SHELLMATTA_OK; } +static shellmatta_retCode_t shell_cmd_ls(const shellmatta_handle_t handle, const char *arguments, + uint32_t length) +{ + (void)length; + (void)arguments; + DIR dir; + FRESULT res; + FILINFO fno; + + res = f_opendir(&dir, "/"); + if (res != FR_OK) { + shellmatta_printf(handle, "Filesystem inaccessible. Is an SD Card inserted?\r\n"); + return SHELLMATTA_OK; + } + + while (f_readdir(&dir, &fno) == FR_OK) { + if (fno.fname[0] == 0) + break; + shellmatta_printf(handle, "%c\t%s\r\n", (fno.fattrib & AM_DIR ? 'd' : 'f'), fno.fname); + } + + f_closedir(&dir); + + return SHELLMATTA_OK; +} + +static shellmatta_retCode_t shell_cmd_reset(const shellmatta_handle_t handle, const char *arguments, + uint32_t length) +{ + (void)handle; + (void)length; + (void)arguments; + + NVIC_SystemReset(); + + return SHELLMATTA_BUSY; +} //typedef struct shellmatta_cmd //{ // char *cmd; /**< command name */ @@ -300,7 +339,7 @@ static shellmatta_retCode_t shell_cmd_pt1000_res_loop(const shellmatta_handle_t // struct shellmatta_cmd *next; /**< pointer to next command or NULL */ //} shellmatta_cmd_t; -static shellmatta_cmd_t cmd[10] = { +static shellmatta_cmd_t cmd[12] = { { .cmd = "version", .cmdAlias = "ver", @@ -379,8 +418,25 @@ static shellmatta_cmd_t cmd[10] = { .helpText = "Get current rotary encoder value", .usageText = "", .cmdFct = shell_cmd_rot, + .next = &cmd[10], + }, + { + .cmd = "ls", + .cmdAlias = NULL, + .helpText = "List filesystem contents", + .usageText = "", + .cmdFct = shell_cmd_ls, + .next = &cmd[11], + }, + { + .cmd = "reset", + .cmdAlias = NULL, + .helpText = "Reset controller", + .usageText = "Resets the controller", + .cmdFct = shell_cmd_reset, .next = NULL, } + }; shellmatta_handle_t shell_init(shellmatta_write_t write_func) @@ -396,6 +452,12 @@ shellmatta_handle_t shell_init(shellmatta_write_t write_func) return handle; } +void shell_print_motd(shellmatta_handle_t shell) +{ + shellmatta_printf(shell, "\r\nShimatta ไป•่ˆžใฃใŸ Reflow Controller ready\r\n\r\n"); + shell_cmd_ver(shell, NULL, 0UL); + shell_handle_input(shell, "\r\n", 2UL); +} void shell_handle_input(shellmatta_handle_t shell, const char *data, size_t len) {