Skip to content

Commit

Permalink
Merge pull request #110 from Jamezz/volt
Browse files Browse the repository at this point in the history
Add safety hook for ignition state for car platforms that don't support GPIO pin
  • Loading branch information
geohot committed Apr 12, 2018
2 parents 13e850e + 9f925ba commit af9af6d
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 2 deletions.
9 changes: 8 additions & 1 deletion board/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
9 changes: 9 additions & 0 deletions board/safety.h
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
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 int (*fwd_hook)(int bus_num, CAN_FIFOMailBox_TypeDef *to_fwd);

typedef struct {
safety_hook_init init;
ign_hook ignition;
rx_hook rx;
tx_hook tx;
tx_lin_hook tx_lin;
Expand Down Expand Up @@ -43,6 +46,12 @@ 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();
}
int safety_fwd_hook(int bus_num, CAN_FIFOMailBox_TypeDef *to_fwd) {
return current_hooks->fwd(bus_num, to_fwd);
}
Expand Down
9 changes: 9 additions & 0 deletions board/safety/safety_defaults.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ static int nooutput_tx_lin_hook(int lin_num, uint8_t *data, int len) {
return false;
}

static int nooutput_ign_hook() {
return -1;
}
static int nooutput_fwd_hook(int bus_num, CAN_FIFOMailBox_TypeDef *to_fwd) {
return -1;
}
Expand All @@ -23,6 +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,
.fwd = nooutput_fwd_hook,
};

Expand All @@ -40,6 +44,10 @@ static int alloutput_tx_lin_hook(int lin_num, uint8_t *data, int len) {
return true;
}

static int alloutput_ign_hook() {
return -1;
}

static int alloutput_fwd_hook(int bus_num, CAN_FIFOMailBox_TypeDef *to_fwd) {
return -1;
}
Expand All @@ -49,6 +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,
.fwd = alloutput_fwd_hook,
};

5 changes: 5 additions & 0 deletions board/safety/safety_elm327.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ static void elm327_init(int16_t param) {
controls_allowed = 1;
}

static int elm327_ign_hook() {
return -1;
}

static int elm327_fwd_hook(int bus_num, CAN_FIFOMailBox_TypeDef *to_fwd) {
return -1;
}
Expand All @@ -40,5 +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,
.fwd = elm327_fwd_hook,
};
15 changes: 14 additions & 1 deletion board/safety/safety_gm.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Expand Down Expand Up @@ -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;
}

static int gm_fwd_hook(int bus_num, CAN_FIFOMailBox_TypeDef *to_fwd) {
Expand All @@ -181,6 +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,
.fwd = gm_fwd_hook,
};

6 changes: 6 additions & 0 deletions board/safety/safety_honda.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,16 @@ static int honda_fwd_hook(int bus_num, CAN_FIFOMailBox_TypeDef *to_fwd) {
return -1;
}

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,
.fwd = honda_fwd_hook,
};

Expand All @@ -160,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,
};
6 changes: 6 additions & 0 deletions board/safety/safety_toyota.h
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,10 @@ static void toyota_init(int16_t param) {
dbc_eps_torque_factor = param;
}

static int toyota_ign_hook() {
return -1;
}

static int toyota_fwd_hook(int bus_num, CAN_FIFOMailBox_TypeDef *to_fwd) {
return -1;
}
Expand All @@ -188,6 +192,7 @@ const safety_hooks toyota_hooks = {
.rx = toyota_rx_hook,
.tx = toyota_tx_hook,
.tx_lin = toyota_tx_lin_hook,
.ignition = toyota_ign_hook,
.fwd = toyota_fwd_hook,
};

Expand All @@ -202,5 +207,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,
.fwd = toyota_fwd_hook,
};

0 comments on commit af9af6d

Please sign in to comment.