rusefi-1/firmware/hw_layer/drivers/can/can_hw.cpp

322 lines
8.5 KiB
C++
Raw Normal View History

2015-07-10 06:01:56 -07:00
/**
* @file can_hw.cpp
* @brief CAN bus low level code
*
* todo: this file should be split into two - one for CAN transport level ONLY and
* another one with actual messages
*
2020-08-29 11:36:23 -07:00
* @see can_verbose.cpp for higher level logic
* @see obd2.cpp for OBD-II messages via CAN
*
2015-07-10 06:01:56 -07:00
* @date Dec 11, 2013
2020-01-13 18:57:43 -08:00
* @author Andrey Belomutskiy, (c) 2012-2020
2015-07-10 06:01:56 -07:00
*/
#include "pch.h"
2015-07-10 06:01:56 -07:00
2019-04-12 17:52:51 -07:00
#if EFI_CAN_SUPPORT
2018-01-29 16:41:39 -08:00
#include "can.h"
2019-01-27 21:52:21 -08:00
#include "can_hw.h"
#include "can_msg_tx.h"
2019-01-27 21:52:21 -08:00
#include "string.h"
2017-04-12 06:17:06 -07:00
#include "mpu_util.h"
2015-07-10 06:01:56 -07:00
2016-12-19 14:01:43 -08:00
static bool isCanEnabled = false;
2015-07-10 06:01:56 -07:00
2019-07-06 01:54:02 -07:00
// Values below calculated with http://www.bittiming.can-wiki.info/
// Pick ST micro bxCAN
// Clock rate of 42mhz for f4, 54mhz for f7, 80mhz for h7
2019-07-06 01:54:02 -07:00
#ifdef STM32F4XX
// These have an 85.7% sample point
#define CAN_BTR_100 (CAN_BTR_SJW(0) | CAN_BTR_BRP(29) | CAN_BTR_TS1(10) | CAN_BTR_TS2(1))
2019-07-06 01:54:02 -07:00
#define CAN_BTR_250 (CAN_BTR_SJW(0) | CAN_BTR_BRP(11) | CAN_BTR_TS1(10) | CAN_BTR_TS2(1))
#define CAN_BTR_500 (CAN_BTR_SJW(0) | CAN_BTR_BRP(5) | CAN_BTR_TS1(10) | CAN_BTR_TS2(1))
#define CAN_BTR_1k0 (CAN_BTR_SJW(0) | CAN_BTR_BRP(2) | CAN_BTR_TS1(10) | CAN_BTR_TS2(1))
#elif defined(STM32F7XX)
// These have an 88.9% sample point
#define CAN_BTR_100 (CAN_BTR_SJW(0) | CAN_BTR_BRP(30) | CAN_BTR_TS1(15) | CAN_BTR_TS2(2))
2019-07-06 01:54:02 -07:00
#define CAN_BTR_250 (CAN_BTR_SJW(0) | CAN_BTR_BRP(11) | CAN_BTR_TS1(14) | CAN_BTR_TS2(1))
#define CAN_BTR_500 (CAN_BTR_SJW(0) | CAN_BTR_BRP(5) | CAN_BTR_TS1(14) | CAN_BTR_TS2(1))
#define CAN_BTR_1k0 (CAN_BTR_SJW(0) | CAN_BTR_BRP(2) | CAN_BTR_TS1(14) | CAN_BTR_TS2(1))
#elif defined(STM32H7XX)
// These have an 87.5% sample point
// FDCAN driver has different bit timing registers (yes, different format)
// for the arbitration and data phases
#define CAN_NBTP_100 0x00310C01
#define CAN_DBTP_100 0x00310C13
#define CAN_NBTP_250 0x00130C01
#define CAN_DBTP_250 0x00130C13
#define CAN_NBTP_500 0x00090C01
#define CAN_DBTP_500 0x00090C13
#define CAN_NBTP_1k0 0x00040C01
#define CAN_DBTP_1k0 0x00040C13
2019-07-06 01:54:02 -07:00
#else
#error Please define CAN BTR settings for your MCU!
#endif
2015-07-10 06:01:56 -07:00
/*
* 500KBaud
* automatic wakeup
* automatic recover from abort mode
* See section 22.7.7 on the STM32 reference manual.
2019-07-06 01:54:02 -07:00
*
2016-02-25 10:01:33 -08:00
* 29 bit would be CAN_TI0R_EXID (?) but we do not mention it here
* CAN_TI0R_STID "Standard Identifier or Extended Identifier"? not mentioned as well
2015-07-10 06:01:56 -07:00
*/
#if defined(STM32F4XX) || defined(STM32F7XX)
static const CANConfig canConfig100 = {
.mcr = CAN_MCR_ABOM | CAN_MCR_AWUM | CAN_MCR_TXFP,
.btr = CAN_BTR_100
};
2019-07-06 01:54:02 -07:00
static const CANConfig canConfig250 = {
.mcr = CAN_MCR_ABOM | CAN_MCR_AWUM | CAN_MCR_TXFP,
.btr = CAN_BTR_250
};
2018-10-16 18:31:48 -07:00
2019-07-06 01:54:02 -07:00
static const CANConfig canConfig500 = {
.mcr = CAN_MCR_ABOM | CAN_MCR_AWUM | CAN_MCR_TXFP,
.btr = CAN_BTR_500
};
2018-10-16 20:05:38 -07:00
2019-07-06 01:54:02 -07:00
static const CANConfig canConfig1000 = {
2018-10-16 20:05:38 -07:00
CAN_MCR_ABOM | CAN_MCR_AWUM | CAN_MCR_TXFP,
2019-07-06 01:54:02 -07:00
CAN_BTR_1k0 };
#elif defined(STM32H7XX)
static const CANConfig canConfig100 = {
.NBTP = CAN_NBTP_100,
.DBTP = CAN_DBTP_100,
.CCCR = 0,
.TEST = 0,
.RXGFC = 0,
};
static const CANConfig canConfig250 = {
.NBTP = CAN_NBTP_250,
.DBTP = CAN_DBTP_250,
.CCCR = 0,
.TEST = 0,
.RXGFC = 0,
};
static const CANConfig canConfig500 = {
.NBTP = CAN_NBTP_500,
.DBTP = CAN_DBTP_500,
.CCCR = 0,
.TEST = 0,
.RXGFC = 0,
};
static const CANConfig canConfig1000 = {
.NBTP = CAN_NBTP_1k0,
.DBTP = CAN_DBTP_1k0,
.CCCR = 0,
.TEST = 0,
.RXGFC = 0,
};
#endif
2018-10-16 20:05:38 -07:00
static const CANConfig *canConfig = &canConfig500;
class CanRead final : public ThreadController<UTILITY_THREAD_STACK_SIZE> {
public:
CanRead(size_t index)
: ThreadController("CAN RX", PRIO_CAN_RX)
, m_index(index)
{
2017-01-16 13:03:37 -08:00
}
2015-07-10 06:01:56 -07:00
void ThreadTask() override {
CANDriver* device = detectCanDevice(m_index);
2015-07-10 06:01:56 -07:00
if (!device) {
warning(CUSTOM_ERR_CAN_CONFIGURATION, "CAN configuration issue");
return;
}
2015-07-10 06:01:56 -07:00
while (true) {
// Block until we get a message
msg_t result = canReceiveTimeout(device, CAN_ANY_MAILBOX, &m_buffer, TIME_INFINITE);
2015-07-10 06:01:56 -07:00
if (result != MSG_OK) {
continue;
}
2015-07-10 06:01:56 -07:00
// Process the message
2021-12-20 21:48:13 -08:00
engine->outputChannels.canReadCounter++;
2021-12-20 21:48:13 -08:00
processCanRxMessage(m_index, m_buffer, getTimeNowNt());
2015-07-10 06:01:56 -07:00
}
}
private:
const size_t m_index;
CANRxFrame m_buffer;
};
CCM_OPTIONAL static CanRead canRead1(0);
CCM_OPTIONAL static CanRead canRead2(1);
static CanWrite canWrite CCM_OPTIONAL;
static void canInfo() {
2016-12-19 14:01:43 -08:00
if (!isCanEnabled) {
efiPrintf("CAN is not enabled, please enable & restart");
2016-01-26 10:01:40 -08:00
return;
}
efiPrintf("CAN TX %s speed %d", hwPortname(engineConfiguration->canTxPin), engineConfiguration->canBaudRate);
efiPrintf("CAN RX %s", hwPortname(engineConfiguration->canRxPin));
efiPrintf("type=%d canReadEnabled=%s canWriteEnabled=%s period=%d", engineConfiguration->canNbcType,
2015-07-10 06:01:56 -07:00
boolToString(engineConfiguration->canReadEnabled), boolToString(engineConfiguration->canWriteEnabled),
2019-02-10 19:47:49 -08:00
engineConfiguration->canSleepPeriodMs);
2015-07-10 06:01:56 -07:00
2021-12-20 21:48:13 -08:00
efiPrintf("CAN rx_cnt=%d/tx_ok=%d/tx_not_ok=%d",
engine->outputChannels.canReadCounter,
engine->outputChannels.canWriteOk,
engine->outputChannels.canWriteNotOk);
2015-07-10 06:01:56 -07:00
}
2017-01-11 18:04:22 -08:00
void setCanType(int type) {
engineConfiguration->canNbcType = (can_nbc_e)type;
canInfo();
}
2019-11-07 12:22:17 -08:00
#if EFI_TUNER_STUDIO
2021-12-20 21:48:13 -08:00
void postCanState() {
if (!isCanEnabled) {
engine->outputChannels.canReadCounter = -1;
engine->outputChannels.canWriteOk = -1;
engine->outputChannels.canWriteNotOk = -1;
}
2018-01-29 16:41:39 -08:00
}
#endif /* EFI_TUNER_STUDIO */
2015-07-10 06:01:56 -07:00
void enableFrankensoCan() {
engineConfiguration->canTxPin = GPIOB_6;
engineConfiguration->canRxPin = GPIOB_12;
2016-01-24 12:02:46 -08:00
engineConfiguration->canReadEnabled = false;
}
2015-07-10 06:01:56 -07:00
void stopCanPins() {
efiSetPadUnusedIfConfigurationChanged(canTxPin);
efiSetPadUnusedIfConfigurationChanged(canRxPin);
2021-12-20 21:48:13 -08:00
efiSetPadUnusedIfConfigurationChanged(can2TxPin);
efiSetPadUnusedIfConfigurationChanged(can2RxPin);
2016-12-19 17:01:37 -08:00
}
// at the moment we support only very limited runtime configuration change, still not supporting online CAN toggle
void startCanPins() {
// nothing to do if we aren't enabled...
if (!isCanEnabled) {
return;
2017-01-16 13:03:37 -08:00
}
2016-12-19 14:01:43 -08:00
// Validate pins
if (!isValidCanTxPin(engineConfiguration->canTxPin)) {
if (engineConfiguration->canTxPin == GPIO_UNASSIGNED) {
// todo: smarter online change of settings, kill isCanEnabled with fire
return;
}
firmwareError(CUSTOM_OBD_70, "invalid CAN TX %s", hwPortname(engineConfiguration->canTxPin));
2015-07-10 06:01:56 -07:00
return;
}
if (!isValidCanRxPin(engineConfiguration->canRxPin)) {
if (engineConfiguration->canRxPin == GPIO_UNASSIGNED) {
// todo: smarter online change of settings, kill isCanEnabled with fire
return;
}
firmwareError(CUSTOM_OBD_70, "invalid CAN RX %s", hwPortname(engineConfiguration->canRxPin));
return;
}
2015-07-10 06:01:56 -07:00
efiSetPadModeIfConfigurationChanged("CAN TX", canTxPin, PAL_MODE_ALTERNATE(EFI_CAN_TX_AF));
efiSetPadModeIfConfigurationChanged("CAN RX", canRxPin, PAL_MODE_ALTERNATE(EFI_CAN_RX_AF));
2021-12-20 21:48:13 -08:00
efiSetPadModeIfConfigurationChanged("CAN2 TX", can2TxPin, PAL_MODE_ALTERNATE(EFI_CAN_TX_AF));
efiSetPadModeIfConfigurationChanged("CAN2 RX", can2RxPin, PAL_MODE_ALTERNATE(EFI_CAN_RX_AF));
}
void initCan(void) {
addConsoleAction("caninfo", canInfo);
2021-12-01 08:13:36 -08:00
isCanEnabled = false;
bool isCanConfigGood =
(isBrainPinValid(engineConfiguration->canTxPin)) && // both pins are set...
(isBrainPinValid(engineConfiguration->canRxPin)) &&
(engineConfiguration->canWriteEnabled || engineConfiguration->canReadEnabled) ; // ...and either read or write is enabled
// nothing to do if we aren't enabled...
2021-12-01 08:13:36 -08:00
if (!isCanConfigGood) {
return;
}
switch (engineConfiguration->canBaudRate) {
case B100KBPS:
canConfig = &canConfig100;
break;
case B250KBPS:
canConfig = &canConfig250;
break;
case B500KBPS:
canConfig = &canConfig500;
break;
case B1MBPS:
canConfig = &canConfig1000;
break;
default:
break;
}
// Initialize hardware
2019-04-12 17:52:51 -07:00
#if STM32_CAN_USE_CAN2
2015-07-10 06:01:56 -07:00
// CAN1 is required for CAN2
canStart(&CAND1, canConfig);
canStart(&CAND2, canConfig);
2015-07-10 06:01:56 -07:00
#else
canStart(&CAND1, canConfig);
2017-04-12 06:17:06 -07:00
#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
2021-12-20 21:48:13 -08:00
CanTxMessage::setDevice(detectCanDevice(0), detectCanDevice(1));
// fire up threads, as necessary
if (engineConfiguration->canWriteEnabled) {
canWrite.Start();
}
2015-07-10 06:01:56 -07:00
if (engineConfiguration->canReadEnabled) {
canRead1.Start();
canRead2.Start();
}
2021-12-01 08:13:36 -08:00
isCanEnabled = true;
}
bool getIsCanEnabled(void) {
return isCanEnabled;
2015-07-10 06:01:56 -07:00
}
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;
}
2015-07-10 06:01:56 -07:00
#endif /* EFI_CAN_SUPPORT */