2020-01-26 21:07:54 +01:00
|
|
|
/*
|
|
|
|
* main.c
|
|
|
|
*
|
|
|
|
* Created on: Apr 25, 2015
|
|
|
|
* Author: mari
|
|
|
|
*/
|
|
|
|
#include <stm32f4xx.h>
|
2020-02-02 00:01:08 +01:00
|
|
|
#include <systick.h>
|
2020-01-26 21:07:54 +01:00
|
|
|
//#include <arm_math.h>
|
|
|
|
#include <system_stm32f4xx.h>
|
|
|
|
#include <stdlib.h>
|
2020-02-02 00:01:08 +01:00
|
|
|
#include <string.h>
|
2020-01-26 21:07:54 +01:00
|
|
|
|
|
|
|
#define OUTPUT(pin) (0b01 << (pin * 2))
|
|
|
|
#define ANALOG(pin) (0x03 << (pin * 2))
|
|
|
|
|
|
|
|
struct adc_conversions {
|
|
|
|
uint16_t pa2_raw;
|
|
|
|
uint16_t ref_raw;
|
|
|
|
uint16_t temp_raw;
|
|
|
|
uint16_t vbat_raw;
|
|
|
|
};
|
|
|
|
|
|
|
|
static volatile struct adc_conversions adc_results;
|
|
|
|
|
|
|
|
volatile uint64_t sample_count = 0;
|
|
|
|
volatile uint8_t new_data = 0;
|
|
|
|
|
|
|
|
|
|
|
|
void DMA2_Stream0_IRQHandler()
|
|
|
|
{
|
|
|
|
uint32_t lisr;
|
|
|
|
|
|
|
|
lisr = DMA2->LISR;
|
|
|
|
DMA2->LIFCR = lisr;
|
|
|
|
|
|
|
|
if (lisr & DMA_LISR_TCIF0) {
|
2020-02-02 00:01:08 +01:00
|
|
|
if (new_data)
|
|
|
|
new_data = 2;
|
2020-01-26 21:07:54 +01:00
|
|
|
new_data = 1;
|
|
|
|
sample_count++;
|
2020-02-02 00:01:08 +01:00
|
|
|
|
|
|
|
GPIOB->ODR ^= (1<<3);
|
2020-01-26 21:07:54 +01:00
|
|
|
}
|
2020-02-02 00:01:08 +01:00
|
|
|
|
2020-01-26 21:07:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void setup_dma(void *dest, size_t size)
|
|
|
|
{
|
|
|
|
RCC->AHB1ENR |= RCC_AHB1ENR_DMA2EN;
|
|
|
|
|
|
|
|
DMA2_Stream0->M0AR = (uint32_t)dest;
|
|
|
|
DMA2_Stream0->PAR = (uint32_t)&ADC1->DR;
|
|
|
|
DMA2_Stream0->CR = DMA_SxCR_PL_1 | DMA_SxCR_MSIZE_0 | DMA_SxCR_PSIZE_0 | DMA_SxCR_MINC |
|
|
|
|
DMA_SxCR_CIRC | DMA_SxCR_TCIE;
|
|
|
|
DMA2_Stream0->NDTR = size;
|
|
|
|
NVIC_EnableIRQ(DMA2_Stream0_IRQn);
|
|
|
|
|
|
|
|
DMA2_Stream0->CR |= DMA_SxCR_EN;
|
|
|
|
new_data = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
float ext_lf_corr;
|
|
|
|
|
|
|
|
float temp_lf_corr;
|
|
|
|
|
|
|
|
float ref_lf;
|
|
|
|
float vdd_calculated = 3.3f;
|
|
|
|
|
|
|
|
float vbat_lf_corr;
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-02-02 00:01:08 +01:00
|
|
|
static void setup_timers(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
struct adc_conversions working;
|
2020-01-26 21:07:54 +01:00
|
|
|
|
|
|
|
RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN;
|
|
|
|
RCC->APB2ENR |= RCC_APB2ENR_ADC1EN;
|
|
|
|
RCC->AHB1ENR |= RCC_AHB1ENR_GPIOBEN;
|
|
|
|
__DSB();
|
|
|
|
GPIOB->MODER = OUTPUT(2) | OUTPUT(3);
|
|
|
|
GPIOA->MODER |= ANALOG(2);
|
|
|
|
GPIOB->ODR |= (1<<2);
|
|
|
|
|
|
|
|
ADC1->SMPR2 = (7U<<(3*2));
|
|
|
|
ADC1->SMPR1 = (7U<<18) | (7U<<21) | (7U<<24);
|
|
|
|
ADC1->SQR1 = (2<<20);
|
|
|
|
ADC1->SQR3 = (2<<0) | (16<<(5*2)) | (17<<(5*1));
|
|
|
|
|
|
|
|
ADC->CCR |= (0x2<<16) | ADC_CCR_TSVREFE;
|
|
|
|
ADC1->CR1 = ADC_CR1_SCAN;
|
|
|
|
ADC1->CR2 = ADC_CR2_ADON | ADC_CR2_CONT | ADC_CR2_DMA | ADC_CR2_DDS;
|
|
|
|
|
|
|
|
//while(1);
|
|
|
|
|
2020-02-02 00:01:08 +01:00
|
|
|
systick_setup();
|
|
|
|
|
2020-01-26 21:07:54 +01:00
|
|
|
setup_dma(&adc_results, 3);
|
|
|
|
|
|
|
|
ADC1->CR2 |= ADC_CR2_SWSTART;
|
|
|
|
|
|
|
|
while(1) {
|
2020-02-02 00:01:08 +01:00
|
|
|
if (!new_data) {
|
2020-01-26 21:07:54 +01:00
|
|
|
continue;
|
2020-02-02 00:01:08 +01:00
|
|
|
}
|
2020-01-26 21:07:54 +01:00
|
|
|
|
2020-02-02 00:01:08 +01:00
|
|
|
|
2020-01-26 21:07:54 +01:00
|
|
|
memcpy(&working, &adc_results, sizeof(adc_results));
|
2020-02-02 00:01:08 +01:00
|
|
|
new_data = 0;
|
|
|
|
//ref_lf = 0.995f * ref_lf + 0.005f * (float)working.ref_raw;
|
|
|
|
//vdd_calculated = ((1.21f * 4096)/ ref_lf);
|
2020-01-26 21:07:54 +01:00
|
|
|
|
2020-02-02 00:01:08 +01:00
|
|
|
//temp_lf_corr = 0.99f * temp_lf_corr + 0.01 * (float)working.temp_raw * vdd_calculated / 2.495f;
|
2020-01-26 21:07:54 +01:00
|
|
|
ext_lf_corr = 0.995f * ext_lf_corr + 0.005f * (float)working.pa2_raw / 4096 * 2500.0f; // * vdd_calculated / 2.495f;
|
|
|
|
|
|
|
|
//vbat_lf_corr = 0.99 * vbat_lf_corr + 0.01 * (float)working.vbat_raw / 4096 * vdd_calculated * 2.0f;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-02-02 00:01:08 +01:00
|
|
|
|