rusEFI
The most advanced open source ECU
Loading...
Searching...
No Matches
hellen_common.cpp
Go to the documentation of this file.
1#include "pch.h"
2#include "hellen_meta.h"
3#include "adc_subscription.h"
4#include "mmc_card.h"
5#include "storage.h"
6
7void hellenWbo() {
8 engineConfiguration->enableAemXSeries = true;
9}
10
11// same for MM100, 144 and 176 - but different for 64 see 'setHellen64Can'
13 engineConfiguration->canTxPin = H176_CAN_TX;
14 engineConfiguration->canRxPin = H176_CAN_RX;
15}
16
17// same for lqfp64
19 engineConfiguration->can2RxPin = Gpio::B12;
20 engineConfiguration->can2TxPin = Gpio::B13;
21}
22
23static void init5vpDiag() {
24#ifdef DIAG_5VP_PIN
25static bool is5vpInit = false;
26 if (!is5vpInit) {
27 efiSetPadMode("5VP_STATE", DIAG_5VP_PIN, PAL_MODE_INPUT);
28 is5vpInit = true;
29 }
30#endif // DIAG_5VP_PIN
31}
32
34 // 4.7k high side/4.7k low side = 2.0 ratio divider
35 engineConfiguration->analogInputDividerCoefficient = 2.0f;
36
37 // set vbatt_divider 5.835
38 // 33k / 6.8k
39 engineConfiguration->vbattDividerCoeff = (33 + 6.8) / 6.8; // 5.835
40 engineConfiguration->adcVcc = 3.3f;
41}
42
45
46 engineConfiguration->vbattAdcChannel = H144_IN_VBATT;
47
48 init5vpDiag(); // piggy back on popular 'setHellenVbatt' method
49}
50
52 engineConfiguration->canTxPin = Gpio::B9;
53 engineConfiguration->canRxPin = Gpio::B8;
54}
55
57
58// newer Hellen boards with megamodule have power management for SD cards etc, older Hellen board do not have that
59PUBLIC_API_WEAK bool isBoardWithPowerManagement() {
60 return false;
61}
62
66
68 // when board is powered down we should be more CANbus silent
69 return getHellenBoardEnabled();
70}
71
72static bool hellenEnPinInitialized = false;
73
74/*PUBLIC_API_WEAK*/ bool fansDisabledByBoardStatus() {
75 return !getHellenBoardEnabled();
76}
77
78PUBLIC_API_WEAK void onHellenEnChange(int value) { UNUSED(value); }
79
80// Board specific helper to enable SD card only
81PUBLIC_API_WEAK bool onHellenSdChange(int value) {
82 UNUSED(value);
83
84 // most Hellen board have no separate SD card power control
85 // return false and let MegaEn to be enabled
86 return false;
87}
88
89#ifndef EFI_BOOTLOADER
91#endif
92
93static void setHellenEnValue(int value) {
94 // todo: can we use 'megaEn.getLogicValue()' instead?
95 static int currentHellenEnValue = -1;
96
97 megaEn.setValue(value, /*isForce*/ true);
98 if (currentHellenEnValue != value) {
99 currentHellenEnValue = value;
100#ifndef EFI_BOOTLOADER
102#endif
103 }
104 // todo: shall we move below callbacks into 'only-if-changed' conditional block?
105 onHellenEnChange(value);
107}
108
109void hellenEnableEn(const char *msg) {
110 efiPrintf("Turning board ON [%s]", msg);
112#if EFI_STORAGE_MFS
113 chThdSleepMilliseconds(1);
115#endif
116}
117
118void hellenDisableEn(const char *msg) {
119#if (EFI_FILE_LOGGING || EFI_CONFIGURATION_STORAGE) && EFI_PROD_CODE
120 // un-mount before turning power off SD card
121 // wait up to 1 second for SD card to become unmounted
122 efiPrintf("Long poll for SD card unmount");
123 int timeout = 1000;
125#if EFI_CONFIGURATION_STORAGE
126 // safe to call, even board does not have EXT FLASH
128#endif // EFI_CONFIGURATION_STORAGE
129 do {
130 chThdSleepMilliseconds(10);
132 break;
133 }
134 timeout -= 10;
135 } while (timeout > 0);
136#if EFI_CONFIGURATION_STORAGE
137 while (storageIsStorageRegistered(STORAGE_MFS_EXT_FLASH) && timeout > 0) {
138 chThdSleepMilliseconds(10);
139 timeout -= 10;
140 }
141#endif // EFI_CONFIGURATION_STORAGE
142#endif
143 efiPrintf("Turning board off [%s]", msg);
145}
146
148 // this function is called from criticalShutdown() that may be called from hardFault handler
149 // please no call to OS functions!
151}
152
153void setHellenEnPin(Gpio pin, bool enableBoardOnStartUp) {
156 megaEn.initPin("EN", pin);
157 if (enableBoardOnStartUp) {
158 hellenEnableEn("start-up");
159 }
160 }
161}
162
163void setHellenMegaEnPin(bool enableBoardOnStartUp) {
164 // H144_GP8 matches MM100_GP8 which is used as PWR_EN on early mm100
165 setHellenEnPin(H144_GP8, enableBoardOnStartUp); // OUT_PWR_EN
166}
167
169 setHellenEnPin(Gpio::MM64_GP1); // OUT_PWR_EN
170}
171
173 // we need to turn 'megaEn' and pause for a bit to make sure that WBO is off and does not wake main firmware right away
175 // todo: 200ms is totally random what's the science for this sleep duration?
176 chThdSleepMilliseconds(200);
177}
178
179/**
180 * We need to make sure that accelerometer device which physically exists does not conflict with SD card
181 * in case of shared SPI.
182 * We reply on specific order of execution here:
183 * 1) accelerometer pre-initialization into safe CS pin state
184 * 2) SD card initialization
185 * 3) accelerometer main initialization if accelerometer feature is desired
186 */
188
191 // weird order of operations? i guess it does not really matter
193}
194
200
202#if EFI_ONBOARD_MEMS
204 accelerometerChipSelect.initPin("mm-CS2", Gpio::H_SPI1_CS2);
206 }
207#else
208 criticalError("probably broken MEMS configuration?");
209#endif // EFI_ONBOARD_MEMS
210}
211
213 static bool initialized = false;
214 static OutputPin terminatorControlPin;
215 if (!initialized) {
216 initialized = true;
217 terminatorControlPin.initPin("CAN-term", H_SPI3_CS); // todo: make this pin configurable
218 terminatorControlPin.setValue(engineConfiguration->boardUseCanTerminator);
219 }
220}
221
223#ifndef EFI_BOOTLOADER
225#endif /* EFI_BOOTLOADER */
226}
227
229{
230#ifdef DIAG_5VP_PIN
231 /* paranoid check */
232 if (!isBrainPinValid(DIAG_5VP_PIN)) {
233 /* Pin is not defined - return success */
234 return ObdCode::None;
235 }
236
238#else
239 return ObdCode::None;
240#endif
241}
242
244#if (HELLEN_BOARD_MM64 == TRUE)
245 (void)hwChannel;
246 if (0)
247#endif
248#if (HELLEN_BOARD_MM100 == TRUE)
249 if (hwChannel == MM100_IN_CRANK_ANALOG) [[unlikely]]
250#endif
251#if (HELLEN_BOARD_MM144 == TRUE)
252 if (hwChannel == H144_IN_CRANK_ANALOG) [[unlikely]]
253#endif
254#if (HELLEN_BOARD_MM176 == TRUE)
255 if (hwChannel == MM176_IN_CRANK_ANALOG) [[unlikely]]
256#endif
257 {
258 // (4.7K || 5.1K) + 4.7K divider
259 // 4.7K || 5.1K == 2.445K
260 return (4.7f + 2.445f) / 4.7f;
261 }
262
263 return engineConfiguration->analogInputDividerCoefficient;
264}
265
266#ifndef EFI_BOOTLOADER
268 // on mega-module we manage SD card power supply
269 if (getHellenBoardEnabled()) {
270 return true;
271 }
272
273 // Board can enable SD card power without enabling WBOs
274 if (onHellenSdChange(1)) {
275 efiPrintf(" *** turning SD power ONLY ***");
276 return true;
277 }
278
279 if (getTimeNowS() > 4 && !isIgnVoltage()) {
280 // looks like vehicle is OFF and we are hooked to USB - turn on peripheral to get Mass Storage Device USB profile
281 efiPrintf(" *** turning board ON to power SD card ***");
283 chThdSleepMilliseconds(200);
284
285 //check state
286 return getHellenBoardEnabled();
287 }
288
289 return false;
290}
291#endif // ! EFI_BOOTLOADER
void efiSetPadMode(const char *msg, brain_pin_e brainPin, iomode_t mode)
static void ResetFilters()
EngineState engineState
Definition engine.h:344
Single output pin reference and state.
Definition efi_output.h:49
void initPin(const char *msg, brain_pin_e brainPin, pin_output_mode_e outputMode, bool forceInitWithFatalError=false)
Definition efi_gpio.cpp:711
bool getLogicValue() const
Definition efi_gpio.cpp:667
void setValue(const char *msg, int logicValue, bool isForce=false)
Definition efi_gpio.cpp:604
bool isInitialized() const
Definition efi_gpio.cpp:559
efitimesec_t getTimeNowS()
Current system time in seconds (32 bits)
Definition efitime.cpp:42
static EngineAccessor engine
Definition engine.h:413
static constexpr engine_configuration_s * engineConfiguration
int hackHellenBoardId(int detectedId)
int detectHellenBoardId()
void hellenBoardStandBy()
ObdCode boardGetAnalogDiagnostic()
void setHellen64MegaEnPin()
PUBLIC_API_WEAK bool isBoardWithPowerManagement()
void configureHellenCanTerminator()
void hellenMegaSdWithAccelerometer()
Timer hellenEnPinStateChange
void hellenEnableEn(const char *msg)
static void init5vpDiag()
void setHellenCan()
void hellenMegaModule()
void hellenMegaAccelerometerPreInitCS2Pin()
bool boardSdCardEnable()
OutputPin accelerometerChipSelect
float getAnalogInputDividerCoefficient(adc_channel_e hwChannel)
void setHellenAnalogDividers()
PUBLIC_API_WEAK bool onHellenSdChange(int value)
void hellenWbo()
bool boardEnableSendWidebandInfo()
bool getHellenBoardEnabled()
void setHellen64Can()
void hellenDisableEn(const char *msg)
static void setHellenEnValue(int value)
void detectHellenBoardType()
void setHellenVbatt()
void setHellenEnPin(Gpio pin, bool enableBoardOnStartUp)
void setHellenMegaEnPin(bool enableBoardOnStartUp)
static bool hellenEnPinInitialized
PUBLIC_API_WEAK void onHellenEnChange(int value)
bool fansDisabledByBoardStatus()
static OutputPin megaEn
void hellenDisableEnSilently()
void setHellenCan2()
void setDefaultHellenAtPullUps(float pullup=HELLEN_DEFAULT_AT_PULLUP)
static void setHellenSdCardSpi1Hardware()
static int initialized
Definition histogram.cpp:39
bool isIgnVoltage()
bool efiReadPin(brain_pin_e pin)
Definition io_pins.cpp:89
UNUSED(samplingTimeSeconds)
void sdCardRequestMode(SD_MODE mode)
SD_MODE sdCardGetCurrentMode(void)
@ SD_MODE_UNMOUNT
Definition mmc_card.h:20
@ SD_MODE_IDLE
Definition mmc_card.h:17
ObdCode
@ OBD_ECM_VSS_OUTPUT_A_MALFUNCTION
bool isBrainPinValid(brain_pin_e brainPin)
brain_pin_e pin
Definition stm32_adc.cpp:15
bool storageIsStorageRegistered(StorageType type)
Definition storage.cpp:230
bool storagRequestUnregisterStorage(StorageType id)
Definition storage.cpp:243
bool storagRequestRegisterStorage(StorageType id)
Definition storage.cpp:238
@ STORAGE_MFS_EXT_FLASH
Definition storage.h:40