From 19bd043ac3f7222609f9cb3d954281bc13d6c3de Mon Sep 17 00:00:00 2001 From: "S.J.R. van Schaik" Date: Sat, 11 Mar 2017 13:02:26 +0000 Subject: [PATCH] shell: use flash driver model instead --- source/shell.c | 36 ++++++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/source/shell.c b/source/shell.c index 83bdc9b..ef8bb1f 100644 --- a/source/shell.c +++ b/source/shell.c @@ -10,10 +10,9 @@ #include #include -#include "macros.h" -#include "shell.h" -#include "spi_flash.h" -#include "usart.h" +#include +#include +#include static void do_probe(const char *s); static void do_read(const char *s); @@ -28,6 +27,8 @@ struct cmd cmds[] = { { NULL, NULL }, }; +static struct flash_dev *flash = NULL; + /* TODO: implement a parser for hex arrays that is more user-friendly. * Also implement dry runs for programmers. */ static void parse_hex(FILE *fp, char *buf, size_t len) @@ -117,13 +118,9 @@ err_free_line: static void do_probe(const char *s) { - char cmd[1] = { CMD_READ_ID }; - char buf[6]; - (void)s; - spi_send_cmd(SPI1, cmd, 1, buf, 6); - printf("JEDEC ID: %02x%02x%02x\n", buf[0], buf[1], buf[2]); + flash = flash_probe(); } static void do_read(const char *s) @@ -131,6 +128,11 @@ static void do_read(const char *s) char buf[256], *end = NULL; size_t addr, len, nbytes; + if (!flash) { + fprintf(stderr, "error: no flash device probed.\n"); + return; + } + if (strncmp(s, "0x", 2) == 0) s += 2; @@ -156,7 +158,7 @@ static void do_read(const char *s) while (len) { nbytes = min(len, 256); - spi_flash_read(SPI1, addr, buf, nbytes); + flash_read(flash, addr, buf, nbytes); print_hex_ascii(buf, nbytes); addr += nbytes; @@ -169,6 +171,11 @@ static void do_write(const char *s) char buf[256], *end = NULL; size_t addr, len, nbytes; + if (!flash) { + fprintf(stderr, "error: no flash device probed.\n"); + return; + } + if (strncmp(s, "0x", 2) == 0) s += 2; @@ -197,7 +204,7 @@ static void do_write(const char *s) parse_hex(stdin, buf, nbytes); printf("\n"); print_hex_ascii(buf, nbytes); - spi_flash_write(SPI1, addr, buf, nbytes); + flash_write(flash, addr, buf, nbytes); addr += nbytes; len -= nbytes; @@ -209,6 +216,11 @@ static void do_erase(const char *s) char *end = NULL; size_t addr, len; + if (!flash) { + fprintf(stderr, "error: no flash device probed.\n"); + return; + } + if (strncmp(s, "0x", 2) == 0) s += 2; @@ -232,7 +244,7 @@ static void do_erase(const char *s) if (!len) return; - spi_flash_erase(SPI1, addr, len); + flash_erase(flash, addr, len); } static void parse_cmd(const char *s)