From ae2dad0ff281909526b24f4849c55af48f8b3fc5 Mon Sep 17 00:00:00 2001 From: hugen79 Date: Mon, 9 Dec 2019 00:42:18 +0800 Subject: [PATCH] =?UTF-8?q?nanovna-h=20F303=EF=BC=8Cui=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .cproject | 153 +++++ .project | 27 + NANOVNA_STM32_F303/board.h | 1 + NanoVNA_DAP.cfg | 4 + adc.c | 13 +- dsp.c | 33 +- fft.h | 16 +- flash.c | 32 +- ili9341.c | 449 ++++++------- main.c | 1252 ++++++++++++++++++++---------------- mcuconf.h | 3 +- nanovna.h | 115 ++-- plot.c | 622 ++++++++---------- si5351.c | 79 ++- si5351.h | 14 +- tlv320aic3204.c | 11 +- ui.c | 1074 ++++++++++++++----------------- 17 files changed, 1951 insertions(+), 1947 deletions(-) create mode 100644 .cproject create mode 100644 .project create mode 100644 NanoVNA_DAP.cfg diff --git a/.cproject b/.cproject new file mode 100644 index 00000000..83547e2f --- /dev/null +++ b/.cproject @@ -0,0 +1,153 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/.project b/.project new file mode 100644 index 00000000..c33482a8 --- /dev/null +++ b/.project @@ -0,0 +1,27 @@ + + + NanoVNA_AA6KL + + + + + + org.eclipse.cdt.managedbuilder.core.genmakebuilder + clean,full,incremental, + + + + + org.eclipse.cdt.managedbuilder.core.ScannerConfigBuilder + full,incremental, + + + + + + org.eclipse.cdt.core.cnature + org.eclipse.cdt.core.ccnature + org.eclipse.cdt.managedbuilder.core.managedBuildNature + org.eclipse.cdt.managedbuilder.core.ScannerConfigNature + + diff --git a/NANOVNA_STM32_F303/board.h b/NANOVNA_STM32_F303/board.h index 8408c3d8..8b015b79 100644 --- a/NANOVNA_STM32_F303/board.h +++ b/NANOVNA_STM32_F303/board.h @@ -58,6 +58,7 @@ #define GPIOA_LEVER1 1 #define GPIOA_LEVER2 2 #define GPIOA_PUSH 3 +#define GPIOA_VBUS 4 #define GPIOA_DAC2 5 #define GPIOA_XP 6 #define GPIOA_YP 7 diff --git a/NanoVNA_DAP.cfg b/NanoVNA_DAP.cfg new file mode 100644 index 00000000..5723773f --- /dev/null +++ b/NanoVNA_DAP.cfg @@ -0,0 +1,4 @@ +interface cmsis-dap +transport select swd +source [find target/stm32f3x.cfg] +adapter_khz 8000 diff --git a/adc.c b/adc.c index c3cd93e6..db9a7278 100644 --- a/adc.c +++ b/adc.c @@ -126,7 +126,7 @@ void adc_init(void) uint16_t adc_single_read(ADC_TypeDef *adc, uint32_t chsel) { - /* ADC setup */ + /* ADC setup */ #ifdef NANOVNA_F303 adcStart(&ADCD2, NULL); adcgrpcfgXY.sqr[0] = ADC_SQR1_SQ1_N(chsel); @@ -148,12 +148,12 @@ uint16_t adc_single_read(ADC_TypeDef *adc, uint32_t chsel) return adc->DR; #endif + (void)adc; } int16_t adc_vbat_read(ADC_TypeDef *adc) { #define ADC_FULL_SCALE 3300 -#define VBAT_DIODE_VF 160 #define VREFINT_CAL (*((uint16_t*)0x1FFFF7BA)) float vbat = 0; float vrefint = 0; @@ -177,13 +177,13 @@ int16_t adc_vbat_read(ADC_TypeDef *adc) return -1; } - return vbat_raw + VBAT_DIODE_VF; - + return vbat_raw + config.vbat_offset; +(void)adc; } void adc_start_analog_watchdogd(ADC_TypeDef *adc, uint32_t chsel) { - uint32_t cfgr1; + // uint32_t cfgr1; #ifdef NANOVNA_F303 adcStart(&ADCD2, NULL); @@ -213,6 +213,7 @@ void adc_start_analog_watchdogd(ADC_TypeDef *adc, uint32_t chsel) /* ADC conversion start.*/ adc->CR |= ADC_CR_ADSTART; #endif + (void)adc; } void adc_stop(ADC_TypeDef *adc) @@ -242,6 +243,7 @@ void adc_stop(ADC_TypeDef *adc) ;*/ } #endif + (void)adc; } void adc_interrupt(ADC_TypeDef *adc) @@ -272,6 +274,7 @@ void adc_interrupt(ADC_TypeDef *adc) handle_touch_interrupt(); } #endif + (void)adc; } OSAL_IRQ_HANDLER(STM32_ADC1_HANDLER) diff --git a/dsp.c b/dsp.c index 6e9f607c..f1bd1187 100644 --- a/dsp.c +++ b/dsp.c @@ -21,12 +21,12 @@ #include #include "nanovna.h" -#ifdef ENABLED_DUMP +#ifdef __DUMP_CMD__ int16_t samp_buf[SAMPLE_LEN]; int16_t ref_buf[SAMPLE_LEN]; -#endif +#endif //__DUMP_CMD__ -const int16_t sincos_tbl[48][2] = { +static const int16_t sincos_tbl[48][2] = { { 10533, 31029 }, { 27246, 18205 }, { 32698, -2143 }, { 24636, -21605 }, { 6393, -32138 }, {-14493, -29389 }, {-29389, -14493 }, {-32138, 6393 }, {-21605, 24636 }, { -2143, 32698 }, { 18205, 27246 }, { 31029, 10533 }, @@ -41,13 +41,12 @@ const int16_t sincos_tbl[48][2] = { {-24636, -21605 }, {-32698, -2143 }, {-27246, 18205 }, {-10533, 31029 } }; -int32_t acc_samp_s; -int32_t acc_samp_c; -int32_t acc_ref_s; -int32_t acc_ref_c; +static int32_t acc_samp_s; +static int32_t acc_samp_c; +static int32_t acc_ref_s; +static int32_t acc_ref_c; -void -dsp_process(int16_t *capture, size_t length) +void dsp_process(int16_t *capture, size_t length) { uint32_t *p = (uint32_t*)capture; uint32_t len = length / 2; @@ -61,10 +60,10 @@ dsp_process(int16_t *capture, size_t length) uint32_t sr = *p++; int16_t ref = sr & 0xffff; int16_t smp = (sr>>16) & 0xffff; -#ifdef ENABLED_DUMP +#ifdef __DUMP_CMD__ ref_buf[i] = ref; samp_buf[i] = smp; -#endif +#endif //__DUMP_CMD__ int32_t s = sincos_tbl[i][0]; int32_t c = sincos_tbl[i][1]; samp_s += smp * s / 16; @@ -85,8 +84,7 @@ dsp_process(int16_t *capture, size_t length) acc_ref_c = ref_c; } -void -calculate_gamma(float gamma[2]) +void calculate_gamma(float gamma[2]) { #if 1 // calculate reflection coeff. by samp divide by ref @@ -107,22 +105,19 @@ calculate_gamma(float gamma[2]) #endif } -void -fetch_amplitude(float gamma[2]) +void fetch_amplitude(float gamma[2]) { gamma[0] = acc_samp_s * 1e-9; gamma[1] = acc_samp_c * 1e-9; } -void -fetch_amplitude_ref(float gamma[2]) +void fetch_amplitude_ref(float gamma[2]) { gamma[0] = acc_ref_s * 1e-9; gamma[1] = acc_ref_c * 1e-9; } -void -reset_dsp_accumerator(void) +void reset_dsp_accumerator(void) { acc_ref_s = 0; acc_ref_c = 0; diff --git a/fft.h b/fft.h index 17dece95..9f8c44cb 100644 --- a/fft.h +++ b/fft.h @@ -25,12 +25,10 @@ #include #include -#include static uint16_t reverse_bits(uint16_t x, int n) { uint16_t result = 0; - int i; - for (i = 0; i < n; i++, x >>= 1) + for (int i = 0; i < n; i++, x >>= 1) result = (result << 1) | (x & 1U); return result; } @@ -45,10 +43,8 @@ static void fft256(float array[][2], const uint8_t dir) { const uint8_t real = dir & 1; const uint8_t imag = ~real & 1; - uint16_t i; - uint16_t size; - for (i = 0; i < n; i++) { + for (uint16_t i = 0; i < n; i++) { uint16_t j = reverse_bits(i, levels); if (j > i) { float temp = array[i][real]; @@ -61,13 +57,11 @@ static void fft256(float array[][2], const uint8_t dir) { } // Cooley-Tukey decimation-in-time radix-2 FFT - for (size = 2; size <= n; size *= 2) { + for (uint16_t size = 2; size <= n; size *= 2) { uint16_t halfsize = size / 2; uint16_t tablestep = n / size; - uint16_t i; - for (i = 0; i < n; i += size) { - uint16_t j, k; - for (j = i, k = 0; j < i + halfsize; j++, k += tablestep) { + for (uint16_t i = 0; i < n; i += size) { + for (uint16_t j = i, k = 0; j < i + halfsize; j++, k += tablestep) { uint16_t l = j + halfsize; float tpre = array[l][real] * cos(2 * M_PI * k / 256) + array[l][imag] * sin(2 * M_PI * k / 256); float tpim = -array[l][real] * sin(2 * M_PI * k / 256) + array[l][imag] * cos(2 * M_PI * k / 256); diff --git a/flash.c b/flash.c index 2129228b..655d277c 100644 --- a/flash.c +++ b/flash.c @@ -40,7 +40,7 @@ static void flash_erase_page0(uint32_t page_address) FLASH->CR &= ~FLASH_CR_PER; } -int flash_erase_page(uint32_t page_address) +static int flash_erase_page(uint32_t page_address) { chSysLock(); flash_erase_page0(page_address); @@ -48,7 +48,7 @@ int flash_erase_page(uint32_t page_address) return 0; } -void flash_program_half_word(uint32_t address, uint16_t data) +static void flash_program_half_word(uint32_t address, uint16_t data) { flash_wait_for_last_operation(); FLASH->CR |= FLASH_CR_PG; @@ -57,7 +57,7 @@ void flash_program_half_word(uint32_t address, uint16_t data) FLASH->CR &= ~FLASH_CR_PG; } -void flash_unlock(void) +static void flash_unlock(void) { // unlock sequence FLASH->KEYR = 0x45670123; @@ -65,11 +65,10 @@ void flash_unlock(void) } -static uint32_t -checksum(const void *start, size_t len) +static uint32_t checksum(const void *start, size_t len) { uint32_t *p = (uint32_t*)start; - uint32_t *tail = (uint32_t*)(start + len); + uint32_t *tail = (uint32_t*)((uint8_t*)start + len); uint32_t value = 0; while (p < tail) value ^= *p++; @@ -85,8 +84,7 @@ const uint32_t save_config_area = 0x08030000; const uint32_t save_config_area = 0x08018000; #endif -int -config_save(void) +int config_save(void) { uint16_t *src = (uint16_t*)&config; uint16_t *dst = (uint16_t*)save_config_area; @@ -110,8 +108,7 @@ config_save(void) return 0; } -int -config_recall(void) +int config_recall(void) { const config_t *src = (const config_t*)save_config_area; void *dst = &config; @@ -137,8 +134,7 @@ uint32_t saveareas[] = int16_t lastsaveid = 0; -int -caldata_save(int id) +int caldata_save(int id) { uint16_t *src = (uint16_t*)¤t_props; uint16_t *dst; @@ -155,8 +151,8 @@ caldata_save(int id) flash_unlock(); /* erase flash pages */ - void *p = dst; - void *tail = p + sizeof(properties_t); + uint8_t* p = (uint8_t*)dst; + uint8_t* tail = p + sizeof(properties_t); while (p < tail) { flash_erase_page((uint32_t)p); p += FLASH_PAGESIZE; @@ -175,7 +171,6 @@ caldata_save(int id) return 0; } -int caldata_recall(int id) { properties_t *src; @@ -202,8 +197,7 @@ caldata_recall(int id) return 0; } -const properties_t * -caldata_ref(int id) +const properties_t* caldata_ref(int id) { const properties_t *src; if (id < 0 || id >= SAVEAREA_MAX) @@ -225,8 +219,8 @@ clear_all_config_prop_data(void) flash_unlock(); /* erase flash pages */ - void *p = (void*)save_config_area; - void *tail = p + save_config_prop_area_size; + uint8_t* p = (uint8_t*)save_config_area; + uint8_t* tail = p + save_config_prop_area_size; while (p < tail) { flash_erase_page((uint32_t)p); p += FLASH_PAGESIZE; diff --git a/ili9341.c b/ili9341.c index 5a15732b..1e6327dd 100644 --- a/ili9341.c +++ b/ili9341.c @@ -30,30 +30,26 @@ uint16_t spi_buffer[SPI_BUFFER_SIZE]; -void -ssp_wait(void) -{ - while (SPI1->SR & SPI_SR_BSY) - ; -} +//static void ssp_wait(void) +//{ +// while (SPI1->SR & SPI_SR_BSY) +// ; +//} -void -ssp_wait_slot(void) +static void ssp_wait_slot(void) { while ((SPI1->SR & 0x1800) == 0x1800) ; } -void -ssp_senddata(uint8_t x) +static void ssp_senddata(uint8_t x) { *(uint8_t*)(&SPI1->DR) = x; while (SPI1->SR & SPI_SR_BSY) ; } -uint8_t -ssp_sendrecvdata(uint8_t x) +static uint8_t ssp_sendrecvdata(uint8_t x) { while (!(SPI1->SR & SPI_SR_TXE)); // clear OVR @@ -64,8 +60,7 @@ ssp_sendrecvdata(uint8_t x) return SPI1->DR; } -void -ssp_senddata16(uint16_t x) +static void ssp_senddata16(uint16_t x) { ssp_wait_slot(); SPI1->DR = x; @@ -73,8 +68,8 @@ ssp_senddata16(uint16_t x) // ; } -static inline void -ssp_senddata16RGB(uint16_t x) + #ifdef ILI9488 +static inline void ssp_senddata16RGB(uint16_t x) { uint8_t r = (x & 0xF800) >> 8; uint8_t g = (x & 0x07E0) >> 3; @@ -83,32 +78,30 @@ ssp_senddata16RGB(uint16_t x) ssp_senddata(g); ssp_senddata(b); } +#endif -void -ssp_databit8(void) +static void ssp_databit8(void) { SPI1->CR2 = (SPI1->CR2 & 0xf0ff) | 0x0700; //LPC_SSP1->CR0 = (LPC_SSP1->CR0 & 0xf0) | SSP_DATABIT_8; } -void -ssp_databit16(void) -{ - SPI1->CR2 = (SPI1->CR2 & 0xf0ff) | 0x0f00; - //LPC_SSP1->CR0 = (LPC_SSP1->CR0 & 0xf0) | SSP_DATABIT_16; -} +//static void ssp_databit16(void) +//{ +// SPI1->CR2 = (SPI1->CR2 & 0xf0ff) | 0x0f00; +// //LPC_SSP1->CR0 = (LPC_SSP1->CR0 & 0xf0) | SSP_DATABIT_16; +//} -const stm32_dma_stream_t *dmatx; -uint32_t txdmamode; +static const stm32_dma_stream_t *dmatx; +static uint32_t txdmamode; static void spi_lld_serve_tx_interrupt(SPIDriver *spip, uint32_t flags) { (void)spip; (void)flags; } -void -spi_init(void) +static void spi_init(void) { rccEnableSPI1(FALSE); @@ -127,15 +120,12 @@ spi_init(void) dmaStreamSetPeripheral(dmatx, &SPI1->DR); SPI1->CR1 = 0; - // SPI1->CR1 = SPI_CR1_MSTR | SPI_CR1_SSM | SPI_CR1_SSI; // | SPI_CR1_BR_0; // | SPI_CR1_BR_0 ;// | SPI_CR1_BR_1; fPCLK/4 - //SPI1->CR1 = SPI_CR1_MSTR | SPI_CR1_SSM | SPI_CR1_SSI | SPI_CR1_BR_2 | SPI_CR1_BR_1 | SPI_CR1_BR_0; // | SPI_CR1_BR_0 ;// | SPI_CR1_BR_1; fPCLK/4 - SPI1->CR1 = SPI_CR1_MSTR | SPI_CR1_SSM | SPI_CR1_SSI ; + SPI1->CR1 = SPI_CR1_MSTR | SPI_CR1_SSM | SPI_CR1_SSI;// | SPI_CR1_BR_1; SPI1->CR2 = 0x0700 | SPI_CR2_TXDMAEN | SPI_CR2_FRXTH; SPI1->CR1 |= SPI_CR1_SPE; } -void -send_command(uint8_t cmd, int len, const uint8_t *data) +static void send_command(uint8_t cmd, int len, const uint8_t *data) { CS_LOW; DC_CMD; @@ -165,20 +155,19 @@ send_command(uint8_t cmd, int len, const uint8_t *data) //CS_HIGH; } -void -send_command16(uint8_t cmd, int data) -{ - CS_LOW; - DC_CMD; - ssp_databit8(); - ssp_senddata(cmd); - DC_DATA; - ssp_databit16(); - ssp_senddata16(data); - CS_HIGH; -} +//static void send_command16(uint8_t cmd, int data) +//{ +// CS_LOW; +// DC_CMD; +// ssp_databit8(); +// ssp_senddata(cmd); +// DC_DATA; +// ssp_databit16(); +// ssp_senddata16(data); +// CS_HIGH; +//} -const uint8_t ili9341_init_seq[] = { +static const uint8_t ili9341_init_seq[] = { // cmd, len, data..., // Power control B 0xCF, 3, 0x00, 0x83, 0x30, @@ -295,10 +284,11 @@ const uint8_t ili9488_init_seq[] = { }; -void -ili9341_init(void) + +void ili9341_init(void) { - spi_init(); + chMtxLock(&mutex_ili9341); + spi_init(); DC_DATA; RESET_ASSERT; @@ -322,6 +312,7 @@ ili9341_init(void) chThdSleepMilliseconds(100); send_command(0x29, 0, NULL); // display on + chMtxUnlock(&mutex_ili9341); } void ili9341_pixel(int x, int y, int color) @@ -340,6 +331,7 @@ void ili9341_pixel(int x, int y, int color) #if defined(ILI9488) void ili9341_fill(int x, int y, int w, int h, int color) { + chMtxLock(&mutex_ili9341); if (((x+w)>LCD_WIDTH) || ((y+h)>LCD_HEIGHT)) { return; } @@ -361,10 +353,12 @@ void ili9341_fill(int x, int y, int w, int h, int color) ssp_senddata16(gb); len -= 2; } + chMtxUnlock(&mutex_ili9341); } #else void ili9341_fill(int x, int y, int w, int h, int color) { + chMtxLock(&mutex_ili9341); uint8_t xx[4] = { x >> 8, x, (x+w-1) >> 8, (x+w-1) }; uint8_t yy[4] = { y >> 8, y, (y+h-1) >> 8, (y+h-1) }; int len = w * h; @@ -373,12 +367,14 @@ void ili9341_fill(int x, int y, int w, int h, int color) send_command(0x2C, 0, NULL); while (len-- > 0) ssp_senddata16(color); + chMtxUnlock(&mutex_ili9341); } #endif #if 0 void ili9341_bulk(int x, int y, int w, int h) { + chMtxLock(&mutex_ili9341); uint8_t xx[4] = { x >> 8, x, (x+w-1) >> 8, (x+w-1) }; uint8_t yy[4] = { y >> 8, y, (y+h-1) >> 8, (y+h-1) }; uint16_t *buf = spi_buffer; @@ -388,10 +384,12 @@ void ili9341_bulk(int x, int y, int w, int h) send_command(0x2C, 0, NULL); while (len-- > 0) ssp_senddata16(*buf++); + chMtxUnlock(&mutex_ili9341); } #else void ili9341_bulk(int x, int y, int w, int h) { + chMtxLock(&mutex_ili9341); uint8_t xx[4] = { x >> 8, x, (x+w-1) >> 8, (x+w-1) }; uint8_t yy[4] = { y >> 8, y, (y+h-1) >> 8, (y+h-1) }; int len = w * h; @@ -412,11 +410,11 @@ void ili9341_bulk(int x, int y, int w, int h) dmaStreamEnable(dmatx); dmaWaitCompletion(dmatx); #endif + chMtxUnlock(&mutex_ili9341); } #endif -void -ili9341_read_memory_raw(uint8_t cmd, int len, uint16_t* out) +static void ili9341_read_memory_raw(uint8_t cmd, int len, uint16_t* out) { uint8_t r, g, b; send_command(cmd, 0, NULL); @@ -445,9 +443,9 @@ ili9341_read_memory_raw(uint8_t cmd, int len, uint16_t* out) CS_HIGH; } -void -ili9341_read_memory(int x, int y, int w, int h, int len, uint16_t *out) +void ili9341_read_memory(int x, int y, int w, int h, int len, uint16_t *out) { + chMtxLock(&mutex_ili9341); uint8_t xx[4] = { x >> 8, x, (x+w-1) >> 8, (x+w-1) }; uint8_t yy[4] = { y >> 8, y, (y+h-1) >> 8, (y+h-1) }; @@ -455,85 +453,78 @@ ili9341_read_memory(int x, int y, int w, int h, int len, uint16_t *out) send_command(0x2B, 4, yy); ili9341_read_memory_raw(0x2E, len, out); + chMtxUnlock(&mutex_ili9341); } -void -ili9341_read_memory_continue(int len, uint16_t* out) +void ili9341_read_memory_continue(int len, uint16_t* out) { + chMtxLock(&mutex_ili9341); ili9341_read_memory_raw(0x3E, len, out); + chMtxUnlock(&mutex_ili9341); } #if !defined(ST7796S) -void -ili9341_drawchar_5x7(uint8_t ch, int x, int y, uint16_t fg, uint16_t bg) -{ - #if defined(ILI9488) - ili9341_drawchar_size(ch, x, y, fg, bg, 1); - #else - uint16_t *buf = spi_buffer; - uint16_t bits; - int c, r; - for(c = 0; c < 7; c++) { - bits = x5x7_bits[(ch * 7) + c]; - for (r = 0; r < 5; r++) { - *buf++ = (0x8000 & bits) ? fg : bg; - bits <<= 1; +void ili9341_drawchar_5x7(uint8_t ch, int x, int y, uint16_t fg, uint16_t bg) +{ + chMtxLock(&mutex_ili9341); + uint16_t *buf = spi_buffer; + uint8_t bits; + int c, r; + for(c = 0; c < 7; c++) { + bits = x5x7_bits[(ch * 7) + c]; + for (r = 0; r < 5; r++) { + *buf++ = (0x80 & bits) ? fg : bg; + bits <<= 1; + } } - } - ili9341_bulk(x, y, 5, 7); - #endif -} - -void -ili9341_drawstring_5x7(const char *str, int x, int y, uint16_t fg, uint16_t bg) -{ - while (*str) { - ili9341_drawchar_5x7(*str, x, y, fg, bg); - x += X_SPACE; - str++; - } -} - -void -ili9341_drawstring_5x7_inv(const char *str, int x, int y, uint16_t fg, uint16_t bg, bool invert) -{ - if (invert) - ili9341_drawstring_5x7(str, x, y, bg, fg); - else - ili9341_drawstring_5x7(str, x, y, fg, bg); + ili9341_bulk(x, y, 5, 7); + chMtxUnlock(&mutex_ili9341); } -void -ili9341_drawchar_size(uint8_t ch, int x, int y, uint16_t fg, uint16_t bg, uint8_t size) +void ili9341_drawstring_5x7(const char *str, int x, int y, uint16_t fg, uint16_t bg) { - uint16_t *buf = spi_buffer; - uint16_t bits; - int c, r; - for(c = 0; c < 7*size; c++) { - bits = x5x7_bits[(ch * 7) + (c / size)]; - for (r = 0; r < 5*size; r++) { - *buf++ = (0x8000 & bits) ? fg : bg; - if (r % size == (size-1)) { - bits <<= 1; - } + chMtxLock(&mutex_ili9341); + while (*str) { + ili9341_drawchar_5x7(*str, x, y, fg, bg); + x += 5; + str++; } - } - ili9341_bulk(x, y, 5*size, 7*size); + chMtxUnlock(&mutex_ili9341); +} + +void ili9341_drawchar_size(uint8_t ch, int x, int y, uint16_t fg, uint16_t bg, uint8_t size) +{ + chMtxLock(&mutex_ili9341); + uint16_t *buf = spi_buffer; + uint8_t bits; + int c, r; + for(c = 0; c < 7*size; c++) { + bits = x5x7_bits[(ch * 7) + (c / size)]; + for (r = 0; r < 5*size; r++) { + *buf++ = (0x80 & bits) ? fg : bg; + if (r % size == (size-1)) { + bits <<= 1; + } + } + } + ili9341_bulk(x, y, 5*size, 7*size); + chMtxUnlock(&mutex_ili9341); } -void -ili9341_drawstring_size(const char *str, int x, int y, uint16_t fg, uint16_t bg, uint8_t size) +void ili9341_drawstring_size(const char *str, int x, int y, uint16_t fg, uint16_t bg, uint8_t size) { - while (*str) { - ili9341_drawchar_size(*str, x, y, fg, bg, size); - x += 5 * size; - str++; - } + chMtxLock(&mutex_ili9341); + while (*str) { + ili9341_drawchar_size(*str, x, y, fg, bg, size); + x += 5 * size; + str++; + } + chMtxUnlock(&mutex_ili9341); } #else -void -ili9341_drawchar_7x13(uint8_t ch, int x, int y, uint16_t fg, uint16_t bg) + +void ili9341_drawchar_7x13(uint8_t ch, int x, int y, uint16_t fg, uint16_t bg) { uint16_t *buf = spi_buffer; uint16_t bits; @@ -551,26 +542,20 @@ ili9341_drawchar_7x13(uint8_t ch, int x, int y, uint16_t fg, uint16_t bg) void ili9341_drawstring_7x13(const char *str, int x, int y, uint16_t fg, uint16_t bg) { +chMtxLock(&mutex_ili9341); while (*str) { ili9341_drawchar_7x13(*str, x, y, fg, bg); x += 7; str++; } -} - -void -ili9341_drawstring_7x13_inv(const char *str, int x, int y, uint16_t fg, uint16_t bg, bool invert) -{ - if (invert) - ili9341_drawstring_7x13(str, x, y, bg, fg); - else - ili9341_drawstring_7x13(str, x, y, fg, bg); +chMtxUnlock(&mutex_ili9341); } void ili9341_drawchar_size(uint8_t ch, int x, int y, uint16_t fg, uint16_t bg, uint8_t size) { + chMtxLock(&mutex_ili9341); uint16_t *buf = spi_buffer; uint16_t bits; int c, r; @@ -584,129 +569,130 @@ ili9341_drawchar_size(uint8_t ch, int x, int y, uint16_t fg, uint16_t bg, uint8_ } } ili9341_bulk(x, y, 7*size, 13*size); +chMtxUnlock(&mutex_ili9341); } void ili9341_drawstring_size(const char *str, int x, int y, uint16_t fg, uint16_t bg, uint8_t size) { + chMtxLock(&mutex_ili9341); while (*str) { ili9341_drawchar_size(*str, x, y, fg, bg, size); x += 7 * size; str++; } + chMtxUnlock(&mutex_ili9341); } #endif -#define SWAP(x,y) do { int z=x; x = y; y = z; } while(0) +#define SWAP(x,y) { int z=x; x = y; y = z; } -void -ili9341_line(int x0, int y0, int x1, int y1, uint16_t fg) +void ili9341_line(int x0, int y0, int x1, int y1, uint16_t fg) { - if (x0 > x1) { - SWAP(x0, x1); - SWAP(y0, y1); - } + chMtxLock(&mutex_ili9341); + if (x0 > x1) { + SWAP(x0, x1); + SWAP(y0, y1); + } - while (x0 <= x1) { - int dx = x1 - x0 + 1; - int dy = y1 - y0; - if (dy >= 0) { - dy++; - if (dy > dx) { - dy /= dx; dx = 1; - } else { - dx /= dy; dy = 1; - } - } else { - dy--; - if (-dy > dx) { - dy /= dx; dx = 1; - } else { - dx /= -dy; dy = -1; - } + while (x0 <= x1) { + int dx = x1 - x0 + 1; + int dy = y1 - y0; + if (dy >= 0) { + dy++; + if (dy > dx) { + dy /= dx; dx = 1; + } else { + dx /= dy; dy = 1; + } + } else { + dy--; + if (-dy > dx) { + dy /= dx; dx = 1; + } else { + dx /= -dy; dy = -1; + } + } + if (dy > 0) + ili9341_fill(x0, y0, dx, dy, fg); + else + ili9341_fill(x0, y0+dy, dx, -dy, fg); + x0 += dx; + y0 += dy; } - if (dy > 0) - ili9341_fill(x0, y0, dx, dy, fg); - else - ili9341_fill(x0, y0+dy, dx, -dy, fg); - x0 += dx; - y0 += dy; - } + chMtxUnlock(&mutex_ili9341); } const font_t NF20x22 = { 20, 22, 1, 3*22, (const uint8_t *)numfont20x22 }; -void -ili9341_drawfont(uint8_t ch, const font_t *font, int x, int y, uint16_t fg, uint16_t bg) -{ - uint16_t *buf = spi_buffer; - const uint8_t *bitmap = &font->bitmap[font->slide * ch]; - int c, r; - - for (c = 0; c < font->height; c++) { - uint8_t bits = *bitmap++; - uint8_t m = 0x80; - for (r = 0; r < font->width; r++) { - *buf++ = (bits & m) ? fg : bg; - m >>= 1; - - if (m == 0) { - bits = *bitmap++; - m = 0x80; - } +void ili9341_drawfont(uint8_t ch, const font_t *font, int x, int y, uint16_t fg, uint16_t bg) +{ + chMtxLock(&mutex_ili9341); + uint16_t *buf = spi_buffer; + const uint8_t *bitmap = &font->bitmap[font->slide * ch]; + int c, r; + + for (c = 0; c < font->height; c++) { + uint8_t bits = *bitmap++; + uint8_t m = 0x80; + for (r = 0; r < font->width; r++) { + *buf++ = (bits & m) ? fg : bg; + m >>= 1; + + if (m == 0) { + bits = *bitmap++; + m = 0x80; + } + } } - } ili9341_bulk(x, y, font->width, font->height); + chMtxUnlock(&mutex_ili9341); } -#if 1 +#if 0 +static const uint16_t colormap[] = { + RGBHEX(0x00ff00), RGBHEX(0x0000ff), RGBHEX(0xff0000), + RGBHEX(0x00ffff), RGBHEX(0xff00ff), RGBHEX(0xffff00) +}; -static long rands = 654321; -uint16_t my_rand () { - rands = (54321123457 * rands) % 31459; - return rands; +static void ili9341_pixel(int x, int y, int color) +{ + uint8_t xx[4] = { x >> 8, x, (x+1) >> 8, (x+1) }; + uint8_t yy[4] = { y >> 8, y, (y+1) >> 8, (y+1) }; + uint8_t cc[2] = { color >> 8, color }; + send_command(0x2A, 4, xx); + send_command(0x2B, 4, yy); + send_command(0x2C, 2, cc); + //send_command16(0x2C, color); } -#if 1 -const uint16_t colormap[] = { - RGB565(255,0,0), RGB565(0,255,0), RGB565(0,0,255), - RGB565(255,255,0), RGB565(0,255,255), RGB565(255,0,255) -}; - -void -ili9341_test(int mode) +void ili9341_test(int mode) { - int x, y; - int i; - uint16_t x0, x1, y0, y1; - int c; - // int r; - switch (mode) { - default: + chMtxLock(&mutex_ili9341); + int x, y; + int i; + switch (mode) { + default: #if 1 - //ili9341_fill(0, 0, LCD_WIDTH, LCD_HEIGHT, 0); - for (y = 0; y < LCD_HEIGHT; y++) { - ili9341_fill(0, y, LCD_WIDTH, 1, RGB565(y, (y + 120) % 256, LCD_HEIGHT-y)); - } - break; - case 1: - ili9341_fill(0, 0, LCD_WIDTH, LCD_HEIGHT, 0); - for (y = 0; y < LCD_HEIGHT; y++) { - for (x = 0; x < LCD_WIDTH; x++) { - ili9341_pixel(x, y, (y<<8)|x); - } - } - break; - case 2: - //send_command16(0x55, 0xff00); - for (y = 0; y < LCD_HEIGHT; y++) { - for (x = 0; x < LCD_WIDTH; x++) { - ili9341_pixel(x, y, my_rand()%65536); - } - } - break; + ili9341_fill(0, 0, LCD_WIDTH, LCD_HEIGHT, 0); + for (y = 0; y < LCD_HEIGHT; y++) { + ili9341_fill(0, y, LCD_WIDTH, 1, RGB(LCD_HEIGHT-y, y, (y + 120) % 256)); + } + break; + case 1: + ili9341_fill(0, 0, LCD_WIDTH, LCD_HEIGHT, 0); + for (y = 0; y < LCD_HEIGHT; y++) { + for (x = 0; x < LCD_WIDTH; x++) { + ili9341_pixel(x, y, (y<<8)|x); + } + } + break; + case 2: + //send_command16(0x55, 0xff00); + ili9341_pixel(64, 64, 0xaa55); + break; #endif #if 1 case 3: @@ -719,34 +705,13 @@ ili9341_test(int mode) draw_grid(10, 8, 29, 29, 15, 0, 0xffff, 0); break; #endif -#if 1 - case 4: - ili9341_fill(0, 0, LCD_WIDTH, LCD_HEIGHT, 0); - x0 = (uint16_t) my_rand() % LCD_WIDTH; - y0 = (uint16_t) my_rand() % LCD_HEIGHT-4; - if ((x0>=LCD_WIDTH) || (y0>=LCD_HEIGHT)) { - return; - } - //r = (int) my_rand() % 100; - for (i=0; i<100; i++) { - x1 = (uint16_t) my_rand() % LCD_WIDTH; - y1 = (uint16_t) my_rand() % LCD_HEIGHT-4; - if ((x1>=LCD_WIDTH) || (y1>=LCD_HEIGHT)) { - return; - } - //c = RGB565(255,255,255); - //c = colormap[i % 6]; - c = (uint16_t) my_rand() % 65536; - ili9341_line(x0, y0, x1, y1, c); - ili9341_line(x0, y0+1, x1, y1+1, c); - //ili9341_line(x0, y0+2, x1, y1+2, c); - //ili9341_line(x0, y0+3, x1, y1+3, c); - x0 = x1; - y0 = y1; + case 4: + ili9341_line(0, 0, 15, 100, 0xffff); + ili9341_line(0, 0, 100, 100, 0xffff); + ili9341_line(0, 15, 100, 0, 0xffff); + ili9341_line(0, 100, 100, 0, 0xffff); + break; } - break; -#endif - } + chMtxUnlock(&mutex_ili9341); } #endif -#endif diff --git a/main.c b/main.c index 3df39d6e..9f87f91a 100644 --- a/main.c +++ b/main.c @@ -34,30 +34,30 @@ #include #include #include -#include -//#define ENABLED_DUMP +#define START_MIN 10000 +#define STOP_MAX 1500000000 static void apply_error_term_at(int i); static void apply_edelay_at(int i); static void cal_interpolate(int s); -void update_frequencies(void); -void set_frequencies(uint32_t start, uint32_t stop, int16_t points); - -bool sweep(bool break_on_operation); +static void update_frequencies(void); +static void set_frequencies(uint32_t start, uint32_t stop, int16_t points); +static bool sweep(bool break_on_operation); -static MUTEX_DECL(mutex); +mutex_t mutex_sweep; +mutex_t mutex_ili9341; #define DRIVE_STRENGTH_AUTO (-1) #define FREQ_HARMONICS (config.harmonic_freq_threshold) #define IS_HARMONIC_MODE(f) ((f) > FREQ_HARMONICS) -int32_t frequency_offset = 5000; -uint32_t frequency = 10000000; -int8_t drive_strength = DRIVE_STRENGTH_AUTO; +static int32_t frequency_offset = 5000; +static uint32_t frequency = 10000000; +static int8_t drive_strength = SI5351_CLK_DRIVE_STRENGTH_8MA; int8_t sweep_enabled = TRUE; -int8_t sweep_once = FALSE; -int8_t cal_auto_interpolate = TRUE; +static int8_t sweep_once = FALSE; +static int8_t cal_auto_interpolate = TRUE; uint16_t redraw_request = 0; // contains REDRAW_XXX flags int16_t vbat = 0; bool pll_lock_failed; @@ -66,67 +66,68 @@ bool pll_lock_failed; static THD_WORKING_AREA(waThread1, 640); static THD_FUNCTION(Thread1, arg) { - (void)arg; chRegSetThreadName("sweep"); while (1) { - bool completed = false; - if (sweep_enabled || sweep_once) { - chMtxLock(&mutex); - completed = sweep(true); - sweep_once = FALSE; - chMtxUnlock(&mutex); - } else { - __WFI(); - } + + bool completed = false; + if (sweep_enabled || sweep_once) { + chMtxLock(&mutex_sweep); + palClearPad(GPIOC, GPIOC_LED); // disable led and wait for voltage stabilization + chThdSleepMilliseconds(10); + + completed = sweep(true); + sweep_once = FALSE; + + // enable led + palSetPad(GPIOC, GPIOC_LED); + chMtxUnlock(&mutex_sweep); + } else { + __WFI(); + } - chMtxLock(&mutex); - ui_process(); + chMtxLock(&mutex_sweep); - if (sweep_enabled) { - if (vbat != -1) { - adc_stop(ADC1); - vbat = adc_vbat_read(ADC1); - touch_start_watchdog(); - draw_battery_status(); - } + ui_process(); - if (pll_lock_failed) { - draw_pll_lock_error(); - } + if (sweep_enabled) { + adc_stop(ADC1); + vbat = adc_vbat_read(ADC1); + touch_start_watchdog(); + draw_battery_status(); - /* calculate trace coordinates and plot only if scan completed */ - if (completed) { - plot_into_index(measured); - redraw_request |= REDRAW_CELLS; - } - } + // if (pll_lock_failed) { +// draw_pll_lock_error(); +// } - /* plot trace and other indications as raster */ - draw_all(completed); // flush markmap only if scan completed to prevent remaining traces - chMtxUnlock(&mutex); + /* calculate trace coordinates and plot only if scan completed */ + if (completed) { + plot_into_index(measured); + redraw_request |= REDRAW_CELLS; + } + } + /* plot trace and other indications as raster */ + draw_all(completed); // flush markmap only if scan completed to prevent remaining traces + chMtxUnlock(&mutex_sweep); } } -void -pause_sweep(void) +static void pause_sweep(void) { - sweep_enabled = FALSE; + sweep_enabled = FALSE; } -void -resume_sweep(void) +static void resume_sweep(void) { - sweep_enabled = TRUE; + sweep_enabled = TRUE; } -void -toggle_sweep(void) +void toggle_sweep(void) { sweep_enabled = !sweep_enabled; } -float bessel0(float x) { +static float bessel0(float x) { const float eps = 0.0001; float ret = 0; @@ -142,35 +143,35 @@ float bessel0(float x) { return ret; } -float kaiser_window(float k, float n, float beta) { +static float kaiser_window(float k, float n, float beta) { if (beta == 0.0) return 1.0; float r = (2 * k) / (n - 1) - 1; return bessel0(beta * sqrt(1 - r * r)) / bessel0(beta); } -static -void -transform_domain(void) +static void transform_domain(void) { - if ((domain_mode & DOMAIN_MODE) != DOMAIN_TIME) return; // nothing to do for freq domain - // use spi_buffer as temporary buffer - // and calculate ifft for time domain - float* tmp = (float*)spi_buffer; - - uint16_t window_size = SWEEP_POINTS, offset = 0; - uint8_t is_lowpass = FALSE; - switch (domain_mode & TD_FUNC) { - case TD_FUNC_BANDPASS: - offset = 0; - window_size = SWEEP_POINTS; - break; - case TD_FUNC_LOWPASS_IMPULSE: - case TD_FUNC_LOWPASS_STEP: - is_lowpass = TRUE; - offset = SWEEP_POINTS; - window_size = SWEEP_POINTS*2; - break; - } + if ((domain_mode & DOMAIN_MODE) != DOMAIN_TIME) return; // nothing to do for freq domain + + chMtxLock(&mutex_ili9341); // [protect spi_buffer] + // use spi_buffer as temporary buffer + // and calculate ifft for time domain + float* tmp = (float*)spi_buffer; + + uint8_t window_size = POINT_COUNT, offset = 0; + uint8_t is_lowpass = FALSE; + switch (domain_mode & TD_FUNC) { + case TD_FUNC_BANDPASS: + offset = 0; + window_size = POINT_COUNT; + break; + case TD_FUNC_LOWPASS_IMPULSE: + case TD_FUNC_LOWPASS_STEP: + is_lowpass = TRUE; + offset = POINT_COUNT; + window_size = POINT_COUNT * 2; + break; + } float beta = 0.0; switch (domain_mode & TD_WINDOW) { @@ -186,40 +187,44 @@ transform_domain(void) } - for (int ch = 0; ch < 2; ch++) { - memcpy(tmp, measured[ch], sizeof(measured[0])); - for (int i = 0; i < SWEEP_POINTS; i++) { - float w = kaiser_window(i+offset, window_size, beta); - tmp[i*2+0] *= w; - tmp[i*2+1] *= w; - } - for (int i = SWEEP_POINTS; i < FFT_SIZE; i++) { - tmp[i*2+0] = 0.0; - tmp[i*2+1] = 0.0; - } - if (is_lowpass) { - for (int i = 1; i < SWEEP_POINTS; i++) { - tmp[(FFT_SIZE-i)*2+0] = tmp[i*2+0]; - tmp[(FFT_SIZE-i)*2+1] = -tmp[i*2+1]; - } - } + for (int ch = 0; ch < 2; ch++) { + memcpy(tmp, measured[ch], sizeof(measured[0])); + for (int i = 0; i < POINT_COUNT; i++) { + float w = kaiser_window(i+offset, window_size, beta); + tmp[i*2+0] *= w; + tmp[i*2+1] *= w; + } +#if POINT_COUNT >= FFT_SIZE +#error CHECK ME +#endif + for (int i = POINT_COUNT; i < FFT_SIZE; i++) { + tmp[i*2+0] = 0.0; + tmp[i*2+1] = 0.0; + } + if (is_lowpass) { + for (int i = 1; i < POINT_COUNT; i++) { + tmp[(FFT_SIZE-i)*2+0] = tmp[i*2+0]; + tmp[(FFT_SIZE-i)*2+1] = -tmp[i*2+1]; + } + } - fft256_inverse((float(*)[2])tmp); - memcpy(measured[ch], tmp, sizeof(measured[0])); - for (int i = 0; i < SWEEP_POINTS; i++) { - measured[ch][i][0] /= (float)FFT_SIZE; - if (is_lowpass) { - measured[ch][i][1] = 0.0; - } else { - measured[ch][i][1] /= (float)FFT_SIZE; - } - } - if ( (domain_mode & TD_FUNC) == TD_FUNC_LOWPASS_STEP ) { - for (int i = 1; i < SWEEP_POINTS; i++) { - measured[ch][i][0] += measured[ch][i-1][0]; - } - } - } + fft256_inverse((float(*)[2])tmp); + memcpy(measured[ch], tmp, sizeof(measured[0])); + for (int i = 0; i < POINT_COUNT; i++) { + measured[ch][i][0] /= (float)FFT_SIZE; + if (is_lowpass) { + measured[ch][i][1] = 0.0; + } else { + measured[ch][i][1] /= (float)FFT_SIZE; + } + } + if ( (domain_mode & TD_FUNC) == TD_FUNC_LOWPASS_STEP ) { + for (int i = 1; i < POINT_COUNT; i++) { + measured[ch][i][0] += measured[ch][i-1][0]; + } + } + } + chMtxUnlock(&mutex_ili9341); // [/protect spi_buffer] } static void cmd_pause(BaseSequentialStream *chp, int argc, char *argv[]) @@ -237,10 +242,11 @@ static void cmd_resume(BaseSequentialStream *chp, int argc, char *argv[]) (void)argv; // restore frequencies array and cal + chMtxLock(&mutex_sweep); update_frequencies(); if (cal_auto_interpolate && (cal_status & CALSTAT_APPLY)) - cal_interpolate(lastsaveid); - + cal_interpolate(lastsaveid); + chMtxUnlock(&mutex_sweep); resume_sweep(); } @@ -269,33 +275,35 @@ static void cmd_reset(BaseSequentialStream *chp, int argc, char *argv[]) ; } -const int8_t gain_table[] = { - 0, // 0 ~ 300MHz - 40, // 300 ~ 600MHz - 50, // 600 ~ 900MHz - 75, // 900 ~ 1200MHz - 85, // 1200 ~ 1400MHz - 95 // 1400MHz ~ +static const int8_t gain_table[][2] = { + { 0, 0 }, // 1st: 0 ~ 300MHz + { 40, 40 }, // 2nd: 300 ~ 600MHz + { 50, 50 }, // 3rd: 600 ~ 900MHz + { 70, 70 }, // 4th: 900 ~ 1200MHz + { 80, 80 }, // 5th: 1200 ~ 1400MHz + { 90, 90 }, // 6th: 1400MHz ~ }; -#define DELAY_GAIN_CHANGE 10 - -static int -adjust_gain(int newfreq) +static int adjust_gain(int newfreq) { int delay = 0; int new_order = newfreq / FREQ_HARMONICS; int old_order = frequency / FREQ_HARMONICS; if (new_order != old_order) { - tlv320aic3204_set_gain(gain_table[new_order], gain_table[new_order]); - delay += DELAY_GAIN_CHANGE; + tlv320aic3204_set_gain(gain_table[new_order][0], gain_table[new_order][1]); + delay += 10; } return delay; } -int set_frequency(uint32_t freq) +static int set_frequency(uint32_t freq) { - int delay = adjust_gain(freq); + int delay = 0; + if (frequency == freq) + return delay; + + delay += adjust_gain(freq); + int8_t ds = drive_strength; if (ds == DRIVE_STRENGTH_AUTO) { ds = freq > FREQ_HARMONICS ? SI5351_CLK_DRIVE_STRENGTH_8MA : SI5351_CLK_DRIVE_STRENGTH_2MA; @@ -312,8 +320,10 @@ static void cmd_offset(BaseSequentialStream *chp, int argc, char *argv[]) chprintf(chp, "usage: offset {frequency offset(Hz)}\r\n"); return; } + chMtxLock(&mutex_sweep); frequency_offset = atoi(argv[0]); set_frequency(frequency); + chMtxUnlock(&mutex_sweep); } static void cmd_freq(BaseSequentialStream *chp, int argc, char *argv[]) @@ -324,10 +334,10 @@ static void cmd_freq(BaseSequentialStream *chp, int argc, char *argv[]) return; } pause_sweep(); - chMtxLock(&mutex); + chMtxLock(&mutex_sweep); freq = atoi(argv[0]); set_frequency(freq); - chMtxUnlock(&mutex); + chMtxUnlock(&mutex_sweep); } static void cmd_power(BaseSequentialStream *chp, int argc, char *argv[]) @@ -337,7 +347,9 @@ static void cmd_power(BaseSequentialStream *chp, int argc, char *argv[]) return; } drive_strength = atoi(argv[0]); + chMtxLock(&mutex_sweep); set_frequency(frequency); + chMtxUnlock(&mutex_sweep); } static void cmd_time(BaseSequentialStream *chp, int argc, char *argv[]) @@ -372,7 +384,9 @@ static void cmd_threshold(BaseSequentialStream *chp, int argc, char *argv[]) return; } value = atoi(argv[0]); + chMtxLock(&mutex_sweep); config.harmonic_freq_threshold = value; + chMtxUnlock(&mutex_sweep); } static void cmd_saveconfig(BaseSequentialStream *chp, int argc, char *argv[]) @@ -394,8 +408,9 @@ static void cmd_clearconfig(BaseSequentialStream *chp, int argc, char *argv[]) chprintf(chp, "Key unmatched.\r\n"); return; } - + chMtxLock(&mutex_sweep); // TODO: separate mutex? clear_all_config_prop_data(); + chMtxUnlock(&mutex_sweep); chprintf(chp, "Config and all cal data cleared.\r\n"); chprintf(chp, "Do reset manually to take effect. Then do touch cal and save.\r\n"); } @@ -404,27 +419,23 @@ static struct { int16_t rms[2]; int16_t ave[2]; int callback_count; - -#if 0 - int32_t last_counter_value; - int32_t interval_cycles; - int32_t busy_cycles; -#endif +// int32_t last_counter_value; +// int32_t interval_cycles; +// int32_t busy_cycles; } stat; -int16_t rx_buffer[AUDIO_BUFFER_LEN * 2]; +static int16_t rx_buffer[AUDIO_BUFFER_LEN * 2]; -#ifdef ENABLED_DUMP +#ifdef __DUMP_CMD__ int16_t dump_buffer[AUDIO_BUFFER_LEN]; int16_t dump_selection = 0; #endif -volatile int16_t wait_count = 0; +static volatile int16_t wait_count = 0; -float measured[2][SWEEP_POINTS][2]; +float measured[2][POINT_COUNT][2]; -static void -wait_dsp(int count) +static void wait_dsp(int count) { wait_count = count; //reset_dsp_accumerator(); @@ -432,9 +443,8 @@ wait_dsp(int count) __WFI(); } -#ifdef ENABLED_DUMP -static void -duplicate_buffer_to_dump(int16_t *p) +#ifdef __DUMP_CMD__ +static void duplicate_buffer_to_dump(int16_t *p) { if (dump_selection == 1) p = samp_buf; @@ -444,10 +454,9 @@ duplicate_buffer_to_dump(int16_t *p) } #endif -void i2s_end_callback(I2SDriver *i2sp, size_t offset, size_t n) +static void i2s_end_callback(I2SDriver *i2sp, size_t offset, size_t n) { - // #if PORT_SUPPORTS_RT -#if 0 +#if PORT_SUPPORTS_RT int32_t cnt_s = port_rt_get_counter_value(); int32_t cnt_e; #endif @@ -458,59 +467,68 @@ void i2s_end_callback(I2SDriver *i2sp, size_t offset, size_t n) if (wait_count > 0) { if (wait_count == 1) dsp_process(p, n); -#ifdef ENABLED_DUMP +#ifdef __DUMP_CMD__ duplicate_buffer_to_dump(p); #endif --wait_count; } -//#if PORT_SUPPORTS_RT -#if 0 +#if PORT_SUPPORTS_RT cnt_e = port_rt_get_counter_value(); - stat.interval_cycles = cnt_s - stat.last_counter_value; - stat.busy_cycles = cnt_e - cnt_s; - stat.last_counter_value = cnt_s; +// stat.interval_cycles = cnt_s - stat.last_counter_value; +// stat.busy_cycles = cnt_e - cnt_s; +// stat.last_counter_value = cnt_s; #endif stat.callback_count++; } static const I2SConfig i2sconfig = { - NULL, // TX Buffer - rx_buffer, // RX Buffer - AUDIO_BUFFER_LEN * 2, - NULL, // tx callback - i2s_end_callback, // rx callback - 0, // i2scfgr - 2 // i2spr + .tx_buffer = NULL, // TX Buffer + .rx_buffer = rx_buffer, // RX Buffer + .size = AUDIO_BUFFER_LEN * 2, + .tx_end_cb = NULL, // tx callback + .rx_end_cb = i2s_end_callback, // rx callback + .i2scfgr = 0, // i2scfgr + .i2spr = 2 // i2spr }; static void cmd_data(BaseSequentialStream *chp, int argc, char *argv[]) { - int i; - int sel = 0; - - if (argc == 1) - sel = atoi(argv[0]); - if (sel == 0 || sel == 1) { - chMtxLock(&mutex); - for (i = 0; i < sweep_points; i++) { - if (frequencies[i] != 0) - chprintf(chp, "%f %f\r\n", measured[sel][i][0], measured[sel][i][1]); - } - chMtxUnlock(&mutex); - } else if (sel >= 2 && sel < 7) { - chMtxLock(&mutex); - for (i = 0; i < sweep_points; i++) { - if (frequencies[i] != 0) - chprintf(chp, "%f %f\r\n", cal_data[sel-2][i][0], cal_data[sel-2][i][1]); + int sel = 0; + if (argc == 1) + sel = atoi(argv[0]); + if (sel < 0 || sel > 6) { + chprintf(chp, "usage: data [array]\r\n"); + } else { + if (sel > 1) + sel = sel-2; + chMtxLock(&mutex_sweep); + for (int i = 0; i < sweep_points; i++) { +#ifndef __USE_STDIO__ + // WARNING: chprintf doesn't support proper float formatting + chprintf(chp, "%f %f\r\n", measured[sel][i][0], measured[sel][i][1]); +#else + // printf floating point losslessly: float="%.9g", double="%.17g" + char tmpbuf[20]; + int leng; + leng = snprintf(tmpbuf, sizeof(tmpbuf), "%.9g", measured[sel][i][0]); + for (int j=0; j < leng; j++) { + streamPut(chp, (uint8_t)tmpbuf[j]); + } + streamPut(chp, (uint8_t)' '); + leng = snprintf(tmpbuf, sizeof(tmpbuf), "%.9g", measured[sel][i][1]); + for (int j=0; j < leng; j++) { + streamPut(chp, (uint8_t)tmpbuf[j]); + } + streamPut(chp, (uint8_t)'\r'); + streamPut(chp, (uint8_t)'\n'); +#endif // __USE_STDIO__ + } + chMtxUnlock(&mutex_sweep); } - chMtxUnlock(&mutex); - } else { - chprintf(chp, "usage: data [array]\r\n"); - } } -#ifdef ENABLED_DUMP +#ifdef __DUMP_CMD__ static void cmd_dump(BaseSequentialStream *chp, int argc, char *argv[]) { int i, j; @@ -537,10 +555,8 @@ static void cmd_capture(BaseSequentialStream *chp, int argc, char *argv[]) { // read pixel count at one time (PART*2 bytes required for read buffer) #define PART LCD_WIDTH - (void)argc; - (void)argv; - chMtxLock(&mutex); + chMtxLock(&mutex_ili9341); // [capture display + spi_buffer] // use uint16_t spi_buffer[1024] (defined in ili9341) for read buffer uint16_t *buf = &spi_buffer[0]; @@ -563,25 +579,23 @@ static void cmd_capture(BaseSequentialStream *chp, int argc, char *argv[]) } //*/ - chMtxUnlock(&mutex); + chMtxUnlock(&mutex_ili9341); // [/capture display + spi_buffer] } -#if 0 -static void cmd_gamma(BaseSequentialStream *chp, int argc, char *argv[]) -{ - float gamma[2]; - (void)argc; - (void)argv; - - pause_sweep(); - chMtxLock(&mutex); - wait_dsp(4); - calculate_gamma(gamma); - chMtxUnlock(&mutex); - - chprintf(chp, "%d %d\r\n", gamma[0], gamma[1]); -} -#endif +//static void cmd_gamma(BaseSequentialStream *chp, int argc, char *argv[]) +//{ +// float gamma[2]; +// (void)argc; +// (void)argv; +// +// pause_sweep(); +// chMtxLock(&mutex_sweep); +// wait_dsp(4); +// calculate_gamma(gamma); +// chMtxUnlock(&mutex_sweep); +// +// chprintf(chp, "%d %d\r\n", gamma[0], gamma[1]); +//} static void (*sample_func)(float *gamma) = calculate_gamma; @@ -606,11 +620,11 @@ static void cmd_sample(BaseSequentialStream *chp, int argc, char *argv[]) #if 0 int32_t frequency0 = 1000000; int32_t frequency1 = 300000000; -int16_t sweep_points = SWEEP_POINTS; +int16_t sweep_points = POINT_COUNT; -uint32_t frequencies[SWEEP_POINTS]; +uint32_t frequencies[POINT_COUNT]; uint16_t cal_status; -float cal_data[5][SWEEP_POINTS][2]; +float cal_data[5][POINT_COUNT][2]; #endif config_t config = { @@ -619,42 +633,45 @@ config_t config = { .grid_color = 0x1084, .menu_normal_color = 0xffff, .menu_active_color = 0x7777, - .trace_color = { RGB565(0,255,255), RGB565(255,0,40), RGB565(0,0,255), RGB565(50,255,0) }, -// .touch_cal = { 693, 605, 124, 171 }, // 2.4 inch LCD panel - .touch_cal = { 338, 522, 153, 192 }, // 2.8 inch LCD panel + .trace_color = { RGBHEX(0xffe31f), RGBHEX(0x00bfe7), RGBHEX(0x1fe300), RGBHEX(0xe7079f) }, +#if !defined(ST7796S) + .touch_cal = { 370, 540, 154, 191 }, //{ 620, 600, 160, 190 }, +#else + .touch_cal = { 332, 531, 107, 145 }, //4.0" LCD +#endif .default_loadcal = 0, .harmonic_freq_threshold = 300000000, + .vbat_offset = 100, .checksum = 0 }; properties_t current_props = { - /* magic */ CONFIG_MAGIC, - /* frequency0 */ 50000, // start = 50kHz - /* frequency1 */ 900000000, // end = 900MHz - /* sweep_points */ SWEEP_POINTS, - /* cal_status */ 0, - /* frequencies */ {}, - /* cal_data */ {}, - /* electrical_delay */ 0, - /* trace[4] */ + .magic = CONFIG_MAGIC, + ._frequency0 = 50000, // start = 50kHz + ._frequency1 = 900000000, // end = 900MHz + ._sweep_points = POINT_COUNT, + ._cal_status = 0, + //._frequencies = {}, + //._cal_data = {}, + ._electrical_delay = 0, + ._trace = /*[4] */ {/*enable, type, channel, polar, scale, refpos*/ { 1, TRC_LOGMAG, 0, 0, 1.0, 7.0 }, { 1, TRC_LOGMAG, 1, 0, 1.0, 7.0 }, { 1, TRC_SMITH, 0, 1, 1.0, 0.0 }, { 1, TRC_PHASE, 1, 0, 1.0, 4.0 } }, - /* markers[4] */ { + ._markers = /*[4] */ { { 1, 30, 0 }, { 0, 40, 0 }, { 0, 60, 0 }, { 0, 80, 0 } }, - /* active_marker */ 0, - /* domain_mode */ 0, - /* velocity_factor */ 70, - /* checksum */ 0 + ._active_marker = 0, + ._domain_mode = 0, + ._velocity_factor = 70, + .checksum = 0 }; properties_t *active_props = ¤t_props; -void -ensure_edit_config(void) +static void ensure_edit_config(void) { if (active_props == ¤t_props) return; @@ -665,40 +682,29 @@ ensure_edit_config(void) cal_status = 0; } -#define DELAY_CHANNEL_CHANGE 3 - // main loop for measurement -bool sweep(bool break_on_operation) +static bool sweep(bool break_on_operation) { pll_lock_failed = false; - int i; - - for (i = 0; i < sweep_points; i++) { - int delay = set_frequency(frequencies[i]); - tlv320aic3204_select(0); // CH0:REFLECT - wait_dsp(delay); - - // blink LED while scanning - #if !defined(LED_OFF) - palClearPad(GPIOC, GPIOC_LED); - #endif + for (int i = 0; i < sweep_points; i++) { + int delay = set_frequency(frequencies[i]); + delay = delay < 3 ? 3 : delay; + delay = delay > 8 ? 8 : delay; + + tlv320aic3204_select(0); // CH0:REFLECT + wait_dsp(delay); - /* calculate reflection coeficient */ - (*sample_func)(measured[0][i]); + /* calculate reflection coeficient */ + (*sample_func)(measured[0][i]); - tlv320aic3204_select(1); // CH1:TRANSMISSION - wait_dsp(DELAY_CHANNEL_CHANGE); + tlv320aic3204_select(1); // CH1:TRANSMISSION + wait_dsp(delay); - /* calculate transmission coeficient */ - (*sample_func)(measured[1][i]); + /* calculate transmission coeficient */ + (*sample_func)(measured[1][i]); - #if !defined(LED_OFF) - // blink LED while scanning - palSetPad(GPIOC, GPIOC_LED); - #endif - - if (cal_status & CALSTAT_APPLY) - apply_error_term_at(i); + if (cal_status & CALSTAT_APPLY) + apply_error_term_at(i); if (electrical_delay != 0) apply_edelay_at(i); @@ -712,13 +718,102 @@ bool sweep(bool break_on_operation) return true; } +#ifdef __SCANRAW_CMD__ +static void measure_gamma_avg(uint8_t channel, uint32_t freq, uint16_t avg_count, float* gamma) { + int delay = set_frequency(freq); + delay = delay < 3 ? 3 : delay; + delay = delay > 8 ? 8 : delay; + + tlv320aic3204_select(channel); + wait_dsp(delay); + + gamma[0] = 0.0; + gamma[1] = 0.0; + float gamma_acc[2] = { 0, 0 }; + for (int j = 0; j < avg_count; j++) { + + wait_dsp(1); + /* calculate reflection/transmission coeficient */ + (*sample_func)(gamma); + + if (avg_count == 1) break; + gamma_acc[0] += gamma[0]; + gamma_acc[1] += gamma[1]; + } + if (avg_count > 1) { + gamma[0] = gamma_acc[0] / avg_count; + gamma[1] = gamma_acc[1] / avg_count; + } +} + +static void cmd_scanraw(BaseSequentialStream *chp, int argc, char *argv[]) +{ + int32_t chan, freq, step, count, avg_count; + if (argc != 4 && argc != 5) { + chprintf(chp, "usage: scanraw {channel(0|1)} {start(Hz)} {stEp(Hz)} {count} [average]\r\n"); + return; + } + chan = atoi(argv[0]); + freq = atoi(argv[1]); + step = atoi(argv[2]); + count = atoi(argv[3]); + avg_count = 1; + if (argc == 5) + avg_count = atoi(argv[4]); + if (chan < 0 || chan > 1) { + chprintf(chp, "error: invalid channel\r\n"); + return; + } + if (freq < START_MIN || + (freq+(uint64_t)step*count) < START_MIN || + (freq+(uint64_t)step*count) > STOP_MAX) { + chprintf(chp, "error: invalid frequency range\r\n"); + return; + } + if (avg_count < 1 || avg_count > 1000) { + chprintf(chp, "error: invalid average\r\n"); + return; + } + + chMtxLock(&mutex_sweep); + palClearPad(GPIOC, GPIOC_LED); // disable led and wait for voltage stabilization + chThdSleepMilliseconds(10); + + for (int i = 0; i < count; i++, freq += step) { + float gamma[2]; + measure_gamma_avg(chan, freq, avg_count, gamma); + +#ifndef __USE_STDIO__ + // WARNING: chprintf doesn't support proper float formatting + chprintf(chp, "%f\t%f\r\n", gamma[0], gamma[1]); +#else + // printf floating point losslessly: float="%.9g", double="%.17g" + char tmpbuf[20]; + int leng; + leng = snprintf(tmpbuf, sizeof(tmpbuf), "%.9g", gamma[0]); + for (int j=0; j < leng; j++) { + streamPut(chp, (uint8_t)tmpbuf[j]); + } + streamPut(chp, (uint8_t)'\t'); + leng = snprintf(tmpbuf, sizeof(tmpbuf), "%.9g", gamma[1]); + for (int j=0; j < leng; j++) { + streamPut(chp, (uint8_t)tmpbuf[j]); + } + streamPut(chp, (uint8_t)'\r'); + streamPut(chp, (uint8_t)'\n'); +#endif // __USE_STDIO__ + } + chMtxUnlock(&mutex_sweep); +} +#endif //__SCANRAW_CMD__ + static void cmd_scan(BaseSequentialStream *chp, int argc, char *argv[]) { int32_t start, stop; int16_t points = sweep_points; if (argc != 2 && argc != 3) { - chprintf(chp, "usage: sweep {start(Hz)} {stop(Hz)} [points]\r\n"); + chprintf(chp, "usage: scan {start(Hz)} {stop(Hz)} [points]\r\n"); return; } @@ -737,25 +832,24 @@ static void cmd_scan(BaseSequentialStream *chp, int argc, char *argv[]) } pause_sweep(); - chMtxLock(&mutex); + chMtxLock(&mutex_sweep); set_frequencies(start, stop, points); if (cal_auto_interpolate && (cal_status & CALSTAT_APPLY)) cal_interpolate(lastsaveid); sweep_once = TRUE; - chMtxUnlock(&mutex); + chMtxUnlock(&mutex_sweep); // wait finishing sweep while (sweep_once) chThdSleepMilliseconds(10); } -static void -update_marker_index(void) +static void update_marker_index(void) { int m; int i; - for (m = 0; m < 4; m++) { + for (m = 0; m < MARKER_COUNT; m++) { if (!markers[m].enabled) continue; uint32_t f = markers[m].frequency; @@ -781,23 +875,24 @@ update_marker_index(void) } } -void -set_frequencies(uint32_t start, uint32_t stop, int16_t points) +static void set_frequencies(uint32_t start, uint32_t stop, int16_t points) { - int i; - float span = stop - start; + chMtxLock(&mutex_sweep); + uint32_t i; + uint32_t span = stop - start; for (i = 0; i < points; i++) { - float offset = i * span / (float)(points - 1); + uint32_t offset = (uint32_t)((i * (uint64_t)span) / (points - 1)); frequencies[i] = start + (uint32_t)offset; } // disable at out of sweep range for (; i < sweep_points; i++) frequencies[i] = 0; + chMtxUnlock(&mutex_sweep); } -void -update_frequencies(void) +static void update_frequencies(void) { + chMtxLock(&mutex_sweep); uint32_t start, stop; if (frequency1 > 0) { start = frequency0; @@ -816,10 +911,10 @@ update_frequencies(void) // set grid layout update_grid(); + chMtxUnlock(&mutex_sweep); } -void -freq_mode_startstop(void) +static void freq_mode_startstop(void) { if (frequency1 <= 0) { int center = frequency0; @@ -830,8 +925,7 @@ freq_mode_startstop(void) } } -void -freq_mode_centerspan(void) +static void freq_mode_centerspan(void) { if (frequency1 > 0) { int start = frequency0; @@ -843,102 +937,98 @@ freq_mode_centerspan(void) } -#define START_MIN 50000 -//#define STOP_MAX 900000000 -#define STOP_MAX 1500000000 - -void -set_sweep_frequency(int type, int32_t freq) +void set_sweep_frequency(int type, int32_t freq) { + chMtxLock(&mutex_sweep); + int32_t center; + int32_t span; int cal_applied = cal_status & CALSTAT_APPLY; - // negative value indicate overflow, do nothing - if (freq < 0) - return; switch (type) { case ST_START: + ensure_edit_config(); freq_mode_startstop(); if (freq < START_MIN) freq = START_MIN; if (freq > STOP_MAX) freq = STOP_MAX; - if (frequency0 != freq) { - ensure_edit_config(); - frequency0 = freq; - // if start > stop then make start = stop - if (frequency1 < freq) - frequency1 = freq; - update_frequencies(); - } + frequency0 = freq; + // if start > stop then make start = stop + if (frequency1 < freq) + frequency1 = freq; + update_frequencies(); break; case ST_STOP: + ensure_edit_config(); freq_mode_startstop(); if (freq > STOP_MAX) freq = STOP_MAX; if (freq < START_MIN) freq = START_MIN; - if (frequency1 != freq) { - ensure_edit_config(); - frequency1 = freq; - // if start > stop then make start = stop - if (frequency0 > freq) - frequency0 = freq; - update_frequencies(); - } + frequency1 = freq; + // if start > stop then make start = stop + if (frequency0 > freq) + frequency0 = freq; + update_frequencies(); break; case ST_CENTER: ensure_edit_config(); freq_mode_centerspan(); - if (frequency0 != freq) { - ensure_edit_config(); - frequency0 = freq; - int center = frequency0; - int span = -frequency1; - if (center-span/2 < START_MIN) { - span = (center - START_MIN) * 2; - frequency1 = -span; - } - if (center+span/2 > STOP_MAX) { - span = (STOP_MAX - center) * 2; - frequency1 = -span; - } - update_frequencies(); + if (freq > STOP_MAX) + freq = STOP_MAX; + if (freq < START_MIN) + freq = START_MIN; + frequency0 = freq; + center = frequency0; + span = -frequency1; + if (center-span/2 < START_MIN) { + span = (center - START_MIN) * 2; + frequency1 = -span; + } + if (center+span/2 > STOP_MAX) { + span = (STOP_MAX - center) * 2; + frequency1 = -span; } + update_frequencies(); break; case ST_SPAN: + ensure_edit_config(); freq_mode_centerspan(); - if (frequency1 != -freq) { - ensure_edit_config(); - frequency1 = -freq; - int center = frequency0; - int span = -frequency1; - if (center-span/2 < START_MIN) { - center = START_MIN + span/2; - frequency0 = center; - } - if (center+span/2 > STOP_MAX) { - center = STOP_MAX - span/2; - frequency0 = center; - } - update_frequencies(); + if (freq > STOP_MAX-START_MIN) + freq = STOP_MAX-START_MIN; + if (freq < 0) + freq = 0; + frequency1 = -freq; + center = frequency0; + span = -frequency1; + if (center-span/2 < START_MIN) { + center = START_MIN + span/2; + frequency0 = center; } + if (center+span/2 > STOP_MAX) { + center = STOP_MAX - span/2; + frequency0 = center; + } + update_frequencies(); break; case ST_CW: + ensure_edit_config(); freq_mode_centerspan(); - if (frequency0 != freq || frequency1 != 0) { - ensure_edit_config(); - frequency0 = freq; - frequency1 = 0; - update_frequencies(); - } + if (freq > STOP_MAX) + freq = STOP_MAX; + if (freq < START_MIN) + freq = START_MIN; + frequency0 = freq; + frequency1 = 0; + update_frequencies(); break; } if (cal_auto_interpolate && cal_applied) cal_interpolate(lastsaveid); + chMtxUnlock(&mutex_sweep); } -uint32_t -get_sweep_frequency(int type) +uint32_t get_sweep_frequency(int type) { if (frequency1 >= 0) { switch (type) { @@ -1010,8 +1100,7 @@ static void cmd_sweep(BaseSequentialStream *chp, int argc, char *argv[]) } -static void -eterm_set(int term, float re, float im) +static void eterm_set(int term, float re, float im) { int i; for (i = 0; i < sweep_points; i++) { @@ -1020,19 +1109,18 @@ eterm_set(int term, float re, float im) } } -static void -eterm_copy(int dst, int src) +static void eterm_copy(int dst, int src) { memcpy(cal_data[dst], cal_data[src], sizeof cal_data[dst]); } -const struct open_model { - float c0; - float c1; - float c2; - float c3; -} open_model = { 50, 0, -300, 27 }; +//const struct open_model { +// float c0; +// float c1; +// float c2; +// float c3; +//} open_model = { 50, 0, -300, 27 }; #if 0 static void @@ -1052,8 +1140,7 @@ adjust_ed(void) } #endif -static void -eterm_calc_es(void) +static void eterm_calc_es(void) { int i; for (i = 0; i < sweep_points; i++) { @@ -1087,8 +1174,7 @@ eterm_calc_es(void) cal_status |= CALSTAT_ES; } -static void -eterm_calc_er(int sign) +static void eterm_calc_er(int sign) { int i; for (i = 0; i < sweep_points; i++) { @@ -1116,8 +1202,7 @@ eterm_calc_er(int sign) } // CAUTION: Et is inversed for efficiency -static void -eterm_calc_et(void) +static void eterm_calc_et(void) { int i; for (i = 0; i < sweep_points; i++) { @@ -1168,7 +1253,7 @@ void apply_error_term(void) } #endif -void apply_error_term_at(int i) +static void apply_error_term_at(int i) { // S11m' = S11m - Ed // S11a = S11m' / (Er + Es S11m') @@ -1212,11 +1297,10 @@ static void apply_edelay_at(int i) measured[1][i][1] = imag * c + real * s; } -void -cal_collect(int type) +void cal_collect(int type) { + chMtxLock(&mutex_sweep); ensure_edit_config(); - chMtxLock(&mutex); switch (type) { case CAL_LOAD: @@ -1246,13 +1330,13 @@ cal_collect(int type) memcpy(cal_data[CAL_ISOLN], measured[1], sizeof measured[0]); break; } - chMtxUnlock(&mutex); redraw_request |= REDRAW_CAL_STATUS; + chMtxUnlock(&mutex_sweep); } -void -cal_done(void) +void cal_done(void) { + chMtxLock(&mutex_sweep); ensure_edit_config(); if (!(cal_status & CALSTAT_LOAD)) eterm_set(ETERM_ED, 0.0, 0.0); @@ -1283,16 +1367,19 @@ cal_done(void) cal_status |= CALSTAT_APPLY; redraw_request |= REDRAW_CAL_STATUS; + chMtxUnlock(&mutex_sweep); } -void -cal_interpolate(int s) +static void cal_interpolate(int s) { + chMtxLock(&mutex_sweep); const properties_t *src = caldata_ref(s); int i, j; int eterm; - if (src == NULL) + if (src == NULL) { + chMtxUnlock(&mutex_sweep); return; + } ensure_edit_config(); @@ -1346,7 +1433,8 @@ cal_interpolate(int s) } cal_status |= src->_cal_status | CALSTAT_APPLY | CALSTAT_INTERPOLATED; - redraw_request |= REDRAW_CAL_STATUS; + redraw_request |= REDRAW_CAL_STATUS; + chMtxUnlock(&mutex_sweep); } static void cmd_cal(BaseSequentialStream *chp, int argc, char *argv[]) @@ -1378,16 +1466,26 @@ static void cmd_cal(BaseSequentialStream *chp, int argc, char *argv[]) cal_done(); return; } else if (strcmp(cmd, "on") == 0) { + if (cal_status & CALSTAT_APPLY) + return; + chMtxLock(&mutex_sweep); cal_status |= CALSTAT_APPLY; redraw_request |= REDRAW_CAL_STATUS; + chMtxUnlock(&mutex_sweep); return; } else if (strcmp(cmd, "off") == 0) { + if (!(cal_status & CALSTAT_APPLY)) + return; + chMtxLock(&mutex_sweep); cal_status &= ~CALSTAT_APPLY; redraw_request |= REDRAW_CAL_STATUS; + chMtxUnlock(&mutex_sweep); return; } else if (strcmp(cmd, "reset") == 0) { + chMtxLock(&mutex_sweep); cal_status = 0; redraw_request |= REDRAW_CAL_STATUS; + chMtxUnlock(&mutex_sweep); return; } else if (strcmp(cmd, "data") == 0) { chprintf(chp, "%f %f\r\n", cal_data[CAL_LOAD][0][0], cal_data[CAL_LOAD][0][1]); @@ -1411,48 +1509,36 @@ static void cmd_cal(BaseSequentialStream *chp, int argc, char *argv[]) static void cmd_save(BaseSequentialStream *chp, int argc, char *argv[]) { - (void)chp; - - if (argc != 1) - goto usage; - - int id = atoi(argv[0]); - if (id < 0 || id >= SAVEAREA_MAX) - goto usage; - caldata_save(id); - redraw_request |= REDRAW_CAL_STATUS; - return; - - usage: - chprintf(chp, "save {id}\r\n"); + int id = argc == 1 ? atoi(argv[0]) : -1; + if (argc != 1 || id < 0 || id >= SAVEAREA_MAX) { + chprintf(chp, "save {id}\r\n"); + return; + } + chMtxLock(&mutex_sweep); + caldata_save(id); + redraw_request |= REDRAW_CAL_STATUS; + chMtxUnlock(&mutex_sweep); } static void cmd_recall(BaseSequentialStream *chp, int argc, char *argv[]) { - (void)chp; - if (argc != 1) - goto usage; - - int id = atoi(argv[0]); - if (id < 0 || id >= SAVEAREA_MAX) - goto usage; - - pause_sweep(); - chMtxLock(&mutex); - if (caldata_recall(id) == 0) { - // success - update_frequencies(); - redraw_request |= REDRAW_CAL_STATUS; - } - chMtxUnlock(&mutex); - resume_sweep(); - return; - - usage: - chprintf(chp, "recall {id}\r\n"); + int id = argc == 1 ? atoi(argv[0]) : -1; + if (argc != 1 || id < 0 || id >= SAVEAREA_MAX) { + chprintf(chp, "recall {id}\r\n"); + return; + } + pause_sweep(); + chMtxLock(&mutex_sweep); + if (caldata_recall(id) == 0) { + // success + update_frequencies(); + redraw_request |= REDRAW_CAL_STATUS; + } + chMtxUnlock(&mutex_sweep); + resume_sweep(); } -const struct { +static const struct { const char *name; uint16_t refpos; float scale_unit; @@ -1470,12 +1556,11 @@ const struct { { "X", 4, 100 } }; -const char * const trc_channel_name[] = { +static const char * const trc_channel_name[] = { "CH0", "CH1" }; -const char * -get_trace_typename(int t) +const char* get_trace_typename(int t) { return trace_info[trace[t].type].name; } @@ -1541,19 +1626,18 @@ float get_trace_refpos(int t) return trace[t].refpos; } -float -my_atof(const char *p) +double my_atof(const char *p) { int neg = FALSE; if (*p == '-') neg = TRUE; if (*p == '-' || *p == '+') p++; - float x = atoi(p); + double x = atoi(p); while (isdigit((int)*p)) p++; if (*p == '.') { - float d = 1.0f; + double d = 1.0f; p++; while (isdigit((int)*p)) { d /= 10; @@ -1692,90 +1776,88 @@ static void cmd_edelay(BaseSequentialStream *chp, int argc, char *argv[]) static void cmd_marker(BaseSequentialStream *chp, int argc, char *argv[]) { - int t; - if (argc == 0) { - for (t = 0; t < 4; t++) { - if (markers[t].enabled) { - chprintf(chp, "%d %d %d\r\n", t+1, markers[t].index, markers[t].frequency); - } - } - return; - } - if (strcmp(argv[0], "off") == 0) { - active_marker = -1; - for (t = 0; t < 4; t++) - markers[t].enabled = FALSE; - redraw_request |= REDRAW_MARKER; - return; - } - - t = atoi(argv[0])-1; - if (t < 0 || t >= 4) - goto usage; - if (argc == 1) { - chprintf(chp, "%d %d %d\r\n", t+1, markers[t].index, frequency); - active_marker = t; - // select active marker - markers[t].enabled = TRUE; - redraw_request |= REDRAW_MARKER; - return; - } - if (argc > 1) { - if (strcmp(argv[1], "off") == 0) { - markers[t].enabled = FALSE; - if (active_marker == t) + chMtxLock(&mutex_sweep); + if (argc == 0) { + for (int t = 0; t < MARKER_COUNT; t++) { + if (markers[t].enabled) { + chprintf(chp, "%d %d %d\r\n", t+1, markers[t].index, markers[t].frequency); + } + } + chMtxUnlock(&mutex_sweep); + return; + } + if (strcmp(argv[0], "off") == 0) { active_marker = -1; - redraw_request |= REDRAW_MARKER; - } else if (strcmp(argv[1], "on") == 0) { - markers[t].enabled = TRUE; - active_marker = t; - redraw_request |= REDRAW_MARKER; - } else { - // select active marker and move to index - markers[t].enabled = TRUE; - int index = atoi(argv[1]); - markers[t].index = index; - markers[t].frequency = frequencies[index]; - active_marker = t; - redraw_request |= REDRAW_MARKER; + for (int t = 0; t < MARKER_COUNT; t++) + markers[t].enabled = FALSE; + redraw_request |= REDRAW_MARKER; + chMtxUnlock(&mutex_sweep); + return; } - } - return; - usage: - chprintf(chp, "marker [n] [off|{index}]\r\n"); + int t = atoi(argv[0]) - 1; + if (t < 0 || t >= MARKER_COUNT) { + chprintf(chp, "marker [n] [off|{index}]\r\n"); + chMtxUnlock(&mutex_sweep); + return; + } + if (argc == 1) { + chprintf(chp, "%d %d %d\r\n", t+1, markers[t].index, frequency); + active_marker = t; + // select active marker + markers[t].enabled = TRUE; + redraw_request |= REDRAW_MARKER; + } + else if (argc > 1) { + if (strcmp(argv[1], "off") == 0) { + markers[t].enabled = FALSE; + if (active_marker == t) { + active_marker = -1; + } + redraw_request |= REDRAW_MARKER; + } else if (strcmp(argv[1], "on") == 0) { + markers[t].enabled = TRUE; + active_marker = t; + redraw_request |= REDRAW_MARKER; + } else { + // select active marker and move to index + int index = atoi(argv[1]); + if (index < 0 || index >= POINT_COUNT) { + chprintf(chp, "error: invalid index\r\n"); + } else { + markers[t].index = index; + markers[t].frequency = frequencies[index]; + markers[t].enabled = TRUE; + previous_marker = -1; + active_marker = t; + redraw_request |= REDRAW_MARKER; + } + } + } + chMtxUnlock(&mutex_sweep); } static void cmd_touchcal(BaseSequentialStream *chp, int argc, char *argv[]) { - (void)argc; - (void)argv; - //extern int16_t touch_cal[4]; - int i; - - chMtxLock(&mutex); + chMtxLock(&mutex_sweep); chprintf(chp, "first touch upper left, then lower right..."); touch_cal_exec(); chprintf(chp, "done\r\n"); chprintf(chp, "touch cal params: "); - for (i = 0; i < 4; i++) { + for (int i = 0; i < 4; i++) { chprintf(chp, "%d ", config.touch_cal[i]); } chprintf(chp, "\r\n"); - chMtxUnlock(&mutex); + chMtxUnlock(&mutex_sweep); } static void cmd_touchtest(BaseSequentialStream *chp, int argc, char *argv[]) { - (void)chp; - (void)argc; - (void)argv; - chMtxLock(&mutex); + chMtxLock(&mutex_sweep); do { touch_draw_test(); } while(argc); - chMtxUnlock(&mutex); - + chMtxUnlock(&mutex_sweep); } static void cmd_frequencies(BaseSequentialStream *chp, int argc, char *argv[]) @@ -1790,24 +1872,20 @@ static void cmd_frequencies(BaseSequentialStream *chp, int argc, char *argv[]) } } -static void -set_domain_mode(int mode) // accept DOMAIN_FREQ or DOMAIN_TIME +static void set_domain_mode(int mode) // accept DOMAIN_FREQ or DOMAIN_TIME { if (mode != (domain_mode & DOMAIN_MODE)) { domain_mode = (domain_mode & ~DOMAIN_MODE) | (mode & DOMAIN_MODE); redraw_request |= REDRAW_FREQUENCY; - uistat.lever_mode = LM_MARKER; } } -static void -set_timedomain_func(int func) // accept TD_FUNC_LOWPASS_IMPULSE, TD_FUNC_LOWPASS_STEP or TD_FUNC_BANDPASS +static void set_timedomain_func(int func) // accept TD_FUNC_LOWPASS_IMPULSE, TD_FUNC_LOWPASS_STEP or TD_FUNC_BANDPASS { domain_mode = (domain_mode & ~TD_FUNC) | (func & TD_FUNC); } -static void -set_timedomain_window(int func) // accept TD_WINDOW_MINIMUM/TD_WINDOW_NORMAL/TD_WINDOW_MAXIMUM +static void set_timedomain_window(int func) // accept TD_WINDOW_MINIMUM/TD_WINDOW_NORMAL/TD_WINDOW_MAXIMUM { domain_mode = (domain_mode & ~TD_WINDOW) | (func & TD_WINDOW); } @@ -1849,59 +1927,56 @@ static void cmd_transform(BaseSequentialStream *chp, int argc, char *argv[]) static void cmd_test(BaseSequentialStream *chp, int argc, char *argv[]) { - (void)chp; - (void)argc; - (void)argv; - + chMtxLock(&mutex_sweep); #if 0 - int i; - for (i = 0; i < 100; i++) { - palClearPad(GPIOC, GPIOC_LED); - set_frequency(10000000); - palSetPad(GPIOC, GPIOC_LED); - chThdSleepMilliseconds(50); - - palClearPad(GPIOC, GPIOC_LED); - set_frequency(90000000); - palSetPad(GPIOC, GPIOC_LED); - chThdSleepMilliseconds(50); - } + for (int i = 0; i < 100; i++) { + palClearPad(GPIOC, GPIOC_LED); + set_frequency(10000000); + palSetPad(GPIOC, GPIOC_LED); + chThdSleepMilliseconds(50); + + palClearPad(GPIOC, GPIOC_LED); + set_frequency(90000000); + palSetPad(GPIOC, GPIOC_LED); + chThdSleepMilliseconds(50); + } #endif #if 0 - int i; - int mode = 0; - if (argc >= 1) - mode = atoi(argv[0]); - - for (i = 0; i < 20; i++) { - palClearPad(GPIOC, GPIOC_LED); - ili9341_test(mode); - palSetPad(GPIOC, GPIOC_LED); - chThdSleepMilliseconds(50); - } + int mode = 0; + if (argc >= 1) + mode = atoi(argv[0]); + + for (int i = 0; i < 20; i++) { + palClearPad(GPIOC, GPIOC_LED); + ili9341_test(mode); + palSetPad(GPIOC, GPIOC_LED); + chThdSleepMilliseconds(50); + } #endif #if 0 - //extern adcsample_t adc_samples[2]; - //chprintf(chp, "adc: %d %d\r\n", adc_samples[0], adc_samples[1]); - int i; - int x, y; - for (i = 0; i < 50; i++) { - test_touch(&x, &y); - chprintf(chp, "adc: %d %d\r\n", x, y); - chThdSleepMilliseconds(200); - } - //extern int touch_x, touch_y; - //chprintf(chp, "adc: %d %d\r\n", touch_x, touch_y); + //extern adcsample_t adc_samples[2]; + //chprintf(chp, "adc: %d %d\r\n", adc_samples[0], adc_samples[1]); + int x, y; + for (int i = 0; i < 50; i++) { + test_touch(&x, &y); + chprintf(chp, "adc: %d %d\r\n", x, y); + chThdSleepMilliseconds(200); + } + //extern int touch_x, touch_y; + //chprintf(chp, "adc: %d %d\r\n", touch_x, touch_y); #endif - while (argc > 1) { - int x, y; - touch_position(&x, &y); - chprintf(chp, "touch: %d %d\r\n", x, y); - chThdSleepMilliseconds(200); - } +#if 1 + while (argc > 1) { + int x, y; + touch_position(&x, &y); + chprintf(chp, "touch: %d %d\r\n", x, y); + chThdSleepMilliseconds(200); + } +#endif + chMtxUnlock(&mutex_sweep); } static void cmd_gain(BaseSequentialStream *chp, int argc, char *argv[]) @@ -1950,8 +2025,8 @@ static void cmd_stat(BaseSequentialStream *chp, int argc, char *argv[]) acc0 += (p[i] - ave0)*(p[i] - ave0); acc1 += (p[i+1] - ave1)*(p[i+1] - ave1); } - stat.rms[0] = sqrtf(acc0 / count); - stat.rms[1] = sqrtf(acc1 / count); + stat.rms[0] = (int16_t)sqrtf(acc0 / count); + stat.rms[1] = (int16_t)sqrtf(acc1 / count); stat.ave[0] = ave0; stat.ave[1] = ave1; @@ -1970,7 +2045,7 @@ static void cmd_stat(BaseSequentialStream *chp, int argc, char *argv[]) #define VERSION "unknown" #endif -const char NANOVNA_VERSION[] = VERSION; +static const char NANOVNA_VERSION[] = VERSION; static void cmd_version(BaseSequentialStream *chp, int argc, char *argv[]) { @@ -1986,7 +2061,73 @@ static void cmd_vbat(BaseSequentialStream *chp, int argc, char *argv[]) chprintf(chp, "%d mV\r\n", vbat); } -static THD_WORKING_AREA(waThread2, /* cmd_* max stack size + alpha */442); +#ifdef __COLOR_CMD__ +static void cmd_color(BaseSequentialStream *chp, int argc, char *argv[]) +{ + if (argc != 2) { + chprintf(chp, "usage: color {id} {rgb24}\r\n"); + for (int i=-3; i < TRACE_COUNT; i++) { + uint32_t color = 0; + if (i==-3) + color = config.menu_active_color; + else if (i==-2) + color = config.menu_normal_color; + else if (i==-1) + color = config.grid_color; + else + color = config.trace_color[i]; + color = ((color >> 3) & 0x001c00) | + ((color >> 5) & 0x0000f8) | + ((color << 16) & 0xf80000) | + ((color << 13) & 0x00e000); + color |= 0x070307; + if ((color & 0xff0000) == 0x070000) + color &= 0xf8ffff; + if ((color & 0x00ff00) == 0x000300) + color &= 0xfffcff; + if ((color & 0x0000ff) == 0x000007) + color &= 0xfffff8; + chprintf(chp, " %d: 0x%06x\r\n", i, color); + } + return; + } + int track = atoi(argv[0]); + int color = atoi(argv[1]); + if (strlen(argv[1]) > 2 && argv[1][0] == '0' && (argv[1][1] == 'X' || argv[1][1] == 'x')) { + color = strtol(&argv[1][2], (char **)0, 16); + } + if (color < 0 || color > 0xffffff) { + chprintf(chp, "error: invalid color\r\n"); + return; + } + color = RGBHEX(color); + if (track < -3 || track > (TRACE_COUNT-1)) { + chprintf(chp, "error: invalid track\r\n"); + return; + } + if (track >= 0) + config.trace_color[track] = (uint16_t)color; + else if (track == -1) + config.grid_color = (uint16_t)color; + else if (track == -2) + config.menu_normal_color = (uint16_t)color; + else if (track == -3) + config.menu_active_color = (uint16_t)color; +} +#endif + +/* +static void cmd_vbat_offset(BaseSequentialStream *chp, int argc, char *argv[]) +{ + if (argc != 1) { + chprintf(chp, "%d\r\n", config.vbat_offset); + return; + } + int offset = atoi(argv[0]); + config.vbat_offset = (int16_t)offset; +} +*/ +static THD_WORKING_AREA(waThread2, /* cmd_* max stack size + alpha */510 + 32); static const ShellCommand commands[] = { @@ -1999,7 +2140,7 @@ static const ShellCommand commands[] = { "saveconfig", cmd_saveconfig }, { "clearconfig", cmd_clearconfig }, { "data", cmd_data }, -#ifdef ENABLED_DUMP +#ifdef __DUMP_CMD__ { "dump", cmd_dump }, #endif { "frequencies", cmd_frequencies }, @@ -2010,6 +2151,9 @@ static const ShellCommand commands[] = { "sample", cmd_sample }, //{ "gamma", cmd_gamma }, { "scan", cmd_scan }, +#ifdef __SCANRAW_CMD__ + { "scanraw", cmd_scanraw }, +#endif // __SCANRAW_CMD__ { "sweep", cmd_sweep }, { "test", cmd_test }, { "touchcal", cmd_touchcal }, @@ -2026,25 +2170,29 @@ static const ShellCommand commands[] = { "vbat", cmd_vbat }, { "transform", cmd_transform }, { "threshold", cmd_threshold }, +#ifdef __COLOR_CMD__ + { "color", cmd_color }, +#endif +// { "vbat_offset", cmd_vbat_offset }, { NULL, NULL } }; static const ShellConfig shell_cfg1 = { - (BaseSequentialStream *)&SDU1, - commands + .sc_channel = (BaseSequentialStream *)&SDU1, + .sc_commands = commands }; static const I2CConfig i2ccfg = { - 0x00300506, //voodoo magic 400kHz @ HSI 8MHz - 0, - 0 + .timingr = 0x00300506, //voodoo magic 400kHz @ HSI 8MHz + .cr1 = 0, + .cr2 = 0 }; static DACConfig dac1cfg1 = { - //init: 2047U, - init: 1922U, - datamode: DAC_DHRM_12BIT_RIGHT + //.init = 2047U, + .init = 1922U, + .datamode = DAC_DHRM_12BIT_RIGHT }; int main(void) @@ -2052,12 +2200,14 @@ int main(void) halInit(); chSysInit(); - palClearPad(GPIOC, GPIOC_LED); - chMtxObjectInit(&mutex); + chMtxObjectInit(&mutex_sweep); + chMtxObjectInit(&mutex_ili9341); - // SPI LCD Initialize - ili9341_init(); - ili9341_fill(0, 0, LCD_WIDTH, LCD_HEIGHT, 0x0000); +/* + * SPI LCD Initialize + */ + ili9341_init(); + show_logo(); //palSetPadMode(GPIOB, 8, PAL_MODE_ALTERNATE(1) | PAL_STM32_OTYPE_OPENDRAIN); //palSetPadMode(GPIOB, 9, PAL_MODE_ALTERNATE(1) | PAL_STM32_OTYPE_OPENDRAIN); @@ -2106,7 +2256,8 @@ int main(void) if (config.default_loadcal >= 0) caldata_recall(config.default_loadcal); - redraw_frame(); + + /* * I2S Initialize @@ -2124,37 +2275,28 @@ int main(void) */ shellInit(); + + // redraw_frame(); + draw_frequencies(); + draw_cal_status(); + chThdSetPriority(HIGHPRIO); chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL); while (1) { - if (SDU1.config->usbp->state == USB_ACTIVE) { - thread_t *shelltp = chThdCreateStatic(waThread2, sizeof(waThread2), - NORMALPRIO + 1, - shellThread, (void *)&shell_cfg1); - chThdWait(shelltp); /* Waiting termination. */ - } - - chThdSleepMilliseconds(1000); + if (SDU1.config->usbp->state == USB_ACTIVE) { + thread_t *shelltp = chThdCreateStatic( + waThread2, sizeof(waThread2), + NORMALPRIO + 1, + shellThread, (void*)&shell_cfg1); + chThdWait(shelltp); /* Waiting termination. */ + } + chThdSleepMilliseconds(1000); } } -/* The prototype shows it is a naked function - in effect this is just an -assembly function. */ -void HardFault_Handler( void ); - -void hard_fault_handler_c(uint32_t *sp) __attribute__( ( naked ) );; - +// see also ch.dbg.panic_msg void HardFault_Handler(void) { - uint32_t* sp; - //__asm volatile ("mrs %0, msp \n\t": "=r" (sp) ); - __asm volatile ("mrs %0, psp \n\t": "=r" (sp) ); - hard_fault_handler_c(sp); -} - -void hard_fault_handler_c(uint32_t* sp) -{ - (void)sp; - while (true) {} + while (true) {} } diff --git a/mcuconf.h b/mcuconf.h index 7d38b678..05a989b8 100644 --- a/mcuconf.h +++ b/mcuconf.h @@ -121,7 +121,8 @@ #define STM32_I2C_BUSY_TIMEOUT 50 #define STM32_I2C_I2C1_IRQ_PRIORITY 3 #define STM32_I2C_I2C2_IRQ_PRIORITY 3 -#define STM32_I2C_USE_DMA TRUE +// note: for unknown reason I2C read operation doesn't works with USE_DMA=TRUE +#define STM32_I2C_USE_DMA FALSE #define STM32_I2C_I2C1_DMA_PRIORITY 1 #define STM32_I2C_I2C2_DMA_PRIORITY 1 #define STM32_I2C_I2C1_RX_DMA_STREAM STM32_DMA_STREAM_ID(1, 3) diff --git a/nanovna.h b/nanovna.h index f54f7557..70834713 100644 --- a/nanovna.h +++ b/nanovna.h @@ -29,10 +29,10 @@ void adc_init(void); uint16_t adc_single_read(ADC_TypeDef *adc, uint32_t chsel); void adc_start_analog_watchdogd(ADC_TypeDef *adc, uint32_t chsel); -#define SWEEP_POINTS 101 +#define POINT_COUNT 101 #define SPI_BUFFER_SIZE 2048 #else -#define SWEEP_POINTS 101 +#define POINT_COUNT 101 #define SPI_BUFFER_SIZE 1024 #endif @@ -54,10 +54,11 @@ void ili9341_test(int); #define YSTEP 7 //#define SPI_BUFFER_SIZE 1024 #endif +#define MARKER_COUNT 4 +#define TRACE_COUNT 4 - -extern float measured[2][SWEEP_POINTS][2]; +extern float measured[2][POINT_COUNT][2]; #define CAL_LOAD 0 #define CAL_OPEN 1 @@ -84,17 +85,17 @@ extern float measured[2][SWEEP_POINTS][2]; #define ETERM_ET 3 /* error term transmission tracking */ #define ETERM_EX 4 /* error term isolation */ -#define DOMAIN_MODE (1<<0) -#define DOMAIN_FREQ (0<<0) -#define DOMAIN_TIME (1<<0) -#define TD_FUNC (0b11<<1) -#define TD_FUNC_BANDPASS (0b00<<1) -#define TD_FUNC_LOWPASS_IMPULSE (0b01<<1) -#define TD_FUNC_LOWPASS_STEP (0b10<<1) -#define TD_WINDOW (0b11<<3) -#define TD_WINDOW_NORMAL (0b00<<3) -#define TD_WINDOW_MINIMUM (0b01<<3) -#define TD_WINDOW_MAXIMUM (0b10<<3) +#define DOMAIN_MODE (1<<0) +#define DOMAIN_FREQ (0<<0) +#define DOMAIN_TIME (1<<0) +#define TD_FUNC (3<<1) +#define TD_FUNC_BANDPASS (0<<1) +#define TD_FUNC_LOWPASS_IMPULSE (1<<1) +#define TD_FUNC_LOWPASS_STEP (2<<1) +#define TD_WINDOW (3<<3) +#define TD_WINDOW_NORMAL (0<<3) +#define TD_WINDOW_MINIMUM (1<<3) +#define TD_WINDOW_MAXIMUM (2<<3) #define FFT_SIZE 256 @@ -126,8 +127,6 @@ enum { void set_sweep_frequency(int type, int32_t frequency); uint32_t get_sweep_frequency(int type); -float my_atof(const char *p); - void toggle_sweep(void); extern int8_t sweep_enabled; @@ -146,13 +145,11 @@ extern uint8_t operation_requested; */ // 5ms @ 48kHz #define AUDIO_BUFFER_LEN 96 - -extern int16_t rx_buffer[]; - -#define STATE_LEN 32 + +//#define STATE_LEN 32 #define SAMPLE_LEN 48 -#ifdef ENABLED_DUMP +#ifdef __DUMP_CMD__ extern int16_t ref_buf[]; extern int16_t samp_buf[]; #endif @@ -163,8 +160,6 @@ void calculate_gamma(float *gamma); void fetch_amplitude(float *gamma); void fetch_amplitude_ref(float *gamma); -int si5351_set_frequency_with_offset(uint32_t freq, int offset, uint8_t drive_strength); - /* * tlv320aic3204.c @@ -178,16 +173,17 @@ extern void tlv320aic3204_select(int channel); /* * plot.c */ -#define GRIDY (LCD_HEIGHT/8-1) -#define OFFSETX 15 +#define GRIDY (LCD_HEIGHT/8-2) #define OFFSETY 0 #define WIDTH (LCD_WIDTH-GRIDY) #if defined(ILI9488) || defined(ILI9486) || defined(ST7796S) #define FONT_HEIGHT 13 +#define OFFSETX 21 #else #define FONT_HEIGHT 7 +#define OFFSETX 15 #endif -#define HEIGHT (LCD_HEIGHT-FONT_HEIGHT) +#define HEIGHT (GRIDY*8 +1) #define CELLOFFSETX 5 #define AREA_WIDTH_NORMAL (WIDTH + CELLOFFSETX*2) @@ -212,8 +208,6 @@ extern const uint8_t numfont20x22[][22 * 3]; // trace -#define TRACES_MAX 4 - enum { TRC_LOGMAG, TRC_PHASE, TRC_DELAY, TRC_SMITH, TRC_POLAR, TRC_LINEAR, TRC_SWR, TRC_REAL, TRC_IMAG, TRC_R, TRC_X, TRC_OFF }; @@ -238,16 +232,17 @@ typedef struct { } trace_t; typedef struct { - int32_t magic; - uint16_t dac_value; - uint16_t grid_color; - uint16_t menu_normal_color; - uint16_t menu_active_color; - uint16_t trace_color[TRACES_MAX]; - int16_t touch_cal[4]; - int8_t default_loadcal; - uint32_t harmonic_freq_threshold; - int32_t checksum; + int32_t magic; + uint16_t dac_value; + uint16_t grid_color; + uint16_t menu_normal_color; + uint16_t menu_active_color; + uint16_t trace_color[TRACE_COUNT]; + int16_t touch_cal[4]; + int8_t default_loadcal; + uint32_t harmonic_freq_threshold; + int16_t vbat_offset; + int32_t checksum; } config_t; extern config_t config; @@ -287,20 +282,15 @@ void request_to_draw_cells_behind_menu(void); void request_to_draw_cells_behind_numeric_input(void); void redraw_marker(int marker, int update_info); void trace_get_info(int t, char *buf, int len); -void plot_into_index(float measured[2][SWEEP_POINTS][2]); +void plot_into_index(float measured[2][POINT_COUNT][2]); void force_set_markmap(void); void draw_frequencies(void); void draw_all(bool flush); void draw_cal_status(void); -void markmap_all_markers(void); - void marker_position(int m, int t, int *x, int *y); int search_nearest_index(int x, int y, int t); -int marker_search(int mode); -int marker_search_left(int from); -int marker_search_right(int from); extern uint16_t redraw_request; @@ -316,7 +306,7 @@ extern bool pll_lock_failed; /* * ili9341.c */ -#define RGB565(b,r,g) ( (((b)<<8)&0xfc00) | (((r)<<2)&0x03e0) | (((g)>>3)&0x001f) ) +//gggBBBbb RRRrrGGG #define RGB(r,g,b) ( (((g)&0x1c)<<11) | (((b)&0xf8)<<5) | ((r)&0xf8) | (((g)&0xe0)>>5) ) #define RGBHEX(hex) ( (((hex)&0x001c00)<<3) | (((hex)&0x0000f8)<<5) | (((hex)&0xf80000)>>16) | (((hex)&0x00e000)>>13) ) @@ -332,6 +322,8 @@ extern const font_t NF20x22; extern uint16_t spi_buffer[SPI_BUFFER_SIZE]; +extern mutex_t mutex_ili9341; + void ili9341_init(void); void ili9341_test(int mode); void ili9341_bulk(int x, int y, int w, int h); @@ -339,18 +331,18 @@ void ili9341_fill(int x, int y, int w, int h, int color); #if !defined(ST7796S) void ili9341_drawchar_5x7(uint8_t ch, int x, int y, uint16_t fg, uint16_t bg); void ili9341_drawstring_5x7(const char *str, int x, int y, uint16_t fg, uint16_t bg); -void ili9341_drawstring_5x7_inv(const char *str, int x, int y, uint16_t fg, uint16_t bg, bool inv); #else void ili9341_drawchar_7x13(uint8_t ch, int x, int y, uint16_t fg, uint16_t bg); void ili9341_drawstring_7x13(const char *str, int x, int y, uint16_t fg, uint16_t bg); -void ili9341_drawstring_7x13_inv(const char *str, int x, int y, uint16_t fg, uint16_t bg, bool inv); #endif void ili9341_drawchar_size(uint8_t ch, int x, int y, uint16_t fg, uint16_t bg, uint8_t size); void ili9341_drawstring_size(const char *str, int x, int y, uint16_t fg, uint16_t bg, uint8_t size); void ili9341_drawfont(uint8_t ch, const font_t *font, int x, int y, uint16_t fg, uint16_t bg); void ili9341_read_memory(int x, int y, int w, int h, int len, uint16_t* out); void ili9341_read_memory_continue(int len, uint16_t* out); - +void ili9341_line(int x0, int y0, int x1, int y1, uint16_t fg); +void show_version(void); +void show_logo(void); /* * flash.c @@ -364,12 +356,12 @@ typedef struct { int16_t _sweep_points; uint16_t _cal_status; - uint32_t _frequencies[SWEEP_POINTS]; - float _cal_data[5][SWEEP_POINTS][2]; + uint32_t _frequencies[POINT_COUNT]; + float _cal_data[5][POINT_COUNT][2]; float _electrical_delay; // picoseconds - trace_t _trace[TRACES_MAX]; - marker_t _markers[4]; + trace_t _trace[TRACE_COUNT]; + marker_t _markers[MARKER_COUNT]; int _active_marker; uint8_t _domain_mode; /* 0bxxxxxffm : where ff: TD_FUNC m: DOMAIN_MODE */ uint8_t _velocity_factor; // % @@ -412,18 +404,12 @@ void clear_all_config_prop_data(void); * ui.c */ -// lever_mode -enum { - LM_MARKER, LM_SEARCH, LM_CENTER, LM_SPAN -}; - typedef struct { int8_t digit; /* 0~5 */ int8_t digit_mode; int8_t current_trace; /* 0..3 */ uint32_t value; // for editing at numeric input area uint32_t previous_value; - uint8_t lever_mode; } uistat_t; extern uistat_t uistat; @@ -444,6 +430,7 @@ void touch_cal_exec(void); void touch_draw_test(void); void enter_dfu(void); +extern double my_atof(const char *p); /* * adc.c */ @@ -454,7 +441,7 @@ void adc_start_analog_watchdogd(ADC_TypeDef *adc, uint32_t chsel); void adc_stop(ADC_TypeDef *adc); void adc_interrupt(ADC_TypeDef *adc); int16_t adc_vbat_read(ADC_TypeDef *adc); - +int16_t adc_tjun_read(ADC_TypeDef *adc); #ifdef NANOVNA_F303 #define ADC_CHSELR_VREFINT ADC_CHANNEL_IN18 #define ADC_CHSELR_VBAT ADC_CHANNEL_IN17 @@ -466,11 +453,7 @@ int16_t adc_vbat_read(ADC_TypeDef *adc); /* * misclinous */ -#if defined(LED_OFF) -#define PULSE -#else -#define PULSE do { palClearPad(GPIOC, GPIOC_LED); palSetPad(GPIOC, GPIOC_LED);} while(0) -#endif +#define PULSE { palClearPad(GPIOC, GPIOC_LED); palSetPad(GPIOC, GPIOC_LED);} // convert vbat [mV] to battery indicator static inline uint8_t vbat2bati(int16_t vbat) @@ -482,4 +465,4 @@ static inline uint8_t vbat2bati(int16_t vbat) return 100; } -/*EOF*/ + diff --git a/plot.c b/plot.c index 5e46fbf0..c467dbc3 100644 --- a/plot.c +++ b/plot.c @@ -5,22 +5,26 @@ #include "chprintf.h" #include "nanovna.h" -#define SWAP(x,y) do { int z=x; x = y; y = z; } while(0) +//#define __DRAW_Z__ + + + +#define SWAP(x,y) { int z=x; x = y; y = z; } static void cell_draw_marker_info(int m, int n, int w, int h); -void frequency_string(char *buf, size_t len, int32_t freq); -void markmap_all_markers(void); +static void frequency_string(char *buf, size_t len, int32_t freq); +static void markmap_all_markers(void); //#define GRID_COLOR 0x0863 //uint16_t grid_color = 0x1084; /* indicate dirty cells */ -uint16_t markmap[2][10]; -uint16_t current_mappage = 0; +static uint16_t markmap[2][10]; +static uint16_t current_mappage = 0; -int32_t fgrid = 50000000; -int16_t grid_offset; -int16_t grid_width; +static int32_t fgrid = 50000000; +static int16_t grid_offset; +static int16_t grid_width; int area_width = AREA_WIDTH_NORMAL; int area_height = HEIGHT; @@ -41,7 +45,7 @@ int area_height = HEIGHT; * CELL_X[5:9] position in the cell * CELL_Y[0:4] */ -uint32_t trace_index[TRACES_MAX][SWEEP_POINTS]; +static uint32_t trace_index[TRACE_COUNT][POINT_COUNT]; #define INDEX(x, y, n) \ ((((x)&0x03e0UL)<<22) | (((y)&0x03e0UL)<<17) | (((n)&0x0fffUL)<<10) \ @@ -91,8 +95,7 @@ void update_grid(void) redraw_request |= REDRAW_FREQUENCY; } -static inline int -circle_inout(int x, int y, int r) +static int circle_inout(int x, int y, int r) { int d = x*x + y*y - r*r; if (d <= -r) @@ -103,12 +106,11 @@ circle_inout(int x, int y, int r) } -#define P_CENTER_X (LCD_WIDTH/2-OFFSETX-CELLOFFSETX) -#define P_CENTER_Y (LCD_HEIGHT/2-4) -#define P_RADIUS P_CENTER_Y +#define P_CENTER_X (WIDTH/2 ) +#define P_CENTER_Y (HEIGHT/2) +#define P_RADIUS (HEIGHT/2) -static int -polar_grid(int x, int y) +static int polar_grid(int x, int y) { int c = config.grid_color; int d; @@ -151,8 +153,7 @@ polar_grid(int x, int y) * Constant Resistance circle: (u - r/(r+1))^2 + v^2 = 1/(r+1)^2 * Constant Reactance circle: (u - 1)^2 + (v-1/x)^2 = 1/x^2 */ -int -smith_grid(int x, int y) +static int smith_grid(int x, int y) { int c = config.grid_color; int d; @@ -307,8 +308,7 @@ const int cirs[][4] = { { 0, 0, 0, 0 } // sentinel }; -int -smith_grid3(int x, int y) +static int smith_grid3(int x, int y) { int c = config.grid_color; int d; @@ -364,8 +364,7 @@ rectangular_grid(int x, int y) } #endif -static int -rectangular_grid_x(int x) +static int rectangular_grid_x(int x) { int c = config.grid_color; if (x < 0) @@ -377,8 +376,7 @@ rectangular_grid_x(int x) return 0; } -static int -rectangular_grid_y(int y) +static int rectangular_grid_y(int y) { int c = config.grid_color; if (y < 0) @@ -427,7 +425,7 @@ draw_on_strut(int v0, int d, int color) /* * calculate log10(abs(gamma)) */ -float logmag(float *v) +static float logmag(float *v) { return log10f(v[0]*v[0] + v[1]*v[1]) * 10; } @@ -435,38 +433,48 @@ float logmag(float *v) /* * calculate phase[-2:2] of coefficient */ -float phase(float *v) +static float phase(float *v) { return 2 * atan2f(v[1], v[0]) / M_PI * 90; } /* - * calculate groupdelay - */ -float groupdelay(float *v, float *w, float deltaf) -{ -#if 1 - // atan(w)-atan(v) = atan((w-v)/(1+wv)) - float r = w[0]*v[1] - w[1]*v[0]; - float i = w[0]*v[0] + w[1]*v[1]; - return atan2f(r, i) / (2 * M_PI * deltaf); -#else - return (atan2f(w[0], w[1]) - atan2f(v[0], v[1])) / (2 * M_PI * deltaf); -#endif + * calculate group_delay = -deltaAngle(gamma) / (deltaf * 360) + */ +static float group_delay(float gamma[POINT_COUNT][2], uint32_t* freq, int count, int index) +{ + float *v, *w; + float deltaf; + if (index == count-1) { + deltaf = freq[index] - freq[index-1]; + v = gamma[index-1]; + w = gamma[index]; + } + else { + deltaf = freq[index+1] - freq[index]; + v = gamma[index]; + w = gamma[index+1]; + } + // w = w[0]/w[1] + // v = v[0]/v[1] + // atan(w)-atan(v) = atan((w-v)/(1+wv)) + float r = w[0]*v[1] - w[1]*v[0]; + float i = w[0]*v[0] + w[1]*v[1]; + return atan2f(r, i) / (2 * M_PI * deltaf); } /* * calculate abs(gamma) */ -float linear(float *v) +static float linear(float *v) { - return - sqrtf(v[0]*v[0] + v[1]*v[1]); + return sqrtf(v[0]*v[0] + v[1]*v[1]); } /* * calculate vswr; (1+gamma)/(1-gamma) */ -float swr(float *v) +static float swr(float *v) { float x = sqrtf(v[0]*v[0] + v[1]*v[1]); if (x > 1) @@ -474,14 +482,14 @@ float swr(float *v) return (1 + x)/(1 - x); } -float resitance(float *v) { +static float resitance(float *v) { float z0 = 50; float d = z0 / ((1-v[0])*(1-v[0])+v[1]*v[1]); float zr = ((1+v[0])*(1-v[0]) - v[1]*v[1]) * d; return zr; } -float reactance(float *v) { +static float reactance(float *v) { float z0 = 50; float d = z0 / ((1-v[0])*(1-v[0])+v[1]*v[1]); float zi = 2*v[1] * d; @@ -489,12 +497,11 @@ float reactance(float *v) { } #define RADIUS ((HEIGHT-1)/2) -void -cartesian_scale(float re, float im, int *xp, int *yp, float scale) +static void cartesian_scale(float re, float im, int *xp, int *yp, float scale) { //float scale = 4e-3; - int x = re * RADIUS * scale; - int y = im * RADIUS * scale; + int x = (int)(re * RADIUS * scale); + int y = (int)(im * RADIUS * scale); if (x < -RADIUS) x = -RADIUS; if (y < -RADIUS) y = -RADIUS; if (x > RADIUS) x = RADIUS; @@ -503,72 +510,59 @@ cartesian_scale(float re, float im, int *xp, int *yp, float scale) *yp = HEIGHT/2 - y; } -static float -groupdelay_from_array(int i, float array[101][2]) -{ - if (i == 0) { - float deltaf = frequencies[1] - frequencies[0]; - return groupdelay(array[0], array[1], deltaf); - } else if (i == 100) { - float deltaf = frequencies[i] - frequencies[i-1]; - return groupdelay(array[i-1], array[i], deltaf); - } else { - float deltaf = frequencies[i+1] - frequencies[i-1]; - return groupdelay(array[i-1], array[i+1], deltaf); - } -} -uint32_t -trace_into_index(int x, int t, int i, float array[101][2]) +static uint32_t trace_into_index( + int x, int t, int i, + float coeff[POINT_COUNT][2], + uint32_t freq[POINT_COUNT], + int point_count) { int y = 0; float v = 0; - float *coeff = array[i]; float refpos = 8 - get_trace_refpos(t); float scale = 1 / get_trace_scale(t); switch (trace[t].type) { case TRC_LOGMAG: - v = refpos - logmag(coeff) * scale; + v = refpos - logmag(coeff[i]) * scale; break; case TRC_PHASE: - v = refpos - phase(coeff) * scale; + v = refpos - phase(coeff[i]) * scale; break; case TRC_DELAY: - v = refpos - groupdelay_from_array(i, array) * scale; + v = refpos - group_delay(coeff, freq, point_count, i) * scale; break; case TRC_LINEAR: - v = refpos + linear(coeff) * scale; + v = refpos + linear(coeff[i]) * scale; break; case TRC_SWR: - v = refpos+ (1 - swr(coeff)) * scale; + v = refpos+ (1 - swr(coeff[i])) * scale; break; case TRC_REAL: - v = refpos - coeff[0] * scale; + v = refpos - coeff[i][0] * scale; break; case TRC_IMAG: - v = refpos - coeff[1] * scale; + v = refpos - coeff[i][1] * scale; break; case TRC_R: - v = refpos - resitance(coeff) * scale; + v = refpos - resitance(coeff[i]) * scale; break; case TRC_X: - v = refpos - reactance(coeff) * scale; + v = refpos - reactance(coeff[i]) * scale; break; case TRC_SMITH: //case TRC_ADMIT: case TRC_POLAR: - cartesian_scale(coeff[0], coeff[1], &x, &y, scale); + cartesian_scale(coeff[i][0], coeff[i][1], &x, &y, scale); return INDEX(x +CELLOFFSETX, y, i); break; } if (v < 0) v = 0; if (v > 8) v = 8; - y = v * GRIDY; + y = (int)(v * GRIDY); return INDEX(x +CELLOFFSETX, y, i); } -static int -string_value_with_prefix(char *buf, int len, float val, char unit) +static int string_value_with_prefix(char *buf, int len, float val, char unit) { char prefix; int n; @@ -624,8 +618,7 @@ string_value_with_prefix(char *buf, int len, float val, char unit) #define PI2 6.283184 -static void -gamma2imp(char *buf, int len, const float coeff[2], uint32_t frequency) +static void gamma2imp(char *buf, int len, const float coeff[2], uint32_t frequency) { // z = (gamma+1)/(gamma-1) * z0 float z0 = 50; @@ -646,8 +639,7 @@ gamma2imp(char *buf, int len, const float coeff[2], uint32_t frequency) } } -static void -gamma2resistance(char *buf, int len, const float coeff[2]) +static void gamma2resistance(char *buf, int len, const float coeff[2]) { float z0 = 50; float d = z0 / ((1-coeff[0])*(1-coeff[0])+coeff[1]*coeff[1]); @@ -655,8 +647,7 @@ gamma2resistance(char *buf, int len, const float coeff[2]) string_value_with_prefix(buf, len, zr, S_OHM[0]); } -static void -gamma2reactance(char *buf, int len, const float coeff[2]) +static void gamma2reactance(char *buf, int len, const float coeff[2]) { float z0 = 50; float d = z0 / ((1-coeff[0])*(1-coeff[0])+coeff[1]*coeff[1]); @@ -664,59 +655,60 @@ gamma2reactance(char *buf, int len, const float coeff[2]) string_value_with_prefix(buf, len, zi, S_OHM[0]); } -static void -trace_get_value_string(int t, char *buf, int len, float array[101][2], int i) +static void trace_get_value_string( + int t, char *buf, int len, + int i, float coeff[POINT_COUNT][2], + uint32_t freq[POINT_COUNT], + int point_count) { - float *coeff = array[i]; float v; switch (trace[t].type) { case TRC_LOGMAG: - v = logmag(coeff); + v = logmag(coeff[i]); if (v == -INFINITY) chsnprintf(buf, len, "-INF dB"); else chsnprintf(buf, len, "%.2fdB", v); break; case TRC_PHASE: - v = phase(coeff); - chsnprintf(buf, len, "%.2f" S_DEGREE, v); + v = phase(coeff[i]); + chsnprintf(buf, len, "%.3f" S_DEGREE, v); break; case TRC_DELAY: - v = groupdelay_from_array(i, array); + v = group_delay(coeff, freq, point_count, i); string_value_with_prefix(buf, len, v, 's'); break; case TRC_LINEAR: - v = linear(coeff); - chsnprintf(buf, len, "%.2f", v); + v = linear(coeff[i]); + chsnprintf(buf, len, "%.3f", v); break; case TRC_SWR: - v = swr(coeff); + v = swr(coeff[i]); chsnprintf(buf, len, "%.2f", v); break; case TRC_SMITH: - gamma2imp(buf, len, coeff, frequencies[i]); + gamma2imp(buf, len, coeff[i], freq[i]); break; case TRC_REAL: - chsnprintf(buf, len, "%.2f", coeff[0]); + chsnprintf(buf, len, "%.3f", coeff[i][0]); break; case TRC_IMAG: - chsnprintf(buf, len, "%.2fj", coeff[1]); + chsnprintf(buf, len, "%.3fj", coeff[i][1]); break; case TRC_R: - gamma2resistance(buf, len, coeff); + gamma2resistance(buf, len, coeff[i]); break; case TRC_X: - gamma2reactance(buf, len, coeff); + gamma2reactance(buf, len, coeff[i]); break; //case TRC_ADMIT: case TRC_POLAR: - chsnprintf(buf, len, "%.2f %.2fj", coeff[0], coeff[1]); + chsnprintf(buf, len, "%.3f %.3fj", coeff[i][0], coeff[i][1]); break; } } -void -trace_get_info(int t, char *buf, int len) +void trace_get_info(int t, char *buf, int len) { const char *type = get_trace_typename(t); int n; @@ -750,50 +742,43 @@ static float distance_of_index(int idx) { } -static inline void -mark_map(int x, int y) +static inline void mark_map(int x, int y) { if (y >= 0 && y < 10 && x >= 0 && x < 16) markmap[current_mappage][y] |= 1<= 0 && y0 < h && x0 >= 0 && x0 < w) spi_buffer[y0*w+x0] |= c; } } - + #if 0 int -search_index_range(int x, int y, uint32_t index[SWEEP_POINTS], int *i0, int *i1) +search_index_range(int x, int y, uint32_t index[101], int *i0, int *i1) { int i, j; int head = 0; @@ -962,14 +947,16 @@ search_index_range(int x, int y, uint32_t index[SWEEP_POINTS], int *i0, int *i1) j--; *i0 = j; j = i; - while (j < (SWEEP_POINTS-1) && x == CELL_X0(index[j+1]) && y == CELL_Y0(index[j+1])) + while (j < 100 && x == CELL_X0(index[j+1]) && y == CELL_Y0(index[j+1])) j++; *i1 = j; return TRUE; } + + #endif + -int -search_index_range_x(int x, uint32_t index[SWEEP_POINTS], int *i0, int *i1) +static int search_index_range_x(int x, uint32_t index[POINT_COUNT], int *i0, int *i1) { int i, j; int head = 0; @@ -999,14 +986,13 @@ search_index_range_x(int x, uint32_t index[SWEEP_POINTS], int *i0, int *i1) j--; *i0 = j; j = i; - while (j < (SWEEP_POINTS-1) && x == CELL_X0(index[j+1])) + while (j < (POINT_COUNT-1) && x == CELL_X0(index[j+1])) j++; *i1 = j; return TRUE; } -void -draw_refpos(int w, int h, int x, int y, int c) +static void draw_refpos(int w, int h, int x, int y, int c) { // draw triangle int i, j; @@ -1027,13 +1013,12 @@ draw_refpos(int w, int h, int x, int y, int c) } -void -cell_draw_refpos(int m, int n, int w, int h) +static void cell_draw_refpos(int m, int n, int w, int h) { int x0 = m * CELLWIDTH; int y0 = n * CELLHEIGHT; int t; - for (t = 0; t < TRACES_MAX; t++) { + for (t = 0; t < TRACE_COUNT; t++) { if (!trace[t].enabled) continue; if (trace[t].type == TRC_SMITH || trace[t].type == TRC_POLAR) @@ -1045,9 +1030,7 @@ cell_draw_refpos(int m, int n, int w, int h) } } - -void -draw_marker(int w, int h, int x, int y, int c, int ch) +static void draw_marker(int w, int h, int x, int y, int c, int ch) { int i, j; for (j = FONT_HEIGHT+3; j >= 0; j--) { @@ -1074,104 +1057,14 @@ draw_marker(int w, int h, int x, int y, int c, int ch) } } -void -marker_position(int m, int t, int *x, int *y) +void marker_position(int m, int t, int *x, int *y) { uint32_t index = trace_index[t][markers[m].index]; *x = CELL_X(index); *y = CELL_Y(index); } -static int greater(int x, int y) { return x > y; } -static int lesser(int x, int y) { return x < y; } - -static int (*compare)(int x, int y) = lesser; - - -int -marker_search(int mode) -{ - int i; - int found = 0; - - if (mode == 0) - compare = greater; - else - compare = lesser; - - if (uistat.current_trace == -1) - return -1; - - int value = CELL_Y(trace_index[uistat.current_trace][0]); - for (i = 0; i < 101; i++) { - uint32_t index = trace_index[uistat.current_trace][i]; - if ((*compare)(value, CELL_Y(index))) { - value = CELL_Y(index); - found = i; - } - } - - return found; -} - -int -marker_search_left(int from) -{ - int i; - int found = -1; - - if (uistat.current_trace == -1) - return -1; - - int value = CELL_Y(trace_index[uistat.current_trace][from]); - for (i = from - 1; i >= 0; i--) { - uint32_t index = trace_index[uistat.current_trace][i]; - if ((*compare)(value, CELL_Y(index))) - break; - value = CELL_Y(index); - } - - for (; i >= 0; i--) { - uint32_t index = trace_index[uistat.current_trace][i]; - if ((*compare)(CELL_Y(index), value)) { - break; - } - found = i; - value = CELL_Y(index); - } - return found; -} - -int -marker_search_right(int from) -{ - int i; - int found = -1; - - if (uistat.current_trace == -1) - return -1; - - int value = CELL_Y(trace_index[uistat.current_trace][from]); - for (i = from + 1; i < 101; i++) { - uint32_t index = trace_index[uistat.current_trace][i]; - if ((*compare)(value, CELL_Y(index))) - break; - value = CELL_Y(index); - } - - for (; i < 101; i++) { - uint32_t index = trace_index[uistat.current_trace][i]; - if ((*compare)(CELL_Y(index), value)) { - break; - } - found = i; - value = CELL_Y(index); - } - return found; -} - -int -search_nearest_index(int x, int y, int t) +int search_nearest_index(int x, int y, int t) { uint32_t *index = trace_index[t]; int min_i = -1; @@ -1193,61 +1086,63 @@ search_nearest_index(int x, int y, int t) return min_i; } -void -cell_draw_markers(int m, int n, int w, int h) +static void cell_draw_markers(int m, int n, int w, int h) { int x0 = m * CELLWIDTH; int y0 = n * CELLHEIGHT; int t, i; - for (i = 0; i < 4; i++) { + for (i = 0; i < MARKER_COUNT; i++) { if (!markers[i].enabled) continue; - for (t = 0; t < TRACES_MAX; t++) { + for (t = 0; t < TRACE_COUNT; t++) { if (!trace[t].enabled) continue; uint32_t index = trace_index[t][markers[i].index]; int x = CELL_X(index) - x0; int y = CELL_Y(index) - y0; - if (x > -6 && x < w+6 && y >= 0 && y < h+12) + if (x >= (-(FONT_HEIGHT+3)/2) && x <= w+(FONT_HEIGHT+3)/2 && y >= 0 && y <= h+FONT_HEIGHT+3) + draw_marker(w, h, x, y, config.trace_color[t], '1' + i); + } } } -void -markmap_marker(int marker) -{ - int t; - if (!markers[marker].enabled) - return; - for (t = 0; t < TRACES_MAX; t++) { - if (!trace[t].enabled) - continue; - uint32_t index = trace_index[t][markers[marker].index]; - int x = CELL_X(index); - int y = CELL_Y(index); - int m = x>>5; - int n = y>>5; - mark_map(m, n); - if ((x&31) < 6) - mark_map(m-1, n); - if ((x&31) > 32-6) - mark_map(m+1, n); - if ((y&31) < 12) { - mark_map(m, n-1); - if ((x&31) < 6) - mark_map(m-1, n-1); - if ((x&31) > 32-6) - mark_map(m+1, n-1); +static void markmap_marker(int marker) +{ + int t; + if (!markers[marker].enabled) + return; + for (t = 0; t < TRACE_COUNT; t++) { + if (!trace[t].enabled) + continue; + uint32_t index = markers[marker].index; + if (index >= POINT_COUNT) + continue; + index = trace_index[t][index]; + int x = CELL_X(index); + int y = CELL_Y(index); + int m = x>>5; + int n = y>>5; + mark_map(m, n); + if ((x&31) < 6) + mark_map(m-1, n); + if ((x&31) > 32-6) + mark_map(m+1, n); + if ((y&31) < 12) { + mark_map(m, n-1); + if ((x&31) < 6) + mark_map(m-1, n-1); + if ((x&31) > 32-6) + mark_map(m+1, n-1); + } } - } } -void -markmap_all_markers(void) +static void markmap_all_markers(void) { int i; - for (i = 0; i < 4; i++) { + for (i = 0; i < MARKER_COUNT; i++) { if (!markers[i].enabled) continue; markmap_marker(i); @@ -1256,8 +1151,7 @@ markmap_all_markers(void) } -static void -draw_cell(int m, int n) +static void draw_cell(int m, int n) { int x0 = m * CELLWIDTH; int y0 = n * CELLHEIGHT; @@ -1276,8 +1170,9 @@ draw_cell(int m, int n) if (w <= 0 || h <= 0) return; + chMtxLock(&mutex_ili9341); // [protect spi_buffer] uint16_t grid_mode = 0; - for (t = 0; t < TRACES_MAX; t++) { + for (t = 0; t < TRACE_COUNT; t++) { if (!trace[t].enabled) continue; @@ -1327,7 +1222,7 @@ draw_cell(int m, int n) #if 1 /* draw rectanglar plot */ - for (t = 0; t < TRACES_MAX; t++) { + for (t = 0; t < TRACE_COUNT; t++) { if (!trace[t].enabled) continue; if (trace[t].type == TRC_SMITH || trace[t].type == TRC_POLAR) @@ -1336,7 +1231,7 @@ draw_cell(int m, int n) if (search_index_range_x(x0, trace_index[t], &i0, &i1)) { if (i0 > 0) i0--; - if (i1 < SWEEP_POINTS-1) + if (i1 < POINT_COUNT-1) i1++; for (i = i0; i < i1; i++) { int x1 = CELL_X(trace_index[t][i]); @@ -1351,7 +1246,7 @@ draw_cell(int m, int n) #endif #if 1 /* draw polar plot */ - for (t = 0; t < TRACES_MAX; t++) { + for (t = 0; t < TRACE_COUNT; t++) { int c = config.trace_color[t]; if (!trace[t].enabled) continue; @@ -1383,10 +1278,10 @@ draw_cell(int m, int n) cell_draw_refpos(m, n, w, h); ili9341_bulk(OFFSETX + x0off, OFFSETY + y0, w, h); + chMtxUnlock(&mutex_ili9341); // [/protect spi_buffer] } -void -draw_all_cells(bool flush_markmap) +static void draw_all_cells(bool flush_markmap) { int m, n; for (m = 0; m < (area_width+CELLWIDTH-1) / CELLWIDTH; m++) @@ -1403,8 +1298,7 @@ draw_all_cells(bool flush_markmap) } } -void -draw_all(bool flush) +void draw_all(bool flush) { if (redraw_request & REDRAW_CELLS) draw_all_cells(flush); @@ -1415,8 +1309,7 @@ draw_all(bool flush) redraw_request = 0; } -void -redraw_marker(int marker, int update_info) +void redraw_marker(int marker, int update_info) { // mark map on new position of marker markmap_marker(marker); @@ -1428,8 +1321,7 @@ redraw_marker(int marker, int update_info) draw_all_cells(TRUE); } -void -request_to_draw_cells_behind_menu(void) +void request_to_draw_cells_behind_menu(void) { int n, m; for (m = LCD_WIDTH/32-3; m <= LCD_WIDTH/32-1; m++) @@ -1438,8 +1330,7 @@ request_to_draw_cells_behind_menu(void) redraw_request |= REDRAW_CELLS; } -void -request_to_draw_cells_behind_numeric_input(void) +void request_to_draw_cells_behind_numeric_input(void) { int n, m; for (m = 0; m <= 9; m++) @@ -1450,8 +1341,7 @@ request_to_draw_cells_behind_numeric_input(void) #if !(defined(ILI9488) || defined(ILI9486) || defined(ST7796S)) -void -cell_drawchar_5x7(int w, int h, uint8_t ch, int x, int y, uint16_t fg, int invert) +static void cell_drawchar_5x7(int w, int h, uint8_t ch, int x, int y, uint16_t fg, int invert) { uint8_t bits; int c, r; @@ -1471,8 +1361,7 @@ cell_drawchar_5x7(int w, int h, uint8_t ch, int x, int y, uint16_t fg, int inver } } -void -cell_drawstring_5x7(int w, int h, char *str, int x, int y, uint16_t fg) +static void cell_drawstring_5x7(int w, int h, char *str, int x, int y, uint16_t fg) { while (*str) { cell_drawchar_5x7(w, h, *str, x, y, fg, FALSE); @@ -1481,8 +1370,7 @@ cell_drawstring_5x7(int w, int h, char *str, int x, int y, uint16_t fg) } } -void -cell_drawstring_invert_5x7(int w, int h, char *str, int x, int y, uint16_t fg, int invert) +static void cell_drawstring_invert_5x7(int w, int h, char *str, int x, int y, uint16_t fg, int invert) { while (*str) { cell_drawchar_5x7(w, h, *str, x, y, fg, invert); @@ -1491,8 +1379,7 @@ cell_drawstring_invert_5x7(int w, int h, char *str, int x, int y, uint16_t fg, i } } -static void -cell_draw_marker_info(int m, int n, int w, int h) +static void cell_draw_marker_info(int m, int n, int w, int h) { char buf[24]; int t; @@ -1502,7 +1389,7 @@ cell_draw_marker_info(int m, int n, int w, int h) return; int idx = markers[active_marker].index; int j = 0; - for (t = 0; t < TRACES_MAX; t++) { + for (t = 0; t < TRACE_COUNT; t++) { if (!trace[t].enabled) continue; int xpos = 1 + (j%2)*146; @@ -1515,11 +1402,17 @@ cell_draw_marker_info(int m, int n, int w, int h) trace_get_info(t, buf, sizeof buf); cell_drawstring_5x7(w, h, buf, xpos, ypos, config.trace_color[t]); xpos += 64; - trace_get_value_string(t, buf, sizeof buf, measured[trace[t].channel], idx); + trace_get_value_string( + t, buf, sizeof buf, + idx, measured[trace[t].channel], frequencies, sweep_points); cell_drawstring_5x7(w, h, buf, xpos, ypos, config.trace_color[t]); j++; } - + j += j&1; + + // LEFT + int ypos = 1 + (j/2)*7; + ypos -= n * CELLHEIGHT; if (electrical_delay != 0) { // draw electrical delay int xpos = 21; @@ -1539,7 +1432,7 @@ cell_draw_marker_info(int m, int n, int w, int h) // draw marker frequency int xpos = 192; - int ypos = 1 + (j/2)*FONT_HEIGHT; + ypos = 1 + (j/2)*FONT_HEIGHT; xpos -= m * CELLWIDTH -CELLOFFSETX; ypos -= n * CELLHEIGHT; chsnprintf(buf, sizeof buf, "%d:", active_marker + 1); @@ -1562,7 +1455,7 @@ cell_draw_marker_info(int m, int n, int w, int h) xpos = 192; xpos -= m * CELLWIDTH -CELLOFFSETX; ypos += FONT_HEIGHT; - chsnprintf(buf, sizeof buf, "\001%d:", previous_marker+1); + chsnprintf(buf, sizeof buf, "D%d:", previous_marker+1); cell_drawstring_5x7(w, h, buf, xpos, ypos, 0xffff); xpos += 19; if ((domain_mode & DOMAIN_MODE) == DOMAIN_FREQ) { @@ -1578,8 +1471,7 @@ cell_draw_marker_info(int m, int n, int w, int h) } } -void -frequency_string(char *buf, size_t len, int32_t freq) +static void frequency_string(char *buf, size_t len, int32_t freq) { if (freq < 0) { freq = -freq; @@ -1600,8 +1492,7 @@ frequency_string(char *buf, size_t len, int32_t freq) } } -void -draw_frequencies(void) +void draw_frequencies(void) { char buf[24]; if ((domain_mode & DOMAIN_MODE) == DOMAIN_FREQ) { @@ -1642,14 +1533,13 @@ draw_frequencies(void) ili9341_drawstring_5x7(buf, OFFSETX, HEIGHT, 0xffff, 0x0000); strcpy(buf, "STOP "); - chsnprintf(buf+5, 24-5, "%d ns", (uint16_t)(time_of_index(SWEEP_POINTS) * 1e9)); + chsnprintf(buf+5, 24-5, "%d ns", (uint16_t)(time_of_index(POINT_COUNT-1) * 1e9)); strcat(buf, " "); ili9341_drawstring_5x7(buf, 205, HEIGHT, 0xffff, 0x0000); } } -void -draw_cal_status(void) +void draw_cal_status(void) { int x = 0; int y = 100; @@ -1689,8 +1579,7 @@ draw_cal_status(void) #else -void -cell_drawchar_7x13(int w, int h, uint8_t ch, int x, int y, uint16_t fg, int invert) +static void cell_drawchar_7x13(int w, int h, uint8_t ch, int x, int y, uint16_t fg, int invert) { uint16_t bits; int c, r; @@ -1710,8 +1599,7 @@ cell_drawchar_7x13(int w, int h, uint8_t ch, int x, int y, uint16_t fg, int inve } } -void -cell_drawstring_7x13(int w, int h, char *str, int x, int y, uint16_t fg) +static void cell_drawstring_7x13(int w, int h, char *str, int x, int y, uint16_t fg) { while (*str) { cell_drawchar_7x13(w, h, *str, x, y, fg, FALSE); @@ -1720,8 +1608,7 @@ cell_drawstring_7x13(int w, int h, char *str, int x, int y, uint16_t fg) } } -void -cell_drawstring_invert_7x13(int w, int h, char *str, int x, int y, uint16_t fg, int invert) +static void cell_drawstring_invert_7x13(int w, int h, char *str, int x, int y, uint16_t fg, int invert) { while (*str) { cell_drawchar_7x13(w, h, *str, x, y, fg, invert); @@ -1730,22 +1617,22 @@ cell_drawstring_invert_7x13(int w, int h, char *str, int x, int y, uint16_t fg, } } -static void -cell_draw_marker_info(int m, int n, int w, int h) + +static void cell_draw_marker_info(int m, int n, int w, int h) { char buf[24]; int t; - if (n >1) + if (n > 1) return; if (active_marker < 0) return; int idx = markers[active_marker].index; int j = 0; - for (t = 0; t < TRACES_MAX; t++) { + for (t = 0; t < TRACE_COUNT; t++) { if (!trace[t].enabled) continue; - int xpos = 1 + (j%2)*211; - int ypos = 1 + (j/2)*13; + int xpos = 1 + (j%2)*221; + int ypos = 1 + (j/2)*FONT_HEIGHT; xpos -= m * CELLWIDTH -CELLOFFSETX; ypos -= n * CELLHEIGHT; chsnprintf(buf, sizeof buf, "CH%d", trace[t].channel); @@ -1754,32 +1641,38 @@ cell_draw_marker_info(int m, int n, int w, int h) trace_get_info(t, buf, sizeof buf); cell_drawstring_7x13(w, h, buf, xpos, ypos, config.trace_color[t]); xpos += 90; - trace_get_value_string(t, buf, sizeof buf, measured[trace[t].channel], idx); + trace_get_value_string( + t, buf, sizeof buf, + idx, measured[trace[t].channel], frequencies, sweep_points); cell_drawstring_7x13(w, h, buf, xpos, ypos, config.trace_color[t]); j++; } - if (electrical_delay != 0) { - // draw electrical delay - int xpos = 29; - int ypos = 1 + ((j+1)/2)*(2*FONT_HEIGHT); - xpos -= m * CELLWIDTH -CELLOFFSETX; - ypos -= n * CELLHEIGHT; - chsnprintf(buf, sizeof buf, "Edelay"); - cell_drawstring_7x13(w, h, buf, xpos, ypos, 0xffff); - xpos += 7 * 7; - int n = string_value_with_prefix(buf, sizeof buf, electrical_delay * 1e-12, 's'); - cell_drawstring_7x13(w, h, buf, xpos, ypos, 0xffff); - xpos += n * 7 + 5; - float light_speed_ps = 299792458e-12; //(m/ps) - string_value_with_prefix(buf, sizeof buf, electrical_delay * light_speed_ps * velocity_factor / 100.0, 'm'); - cell_drawstring_7x13(w, h, buf, xpos, ypos, 0xffff); + j += j&1; - } + // LEFT + int ypos = 1 + (j/2)*7; + ypos -= n * CELLHEIGHT; + if (electrical_delay != 0) { + // draw electrical delay + int xpos = 29; + int ypos = 1 + ((j+1)/2)*FONT_HEIGHT; + xpos -= m * CELLWIDTH -CELLOFFSETX; + ypos -= n * CELLHEIGHT; + chsnprintf(buf, sizeof buf, "Edelay"); + cell_drawstring_7x13(w, h, buf, xpos, ypos, 0xffff); + xpos += 7 * 7; + int n = string_value_with_prefix(buf, sizeof buf, electrical_delay * 1e-12, 's'); + cell_drawstring_7x13(w, h, buf, xpos, ypos, 0xffff); + xpos += n * 7 + 5; + float light_speed_ps = 299792458e-12; //(m/ps) + string_value_with_prefix(buf, sizeof buf, electrical_delay * light_speed_ps * velocity_factor / 100.0, 'm'); + cell_drawstring_7x13(w, h, buf, xpos, ypos, 0xffff); + } // draw marker frequency - int xpos = 230; - int ypos = 1 + (j/2)*FONT_HEIGHT; + int xpos = 260; + ypos = 1 + (j/2)*FONT_HEIGHT; xpos -= m * CELLWIDTH -CELLOFFSETX; ypos -= n * CELLHEIGHT; chsnprintf(buf, sizeof buf, "%d:", active_marker + 1); @@ -1799,10 +1692,10 @@ cell_draw_marker_info(int m, int n, int w, int h) // draw marker delta if (previous_marker >= 0 && active_marker != previous_marker && markers[previous_marker].enabled) { int idx0 = markers[previous_marker].index; - xpos = 230; + xpos = 260; xpos -= m * CELLWIDTH -CELLOFFSETX; ypos += 13; - chsnprintf(buf, sizeof buf, "\001%d:", previous_marker+1); + chsnprintf(buf, sizeof buf, "D%d:", previous_marker+1); cell_drawstring_7x13(w, h, buf, xpos, ypos, 0xffff); xpos += 23; if ((domain_mode & DOMAIN_MODE) == DOMAIN_FREQ) { @@ -1816,10 +1709,11 @@ cell_draw_marker_info(int m, int n, int w, int h) } cell_drawstring_7x13(w, h, buf, xpos, ypos, 0xffff); } + mark_map(m, n); + } -void -frequency_string(char *buf, size_t len, int32_t freq) +static void frequency_string(char *buf, size_t len, int32_t freq) { if (freq < 0) { freq = -freq; @@ -1840,8 +1734,7 @@ frequency_string(char *buf, size_t len, int32_t freq) } } -void -draw_frequencies(void) +void draw_frequencies(void) { char buf[24]; if ((domain_mode & DOMAIN_MODE) == DOMAIN_FREQ) { @@ -1851,40 +1744,40 @@ draw_frequencies(void) strcpy(buf, "START "); frequency_string(buf+6, 24-6, start); strcat(buf, " "); - ili9341_drawstring_7x13(buf, OFFSETX, HEIGHT, 0xffff, 0x0000); + ili9341_drawstring_7x13(buf, OFFSETX, HEIGHT+1, 0xffff, 0x0000); strcpy(buf, "STOP "); frequency_string(buf+5, 24-5, stop); strcat(buf, " "); - ili9341_drawstring_7x13(buf, 205, HEIGHT, 0xffff, 0x0000); + ili9341_drawstring_7x13(buf, 310, HEIGHT+1, 0xffff, 0x0000); } else if (frequency1 < 0) { int fcenter = frequency0; int fspan = -frequency1; strcpy(buf, "CENTER "); frequency_string(buf+7, 24-7, fcenter); strcat(buf, " "); - ili9341_drawstring_7x13(buf, OFFSETX, HEIGHT, 0xffff, 0x0000); + ili9341_drawstring_7x13(buf, OFFSETX, HEIGHT+1, 0xffff, 0x0000); strcpy(buf, "SPAN "); frequency_string(buf+5, 24-5, fspan); strcat(buf, " "); - ili9341_drawstring_7x13(buf, 205, HEIGHT, 0xffff, 0x0000); + ili9341_drawstring_7x13(buf, 310, HEIGHT+1, 0xffff, 0x0000); } else { int fcenter = frequency0; chsnprintf(buf, 24, "CW %d.%03d %03d MHz ", (int)(fcenter / 1000000), (int)((fcenter / 1000) % 1000), (int)(fcenter % 1000)); - ili9341_drawstring_7x13(buf, OFFSETX, HEIGHT, 0xffff, 0x0000); + ili9341_drawstring_7x13(buf, OFFSETX, HEIGHT+1, 0xffff, 0x0000); chsnprintf(buf, 24, " "); - ili9341_drawstring_7x13(buf, 205, HEIGHT, 0xffff, 0x0000); + ili9341_drawstring_7x13(buf, 310, HEIGHT+1, 0xffff, 0x0000); } } else { strcpy(buf, "START 0s "); - ili9341_drawstring_7x13(buf, OFFSETX, HEIGHT, 0xffff, 0x0000); + ili9341_drawstring_7x13(buf, OFFSETX, HEIGHT+1, 0xffff, 0x0000); strcpy(buf, "STOP "); - chsnprintf(buf+5, 24-5, "%d ns", (uint16_t)(time_of_index(SWEEP_POINTS) * 1e9)); + chsnprintf(buf+5, 24-5, "%d ns", (uint16_t)(time_of_index(POINT_COUNT-1) * 1e9)); strcat(buf, " "); - ili9341_drawstring_7x13(buf, 205, HEIGHT, 0xffff, 0x0000); + ili9341_drawstring_7x13(buf, 310, HEIGHT+1, 0xffff, 0x0000); } } @@ -1932,15 +1825,18 @@ draw_cal_status(void) #endif -void -draw_battery_status(void) +void draw_battery_status(void) { + chMtxLock(&mutex_ili9341); // [protect spi_buffer] int w = 10, h = 14; int x = 0, y = 0; int i, c; uint16_t *buf = spi_buffer; uint8_t vbati = vbat2bati(vbat); - uint16_t col = vbati == 0 ? RGB565(0, 255, 0) : RGB565(0, 0, 240); + uint16_t col = vbati == 0 ? RGBHEX(0xff0000) : RGBHEX(0x1fe300); + if (palReadPort(GPIOA) & 0x0010) { + col = RGBHEX(0xffff00); + } memset(spi_buffer, 0, w * h * 2); // battery head @@ -2000,23 +1896,11 @@ draw_battery_status(void) buf[y * w + x++] = col; ili9341_bulk(0, 1, w, h); +chMtxUnlock(&mutex_ili9341); // [/protect spi_buffer] } -void draw_pll_lock_error(void) -{ - int y = 1+7*2; - ili9341_fill(0, y, 10, 4*7, RGBHEX(0x000000)); - y += 4; - ili9341_fill(1, y, 8, 3*7+4, RGBHEX(0xff0000)); - y += 2; -// ili9341_drawchar_5x7('P', 3, y, RGBHEX(0x000000), RGBHEX(0xff0000)); - y+=7; -// ili9341_drawchar_5x7('L', 3, y, RGBHEX(0x000000), RGBHEX(0xff0000)); - y+=7; - // ili9341_drawchar_5x7('L', 3, y, RGBHEX(0x000000), RGBHEX(0xff0000)); -} - -void request_to_redraw_grid(void) +void +request_to_redraw_grid(void) { force_set_markmap(); redraw_request |= REDRAW_CELLS; diff --git a/si5351.c b/si5351.c index 7398a5e7..d02a34f4 100644 --- a/si5351.c +++ b/si5351.c @@ -82,6 +82,7 @@ static bool si5351_wait_ready(void) return false; } +#if 1 static void si5351_wait_pll_lock(void) { systime_t start = chVTGetSystemTime(); @@ -97,11 +98,12 @@ static void si5351_wait_pll_lock(void) status = 0xff; // comm timeout if ((status & 0x60) == 0) return; - chThdSleepMilliseconds(10); } pll_lock_failed = true; } +#endif + bool si5351_init(void) { if (!si5351_wait_ready()) @@ -116,7 +118,7 @@ bool si5351_init(void) return true; } -void si5351_disable_output(void) +static void si5351_disable_output(void) { uint8_t reg[4]; si5351_write(SI5351_REG_3_OUTPUT_ENABLE_CONTROL, 0xff); @@ -127,21 +129,22 @@ void si5351_disable_output(void) si5351_bulk_write(reg, 4); } -void si5351_enable_output(void) +static void si5351_enable_output(void) { si5351_write(SI5351_REG_3_OUTPUT_ENABLE_CONTROL, 0x00); } -void si5351_reset_pll(void) +static void si5351_reset_pll(void) { //si5351_write(SI5351_REG_177_PLL_RESET, SI5351_PLL_RESET_A | SI5351_PLL_RESET_B); si5351_write(SI5351_REG_177_PLL_RESET, 0xAC); } -void si5351_setupPLL(uint8_t pll, /* SI5351_PLL_A or SI5351_PLL_B */ - uint8_t mult, - uint32_t num, - uint32_t denom) +static void si5351_setupPLL( + uint8_t pll, /* SI5351_PLL_A or SI5351_PLL_B */ + uint8_t mult, + uint32_t num, + uint32_t denom) { /* Get the appropriate starting point for the PLL registers */ const uint8_t pllreg_base[] = { @@ -194,14 +197,14 @@ void si5351_setupPLL(uint8_t pll, /* SI5351_PLL_A or SI5351_PLL_B */ si5351_bulk_write(reg, 9); } -void -si5351_setupMultisynth(uint8_t output, - uint8_t pllSource, - uint32_t div, // 4,6,8, 8+ ~ 900 - uint32_t num, - uint32_t denom, - uint32_t rdiv, // SI5351_R_DIV_1~128 - uint8_t drive_strength) +static void si5351_setupMultisynth( + uint8_t output, + uint8_t pllSource, + uint32_t div, // 4,6,8, 8+ ~ 900 + uint32_t num, + uint32_t denom, + uint32_t rdiv, // SI5351_R_DIV_1~128 + uint8_t drive_strength) { /* Get the appropriate starting point for the PLL registers */ const uint8_t msreg_base[] = { @@ -269,8 +272,7 @@ si5351_setupMultisynth(uint8_t output, si5351_write(clkctrl[output], dat); } -static uint32_t -gcd(uint32_t x, uint32_t y) +static uint32_t gcd(uint32_t x, uint32_t y) { uint32_t z; while (y != 0) { @@ -285,9 +287,9 @@ gcd(uint32_t x, uint32_t y) #define PLL_N 32 #define PLLFREQ (XTALFREQ * PLL_N) -void -si5351_set_frequency_fixedpll(int channel, int pll, int pllfreq, int freq, - uint32_t rdiv, uint8_t drive_strength) +static void si5351_set_frequency_fixedpll( + int channel, int pll, int pllfreq, int freq, + uint32_t rdiv, uint8_t drive_strength) { int32_t div = pllfreq / freq; // range: 8 ~ 1800 int32_t num = pllfreq - freq * div; @@ -303,9 +305,9 @@ si5351_set_frequency_fixedpll(int channel, int pll, int pllfreq, int freq, si5351_setupMultisynth(channel, pll, div, num, denom, rdiv, drive_strength); } -void -si5351_set_frequency_fixeddiv(int channel, int pll, int freq, int div, - uint8_t drive_strength) +static void si5351_set_frequency_fixeddiv( + int channel, int pll, int freq, int div, + uint8_t drive_strength) { int32_t pllfreq = freq * div; int32_t multi = pllfreq / XTALFREQ; @@ -327,8 +329,7 @@ si5351_set_frequency_fixeddiv(int channel, int pll, int freq, int div, * 100~150MHz fractional PLL 600-900MHz, fixed divider 6 * 150~200MHz fractional PLL 600-900MHz, fixed divider 4 */ -void -si5351_set_frequency(int channel, int freq, uint8_t drive_strength) +void si5351_set_frequency(int channel, int freq, uint8_t drive_strength) { if (freq <= 100000000) { si5351_setupPLL(SI5351_PLL_B, 32, 0, 1); @@ -338,16 +339,11 @@ si5351_set_frequency(int channel, int freq, uint8_t drive_strength) } else { si5351_set_frequency_fixeddiv(channel, SI5351_PLL_B, freq, 4, drive_strength); } - si5351_wait_pll_lock(); } static int current_band = -1; -#define DELAY_NORMAL 3 -#define DELAY_BANDCHANGE 10 -#define DELAY_LOWBAND 2 - /* * configure output as follows: * CLK0: frequency + offset @@ -355,14 +351,20 @@ static int current_band = -1; * CLK2: fixed 8MHz */ #define CLK2_FREQUENCY 8000000L -int -si5351_set_frequency_with_offset(uint32_t freq, int offset, uint8_t drive_strength) +int si5351_set_frequency_with_offset(uint32_t freq, int offset, uint8_t drive_strength) { int band; - int delay = DELAY_NORMAL; + int delay = 3; uint32_t ofreq = freq + offset; uint32_t rdiv = SI5351_R_DIV_1; - if (freq >= config.harmonic_freq_threshold * 3) { +#if 0 + if (freq >= config.harmonic_freq_threshold * 5) { + freq /= 7; + ofreq /= 9; + } + else +#endif +if (freq >= config.harmonic_freq_threshold * 3) { freq /= 5; ofreq /= 7; } else if (freq >= config.harmonic_freq_threshold) { @@ -437,16 +439,13 @@ si5351_set_frequency_with_offset(uint32_t freq, int offset, uint8_t drive_streng } if (current_band != band) { - si5351_reset_pll(); si5351_reset_pll(); si5351_wait_pll_lock(); #if 1 si5351_enable_output(); #endif - delay += DELAY_BANDCHANGE; - } - //if (band == 0) - // delay += DELAY_LOWBAND; + delay += 10; + } current_band = band; return delay; diff --git a/si5351.h b/si5351.h index feb24f14..6792143c 100644 --- a/si5351.h +++ b/si5351.h @@ -79,19 +79,7 @@ #define SI5351_CRYSTAL_FREQ_25MHZ 25000000 bool si5351_init(void); - -void si5351_setupPLL(uint8_t pll, /* SI5351_PLL_A or SI5351_PLL_B */ - uint8_t mult, - uint32_t num, - uint32_t denom); -void si5351_setupMultisynth(uint8_t output, - uint8_t pllSource, - uint32_t div, - uint32_t num, - uint32_t denom, - uint32_t rdiv, - uint8_t drive_strength); - void si5351_set_frequency(int channel, int freq, uint8_t drive_strength); +int si5351_set_frequency_with_offset(uint32_t freq, int offset, uint8_t drive_strength); #endif //__SI5351_H__ diff --git a/tlv320aic3204.c b/tlv320aic3204.c index cb86ad59..4ebfb9ed 100644 --- a/tlv320aic3204.c +++ b/tlv320aic3204.c @@ -79,15 +79,14 @@ static const uint8_t conf_data_routing[] = { 0 // sentinel }; -const uint8_t conf_data_unmute[] = { +static const uint8_t conf_data_unmute[] = { 2, 0x00, 0x00, /* Select Page 0 */ 2, 0x51, 0xc0, /* Power up Left and Right ADC Channels */ 2, 0x52, 0x00, /* Unmute Left and Right ADC Digital Volume Control */ 0 // sentinel }; -static void -tlv320aic3204_bulk_write(const uint8_t *buf, int len) +static void tlv320aic3204_bulk_write(const uint8_t *buf, int len) { int addr = AIC3204_ADDR; i2cAcquireBus(&I2CD1); @@ -96,8 +95,7 @@ tlv320aic3204_bulk_write(const uint8_t *buf, int len) } #if 0 -static int -tlv320aic3204_read(uint8_t d0) +static int tlv320aic3204_read(uint8_t d0) { int addr = AIC3204_ADDR; uint8_t buf[] = { d0 }; @@ -108,8 +106,7 @@ tlv320aic3204_read(uint8_t d0) } #endif -static void -tlv320aic3204_config(const uint8_t *data) +static void tlv320aic3204_config(const uint8_t *data) { const uint8_t *p = data; while (*p) { diff --git a/ui.c b/ui.c index 662092bd..c3aa2120 100644 --- a/ui.c +++ b/ui.c @@ -30,9 +30,8 @@ #endif uistat_t uistat = { - digit: 6, - current_trace: 0, - lever_mode: LM_MARKER + .digit = 6, + .current_trace = 0 }; @@ -48,7 +47,7 @@ uistat_t uistat = { #define BUTTON_DOWN_LONG_TICKS 5000 /* 1sec */ #define BUTTON_DOUBLE_TICKS 5000 /* 500ms */ #define BUTTON_REPEAT_TICKS 1000 /* 100ms */ -#define BUTTON_DEBOUNCE_TICKS 80 +#define BUTTON_DEBOUNCE_TICKS 200 /* lever switch assignment */ #define BIT_UP1 3 @@ -56,9 +55,9 @@ uistat_t uistat = { #define BIT_DOWN1 1 #define READ_PORT() palReadPort(GPIOA) -#define BUTTON_MASK 0b1111 +#define BUTTON_MASK (0x0f) -static uint16_t last_button = 0b0000; +static uint16_t last_button = 0; static uint32_t last_button_down_ticks; static uint32_t last_button_repeat_ticks; static int8_t inhibit_until_release = FALSE; @@ -75,19 +74,43 @@ enum { KM_START, KM_STOP, KM_CENTER, KM_SPAN, KM_CW, KM_SCALE, KM_REFPOS, KM_EDELAY, KM_VELOCITY_FACTOR, KM_SCALEDELAY }; -uint8_t ui_mode = UI_NORMAL; -uint8_t keypad_mode; -int8_t selection = 0; +static uint8_t ui_mode = UI_NORMAL; +static uint8_t keypad_mode; +static int8_t selection = 0; -typedef struct { +typedef void (*menuaction_cb_t)(int item); + +typedef struct menuitem_t menuitem_t; + +struct menuitem_t { uint8_t type; - char *label; - const void *reference; -} menuitem_t; + char* label; + union { + const menuaction_cb_t pFunc; + const menuitem_t* pMenu; + }; +}; + +// type of menu item +enum { + MT_NONE, + MT_BLANK, + MT_SUBMENU, + MT_CALLBACK, + MT_CANCEL, +// MT_CLOSE +}; + +#define MENUITEM_MENU(text, pmenu) { .type=MT_SUBMENU, .label=text, .pMenu=pmenu } +#define MENUITEM_FUNC(text, pfunc) { .type=MT_CALLBACK, .label=text, .pFunc=pfunc } +//#define MENUITEM_CLOSE { .type=MT_CLOSE, .label="CLOSE", .pMenu=NULL } +#define MENUITEM_BACK { .type=MT_CANCEL, .label=S_LARROW" BACK", .pMenu=NULL } +#define MENUITEM_END { .type=MT_NONE, .label=NULL, .pMenu=NULL } /* sentinel */ + -int8_t last_touch_status = FALSE; -int16_t last_touch_x; -int16_t last_touch_y; +static int8_t last_touch_status = FALSE; +static int16_t last_touch_x; +static int16_t last_touch_y; //int16_t touch_cal[4] = { 1000, 1000, 10*16, 12*16 }; //int16_t touch_cal[4] = { 620, 600, 130, 180 }; #define EVT_TOUCH_NONE 0 @@ -104,21 +127,230 @@ int awd_count; #define KP_DONE 1 #define KP_CANCEL 2 -char kp_buf[11]; -int8_t kp_index = 0; +static char kp_buf[11]; +static int8_t kp_index = 0; -void ui_mode_normal(void); -void ui_mode_menu(void); -void ui_mode_numeric(int _keypad_mode); -void ui_mode_keypad(int _keypad_mode); -void draw_menu(void); -void leave_ui_mode(void); -void erase_menu_buttons(void); -void ui_process_keypad(void); -static void ui_process_numeric(void); +static void ui_mode_normal(void); +static void ui_mode_menu(void); +static void ui_mode_numeric(int _keypad_mode); +static void ui_mode_keypad(int _keypad_mode); +static void draw_menu(void); +static void leave_ui_mode(void); +static void erase_menu_buttons(void); +static void ui_process_keypad(void); +//static void ui_process_numeric(void); static void menu_push_submenu(const menuitem_t *submenu); +static void menu_move_back(void); +static void menu_calop_cb(int item); +static void menu_caldone_cb(int item); +static void menu_save_cb(int item); +static void menu_cal2_cb(int item); +static void menu_trace_cb(int item); +static void menu_format2_cb(int item); +static void menu_format_cb(int item); +static void menu_scale_cb(int item); +static void menu_channel_cb(int item); +static void menu_transform_window_cb(int item); +static void menu_transform_cb(int item); +static void menu_stimulus_cb(int item); +static void menu_marker_sel_cb(int item); +static void menu_marker_op_cb(int item); +static void menu_recall_cb(int item); +static void menu_dfu_cb(int item); +static void menu_config_cb(int item); + +// ===[MENU DEFINITION]========================================================= +static const menuitem_t menu_calop[] = { + MENUITEM_FUNC("OPEN", menu_calop_cb), + MENUITEM_FUNC("SHORT", menu_calop_cb), + MENUITEM_FUNC("LOAD", menu_calop_cb), + MENUITEM_FUNC("ISOLN", menu_calop_cb), + MENUITEM_FUNC("THRU", menu_calop_cb), + MENUITEM_FUNC("DONE", menu_caldone_cb), + MENUITEM_BACK, + MENUITEM_END +}; + +static const menuitem_t menu_save[] = { + MENUITEM_FUNC("SAVE 0", menu_save_cb), + MENUITEM_FUNC("SAVE 1", menu_save_cb), + MENUITEM_FUNC("SAVE 2", menu_save_cb), + MENUITEM_FUNC("SAVE 3", menu_save_cb), + #if !defined(ANTENNA_ANALYZER) + MENUITEM_FUNC("SAVE 4", menu_save_cb), + #endif + MENUITEM_BACK, + MENUITEM_END +}; + +static const menuitem_t menu_cal[] = { + MENUITEM_MENU("CALIBRATE", menu_calop), + MENUITEM_MENU("SAVE", menu_save), + MENUITEM_FUNC("RESET", menu_cal2_cb), + MENUITEM_FUNC("CORRECTION", menu_cal2_cb), + MENUITEM_BACK, + MENUITEM_END +}; + +static const menuitem_t menu_trace[] = { + MENUITEM_FUNC("TRACE 0", menu_trace_cb), + MENUITEM_FUNC("TRACE 1", menu_trace_cb), +// #if !defined(ANTENNA_ANALYZER) + MENUITEM_FUNC("TRACE 2", menu_trace_cb), + MENUITEM_FUNC("TRACE 3", menu_trace_cb), +// #endif + MENUITEM_BACK, + MENUITEM_END +}; + +static const menuitem_t menu_format2[] = { + MENUITEM_FUNC("POLAR", menu_format2_cb), + MENUITEM_FUNC("LINEAR", menu_format2_cb), + MENUITEM_FUNC("REAL", menu_format2_cb), + MENUITEM_FUNC("IMAG", menu_format2_cb), + MENUITEM_FUNC("RESISTANCE", menu_format2_cb), + MENUITEM_FUNC("REACTANCE", menu_format2_cb), + MENUITEM_BACK, + MENUITEM_END +}; + +static const menuitem_t menu_format[] = { + MENUITEM_FUNC("LOGMAG", menu_format_cb), + MENUITEM_FUNC("PHASE", menu_format_cb), + MENUITEM_FUNC("DELAY", menu_format_cb), + MENUITEM_FUNC("SMITH", menu_format_cb), + MENUITEM_FUNC("SWR", menu_format_cb), + MENUITEM_MENU(S_RARROW" MORE", menu_format2), + //MENUITEM_FUNC("LINEAR", menu_format_cb), + //MENUITEM_FUNC("SWR", menu_format_cb), + MENUITEM_BACK, + MENUITEM_END +}; + +static const menuitem_t menu_scale[] = { + MENUITEM_FUNC("SCALE/DIV", menu_scale_cb), + MENUITEM_FUNC("\2REFERENCE\0POSITION", menu_scale_cb), + MENUITEM_FUNC("\2ELECTRICAL\0DELAY", menu_scale_cb), + MENUITEM_BACK, + MENUITEM_END +}; + + +static const menuitem_t menu_channel[] = { + MENUITEM_FUNC("\2CH0\0REFLECT", menu_channel_cb), + MENUITEM_FUNC("\2CH1\0THROUGH", menu_channel_cb), + MENUITEM_BACK, + MENUITEM_END +}; + +static const menuitem_t menu_transform_window[] = { + MENUITEM_FUNC("MINIMUM", menu_transform_window_cb), + MENUITEM_FUNC("NORMAL", menu_transform_window_cb), + MENUITEM_FUNC("MAXIMUM", menu_transform_window_cb), + MENUITEM_BACK, + MENUITEM_END +}; + +static const menuitem_t menu_transform[] = { + MENUITEM_FUNC("\2TRANSFORM\0ON", menu_transform_cb), + MENUITEM_FUNC("\2LOW PASS\0IMPULSE", menu_transform_cb), + MENUITEM_FUNC("\2LOW PASS\0STEP", menu_transform_cb), + MENUITEM_FUNC("BANDPASS", menu_transform_cb), + MENUITEM_MENU("WINDOW", menu_transform_window), + MENUITEM_FUNC("\2VELOCITY\0FACTOR", menu_transform_cb), + MENUITEM_BACK, + MENUITEM_END +}; + +static const menuitem_t menu_display[] = { + MENUITEM_MENU("TRACE", menu_trace), + MENUITEM_MENU("FORMAT", menu_format), + MENUITEM_MENU("SCALE", menu_scale), + MENUITEM_MENU("CHANNEL", menu_channel), + MENUITEM_MENU("TRANSFORM", menu_transform), + MENUITEM_BACK, + MENUITEM_END +}; + +static const menuitem_t menu_stimulus[] = { + MENUITEM_FUNC("START", menu_stimulus_cb), + MENUITEM_FUNC("STOP", menu_stimulus_cb), + MENUITEM_FUNC("CENTER", menu_stimulus_cb), + MENUITEM_FUNC("SPAN", menu_stimulus_cb), + MENUITEM_FUNC("CW FREQ", menu_stimulus_cb), + MENUITEM_FUNC("\2PAUSE\0SWEEP", menu_stimulus_cb), + MENUITEM_BACK, + MENUITEM_END +}; + +static const menuitem_t menu_marker_sel[] = { + MENUITEM_FUNC("MARKER 1", menu_marker_sel_cb), + MENUITEM_FUNC("MARKER 2", menu_marker_sel_cb), + MENUITEM_FUNC("MARKER 3", menu_marker_sel_cb), + MENUITEM_FUNC("MARKER 4", menu_marker_sel_cb), + MENUITEM_FUNC("ALL OFF", menu_marker_sel_cb), + MENUITEM_BACK, + MENUITEM_END +}; + +static const menuitem_t menu_marker[] = { + MENUITEM_MENU("\2SELECT\0MARKER", menu_marker_sel), + MENUITEM_FUNC(S_RARROW"START", menu_marker_op_cb), + MENUITEM_FUNC(S_RARROW"STOP", menu_marker_op_cb), + MENUITEM_FUNC(S_RARROW"CENTER", menu_marker_op_cb), + MENUITEM_FUNC(S_RARROW"SPAN", menu_marker_op_cb), + MENUITEM_BACK, + MENUITEM_END +}; + +static const menuitem_t menu_recall[] = { + MENUITEM_FUNC("RECALL 0", menu_recall_cb), + MENUITEM_FUNC("RECALL 1", menu_recall_cb), + MENUITEM_FUNC("RECALL 2", menu_recall_cb), + MENUITEM_FUNC("RECALL 3", menu_recall_cb), + #if !defined(ANTENNA_ANALYZER) + MENUITEM_FUNC("RECALL 4", menu_recall_cb), + #endif + MENUITEM_BACK, + MENUITEM_END +}; + +static const menuitem_t menu_dfu[] = { + MENUITEM_FUNC("\2RESET AND\0ENTER DFU", menu_dfu_cb), + MENUITEM_BACK, + MENUITEM_END +}; + +static const menuitem_t menu_config[] = { + MENUITEM_FUNC("TOUCH CAL", menu_config_cb), + MENUITEM_FUNC("TOUCH TEST", menu_config_cb), + MENUITEM_FUNC("SAVE", menu_config_cb), + MENUITEM_FUNC("VERSION", menu_config_cb), + MENUITEM_MENU(S_RARROW"DFU", menu_dfu), + MENUITEM_BACK, + MENUITEM_END +}; + +static const menuitem_t menu_top[] = { + MENUITEM_MENU("DISPLAY", menu_display), + MENUITEM_MENU("MARKER", menu_marker), + MENUITEM_MENU("STIMULUS", menu_stimulus), + MENUITEM_MENU("CAL", menu_cal), + MENUITEM_MENU("RECALL", menu_recall), + MENUITEM_MENU("CONFIG", menu_config), + // MENUITEM_CLOSE, + MENUITEM_END +}; + +#define MENU_STACK_DEPTH_MAX 4 +static uint8_t menu_current_level = 0; +static const menuitem_t *menu_stack[MENU_STACK_DEPTH_MAX] = { + menu_top, NULL, NULL, NULL +}; + +// ===[/MENU DEFINITION]======================================================== @@ -203,8 +435,7 @@ static int btn_wait_release(void) } } -int -touch_measure_y(void) +static int touch_measure_y(void) { int v; // open Y line @@ -223,8 +454,7 @@ touch_measure_y(void) return v; } -int -touch_measure_x(void) +static int touch_measure_x(void) { int v; // open X line @@ -243,8 +473,7 @@ touch_measure_x(void) return v; } -void -touch_prepare_sense(void) +static void touch_prepare_sense(void) { // open Y line palSetPadMode(GPIOB, 1, PAL_MODE_INPUT_PULLDOWN ); @@ -256,21 +485,19 @@ touch_prepare_sense(void) palSetPad(GPIOA, 6); } -void -touch_start_watchdog(void) +void touch_start_watchdog(void) { touch_prepare_sense(); adc_start_analog_watchdogd(ADC1, ADC_CHSELR_CHSEL7); } -int -touch_status(void) +static int touch_status(void) { touch_prepare_sense(); return adc_single_read(ADC1, ADC_CHSELR_CHSEL7) > TOUCH_THRESHOLD; } -int touch_check(void) +static int touch_check(void) { int stat = touch_status(); if (stat) { @@ -299,7 +526,7 @@ int touch_check(void) } } -void touch_wait_release(void) +static void touch_wait_release(void) { int status; /* wait touch release */ @@ -308,10 +535,7 @@ void touch_wait_release(void) } while(status != EVT_TOUCH_RELEASED); } -extern void ili9341_line(int, int, int, int, int); - -void -touch_cal_exec(void) +void touch_cal_exec(void) { int status; int x1, x2, y1, y2; @@ -356,8 +580,7 @@ ili9341_drawstring_7x13("TOUCH LOWER RIGHT", 356, 254, 0xffff, 0x0000); touch_start_watchdog(); } -void -touch_draw_test(void) +void touch_draw_test(void) { int status; int x0, y0; @@ -369,7 +592,7 @@ touch_draw_test(void) #if !defined(ST7796S) ili9341_drawstring_5x7("TOUCH TEST: DRAG PANEL", OFFSETX, 233, 0xffff, 0x0000); #else -ili9341_drawstring_7x13("TOUCH TEST: DRAG PANEL", OFFSETX, 267, 0xffff, 0x0000); +ili9341_drawstring_7x13("TOUCH TEST: DRAG PANEL", OFFSETX, 307, 0xffff, 0x0000); #endif do { status = touch_check(); @@ -389,8 +612,7 @@ ili9341_drawstring_7x13("TOUCH TEST: DRAG PANEL", OFFSETX, 267, 0xffff, 0x0000); } -void -touch_position(int *x, int *y) +void touch_position(int *x, int *y) { *x = (last_touch_x - config.touch_cal[0]) * 16 / config.touch_cal[2]; *y = (last_touch_y - config.touch_cal[1]) * 16 / config.touch_cal[3]; @@ -400,29 +622,11 @@ touch_position(int *x, int *y) void show_version(void) { - int x = 5, y = 5; - typedef void (*drawstring_function_t)(const char *, int , int , uint16_t , uint16_t ); - drawstring_function_t drawstring_function; - #if defined(ILI9488) || defined(ILI9486) || defined(ST7796S) - drawstring_function = ili9341_drawstring_7x13; - #else - drawstring_function = ili9341_drawstring_5x7; - #endif - adc_stop(ADC1); - ili9341_fill(0, 0, LCD_WIDTH, LCD_HEIGHT, 0); - ili9341_drawstring_size(BOARD_NAME, x, y, 0xffff, 0x0000, 4); - y += LINE_SPACE*3; - drawstring_function("2016-2019 Copyright @edy555", x, y += LINE_SPACE, 0xffff, 0x0000); - drawstring_function("Licensed under GPL. See: https://github.com/ttrftech/NanoVNA", x, y += LINE_SPACE, 0xffff, 0x0000); - drawstring_function("Version: " VERSION, x, y += LINE_SPACE, 0xffff, 0x0000); - drawstring_function("Build Time: " __DATE__ " - " __TIME__, x, y += LINE_SPACE, 0xffff, 0x0000); - y += LINE_SPACE; - drawstring_function("Kernel: " CH_KERNEL_VERSION, x, y += LINE_SPACE, 0xffff, 0x0000); - drawstring_function("Compiler: " PORT_COMPILER_NAME, x, y += LINE_SPACE, 0xffff, 0x0000); - drawstring_function("Architecture: " PORT_ARCHITECTURE_NAME " Core Variant: " PORT_CORE_VARIANT_NAME, x, y += LINE_SPACE, 0xffff, 0x0000); - drawstring_function("Port Info: " PORT_INFO, x, y += LINE_SPACE, 0xffff, 0x0000); - drawstring_function("Platform: " PLATFORM_NAME, x, y += LINE_SPACE, 0xffff, 0x0000); + + adc_stop(ADC1); + show_logo(); + while (true) { if (touch_check() == EVT_TOUCH_PRESSED) break; @@ -434,7 +638,54 @@ show_version(void) } void -enter_dfu(void) +show_logo(void) +{ + +#if !defined(ST7796S) + int x = 15, y = 30; + ili9341_fill(0, 0, LCD_WIDTH, LCD_HEIGHT, 0); + ili9341_drawstring_size(BOARD_NAME, x+60, y, RGBHEX(0x0000FF), 0x0000, 4); + y += 25; + + ili9341_drawstring_size("NANOVNA.COM", x+100, y += 10, 0xffff, 0x0000, 2); + ili9341_drawstring_5x7("https://github.com/hugen79/NanoVNA-H", x, y += 20, 0xffff, 0x0000); + ili9341_drawstring_5x7("Based on edy555 design", x, y += 10, 0xffff, 0x0000); + ili9341_drawstring_5x7("2016-2019 Copyright @edy555", x, y += 10, 0xffff, 0x0000); + ili9341_drawstring_5x7("Licensed under GPL. See: https://github.com/ttrftech/NanoVNA", x, y += 10, 0xffff, 0x0000); + ili9341_drawstring_5x7("Version: " VERSION, x, y += 10, 0xffff, 0x0000); + ili9341_drawstring_5x7("Build Time: " __DATE__ " - " __TIME__, x, y += 10, 0xffff, 0x0000); +// y += 5; +// ili9341_drawstring_5x7("Kernel: " CH_KERNEL_VERSION, x, y += 10, 0xffff, 0x0000); +// ili9341_drawstring_5x7("Architecture: " PORT_ARCHITECTURE_NAME " Core Variant: " PORT_CORE_VARIANT_NAME, x, y += 10, 0xffff, 0x0000); +// ili9341_drawstring_5x7("Port Info: " PORT_INFO, x, y += 10, 0xffff, 0x0000); +// ili9341_drawstring_5x7("Platform: " PLATFORM_NAME, x, y += 10, 0xffff, 0x0000); + +#else + + int x = 20, y = 30; + ili9341_fill(0, 0, LCD_WIDTH, LCD_HEIGHT, 0); + ili9341_drawstring_size(BOARD_NAME, x+130, y, RGBHEX(0x0000FF), 0x0000, 4); + y += 35; + + ili9341_drawstring_size("NANOVNA.COM", x+150, y += 15, 0xffff, 0x0000, 2); + ili9341_drawstring_7x13("https://github.com/hugen79/NanoVNA-H", x, y += 30, 0xffff, 0x0000); + ili9341_drawstring_7x13("Based on edy555 design", x, y += 15, 0xffff, 0x0000); + ili9341_drawstring_7x13("2016-2019 Copyright @edy555", x, y += 15, 0xffff, 0x0000); + ili9341_drawstring_7x13("Licensed under GPL. See: https://github.com/ttrftech/NanoVNA", x, y += 15, 0xffff, 0x0000); + ili9341_drawstring_7x13("Version: " VERSION, x, y += 15, 0xffff, 0x0000); + ili9341_drawstring_7x13("Build Time: " __DATE__ " - " __TIME__, x, y += 15, 0xffff, 0x0000); + + y += 10; + ili9341_drawstring_7x13("Kernel: " CH_KERNEL_VERSION, x, y += 15, 0xffff, 0x0000); + ili9341_drawstring_7x13("Architecture: " PORT_ARCHITECTURE_NAME " Core Variant: " PORT_CORE_VARIANT_NAME, x, y += 15, 0xffff, 0x0000); + ili9341_drawstring_7x13("Port Info: " PORT_INFO, x, y += 15, 0xffff, 0x0000); + ili9341_drawstring_7x13("Platform: " PLATFORM_NAME, x, y += 15, 0xffff, 0x0000); +#endif +} + + + +void enter_dfu(void) { adc_stop(ADC1); @@ -456,24 +707,8 @@ enter_dfu(void) } -// type of menu item -enum { - MT_NONE, - MT_BLANK, - MT_SUBMENU, - MT_CALLBACK, - MT_CANCEL, - MT_CLOSE -}; - -typedef void (*menuaction_cb_t)(int item); - - -static void menu_move_back(void); - -static void -menu_calop_cb(int item) +static void menu_calop_cb(int item) { switch (item) { case 0: // OPEN @@ -497,20 +732,16 @@ menu_calop_cb(int item) draw_menu(); } -static void -menu_caldone_cb(int item) +static void menu_caldone_cb(int item) { - extern const menuitem_t menu_save[]; - //extern const menuitem_t menu_cal[]; - (void)item; - cal_done(); + (void) item; + cal_done(); draw_cal_status(); menu_move_back(); menu_push_submenu(menu_save); } -static void -menu_cal2_cb(int item) +static void menu_cal2_cb(int item) { switch (item) { case 2: // RESET @@ -527,8 +758,7 @@ menu_cal2_cb(int item) //menu_move_back(); } -static void -menu_recall_cb(int item) +static void menu_recall_cb(int item) { if (item < 0 || item >= 5) return; @@ -540,8 +770,7 @@ menu_recall_cb(int item) } } -static void -menu_config_cb(int item) +static void menu_config_cb(int item) { switch (item) { case 0: @@ -569,8 +798,7 @@ menu_config_cb(int item) } } -static void -menu_dfu_cb(int item) +static void menu_dfu_cb(int item) { switch (item) { case 0: @@ -578,8 +806,7 @@ menu_dfu_cb(int item) } } -static void -menu_save_cb(int item) +static void menu_save_cb(int item) { if (item < 0 || item >= 5) return; @@ -590,24 +817,22 @@ menu_save_cb(int item) } } -static void -choose_active_trace(void) +static void choose_active_trace(void) { int i; if (trace[uistat.current_trace].enabled) // do nothing return; - for (i = 0; i < 4; i++) + for (i = 0; i < TRACE_COUNT ; i++) if (trace[i].enabled) { uistat.current_trace = i; return; } } -static void -menu_trace_cb(int item) +static void menu_trace_cb(int item) { - if (item < 0 || item >= 4) + if (item < 0 || item >= TRACE_COUNT ) return; if (trace[item].enabled) { if (item == uistat.current_trace) { @@ -626,8 +851,7 @@ menu_trace_cb(int item) draw_menu(); } -static void -menu_format_cb(int item) +static void menu_format_cb(int item) { switch (item) { case 0: @@ -652,8 +876,7 @@ menu_format_cb(int item) //redraw_all(); } -static void -menu_format2_cb(int item) +static void menu_format2_cb(int item) { switch (item) { case 0: @@ -680,8 +903,7 @@ menu_format2_cb(int item) ui_mode_normal(); } -static void -menu_channel_cb(int item) +static void menu_channel_cb(int item) { if (item < 0 || item >= 2) return; @@ -690,8 +912,7 @@ menu_channel_cb(int item) ui_mode_normal(); } -static void -menu_transform_window_cb(int item) +static void menu_transform_window_cb(int item) { // TODO switch (item) { @@ -710,8 +931,7 @@ menu_transform_window_cb(int item) } } -static void -menu_transform_cb(int item) +static void menu_transform_cb(int item) { int status; switch (item) { @@ -721,7 +941,6 @@ menu_transform_cb(int item) } else { domain_mode = (domain_mode & ~DOMAIN_MODE) | DOMAIN_TIME; } - uistat.lever_mode = LM_MARKER; draw_frequencies(); ui_mode_normal(); break; @@ -741,7 +960,7 @@ menu_transform_cb(int item) status = btn_wait_release(); if (status & EVT_BUTTON_DOWN_LONG) { ui_mode_numeric(KM_VELOCITY_FACTOR); - ui_process_numeric(); +// ui_process_numeric(); } else { ui_mode_keypad(KM_VELOCITY_FACTOR); ui_process_keypad(); @@ -750,11 +969,10 @@ menu_transform_cb(int item) } } -static void -choose_active_marker(void) +static void choose_active_marker(void) { int i; - for (i = 0; i < 4; i++) + for (i = 0; i < MARKER_COUNT; i++) if (markers[i].enabled) { active_marker = i; return; @@ -762,8 +980,7 @@ choose_active_marker(void) active_marker = -1; } -static void -menu_scale_cb(int item) +static void menu_scale_cb(int item) { int status; int km = KM_SCALE + item; @@ -773,15 +990,14 @@ menu_scale_cb(int item) status = btn_wait_release(); if (status & EVT_BUTTON_DOWN_LONG) { ui_mode_numeric(km); - ui_process_numeric(); +// ui_process_numeric(); } else { ui_mode_keypad(km); ui_process_keypad(); } } -static void -menu_stimulus_cb(int item) +static void menu_stimulus_cb(int item) { int status; switch (item) { @@ -790,11 +1006,10 @@ menu_stimulus_cb(int item) case 2: /* CENTER */ case 3: /* SPAN */ case 4: /* CW */ - uistat.lever_mode = item == 3 ? LM_SPAN : LM_CENTER; status = btn_wait_release(); if (status & EVT_BUTTON_DOWN_LONG) { ui_mode_numeric(item); - ui_process_numeric(); +// ui_process_numeric(); } else { ui_mode_keypad(item); ui_process_keypad(); @@ -810,51 +1025,49 @@ menu_stimulus_cb(int item) } -static int32_t -get_marker_frequency(int marker) +static int32_t get_marker_frequency(int marker) { - if (marker < 0 || marker >= 4) + if (marker < 0 || marker >= MARKER_COUNT) return -1; if (!markers[marker].enabled) return -1; return frequencies[markers[marker].index]; } -static void -menu_marker_op_cb(int item) +static void menu_marker_op_cb(int item) { int32_t freq = get_marker_frequency(active_marker); if (freq < 0) return; // no active marker switch (item) { - case 0: /* MARKER->START */ + case 1: /* MARKER->START */ set_sweep_frequency(ST_START, freq); break; - case 1: /* MARKER->STOP */ + case 2: /* MARKER->STOP */ set_sweep_frequency(ST_STOP, freq); break; - case 2: /* MARKER->CENTER */ + case 3: /* MARKER->CENTER */ set_sweep_frequency(ST_CENTER, freq); break; - case 3: /* MARKERS->SPAN */ + case 4: /* MARKERS->SPAN */ { - if (previous_marker == -1 || active_marker == previous_marker) { - int32_t center = get_sweep_frequency(ST_CENTER); - int32_t span = center - freq; - if (span < 0) span = -span; - set_sweep_frequency(ST_SPAN, span * 2); - } else { - int32_t freq2 = get_marker_frequency(previous_marker); - if (freq2 < 0) - return; - if (freq > freq2) { - freq2 = freq; - freq = get_marker_frequency(previous_marker); - } - set_sweep_frequency(ST_START, freq); - set_sweep_frequency(ST_STOP, freq2); + if (previous_marker == active_marker) + return; + int32_t freq2 = get_marker_frequency(previous_marker); + if (freq2 < 0) + return; + if (freq > freq2) { + freq2 = freq; + freq = get_marker_frequency(previous_marker); } + set_sweep_frequency(ST_START, freq); + set_sweep_frequency(ST_STOP, freq2); +#if 0 + int32_t span = (freq - freq2) * 2; + if (span < 0) span = -span; + set_sweep_frequency(ST_SPAN, span); +#endif } break; } @@ -863,40 +1076,7 @@ menu_marker_op_cb(int item) //redraw_all(); } -static void -menu_marker_search_cb(int item) -{ - int i; - if (active_marker == -1) - return; - - switch (item) { - case 0: /* maximum */ - case 1: /* minimum */ - i = marker_search(item); - if (i != -1) - markers[active_marker].index = i; - draw_menu(); - break; - case 2: /* search Left */ - i = marker_search_left(markers[active_marker].index); - if (i != -1) - markers[active_marker].index = i; - draw_menu(); - break; - case 3: /* search right */ - i = marker_search_right(markers[active_marker].index); - if (i != -1) - markers[active_marker].index = i; - draw_menu(); - break; - } - redraw_marker(active_marker, TRUE); - uistat.lever_mode = LM_SEARCH; -} - -void -active_marker_select(int item) +static void active_marker_select(int item) { if (item == -1) { active_marker = previous_marker; @@ -911,10 +1091,9 @@ active_marker_select(int item) } } -static void -menu_marker_sel_cb(int item) +static void menu_marker_sel_cb(int item) { - if (item >= 0 && item < 4) { + if (item >= 0 && item < 4 && item < MARKER_COUNT) { if (markers[item].enabled) { if (item == active_marker) { // disable if active trace is selected @@ -937,210 +1116,9 @@ menu_marker_sel_cb(int item) } redraw_marker(active_marker, TRUE); draw_menu(); - uistat.lever_mode = LM_MARKER; -} - -const menuitem_t menu_calop[] = { - { MT_CALLBACK, "OPEN", menu_calop_cb }, - { MT_CALLBACK, "SHORT", menu_calop_cb }, - { MT_CALLBACK, "LOAD", menu_calop_cb }, - { MT_CALLBACK, "ISOLN", menu_calop_cb }, - { MT_CALLBACK, "THRU", menu_calop_cb }, - { MT_CALLBACK, "DONE", menu_caldone_cb }, - { MT_CANCEL, S_LARROW" BACK", NULL }, - { MT_NONE, NULL, NULL } // sentinel -}; - -const menuitem_t menu_save[] = { - { MT_CALLBACK, "SAVE 0", menu_save_cb }, - { MT_CALLBACK, "SAVE 1", menu_save_cb }, - { MT_CALLBACK, "SAVE 2", menu_save_cb }, - { MT_CALLBACK, "SAVE 3", menu_save_cb }, - { MT_CALLBACK, "SAVE 4", menu_save_cb }, - { MT_CANCEL, S_LARROW" BACK", NULL }, - { MT_NONE, NULL, NULL } // sentinel -}; - -const menuitem_t menu_cal[] = { - { MT_SUBMENU, "CALIBRATE", menu_calop }, - { MT_SUBMENU, "SAVE", menu_save }, - { MT_CALLBACK, "RESET", menu_cal2_cb }, - { MT_CALLBACK, "CORRECTION", menu_cal2_cb }, - { MT_CANCEL, S_LARROW" BACK", NULL }, - { MT_NONE, NULL, NULL } // sentinel -}; - -const menuitem_t menu_trace[] = { - { MT_CALLBACK, "TRACE 0", menu_trace_cb }, - { MT_CALLBACK, "TRACE 1", menu_trace_cb }, - { MT_CALLBACK, "TRACE 2", menu_trace_cb }, - { MT_CALLBACK, "TRACE 3", menu_trace_cb }, - { MT_CANCEL, S_LARROW" BACK", NULL }, - { MT_NONE, NULL, NULL } // sentinel -}; - -const menuitem_t menu_format2[] = { - { MT_CALLBACK, "POLAR", menu_format2_cb }, - { MT_CALLBACK, "LINEAR", menu_format2_cb }, - { MT_CALLBACK, "REAL", menu_format2_cb }, - { MT_CALLBACK, "IMAG", menu_format2_cb }, - { MT_CALLBACK, "RESISTANCE", menu_format2_cb }, - { MT_CALLBACK, "REACTANCE", menu_format2_cb }, - { MT_CANCEL, S_LARROW" BACK", NULL }, - { MT_NONE, NULL, NULL } // sentinel -}; - -const menuitem_t menu_format[] = { - { MT_CALLBACK, "LOGMAG", menu_format_cb }, - { MT_CALLBACK, "PHASE", menu_format_cb }, - { MT_CALLBACK, "DELAY", menu_format_cb }, - { MT_CALLBACK, "SMITH", menu_format_cb }, - { MT_CALLBACK, "SWR", menu_format_cb }, - { MT_SUBMENU, S_RARROW" MORE", menu_format2 }, - //{ MT_CALLBACK, "LINEAR", menu_format_cb }, - //{ MT_CALLBACK, "SWR", menu_format_cb }, - { MT_CANCEL, S_LARROW" BACK", NULL }, - { MT_NONE, NULL, NULL } // sentinel -}; - -const menuitem_t menu_scale[] = { - { MT_CALLBACK, "SCALE/DIV", menu_scale_cb }, - { MT_CALLBACK, "\2REFERENCE\0POSITION", menu_scale_cb }, - { MT_CALLBACK, "\2ELECTRICAL\0DELAY", menu_scale_cb }, - { MT_CANCEL, S_LARROW" BACK", NULL }, - { MT_NONE, NULL, NULL } // sentinel -}; - - -const menuitem_t menu_channel[] = { - { MT_CALLBACK, "\2CH0\0REFLECT", menu_channel_cb }, - { MT_CALLBACK, "\2CH1\0THROUGH", menu_channel_cb }, - { MT_CANCEL, S_LARROW" BACK", NULL }, - { MT_NONE, NULL, NULL } // sentinel -}; - -const menuitem_t menu_transform_window[] = { - { MT_CALLBACK, "MINIMUM", menu_transform_window_cb }, - { MT_CALLBACK, "NORMAL", menu_transform_window_cb }, - { MT_CALLBACK, "MAXIMUM", menu_transform_window_cb }, - { MT_CANCEL, S_LARROW" BACK", NULL }, - { MT_NONE, NULL, NULL } // sentinel -}; - -const menuitem_t menu_transform[] = { - { MT_CALLBACK, "\2TRANSFORM\0ON", menu_transform_cb }, - { MT_CALLBACK, "\2LOW PASS\0IMPULSE", menu_transform_cb }, - { MT_CALLBACK, "\2LOW PASS\0STEP", menu_transform_cb }, - { MT_CALLBACK, "BANDPASS", menu_transform_cb }, - { MT_SUBMENU, "WINDOW", menu_transform_window }, - { MT_CALLBACK, "\2VELOCITY\0FACTOR", menu_transform_cb }, - { MT_CANCEL, S_LARROW" BACK", NULL }, - { MT_NONE, NULL, NULL } // sentinel -}; - -const menuitem_t menu_display[] = { - { MT_SUBMENU, "TRACE", menu_trace }, - { MT_SUBMENU, "FORMAT", menu_format }, - { MT_SUBMENU, "SCALE", menu_scale }, - { MT_SUBMENU, "CHANNEL", menu_channel }, - { MT_SUBMENU, "TRANSFORM", menu_transform }, - { MT_CANCEL, S_LARROW" BACK", NULL }, - { MT_NONE, NULL, NULL } // sentinel -}; - -const menuitem_t menu_stimulus[] = { - { MT_CALLBACK, "START", menu_stimulus_cb }, - { MT_CALLBACK, "STOP", menu_stimulus_cb }, - { MT_CALLBACK, "CENTER", menu_stimulus_cb }, - { MT_CALLBACK, "SPAN", menu_stimulus_cb }, - { MT_CALLBACK, "CW FREQ", menu_stimulus_cb }, - { MT_CALLBACK, "\2PAUSE\0SWEEP", menu_stimulus_cb }, - { MT_CANCEL, S_LARROW" BACK", NULL }, - { MT_NONE, NULL, NULL } // sentinel -}; - -const menuitem_t menu_marker_sel[] = { - { MT_CALLBACK, "MARKER 1", menu_marker_sel_cb }, - { MT_CALLBACK, "MARKER 2", menu_marker_sel_cb }, - { MT_CALLBACK, "MARKER 3", menu_marker_sel_cb }, - { MT_CALLBACK, "MARKER 4", menu_marker_sel_cb }, - { MT_CALLBACK, "ALL OFF", menu_marker_sel_cb }, - { MT_CANCEL, S_LARROW" BACK", NULL }, - { MT_NONE, NULL, NULL } // sentinel -}; - -const menuitem_t menu_marker_ops[] = { - { MT_CALLBACK, S_RARROW"START", menu_marker_op_cb }, - { MT_CALLBACK, S_RARROW"STOP", menu_marker_op_cb }, - { MT_CALLBACK, S_RARROW"CENTER", menu_marker_op_cb }, - { MT_CALLBACK, S_RARROW"SPAN", menu_marker_op_cb }, - { MT_CANCEL, S_LARROW" BACK", NULL }, - { MT_NONE, NULL, NULL } // sentinel -}; - -const menuitem_t menu_marker_search[] = { - //{ MT_CALLBACK, "OFF", menu_marker_search_cb }, - { MT_CALLBACK, "MAXIMUM", menu_marker_search_cb }, - { MT_CALLBACK, "MINIMUM", menu_marker_search_cb }, - { MT_CALLBACK, "\2SEARCH\0" S_LARROW" LEFT", menu_marker_search_cb }, - { MT_CALLBACK, "\2SEARCH\0" S_RARROW" RIGHT", menu_marker_search_cb }, - //{ MT_CALLBACK, "TRACKING", menu_marker_search_cb }, - { MT_CANCEL, S_LARROW" BACK", NULL }, - { MT_NONE, NULL, NULL } // sentinel -}; - -const menuitem_t menu_marker[] = { - { MT_SUBMENU, "\2SELECT\0MARKER", menu_marker_sel }, - { MT_SUBMENU, "SEARCH", menu_marker_search }, - { MT_SUBMENU, "OPERATIONS", menu_marker_ops }, - { MT_CANCEL, S_LARROW" BACK", NULL }, - { MT_NONE, NULL, NULL } // sentinel -}; - -const menuitem_t menu_recall[] = { - { MT_CALLBACK, "RECALL 0", menu_recall_cb }, - { MT_CALLBACK, "RECALL 1", menu_recall_cb }, - { MT_CALLBACK, "RECALL 2", menu_recall_cb }, - { MT_CALLBACK, "RECALL 3", menu_recall_cb }, - { MT_CALLBACK, "RECALL 4", menu_recall_cb }, - { MT_CANCEL, S_LARROW" BACK", NULL }, - { MT_NONE, NULL, NULL } // sentinel -}; - -const menuitem_t menu_dfu[] = { - { MT_CALLBACK, "\2RESET AND\0ENTER DFU", menu_dfu_cb }, - { MT_CANCEL, S_LARROW"CANCEL", NULL }, - { MT_NONE, NULL, NULL } // sentinel -}; - -const menuitem_t menu_config[] = { - { MT_CALLBACK, "TOUCH CAL", menu_config_cb }, - { MT_CALLBACK, "TOUCH TEST", menu_config_cb }, - { MT_CALLBACK, "SAVE", menu_config_cb }, - { MT_CALLBACK, "VERSION", menu_config_cb }, - { MT_SUBMENU, S_RARROW"DFU", menu_dfu }, - { MT_CANCEL, S_LARROW" BACK", NULL }, - { MT_NONE, NULL, NULL } // sentinel -}; - -const menuitem_t menu_top[] = { - { MT_SUBMENU, "DISPLAY", menu_display }, - { MT_SUBMENU, "MARKER", menu_marker }, - { MT_SUBMENU, "STIMULUS", menu_stimulus }, - { MT_SUBMENU, "CAL", menu_cal }, - { MT_SUBMENU, "RECALL", menu_recall }, - { MT_SUBMENU, "CONFIG", menu_config }, - { MT_NONE, NULL, NULL } // sentinel -}; - -#define MENU_STACK_DEPTH_MAX 4 -uint8_t menu_current_level = 0; -const menuitem_t *menu_stack[4] = { - menu_top, NULL, NULL, NULL -}; +} -static void -ensure_selection(void) +static void ensure_selection(void) { const menuitem_t *menu = menu_stack[menu_current_level]; int i; @@ -1182,7 +1160,7 @@ static void menu_move_top(void) } */ -void menu_invoke(int item) +static void menu_invoke(int item) { const menuitem_t *menu = menu_stack[menu_current_level]; menu = &menu[item]; @@ -1190,7 +1168,7 @@ void menu_invoke(int item) switch (menu->type) { case MT_NONE: case MT_BLANK: - case MT_CLOSE: +// case MT_CLOSE: ui_mode_normal(); break; @@ -1199,7 +1177,7 @@ void menu_invoke(int item) break; case MT_CALLBACK: { - menuaction_cb_t cb = (menuaction_cb_t)menu->reference; + menuaction_cb_t cb = menu->pFunc; if (cb == NULL) return; (*cb)(item); @@ -1207,7 +1185,7 @@ void menu_invoke(int item) } case MT_SUBMENU: - menu_push_submenu((const menuitem_t*)menu->reference); + menu_push_submenu(menu->pMenu); break; } } @@ -1238,10 +1216,10 @@ typedef struct { int8_t c; } keypads_t; -const keypads_t *keypads; -uint8_t keypads_last_index; +static const keypads_t *keypads; +static uint8_t keypads_last_index; -const keypads_t keypads_freq[] = { +static const keypads_t keypads_freq[] = { { KP_X(1), KP_Y(3), KP_PERIOD }, { KP_X(0), KP_Y(3), 0 }, { KP_X(0), KP_Y(2), 1 }, @@ -1261,7 +1239,7 @@ const keypads_t keypads_freq[] = { { 0, 0, -1 } }; -const keypads_t keypads_scale[] = { +static const keypads_t keypads_scale[] = { { KP_X(1), KP_Y(3), KP_PERIOD }, { KP_X(0), KP_Y(3), 0 }, { KP_X(0), KP_Y(2), 1 }, @@ -1278,7 +1256,7 @@ const keypads_t keypads_scale[] = { { 0, 0, -1 } }; -const keypads_t keypads_time[] = { +static const keypads_t keypads_time[] = { { KP_X(1), KP_Y(3), KP_PERIOD }, { KP_X(0), KP_Y(3), 0 }, { KP_X(0), KP_Y(2), 1 }, @@ -1292,12 +1270,12 @@ const keypads_t keypads_time[] = { { KP_X(2), KP_Y(0), 9 }, { KP_X(3), KP_Y(1), KP_N }, { KP_X(3), KP_Y(2), KP_P }, - { KP_X(3), KP_Y(3), KP_MINUS }, + { KP_X(3), KP_Y(3), KP_MINUS }, { KP_X(2), KP_Y(3), KP_BS }, { 0, 0, -1 } }; -const keypads_t * const keypads_mode_tbl[] = { +static const keypads_t * const keypads_mode_tbl[] = { keypads_freq, // start keypads_freq, // stop keypads_freq, // center @@ -1310,12 +1288,11 @@ const keypads_t * const keypads_mode_tbl[] = { keypads_time // scale of delay }; -const char * const keypad_mode_label[] = { +static const char * const keypad_mode_label[] = { "START", "STOP", "CENTER", "SPAN", "CW FREQ", "SCALE", "REFPOS", "EDELAY", "VELOCITY%", "DELAY" }; -void -draw_keypad(void) +static void draw_keypad(void) { int i = 0; while (keypads[i].x) { @@ -1328,20 +1305,18 @@ draw_keypad(void) } } -void -draw_numeric_area_frame(void) +static void draw_numeric_area_frame(void) { ili9341_fill(0, LCD_HEIGHT*0.9, LCD_WIDTH, 32, 0xffff); #if !defined(ST7796S) - ili9341_drawstring_5x7(keypad_mode_label[keypad_mode], 10, 220, 0x0000, 0xffff); + ili9341_drawstring_5x7(keypad_mode_label[keypad_mode], 10, LCD_HEIGHT*0.9+4, 0x0000, 0xffff); #else - ili9341_drawstring_7x13(keypad_mode_label[keypad_mode], 10, 220, 0x0000, 0xffff); + ili9341_drawstring_7x13(keypad_mode_label[keypad_mode], 10, LCD_HEIGHT*0.9+4, 0x0000, 0xffff); #endif - ili9341_drawfont(KP_KEYPAD, &NF20x22, LCD_WIDTH-20, LCD_HEIGHT*0.9, 0x0000, 0xffff); + //ili9341_drawfont(KP_KEYPAD, &NF20x22, LCD_WIDTH-20, LCD_HEIGHT*0.9, 0x0000, 0xffff); } -void -draw_numeric_input(const char *buf) +static void draw_numeric_input(const char *buf) { int i = 0; int x = 64; @@ -1361,7 +1336,7 @@ draw_numeric_input(const char *buf) c = -1; if (uistat.digit == 8-i) { - fg = RGB565(128,255,128); + fg = RGBHEX(0xf7131f); focused = TRUE; if (uistat.digit_mode) bg = 0x0000; @@ -1385,8 +1360,7 @@ draw_numeric_input(const char *buf) } } -static int -menu_is_multiline(const char *label, const char **l1, const char **l2) +static int menu_is_multiline(const char *label, const char **l1, const char **l2) { if (label[0] != '\2') return FALSE; @@ -1396,14 +1370,13 @@ menu_is_multiline(const char *label, const char **l1, const char **l2) return TRUE; } -static void -menu_item_modify_attribute(const menuitem_t *menu, int item, - uint16_t *fg, uint16_t *bg) +static void menu_item_modify_attribute( + const menuitem_t *menu, int item, uint16_t *fg, uint16_t *bg) { - if (menu == menu_trace && item < 4) { + if (menu == menu_trace && item < TRACE_COUNT && item < MARKER_COUNT) { if (trace[item].enabled) *bg = config.trace_color[item]; - } else if (menu == menu_marker_sel && item < 4) { + } else if (menu == menu_marker_sel && item < 4&& item < MARKER_COUNT) { if (markers[item].enabled) { *bg = 0x0000; *fg = 0xffff; @@ -1448,8 +1421,7 @@ menu_item_modify_attribute(const menuitem_t *menu, int item, } } -void -draw_menu_buttons(const menuitem_t *menu) +static void draw_menu_buttons(const menuitem_t *menu) { int i = 0; for (i = 0; i < 7; i++) { @@ -1489,8 +1461,7 @@ draw_menu_buttons(const menuitem_t *menu) } } -void -menu_select_touch(int i) +static void menu_select_touch(int i) { selection = i; draw_menu(); @@ -1499,8 +1470,7 @@ menu_select_touch(int i) menu_invoke(i); } -void -menu_apply_touch(void) +static void menu_apply_touch(void) { int touch_x, touch_y; const menuitem_t *menu = menu_stack[menu_current_level]; @@ -1529,14 +1499,12 @@ menu_apply_touch(void) ui_mode_normal(); } -void -draw_menu(void) +static void draw_menu(void) { draw_menu_buttons(menu_stack[menu_current_level]); } -void -erase_menu_buttons(void) +static void erase_menu_buttons(void) { uint16_t bg = 0; #if !defined(ST7796S) @@ -1554,8 +1522,7 @@ erase_numeric_input(void) ili9341_fill(0, LCD_HEIGHT-32, LCD_WIDTH, 32, bg); } -void -leave_ui_mode() +static void leave_ui_mode(void) { if (ui_mode == UI_MENU) { request_to_draw_cells_behind_menu(); @@ -1567,8 +1534,7 @@ leave_ui_mode() } } -void -fetch_numeric_target(void) +static void fetch_numeric_target(void) { switch (keypad_mode) { case KM_START: @@ -1613,7 +1579,8 @@ fetch_numeric_target(void) uistat.previous_value = uistat.value; } -void set_numeric_value(void) +#if 0 +static void set_numeric_value(void) { switch (keypad_mode) { case KM_START: @@ -1646,8 +1613,9 @@ void set_numeric_value(void) } } -void -draw_numeric_area(void) +#endif + +static void draw_numeric_area(void) { char buf[10]; chsnprintf(buf, sizeof buf, "%9d", uistat.value); @@ -1655,8 +1623,7 @@ draw_numeric_area(void) } -void -ui_mode_menu(void) +static void ui_mode_menu(void) { if (ui_mode == UI_MENU) return; @@ -1673,8 +1640,7 @@ ui_mode_menu(void) draw_menu(); } -void -ui_mode_numeric(int _keypad_mode) +static void ui_mode_numeric(int _keypad_mode) { if (ui_mode == UI_NUMERIC) return; @@ -1692,8 +1658,7 @@ ui_mode_numeric(int _keypad_mode) draw_numeric_area(); } -void -ui_mode_keypad(int _keypad_mode) +static void ui_mode_keypad(int _keypad_mode) { if (ui_mode == UI_KEYPAD) return; @@ -1715,8 +1680,7 @@ ui_mode_keypad(int _keypad_mode) draw_numeric_input(""); } -void -ui_mode_normal(void) +static void ui_mode_normal(void) { if (ui_mode == UI_NORMAL) return; @@ -1727,112 +1691,35 @@ ui_mode_normal(void) ui_mode = UI_NORMAL; } -static void -lever_move_marker(int status) -{ - do { - if (active_marker >= 0 && markers[active_marker].enabled) { - if ((status & EVT_DOWN) && markers[active_marker].index > 0) { - markers[active_marker].index--; - markers[active_marker].frequency = frequencies[markers[active_marker].index]; - redraw_marker(active_marker, FALSE); - } - if ((status & EVT_UP) && markers[active_marker].index < 100) { - markers[active_marker].index++; - markers[active_marker].frequency = frequencies[markers[active_marker].index]; - redraw_marker(active_marker, FALSE); - } - } - status = btn_wait_release(); - } while (status != 0); - if (active_marker >= 0) - redraw_marker(active_marker, TRUE); -} - -static void -lever_search_marker(int status) -{ - if (active_marker >= 0) { - if (status & EVT_DOWN) { - int i = marker_search_left(markers[active_marker].index); - if (i != -1) - markers[active_marker].index = i; - } else if (status & EVT_UP) { - int i = marker_search_right(markers[active_marker].index); - if (i != -1) - markers[active_marker].index = i; - } - redraw_marker(active_marker, TRUE); - } -} - -// ex. 10942 -> 10000 -// 6791 -> 5000 -// 341 -> 200 -static uint32_t -step_round(uint32_t v) -{ - // decade step - uint32_t x = 1; - for (x = 1; x*10 < v; x *= 10) - ; - - // 1-2-5 step - if (x * 2 > v) - return x; - else if (x * 5 > v) - return x * 2; - else - return x * 5; -} - -static void -lever_zoom_span(int status) -{ - uint32_t span = get_sweep_frequency(ST_SPAN); - if (status & EVT_UP) { - span = step_round(span - 1); - set_sweep_frequency(ST_SPAN, span); - } else if (status & EVT_DOWN) { - span = step_round(span + 1); - span = step_round(span * 3); - set_sweep_frequency(ST_SPAN, span); - } -} - -static void -lever_move_center(int status) -{ - uint32_t center = get_sweep_frequency(ST_CENTER); - uint32_t span = get_sweep_frequency(ST_SPAN); - span = step_round(span / 3); - if (status & EVT_UP) { - set_sweep_frequency(ST_CENTER, center + span); - } else if (status & EVT_DOWN) { - set_sweep_frequency(ST_CENTER, center - span); - } -} - -static void -ui_process_normal(void) +static void ui_process_normal(void) { int status = btn_check(); if (status != 0) { if (status & EVT_BUTTON_SINGLE_CLICK) { ui_mode_menu(); } else { - switch (uistat.lever_mode) { - case LM_MARKER: lever_move_marker(status); break; - case LM_SEARCH: lever_search_marker(status); break; - case LM_CENTER: lever_move_center(status); break; - case LM_SPAN: lever_zoom_span(status); break; - } + do { + if (active_marker >= 0 && markers[active_marker].enabled) { + if ((status & EVT_DOWN) && markers[active_marker].index > 0) { + markers[active_marker].index--; + markers[active_marker].frequency = frequencies[markers[active_marker].index]; + redraw_marker(active_marker, FALSE); + } + if ((status & EVT_UP) && markers[active_marker].index < (POINT_COUNT-1)) { + markers[active_marker].index++; + markers[active_marker].frequency = frequencies[markers[active_marker].index]; + redraw_marker(active_marker, FALSE); + } + } + status = btn_wait_release(); + } while (status != 0); + if (active_marker >= 0) + redraw_marker(active_marker, TRUE); } } } -static void -ui_process_menu(void) +static void ui_process_menu(void) { int status = btn_check(); if (status != 0) { @@ -1840,16 +1727,18 @@ ui_process_menu(void) menu_invoke(selection); } else { do { - if (status & EVT_UP) { - // close menu if next item is sentinel - if (menu_stack[menu_current_level][selection+1].type == MT_NONE) - goto menuclose; - selection++; + if (status & EVT_UP){ + if (menu_stack[menu_current_level][selection+1].type == MT_NONE) { + ui_mode_normal(); + return; + } + selection++; draw_menu(); } - if (status & EVT_DOWN) { - if (selection == 0) - goto menuclose; + if (status & EVT_DOWN){ + if (selection == 0){ + ui_mode_normal(); + return;} selection--; draw_menu(); } @@ -1857,18 +1746,13 @@ ui_process_menu(void) } while (status != 0); } } - return; - -menuclose: - ui_mode_normal(); } -static int -keypad_click(int key) +static int keypad_click(int key) { int c = keypads[key].c; if ((c >= KP_X1 && c <= KP_G) || c == KP_N || c == KP_P) { - float scale = 1; + int32_t scale = 1; if (c >= KP_X1 && c <= KP_G) { int n = c - KP_X1; while (n-- > 0) @@ -1877,22 +1761,22 @@ keypad_click(int key) scale *= 1000; } /* numeric input done */ - float value = my_atof(kp_buf) * scale; + double value = my_atof(kp_buf) * (double)scale; switch (keypad_mode) { case KM_START: - set_sweep_frequency(ST_START, value); + set_sweep_frequency(ST_START, (int32_t)value); break; case KM_STOP: - set_sweep_frequency(ST_STOP, value); + set_sweep_frequency(ST_STOP, (int32_t)value); break; case KM_CENTER: - set_sweep_frequency(ST_CENTER, value); + set_sweep_frequency(ST_CENTER, (int32_t)value); break; case KM_SPAN: - set_sweep_frequency(ST_SPAN, value); + set_sweep_frequency(ST_SPAN, (int32_t)value); break; case KM_CW: - set_sweep_frequency(ST_CW, value); + set_sweep_frequency(ST_CW, (int32_t)value); break; case KM_SCALE: set_trace_scale(uistat.current_trace, value); @@ -1904,7 +1788,7 @@ keypad_click(int key) set_electrical_delay(value); // pico seconds break; case KM_VELOCITY_FACTOR: - velocity_factor = value; + velocity_factor = (uint8_t)value; break; case KM_SCALEDELAY: set_trace_scale(uistat.current_trace, value * 1e-12); // pico second @@ -1936,8 +1820,7 @@ keypad_click(int key) return KP_CONTINUE; } -static int -keypad_apply_touch(void) +static int keypad_apply_touch(void) { int touch_x, touch_y; int i = 0; @@ -1965,8 +1848,7 @@ keypad_apply_touch(void) return -1; } -static void -numeric_apply_touch(void) +static void numeric_apply_touch(void) { int touch_x, touch_y; touch_position(&touch_x, &touch_y); @@ -2006,8 +1888,8 @@ numeric_apply_touch(void) return; } -static void -ui_process_numeric(void) +#if 0 +static void ui_process_numeric(void) { int status = btn_check(); @@ -2072,9 +1954,10 @@ ui_process_numeric(void) // cancel operation ui_mode_normal(); } +#endif -void -ui_process_keypad(void) + +static void ui_process_keypad(void) { int status; adc_stop(ADC1); @@ -2117,7 +2000,7 @@ ui_process_keypad(void) break; else if (key == -2) { //xxx; - return; + // return; } } } @@ -2129,8 +2012,7 @@ ui_process_keypad(void) touch_start_watchdog(); } -static void -ui_process_lever(void) +static void ui_process_lever(void) { switch (ui_mode) { case UI_NORMAL: @@ -2139,9 +2021,9 @@ ui_process_lever(void) case UI_MENU: ui_process_menu(); break; - case UI_NUMERIC: - ui_process_numeric(); - break; +// case UI_NUMERIC: +// ui_process_numeric(); +// break; case UI_KEYPAD: ui_process_keypad(); break; @@ -2149,8 +2031,7 @@ ui_process_lever(void) } -static void -drag_marker(int t, int m) +static void drag_marker(int t, int m) { int status; /* wait touch release */ @@ -2171,14 +2052,12 @@ drag_marker(int t, int m) } while(status != EVT_TOUCH_RELEASED); } -static int -sq_distance(int x0, int y0) +static int sq_distance(int x0, int y0) { return x0*x0 + y0*y0; } -static int -touch_pickup_marker(void) +static int touch_pickup_marker(void) { int touch_x, touch_y; int m, t; @@ -2186,11 +2065,11 @@ touch_pickup_marker(void) touch_x -= OFFSETX; touch_y -= OFFSETY; - for (m = 0; m < 4; m++) { + for (m = 0; m < MARKER_COUNT; m++) { if (!markers[m].enabled) continue; - for (t = 0; t < 4; t++) { + for (t = 0; t < TRACE_COUNT; t++) { int x, y; if (!trace[t].enabled) continue; @@ -2217,8 +2096,7 @@ touch_pickup_marker(void) } -static -void ui_process_touch(void) +static void ui_process_touch(void) { awd_count++; adc_stop(ADC1); @@ -2251,8 +2129,7 @@ void ui_process_touch(void) touch_start_watchdog(); } -void -ui_process(void) +void ui_process(void) { switch (operation_requested) { case OP_LEVER: @@ -2308,8 +2185,7 @@ static const GPTConfig gpt3cfg = { 0 }; -void -test_touch(int *x, int *y) +void test_touch(int *x, int *y) { adc_stop(ADC1); @@ -2319,14 +2195,12 @@ test_touch(int *x, int *y) touch_start_watchdog(); } -void -handle_touch_interrupt(void) +void handle_touch_interrupt(void) { operation_requested = OP_TOUCH; } -void -ui_init() +void ui_init() { adc_init();