reflow-oven-control-sw/stm-firmware/adc-meas.c

124 lines
2.5 KiB
C
Raw Normal View History

#include <adc-meas.h>
#include <stm32f4xx.h>
#include <stm32-gpio-macros.h>
static float pt1000_offset;
static float pt1000_sens_dev;
static bool calibration_active;
static float filter_alpha;
static float pt1000_adc_raw_lf;
static bool streaming_active;
static volatile bool filter_ready;
static volatile enum adc_p1000_error pt1000_error;
#define ADC_TO_RES(adc) ((float)(adc) / 4096.0f * 2500.0f)
void adc_pt1000_setup_meas()
{
RCC->APB2ENR |= RCC_APB2ENR_ADC1EN;
RCC->AHB1ENR |= ADC_PT1000_PORT_RCC_MASK;
ADC_PT1000_PORT->MODER |= ANALOG(ADC_PT1000_PIN);
}
void adc_pt1000_set_moving_average_filter_param(float alpha)
{
filter_alpha = alpha;
}
void adc_pt1000_set_resistance_calibration(float offset, float sensitivity_deviation, bool active)
{
pt1000_offset = offset;
pt1000_sens_dev = sensitivity_deviation;
calibration_active = active;
}
void adc_pt1000_get_resistance_calibration(float *offset, float *sensitivity_deviation, bool *active)
{
if (!offset || !sensitivity_deviation || !active)
return;
*offset = pt1000_offset;
*sensitivity_deviation = pt1000_sens_dev;
*active = calibration_active;
}
int adc_pt1000_get_current_resistance(float *resistance)
{
int ret_val = 0;
if (!resistance)
return -1001;
if (calibration_active)
*resistance = ADC_TO_RES(pt1000_adc_raw_lf) * (1 + pt1000_sens_dev) + pt1000_offset;
else
*resistance = ADC_TO_RES(pt1000_adc_raw_lf);
if (adc_pt1000_check_error()) {
ret_val = -100;
goto return_value;
}
if (streaming_active) {
ret_val = 1;
goto return_value;
}
if (!filter_ready)
{
ret_val = 2;
}
return_value:
return ret_val;
}
int adc_pt1000_stream_raw_value_to_memory(uint16_t *adc_array, uint32_t length, volatile uint8_t *flag_to_set)
{
}
void adc_pt1000_convert_raw_value_array_to_resistance(float *resistance_dest, uint16_t *raw_source, uint32_t count)
{
}
enum adc_p1000_error adc_pt1000_check_error()
{
return pt1000_error;
}
void adc_pt1000_clear_error()
{
pt1000_error = ADC_PT1000_NO_ERR;
}
static void adc_pt1000_filter(uint16_t adc_value)
{
pt1000_adc_raw_lf = (1-filter_alpha) * pt1000_adc_raw_lf + filter_alpha * (float)adc_value;
}
void ADC_IRQHandler(void)
{
uint32_t adc1_sr;
adc1_sr = ADC1->SR;
if (ADC1->SR & ADC_SR_EOC) {
ADC1->SR &= ~ADC_SR_EOC;
adc_pt1000_filter(ADC1->DR);
}
if (adc1_sr & ADC_SR_OVR) {
ADC1->SR &= ~ADC_SR_OVR;
pt1000_error = ADC_PT1000_OVERFLOW;
/* Disable ADC in case of overrrun*/
ADC1->CR2 &= ADC_CR2_ADON;
}
if (adc1_sr & ADC_SR_AWD) {
ADC1->SR &= ~ADC_SR_AWD;
pt1000_error = ADC_PT1000_WATCHDOG_ERROR;
}
}