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
10
11// same for MM100, 144 and 176
13 engineConfiguration->canTxPin = H176_CAN_TX;
14 engineConfiguration->canRxPin = H176_CAN_RX;
15}
16
21
22static void init5vpDiag() {
23#ifdef DIAG_5VP_PIN
24static bool is5vpInit = false;
25 if (!is5vpInit) {
26 efiSetPadMode("5VP_STATE", DIAG_5VP_PIN, PAL_MODE_INPUT);
27 is5vpInit = true;
28 }
29#endif // DIAG_5VP_PIN
30}
31
33 // 4.7k high side/4.7k low side = 2.0 ratio divider
35
36 // set vbatt_divider 5.835
37 // 33k / 6.8k
38 engineConfiguration->vbattDividerCoeff = (33 + 6.8) / 6.8; // 5.835
40}
41
44
45 engineConfiguration->vbattAdcChannel = H144_IN_VBATT;
46
47 init5vpDiag(); // piggy back on popular 'setHellenVbatt' method
48}
49
54
56
57// newer Hellen boards with megamodule have power management for SD cards etc, older Hellen board do not have that
58PUBLIC_API_WEAK bool isBoardWithPowerManagement() {
59 return false;
60}
61
65
67 // when board is powered down we should be more CANbus silent
68 return getHellenBoardEnabled();
69}
70
71static bool hellenEnPinInitialized = false;
72
73/*PUBLIC_API_WEAK*/ bool fansDisabledByBoardStatus() {
74 return !getHellenBoardEnabled();
75}
76
77PUBLIC_API_WEAK void onHellenEnChange(int value) { UNUSED(value); }
78
79// Board specific helper to enable SD card only
80PUBLIC_API_WEAK bool onHellenSdChange(int value) {
81 UNUSED(value);
82
83 // most Hellen board have no separate SD card power control
84 // return false and let MegaEn to be enabled
85 return false;
86}
87
88#ifndef EFI_BOOTLOADER
90#endif
91
92static void setHellenEnValue(int value) {
93 // todo: can we use 'megaEn.getLogicValue()' instead?
94 static int currentHellenEnValue = -1;
95
96 megaEn.setValue(value, /*isForce*/ true);
97 if (currentHellenEnValue != value) {
98 currentHellenEnValue = value;
99#ifndef EFI_BOOTLOADER
101#endif
102 }
103 // todo: shall we move below callbacks into 'only-if-changed' conditional block?
104 onHellenEnChange(value);
106}
107
108void hellenEnableEn(const char *msg) {
109 efiPrintf("Turning board ON [%s]", msg);
111#if EFI_STORAGE_MFS
112 chThdSleepMilliseconds(1);
114#endif
115}
116
117void hellenDisableEn(const char *msg) {
118#if (EFI_FILE_LOGGING || EFI_CONFIGURATION_STORAGE) && EFI_PROD_CODE
119 // un-mount before turning power off SD card
120 // wait up to 1 second for SD card to become unmounted
121 efiPrintf("Long poll for SD card unmount");
122 int timeout = 1000;
124#if EFI_CONFIGURATION_STORAGE
125 // safe to call, even board does not have EXT FLASH
127#endif // EFI_CONFIGURATION_STORAGE
128 do {
129 chThdSleepMilliseconds(10);
131 break;
132 }
133 timeout -= 10;
134 } while (timeout > 0);
135#if EFI_CONFIGURATION_STORAGE
136 while (storageIsStorageRegistered(STORAGE_MFS_EXT_FLASH) && timeout > 0) {
137 chThdSleepMilliseconds(10);
138 timeout -= 10;
139 }
140#endif // EFI_CONFIGURATION_STORAGE
141#endif
142 efiPrintf("Turning board off [%s]", msg);
144}
145
147 // this function is called from criticalShutdown() that may be called from hardFault handler
148 // please no call to OS functions!
150}
151
152void setHellenEnPin(Gpio pin, bool enableBoardOnStartUp) {
155 megaEn.initPin("EN", pin);
156 if (enableBoardOnStartUp) {
157 hellenEnableEn("start-up");
158 }
159 }
160}
161
162void setHellenMegaEnPin(bool enableBoardOnStartUp) {
163 // H144_GP8 matches MM100_GP8 which is used as PWR_EN on early mm100
164 setHellenEnPin(H144_GP8, enableBoardOnStartUp); // OUT_PWR_EN
165}
166
168 setHellenEnPin(Gpio::MM64_GP1); // OUT_PWR_EN
169}
170
172 // 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
174 // todo: 200ms is totally random what's the science for this sleep duration?
175 chThdSleepMilliseconds(200);
176}
177
178/**
179 * We need to make sure that accelerometer device which physically exists does not conflict with SD card
180 * in case of shared SPI.
181 * We reply on specific order of execution here:
182 * 1) accelerometer pre-initialization into safe CS pin state
183 * 2) SD card initialization
184 * 3) accelerometer main initialization if accelerometer feature is desired
185 */
187
190 // weird order of operations? i guess it does not really matter
192}
193
199
201#if EFI_ONBOARD_MEMS
203 accelerometerChipSelect.initPin("mm-CS2", Gpio::H_SPI1_CS2);
205 }
206#else
207 criticalError("probably broken MEMS configuration?");
208#endif // EFI_ONBOARD_MEMS
209}
210
212 static bool initialized = false;
213 static OutputPin terminatorControlPin;
214 if (!initialized) {
215 initialized = true;
216 terminatorControlPin.initPin("CAN-term", H_SPI3_CS); // todo: make this pin configurable
218 }
219}
220
222#ifndef EFI_BOOTLOADER
224#endif /* EFI_BOOTLOADER */
225}
226
228{
229#ifdef DIAG_5VP_PIN
230 /* paranoid check */
231 if (!isBrainPinValid(DIAG_5VP_PIN)) {
232 /* Pin is not defined - return success */
233 return ObdCode::None;
234 }
235
237#else
238 return ObdCode::None;
239#endif
240}
241
243#if (HELLEN_BOARD_MM64 == TRUE)
244 (void)hwChannel;
245 if (0)
246#endif
247#if (HELLEN_BOARD_MM100 == TRUE)
248 if (hwChannel == MM100_IN_CRANK_ANALOG) [[unlikely]]
249#endif
250#if (HELLEN_BOARD_MM144 == TRUE)
251 if (hwChannel == H144_IN_CRANK_ANALOG) [[unlikely]]
252#endif
253#if (HELLEN_BOARD_MM176 == TRUE)
254 if (hwChannel == MM176_IN_CRANK_ANALOG) [[unlikely]]
255#endif
256 {
257 // (4.7K || 5.1K) + 4.7K divider
258 // 4.7K || 5.1K == 2.445K
259 return (4.7f + 2.445f) / 4.7f;
260 }
261
263}
264
265#ifndef EFI_BOOTLOADER
267 // on mega-module we manage SD card power supply
268 if (getHellenBoardEnabled()) {
269 return true;
270 }
271
272 // Board can enable SD card power without enabling WBOs
273 if (onHellenSdChange(1)) {
274 efiPrintf(" *** turning SD power ONLY ***");
275 return true;
276 }
277
278 if (getTimeNowS() > 4 && !isIgnVoltage()) {
279 // looks like vehicle is OFF and we are hooked to USB - turn on peripheral to get Mass Storage Device USB profile
280 efiPrintf(" *** turning board ON to power SD card ***");
282 chThdSleepMilliseconds(200);
283
284 //check state
285 return getHellenBoardEnabled();
286 }
287
288 return false;
289}
290#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