pico430prog/src/cli/cli.c

152 lines
3.2 KiB
C

#include <stddef.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <hardware/structs/iobank0.h>
#include <hardware/adc.h>
#include <hardware/flash.h>
#include <hardware/gpio.h>
#include <pico/stdlib.h>
#include <pico/stdio_usb.h>
#include <pico/binary_info.h>
#include "pinout.h"
#include "cli.h"
void piotest(void);
void delaytest(void);
void piodump_main(void);
void cli_dragonzap_test(void);
void cli_msp430_phy(void);
void cli_msp430_conn(void);
void cli_msp430_dump(void);
void cli_msp430_flash(void);
void cli_tool78_testtest(void);
void cli_tool78_prototest(void);
void cli_tool78_ocdtest(void);
void cli_tool78_g10_prototest(void);
void cli_tool78_g10_ocdtest(void);
void cli_tool78_glitch_dump(void);
void cli_tool78_glitch_paramsearch(void);
void cli_tool78_glitch_ocd_dump(void);
void cli_tool78_glitch_param_g10(void);
void cli_tool78_glitch_ocd_g10(void);
static void adc_init_stuff() {
adc_init();
adc_gpio_init(27);
adc_select_input(1);
sio_hw->gpio_set = 1u << 26;
sio_hw->gpio_oe_set = 1u << 26;
gpio_set_function(26, GPIO_FUNC_SIO);
}
static void cli_adc_test(void) {
adc_init_stuff();
while (true) {
printf("ADC=%04lx\n", (uint32_t)adc_read());
busy_wait_ms(16);
}
}
static void cli_erase_flash(void) {
flash_range_erase(0, FLASH_PAGE_SIZE);
static const uint8_t eyecatcher[FLASH_PAGE_SIZE] = "NUKE";
flash_range_program(0, eyecatcher, FLASH_PAGE_SIZE);
}
static struct cli_cmd cmds[] = {
{ "adctest", cli_adc_test },
{ "piotest", piotest },
{ "delaytest", delaytest },
{ "eraseflash", cli_erase_flash },
{ "piodump", piodump_main },
{ "m430phy", cli_msp430_phy },
{ "m430conn", cli_msp430_conn },
{ "m430dump", cli_msp430_dump },
{ "m430flash", cli_msp430_flash },
{ "rl78phy", cli_tool78_testtest },
{ "rl78sfp", cli_tool78_prototest },
{ "rl78ocd", cli_tool78_ocdtest },
{ "rl10sfp", cli_tool78_g10_prototest },
{ "rl10ocd", cli_tool78_g10_ocdtest },
{ "g78dump", cli_tool78_glitch_dump },
{ "g78param", cli_tool78_glitch_paramsearch },
{ "g78ocd", cli_tool78_glitch_ocd_dump },
{ "g10param", cli_tool78_glitch_param_g10 },
{ "g10ocd", cli_tool78_glitch_ocd_g10 },
{ "zap", cli_dragonzap_test },
{ NULL, NULL } // final
};
void cli_init(void) {
stdio_usb_init();
bsp_init_stuff();
while (!stdio_usb_connected()) ;
printf("Hi! Run a command, or 'help' for help.\n");
}
static char buf[256];
static const char* readline(void) {
memset(buf, 0, sizeof buf);
for (size_t off = 0; ; ) {
int c = getchar();
if (c == EOF) break;
putchar(c);
if (c == '\r') return buf;
if (c == '\n') continue;
if (strlen(buf) >= 255) return buf;
if (c == '\b') {
buf[off] = '\0';
--off;
} else if (c == '\t') {
// TODO: tab completion?
} else {
buf[off] = (char)c;
++off;
}
}
return buf;
}
static void print_help(void) {
printf("Enter 'help' or one of the below commands from the list to run:\n");
printf("\n");
printf("\t%s\n", "help");
for (size_t i = 0; cmds[i].name && cmds[i].func; ++i) {
printf("\t%s\n", cmds[i].name);
}
}
void cli_main(void) {
printf("\n> "); fflush(stdout);
const char* line = readline();
printf("\n");
for (size_t i = 0; cmds[i].name && cmds[i].func; ++i) {
if (!strcmp(line, cmds[i].name)) {
cmds[i].func();
return;
}
}
print_help();
}