This commit is contained in:
Matthew Kennedy 2020-09-19 21:39:29 -07:00
parent de2dfe8f60
commit f64d586411
4 changed files with 57 additions and 2 deletions

View File

@ -119,9 +119,10 @@ CSRC = $(ALLCSRC) cfg/board.c
# C++ sources that can be compiled in ARM or THUMB mode depending on the global
# setting.
CPPSRC = $(ALLCPPSRC) \
pwm.cpp \
analog_input.cpp \
can.cpp \
can_helper.cpp \
pwm.cpp \
main.cpp
# List ASM source files here.

41
firmware/analog_input.cpp Normal file
View File

@ -0,0 +1,41 @@
#include "analog_input.h"
#include "hal.h"
#define ADC_OVERSAMPLE 8
static adcsample_t adcBuffer[3 * ADC_OVERSAMPLE];
ADCConversionGroup convGroup =
{
false, 3, nullptr, nullptr,
0, // CFGR1
ADC_TR(0, 0), // TR
ADC_SMPR_SMP_28P5, // SMPR
ADC_CHSELR_CHSEL0 | ADC_CHSELR_CHSEL1 | ADC_CHSELR_CHSEL2
};
static float AverageSamples(adcsample_t* buffer, size_t idx)
{
uint32_t sum = 0;
for (size_t i = 0; i < ADC_OVERSAMPLE; i++)
{
sum += buffer[idx];
idx += ADC_OVERSAMPLE;
}
return (float)sum / ADC_OVERSAMPLE;
}
AnalogResult AnalogSample()
{
adcConvert(&ADCD1, &convGroup, adcBuffer, ADC_OVERSAMPLE);
return
{
.NernstVoltage = AverageSamples(adcBuffer, 0),
.VirtualGroundVoltage = AverageSamples(adcBuffer, 1),
.PumpCurrentVoltage = AverageSamples(adcBuffer, 2),
};
}

10
firmware/analog_input.h Normal file
View File

@ -0,0 +1,10 @@
#pragma once
struct AnalogResult
{
float NernstVoltage;
float VirtualGroundVoltage;
float PumpCurrentVoltage;
};
AnalogResult AnalogSample();

View File

@ -1,6 +1,7 @@
#include "ch.h"
#include "hal.h"
#include "analog_input.h"
#include "can.h"
#include "pwm.h"
@ -14,7 +15,7 @@ Pwm pumpDac(PWMD3, 1, 48000000, 1024);
/*
* Application entry point.
*/
int main(void) {
int main() {
halInit();
chSysInit();
@ -27,6 +28,8 @@ int main(void) {
pumpDac.SetDuty(0.4f);
while (true) {
auto result = AnalogSample();
// dummy data
SendCanData(0.5f, 300);
chThdSleepMilliseconds(10);