Skip to content

Commit

Permalink
Squash merge of the following:
Browse files Browse the repository at this point in the history
From: budney
2650024 Renamed unsafe_mode to alternative_experience in subaru-community code.

From: commaai/master
Merge: 0e79ab3^..7104818

7104818 rename unsafeMode to alternativeExperience (commaai#878)
e01c645 Toyota: increase torque rate limit (commaai#887)
0591f68 Honda: fix missing interceptor tests (commaai#886)
991638a Tesla: only allow cancel button (commaai#880)
499906f Nissan + Tesla: don't exit controls allowed on blocked message (commaai#877)
ba10911 GM: enters controls on falling edge of button presses (commaai#876)
868fd53 Hyundai: enter controls on falling edge of buttons (commaai#875)
4acaebb Honda: enter controls on falling edge of button presses (commaai#874)
e6722c7 Honda Nidec: match openpilot engagement behavior (commaai#872)
a53528a Add more hints when troubleshooting panda (commaai#864)
5a7af82 Hyundai: only allow cancel while cruise is enabled (commaai#870)
891653f Docker: bump openpilot and fix Toyota EPS scale factor for test (commaai#869)
234e436 rename to enable_deepsleep
860e5a4 Red Panda: auto switching to CAN FD with BRS (commaai#868)
4f422ed C2: fix bootkick (commaai#854)
de7e1e7 Deep sleep(stop) mode for pandas (commaai#832)
e8e9d84 fix release script
51ccb9f HITL tests: change testing speeds (commaai#862)
8435bda CAN FD: adjust bit timing settings to SAE specifications J2284-4 and J2284-5 (commaai#861)
cb88a17 Honda: revert back to button enable for Nidec (commaai#858)
2806eec python: add safety param arg to set_safety_mode
9d27011 misc CAN FD fixes (commaai#856)
0e79ab3 Fix USB enumeration detection for C2 (commaai#853)
  • Loading branch information
Len Budney authored and Len Budney committed Mar 28, 2022
1 parent 75c4895 commit af5c27d
Show file tree
Hide file tree
Showing 43 changed files with 660 additions and 262 deletions.
2 changes: 1 addition & 1 deletion Dockerfile.panda
Expand Up @@ -62,7 +62,7 @@ RUN cd /tmp && \
git clone https://github.com/martinl/openpilot.git tmppilot || true && \
cd /tmp/tmppilot && \
git fetch && \
git checkout cdd3598146c9e06362683e2993af730157abf0b6 && \
git checkout c6e0f1a94103ae6b6c879615a00ef0855560bd42 && \
git submodule update --init cereal opendbc rednose_repo && \
mkdir /tmp/openpilot && \
cp -pR SConstruct site_scons/ tools/ selfdrive/ common/ cereal/ opendbc/ rednose/ /tmp/openpilot && \
Expand Down
10 changes: 7 additions & 3 deletions board/README.md
Expand Up @@ -21,17 +21,21 @@ Programming
**Panda**

```
scons -u # Compile
./flash.sh # Compile & Flash
scons -u # Compile
./flash_h7.sh # for red panda
./flash.sh # for other pandas
```

Troubleshooting
----

If your panda will not flash and is quickly blinking a single Green LED, use:
```
./recover.sh
./recover_h7.sh # for red panda
./recover.sh # for other pandas
```

A [panda paw](https://comma.ai/shop/products/panda-paw) can be used to put panda into DFU mode.


[dfu-util](http://github.com/dsigma/dfu-util.git) for flashing
2 changes: 2 additions & 0 deletions board/config.h
Expand Up @@ -7,6 +7,8 @@
//#define DEBUG_SPI
//#define DEBUG_FAULTS

#define DEEPSLEEP_WAKEUP_DELAY 3U

#define NULL ((void*)0)
#define COMPILE_TIME_ASSERT(pred) ((void)sizeof(char[1 - (2 * ((int)(!(pred))))]))

Expand Down
7 changes: 6 additions & 1 deletion board/drivers/bxcan.h
Expand Up @@ -9,7 +9,12 @@ bool can_set_speed(uint8_t can_number) {
CAN_TypeDef *CAN = CANIF_FROM_CAN_NUM(can_number);
uint8_t bus_number = BUS_NUM_FROM_CAN_NUM(can_number);

ret &= llcan_set_speed(CAN, bus_config[bus_number].can_speed, can_loopback, (unsigned int)(can_silent) & (1U << can_number));
ret &= llcan_set_speed(
CAN,
bus_config[bus_number].can_speed,
can_loopback,
(unsigned int)(can_silent) & (1U << can_number)
);
return ret;
}

Expand Down
6 changes: 3 additions & 3 deletions board/drivers/can_common.h
Expand Up @@ -161,9 +161,9 @@ void can_clear(can_ring *q) {
// Helpers
// Panda: Bus 0=CAN1 Bus 1=CAN2 Bus 2=CAN3
bus_config_t bus_config[] = {
{ .bus_lookup = 0U, .can_num_lookup = 0U, .can_speed = 5000U, .can_data_speed = 5000U, .canfd_enabled = false, .brs_enabled = false },
{ .bus_lookup = 1U, .can_num_lookup = 1U, .can_speed = 5000U, .can_data_speed = 5000U, .canfd_enabled = false, .brs_enabled = false },
{ .bus_lookup = 2U, .can_num_lookup = 2U, .can_speed = 5000U, .can_data_speed = 5000U, .canfd_enabled = false, .brs_enabled = false },
{ .bus_lookup = 0U, .can_num_lookup = 0U, .can_speed = 5000U, .can_data_speed = 20000U, .canfd_enabled = false, .brs_enabled = false },
{ .bus_lookup = 1U, .can_num_lookup = 1U, .can_speed = 5000U, .can_data_speed = 20000U, .canfd_enabled = false, .brs_enabled = false },
{ .bus_lookup = 2U, .can_num_lookup = 2U, .can_speed = 5000U, .can_data_speed = 20000U, .canfd_enabled = false, .brs_enabled = false },
{ .bus_lookup = 0xFFU, .can_num_lookup = 0xFFU, .can_speed = 333U, .can_data_speed = 333U, .canfd_enabled = false, .brs_enabled = false },
};

Expand Down
19 changes: 18 additions & 1 deletion board/drivers/fdcan.h
Expand Up @@ -17,7 +17,13 @@ bool can_set_speed(uint8_t can_number) {
FDCAN_GlobalTypeDef *CANx = CANIF_FROM_CAN_NUM(can_number);
uint8_t bus_number = BUS_NUM_FROM_CAN_NUM(can_number);

ret &= llcan_set_speed(CANx, bus_config[bus_number].can_speed, bus_config[bus_number].can_data_speed, can_loopback, (unsigned int)(can_silent) & (1U << can_number));
ret &= llcan_set_speed(
CANx,
bus_config[bus_number].can_speed,
bus_config[bus_number].can_data_speed,
can_loopback,
(unsigned int)(can_silent) & (1U << can_number)
);
return ret;
}

Expand Down Expand Up @@ -146,6 +152,9 @@ void can_rx(uint8_t can_number) {
to_push.bus = bus_number;
to_push.data_len_code = ((fifo->header[1] >> 16) & 0xFU);

bool canfd_frame = ((fifo->header[1] >> 21) & 0x1U);
bool brs_frame = ((fifo->header[1] >> 20) & 0x1U);

uint8_t data_len_w = (dlc_to_len[to_push.data_len_code] / 4U);
data_len_w += ((dlc_to_len[to_push.data_len_code] % 4U) > 0U) ? 1U : 0U;
for (unsigned int i = 0; i < data_len_w; i++) {
Expand Down Expand Up @@ -173,6 +182,14 @@ void can_rx(uint8_t can_number) {
current_board->set_led(LED_BLUE, true);
can_send_errs += can_push(&can_rx_q, &to_push) ? 0U : 1U;

// Enable CAN FD and BRS if CAN FD message was received
if (!(bus_config[can_number].canfd_enabled) && (canfd_frame)) {
bus_config[can_number].canfd_enabled = true;
}
if (!(bus_config[can_number].brs_enabled) && (brs_frame)) {
bus_config[can_number].brs_enabled = true;
}

// update read index
CANx->RXF0A = rx_fifo_idx;
}
Expand Down
9 changes: 9 additions & 0 deletions board/drivers/usb.h
Expand Up @@ -685,6 +685,7 @@ void usb_irqhandler(void) {

if ((gintsts & USB_OTG_GINTSTS_USBRST) != 0) {
puts("USB reset\n");
usb_enumerated = false;
usb_reset();
}

Expand Down Expand Up @@ -941,3 +942,11 @@ void usb_outep3_resume_if_paused(void) {
}
EXIT_CRITICAL();
}

void usb_soft_disconnect(bool enable) {
if (enable) {
USBx_DEVICE->DCTL |= USB_OTG_DCTL_SDIS;
} else {
USBx_DEVICE->DCTL &= ~USB_OTG_DCTL_SDIS;
}
}
1 change: 1 addition & 0 deletions board/faults.h
Expand Up @@ -25,6 +25,7 @@
#define FAULT_INTERRUPT_RATE_KLINE_INIT (1U << 19)
#define FAULT_INTERRUPT_RATE_CLOCK_SOURCE (1U << 20)
#define FAULT_INTERRUPT_RATE_TICK (1U << 21)
#define FAULT_INTERRUPT_RATE_EXTI (1U << 22)

// Permanent faults
#define PERMANENT_FAULTS 0U
Expand Down
6 changes: 3 additions & 3 deletions board/health.h
@@ -1,5 +1,5 @@
// When changing this struct, boardd and python/__init__.py needs to be kept up to date!
#define HEALTH_PACKET_VERSION 3
// When changing this struct, python/__init__.py needs to be kept up to date!
#define HEALTH_PACKET_VERSION 4
struct __attribute__((packed)) health_t {
uint32_t uptime_pkt;
uint32_t voltage_pkt;
Expand All @@ -20,6 +20,6 @@ struct __attribute__((packed)) health_t {
uint8_t fault_status_pkt;
uint8_t power_save_enabled_pkt;
uint8_t heartbeat_lost_pkt;
uint16_t unsafe_mode_pkt;
uint16_t alternative_experience_pkt;
uint32_t blocked_msg_cnt_pkt;
};
52 changes: 51 additions & 1 deletion board/main.c
Expand Up @@ -248,7 +248,7 @@ void tick_handler(void) {
}

// enter CDP mode when car starts to ensure we are charging a turned off EON
if (check_started() && (usb_power_mode != USB_POWER_CDP)) {
if (check_started() && ((usb_power_mode != USB_POWER_CDP) || !usb_enumerated)) {
current_board->set_usb_power_mode(USB_POWER_CDP);
}
}
Expand Down Expand Up @@ -276,6 +276,38 @@ void tick_handler(void) {
TICK_TIMER->SR = 0;
}

void EXTI_IRQ_Handler(void) {
if (check_exti_irq()) {
exti_irq_clear();
clock_init();

current_board->set_usb_power_mode(USB_POWER_CDP);
set_power_save_state(POWER_SAVE_STATUS_DISABLED);
deepsleep_requested = false;
heartbeat_counter = 0U;
usb_soft_disconnect(false);

NVIC_EnableIRQ(TICK_TIMER_IRQ);
}
}

uint8_t rtc_counter = 0;
void RTC_WKUP_IRQ_Handler(void) {
exti_irq_clear();
clock_init();

rtc_counter++;
if ((rtc_counter % 2U) == 0U) {
current_board->set_led(LED_BLUE, false);
} else {
current_board->set_led(LED_BLUE, true);
}

if (rtc_counter == __UINT8_MAX__) {
rtc_counter = 1U;
}
}


int main(void) {
// Init interrupt table
Expand Down Expand Up @@ -387,7 +419,25 @@ int main(void) {
}
#endif
} else {
if (deepsleep_requested && !usb_enumerated && !check_started()) {
usb_soft_disconnect(true);
current_board->set_fan_power(0U);
current_board->set_usb_power_mode(USB_POWER_CLIENT);
NVIC_DisableIRQ(TICK_TIMER_IRQ);
delay(512000U);

// Init IRQs for CAN transceiver and ignition line
exti_irq_init();

// Init RTC Wakeup event on EXTI22
REGISTER_INTERRUPT(RTC_WKUP_IRQn, RTC_WKUP_IRQ_Handler, 10U, FAULT_INTERRUPT_RATE_EXTI)
rtc_wakeup_init();

// STOP mode
SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk;
}
__WFI();
SCB->SCR &= ~SCB_SCR_SLEEPDEEP_Msk;
}
}

Expand Down
3 changes: 3 additions & 0 deletions board/main_declarations.h
Expand Up @@ -23,6 +23,9 @@ bool heartbeat_disabled = false; // set over USB
bool heartbeat_engaged = false; // openpilot enabled, passed in heartbeat USB command
uint32_t heartbeat_engaged_mismatches = 0; // count of mismatches between heartbeat_engaged and controls_allowed

// Enter deep sleep mode
bool deepsleep_requested = false;

// siren state
bool siren_enabled = false;
uint32_t siren_countdown = 0; // siren plays while countdown > 0
Expand Down
3 changes: 2 additions & 1 deletion board/safety.h
Expand Up @@ -206,7 +206,7 @@ bool addr_safety_check(CANPacket_t *to_push,

void generic_rx_checks(bool stock_ecu_detected) {
// exit controls on rising edge of gas press
if (gas_pressed && !gas_pressed_prev && !(unsafe_mode & UNSAFE_DISABLE_DISENGAGE_ON_GAS)) {
if (gas_pressed && !gas_pressed_prev && !(alternative_experience & ALT_EXP_DISABLE_DISENGAGE_ON_GAS)) {
controls_allowed = 0;
}
gas_pressed_prev = gas_pressed;
Expand Down Expand Up @@ -278,6 +278,7 @@ int set_safety_hooks(uint16_t mode, int16_t param) {
vehicle_speed = 0;
vehicle_moving = false;
acc_main_on = false;
cruise_button_prev = 0;
desired_torque_last = 0;
rt_torque_last = 0;
ts_angle_last = 0;
Expand Down
8 changes: 4 additions & 4 deletions board/safety/safety_ford.h
Expand Up @@ -12,7 +12,7 @@ static int ford_rx_hook(CANPacket_t *to_push) {

int addr = GET_ADDR(to_push);
int bus = GET_BUS(to_push);
bool unsafe_allow_gas = unsafe_mode & UNSAFE_DISABLE_DISENGAGE_ON_GAS;
bool alt_exp_allow_gas = alternative_experience & ALT_EXP_DISABLE_DISENGAGE_ON_GAS;

if (addr == 0x217) {
// wheel speeds are 14 bits every 16
Expand Down Expand Up @@ -47,7 +47,7 @@ static int ford_rx_hook(CANPacket_t *to_push) {
// exit controls on rising edge of gas press
if (addr == 0x204) {
gas_pressed = ((GET_BYTE(to_push, 0) & 0x03U) | GET_BYTE(to_push, 1)) != 0U;
if (!unsafe_allow_gas && gas_pressed && !gas_pressed_prev) {
if (!alt_exp_allow_gas && gas_pressed && !gas_pressed_prev) {
controls_allowed = 0;
}
gas_pressed_prev = gas_pressed;
Expand All @@ -73,8 +73,8 @@ static int ford_tx_hook(CANPacket_t *to_send) {
// disallow actuator commands if gas or brake (with vehicle moving) are pressed
// and the the latching controls_allowed flag is True
int pedal_pressed = brake_pressed_prev && vehicle_moving;
bool unsafe_allow_gas = unsafe_mode & UNSAFE_DISABLE_DISENGAGE_ON_GAS;
if (!unsafe_allow_gas) {
bool alt_exp_allow_gas = alternative_experience & ALT_EXP_DISABLE_DISENGAGE_ON_GAS;
if (!alt_exp_allow_gas) {
pedal_pressed = pedal_pressed || gas_pressed_prev;
}
bool current_controls_allowed = controls_allowed && !(pedal_pressed);
Expand Down
35 changes: 23 additions & 12 deletions board/safety/safety_gm.h
Expand Up @@ -34,6 +34,14 @@ AddrCheckStruct gm_addr_checks[] = {
#define GM_RX_CHECK_LEN (sizeof(gm_addr_checks) / sizeof(gm_addr_checks[0]))
addr_checks gm_rx_checks = {gm_addr_checks, GM_RX_CHECK_LEN};


enum {
GM_BTN_UNPRESS = 1,
GM_BTN_RESUME = 2,
GM_BTN_SET = 3,
GM_BTN_CANCEL = 6,
};

static int gm_rx_hook(CANPacket_t *to_push) {

bool valid = addr_safety_check(to_push, &gm_rx_checks, NULL, NULL, NULL);
Expand All @@ -57,17 +65,20 @@ static int gm_rx_hook(CANPacket_t *to_push) {
// ACC steering wheel buttons
if (addr == 481) {
int button = (GET_BYTE(to_push, 5) & 0x70U) >> 4;
switch (button) {
case 2: // resume
case 3: // set
controls_allowed = 1;
break;
case 6: // cancel
controls_allowed = 0;
break;
default:
break; // any other button is irrelevant

// exit controls on cancel press
if (button == GM_BTN_CANCEL) {
controls_allowed = 0;
}

// enter controls on falling edge of set or resume
bool set = (button == GM_BTN_UNPRESS) && (cruise_button_prev == GM_BTN_SET);
bool res = (button == GM_BTN_UNPRESS) && (cruise_button_prev == GM_BTN_RESUME);
if (set || res) {
controls_allowed = 1;
}

cruise_button_prev = button;
}

if (addr == 201) {
Expand Down Expand Up @@ -113,8 +124,8 @@ static int gm_tx_hook(CANPacket_t *to_send) {
// disallow actuator commands if gas or brake (with vehicle moving) are pressed
// and the the latching controls_allowed flag is True
int pedal_pressed = brake_pressed_prev && vehicle_moving;
bool unsafe_allow_gas = unsafe_mode & UNSAFE_DISABLE_DISENGAGE_ON_GAS;
if (!unsafe_allow_gas) {
bool alt_exp_allow_gas = alternative_experience & ALT_EXP_DISABLE_DISENGAGE_ON_GAS;
if (!alt_exp_allow_gas) {
pedal_pressed = pedal_pressed || gas_pressed_prev;
}
bool current_controls_allowed = controls_allowed && !pedal_pressed;
Expand Down

0 comments on commit af5c27d

Please sign in to comment.