surenyi
6 years ago
11 changed files with 302 additions and 1945 deletions
@ -0,0 +1,212 @@ |
|||
#include <stdio.h> |
|||
#include <stdlib.h> |
|||
#include <string.h> |
|||
#include <ctype.h> |
|||
#include <stdint.h> |
|||
#include "cmd.h" |
|||
|
|||
#define MAX_HISTORY (4) |
|||
#define CMDBUF_SIZE (256) |
|||
|
|||
enum KEY_ACTION{ |
|||
KEY_NULL = 0, /* NULL */ |
|||
CTRL_A = 1, /* Ctrl+a */ |
|||
CTRL_B = 2, /* Ctrl-b */ |
|||
CTRL_C = 3, /* Ctrl-c */ |
|||
CTRL_D = 4, /* Ctrl-d */ |
|||
CTRL_E = 5, /* Ctrl-e */ |
|||
CTRL_F = 6, /* Ctrl-f */ |
|||
CTRL_H = 8, /* Ctrl-h */ |
|||
TAB = 9, /* Tab */ |
|||
CTRL_K = 11, /* Ctrl+k */ |
|||
CTRL_L = 12, /* Ctrl+l */ |
|||
ENTER = 13, /* Enter */ |
|||
CTRL_N = 14, /* Ctrl-n */ |
|||
CTRL_P = 16, /* Ctrl-p */ |
|||
CTRL_T = 20, /* Ctrl-t */ |
|||
CTRL_U = 21, /* Ctrl+u */ |
|||
CTRL_W = 23, /* Ctrl+w */ |
|||
ESC = 27, /* Escape */ |
|||
BACKSPACE = 127 /* Backspace */ |
|||
}; |
|||
|
|||
static int maxargs = 4; |
|||
|
|||
static char cmdbuf[CMDBUF_SIZE]; |
|||
static int bufidx; |
|||
|
|||
static int do_help(cmd_tbl_t s, int argc, char *argv[]); |
|||
CON_CMD(help, 0, "commands help", do_help) |
|||
|
|||
static struct cmd_tbl_s *__cmds[] = { |
|||
&__con_cmd_help, |
|||
NULL |
|||
}; |
|||
|
|||
static int do_help(cmd_tbl_t s, int argc, char *argv[]) |
|||
{ |
|||
cmd_tbl_t it = __cmds[0]; |
|||
|
|||
printf("support commands:\r\n\r\n"); |
|||
while (it) { |
|||
printf("%s\t%s\r\n", it->name, it->help); |
|||
++it; |
|||
} |
|||
return 0; |
|||
} |
|||
|
|||
int cmd_init() |
|||
{ |
|||
cmd_tbl_t it = __cmds[0]; |
|||
|
|||
while (it) { |
|||
if (it->maxargs > maxargs) |
|||
maxargs = it->maxargs + 1; |
|||
++it; |
|||
} |
|||
|
|||
setvbuf(stdout, NULL, _IONBF, 0); |
|||
setvbuf(stderr, NULL, _IONBF, 0); |
|||
|
|||
return 0; |
|||
} |
|||
|
|||
static int32_t parse_line(char *cmd, uint32_t len, char *argv[], int _maxargs) |
|||
{ |
|||
uint32_t argc; |
|||
char *p; |
|||
uint32_t position; |
|||
|
|||
/* Init params */ |
|||
p = cmd; |
|||
position = 0; |
|||
argc = 0; |
|||
|
|||
while ((position < len) && (argc < _maxargs)) { |
|||
/* Skip all blanks */ |
|||
while (((char)(*p) == ' ') && (position < len)) { |
|||
*p = '\0'; |
|||
p++; |
|||
position++; |
|||
} |
|||
/* Process begin of a string */ |
|||
if (*p == '"') { |
|||
p++; |
|||
position++; |
|||
argv[argc] = p; |
|||
argc++; |
|||
/* Skip this string */ |
|||
while ((*p != '"') && (position < len)) { |
|||
p++; |
|||
position++; |
|||
} |
|||
/* Skip '"' */ |
|||
*p = '\0'; |
|||
p++; |
|||
position++; |
|||
} else {/* Normal char */ |
|||
argv[argc] = p; |
|||
argc++; |
|||
while (((char)*p != ' ') && ((char)*p != '\t') && (position < len)) { |
|||
p++; |
|||
position++; |
|||
} |
|||
} |
|||
} |
|||
return argc; |
|||
} |
|||
|
|||
int cmd_process(char *line) |
|||
{ |
|||
cmd_tbl_t it = __cmds[0]; |
|||
int i, argc, namelen; |
|||
int size = strlen(line); |
|||
char *argv[maxargs]; |
|||
|
|||
/* split line */ |
|||
for (i = 0; i < maxargs; ++i) { |
|||
argv[i] = NULL; |
|||
} |
|||
|
|||
argc = parse_line(line, size, argv, maxargs); |
|||
|
|||
if (argc <= 0) |
|||
goto recovery; |
|||
|
|||
while (it) { |
|||
namelen = strlen(it->name); |
|||
if (strncasecmp(argv[0], it->name, namelen) == 0) { |
|||
it->cmd(it, argc, argv); |
|||
return 1; |
|||
} |
|||
++it; |
|||
} |
|||
|
|||
/* recovery line */ |
|||
recovery: |
|||
for (i = 0; i < size; ++i) { |
|||
if (line[i] == '\0') |
|||
line[i] = ' '; |
|||
} |
|||
|
|||
return 0; |
|||
} |
|||
|
|||
void cmd_loop() |
|||
{ |
|||
char c; |
|||
int n; |
|||
|
|||
do { |
|||
c = fgetc(stdin); |
|||
if (c <= 0) |
|||
break; |
|||
if (bufidx >= CMDBUF_SIZE) { |
|||
printf("too long\r\n"); |
|||
bufidx = 0; |
|||
printf("\r\n> "); |
|||
} |
|||
|
|||
switch (c) { |
|||
case CTRL_C: |
|||
bufidx = 0; |
|||
printf("\r\n> "); |
|||
return; |
|||
case BACKSPACE: |
|||
case CTRL_H: |
|||
if (bufidx > 0) { |
|||
printf("\b \b"); |
|||
bufidx--; |
|||
cmdbuf[bufidx] = '\0'; |
|||
} |
|||
return; |
|||
case CTRL_L: |
|||
printf("\x1b[H\x1b[2J> "); |
|||
if (bufidx > 0) { |
|||
for (n = 0; n < bufidx; ++n) { |
|||
fputc(cmdbuf[n], stdout); |
|||
} |
|||
} |
|||
return; |
|||
} |
|||
|
|||
/* end of line */ |
|||
if (c == '\r' || c == '\n') { |
|||
printf("\r\n"); |
|||
if (bufidx > 0) { |
|||
cmdbuf[bufidx] = '\0'; |
|||
cmd_process(cmdbuf); |
|||
printf("\r\n"); |
|||
bufidx = 0; |
|||
} |
|||
printf("> "); |
|||
continue; |
|||
} |
|||
|
|||
if (isprint(c)) { |
|||
cmdbuf[bufidx++] = c; |
|||
fputc(c, stdout); |
|||
} |
|||
} while (1) ; |
|||
} |
|||
|
@ -0,0 +1,31 @@ |
|||
#ifndef __CMD_H__ |
|||
#define __CMD_H__ |
|||
#ifdef __cplusplus |
|||
extern "C" { |
|||
#endif |
|||
|
|||
struct cmd_tbl_s { |
|||
const char *name; |
|||
const char *help; |
|||
int maxargs; |
|||
int (*cmd)(struct cmd_tbl_s *, int argc, char *argv[]); |
|||
}; |
|||
|
|||
typedef struct cmd_tbl_s *cmd_tbl_t; |
|||
|
|||
#define CON_CMD(name, maxarg, usage, handler) \ |
|||
struct cmd_tbl_s __con_cmd_##name __attribute__((unused, section(".cmdline_cmd"))) = \ |
|||
{ #name, usage, maxarg, handler}; |
|||
|
|||
|
|||
/* initialize command */ |
|||
int cmd_init(void); |
|||
|
|||
/* process debug console input */ |
|||
void cmd_loop(void); |
|||
|
|||
#ifdef __cplusplus |
|||
} |
|||
#endif |
|||
#endif |
|||
|
@ -0,0 +1,34 @@ |
|||
FLASH 0x08000000 0x200000 |
|||
{ |
|||
EXEC_ROM 0x08000000 0x200000 |
|||
{ |
|||
startup_stm32f429_439xx.o(RESET, +FIRST) |
|||
*(+RO) |
|||
} |
|||
} |
|||
|
|||
CCM 0x10000000 0x10000 |
|||
{ |
|||
HEAP 0x10000000 0x2000 |
|||
{ |
|||
*(HEAP) |
|||
} |
|||
|
|||
STACK 0x10002000 0xE000 |
|||
{ |
|||
*(STACK) |
|||
} |
|||
} |
|||
|
|||
SRAM 0x20000000 0x30000 |
|||
{ |
|||
DATA_RAM 0x20000000 0x20000 |
|||
{ |
|||
*(+RW +ZI) |
|||
} |
|||
|
|||
TXDMA 0x20020000 0x10000 |
|||
{ |
|||
serial.o(.txdma* +ZI) |
|||
} |
|||
} |
File diff suppressed because it is too large
Loading…
Reference in new issue