Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Smith can be displayed, but the position is a bit to the left; #1

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
153 changes: 153 additions & 0 deletions .cproject

Large diffs are not rendered by default.

27 changes: 27 additions & 0 deletions .project
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>NanoVNA_AA6KL</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.cdt.managedbuilder.core.genmakebuilder</name>
<triggers>clean,full,incremental,</triggers>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.cdt.managedbuilder.core.ScannerConfigBuilder</name>
<triggers>full,incremental,</triggers>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.cdt.core.cnature</nature>
<nature>org.eclipse.cdt.core.ccnature</nature>
<nature>org.eclipse.cdt.managedbuilder.core.managedBuildNature</nature>
<nature>org.eclipse.cdt.managedbuilder.core.ScannerConfigNature</nature>
</natures>
</projectDescription>
2 changes: 1 addition & 1 deletion ChibiOS
1 change: 1 addition & 0 deletions NANOVNA_STM32_F303/board.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions NanoVNA_DAP.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
interface cmsis-dap
transport select swd
source [find target/stm32f3x.cfg]
adapter_khz 8000
105 changes: 68 additions & 37 deletions adc.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,24 @@
#ifdef NANOVNA_F303
#include "adc_F303.h"

static adcsample_t samplesVBAT[2];
#define ADC_SMPR_SMP_TIME ADC_SMPR_SMP_61P5
#define ADC_GRP_NUM_CHANNELS_VBAT 3
#define ADC_GRP_BUF_DEPTH_VBAT 2
static adcsample_t samplesVBAT[ADC_GRP_NUM_CHANNELS_VBAT*ADC_GRP_BUF_DEPTH_VBAT];
static adcsample_t samples[2];


