2020-10-31 21:25:49 +01:00
|
|
|
#include <stdbool.h>
|
|
|
|
|
|
|
|
#include <main.h>
|
|
|
|
#include <adc.h>
|
|
|
|
|
|
|
|
#include <frontend.h>
|
|
|
|
|
|
|
|
#include <logger.h>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static const double SHUNT_RESISTOR = 20.0;
|
|
|
|
static const double U_REF = 3.3;
|
|
|
|
static const double I_THRESHOLD = 0.005;
|
|
|
|
static const uint16_t N_MAX = 4095;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static volatile uint16_t frontendAdcThreshold = 0;
|
|
|
|
static volatile bool frontendEnabled = false;
|
|
|
|
|
|
|
|
|
|
|
|
void frontendInit() {
|
2020-10-31 22:19:48 +01:00
|
|
|
// double u_threshold = I_THRESHOLD * SHUNT_RESISTOR;
|
|
|
|
// frontendAdcThreshold = (uint16_t) (u_threshold / U_REF * ((double) N_MAX));
|
|
|
|
// logMsg("frontendInit, threshold calculated: %d", frontendAdcThreshold);
|
2020-10-31 21:25:49 +01:00
|
|
|
|
|
|
|
HAL_ADCEx_Calibration_Start(&frontendAdc);
|
|
|
|
logMsg("frontendInit, calibration done");
|
|
|
|
HAL_ADC_Start_IT(&frontendAdc);
|
|
|
|
logMsg("frontendInit, adc started");
|
|
|
|
}
|
|
|
|
|
|
|
|
void frontendSetThreshold(uint16_t threshold) {
|
|
|
|
frontendAdcThreshold = threshold;
|
|
|
|
}
|
|
|
|
|
|
|
|
void frontendEnable() {
|
|
|
|
frontendEnabled = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void frontendDisable() {
|
|
|
|
frontendEnabled = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void frontendAdcCallback(ADC_HandleTypeDef* hadc) {
|
2020-10-31 23:50:11 +01:00
|
|
|
uint16_t rawValue = HAL_ADC_GetValue(hadc);
|
2020-10-31 23:19:07 +01:00
|
|
|
HAL_GPIO_TogglePin(Debug_Signal_GPIO_Port, Debug_Signal_Pin);
|
2020-10-31 21:25:49 +01:00
|
|
|
|
|
|
|
}
|