rusefi/firmware/hw_layer/kline.cpp

190 lines
6.1 KiB
C++
Raw Normal View History

#include "pch.h"
#include "kline.h"
#include "hellen_meta.h"
#include "crc8hondak.h"
size_t readWhileGives(ByteSource source, uint8_t *buffer, int bufferSize) {
size_t totalBytes = 0;
while (totalBytes < bufferSize) {
size_t readThisTime = source(&buffer[totalBytes], bufferSize - totalBytes);
if (readThisTime == 0) {
// looks like idle gap
break;
}
totalBytes += readThisTime;
}
return totalBytes;
}
2023-03-12 09:01:51 -07:00
#define HONDA_K_40_PACKET 0x40
bool kAcRequestState;
static void handleHonda(uint8_t *bufferIn) {
uint8_t acByte = bufferIn[2];
kAcRequestState = acByte & 0x80;
if (engineConfiguration->verboseKLine) {
if (engineConfiguration->verboseKLine) {
efiPrintf("honda 0x40 packet with 0x%02x state %d", acByte, kAcRequestState);
}
}
}
#ifdef EFI_KLINE
static SerialDriver* const klDriver = &KLINE_SERIAL_DEVICE;
static THD_WORKING_AREA(klThreadStack, UTILITY_THREAD_STACK_SIZE);
2023-02-18 20:13:24 -08:00
static int totalBytes = 0;
2023-03-02 18:29:40 -08:00
static bool kLineOutPending = false;
static int kLineOut;
2023-02-18 20:13:24 -08:00
void kLineThread(void*) {
2023-04-03 13:54:26 -07:00
// due to single wire we read everything we've transmitted
bool ignoreRecentTransmit = false;
2023-04-04 19:41:22 -07:00
int sendCounter = 0;
while (1) {
2023-04-03 13:54:26 -07:00
/**
* under the hood there is SERIAL_BUFFERS_SIZE which we hope to help us
*/
uint8_t bufferIn[16];
// a bit of a busy read open question if this would affect performance?
// on 2003 Honda for instance the bus seems to be 70%-ish busy. 9600 baud is 1.04ms per byte, a bit below 1kHz
ByteSource serialSource = [] (uint8_t * buffer, int maxSize) {
return chnReadTimeout(klDriver,buffer, maxSize, TIME_US2I(engineConfiguration->kLinePeriodUs));
};
size_t len = readWhileGives(serialSource, bufferIn, sizeof(bufferIn));
2023-04-03 13:50:24 -07:00
if (engineConfiguration->verboseKLine) {
efiPrintf("ignoreRecentTransmit %d", ignoreRecentTransmit);
}
// to begin with just write byte to console
if (len > 0) {
2023-03-12 09:01:51 -07:00
if (engineConfiguration->verboseKLine) {
efiPrintf("kline: got count 0x%02x", len);
}
for (size_t i =0;i<len;i++) {
2023-03-12 09:01:51 -07:00
if (engineConfiguration->verboseKLine) {
efiPrintf("kline: got 0x%02x", bufferIn[i]);
}
totalBytes++;
}
if (len > 1) {
int crc = crc_hondak_calc(bufferIn, len - 1);
if (crc == bufferIn[len - 1]) {
2023-03-12 09:01:51 -07:00
if (engineConfiguration->verboseKLine) {
efiPrintf("happy CRC 0x%02x", crc);
}
if (bufferIn[0] == HONDA_K_40_PACKET) {
handleHonda(bufferIn);
}
2023-04-03 10:52:23 -07:00
} else if (bufferIn[0] == HONDA_K_40_PACKET && bufferIn[4] == crc_hondak_calc(bufferIn, 4)) {
2023-04-03 09:41:17 -07:00
if (engineConfiguration->verboseKLine) {
efiPrintf("hack for now, happy CRC 0x%02x", crc);
}
handleHonda(bufferIn);
}
2023-04-03 11:10:07 -07:00
2023-04-03 13:33:49 -07:00
if (engineConfiguration->kLineDoHondaSend && !ignoreRecentTransmit) {
2023-04-04 19:41:22 -07:00
sendCounter++;
#define PACKET_SIZE 7
const char out2[] = {0x2, 0x0, 0x0, 0x50, 0x0, 0x0, 149};
static_assert(sizeof(out2) == PACKET_SIZE);
const char outB[] = {0x42, 0x0, 0x0, 0x50, 0x0, 0x0, 164};
static_assert(sizeof(outB) == PACKET_SIZE);
const char *out = (sendCounter % 3 == 0) ? outB : out2;
2023-04-03 13:33:49 -07:00
if (engineConfiguration->verboseKLine) {
efiPrintf("kline doSend");
}
2023-04-04 19:41:22 -07:00
chnWrite(klDriver, (const uint8_t *)out, PACKET_SIZE);
2023-04-03 13:30:00 -07:00
ignoreRecentTransmit = true;
} else {
ignoreRecentTransmit = false;
2023-04-03 11:10:07 -07:00
}
}
}
2023-03-02 18:29:40 -08:00
if (kLineOutPending) {
kLineOutPending = false;
efiPrintf("kline OUT: 0x%02x", kLineOut);
chnWrite(klDriver, (const uint8_t *)kLineOut, 1);
}
}
}
#endif // EFI_KLINE
void startKLine() {
#ifdef EFI_KLINE
2023-03-09 14:05:46 -08:00
if (!engineConfiguration->enableKline) {
return;
}
#if EFI_PROD_CODE
efiSetPadMode("K-Line UART RX", KLINE_SERIAL_DEVICE_RX, PAL_MODE_ALTERNATE(TS_SERIAL_AF));
efiSetPadMode("K-Line UART TX", KLINE_SERIAL_DEVICE_TX, PAL_MODE_ALTERNATE(TS_SERIAL_AF));
#endif /* EFI_PROD_CODE */
2023-03-03 19:03:00 -08:00
static SerialConfig cfg = {
#if EFI_PROD_CODE
2023-03-03 18:27:48 -08:00
.speed = 0,
.cr1 = 0,
.cr2 = USART_CR2_STOP1_BITS | USART_CR2_LINEN,
.cr3 = 0
#endif // EFI_PROD_CODE
};
2023-03-03 18:27:48 -08:00
if (engineConfiguration->kLineBaudRate < 100)
engineConfiguration->kLineBaudRate = KLINE_BAUD_RATE;
cfg.speed = engineConfiguration->kLineBaudRate;
sdStart(klDriver, &cfg);
#endif // EFI_KLINE
}
void stopKLine() {
#ifdef EFI_KLINE
#if EFI_PROD_CODE
2023-03-09 14:05:46 -08:00
if (activeConfiguration.enableKline) {
efiSetPadUnused(KLINE_SERIAL_DEVICE_RX);
efiSetPadUnused(KLINE_SERIAL_DEVICE_TX);
2023-03-09 14:05:46 -08:00
sdStop(klDriver);
}
#endif /* EFI_PROD_CODE */
#endif // EFI_KLINE
}
void initKLine() {
2023-03-09 14:05:46 -08:00
if (!engineConfiguration->enableKline) {
return;
}
#ifdef EFI_KLINE
startKLine();
if (engineConfiguration->kLinePeriodUs == 0) {
2023-04-03 08:51:11 -07:00
engineConfiguration->kLinePeriodUs = 300 /* us*/;
}
chThdCreateStatic(klThreadStack, sizeof(klThreadStack), NORMALPRIO + 1, kLineThread, nullptr);
addConsoleAction("kline", [](){
efiPrintf("kline totalBytes %d", totalBytes);
});
2023-04-03 11:10:07 -07:00
addConsoleAction("klineyes", [](){
2023-04-03 13:33:49 -07:00
engineConfiguration->kLineDoHondaSend = true;
efiPrintf("kline send %d", engineConfiguration->kLineDoHondaSend);
2023-04-03 11:10:07 -07:00
});
2023-04-03 11:51:26 -07:00
addConsoleAction("klineno", [](){
2023-04-03 13:33:49 -07:00
engineConfiguration->kLineDoHondaSend = false;
efiPrintf("kline send %d", engineConfiguration->kLineDoHondaSend);
2023-04-03 11:10:07 -07:00
});
addConsoleActionI("klinesend", [](int value){
kLineOutPending = true;
kLineOut = value;
});
#endif // EFI_KLINE
}