uSDR-pico/monitor.c

125 wiersze
2.8 KiB
C
Czysty Zwykły widok Historia

/*
* monitor.c
*
* Created: Mar 2021
* Author: Arjan te Marvelde
*
* Command shell on stdin/stdout.
* Collects characters and parses commandstring.
*/
#include <stdio.h>
#include <string.h>
#include "pico/stdlib.h"
2021-04-07 20:13:13 +00:00
#include "lcd.h"
#include "si5351.h"
#include "dsp.h"
#include "monitor.h"
/* Monitor definitions */
#define ENDSTDIN 255
#define CR 13
#define LF 10
#define CMD_LEN 32
char mon_cmd[CMD_LEN+1];
uint8_t si5351_reg[200];
bool ptt = false;
2021-04-27 18:20:09 +00:00
extern volatile uint32_t fifo_overrun, fifo_rx, fifo_tx, fifo_xx, fifo_incnt;
2021-04-25 09:31:07 +00:00
extern uint32_t adc_count;
2021-04-16 17:51:51 +00:00
/* Commandstring parser */
char delim[] = " ";
2021-04-25 09:31:07 +00:00
#define NCMD 5
char shell[NCMD][3] = {"si", "lt", "fo", "pt", "ad"};
void mon_parse(char* s)
{
char *p;
int base, nreg, i;
2021-04-07 20:13:13 +00:00
p = s; //strtok(s, delim); // Get command part of string
for (i=0; i<NCMD; i++)
2021-04-07 20:13:13 +00:00
if (strncmp(p, shell[i], 2) == 0) break;
switch(i)
{
case 0:
// Next: p = strtok(NULL, delim); (returns NULL if none left)
for (i=0; i<nreg; i++) si5351_reg[i] = 0xaa;
si_getreg(si5351_reg, (uint8_t)base, (uint8_t)nreg);
for (i=0; i<nreg; i++) printf("%02x ",(int)(si5351_reg[i]));
printf("\n");
break;
case 1:
printf("%s\n", p);
2021-04-07 20:13:13 +00:00
lcd_test();
break;
case 2:
2021-04-27 18:20:09 +00:00
printf("Fifo input: %lu\n", fifo_incnt);
printf("Fifo rx: %lu\n", fifo_rx);
printf("Fifo tx: %lu\n", fifo_tx);
printf("Fifo unknown: %lu\n", fifo_xx);
2021-04-25 09:31:07 +00:00
printf("Fifo overruns: %lu\n", fifo_overrun);
break;
case 3:
if (ptt)
{
ptt = false;
printf("PTT released\n");
}
else
{
ptt = true;
printf("PTT active\n");
}
dsp_ptt(ptt);
break;
2021-04-25 09:31:07 +00:00
case 4:
printf("ADC IRQ count: %lu\n", adc_count);
break;
default:
2021-04-07 20:13:13 +00:00
printf("??\n");
break;
}
}
void mon_init()
{
2021-04-16 17:51:51 +00:00
stdio_init_all(); // Initialize Standard IO
mon_cmd[CMD_LEN] = '\0'; // Termination to be sure
}
2021-04-16 17:51:51 +00:00
/*
* This function collects characters from stdin until CR or LF
* Then the command is send to a parser and executed.
*/
void mon_evaluate(uint32_t timeout)
{
2021-04-08 20:32:27 +00:00
static int i = 0;
2021-04-16 17:51:51 +00:00
int c = getchar_timeout_us(timeout); // This is the only SDK way to read from stdin
if (c==PICO_ERROR_TIMEOUT) return; // Early bail out
switch (c)
{
2021-04-16 17:51:51 +00:00
case CR: // CR : need to parse command string
putchar((char)c); // Echo character
mon_cmd[i] = '\0'; // Terminate command string
if (i>0) // something to parse?
mon_parse(mon_cmd); // process command
i=0; // reset index
printf("Pico> "); // prompt
break;
2021-04-16 17:51:51 +00:00
case LF:
putchar((char)c); // Echo character
break; // Further ignore, assume CR as terminator
default:
2021-04-16 17:51:51 +00:00
if ((c<32)||(c>=128)) break; // Only allow alfanumeric
putchar((char)c); // Echo character
mon_cmd[i] = (char)c; // store in command string
if (i<CMD_LEN) i++; // check range and increment
break;
}
}