Skip to content

Commit

Permalink
make ignition logic common for all cars (commaai#303)
Browse files Browse the repository at this point in the history
* make ignition logic common for all cars

* ignition_can is not a safety thing. move it to can drivers

* bump version

* gm safety passive is deprecated

* move tx 1 on logic into board specific headers.
  • Loading branch information
rbiasini authored Oct 22, 2019
1 parent 3a110c6 commit 1102e69
Show file tree
Hide file tree
Showing 24 changed files with 47 additions and 126 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v1.5.4
v1.5.5
3 changes: 2 additions & 1 deletion board/boards/black.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ void black_enable_can_transciever(uint8_t transciever, bool enabled) {
}

void black_enable_can_transcievers(bool enabled) {
for(uint8_t i=1U; i<=4U; i++){
uint8_t t1 = enabled ? 1U : 2U; // leave transciever 1 enabled to detect CAN ignition
for(uint8_t i=t1; i<=4U; i++) {
black_enable_can_transciever(i, enabled);
}
}
Expand Down
4 changes: 3 additions & 1 deletion board/boards/white.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ void white_enable_can_transciever(uint8_t transciever, bool enabled) {
}

void white_enable_can_transcievers(bool enabled) {
for(uint8_t i=1; i<=3U; i++)
uint8_t t1 = enabled ? 1U : 2U; // leave transciever 1 enabled to detect CAN ignition
for(uint8_t i=t1; i<=3U; i++) {
white_enable_can_transciever(i, enabled);
}
}

void white_set_led(uint8_t color, bool enabled) {
Expand Down
36 changes: 36 additions & 0 deletions board/drivers/can.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ void can_init_all(void);
void can_send(CAN_FIFOMailBox_TypeDef *to_push, uint8_t bus_number);
bool can_pop(can_ring *q, CAN_FIFOMailBox_TypeDef *elem);

// Ignition detected from CAN meessages
bool ignition_can = false;

// end API

#define ALL_CAN_SILENT 0xFF
Expand Down Expand Up @@ -332,6 +335,38 @@ void process_can(uint8_t can_number) {
}
}

void ignition_can_hook(CAN_FIFOMailBox_TypeDef *to_push) {

int bus = GET_BUS(to_push);
int addr = GET_ADDR(to_push);
int len = GET_LEN(to_push);

if (bus == 0) {
// GM exception
if ((addr == 0x1F1) && (len == 8)) {
//Bit 5 is ignition "on"
ignition_can = (GET_BYTE(to_push, 0) & 0x20) != 0;
}
// Tesla exception
if ((addr == 0x348) && (len == 8)) {
// GTW_status
ignition_can = (GET_BYTE(to_push, 0) & 0x1) != 0;
}
// Cadillac exception
if ((addr == 0x160) && (len == 5)) {
// this message isn't all zeros when ignition is on
ignition_can = GET_BYTES_04(to_push) != 0;
}
// VW exception (not used yet)
// While we do monitor VW Terminal 15 (ignition-on) state, we are not currently acting on it. Instead we use the
// default GPIO ignition hook. We may do so in the future for harness integrations at the camera (where we only have
// T30 unswitched power) instead of the gateway (where we have both T30 and T15 ignition-switched power).
//if ((bus == 0) && (addr == 0x3C0)) {
// vw_ignition_started = (GET_BYTE(to_push, 2) & 0x2) >> 1;
//}
}
}

// CAN receive handlers
// blink blue when we are receiving CAN messages
void can_rx(uint8_t can_number) {
Expand Down Expand Up @@ -365,6 +400,7 @@ void can_rx(uint8_t can_number) {
}

safety_rx_hook(&to_push);
ignition_can_hook(&to_push);

current_board->set_led(LED_BLUE, true);
can_send_errs += can_push(&can_rx_q, &to_push) ? 0U : 1U;
Expand Down
18 changes: 3 additions & 15 deletions board/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -139,13 +139,6 @@ void set_safety_mode(uint16_t mode, int16_t param) {
can_silent = ALL_CAN_LIVE;
break;
}
if (safety_ignition_hook() != -1) {
// if the ignition hook depends on something other than the started GPIO
// we have to disable power savings (fix for GM and Tesla)
set_power_save_state(POWER_SAVE_STATUS_DISABLED);
} else {
// power mode is already POWER_SAVE_STATUS_DISABLED and CAN TXs are active
}
can_init_all();
}
}
Expand Down Expand Up @@ -175,14 +168,9 @@ int get_health_pkt(void *dat) {
health->current_pkt = 0;
}

int safety_ignition = safety_ignition_hook();
if (safety_ignition < 0) {
//Use the GPIO pin to determine ignition
health->started_pkt = (uint8_t)(current_board->check_ignition());
} else {
//Current safety hooks want to determine ignition (ex: GM)
health->started_pkt = safety_ignition;
}
//Use the GPIO pin to determine ignition or use a CAN based logic
bool ignition = current_board->check_ignition() || ignition_can;
health->started_pkt = (uint8_t)(ignition);

health->controls_allowed_pkt = controls_allowed;
health->gas_interceptor_detected_pkt = gas_interceptor_detected;
Expand Down
5 changes: 2 additions & 3 deletions board/power_saving.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ void set_power_save_state(int state) {
enable = true;
}

// Switch CAN transcievers
current_board->enable_can_transcievers(enable);

// Switch EPS/GPS
Expand All @@ -34,13 +33,13 @@ void set_power_save_state(int state) {
} else {
current_board->set_esp_gps_mode(ESP_GPS_DISABLED);
}

if(hw_type != HW_TYPE_BLACK_PANDA){
// turn on GMLAN
set_gpio_output(GPIOB, 14, enable);
set_gpio_output(GPIOB, 15, enable);

// turn on LIN
// turn on LIN
set_gpio_output(GPIOB, 7, enable);
set_gpio_output(GPIOA, 14, enable);
}
Expand Down
8 changes: 0 additions & 8 deletions board/safety.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
#define SAFETY_CHRYSLER 9U
#define SAFETY_TESLA 10U
#define SAFETY_SUBARU 11U
#define SAFETY_GM_PASSIVE 12U
#define SAFETY_MAZDA 13U
#define SAFETY_VOLKSWAGEN 15U
#define SAFETY_TOYOTA_IPAS 16U
Expand All @@ -52,12 +51,6 @@ 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 All @@ -80,7 +73,6 @@ const safety_hook_config safety_hook_registry[] = {
{SAFETY_CHRYSLER, &chrysler_hooks},
{SAFETY_TESLA, &tesla_hooks},
{SAFETY_SUBARU, &subaru_hooks},
{SAFETY_GM_PASSIVE, &gm_passive_hooks},
{SAFETY_MAZDA, &mazda_hooks},
{SAFETY_VOLKSWAGEN, &volkswagen_hooks},
{SAFETY_TOYOTA_IPAS, &toyota_ipas_hooks},
Expand Down
12 changes: 0 additions & 12 deletions board/safety/safety_cadillac.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ const int CADILLAC_MAX_RATE_DOWN = 5;
const int CADILLAC_DRIVER_TORQUE_ALLOWANCE = 50;
const int CADILLAC_DRIVER_TORQUE_FACTOR = 4;

bool cadillac_ign = 0;
int cadillac_cruise_engaged_last = 0;
int cadillac_rt_torque_last = 0;
const int cadillac_torque_msgs_n = 4;
Expand All @@ -35,11 +34,6 @@ static void cadillac_rx_hook(CAN_FIFOMailBox_TypeDef *to_push) {
update_sample(&cadillac_torque_driver, torque_driver_new);
}

// this message isn't all zeros when ignition is on
if ((addr == 0x160) && (bus == 0)) {
cadillac_ign = GET_BYTES_04(to_push) != 0;
}

// enter controls on rising edge of ACC, exit controls on ACC off
if ((addr == 0x370) && (bus == 0)) {
int cruise_engaged = GET_BYTE(to_push, 2) & 0x80; // bit 23
Expand Down Expand Up @@ -118,18 +112,12 @@ static int cadillac_tx_hook(CAN_FIFOMailBox_TypeDef *to_send) {
static void cadillac_init(int16_t param) {
UNUSED(param);
controls_allowed = 0;
cadillac_ign = 0;
}

static int cadillac_ign_hook(void) {
return cadillac_ign;
}

const safety_hooks cadillac_hooks = {
.init = cadillac_init,
.rx = cadillac_rx_hook,
.tx = cadillac_tx_hook,
.tx_lin = nooutput_tx_lin_hook,
.ignition = cadillac_ign_hook,
.fwd = default_fwd_hook,
};
1 change: 0 additions & 1 deletion board/safety/safety_chrysler.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,5 @@ const safety_hooks chrysler_hooks = {
.rx = chrysler_rx_hook,
.tx = chrysler_tx_hook,
.tx_lin = nooutput_tx_lin_hook,
.ignition = default_ign_hook,
.fwd = chrysler_fwd_hook,
};
6 changes: 0 additions & 6 deletions board/safety/safety_defaults.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@ void default_rx_hook(CAN_FIFOMailBox_TypeDef *to_push) {
UNUSED(to_push);
}

int default_ign_hook(void) {
return -1; // use GPIO to determine ignition
}

// *** no output safety mode ***

static void nooutput_init(int16_t param) {
Expand Down Expand Up @@ -36,7 +32,6 @@ const safety_hooks nooutput_hooks = {
.rx = default_rx_hook,
.tx = nooutput_tx_hook,
.tx_lin = nooutput_tx_lin_hook,
.ignition = default_ign_hook,
.fwd = default_fwd_hook,
};

Expand Down Expand Up @@ -64,6 +59,5 @@ const safety_hooks alloutput_hooks = {
.rx = default_rx_hook,
.tx = alloutput_tx_hook,
.tx_lin = alloutput_tx_lin_hook,
.ignition = default_ign_hook,
.fwd = default_fwd_hook,
};
1 change: 0 additions & 1 deletion board/safety/safety_elm327.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,5 @@ const safety_hooks elm327_hooks = {
.rx = default_rx_hook,
.tx = elm327_tx_hook,
.tx_lin = elm327_tx_lin_hook,
.ignition = default_ign_hook,
.fwd = default_fwd_hook,
};
1 change: 0 additions & 1 deletion board/safety/safety_ford.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,5 @@ const safety_hooks ford_hooks = {
.rx = ford_rx_hook,
.tx = ford_tx_hook,
.tx_lin = nooutput_tx_lin_hook,
.ignition = default_ign_hook,
.fwd = default_fwd_hook,
};
36 changes: 0 additions & 36 deletions board/safety/safety_gm.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ int gm_gas_prev = 0;
bool gm_moving = false;
// silence everything if stock car control ECUs are still online
bool gm_ascm_detected = 0;
bool gm_ignition_started = 0;
int gm_rt_torque_last = 0;
int gm_desired_torque_last = 0;
uint32_t gm_ts_last = 0;
Expand All @@ -41,13 +40,6 @@ static void gm_rx_hook(CAN_FIFOMailBox_TypeDef *to_push) {
update_sample(&gm_torque_driver, torque_driver_new);
}

if ((addr == 0x1F1) && (bus_number == 0)) {
//Bit 5 should be ignition "on"
//Backup plan is Bit 2 (accessory power)
bool ign = (GET_BYTE(to_push, 0) & 0x20) != 0;
gm_ignition_started = ign;
}

// sample speed, really only care if car is moving or not
// rear left wheel speed
if (addr == 842) {
Expand Down Expand Up @@ -224,41 +216,13 @@ static int gm_tx_hook(CAN_FIFOMailBox_TypeDef *to_send) {
static void gm_init(int16_t param) {
UNUSED(param);
controls_allowed = 0;
gm_ignition_started = 0;
}

static int gm_ign_hook(void) {
return gm_ignition_started;
}

// All sending is disallowed.
// The only difference from "no output" model
// is using GM ignition hook.

static void gm_passive_rx_hook(CAN_FIFOMailBox_TypeDef *to_push) {
int bus_number = GET_BUS(to_push);
int addr = GET_ADDR(to_push);

if ((addr == 0x1F1) && (bus_number == 0)) {
bool ign = (GET_BYTE(to_push, 0) & 0x20) != 0;
gm_ignition_started = ign;
}
}

const safety_hooks gm_hooks = {
.init = gm_init,
.rx = gm_rx_hook,
.tx = gm_tx_hook,
.tx_lin = nooutput_tx_lin_hook,
.ignition = gm_ign_hook,
.fwd = default_fwd_hook,
};

const safety_hooks gm_passive_hooks = {
.init = gm_init,
.rx = gm_passive_rx_hook,
.tx = nooutput_tx_hook,
.tx_lin = nooutput_tx_lin_hook,
.ignition = gm_ign_hook,
.fwd = default_fwd_hook,
};
1 change: 0 additions & 1 deletion board/safety/safety_gm_ascm.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ const safety_hooks gm_ascm_hooks = {
.rx = default_rx_hook,
.tx = alloutput_tx_hook,
.tx_lin = nooutput_tx_lin_hook,
.ignition = default_ign_hook,
.fwd = gm_ascm_fwd_hook,
};

2 changes: 0 additions & 2 deletions board/safety/safety_honda.h
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,6 @@ const safety_hooks honda_hooks = {
.rx = honda_rx_hook,
.tx = honda_tx_hook,
.tx_lin = nooutput_tx_lin_hook,
.ignition = default_ign_hook,
.fwd = honda_fwd_hook,
};

Expand All @@ -240,6 +239,5 @@ const safety_hooks honda_bosch_hooks = {
.rx = honda_rx_hook,
.tx = honda_tx_hook,
.tx_lin = nooutput_tx_lin_hook,
.ignition = default_ign_hook,
.fwd = honda_bosch_fwd_hook,
};
1 change: 0 additions & 1 deletion board/safety/safety_hyundai.h
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,5 @@ const safety_hooks hyundai_hooks = {
.rx = hyundai_rx_hook,
.tx = hyundai_tx_hook,
.tx_lin = nooutput_tx_lin_hook,
.ignition = default_ign_hook,
.fwd = hyundai_fwd_hook,
};
1 change: 0 additions & 1 deletion board/safety/safety_mazda.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,5 @@ const safety_hooks mazda_hooks = {
.rx = mazda_rx_hook,
.tx = mazda_tx_hook,
.tx_lin = nooutput_tx_lin_hook,
.ignition = default_ign_hook,
.fwd = mazda_fwd_hook,
};
1 change: 0 additions & 1 deletion board/safety/safety_subaru.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,5 @@ const safety_hooks subaru_hooks = {
.rx = subaru_rx_hook,
.tx = subaru_tx_hook,
.tx_lin = nooutput_tx_lin_hook,
.ignition = default_ign_hook,
.fwd = subaru_fwd_hook,
};
Loading

0 comments on commit 1102e69

Please sign in to comment.