Skip to content

Commit

Permalink
Merge remote-tracking branch 'commaai/panda/master' into camera-scc-long
Browse files Browse the repository at this point in the history
  • Loading branch information
sunnyhaibin committed Sep 8, 2023
2 parents 7eccd7b + d87d33c commit 4dcde30
Show file tree
Hide file tree
Showing 25 changed files with 237 additions and 115 deletions.
7 changes: 6 additions & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
name: tests
on: [push, pull_request]

on:
push:
branches:
- master
pull_request:

concurrency:
group: ${{ github.workflow }}-${{ github.ref != 'refs/heads/master' && github.ref || github.run_id }}-${{ github.event_name }}
Expand Down
4 changes: 3 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ repos:
- id: check-yaml
- id: check-merge-conflict
- id: check-symlinks
- id: check-executables-have-shebangs
- id: check-shebang-scripts-are-executable
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.5.1
hooks:
- id: mypy
additional_dependencies: ['git+https://github.com/numpy/numpy-stubs', 'types-requests', 'types-atomicwrites',
'types-pycurl']
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.0.285
rev: v0.0.287
hooks:
- id: ruff
8 changes: 5 additions & 3 deletions board/drivers/fdcan.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,11 @@ void update_can_health_pkt(uint8_t can_number, uint32_t ir_reg) {
if ((ir_reg & (FDCAN_IR_RF0L)) != 0) {
can_health[can_number].total_rx_lost_cnt += 1U;
}
// While multiplexing between buses 1 and 3 we are getting ACK errors that overwhelm CAN core
// By resseting CAN core when no ACK is detected for a while(until TEC counter reaches 127) it can recover faster
if (((can_health[can_number].last_error == CAN_ACK_ERROR) || (can_health[can_number].last_data_error == CAN_ACK_ERROR)) && (can_health[can_number].transmit_error_cnt > 127U)) {
// Cases:
// 1. while multiplexing between buses 1 and 3 we are getting ACK errors that overwhelm CAN core, by resetting it recovers faster
// 2. H7 gets stuck in bus off recovery state indefinitely
if ((((can_health[can_number].last_error == CAN_ACK_ERROR) || (can_health[can_number].last_data_error == CAN_ACK_ERROR)) && (can_health[can_number].transmit_error_cnt > 127U)) ||
((ir_reg & FDCAN_IR_BO) != 0)) {
can_health[can_number].can_core_reset_cnt += 1U;
can_health[can_number].total_tx_lost_cnt += (FDCAN_TX_FIFO_EL_CNT - (FDCANx->TXFQS & FDCAN_TXFQS_TFFL)); // TX FIFO msgs will be lost after reset
llcan_clear_send(FDCANx);
Expand Down
15 changes: 6 additions & 9 deletions board/jungle/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,6 @@ void tick_handler(void) {
}

#ifdef FINAL_PROVISIONING
// Reset CAN core if it got stuck in bus off state after shorted CANH/L
// Looks like an issue with H7 that it doesn't want to recover from bus off
// FIXME: this must be fixed on the driver level, temporary workaround
for (uint8_t i = 0U; i < 3U; i++) {
update_can_health_pkt(i, 0);
if (can_health[i].bus_off == 1U) {
can_init(i);
}
}
// Ignition blinking
uint8_t ignition_bitmask = 0U;
for (uint8_t i = 0U; i < 6U; i++) {
Expand Down Expand Up @@ -150,6 +141,9 @@ int main(void) {
clock_init();
peripherals_init();
detect_board_type();
// red+green leds enabled until succesful USB init, as a debug indicator
current_board->set_led(LED_RED, true);
current_board->set_led(LED_GREEN, true);

// print hello
print("\n\n\n************************ MAIN START ************************\n");
Expand Down Expand Up @@ -181,6 +175,9 @@ int main(void) {
// enable USB (right before interrupts or enum can fail!)
usb_init();

current_board->set_led(LED_RED, false);
current_board->set_led(LED_GREEN, false);

print("**** INTERRUPTS ON ****\n");
enable_interrupts();

Expand Down
10 changes: 8 additions & 2 deletions board/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,8 @@ void tick_handler(void) {
if (som_running) {
log("device booted");
waiting_to_boot = false;
} else if (waiting_to_boot_count == 10U) {
log("not booted after 10s");
} else if (waiting_to_boot_count == 45U) {
log("not booted after 45s");
} else {

}
Expand Down Expand Up @@ -348,6 +348,9 @@ int main(void) {
clock_init();
peripherals_init();
detect_board_type();
// red+green leds enabled until succesful USB/SPI init, as a debug indicator
current_board->set_led(LED_RED, true);
current_board->set_led(LED_GREEN, true);
adc_init();
logging_init();

Expand Down Expand Up @@ -410,6 +413,9 @@ int main(void) {
}
#endif

current_board->set_led(LED_RED, false);
current_board->set_led(LED_GREEN, false);

print("**** INTERRUPTS ON ****\n");
enable_interrupts();

Expand Down
8 changes: 5 additions & 3 deletions board/safety/safety_nissan.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ AddrCheckStruct nissan_addr_checks[] = {
addr_checks nissan_rx_checks = {nissan_addr_checks, NISSAN_ADDR_CHECK_LEN};

// EPS Location. false = V-CAN, true = C-CAN
const int NISSAN_PARAM_ALT_EPS_BUS = 1;

bool nissan_alt_eps = false;

static int nissan_rx_hook(CANPacket_t *to_push) {
Expand All @@ -48,7 +50,7 @@ static int nissan_rx_hook(CANPacket_t *to_push) {
int bus = GET_BUS(to_push);
int addr = GET_ADDR(to_push);

if (((bus == 0) && (!nissan_alt_eps)) || ((bus == 1) && (nissan_alt_eps))) {
if (bus == (nissan_alt_eps ? 1 : 0)) {
if (addr == 0x2) {
// Current steering angle
// Factor -0.1, little endian
Expand Down Expand Up @@ -88,7 +90,7 @@ static int nissan_rx_hook(CANPacket_t *to_push) {
}

// Handle cruise enabled
if ((addr == 0x30f) && (((bus == 2) && (!nissan_alt_eps)) || ((bus == 1) && (nissan_alt_eps)))) {
if ((addr == 0x30f) && (bus == (nissan_alt_eps ? 1 : 2))) {
bool cruise_engaged = (GET_BYTE(to_push, 0) >> 3) & 1U;
pcm_cruise_check(cruise_engaged);
}
Expand Down Expand Up @@ -158,7 +160,7 @@ static int nissan_fwd_hook(int bus_num, int addr) {
}

static const addr_checks* nissan_init(uint16_t param) {
nissan_alt_eps = param ? 1 : 0;
nissan_alt_eps = GET_FLAG(param, NISSAN_PARAM_ALT_EPS_BUS);
return &nissan_rx_checks;
}

Expand Down
88 changes: 63 additions & 25 deletions board/safety/safety_subaru.h
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
#define SUBARU_STEERING_LIMITS_GENERATOR(steer_max, rate_up, rate_down) \
{ \
.max_steer = (steer_max), \
.max_rt_delta = 940, \
.max_rt_interval = 250000, \
.max_rate_up = (rate_up), \
.max_rate_down = (rate_down), \
.driver_torque_factor = 50, \
.driver_torque_allowance = 60, \
.type = TorqueDriverLimited, \
} \
#define SUBARU_STEERING_LIMITS_GENERATOR(steer_max, rate_up, rate_down) \
{ \
.max_steer = (steer_max), \
.max_rt_delta = 940, \
.max_rt_interval = 250000, \
.max_rate_up = (rate_up), \
.max_rate_down = (rate_down), \
.driver_torque_factor = 50, \
.driver_torque_allowance = 60, \
.type = TorqueDriverLimited, \
/* the EPS will temporary fault if the steering rate is too high, so we cut the \
the steering torque every 7 frames for 1 frame if the steering rate is high */ \
.min_valid_request_frames = 7, \
.max_invalid_request_frames = 1, \
.min_valid_request_rt_interval = 144000, /* 10% tolerance */ \
.has_steer_req_tolerance = true, \
}


const SteeringLimits SUBARU_STEERING_LIMITS = SUBARU_STEERING_LIMITS_GENERATOR(2047, 50, 70);
const SteeringLimits SUBARU_GEN2_STEERING_LIMITS = SUBARU_STEERING_LIMITS_GENERATOR(1000, 40, 40);
Expand Down Expand Up @@ -39,20 +46,32 @@ const LongitudinalLimits SUBARU_LONG_LIMITS = {
#define MSG_SUBARU_ES_LKAS_State 0x322
#define MSG_SUBARU_ES_Infotainment 0x323

#define MSG_SUBARU_ES_UDS_Request 0x787

#define MSG_SUBARU_ES_HighBeamAssist 0x121
#define MSG_SUBARU_ES_STATIC_1 0x22a
#define MSG_SUBARU_ES_STATIC_2 0x325

#define SUBARU_MAIN_BUS 0
#define SUBARU_ALT_BUS 1
#define SUBARU_CAM_BUS 2

#define SUBARU_COMMON_TX_MSGS(alt_bus, lkas_msg) \
{MSG_SUBARU_ES_LKAS, SUBARU_MAIN_BUS, 8}, \
{MSG_SUBARU_ES_Distance, alt_bus, 8}, \
{MSG_SUBARU_ES_DashStatus, SUBARU_MAIN_BUS, 8}, \
{MSG_SUBARU_ES_LKAS_State, SUBARU_MAIN_BUS, 8}, \
{MSG_SUBARU_ES_Infotainment, SUBARU_MAIN_BUS, 8}, \
#define SUBARU_COMMON_TX_MSGS(alt_bus, lkas_msg) \
{lkas_msg, SUBARU_MAIN_BUS, 8}, \
{MSG_SUBARU_ES_Distance, alt_bus, 8}, \
{MSG_SUBARU_ES_DashStatus, SUBARU_MAIN_BUS, 8}, \
{MSG_SUBARU_ES_LKAS_State, SUBARU_MAIN_BUS, 8}, \
{MSG_SUBARU_ES_Infotainment, SUBARU_MAIN_BUS, 8}, \

#define SUBARU_COMMON_LONG_TX_MSGS(alt_bus) \
{MSG_SUBARU_ES_Brake, SUBARU_MAIN_BUS, 8}, \
{MSG_SUBARU_ES_Status, SUBARU_MAIN_BUS, 8}, \
#define SUBARU_COMMON_LONG_TX_MSGS(alt_bus) \
{MSG_SUBARU_ES_Brake, alt_bus, 8}, \
{MSG_SUBARU_ES_Status, alt_bus, 8}, \

#define SUBARU_GEN2_LONG_ADDITIONAL_TX_MSGS() \
{MSG_SUBARU_ES_UDS_Request, SUBARU_CAM_BUS, 8}, \
{MSG_SUBARU_ES_HighBeamAssist, SUBARU_MAIN_BUS, 8}, \
{MSG_SUBARU_ES_STATIC_1, SUBARU_MAIN_BUS, 8}, \
{MSG_SUBARU_ES_STATIC_2, SUBARU_MAIN_BUS, 8}, \

#define SUBARU_COMMON_ADDR_CHECKS(alt_bus) \
{.msg = {{MSG_SUBARU_Throttle, SUBARU_MAIN_BUS, 8, .check_checksum = true, .max_counter = 15U, .expected_timestep = 10000U}, { 0 }, { 0 }}}, \
Expand All @@ -77,6 +96,13 @@ const CanMsg SUBARU_GEN2_TX_MSGS[] = {
};
#define SUBARU_GEN2_TX_MSGS_LEN (sizeof(SUBARU_GEN2_TX_MSGS) / sizeof(SUBARU_GEN2_TX_MSGS[0]))

const CanMsg SUBARU_GEN2_LONG_TX_MSGS[] = {
SUBARU_COMMON_TX_MSGS(SUBARU_ALT_BUS, MSG_SUBARU_ES_LKAS)
SUBARU_COMMON_LONG_TX_MSGS(SUBARU_ALT_BUS)
SUBARU_GEN2_LONG_ADDITIONAL_TX_MSGS()
};
#define SUBARU_GEN2_LONG_TX_MSGS_LEN (sizeof(SUBARU_GEN2_LONG_TX_MSGS) / sizeof(SUBARU_GEN2_LONG_TX_MSGS[0]))

AddrCheckStruct subaru_addr_checks[] = {
SUBARU_COMMON_ADDR_CHECKS(SUBARU_MAIN_BUS)
};
Expand Down Expand Up @@ -175,12 +201,14 @@ static int subaru_tx_hook(CANPacket_t *to_send) {
int addr = GET_ADDR(to_send);
bool violation = false;

if (subaru_gen2) {
tx = msg_allowed(to_send, SUBARU_GEN2_TX_MSGS, SUBARU_GEN2_TX_MSGS_LEN);
if (subaru_gen2 && subaru_longitudinal) {
tx = msg_allowed(to_send, SUBARU_GEN2_LONG_TX_MSGS, SUBARU_GEN2_LONG_TX_MSGS_LEN);
} else if (subaru_gen2) {
tx = msg_allowed(to_send, SUBARU_GEN2_TX_MSGS, SUBARU_GEN2_TX_MSGS_LEN);
} else if (subaru_longitudinal) {
tx = msg_allowed(to_send, SUBARU_LONG_TX_MSGS, SUBARU_LONG_TX_MSGS_LEN);
tx = msg_allowed(to_send, SUBARU_LONG_TX_MSGS, SUBARU_LONG_TX_MSGS_LEN);
} else {
tx = msg_allowed(to_send, SUBARU_TX_MSGS, SUBARU_TX_MSGS_LEN);
tx = msg_allowed(to_send, SUBARU_TX_MSGS, SUBARU_TX_MSGS_LEN);
}

// steer cmd checks
Expand Down Expand Up @@ -221,6 +249,16 @@ static int subaru_tx_hook(CANPacket_t *to_send) {
violation |= longitudinal_transmission_rpm_checks(transmission_rpm, SUBARU_LONG_LIMITS);
}

if (addr == MSG_SUBARU_ES_UDS_Request) {
// tester present ('\x02\x3E\x80\x00\x00\x00\x00\x00') is allowed for gen2 longitudinal to keep eyesight disabled
bool is_tester_present = (GET_BYTES(to_send, 0, 4) == 0x00803E02U) && (GET_BYTES(to_send, 4, 4) == 0x0U);

// reading ES button data by identifier (b'\x03\x22\x11\x30\x00\x00\x00\x00') is also allowed (DID 0x1130)
bool is_button_rdbi = (GET_BYTES(to_send, 0, 4) == 0x30112203U) && (GET_BYTES(to_send, 4, 4) == 0x0U);

violation |= !(is_tester_present || is_button_rdbi);
}

if (violation){
tx = 0;
}
Expand Down Expand Up @@ -258,7 +296,7 @@ static const addr_checks* subaru_init(uint16_t param) {
subaru_gen2 = GET_FLAG(param, SUBARU_PARAM_GEN2);

#ifdef ALLOW_DEBUG
subaru_longitudinal = GET_FLAG(param, SUBARU_PARAM_LONGITUDINAL) && !subaru_gen2;
subaru_longitudinal = GET_FLAG(param, SUBARU_PARAM_LONGITUDINAL);
#endif

if (subaru_gen2) {
Expand Down
4 changes: 3 additions & 1 deletion board/safety/safety_volkswagen_mqb.h
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,9 @@ static int volkswagen_mqb_tx_hook(CANPacket_t *to_send) {
desired_torque *= -1;
}

if (steer_torque_cmd_checks(desired_torque, -1, VOLKSWAGEN_MQB_STEERING_LIMITS)) {
bool steer_req = GET_BIT(to_send, 30U) != 0U;

if (steer_torque_cmd_checks(desired_torque, steer_req, VOLKSWAGEN_MQB_STEERING_LIMITS)) {
tx = 0;
}
}
Expand Down
Empty file modified examples/tesla_tester.py
100644 → 100755
Empty file.
4 changes: 3 additions & 1 deletion python/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,8 @@ class Panda:
FLAG_SUBARU_GEN2 = 1
FLAG_SUBARU_LONG = 2

FLAG_NISSAN_ALT_EPS_BUS = 1

FLAG_GM_HW_CAM = 1
FLAG_GM_HW_CAM_LONG = 2

Expand Down Expand Up @@ -456,7 +458,7 @@ def reconnect(self):

success = False
# wait up to 15 seconds
for _ in range(0, 15*10):
for _ in range(15*10):
try:
self.connect()
success = True
Expand Down
1 change: 0 additions & 1 deletion python/ccp.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#!/usr/bin/env python3
import sys
import time
import struct
Expand Down
2 changes: 1 addition & 1 deletion python/spi.py
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ def close(self):
def program(self, address, dat):
bs = 256 # max block size for writing to flash over SPI
dat += b"\xFF" * ((bs - len(dat)) % bs)
for i in range(0, len(dat) // bs):
for i in range(len(dat) // bs):
block = dat[i * bs:(i + 1) * bs]
self._cmd(0x31, data=[
struct.pack('>I', address + i*bs),
Expand Down
1 change: 0 additions & 1 deletion python/uds.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#!/usr/bin/env python3
import time
import struct
from collections import deque
Expand Down
2 changes: 1 addition & 1 deletion python/usb.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def program(self, address, dat):
# Program
bs = min(len(dat), self._mcu_type.config.block_size)
dat += b"\xFF" * ((bs - len(dat)) % bs)
for i in range(0, len(dat) // bs):
for i in range(len(dat) // bs):
ldat = dat[i * bs:(i + 1) * bs]
print("programming %d with length %d" % (i, len(ldat)))
self._libusb_handle.controlWrite(0x21, self.DFU_DNLOAD, 2 + i, 0, ldat)
Expand Down
Empty file modified tests/read_winusb_descriptors.py
100644 → 100755
Empty file.
Loading

0 comments on commit 4dcde30

Please sign in to comment.