| Line | Branch | Decision | Exec | Source |
|---|---|---|---|---|
| 1 | /** | |||
| 2 | * @file boards/hellen/hellen_board_id.h | |||
| 3 | * @brief Board-Id detector for Hellen boards | |||
| 4 | * | |||
| 5 | * @author andreika <prometheus.pcb@gmail.com> | |||
| 6 | * @author Andrey Belomutskiy, (c) 2012-2022 | |||
| 7 | */ | |||
| 8 | ||||
| 9 | #pragma once | |||
| 10 | ||||
| 11 | // this is used by the detection method and should be visible to the interrupt handler (hellenBoardIdInputCallback) | |||
| 12 | class HellenBoardIdFinderState | |||
| 13 | { | |||
| 14 | public: | |||
| 15 | efitick_t timeChargeNt = 0; | |||
| 16 | ||||
| 17 | ioportid_t rOutputPinPort; | |||
| 18 | int rOutputPinIdx; | |||
| 19 | ioportid_t rInputPinPort; | |||
| 20 | int rInputPinIdx; | |||
| 21 | ||||
| 22 | #if EFI_PROD_CODE | |||
| 23 | semaphore_t boardId_wake; | |||
| 24 | #endif /* EFI_PROD_CODE */ | |||
| 25 | }; | |||
| 26 | ||||
| 27 | // We need to solve the following equation for R or C: | |||
| 28 | // X^Td - X^(Tc1+Td) + X^(Tc2-Tc1) - 1 = 0 | |||
| 29 | // where: X = exp(-1/(RC)) | |||
| 30 | class HellenBoardIdSolver : public NewtonsMethodSolver | |||
| 31 | { | |||
| 32 | public: | |||
| 33 | 9 | float fx(float x) override { | ||
| 34 | 9 | return exp(k1 / x) - exp(k2 / x) + exp(k3 / x) - 1.0; | ||
| 35 | } | |||
| 36 | ||||
| 37 | // first-order derivative | |||
| 38 | 9 | float dfx(float x) override { | ||
| 39 | 9 | return (-1.0f / (x * x)) * (k1 * exp(k1 / x) - k2 * exp(k2 / x) + k3 * exp(k3 / x)); | ||
| 40 | } | |||
| 41 | ||||
| 42 | // Newton numerical method (x is R and y is C, or vice-versa) | |||
| 43 | float solve(float Tc1, float Tc2, float x0, float y, float deltaX); | |||
| 44 | ||||
| 45 | private: | |||
| 46 | // exponential function coefs (see solve()) | |||
| 47 | float k1, k2, k3; | |||
| 48 | }; | |||
| 49 | ||||
| 50 | ||||
| 51 | class HellenBoardIdFinderBase | |||
| 52 | { | |||
| 53 | public: | |||
| 54 | float calc(float Tc1_us, float Tc2_us, float Rest, float C, bool testOnlyMajorSeries, float *Rmeasured, float *Cest, int *rIdx); | |||
| 55 | ||||
| 56 | float findClosestResistor(float R, bool testOnlyMajorSeries, int *rIdx); | |||
| 57 | float calcEstimatedResistance(float Tc1_us, float C); | |||
| 58 | ||||
| 59 | public: | |||
| 60 | HellenBoardIdFinderState state; | |||
| 61 | }; | |||
| 62 | ||||
| 63 | template <size_t NumPins> | |||
| 64 | class HellenBoardIdFinder : public HellenBoardIdFinderBase | |||
| 65 | { | |||
| 66 | public: | |||
| 67 | HellenBoardIdFinder(brain_pin_e (&rP)[NumPins]) : rPins(rP) {} | |||
| 68 | ||||
| 69 | // R1 or R2 | |||
| 70 | bool measureChargingTimes(int i, float & Tc1_us, float & Tc2_us); | |||
| 71 | bool measureChargingTimesAveraged(int i, float & Tc1_us, float & Tc2_us); | |||
| 72 | ||||
| 73 | public: | |||
| 74 | brain_pin_e (&rPins)[NumPins]; | |||
| 75 | HellenBoardIdFinderState state; | |||
| 76 | }; | |||
| 77 | ||||
| 78 |