From 63af60c1b0b0047ba6f1a40ea813ae9b958e32f0 Mon Sep 17 00:00:00 2001 From: Jessy Diamond Exum Date: Wed, 16 Aug 2017 17:39:46 -0700 Subject: [PATCH] ELM327: Added ISO 14230-4 (KWP FAST) wakeup pulse. --- boardesp/elm327.c | 21 ++++++++++++++++----- tests/elm_car_simulator.py | 5 +++-- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/boardesp/elm327.c b/boardesp/elm327.c index 60e07d839..5deaabed3 100644 --- a/boardesp/elm327.c +++ b/boardesp/elm327.c @@ -212,6 +212,7 @@ static int ICACHE_FLASH_ATTR panda_usbemu_ctrl_write(uint8_t request_type, uint8 #define panda_set_can0_cbaud(cbps) panda_usbemu_ctrl_write(0x40, 0xde, 0, cbps, 0) #define panda_set_can0_kbaud(kbps) panda_usbemu_ctrl_write(0x40, 0xde, 0, kbps*10, 0) #define panda_set_safety_mode(mode) panda_usbemu_ctrl_write(0x40, 0xdc, mode, 0, 0) +#define panda_kline_wakeup_pulse() panda_usbemu_ctrl_write(0x40, 0xf0, 0, 0, 0) static int ICACHE_FLASH_ATTR panda_usbemu_can_read(panda_can_msg_t** can_msgs) { int returned_count = spi_comm((uint8_t *)((const uint16 []){1,0}), 4, pandaRecvData, 0x40); @@ -532,7 +533,7 @@ int ICACHE_FLASH_ATTR elm_LINFast_process_echo() { return 1; } else { #ifdef ELM_DEBUG - os_printf("Somehow failed\n"); + os_printf("Echo not correct.\n"); os_printf(" RB Data (%d %d %d): ", lin_ringbuff_start, lin_ringbuff_end, lin_ringbuff_len); for(int i = 0; i < sizeof(lin_ringbuff); i++) os_printf("%02x ", lin_ringbuff[i]); @@ -543,8 +544,14 @@ int ICACHE_FLASH_ATTR elm_LINFast_process_echo() { os_printf("\n"); #endif - lin_ringbuff_clear(); - return -1; + if(lin_bus_initialized) { + lin_ringbuff_clear(); + return -1; + } else { + os_printf("Lin init echo misaligned? Consuming byte (%02x). Retry.\n", lin_ringbuff_get(0)); + lin_ringbuff_consume(1); + continue; + } } } } @@ -658,6 +665,10 @@ void ICACHE_FLASH_ATTR elm_LINFast_businit_timer_cb(void *arg){ if(echo_result == -1 || (echo_result == 0 && loopcount == 0)) { os_printf("Init failed with echo test\n"); + + loopcount = 0; + lin_bus_initialized = 0; + if(!is_auto_detecting){ if(echo_result == -1) elm_append_rsp_const("BUS ERROR\r\r>"); @@ -667,8 +678,6 @@ void ICACHE_FLASH_ATTR elm_LINFast_businit_timer_cb(void *arg){ } else { elm_autodetect_cb(false); } - loopcount = 0; - lin_bus_initialized = 0; return; } @@ -785,6 +794,8 @@ static void ICACHE_FLASH_ATTR elm_process_obd_cmd_LINFast(const elm_protocol_t* bytelen = 1; msg.dat[0] = 0x81; msg.dat[1] = 0x81; // checksum + + panda_kline_wakeup_pulse(); } else { bytelen = min(bytelen, 7); for(int i = 0; i < bytelen; i++){ diff --git a/tests/elm_car_simulator.py b/tests/elm_car_simulator.py index 83b33123e..ad22c5b63 100755 --- a/tests/elm_car_simulator.py +++ b/tests/elm_car_simulator.py @@ -84,7 +84,7 @@ class ELMCarSimulator(): continue lin_buff += lin_msg - if lin_buff.endswith(b'\xc1\x33\xf1\x81\x66'): + if lin_buff.endswith(b'\x00\xc1\x33\xf1\x81\x66'): # Leading 0 is wakeup lin_buff = bytearray() self.__lin_active = True print("GOT LIN (KWP FAST) WAKEUP SIGNAL") @@ -111,7 +111,7 @@ class ELMCarSimulator(): RECV = 0xF1 SEND = 0x33 # Car OBD Functional Address headers = struct.pack("BBB", PHYS_ADDR | len(msg), RECV, to_addr) - print(" Sending LIN", binascii.hexlify(headers+msg)) + print(" Sending LIN", binascii.hexlify(headers+msg), hex(sum(bytearray(headers+msg))%0x100)) self.panda.kline_send(headers + msg) def __reset_lin_timeout(self): @@ -165,6 +165,7 @@ class ELMCarSimulator(): while not self.__stop: for address, ts, data, src in self.panda.can_recv(): if self.__on and src is 0 and len(data) == 8 and data[0] >= 2: + print("Processing CAN message", src, hex(address), binascii.hexlify(data)) self.__can_process_msg(data[1], data[2], address, ts, data, src) else: print("Rejecting CAN message", src, hex(address), binascii.hexlify(data))