Skip to content

Commit

Permalink
Squashed 'panda/' changes from 769ade0..d32345dd
Browse files Browse the repository at this point in the history
d32345dd Removed prints that were used for test, but cannot be compiled for the target
ba74138 Added BMW safety tests. Updated safety to limit maximum commanded target delta. Fixed safety bugs with speed scaling and other type bugs.
d3cde431 Testing helpers
c4c8cce7 Added dummy state to gmlan testing
9a2e2d6c Reduced repeated code
e1aef2e1 Made compatible with d301a59
ed075850 Merge branch 'master' of https://github.com/commaai/panda
437d7ac2 BMW hooks. Number commaai#12 used for safety.
74c8ee0 Subaru brake check (commaai#463)
4ecc6b3 Cleanup: avoid unnecessary bus checks in rx hooks
c7d0d5f Volkswagen safety updates: Phase 3 (commaai#462)
4368748 WIP: Toyota brake check.  (commaai#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 (commaai#460)
a379faf White Panda's Wi-Fi setup instructions (commaai#457)
11ef24b Improve tests (commaai#456)
fb02390 Subaru checksum counter (commaai#455)
9a44499 Fix Subaru Legacy Torque driver bug (commaai#454)
dfa6b07 separating subary legacy safety mode from global (commaai#452)
dad5858 Chrysler: add brakepress cancellation (commaai#451)
db94a5b Added Nissan safety (commaai#244)
f88af30e Merge remote-tracking branch 'remotes/Comma/master'
d7f1195 Chrysler Checksum/counter (commaai#450)
96e535e abstract crc function (commaai#448)
1b49d3e Hyundai: add gas disengage and tests (commaai#447)
598074c Volkswagen safety updates: Phase 2 (commaai#445)
b2ffaae Chrysler: disengage on gas press  (commaai#442)
2ebbe36 Subaru: disengage on gas press (commaai#446)
ccf75c4 Volkswagen safety updates: Phase 1 (commaai#444)
d686a2ce Fixed GMLAN switch tickle interrupt

git-subtree-dir: panda
git-subtree-split: d32345ddcf9f70872b54886fc6c949c20dcc8650
  • Loading branch information
dzid26 committed Feb 22, 2022
1 parent 6b8726e commit 0d14ed8
Show file tree
Hide file tree
Showing 39 changed files with 2,415 additions and 1,052 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ examples/output.csv
.DS_Store
.vscode
nosetests.xml
*bz2
16 changes: 16 additions & 0 deletions board/crc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
uint8_t crc_checksum(uint8_t *dat, int len, const uint8_t poly) {
uint8_t crc = 0xFF;
int i, j;
for (i = len - 1; i >= 0; i--) {
crc ^= dat[i];
for (j = 0; j < 8; j++) {
if ((crc & 0x80U) != 0U) {
crc = (uint8_t)((crc << 1) ^ poly);
}
else {
crc <<= 1;
}
}
}
return crc;
}
7 changes: 7 additions & 0 deletions board/drivers/can.h
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,13 @@ void ignition_can_hook(CAN_FIFOMailBox_TypeDef *to_push) {
ignition_can = GET_BYTES_04(to_push) != 0;
}
}
if (bus == 1) {
// BMW case
if ((addr == 0x130) && (len == 5)) {
// bits 22 and 23 are zero when ignition is on
ignition_can = (GET_BYTE(to_push, 3) & 0xC0) == 0;
}
}
}

// CAN receive handlers
Expand Down
5 changes: 4 additions & 1 deletion board/drivers/gmlan_alt.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,12 +148,15 @@ int inverted_bit_to_send = GMLAN_HIGH;
int gmlan_switch_below_timeout = -1;
int gmlan_switch_timeout_enable = 0;

void TIM4_IRQ_Handler(void);

void gmlan_switch_init(int timeout_enable) {
gmlan_switch_timeout_enable = timeout_enable;
gmlan_alt_mode = GPIO_SWITCH;
gmlan_switch_below_timeout = 1;
set_gpio_mode(GPIOB, 13, MODE_OUTPUT);


REGISTER_INTERRUPT(TIM4_IRQn, TIM4_IRQ_Handler, 40000U, FAULT_INTERRUPT_RATE_GMLAN)
setup_timer4();

inverted_bit_to_send = GMLAN_LOW; //We got initialized, set the output low
Expand Down
2 changes: 1 addition & 1 deletion board/drivers/llcan.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ bool llcan_set_speed(CAN_TypeDef *CAN_obj, uint32_t speed, bool loopback, bool s
void llcan_init(CAN_TypeDef *CAN_obj) {
// Enter init mode
register_set_bits(&(CAN_obj->FMR), CAN_FMR_FINIT);

// Wait for INAK bit to be set
while(((CAN_obj->MSR & CAN_MSR_INAK) == CAN_MSR_INAK)) {}

Expand Down
27 changes: 5 additions & 22 deletions board/pedal/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "drivers/timer.h"

#include "gpio.h"
#include "crc.h"

#define CAN CAN1

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

#endif

// ***************************** pedal can checksum *****************************

uint8_t pedal_checksum(uint8_t *dat, int len) {
uint8_t crc = 0xFF;
uint8_t poly = 0xD5; // standard crc8
int i, j;
for (i = len - 1; i >= 0; i--) {
crc ^= dat[i];
for (j = 0; j < 8; j++) {
if ((crc & 0x80U) != 0U) {
crc = (uint8_t)((crc << 1) ^ poly);
}
else {
crc <<= 1;
}
}
}
return crc;
}

// ***************************** can port *****************************

// addresses to be used on CAN
Expand Down Expand Up @@ -155,6 +136,8 @@ uint32_t current_index = 0;
#define FAULT_INVALID 6U
uint8_t state = FAULT_STARTUP;

const uint8_t crc_poly = 0xD5; // standard crc8

void CAN1_RX0_IRQ_Handler(void) {
while ((CAN->RF0R & CAN_RF0R_FMP0) != 0) {
#ifdef DEBUG
Expand Down Expand Up @@ -184,7 +167,7 @@ void CAN1_RX0_IRQ_Handler(void) {
uint16_t value_1 = (dat[2] << 8) | dat[3];
bool enable = ((dat[4] >> 7) & 1U) != 0U;
uint8_t index = dat[4] & COUNTER_CYCLE;
if (pedal_checksum(dat, CAN_GAS_SIZE - 1) == dat[5]) {
if (crc_checksum(dat, CAN_GAS_SIZE - 1, crc_poly) == dat[5]) {
if (((current_index + 1U) & COUNTER_CYCLE) == index) {
#ifdef DEBUG
puts("setting gas ");
Expand Down Expand Up @@ -247,7 +230,7 @@ void TIM3_IRQ_Handler(void) {
dat[2] = (pdl1 >> 8) & 0xFFU;
dat[3] = (pdl1 >> 0) & 0xFFU;
dat[4] = ((state & 0xFU) << 4) | pkt_idx;
dat[5] = pedal_checksum(dat, CAN_GAS_SIZE - 1);
dat[5] = crc_checksum(dat, CAN_GAS_SIZE - 1, crc_poly);
CAN->sTxMailBox[0].TDLR = dat[0] | (dat[1] << 8) | (dat[2] << 16) | (dat[3] << 24);
CAN->sTxMailBox[0].TDHR = dat[4] | (dat[5] << 8);
CAN->sTxMailBox[0].TDTR = 6; // len of packet is 5
Expand Down
30 changes: 25 additions & 5 deletions board/safety.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include "safety/safety_defaults.h"
#include "safety/safety_honda.h"
#include "safety/safety_toyota.h"
#include "safety/safety_toyota_ipas.h"
#include "safety/safety_bmw.h"
#include "safety/safety_tesla.h"
#include "safety/safety_gm_ascm.h"
#include "safety/safety_gm.h"
Expand All @@ -14,6 +14,7 @@
#include "safety/safety_chrysler.h"
#include "safety/safety_subaru.h"
#include "safety/safety_mazda.h"
#include "safety/safety_nissan.h"
#include "safety/safety_volkswagen.h"
#include "safety/safety_elm327.h"

Expand All @@ -30,13 +31,15 @@
#define SAFETY_CHRYSLER 9U
#define SAFETY_TESLA 10U
#define SAFETY_SUBARU 11U
#define SAFETY_BMW 12U
#define SAFETY_MAZDA 13U
#define SAFETY_VOLKSWAGEN 15U
#define SAFETY_TOYOTA_IPAS 16U
#define SAFETY_NISSAN 14U
#define SAFETY_VOLKSWAGEN_MQB 15U
#define SAFETY_ALLOUTPUT 17U
#define SAFETY_GM_ASCM 18U
#define SAFETY_NOOUTPUT 19U
#define SAFETY_HONDA_BOSCH_HARNESS 20U
#define SAFETY_SUBARU_LEGACY 22U

uint16_t current_safety_mode = SAFETY_SILENT;
const safety_hooks *current_hooks = &nooutput_hooks;
Expand All @@ -57,6 +60,21 @@ int safety_fwd_hook(int bus_num, CAN_FIFOMailBox_TypeDef *to_fwd) {
return current_hooks->fwd(bus_num, to_fwd);
}

// Given a CRC-8 poly, generate a static lookup table to use with a fast CRC-8
// algorithm. Called at init time for safety modes using CRC-8.
void gen_crc_lookup_table(uint8_t poly, uint8_t crc_lut[]) {
for (int i = 0; i < 256; i++) {
uint8_t crc = i;
for (int j = 0; j < 8; j++) {
if ((crc & 0x80U) != 0U)
crc = (uint8_t)((crc << 1) ^ poly);
else
crc <<= 1;
}
crc_lut[i] = crc;
}
}

bool msg_allowed(int addr, int bus, const AddrBus addr_list[], int len) {
bool allowed = false;
for (int i = 0; i < len; i++) {
Expand Down Expand Up @@ -184,13 +202,15 @@ const safety_hook_config safety_hook_registry[] = {
{SAFETY_HYUNDAI, &hyundai_hooks},
{SAFETY_CHRYSLER, &chrysler_hooks},
{SAFETY_SUBARU, &subaru_hooks},
{SAFETY_SUBARU_LEGACY, &subaru_legacy_hooks},
{SAFETY_MAZDA, &mazda_hooks},
{SAFETY_VOLKSWAGEN, &volkswagen_hooks},
{SAFETY_VOLKSWAGEN_MQB, &volkswagen_mqb_hooks},
{SAFETY_BMW, &bmw_hooks },
{SAFETY_NOOUTPUT, &nooutput_hooks},
#ifdef ALLOW_DEBUG
{SAFETY_CADILLAC, &cadillac_hooks},
{SAFETY_TOYOTA_IPAS, &toyota_ipas_hooks},
{SAFETY_TESLA, &tesla_hooks},
{SAFETY_NISSAN, &nissan_hooks},
{SAFETY_ALLOUTPUT, &alloutput_hooks},
{SAFETY_GM_ASCM, &gm_ascm_hooks},
{SAFETY_FORD, &ford_hooks},
Expand Down
Loading

0 comments on commit 0d14ed8

Please sign in to comment.