2015-07-10 06:01:56 -07:00
|
|
|
/**
|
2019-03-29 06:11:13 -07:00
|
|
|
* @file efi_gpio.h
|
2017-04-21 10:36:51 -07:00
|
|
|
* @brief EFI-related GPIO code
|
|
|
|
*
|
2015-07-10 06:01:56 -07:00
|
|
|
*
|
|
|
|
* @date Sep 26, 2014
|
2020-01-07 21:02:40 -08:00
|
|
|
* @author Andrey Belomutskiy, (c) 2012-2020
|
2015-07-10 06:01:56 -07:00
|
|
|
*/
|
2019-12-11 14:48:55 -08:00
|
|
|
|
|
|
|
#pragma once
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2018-09-16 19:26:57 -07:00
|
|
|
#include "global.h"
|
2019-07-05 16:00:44 -07:00
|
|
|
#include "io_pins.h"
|
2019-09-19 18:41:52 -07:00
|
|
|
#include "engine_configuration.h"
|
2020-04-06 11:13:29 -07:00
|
|
|
#include "smart_gpio.h"
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2019-11-25 17:08:01 -08:00
|
|
|
void initPrimaryPins(Logging *sharedLogger);
|
2019-09-19 18:41:52 -07:00
|
|
|
void initOutputPins(DECLARE_ENGINE_PARAMETER_SIGNATURE);
|
2017-04-21 10:36:51 -07:00
|
|
|
|
2019-04-12 19:07:03 -07:00
|
|
|
#if EFI_GPIO_HARDWARE
|
2017-04-21 10:36:51 -07:00
|
|
|
void turnAllPinsOff(void);
|
2017-04-21 12:14:37 -07:00
|
|
|
#else /* EFI_GPIO_HARDWARE */
|
2017-04-21 10:36:51 -07:00
|
|
|
#define turnAllPinsOff() {}
|
2017-04-21 12:14:37 -07:00
|
|
|
#endif /* EFI_GPIO_HARDWARE */
|
|
|
|
|
2020-06-11 17:43:26 -07:00
|
|
|
// Used if you want a function to be virtual only for unit testing purposes
|
|
|
|
#if EFI_UNIT_TEST
|
|
|
|
#define TEST_VIRTUAL virtual
|
|
|
|
#else
|
|
|
|
#define TEST_VIRTUAL
|
|
|
|
#endif
|
|
|
|
|
2019-04-04 16:56:03 -07:00
|
|
|
#ifdef __cplusplus
|
2017-04-21 12:14:37 -07:00
|
|
|
/**
|
|
|
|
* @brief Single output pin reference and state
|
|
|
|
*/
|
|
|
|
class OutputPin {
|
|
|
|
public:
|
|
|
|
OutputPin();
|
2017-07-10 18:41:13 -07:00
|
|
|
/**
|
|
|
|
* initializes pin & registers it in pin repository
|
2020-05-15 15:08:11 -07:00
|
|
|
* outputMode being a pointer allow us to change configuration (for example invert logical pin) in configuration and get resuts applied
|
|
|
|
* away, or at least I hope that's why
|
2017-07-10 18:41:13 -07:00
|
|
|
*/
|
2019-01-16 05:24:37 -08:00
|
|
|
void initPin(const char *msg, brain_pin_e brainPin, const pin_output_mode_e *outputMode);
|
2017-07-10 18:41:13 -07:00
|
|
|
/**
|
|
|
|
* same as above, with DEFAULT_OUTPUT mode
|
|
|
|
*/
|
|
|
|
void initPin(const char *msg, brain_pin_e brainPin);
|
|
|
|
/**
|
2017-07-10 19:05:40 -07:00
|
|
|
* dissociates pin from this output and un-registers it in pin repository
|
2017-07-10 18:41:13 -07:00
|
|
|
*/
|
2019-11-24 21:02:53 -08:00
|
|
|
void unregisterOutput(brain_pin_e oldPin);
|
2017-07-10 18:41:13 -07:00
|
|
|
|
2017-04-21 12:14:37 -07:00
|
|
|
bool isInitialized();
|
2017-07-10 18:41:13 -07:00
|
|
|
|
2020-03-30 22:06:19 -07:00
|
|
|
bool getAndSet(int logicValue);
|
2020-06-11 17:43:26 -07:00
|
|
|
TEST_VIRTUAL void setValue(int logicValue);
|
2018-01-28 08:08:37 -08:00
|
|
|
void toggle();
|
2019-06-08 06:51:36 -07:00
|
|
|
bool getLogicValue() const;
|
2017-07-10 18:34:31 -07:00
|
|
|
|
2017-04-21 14:50:28 -07:00
|
|
|
|
2019-04-12 19:07:03 -07:00
|
|
|
#if EFI_GPIO_HARDWARE
|
2020-04-12 06:39:14 -07:00
|
|
|
ioportid_t port = 0;
|
|
|
|
uint8_t pin = 0;
|
2019-04-10 05:43:54 -07:00
|
|
|
#if (BOARD_EXT_GPIOCHIPS > 0)
|
|
|
|
/* used for external pins */
|
|
|
|
brain_pin_e brainPin;
|
|
|
|
bool ext;
|
|
|
|
#endif
|
2017-04-21 13:52:02 -07:00
|
|
|
#endif /* EFI_GPIO_HARDWARE */
|
2020-04-12 06:39:14 -07:00
|
|
|
int8_t currentLogicValue = INITIAL_PIN_STATE;
|
2017-04-21 12:14:37 -07:00
|
|
|
/**
|
|
|
|
* we track current pin status so that we do not touch the actual hardware if we want to write new pin bit
|
|
|
|
* which is same as current pin value. This maybe helps in case of status leds, but maybe it's a total over-engineering
|
|
|
|
*/
|
2017-07-10 19:05:40 -07:00
|
|
|
private:
|
|
|
|
// todo: inline this method?
|
2019-01-16 05:24:37 -08:00
|
|
|
void setDefaultPinState(const pin_output_mode_e *defaultState);
|
2017-07-10 19:05:40 -07:00
|
|
|
|
|
|
|
// 4 byte pointer is a bit of a memory waste here
|
2019-01-16 05:24:37 -08:00
|
|
|
const pin_output_mode_e *modePtr;
|
2017-04-21 12:14:37 -07:00
|
|
|
};
|
|
|
|
|
2017-04-21 10:36:51 -07:00
|
|
|
|
2015-07-10 06:01:56 -07:00
|
|
|
class NamedOutputPin : public OutputPin {
|
|
|
|
public:
|
|
|
|
NamedOutputPin();
|
2019-06-08 06:51:36 -07:00
|
|
|
explicit NamedOutputPin(const char *name);
|
2017-04-21 16:23:20 -07:00
|
|
|
void setHigh();
|
|
|
|
void setLow();
|
2019-06-08 06:51:36 -07:00
|
|
|
const char *getName() const;
|
|
|
|
const char *getShortName() const;
|
2016-11-03 20:02:58 -07:00
|
|
|
/**
|
|
|
|
* @return true if pin was stopped
|
|
|
|
*/
|
|
|
|
bool stop();
|
2017-01-06 08:02:49 -08:00
|
|
|
// todo: char pointer is a bit of a memory waste here, we can reduce RAM usage by software-based getName() method
|
2015-07-10 06:01:56 -07:00
|
|
|
const char *name;
|
2019-08-18 12:27:10 -07:00
|
|
|
/**
|
|
|
|
* rusEfi Engine Sniffer protocol uses these short names to reduce bytes usage
|
|
|
|
*/
|
2019-05-15 01:26:41 -07:00
|
|
|
const char *shortName = NULL;
|
2015-07-10 06:01:56 -07:00
|
|
|
};
|
|
|
|
|
2016-09-03 21:03:27 -07:00
|
|
|
class InjectorOutputPin : public NamedOutputPin {
|
|
|
|
public:
|
|
|
|
InjectorOutputPin();
|
|
|
|
void reset();
|
2020-07-19 13:46:28 -07:00
|
|
|
|
|
|
|
void open();
|
|
|
|
void close();
|
|
|
|
|
2020-01-10 20:24:17 -08:00
|
|
|
// todo: re-implement this injectorIndex via address manipulation to reduce memory usage?
|
2017-01-06 08:02:49 -08:00
|
|
|
int8_t injectorIndex;
|
2020-07-19 13:46:28 -07:00
|
|
|
|
|
|
|
private:
|
2017-01-06 08:02:49 -08:00
|
|
|
int8_t overlappingCounter;
|
2016-09-03 21:03:27 -07:00
|
|
|
};
|
|
|
|
|
2016-10-31 19:02:12 -07:00
|
|
|
class IgnitionOutputPin : public NamedOutputPin {
|
|
|
|
public:
|
|
|
|
IgnitionOutputPin();
|
|
|
|
void reset();
|
2016-11-01 20:01:54 -07:00
|
|
|
int signalFallSparkId;
|
|
|
|
bool outOfOrder; // https://sourceforge.net/p/rusefi/tickets/319/
|
2016-10-31 19:02:12 -07:00
|
|
|
};
|
|
|
|
|
2016-11-03 20:02:58 -07:00
|
|
|
class EnginePins {
|
2016-09-06 21:02:11 -07:00
|
|
|
public:
|
2016-11-03 20:02:58 -07:00
|
|
|
EnginePins();
|
2020-03-26 05:03:55 -07:00
|
|
|
void startPins();
|
2016-11-01 06:02:29 -07:00
|
|
|
void reset();
|
2016-11-03 20:02:58 -07:00
|
|
|
bool stopPins();
|
2017-06-04 13:35:13 -07:00
|
|
|
void unregisterPins();
|
2017-06-04 15:43:08 -07:00
|
|
|
void startInjectionPins();
|
|
|
|
void startIgnitionPins();
|
2017-11-26 19:30:37 -08:00
|
|
|
void startAuxValves();
|
2017-06-04 15:43:08 -07:00
|
|
|
void stopInjectionPins();
|
|
|
|
void stopIgnitionPins();
|
2015-07-10 06:01:56 -07:00
|
|
|
OutputPin mainRelay;
|
2020-03-24 22:28:37 -07:00
|
|
|
|
|
|
|
// this one cranks engine
|
|
|
|
OutputPin starterControl;
|
|
|
|
// this one prevents driver from cranknig engine
|
2020-03-18 12:56:17 -07:00
|
|
|
OutputPin starterRelayDisable;
|
2020-03-24 22:28:37 -07:00
|
|
|
|
2015-07-10 06:01:56 -07:00
|
|
|
OutputPin fanRelay;
|
2019-09-08 13:45:02 -07:00
|
|
|
// see acRelayPin
|
2015-07-10 06:01:56 -07:00
|
|
|
OutputPin acRelay;
|
|
|
|
OutputPin fuelPumpRelay;
|
|
|
|
OutputPin o2heater;
|
2017-01-30 03:02:53 -08:00
|
|
|
/**
|
|
|
|
* brain board RED LED by default
|
|
|
|
*/
|
2017-02-24 16:42:34 -08:00
|
|
|
OutputPin errorLedPin;
|
2018-07-26 12:51:06 -07:00
|
|
|
OutputPin communicationLedPin; // blue LED on brain board by default
|
|
|
|
OutputPin warningLedPin; // orange LED on brain board by default
|
|
|
|
OutputPin runningLedPin; // green LED on brain board by default
|
2017-02-24 16:42:34 -08:00
|
|
|
|
2019-11-10 09:39:47 -08:00
|
|
|
OutputPin debugTriggerSync;
|
2020-01-31 11:29:52 -08:00
|
|
|
OutputPin boostPin;
|
2016-09-13 21:03:14 -07:00
|
|
|
OutputPin idleSolenoidPin;
|
2020-02-03 22:22:04 -08:00
|
|
|
OutputPin secondIdleSolenoidPin;
|
2016-09-13 21:03:14 -07:00
|
|
|
OutputPin alternatorPin;
|
2017-01-30 03:02:53 -08:00
|
|
|
/**
|
|
|
|
* this one is usually on the gauge cluster, not on the ECU
|
|
|
|
*/
|
2016-09-13 22:01:57 -07:00
|
|
|
OutputPin checkEnginePin;
|
2017-11-26 19:30:37 -08:00
|
|
|
|
2016-09-13 22:01:57 -07:00
|
|
|
NamedOutputPin tachOut;
|
2017-11-26 19:30:37 -08:00
|
|
|
NamedOutputPin dizzyOutput;
|
|
|
|
|
2017-06-25 23:14:31 -07:00
|
|
|
OutputPin fsioOutputs[FSIO_COMMAND_COUNT];
|
2016-09-14 16:03:00 -07:00
|
|
|
OutputPin triggerDecoderErrorPin;
|
|
|
|
OutputPin hipCs;
|
|
|
|
OutputPin sdCsPin;
|
2017-08-27 21:08:37 -07:00
|
|
|
OutputPin accelerometerCs;
|
2016-09-13 21:03:14 -07:00
|
|
|
|
2016-09-03 21:03:27 -07:00
|
|
|
InjectorOutputPin injectors[INJECTION_PIN_COUNT];
|
2016-10-31 19:02:12 -07:00
|
|
|
IgnitionOutputPin coils[IGNITION_PIN_COUNT];
|
2017-11-26 19:30:37 -08:00
|
|
|
NamedOutputPin auxValve[AUX_DIGITAL_VALVE_COUNT];
|
2016-09-06 21:02:11 -07:00
|
|
|
};
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2019-04-04 16:56:03 -07:00
|
|
|
#endif /* __cplusplus */
|
|
|
|
|
2015-07-10 06:01:56 -07:00
|
|
|
/**
|
|
|
|
* it's a macro to be sure that stack is not used
|
|
|
|
* @return 0 for OM_DEFAULT and OM_OPENDRAIN
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define getElectricalValue0(mode) ((mode) == OM_INVERTED || (mode) == OM_OPENDRAIN_INVERTED)
|
|
|
|
|
|
|
|
/**
|
|
|
|
* it's a macro to be sure that stack is not used
|
|
|
|
* @return 1 for OM_DEFAULT and OM_OPENDRAIN
|
|
|
|
*/
|
|
|
|
#define getElectricalValue1(mode) ((mode) == OM_DEFAULT || (mode) == OM_OPENDRAIN)
|
|
|
|
|
|
|
|
#define getElectricalValue(logicalValue, mode) \
|
|
|
|
(logicalValue ? getElectricalValue1(mode) : getElectricalValue0(mode))
|
|
|
|
|
2019-04-12 19:07:03 -07:00
|
|
|
#if EFI_GPIO_HARDWARE
|
2017-04-21 14:38:13 -07:00
|
|
|
|
2019-04-04 16:56:03 -07:00
|
|
|
EXTERNC ioportmask_t getHwPin(const char *msg, brain_pin_e brainPin);
|
|
|
|
EXTERNC ioportid_t getHwPort(const char *msg, brain_pin_e brainPin);
|
2017-04-21 13:20:06 -07:00
|
|
|
const char *portname(ioportid_t GPIOx);
|
2017-04-21 09:31:17 -07:00
|
|
|
|
2017-04-21 13:20:06 -07:00
|
|
|
#endif /* EFI_GPIO_HARDWARE */
|
|
|
|
|
2020-04-08 19:36:18 -07:00
|
|
|
void printSpiConfig(Logging *logging, const char *msg, spi_device_e device);
|
2017-04-21 13:20:06 -07:00
|
|
|
brain_pin_e parseBrainPin(const char *str);
|
|
|
|
const char *hwPortname(brain_pin_e brainPin);
|