GCC Code Coverage Report


Directory: ./
File: firmware/hw_layer/digital_input/trigger/trigger_input.cpp
Date: 2025-10-03 00:57:22
Coverage Exec Excl Total
Lines: 66.7% 2 0 3
Functions: 100.0% 1 0 1
Branches: 50.0% 1 0 2
Decisions: 50.0% 1 - 2

Line Branch Decision Exec Source
1 /*
2 * @file trigger_input.cpp
3 *
4 * @date Nov 11, 2019
5 * @author Andrey Belomutskiy, (c) 2012-2021
6 */
7
8 #include "pch.h"
9 #include "trigger_input.h"
10
11 /* TODO:
12 * - merge comparator trigger
13 */
14
15 #if (EFI_SHAFT_POSITION_INPUT) || defined(__DOXYGEN__)
16
17 #if (HAL_TRIGGER_USE_PAL == TRUE) || (HAL_TRIGGER_USE_ADC == TRUE)
18
19 #if (HAL_TRIGGER_USE_PAL == TRUE)
20 int extiTriggerTurnOnInputPin(const char *msg, int index, bool isTriggerShaft);
21 void extiTriggerTurnOffInputPin(brain_pin_e brainPin);
22 #else
23 int extiTriggerTurnOnInputPin(const char *msg, int index, bool isTriggerShaft) {
24 UNUSED(msg);
25 UNUSED(index);
26 UNUSED(isTriggerShaft);
27
28 return -2;
29 }
30 #define extiTriggerTurnOffInputPin(brainPin) ((void)0)
31 #endif
32
33 #if (HAL_TRIGGER_USE_ADC == TRUE)
34 void adcTriggerTurnOnInputPins();
35 int adcTriggerTurnOnInputPin(const char *msg, int index, bool isTriggerShaft);
36 void adcTriggerTurnOffInputPin(brain_pin_e brainPin);
37 #else
38 #define adcTriggerTurnOnInputPins() ((void)0)
39 int adcTriggerTurnOnInputPin(const char *msg, int index, bool isTriggerShaft) {
40 UNUSED(msg);
41 UNUSED(index);
42 UNUSED(isTriggerShaft);
43
44 return -2;
45 }
46 #define adcTriggerTurnOffInputPin(brainPin) ((void)0)
47 #endif
48
49 enum triggerType {
50 TRIGGER_NONE,
51 TRIGGER_EXTI,
52 TRIGGER_ADC,
53 };
54
55 static triggerType shaftTriggerType[TRIGGER_INPUT_PIN_COUNT];
56 static triggerType camTriggerType[CAM_INPUTS_COUNT];
57
58 static int turnOnTriggerInputPin(const char *msg, int index, bool isTriggerShaft) {
59 brain_pin_e brainPin = isTriggerShaft ?
60 engineConfiguration->triggerInputPins[index] : engineConfiguration->camInputs[index];
61
62 if (isTriggerShaft) {
63 shaftTriggerType[index] = TRIGGER_NONE;
64 } else {
65 camTriggerType[index] = TRIGGER_NONE;
66 }
67
68 if (!isBrainPinValid(brainPin)) {
69 return 0;
70 }
71
72 /* ... then ADC */
73 #if HAL_TRIGGER_USE_ADC
74 if (adcTriggerTurnOnInputPin(msg, index, isTriggerShaft) >= 0) {
75 if (isTriggerShaft) {
76 shaftTriggerType[index] = TRIGGER_ADC;
77 } else {
78 camTriggerType[index] = TRIGGER_ADC;
79 }
80 return 0;
81 }
82 #endif
83
84 /* ... then EXTI */
85 if (extiTriggerTurnOnInputPin(msg, index, isTriggerShaft) >= 0) {
86 if (isTriggerShaft) {
87 shaftTriggerType[index] = TRIGGER_EXTI;
88 } else {
89 camTriggerType[index] = TRIGGER_EXTI;
90 }
91 return 0;
92 }
93
94 firmwareError(ObdCode::CUSTOM_ERR_NOT_INPUT_PIN, "%s: Not input pin %s", msg, hwPortname(brainPin));
95
96 return -1;
97 }
98
99 static void turnOffTriggerInputPin(int index, bool isTriggerShaft) {
100 brain_pin_e brainPin = isTriggerShaft ?
101 activeConfiguration.triggerInputPins[index] : activeConfiguration.camInputs[index];
102
103 if (isTriggerShaft) {
104 if (shaftTriggerType[index] == TRIGGER_ADC) {
105 adcTriggerTurnOffInputPin(brainPin);
106 }
107
108 if (shaftTriggerType[index] == TRIGGER_EXTI) {
109 extiTriggerTurnOffInputPin(brainPin);
110 }
111
112 shaftTriggerType[index] = TRIGGER_NONE;
113 } else {
114 if (camTriggerType[index] == TRIGGER_ADC) {
115 adcTriggerTurnOffInputPin(brainPin);
116 }
117
118 if (camTriggerType[index] == TRIGGER_EXTI) {
119 extiTriggerTurnOffInputPin(brainPin);
120 }
121
122 camTriggerType[index] = TRIGGER_NONE;
123 }
124 }
125
126 /*==========================================================================*/
127 /* Exported functions. */
128 /*==========================================================================*/
129
130 void stopTriggerInputPins() {
131 for (int i = 0; i < TRIGGER_INPUT_PIN_COUNT; i++) {
132 if (isConfigurationChanged(triggerInputPins[i])) {
133 turnOffTriggerInputPin(i, /*isTriggerShaft*/true);
134 }
135 }
136 for (int i = 0; i < CAM_INPUTS_COUNT; i++) {
137 if (isConfigurationChanged(camInputs[i])) {
138 turnOffTriggerInputPin(i, /*isTriggerShaft*/false);
139 }
140 }
141 }
142
143 static const char* const camNames[] = { "cam1", "cam2", "cam3", "cam4"};
144
145 void startTriggerInputPins() {
146 for (int i = 0; i < TRIGGER_INPUT_PIN_COUNT; i++) {
147 if (isConfigurationChanged(triggerInputPins[i])) {
148 const char * msg = (i == 0 ? "Trigger #1" : "Trigger #2");
149 turnOnTriggerInputPin(msg, i, /*isTriggerShaft*/true);
150 }
151 }
152
153 for (int i = 0; i < CAM_INPUTS_COUNT; i++) {
154 if (isConfigurationChanged(camInputs[i])) {
155 turnOnTriggerInputPin(camNames[i], i, /*isTriggerShaft*/false);
156 }
157 }
158 }
159
160 void onEcuStartTriggerImplementation() {
161 }
162
163 #endif /* (HAL_TRIGGER_USE_PAL == TRUE) || (HAL_TRIGGER_USE_ADC == TRUE) */
164
165 #endif /* EFI_SHAFT_POSITION_INPUT */
166
167 583 void onEcuStartDoSomethingTriggerInputPins() {
168
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 583 times.
1/2
✗ Decision 'true' not taken.
✓ Decision 'false' taken 583 times.
583 if (hasFirmwareError()) {
169 return;
170 }
171
172 #if EFI_PROD_CODE && EFI_SHAFT_POSITION_INPUT
173 if (isBrainPinValid(engineConfiguration->triggerInputPins[0])) {
174 // todo: we have another 'rpmCalculator.Register' for UNIT_TEST would be great to unify
175 engine->rpmCalculator.Register();
176 } else {
177 // if we do not have primary input channel maybe it's BCM mode and we inject RPM value via Lua?
178 engine->rpmCalculator.unregister();
179 }
180 #endif /* EFI_PROD_CODE && EFI_SHAFT_POSITION_INPUT */
181 }
182