Skip to content

Commit cd8cc37

Browse files
author
Vehicle Researcher
committed
Squashed 'panda/' changes from 769ade0..0696730
0696730 Toyota: add missing offset from speed signal (#469) 5b1a8dc Filtering LKAS HUD messages (#468) 99050f4 test_spam_can_buses was missing from Nissan 0f21b19 Cleanup pedal nomenclature (#467) ceff91d Standardize brake safety tests (#465) 04809e1 Hyundai brake check (#464) 74c8ee0 Subaru brake check (#463) 4ecc6b3 Cleanup: avoid unnecessary bus checks in rx hooks c7d0d5f Volkswagen safety updates: Phase 3 (#462) 4368748 WIP: Toyota brake check. (#459) 2ef996f fix addr frequencies e063b26 Second test fix tentative 88e2593 This should fix the test replay ebb8866 Added NISSAN replay test b2dbb50 remove toyota ipas safety code and tests (#460) a379faf White Panda's Wi-Fi setup instructions (#457) 11ef24b Improve tests (#456) fb02390 Subaru checksum counter (#455) 9a44499 Fix Subaru Legacy Torque driver bug (#454) dfa6b07 separating subary legacy safety mode from global (#452) dad5858 Chrysler: add brakepress cancellation (#451) db94a5b Added Nissan safety (#244) d7f1195 Chrysler Checksum/counter (#450) 96e535e abstract crc function (#448) 1b49d3e Hyundai: add gas disengage and tests (#447) 598074c Volkswagen safety updates: Phase 2 (#445) b2ffaae Chrysler: disengage on gas press (#442) 2ebbe36 Subaru: disengage on gas press (#446) ccf75c4 Volkswagen safety updates: Phase 1 (#444) git-subtree-dir: panda git-subtree-split: 0696730
1 parent 6b8726e commit cd8cc37

37 files changed

+1878
-1140
lines changed

board/config.h

+4
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@
3636
__typeof__ (b) _b = (b); \
3737
(_a > _b) ? _a : _b; })
3838

39+
#define ABS(a) \
40+
({ __typeof__ (a) _a = (a); \
41+
(_a > 0) ? _a : (-_a); })
42+
3943
#define MAX_RESP_LEN 0x40U
4044

4145
// Around (1Mbps / 8 bits/byte / 12 bytes per message)

board/crc.h

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
uint8_t crc_checksum(uint8_t *dat, int len, const uint8_t poly) {
2+
uint8_t crc = 0xFF;
3+
int i, j;
4+
for (i = len - 1; i >= 0; i--) {
5+
crc ^= dat[i];
6+
for (j = 0; j < 8; j++) {
7+
if ((crc & 0x80U) != 0U) {
8+
crc = (uint8_t)((crc << 1) ^ poly);
9+
}
10+
else {
11+
crc <<= 1;
12+
}
13+
}
14+
}
15+
return crc;
16+
}

