Skip to content

Commit

Permalink
Squashed 'panda/' changes from 869f123..b2c720b
Browse files Browse the repository at this point in the history
b2c720b Dos (#533)
01bf740 remove 0x1BE checksum test
0bd06c9 remove 0x1BE check (breaks some vehicles)
c31b899 honda bosch longitudinal safety
66250c4 Disable docker layer caching (#534)
6b19fa4 include nissan safety in release build
db31886 gate mazda safety behind debug flag
e4558c0 Safety: message length check on RX and TX (#529)

git-subtree-dir: panda
git-subtree-split: b2c720b
  • Loading branch information
Vehicle Researcher committed May 17, 2020
1 parent eb0dff8 commit eb1351c
Show file tree
Hide file tree
Showing 20 changed files with 526 additions and 159 deletions.
12 changes: 6 additions & 6 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ version: 2
jobs:
safety:
machine:
docker_layer_caching: true
image: ubuntu-1604:202004-01
steps:
- checkout
- run:
Expand All @@ -15,7 +15,7 @@ jobs:
misra-c2012:
machine:
docker_layer_caching: true
image: ubuntu-1604:202004-01
steps:
- checkout
- run:
Expand All @@ -35,7 +35,7 @@ jobs:

build:
machine:
docker_layer_caching: true
image: ubuntu-1604:202004-01
steps:
- checkout
- run:
Expand Down Expand Up @@ -68,7 +68,7 @@ jobs:
safety_replay:
machine:
docker_layer_caching: true
image: ubuntu-1604:202004-01
steps:
- checkout
- run:
Expand All @@ -81,7 +81,7 @@ jobs:
language_check:
machine:
docker_layer_caching: true
image: ubuntu-1604:202004-01
steps:
- checkout
- run:
Expand All @@ -94,7 +94,7 @@ jobs:
linter_python:
machine:
docker_layer_caching: true
image: ubuntu-1604:202004-01
steps:
- checkout
- run:
Expand Down
12 changes: 8 additions & 4 deletions board/board.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "boards/grey.h"
#include "boards/black.h"
#include "boards/uno.h"
#include "boards/dos.h"
#else
#include "boards/pedal.h"
#endif
Expand All @@ -22,7 +23,10 @@ void detect_board_type(void) {
// SPI lines floating: white (TODO: is this reliable? Not really, we have to enable ESP/GPS to be able to detect this on the UART)
set_gpio_output(GPIOC, 14, 1);
set_gpio_output(GPIOC, 5, 1);
if((detect_with_pull(GPIOA, 4, PULL_DOWN)) || (detect_with_pull(GPIOA, 5, PULL_DOWN)) || (detect_with_pull(GPIOA, 6, PULL_DOWN)) || (detect_with_pull(GPIOA, 7, PULL_DOWN))){
if(!detect_with_pull(GPIOB, 1, PULL_UP)){
hw_type = HW_TYPE_DOS;
current_board = &board_dos;
} else if((detect_with_pull(GPIOA, 4, PULL_DOWN)) || (detect_with_pull(GPIOA, 5, PULL_DOWN)) || (detect_with_pull(GPIOA, 6, PULL_DOWN)) || (detect_with_pull(GPIOA, 7, PULL_DOWN))){
hw_type = HW_TYPE_WHITE_PANDA;
current_board = &board_white;
} else if(detect_with_pull(GPIOA, 13, PULL_DOWN)) { // Rev AB deprecated, so no pullup means black. In REV C, A13 is pulled up to 5V with a 10K
Expand Down Expand Up @@ -78,17 +82,17 @@ bool board_has_gmlan(void) {
}

bool board_has_obd(void) {
return ((hw_type == HW_TYPE_BLACK_PANDA) || (hw_type == HW_TYPE_UNO));
return ((hw_type == HW_TYPE_BLACK_PANDA) || (hw_type == HW_TYPE_UNO) || (hw_type == HW_TYPE_DOS));
}

bool board_has_lin(void) {
return ((hw_type == HW_TYPE_WHITE_PANDA) || (hw_type == HW_TYPE_GREY_PANDA));
}

bool board_has_rtc(void) {
return (hw_type == HW_TYPE_UNO);
return ((hw_type == HW_TYPE_UNO) || (hw_type == HW_TYPE_DOS));
}

bool board_has_relay(void) {
return ((hw_type == HW_TYPE_BLACK_PANDA) || (hw_type == HW_TYPE_UNO));
return ((hw_type == HW_TYPE_BLACK_PANDA) || (hw_type == HW_TYPE_UNO) || (hw_type == HW_TYPE_DOS));
}
1 change: 1 addition & 0 deletions board/board_declarations.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ struct board {
#define HW_TYPE_BLACK_PANDA 3U
#define HW_TYPE_PEDAL 4U
#define HW_TYPE_UNO 5U
#define HW_TYPE_DOS 6U

// LED colors
#define LED_RED 0U
Expand Down
224 changes: 224 additions & 0 deletions board/boards/dos.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,224 @@
// ///////////// //
// Dos + Harness //
// ///////////// //

void dos_enable_can_transciever(uint8_t transciever, bool enabled) {
switch (transciever){
case 1U:
set_gpio_output(GPIOC, 1, !enabled);
break;
case 2U:
set_gpio_output(GPIOC, 13, !enabled);
break;
case 3U:
set_gpio_output(GPIOA, 0, !enabled);
break;
case 4U:
set_gpio_output(GPIOB, 10, !enabled);
break;
default:
puts("Invalid CAN transciever ("); puth(transciever); puts("): enabling failed\n");
break;
}
}

void dos_enable_can_transcievers(bool enabled) {
for(uint8_t i=1U; i<=4U; i++){
// Leave main CAN always on for CAN-based ignition detection
if((car_harness_status == HARNESS_STATUS_FLIPPED) ? (i == 3U) : (i == 1U)){
uno_enable_can_transciever(i, true);
} else {
uno_enable_can_transciever(i, enabled);
}
}
}

void dos_set_led(uint8_t color, bool enabled) {
switch (color){
case LED_RED:
set_gpio_output(GPIOC, 9, !enabled);
break;
case LED_GREEN:
set_gpio_output(GPIOC, 7, !enabled);
break;
case LED_BLUE:
set_gpio_output(GPIOC, 6, !enabled);
break;
default:
break;
}
}

void dos_set_gps_load_switch(bool enabled) {
UNUSED(enabled);
}

void dos_set_bootkick(bool enabled){
UNUSED(enabled);
}

void dos_bootkick(void) {}

void dos_set_phone_power(bool enabled){
UNUSED(enabled);
}

void dos_set_usb_power_mode(uint8_t mode) {
UNUSED(mode);
}

void dos_set_esp_gps_mode(uint8_t mode) {
UNUSED(mode);
}

void dos_set_can_mode(uint8_t mode){
switch (mode) {
case CAN_MODE_NORMAL:
case CAN_MODE_OBD_CAN2:
if ((bool)(mode == CAN_MODE_NORMAL) != (bool)(car_harness_status == HARNESS_STATUS_FLIPPED)) {
// B12,B13: disable OBD mode
set_gpio_mode(GPIOB, 12, MODE_INPUT);
set_gpio_mode(GPIOB, 13, MODE_INPUT);

// B5,B6: normal CAN2 mode
set_gpio_alternate(GPIOB, 5, GPIO_AF9_CAN2);
set_gpio_alternate(GPIOB, 6, GPIO_AF9_CAN2);
} else {
// B5,B6: disable normal CAN2 mode
set_gpio_mode(GPIOB, 5, MODE_INPUT);
set_gpio_mode(GPIOB, 6, MODE_INPUT);

// B12,B13: OBD mode
set_gpio_alternate(GPIOB, 12, GPIO_AF9_CAN2);
set_gpio_alternate(GPIOB, 13, GPIO_AF9_CAN2);
}
break;
default:
puts("Tried to set unsupported CAN mode: "); puth(mode); puts("\n");
break;
}
}

void dos_usb_power_mode_tick(uint32_t uptime){
UNUSED(uptime);
if(bootkick_timer != 0U){
bootkick_timer--;
} else {
dos_set_bootkick(false);
}
}

bool dos_check_ignition(void){
// ignition is checked through harness
return harness_check_ignition();
}

void dos_set_usb_switch(bool phone){
set_gpio_output(GPIOB, 3, phone);
}

void dos_set_ir_power(uint8_t percentage){
pwm_set(TIM4, 2, percentage);
}

void dos_set_fan_power(uint8_t percentage){
// Enable fan power only if percentage is non-zero.
set_gpio_output(GPIOA, 1, (percentage != 0U));
fan_set_power(percentage);
}

uint32_t dos_read_current(void){
// No current sense on Dos
return 0U;
}

void dos_init(void) {
common_init_gpio();

// A8,A15: normal CAN3 mode
set_gpio_alternate(GPIOA, 8, GPIO_AF11_CAN3);
set_gpio_alternate(GPIOA, 15, GPIO_AF11_CAN3);

// C0: OBD_SBU1 (orientation detection)
// C3: OBD_SBU2 (orientation detection)
set_gpio_mode(GPIOC, 0, MODE_ANALOG);
set_gpio_mode(GPIOC, 3, MODE_ANALOG);

// C10: OBD_SBU1_RELAY (harness relay driving output)
// C11: OBD_SBU2_RELAY (harness relay driving output)
set_gpio_mode(GPIOC, 10, MODE_OUTPUT);
set_gpio_mode(GPIOC, 11, MODE_OUTPUT);
set_gpio_output_type(GPIOC, 10, OUTPUT_TYPE_OPEN_DRAIN);
set_gpio_output_type(GPIOC, 11, OUTPUT_TYPE_OPEN_DRAIN);
set_gpio_output(GPIOC, 10, 1);
set_gpio_output(GPIOC, 11, 1);

// C8: FAN PWM aka TIM3_CH3
set_gpio_alternate(GPIOC, 8, GPIO_AF2_TIM3);

// Initialize IR PWM and set to 0%
set_gpio_alternate(GPIOB, 7, GPIO_AF2_TIM4);
pwm_init(TIM4, 2);
dos_set_ir_power(0U);

// Initialize fan and set to 0%
fan_init();
dos_set_fan_power(0U);

// Initialize harness
harness_init();

// Initialize RTC
rtc_init();

// Enable CAN transcievers
dos_enable_can_transcievers(true);

// Disable LEDs
dos_set_led(LED_RED, false);
dos_set_led(LED_GREEN, false);
dos_set_led(LED_BLUE, false);

// Set normal CAN mode
dos_set_can_mode(CAN_MODE_NORMAL);

// flip CAN0 and CAN2 if we are flipped
if (car_harness_status == HARNESS_STATUS_FLIPPED) {
can_flip_buses(0, 2);
}

// init multiplexer
can_set_obd(car_harness_status, false);
}

const harness_configuration dos_harness_config = {
.has_harness = true,
.GPIO_SBU1 = GPIOC,
.GPIO_SBU2 = GPIOC,
.GPIO_relay_SBU1 = GPIOC,
.GPIO_relay_SBU2 = GPIOC,
.pin_SBU1 = 0,
.pin_SBU2 = 3,
.pin_relay_SBU1 = 10,
.pin_relay_SBU2 = 11,
.adc_channel_SBU1 = 10,
.adc_channel_SBU2 = 13
};

const board board_dos = {
.board_type = "Dos",
.harness_config = &dos_harness_config,
.init = dos_init,
.enable_can_transciever = dos_enable_can_transciever,
.enable_can_transcievers = dos_enable_can_transcievers,
.set_led = dos_set_led,
.set_usb_power_mode = dos_set_usb_power_mode,
.set_esp_gps_mode = dos_set_esp_gps_mode,
.set_can_mode = dos_set_can_mode,
.usb_power_mode_tick = dos_usb_power_mode_tick,
.check_ignition = dos_check_ignition,
.read_current = dos_read_current,
.set_fan_power = dos_set_fan_power,
.set_ir_power = dos_set_ir_power,
.set_phone_power = dos_set_phone_power
};
1 change: 1 addition & 0 deletions board/drivers/llcan.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#define GET_BYTE(msg, b) (((int)(b) > 3) ? (((msg)->RDHR >> (8U * ((unsigned int)(b) % 4U))) & 0xFFU) : (((msg)->RDLR >> (8U * (unsigned int)(b))) & 0xFFU))
#define GET_BYTES_04(msg) ((msg)->RDLR)
#define GET_BYTES_48(msg) ((msg)->RDHR)
#define GET_FLAG(value, mask) (((__typeof__(mask))param & mask) == mask)

#define CAN_INIT_TIMEOUT_MS 500U
#define CAN_NAME_FROM_CANIF(CAN_DEV) (((CAN_DEV)==CAN1) ? "CAN1" : (((CAN_DEV) == CAN2) ? "CAN2" : "CAN3"))
Expand Down
18 changes: 12 additions & 6 deletions board/safety.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,14 @@ void gen_crc_lookup_table(uint8_t poly, uint8_t crc_lut[]) {
}
}

bool msg_allowed(int addr, int bus, const AddrBus addr_list[], int len) {
bool msg_allowed(CAN_FIFOMailBox_TypeDef *to_send, const CanMsg msg_list[], int len) {
int addr = GET_ADDR(to_send);
int bus = GET_BUS(to_send);
int length = GET_LEN(to_send);

bool allowed = false;
for (int i = 0; i < len; i++) {
if ((addr == addr_list[i].addr) && (bus == addr_list[i].bus)) {
if ((addr == msg_list[i].addr) && (bus == msg_list[i].bus) && (length == msg_list[i].len)) {
allowed = true;
break;
}
Expand All @@ -92,11 +96,13 @@ uint32_t get_ts_elapsed(uint32_t ts, uint32_t ts_last) {
int get_addr_check_index(CAN_FIFOMailBox_TypeDef *to_push, AddrCheckStruct addr_list[], const int len) {
int bus = GET_BUS(to_push);
int addr = GET_ADDR(to_push);
int length = GET_LEN(to_push);

int index = -1;
for (int i = 0; i < len; i++) {
for (uint8_t j = 0U; addr_list[i].addr[j] != 0; j++) {
if ((addr == addr_list[i].addr[j]) && (bus == addr_list[i].bus)) {
for (uint8_t j = 0U; addr_list[i].msg[j].addr != 0; j++) {
if ((addr == addr_list[i].msg[j].addr) && (bus == addr_list[i].msg[j].bus) &&
(length == addr_list[i].msg[j].len)) {
index = i;
goto Return;
}
Expand Down Expand Up @@ -209,13 +215,13 @@ const safety_hook_config safety_hook_registry[] = {
{SAFETY_CHRYSLER, &chrysler_hooks},
{SAFETY_SUBARU, &subaru_hooks},
{SAFETY_SUBARU_LEGACY, &subaru_legacy_hooks},
{SAFETY_MAZDA, &mazda_hooks},
{SAFETY_VOLKSWAGEN_MQB, &volkswagen_mqb_hooks},
{SAFETY_VOLKSWAGEN_PQ, &volkswagen_pq_hooks},
{SAFETY_NISSAN, &nissan_hooks},
{SAFETY_NOOUTPUT, &nooutput_hooks},
#ifdef ALLOW_DEBUG
{SAFETY_MAZDA, &mazda_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 eb1351c

Please sign in to comment.