fome-fw/firmware/console/binary/ts_can_channel.cpp

110 lines
2.7 KiB
C++
Raw Normal View History

2021-12-01 08:13:36 -08:00
/**
* @file This file implements CAN-to-TS bridge.
*
* @date Apr 24, 2021
* @author andreika <prometheus.pcb@gmail.com>
* @author Andrey Belomutskiy, (c) 2012-2021
*/
#include "pch.h"
#include "tunerstudio.h"
#include "tunerstudio_io.h"
#if EFI_CAN_SERIAL
#include "serial_can.h"
2021-12-01 08:13:36 -08:00
#include "can_hw.h"
#if !EFI_CAN_SUPPORT
#error "EFI_CAN_SERIAL requires EFI_CAN_SUPPORT"
#endif
class CanTsChannel : public TsChannelBase {
2021-12-01 08:13:36 -08:00
public:
2021-12-03 19:20:40 -08:00
CanTsChannel() : TsChannelBase("CAN") {
}
void start();
// TsChannelBase implementation
2021-12-03 19:20:40 -08:00
void write(const uint8_t* buffer, size_t size, bool) override;
size_t readTimeout(uint8_t* buffer, size_t size, int timeout) override;
void flush() override;
2021-12-01 08:13:36 -08:00
bool isReady() const override;
void stop() override;
// Special override for writeCrcPacket for small packets
void writeCrcPacket(uint8_t responseCode, const uint8_t* buf, size_t size, bool allowLongPackets = false) override;
};
void CanTsChannel::start() {
2021-12-01 08:13:36 -08:00
if (!getIsCanEnabled()) {
warning(CUSTOM_ERR_CAN_CONFIGURATION, "CAN not enabled");
return;
}
2021-12-03 19:20:40 -08:00
if (!engineConfiguration->canReadEnabled || !engineConfiguration->canWriteEnabled) {
2021-12-01 08:13:36 -08:00
warning(CUSTOM_ERR_CAN_CONFIGURATION, "CAN read or write not enabled");
}
}
void CanTsChannel::stop() {
}
void CanTsChannel::writeCrcPacket(uint8_t responseCode, const uint8_t* buf, size_t size, bool allowLongPackets) {
#ifdef TS_CAN_DEVICE_SHORT_PACKETS_IN_ONE_FRAME
2021-12-01 08:13:36 -08:00
// a special case for short packets: we can send them in 1 frame, without CRC & size,
// because the CAN protocol is already protected by its own checksum.
if ((size + 1) <= 7) {
2021-12-03 19:20:40 -08:00
write(&responseCode, 1, false); // header without size
if (size > 0) {
2021-12-03 19:20:40 -08:00
write(buf, size, false); // body
}
flush();
return;
}
2021-12-01 08:13:36 -08:00
#endif /* TS_CAN_DEVICE_SHORT_PACKETS_IN_ONE_FRAME */
// Packet too large, use default implementation
TsChannelBase::writeCrcPacket(responseCode, buf, size, allowLongPackets);
}
2021-12-03 19:20:40 -08:00
void CanTsChannel::write(const uint8_t* buffer, size_t size, bool) {
2021-12-01 08:13:36 -08:00
canStreamAddToTxTimeout(&size, buffer, BINARY_IO_TIMEOUT);
}
size_t CanTsChannel::readTimeout(uint8_t* buffer, size_t size, int timeout) {
2021-12-01 08:13:36 -08:00
canStreamReceiveTimeout(&size, buffer, timeout);
return size;
}
void CanTsChannel::flush() {
2021-12-01 08:13:36 -08:00
canStreamFlushTx(BINARY_IO_TIMEOUT);
}
2021-12-01 08:13:36 -08:00
bool CanTsChannel::isReady() const {
// this channel is always ready
return true;
}
2021-12-01 08:13:36 -08:00
static CanTsChannel canChannel;
struct CanTsThread : public TunerstudioThread {
CanTsThread() : TunerstudioThread("CAN TS Channel") { }
TsChannelBase* setupChannel() override {
canChannel.start();
return &canChannel;
}
};
static CanTsThread canTsThread;
void startCanConsole() {
2022-07-21 12:17:32 -07:00
canTsThread.start();
2021-12-01 08:13:36 -08:00
canStreamInit();
}
#endif // EFI_CAN_SERIAL