mirror of
https://github.com/logicog/RTLPlayground.git
synced 2026-08-30 14:52:51 +08:00
Use external command parser module. Add support for mirroring
This commit is contained in:
+3
-203
@@ -10,6 +10,7 @@
|
||||
#include "rtl837x_flash.h"
|
||||
#include "rtl837x_phy.h"
|
||||
#include "rtl837x_port.h"
|
||||
#include "cmd_parser.h"
|
||||
|
||||
#define SYS_TICK_HZ 100
|
||||
#define SERIAL_BAUD_RATE 115200
|
||||
@@ -47,11 +48,7 @@ volatile __xdata uint32_t ticks;
|
||||
volatile __xdata uint8_t sec_counter;
|
||||
volatile __xdata uint16_t sleep_ticks;
|
||||
|
||||
#define N_WORDS 16
|
||||
__xdata signed char cmd_words_b[N_WORDS];
|
||||
|
||||
// Buffer for serial input, SBUF_SIZE must be power of 2 < 256
|
||||
#define SBUF_SIZE 32
|
||||
__xdata char sbuf_ptr;
|
||||
__xdata uint8_t sbuf[SBUF_SIZE];
|
||||
__xdata uint8_t sfr_data[4];
|
||||
@@ -1497,26 +1494,6 @@ void setup_serial(void)
|
||||
}
|
||||
|
||||
|
||||
uint8_t cmd_compare(uint8_t start, uint8_t * __code cmd)
|
||||
{
|
||||
signed char i;
|
||||
signed char j = 0;
|
||||
|
||||
for (i = cmd_words_b[start]; i < cmd_words_b[start + 1] && sbuf[i] != ' '; i++) {
|
||||
// print_short(i); write_char(':'); print_short(j); write_char('#'); print_string("\r\n");
|
||||
// write_char('>'); write_char(cmd[j]); write_char('-'); write_char(sbuf[i]); print_string("\r\n");
|
||||
if (!cmd[j])
|
||||
return 1;
|
||||
if (sbuf[i] != cmd[j++])
|
||||
break;
|
||||
}
|
||||
// write_char('.'); print_short(i); write_char(':'); print_short(i);
|
||||
if (i >= cmd_words_b[start + 1] || sbuf[i] == ' ')
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
void setup_i2c(void)
|
||||
{
|
||||
REG_SET(0x0414, 0);
|
||||
@@ -1531,21 +1508,6 @@ void setup_i2c(void)
|
||||
}
|
||||
|
||||
|
||||
uint8_t atoi_short(uint16_t *vlan, uint8_t idx)
|
||||
{
|
||||
uint8_t err = 1;
|
||||
*vlan = 0;
|
||||
|
||||
while (sbuf[idx] >= '0' && sbuf[idx] <= '9') {
|
||||
err = 0;
|
||||
*vlan = (*vlan * 10) + sbuf[idx] - '0';
|
||||
idx++;
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void bootloader(void)
|
||||
{
|
||||
ticks = 0;
|
||||
@@ -1636,171 +1598,9 @@ void bootloader(void)
|
||||
port_stats_print();
|
||||
|
||||
print_string("\r\n> ");
|
||||
char l = sbuf_ptr;
|
||||
char line_ptr = l;
|
||||
char is_white = 1;
|
||||
cmd_parser_setup();
|
||||
while (1) {
|
||||
while (l != sbuf_ptr) {
|
||||
write_char(sbuf[l]);
|
||||
// Check whether there is a full line:
|
||||
if (sbuf[l] == '\n' || sbuf[l] == '\r') {
|
||||
#ifdef DEBUG
|
||||
print_long(ticks);
|
||||
#endif
|
||||
// Print line and parse command into words
|
||||
is_white = 1;
|
||||
uint8_t word = 0;
|
||||
cmd_words_b[0] = -1;
|
||||
while (line_ptr != l) {
|
||||
if (is_white && sbuf[line_ptr] != ' ') {
|
||||
is_white = 0;
|
||||
cmd_words_b[word++] = line_ptr;
|
||||
}
|
||||
if (sbuf[line_ptr] == ' ')
|
||||
is_white = 1;
|
||||
write_char(sbuf[line_ptr++]);
|
||||
line_ptr &= SBUF_SIZE - 1;
|
||||
if (word >= N_WORDS - 1) {
|
||||
print_string("\r\ntoo many arguments, truncated");
|
||||
line_ptr = l; // BUG: We should probably ignore the command
|
||||
break;
|
||||
}
|
||||
}
|
||||
cmd_words_b[word++] = line_ptr;
|
||||
cmd_words_b[word++] = -1;
|
||||
line_ptr = (l + 1) & (SBUF_SIZE - 1);
|
||||
|
||||
// Identify command
|
||||
signed char i = cmd_words_b[0];
|
||||
if (i >= 0 && cmd_words_b[1] >= 0) {
|
||||
/* print_string("\r\n THERE may be a command: ");
|
||||
print_short(i); print_string("\r\n");
|
||||
print_short(cmd_words_b[0]); print_string("\r\n");
|
||||
print_short(cmd_words_b[1]); print_string("\r\n");
|
||||
print_short(cmd_words_b[2]); print_string("\r\n");
|
||||
print_short(cmd_words_b[3]); print_string("\r\n");
|
||||
*/
|
||||
if (cmd_compare(0, "reset")) {
|
||||
print_string("\r\nRESET\n\n");
|
||||
reset_chip();
|
||||
}
|
||||
if (cmd_compare(0, "sfp")) {
|
||||
uint8_t rate = sfp_read_reg(12);
|
||||
print_string("\r\nRate: "); print_byte(rate);
|
||||
print_string(" Encoding: "); print_byte(sfp_read_reg(11));
|
||||
print_string("\r\n");
|
||||
for (uint8_t i = 20; i < 60; i++) {
|
||||
uint8_t c = sfp_read_reg(i);
|
||||
if (c)
|
||||
write_char(c);
|
||||
}
|
||||
}
|
||||
if (cmd_compare(0, "stat")) {
|
||||
port_stats_print();
|
||||
}
|
||||
if (cmd_compare(0, "flash") && cmd_words_b[1] > 0 && sbuf[cmd_words_b[1]] == 'r') {
|
||||
print_string("\r\nPRINT SECURITY REGISTERS\r\n");
|
||||
// The following will only show something else then 0xff if it was programmed for a managed switch
|
||||
flash_read_security(0x0001000, 40);
|
||||
flash_read_security(0x0002000, 40);
|
||||
flash_read_security(0x0003000, 40);
|
||||
}
|
||||
if (cmd_compare(0, "flash") && cmd_words_b[1] > 0 && sbuf[cmd_words_b[1]] == 'd') {
|
||||
print_string("\r\nDUMPING FLASH\r\n");
|
||||
flash_dump(0, 255);
|
||||
}
|
||||
if (cmd_compare(0, "flash") && cmd_words_b[1] > 0 && sbuf[cmd_words_b[1]] == 'j') {
|
||||
print_string("\r\nJEDEC ID\r\n");
|
||||
flash_read_jedecid();
|
||||
}
|
||||
if (cmd_compare(0, "flash") && cmd_words_b[1] > 0 && sbuf[cmd_words_b[1]] == 'u') {
|
||||
print_string("\r\nUNIQUE ID\r\n");
|
||||
flash_read_uid();
|
||||
}
|
||||
// Switch to flash 62.5 MHz mode
|
||||
if (cmd_compare(0, "flash") && cmd_words_b[1] > 0 && sbuf[cmd_words_b[1]] == 's') {
|
||||
print_string("\r\nFLASH FAST MODE\r\n");
|
||||
flash_init(1);
|
||||
print_string("\r\nNow dumping flash\r\n");
|
||||
flash_dump(0, 255);
|
||||
}
|
||||
if (cmd_compare(0, "flash") && cmd_words_b[1] > 0 && sbuf[cmd_words_b[1]] == 'e') {
|
||||
print_string("\r\nFLASH erase\r\n");
|
||||
flash_block_erase(0x20000);
|
||||
}
|
||||
if (cmd_compare(0, "flash") && cmd_words_b[1] > 0 && sbuf[cmd_words_b[1]] == 'w') {
|
||||
print_string("\r\nFLASH write\r\n");
|
||||
for (uint8_t i = 0; i < 20; i++)
|
||||
flash_buf[i] = greeting[i];
|
||||
flash_write_bytes(0x20000, flash_buf, 20);
|
||||
}
|
||||
if (cmd_compare(0, "port") && cmd_words_b[1] > 0) {
|
||||
print_string("\r\nPORT ");
|
||||
uint8_t p = sbuf[cmd_words_b[1]] - '1';
|
||||
print_byte(p);
|
||||
if (cmd_words_b[2] > 0 && cmd_compare(2, "2g5")) {
|
||||
print_string(" 2.5G\r\n");
|
||||
phy_set_mode(p, PHY_SPEED_2G5, 0, 0);
|
||||
}
|
||||
if (cmd_words_b[2] > 0 && cmd_compare(2, "1g")) {
|
||||
print_string(" 1G\r\n");
|
||||
phy_set_mode(p, PHY_SPEED_1G, 0, 0);
|
||||
}
|
||||
if (cmd_words_b[2] > 0 && cmd_compare(2, "auto")) {
|
||||
print_string(" AUTO\r\n");
|
||||
phy_set_mode(p, PHY_SPEED_AUTO, 0, 0);
|
||||
}
|
||||
if (cmd_words_b[2] > 0 && cmd_compare(2, "off")) {
|
||||
print_string(" OFF\r\n");
|
||||
phy_set_mode(p, PHY_OFF, 0, 0);
|
||||
}
|
||||
}
|
||||
if (cmd_compare(0, "l2")) {
|
||||
port_l2_learned();
|
||||
}
|
||||
if (cmd_compare(0, "pvid") && cmd_words_b[1] > 0 && cmd_words_b[2] > 0) {
|
||||
__xdata uint16_t pvid;
|
||||
uint8_t port;
|
||||
port = sbuf[cmd_words_b[1]] - '0';
|
||||
if (!atoi_short(&pvid, cmd_words_b[2]))
|
||||
port_pvid_set(port, pvid);
|
||||
}
|
||||
if (cmd_compare(0, "vlan")) {
|
||||
__xdata uint16_t vlan;
|
||||
__xdata uint16_t members = 0;
|
||||
__xdata uint16_t tagged = 0;
|
||||
if (!atoi_short(&vlan, cmd_words_b[1])) {
|
||||
print_short(vlan);
|
||||
if (cmd_words_b[2] > 0 && sbuf[cmd_words_b[2]] == 'd') {
|
||||
vlan_delete(vlan);
|
||||
} else {
|
||||
uint8_t w = 2;
|
||||
while (cmd_words_b[w] > 0) {
|
||||
uint8_t port;
|
||||
if (sbuf[cmd_words_b[w]] >= '0' && sbuf[cmd_words_b[w]] <= '9') {
|
||||
port = sbuf[cmd_words_b[w]] - '1';
|
||||
if (sbuf[cmd_words_b[w] + 1] >= '0' && sbuf[cmd_words_b[w] + 1] <= '9') {
|
||||
port = (port + 1) * 10 + sbuf[cmd_words_b[w] + 1] - '1';
|
||||
if (sbuf[cmd_words_b[w] + 2] == 't')
|
||||
tagged |= ((uint16_t)1) << port;
|
||||
} else {
|
||||
if (sbuf[cmd_words_b[w] + 1] == 't')
|
||||
tagged |= ((uint16_t)1) << port;
|
||||
}
|
||||
members |= ((uint16_t)1) << port;
|
||||
}
|
||||
w++;
|
||||
}
|
||||
vlan_create(vlan, members, tagged);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
print_string("\r\n> ");
|
||||
}
|
||||
l++;
|
||||
l &= (SBUF_SIZE - 1);
|
||||
}
|
||||
cmd_parser();
|
||||
idle(); // Enter Idle mode until interrupt occurs
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user