board/drivers/llcan.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ bool llcan_set_speed(CAN_TypeDef *CAN_obj, uint32_t speed, bool loopback, bool s
5252
void llcan_init(CAN_TypeDef *CAN_obj) {
5353
// Enter init mode
5454
register_set_bits(&(CAN_obj->FMR), CAN_FMR_FINIT);
55-
55+
5656
// Wait for INAK bit to be set
5757
while(((CAN_obj->MSR & CAN_MSR_INAK) == CAN_MSR_INAK)) {}
5858

board/pedal/main.c

+5-22
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "drivers/timer.h"
2020

2121
#include "gpio.h"
22+
#include "crc.h"
2223

2324
#define CAN CAN1
2425

@@ -105,26 +106,6 @@ int usb_cb_control_msg(USB_Setup_TypeDef *setup, uint8_t *resp, bool hardwired)
105106

106107
#endif
107108

108-
// ***************************** pedal can checksum *****************************
109-
110-
uint8_t pedal_checksum(uint8_t *dat, int len) {
111-
uint8_t crc = 0xFF;
112-
uint8_t poly = 0xD5; // standard crc8
113-
int i, j;
114-
for (i = len - 1; i >= 0; i--) {
115-
crc ^= dat[i];
116-
for (j = 0; j < 8; j++) {
117-
if ((crc & 0x80U) != 0U) {
118-
crc = (uint8_t)((crc << 1) ^ poly);
119-
}
120-
else {
121-
crc <<= 1;
122-
}
123-
}
124-
}
125-
return crc;
126-
}
127-
128109
// ***************************** can port *****************************
129110

130111
// addresses to be used on CAN
@@ -155,6 +136,8 @@ uint32_t current_index = 0;
155136
#define FAULT_INVALID 6U
156137
uint8_t state = FAULT_STARTUP;
157138

139+
const uint8_t crc_poly = 0xD5; // standard crc8
140+
158141
void CAN1_RX0_IRQ_Handler(void) {
159142
while ((CAN->RF0R & CAN_RF0R_FMP0) != 0) {
160143
#ifdef DEBUG
@@ -184,7 +167,7 @@ void CAN1_RX0_IRQ_Handler(void) {
184167
uint16_t value_1 = (dat[2] << 8) | dat[3];
185168
bool enable = ((dat[4] >> 7) & 1U) != 0U;
186169
uint8_t index = dat[4] & COUNTER_CYCLE;
187-
if (pedal_checksum(dat, CAN_GAS_SIZE - 1) == dat[5]) {
170+
if (crc_checksum(dat, CAN_GAS_SIZE - 1, crc_poly) == dat[5]) {
188171
if (((current_index + 1U) & COUNTER_CYCLE) == index) {
189172
#ifdef DEBUG
190173
puts("setting gas ");
@@ -247,7 +230,7 @@ void TIM3_IRQ_Handler(void) {
247230
dat[2] = (pdl1 >> 8) & 0xFFU;
248231
dat[3] = (pdl1 >> 0) & 0xFFU;
249232
dat[4] = ((state & 0xFU) << 4) | pkt_idx;
250-
dat[5] = pedal_checksum(dat, CAN_GAS_SIZE - 1);
233+
dat[5] = crc_checksum(dat, CAN_GAS_SIZE - 1, crc_poly);
251234
CAN->sTxMailBox[0].TDLR = dat[0] | (dat[1] << 8) | (dat[2] << 16) | (dat[3] << 24);
252235
CAN->sTxMailBox[0].TDHR = dat[4] | (dat[5] << 8);
253236
CAN->sTxMailBox[0].TDTR = 6; // len of packet is 5

board/safety.h

+22-5
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
#include "safety/safety_defaults.h"
55
#include "safety/safety_honda.h"
66
#include "safety/safety_toyota.h"
7-
#include "safety/safety_toyota_ipas.h"
87
#include "safety/safety_tesla.h"
98
#include "safety/safety_gm_ascm.h"
109
#include "safety/safety_gm.h"
@@ -14,6 +13,7 @@
1413
#include "safety/safety_chrysler.h"
1514
#include "safety/safety_subaru.h"
1615
#include "safety/safety_mazda.h"
16+
#include "safety/safety_nissan.h"
1717
#include "safety/safety_volkswagen.h"
1818
#include "safety/safety_elm327.h"
1919

@@ -31,12 +31,13 @@
3131
#define SAFETY_TESLA 10U
3232
#define SAFETY_SUBARU 11U
3333
#define SAFETY_MAZDA 13U
34-
#define SAFETY_VOLKSWAGEN 15U
35-
#define SAFETY_TOYOTA_IPAS 16U
34+
#define SAFETY_NISSAN 14U
35+
#define SAFETY_VOLKSWAGEN_MQB 15U
3636
#define SAFETY_ALLOUTPUT 17U
3737
#define SAFETY_GM_ASCM 18U
3838
#define SAFETY_NOOUTPUT 19U
3939
#define SAFETY_HONDA_BOSCH_HARNESS 20U
40+
#define SAFETY_SUBARU_LEGACY 22U
4041

4142
uint16_t current_safety_mode = SAFETY_SILENT;
4243
const safety_hooks *current_hooks = &nooutput_hooks;
@@ -57,6 +58,21 @@ int safety_fwd_hook(int bus_num, CAN_FIFOMailBox_TypeDef *to_fwd) {
5758
return current_hooks->fwd(bus_num, to_fwd);
5859
}
5960

61+
// Given a CRC-8 poly, generate a static lookup table to use with a fast CRC-8
62+
// algorithm. Called at init time for safety modes using CRC-8.
63+
void gen_crc_lookup_table(uint8_t poly, uint8_t crc_lut[]) {
64+
for (int i = 0; i < 256; i++) {
65+
uint8_t crc = i;
66+
for (int j = 0; j < 8; j++) {
67+
if ((crc & 0x80U) != 0U)
68+
crc = (uint8_t)((crc << 1) ^ poly);
69+
else
70+
crc <<= 1;
71+
}
72+
crc_lut[i] = crc;
73+
}
74+
}
75+
6076
bool msg_allowed(int addr, int bus, const AddrBus addr_list[], int len) {
6177
bool allowed = false;
6278
for (int i = 0; i < len; i++) {
@@ -184,13 +200,14 @@ const safety_hook_config safety_hook_registry[] = {
184200
{SAFETY_HYUNDAI, &hyundai_hooks},
185201
{SAFETY_CHRYSLER, &chrysler_hooks},
186202
{SAFETY_SUBARU, &subaru_hooks},
203+
{SAFETY_SUBARU_LEGACY, &subaru_legacy_hooks},
187204
{SAFETY_MAZDA, &mazda_hooks},
188-
{SAFETY_VOLKSWAGEN, &volkswagen_hooks},
205+
{SAFETY_VOLKSWAGEN_MQB, &volkswagen_mqb_hooks},
189206
{SAFETY_NOOUTPUT, &nooutput_hooks},
190207
#ifdef ALLOW_DEBUG
191208
{SAFETY_CADILLAC, &cadillac_hooks},
192-
{SAFETY_TOYOTA_IPAS, &toyota_ipas_hooks},
193209
{SAFETY_TESLA, &tesla_hooks},
210+
{SAFETY_NISSAN, &nissan_hooks},
194211
{SAFETY_ALLOUTPUT, &alloutput_hooks},
195212
{SAFETY_GM_ASCM, &gm_ascm_hooks},
196213
{SAFETY_FORD, &ford_hooks},

board/safety/safety_chrysler.h

+80-8
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,77 @@ const uint32_t CHRYSLER_RT_INTERVAL = 250000; // 250ms between real time checks
44
const int CHRYSLER_MAX_RATE_UP = 3;
55
const int CHRYSLER_MAX_RATE_DOWN = 3;
66
const int CHRYSLER_MAX_TORQUE_ERROR = 80; // max torque cmd in excess of torque motor
7+
const int CHRYSLER_GAS_THRSLD = 30; // 7% more than 2m/s
8+
const int CHRYSLER_STANDSTILL_THRSLD = 10; // about 1m/s
79
const AddrBus CHRYSLER_TX_MSGS[] = {{571, 0}, {658, 0}, {678, 0}};
810

911
// TODO: do checksum and counter checks
1012
AddrCheckStruct chrysler_rx_checks[] = {
11-
{.addr = {544}, .bus = 0, .expected_timestep = 10000U},
12-
{.addr = {500}, .bus = 0, .expected_timestep = 20000U},
13+
{.addr = {544}, .bus = 0, .check_checksum = true, .max_counter = 15U, .expected_timestep = 10000U},
14+
{.addr = {514}, .bus = 0, .check_checksum = false, .max_counter = 0U, .expected_timestep = 10000U},
15+
{.addr = {500}, .bus = 0, .check_checksum = true, .max_counter = 15U, .expected_timestep = 20000U},
16+
{.addr = {308}, .bus = 0, .check_checksum = false, .max_counter = 15U, .expected_timestep = 20000U},
17+
{.addr = {320}, .bus = 0, .check_checksum = true, .max_counter = 15U, .expected_timestep = 20000U},
1318
};
1419
const int CHRYSLER_RX_CHECK_LEN = sizeof(chrysler_rx_checks) / sizeof(chrysler_rx_checks[0]);
1520

1621
int chrysler_rt_torque_last = 0;
1722
int chrysler_desired_torque_last = 0;
1823
int chrysler_cruise_engaged_last = 0;
24+
int chrysler_speed = 0;
1925
uint32_t chrysler_ts_last = 0;
2026
struct sample_t chrysler_torque_meas; // last few torques measured
2127

28+
static uint8_t chrysler_get_checksum(CAN_FIFOMailBox_TypeDef *to_push) {
29+
int checksum_byte = GET_LEN(to_push) - 1;
30+
return (uint8_t)(GET_BYTE(to_push, checksum_byte));
31+
}
32+
33+
static uint8_t chrysler_compute_checksum(CAN_FIFOMailBox_TypeDef *to_push) {
34+
/* This function does not want the checksum byte in the input data.
35+
jeep chrysler canbus checksum from http://illmatics.com/Remote%20Car%20Hacking.pdf */
36+
uint8_t checksum = 0xFF;
37+
int len = GET_LEN(to_push);
38+
for (int j = 0; j < (len - 1); j++) {
39+
uint8_t shift = 0x80;
40+
uint8_t curr = (uint8_t)GET_BYTE(to_push, j);
41+
for (int i=0; i<8; i++) {
42+
uint8_t bit_sum = curr & shift;
43+
uint8_t temp_chk = checksum & 0x80U;
44+
if (bit_sum != 0U) {
45+
bit_sum = 0x1C;
46+
if (temp_chk != 0U) {
47+
bit_sum = 1;
48+
}
49+
checksum = checksum << 1;
50+
temp_chk = checksum | 1U;
51+
bit_sum ^= temp_chk;
52+
} else {
53+
if (temp_chk != 0U) {
54+
bit_sum = 0x1D;
55+
}
56+
checksum = checksum << 1;
57+
bit_sum ^= checksum;
58+
}
59+
checksum = bit_sum;
60+
shift = shift >> 1;
61+
}
62+
}
63+
return ~checksum;
64+
}
65+
66+
static uint8_t chrysler_get_counter(CAN_FIFOMailBox_TypeDef *to_push) {
67+
// Well defined counter only for 8 bytes messages
68+
return (uint8_t)(GET_BYTE(to_push, 6) >> 4);
69+
}
70+
2271
static int chrysler_rx_hook(CAN_FIFOMailBox_TypeDef *to_push) {
2372

2473
bool valid = addr_safety_check(to_push, chrysler_rx_checks, CHRYSLER_RX_CHECK_LEN,
25-
NULL, NULL, NULL);
74+
chrysler_get_checksum, chrysler_compute_checksum,
75+
chrysler_get_counter);
2676

27-
if (valid) {
28-
int bus = GET_BUS(to_push);
77+
if (valid && (GET_BUS(to_push) == 0)) {
2978
int addr = GET_ADDR(to_push);
3079

3180
// Measured eps torque
@@ -37,7 +86,7 @@ static int chrysler_rx_hook(CAN_FIFOMailBox_TypeDef *to_push) {
3786
}
3887

3988
// enter controls on rising edge of ACC, exit controls on ACC off
40-
if (addr == 0x1F4) {
89+
if (addr == 500) {
4190
int cruise_engaged = ((GET_BYTE(to_push, 2) & 0x38) >> 3) == 7;
4291
if (cruise_engaged && !chrysler_cruise_engaged_last) {
4392
controls_allowed = 1;
@@ -48,10 +97,33 @@ static int chrysler_rx_hook(CAN_FIFOMailBox_TypeDef *to_push) {
4897
chrysler_cruise_engaged_last = cruise_engaged;
4998
}
5099

51-
// TODO: add gas pressed check
100+
// update speed
101+
if (addr == 514) {
102+
int speed_l = (GET_BYTE(to_push, 0) << 4) + (GET_BYTE(to_push, 1) >> 4);
103+
int speed_r = (GET_BYTE(to_push, 2) << 4) + (GET_BYTE(to_push, 3) >> 4);
104+
chrysler_speed = (speed_l + speed_r) / 2;
105+
}
106+
107+
// exit controls on rising edge of gas press
108+
if (addr == 308) {
109+
bool gas_pressed = (GET_BYTE(to_push, 5) & 0x7F) != 0;
110+
if (gas_pressed && !gas_pressed_prev && (chrysler_speed > CHRYSLER_GAS_THRSLD)) {
111+
controls_allowed = 0;
112+
}
113+
gas_pressed_prev = gas_pressed;
114+
}
115+
116+
// exit controls on rising edge of brake press
117+
if (addr == 320) {
118+
bool brake_pressed = (GET_BYTE(to_push, 0) & 0x7) == 5;
119+
if (brake_pressed && (!brake_pressed_prev || (chrysler_speed > CHRYSLER_STANDSTILL_THRSLD))) {
120+
controls_allowed = 0;
121+
}
122+
brake_pressed_prev = brake_pressed;
123+
}
52124

53125
// check if stock camera ECU is on bus 0
54-
if ((safety_mode_cnt > RELAY_TRNS_TIMEOUT) && (bus == 0) && (addr == 0x292)) {
126+
if ((safety_mode_cnt > RELAY_TRNS_TIMEOUT) && (addr == 0x292)) {
55127
relay_malfunction = true;
56128
}
57129
}

board/safety/safety_ford.h

+7-9
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
// brake rising edge
88
// brake > 0mph
99

10-
int ford_brake_prev = 0;
11-
int ford_gas_prev = 0;
1210
bool ford_moving = false;
1311

1412
static int ford_rx_hook(CAN_FIFOMailBox_TypeDef *to_push) {
@@ -39,20 +37,20 @@ static int ford_rx_hook(CAN_FIFOMailBox_TypeDef *to_push) {
3937
// exit controls on rising edge of brake press or on brake press when
4038
// speed > 0
4139
if (addr == 0x165) {
42-
int brake = GET_BYTE(to_push, 0) & 0x20;
43-
if (brake && (!(ford_brake_prev) || ford_moving)) {
40+
int brake_pressed = GET_BYTE(to_push, 0) & 0x20;
41+
if (brake_pressed && (!brake_pressed_prev || ford_moving)) {
4442
controls_allowed = 0;
4543
}
46-
ford_brake_prev = brake;
44+
brake_pressed_prev = brake_pressed;
4745
}
4846

4947
// exit controls on rising edge of gas press
5048
if (addr == 0x204) {
51-
int gas = (GET_BYTE(to_push, 0) & 0x03) | GET_BYTE(to_push, 1);
52-
if (gas && !(ford_gas_prev)) {
49+
bool gas_pressed = ((GET_BYTE(to_push, 0) & 0x03) | GET_BYTE(to_push, 1)) != 0;
50+
if (gas_pressed && !gas_pressed_prev) {
5351
controls_allowed = 0;
5452
}
55-
ford_gas_prev = gas;
53+
gas_pressed_prev = gas_pressed;
5654
}
5755

5856
if ((safety_mode_cnt > RELAY_TRNS_TIMEOUT) && (bus == 0) && (addr == 0x3CA)) {
@@ -74,7 +72,7 @@ static int ford_tx_hook(CAN_FIFOMailBox_TypeDef *to_send) {
7472

7573
// disallow actuator commands if gas or brake (with vehicle moving) are pressed
7674
// and the the latching controls_allowed flag is True
77-
int pedal_pressed = ford_gas_prev || (ford_brake_prev && ford_moving);
75+
int pedal_pressed = gas_pressed_prev || (brake_pressed_prev && ford_moving);
7876
bool current_controls_allowed = controls_allowed && !(pedal_pressed);
7977

8078
if (relay_malfunction) {

0 commit comments

Comments
 (0)