From 408d4c6ebfbaead7d8c852bf917cc19416d0d991 Mon Sep 17 00:00:00 2001 From: Matthew Kennedy Date: Fri, 10 Dec 2021 14:08:10 -0800 Subject: [PATCH] multi CAN plumbing (#3688) * framing for multi CAN * typoooooooo --- firmware/hw_layer/drivers/can/can_hw.cpp | 31 ++++++++++++++----- firmware/hw_layer/drivers/can/can_hw.h | 2 ++ firmware/hw_layer/ports/cypress/mpu_util.cpp | 2 +- firmware/hw_layer/ports/kinetis/mpu_util.cpp | 2 +- firmware/hw_layer/ports/mpu_util.h | 2 +- .../hw_layer/ports/stm32/stm32_common.cpp | 2 +- 6 files changed, 30 insertions(+), 11 deletions(-) diff --git a/firmware/hw_layer/drivers/can/can_hw.cpp b/firmware/hw_layer/drivers/can/can_hw.cpp index 918d0816bc..ea0e7543e0 100644 --- a/firmware/hw_layer/drivers/can/can_hw.cpp +++ b/firmware/hw_layer/drivers/can/can_hw.cpp @@ -127,13 +127,14 @@ static const CANConfig *canConfig = &canConfig500; class CanRead final : public ThreadController { public: - CanRead() + CanRead(size_t index) : ThreadController("CAN RX", PRIO_CAN_RX) + , m_index(index) { } void ThreadTask() override { - CANDriver* device = detectCanDevice(engineConfiguration->canRxPin, engineConfiguration->canTxPin); + CANDriver* device = detectCanDevice(m_index); if (!device) { warning(CUSTOM_ERR_CAN_CONFIGURATION, "CAN configuration issue"); @@ -156,10 +157,12 @@ public: } private: + const size_t m_index; CANRxFrame m_buffer; }; -static CanRead canRead CCM_OPTIONAL; +CCM_OPTIONAL static CanRead canRead(0); +//CCM_OPTIONAL static CanRead canRead2(1); static CanWrite canWrite CCM_OPTIONAL; static void canInfo() { @@ -272,11 +275,13 @@ void initCan(void) { canStart(&CAND1, canConfig); #endif /* STM32_CAN_USE_CAN2 */ + if (detectCanDevice(0) == detectCanDevice(1)) { + firmwareError(OBD_PCM_Processor_Fault, "CAN pins must be set to different devices"); + return; + } + // Plumb CAN device to tx system - CanTxMessage::setDevice(detectCanDevice( - engineConfiguration->canRxPin, - engineConfiguration->canTxPin - )); + CanTxMessage::setDevice(detectCanDevice(0)); // fire up threads, as necessary if (engineConfiguration->canWriteEnabled) { @@ -285,6 +290,7 @@ void initCan(void) { if (engineConfiguration->canReadEnabled) { canRead.Start(); + //canRead2.Start(); } isCanEnabled = true; @@ -294,4 +300,15 @@ bool getIsCanEnabled(void) { return isCanEnabled; } +CANDriver* detectCanDevice(size_t logicalIndex) { + switch (logicalIndex) { + case 0: + return detectCanDeviceImpl(engineConfiguration->canRxPin, engineConfiguration->canTxPin); + case 1: + return detectCanDeviceImpl(engineConfiguration->can2RxPin, engineConfiguration->can2TxPin); + } + + return nullptr; +} + #endif /* EFI_CAN_SUPPORT */ diff --git a/firmware/hw_layer/drivers/can/can_hw.h b/firmware/hw_layer/drivers/can/can_hw.h index 7a86db5258..165f1be346 100644 --- a/firmware/hw_layer/drivers/can/can_hw.h +++ b/firmware/hw_layer/drivers/can/can_hw.h @@ -14,6 +14,8 @@ void setCanType(int type); void setCanVss(int type); #if EFI_CAN_SUPPORT +CANDriver* detectCanDevice(size_t logicalIndex); + void stopCanPins(); void startCanPins(); void enableFrankensoCan(); diff --git a/firmware/hw_layer/ports/cypress/mpu_util.cpp b/firmware/hw_layer/ports/cypress/mpu_util.cpp index 9af4da4b60..dc368899a7 100644 --- a/firmware/hw_layer/ports/cypress/mpu_util.cpp +++ b/firmware/hw_layer/ports/cypress/mpu_util.cpp @@ -224,7 +224,7 @@ bool isValidCanRxPin(brain_pin_e pin) { return isValidCan1RxPin(pin) || isValidCan2RxPin(pin); } -CANDriver * detectCanDevice(brain_pin_e pinRx, brain_pin_e pinTx) { +CANDriver* detectCanDeviceImpl(brain_pin_e pinRx, brain_pin_e pinTx) { if (isValidCan1RxPin(pinRx) && isValidCan1TxPin(pinTx)) return &CAND1; if (isValidCan2RxPin(pinRx) && isValidCan2TxPin(pinTx)) diff --git a/firmware/hw_layer/ports/kinetis/mpu_util.cpp b/firmware/hw_layer/ports/kinetis/mpu_util.cpp index 43e7b3393f..222902d4fb 100644 --- a/firmware/hw_layer/ports/kinetis/mpu_util.cpp +++ b/firmware/hw_layer/ports/kinetis/mpu_util.cpp @@ -216,7 +216,7 @@ bool isValidCanRxPin(brain_pin_e pin) { return isValidCan1RxPin(pin) || isValidCan2RxPin(pin); } -CANDriver * detectCanDevice(brain_pin_e pinRx, brain_pin_e pinTx) { +CANDriver* detectCanDeviceImpl(brain_pin_e pinRx, brain_pin_e pinTx) { if (isValidCan1RxPin(pinRx) && isValidCan1TxPin(pinTx)) return &CAND1; if (isValidCan2RxPin(pinRx) && isValidCan2TxPin(pinTx)) diff --git a/firmware/hw_layer/ports/mpu_util.h b/firmware/hw_layer/ports/mpu_util.h index 01d27bc3b3..f7b78b8263 100644 --- a/firmware/hw_layer/ports/mpu_util.h +++ b/firmware/hw_layer/ports/mpu_util.h @@ -23,7 +23,7 @@ bool readSlowAnalogInputs(adcsample_t* convertedSamples); #if HAL_USE_CAN bool isValidCanTxPin(brain_pin_e pin); bool isValidCanRxPin(brain_pin_e pin); -CANDriver * detectCanDevice(brain_pin_e pinRx, brain_pin_e pinTx); +CANDriver* detectCanDeviceImpl(brain_pin_e pinRx, brain_pin_e pinTx); #endif // HAL_USE_CAN bool isValidSerialTxPin(brain_pin_e pin); diff --git a/firmware/hw_layer/ports/stm32/stm32_common.cpp b/firmware/hw_layer/ports/stm32/stm32_common.cpp index c720ac7ff1..3439e45b19 100644 --- a/firmware/hw_layer/ports/stm32/stm32_common.cpp +++ b/firmware/hw_layer/ports/stm32/stm32_common.cpp @@ -778,7 +778,7 @@ bool isValidCanRxPin(brain_pin_e pin) { return isValidCan1RxPin(pin) || isValidCan2RxPin(pin); } -CANDriver * detectCanDevice(brain_pin_e pinRx, brain_pin_e pinTx) { +CANDriver* detectCanDeviceImpl(brain_pin_e pinRx, brain_pin_e pinTx) { #if STM32_CAN_USE_CAN1 || STM32_CAN_USE_FDCAN1 if (isValidCan1RxPin(pinRx) && isValidCan1TxPin(pinTx)) return &CAND1;