From d32b6ca4101c9ec45ab212c0e832c21622672bb0 Mon Sep 17 00:00:00 2001 From: erikkaashoek Date: Tue, 29 Sep 2026 18:30:28 +0200 Subject: [PATCH 1/2] Add RLE8 screen capture and faster capture (tinySA ULTRA) 'capture' with any argument (tinySA-App sends 'capture rle') sends the screen in the NanoVNA-D (DiSlord) RLE8 format: "BM", width, height, bpp 8, compression 1, the palette, then per row the PackBits packed palette indices. Plain 'capture' still sends raw RGB565. The palette holds all colours the tinySA draws, so the screen is read once: the LCD palette, the waterfall colours and the trace colours ORed over each other and over the colours drawn before them in draw_cell(). The image viewer and the display test set capture_verify, as does a colour not in the palette: the next capture then reads the screen twice to add its colours, or sends raw when there are more than 256. The screen is now read as one DMA stream (ili9341_read_memory_start / next / end) so the next row is read while the current one is packed or sent. Tested on HW V0.4.5.1, pixel exact against raw on normal, 4 trace, waterfall, menu and keypad screens: - capture rle: about 28 KB, 253 ms (was 482 ms raw) - capture: 345 ms (was 482 ms) Co-Authored-By: Claude Opus 5.5 (1M context) --- ili9341.c | 31 ++++++++-- main.c | 152 +++++++++++++++++++++++++++++++++++++++++++++++++- nanovna.h | 12 ++++ plot.c | 25 +++++++++ vna_browser.c | 3 + 5 files changed, 216 insertions(+), 7 deletions(-) diff --git a/ili9341.c b/ili9341.c index 2fab13d..4de19fb 100644 --- a/ili9341.c +++ b/ili9341.c @@ -734,10 +734,10 @@ void ili9341_read_memory(int x, int y, int w, int h, uint16_t *out) #ifdef LCD_DRIVER_ST7796S // ST7796S send data in RGB565 format, not need parse it -// Copy ST7796S screen data to buffer -void ili9341_read_memory(int x, int y, int w, int h, uint16_t *out) +// Read a screen area as one stream: ili9341_read_memory_start(), ili9341_read_memory_next() for every +// part, ili9341_read_memory_end(). With DMA a part is read while the caller processes the previous one. +void ili9341_read_memory_start(int x, int y, int w, int h) { - uint16_t len = w * h; ili9341_setWindow(ILI9341_MEMORY_READ, x, y, w, h); // Skip data from rx buffer spi_DropRx(); @@ -747,11 +747,24 @@ void ili9341_read_memory(int x, int y, int w, int h, uint16_t *out) #endif // require 8bit dummy clock spi_RxByte(); - // receive pixel data to buffer +} + +// Wait for the previous part, then read len pixels to out +void ili9341_read_memory_next(uint16_t *out, int len) +{ #ifndef __USE_DISPLAY_DMA_RX__ spi_RxBuffer((uint8_t *)out, len * 2); #else - spi_DMARxBuffer((uint8_t *)out, len * 2, true); + dmaChannelWaitCompletionRxTx(); + spi_DMARxBuffer((uint8_t *)out, len * 2, false); +#endif +} + +// Wait for the last part +void ili9341_read_memory_end(void) +{ +#ifdef __USE_DISPLAY_DMA_RX__ + dmaChannelWaitCompletionRxTx(); #endif // restore speed if need #ifdef LCD_SPI_RX_SPEED @@ -759,6 +772,14 @@ void ili9341_read_memory(int x, int y, int w, int h, uint16_t *out) #endif LCD_CS_HIGH; } + +// Copy ST7796S screen data to buffer +void ili9341_read_memory(int x, int y, int w, int h, uint16_t *out) +{ + ili9341_read_memory_start(x, y, w, h); + ili9341_read_memory_next(out, w * h); + ili9341_read_memory_end(); +} #endif void ili9341_clear_screen(void) diff --git a/main.c b/main.c index e0a4bea..fe6eb85 100644 --- a/main.c +++ b/main.c @@ -1022,20 +1022,167 @@ VNA_SHELL_FUNCTION(cmd_release) #endif +#ifdef TINYSA4 +// Row y of the screen for a capture, y counting up from 0: the screen is read as one DMA stream, +// the next row into the other half of buf (2 * LCD_WIDTH pixels) while the caller uses row y. +// A caller leaving before the last row ends the stream with ili9341_read_memory_end(). +static uint16_t *capture_read_row(uint16_t *buf, int y) +{ + if (y == 0) { + ili9341_read_memory_start(0, 0, LCD_WIDTH, LCD_HEIGHT); + ili9341_read_memory_next(buf, LCD_WIDTH); + } + if (y + 1 < LCD_HEIGHT) + ili9341_read_memory_next(&buf[((y + 1) & 1) * LCD_WIDTH], LCD_WIDTH); + else + ili9341_read_memory_end(); + return &buf[(y & 1) * LCD_WIDTH]; +} +#endif + +#ifdef __CAPTURE_RLE8__ +/* + * Screen capture in the NanoVNA-D (DiSlord) RLE8 format, sent by "capture" with any argument: + * "BM", u16 width, u16 height, u8 8 (bits per pixel), u8 1 (compression), + * u16 palette size in bytes, palette (pixels as read from the LCD), + * then for every row u16 size + the PackBits packed palette indices of the row + * The palette holds all colours the tinySA draws (see plot_capture_colors()), so the screen is read once. + * Other content (image viewer, display test) sets capture_verify: the screen is read twice to add its colours. + */ +#define CAPTURE_PALETTE_SIZE 256 +bool capture_verify = false; +static uint16_t capture_palette[CAPTURE_PALETTE_SIZE]; // sorted +static uint16_t capture_colors; + +// Position of colour c in the sorted palette, or where it has to be inserted +static int capture_palette_find(uint16_t c) +{ + int lo = 0, hi = capture_colors; + while (lo < hi) { + int mid = (lo + hi) / 2; + if (capture_palette[mid] < c) lo = mid + 1; + else hi = mid; + } + return lo; +} + +// Returns false if the palette is full +bool capture_palette_add(uint16_t c) +{ + int i = capture_palette_find(c); + if (i < capture_colors && capture_palette[i] == c) + return true; + if (capture_colors >= CAPTURE_PALETTE_SIZE) + return false; + memmove(&capture_palette[i + 1], &capture_palette[i], (capture_colors - i) * sizeof(uint16_t)); + capture_palette[i] = c; + capture_colors++; + return true; +} + +// PackBits (DiSlord, NanoVNA-D common.c), returns the packed size, at most size + size / 128 + 1 +static int packbits(const uint8_t *source, uint8_t *dest, int size) +{ + int i = 0, rle, l, pk = 0, sz = -1; + while (i < size) { + l = size - i; + if (l > 128) l = 128; // limit a run to 128 bytes + uint8_t c = source[i++]; + for (rle = 0; --l && c == source[i + rle];) rle++; // run of rle + 1 bytes + if (sz >= 0 && rle < 2) rle = 0; // keep runs shorter than 3 in the literal block + else if (sz < 0 || rle > 0) sz = pk++; // start a new block + dest[pk++] = c; + if (rle > 0) {i += rle; dest[sz] = -rle;} // run block done + else if ((dest[sz] = pk - sz - 2) < 127) // extend the literal block up to 128 bytes + continue; + sz = -1; + } + return pk; +} + +// Returns false, with nothing sent, if the screen has too many colours +static bool capture_rle8(void) +{ +#if SPI_BUFFER_SIZE < (256 + 2*LCD_WIDTH) +#error "Low size of spi_buffer for capture_rle8" +#endif +#ifdef __USE_SD_CARD__ + _Static_assert((256 + 2*LCD_WIDTH) * sizeof(pixel_t) <= SPI_BUFFER_SIZE * sizeof(pixel_t) - sizeof(FATFS) - sizeof(FIL), + "capture_rle8 buffers overlap the FatFs objects at the end of spi_buffer"); +#endif + static const struct {uint16_t header, width, height; uint8_t bit_per_pixel, compression;} + rle8_header = {0x4D42, LCD_WIDTH, LCD_HEIGHT, 8, 1}; + // spi_buffer: u16 size + packed row (at most 2 + LCD_WIDTH + LCD_WIDTH/128 + 1 bytes), then the + // two row buffers of capture_read_row(). A row is replaced by its indices in place. + uint16_t *packed = spi_buffer; + uint16_t *rows = &spi_buffer[256]; + capture_colors = 0; + for (int i = 0; i < MAX_PALETTE; i++) + capture_palette_add(GET_PALTETTE_COLOR(i)); + plot_capture_colors(); + // With other content on the screen first add the colours on it (pass 0), then send (pass 1) + for (int pass = capture_verify ? 0 : 1; pass < 2; pass++) { + uint16_t colors = capture_colors, c = capture_palette[0]; + int i = 0; + if (pass == 1) { + uint16_t size = capture_colors * sizeof(uint16_t); + streamWrite(shell_stream, (void *)&rle8_header, sizeof(rle8_header)); + streamWrite(shell_stream, (void *)&size, sizeof(size)); + streamWrite(shell_stream, (void *)capture_palette, size); + } + for (int y = 0; y < LCD_HEIGHT; y++) { + uint16_t *row = capture_read_row(rows, y); + if (pass == 0) { + for (int x = 0; x < LCD_WIDTH; x++) + if (row[x] != c && !capture_palette_add(c = row[x])) { + if (y + 1 < LCD_HEIGHT) + ili9341_read_memory_end(); + return false; + } + continue; + } + uint8_t *index = (uint8_t *)row; + for (int x = 0; x < LCD_WIDTH; x++) { + if (row[x] != c) { + c = row[x]; + i = capture_palette_find(c); + if (i >= capture_colors || capture_palette[i] != c) { + i = 0; // not in the palette: the next capture reads the screen twice + capture_verify = true; + } + } + index[x] = i; + } + packed[0] = packbits(index, (uint8_t *)&packed[1], LCD_WIDTH); + streamWrite(shell_stream, (void *)packed, packed[0] + sizeof(uint16_t)); + } + if (pass == 0 && capture_colors == colors) // nothing new, the other content is gone + capture_verify = false; + } + return true; +} +#endif + VNA_SHELL_FUNCTION(cmd_capture) { // read pixel count at one time (PART*2 bytes required for read buffer) (void)argc; (void)argv; +#ifdef __CAPTURE_RLE8__ + if (argc > 0 && capture_rle8()) // any argument as in NanoVNA-D, raw if the screen has too many colours + return; +#endif int y; -#ifdef TINYSA4 +#ifdef TINYSA4 #if SPI_BUFFER_SIZE < (2*LCD_WIDTH) #error "Low size of spi_buffer for cmd_capture" #endif + // DMA reads the next row while USB sends this one + for (y = 0; y < LCD_HEIGHT; y++) + streamWrite(shell_stream, (void*)capture_read_row(spi_buffer, y), LCD_WIDTH * sizeof(uint16_t)); #else #if SPI_BUFFER_SIZE < (3*LCD_WIDTH + 1) #error "Low size of spi_buffer for cmd_capture" -#endif #endif // read 2 row pixel time (read buffer limit by 2/3 + 1 from spi_buffer size) for (y = 0; y < LCD_HEIGHT; y += 2) { @@ -1044,6 +1191,7 @@ VNA_SHELL_FUNCTION(cmd_capture) ili9341_read_memory(0, y, LCD_WIDTH, 2, spi_buffer); streamWrite(shell_stream, (void*)buf, 2 * LCD_WIDTH * sizeof(uint16_t)); } +#endif } #ifdef ENABLE_SD_CARD_CMD diff --git a/nanovna.h b/nanovna.h index 36f589b..16b1dd2 100644 --- a/nanovna.h +++ b/nanovna.h @@ -110,6 +110,7 @@ #define __SD_CARD_DUMP_FIRMWARE__ // Allow dump firmware to SD card #define __SD_FILE_BROWSER__ #define __USE_USB_MSC__ // USB DISK mode: SD card as USB mass storage next to the USB serial port +#define __CAPTURE_RLE8__ // capture with an argument sends the screen RLE8 compressed (NanoVNA-D format) #define __LCD_BRIGHTNESS__ // LCD or hardware allow change brightness, add menu item for this #define __HARMONIC__ #define __NOISE_FIGURE__ @@ -307,6 +308,12 @@ typedef struct { void send_region(remote_region_t *rd, uint8_t * buf, uint16_t size); #endif +#ifdef __CAPTURE_RLE8__ +extern bool capture_verify; // screen may show colours outside the capture palette +bool capture_palette_add(uint16_t c); +void plot_capture_colors(void); +#endif + void set_marker_frequency(int m, freq_t f); void set_marker_time(int m, float f); void set_marker_index(int m, int16_t idx); @@ -1214,6 +1221,11 @@ int ili9341_drawchar_size(uint8_t ch, int x, int y, uint8_t size, int x_max); int ili9341_drawstring_size(const char *str, int x, int y, uint8_t size, int x_max); void ili9341_drawfont(uint8_t ch, int x, int y); void ili9341_read_memory(int x, int y, int w, int h, uint16_t* out); +#ifdef TINYSA4 +void ili9341_read_memory_start(int x, int y, int w, int h); +void ili9341_read_memory_next(uint16_t *out, int len); +void ili9341_read_memory_end(void); +#endif void ili9341_line(int x0, int y0, int x1, int y1); void ili9341_define_scroll(uint16_t tfa, uint16_t bfa); void ili9341_scroll(uint16_t start); diff --git a/plot.c b/plot.c index 4335a55..51f4e39 100644 --- a/plot.c +++ b/plot.c @@ -2151,6 +2151,9 @@ int display_test(void) return false; if (!display_test_pattern(0xd200)) return false; +#endif +#ifdef __CAPTURE_RLE8__ + capture_verify = true; // the test pattern is not in the capture palette #endif for (int h = 0; h < LCD_HEIGHT; h++) { // write test pattern to LCD @@ -2174,6 +2177,28 @@ int display_test(void) #include "waterfall.c" #endif +#ifdef __CAPTURE_RLE8__ +#ifndef _USE_WATERFALL_PALETTE +#error "__CAPTURE_RLE8__ needs _USE_WATERFALL_PALETTE" +#endif +// Add the colours drawn outside the LCD palette to the capture palette: the waterfall, and the +// traces, which draw_cell() ORs over each other and over the colours drawn before them +void plot_capture_colors(void) +{ + static const uint8_t below_traces[] = {LCD_BG_COLOR, LCD_HAM_COLOR, LCD_GRID_COLOR, LCD_TRIGGER_COLOR, LCD_GRID_VALUE_COLOR}; + for (uint32_t i = 0; i < ARRAY_COUNT(waterfall_palette); i++) + capture_palette_add(waterfall_palette[i]); + for (int traces = 1; traces < (1 << TRACES_MAX); traces++) { + uint16_t c = 0; + for (int t = 0; t < TRACES_MAX; t++) + if (traces & (1 << t)) + c |= GET_PALTETTE_COLOR(LCD_TRACE_1_COLOR + t); + for (uint32_t i = 0; i < ARRAY_COUNT(below_traces); i++) + capture_palette_add(c | GET_PALTETTE_COLOR(below_traces[i])); + } +} +#endif + static void update_waterfall(void){ int i = CHART_BOTTOM-1; int w_width = area_width < WIDTH ? area_width : WIDTH; diff --git a/vna_browser.c b/vna_browser.c index 0773ea8..ad13969 100644 --- a/vna_browser.c +++ b/vna_browser.c @@ -281,6 +281,9 @@ static char cmd_buffer[256+128]; uint16_t *buf_16 = spi_buffer; // prepare buffer res = f_read(fs_file, (void *)buf_16, sizeof(bmp_header_v4), &size); // read heaser if (res != FR_OK || buf_16[9] != LCD_WIDTH || buf_16[11] != LCD_HEIGHT || buf_16[14] != 16) {error = "Format err"; break;} +#ifdef __CAPTURE_RLE8__ + capture_verify = true; // the image can hold any colour +#endif for (y = LCD_HEIGHT-1; y >=0 && res == FR_OK; y--) { res = f_read(fs_file, (void *)buf_16, LCD_WIDTH * sizeof(uint16_t), &size); swap_bytes(buf_16, LCD_WIDTH); From b6699cfdb9bfd60fc1b549a743577b1864d4f780 Mon Sep 17 00:00:00 2001 From: erikkaashoek Date: Tue, 29 Sep 2026 18:30:45 +0200 Subject: [PATCH 2/2] fix: 'reset dfu' on the tinySA ULTRA - 'reset dfu' and enter_dfu() are now also built for the tinySA4 (the DFU menu stays tinySA3 only); the reply is sent before the reset - F303 __early_init(): the jump to the system bootloader failed and the bootloader reset the MCU. crt0 calls __early_init() on the process stack with interrupts disabled, so switch to the main stack (CONTROL = 0) and enable interrupts before the jump, as in the F072 board.c. Also enable the SYSCFG clock for the system memory remap and replace the logical || that made it write 1 to SYSCFG_CFGR1 Tested on HW V0.4.5.1: 'reset dfu' enters the STM32 BOOTLOADER and dfu-util can flash it. Co-Authored-By: Claude Opus 5.5 (1M context) --- NANOVNA_STM32_F303/board.c | 12 ++++++++---- main.c | 3 +-- ui.c | 7 +++---- 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/NANOVNA_STM32_F303/board.c b/NANOVNA_STM32_F303/board.c index 6ca71af..677c9b7 100644 --- a/NANOVNA_STM32_F303/board.c +++ b/NANOVNA_STM32_F303/board.c @@ -114,11 +114,15 @@ void __early_init(void) { bootloader(); while(1); #else - __disable_irq(); - uint32_t foo = SYSCFG->CFGR1; - foo = (foo & ~SYSCFG_CFGR1_MEM_MODE) || SYSCFG_CFGR1_MEM_MODE_0; - SYSCFG->CFGR1 = foo; + // Map the system memory at 0 so interrupts use the bootloader vectors (SYSCFG needs its clock) + RCC->APB2ENR |= RCC_APB2ENR_SYSCFGEN; + SYSCFG->CFGR1 = (SYSCFG->CFGR1 & ~SYSCFG_CFGR1_MEM_MODE) | SYSCFG_CFGR1_MEM_MODE_0; __DSB(); + __ISB(); + // The bootloader needs interrupts, crt0 disabled them (as in the F072 board.c) + __enable_irq(); + // and the main stack, crt0 switched to the process stack + __set_CONTROL(0); __ISB(); __set_MSP(*((uint32_t *)(STM32F303xC_SYSTEM_MEMORY))); diff --git a/main.c b/main.c index fe6eb85..97a438e 100644 --- a/main.c +++ b/main.c @@ -578,15 +578,14 @@ VNA_SHELL_FUNCTION(cmd_reset) { (void)argc; (void)argv; -#ifndef TINYSA4 if (argc == 1) { if (get_str_index(argv[0], "dfu") == 0) { shell_printf("Performing reset to DFU mode\r\n"); + chThdSleepMilliseconds(100); // let USB send the text enter_dfu(); return; } } -#endif shell_printf("Performing reset\r\n"); rccEnableWWDG(FALSE); WWDG->CFR = 0x60; diff --git a/ui.c b/ui.c index 63744a0..979a164 100644 --- a/ui.c +++ b/ui.c @@ -750,22 +750,21 @@ extern const char *states[]; lcd_set_font(FONT_SMALL); } -#ifdef __HAS_DFU__ +// Also used by "reset dfu" on the tinySA4, which has no DFU menu (__HAS_DFU__) void enter_dfu(void) { int x = 5, y = 5; ili9341_set_foreground(LCD_FG_COLOR); ili9341_set_background(LCD_BG_COLOR); - // leave a last message + // leave a last message ili9341_clear_screen(); ili9341_drawstring_7x13("DFU: Device Firmware Update Mode\n" "To exit DFU mode, please reset device yourself.", x, y); - // see __early_init in ./NANOVNA_STM32_F072/board.c + // see __early_init in ./NANOVNA_STM32_F072/board.c and ./NANOVNA_STM32_F303/board.c *((unsigned long *)BOOT_FROM_SYTEM_MEMORY_MAGIC_ADDRESS) = BOOT_FROM_SYTEM_MEMORY_MAGIC; NVIC_SystemReset(); } -#endif static void select_lever_mode(int mode)