Remove Mutex use (CH_CFG_USE_MUTEXES = FALSE), now all Mutex depend functions run in sweep thread

It allow:
- reduce shell thread stack size
- more compact code
- fix some hardcoded scan command code, allow write better scan version
- run calibrate (not depend from pause sweep flag)

Rewrite uint32_t my_atoui(const char *p), now its allow read:
hex 0xaAbBcC1122
dec 12345678
bin 0b00011100
oct 0o12345678

Add some comments
This commit is contained in:
DiSlord 2020-03-12 19:53:58 +03:00
parent 51b5cce016
commit 6f25d0d43f
3 changed files with 85 additions and 77 deletions

View file

@ -183,7 +183,7 @@
* *
* @note The default is @p TRUE. * @note The default is @p TRUE.
*/ */
#define CH_CFG_USE_MUTEXES TRUE #define CH_CFG_USE_MUTEXES FALSE
/** /**
* @brief Enables recursive behavior on mutexes. * @brief Enables recursive behavior on mutexes.

101
main.c
View file

@ -51,8 +51,11 @@ static BaseSequentialStream *shell_stream = (BaseSequentialStream *)&SDU1;
typedef void (*vna_shellcmd_t)(int argc, char *argv[]); typedef void (*vna_shellcmd_t)(int argc, char *argv[]);
#define VNA_SHELL_FUNCTION(command_name) static void command_name(int argc, char *argv[]) #define VNA_SHELL_FUNCTION(command_name) static void command_name(int argc, char *argv[])
// Shell command line buffer // Shell command line buffer, args, nargs, and function ptr
static char shell_line[VNA_SHELL_MAX_LENGTH]; static char shell_line[VNA_SHELL_MAX_LENGTH];
static char *shell_args[VNA_SHELL_MAX_ARGUMENTS + 1];
static uint16_t shell_nargs;
static volatile vna_shellcmd_t shell_function = 0;
//#define ENABLED_DUMP //#define ENABLED_DUMP
//#define ENABLE_THREADS_COMMAND //#define ENABLE_THREADS_COMMAND
@ -65,12 +68,9 @@ static void apply_edelay_at(int i);
static void cal_interpolate(int s); static void cal_interpolate(int s);
void update_frequencies(void); void update_frequencies(void);
void set_frequencies(uint32_t start, uint32_t stop, uint16_t points); void set_frequencies(uint32_t start, uint32_t stop, uint16_t points);
static bool sweep(bool break_on_operation); static bool sweep(bool break_on_operation);
static void transform_domain(void); static void transform_domain(void);
static MUTEX_DECL(mutex);
#define DRIVE_STRENGTH_AUTO (-1) #define DRIVE_STRENGTH_AUTO (-1)
#define FREQ_HARMONICS (config.harmonic_freq_threshold) #define FREQ_HARMONICS (config.harmonic_freq_threshold)
#define IS_HARMONIC_MODE(f) ((f) > FREQ_HARMONICS) #define IS_HARMONIC_MODE(f) ((f) > FREQ_HARMONICS)
@ -107,22 +107,23 @@ static THD_FUNCTION(Thread1, arg)
while (1) { while (1) {
bool completed = false; bool completed = false;
if (sweep_mode&(SWEEP_ENABLE|SWEEP_ONCE)) { if (sweep_mode&(SWEEP_ENABLE|SWEEP_ONCE)) {
chMtxLock(&mutex);
completed = sweep(true); completed = sweep(true);
sweep_mode&=~SWEEP_ONCE; sweep_mode&=~SWEEP_ONCE;
chMtxUnlock(&mutex);
} else { } else {
__WFI(); __WFI();
} }
// Run Shell command in sweep thread
chMtxLock(&mutex); if (shell_function){
shell_function(shell_nargs-1, &shell_args[1]);
shell_function = 0;
}
// Process UI inputs
ui_process(); ui_process();
// Process collected data, calculate trace coordinates and plot only if scan completed
if (sweep_mode&SWEEP_ENABLE) { if (sweep_mode&SWEEP_ENABLE && completed) {
// calculate trace coordinates and plot only if scan completed
if (completed) {
if ((domain_mode & DOMAIN_MODE) == DOMAIN_TIME) if ((domain_mode & DOMAIN_MODE) == DOMAIN_TIME)
transform_domain(); transform_domain();
// Prepare draw graphics, cache all lines, mark screen cells for redraw
plot_into_index(measured); plot_into_index(measured);
redraw_request |= REDRAW_CELLS|REDRAW_BATTERY; redraw_request |= REDRAW_CELLS|REDRAW_BATTERY;
@ -134,10 +135,8 @@ static THD_FUNCTION(Thread1, arg)
} }
} }
} }
}
// plot trace and other indications as raster // plot trace and other indications as raster
draw_all(completed); // flush markmap only if scan completed to prevent remaining traces draw_all(completed); // flush markmap only if scan completed to prevent remaining traces
chMtxUnlock(&mutex);
} }
} }
@ -364,13 +363,30 @@ static int32_t my_atoi(const char *p){
} }
// Convert string to uint32 // Convert string to uint32
uint32_t my_atoui(const char *p){ // 0x - for hex radix
uint32_t value = 0; // 0o - for oct radix
uint32_t c; // 0b - for bin radix
// default dec radix
uint32_t my_atoui(const char *p) {
uint32_t value = 0, radix = 10, c;
if (*p == '+') p++; if (*p == '+') p++;
while ((c = *p++ - '0') < 10) if (*p == '0') {
value = value * 10 + c; switch (p[1]) {
return value; case 'x': radix = 16; break;
case 'o': radix = 8; break;
case 'b': radix = 2; break;
default: goto calculate;
}
p+=2;
}
calculate:
while (1) {
c = *p++ - '0';
// c = to_upper(*p) - 'A' + 10
if (c >= 'A' - '0') c = (c&(~0x20)) - ('A' - '0') + 10;
if (c >= radix) return value;
value = value * radix + c;
}
} }
double double
@ -788,7 +804,7 @@ bool sweep(bool break_on_operation)
for (i = 0; i < sweep_points; i++) { // 5300 for (i = 0; i < sweep_points; i++) { // 5300
delay = set_frequency(frequencies[i]); // 700 delay = set_frequency(frequencies[i]); // 700
tlv320aic3204_select(0); // 60 CH0:REFLECT, reset and begin measure tlv320aic3204_select(0); // 60 CH0:REFLECT, reset and begin measure
DSP_START(delay+((i==0)?1:0)); // 1900 DSP_START(delay+((i==0)?2:0)); // 1900
//================================================ //================================================
// Place some code thats need execute while delay // Place some code thats need execute while delay
//================================================ //================================================
@ -844,18 +860,11 @@ VNA_SHELL_FUNCTION(cmd_scan)
} }
} }
pause_sweep();
chMtxLock(&mutex);
set_frequencies(start, stop, points); set_frequencies(start, stop, points);
if (cal_auto_interpolate && (cal_status & CALSTAT_APPLY)) if (cal_auto_interpolate && (cal_status & CALSTAT_APPLY))
cal_interpolate(lastsaveid); cal_interpolate(lastsaveid);
pause_sweep();
sweep_mode|= SWEEP_ONCE; sweep(false);
chMtxUnlock(&mutex);
// wait finishing sweep
while (sweep_mode&SWEEP_ONCE)
chThdSleepMilliseconds(10);
} }
static void static void
@ -1266,6 +1275,8 @@ cal_collect(int type)
default: default:
return; return;
} }
// Run sweep for collect data
sweep(false);
// Copy calibration data // Copy calibration data
memcpy(cal_data[dst], measured[src], sizeof measured[0]); memcpy(cal_data[dst], measured[src], sizeof measured[0]);
redraw_request |= REDRAW_CAL_STATUS; redraw_request |= REDRAW_CAL_STATUS;
@ -1960,7 +1971,7 @@ typedef struct {
} VNAShellCommand; } VNAShellCommand;
#pragma pack(pop) #pragma pack(pop)
// Some commands can executed only if process thread not in main cycle // Some commands can executed only in sweep thread, not in main cycle
#define CMD_WAIT_MUTEX 1 #define CMD_WAIT_MUTEX 1
static const VNAShellCommand commands[] = static const VNAShellCommand commands[] =
{ {
@ -1985,7 +1996,7 @@ static const VNAShellCommand commands[] =
{"power" , cmd_power , 0}, {"power" , cmd_power , 0},
{"sample" , cmd_sample , 0}, {"sample" , cmd_sample , 0},
// {"gamma" , cmd_gamma , 0}, // {"gamma" , cmd_gamma , 0},
{"scan" , cmd_scan , 0}, // Wait mutex hardcoded in cmd, need wait one sweep manually {"scan" , cmd_scan , CMD_WAIT_MUTEX},
{"sweep" , cmd_sweep , 0}, {"sweep" , cmd_sweep , 0},
{"test" , cmd_test , 0}, {"test" , cmd_test , 0},
{"touchcal" , cmd_touchcal , CMD_WAIT_MUTEX}, {"touchcal" , cmd_touchcal , CMD_WAIT_MUTEX},
@ -2079,44 +2090,44 @@ static int VNAShell_readLine(char *line, int max_size){
// //
static void VNAShell_executeLine(char *line){ static void VNAShell_executeLine(char *line){
// Parse and execute line // Parse and execute line
char *args[VNA_SHELL_MAX_ARGUMENTS + 1];
int n = 0;
char *lp = line, *ep; char *lp = line, *ep;
shell_nargs = 0;
while (*lp!=0){ while (*lp!=0){
// Skipping white space and tabs at string begin. // Skipping white space and tabs at string begin.
while (*lp==' ' || *lp=='\t') lp++; while (*lp==' ' || *lp=='\t') lp++;
// If an argument starts with a double quote then its delimiter is another quote, else delimiter is white space. // If an argument starts with a double quote then its delimiter is another quote, else delimiter is white space.
ep = (*lp == '"') ? strpbrk(++lp, "\"") : strpbrk( lp, " \t"); ep = (*lp == '"') ? strpbrk(++lp, "\"") : strpbrk( lp, " \t");
// Store in args string // Store in args string
args[n++]=lp; shell_args[shell_nargs++]=lp;
// Stop, end of input string // Stop, end of input string
if ((lp = ep) == NULL) if ((lp = ep) == NULL)
break; break;
// Argument limits check // Argument limits check
if (n > VNA_SHELL_MAX_ARGUMENTS) { if (shell_nargs > VNA_SHELL_MAX_ARGUMENTS) {
shell_printf("too many arguments, max "define_to_STR(VNA_SHELL_MAX_ARGUMENTS)""VNA_SHELL_NEWLINE_STR); shell_printf("too many arguments, max "define_to_STR(VNA_SHELL_MAX_ARGUMENTS)""VNA_SHELL_NEWLINE_STR);
return; return;
} }
// Set zero at the end of string and continue check // Set zero at the end of string and continue check
*lp++ = 0; *lp++ = 0;
} }
if (n == 0) if (shell_nargs == 0)
return; return;
// Execute line // Execute line
const VNAShellCommand *scp; const VNAShellCommand *scp;
for (scp = commands; scp->sc_name!=NULL;scp++) { for (scp = commands; scp->sc_name!=NULL;scp++) {
if (strcmp(scp->sc_name, args[0]) == 0) { if (strcmp(scp->sc_name, shell_args[0]) == 0) {
if (scp->flags&CMD_WAIT_MUTEX) { if (scp->flags&CMD_WAIT_MUTEX){
chMtxLock(&mutex); shell_function= scp->sc_function;
scp->sc_function(n-1, &args[1]); // Wait execute command in sweep thread
chMtxUnlock(&mutex); while(shell_function)
osalThreadSleepMilliseconds(100);
} }
else else
scp->sc_function(n-1, &args[1]); scp->sc_function(shell_nargs-1, &shell_args[1]);
return; return;
} }
} }
shell_printf("%s?"VNA_SHELL_NEWLINE_STR, args[0]); shell_printf("%s?"VNA_SHELL_NEWLINE_STR, shell_args[0]);
} }
#ifdef VNA_SHELL_THREAD #ifdef VNA_SHELL_THREAD
@ -2179,8 +2190,6 @@ int main(void)
halInit(); halInit();
chSysInit(); chSysInit();
chMtxObjectInit(&mutex);
//palSetPadMode(GPIOB, 8, PAL_MODE_ALTERNATE(1) | PAL_STM32_OTYPE_OPENDRAIN); //palSetPadMode(GPIOB, 8, PAL_MODE_ALTERNATE(1) | PAL_STM32_OTYPE_OPENDRAIN);
//palSetPadMode(GPIOB, 9, PAL_MODE_ALTERNATE(1) | PAL_STM32_OTYPE_OPENDRAIN); //palSetPadMode(GPIOB, 9, PAL_MODE_ALTERNATE(1) | PAL_STM32_OTYPE_OPENDRAIN);
i2cStart(&I2CD1, &i2ccfg); i2cStart(&I2CD1, &i2ccfg);

9
ui.c
View file

@ -2142,20 +2142,19 @@ void ui_process_touch(void)
if (status == EVT_TOUCH_PRESSED || status == EVT_TOUCH_DOWN) { if (status == EVT_TOUCH_PRESSED || status == EVT_TOUCH_DOWN) {
switch (ui_mode) { switch (ui_mode) {
case UI_NORMAL: case UI_NORMAL:
// Try drag marker
if (touch_pickup_marker()) if (touch_pickup_marker())
break; break;
// Try select lever mode (top and bottom screen)
if (touch_lever_mode_select()) { if (touch_lever_mode_select()) {
touch_wait_release(); touch_wait_release();
break; break;
} }
// switch menu mode after release
touch_wait_release(); touch_wait_release();
selection = -1; // hide keyboard mode selection
// switch menu mode
selection = -1;
ui_mode_menu(); ui_mode_menu();
break; break;
case UI_MENU: case UI_MENU:
menu_apply_touch(); menu_apply_touch();
break; break;