diff --git a/firmware/boards/f0_module/io/io_pins.h b/firmware/boards/f0_module/io/io_pins.h index 8aafb5d..bea6f5c 100644 --- a/firmware/boards/f0_module/io/io_pins.h +++ b/firmware/boards/f0_module/io/io_pins.h @@ -6,8 +6,8 @@ #define LED_GREEN_PORT GPIOB #define LED_GREEN_PIN 6 -#define NERNST_ESR_DRIVER_PORT GPIOB -#define NERNST_ESR_DRIVER_PIN 7 +#define NERNST_49_ESR_DRIVER_PORT GPIOB +#define NERNST_49_ESR_DRIVER_PIN 7 // PA7 #define HEATER_PWM_DEVICE PWMD1 diff --git a/firmware/boards/f0_module/port.cpp b/firmware/boards/f0_module/port.cpp index a0a06eb..17343e3 100644 --- a/firmware/boards/f0_module/port.cpp +++ b/firmware/boards/f0_module/port.cpp @@ -149,3 +149,20 @@ SensorType GetSensorType() { return SensorType::LSU49; } + +void SetupESRDriver(SensorType sensor) +{ + // NOP +} + +int GetESRSupplyR() +{ + // Nernst AC injection resistor value + return 22000; +} + +void ToggleESRDriver(SensorType sensor) +{ + (void)sensor; + palTogglePad(NERNST_49_ESR_DRIVER_PORT, NERNST_49_ESR_DRIVER_PIN); +} diff --git a/firmware/boards/f1_common/f1_port.cpp b/firmware/boards/f1_common/f1_port.cpp index c1004c8..26332a8 100644 --- a/firmware/boards/f1_common/f1_port.cpp +++ b/firmware/boards/f1_common/f1_port.cpp @@ -23,10 +23,15 @@ static MFSDriver mfs1; static Configuration cfg; #define MFS_CONFIGURATION_RECORD_ID 1 +#ifndef BOARD_DEFAULT_SENSOR_TYPE +#define BOARD_DEFAULT_SENSOR_TYPE SensorType::LSU49 +#endif + // Configuration defaults void Configuration::LoadDefaults() { CanIndexOffset = 0; + sensorType = BOARD_DEFAULT_SENSOR_TYPE; /* Finaly */ Tag = ExpectedTag; @@ -84,13 +89,20 @@ const char *getTsSignature() { SensorType GetSensorType() { - /* TODO: load from settings */ -#if defined(BOARD_SENSOR_LSU42) - return SensorType::LSU42; -#elif defined(BOARD_SENSOR_LSUADV) - return SensorType::LSUADV; -#else - /* default is LSU4.9 */ - return SensorType::LSU49; -#endif + return cfg.sensorType; } + +void ToggleESRDriver(SensorType sensor) +{ + switch (sensor) { + case SensorType::LSU42: + palTogglePad(NERNST_42_ESR_DRIVER_PORT, NERNST_42_ESR_DRIVER_PIN); + break; + case SensorType::LSU49: + palTogglePad(NERNST_49_ESR_DRIVER_PORT, NERNST_49_ESR_DRIVER_PIN); + break; + case SensorType::LSUADV: + palTogglePad(NERNST_ADV_ESR_DRIVER_PORT, NERNST_ADV_ESR_DRIVER_PIN); + break; + } +} \ No newline at end of file diff --git a/firmware/boards/f1_dual/io/io_pins.h b/firmware/boards/f1_dual/io/io_pins.h index 0699afa..08a7efd 100644 --- a/firmware/boards/f1_dual/io/io_pins.h +++ b/firmware/boards/f1_dual/io/io_pins.h @@ -22,8 +22,11 @@ #define NERNST_42_ESR_DRIVER_PIN 12 // LSU 4.9 - 22K -#define NERNST_ESR_DRIVER_PORT GPIOB -#define NERNST_ESR_DRIVER_PIN 11 +#define NERNST_49_ESR_DRIVER_PORT GPIOB +#define NERNST_49_ESR_DRIVER_PIN 11 + +#define NERNST_49_BIAS_PORT GPIOB +#define NERNST_49_BIAS_PIN 2 // LSU ADV - 47K #define NERNST_ADV_ESR_DRIVER_PORT GPIOB diff --git a/firmware/boards/f1_dual/port.cpp b/firmware/boards/f1_dual/port.cpp index e0d6a9a..549c55a 100644 --- a/firmware/boards/f1_dual/port.cpp +++ b/firmware/boards/f1_dual/port.cpp @@ -126,3 +126,63 @@ AnalogResult AnalogSample() .VirtualGroundVoltageInt = HALF_VCC, }; } + +/* TODO: optimize */ +void SetupESRDriver(SensorType sensor) +{ + switch (sensor) { + case SensorType::LSU42: + /* disable bias */ + palSetPadMode(NERNST_49_BIAS_PORT, NERNST_49_BIAS_PIN, + PAL_MODE_INPUT); + /* disable all others ESR drivers */ + palSetPadMode(NERNST_49_ESR_DRIVER_PORT, NERNST_49_ESR_DRIVER_PIN, + PAL_MODE_INPUT); + palSetPadMode(NERNST_ADV_ESR_DRIVER_PORT, NERNST_ADV_ESR_DRIVER_PIN, + PAL_MODE_INPUT); + /* enable LSU4.2 */ + palSetPadMode(NERNST_42_ESR_DRIVER_PORT, NERNST_42_ESR_DRIVER_PIN, + PAL_MODE_OUTPUT_PUSHPULL); + break; + case SensorType::LSU49: + /* disable all others ESR drivers */ + palSetPadMode(NERNST_42_ESR_DRIVER_PORT, NERNST_42_ESR_DRIVER_PIN, + PAL_MODE_INPUT); + palSetPadMode(NERNST_ADV_ESR_DRIVER_PORT, NERNST_ADV_ESR_DRIVER_PIN, + PAL_MODE_INPUT); + /* enable LSU4.2 */ + palSetPadMode(NERNST_49_ESR_DRIVER_PORT, NERNST_49_ESR_DRIVER_PIN, + PAL_MODE_OUTPUT_PUSHPULL); + /* enable bias */ + palSetPadMode(NERNST_49_BIAS_PORT, NERNST_49_BIAS_PIN, + PAL_MODE_OUTPUT_PUSHPULL); + palSetPad(NERNST_49_BIAS_PORT, NERNST_49_BIAS_PIN); + break; + case SensorType::LSUADV: + /* disable bias */ + palSetPadMode(NERNST_49_BIAS_PORT, NERNST_49_BIAS_PIN, + PAL_MODE_INPUT); + /* disable all others ESR drivers */ + palSetPadMode(NERNST_49_ESR_DRIVER_PORT, NERNST_49_ESR_DRIVER_PIN, + PAL_MODE_INPUT); + palSetPadMode(NERNST_42_ESR_DRIVER_PORT, NERNST_42_ESR_DRIVER_PIN, + PAL_MODE_INPUT); + /* enable LSU4.2 */ + palSetPadMode(NERNST_ADV_ESR_DRIVER_PORT, NERNST_ADV_ESR_DRIVER_PIN, + PAL_MODE_OUTPUT_PUSHPULL); + break; + } +} + +int GetESRSupplyR() +{ + switch (GetSensorType()) { + case SensorType::LSU42: + return 6800; + case SensorType::LSU49: + return 22000; + case SensorType::LSUADV: + return 47000; + } + return 0; +} diff --git a/firmware/boards/f1_rev2/board.h b/firmware/boards/f1_rev2/board.h index fcedb1b..d22c920 100644 --- a/firmware/boards/f1_rev2/board.h +++ b/firmware/boards/f1_rev2/board.h @@ -99,20 +99,20 @@ * Port B setup. * PB0 - Vbatt_sense (analog in). * PB1 - Heater_sense (analog in). - * PB2 - Nernsr_4.9_bias (digital output, 2 Mhz) + * PB2 - Nernsr_4.9_bias (digital input, no pull) - keep high-Z after power on * PB3 - SWO (digital input) * PB4..PB5 - DISP1..DISP2 - unused * PB6 - heater_pwm (output pushpull, alternate, 2 MHz). * PB7..PB9 - DISP4..DISP6 - unused * PB10 - Nernsr_ADV_esr_drive (digital input, no pull) - keep high-Z after power on - * PB11 - Nernsr_4.9_esr_drive (output pushpull, 50 Mhz) + * PB11 - Nernsr_4.9_esr_drive (digital input, no pull) - keep high-Z after power on * PB12 - Nernsr_4.2_esr_drive (digital input, no pull) - keep high-Z after power on * PB13 - Blue LED (output pushpull, 2 MHz) * PB14 - PWMout2 (output pushpull, alternate, 2 Mhz). * PB15 - PWMout1 (output pushpull, alternate, 2 Mhz). */ -#define VAL_GPIOBCRL 0x8A888200 /* PB7...PB0 */ -#define VAL_GPIOBCRH 0xAA243488 /* PB15...PB8 */ +#define VAL_GPIOBCRL 0x8A888400 /* PB7...PB0 */ +#define VAL_GPIOBCRH 0xAA244488 /* PB15...PB8 */ #define VAL_GPIOBODR 0x0000FFFF /* diff --git a/firmware/boards/f1_rev2/build_wideband_lsu42.sh b/firmware/boards/f1_rev2/build_wideband_lsu42.sh new file mode 100755 index 0000000..c9281b1 --- /dev/null +++ b/firmware/boards/f1_rev2/build_wideband_lsu42.sh @@ -0,0 +1,4 @@ +#!/bin/bash + +UDEFS="-DBOARD_DEFAULT_SENSOR_TYPE=SensorType::LSU42" \ + ./build_wideband.sh diff --git a/firmware/boards/f1_rev2/io/io_pins.h b/firmware/boards/f1_rev2/io/io_pins.h index 4d8d35f..00a1fe7 100644 --- a/firmware/boards/f1_rev2/io/io_pins.h +++ b/firmware/boards/f1_rev2/io/io_pins.h @@ -22,8 +22,11 @@ #define NERNST_42_ESR_DRIVER_PIN 12 // LSU 4.9 - 22K -#define NERNST_ESR_DRIVER_PORT GPIOB -#define NERNST_ESR_DRIVER_PIN 11 +#define NERNST_49_ESR_DRIVER_PORT GPIOB +#define NERNST_49_ESR_DRIVER_PIN 11 + +#define NERNST_49_BIAS_PORT GPIOB +#define NERNST_49_BIAS_PIN 2 // LSU ADV - 47K #define NERNST_ADV_ESR_DRIVER_PORT GPIOB diff --git a/firmware/boards/f1_rev2/port.cpp b/firmware/boards/f1_rev2/port.cpp index 0862784..ad7b0b0 100644 --- a/firmware/boards/f1_rev2/port.cpp +++ b/firmware/boards/f1_rev2/port.cpp @@ -76,3 +76,63 @@ AnalogResult AnalogSample() .VirtualGroundVoltageInt = HALF_VCC, }; } + +/* TODO: optimize */ +void SetupESRDriver(SensorType sensor) +{ + switch (sensor) { + case SensorType::LSU42: + /* disable bias */ + palSetPadMode(NERNST_49_BIAS_PORT, NERNST_49_BIAS_PIN, + PAL_MODE_INPUT); + /* disable all others ESR drivers */ + palSetPadMode(NERNST_49_ESR_DRIVER_PORT, NERNST_49_ESR_DRIVER_PIN, + PAL_MODE_INPUT); + palSetPadMode(NERNST_ADV_ESR_DRIVER_PORT, NERNST_ADV_ESR_DRIVER_PIN, + PAL_MODE_INPUT); + /* enable LSU4.2 */ + palSetPadMode(NERNST_42_ESR_DRIVER_PORT, NERNST_42_ESR_DRIVER_PIN, + PAL_MODE_OUTPUT_PUSHPULL); + break; + case SensorType::LSU49: + /* disable all others ESR drivers */ + palSetPadMode(NERNST_42_ESR_DRIVER_PORT, NERNST_42_ESR_DRIVER_PIN, + PAL_MODE_INPUT); + palSetPadMode(NERNST_ADV_ESR_DRIVER_PORT, NERNST_ADV_ESR_DRIVER_PIN, + PAL_MODE_INPUT); + /* enable LSU4.2 */ + palSetPadMode(NERNST_49_ESR_DRIVER_PORT, NERNST_49_ESR_DRIVER_PIN, + PAL_MODE_OUTPUT_PUSHPULL); + /* enable bias */ + palSetPadMode(NERNST_49_BIAS_PORT, NERNST_49_BIAS_PIN, + PAL_MODE_OUTPUT_PUSHPULL); + palSetPad(NERNST_49_BIAS_PORT, NERNST_49_BIAS_PIN); + break; + case SensorType::LSUADV: + /* disable bias */ + palSetPadMode(NERNST_49_BIAS_PORT, NERNST_49_BIAS_PIN, + PAL_MODE_INPUT); + /* disable all others ESR drivers */ + palSetPadMode(NERNST_49_ESR_DRIVER_PORT, NERNST_49_ESR_DRIVER_PIN, + PAL_MODE_INPUT); + palSetPadMode(NERNST_42_ESR_DRIVER_PORT, NERNST_42_ESR_DRIVER_PIN, + PAL_MODE_INPUT); + /* enable LSU4.2 */ + palSetPadMode(NERNST_ADV_ESR_DRIVER_PORT, NERNST_ADV_ESR_DRIVER_PIN, + PAL_MODE_OUTPUT_PUSHPULL); + break; + } +} + +int GetESRSupplyR() +{ + switch (GetSensorType()) { + case SensorType::LSU42: + return 6800; + case SensorType::LSU49: + return 22000; + case SensorType::LSUADV: + return 47000; + } + return 0; +} diff --git a/firmware/boards/f1_rev3/io/io_pins.h b/firmware/boards/f1_rev3/io/io_pins.h index d72b5d7..33c131f 100644 --- a/firmware/boards/f1_rev3/io/io_pins.h +++ b/firmware/boards/f1_rev3/io/io_pins.h @@ -22,8 +22,11 @@ #define NERNST_42_ESR_DRIVER_PIN 12 // LSU 4.9 - 22K -#define NERNST_ESR_DRIVER_PORT GPIOB -#define NERNST_ESR_DRIVER_PIN 11 +#define NERNST_49_ESR_DRIVER_PORT GPIOB +#define NERNST_49_ESR_DRIVER_PIN 11 + +#define NERNST_49_BIAS_PORT GPIOB +#define NERNST_49_BIAS_PIN 2 // LSU ADV - 47K #define NERNST_ADV_ESR_DRIVER_PORT GPIOB diff --git a/firmware/boards/f1_rev3/port.cpp b/firmware/boards/f1_rev3/port.cpp index 5b57e04..b4e13d6 100644 --- a/firmware/boards/f1_rev3/port.cpp +++ b/firmware/boards/f1_rev3/port.cpp @@ -73,3 +73,63 @@ AnalogResult AnalogSample() .VirtualGroundVoltageInt = HALF_VCC, }; } + +/* TODO: optimize */ +void SetupESRDriver(SensorType sensor) +{ + switch (sensor) { + case SensorType::LSU42: + /* disable bias */ + palSetPadMode(NERNST_49_BIAS_PORT, NERNST_49_BIAS_PIN, + PAL_MODE_INPUT); + /* disable all others ESR drivers */ + palSetPadMode(NERNST_49_ESR_DRIVER_PORT, NERNST_49_ESR_DRIVER_PIN, + PAL_MODE_INPUT); + palSetPadMode(NERNST_ADV_ESR_DRIVER_PORT, NERNST_ADV_ESR_DRIVER_PIN, + PAL_MODE_INPUT); + /* enable LSU4.2 */ + palSetPadMode(NERNST_42_ESR_DRIVER_PORT, NERNST_42_ESR_DRIVER_PIN, + PAL_MODE_OUTPUT_PUSHPULL); + break; + case SensorType::LSU49: + /* disable all others ESR drivers */ + palSetPadMode(NERNST_42_ESR_DRIVER_PORT, NERNST_42_ESR_DRIVER_PIN, + PAL_MODE_INPUT); + palSetPadMode(NERNST_ADV_ESR_DRIVER_PORT, NERNST_ADV_ESR_DRIVER_PIN, + PAL_MODE_INPUT); + /* enable LSU4.2 */ + palSetPadMode(NERNST_49_ESR_DRIVER_PORT, NERNST_49_ESR_DRIVER_PIN, + PAL_MODE_OUTPUT_PUSHPULL); + /* enable bias */ + palSetPadMode(NERNST_49_BIAS_PORT, NERNST_49_BIAS_PIN, + PAL_MODE_OUTPUT_PUSHPULL); + palSetPad(NERNST_49_BIAS_PORT, NERNST_49_BIAS_PIN); + break; + case SensorType::LSUADV: + /* disable bias */ + palSetPadMode(NERNST_49_BIAS_PORT, NERNST_49_BIAS_PIN, + PAL_MODE_INPUT); + /* disable all others ESR drivers */ + palSetPadMode(NERNST_49_ESR_DRIVER_PORT, NERNST_49_ESR_DRIVER_PIN, + PAL_MODE_INPUT); + palSetPadMode(NERNST_42_ESR_DRIVER_PORT, NERNST_42_ESR_DRIVER_PIN, + PAL_MODE_INPUT); + /* enable LSU4.2 */ + palSetPadMode(NERNST_ADV_ESR_DRIVER_PORT, NERNST_ADV_ESR_DRIVER_PIN, + PAL_MODE_OUTPUT_PUSHPULL); + break; + } +} + +int GetESRSupplyR() +{ + switch (GetSensorType()) { + case SensorType::LSU42: + return 6800; + case SensorType::LSU49: + return 22000; + case SensorType::LSUADV: + return 47000; + } + return 0; +} diff --git a/firmware/boards/port.h b/firmware/boards/port.h index f718de8..9022bd7 100644 --- a/firmware/boards/port.h +++ b/firmware/boards/port.h @@ -76,3 +76,6 @@ const char *getTsSignature(); // LSU4.2, LSU4.9 or LSU_ADV SensorType GetSensorType(); +void SetupESRDriver(SensorType sensor); +void ToggleESRDriver(SensorType sensor); +int GetESRSupplyR(); diff --git a/firmware/sampling.cpp b/firmware/sampling.cpp index 790be3f..a54ab3c 100644 --- a/firmware/sampling.cpp +++ b/firmware/sampling.cpp @@ -45,6 +45,7 @@ static void SamplingThread(void*) chRegSetThreadName("Sampling"); + SetupESRDriver(GetSensorType()); /* GD32: Insert 20us delay after ADC enable */ chThdSleepMilliseconds(1); @@ -54,7 +55,7 @@ static void SamplingThread(void*) auto result = AnalogSample(); // Toggle the pin after sampling so that any switching noise occurs while we're doing our math instead of when sampling - palTogglePad(NERNST_ESR_DRIVER_PORT, NERNST_ESR_DRIVER_PIN); + ToggleESRDriver(GetSensorType()); for (int ch = 0; ch < AFR_CHANNELS; ch++) { measure_results &res = results[ch]; @@ -109,8 +110,8 @@ float GetNernstAc(int ch) float GetSensorInternalResistance(int ch) { - // Sensor is the lowside of a divider, top side is 22k, and 3.3v AC pk-pk is injected - float totalEsr = ESR_SUPPLY_R / (VCC_VOLTS / GetNernstAc(ch) - 1); + // Sensor is the lowside of a divider, top side is GetESRSupplyR(), and 3.3v AC pk-pk is injected + float totalEsr = GetESRSupplyR() / (VCC_VOLTS / GetNernstAc(ch) - 1); // There is a resistor between the opamp and Vm sensor pin. Remove the effect of that // resistor so that the remainder is only the ESR of the sensor itself diff --git a/firmware/wideband_config.h b/firmware/wideband_config.h index 41b3c99..936d283 100644 --- a/firmware/wideband_config.h +++ b/firmware/wideband_config.h @@ -14,9 +14,6 @@ // Nernst voltage & ESR sense // ******************************* -// Nernst AC injection resistor value -#define ESR_SUPPLY_R (22000) - // Heater low pass filter #define ESR_SENSE_ALPHA (0.002f)