From 61446a6170e1b92590aa83585b182328d6fcceff Mon Sep 17 00:00:00 2001 From: shadowm60 Date: Sat, 24 Apr 2021 03:16:27 +0300 Subject: [PATCH] Handle can message cyclicity (#2573) * Handle can message cyclicity update of canDash to support different cyclicity of messages * review updates --- firmware/controllers/can/can.h | 55 +++ firmware/controllers/can/can_dash.cpp | 474 +++++++++++++---------- firmware/controllers/can/can_dash.h | 9 +- firmware/controllers/can/can_tx.cpp | 84 ++-- firmware/hw_layer/drivers/can/can_hw.cpp | 1 - 5 files changed, 376 insertions(+), 247 deletions(-) diff --git a/firmware/controllers/can/can.h b/firmware/controllers/can/can.h index c49ac56537..4c57661d95 100644 --- a/firmware/controllers/can/can.h +++ b/firmware/controllers/can/can.h @@ -16,6 +16,25 @@ #define CAN_TIMEOUT MS2NT(100) +//can tx periodic task cycle time in frequency, 200hz -> 5ms period +#define CAN_CYCLE_FREQ (200.0f) +//can tx periodic task cycle time in ms +#define CAN_CYCLE_PERIOD (CH_CFG_ST_FREQUENCY / CAN_CYCLE_FREQ) + +enum class CanInterval : uint16_t { + None = 0, + _5ms = 1 << 0, + _10ms = 1 << 1, + _20ms = 1 << 2, + _50ms = 1 << 3, + _100ms = 1 << 4, + _200ms = 1 << 5, + _250ms = 1 << 6, + _500ms = 1 << 7, + _1000ms = 1 << 8, + _MAX_Cycle = _1000ms, +}; + class CanListener; class CanSensorBase; @@ -39,6 +58,42 @@ public: void PeriodicTask(efitime_t nowNt) override; }; +// allow using shorthand CI +using CI = CanInterval; + +// logical and/or operators so we can use our enum like an int +constexpr CI operator |(CI lhs, CI rhs) { + using T = std::underlying_type_t; + return static_cast(static_cast(lhs) | static_cast(rhs)); +} + +constexpr CI operator &(CI lhs, CI rhs) { + using T = std::underlying_type_t; + return static_cast(static_cast(lhs) & static_cast(rhs)); +} + +constexpr CI& operator |=(CI& lhs, CI rhs) { + lhs = lhs | rhs; + return lhs; +} + +class CanCycle { +public: + explicit CanCycle(uint32_t cycleCounter200hz) + : m_cycleFlags(computeFlags(cycleCounter200hz)) + { + } + + bool isInterval(CanInterval interval) { + return CanInterval::None != (m_cycleFlags & interval); + } + +private: + static CanInterval computeFlags(uint32_t cycleCount); + + const CanInterval m_cycleFlags; +}; + // We need these helpers because the frame layout is different on STM32H7 #ifdef STM32H7XX #define CAN_SID(f) ((f).std.SID) diff --git a/firmware/controllers/can/can_dash.cpp b/firmware/controllers/can/can_dash.cpp index 9a898df9d2..08f77e908f 100644 --- a/firmware/controllers/can/can_dash.cpp +++ b/firmware/controllers/can/can_dash.cpp @@ -30,46 +30,47 @@ EXTERN_ENGINE; * All the below packets are using 500kb/s * */ -#define CAN_BMW_E46_SPEED 0x153 -#define CAN_BMW_E46_RPM 0x316 -#define CAN_BMW_E46_DME2 0x329 -#define CAN_BMW_E46_CLUSTER_STATUS 0x613 -#define CAN_BMW_E46_CLUSTER_STATUS_2 0x615 -#define CAN_FIAT_MOTOR_INFO 0x561 -#define CAN_MAZDA_RX_RPM_SPEED 0x201 +#define CAN_BMW_E46_SPEED 0x153 +#define CAN_BMW_E46_RPM 0x316 +#define CAN_BMW_E46_DME2 0x329 +#define CAN_BMW_E46_CLUSTER_STATUS 0x613 +#define CAN_BMW_E46_CLUSTER_STATUS_2 0x615 +#define CAN_FIAT_MOTOR_INFO 0x561 +#define CAN_MAZDA_RX_RPM_SPEED 0x201 #define CAN_MAZDA_RX_STEERING_WARNING 0x300 -#define CAN_MAZDA_RX_STATUS_1 0x212 -#define CAN_MAZDA_RX_STATUS_2 0x420 +#define CAN_MAZDA_RX_STATUS_1 0x212 +#define CAN_MAZDA_RX_STATUS_2 0x420 + // https://wiki.openstreetmap.org/wiki/VW-CAN -#define CAN_VAG_RPM 0x280 -#define CAN_VAG_CLT 0x288 -#define CAN_VAG_CLT_V2 0x420 -#define CAN_VAG_IMMO 0x3D0 +#define CAN_VAG_RPM 0x280 /* _10ms cycle */ +#define CAN_VAG_CLT 0x288 /* _10ms cycle */ +#define CAN_VAG_CLT_V2 0x420 /* _10ms cycle */ +#define CAN_VAG_IMMO 0x3D0 /* _10ms cycle */ + //w202 DASH -#define W202_STAT_1 0x308 -#define W202_STAT_2 0x608 -#define W202_ALIVE 0x210 -#define W202_STAT_3 0x310 +#define W202_STAT_1 0x308 /* _20ms cycle */ +#define W202_STAT_2 0x608 /* _100ms cycle */ +#define W202_ALIVE 0x210 /* _200ms cycle */ +#define W202_STAT_3 0x310 /* _200ms cycle */ //BMW E90 DASH -#define E90_ABS_COUNTER 0x0C0 +#define E90_ABS_COUNTER 0x0C0 #define E90_SEATBELT_COUNTER 0x0D7 -#define E90_T15 0x130 -#define E90_RPM 0x175 -#define E90_BRAKE_COUNTER 0x19E -#define E90_SPEED 0x1A6 -#define E90_TEMP 0x1D0 -#define E90_GEAR 0x1D2 -#define E90_FUEL 0x349 -#define E90_EBRAKE 0x34F -#define E90_TIME 0x39E +#define E90_T15 0x130 +#define E90_RPM 0x175 +#define E90_BRAKE_COUNTER 0x19E +#define E90_SPEED 0x1A6 +#define E90_TEMP 0x1D0 +#define E90_GEAR 0x1D2 +#define E90_FUEL 0x349 +#define E90_EBRAKE 0x34F +#define E90_TIME 0x39E // Nissan z33 350Z and else // 0x23d = 573 -#define NISSAN_RPM_CLT 0x23d +#define NISSAN_RPM_CLT 0x23D static uint8_t rpmcounter; -static uint16_t e90msgcounter; static uint8_t seatbeltcnt; static uint8_t abscounter = 0xF0; static uint8_t brakecnt_1 = 0xF0, brakecnt_2 = 0xF0; @@ -81,210 +82,268 @@ static bool cluster_time_set; constexpr uint8_t e90_temp_offset = 49; -void canDashboardBMW(void) { - //BMW Dashboard - { - CanTxMessage msg(CAN_BMW_E46_SPEED); - msg.setShortValue(10 * 8, 1); - } +void canDashboardBMW(CanCycle cycle); +void canDashboardFiat(CanCycle cycle); +void canDashboardVAG(CanCycle cycle); +void canMazdaRX8(CanCycle cycle); +void canDashboardW202(CanCycle cycle); +void canDashboardBMWE90(CanCycle cycle); +void canDashboardVagMqb(CanCycle cycle); - { - CanTxMessage msg(CAN_BMW_E46_RPM); - msg.setShortValue((int) (GET_RPM() * 6.4), 2); - } +void updateDash(CanCycle cycle) { - { - CanTxMessage msg(CAN_BMW_E46_DME2); - msg.setShortValue((int) ((Sensor::get(SensorType::Clt).value_or(0) + 48.373) / 0.75), 1); + // Transmit dash data, if enabled + switch (CONFIG(canNbcType)) { + case CAN_BUS_NBC_BMW: + canDashboardBMW(cycle); + break; + case CAN_BUS_NBC_FIAT: + canDashboardFiat(cycle); + break; + case CAN_BUS_NBC_VAG: + canDashboardVAG(cycle); + break; + case CAN_BUS_MAZDA_RX8: + canMazdaRX8(cycle); + break; + case CAN_BUS_W202_C180: + canDashboardW202(cycle); + break; + case CAN_BUS_BMW_E90: + canDashboardBMWE90(cycle); + break; + case CAN_BUS_MQB: + canDashboardVagMqb(cycle); + break; + default: + break; } } -void canMazdaRX8(void) { - { - CanTxMessage msg(CAN_MAZDA_RX_STEERING_WARNING); - // todo: something needs to be set here? see http://rusefi.com/wiki/index.php?title=Vehicle:Mazda_Rx8_2004 - } - - { - CanTxMessage msg(CAN_MAZDA_RX_RPM_SPEED); - - float kph = getVehicleSpeed(); - - msg.setShortValue(SWAP_UINT16(GET_RPM() * 4), 0); - msg.setShortValue(0xFFFF, 2); - msg.setShortValue(SWAP_UINT16((int )(100 * kph + 10000)), 4); - msg.setShortValue(0, 6); - } - - { - CanTxMessage msg(CAN_MAZDA_RX_STATUS_1); - msg[0] = 0xFE; //Unknown - msg[1] = 0xFE; //Unknown - msg[2] = 0xFE; //Unknown - msg[3] = 0x34; //DSC OFF in combo with byte 5 Live data only seen 0x34 - msg[4] = 0x00; // B01000000; // Brake warning B00001000; //ABS warning - msg[5] = 0x40; // TCS in combo with byte 3 - msg[6] = 0x00; // Unknown - msg[7] = 0x00; // Unused - } - - { - CanTxMessage msg(CAN_MAZDA_RX_STATUS_2); - auto clt = Sensor::get(SensorType::Clt); - msg[0] = (uint8_t)(clt.value_or(0) + 69); //temp gauge //~170 is red, ~165 last bar, 152 centre, 90 first bar, 92 second bar - msg[1] = ((int16_t)(engine->engineState.vssEventCounter*(engineConfiguration->vehicleSpeedCoef*0.277*2.58))) & 0xff; - msg[2] = 0x00; // unknown - msg[3] = 0x00; //unknown - msg[4] = 0x01; //Oil Pressure (not really a gauge) - msg[5] = 0x00; //check engine light - msg[6] = 0x00; //Coolant, oil and battery - if ((GET_RPM()>0) && (Sensor::get(SensorType::BatteryVoltage).value_or(VBAT_FALLBACK_VALUE)<13)) { - msg.setBit(6, 6); // battery light +//BMW Dashboard +//todo: we use 50ms fixed cycle, trace is needed to check for correct period +void canDashboardBMW(CanCycle cycle) { + + if (cycle.isInterval(CI::_50ms)) { + { + CanTxMessage msg(CAN_BMW_E46_SPEED); + msg.setShortValue(10 * 8, 1); } - if (!clt.Valid || clt.Value > 105) { - // coolant light, 101 - red zone, light means its get too hot - // Also turn on the light in case of sensor failure - msg.setBit(6, 1); + + { + CanTxMessage msg(CAN_BMW_E46_RPM); + msg.setShortValue((int) (GET_RPM() * 6.4), 2); + } + + { + CanTxMessage msg(CAN_BMW_E46_DME2); + msg.setShortValue((int) ((Sensor::get(SensorType::Clt).value_or(0) + 48.373) / 0.75), 1); } - //oil pressure warning lamp bit is 7 - msg[7] = 0x00; //unused } } -void canDashboardFiat(void) { - { - //Fiat Dashboard - CanTxMessage msg(CAN_FIAT_MOTOR_INFO); - msg.setShortValue((int) (Sensor::get(SensorType::Clt).value_or(0) - 40), 3); //Coolant Temp - msg.setShortValue(GET_RPM() / 32, 6); //RPM +//todo: we use 50ms fixed cycle, trace is needed to check for correct period +void canMazdaRX8(CanCycle cycle) { + if (cycle.isInterval(CI::_50ms)) { + { + CanTxMessage msg(CAN_MAZDA_RX_STEERING_WARNING); + // todo: something needs to be set here? see http://rusefi.com/wiki/index.php?title=Vehicle:Mazda_Rx8_2004 + } + + { + CanTxMessage msg(CAN_MAZDA_RX_RPM_SPEED); + + float kph = getVehicleSpeed(); + + msg.setShortValue(SWAP_UINT16(GET_RPM() * 4), 0); + msg.setShortValue(0xFFFF, 2); + msg.setShortValue(SWAP_UINT16((int )(100 * kph + 10000)), 4); + msg.setShortValue(0, 6); + } + + { + CanTxMessage msg(CAN_MAZDA_RX_STATUS_1); + msg[0] = 0xFE; //Unknown + msg[1] = 0xFE; //Unknown + msg[2] = 0xFE; //Unknown + msg[3] = 0x34; //DSC OFF in combo with byte 5 Live data only seen 0x34 + msg[4] = 0x00; // B01000000; // Brake warning B00001000; //ABS warning + msg[5] = 0x40; // TCS in combo with byte 3 + msg[6] = 0x00; // Unknown + msg[7] = 0x00; // Unused + } + + { + CanTxMessage msg(CAN_MAZDA_RX_STATUS_2); + auto clt = Sensor::get(SensorType::Clt); + msg[0] = (uint8_t)(clt.value_or(0) + 69); //temp gauge //~170 is red, ~165 last bar, 152 centre, 90 first bar, 92 second bar + msg[1] = ((int16_t)(engine->engineState.vssEventCounter*(engineConfiguration->vehicleSpeedCoef*0.277*2.58))) & 0xff; + msg[2] = 0x00; // unknown + msg[3] = 0x00; //unknown + msg[4] = 0x01; //Oil Pressure (not really a gauge) + msg[5] = 0x00; //check engine light + msg[6] = 0x00; //Coolant, oil and battery + if ((GET_RPM()>0) && (Sensor::get(SensorType::BatteryVoltage).value_or(VBAT_FALLBACK_VALUE)<13)) { + msg.setBit(6, 6); // battery light + } + if (!clt.Valid || clt.Value > 105) { + // coolant light, 101 - red zone, light means its get too hot + // Also turn on the light in case of sensor failure + msg.setBit(6, 1); + } + //oil pressure warning lamp bit is 7 + msg[7] = 0x00; //unused + } + } + +} + +void canDashboardFiat(CanCycle cycle) { + if (cycle.isInterval(CI::_50ms)) { + { + //Fiat Dashboard + CanTxMessage msg(CAN_FIAT_MOTOR_INFO); + msg.setShortValue((int) (Sensor::get(SensorType::Clt).value_or(0) - 40), 3); //Coolant Temp + msg.setShortValue(GET_RPM() / 32, 6); //RPM + } } } -void canDashboardVAG(void) { - { - //VAG Dashboard - CanTxMessage msg(CAN_VAG_RPM); - msg.setShortValue(GET_RPM() * 4, 2); //RPM - } +void canDashboardVAG(CanCycle cycle) { + if (cycle.isInterval(CI::_10ms)) { + { + //VAG Dashboard + CanTxMessage msg(CAN_VAG_RPM); + msg.setShortValue(GET_RPM() * 4, 2); //RPM + } - float clt = Sensor::get(SensorType::Clt).value_or(0); + float clt = Sensor::get(SensorType::Clt).value_or(0); - { - CanTxMessage msg(CAN_VAG_CLT); - msg.setShortValue((int) ((clt + 48.373) / 0.75), 1); //Coolant Temp - } + { + CanTxMessage msg(CAN_VAG_CLT); + msg.setShortValue((int) ((clt + 48.373) / 0.75), 1); //Coolant Temp + } - { - CanTxMessage msg(CAN_VAG_CLT_V2); - msg.setShortValue((int) ((clt + 48.373) / 0.75), 4); //Coolant Temp - } + { + CanTxMessage msg(CAN_VAG_CLT_V2); + msg.setShortValue((int) ((clt + 48.373) / 0.75), 4); //Coolant Temp + } - { - CanTxMessage msg(CAN_VAG_IMMO); - msg.setShortValue(0x80, 1); + { + CanTxMessage msg(CAN_VAG_IMMO); + msg.setShortValue(0x80, 1); + } } } -void canDashboardW202(void) { - { - CanTxMessage msg(W202_STAT_1); - uint16_t tmp = GET_RPM(); - msg[0] = 0x08; // Unknown - msg[1] = (tmp >> 8); //RPM - msg[2] = (tmp & 0xff); //RPM - msg[3] = 0x00; // 0x01 - tank blink, 0x02 - EPC - msg[4] = 0x00; // Unknown - msg[5] = 0x00; // Unknown - msg[6] = 0x00; // Unknown - oil info - msg[7] = 0x00; // Unknown - oil info +void canDashboardW202(CanCycle cycle) { + if (cycle.isInterval(CI::_20ms)) { + { + CanTxMessage msg(W202_STAT_1); + uint16_t tmp = GET_RPM(); + msg[0] = 0x08; // Unknown + msg[1] = (tmp >> 8); //RPM + msg[2] = (tmp & 0xff); //RPM + msg[3] = 0x00; // 0x01 - tank blink, 0x02 - EPC + msg[4] = 0x00; // Unknown + msg[5] = 0x00; // Unknown + msg[6] = 0x00; // Unknown - oil info + msg[7] = 0x00; // Unknown - oil info + } } - { - CanTxMessage msg(W202_STAT_2); //dlc 7 - msg[0] = (int)(Sensor::get(SensorType::Clt).value_or(0) + 40); // CLT -40 offset - msg[1] = 0x3D; // TBD - msg[2] = 0x63; // Const - msg[3] = 0x41; // Const - msg[4] = 0x00; // Unknown - msg[5] = 0x05; // Const - msg[6] = 0x50; // TBD - msg[7] = 0x00; // Unknown + if (cycle.isInterval(CI::_100ms)) { + { + CanTxMessage msg(W202_STAT_2); //dlc 7 + msg[0] = (int)(Sensor::get(SensorType::Clt).value_or(0) + 40); // CLT -40 offset + msg[1] = 0x3D; // TBD + msg[2] = 0x63; // Const + msg[3] = 0x41; // Const + msg[4] = 0x00; // Unknown + msg[5] = 0x05; // Const + msg[6] = 0x50; // TBD + msg[7] = 0x00; // Unknown + } } - { - CanTxMessage msg(W202_ALIVE); - msg[0] = 0x0A; // Const - msg[1] = 0x18; // Const - msg[2] = 0x00; // Const - msg[3] = 0x00; // Const - msg[4] = 0xC0; // Const - msg[5] = 0x00; // Const - msg[6] = 0x00; // Const - msg[7] = 0x00; // Const - } + if (cycle.isInterval(CI::_200ms)) { + { + CanTxMessage msg(W202_ALIVE); + msg[0] = 0x0A; // Const + msg[1] = 0x18; // Const + msg[2] = 0x00; // Const + msg[3] = 0x00; // Const + msg[4] = 0xC0; // Const + msg[5] = 0x00; // Const + msg[6] = 0x00; // Const + msg[7] = 0x00; // Const + } - { - CanTxMessage msg(W202_STAT_3); - msg[0] = 0x00; // Const - msg[1] = 0x00; // Const - msg[2] = 0x6D; // TBD - msg[3] = 0x7B; // Const - msg[4] = 0x21; // TBD - msg[5] = 0x07; // Const - msg[6] = 0x33; // Const - msg[7] = 0x05; // Const + { + CanTxMessage msg(W202_STAT_3); + msg[0] = 0x00; // Const + msg[1] = 0x00; // Const + msg[2] = 0x6D; // TBD + msg[3] = 0x7B; // Const + msg[4] = 0x21; // TBD + msg[5] = 0x07; // Const + msg[6] = 0x33; // Const + msg[7] = 0x05; // Const + } } } /** * https://docs.google.com/spreadsheets/d/1XMfeGlhgl0lBL54lNtPdmmFd8gLr2T_YTriokb30kJg */ -void canDashboardVagMqb() { +void canDashboardVagMqb(CanCycle cycle) { + if (cycle.isInterval(CI::_50ms)) { - { // 'turn-on' - CanTxMessage msg(0x3C0, 4); - // ignition ON - msg[2] = 3; - } - { //RPM - CanTxMessage msg(0x107, 8); - msg[3] = ((int)(GET_RPM() / 3.5)) & 0xFF; - msg[4] = ((int)(GET_RPM() / 3.5)) >> 8; + { // 'turn-on' + CanTxMessage msg(0x3C0, 4); + // ignition ON + msg[2] = 3; + } + + { //RPM + CanTxMessage msg(0x107, 8); + msg[3] = ((int)(GET_RPM() / 3.5)) & 0xFF; + msg[4] = ((int)(GET_RPM() / 3.5)) >> 8; + } } } -void canDashboardBMWE90() +void canDashboardBMWE90(CanCycle cycle) { - if (e90msgcounter == UINT16_MAX) - e90msgcounter = 0; - e90msgcounter++; - { //T15 'turn-on' - CanTxMessage msg(E90_T15, 5); - msg[0] = 0x45; - msg[1] = 0x41; - msg[2] = 0x61; - msg[3] = 0x8F; - msg[4] = 0xFC; - } + if (cycle.isInterval(CI::_50ms)) { + + { //T15 'turn-on' + CanTxMessage msg(E90_T15, 5); + msg[0] = 0x45; + msg[1] = 0x41; + msg[2] = 0x61; + msg[3] = 0x8F; + msg[4] = 0xFC; + } - { //Ebrake light - CanTxMessage msg(E90_EBRAKE, 2); - msg[0] = 0xFD; - msg[1] = 0xFF; - } + { //Ebrake light + CanTxMessage msg(E90_EBRAKE, 2); + msg[0] = 0xFD; + msg[1] = 0xFF; + } - { //RPM - rpmcounter++; - if (rpmcounter > 0xFE) - rpmcounter = 0xF0; - CanTxMessage msg(E90_RPM, 3); - msg[0] = rpmcounter; - msg[1] = (GET_RPM() * 4) & 0xFF; - msg[2] = (GET_RPM() * 4) >> 8; - } + { //RPM + rpmcounter++; + if (rpmcounter > 0xFE) + rpmcounter = 0xF0; + CanTxMessage msg(E90_RPM, 3); + msg[0] = rpmcounter; + msg[1] = (GET_RPM() * 4) & 0xFF; + msg[2] = (GET_RPM() * 4) >> 8; + } - { //oil & coolant temp (all in C, despite gauge being F) + { //oil & coolant temp (all in C, despite gauge being F) tmp_cnt++; if (tmp_cnt >= 0x0F) tmp_cnt = 0x00; @@ -297,10 +356,12 @@ void canDashboardBMWE90() msg[5] = 0xD3; msg[6] = 0x0D; msg[7] = 0xA8; + } } - { //Seatbelt counter - if (e90msgcounter % 2) { + if (cycle.isInterval(CI::_100ms)) { + { + //Seatbelt counter seatbeltcnt++; if (seatbeltcnt > 0xFE) seatbeltcnt = 0x00; @@ -308,10 +369,9 @@ void canDashboardBMWE90() msg[0] = seatbeltcnt; msg[1] = 0xFF; } - } - - { //Brake counter - if (e90msgcounter % 2) { + + { + //Brake counter 100ms brakecnt_1 += 16; brakecnt_2 += 16; if (brakecnt_1 > 0xEF) @@ -328,10 +388,8 @@ void canDashboardBMWE90() msg[6] = 0x00; msg[7] = brakecnt_2; } - } - { //ABS counter - if (e90msgcounter % 2) { + { //ABS counter abscounter++; if (abscounter > 0xFE) abscounter = 0xF0; @@ -339,10 +397,8 @@ void canDashboardBMWE90() msg[0] = abscounter; msg[1] = 0xFF; } - } - { //Fuel gauge - if (e90msgcounter % 2) { + { //Fuel gauge CanTxMessage msg(E90_FUEL, 5); //fuel gauge msg[0] = 0x76; msg[1] = 0x0F; @@ -350,10 +406,8 @@ void canDashboardBMWE90() msg[3] = 0x1A; msg[4] = 0x00; } - } - { //Gear indicator/counter - if (e90msgcounter % 2) { + { //Gear indicator/counter gear_cnt++; if (gear_cnt >= 0x0F) gear_cnt = 0x00; @@ -365,10 +419,8 @@ void canDashboardBMWE90() msg[4] = 0xF1; msg[5] = 0xFF; } - } - { //E90_SPEED - if (e90msgcounter % 2) { + { //E90_SPEED float mph = getVehicleSpeed() * 0.6213712; mph_ctr = ((TIME_I2MS(chVTGetSystemTime()) - mph_timer) / 50); mph_a = (mph_ctr * mph / 2); diff --git a/firmware/controllers/can/can_dash.h b/firmware/controllers/can/can_dash.h index edb1f581f4..3719cde50b 100644 --- a/firmware/controllers/can/can_dash.h +++ b/firmware/controllers/can/can_dash.h @@ -6,11 +6,6 @@ */ #pragma once +#include "can.h" -void canDashboardBMW(); -void canDashboardFiat(); -void canDashboardVAG(); -void canMazdaRX8(); -void canDashboardW202(); -void canDashboardBMWE90(); -void canDashboardVagMqb(); +void updateDash(CanCycle cycle); \ No newline at end of file diff --git a/firmware/controllers/can/can_tx.cpp b/firmware/controllers/can/can_tx.cpp index 9a6b88707a..a598dc047f 100644 --- a/firmware/controllers/can/can_tx.cpp +++ b/firmware/controllers/can/can_tx.cpp @@ -23,16 +23,24 @@ EXTERN_ENGINE; extern CanListener* canListeners_head; CanWrite::CanWrite() - : PeriodicController("CAN TX", PRIO_CAN_TX, 50) + : PeriodicController("CAN TX", PRIO_CAN_TX, CAN_CYCLE_FREQ) { } void CanWrite::PeriodicTask(efitime_t nowNt) { UNUSED(nowNt); + static uint16_t cycleCount = 0; + CanCycle cycle(cycleCount); + //in case we have Verbose Can enabled, we should keep user configured period if (CONFIG(enableVerboseCanTx)) { - void sendCanVerbose(); - sendCanVerbose(); + uint16_t cycleCountsPeriodMs = cycleCount * CAN_CYCLE_PERIOD; + if (0 != CONFIG(canSleepPeriodMs)) { + if (cycleCountsPeriodMs % CONFIG(canSleepPeriodMs)) { + void sendCanVerbose(); + sendCanVerbose(); + } + } } CanListener* current = canListeners_head; @@ -41,32 +49,52 @@ void CanWrite::PeriodicTask(efitime_t nowNt) { current = current->request(); } - // Transmit dash data, if enabled - switch (CONFIG(canNbcType)) { - case CAN_BUS_NBC_BMW: - canDashboardBMW(); - break; - case CAN_BUS_NBC_FIAT: - canDashboardFiat(); - break; - case CAN_BUS_NBC_VAG: - canDashboardVAG(); - break; - case CAN_BUS_MAZDA_RX8: - canMazdaRX8(); - break; - case CAN_BUS_W202_C180: - canDashboardW202(); - break; - case CAN_BUS_BMW_E90: - canDashboardBMWE90(); - break; - case CAN_BUS_MQB: - canDashboardVagMqb(); - break; - default: - break; + if (cycle.isInterval(CI::_MAX_Cycle)) { + //we now reset cycleCount since we reached max cycle count + cycleCount = 0; } + + updateDash(cycle); + + cycleCount++; +} + +CanInterval CanCycle::computeFlags(uint32_t cycleCount) { + CanInterval cycleMask = CanInterval::_5ms; + + if ((cycleCount % 2) == 0) { + cycleMask |= CI::_10ms; + } + + if ((cycleCount % 4) == 0) { + cycleMask |= CI::_20ms; + } + + if ((cycleCount % 10) == 0) { + cycleMask |= CI::_50ms; + } + + if ((cycleCount % 20) == 0) { + cycleMask |= CI::_100ms; + } + + if ((cycleCount % 40) == 0) { + cycleMask |= CI::_200ms; + } + + if ((cycleCount % 50) == 0) { + cycleMask |= CI::_250ms; + } + + if ((cycleCount % 100) == 0) { + cycleMask |= CI::_500ms; + } + + if ((cycleCount % 200) == 0) { + cycleMask |= CI::_1000ms; + } + + return cycleMask; } #endif // EFI_CAN_SUPPORT diff --git a/firmware/hw_layer/drivers/can/can_hw.cpp b/firmware/hw_layer/drivers/can/can_hw.cpp index 2420ee65b4..c45d3a9d15 100644 --- a/firmware/hw_layer/drivers/can/can_hw.cpp +++ b/firmware/hw_layer/drivers/can/can_hw.cpp @@ -278,7 +278,6 @@ void initCan(void) { // fire up threads, as necessary if (CONFIG(canWriteEnabled)) { - canWrite.setPeriod(CONFIG(canSleepPeriodMs)); canWrite.Start(); }