2021-07-28 04:44:44 -07:00
|
|
|
#include "pch.h"
|
|
|
|
|
|
|
|
#include "vr_pwm.h"
|
|
|
|
|
2021-09-13 08:24:29 -07:00
|
|
|
static OutputPin pins[VR_THRESHOLD_COUNT];
|
|
|
|
static SimplePwm pwms[VR_THRESHOLD_COUNT];
|
2021-07-28 04:44:44 -07:00
|
|
|
|
2021-11-16 01:15:29 -08:00
|
|
|
static void updateVrPwm(int rpm, size_t index) {
|
2021-11-17 00:54:21 -08:00
|
|
|
auto& cfg = engineConfiguration->vrThreshold[index];
|
2021-07-28 04:44:44 -07:00
|
|
|
|
|
|
|
if (cfg.pin == GPIO_UNASSIGNED) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-09-18 18:27:17 -07:00
|
|
|
float thresholdVoltage = interpolate2d(rpm / RPM_1_BYTE_PACKING_MULT, cfg.rpmBins, cfg.values) / PACK_PERCENT_BYTE_MULT;
|
2021-07-28 04:44:44 -07:00
|
|
|
|
|
|
|
// 0v threshold voltage = 3.3v output from mcu = 100% duty
|
|
|
|
// 2.5v threshold voltage = 0v output from mcu = 0% duty
|
|
|
|
float duty = interpolateClamped(0, 1, 2.5f, 0, thresholdVoltage);
|
|
|
|
|
|
|
|
pwms[index].setSimplePwmDutyCycle(duty);
|
|
|
|
}
|
|
|
|
|
2021-11-16 01:15:29 -08:00
|
|
|
void updateVrPwm() {
|
2021-07-28 04:44:44 -07:00
|
|
|
auto rpm = GET_RPM();
|
|
|
|
|
2021-11-17 00:54:21 -08:00
|
|
|
for (size_t i = 0; i < efi::size(engineConfiguration->vrThreshold); i++) {
|
2021-11-16 01:15:29 -08:00
|
|
|
updateVrPwm(rpm, i);
|
2021-07-28 04:44:44 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-16 01:15:29 -08:00
|
|
|
void initVrPwm() {
|
2021-11-17 00:54:21 -08:00
|
|
|
for (size_t i = 0; i < efi::size(engineConfiguration->vrThreshold); i++) {
|
|
|
|
auto& cfg = engineConfiguration->vrThreshold[i];
|
2021-07-28 04:44:44 -07:00
|
|
|
|
|
|
|
if (cfg.pin == GPIO_UNASSIGNED) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
startSimplePwmHard(&pwms[i], "VR PWM",
|
|
|
|
&engine->executor,
|
|
|
|
cfg.pin,
|
|
|
|
&pins[i],
|
|
|
|
10000, // it's guaranteed to be hardware PWM, the faster the PWM, the less noise makes it through
|
|
|
|
0
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|