Reworking raw adc, handle temp done

Pre seed adc values

Pinecil port

Update PIDThread.cpp

TRGO is more stable for timing (buffered)?
This commit is contained in:
Ben V. Brown
2021-09-11 10:45:15 +10:00
committed by Ben V. Brown
parent fc47c71ec3
commit af0adb0708
14 changed files with 153 additions and 205 deletions

View File

@@ -21,27 +21,7 @@ uint16_t totalPWM; // htim2.Init.Period, the full PWM cycle
history<uint16_t, PID_TIM_HZ> 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() {

View File

@@ -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

View File

@@ -9,10 +9,11 @@
#include "Debug.h"
#include "Pins.h"
#include "gd32vf103.h"
#include "history.hpp"
#include <string.h>
#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<uint16_t, ADC_FILTER_LEN> 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<uint16_t, ADC_FILTER_LEN> 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<uint16_t, ADC_FILTER_LEN> 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);

View File

@@ -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