📄 shell.c
字号:
#include <stdio.h>
#include <string.h>
#include <ucos_ii.h> //ucos2
#include "config.h"
#include "os_c_lib_func.h"
#include "os_uart.h"
#include "os_shell.h"
#include "hal/isr.h"
#include "hal/hal_uart.h"
// #define DEBUG_SHELL
// Global Variable declaration
static function_entity_t func_list[];
OS_STK CmdShell_Stk[TaskStkLength]; // 64K
/* ---------- */
int r1(int argc, char *argv[])
{
unsigned int addr;
unsigned char value;
if(argc < 1)
{
OS_printf("Usage: r1 32-bit_addr\n");
OS_printf("Notice: read a non-legal address will cause system fail !!\n");
return -1;
}
sscanf(argv[0], "%x", &addr);
value = *(unsigned char*)(addr);
OS_printf("Memory 0x%08X = 0x%02X\n", addr, value);
return 0;
}
int r2(int argc, char *argv[])
{
unsigned int addr;
unsigned short value;
if(argc < 1)
{
OS_printf("Usage: r2 32-bit_addr\n");
OS_printf("Notice: read a non-legal address will cause system fail !!\n");
return -1;
}
sscanf(argv[0], "%x", &addr);
addr = addr & (~1); // mask LSB-1bits to avoid memory mis-alignment
value = *(unsigned short*)(addr);
OS_printf("Memory 0x%08X = 0x%04X\n", addr, value);
return 0;
}
int r4(int argc, char *argv[])
{
unsigned int addr;
unsigned int value;
if(argc < 1)
{
OS_printf("Usage: r4 32-bit_addr\n");
OS_printf("Notice: read a non-legal address will cause system fail !!\n");
return -1;
}
sscanf(argv[0], "%x", &addr);
addr = addr & (~3); // mask LSB-2bits to avoid memory mis-alignment
value = *(unsigned int*)(addr);
OS_printf("Memory 0x%08X = 0x%08X\n", addr, value);
return 0;
}
/* ---------- */
int w1(int argc, char *argv[])
{
unsigned int addr, value;
if(argc < 2)
{
OS_printf("Usage: w1 32-bit_addr 8-bit_value\n");
OS_printf("Notice: write a non-legal address will cause system fail !!\n");
return -1;
}
sscanf(argv[0], "%x", &addr);
sscanf(argv[1], "%x", &value);
*(unsigned char*)(addr) = (unsigned char)(value & 0xFF);
OS_printf("Write 0x%02X to 0x%08X\n", value, addr);
return 0;
}
int w2(int argc, char *argv[])
{
unsigned int addr, value;
if(argc < 2)
{
OS_printf("Usage: r2 32-bit_addr 16-bit_value\n");
OS_printf("Notice: write a non-legal address will cause system fail !!\n");
return -1;
}
sscanf(argv[0], "%x", &addr);
sscanf(argv[1], "%x", &value);
addr = addr & (~1); // mask LSB-1bits to avoid memory mis-alignment
*(unsigned short*)(addr) = (unsigned short)(value & 0xFFFF);
OS_printf("Write 0x%04X to 0x%08X\n", value, addr);
return 0;
}
int w4(int argc, char *argv[])
{
unsigned int addr;
unsigned int value;
if(argc < 2)
{
OS_printf("Usage: w4 32-bit_addr 32-bit_value\n");
OS_printf("Notice: writea non-legal address will cause system fail !!\n");
return -1;
}
sscanf(argv[0], "%x", &addr);
sscanf(argv[1], "%x", &value);
addr = addr & (~3); // mask LSB-2bits to avoid memory mis-alignment
*(unsigned int*)(addr) = value;
OS_printf("Write 0x%08X to 0x%08X\n", value, addr);
return 0;
}
/* ---------- */
int show_help(int argc, char *argv[])
{
function_entity_t *func_ptr = func_list;
OS_printf("Support command of "SHELL_NAME": \n");
while(func_ptr->cmd != NULL) // go through the function table
{
OS_printf("%-10s%s\n",
func_ptr->cmd,
func_ptr->help_text);
func_ptr++;
}
return 0;
}
/* ---------- */
static void show_irq_operation_help(void)
{
OS_printf(
"Usage: irq [ show | reset ]\n"
" show: display current IRQ status\n"
" reset: reset IRQ count\n");
}
int irq_operation(int argc, char *argv[])
{
int i;
INT_table_t* int_table = isr_get_int_table();
if(argc < 1)
{
show_irq_operation_help();
return 0;
}
if(strcmp(argv[0], "show") == 0)
{
/*
12345678123456789012123456789012345678123456789
IRQ# IRQ name # of ISR hooked Call count
8 14 20 10
*/
OS_printf("IRQ# IRQ name # of ISR hooked Call count\n");
for(i=0; i< IRQ_INT_NO; i++)
{
OS_printf("%-8d%-12s%-18d%-d\n",
i,
int_table->irq_name[i],
int_table->irq_entry[i].num_irq_func,
int_table->irq_count[i]);
}
}
else if(strcmp(argv[0], "reset") == 0)
{
isr_reset_irq_count();
}
else
{
show_irq_operation_help();
}
return 0;
}
/* ---------- */
void show_uart_operation_help(void)
{
OS_printf(
"Usage: uart [ show | flush ]\n"
" show: display current UART status\n"
" flush: flush UART buffer\n");
}
int uart_operation(int argc, char *argv[])
{
uart_buf_t *os_uart_buf = uart_get_buf();
hal_uart_buf_t *hal_uart_buf = hal_uart_get_buf();
if(argc < 1)
{
show_uart_operation_help();
return 0;
}
if(strcmp(argv[0], "show") == 0)
{
OS_printf(
"HAL Level:\nTX status\n"
"\tputindex:%d\n\tgetindex:%d\n\tbuffersize:%d\n\n",
hal_uart_buf->tx_buf.putindex, hal_uart_buf->tx_buf.getindex, hal_uart_buf->tx_buf.buffersize);
OS_printf(
"OS Level:\nTX status\n"
"\tputindex:%d\n\tgetindex:%d\n\tbuffersize:%d\n"
"RX status\n"
"\tputindex:%d\n\tgetindex:%d\n\tbuffersize:%d\n\n",
os_uart_buf->tx_buf.putindex, os_uart_buf->tx_buf.getindex, os_uart_buf->tx_buf.buffersize,
os_uart_buf->rx_buf.putindex, os_uart_buf->rx_buf.getindex, os_uart_buf->rx_buf.buffersize);
}
else if(strcmp(argv[0], "flush") == 0)
{
hal_ring_flush(&(hal_uart_buf->tx_buf));
ring_flush(&(os_uart_buf->tx_buf));
ring_flush(&(os_uart_buf->rx_buf));
}
else
{
show_uart_operation_help();
}
return 0;
}
/* ---------- */
static function_entity_t func_list[] =
{
{"help", show_help, "Show this help"},
{"irq", irq_operation, "Show IRQ status, incluing IRQ name, # of ISR hooked, Call count"},
{"r1", r1, "Read 1-byte from memory"},
{"r2", r2, "Read 2-byte from memory"},
{"r4", r4, "Read 4-byte from memory"},
{"uart", uart_operation, "UART related operation"},
{"w1", w1, "Write 1-byte to memory"},
{"w2", w2, "Write 2-byte to memory"},
{"w4", w4, "Write 4-byte to memory"},
{NULL, NULL},
};
/* ---------- */
int exec_cmd(int argc, char *argv[])
{
function_entity_t *func_ptr = func_list;
while(func_ptr->cmd != NULL) // go through the function table
{
if(strcmp(func_ptr->cmd, argv[0]) == 0)
{
return func_ptr->func((argc-1), argv+1); // call the function & ignore 1st parameter (parameter consumed @ this stage)
}
func_ptr++;
}
OS_printf("Command not supported. Type 'help' for available commands\n");
return -1;
}
int parse_cmd(int *argc, char *argv[], int max_cmd, char *cmd_line)
{
char *ptr, *sep = " ";
*argc = 0;
ptr = strtok(cmd_line, sep);
while(ptr != NULL)
{
if(*argc < max_cmd)
{
argv[*argc] = ptr;
(*argc)++;
ptr = strtok(NULL, sep);
}
else
return -1;
}
return 0;
}
void CmdShell_Task(void *pdata)
{
char cmd_buf[SHELL_COMMAND_LINE_BUFFER_SIZE];
int argc;
char *argv[SHELL_MAX_NUM_COMMAND_TOKEN];
while(1)
{
int len_str;
#ifdef DEBUG_SHELL
int i;
#endif
OS_printf(SHELL_PROMPT);
len_str = OS_uart_getstring(cmd_buf, SHELL_COMMAND_LINE_BUFFER_SIZE);
if(len_str > 0)
{
if(parse_cmd(&argc, argv, SHELL_MAX_NUM_COMMAND_TOKEN, cmd_buf) < 0)
OS_printf("[SHELL] Error: Too many tokens, only %d commands will be processed\n", SHELL_MAX_NUM_COMMAND_TOKEN);
#ifdef DEBUG_SHELL
OS_printf("Length_string: %d\n", strlen(cmd_buf));
OS_printf("Parse the input command... \n");
OS_printf("Num of tokens: %d\n", argc);
for(i=0; i<argc; i++)
{
OS_printf("%d, %d, %s\n", i, strlen(argv[i]), argv[i]);
}
#endif
exec_cmd(argc, argv);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -