From 9fd1d366b90ddc19f8d16e8f8825dce6fe5a3387 Mon Sep 17 00:00:00 2001 From: "Ben V. Brown" Date: Tue, 24 May 2022 23:31:45 +1000 Subject: [PATCH] Make adc range scalable --- source/Core/BSP/Magic/BSP.cpp | 10 +++------- source/Core/BSP/Magic/IRQ.cpp | 2 +- source/Core/BSP/Magic/Setup.cpp | 16 ++++++++++------ source/Core/BSP/Magic/configuration.h | 4 +++- source/Core/Drivers/TipThermoModel.cpp | 6 +++--- source/Core/Inc/history.hpp | 2 +- 6 files changed, 21 insertions(+), 19 deletions(-) diff --git a/source/Core/BSP/Magic/BSP.cpp b/source/Core/BSP/Magic/BSP.cpp index 42d9f75c..7ec29ff6 100644 --- a/source/Core/BSP/Magic/BSP.cpp +++ b/source/Core/BSP/Magic/BSP.cpp @@ -234,11 +234,12 @@ uint8_t getTipResitanceX10() { return lastTipResistance; } void startMeasureTipResistance() { - if (isTipDisconnected()) { return; } - + if (lastTipReadinguV) { + return; + } // We want to calculate lastTipResistance // If tip is connected, and the tip is cold and the tip is not being heated // We can use the GPIO to inject a small current into the tip and measure this @@ -277,10 +278,5 @@ void FinishMeasureTipResistance() { } else { newRes = 80; } - if (lastTipResistance != newRes) { -#ifdef POW_PD - USBPowerDelivery::triggerRenegotiation(); -#endif - } lastTipResistance = newRes; } \ No newline at end of file diff --git a/source/Core/BSP/Magic/IRQ.cpp b/source/Core/BSP/Magic/IRQ.cpp index 9e514faf..a6751680 100644 --- a/source/Core/BSP/Magic/IRQ.cpp +++ b/source/Core/BSP/Magic/IRQ.cpp @@ -174,4 +174,4 @@ uint16_t getADCHandleTemp(uint8_t sample) { return ADC_Temp.average(); } uint16_t getADCVin(uint8_t sample) { return ADC_Vin.average(); } // Returns either average or instant value. When sample is set the samples from the injected ADC are copied to the filter and then the raw reading is returned -uint16_t getTipRawTemp(uint8_t sample) { return ADC_Tip.average() >> 1; } +uint16_t getTipRawTemp(uint8_t sample) { return ADC_Tip.average(); } diff --git a/source/Core/BSP/Magic/Setup.cpp b/source/Core/BSP/Magic/Setup.cpp index 7b62ee47..71565ada 100644 --- a/source/Core/BSP/Magic/Setup.cpp +++ b/source/Core/BSP/Magic/Setup.cpp @@ -29,13 +29,17 @@ void hardware_init() { gpio_set_mode(TMP36_INPUT_Pin, GPIO_INPUT_MODE); gpio_set_mode(TIP_TEMP_Pin, GPIO_INPUT_MODE); gpio_set_mode(VIN_Pin, GPIO_INPUT_MODE); - gpio_set_mode(TIP_RESISTANCE_SENSE, GPIO_OUTPUT_PP_MODE); + + gpio_set_mode(TIP_RESISTANCE_SENSE, GPIO_OUTPUT_MODE); gpio_write(TIP_RESISTANCE_SENSE, 0); + MSG((char *)"Magic Starting\r\n"); setup_timer_scheduler(); setup_adc(); setup_pwm(); I2C_ClockSet(I2C0_ID, 400000); // Sets clock to around 375kHz + TIMER_SetCompValue(TIMER_CH0, TIMER_COMP_ID_1, 0); + PWM_Channel_Enable(PWM_Channel); } void setup_pwm(void) { // Setup PWM we use for driving the tip @@ -56,8 +60,8 @@ void setup_pwm(void) { } const ADC_Chan_Type adc_tip_pos_chans[] - = {TIP_TEMP_ADC_CHANNEL, TIP_TEMP_ADC_CHANNEL, TIP_TEMP_ADC_CHANNEL, TIP_TEMP_ADC_CHANNEL, TMP36_ADC_CHANNEL, VIN_ADC_CHANNEL, TMP36_ADC_CHANNEL, VIN_ADC_CHANNEL}; -const ADC_Chan_Type adc_tip_neg_chans[] = {ADC_CHAN_GND, ADC_CHAN_GND, ADC_CHAN_GND, ADC_CHAN_GND, ADC_CHAN_GND, ADC_CHAN_GND, ADC_CHAN_GND, ADC_CHAN_GND}; + = {VIN_ADC_CHANNEL, TIP_TEMP_ADC_CHANNEL, TIP_TEMP_ADC_CHANNEL, TIP_TEMP_ADC_CHANNEL, TIP_TEMP_ADC_CHANNEL, TMP36_ADC_CHANNEL, VIN_ADC_CHANNEL, TMP36_ADC_CHANNEL, VIN_ADC_CHANNEL}; +const ADC_Chan_Type adc_tip_neg_chans[] = {ADC_CHAN_GND, ADC_CHAN_GND, ADC_CHAN_GND, ADC_CHAN_GND, ADC_CHAN_GND, ADC_CHAN_GND, ADC_CHAN_GND, ADC_CHAN_GND, ADC_CHAN_GND}; static_assert(sizeof(adc_tip_pos_chans) == sizeof(adc_tip_neg_chans)); void setup_adc(void) { @@ -71,9 +75,9 @@ void setup_adc(void) { adc_cfg.clkDiv = ADC_CLK_DIV_4; adc_cfg.vref = ADC_VREF_3P2V; - adc_cfg.resWidth = ADC_DATA_WIDTH_12; + adc_cfg.resWidth = ADC_DATA_WIDTH_16_WITH_128_AVERAGE; adc_cfg.inputMode = ADC_INPUT_SINGLE_END; - adc_cfg.v18Sel = ADC_V18_SEL_1P82V; + adc_cfg.v18Sel = ADC_V18_SEL_1P72V; adc_cfg.v11Sel = ADC_V11_SEL_1P1V; adc_cfg.gain1 = ADC_PGA_GAIN_NONE; adc_cfg.gain2 = ADC_PGA_GAIN_NONE; @@ -89,7 +93,7 @@ void setup_adc(void) { ADC_Init(&adc_cfg); adc_fifo_cfg.dmaEn = DISABLE; - adc_fifo_cfg.fifoThreshold = ADC_FIFO_THRESHOLD_8; + adc_fifo_cfg.fifoThreshold = ADC_FIFO_THRESHOLD_4; ADC_FIFO_Cfg(&adc_fifo_cfg); ADC_MIC_Bias_Disable(); ADC_Tsen_Disable(); diff --git a/source/Core/BSP/Magic/configuration.h b/source/Core/BSP/Magic/configuration.h index 5684486f..f42044df 100644 --- a/source/Core/BSP/Magic/configuration.h +++ b/source/Core/BSP/Magic/configuration.h @@ -68,6 +68,7 @@ #define TEMP_CHANGE_LONG_STEP_MAX 90 // Temp change long step MAX value /* Power pulse for keeping power banks awake*/ + #define POWER_PULSE_INCREMENT 1 #define POWER_PULSE_MAX 100 // x10 max watts #define POWER_PULSE_WAIT_MAX 9 // 9*2.5s = 22.5 seconds @@ -114,7 +115,8 @@ #endif #ifdef MODEL_Magic -#define ADC_VDD_MV 3200 // ADC max reading millivolts +#define ADC_VDD_MV 3300 // ADC max reading millivolts +#define ADC_MAX_READING 42000 // Maximum reading of the adc #define SOLDERING_TEMP 320 // Default soldering temp is 320.0 °C #define VOLTAGE_DIV 600 // 600 - Default divider from schematic #define CALIBRATION_OFFSET 800 // 900 - Default adc offset in uV diff --git a/source/Core/Drivers/TipThermoModel.cpp b/source/Core/Drivers/TipThermoModel.cpp index 9c41bcec..1a9da1bb 100644 --- a/source/Core/Drivers/TipThermoModel.cpp +++ b/source/Core/Drivers/TipThermoModel.cpp @@ -36,7 +36,7 @@ uint32_t TipThermoModel::convertTipRawADCTouV(uint16_t rawADC, bool ski uint32_t vddRailmVX10 = ADC_VDD_MV * 10; // The vreg is +-2%, but we have no higher accuracy available // 4096 * 8 readings for full scale // Convert the input ADC reading back into mV times 10 format. - uint32_t rawInputmVX10 = (rawADC * vddRailmVX10) / (4096 * 8); + uint32_t rawInputmVX10 = (rawADC * vddRailmVX10) / (ADC_MAX_READING); uint32_t valueuV = rawInputmVX10 * 100; // shift into uV // Now to divide this down by the gain @@ -90,7 +90,7 @@ uint32_t TipThermoModel::getTipInF(bool sampleNow) { } uint32_t TipThermoModel::getTipMaxInC() { - uint32_t maximumTipTemp = TipThermoModel::convertTipRawADCToDegC(0x7FFF - (21 * 3)); // back off approx 5 deg c from ADC max - maximumTipTemp += getHandleTemperature(0) / 10; // Add handle offset + uint32_t maximumTipTemp = TipThermoModel::convertTipRawADCToDegC(ADC_MAX_READING - (21 * 3)); // back off approx 5 deg c from ADC max + maximumTipTemp += getHandleTemperature(0) / 10; // Add handle offset return maximumTipTemp - 1; } diff --git a/source/Core/Inc/history.hpp b/source/Core/Inc/history.hpp index 6718e3c6..6ab51b21 100644 --- a/source/Core/Inc/history.hpp +++ b/source/Core/Inc/history.hpp @@ -14,7 +14,7 @@ template struct history { static const uint8_t size = SIZE; T buf[size]; - int32_t sum; + uint32_t sum; uint8_t loc; void update(T const val) {