Skip to content

Commit

Permalink
Add safety hook for ignition and have GM use gear selector to determi…
Browse files Browse the repository at this point in the history
…ne ignition
  • Loading branch information
Jamezz committed Apr 11, 2018
1 parent 2434f1c commit 4fc83a5
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 4 deletions.
13 changes: 10 additions & 3 deletions 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 Expand Up @@ -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();
Expand Down
10 changes: 10 additions & 0 deletions board/safety.h
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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;
Expand Down
10 changes: 10 additions & 0 deletions board/safety/safety_defaults.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 ***
Expand All @@ -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
};

5 changes: 5 additions & 0 deletions board/safety/safety_elm327.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
};
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,12 +177,18 @@ 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 = {
.init = gm_init,
.rx = gm_rx_hook,
.tx = gm_tx_hook,
.tx_lin = gm_tx_lin_hook,
.ignition = gm_ign_hook
};

5 changes: 5 additions & 0 deletions board/safety/safety_honda.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
};

6 changes: 6 additions & 0 deletions board/safety/safety_toyota.h
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
};

0 comments on commit 4fc83a5

Please sign in to comment.