|
|
|
@ -18,10 +18,10 @@ |
|
|
|
|
#include "spi_flash.h" |
|
|
|
|
#include "usart.h" |
|
|
|
|
|
|
|
|
|
static void do_probe(FILE *fp, const char *s); |
|
|
|
|
static void do_read(FILE *fp, const char *s); |
|
|
|
|
static void do_write(FILE *fp, const char *s); |
|
|
|
|
static void do_erase(FILE *fp, const char *s); |
|
|
|
|
static void do_probe(const char *s); |
|
|
|
|
static void do_read(const char *s); |
|
|
|
|
static void do_write(const char *s); |
|
|
|
|
static void do_erase(const char *s); |
|
|
|
|
|
|
|
|
|
struct cmd cmds[] = { |
|
|
|
|
{ "probe", do_probe }, |
|
|
|
@ -41,21 +41,21 @@ static void parse_hex(FILE *fp, char *buf, size_t len) |
|
|
|
|
for (i = 0; i < len; ++i) { |
|
|
|
|
memset(s, '\0', 3); |
|
|
|
|
|
|
|
|
|
while ((s[0] = fgetc(fp)) && !isxdigit(s[0])) |
|
|
|
|
fputc(s[0], fp); |
|
|
|
|
while ((s[0] = getc(fp)) && !isxdigit(s[0])) |
|
|
|
|
putchar(s[0]); |
|
|
|
|
|
|
|
|
|
fputc(s[0], fp); |
|
|
|
|
putchar(s[0]); |
|
|
|
|
|
|
|
|
|
while ((s[1] = fgetc(fp)) && !isxdigit(s[1])) |
|
|
|
|
fputc(s[1], fp); |
|
|
|
|
while ((s[1] = getc(fp)) && !isxdigit(s[1])) |
|
|
|
|
putchar(s[1]); |
|
|
|
|
|
|
|
|
|
fputc(s[1], fp); |
|
|
|
|
putchar(s[1]); |
|
|
|
|
|
|
|
|
|
*buf++ = strtoul(s, NULL, 16); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static void print_hex_ascii(FILE *fp, const char *buf, size_t len) |
|
|
|
|
static void print_hex_ascii(const char *buf, size_t len) |
|
|
|
|
{ |
|
|
|
|
size_t n, i; |
|
|
|
|
char c; |
|
|
|
@ -65,35 +65,35 @@ static void print_hex_ascii(FILE *fp, const char *buf, size_t len) |
|
|
|
|
|
|
|
|
|
for (i = 0; i < 16; ++i) { |
|
|
|
|
c = (i < n) ? buf[i] : 0; |
|
|
|
|
fprintf(fp, "%02x", c); |
|
|
|
|
printf("%02x", c); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fprintf(fp, " "); |
|
|
|
|
printf(" "); |
|
|
|
|
|
|
|
|
|
for (i = 0; i < 16; ++i) { |
|
|
|
|
c = (i < n) ? buf[i] : 0; |
|
|
|
|
fprintf(fp, "%c", isalnum(c) ? c : '.'); |
|
|
|
|
printf("%c", isalnum(c) ? c : '.'); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fprintf(fp, "\n"); |
|
|
|
|
printf("\n"); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static char *prompt(FILE *fp, const char *prefix) |
|
|
|
|
static char *prompt(const char *prefix) |
|
|
|
|
{ |
|
|
|
|
char *alloc, *line, *s; |
|
|
|
|
size_t nbytes = 0, nalloc_bytes = 16; |
|
|
|
|
char c; |
|
|
|
|
|
|
|
|
|
fprintf(fp, prefix); |
|
|
|
|
printf(prefix); |
|
|
|
|
|
|
|
|
|
if (!(line = malloc(nalloc_bytes))) |
|
|
|
|
return NULL; |
|
|
|
|
|
|
|
|
|
s = line; |
|
|
|
|
|
|
|
|
|
while ((c = fgetc(fp)) && c != '\n') { |
|
|
|
|
fputc(c, fp); |
|
|
|
|
while ((c = getchar()) && c != '\n') { |
|
|
|
|
putchar(c); |
|
|
|
|
|
|
|
|
|
if (nbytes + 1 >= nalloc_bytes) { |
|
|
|
|
nalloc_bytes *= 2; |
|
|
|
@ -108,7 +108,7 @@ static char *prompt(FILE *fp, const char *prefix) |
|
|
|
|
nbytes++; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fprintf(fp, "\n"); |
|
|
|
|
printf("\n"); |
|
|
|
|
*s = '\0'; |
|
|
|
|
|
|
|
|
|
return line; |
|
|
|
@ -118,7 +118,7 @@ err_free_line: |
|
|
|
|
return NULL; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static void do_probe(FILE *fp, const char *s) |
|
|
|
|
static void do_probe(const char *s) |
|
|
|
|
{ |
|
|
|
|
char cmd[1] = { CMD_READ_ID }; |
|
|
|
|
char buf[6]; |
|
|
|
@ -126,10 +126,10 @@ static void do_probe(FILE *fp, const char *s) |
|
|
|
|
(void)s; |
|
|
|
|
|
|
|
|
|
spi_send_cmd(SPI1, cmd, 1, buf, 6); |
|
|
|
|
fprintf(fp, "JEDEC ID: %02x%02x%02x\n", buf[0], buf[1], buf[2]); |
|
|
|
|
printf("JEDEC ID: %02x%02x%02x\n", buf[0], buf[1], buf[2]); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static void do_read(FILE *fp, const char *s) |
|
|
|
|
static void do_read(const char *s) |
|
|
|
|
{ |
|
|
|
|
char buf[256], *end = NULL; |
|
|
|
|
size_t addr, len, nbytes; |
|
|
|
@ -160,14 +160,14 @@ static void do_read(FILE *fp, const char *s) |
|
|
|
|
while (len) { |
|
|
|
|
nbytes = min(len, 256); |
|
|
|
|
spi_flash_read(SPI1, addr, buf, nbytes); |
|
|
|
|
print_hex_ascii(fp, buf, nbytes); |
|
|
|
|
print_hex_ascii(buf, nbytes); |
|
|
|
|
|
|
|
|
|
addr += nbytes; |
|
|
|
|
len -= nbytes; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static void do_write(FILE *fp, const char *s) |
|
|
|
|
static void do_write(const char *s) |
|
|
|
|
{ |
|
|
|
|
char buf[256], *end = NULL; |
|
|
|
|
size_t addr, len, nbytes; |
|
|
|
@ -197,9 +197,9 @@ static void do_write(FILE *fp, const char *s) |
|
|
|
|
|
|
|
|
|
while (len) { |
|
|
|
|
nbytes = (len > 256) ? 256 : len; |
|
|
|
|
parse_hex(fp, buf, nbytes); |
|
|
|
|
fprintf(fp, "\n"); |
|
|
|
|
print_hex_ascii(fp, buf, nbytes); |
|
|
|
|
parse_hex(stdin, buf, nbytes); |
|
|
|
|
printf("\n"); |
|
|
|
|
print_hex_ascii(buf, nbytes); |
|
|
|
|
spi_flash_write(SPI1, addr, buf, nbytes); |
|
|
|
|
|
|
|
|
|
addr += nbytes; |
|
|
|
@ -207,13 +207,11 @@ static void do_write(FILE *fp, const char *s) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static void do_erase(FILE *fp, const char *s) |
|
|
|
|
static void do_erase(const char *s) |
|
|
|
|
{ |
|
|
|
|
char *end = NULL; |
|
|
|
|
size_t addr, len; |
|
|
|
|
|
|
|
|
|
(void)fp; |
|
|
|
|
|
|
|
|
|
if (strncmp(s, "0x", 2) == 0) |
|
|
|
|
s += 2; |
|
|
|
|
|
|
|
|
@ -237,10 +235,10 @@ static void do_erase(FILE *fp, const char *s) |
|
|
|
|
if (!len) |
|
|
|
|
return; |
|
|
|
|
|
|
|
|
|
spi_flash_erase(fp, SPI1, addr, len); |
|
|
|
|
spi_flash_erase(SPI1, addr, len); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static void parse_cmd(FILE *fp, const char *s) |
|
|
|
|
static void parse_cmd(const char *s) |
|
|
|
|
{ |
|
|
|
|
struct cmd *cmd; |
|
|
|
|
const char *args; |
|
|
|
@ -255,17 +253,17 @@ static void parse_cmd(FILE *fp, const char *s) |
|
|
|
|
|
|
|
|
|
for (cmd = cmds; cmd->key; cmd++) { |
|
|
|
|
if (strcmp(cmd->key, key) == 0) |
|
|
|
|
cmd->exec(fp, args); |
|
|
|
|
cmd->exec(args); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void cmd_loop(FILE *fp, const char *show) |
|
|
|
|
void cmd_loop(const char *show) |
|
|
|
|
{ |
|
|
|
|
char *line; |
|
|
|
|
|
|
|
|
|
while (1) { |
|
|
|
|
line = prompt(fp, show); |
|
|
|
|
parse_cmd(fp, line); |
|
|
|
|
line = prompt(show); |
|
|
|
|
parse_cmd(line); |
|
|
|
|
free(line); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|