From 82a8f19c2553f473695211a116860e30360756dc Mon Sep 17 00:00:00 2001 From: TT Date: Tue, 4 Oct 2016 08:16:01 +0900 Subject: [PATCH] add smith plot, spi dma transfer --- ili9431.c | 188 +++++++++++++++++++++++++++++++++++++++++++++++------- main.c | 57 ++++++++++++----- nanovna.h | 5 +- 3 files changed, 207 insertions(+), 43 deletions(-) diff --git a/ili9431.c b/ili9431.c index 04eda76..09307b7 100644 --- a/ili9431.c +++ b/ili9431.c @@ -11,7 +11,7 @@ #define DC_CMD palClearPad(GPIOB, 7) #define DC_DATA palSetPad(GPIOB, 7) -#define RGB565(r,g,b) ( (((r)<<8)&0xf800) | (((g)<<3)&0x07e0) | (((b)>>3)&0x001f) ) +#define RGB565(b,r,g) ( (((b)<<8)&0xfc00) | (((r)<<2)&0x03e0) | (((g)>>3)&0x001f) ) uint16_t spi_buffer[1024]; @@ -71,15 +71,38 @@ ssp_databit16(void) //LPC_SSP1->CR0 = (LPC_SSP1->CR0 & 0xf0) | SSP_DATABIT_16; } + +const stm32_dma_stream_t *dmatx; +uint32_t txdmamode; + +static void spi_lld_serve_tx_interrupt(SPIDriver *spip, uint32_t flags) { + (void)spip; + (void)flags; +} + void spi_init(void) { rccEnableSPI1(FALSE); + dmatx = STM32_DMA_STREAM(STM32_SPI_SPI1_TX_DMA_STREAM); + txdmamode = STM32_DMA_CR_CHSEL(SPI1_TX_DMA_CHANNEL) | + STM32_DMA_CR_PL(STM32_SPI_SPI1_DMA_PRIORITY) | + STM32_DMA_CR_DIR_M2P | + STM32_DMA_CR_DMEIE | + STM32_DMA_CR_TEIE | + STM32_DMA_CR_PSIZE_HWORD | + STM32_DMA_CR_MSIZE_HWORD; + dmaStreamAllocate(dmatx, + STM32_SPI_SPI1_IRQ_PRIORITY, + (stm32_dmaisr_t)spi_lld_serve_tx_interrupt, + NULL); + dmaStreamSetPeripheral(dmatx, &SPI1->DR); + //spiStart(&SPID1, &spicfg); /* Setup transfer parameters. */ SPI1->CR1 = 0; SPI1->CR1 = SPI_CR1_MSTR | SPI_CR1_BIDIOE | SPI_CR1_SSM | SPI_CR1_SSI;// | SPI_CR1_BR_1; - SPI1->CR2 = 0x0700; + SPI1->CR2 = 0x0700 | SPI_CR2_TXDMAEN; SPI1->CR1 |= SPI_CR1_SPE; } @@ -226,6 +249,7 @@ void ili9341_fill(int x, int y, int w, int h, int color) ssp_senddata16(color); } +#if 0 void ili9341_bulk(int x, int y, int w, int h) { uint8_t xx[4] = { x >> 8, x, (x+w-1) >> 8, (x+w-1) }; @@ -238,6 +262,62 @@ void ili9341_bulk(int x, int y, int w, int h) while (len-- > 0) ssp_senddata16(*buf++); } +#else +void ili9341_bulk(int x, int y, int w, int h) +{ + 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; + + send_command(0x2A, 4, xx); + send_command(0x2B, 4, yy); + send_command(0x2C, 0, NULL); + + dmaStreamSetMemory0(dmatx, spi_buffer); + dmaStreamSetTransactionSize(dmatx, len); + dmaStreamSetMode(dmatx, txdmamode | STM32_DMA_CR_MINC); + dmaStreamEnable(dmatx); + dmaWaitCompletion(dmatx); +} +#endif + +#define SWAP(x,y) do { int z=x; x = y; y = z; } while(0) + +void +ili9341_line(int x0, int y0, int x1, int y1, uint16_t fg) +{ + 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; + } + } + if (dy > 0) + ili9341_fill(x0, y0, dx, dy, fg); + else + ili9341_fill(x0, y0+dy, dx, -dy, fg); + x0 += dx; + y0 += dy; + } +} + typedef struct { uint16_t width; @@ -283,6 +363,7 @@ ili9341_drawfont(uint8_t ch, const font_t *font, int x, int y, uint16_t fg, uint } +#if 0 void draw_grid(int n, int m, int w, int h, int x, int y, uint16_t fg, uint16_t bg) { @@ -303,7 +384,7 @@ draw_grid(int n, int m, int w, int h, int x, int y, uint16_t fg, uint16_t bg) yy += h; } } - +#endif const uint16_t colormap[] = { RGB565(255,0,0), RGB565(0,255,0), RGB565(0,0,255), @@ -342,9 +423,17 @@ ili9341_test(int mode) ili9341_drawfont(i, &NF20x24, i*20, 120, colormap[i%6], 0x0000); break; #endif +#if 0 case 4: draw_grid(10, 8, 29, 29, 15, 0, 0xffff, 0); break; +#endif + 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; } } @@ -356,6 +445,8 @@ int32_t fstart = 0; int32_t fstop = 300000000; int32_t fspan = 300000000; int32_t fgrid = 50000000; +int grid_offset; +int grid_width; #define OFFSETX 15 #define OFFSETY 0 @@ -387,6 +478,9 @@ void set_sweep(int32_t start, int stop) } fgrid = grid; + grid_offset = (WIDTH-1) * ((fstart % fgrid) / 100) / (fspan / 100); + grid_width = (WIDTH-1) * (fgrid / 100) / (fspan / 100); + draw_frequencies(); } @@ -441,10 +535,11 @@ rectangular_grid(int x, int y) { #define FREQ(x) (((x) * (fspan / 1000) / (WIDTH-1)) * 1000 + fstart) int c = grid_color; - int32_t n = FREQ(x-1) / fgrid; - int32_t m = FREQ(x) / fgrid; - if ((m - n) > 0) + //int32_t n = FREQ(x-1) / fgrid; + //int32_t m = FREQ(x) / fgrid; + //if ((m - n) > 0) //if (((x * 6) % (WIDTH-1)) < 6) + if (((x - grid_offset) % grid_width) == 0) return c; if (x == 0 || x == (WIDTH-1)) return c; @@ -484,17 +579,22 @@ draw_on_strut(int v0, int d, int color) v = v1; d = v0 - v1 + 1; } while (d-- > 0) - spi_buffer[v++] = color; + spi_buffer[v++] |= color; } +#define TRACES_MAX 4 + struct { + int enabled; float value; float prev_value; float d; uint16_t color; -} trace[2] = { - { 0, 0, 0, RGB565(0,255,255) }, - { 0, 0, 0, RGB565(255,0,40) } +} trace[TRACES_MAX] = { + { 1, 0, 0, 0, RGB565(0,255,255) }, + { 1, 0, 0, 0, RGB565(255,0,40) }, + { 0, 0, 0, 0, RGB565(0,0,255) }, + { 0, 0, 0, 0, RGB565(0,255,0) } }; float logmag(float *v) @@ -502,13 +602,23 @@ float logmag(float *v) return 11 - log10f(v[0]*v[0] + v[1]*v[1]); } -void sweep_plot(int32_t freq, int first) +float phase(float *v) +{ + return 4 + 2 * atan2f(v[1], v[0]) / M_PI; +} + +void sweep_plot(int32_t freq, int first, float *measured) { int curr_x = ((float)WIDTH * (freq - fstart) / fspan); - //float value = 11 - log10f(measured[0]*measured[0] + measured[1]*measured[1]); - //value *= 29; - trace[0].value = logmag(&measured[0]) * 29; - trace[1].value = logmag(&measured[2]) * 29; + int i; + if (trace[0].enabled) + trace[0].value = logmag(&measured[0]) * 29; + if (trace[1].enabled) + trace[1].value = logmag(&measured[2]) * 29; + if (trace[2].enabled) + trace[2].value = phase(&measured[0]) * 29; + if (trace[3].enabled) + trace[3].value = phase(&measured[2]) * 29; if (first) { prev_x = 0; @@ -519,21 +629,24 @@ void sweep_plot(int32_t freq, int first) } } else { int w = curr_x - prev_x; - trace[0].d = (trace[0].value - trace[0].prev_value) / w; - trace[1].d = (trace[1].value - trace[1].prev_value) / w; + for (i = 0; i < TRACES_MAX; i++) + if (trace[i].enabled) + trace[i].d = (trace[i].value - trace[i].prev_value) / w; while (prev_x < curr_x) { int len = set_strut_grid(prev_x); - draw_on_strut(trace[0].prev_value, trace[0].d, trace[0].color); - trace[0].prev_value += trace[0].d; - draw_on_strut(trace[1].prev_value, trace[1].d, trace[1].color); - trace[1].prev_value += trace[1].d; + for (i = 0; i < TRACES_MAX; i++) + if (trace[i].enabled) { + draw_on_strut(trace[i].prev_value, trace[i].d, trace[i].color); + trace[i].prev_value += trace[i].d; + } ili9341_bulk(OFFSETX + prev_x, OFFSETY, 1, len); prev_x++; } } - trace[0].prev_value = trace[0].value; - trace[1].prev_value = trace[1].value; + for (i = 0; i < TRACES_MAX; i++) + if (trace[i].enabled) + trace[i].prev_value = trace[i].value; } void sweep_tail() @@ -545,6 +658,35 @@ void sweep_tail() } } +void +cartesian_scale(float re, float im, int *xp, int *yp) +{ + float scale = 4e-3; + int x = WIDTH / 2 - re * scale; + int y = HEIGHT / 2 + im * scale; + if (x < 0) x = 0; + if (y < 0) y = 0; + if (x > WIDTH) x = WIDTH; + if (y > HEIGHT) y = HEIGHT; + *xp = OFFSETX + x; + *yp = OFFSETY + y; +} + +void polar_plot(float measured[101][4]) +{ + int x0, y0; + int i; + cartesian_scale(measured[0][1], measured[0][0], &x0, &y0); + for (i = 1; i < 101; i++) { + int x1, y1; + cartesian_scale(measured[i][1], measured[i][0], &x1, &y1); + ili9341_line(x0, y0, x1, y1, trace[2].color); + x0 = x1; + y0 = y1; + } +} + + void ili9341_drawchar_5x7(uint8_t ch, int x, int y, uint16_t fg, uint16_t bg) { diff --git a/main.c b/main.c index bda830b..4c76858 100644 --- a/main.c +++ b/main.c @@ -213,8 +213,8 @@ volatile int16_t wait_count = 0; int16_t dump_selection = 0; int16_t dsp_disabled = FALSE; -float measured[4]; - +float measured[101][4]; +uint32_t frequencies[101]; void i2s_end_callback(I2SDriver *i2sp, size_t offset, size_t n) @@ -290,18 +290,19 @@ static void cmd_data(BaseSequentialStream *chp, int argc, char *argv[]) static void cmd_gamma(BaseSequentialStream *chp, int argc, char *argv[]) { + float gamma[2]; (void)argc; (void)argv; - + pause_sweep(); wait_count = 4; while (wait_count) ; dsp_disabled = TRUE; - calclate_gamma(&measured[0]); + calclate_gamma(gamma); dsp_disabled = FALSE; - chprintf(chp, "%d %d %d %d\r\n", measured[0], measured[1], measured[2], measured[3]); + chprintf(chp, "%d %d\r\n", gamma[0], gamma[1]); } @@ -311,6 +312,7 @@ int16_t sweep_points = 101; static void cmd_scan(BaseSequentialStream *chp, int argc, char *argv[]) { + float gamma[2]; int i; int32_t freq, step; int delay; @@ -331,35 +333,30 @@ static void cmd_scan(BaseSequentialStream *chp, int argc, char *argv[]) __disable_irq(); delay = set_frequency(freq); palClearPad(GPIOC, GPIOC_LED); - calclate_gamma(&measured[0]); + calclate_gamma(gamma); palSetPad(GPIOC, GPIOC_LED); //dsp_disabled = FALSE; __enable_irq(); - chprintf(chp, "%d %d %d %d\r\n", measured[0], measured[1], measured[2], measured[3]); + chprintf(chp, "%d %d\r\n", gamma[0], gamma[1]); } } void scan_lcd(void) { int i; - int32_t freq, cur_freq, step; int delay; int first = TRUE; - freq = freq_start; - step = (freq_stop - freq_start) / (sweep_points-1); - delay = set_frequency(freq); + delay = set_frequency(frequencies[0]); delay += 2; for (i = 0; i < sweep_points; i++) { - cur_freq = freq; - freq = freq + step; wait_count = delay; tlv320aic3204_select_in3(); while (wait_count) ; palClearPad(GPIOC, GPIOC_LED); __disable_irq(); - calclate_gamma(&measured[0]); + calclate_gamma(&measured[i][0]); __enable_irq(); tlv320aic3204_select_in1(); @@ -367,15 +364,24 @@ void scan_lcd(void) while (wait_count) ; __disable_irq(); - calclate_gamma(&measured[2]); + calclate_gamma(&measured[i][2]); __enable_irq(); - delay = set_frequency(freq); - sweep_plot(cur_freq, first); + delay = set_frequency(frequencies[(i+1)%sweep_points]); +#if 0 + sweep_plot(frequencies[i], first, measured[i]); first = FALSE; +#endif palSetPad(GPIOC, GPIOC_LED); } +#if 1 + for (i = 0; i < sweep_points; i++) { + sweep_plot(frequencies[i], first, measured[i]); + first = FALSE; + } +#endif sweep_tail(); + polar_plot(measured); } static void cmd_scan_lcd(BaseSequentialStream *chp, int argc, char *argv[]) @@ -387,6 +393,16 @@ static void cmd_scan_lcd(BaseSequentialStream *chp, int argc, char *argv[]) scan_lcd(); } + +void +set_frequencies(void) +{ + int i; + int32_t span = (freq_stop - freq_start)/100; + for (i = 0; i < sweep_points; i++) + frequencies[i] = freq_start + span * i / (sweep_points - 1) * 100; +} + static void cmd_sweep(BaseSequentialStream *chp, int argc, char *argv[]) { if (argc == 0) { @@ -420,6 +436,8 @@ static void cmd_sweep(BaseSequentialStream *chp, int argc, char *argv[]) } sweep_points = x; } + + set_frequencies(); set_sweep(freq_start, freq_stop); } @@ -598,8 +616,13 @@ int main(void) * SPI LCD Initialize */ ili9341_init(); + set_sweep(freq_start, freq_stop); redraw(); + /* + */ + set_frequencies(); + /* * I2S Initialize */ diff --git a/nanovna.h b/nanovna.h index d45b4af..a74cf15 100644 --- a/nanovna.h +++ b/nanovna.h @@ -37,8 +37,6 @@ extern int16_t samp_buf[]; //extern int16_t refq_buf[]; extern int16_t refiq_buf[]; -extern float measured[4]; - void dsp_process(int16_t *src, size_t len); void calclate_gamma(float *gamma); @@ -48,9 +46,10 @@ void ili9341_init(void); void ili9341_test(int mode); void set_sweep(int32_t start, int stop); -void sweep_plot(int32_t freq, int first); +void sweep_plot(int32_t freq, int first, float measured[4]); void sweep_tail(void); void redraw(void); +void polar_plot(float measured[101][4]); extern const uint16_t x5x7_bits []; extern const uint32_t numfont20x24[][24];