2023-02-17 05:18:36 -08:00
|
|
|
#include "pch.h"
|
|
|
|
#include "kline.h"
|
2023-02-18 06:34:13 -08:00
|
|
|
#include "hellen_meta.h"
|
2023-03-09 16:11:36 -08:00
|
|
|
#include "crc8hondak.h"
|
2023-02-17 05:18:36 -08:00
|
|
|
|
2023-03-10 15:36:03 -08:00
|
|
|
size_t readWhileGives(ByteSource source, uint8_t *buffer, int bufferSize) {
|
|
|
|
size_t totalBytes = 0;
|
|
|
|
while (totalBytes < bufferSize) {
|
2023-03-10 20:02:01 -08:00
|
|
|
size_t readThisTime = source(&buffer[totalBytes], bufferSize - totalBytes);
|
2023-03-10 15:36:03 -08:00
|
|
|
if (readThisTime == 0) {
|
|
|
|
// looks like idle gap
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
totalBytes += readThisTime;
|
|
|
|
}
|
|
|
|
return totalBytes;
|
|
|
|
}
|
|
|
|
|
2023-02-17 06:21:09 -08:00
|
|
|
#ifdef EFI_KLINE
|
2023-03-09 14:25:03 -08:00
|
|
|
static SerialDriver* const klDriver = &KLINE_SERIAL_DEVICE;
|
2023-02-17 05:18:36 -08:00
|
|
|
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
|
|
|
|
2023-03-03 18:33:44 -08:00
|
|
|
void kLineThread(void*) {
|
|
|
|
while (1) {
|
2023-03-10 12:56:08 -08:00
|
|
|
/**
|
|
|
|
* under the hood there is SERIAL_BUFFERS_SIZE which we hope to help us
|
|
|
|
*/
|
|
|
|
|
2023-03-09 15:56:26 -08:00
|
|
|
uint8_t bufferIn[16];
|
2023-03-10 12:56:08 -08:00
|
|
|
// 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
|
2023-03-10 15:36:03 -08:00
|
|
|
ByteSource serialSource = [] (uint8_t * buffer, int maxSize) {
|
2023-03-11 05:42:15 -08:00
|
|
|
return chnReadTimeout(klDriver,buffer, maxSize, TIME_US2I(engineConfiguration->kLinePeriodUs));
|
2023-03-10 15:36:03 -08:00
|
|
|
};
|
2023-03-10 20:02:01 -08:00
|
|
|
size_t len = readWhileGives(serialSource, bufferIn, sizeof(bufferIn));
|
2023-03-10 15:36:03 -08:00
|
|
|
|
2023-02-17 05:18:36 -08:00
|
|
|
// to begin with just write byte to console
|
2023-03-10 19:18:25 -08:00
|
|
|
if (len > 0) {
|
|
|
|
efiPrintf("kline: got count 0x%02x", len);
|
|
|
|
for (size_t i =0;i<len;i++) {
|
2023-03-09 15:56:26 -08:00
|
|
|
efiPrintf("kline: got 0x%02x", bufferIn[i]);
|
2023-03-09 16:11:36 -08:00
|
|
|
totalBytes++;
|
|
|
|
}
|
2023-03-10 19:18:25 -08:00
|
|
|
if (len > 1) {
|
|
|
|
int crc = crc_hondak_calc(bufferIn, len - 1);
|
|
|
|
if (crc == bufferIn[len - 1]) {
|
2023-03-09 16:11:36 -08:00
|
|
|
efiPrintf("happy CRC 0x%02x", crc);
|
|
|
|
}
|
2023-03-09 15:56:26 -08:00
|
|
|
}
|
2023-02-17 06:21:09 -08: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);
|
|
|
|
}
|
2023-02-17 05:18:36 -08:00
|
|
|
}
|
|
|
|
}
|
2023-03-03 18:33:44 -08:00
|
|
|
#endif // EFI_KLINE
|
2023-02-17 05:18:36 -08:00
|
|
|
|
2023-02-23 13:05:59 -08:00
|
|
|
void startKLine() {
|
2023-02-17 05:18:36 -08:00
|
|
|
#ifdef EFI_KLINE
|
2023-03-09 14:05:46 -08:00
|
|
|
if (!engineConfiguration->enableKline) {
|
|
|
|
return;
|
|
|
|
}
|
2023-02-18 05:45:40 -08:00
|
|
|
#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 = {
|
2023-02-17 06:21:09 -08:00
|
|
|
#if EFI_PROD_CODE
|
2023-03-03 18:27:48 -08:00
|
|
|
.speed = 0,
|
2023-02-17 06:21:09 -08:00
|
|
|
.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;
|
|
|
|
|
2023-02-17 06:21:09 -08:00
|
|
|
sdStart(klDriver, &cfg);
|
2023-03-03 18:33:44 -08:00
|
|
|
#endif // EFI_KLINE
|
2023-02-17 06:23:16 -08:00
|
|
|
}
|
2023-02-23 13:05:59 -08:00
|
|
|
|
|
|
|
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-03 18:33:44 -08:00
|
|
|
|
2023-03-09 14:05:46 -08:00
|
|
|
sdStop(klDriver);
|
|
|
|
}
|
2023-03-03 18:33:44 -08:00
|
|
|
|
2023-02-23 13:05:59 -08:00
|
|
|
#endif /* EFI_PROD_CODE */
|
2023-03-03 18:33:44 -08:00
|
|
|
#endif // EFI_KLINE
|
2023-02-23 13:05:59 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void initKLine() {
|
2023-03-09 14:05:46 -08:00
|
|
|
if (!engineConfiguration->enableKline) {
|
|
|
|
return;
|
|
|
|
}
|
2023-02-23 13:05:59 -08:00
|
|
|
#ifdef EFI_KLINE
|
|
|
|
startKLine();
|
2023-03-03 18:33:44 -08:00
|
|
|
|
2023-03-11 05:42:15 -08:00
|
|
|
if (engineConfiguration->kLinePeriodUs == 0) {
|
|
|
|
engineConfiguration->kLinePeriodUs = 1000 /* us*/;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-03-03 18:33:44 -08:00
|
|
|
chThdCreateStatic(klThreadStack, sizeof(klThreadStack), NORMALPRIO + 1, kLineThread, nullptr);
|
|
|
|
addConsoleAction("kline", [](){
|
|
|
|
efiPrintf("kline totalBytes %d", totalBytes);
|
|
|
|
});
|
|
|
|
addConsoleActionI("klinesend", [](int value){
|
|
|
|
kLineOutPending = true;
|
|
|
|
kLineOut = value;
|
|
|
|
});
|
|
|
|
|
|
|
|
#endif // EFI_KLINE
|
2023-02-23 13:05:59 -08:00
|
|
|
}
|