Skip to content

Commit 5810479

Browse files
author
Vehicle Researcher
committedApr 9, 2020
Squashed 'panda/' changes from 0696730..bc90b60
bc90b60 toyota: use universal gas pressed bit (#488) 74d10cc Fixed possible race condition (#487) a05361e cleanup safety_replay dockerfile (#486) fe73dcc Openpilot-tools is deprecated (#484) da8e00f TX message guaranteed delivery (#421) d8f6184 Add ISO number for longitudinal limits flag comment 6a60b78 touch ups 2ce6536 comments on unsafe flags d880134 remove from there as well 055ea07 remove that unsafe flag since it isn't implemented and it's unclear how to 4e98bbe Apply unsafe allow gas mode to all cars. (#480) 0c2c149 Fixing libusb busy error (#174) 753c42c Update Board Mac SDK Install script to work on clean mac (#146) b9a9ea3 Unsafe gas disengage mods, fix test compile warning (#481) 08ef92d Safety model for Volkswagen PQ35/PQ46/NMS (#474) 51e0a55 Support code for unsafe mode unit tests (#478) 5325b62 current_safety_mode 7908b72 update updating unsafe mode 98503e8 disable stock honda AEB in unsafe mode (#477) 01b2ccb one more 9a30265 weak steering while not engaged 577f10b added options for unsafe mode 83cf7bf update comment 4556e74 enable unsafe mode, toggle for use by forks that so choose de89fcd Nissan leaf (#473) git-subtree-dir: panda git-subtree-split: bc90b60
1 parent a3690e4 commit 5810479

37 files changed

+1154
-150
lines changed
 

‎VERSION

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
v1.7.3
1+
v1.7.4

‎board/drivers/can.h

+27
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ void can_set_forwarding(int from, int to);
2828

2929
void can_init(uint8_t can_number);
3030
void can_init_all(void);
31+
bool can_tx_check_min_slots_free(uint32_t min);
3132
void can_send(CAN_FIFOMailBox_TypeDef *to_push, uint8_t bus_number, bool skip_tx_hook);
3233
bool can_pop(can_ring *q, CAN_FIFOMailBox_TypeDef *elem);
3334

@@ -107,6 +108,20 @@ bool can_push(can_ring *q, CAN_FIFOMailBox_TypeDef *elem) {
107108
return ret;
108109
}
109110

111+
uint32_t can_slots_empty(can_ring *q) {
112+
uint32_t ret = 0;
113+
114+
ENTER_CRITICAL();
115+
if (q->w_ptr >= q->r_ptr) {
116+
ret = q->fifo_size - 1U - q->w_ptr + q->r_ptr;
117+
} else {
118+
ret = q->r_ptr - q->w_ptr - 1U;
119+
}
120+
EXIT_CRITICAL();
121+
122+
return ret;
123+
}
124+
110125
void can_clear(can_ring *q) {
111126
ENTER_CRITICAL();
112127
q->w_ptr = 0;
@@ -317,6 +332,10 @@ void process_can(uint8_t can_number) {
317332
CAN->sTxMailBox[0].TDHR = to_send.RDHR;
318333
CAN->sTxMailBox[0].TDTR = to_send.RDTR;
319334
CAN->sTxMailBox[0].TIR = to_send.RIR;
335+
336+
if (can_tx_check_min_slots_free(MAX_CAN_MSGS_PER_BULK_TRANSFER)) {
337+
usb_outep3_resume_if_paused();
338+
}
320339
}
321340
}
322341

@@ -405,6 +424,14 @@ void CAN3_TX_IRQ_Handler(void) { process_can(2); }
405424
void CAN3_RX0_IRQ_Handler(void) { can_rx(2); }
406425
void CAN3_SCE_IRQ_Handler(void) { can_sce(CAN3); }
407426

427+
bool can_tx_check_min_slots_free(uint32_t min) {
428+
return
429+
(can_slots_empty(&can_tx1_q) >= min) &&
430+
(can_slots_empty(&can_tx2_q) >= min) &&
431+
(can_slots_empty(&can_tx3_q) >= min) &&
432+
(can_slots_empty(&can_txgmlan_q) >= min);
433+
}
434+
408435
void can_send(CAN_FIFOMailBox_TypeDef *to_push, uint8_t bus_number, bool skip_tx_hook) {
409436
if (skip_tx_hook || safety_tx_hook(to_push) != 0) {
410437
if (bus_number < BUS_MAX) {

‎board/drivers/llcan.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#define GET_BUS(msg) (((msg)->RDTR >> 4) & 0xFF)
1212
#define GET_LEN(msg) ((msg)->RDTR & 0xF)
1313
#define GET_ADDR(msg) ((((msg)->RIR & 4) != 0) ? ((msg)->RIR >> 3) : ((msg)->RIR >> 21))
14-
#define GET_BYTE(msg, b) (((int)(b) > 3) ? (((msg)->RDHR >> (8U * ((unsigned int)(b) % 4U))) & 0XFFU) : (((msg)->RDLR >> (8U * (unsigned int)(b))) & 0xFFU))
14+
#define GET_BYTE(msg, b) (((int)(b) > 3) ? (((msg)->RDHR >> (8U * ((unsigned int)(b) % 4U))) & 0xFFU) : (((msg)->RDLR >> (8U * (unsigned int)(b))) & 0xFFU))
1515
#define GET_BYTES_04(msg) ((msg)->RDLR)
1616
#define GET_BYTES_48(msg) ((msg)->RDHR)
1717

‎board/drivers/usb.h

+21-4
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,16 @@ typedef union _USB_Setup {
2323
}
2424
USB_Setup_TypeDef;
2525

26+
#define MAX_CAN_MSGS_PER_BULK_TRANSFER 4U
27+
2628
void usb_init(void);
2729
int usb_cb_control_msg(USB_Setup_TypeDef *setup, uint8_t *resp, bool hardwired);
2830
int usb_cb_ep1_in(void *usbdata, int len, bool hardwired);
2931
void usb_cb_ep2_out(void *usbdata, int len, bool hardwired);
3032
void usb_cb_ep3_out(void *usbdata, int len, bool hardwired);
33+
void usb_cb_ep3_out_complete(void);
3134
void usb_cb_enumeration_complete(void);
35+
void usb_outep3_resume_if_paused(void);
3236

3337
// **** supporting defines ****
3438

@@ -380,6 +384,7 @@ USB_Setup_TypeDef setup;
380384
uint8_t usbdata[0x100];
381385
uint8_t* ep0_txdata = NULL;
382386
uint16_t ep0_txlen = 0;
387+
bool outep3_processing = false;
383388

384389
// Store the current interface alt setting.
385390
int current_int0_alt_setting = 0;
@@ -744,6 +749,7 @@ void usb_irqhandler(void) {
744749
}
745750

746751
if (endpoint == 3) {
752+
outep3_processing = true;
747753
usb_cb_ep3_out(usbdata, len, 1);
748754
}
749755
} else if (status == STS_SETUP_UPDT) {
@@ -816,15 +822,17 @@ void usb_irqhandler(void) {
816822
#ifdef DEBUG_USB
817823
puts(" OUT3 PACKET XFRC\n");
818824
#endif
819-
USBx_OUTEP(3)->DOEPTSIZ = (1U << 19) | 0x40U;
820-
USBx_OUTEP(3)->DOEPCTL |= USB_OTG_DOEPCTL_EPENA | USB_OTG_DOEPCTL_CNAK;
825+
// NAK cleared by process_can (if tx buffers have room)
826+
outep3_processing = false;
827+
usb_cb_ep3_out_complete();
821828
} else if ((USBx_OUTEP(3)->DOEPINT & 0x2000) != 0) {
822829
#ifdef DEBUG_USB
823830
puts(" OUT3 PACKET WTF\n");
824831
#endif
825832
// if NAK was set trigger this, unknown interrupt
826-
USBx_OUTEP(3)->DOEPTSIZ = (1U << 19) | 0x40U;
827-
USBx_OUTEP(3)->DOEPCTL |= USB_OTG_DOEPCTL_CNAK;
833+
// TODO: why was this here? fires when TX buffers when we can't clear NAK
834+
// USBx_OUTEP(3)->DOEPTSIZ = (1U << 19) | 0x40U;
835+
// USBx_OUTEP(3)->DOEPCTL |= USB_OTG_DOEPCTL_CNAK;
828836
} else if ((USBx_OUTEP(3)->DOEPINT) != 0) {
829837
puts("OUTEP3 error ");
830838
puth(USBx_OUTEP(3)->DOEPINT);
@@ -932,6 +940,15 @@ void usb_irqhandler(void) {
932940
//USBx->GINTMSK = 0xFFFFFFFF & ~(USB_OTG_GINTMSK_NPTXFEM | USB_OTG_GINTMSK_PTXFEM | USB_OTG_GINTSTS_SOF | USB_OTG_GINTSTS_EOPF);
933941
}
934942

943+
void usb_outep3_resume_if_paused() {
944+
ENTER_CRITICAL();
945+
if (!outep3_processing && (USBx_OUTEP(3)->DOEPCTL & USB_OTG_DOEPCTL_NAKSTS) != 0) {
946+
USBx_OUTEP(3)->DOEPTSIZ = (1U << 19) | 0x40U;
947+
USBx_OUTEP(3)->DOEPCTL |= USB_OTG_DOEPCTL_EPENA | USB_OTG_DOEPCTL_CNAK;
948+
}
949+
EXIT_CRITICAL();
950+
}
951+
935952
void OTG_FS_IRQ_Handler(void) {
936953
NVIC_DisableIRQ(OTG_FS_IRQn);
937954
//__disable_irq();

‎board/get_sdk_mac.sh

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#!/bin/bash
22
# Need formula for gcc
3+
sudo easy_install pip
4+
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
35
brew tap ArmMbed/homebrew-formulae
46
brew install python dfu-util arm-none-eabi-gcc
57
pip install --user libusb1 pycrypto requests

‎board/main.c

+15
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,12 @@ void usb_cb_ep3_out(void *usbdata, int len, bool hardwired) {
235235
}
236236
}
237237

238+
void usb_cb_ep3_out_complete() {
239+
if (can_tx_check_min_slots_free(MAX_CAN_MSGS_PER_BULK_TRANSFER)) {
240+
usb_outep3_resume_if_paused();
241+
}
242+
}
243+
238244
void usb_cb_enumeration_complete() {
239245
puts("USB enumeration complete\n");
240246
is_enumerated = 1;
@@ -469,6 +475,15 @@ int usb_cb_control_msg(USB_Setup_TypeDef *setup, uint8_t *resp, bool hardwired)
469475
can_init(CAN_NUM_FROM_BUS_NUM(setup->b.wValue.w));
470476
}
471477
break;
478+
// **** 0xdf: set unsafe mode
479+
case 0xdf:
480+
// you can only set this if you are in a non car safety mode
481+
if ((current_safety_mode == SAFETY_SILENT) ||
482+
(current_safety_mode == SAFETY_NOOUTPUT) ||
483+
(current_safety_mode == SAFETY_ELM327)) {
484+
unsafe_mode = setup->b.wValue.w;
485+
}
486+
break;
472487
// **** 0xe0: uart read
473488
case 0xe0:
474489
ur = get_ring_by_number(setup->b.wValue.w);

‎board/pedal/main.c

+1
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ void usb_cb_ep3_out(void *usbdata, int len, bool hardwired) {
7676
UNUSED(len);
7777
UNUSED(hardwired);
7878
}
79+
void usb_cb_ep3_out_complete(void) {}
7980
void usb_cb_enumeration_complete(void) {}
8081

8182
int usb_cb_control_msg(USB_Setup_TypeDef *setup, uint8_t *resp, bool hardwired) {

‎board/safety.h

+2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#define SAFETY_GM_ASCM 18U
3838
#define SAFETY_NOOUTPUT 19U
3939
#define SAFETY_HONDA_BOSCH_HARNESS 20U
40+
#define SAFETY_VOLKSWAGEN_PQ 21U
4041
#define SAFETY_SUBARU_LEGACY 22U
4142

4243
uint16_t current_safety_mode = SAFETY_SILENT;
@@ -203,6 +204,7 @@ const safety_hook_config safety_hook_registry[] = {
203204
{SAFETY_SUBARU_LEGACY, &subaru_legacy_hooks},
204205
{SAFETY_MAZDA, &mazda_hooks},
205206
{SAFETY_VOLKSWAGEN_MQB, &volkswagen_mqb_hooks},
207+
{SAFETY_VOLKSWAGEN_PQ, &volkswagen_pq_hooks},
206208
{SAFETY_NOOUTPUT, &nooutput_hooks},
207209
#ifdef ALLOW_DEBUG
208210
{SAFETY_CADILLAC, &cadillac_hooks},

‎board/safety/safety_chrysler.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ static int chrysler_rx_hook(CAN_FIFOMailBox_TypeDef *to_push) {
7474
chrysler_get_checksum, chrysler_compute_checksum,
7575
chrysler_get_counter);
7676

77+
bool unsafe_allow_gas = unsafe_mode & UNSAFE_DISABLE_DISENGAGE_ON_GAS;
78+
7779
if (valid && (GET_BUS(to_push) == 0)) {
7880
int addr = GET_ADDR(to_push);
7981

@@ -107,7 +109,7 @@ static int chrysler_rx_hook(CAN_FIFOMailBox_TypeDef *to_push) {
107109
// exit controls on rising edge of gas press
108110
if (addr == 308) {
109111
bool gas_pressed = (GET_BYTE(to_push, 5) & 0x7F) != 0;
110-
if (gas_pressed && !gas_pressed_prev && (chrysler_speed > CHRYSLER_GAS_THRSLD)) {
112+
if (!unsafe_allow_gas && gas_pressed && !gas_pressed_prev && (chrysler_speed > CHRYSLER_GAS_THRSLD)) {
111113
controls_allowed = 0;
112114
}
113115
gas_pressed_prev = gas_pressed;

‎board/safety/safety_ford.h

+7-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ static int ford_rx_hook(CAN_FIFOMailBox_TypeDef *to_push) {
1313

1414
int addr = GET_ADDR(to_push);
1515
int bus = GET_BUS(to_push);
16+
bool unsafe_allow_gas = unsafe_mode & UNSAFE_DISABLE_DISENGAGE_ON_GAS;
1617

1718
if (addr == 0x217) {
1819
// wheel speeds are 14 bits every 16
@@ -47,7 +48,7 @@ static int ford_rx_hook(CAN_FIFOMailBox_TypeDef *to_push) {
4748
// exit controls on rising edge of gas press
4849
if (addr == 0x204) {
4950
bool gas_pressed = ((GET_BYTE(to_push, 0) & 0x03) | GET_BYTE(to_push, 1)) != 0;
50-
if (gas_pressed && !gas_pressed_prev) {
51+
if (!unsafe_allow_gas && gas_pressed && !gas_pressed_prev) {
5152
controls_allowed = 0;
5253
}
5354
gas_pressed_prev = gas_pressed;
@@ -72,7 +73,11 @@ static int ford_tx_hook(CAN_FIFOMailBox_TypeDef *to_send) {
7273

7374
// disallow actuator commands if gas or brake (with vehicle moving) are pressed
7475
// and the the latching controls_allowed flag is True
75-
int pedal_pressed = gas_pressed_prev || (brake_pressed_prev && ford_moving);
76+
int pedal_pressed = brake_pressed_prev && ford_moving;
77+
bool unsafe_allow_gas = unsafe_mode & UNSAFE_DISABLE_DISENGAGE_ON_GAS;
78+
if (!unsafe_allow_gas) {
79+
pedal_pressed = pedal_pressed || gas_pressed_prev;
80+
}
7681
bool current_controls_allowed = controls_allowed && !(pedal_pressed);
7782

7883
if (relay_malfunction) {

‎board/safety/safety_gm.h

+8-2
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ static int gm_rx_hook(CAN_FIFOMailBox_TypeDef *to_push) {
4444
bool valid = addr_safety_check(to_push, gm_rx_checks, GM_RX_CHECK_LEN,
4545
NULL, NULL, NULL);
4646

47+
bool unsafe_allow_gas = unsafe_mode & UNSAFE_DISABLE_DISENGAGE_ON_GAS;
48+
4749
if (valid && (GET_BUS(to_push) == 0)) {
4850
int addr = GET_ADDR(to_push);
4951

@@ -91,7 +93,7 @@ static int gm_rx_hook(CAN_FIFOMailBox_TypeDef *to_push) {
9193
// exit controls on rising edge of gas press
9294
if (addr == 417) {
9395
bool gas_pressed = GET_BYTE(to_push, 6) != 0;
94-
if (gas_pressed && !gas_pressed_prev) {
96+
if (!unsafe_allow_gas && gas_pressed && !gas_pressed_prev) {
9597
controls_allowed = 0;
9698
}
9799
gas_pressed_prev = gas_pressed;
@@ -138,7 +140,11 @@ static int gm_tx_hook(CAN_FIFOMailBox_TypeDef *to_send) {
138140

139141
// disallow actuator commands if gas or brake (with vehicle moving) are pressed
140142
// and the the latching controls_allowed flag is True
141-
int pedal_pressed = gas_pressed_prev || (brake_pressed_prev && gm_moving);
143+
int pedal_pressed = brake_pressed_prev && gm_moving;
144+
bool unsafe_allow_gas = unsafe_mode & UNSAFE_DISABLE_DISENGAGE_ON_GAS;
145+
if (!unsafe_allow_gas) {
146+
pedal_pressed = pedal_pressed || gas_pressed_prev;
147+
}
142148
bool current_controls_allowed = controls_allowed && !pedal_pressed;
143149

144150
// BRAKE: safety check

‎board/safety/safety_honda.h

+25-16
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ static int honda_rx_hook(CAN_FIFOMailBox_TypeDef *to_push) {
7272
honda_get_checksum, honda_compute_checksum, honda_get_counter);
7373
}
7474

75+
bool unsafe_allow_gas = unsafe_mode & UNSAFE_DISABLE_DISENGAGE_ON_GAS;
76+
7577
if (valid) {
7678
int addr = GET_ADDR(to_push);
7779
int len = GET_LEN(to_push);
@@ -121,7 +123,7 @@ static int honda_rx_hook(CAN_FIFOMailBox_TypeDef *to_push) {
121123
if ((addr == 0x201) && (len == 6)) {
122124
gas_interceptor_detected = 1;
123125
int gas_interceptor = GET_INTERCEPTOR(to_push);
124-
if ((gas_interceptor > HONDA_GAS_INTERCEPTOR_THRESHOLD) &&
126+
if (!unsafe_allow_gas && (gas_interceptor > HONDA_GAS_INTERCEPTOR_THRESHOLD) &&
125127
(gas_interceptor_prev <= HONDA_GAS_INTERCEPTOR_THRESHOLD)) {
126128
controls_allowed = 0;
127129
}
@@ -132,24 +134,28 @@ static int honda_rx_hook(CAN_FIFOMailBox_TypeDef *to_push) {
132134
if (!gas_interceptor_detected) {
133135
if (addr == 0x17C) {
134136
bool gas_pressed = GET_BYTE(to_push, 0) != 0;
135-
if (gas_pressed && !gas_pressed_prev) {
137+
if (!unsafe_allow_gas && gas_pressed && !gas_pressed_prev) {
136138
controls_allowed = 0;
137139
}
138140
gas_pressed_prev = gas_pressed;
139141
}
140142
}
141-
if ((bus == 2) && (addr == 0x1FA)) {
142-
bool honda_stock_aeb = GET_BYTE(to_push, 3) & 0x20;
143-
int honda_stock_brake = (GET_BYTE(to_push, 0) << 2) + ((GET_BYTE(to_push, 1) >> 6) & 0x3);
144-
145-
// Forward AEB when stock braking is higher than openpilot braking
146-
// only stop forwarding when AEB event is over
147-
if (!honda_stock_aeb) {
148-
honda_fwd_brake = false;
149-
} else if (honda_stock_brake >= honda_brake) {
150-
honda_fwd_brake = true;
151-
} else {
152-
// Leave Honda forward brake as is
143+
144+
// disable stock Honda AEB in unsafe mode
145+
if ( !(unsafe_mode & UNSAFE_DISABLE_STOCK_AEB) ) {
146+
if ((bus == 2) && (addr == 0x1FA)) {
147+
bool honda_stock_aeb = GET_BYTE(to_push, 3) & 0x20;
148+
int honda_stock_brake = (GET_BYTE(to_push, 0) << 2) + ((GET_BYTE(to_push, 1) >> 6) & 0x3);
149+
150+
// Forward AEB when stock braking is higher than openpilot braking
151+
// only stop forwarding when AEB event is over
152+
if (!honda_stock_aeb) {
153+
honda_fwd_brake = false;
154+
} else if (honda_stock_brake >= honda_brake) {
155+
honda_fwd_brake = true;
156+
} else {
157+
// Leave Honda forward brake as is
158+
}
153159
}
154160
}
155161

@@ -192,8 +198,11 @@ static int honda_tx_hook(CAN_FIFOMailBox_TypeDef *to_send) {
192198

193199
// disallow actuator commands if gas or brake (with vehicle moving) are pressed
194200
// and the the latching controls_allowed flag is True
195-
int pedal_pressed = gas_pressed_prev || (gas_interceptor_prev > HONDA_GAS_INTERCEPTOR_THRESHOLD) ||
196-
(brake_pressed_prev && honda_moving);
201+
int pedal_pressed = brake_pressed_prev && honda_moving;
202+
bool unsafe_allow_gas = unsafe_mode & UNSAFE_DISABLE_DISENGAGE_ON_GAS;
203+
if (!unsafe_allow_gas) {
204+
pedal_pressed = pedal_pressed || gas_pressed_prev || (gas_interceptor_prev > HONDA_GAS_INTERCEPTOR_THRESHOLD);
205+
}
197206
bool current_controls_allowed = controls_allowed && !(pedal_pressed);
198207

199208
// BRAKE: safety check

‎board/safety/safety_hyundai.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ static int hyundai_rx_hook(CAN_FIFOMailBox_TypeDef *to_push) {
3030
bool valid = addr_safety_check(to_push, hyundai_rx_checks, HYUNDAI_RX_CHECK_LEN,
3131
NULL, NULL, NULL);
3232

33+
bool unsafe_allow_gas = unsafe_mode & UNSAFE_DISABLE_DISENGAGE_ON_GAS;
34+
3335
if (valid && GET_BUS(to_push) == 0) {
3436
int addr = GET_ADDR(to_push);
3537

@@ -55,7 +57,7 @@ static int hyundai_rx_hook(CAN_FIFOMailBox_TypeDef *to_push) {
5557
// exit controls on rising edge of gas press
5658
if (addr == 608) {
5759
bool gas_pressed = (GET_BYTE(to_push, 7) >> 6) != 0;
58-
if (gas_pressed && !gas_pressed_prev) {
60+
if (!unsafe_allow_gas && gas_pressed && !gas_pressed_prev) {
5961
controls_allowed = 0;
6062
}
6163
gas_pressed_prev = gas_pressed;

0 commit comments

Comments
 (0)