2020-03-18 19:07:41 -07:00
|
|
|
/**
|
|
|
|
* @file can_msg_tx.cpp
|
|
|
|
*
|
|
|
|
* CAN message transmission
|
|
|
|
*
|
|
|
|
* @date Mar 13, 2020
|
|
|
|
* @author Matthew Kennedy, (c) 2012-2020
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "efifeatures.h"
|
|
|
|
#include "global.h"
|
|
|
|
|
|
|
|
#if EFI_CAN_SUPPORT
|
|
|
|
#include "can_msg_tx.h"
|
2020-03-31 17:57:52 -07:00
|
|
|
#include "engine_configuration.h"
|
2020-03-18 19:07:41 -07:00
|
|
|
|
2020-03-31 17:57:52 -07:00
|
|
|
EXTERN_CONFIG
|
|
|
|
extern LoggingWithStorage sharedLogger;
|
2020-03-18 19:07:41 -07:00
|
|
|
|
2020-09-06 18:30:14 -07:00
|
|
|
extern int canWriteOk;
|
|
|
|
extern int canWriteNotOk;
|
|
|
|
|
2020-03-18 19:07:41 -07:00
|
|
|
/*static*/ CANDriver* CanTxMessage::s_device = nullptr;
|
|
|
|
|
|
|
|
/*static*/ void CanTxMessage::setDevice(CANDriver* device) {
|
|
|
|
s_device = device;
|
|
|
|
}
|
|
|
|
|
2020-12-12 13:34:21 -08:00
|
|
|
CanTxMessage::CanTxMessage(uint32_t eid, uint8_t dlc, bool isExtended) {
|
|
|
|
m_frame.IDE = isExtended ? CAN_IDE_EXT : CAN_IDE_STD;
|
2020-03-18 19:07:41 -07:00
|
|
|
m_frame.EID = eid;
|
|
|
|
m_frame.RTR = CAN_RTR_DATA;
|
2020-04-13 14:05:52 -07:00
|
|
|
m_frame.DLC = dlc;
|
2020-03-18 19:07:41 -07:00
|
|
|
memset(m_frame.data8, 0, sizeof(m_frame.data8));
|
|
|
|
}
|
|
|
|
|
|
|
|
CanTxMessage::~CanTxMessage() {
|
|
|
|
auto device = s_device;
|
|
|
|
|
|
|
|
if (!device) {
|
|
|
|
warning(CUSTOM_ERR_CAN_CONFIGURATION, "CAN configuration issue");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-03-31 17:57:52 -07:00
|
|
|
if (CONFIG(debugMode) == DBG_CAN) {
|
|
|
|
scheduleMsg(&sharedLogger, "Sending CAN message: SID %x/%x %x %x %x %x %x %x %x %x", m_frame.SID, m_frame.DLC,
|
|
|
|
m_frame.data8[0], m_frame.data8[1],
|
|
|
|
m_frame.data8[2], m_frame.data8[3],
|
|
|
|
m_frame.data8[4], m_frame.data8[5],
|
|
|
|
m_frame.data8[6], m_frame.data8[7]);
|
|
|
|
}
|
|
|
|
|
2020-03-18 19:07:41 -07:00
|
|
|
// 100 ms timeout
|
2020-09-06 18:30:14 -07:00
|
|
|
msg_t msg = canTransmit(device, CAN_ANY_MAILBOX, &m_frame, TIME_MS2I(100));
|
|
|
|
if (msg == MSG_OK) {
|
|
|
|
canWriteOk++;
|
|
|
|
} else {
|
|
|
|
canWriteNotOk++;
|
|
|
|
}
|
2020-03-18 19:07:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t& CanTxMessage::operator[](size_t index) {
|
|
|
|
return m_frame.data8[index];
|
|
|
|
}
|
|
|
|
|
|
|
|
void CanTxMessage::setShortValue(uint16_t value, size_t offset) {
|
|
|
|
m_frame.data8[offset] = value & 0xFF;
|
|
|
|
m_frame.data8[offset + 1] = value >> 8;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CanTxMessage::setBit(size_t byteIdx, size_t bitIdx) {
|
|
|
|
m_frame.data8[byteIdx] |= 1 << bitIdx;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // EFI_CAN_SUPPORT
|