Skip to content

Commit

Permalink
Merge branch 'upstream/panda/master' into palisade-2023-port-1
Browse files Browse the repository at this point in the history
  • Loading branch information
sunnyhaibin committed Mar 27, 2023
2 parents 7f77ee9 + d70fa4e commit f179513
Show file tree
Hide file tree
Showing 61 changed files with 837 additions and 397 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pip install -r requirements.txt

# macOS
brew tap ArmMbed/homebrew-formulae
brew install python dfu-util arm-none-eabi-gcc
brew install python dfu-util arm-none-eabi-gcc gcc@12
pip install -r requirements.txt
```

Expand Down
5 changes: 4 additions & 1 deletion board/boards/black.h
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,9 @@ const board board_black = {
.has_canfd = false,
.has_rtc_battery = false,
.fan_max_rpm = 0U,
.adc_scale = 8862U,
.fan_stall_recovery = false,
.fan_enable_cooldown_time = 0U,
.init = black_init,
.enable_can_transceiver = black_enable_can_transceiver,
.enable_can_transceivers = black_enable_can_transceivers,
Expand All @@ -204,4 +207,4 @@ const board board_black = {
.set_phone_power = unused_set_phone_power,
.set_siren = unused_set_siren,
.read_som_gpio = unused_read_som_gpio
};
};
3 changes: 3 additions & 0 deletions board/boards/board_declarations.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ struct board {
const bool has_canfd;
const bool has_rtc_battery;
const uint16_t fan_max_rpm;
const uint16_t adc_scale;
const bool fan_stall_recovery;
const uint8_t fan_enable_cooldown_time;
board_init init;
board_enable_can_transceiver enable_can_transceiver;
board_enable_can_transceivers enable_can_transceivers;
Expand Down
11 changes: 5 additions & 6 deletions board/boards/dos.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,12 +140,8 @@ void dos_init(void) {
set_gpio_output(GPIOC, 11, 1);

#ifdef ENABLE_SPI
// A4-A7: SPI
set_gpio_alternate(GPIOA, 4, GPIO_AF5_SPI1);
set_gpio_alternate(GPIOA, 5, GPIO_AF5_SPI1);
set_gpio_alternate(GPIOA, 6, GPIO_AF5_SPI1);
set_gpio_alternate(GPIOA, 7, GPIO_AF5_SPI1);
register_set_bits(&(GPIOA->OSPEEDR), GPIO_OSPEEDER_OSPEEDR4 | GPIO_OSPEEDER_OSPEEDR5 | GPIO_OSPEEDER_OSPEEDR6 | GPIO_OSPEEDER_OSPEEDR7);
// SPI init
gpio_spi_init();
#endif

// C8: FAN PWM aka TIM3_CH3
Expand Down Expand Up @@ -219,6 +215,9 @@ const board board_dos = {
.has_canfd = false,
.has_rtc_battery = true,
.fan_max_rpm = 6500U,
.adc_scale = 8862U,
.fan_stall_recovery = true,
.fan_enable_cooldown_time = 0U,
.init = dos_init,
.enable_can_transceiver = dos_enable_can_transceiver,
.enable_can_transceivers = dos_enable_can_transceivers,
Expand Down
3 changes: 3 additions & 0 deletions board/boards/grey.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ const board board_grey = {
.has_canfd = false,
.has_rtc_battery = false,
.fan_max_rpm = 0U,
.adc_scale = 8862U,
.fan_stall_recovery = false,
.fan_enable_cooldown_time = 0U,
.init = grey_init,
.enable_can_transceiver = white_enable_can_transceiver,
.enable_can_transceivers = white_enable_can_transceivers,
Expand Down
3 changes: 3 additions & 0 deletions board/boards/pedal.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ const board board_pedal = {
.has_canfd = false,
.has_rtc_battery = false,
.fan_max_rpm = 0U,
.adc_scale = 8862U,
.fan_stall_recovery = false,
.fan_enable_cooldown_time = 0U,
.init = pedal_init,
.enable_can_transceiver = pedal_enable_can_transceiver,
.enable_can_transceivers = pedal_enable_can_transceivers,
Expand Down
3 changes: 3 additions & 0 deletions board/boards/red.h
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,9 @@ const board board_red = {
.has_canfd = true,
.has_rtc_battery = false,
.fan_max_rpm = 0U,
.adc_scale = 5539U,
.fan_stall_recovery = false,
.fan_enable_cooldown_time = 0U,
.init = red_init,
.enable_can_transceiver = red_enable_can_transceiver,
.enable_can_transceivers = red_enable_can_transceivers,
Expand Down
5 changes: 1 addition & 4 deletions board/boards/red_chiplet.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,10 @@ void red_chiplet_init(void) {
set_gpio_pullup(GPIOD, 3, PULL_NONE);
set_gpio_mode(GPIOD, 3, MODE_OUTPUT);

//B0: 5VOUT_S
// B0: 5VOUT_S
set_gpio_pullup(GPIOB, 0, PULL_NONE);
set_gpio_mode(GPIOB, 0, MODE_ANALOG);

// Turn on USB load switch.
red_chiplet_set_fan_or_usb_load_switch(true);

// Initialize harness
harness_init();

Expand Down
13 changes: 12 additions & 1 deletion board/boards/red_v2.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@
// Red Panda V2 with chiplet + Harness //
// ///////////////////// //

void red_panda_v2_init(void) {
// common chiplet init
red_chiplet_init();

// Turn on USB load switch
red_chiplet_set_fan_or_usb_load_switch(true);
}

const board board_red_v2 = {
.board_type = "Red_v2",
.board_tick = unused_board_tick,
Expand All @@ -14,7 +22,10 @@ const board board_red_v2 = {
.has_canfd = true,
.has_rtc_battery = true,
.fan_max_rpm = 0U,
.init = red_chiplet_init,
.adc_scale = 5539U,
.fan_stall_recovery = false,
.fan_enable_cooldown_time = 0U,
.init = red_panda_v2_init,
.enable_can_transceiver = red_chiplet_enable_can_transceiver,
.enable_can_transceivers = red_chiplet_enable_can_transceivers,
.set_led = red_set_led,
Expand Down
11 changes: 5 additions & 6 deletions board/boards/tres.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,7 @@ void tres_init(void) {
uart_init(&uart_ring_som_debug, 115200);

// SPI init
set_gpio_alternate(GPIOE, 11, GPIO_AF5_SPI4);
set_gpio_alternate(GPIOE, 12, GPIO_AF5_SPI4);
set_gpio_alternate(GPIOE, 13, GPIO_AF5_SPI4);
set_gpio_alternate(GPIOE, 14, GPIO_AF5_SPI4);
register_set_bits(&(GPIOE->OSPEEDR), GPIO_OSPEEDR_OSPEED11 | GPIO_OSPEEDR_OSPEED12 | GPIO_OSPEEDR_OSPEED13 | GPIO_OSPEEDR_OSPEED14);
gpio_spi_init();

// fan setup
set_gpio_alternate(GPIOC, 8, GPIO_AF2_TIM3);
Expand Down Expand Up @@ -97,7 +93,10 @@ const board board_tres = {
.has_spi = true,
.has_canfd = true,
.has_rtc_battery = true,
.fan_max_rpm = 6500U, // TODO: verify this, copied from dos
.fan_max_rpm = 6600U,
.adc_scale = 3021U,
.fan_stall_recovery = false,
.fan_enable_cooldown_time = 3U,
.init = tres_init,
.enable_can_transceiver = red_chiplet_enable_can_transceiver,
.enable_can_transceivers = red_chiplet_enable_can_transceivers,
Expand Down
5 changes: 4 additions & 1 deletion board/boards/uno.h
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ void uno_init(void) {
}

// Switch to phone usb mode if harness connection is powered by less than 7V
if(adc_get_voltage() < 7000U){
if(adc_get_voltage(current_board->adc_scale) < 7000U){
uno_set_usb_switch(true);
} else {
uno_set_usb_switch(false);
Expand Down Expand Up @@ -251,6 +251,9 @@ const board board_uno = {
.has_canfd = false,
.has_rtc_battery = true,
.fan_max_rpm = 5100U,
.adc_scale = 8862U,
.fan_stall_recovery = false,
.fan_enable_cooldown_time = 0U,
.init = uno_init,
.enable_can_transceiver = uno_enable_can_transceiver,
.enable_can_transceivers = uno_enable_can_transceivers,
Expand Down
5 changes: 4 additions & 1 deletion board/boards/white.h
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ void white_grey_common_init(void) {
white_set_can_mode(CAN_MODE_NORMAL);

// Init usb power mode
uint32_t voltage = adc_get_voltage();
uint32_t voltage = adc_get_voltage(current_board->adc_scale);
// Init in CDP mode only if panda is powered by 12V.
// Otherwise a PC would not be able to flash a standalone panda
if (voltage > 8000U) { // 8V threshold
Expand Down Expand Up @@ -247,6 +247,9 @@ const board board_white = {
.has_canfd = false,
.has_rtc_battery = false,
.fan_max_rpm = 0U,
.adc_scale = 8862U,
.fan_stall_recovery = false,
.fan_enable_cooldown_time = 0U,
.init = white_init,
.enable_can_transceiver = white_enable_can_transceiver,
.enable_can_transceivers = white_enable_can_transceivers,
Expand Down
4 changes: 2 additions & 2 deletions board/drivers/fake_siren.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ void fake_siren_codec_enable(bool enabled) {
bool success = false;
success &= i2c_set_reg_bits(I2C5, CODEC_I2C_ADDR, 0x2B, (1U << 1)); // Left speaker mix from INA1
success &= i2c_set_reg_bits(I2C5, CODEC_I2C_ADDR, 0x2C, (1U << 1)); // Right speaker mix from INA1
success &= i2c_set_reg_mask(I2C5, CODEC_I2C_ADDR, 0x3D, 0x1F, 0b11111); // Left speaker volume
success &= i2c_set_reg_mask(I2C5, CODEC_I2C_ADDR, 0x3E, 0x1F, 0b11111); // Right speaker volume
success &= i2c_set_reg_mask(I2C5, CODEC_I2C_ADDR, 0x3D, 0x17, 0b11111); // Left speaker volume
success &= i2c_set_reg_mask(I2C5, CODEC_I2C_ADDR, 0x3E, 0x17, 0b11111); // Right speaker volume
success &= i2c_set_reg_mask(I2C5, CODEC_I2C_ADDR, 0x37, 0b101, 0b111); // INA gain
success &= i2c_set_reg_bits(I2C5, CODEC_I2C_ADDR, 0x4C, (1U << 7)); // Enable INA
success &= i2c_set_reg_bits(I2C5, CODEC_I2C_ADDR, 0x51, (1U << 7)); // Disable global shutdown
Expand Down
45 changes: 30 additions & 15 deletions board/drivers/fan.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ struct fan_state_t {
uint8_t power;
float error_integral;
uint8_t stall_counter;
uint8_t cooldown_counter;
} fan_state_t;
struct fan_state_t fan_state;

Expand All @@ -15,6 +16,10 @@ void fan_set_power(uint8_t percentage){
fan_state.target_rpm = ((current_board->fan_max_rpm * MIN(100U, MAX(0U, percentage))) / 100U);
}

void fan_reset_cooldown(void){
fan_state.cooldown_counter = current_board->fan_enable_cooldown_time * 8U;
}

// Call this at 8Hz
void fan_tick(void){
if (current_board->fan_max_rpm > 0U) {
Expand All @@ -24,26 +29,31 @@ void fan_tick(void){
fan_state.rpm = (fan_rpm_fast + (3U * fan_state.rpm)) / 4U;

// Enable fan if we want it to spin
current_board->set_fan_enabled(fan_state.target_rpm > 0U);
current_board->set_fan_enabled((fan_state.target_rpm > 0U) || (fan_state.cooldown_counter > 0U));
if (fan_state.target_rpm > 0U) {
fan_reset_cooldown();
}

// Stall detection
if (fan_state.power > 0U) {
if (fan_rpm_fast == 0U) {
fan_state.stall_counter = MIN(fan_state.stall_counter + 1U, 255U);
} else {
fan_state.stall_counter = 0U;
}
if (current_board->fan_stall_recovery) {
if (fan_state.power > 0U) {
if (fan_rpm_fast == 0U) {
fan_state.stall_counter = MIN(fan_state.stall_counter + 1U, 255U);
} else {
fan_state.stall_counter = 0U;
}

if (fan_state.stall_counter > FAN_STALL_THRESHOLD) {
// Stall detected, power cycling fan controller
current_board->set_fan_enabled(false);
if (fan_state.stall_counter > FAN_STALL_THRESHOLD) {
// Stall detected, power cycling fan controller
current_board->set_fan_enabled(false);

// clip integral, can't fully reset otherwise we may always be stuck in stall detection
fan_state.error_integral = MIN(50.0f, MAX(0.0f, fan_state.error_integral));
// clip integral, can't fully reset otherwise we may always be stuck in stall detection
fan_state.error_integral = MIN(50.0f, MAX(0.0f, fan_state.error_integral));
}
} else {
fan_state.stall_counter = 0U;
fan_state.error_integral = 0.0f;
}
} else {
fan_state.stall_counter = 0U;
fan_state.error_integral = 0.0f;
}

// Update controller
Expand All @@ -55,5 +65,10 @@ void fan_tick(void){

fan_state.power = MIN(100U, MAX(0U, feedforward + fan_state.error_integral));
pwm_set(TIM3, 3, fan_state.power);

// Cooldown counter
if (fan_state.cooldown_counter > 0U) {
fan_state.cooldown_counter--;
}
}
}
13 changes: 13 additions & 0 deletions board/flasher.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
uint32_t *prog_ptr = NULL;
bool unlocked = false;

void spi_init(void);

#ifdef uart_ring
void debug_ring_callback(uart_ring *ring) {}
#endif
Expand Down Expand Up @@ -41,6 +43,11 @@ int comms_control_handler(ControlPacket_t *req, uint8_t *resp) {
resp[1] = 0xff;
}
break;
// **** 0xc1: get hardware type
case 0xc1:
resp[0] = hw_type;
resp_len = 1;
break;
// **** 0xc3: fetch MCU UID
case 0xc3:
#ifdef UID_BASE
Expand Down Expand Up @@ -289,6 +296,12 @@ void soft_flasher_start(void) {
// enable USB
usb_init();

// enable SPI
if (current_board->has_spi) {
gpio_spi_init();
spi_init();
}

// green LED on for flashing
current_board->set_led(LED_GREEN, 1);

Expand Down
2 changes: 1 addition & 1 deletion board/main_comms.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ int get_health_pkt(void *dat) {
struct health_t * health = (struct health_t*)dat;

health->uptime_pkt = uptime_cnt;
health->voltage_pkt = adc_get_voltage();
health->voltage_pkt = adc_get_voltage(current_board->adc_scale);
health->current_pkt = current_board->read_current();

//Use the GPIO pin to determine ignition or use a CAN based logic
Expand Down
12 changes: 10 additions & 2 deletions board/safety.h
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ void update_counter(AddrCheckStruct addr_list[], int index, uint8_t counter) {
bool is_msg_valid(AddrCheckStruct addr_list[], int index) {
bool valid = true;
if (index != -1) {
if ((!addr_list[index].valid_checksum) || (addr_list[index].wrong_counters >= MAX_WRONG_COUNTERS)) {
if (!addr_list[index].valid_checksum || !addr_list[index].valid_quality_flag || (addr_list[index].wrong_counters >= MAX_WRONG_COUNTERS)) {
valid = false;
controls_allowed = 0;
}
Expand All @@ -215,7 +215,8 @@ bool addr_safety_check(CANPacket_t *to_push,
const addr_checks *rx_checks,
uint32_t (*get_checksum)(CANPacket_t *to_push),
uint32_t (*compute_checksum)(CANPacket_t *to_push),
uint8_t (*get_counter)(CANPacket_t *to_push)) {
uint8_t (*get_counter)(CANPacket_t *to_push),
bool (*get_quality_flag_valid)(CANPacket_t *to_push)) {

int index = get_addr_check_index(to_push, rx_checks->check, rx_checks->len);
update_addr_timestamp(rx_checks->check, index);
Expand All @@ -237,6 +238,13 @@ bool addr_safety_check(CANPacket_t *to_push,
} else {
rx_checks->check[index].wrong_counters = 0U;
}

// quality flag check
if ((get_quality_flag_valid != NULL) && rx_checks->check[index].msg[rx_checks->check[index].index].quality_flag) {
rx_checks->check[index].valid_quality_flag = get_quality_flag_valid(to_push);
} else {
rx_checks->check[index].valid_quality_flag = true;
}
}
return is_msg_valid(rx_checks->check, index);
}
Expand Down
2 changes: 1 addition & 1 deletion board/safety/safety_body.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ addr_checks body_rx_checks = {body_addr_checks, BODY_ADDR_CHECK_LEN};

static int body_rx_hook(CANPacket_t *to_push) {

bool valid = addr_safety_check(to_push, &body_rx_checks, NULL, NULL, NULL);
bool valid = addr_safety_check(to_push, &body_rx_checks, NULL, NULL, NULL, NULL);

controls_allowed = valid;

Expand Down
4 changes: 2 additions & 2 deletions board/safety/safety_chrysler.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const SteeringLimits CHRYSLER_STEERING_LIMITS = {
};

const SteeringLimits CHRYSLER_RAM_DT_STEERING_LIMITS = {
.max_steer = 261,
.max_steer = 350,
.max_rt_delta = 112,
.max_rt_interval = 250000,
.max_rate_up = 6,
Expand Down Expand Up @@ -180,7 +180,7 @@ static int chrysler_rx_hook(CANPacket_t *to_push) {

bool valid = addr_safety_check(to_push, &chrysler_rx_checks,
chrysler_get_checksum, chrysler_compute_checksum,
chrysler_get_counter);
chrysler_get_counter, NULL);

const int bus = GET_BUS(to_push);
const int addr = GET_ADDR(to_push);
Expand Down
Loading

0 comments on commit f179513

Please sign in to comment.