From e22b9c681f0d2095abfbc10675d978bc1ad04595 Mon Sep 17 00:00:00 2001 From: LucianoDev Date: Tue, 31 Mar 2026 10:15:27 +0200 Subject: [PATCH] Dfi now works and all vpdiag compatible, also cleaned up a bit CMDs --- Core/Inc/ee_manager.h | 6 ++++-- Core/Kline_Libs/IKW1281Connection.c | 2 ++ Core/Kline_Libs/IKW1281Connection.h | 8 ++++++-- Core/Kline_Libs/kline.c | 31 ++++++++++++++++++++--------- Core/Src/ee_manager.c | 17 ++++++++++------ 5 files changed, 45 insertions(+), 19 deletions(-) diff --git a/Core/Inc/ee_manager.h b/Core/Inc/ee_manager.h index f2b3aaf..46ad97e 100644 --- a/Core/Inc/ee_manager.h +++ b/Core/Inc/ee_manager.h @@ -14,6 +14,8 @@ extern uint8_t memWrite; /* EEprom values in ram */ extern float dFi; +extern int8_t s_dfi_code; + extern uint8_t inj_mode; extern uint8_t requestWriteHours; @@ -21,9 +23,9 @@ extern uint8_t requestWriteHours; extern void MEM_Init(void); extern void MEM_WriteAll(void); extern void MEM_RetrieveValues(void); -extern void MEM_WriteCustom(float c_dFi, uint8_t c_inj_mode); +extern void MEM_WriteCustom(int8_t c_dFi, uint8_t c_inj_mode); extern void IncrementQuartHourCounter(void); - +extern int8_t GetDfiValue(); void MEM_WriteDefault(void); diff --git a/Core/Kline_Libs/IKW1281Connection.c b/Core/Kline_Libs/IKW1281Connection.c index c880848..663e9e9 100644 --- a/Core/Kline_Libs/IKW1281Connection.c +++ b/Core/Kline_Libs/IKW1281Connection.c @@ -255,6 +255,8 @@ int ReadAndAckByte_Tmo(uint32_t ms, uint8_t *out){ } void SendAckPacket(){ uint8_t a = (uint8_t)PACKET_CMD_ACK; SendPacket(&a, 1); } +void SendNakPacket(){ uint8_t a = (uint8_t)PACKET_CMD_NAK; SendPacket(&a, 1); } + void SendPacket(uint8_t* payload, uint8_t length){ uint8_t packetLength = (uint8_t)(length + 2); static uint8_t packet[1 + 1 + MAX_PACKET_SIZE + 1]; diff --git a/Core/Kline_Libs/IKW1281Connection.h b/Core/Kline_Libs/IKW1281Connection.h index e502056..cd6088b 100644 --- a/Core/Kline_Libs/IKW1281Connection.h +++ b/Core/Kline_Libs/IKW1281Connection.h @@ -41,12 +41,14 @@ typedef struct { typedef enum { PACKET_CMD_ReadIdent = 0x00, - PACKET_CMD_ReadIdentAdress = 0x01, + PACKET_CMD_ReadRam = 0x01, + PACKET_CMD_WriteRam = 0x02, PACKET_CMD_ReadRomEeprom = 0x03, PACKET_CMD_ActuatorTest = 0x04, PACKET_CMD_FaultCodesDelete = 0x05, PACKET_CMD_End = 0x06, PACKET_CMD_FaultCodesRead = 0x07, + PACKET_CMD_SingleRead = 0x08, PACKET_CMD_ACK = 0x09, PACKET_CMD_NAK = 0x0A, PACKET_CMD_SoftwareCoding = 0x10, @@ -61,6 +63,7 @@ typedef enum { PACKET_CMD_ActuatorTestResponse = 0xF5, PACKET_CMD_AsciiData = 0xF6, PACKET_CMD_WriteEepromResponse = 0xF9, + PACKET_CMD_SingleReadResponse = 0xFB, PACKET_CMD_FaultCodesResponse = 0xFC, PACKET_CMD_ReadRomEepromResponse = 0xFD } PacketCommand; @@ -83,7 +86,7 @@ typedef struct { uint8_t isAckNak; } ParsedPacket; -#define KLINE_TXBUF_MAX 32 // enough for KWP1281: 2 header + 16 data + 0x03 + margins +#define KLINE_TXBUF_MAX 24 // enough for KWP1281: 2 header + 16 data + 0x03 + margins // === Non-blocking TX "byte pump" === typedef struct { @@ -117,6 +120,7 @@ void KLine_OnByteReceived(uint8_t byte); // Legacy/blocking API (still available) void SendPacket(uint8_t* payload, uint8_t length); void SendAckPacket(void); +void SendNakPacket(void); void WriteByteAndReadAck(uint8_t b); void WriteByteRaw(uint8_t b); void WriteComplement(uint8_t b); diff --git a/Core/Kline_Libs/kline.c b/Core/Kline_Libs/kline.c index e8b04d9..49cefe2 100644 --- a/Core/Kline_Libs/kline.c +++ b/Core/Kline_Libs/kline.c @@ -15,6 +15,7 @@ #include #include // for roundf + static int ReceivePacket_Tmo(ParsedPacket *out, uint32_t overall_ms); static void KLine_HandleWriteEeprom(const uint8_t *body, uint8_t body_len); static void KLine_HandleReadRomEeprom(const uint8_t *body, uint8_t body_len); @@ -22,7 +23,7 @@ static void KLine_HandleReadIdentAddress(const uint8_t *body, uint8_t body_len); static void KLine_HandleReadEeprom(const uint8_t *body, uint8_t body_len); static void KLine_HandlePassword(const uint8_t *body, uint8_t body_len); static void KLine_HandleEnd(void); -static void KLine_HandleCustomPacket(const uint8_t *body, uint8_t body_len); +static void KLine_HandleWriteRam(const uint8_t *body, uint8_t body_len); static void KLine_TerminateAndRearm(void); extern float dFi; @@ -144,7 +145,6 @@ void KLine_Slave_Init(void) KLine_Rearm5Baud(); // <— call it here //KLine5_ResetToIdle(); - KLine_SetDFI_Value(dFi); } @@ -288,10 +288,14 @@ void KLine_Service(void) Slave_SendAsciiStream_WithAcks(combined, n); KLine_SessionKick(); break; - case PACKET_CMD_ReadIdentAdress: // readIdentAddressCMD + case PACKET_CMD_ReadRam: // readIdentAddressCMD KLine_HandleReadIdentAddress(&packet.raw[3], (uint8_t)(packet.length - 4)); packet.type = PACKET_TYPE_UNKNOWN; break; + case PACKET_CMD_WriteRam: // readIdentAddressCMD + KLine_HandleWriteRam(&packet.raw[2], (uint8_t)(packet.length - 4)); + packet.type = PACKET_TYPE_UNKNOWN; + break; case PACKET_CMD_FaultCodesRead: KLine_DTCResponse(); // <-- respond break; @@ -321,7 +325,8 @@ void KLine_Service(void) break; // early return is fine default: - KLine_HandleCustomPacket(&packet.raw[2], (uint8_t)(packet.length - 4)); + SendNakPacket(); + //KLine_HandleCustomPacket(&packet.raw[2], (uint8_t)(packet.length - 4)); break; } } @@ -594,7 +599,7 @@ static size_t KLine_BuildDTCStream(uint8_t *out /*>=64*/) { KLine_SetDTCList(dtcs, 4); //custom test errors - KLine_SetDFI_Value(dFi); // whatever current DFI value is; this encodes s_dfi_code + //KLine_SetDFI_Value(dFi); // whatever current DFI value is; this encodes s_dfi_code size_t w = 0; for (size_t i = 0; i < DTC_TOTAL_RECORDS; ++i) { @@ -865,7 +870,7 @@ static const uint8_t kReadIdentAddrBody[] = { 0x02, 0x00, 0xC6 }; static const uint8_t kActivateSyncOut[] = { 0x02, 0x88, 0x01, 0x04, 0x06, 0x01 }; -void KLine_SetDFI_Value(float dfi) +/*void KLine_SetDFI_Value(float dfi) { // encode: master decodes as ((int8_t)raw[3]) * 3.0 / 256.0 float scaled = dfi * 256.0f / 3.0f; @@ -873,7 +878,7 @@ void KLine_SetDFI_Value(float dfi) if (val < -128) val = -128; if (val > 127) val = 127; s_dfi_code = (int8_t)val; -} +}*/ float KLine_GetDFI(void) { return ((float)s_dfi_code * 3.0f) / 256.0f; @@ -994,7 +999,12 @@ static void KLine_HandleReadEeprom(const uint8_t *body, uint8_t body_len) // ---- DFI @ 0x0044, len=2 (requires DFI unlock) ---- if (addr == 0x0044 && len_req == 0x02) { - if (!s_eeprom_unlocked_dfi) return; + if (!s_eeprom_unlocked_dfi) { + SendNakPacket(); + return; + } + //KLine_SetDFI_Value(dFi); + int8_t s_dfi_code = GetDfiValue(); uint8_t payload[3]; payload[0] = (uint8_t)PACKET_CMD_ReadEepromResponse; // 0xEF @@ -1142,15 +1152,18 @@ static void KLine_HandleReadIdentAddress(const uint8_t *body, uint8_t body_len) } uint8_t SYNC_PULSE_OUT; -static void KLine_HandleCustomPacket(const uint8_t *body, uint8_t body_len) +static void KLine_HandleWriteRam(const uint8_t *body, uint8_t body_len) { if (body_len < sizeof(kActivateSyncOut)){ if (memcmp(body, kActivateSyncOut, sizeof(kActivateSyncOut)) == 0){ SYNC_PULSE_OUT = 1; SendAckPacket(); KLine_SessionKick(); + return; } } + + SendNakPacket(); /* ParsedPacket ack = (ParsedPacket){0}; if (ReceivePacket_Tmo(&ack, 200) && ack.title == PACKET_CMD_ACK) { diff --git a/Core/Src/ee_manager.c b/Core/Src/ee_manager.c index a2152ba..0722a6a 100644 --- a/Core/Src/ee_manager.c +++ b/Core/Src/ee_manager.c @@ -15,7 +15,7 @@ uint32_t quart_hour_counter = 0; typedef struct { uint8_t isCoded; - float dFi; //-128,127 -> *3/256 = dfi + int8_t dFi; //-128,127 -> *3/256 = dfi uint8_t inj_mode; uint32_t quart_hour_counter; @@ -31,14 +31,16 @@ void MEM_Init(){ MEM_WriteDefault(); } if(ee.isCoded == 255){ - MEM_WriteCustom(0.7, 0); + MEM_WriteCustom(0.0, 0); } } void MEM_RetrieveValues(){ if(!memInit){ MEM_Init(); } - dFi = ee.dFi; + s_dfi_code = ee.dFi; + dFi = (float)s_dfi_code * 3.0 / 256; + inj_mode = ee.inj_mode; quart_hour_counter = ee.quart_hour_counter; @@ -49,7 +51,7 @@ void MEM_Cleanup(){ //only after changes to the struct } void MEM_WriteAll(){ ee.isCoded = 1; - ee.dFi = dFi; + ee.dFi = s_dfi_code; ee.inj_mode = inj_mode; ee.quart_hour_counter = quart_hour_counter; @@ -58,11 +60,11 @@ void MEM_WriteAll(){ void MEM_WriteDefault(){ ee.isCoded = 1; - ee.dFi = 0.0; + ee.dFi = 0; ee.inj_mode = 0; EE_Write(); } -void MEM_WriteCustom(float c_dFi, uint8_t c_inj_mode){ +void MEM_WriteCustom(int8_t c_dFi, uint8_t c_inj_mode){ ee.isCoded = 1; ee.dFi = c_dFi; ee.inj_mode = c_inj_mode; @@ -72,3 +74,6 @@ void IncrementQuartHourCounter(){ quart_hour_counter++; memWrite = 1; } +int8_t GetDfiValue(){ + return ee.dFi; +}