forked from rusefi/rusefi
/
efiGpio.cpp
456 lines (387 loc) · 14 KB
/
efiGpio.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
/**
* @file efiGpio.cpp
* @brief EFI-related GPIO code
*
* @date Sep 26, 2014
* @author Andrey Belomutskiy, (c) 2012-2017
*/
#include "main.h"
#include "engine.h"
#include "efiGpio.h"
#if EFI_GPIO_HARDWARE || defined(__DOXYGEN__)
#include "io_pins.h"
#endif /* EFI_GPIO_HARDWARE */
EXTERN_ENGINE;
#if EFI_ENGINE_SNIFFER || defined(__DOXYGEN__)
#include "engine_sniffer.h"
extern WaveChart waveChart;
#endif /* EFI_ENGINE_SNIFFER */
// todo: clean this mess, this should become 'static'/private
EnginePins enginePins;
extern LoggingWithStorage sharedLogger;
pin_output_mode_e DEFAULT_OUTPUT = OM_DEFAULT;
static const char *sparkNames[IGNITION_PIN_COUNT] = { "c1", "c2", "c3", "c4", "c5", "c6", "c7", "c8",
"c9", "cA", "cB", "cD"};
static const char *injectorNames[INJECTION_PIN_COUNT] = { "i1", "i2", "i3", "i4", "i5", "i6", "i7", "i8",
"j9", "iA", "iB", "iC"};
EnginePins::EnginePins() {
dizzyOutput.name = DIZZY_NAME;
tachOut.name = TACH_NAME;
for (int i = 0; i < IGNITION_PIN_COUNT;i++) {
enginePins.coils[i].name = sparkNames[i];
}
for (int i = 0; i < INJECTION_PIN_COUNT;i++) {
enginePins.injectors[i].injectorIndex = i;
enginePins.injectors[i].name = injectorNames[i];
}
}
/**
* Sets the value of the pin. On this layer the value is assigned as is, without any conversion.
*/
#if EFI_PROD_CODE
#define setPinValue(outputPin, electricalValue, logicValue) \
{ \
if ((outputPin)->currentLogicValue != (logicValue)) { \
palWritePad((outputPin)->port, (outputPin)->pin, (electricalValue)); \
(outputPin)->currentLogicValue = (logicValue); \
} \
}
#else /* EFI_PROD_CODE */
#define setPinValue(outputPin, electricalValue, logicValue) \
{ \
if ((outputPin)->currentLogicValue != (logicValue)) { \
(outputPin)->currentLogicValue = (logicValue); \
} \
}
#endif /* EFI_PROD_CODE */
bool EnginePins::stopPins() {
bool result = false;
for (int i = 0; i < IGNITION_PIN_COUNT; i++) {
result |= coils[i].stop();
}
for (int i = 0; i < INJECTION_PIN_COUNT; i++) {
result |= injectors[i].stop();
}
return result;
}
void EnginePins::unregisterPins() {
#if EFI_PROD_CODE || defined(__DOXYGEN__)
fuelPumpRelay.unregisterOutput(activeConfiguration.bc.fuelPumpPin, engineConfiguration->bc.fuelPumpPin);
fanRelay.unregisterOutput(activeConfiguration.bc.fanPin, engineConfiguration->bc.fanPin);
hipCs.unregisterOutput(activeConfiguration.bc.hip9011CsPin, engineConfiguration->bc.hip9011CsPin);
triggerDecoderErrorPin.unregisterOutput(activeConfiguration.bc.triggerErrorPin,
engineConfiguration->bc.triggerErrorPin);
sdCsPin.unregisterOutput(activeConfiguration.bc.sdCardCsPin, engineConfiguration->bc.sdCardCsPin);
accelerometerCs.unregisterOutput(activeConfiguration.LIS302DLCsPin, engineConfiguration->LIS302DLCsPin);
etbOutput1.unregisterOutput(activeConfiguration.bc.etbDirectionPin1,
engineConfiguration->bc.etbDirectionPin1);
etbOutput2.unregisterOutput(activeConfiguration.bc.etbDirectionPin2,
engineConfiguration->bc.etbDirectionPin2);
checkEnginePin.unregisterOutput(activeConfiguration.bc.malfunctionIndicatorPin,
engineConfiguration->bc.malfunctionIndicatorPin);
dizzyOutput.unregisterOutput(activeConfiguration.dizzySparkOutputPin,
engineConfiguration->dizzySparkOutputPin);
tachOut.unregisterOutput(activeConfiguration.bc.tachOutputPin,
engineConfiguration->bc.tachOutputPin);
idleSolenoidPin.unregisterOutput(activeConfiguration.bc.idle.solenoidPin,
engineConfiguration->bc.idle.solenoidPin);
for (int i = 0;i < FSIO_COMMAND_COUNT;i++) {
fsioOutputs[i].unregisterOutput(activeConfiguration.bc.fsioOutputPins[i],
engineConfiguration->bc.fsioOutputPins[i]);
}
alternatorPin.unregisterOutput(activeConfiguration.bc.alternatorControlPin,
engineConfiguration->bc.alternatorControlPin);
mainRelay.unregisterOutput(activeConfiguration.bc.mainRelayPin,
engineConfiguration->bc.mainRelayPin);
#endif /* EFI_PROD_CODE */
}
void EnginePins::reset() {
for (int i = 0; i < INJECTION_PIN_COUNT;i++) {
injectors[i].reset();
}
for (int i = 0; i < IGNITION_PIN_COUNT;i++) {
coils[i].reset();
}
}
void EnginePins::stopIgnitionPins(void) {
#if EFI_PROD_CODE || defined(__DOXYGEN__)
for (int i = 0; i < IGNITION_PIN_COUNT; i++) {
NamedOutputPin *output = &enginePins.coils[i];
output->unregisterOutput(activeConfiguration.bc.ignitionPins[i],
engineConfiguration->bc.ignitionPins[i]);
}
#endif /* EFI_PROD_CODE */
}
void EnginePins::stopInjectionPins(void) {
#if EFI_PROD_CODE || defined(__DOXYGEN__)
for (int i = 0; i < INJECTION_PIN_COUNT; i++) {
NamedOutputPin *output = &enginePins.injectors[i];
output->unregisterOutput(activeConfiguration.bc.injectionPins[i],
engineConfiguration->bc.injectionPins[i]);
}
#endif /* EFI_PROD_CODE */
}
void EnginePins::startIgnitionPins(void) {
#if EFI_PROD_CODE || defined(__DOXYGEN__)
for (int i = 0; i < engineConfiguration->specs.cylindersCount; i++) {
NamedOutputPin *output = &enginePins.coils[i];
// todo: we need to check if mode has changed
if (boardConfiguration->ignitionPins[i] != activeConfiguration.bc.ignitionPins[i]) {
output->initPin(output->name, boardConfiguration->ignitionPins[i],
&boardConfiguration->ignitionPinMode);
}
}
// todo: we need to check if mode has changed
if (engineConfiguration->dizzySparkOutputPin != activeConfiguration.dizzySparkOutputPin) {
enginePins.dizzyOutput.initPin("dizzy tach", engineConfiguration->dizzySparkOutputPin,
&engineConfiguration->dizzySparkOutputPinMode);
}
#endif /* EFI_PROD_CODE */
}
void EnginePins::startInjectionPins(void) {
#if EFI_PROD_CODE || defined(__DOXYGEN__)
// todo: should we move this code closer to the injection logic?
for (int i = 0; i < engineConfiguration->specs.cylindersCount; i++) {
NamedOutputPin *output = &enginePins.injectors[i];
// todo: we need to check if mode has changed
if (engineConfiguration->bc.injectionPins[i] != activeConfiguration.bc.injectionPins[i]) {
output->initPin(output->name, boardConfiguration->injectionPins[i],
&boardConfiguration->injectionPinMode);
}
}
#endif /* EFI_PROD_CODE */
}
NamedOutputPin::NamedOutputPin() : OutputPin() {
name = NULL;
}
NamedOutputPin::NamedOutputPin(const char *name) : OutputPin() {
this->name = name;
}
void NamedOutputPin::setHigh() {
#if EFI_DEFAILED_LOGGING || defined(__DOXYGEN__)
// signal->hi_time = hTimeNow();
#endif /* EFI_DEFAILED_LOGGING */
// turn the output level ACTIVE
setValue(true);
// sleep for the needed duration
#if EFI_ENGINE_SNIFFER || defined(__DOXYGEN__)
// explicit check here is a performance optimization to speed up no-chart mode
if (ENGINE(isEngineChartEnabled)) {
// this is a performance optimization - array index is cheaper then invoking a method with 'switch'
const char *pinName = name;
// dbgDurr = hal_lld_get_counter_value() - dbgStart;
addEngineSniffferEvent(pinName, WC_UP);
}
#endif /* EFI_ENGINE_SNIFFER */
// dbgDurr = hal_lld_get_counter_value() - dbgStart;
}
void NamedOutputPin::setLow() {
// turn off the output
setValue(false);
#if EFI_DEFAILED_LOGGING || defined(__DOXYGEN__)
systime_t after = hTimeNow();
debugInt(&signal->logging, "a_time", after - signal->hi_time);
scheduleLogging(&signal->logging);
#endif /* EFI_DEFAILED_LOGGING */
#if EFI_ENGINE_SNIFFER || defined(__DOXYGEN__)
if (ENGINE(isEngineChartEnabled)) {
// this is a performance optimization - array index is cheaper then invoking a method with 'switch'
const char *pinName = name;
addEngineSniffferEvent(pinName, WC_DOWN);
}
#endif /* EFI_ENGINE_SNIFFER */
}
InjectorOutputPin::InjectorOutputPin() : NamedOutputPin() {
reset();
injectorIndex = -1;
}
bool NamedOutputPin::stop() {
#if EFI_GPIO_HARDWARE || defined(__DOXYGEN__)
if (isInitialized() && getLogicValue()) {
setValue(false);
scheduleMsg(&sharedLogger, "turning off %s", name);
return true;
}
#endif /* EFI_GPIO_HARDWARE */
return false;
}
void InjectorOutputPin::reset() {
overlappingScheduleOffTime = 0;
cancelNextTurningInjectorOff = false;
overlappingCounter = 0;
// todo: this could be refactored by calling some super-reset method
currentLogicValue = INITIAL_PIN_STATE;
}
IgnitionOutputPin::IgnitionOutputPin() {
reset();
}
void IgnitionOutputPin::reset() {
outOfOrder = false;
signalFallSparkId = 0;
}
OutputPin::OutputPin() {
modePtr = &DEFAULT_OUTPUT;
#if EFI_GPIO_HARDWARE || defined(__DOXYGEN__)
port = NULL;
pin = 0;
#endif /* EFI_GPIO_HARDWARE */
currentLogicValue = INITIAL_PIN_STATE;
}
bool OutputPin::isInitialized() {
#if EFI_GPIO_HARDWARE || defined(__DOXYGEN__)
return port != NULL;
#else /* EFI_GPIO_HARDWARE */
return true;
#endif /* EFI_GPIO_HARDWARE */
}
void OutputPin::setValue(int logicValue) {
#if EFI_PROD_CODE
if (port != GPIO_NULL) {
efiAssertVoid(modePtr!=NULL, "pin mode not initialized");
pin_output_mode_e mode = *modePtr;
efiAssertVoid(mode <= OM_OPENDRAIN_INVERTED, "invalid pin_output_mode_e");
int eValue = getElectricalValue(logicValue, mode);
setPinValue(this, eValue, logicValue);
}
#else /* EFI_PROD_CODE */
setPinValue(this, eValue, logicValue);
#endif /* EFI_PROD_CODE */
}
bool OutputPin::getLogicValue() {
return currentLogicValue;
}
void OutputPin::setDefaultPinState(pin_output_mode_e *outputMode) {
pin_output_mode_e mode = *outputMode;
assertOMode(mode);
this->modePtr = outputMode;
setValue(false); // initial state
}
void initOutputPins(void) {
#if EFI_GPIO_HARDWARE || defined(__DOXYGEN__)
/**
* want to make sure it's all zeros so that we can compare in initOutputPinExt() method
*/
// todo: it's too late to clear now? this breaks default status LEDs
// todo: fix this?
// memset(&outputs, 0, sizeof(outputs));
#if HAL_USE_SPI || defined(__DOXYGEN__)
enginePins.sdCsPin.initPin("spi CS5", boardConfiguration->sdCardCsPin);
#endif /* HAL_USE_SPI */
// todo: should we move this code closer to the fuel pump logic?
enginePins.fuelPumpRelay.initPin("fuel pump relay", boardConfiguration->fuelPumpPin, &boardConfiguration->fuelPumpPinMode);
enginePins.mainRelay.initPin("main relay", boardConfiguration->mainRelayPin, &boardConfiguration->mainRelayPinMode);
enginePins.fanRelay.initPin("fan relay", boardConfiguration->fanPin, &boardConfiguration->fanPinMode);
enginePins.o2heater.initPin("o2 heater", boardConfiguration->o2heaterPin);
enginePins.acRelay.initPin("A/C relay", boardConfiguration->acRelayPin, &boardConfiguration->acRelayPinMode);
// digit 1
/*
ledRegister(LED_HUGE_0, GPIOB, 2);
ledRegister(LED_HUGE_1, GPIOE, 7);
ledRegister(LED_HUGE_2, GPIOE, 8);
ledRegister(LED_HUGE_3, GPIOE, 9);
ledRegister(LED_HUGE_4, GPIOE, 10);
ledRegister(LED_HUGE_5, GPIOE, 11);
ledRegister(LED_HUGE_6, GPIOE, 12);
// digit 2
ledRegister(LED_HUGE_7, GPIOE, 13);
ledRegister(LED_HUGE_8, GPIOE, 14);
ledRegister(LED_HUGE_9, GPIOE, 15);
ledRegister(LED_HUGE_10, GPIOB, 10);
ledRegister(LED_HUGE_11, GPIOB, 11);
ledRegister(LED_HUGE_12, GPIOB, 12);
ledRegister(LED_HUGE_13, GPIOB, 13);
// digit 3
ledRegister(LED_HUGE_14, GPIOE, 0);
ledRegister(LED_HUGE_15, GPIOE, 2);
ledRegister(LED_HUGE_16, GPIOE, 4);
ledRegister(LED_HUGE_17, GPIOE, 6);
ledRegister(LED_HUGE_18, GPIOE, 5);
ledRegister(LED_HUGE_19, GPIOE, 3);
ledRegister(LED_HUGE_20, GPIOE, 1);
*/
#endif /* EFI_GPIO_HARDWARE */
}
void OutputPin::initPin(const char *msg, brain_pin_e brainPin) {
initPin(msg, brainPin, &DEFAULT_OUTPUT);
}
void OutputPin::initPin(const char *msg, brain_pin_e brainPin, pin_output_mode_e *outputMode) {
#if EFI_GPIO_HARDWARE || defined(__DOXYGEN__)
if (brainPin == GPIO_UNASSIGNED)
return;
ioportid_t port = getHwPort(msg, brainPin);
int pin = getHwPin(msg, brainPin);
/**
* This method is used for digital GPIO pins only, for peripheral pins see mySetPadMode
*/
if (port == GPIO_NULL) {
// that's for GRIO_NONE
this->port = port;
return;
}
assertOMode(*outputMode);
iomode_t mode = (*outputMode == OM_DEFAULT || *outputMode == OM_INVERTED) ?
PAL_MODE_OUTPUT_PUSHPULL : PAL_MODE_OUTPUT_OPENDRAIN;
/**
* @brief Initialize the hardware output pin while also assigning it a logical name
*/
if (this->port != NULL && (this->port != port || this->pin != pin)) {
/**
* here we check if another physical pin is already assigned to this logical output
*/
// todo: need to clear '&outputs' in io_pins.c
warning(CUSTOM_OBD_PIN_CONFLICT, "outputPin [%s] already assigned to %x%d", msg, this->port, this->pin);
engine->withError = true;
return;
}
this->currentLogicValue = INITIAL_PIN_STATE;
this->port = port;
this->pin = pin;
efiSetPadMode(msg, brainPin, mode);
setDefaultPinState(outputMode);
#endif /* EFI_GPIO_HARDWARE */
}
#if EFI_GPIO_HARDWARE || defined(__DOXYGEN__)
void initPrimaryPins(void) {
enginePins.errorLedPin.initPin("led: ERROR status", LED_ERROR_BRAIN_PIN);
}
/**
* This method is part of fatal error handling.
* Please note that worst case scenario the pins might get re-enabled by some other code :(
* The whole method is pretty naive, but that's at least something.
*/
void turnAllPinsOff(void) {
for (int i = 0; i < INJECTION_PIN_COUNT; i++) {
enginePins.injectors[i].setValue(false);
}
for (int i = 0; i < IGNITION_PIN_COUNT; i++) {
enginePins.coils[i].setValue(false);
}
}
/**
* @deprecated - use hwPortname() instead
*/
const char *portname(ioportid_t GPIOx) {
if (GPIOx == GPIOA)
return "PA";
if (GPIOx == GPIOB)
return "PB";
if (GPIOx == GPIOC)
return "PC";
if (GPIOx == GPIOD)
return "PD";
#if defined(STM32F4XX)
if (GPIOx == GPIOE)
return "PE";
if (GPIOx == GPIOH)
return "PH";
#endif
if (GPIOx == GPIOF)
return "PF";
return "unknown";
}
#else /* EFI_GPIO_HARDWARE */
const char *hwPortname(brain_pin_e brainPin) {
(void)brainPin;
return "N/A";
}
#endif /* EFI_GPIO_HARDWARE */