📄 usb_shell_read_utility.cpp
字号:
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <io.h>
#include <sys/stat.h>
#include "libusb/usb.h"
#include "usb_base.h"
#include "usb_shell.h"
int r1(int argc, char *argv[])
{
unsigned int addr;
unsigned int cmd_code = USB_CMD_READ_R1;
if(argc < 1)
{
printf("Usage: r1 [32-bit HEX addr]\n");
return -1;
}
sscanf(argv[0], "%x", &addr);
/* ---------- BEGIN USB Conversation ---------- */
usb_cmd_t usb_cmd = {0};
usb_cmd.usb_command = cmd_code;
usb_cmd.usb_parameter[0] = addr;
usb_cmd.usb_seq_num = G_usb_seq_num;
if(bulk_write(G_usb_dev, &usb_cmd, sizeof(usb_cmd)) < 0)
{
printf("Write error\n");
return -1;
}
else
{
// ARM -> PC
unsigned char value;
bulk_read(G_usb_dev, &value, sizeof(value));
printf("Received value = 0x%02X\n", value);
// ACK phase
return usb_read_ack(cmd_code, G_usb_seq_num);
}
}
int r2(int argc, char *argv[])
{
unsigned int addr;
unsigned int cmd_code = USB_CMD_READ_R2;
if(argc < 1)
{
printf("Usage: r2 [32-bit HEX addr]\n");
return -1;
}
sscanf(argv[0], "%x", &addr);
/* ---------- BEGIN USB Conversation ---------- */
usb_cmd_t usb_cmd = {0};
usb_cmd.usb_command = cmd_code;
usb_cmd.usb_parameter[0] = addr;
usb_cmd.usb_seq_num = G_usb_seq_num;
if(bulk_write(G_usb_dev, &usb_cmd, sizeof(usb_cmd)) < 0)
{
printf("Write error\n");
return -1;
}
else
{
// ARM -> PC
unsigned short value;
bulk_read(G_usb_dev, &value, sizeof(value));
printf("Received value = 0x%04X\n", value);
// ACK phase
return usb_read_ack(cmd_code, G_usb_seq_num);
}
}
int r4(int argc, char *argv[])
{
unsigned int addr;
unsigned int cmd_code = USB_CMD_READ_R4;
if(argc < 1)
{
printf("Usage: r4 [32-bit HEX addr]\n");
return -1;
}
sscanf(argv[0], "%x", &addr);
addr = addr & (~3); // mask LSB-2bits to avoid memory mis-alignment
/* ---------- BEGIN USB Conversation ---------- */
usb_cmd_t usb_cmd = {0};
usb_cmd.usb_command = cmd_code;
usb_cmd.usb_parameter[0] = addr;
usb_cmd.usb_seq_num = G_usb_seq_num;
if(bulk_write(G_usb_dev, &usb_cmd, sizeof(usb_cmd)) < 0)
{
printf("Write error\n");
return -1;
}
else
{
// ARM -> PC
unsigned int value;
bulk_read(G_usb_dev, &value, sizeof(value));
printf("Received value = 0x%08X\n", value);
// ACK phase
return usb_read_ack(cmd_code, G_usb_seq_num);
}
}
int rr(int argc, char *argv[])
{
unsigned int addr;
unsigned int cmd_code = USB_CMD_READ_RANGE;
int length;
char *filename = NULL;
int fd;
if(argc < 2)
{
printf("Usage: rr [32-bit HEX addr] [Length in Decimal] or\n");
printf(" rr [32-bit HEX addr] [Length in Decimal] [filename to store]\n");
return -1;
}
sscanf(argv[0], "%x", &addr);
sscanf(argv[1], "%d", &length);
filename = argv[2];
if(length <= 0)
{
printf("Length = %d\n", length);
printf("Read length error !!\n");
return -1;
}
/* ---------- BEGIN USB Conversation ---------- */
usb_cmd_t usb_cmd;
usb_cmd.usb_command = cmd_code;
usb_cmd.usb_parameter[0] = addr;
usb_cmd.usb_parameter[1] = length;
usb_cmd.usb_seq_num = G_usb_seq_num;
if(bulk_write(G_usb_dev, &usb_cmd, sizeof(usb_cmd)) < 0)
{
printf("Write error\n");
return -1;
}
else
{
// ARM -> PC
unsigned char *buf = new unsigned char[length];
bulk_read(G_usb_dev, buf, length);
if(argc == 3) // store to file
{
fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC | O_BINARY, _S_IREAD | _S_IWRITE);
if(fd < 0)
{
printf("Unable to open file !!\n");
return -1;
}
write(fd, buf, length);
close(fd);
}
else
{
for(int i=0; i<length; i++)
printf("Addr 0x%08X = 0x%02X\n", addr+i, buf[i]);
}
// ACK phase
return usb_read_ack(cmd_code, G_usb_seq_num);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -