2022-09-29 20:01:06 -07:00
|
|
|
|
|
|
|
#include "pch.h"
|
2023-10-08 12:14:42 -07:00
|
|
|
#include "bench_test.h"
|
2023-08-18 15:41:17 -07:00
|
|
|
#include "board_id.h"
|
2022-09-29 20:01:06 -07:00
|
|
|
#include "can_bench_test.h"
|
|
|
|
#include "can_msg_tx.h"
|
2023-08-13 07:30:46 -07:00
|
|
|
#include "can_common.h"
|
2023-08-19 12:04:46 -07:00
|
|
|
#include "frequency_sensor.h"
|
2023-08-24 06:48:30 -07:00
|
|
|
#include "settings.h"
|
2022-09-29 20:01:06 -07:00
|
|
|
|
2023-08-13 07:30:46 -07:00
|
|
|
#define TRUNCATE_TO_BYTE(i) ((i) & 0xff)
|
2023-08-14 13:28:14 -07:00
|
|
|
// raw values are 0..5V, convert it to 8-bit (0..255)
|
|
|
|
#define RAW_TO_BYTE(v) TRUNCATE_TO_BYTE((int)(v * 255.0 / 5.0))
|
2023-08-13 07:30:46 -07:00
|
|
|
|
2023-10-14 15:45:42 -07:00
|
|
|
/**
|
|
|
|
* QC direct output control API is used by https://github.com/rusefi/stim test device
|
|
|
|
* quite different from bench testing user functionality: QC direct should never be engaged on a real vehicle
|
|
|
|
* Once QC direct control mode is activated the only way out is to reboot the unit!
|
|
|
|
*/
|
2023-08-20 21:40:36 -07:00
|
|
|
bool qcDirectPinControlMode = false;
|
|
|
|
|
2022-09-29 20:01:06 -07:00
|
|
|
#if EFI_CAN_SUPPORT
|
|
|
|
|
2022-09-29 20:45:46 -07:00
|
|
|
static void setPin(const CANRxFrame& frame, int value) {
|
2023-08-29 17:52:47 -07:00
|
|
|
int outputIndex = frame.data8[2];
|
|
|
|
if (outputIndex >= getBoardMetaOutputsCount())
|
2022-09-29 20:45:46 -07:00
|
|
|
return;
|
2023-08-29 17:52:47 -07:00
|
|
|
Gpio pin = getBoardMetaOutputs()[outputIndex];
|
2022-09-29 21:08:26 -07:00
|
|
|
#if EFI_GPIO_HARDWARE && EFI_PROD_CODE
|
2023-08-29 16:45:49 -07:00
|
|
|
|
2023-08-29 17:52:47 -07:00
|
|
|
int hwIndex = brainPin_to_index(pin);
|
|
|
|
if (engine->pinRepository.getBrainUsedPin(hwIndex) == nullptr) {
|
|
|
|
// if pin is assigned we better configure it
|
2023-08-30 21:01:13 -07:00
|
|
|
efiSetPadModeWithoutOwnershipAcquisition("QC_SET", pin, PAL_MODE_OUTPUT_PUSHPULL);
|
2023-08-29 16:45:49 -07:00
|
|
|
}
|
|
|
|
|
2022-09-29 20:45:46 -07:00
|
|
|
palWritePad(getHwPort("can_write", pin), getHwPin("can_write", pin), value);
|
2023-08-20 21:40:36 -07:00
|
|
|
// todo: add smart chip support support
|
2022-09-29 21:08:26 -07:00
|
|
|
#endif // EFI_GPIO_HARDWARE && EFI_PROD_CODE
|
2022-09-29 20:45:46 -07:00
|
|
|
}
|
|
|
|
|
2023-08-13 07:30:46 -07:00
|
|
|
void sendEventCounters() {
|
|
|
|
#if EFI_SHAFT_POSITION_INPUT
|
2023-09-27 13:57:39 -07:00
|
|
|
CanTxMessage msg(CanCategory::BENCH_TEST, (int)bench_test_packet_ids_e::EVENT_COUNTERS, 8, /*bus*/0, /*isExtended*/true);
|
2023-08-13 07:30:46 -07:00
|
|
|
|
|
|
|
int primaryFall = engine->triggerCentral.getHwEventCounter((int)SHAFT_PRIMARY_FALLING);
|
|
|
|
int primaryRise = engine->triggerCentral.getHwEventCounter((int)SHAFT_PRIMARY_RISING);
|
|
|
|
int secondaryFall = engine->triggerCentral.getHwEventCounter((int)SHAFT_SECONDARY_FALLING);
|
|
|
|
int secondaryRise = engine->triggerCentral.getHwEventCounter((int)SHAFT_SECONDARY_RISING);
|
|
|
|
|
|
|
|
msg[0] = TRUNCATE_TO_BYTE(primaryRise + primaryFall);
|
|
|
|
msg[1] = TRUNCATE_TO_BYTE(secondaryRise + secondaryFall);
|
|
|
|
|
|
|
|
for (int camIdx = 0; camIdx < 4; camIdx++) {
|
|
|
|
int vvtRise = 0, vvtFall = 0;
|
|
|
|
if (camIdx < CAM_INPUTS_COUNT) {
|
|
|
|
vvtRise = engine->triggerCentral.vvtEventRiseCounter[camIdx];
|
|
|
|
vvtFall = engine->triggerCentral.vvtEventFallCounter[camIdx];
|
|
|
|
}
|
|
|
|
|
|
|
|
msg[2 + camIdx] = TRUNCATE_TO_BYTE(vvtRise + vvtFall);
|
|
|
|
}
|
2023-08-19 12:04:46 -07:00
|
|
|
|
|
|
|
extern FrequencySensor vehicleSpeedSensor;
|
|
|
|
msg[6] = TRUNCATE_TO_BYTE(vehicleSpeedSensor.eventCounter);
|
2023-08-13 07:30:46 -07:00
|
|
|
#endif // EFI_SHAFT_POSITION_INPUT
|
|
|
|
}
|
|
|
|
|
2023-08-22 11:49:28 -07:00
|
|
|
void sendButtonCounters() {
|
2023-09-27 13:57:39 -07:00
|
|
|
CanTxMessage msg(CanCategory::BENCH_TEST, (int)bench_test_packet_ids_e::BUTTON_COUNTERS, 8, /*bus*/0, /*isExtended*/true);
|
2023-08-22 11:57:14 -07:00
|
|
|
msg[0] = TRUNCATE_TO_BYTE(engine->brakePedalSwitchedState.getCounter());
|
|
|
|
msg[1] = TRUNCATE_TO_BYTE(engine->clutchUpSwitchedState.getCounter());
|
|
|
|
msg[2] = TRUNCATE_TO_BYTE(engine->acButtonSwitchedState.getCounter());
|
|
|
|
// todo: start button
|
2023-08-22 11:49:28 -07:00
|
|
|
}
|
|
|
|
|
2023-08-14 13:28:14 -07:00
|
|
|
void sendRawAnalogValues() {
|
2023-09-01 14:34:01 -07:00
|
|
|
const float values_1[] = {
|
2023-08-21 16:59:51 -07:00
|
|
|
Sensor::getRaw(SensorType::Tps1Primary),
|
|
|
|
Sensor::getRaw(SensorType::Tps1Secondary),
|
|
|
|
Sensor::getRaw(SensorType::AcceleratorPedalPrimary),
|
|
|
|
Sensor::getRaw(SensorType::AcceleratorPedalSecondary),
|
|
|
|
Sensor::getRaw(SensorType::MapSlow),
|
|
|
|
Sensor::getRaw(SensorType::Clt),
|
2023-08-22 05:51:12 -07:00
|
|
|
Sensor::getRaw(SensorType::Iat),
|
2023-08-21 16:59:51 -07:00
|
|
|
Sensor::getRaw(SensorType::BatteryVoltage),
|
2023-08-14 13:28:14 -07:00
|
|
|
};
|
|
|
|
|
2023-09-01 14:34:01 -07:00
|
|
|
const float values_2[] = {
|
|
|
|
Sensor::getRaw(SensorType::Tps2Primary),
|
|
|
|
Sensor::getRaw(SensorType::Tps2Secondary),
|
|
|
|
Sensor::getRaw(SensorType::AuxLinear1),
|
|
|
|
Sensor::getRaw(SensorType::AuxLinear2),
|
2023-09-27 17:43:07 -07:00
|
|
|
Sensor::getRaw(SensorType::OilPressure),
|
|
|
|
Sensor::getRaw(SensorType::FuelPressureLow),
|
|
|
|
Sensor::getRaw(SensorType::FuelPressureHigh),
|
|
|
|
Sensor::getRaw(SensorType::AuxTemp1),
|
2023-09-01 14:34:01 -07:00
|
|
|
};
|
|
|
|
static_assert(efi::size(values_1) <= 8);
|
|
|
|
static_assert(efi::size(values_2) <= 8);
|
|
|
|
|
|
|
|
|
2023-08-14 13:28:14 -07:00
|
|
|
// send the first packet
|
|
|
|
{
|
2023-09-27 13:57:39 -07:00
|
|
|
CanTxMessage msg(CanCategory::BENCH_TEST, (int)bench_test_packet_ids_e::RAW_ANALOG_1, 8, /*bus*/0, /*isExtended*/true);
|
2023-09-01 14:34:01 -07:00
|
|
|
for (int valueIdx = 0; valueIdx < efi::size(values_1); valueIdx++) {
|
|
|
|
msg[valueIdx] = RAW_TO_BYTE(values_1[valueIdx]);
|
2023-08-14 13:28:14 -07:00
|
|
|
}
|
|
|
|
}
|
2023-09-01 14:34:01 -07:00
|
|
|
{
|
2023-09-27 13:57:39 -07:00
|
|
|
CanTxMessage msg(CanCategory::BENCH_TEST, (int)bench_test_packet_ids_e::RAW_ANALOG_2, 8, /*bus*/0, /*isExtended*/true);
|
2023-09-01 14:34:01 -07:00
|
|
|
for (int valueIdx = 0; valueIdx < efi::size(values_2); valueIdx++) {
|
|
|
|
msg[valueIdx] = RAW_TO_BYTE(values_2[valueIdx]);
|
|
|
|
}
|
|
|
|
}
|
2023-08-14 13:28:14 -07:00
|
|
|
}
|
|
|
|
|
2023-08-26 19:15:47 -07:00
|
|
|
static void sendOutBoardMeta() {
|
2023-08-26 19:12:52 -07:00
|
|
|
#if EFI_PROD_CODE
|
2023-09-27 13:57:39 -07:00
|
|
|
CanTxMessage msg(CanCategory::BENCH_TEST, (int)bench_test_packet_ids_e::IO_META_INFO, 8, /*bus*/0, /*isExtended*/true);
|
2023-10-14 15:45:42 -07:00
|
|
|
msg[0] = (int)bench_test_magic_numbers_e::BENCH_HEADER;
|
2023-08-26 19:12:52 -07:00
|
|
|
msg[1] = 0;
|
|
|
|
msg[2] = getBoardMetaOutputsCount();
|
2023-08-29 18:23:30 -07:00
|
|
|
msg[3] = getBoardMetaLowSideOutputsCount();
|
2023-08-26 19:12:52 -07:00
|
|
|
#endif // EFI_PROD_CODE
|
|
|
|
}
|
|
|
|
|
2023-08-18 15:41:17 -07:00
|
|
|
void sendBoardStatus() {
|
|
|
|
#if EFI_PROD_CODE
|
2023-09-27 13:57:39 -07:00
|
|
|
CanTxMessage msg(CanCategory::BENCH_TEST, (int)bench_test_packet_ids_e::BOARD_STATUS, 8, /*bus*/0, /*isExtended*/true);
|
2023-08-18 15:41:17 -07:00
|
|
|
|
|
|
|
int boardId = getBoardId();
|
|
|
|
msg[0] = TRUNCATE_TO_BYTE(boardId >> 8);
|
|
|
|
msg[1] = TRUNCATE_TO_BYTE(boardId);
|
|
|
|
|
|
|
|
int numSecondsSinceReset = getTimeNowS();
|
|
|
|
msg[2] = TRUNCATE_TO_BYTE(numSecondsSinceReset >> 16);
|
|
|
|
msg[3] = TRUNCATE_TO_BYTE(numSecondsSinceReset >> 8);
|
|
|
|
msg[4] = TRUNCATE_TO_BYTE(numSecondsSinceReset);
|
2023-08-21 16:04:29 -07:00
|
|
|
|
2023-08-21 16:28:24 -07:00
|
|
|
int engineType = (int) engineConfiguration->engineType;
|
|
|
|
msg[5] = engineType >> 8;
|
|
|
|
msg[6] = engineType;
|
2023-08-26 19:12:52 -07:00
|
|
|
sendOutBoardMeta();
|
2023-08-18 15:41:17 -07:00
|
|
|
#endif // EFI_PROD_CODE
|
|
|
|
}
|
|
|
|
|
2023-10-10 18:29:27 -07:00
|
|
|
// bench test fuel pump pin #5603
|
2023-10-11 07:34:00 -07:00
|
|
|
static void sendPinStatePackets(bench_mode_e benchModePinIdx) {
|
|
|
|
OutputPin *pin = enginePins.getOutputPinForBenchMode(benchModePinIdx);
|
2023-10-11 07:40:00 -07:00
|
|
|
CanTxMessage msg(CanCategory::BENCH_TEST, (int)bench_test_packet_ids_e::PIN_STATE, 8, /*bus*/0, /*isExtended*/true);
|
2023-10-11 07:34:00 -07:00
|
|
|
if (pin == nullptr)
|
|
|
|
return;
|
|
|
|
#if EFI_UNIT_TEST || EFI_SIMULATOR
|
|
|
|
msg[0] = TRUNCATE_TO_BYTE(pin->pinToggleCounter >> 8);
|
|
|
|
msg[1] = TRUNCATE_TO_BYTE(pin->pinToggleCounter);
|
|
|
|
#endif // EFI_UNIT_TEST || EFI_SIMULATOR
|
|
|
|
|
|
|
|
#if EFI_SIMULATOR
|
|
|
|
for (int i = 0, mIdx = 2; i < 2; i++) {
|
|
|
|
msg[mIdx++] = TRUNCATE_TO_BYTE(pin->durationsInStateMs[i] >> 16);
|
|
|
|
msg[mIdx++] = TRUNCATE_TO_BYTE(pin->durationsInStateMs[i] >> 8);
|
|
|
|
msg[mIdx++] = TRUNCATE_TO_BYTE(pin->durationsInStateMs[i]);
|
|
|
|
}
|
|
|
|
#endif // EFI_SIMULATOR
|
|
|
|
}
|
2023-10-10 18:29:27 -07:00
|
|
|
|
2023-10-16 06:37:59 -07:00
|
|
|
static void resetPinStats(bench_mode_e benchModePinIdx) {
|
|
|
|
OutputPin *pin = enginePins.getOutputPinForBenchMode(benchModePinIdx);
|
|
|
|
|
|
|
|
if (pin == nullptr)
|
|
|
|
return;
|
|
|
|
|
|
|
|
#if EFI_SIMULATOR
|
|
|
|
pin->resetToggleStats();
|
|
|
|
#endif // EFI_SIMULATOR
|
|
|
|
}
|
|
|
|
|
2022-09-29 20:01:06 -07:00
|
|
|
void processCanBenchTest(const CANRxFrame& frame) {
|
2023-09-27 13:57:39 -07:00
|
|
|
if (CAN_EID(frame) != (int)bench_test_packet_ids_e::IO_CONTROL) {
|
2022-09-29 20:01:06 -07:00
|
|
|
return;
|
|
|
|
}
|
2023-10-14 15:45:42 -07:00
|
|
|
if (frame.data8[0] != (int)bench_test_magic_numbers_e::BENCH_HEADER) {
|
2022-09-29 20:01:06 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
uint8_t command = frame.data8[1];
|
2023-10-08 12:14:42 -07:00
|
|
|
if (command == (uint8_t)bench_test_io_control_e::CAN_BENCH_GET_COUNT) {
|
2023-08-26 19:12:52 -07:00
|
|
|
sendOutBoardMeta();
|
2023-10-14 15:45:42 -07:00
|
|
|
} else if (command == (uint8_t)bench_test_io_control_e::CAN_QC_OUTPUT_CONTROL_SET) {
|
2023-10-14 12:14:28 -07:00
|
|
|
qcDirectPinControlMode = true;
|
2022-09-29 20:45:46 -07:00
|
|
|
setPin(frame, 1);
|
2023-10-14 15:45:42 -07:00
|
|
|
} else if (command == (uint8_t)bench_test_io_control_e::CAN_QC_OUTPUT_CONTROL_CLEAR) {
|
2023-10-14 12:14:28 -07:00
|
|
|
qcDirectPinControlMode = true;
|
2022-09-29 20:45:46 -07:00
|
|
|
setPin(frame, 0);
|
2023-10-08 12:14:42 -07:00
|
|
|
} else if (command == (uint8_t)bench_test_io_control_e::CAN_BENCH_SET_ENGINE_TYPE) {
|
2023-08-24 06:48:30 -07:00
|
|
|
int eType = frame.data8[2];
|
2023-08-30 22:10:03 -07:00
|
|
|
// todo: fix firmware for 'false' to be possible - i.e. more of properties should be applied on the fly
|
2023-08-30 21:28:52 -07:00
|
|
|
setEngineType(eType, true);
|
2023-09-01 13:59:29 -07:00
|
|
|
#if EFI_PROD_CODE
|
2023-08-30 22:10:03 -07:00
|
|
|
scheduleReboot();
|
2023-09-01 13:59:29 -07:00
|
|
|
#endif // EFI_PROD_CODE
|
2023-10-16 06:37:59 -07:00
|
|
|
} else if (command == (uint8_t)bench_test_io_control_e::CAN_BENCH_START_PIN_TEST) {
|
|
|
|
bench_mode_e benchModePinIdx = (bench_mode_e)frame.data8[2];
|
|
|
|
// ignore previous pin state and stats
|
|
|
|
resetPinStats(benchModePinIdx);
|
|
|
|
} else if (command == (uint8_t)bench_test_io_control_e::CAN_BENCH_END_PIN_TEST) {
|
2023-10-08 12:14:42 -07:00
|
|
|
} else if (command == (uint8_t)bench_test_io_control_e::CAN_BENCH_EXECUTE_BENCH_TEST) {
|
|
|
|
int benchCommandIdx = frame.data8[2];
|
|
|
|
handleBenchCategory(benchCommandIdx);
|
2023-10-11 07:34:00 -07:00
|
|
|
} else if (command == (uint8_t)bench_test_io_control_e::CAN_BENCH_QUERY_PIN_STATE) {
|
|
|
|
bench_mode_e benchModePinIdx = (bench_mode_e)frame.data8[2];
|
|
|
|
sendPinStatePackets(benchModePinIdx);
|
2022-09-29 20:01:06 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // EFI_CAN_SUPPORT
|