From af0adb0708a2e9443a3bfdb046d2fe14666a0c7d Mon Sep 17 00:00:00 2001 From: "Ben V. Brown" Date: Sat, 11 Sep 2021 10:45:15 +1000 Subject: [PATCH] Reworking raw adc, handle temp done Pre seed adc values Pinecil port Update PIDThread.cpp TRGO is more stable for timing (buffered)? --- source/Core/BSP/BSP.h | 2 +- source/Core/BSP/MHP30/BSP.cpp | 2 +- source/Core/BSP/Miniware/BSP.cpp | 77 ++---------- .../Core/BSP/Miniware/{Setup.c => Setup.cpp} | 110 +++++++++--------- source/Core/BSP/Miniware/Setup.h | 6 +- source/Core/BSP/Miniware/stm32f1xx_hal_msp.c | 4 +- source/Core/BSP/Pine64/BSP.cpp | 53 ++------- source/Core/BSP/Pine64/Debug.cpp | 2 +- source/Core/BSP/Pine64/Setup.cpp | 81 +++++++++---- source/Core/BSP/Pine64/Setup.h | 2 + source/Core/Drivers/TipThermoModel.cpp | 10 +- source/Core/Threads/GUIThread.cpp | 2 +- source/Core/Threads/PIDThread.cpp | 5 + source/Makefile | 2 +- 14 files changed, 153 insertions(+), 205 deletions(-) rename source/Core/BSP/Miniware/{Setup.c => Setup.cpp} (88%) diff --git a/source/Core/BSP/BSP.h b/source/Core/BSP/BSP.h index 82107c73..e0739c60 100644 --- a/source/Core/BSP/BSP.h +++ b/source/Core/BSP/BSP.h @@ -35,7 +35,7 @@ void resetWatchdog(); // Accepts a output level of 0.. to use to control the tip output PWM void setTipPWM(uint8_t pulse); // Returns the Handle temp in C, X10 -uint16_t getHandleTemperature(); +uint16_t getHandleTemperature(uint8_t sample); // Returns the Tip temperature ADC reading in raw units uint16_t getTipRawTemp(uint8_t refresh); // Returns the main DC input voltage, using the adjustable divisor + sample flag diff --git a/source/Core/BSP/MHP30/BSP.cpp b/source/Core/BSP/MHP30/BSP.cpp index 850c1c83..4757f3d3 100644 --- a/source/Core/BSP/MHP30/BSP.cpp +++ b/source/Core/BSP/MHP30/BSP.cpp @@ -201,7 +201,7 @@ void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) { HAL_IncTick(); } } -uint16_t getHandleTemperature() { +uint16_t getHandleTemperature(uint8_t sample) { int32_t result = getADC(0); return Utils::InterpolateLookupTable(NTCHandleLookup, NTCHandleLookupItems, result); } diff --git a/source/Core/BSP/Miniware/BSP.cpp b/source/Core/BSP/Miniware/BSP.cpp index 7c1d149e..7a75d989 100644 --- a/source/Core/BSP/Miniware/BSP.cpp +++ b/source/Core/BSP/Miniware/BSP.cpp @@ -93,12 +93,12 @@ static const uint16_t NTCHandleLookup[] = { }; #endif -uint16_t getHandleTemperature() { +uint16_t getHandleTemperature(uint8_t sample) { + int32_t result = getADCHandleTemp(sample); #ifdef TEMP_NTC // TS80P uses 100k NTC resistors instead // NTCG104EF104FT1X from TDK // For now not doing interpolation - int32_t result = getADC(0); for (uint32_t i = 0; i < (sizeof(NTCHandleLookup) / (2 * sizeof(uint16_t))); i++) { if (result > NTCHandleLookup[(i * 2) + 0]) { return NTCHandleLookup[(i * 2) + 1] * 10; @@ -114,7 +114,6 @@ uint16_t getHandleTemperature() { // mV per count So we need to subtract an offset of 0.5V to center on 0C // (4964.8 counts) // - int32_t result = getADC(0); result -= 4965; // remove 0.5V offset // 10mV per C // 99.29 counts per Deg C above 0C. Tends to read a tad over across all of my sample units @@ -122,72 +121,18 @@ uint16_t getHandleTemperature() { result /= 994; return result; #endif -} - -uint16_t getTipInstantTemperature() { - uint16_t sum = 0; // 12 bit readings * 8 -> 15 bits - uint16_t readings[8]; - // Looking to reject the highest outlier readings. - // As on some hardware these samples can run into the op-amp recovery time - // Once this time is up the signal stabilises quickly, so no need to reject minimums - readings[0] = hadc1.Instance->JDR1; - readings[1] = hadc1.Instance->JDR2; - readings[2] = hadc1.Instance->JDR3; - readings[3] = hadc1.Instance->JDR4; - readings[4] = hadc2.Instance->JDR1; - readings[5] = hadc2.Instance->JDR2; - readings[6] = hadc2.Instance->JDR3; - readings[7] = hadc2.Instance->JDR4; - - for (int i = 0; i < 8; i++) { - sum += readings[i]; - } - return sum; // 8x over sample -} - -uint16_t getTipRawTemp(uint8_t refresh) { - if (refresh) { - uint16_t lastSample = getTipInstantTemperature(); - rawTempFilter.update(lastSample); - return lastSample; - } else { - return rawTempFilter.average(); - } + return 0; } uint16_t getInputVoltageX10(uint16_t divisor, uint8_t sample) { -// ADC maximum is 32767 == 3.3V at input == 28.05V at VIN -// Therefore we can divide down from there -// Multiplying ADC max by 4 for additional calibration options, -// ideal term is 467 -#ifdef MODEL_TS100 -#define BATTFILTERDEPTH 32 -#else -#define BATTFILTERDEPTH 8 - -#endif - static uint8_t preFillneeded = 10; - static uint32_t samples[BATTFILTERDEPTH]; - static uint8_t index = 0; - if (preFillneeded) { - for (uint8_t i = 0; i < BATTFILTERDEPTH; i++) - samples[i] = getADC(1); - preFillneeded--; - } - if (sample) { - samples[index] = getADC(1); - index = (index + 1) % BATTFILTERDEPTH; - } - uint32_t sum = 0; - - for (uint8_t i = 0; i < BATTFILTERDEPTH; i++) - sum += samples[i]; - - sum /= BATTFILTERDEPTH; - if (divisor == 0) { - divisor = 1; - } - return sum * 4 / divisor; + // ADC maximum is 32767 == 3.3V at input == 28.05V at VIN + // Therefore we can divide down from there + // Multiplying ADC max by 4 for additional calibration options, + // ideal term is 467 + uint32_t res = getADCVin(sample); + res *= 4; + res /= divisor; + return res; } void setTipPWM(uint8_t pulse) { diff --git a/source/Core/BSP/Miniware/Setup.c b/source/Core/BSP/Miniware/Setup.cpp similarity index 88% rename from source/Core/BSP/Miniware/Setup.c rename to source/Core/BSP/Miniware/Setup.cpp index dcdf796f..9532503e 100644 --- a/source/Core/BSP/Miniware/Setup.c +++ b/source/Core/BSP/Miniware/Setup.cpp @@ -5,7 +5,10 @@ * Author: Ben V. Brown */ #include "Setup.h" +#include "BSP.h" #include "Pins.h" +#include "history.hpp" +#include ADC_HandleTypeDef hadc1; ADC_HandleTypeDef hadc2; DMA_HandleTypeDef hdma_adc1; @@ -17,9 +20,9 @@ DMA_HandleTypeDef hdma_i2c1_tx; IWDG_HandleTypeDef hiwdg; TIM_HandleTypeDef htim2; TIM_HandleTypeDef htim3; -#define ADC_CHANNELS 2 -#define ADC_SAMPLES 16 -uint32_t ADCReadings[ADC_SAMPLES * ADC_CHANNELS]; // room for 32 lots of the pair of readings +#define ADC_FILTER_LEN 32 +#define ADC_SAMPLES 16 +uint16_t ADCReadings[ADC_SAMPLES]; // Used to store the adc readings for the handle cold junction temp // Functions static void SystemClock_Config(void); @@ -48,24 +51,53 @@ void Setup_HAL() { MX_TIM3_Init(); MX_TIM2_Init(); MX_IWDG_Init(); - HAL_ADC_Start(&hadc2); - HAL_ADCEx_MultiModeStart_DMA(&hadc1, ADCReadings, (ADC_SAMPLES * ADC_CHANNELS)); // start DMA of normal readings - HAL_ADCEx_InjectedStart(&hadc1); // enable injected readings - HAL_ADCEx_InjectedStart(&hadc2); // enable injected readings + HAL_ADC_Start_DMA(&hadc1, (uint32_t *)ADCReadings, (ADC_SAMPLES)); // start DMA of normal readings + HAL_ADCEx_InjectedStart(&hadc1); // enable injected readings + HAL_ADCEx_InjectedStart(&hadc2); // enable injected readings } -// channel 0 -> temperature sensor, 1-> VIN -uint16_t getADC(uint8_t channel) { - uint32_t sum = 0; - for (uint8_t i = 0; i < ADC_SAMPLES; i++) { - uint16_t adc1Sample = ADCReadings[channel + (i * ADC_CHANNELS)]; - uint16_t adc2Sample = ADCReadings[channel + (i * ADC_CHANNELS)] >> 16; - - sum += (adc1Sample + adc2Sample); +uint16_t getADCHandleTemp(uint8_t sample) { + static history filter = {{0}, 0, 0}; + if (sample) { + uint32_t sum = 0; + for (uint8_t i = 0; i < ADC_SAMPLES; i++) { + sum += ADCReadings[i]; + } + filter.update(sum); } - return sum >> 2; + return filter.average() >> 1; } +uint16_t getADCVin(uint8_t sample) { + static history filter = {{0}, 0, 0}; + if (sample) { + uint16_t latestADC = 0; + + latestADC += hadc2.Instance->JDR1; + latestADC += hadc2.Instance->JDR2; + latestADC += hadc2.Instance->JDR3; + latestADC += hadc2.Instance->JDR4; + latestADC <<= 3; + filter.update(latestADC); + } + return filter.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) { + static history filter = {{0}, 0, 0}; + if (sample) { + uint16_t latestADC = 0; + + latestADC += hadc1.Instance->JDR1; + latestADC += hadc1.Instance->JDR2; + latestADC += hadc1.Instance->JDR3; + latestADC += hadc1.Instance->JDR4; + latestADC <<= 1; + filter.update(latestADC); + return latestADC; + } + return filter.average(); +} /** System Clock Configuration */ void SystemClock_Config(void) { @@ -113,7 +145,6 @@ void SystemClock_Config(void) { /* ADC1 init function */ static void MX_ADC1_Init(void) { - ADC_MultiModeTypeDef multimode; ADC_ChannelConfTypeDef sConfig; ADC_InjectionConfTypeDef sConfigInjected; @@ -125,14 +156,9 @@ static void MX_ADC1_Init(void) { hadc1.Init.DiscontinuousConvMode = DISABLE; hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START; hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT; - hadc1.Init.NbrOfConversion = ADC_CHANNELS; + hadc1.Init.NbrOfConversion = 1; HAL_ADC_Init(&hadc1); - /**Configure the ADC multi-mode - */ - multimode.Mode = ADC_DUALMODE_REGSIMULT_INJECSIMULT; - HAL_ADCEx_MultiModeConfigChannel(&hadc1, &multimode); - /**Configure Regular Channel */ sConfig.Channel = TMP36_ADC1_CHANNEL; @@ -140,12 +166,6 @@ static void MX_ADC1_Init(void) { sConfig.SamplingTime = ADC_SAMPLETIME_71CYCLES_5; HAL_ADC_ConfigChannel(&hadc1, &sConfig); - /**Configure Regular Channel - */ - sConfig.Channel = VIN_ADC1_CHANNEL; - sConfig.Rank = ADC_REGULAR_RANK_2; - HAL_ADC_ConfigChannel(&hadc1, &sConfig); - /**Configure Injected Channel */ // F in = 10.66 MHz @@ -157,15 +177,13 @@ static void MX_ADC1_Init(void) { sConfigInjected.InjectedChannel = TIP_TEMP_ADC1_CHANNEL; sConfigInjected.InjectedRank = 1; sConfigInjected.InjectedNbrOfConversion = 4; - sConfigInjected.InjectedSamplingTime = ADC_SAMPLETIME_1CYCLE_5; - sConfigInjected.ExternalTrigInjecConv = ADC_EXTERNALTRIGINJECCONV_T2_CC1; + sConfigInjected.InjectedSamplingTime = ADC_SAMPLETIME_28CYCLES_5; + sConfigInjected.ExternalTrigInjecConv = ADC_EXTERNALTRIGINJECCONV_T2_TRGO; sConfigInjected.AutoInjectedConv = DISABLE; sConfigInjected.InjectedDiscontinuousConvMode = DISABLE; sConfigInjected.InjectedOffset = 0; HAL_ADCEx_InjectedConfigChannel(&hadc1, &sConfigInjected); - sConfigInjected.InjectedSamplingTime = ADC_SAMPLETIME_1CYCLE_5; - sConfigInjected.InjectedRank = 2; HAL_ADCEx_InjectedConfigChannel(&hadc1, &sConfigInjected); sConfigInjected.InjectedRank = 3; @@ -180,44 +198,30 @@ static void MX_ADC1_Init(void) { /* ADC2 init function */ static void MX_ADC2_Init(void) { - ADC_ChannelConfTypeDef sConfig; ADC_InjectionConfTypeDef sConfigInjected; /**Common config */ hadc2.Instance = ADC2; - hadc2.Init.ScanConvMode = ADC_SCAN_ENABLE; + hadc2.Init.ScanConvMode = ADC_SCAN_DISABLE; hadc2.Init.ContinuousConvMode = ENABLE; hadc2.Init.DiscontinuousConvMode = DISABLE; hadc2.Init.ExternalTrigConv = ADC_SOFTWARE_START; hadc2.Init.DataAlign = ADC_DATAALIGN_RIGHT; - hadc2.Init.NbrOfConversion = ADC_CHANNELS; + hadc2.Init.NbrOfConversion = 0; HAL_ADC_Init(&hadc2); - /**Configure Regular Channel - */ - sConfig.Channel = TMP36_ADC2_CHANNEL; - sConfig.Rank = ADC_REGULAR_RANK_1; - sConfig.SamplingTime = ADC_SAMPLETIME_71CYCLES_5; - HAL_ADC_ConfigChannel(&hadc2, &sConfig); - - sConfig.Channel = VIN_ADC2_CHANNEL; - sConfig.Rank = ADC_REGULAR_RANK_2; - HAL_ADC_ConfigChannel(&hadc2, &sConfig); - /**Configure Injected Channel */ - sConfigInjected.InjectedChannel = TIP_TEMP_ADC2_CHANNEL; + sConfigInjected.InjectedChannel = VIN_ADC2_CHANNEL; sConfigInjected.InjectedRank = ADC_INJECTED_RANK_1; sConfigInjected.InjectedNbrOfConversion = 4; - sConfigInjected.InjectedSamplingTime = ADC_SAMPLETIME_1CYCLE_5; - sConfigInjected.ExternalTrigInjecConv = ADC_EXTERNALTRIGINJECCONV_T2_CC1; + sConfigInjected.InjectedSamplingTime = ADC_SAMPLETIME_28CYCLES_5; + sConfigInjected.ExternalTrigInjecConv = ADC_EXTERNALTRIGINJECCONV_T2_TRGO; sConfigInjected.AutoInjectedConv = DISABLE; sConfigInjected.InjectedDiscontinuousConvMode = DISABLE; sConfigInjected.InjectedOffset = 0; HAL_ADCEx_InjectedConfigChannel(&hadc2, &sConfigInjected); - sConfigInjected.InjectedSamplingTime = ADC_SAMPLETIME_1CYCLE_5; - sConfigInjected.InjectedRank = ADC_INJECTED_RANK_2; HAL_ADCEx_InjectedConfigChannel(&hadc2, &sConfigInjected); sConfigInjected.InjectedRank = ADC_INJECTED_RANK_3; @@ -334,7 +338,7 @@ static void MX_TIM2_Init(void) { HAL_TIM_PWM_Init(&htim2); HAL_TIM_OC_Init(&htim2); - sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET; + sMasterConfig.MasterOutputTrigger = TIM_TRGO_OC1; sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE; HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig); diff --git a/source/Core/BSP/Miniware/Setup.h b/source/Core/BSP/Miniware/Setup.h index 9beb0c60..cf157c1d 100644 --- a/source/Core/BSP/Miniware/Setup.h +++ b/source/Core/BSP/Miniware/Setup.h @@ -27,9 +27,9 @@ extern IWDG_HandleTypeDef hiwdg; extern TIM_HandleTypeDef htim2; extern TIM_HandleTypeDef htim3; void Setup_HAL(); -uint16_t getADC(uint8_t channel); - -void HAL_TIM_MspPostInit(TIM_HandleTypeDef *htim); // Since the hal header file does not define this one +uint16_t getADCHandleTemp(uint8_t sample); +uint16_t getADCVin(uint8_t sample); +void HAL_TIM_MspPostInit(TIM_HandleTypeDef *htim); // Since the hal header file does not define this one #ifdef __cplusplus } diff --git a/source/Core/BSP/Miniware/stm32f1xx_hal_msp.c b/source/Core/BSP/Miniware/stm32f1xx_hal_msp.c index 5f6f7624..7b88dc04 100644 --- a/source/Core/BSP/Miniware/stm32f1xx_hal_msp.c +++ b/source/Core/BSP/Miniware/stm32f1xx_hal_msp.c @@ -38,8 +38,8 @@ void HAL_ADC_MspInit(ADC_HandleTypeDef *hadc) { hdma_adc1.Init.Direction = DMA_PERIPH_TO_MEMORY; hdma_adc1.Init.PeriphInc = DMA_PINC_DISABLE; hdma_adc1.Init.MemInc = DMA_MINC_ENABLE; - hdma_adc1.Init.PeriphDataAlignment = DMA_PDATAALIGN_WORD; - hdma_adc1.Init.MemDataAlignment = DMA_MDATAALIGN_WORD; + hdma_adc1.Init.PeriphDataAlignment = DMA_PDATAALIGN_HALFWORD; + hdma_adc1.Init.MemDataAlignment = DMA_MDATAALIGN_HALFWORD; hdma_adc1.Init.Mode = DMA_CIRCULAR; hdma_adc1.Init.Priority = DMA_PRIORITY_MEDIUM; HAL_DMA_Init(&hdma_adc1); diff --git a/source/Core/BSP/Pine64/BSP.cpp b/source/Core/BSP/Pine64/BSP.cpp index 3a63cfcf..a1978bd3 100644 --- a/source/Core/BSP/Pine64/BSP.cpp +++ b/source/Core/BSP/Pine64/BSP.cpp @@ -21,27 +21,7 @@ uint16_t totalPWM; // htim2.Init.Period, the full PWM cycle history rawTempFilter = {{0}, 0, 0}; void resetWatchdog() { fwdgt_counter_reload(); } -uint16_t getTipInstantTemperature() { - volatile uint16_t sum = 0; // 12 bit readings * 8*2 -> 16 bits - - for (int i = 0; i < 4; i++) { - sum += adc_inserted_data_read(ADC0, i); - sum += adc_inserted_data_read(ADC1, i); - } - return sum; // 8x over sample -} - -uint16_t getTipRawTemp(uint8_t refresh) { - if (refresh) { - uint16_t lastSample = getTipInstantTemperature(); - rawTempFilter.update(lastSample); - return lastSample; - } else { - return rawTempFilter.average(); - } -} - -uint16_t getHandleTemperature() { +uint16_t getHandleTemperature(uint8_t sample) { #ifdef TEMP_TMP36 // We return the current handle temperature in X10 C // TMP36 in handle, 0.5V offset and then 10mV per deg C (0.75V @ 25C for @@ -50,7 +30,7 @@ uint16_t getHandleTemperature() { // mV per count So we need to subtract an offset of 0.5V to center on 0C // (4964.8 counts) // - int32_t result = getADC(0); + int32_t result = getADCHandleTemp(sample); result -= 4965; // remove 0.5V offset // 10mV per C // 99.29 counts per Deg C above 0C @@ -58,33 +38,14 @@ uint16_t getHandleTemperature() { result /= 993; return result; #else -#error +#error Pinecil only uses TMP36 #endif } uint16_t getInputVoltageX10(uint16_t divisor, uint8_t sample) { - - static uint8_t preFillneeded = 10; - static uint32_t samples[BATTFILTERDEPTH]; - static uint8_t index = 0; - if (preFillneeded) { - for (uint8_t i = 0; i < BATTFILTERDEPTH; i++) - samples[i] = getADC(1); - preFillneeded--; - } - if (sample) { - samples[index] = getADC(1); - index = (index + 1) % BATTFILTERDEPTH; - } - uint32_t sum = 0; - - for (uint8_t i = 0; i < BATTFILTERDEPTH; i++) - sum += samples[i]; - - sum /= BATTFILTERDEPTH; - if (divisor == 0) { - divisor = 1; - } - return sum * 4 / divisor; + uint32_t res = getADCVin(sample); + res *= 4; + res /= divisor; + return res; } void unstick_I2C() { diff --git a/source/Core/BSP/Pine64/Debug.cpp b/source/Core/BSP/Pine64/Debug.cpp index de40fe7d..3cb296be 100644 --- a/source/Core/BSP/Pine64/Debug.cpp +++ b/source/Core/BSP/Pine64/Debug.cpp @@ -22,7 +22,7 @@ void log_system_state(int32_t PWMWattsx10) { outputLength = snprintf(uartOutputBuffer, uartOutputBufferLength, "%lu,%u,%li,%u,%lu\r\n", // TipThermoModel::getTipInC(false), // Tip temp in C - getHandleTemperature(), // Handle temp in C X10 + getHandleTemperature(0), // Handle temp in C X10 PWMWattsx10, // Output Wattage pendingPWM, // PWM TipThermoModel::convertTipRawADCTouV(getTipRawTemp(0), true) // Tip temp in uV diff --git a/source/Core/BSP/Pine64/Setup.cpp b/source/Core/BSP/Pine64/Setup.cpp index 76bd8260..826dbdc4 100644 --- a/source/Core/BSP/Pine64/Setup.cpp +++ b/source/Core/BSP/Pine64/Setup.cpp @@ -9,10 +9,11 @@ #include "Debug.h" #include "Pins.h" #include "gd32vf103.h" +#include "history.hpp" #include -#define ADC_NORM_CHANNELS 2 -#define ADC_NORM_SAMPLES 32 -uint16_t ADCReadings[ADC_NORM_SAMPLES * ADC_NORM_CHANNELS]; // room for 32 lots of the pair of readings +#define ADC_NORM_SAMPLES 16 +#define ADC_FILTER_LEN 32 +uint16_t ADCReadings[ADC_NORM_SAMPLES]; // room for 32 lots of the pair of readings // Functions void setup_gpio(); @@ -43,12 +44,48 @@ void hardware_init() { timer_enable(TIMER1); timer_enable(TIMER2); } -// channel 0 -> temperature sensor, 1-> VIN -uint16_t getADC(uint8_t channel) { - uint32_t sum = 0; - for (uint8_t i = 0; i < ADC_NORM_SAMPLES; i++) - sum += ADCReadings[channel + (i * ADC_NORM_CHANNELS)]; - return sum >> 2; + +uint16_t getADCHandleTemp(uint8_t sample) { + static history filter = {{0}, 0, 0}; + if (sample) { + uint32_t sum = 0; + for (uint8_t i = 0; i < ADC_NORM_SAMPLES; i++) { + sum += ADCReadings[i]; + } + filter.update(sum); + } + return filter.average() >> 1; +} + +uint16_t getADCVin(uint8_t sample) { + static history filter = {{0}, 0, 0}; + if (sample) { + uint16_t latestADC = 0; + + latestADC += adc_inserted_data_read(ADC1, 0); + latestADC += adc_inserted_data_read(ADC1, 1); + latestADC += adc_inserted_data_read(ADC1, 2); + latestADC += adc_inserted_data_read(ADC1, 3); + latestADC <<= 1; + filter.update(latestADC); + } + return filter.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) { + static history filter = {{0}, 0, 0}; + if (sample) { + uint16_t latestADC = 0; + + latestADC += adc_inserted_data_read(ADC0, 0); + latestADC += adc_inserted_data_read(ADC0, 1); + latestADC += adc_inserted_data_read(ADC0, 2); + latestADC += adc_inserted_data_read(ADC0, 3); + latestADC <<= 1; + filter.update(latestADC); + return latestADC; + } + return filter.average(); } void setup_uart() { @@ -125,7 +162,7 @@ void setup_dma() { dma_data_parameter.periph_width = DMA_PERIPHERAL_WIDTH_16BIT; dma_data_parameter.memory_width = DMA_MEMORY_WIDTH_16BIT; dma_data_parameter.direction = DMA_PERIPHERAL_TO_MEMORY; - dma_data_parameter.number = ADC_NORM_SAMPLES * ADC_NORM_CHANNELS; + dma_data_parameter.number = ADC_NORM_SAMPLES; dma_data_parameter.priority = DMA_PRIORITY_HIGH; dma_init(DMA0, DMA_CH0, &dma_data_parameter); @@ -160,7 +197,7 @@ void setup_adc() { /* config ADC clock */ rcu_adc_clock_config(RCU_CKADC_CKAPB2_DIV16); // Run in normal parallel + inserted parallel - adc_mode_config(ADC0, ADC_DAUL_REGULAL_PARALLEL_INSERTED_PARALLEL); + adc_mode_config(ADC0, ADC_DAUL_INSERTED_PARALLEL); adc_special_function_config(ADC0, ADC_CONTINUOUS_MODE, ENABLE); adc_special_function_config(ADC0, ADC_SCAN_MODE, ENABLE); adc_special_function_config(ADC1, ADC_CONTINUOUS_MODE, ENABLE); @@ -168,28 +205,22 @@ void setup_adc() { // Align right adc_data_alignment_config(ADC0, ADC_DATAALIGN_RIGHT); adc_data_alignment_config(ADC1, ADC_DATAALIGN_RIGHT); - // Setup reading 2 channels on regular mode (Handle Temp + dc in) - adc_channel_length_config(ADC0, ADC_REGULAR_CHANNEL, ADC_NORM_CHANNELS); - adc_channel_length_config(ADC1, ADC_REGULAR_CHANNEL, ADC_NORM_CHANNELS); + // Setup reading the handle temp + adc_channel_length_config(ADC0, ADC_REGULAR_CHANNEL, 1); + adc_channel_length_config(ADC1, ADC_REGULAR_CHANNEL, 0); // Setup the two channels adc_regular_channel_config(ADC0, 0, TMP36_ADC0_CHANNEL, ADC_SAMPLETIME_71POINT5); // temp sensor - adc_regular_channel_config(ADC1, 0, TMP36_ADC1_CHANNEL, - ADC_SAMPLETIME_71POINT5); // temp sensor - adc_regular_channel_config(ADC0, 1, VIN_ADC0_CHANNEL, - ADC_SAMPLETIME_71POINT5); // DC Input voltage - adc_regular_channel_config(ADC1, 1, VIN_ADC1_CHANNEL, - ADC_SAMPLETIME_71POINT5); // DC Input voltage // Setup that we want all 4 inserted readings to be the tip temp adc_channel_length_config(ADC0, ADC_INSERTED_CHANNEL, 4); adc_channel_length_config(ADC1, ADC_INSERTED_CHANNEL, 4); for (int rank = 0; rank < 4; rank++) { - adc_inserted_channel_config(ADC0, rank, TIP_TEMP_ADC0_CHANNEL, ADC_SAMPLETIME_1POINT5); - adc_inserted_channel_config(ADC1, rank, TIP_TEMP_ADC1_CHANNEL, ADC_SAMPLETIME_1POINT5); + adc_inserted_channel_config(ADC0, rank, TIP_TEMP_ADC0_CHANNEL, ADC_SAMPLETIME_28POINT5); + adc_inserted_channel_config(ADC1, rank, VIN_ADC1_CHANNEL, ADC_SAMPLETIME_28POINT5); } // Setup timer 1 channel 0 to trigger injected measurements - adc_external_trigger_source_config(ADC0, ADC_INSERTED_CHANNEL, ADC0_1_EXTTRIG_INSERTED_T1_CH0); - adc_external_trigger_source_config(ADC1, ADC_INSERTED_CHANNEL, ADC0_1_EXTTRIG_INSERTED_T1_CH0); + adc_external_trigger_source_config(ADC0, ADC_INSERTED_CHANNEL, ADC0_1_EXTTRIG_INSERTED_T1_TRGO); + adc_external_trigger_source_config(ADC1, ADC_INSERTED_CHANNEL, ADC0_1_EXTTRIG_INSERTED_T1_TRGO); adc_external_trigger_source_config(ADC0, ADC_REGULAR_CHANNEL, ADC0_1_EXTTRIG_REGULAR_NONE); adc_external_trigger_source_config(ADC1, ADC_REGULAR_CHANNEL, ADC0_1_EXTTRIG_REGULAR_NONE); @@ -255,7 +286,7 @@ void setup_timers() { timer_ocintpara.ocpolarity = TIMER_OC_POLARITY_HIGH; timer_ocintpara.outputstate = TIMER_CCX_ENABLE; timer_channel_output_config(TIMER1, TIMER_CH_1, &timer_ocintpara); - + timer_master_output_trigger_source_select(TIMER1, TIMER_TRI_OUT_SRC_CH0); timer_channel_output_pulse_value_config(TIMER1, TIMER_CH_1, 0); timer_channel_output_mode_config(TIMER1, TIMER_CH_1, TIMER_OC_MODE_PWM0); timer_channel_output_shadow_config(TIMER1, TIMER_CH_1, TIMER_OC_SHADOW_DISABLE); diff --git a/source/Core/BSP/Pine64/Setup.h b/source/Core/BSP/Pine64/Setup.h index d3d6e12d..5da34f5c 100644 --- a/source/Core/BSP/Pine64/Setup.h +++ b/source/Core/BSP/Pine64/Setup.h @@ -16,6 +16,8 @@ extern "C" { uint16_t getADC(uint8_t channel); void hardware_init(); void setupFUSBIRQ(); +uint16_t getADCHandleTemp(uint8_t sample); +uint16_t getADCVin(uint8_t sample); #ifdef __cplusplus } #endif diff --git a/source/Core/Drivers/TipThermoModel.cpp b/source/Core/Drivers/TipThermoModel.cpp index 813bba11..3b37698f 100644 --- a/source/Core/Drivers/TipThermoModel.cpp +++ b/source/Core/Drivers/TipThermoModel.cpp @@ -71,10 +71,10 @@ uint32_t TipThermoModel::convertFtoC(uint32_t degF) { } uint32_t TipThermoModel::getTipInC(bool sampleNow) { int32_t currentTipTempInC = TipThermoModel::convertTipRawADCToDegC(getTipRawTemp(sampleNow)); - currentTipTempInC += getHandleTemperature() / 10; // Add handle offset - // Power usage indicates that our tip temp is lower than our thermocouple temp. - // I found a number that doesn't unbalance the existing PID, causing overshoot. - // This could be tuned in concert with PID parameters... + currentTipTempInC += getHandleTemperature(sampleNow) / 10; // Add handle offset + // Power usage indicates that our tip temp is lower than our thermocouple temp. + // I found a number that doesn't unbalance the existing PID, causing overshoot. + // This could be tuned in concert with PID parameters... #ifdef THERMAL_MASS_OVERSHOOTS currentTipTempInC += x10WattHistory.average() / 25; #else @@ -93,6 +93,6 @@ uint32_t TipThermoModel::getTipInF(bool sampleNow) { uint32_t TipThermoModel::getTipMaxInC() { uint32_t maximumTipTemp = TipThermoModel::convertTipRawADCToDegC(0x7FFF - (21 * 5)); // back off approx 5 deg c from ADC max - maximumTipTemp += getHandleTemperature() / 10; // Add handle offset + maximumTipTemp += getHandleTemperature(0) / 10; // Add handle offset return maximumTipTemp - 1; } diff --git a/source/Core/Threads/GUIThread.cpp b/source/Core/Threads/GUIThread.cpp index 5458fc59..e942c291 100644 --- a/source/Core/Threads/GUIThread.cpp +++ b/source/Core/Threads/GUIThread.cpp @@ -700,7 +700,7 @@ void showDebugMenu(void) { break; case 8: // Handle Temp - OLED::printNumber(getHandleTemperature(), 3, FontStyle::SMALL); + OLED::printNumber(getHandleTemperature(0), 6, FontStyle::SMALL); break; case 9: // Voltage input diff --git a/source/Core/Threads/PIDThread.cpp b/source/Core/Threads/PIDThread.cpp index ff90c76a..1328891f 100644 --- a/source/Core/Threads/PIDThread.cpp +++ b/source/Core/Threads/PIDThread.cpp @@ -37,6 +37,11 @@ void startPIDTask(void const *argument __unused) { uint32_t PIDTempTarget = 0; uint16_t tipTempCRunawayTemp = 0; TickType_t runawaylastChangeTime = 0; + // Pre-seed the adc filters + for (int i = 0; i < 64; i++) { + vTaskDelay(2); + TipThermoModel::getTipInC(true); + } #ifdef SLEW_LIMIT int32_t x10WattsOutLast = 0; #endif diff --git a/source/Makefile b/source/Makefile index bfdfeb83..39eb601c 100644 --- a/source/Makefile +++ b/source/Makefile @@ -521,7 +521,7 @@ $(foreach group_code,$(LANGUAGE_GROUPS),$(eval $(call multi_lang_rule,$(group_co clean : rm -Rf Core/Gen rm -Rf $(OUTPUT_DIR_BASE) - rm -Rf $(HEXFILE_DIR) + rm -Rf $(HEXFILE_DIR)/* style: @for src in $(ALL_SOURCE) $(ALL_INCLUDES); do \