split out can rx logic (#1196)

* move to can folder

* split out can rx

* only print in debug mode

* guard on feature flag

* headers

Co-authored-by: Matthew Kennedy <makenne@microsoft.com>
This commit is contained in:
Matthew Kennedy 2020-03-19 05:43:37 -07:00 committed by GitHub
parent 3ecedc8ccc
commit f2994d6889
6 changed files with 67 additions and 37 deletions

View File

@ -0,0 +1,13 @@
/**
* @file can.h
*
* @date Mar 19, 2020
* @author Matthew Kennedy, (c) 2020
*/
#pragma once
#include "hal.h"
class Logging;
void processCanRxMessage(const CANRxFrame& msg, Logging* logger);

View File

@ -0,0 +1,43 @@
/**
* @file can_rx.cpp
*
* CAN reception handling. This file handles multiplexing incoming CAN frames as appropriate
* to the subsystems that consume them.
*
* @date Mar 19, 2020
* @author Matthew Kennedy, (c) 2020
*/
#include "global.h"
#if EFI_CAN_SUPPORT
#include "can.h"
#include "obd2.h"
#include "engine.h"
EXTERN_ENGINE;
static void printPacket(const CANRxFrame& rx, Logging* logger) {
// only print info if we're in can debug mode
if (CONFIG(debugMode) == DBG_CAN) {
scheduleMsg(logger, "Got CAN message: SID %x/%x %x %x %x %x %x %x %x %x", rx.SID, rx.DLC, rx.data8[0], rx.data8[1],
rx.data8[2], rx.data8[3], rx.data8[4], rx.data8[5], rx.data8[6], rx.data8[7]);
}
}
volatile float aemXSeriesLambda = 0;
void processCanRxMessage(const CANRxFrame& frame, Logging* logger) {
// TODO: if/when we support multiple lambda sensors, sensor N
// has address 0x0180 + N where N = [0, 15]
if (frame.SID == 0x0180) {
// AEM x-series lambda sensor reports in 0.0001 lambda per bit
uint16_t lambdaInt = SWAP_UINT16(frame.data16[0]);
aemXSeriesLambda = 0.0001f * lambdaInt;
} else {
printPacket(frame, logger);
obdOnCanPacketRx(frame);
}
}
#endif // EFI_CAN_SUPPORT

View File

@ -107,8 +107,8 @@ static void obdWriteSupportedPids(int PID, int bitOffset, const int16_t *support
obdSendPacket(1, PID, 4, value);
}
static void handleGetDataRequest(CANRxFrame *rx) {
int pid = rx->data8[2];
static void handleGetDataRequest(const CANRxFrame& rx) {
int pid = rx.data8[2];
switch (pid) {
case PID_SUPPORTED_PIDS_REQUEST_01_20:
scheduleMsg(&logger, "Got lookup request 01-20");
@ -193,17 +193,17 @@ static void handleDtcRequest(int numCodes, int *dtcCode) {
}
#if HAL_USE_CAN
void obdOnCanPacketRx(CANRxFrame *rx) {
if (rx->SID != OBD_TEST_REQUEST) {
void obdOnCanPacketRx(const CANRxFrame& rx) {
if (rx.SID != OBD_TEST_REQUEST) {
return;
}
if (rx->data8[0] == 2 && rx->data8[1] == OBD_CURRENT_DATA) {
if (rx.data8[0] == 2 && rx.data8[1] == OBD_CURRENT_DATA) {
handleGetDataRequest(rx);
} else if (rx->data8[0] == 1 && rx->data8[1] == OBD_STORED_DIAGNOSTIC_TROUBLE_CODES) {
} else if (rx.data8[0] == 1 && rx.data8[1] == OBD_STORED_DIAGNOSTIC_TROUBLE_CODES) {
scheduleMsg(&logger, "Got stored DTC request");
// todo: implement stored/pending difference?
handleDtcRequest(1, &engine->engineState.warnings.lastErrorCode);
} else if (rx->data8[0] == 1 && rx->data8[1] == OBD_PENDING_DIAGNOSTIC_TROUBLE_CODES) {
} else if (rx.data8[0] == 1 && rx.data8[1] == OBD_PENDING_DIAGNOSTIC_TROUBLE_CODES) {
scheduleMsg(&logger, "Got pending DTC request");
// todo: implement stored/pending difference?
handleDtcRequest(1, &engine->engineState.warnings.lastErrorCode);

View File

@ -37,6 +37,6 @@
#define PID_FUEL_RATE 0x5E
#if HAL_USE_CAN
void obdOnCanPacketRx(CANRxFrame *rx);
void obdOnCanPacketRx(const CANRxFrame& rx);
#endif /* HAL_USE_CAN */

View File

@ -35,6 +35,7 @@ CONTROLLERS_SRC_CPP = \
$(CONTROLLERS_DIR)/flash_main.cpp \
$(CONTROLLERS_DIR)/injector_central.cpp \
$(CONTROLLERS_DIR)/can/obd2.cpp \
$(CONTROLLERS_DIR)/can/can_rx.cpp \
$(CONTROLLERS_DIR)/engine_controller.cpp \
$(CONTROLLERS_DIR)/persistent_store.cpp \

View File

@ -13,13 +13,13 @@
#if EFI_CAN_SUPPORT
#include "can.h"
#include "engine_configuration.h"
#include "periodic_thread_controller.h"
#include "pin_repository.h"
#include "can_hw.h"
#include "can_msg_tx.h"
#include "string.h"
#include "obd2.h"
#include "mpu_util.h"
#include "allsensors.h"
#include "vehicle_speed.h"
@ -74,22 +74,6 @@ CAN_BTR_1k0 };
CANTxFrame txmsg;
static void printPacket(CANRxFrame *rx) {
// scheduleMsg(&logger, "CAN FMI %x", rx->FMI);
// scheduleMsg(&logger, "TIME %x", rx->TIME);
scheduleMsg(&logger, "Got CAN message: SID %x/%x %x %x %x %x %x %x %x %x", rx->SID, rx->DLC, rx->data8[0], rx->data8[1],
rx->data8[2], rx->data8[3], rx->data8[4], rx->data8[5], rx->data8[6], rx->data8[7]);
if (rx->SID == CAN_BMW_E46_CLUSTER_STATUS) {
int odometerKm = 10 * (rx->data8[1] << 8) + rx->data8[0];
int odometerMi = (int) (odometerKm * 0.621371);
scheduleMsg(&logger, "GOT odometerKm %d", odometerKm);
scheduleMsg(&logger, "GOT odometerMi %d", odometerMi);
int timeValue = (rx->data8[4] << 8) + rx->data8[3];
scheduleMsg(&logger, "GOT time %d", timeValue);
}
}
static void setShortValue(CANTxFrame *txmsg, int value, int offset) {
txmsg->data8[offset] = value;
txmsg->data8[offset + 1] = value >> 8;
@ -244,8 +228,6 @@ public:
}
};
volatile float aemXSeriesLambda = 0;
class CanRead final : public ThreadController<256> {
public:
CanRead()
@ -272,16 +254,7 @@ public:
// Process the message
canReadCounter++;
// TODO: if/when we support multiple lambda sensors, sensor N
// has address 0x0180 + N where N = [0, 15]
if (m_buffer.SID == 0x0180) {
// AEM x-series lambda sensor reports in 0.0001 lambda per bit
uint16_t lambdaInt = SWAP_UINT16(m_buffer.data16[0]);
aemXSeriesLambda = 0.0001f * lambdaInt;
} else {
printPacket(&m_buffer);
obdOnCanPacketRx(&m_buffer);
}
processCanRxMessage(m_buffer, &logger);
}
}