#define ADC_GRP_NUM_CHANNELS_VBAT 2
#define ADC_GRP_BUF_DEPTH_VBAT 1
static const ADCConversionGroup adcgrpcfgVBAT = {
FALSE,
FALSE,
ADC_GRP_NUM_CHANNELS_VBAT,
NULL,
NULL,
// ADC_CFGR_CONT | ADC_CFGR1_RES_12BIT, /* CFGR1 */
0, /* CFGR1 */
ADC_CFGR_CONT | ADC_CFGR1_RES_12BIT, /* CFGR1 */
ADC_TR(0, 4095), /* TR */
{0,0}, /* SMPR */
{ADC_SQR1_SQ1_N(ADC_CHANNEL_IN17) | ADC_SQR1_SQ2_N(ADC_CHANNEL_IN18),
{0,
ADC_SMPR2_SMP_AN16(ADC_SMPR_SMP_TIME) | ADC_SMPR2_SMP_AN17(ADC_SMPR_SMP_TIME) | ADC_SMPR2_SMP_AN18(ADC_SMPR_SMP_TIME)}, /* SMPR */
{ADC_SQR1_SQ1_N(ADC_CHANNEL_IN17) | ADC_SQR1_SQ2_N(ADC_CHANNEL_IN18)
| ADC_SQR1_SQ3_N(ADC_CHANNEL_IN16) ,
0,0,0} /* CHSELR */
};

Expand All @@ -56,7 +58,8 @@ static ADCConversionGroup adcgrpcfgTouch = {
| ADC_CFGR_AWD1EN
,
ADC_TR(0, TOUCH_THRESHOLD), /* TR1 */
{5,0}, /* SMPR[2] */
{ADC_SMPR1_SMP_AN3(ADC_SMPR_SMP_TIME) | ADC_SMPR1_SMP_AN4(ADC_SMPR_SMP_TIME),
0}, /* SMPR[2] */
{ /* SQR[4] */
ADC_SQR1_SQ1_N(ADC_CHANNEL_IN3) ,
0,
Expand All @@ -72,7 +75,8 @@ static ADCConversionGroup adcgrpcfgXY = {
NULL, /* adcerrorcallback_touch */
ADC_CFGR1_RES_12BIT, /* CFGR */
ADC_TR(0, 0), /* TR1 */
{5,0}, /* SMPR[2] */
{ADC_SMPR1_SMP_AN3(ADC_SMPR_SMP_TIME) | ADC_SMPR1_SMP_AN4(ADC_SMPR_SMP_TIME),
0}, /* SMPR[2] */
{ /* SQR[4] */
ADC_SQR1_SQ1_N(ADC_CHANNEL_IN3) ,
0,
Expand All @@ -94,8 +98,11 @@ void adc_init(void)
#ifdef NANOVNA_F303
// adcStart(&ADCD2, NULL);
adcStart(&ADCD1, NULL);
#ifdef F303_ADC_VREF_ALWAYS_ON
adcSTM32EnableVBAT(&ADCD1);
adcSTM32EnableVREF(&ADCD1);
adcSTM32EnableTS(&ADCD1);
#endif
#else
rccEnableADC1(FALSE);

Expand Down Expand Up @@ -126,7 +133,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);
Expand All @@ -152,33 +159,57 @@ uint16_t adc_single_read(ADC_TypeDef *adc, uint32_t chsel)

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;
#ifdef NANOVNA_F303
// Not to turn on/off ADC to avoid ADC on/off noise.
adcConvert(&ADCD1, &adcgrpcfgVBAT, samplesVBAT, 1);
vbat = samplesVBAT[0];
vrefint = samplesVBAT[1];
#else
ADC->CCR |= ADC_CCR_VREFEN | ADC_CCR_VBATEN;
// VREFINT == ADC_IN17
vrefint = adc_single_read(adc, ADC_CHSELR_VREFINT);
// VBAT == ADC_IN18
// VBATEN enables resiter devider circuit. It consume vbat power.
vbat = adc_single_read(adc, ADC_CHSELR_VBAT);
ADC->CCR &= ~(ADC_CCR_VREFEN | ADC_CCR_VBATEN);
#endif
uint16_t vbat_raw = (ADC_FULL_SCALE * VREFINT_CAL * vbat * 2 / (vrefint * ((1<<12)-1)));
if (vbat_raw < 100) {
// maybe D2 is not installed
return -1;
}
#define ADC_FULL_SCALE 3300
uint16_t VREFINT_CAL = (*((uint16_t*)0x1FFFF7BA));
const uint16_t V25 = 1750;// when V25=1.41V at ref 3.3V
const uint16_t Avg_Slope = 5; //when avg_slope=4.3mV/C at ref 3.3V
uint16_t temperature_cal1 = *((uint16_t*) ((uint32_t)0x1FFFF7B8U));
/* Internal temperature sensor, address of parameter TS_CAL1: On STM32F3,
temperature sensor ADC raw data acquired at temperature 25 DegC (tolerance: +-5 DegC),
Vref+ = 3.3 V (tolerance: +-10 mV). */
uint16_t temperature_cal2 = *((uint16_t*) ((uint32_t)0x1FFFF7C2U));
/* Internal temperature sensor, address of parameter TS_CAL2: On STM32F3,
temperature sensor ADC raw data acquired at temperature 110 DegC (tolerance: +-5 DegC),
Vref+ = 3.3 V (tolerance: +-10 mV). */
float avg_slope = ((float)(temperature_cal1 - temperature_cal2))/(110-25);

float vbat = 0;
float vrefint = 0;
float ts = 0;
#ifdef NANOVNA_F303
#ifndef F303_ADC_VREF_ALWAYS_ON
adcSTM32EnableVBAT(&ADCD1);
adcSTM32EnableVREF(&ADCD1);
adcSTM32EnableTS(&ADCD1);
adcConvert(&ADCD1, &adcgrpcfgVBAT, samplesVBAT, ADC_GRP_BUF_DEPTH_VBAT);
adcSTM32DisableVBAT(&ADCD1);
adcSTM32DisableVREF(&ADCD1);
adcSTM32DisableTS(&ADCD1);
#else
adcConvert(&ADCD1, &adcgrpcfgVBAT, samplesVBAT, ADC_GRP_BUF_DEPTH_VBAT);
#endif
vbat = samplesVBAT[0];
vrefint = samplesVBAT[1];
ts = samplesVBAT[2];
uint16_t vts = (ADC_FULL_SCALE * VREFINT_CAL * ts / (vrefint * ((1<<12)-1)));
uint16_t TemperatureC2 = (uint16_t)((V25-ts)/Avg_Slope+25);
uint16_t TemperatureC = (uint16_t)((V25-ts)/avg_slope+25);
#else
ADC->CCR |= ADC_CCR_VREFEN | ADC_CCR_VBATEN;
// VREFINT == ADC_IN17
vrefint = adc_single_read(adc, ADC_CHSELR_VREFINT);
// VBAT == ADC_IN18
// VBATEN enables resister divider circuit. It consumes vbat power.
vbat = adc_single_read(adc, ADC_CHSELR_VBAT);
ADC->CCR &= ~(ADC_CCR_VREFEN | ADC_CCR_VBATEN);
#endif
uint16_t vbat_raw = (ADC_FULL_SCALE * VREFINT_CAL * vbat * 2 / (vrefint * ((1<<12)-1)));
if (vbat_raw < 100) {
// maybe D2 is not installed
return -1;
}

return vbat_raw + VBAT_DIODE_VF;

return vbat_raw + config.vbat_offset;
}

void adc_start_analog_watchdogd(ADC_TypeDef *adc, uint32_t chsel)
Expand Down
33 changes: 14 additions & 19 deletions dsp.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@
#include <arm_math.h>
#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 },
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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
Expand All @@ -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;
Expand Down
16 changes: 5 additions & 11 deletions fft.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,10 @@

#include <math.h>
#include <stdint.h>
#include <arm_math.h>

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;
}
Expand All @@ -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];
Expand All @@ -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);
Expand Down
Loading