📄 usb_shell.cpp
字号:
#include <stdio.h>
#include <stdlib.h>
#include "libusb/usb.h"
#include "usb_base.h"
#include "usb_shell.h"
/* ---------- */
#define SHELL_COMMAND_LINE_BUFFER_SIZE (256)
#define SHELL_MAX_NUM_COMMAND_TOKEN (16)
/* ---------- */
usb_dev_handle* G_usb_dev;
unsigned int G_usb_seq_num = 0;
/* ---------- */
static function_entity_t func_list[] =
{
{"r1", r1, "Read 1-byte from ARM memory"},
{"r2", r2, "Read 2-byte from ARM memory"},
{"r4", r4, "Read 4-byte from ARM memory"},
{"rr", rr, "Read a range of memory from ARM"},
{"w1", w1, "Write 1-byte to ARM memory"},
{"w2", w2, "Write 2-byte to ARM memory"},
{"w4", w4, "Write 4-byte to ARM memory"},
{"wr", wr, "Write file to ARM memory"},
{"flash_erase", flash_erase, "Erase one block of flash"},
{"flash_read", flash_read, "Read blocks of flash to file"},
{"flash_write", flash_write, "Write file to flash"},
{"help", show_help, "Show this help"},
{NULL, NULL, NULL},
};
int usb_read_ack(unsigned int usb_command, unsigned int usb_seq_num)
{
usb_ack_t usb_ack;
bulk_read(G_usb_dev, &usb_ack, sizeof(usb_ack));
printf("Ack status:\n");
printf("\tusb command: %d\n", usb_ack.usb_command);
printf("\tusb seq num: %d\n", usb_ack.usb_seq_num);
printf("\tusb ack status: %d\n", usb_ack.usb_ack_status);
int rval = 0;
if(usb_ack.usb_command != usb_command)
{
printf("Echo USB command error !!\n");
rval = -1;
}
if(usb_ack.usb_seq_num != usb_seq_num)
{
printf("Echo USB sequence number error !!\n");
rval = -1;
}
if(usb_ack.usb_ack_status != USB_ACK_SUCCESS)
{
printf("USB ACK Error !!\n");
rval = -1;
}
return rval;
}
/* ---------- */
int show_help(int argc, char *argv[])
{
function_entity_t *func_ptr = func_list;
printf("Support command : \n");
while(func_ptr->cmd != NULL) // go through the function table
{
printf("%-15s%s\n",
func_ptr->cmd,
func_ptr->help_text);
func_ptr++;
}
return 0;
}
/* ---------- */
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)
{
G_usb_seq_num++;
return func_ptr->func((argc-1), argv+1); // call the function & ignore 1st parameter (parameter consumed @ this stage)
}
func_ptr++;
}
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 = " ";
unsigned int i;
// remove the change line character
for(i=0; i<strlen(cmd_line); i++)
{
if(cmd_line[i] == '\n' || cmd_line[i] == '\r')
cmd_line[i] = '\0';
}
*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;
}
int main()
{
G_usb_dev = init_usb_if();
if(G_usb_dev == NULL)
{
printf("error: open usb IF error\n");
system("pause");
return -1;
}
while(1)
{
char cmd_buf[SHELL_COMMAND_LINE_BUFFER_SIZE];
int argc;
char *argv[SHELL_MAX_NUM_COMMAND_TOKEN];
printf("USB_SHELL>");
fgets(cmd_buf, SHELL_COMMAND_LINE_BUFFER_SIZE, stdin);
if(parse_cmd(&argc, argv, SHELL_MAX_NUM_COMMAND_TOKEN, cmd_buf) < 0)
printf("[SHELL] Error: Too many tokens, only %d commands will be processed\n", SHELL_MAX_NUM_COMMAND_TOKEN);
if(argc > 0)
exec_cmd(argc, argv);
}
usb_close(G_usb_dev);
system("pause");
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -