Start implementation of PT1000 ADC measurement
This commit is contained in:
		@@ -22,7 +22,7 @@ QUIET=
 | 
			
		||||
endif
 | 
			
		||||
 | 
			
		||||
##Custom Files###
 | 
			
		||||
 | 
			
		||||
CFILES += adc-meas.c
 | 
			
		||||
#TODO
 | 
			
		||||
 | 
			
		||||
###################################################################################
 | 
			
		||||
 
 | 
			
		||||
@@ -1,2 +1,123 @@
 | 
			
		||||
#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;
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -3,6 +3,7 @@
 | 
			
		||||
 | 
			
		||||
#include <stdbool.h>
 | 
			
		||||
#include <stdint.h>
 | 
			
		||||
#include <stm32f4xx.h>
 | 
			
		||||
/**
 | 
			
		||||
 * @brief Moving average filter coefficient for PT1000 measurement
 | 
			
		||||
 */
 | 
			
		||||
@@ -13,6 +14,13 @@
 | 
			
		||||
 */
 | 
			
		||||
#define ADC_PT1000_CHANNEL 2
 | 
			
		||||
 | 
			
		||||
#define ADC_PT1000_PORT GPIOA
 | 
			
		||||
#define ADC_PT1000_PORT_RCC_MASK RCC_AHB1ENR_GPIOAEN
 | 
			
		||||
 | 
			
		||||
#define ADC_PT1000_PIN 2
 | 
			
		||||
 | 
			
		||||
#define ADC_FILTER_STARTUP_CYCLES 200
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @brief Lower value for valid input range for PT1000 measurement
 | 
			
		||||
 *
 | 
			
		||||
@@ -112,4 +120,6 @@ void adc_pt1000_convert_raw_value_array_to_resistance(float *resistance_dest, ui
 | 
			
		||||
 */
 | 
			
		||||
enum adc_p1000_error adc_pt1000_check_error();
 | 
			
		||||
 | 
			
		||||
void adc_pt1000_clear_error();
 | 
			
		||||
 | 
			
		||||
#endif // __ADCMEAS_H__
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										11
									
								
								stm-firmware/include/stm32-gpio-macros.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								stm-firmware/include/stm32-gpio-macros.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,11 @@
 | 
			
		||||
#ifndef __STM32GPIOMACROS_H__
 | 
			
		||||
#define __STM32GPIOMACROS_H__
 | 
			
		||||
 | 
			
		||||
#define OUTPUT(pin)  (0x01U << (pin * 2))
 | 
			
		||||
#define PULLUP(pin) (0x1U << (pin* 2))
 | 
			
		||||
#define ALTFUNC(pin) ((0x2) << (pin * 2))
 | 
			
		||||
#define PINMASK(pin) ((0x3) << (pin * 2))
 | 
			
		||||
#define SETAF(PORT,PIN,AF)  PORT->AFR[(PIN < 8 ? 0 : 1)] |= AF << ((PIN < 8 ? PIN : (PIN - 8)) * 4)
 | 
			
		||||
#define ANALOG(pin)  (0x03 << (pin * 2))
 | 
			
		||||
 | 
			
		||||
#endif /* __STM32GPIOMACROS_H__ */
 | 
			
		||||
@@ -10,6 +10,7 @@
 | 
			
		||||
#include <system_stm32f4xx.h>
 | 
			
		||||
#include <stdlib.h>
 | 
			
		||||
#include <string.h>
 | 
			
		||||
#include <adc-meas.h>
 | 
			
		||||
 | 
			
		||||
#define OUTPUT(pin)  (0b01 << (pin * 2))
 | 
			
		||||
#define ANALOG(pin)  (0x03 << (pin * 2))
 | 
			
		||||
@@ -101,6 +102,7 @@ int main() {
 | 
			
		||||
	systick_setup();
 | 
			
		||||
 | 
			
		||||
	setup_dma(&adc_results, 3);
 | 
			
		||||
	adc_pt1000_setup_meas();
 | 
			
		||||
 | 
			
		||||
	ADC1->CR2 |= ADC_CR2_SWSTART;
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user