From 4fc83a5f5c9419037fbed51fa58c209f029da6bd Mon Sep 17 00:00:00 2001 From: Thomas Helms Date: Tue, 10 Apr 2018 17:55:55 -0700 Subject: [PATCH 1/3] Add safety hook for ignition and have GM use gear selector to determine ignition --- board/main.c | 13 ++++++++++--- board/safety.h | 10 ++++++++++ board/safety/safety_defaults.h | 10 ++++++++++ board/safety/safety_elm327.h | 5 +++++ board/safety/safety_gm.h | 15 ++++++++++++++- board/safety/safety_honda.h | 5 +++++ board/safety/safety_toyota.h | 6 ++++++ 7 files changed, 60 insertions(+), 4 deletions(-) diff --git a/board/main.c b/board/main.c index e46a38856317e8..d633ccdf68e9d0 100644 --- a/board/main.c +++ b/board/main.c @@ -104,7 +104,14 @@ int get_health_pkt(void *dat) { #ifdef PANDA health->current = adc_get(ADCCHAN_CURRENT); - health->started = (GPIOA->IDR & (1 << 1)) == 0; + int safety_ignition = safety_ignition_hook(); + if (safety_ignition < 0) { + //Use the GPIO pin to determine ignition + health->started = (GPIOA->IDR & (1 << 1)) == 0; + } else { + //Current safety hooks want to determine ignition (ex: GM) + health-> started = safety_ignition; + } #else health->current = 0; health->started = (GPIOC->IDR & (1 << 13)) != 0; @@ -534,8 +541,8 @@ int main() { usb_init(); // default to silent mode to prevent issues with Ford - safety_set_mode(SAFETY_NOOUTPUT, 0); - can_silent = ALL_CAN_SILENT; + safety_set_mode(SAFETY_GM, 0); + can_silent = ALL_CAN_LIVE; can_init_all(); adc_init(); diff --git a/board/safety.h b/board/safety.h index dc824ba53c09f0..ff249235e026bc 100644 --- a/board/safety.h +++ b/board/safety.h @@ -1,14 +1,17 @@ void safety_rx_hook(CAN_FIFOMailBox_TypeDef *to_push); int safety_tx_hook(CAN_FIFOMailBox_TypeDef *to_send); int safety_tx_lin_hook(int lin_num, uint8_t *data, int len); +int safety_ignition_hook(); typedef void (*safety_hook_init)(int16_t param); typedef void (*rx_hook)(CAN_FIFOMailBox_TypeDef *to_push); typedef int (*tx_hook)(CAN_FIFOMailBox_TypeDef *to_send); typedef int (*tx_lin_hook)(int lin_num, uint8_t *data, int len); +typedef int (*ign_hook)(); typedef struct { safety_hook_init init; + ign_hook ignition; rx_hook rx; tx_hook tx; tx_lin_hook tx_lin; @@ -38,6 +41,13 @@ int safety_tx_lin_hook(int lin_num, uint8_t *data, int len){ return current_hooks->tx_lin(lin_num, data, len); } +// -1 = Disabled (Use GPIO to determine ignition) +// 0 = Off (not started) +// 1 = On (started) +int safety_ignition_hook() { + return current_hooks->ignition(); +} + typedef struct { uint16_t id; const safety_hooks *hooks; diff --git a/board/safety/safety_defaults.h b/board/safety/safety_defaults.h index b7b4d37295c9c9..d8433c7aa3a6b5 100644 --- a/board/safety/safety_defaults.h +++ b/board/safety/safety_defaults.h @@ -14,11 +14,16 @@ static int nooutput_tx_lin_hook(int lin_num, uint8_t *data, int len) { return false; } +static int nooutput_ign_hook() { + return -1; +} + const safety_hooks nooutput_hooks = { .init = nooutput_init, .rx = default_rx_hook, .tx = nooutput_tx_hook, .tx_lin = nooutput_tx_lin_hook, + .ignition = nooutput_ign_hook }; // *** all output safety mode *** @@ -35,10 +40,15 @@ static int alloutput_tx_lin_hook(int lin_num, uint8_t *data, int len) { return true; } +static int alloutput_ign_hook() { + return -1; +} + const safety_hooks alloutput_hooks = { .init = alloutput_init, .rx = default_rx_hook, .tx = alloutput_tx_hook, .tx_lin = alloutput_tx_lin_hook, + .ignition = alloutput_ign_hook }; diff --git a/board/safety/safety_elm327.h b/board/safety/safety_elm327.h index b9af35ebdc5016..a284af498f394f 100644 --- a/board/safety/safety_elm327.h +++ b/board/safety/safety_elm327.h @@ -31,9 +31,14 @@ static void elm327_init(int16_t param) { controls_allowed = 1; } +static int elm327_ign_hook() { + return -1; +} + const safety_hooks elm327_hooks = { .init = elm327_init, .rx = elm327_rx_hook, .tx = elm327_tx_hook, .tx_lin = elm327_tx_lin_hook, + .ignition = elm327_ign_hook }; diff --git a/board/safety/safety_gm.h b/board/safety/safety_gm.h index 50671d950d416a..042c02738a6d1b 100644 --- a/board/safety/safety_gm.h +++ b/board/safety/safety_gm.h @@ -16,8 +16,9 @@ int gm_speed = 0; // silence everything if stock ECUs are still online int gm_ascm_detected = 0; -static void gm_rx_hook(CAN_FIFOMailBox_TypeDef *to_push) { +int gm_ignition_started = 0; +static void gm_rx_hook(CAN_FIFOMailBox_TypeDef *to_push) { uint32_t addr; if (to_push->RIR & 4) { // Extended @@ -29,6 +30,12 @@ static void gm_rx_hook(CAN_FIFOMailBox_TypeDef *to_push) { addr = to_push->RIR >> 21; } + if (addr == 0x135) { + //Gear selector (used for determining ignition) + int gear = to_push->RDLR & 0x7; + gm_ignition_started = gear > 0; //Park = 0. If out of park, we're "on." + } + // sample speed, really only care if car is moving or not // rear left wheel speed if (addr == 842) { @@ -170,6 +177,11 @@ static int gm_tx_lin_hook(int lin_num, uint8_t *data, int len) { static void gm_init(int16_t param) { controls_allowed = 0; + gm_ignition_started = 0; +} + +static int gm_ign_hook() { + return gm_ignition_started; } const safety_hooks gm_hooks = { @@ -177,5 +189,6 @@ const safety_hooks gm_hooks = { .rx = gm_rx_hook, .tx = gm_tx_hook, .tx_lin = gm_tx_lin_hook, + .ignition = gm_ign_hook }; diff --git a/board/safety/safety_honda.h b/board/safety/safety_honda.h index bf30d847079e09..b7c24b1e9b8138 100644 --- a/board/safety/safety_honda.h +++ b/board/safety/safety_honda.h @@ -119,10 +119,15 @@ static void honda_init(int16_t param) { controls_allowed = 0; } +static int honda_ign_hook() { + return -1; +} + const safety_hooks honda_hooks = { .init = honda_init, .rx = honda_rx_hook, .tx = honda_tx_hook, .tx_lin = honda_tx_lin_hook, + .ignition = honda_ign_hook }; diff --git a/board/safety/safety_toyota.h b/board/safety/safety_toyota.h index 9ddaf172bb47ee..8fa9ce874bad13 100644 --- a/board/safety/safety_toyota.h +++ b/board/safety/safety_toyota.h @@ -161,11 +161,16 @@ static void toyota_init(int16_t param) { dbc_eps_torque_factor = param; } +static int toyota_ign_hook() { + return -1; +} + const safety_hooks toyota_hooks = { .init = toyota_init, .rx = toyota_rx_hook, .tx = toyota_tx_hook, .tx_lin = toyota_tx_lin_hook, + .ignition = toyota_ign_hook }; static void toyota_nolimits_init(int16_t param) { @@ -179,4 +184,5 @@ const safety_hooks toyota_nolimits_hooks = { .rx = toyota_rx_hook, .tx = toyota_tx_hook, .tx_lin = toyota_tx_lin_hook, + .ignition = toyota_ign_hook }; From a0cc51af15458f9e3ef8e23038f4f5954f57a5f6 Mon Sep 17 00:00:00 2001 From: Jamezz Date: Wed, 11 Apr 2018 12:54:16 -0700 Subject: [PATCH 2/3] Undo safety mode override We don't need to force safety to GM for testing, fingerprinting should work. --- board/main.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/board/main.c b/board/main.c index d633ccdf68e9d0..7fdfb50790049d 100644 --- a/board/main.c +++ b/board/main.c @@ -541,8 +541,8 @@ int main() { usb_init(); // default to silent mode to prevent issues with Ford - safety_set_mode(SAFETY_GM, 0); - can_silent = ALL_CAN_LIVE; + safety_set_mode(SAFETY_NOOUTPUT, 0); + can_silent = ALL_CAN_SILENT; can_init_all(); adc_init(); From 9f925ba9d2c4625a086e9fc90be233d7a7b32793 Mon Sep 17 00:00:00 2001 From: Thomas Helms Date: Wed, 11 Apr 2018 13:13:05 -0700 Subject: [PATCH 3/3] Fix the merge mess --- board/safety/safety_defaults.h | 6 +++--- board/safety/safety_elm327.h | 2 +- board/safety/safety_gm.h | 2 +- board/safety/safety_honda.h | 3 ++- board/safety/safety_toyota.h | 4 ++-- 5 files changed, 9 insertions(+), 8 deletions(-) diff --git a/board/safety/safety_defaults.h b/board/safety/safety_defaults.h index ad504682adcc00..16ab3643f83af2 100644 --- a/board/safety/safety_defaults.h +++ b/board/safety/safety_defaults.h @@ -26,7 +26,7 @@ const safety_hooks nooutput_hooks = { .rx = default_rx_hook, .tx = nooutput_tx_hook, .tx_lin = nooutput_tx_lin_hook, - .ignition = nooutput_ign_hook + .ignition = nooutput_ign_hook, .fwd = nooutput_fwd_hook, }; @@ -45,7 +45,7 @@ static int alloutput_tx_lin_hook(int lin_num, uint8_t *data, int len) { } static int alloutput_ign_hook() { - return -1 + return -1; } static int alloutput_fwd_hook(int bus_num, CAN_FIFOMailBox_TypeDef *to_fwd) { @@ -57,7 +57,7 @@ const safety_hooks alloutput_hooks = { .rx = default_rx_hook, .tx = alloutput_tx_hook, .tx_lin = alloutput_tx_lin_hook, - .ignition = alloutput_ign_hook + .ignition = alloutput_ign_hook, .fwd = alloutput_fwd_hook, }; diff --git a/board/safety/safety_elm327.h b/board/safety/safety_elm327.h index 043264f57607fe..0b23fa5dac18e2 100644 --- a/board/safety/safety_elm327.h +++ b/board/safety/safety_elm327.h @@ -44,6 +44,6 @@ const safety_hooks elm327_hooks = { .rx = elm327_rx_hook, .tx = elm327_tx_hook, .tx_lin = elm327_tx_lin_hook, - .ignition = elm327_ign_hook + .ignition = elm327_ign_hook, .fwd = elm327_fwd_hook, }; diff --git a/board/safety/safety_gm.h b/board/safety/safety_gm.h index 771b6f860c1ee8..40fc14d54c8536 100644 --- a/board/safety/safety_gm.h +++ b/board/safety/safety_gm.h @@ -193,7 +193,7 @@ const safety_hooks gm_hooks = { .rx = gm_rx_hook, .tx = gm_tx_hook, .tx_lin = gm_tx_lin_hook, - .ignition = gm_ign_hook + .ignition = gm_ign_hook, .fwd = gm_fwd_hook, }; diff --git a/board/safety/safety_honda.h b/board/safety/safety_honda.h index 798f3934204fa9..bc6ce6e9c08030 100644 --- a/board/safety/safety_honda.h +++ b/board/safety/safety_honda.h @@ -143,7 +143,7 @@ const safety_hooks honda_hooks = { .rx = honda_rx_hook, .tx = honda_tx_hook, .tx_lin = honda_tx_lin_hook, - .ignition = honda_ign_hook + .ignition = honda_ign_hook, .fwd = honda_fwd_hook, }; @@ -165,5 +165,6 @@ const safety_hooks honda_bosch_hooks = { .rx = honda_rx_hook, .tx = honda_tx_hook, .tx_lin = honda_tx_lin_hook, + .ignition = honda_ign_hook, .fwd = honda_bosch_fwd_hook, }; diff --git a/board/safety/safety_toyota.h b/board/safety/safety_toyota.h index ef3e79da64210e..2a9e3eabf38fec 100644 --- a/board/safety/safety_toyota.h +++ b/board/safety/safety_toyota.h @@ -220,7 +220,7 @@ const safety_hooks toyota_hooks = { .rx = toyota_rx_hook, .tx = toyota_tx_hook, .tx_lin = toyota_tx_lin_hook, - .ignition = toyota_ign_hook + .ignition = toyota_ign_hook, .fwd = toyota_fwd_hook, }; @@ -235,6 +235,6 @@ const safety_hooks toyota_nolimits_hooks = { .rx = toyota_rx_hook, .tx = toyota_tx_hook, .tx_lin = toyota_tx_lin_hook, - .ignition = toyota_ign_hook + .ignition = toyota_ign_hook, .fwd = toyota_fwd_hook, };