GCC Code Coverage Report


Directory: ./
File: firmware/controllers/sensors/vr_pwm.cpp
Date: 2025-10-03 00:57:22
Coverage Exec Excl Total
Lines: 53.8% 14 0 26
Functions: 75.0% 3 0 4
Branches: 50.0% 5 0 10
Decisions: 50.0% 5 - 10

Line Branch Decision Exec Source
1 #include "pch.h"
2
3 #include "vr_pwm.h"
4
5 static OutputPin pins[VR_THRESHOLD_COUNT];
6 static SimplePwm pwms[VR_THRESHOLD_COUNT];
7
8 // Default to 3.3v if not defined, most boards wire the VR threshold input directly to an MCU pin.
9 #ifndef VR_SUPPLY_VOLTAGE
10 #define VR_SUPPLY_VOLTAGE 3.3f
11 #endif
12
13 2170 static void updateVrThresholdPwm(float rpm, size_t index) {
14 2170 auto& cfg = engineConfiguration->vrThreshold[index];
15
16
1/2
✓ Branch 1 taken 2170 times.
✗ Branch 2 not taken.
1/2
✓ Decision 'true' taken 2170 times.
✗ Decision 'false' not taken.
2170 if (!isBrainPinValid(cfg.pin)) {
17 2170 return;
18 }
19
20 float thresholdVoltage = interpolate2d(rpm, cfg.rpmBins, cfg.values);
21
22 // 0v threshold voltage = 3.3v output from mcu = 100% duty
23 // 2.5v threshold voltage = 0v output from mcu = 0% duty
24 float thresholdInputVoltage = interpolateClamped(0, 3.3f, 2.5f, 0, thresholdVoltage);
25
26 float duty = thresholdInputVoltage / VR_SUPPLY_VOLTAGE;
27
28 pwms[index].setSimplePwmDutyCycle(duty);
29 }
30
31 1085 void updateVrThresholdPwm() {
32 1085 auto rpm = Sensor::getOrZero(SensorType::Rpm);
33
34
2/2
✓ Branch 1 taken 2170 times.
✓ Branch 2 taken 1085 times.
2/2
✓ Decision 'true' taken 2170 times.
✓ Decision 'false' taken 1085 times.
3255 for (size_t i = 0; i < efi::size(engineConfiguration->vrThreshold); i++) {
35 2170 updateVrThresholdPwm(rpm, i);
36 }
37 1085 }
38
39 void initVrThresholdPwm() {
40 for (size_t i = 0; i < efi::size(engineConfiguration->vrThreshold); i++) {
41 auto& cfg = engineConfiguration->vrThreshold[i];
42
43 if (!isBrainPinValid(cfg.pin)) {
44 continue;
45 }
46
47 startSimplePwmHard(&pwms[i], "VR Threshold",
48 &engine->scheduler,
49 cfg.pin,
50 &pins[i],
51 10000, // it's guaranteed to be hardware PWM, the faster the PWM, the less noise makes it through
52 0
53 );
54 }
55 }
56
57 586 void setDefaultVrThresholds() {
58
2/2
✓ Branch 0 taken 1172 times.
✓ Branch 1 taken 586 times.
2/2
✓ Decision 'true' taken 1172 times.
✓ Decision 'false' taken 586 times.
1758 for (int i = 0;i<VR_THRESHOLD_COUNT;i++) {
59 1172 setRpmTableBin(engineConfiguration->vrThreshold[i].rpmBins);
60 1172 setLinearCurve(engineConfiguration->vrThreshold[i].values, 0.6, 1.2, 0.1);
61 }
62 586 }
63