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
|
|
|
|
*
|
|
|
|
* @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
|
|
|
*/
|
|
|
|
|
2018-09-16 19:26:57 -07:00
|
|
|
#include "global.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
|
|
|
|
2020-03-19 05:43:37 -07:00
|
|
|
#include "can.h"
|
2019-01-27 21:52:21 -08:00
|
|
|
#include "engine_configuration.h"
|
2015-07-10 06:01:56 -07:00
|
|
|
#include "pin_repository.h"
|
2019-01-27 21:52:21 -08:00
|
|
|
#include "can_hw.h"
|
2020-03-18 19:07:41 -07:00
|
|
|
#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"
|
2020-03-19 11:01:07 -07:00
|
|
|
#include "engine.h"
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2020-03-29 16:06:03 -07:00
|
|
|
EXTERN_ENGINE;
|
2015-07-10 06:01:56 -07:00
|
|
|
|
|
|
|
static int canReadCounter = 0;
|
|
|
|
static int canWriteOk = 0;
|
|
|
|
static int canWriteNotOk = 0;
|
2016-12-19 14:01:43 -08:00
|
|
|
static bool isCanEnabled = false;
|
2015-07-10 06:01:56 -07:00
|
|
|
static LoggingWithStorage logger("CAN driver");
|
|
|
|
|
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
|
|
|
|
#ifdef STM32F4XX
|
|
|
|
// These have an 85.7% sample point
|
|
|
|
#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_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))
|
|
|
|
#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
|
|
|
*/
|
|
|
|
|
2019-07-06 01:54:02 -07:00
|
|
|
static const CANConfig canConfig250 = {
|
2018-10-16 18:31:48 -07:00
|
|
|
CAN_MCR_ABOM | CAN_MCR_AWUM | CAN_MCR_TXFP,
|
2019-07-06 01:54:02 -07:00
|
|
|
CAN_BTR_250 };
|
2018-10-16 18:31:48 -07:00
|
|
|
|
2019-07-06 01:54:02 -07:00
|
|
|
static const CANConfig canConfig500 = {
|
|
|
|
CAN_MCR_ABOM | CAN_MCR_AWUM | CAN_MCR_TXFP,
|
|
|
|
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 };
|
2018-10-16 20:05:38 -07:00
|
|
|
|
2019-12-22 09:17:53 -08:00
|
|
|
class CanRead final : public ThreadController<256> {
|
|
|
|
public:
|
|
|
|
CanRead()
|
|
|
|
: ThreadController("CAN RX", NORMALPRIO)
|
|
|
|
{
|
2017-01-16 13:03:37 -08:00
|
|
|
}
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2019-12-22 09:17:53 -08:00
|
|
|
void ThreadTask() override {
|
|
|
|
CANDriver* device = detectCanDevice(CONFIG(canRxPin), CONFIG(canTxPin));
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2019-12-22 09:17:53 -08:00
|
|
|
if (!device) {
|
|
|
|
warning(CUSTOM_ERR_CAN_CONFIGURATION, "CAN configuration issue");
|
|
|
|
return;
|
|
|
|
}
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2019-12-22 09:17:53 -08: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
|
|
|
|
2019-12-22 09:17:53 -08:00
|
|
|
if (result != MSG_OK) {
|
|
|
|
continue;
|
|
|
|
}
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2019-12-22 09:17:53 -08:00
|
|
|
// Process the message
|
|
|
|
canReadCounter++;
|
2020-01-12 00:44:37 -08:00
|
|
|
|
2020-03-31 20:21:05 -07:00
|
|
|
processCanRxMessage(m_buffer, &logger, getTimeNowNt());
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
}
|
2019-12-22 09:17:53 -08:00
|
|
|
|
|
|
|
private:
|
|
|
|
CANRxFrame m_buffer;
|
|
|
|
};
|
|
|
|
|
|
|
|
static CanRead canRead;
|
|
|
|
static CanWrite canWrite;
|
|
|
|
|
2015-07-10 06:01:56 -07:00
|
|
|
|
|
|
|
static void canInfo(void) {
|
2016-12-19 14:01:43 -08:00
|
|
|
if (!isCanEnabled) {
|
2016-01-26 10:01:40 -08:00
|
|
|
scheduleMsg(&logger, "CAN is not enabled, please enable & restart");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-03-31 13:49:19 -07:00
|
|
|
#if EFI_CANBUS_SLAVE
|
|
|
|
scheduleMsg(&logger, "CAN SLAVE MODE");
|
|
|
|
#endif
|
|
|
|
|
2019-12-11 14:48:55 -08:00
|
|
|
scheduleMsg(&logger, "CAN TX %s", hwPortname(CONFIG(canTxPin)));
|
|
|
|
scheduleMsg(&logger, "CAN RX %s", hwPortname(CONFIG(canRxPin)));
|
2015-07-10 06:01:56 -07:00
|
|
|
scheduleMsg(&logger, "type=%d canReadEnabled=%s canWriteEnabled=%s period=%d", engineConfiguration->canNbcType,
|
|
|
|
boolToString(engineConfiguration->canReadEnabled), boolToString(engineConfiguration->canWriteEnabled),
|
2019-02-10 19:47:49 -08:00
|
|
|
engineConfiguration->canSleepPeriodMs);
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2018-10-16 20:05:38 -07:00
|
|
|
scheduleMsg(&logger, "CAN rx_cnt=%d/tx_ok=%d/tx_not_ok=%d", canReadCounter, canWriteOk, 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
|
2018-01-29 16:41:39 -08:00
|
|
|
void postCanState(TunerStudioOutputChannels *tsOutputChannels) {
|
|
|
|
tsOutputChannels->debugIntField1 = isCanEnabled ? canReadCounter : -1;
|
|
|
|
tsOutputChannels->debugIntField2 = isCanEnabled ? canWriteOk : -1;
|
|
|
|
tsOutputChannels->debugIntField3 = isCanEnabled ? canWriteNotOk : -1;
|
|
|
|
}
|
2019-11-06 17:01:53 -08:00
|
|
|
#endif /* EFI_TUNER_STUDIO */
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2017-05-15 20:33:22 -07:00
|
|
|
void enableFrankensoCan(DECLARE_ENGINE_PARAMETER_SIGNATURE) {
|
2019-12-11 14:48:55 -08:00
|
|
|
CONFIG(canTxPin) = GPIOB_6;
|
|
|
|
CONFIG(canRxPin) = GPIOB_12;
|
2016-01-24 12:02:46 -08:00
|
|
|
engineConfiguration->canReadEnabled = false;
|
|
|
|
}
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2017-05-15 20:33:22 -07:00
|
|
|
void stopCanPins(DECLARE_ENGINE_PARAMETER_SIGNATURE) {
|
2019-12-11 14:48:55 -08:00
|
|
|
brain_pin_markUnused(activeConfiguration.canTxPin);
|
|
|
|
brain_pin_markUnused(activeConfiguration.canRxPin);
|
2016-12-19 17:01:37 -08:00
|
|
|
}
|
|
|
|
|
2017-05-15 20:33:22 -07:00
|
|
|
void startCanPins(DECLARE_ENGINE_PARAMETER_SIGNATURE) {
|
2019-12-11 14:48:55 -08:00
|
|
|
efiSetPadMode("CAN TX", CONFIG(canTxPin), PAL_MODE_ALTERNATE(EFI_CAN_TX_AF));
|
|
|
|
efiSetPadMode("CAN RX", CONFIG(canRxPin), PAL_MODE_ALTERNATE(EFI_CAN_RX_AF));
|
2016-12-19 17:01:37 -08:00
|
|
|
}
|
|
|
|
|
2015-07-10 06:01:56 -07:00
|
|
|
void initCan(void) {
|
2019-12-22 09:17:53 -08:00
|
|
|
addConsoleAction("caninfo", canInfo);
|
|
|
|
|
|
|
|
isCanEnabled =
|
|
|
|
(CONFIG(canTxPin) != GPIO_UNASSIGNED) && // both pins are set...
|
|
|
|
(CONFIG(canRxPin) != GPIO_UNASSIGNED) &&
|
|
|
|
(CONFIG(canWriteEnabled) || CONFIG(canReadEnabled)) ; // ...and either read or write is enabled
|
|
|
|
|
|
|
|
// 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
|
|
|
|
2019-12-22 09:17:53 -08:00
|
|
|
// Validate pins
|
|
|
|
if (!isValidCanTxPin(CONFIG(canTxPin))) {
|
|
|
|
firmwareError(CUSTOM_OBD_70, "invalid CAN TX %s", hwPortname(CONFIG(canTxPin)));
|
2015-07-10 06:01:56 -07:00
|
|
|
return;
|
2019-12-22 09:17:53 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!isValidCanRxPin(CONFIG(canRxPin))) {
|
|
|
|
firmwareError(CUSTOM_OBD_70, "invalid CAN RX %s", hwPortname(CONFIG(canRxPin)));
|
|
|
|
return;
|
|
|
|
}
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2019-12-22 09:17:53 -08:00
|
|
|
// 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
|
2018-10-16 18:31:48 -07:00
|
|
|
canStart(&CAND1, &canConfig500);
|
|
|
|
canStart(&CAND2, &canConfig500);
|
2015-07-10 06:01:56 -07:00
|
|
|
#else
|
2018-10-16 18:31:48 -07:00
|
|
|
canStart(&CAND1, &canConfig500);
|
2017-04-12 06:17:06 -07:00
|
|
|
#endif /* STM32_CAN_USE_CAN2 */
|
|
|
|
|
2020-03-18 19:07:41 -07:00
|
|
|
// Plumb CAN device to tx system
|
|
|
|
CanTxMessage::setDevice(detectCanDevice(
|
|
|
|
CONFIG(canRxPin),
|
|
|
|
CONFIG(canTxPin)
|
|
|
|
));
|
|
|
|
|
2019-12-22 09:17:53 -08:00
|
|
|
// fire up threads, as necessary
|
|
|
|
if (CONFIG(canWriteEnabled)) {
|
|
|
|
canWrite.setPeriod(CONFIG(canSleepPeriodMs));
|
|
|
|
canWrite.Start();
|
|
|
|
}
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2019-12-22 09:17:53 -08:00
|
|
|
if (CONFIG(canReadEnabled)) {
|
|
|
|
canRead.Start();
|
|
|
|
}
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2019-12-22 09:17:53 -08:00
|
|
|
startCanPins();
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* EFI_CAN_SUPPORT */
|