Skip to content

Commit

Permalink
Merge branch 'commaai:master' into secoc-long
Browse files Browse the repository at this point in the history
  • Loading branch information
chrispypatt authored Nov 2, 2024
2 parents 65ef827 + 0b364ec commit 4c4ef05
Show file tree
Hide file tree
Showing 15 changed files with 142 additions and 67 deletions.
6 changes: 5 additions & 1 deletion .github/workflows/drivers.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
name: drivers
on: [push, pull_request]
on:
push:
branches:
- master
pull_request:

jobs:
build_socketcan:
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ jobs:
run: ${{ env.RUN }} "scons -j4 --ubsan"
- name: Build jungle firmware with FINAL_PROVISIONING support
run: ${{ env.RUN }} "FINAL_PROVISIONING=1 scons -j4 board/jungle"
- name: Build panda in release mode
run: ${{ env.RUN }} "CERT=certs/debug RELEASE=1 scons -j4"

unit_tests:
name: unit tests
Expand Down
14 changes: 11 additions & 3 deletions board/boards/cuatro.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ static void cuatro_set_led(uint8_t color, bool enabled) {
set_gpio_output(GPIOD, 15, !enabled);
break;
case LED_GREEN:
set_gpio_output(GPIOD, 14, !enabled);
set_gpio_output(GPIOB, 2, !enabled);
break;
case LED_BLUE:
set_gpio_output(GPIOE, 2, !enabled);
Expand Down Expand Up @@ -71,12 +71,16 @@ static void cuatro_set_bootkick(BootState state) {
//set_gpio_output(GPIOC, 12, state != BOOT_RESET);
}

static void cuatro_set_siren(bool enabled){
beeper_enable(enabled);
}

static void cuatro_init(void) {
red_chiplet_init();

// init LEDs as open drain
set_gpio_output_type(GPIOE, 2, OUTPUT_TYPE_OPEN_DRAIN);
set_gpio_output_type(GPIOD, 14, OUTPUT_TYPE_OPEN_DRAIN);
set_gpio_output_type(GPIOB, 2, OUTPUT_TYPE_OPEN_DRAIN);
set_gpio_output_type(GPIOD, 15, OUTPUT_TYPE_OPEN_DRAIN);

// Power readout
Expand Down Expand Up @@ -120,6 +124,10 @@ static void cuatro_init(void) {

// Clock source
clock_source_init();

// Beeper
set_gpio_alternate(GPIOD, 14, GPIO_AF2_TIM4);
beeper_init();
}

board board_cuatro = {
Expand All @@ -143,7 +151,7 @@ board board_cuatro = {
.read_current_mA = cuatro_read_current_mA,
.set_fan_enabled = cuatro_set_fan_enabled,
.set_ir_power = tres_set_ir_power,
.set_siren = unused_set_siren,
.set_siren = cuatro_set_siren,
.set_bootkick = cuatro_set_bootkick,
.read_som_gpio = tres_read_som_gpio
};
1 change: 0 additions & 1 deletion board/boards/tres.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ static void tres_init(void) {
set_gpio_alternate(GPIOC, 10, GPIO_AF4_I2C5);
set_gpio_alternate(GPIOC, 11, GPIO_AF4_I2C5);
register_set_bits(&(GPIOC->OTYPER), GPIO_OTYPER_OT10 | GPIO_OTYPER_OT11); // open drain
fake_siren_init();

// Clock source
clock_source_init();
Expand Down
26 changes: 26 additions & 0 deletions board/drivers/beeper.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

#define BEEPER_COUNTER_OVERFLOW 25000U // 4kHz

void beeper_enable(bool enabled) {
if (enabled) {
register_set_bits(&(TIM4->CCER), TIM_CCER_CC3E);
} else {
register_clear_bits(&(TIM4->CCER), TIM_CCER_CC3E);
}
}

void beeper_init(void) {
// Enable timer and auto-reload
register_set(&(TIM4->CR1), TIM_CR1_CEN | TIM_CR1_ARPE, 0x3FU);

// Set channel as PWM mode 1 and disable output
register_set_bits(&(TIM4->CCMR2), (TIM_CCMR2_OC3M_2 | TIM_CCMR2_OC3M_1 | TIM_CCMR2_OC3PE));
beeper_enable(false);

// Set max counter value and compare to get 50% duty
register_set(&(TIM4->CCR3), BEEPER_COUNTER_OVERFLOW / 2U, 0xFFFFU);
register_set(&(TIM4->ARR), BEEPER_COUNTER_OVERFLOW, 0xFFFFU);

// Update registers and clear counter
TIM4->EGR |= TIM_EGR_UG;
}
8 changes: 8 additions & 0 deletions board/drivers/fake_siren.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#define CODEC_I2C_ADDR 0x10

void fake_siren_init(void);

void fake_siren_codec_enable(bool enabled) {
if (enabled) {
bool success = true;
Expand All @@ -28,8 +30,14 @@ void fake_siren_codec_enable(bool enabled) {


void fake_siren_set(bool enabled) {
static bool initialized = false;
static bool fake_siren_enabled = false;

if (!initialized) {
fake_siren_init();
initialized = true;
}

if (enabled != fake_siren_enabled) {
fake_siren_codec_enable(enabled);
}
Expand Down
5 changes: 3 additions & 2 deletions board/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -325,12 +325,13 @@ int main(void) {
// panda has an FPU, let's use it!
enable_fpu();

microsecond_timer_init();

current_board->set_siren(false);
if (current_board->fan_max_rpm > 0U) {
fan_init();
}

microsecond_timer_init();

// init to SILENT and can silent
set_safety_mode(SAFETY_SILENT, 0U);

Expand Down
50 changes: 25 additions & 25 deletions board/safety/safety_chrysler.h
Original file line number Diff line number Diff line change
Expand Up @@ -211,18 +211,6 @@ static safety_config chrysler_init(uint16_t param) {
.CRUISE_BUTTONS = 0xB1, // Cruise control buttons
};

// CAN messages for the 5th gen RAM HD platform
static const ChryslerAddrs CHRYSLER_RAM_HD_ADDRS = {
.EPS_2 = 0x220, // EPS driver input torque
.ESP_1 = 0x140, // Brake pedal and vehicle speed
.ESP_8 = 0x11C, // Brake pedal and vehicle speed
.ECM_5 = 0x22F, // Throttle position sensor
.DAS_3 = 0x1F4, // ACC engagement states from DASM
.DAS_6 = 0x275, // LKAS HUD and auto headlight control from DASM
.LKAS_COMMAND = 0x276, // LKAS controls from DASM
.CRUISE_BUTTONS = 0x23A, // Cruise control buttons
};

static RxCheck chrysler_ram_dt_rx_checks[] = {
{.msg = {{CHRYSLER_RAM_DT_ADDRS.EPS_2, 0, 8, .check_checksum = true, .max_counter = 15U, .frequency = 100U}, { 0 }, { 0 }}},
{.msg = {{CHRYSLER_RAM_DT_ADDRS.ESP_1, 0, 8, .check_checksum = true, .max_counter = 15U, .frequency = 50U}, { 0 }, { 0 }}},
Expand All @@ -240,14 +228,6 @@ static safety_config chrysler_init(uint16_t param) {
{.msg = {{CHRYSLER_ADDRS.DAS_3, 0, 8, .check_checksum = true, .max_counter = 15U, .frequency = 50U}, { 0 }, { 0 }}},
};

static RxCheck chrysler_ram_hd_rx_checks[] = {
{.msg = {{CHRYSLER_RAM_HD_ADDRS.EPS_2, 0, 8, .check_checksum = true, .max_counter = 15U, .frequency = 100U}, { 0 }, { 0 }}},
{.msg = {{CHRYSLER_RAM_HD_ADDRS.ESP_1, 0, 8, .check_checksum = true, .max_counter = 15U, .frequency = 50U}, { 0 }, { 0 }}},
{.msg = {{CHRYSLER_RAM_HD_ADDRS.ESP_8, 0, 8, .check_checksum = true, .max_counter = 15U, .frequency = 50U}, { 0 }, { 0 }}},
{.msg = {{CHRYSLER_RAM_HD_ADDRS.ECM_5, 0, 8, .check_checksum = true, .max_counter = 15U, .frequency = 50U}, { 0 }, { 0 }}},
{.msg = {{CHRYSLER_RAM_HD_ADDRS.DAS_3, 2, 8, .check_checksum = true, .max_counter = 15U, .frequency = 50U}, { 0 }, { 0 }}},
};

static const CanMsg CHRYSLER_TX_MSGS[] = {
{CHRYSLER_ADDRS.CRUISE_BUTTONS, 0, 3},
{CHRYSLER_ADDRS.LKAS_COMMAND, 0, 6},
Expand All @@ -260,21 +240,41 @@ static safety_config chrysler_init(uint16_t param) {
{CHRYSLER_RAM_DT_ADDRS.DAS_6, 0, 8},
};

#ifdef ALLOW_DEBUG
// CAN messages for the 5th gen RAM HD platform
static const ChryslerAddrs CHRYSLER_RAM_HD_ADDRS = {
.EPS_2 = 0x220, // EPS driver input torque
.ESP_1 = 0x140, // Brake pedal and vehicle speed
.ESP_8 = 0x11C, // Brake pedal and vehicle speed
.ECM_5 = 0x22F, // Throttle position sensor
.DAS_3 = 0x1F4, // ACC engagement states from DASM
.DAS_6 = 0x275, // LKAS HUD and auto headlight control from DASM
.LKAS_COMMAND = 0x276, // LKAS controls from DASM
.CRUISE_BUTTONS = 0x23A, // Cruise control buttons
};

static RxCheck chrysler_ram_hd_rx_checks[] = {
{.msg = {{CHRYSLER_RAM_HD_ADDRS.EPS_2, 0, 8, .check_checksum = true, .max_counter = 15U, .frequency = 100U}, { 0 }, { 0 }}},
{.msg = {{CHRYSLER_RAM_HD_ADDRS.ESP_1, 0, 8, .check_checksum = true, .max_counter = 15U, .frequency = 50U}, { 0 }, { 0 }}},
{.msg = {{CHRYSLER_RAM_HD_ADDRS.ESP_8, 0, 8, .check_checksum = true, .max_counter = 15U, .frequency = 50U}, { 0 }, { 0 }}},
{.msg = {{CHRYSLER_RAM_HD_ADDRS.ECM_5, 0, 8, .check_checksum = true, .max_counter = 15U, .frequency = 50U}, { 0 }, { 0 }}},
{.msg = {{CHRYSLER_RAM_HD_ADDRS.DAS_3, 2, 8, .check_checksum = true, .max_counter = 15U, .frequency = 50U}, { 0 }, { 0 }}},
};

static const CanMsg CHRYSLER_RAM_HD_TX_MSGS[] = {
{CHRYSLER_RAM_HD_ADDRS.CRUISE_BUTTONS, 2, 3},
{CHRYSLER_RAM_HD_ADDRS.LKAS_COMMAND, 0, 8},
{CHRYSLER_RAM_HD_ADDRS.DAS_6, 0, 8},
};

safety_config ret;

bool enable_ram_dt = GET_FLAG(param, CHRYSLER_PARAM_RAM_DT);

#ifdef ALLOW_DEBUG
const uint32_t CHRYSLER_PARAM_RAM_HD = 2U; // set for Ram HD platform
bool enable_ram_hd = GET_FLAG(param, CHRYSLER_PARAM_RAM_HD);
#endif

safety_config ret;

bool enable_ram_dt = GET_FLAG(param, CHRYSLER_PARAM_RAM_DT);

if (enable_ram_dt) {
chrysler_platform = CHRYSLER_RAM_DT;
chrysler_addrs = &CHRYSLER_RAM_DT_ADDRS;
Expand Down
9 changes: 7 additions & 2 deletions board/safety/safety_ford.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,11 @@ static const SteeringLimits FORD_STEERING_LIMITS = {
.max_angle_error = 100, // 0.002 * FORD_STEERING_LIMITS.angle_deg_to_can
.angle_rate_up_lookup = {
{5., 25., 25.},
{0.0002, 0.0001, 0.0001}
{0.00045, 0.0001, 0.0001}
},
.angle_rate_down_lookup = {
{5., 25., 25.},
{0.000225, 0.00015, 0.00015}
{0.00045, 0.00015, 0.00015}
},

// no blending at low speed due to lack of torque wind-up and inaccurate current curvature
Expand Down Expand Up @@ -223,6 +223,9 @@ static bool ford_tx_hook(const CANPacket_t *to_send) {
// Signal: CmbbDeny_B_Actl
bool cmbb_deny = GET_BIT(to_send, 37U);

// Signal: AccBrkPrchg_B_Rq & AccBrkDecel_B_Rq
bool brake_actuation = GET_BIT(to_send, 54U) || GET_BIT(to_send, 55U);

bool violation = false;
violation |= longitudinal_accel_checks(accel, FORD_LONG_LIMITS);
violation |= longitudinal_gas_checks(gas, FORD_LONG_LIMITS);
Expand All @@ -231,6 +234,8 @@ static bool ford_tx_hook(const CANPacket_t *to_send) {
// Safety check for stock AEB
violation |= cmbb_deny; // do not prevent stock AEB actuation

violation |= !get_longitudinal_allowed() && brake_actuation;

if (violation) {
tx = false;
}
Expand Down
1 change: 1 addition & 0 deletions board/stm32h7/board.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "drivers/fan.h"
#include "stm32h7/llfan.h"
#include "stm32h7/lldac.h"
#include "drivers/beeper.h"
#include "drivers/fake_siren.h"
#include "drivers/clock_source.h"
#include "boards/red.h"
Expand Down
32 changes: 23 additions & 9 deletions board/stm32h7/lli2c.h
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@

// TODO: this driver relies heavily on polling,
// if we want it to be more async, we should use interrupts

#define I2C_RETRY_COUNT 10U
#define I2C_TIMEOUT_US 100000U

// cppcheck-suppress misra-c2012-2.7; not sure why it triggers here?
bool i2c_status_wait(const volatile uint32_t *reg, uint32_t mask, uint32_t val) {
uint32_t start_time = microsecond_timer_get();
while(((*reg & mask) != val) && (get_ts_elapsed(microsecond_timer_get(), start_time) < I2C_TIMEOUT_US));
return ((*reg & mask) == val);
}

void i2c_reset(I2C_TypeDef *I2C) {
// peripheral reset
register_clear_bits(&I2C->CR1, I2C_CR1_PE);
while ((I2C->CR1 & I2C_CR1_PE) != 0U);
register_set_bits(&I2C->CR1, I2C_CR1_PE);
}

bool i2c_write_reg(I2C_TypeDef *I2C, uint8_t addr, uint8_t reg, uint8_t value) {
// Setup transfer and send START + addr
bool ret = false;
for(uint32_t i=0U; i<10U; i++) {

// Setup transfer and send START + addr
for (uint32_t i = 0U; i < I2C_RETRY_COUNT; i++) {
register_clear_bits(&I2C->CR2, I2C_CR2_ADD10);
I2C->CR2 = ((uint32_t)addr << 1U) & I2C_CR2_SADD_Msk;
register_clear_bits(&I2C->CR2, I2C_CR2_RD_WRN);
Expand Down Expand Up @@ -57,9 +64,10 @@ bool i2c_write_reg(I2C_TypeDef *I2C, uint8_t addr, uint8_t reg, uint8_t value) {
}

bool i2c_read_reg(I2C_TypeDef *I2C, uint8_t addr, uint8_t reg, uint8_t *value) {
// Setup transfer and send START + addr
bool ret = false;
for(uint32_t i=0U; i<10U; i++) {

// Setup transfer and send START + addr
for (uint32_t i = 0U; i < I2C_RETRY_COUNT; i++) {
register_clear_bits(&I2C->CR2, I2C_CR2_ADD10);
I2C->CR2 = ((uint32_t)addr << 1U) & I2C_CR2_SADD_Msk;
register_clear_bits(&I2C->CR2, I2C_CR2_RD_WRN);
Expand Down Expand Up @@ -116,6 +124,11 @@ bool i2c_read_reg(I2C_TypeDef *I2C, uint8_t addr, uint8_t reg, uint8_t *value) {
I2C->CR2 |= I2C_CR2_STOP;

end:

if (!ret) {
i2c_reset(I2C);
}

return ret;
}

Expand All @@ -131,7 +144,7 @@ bool i2c_set_reg_bits(I2C_TypeDef *I2C, uint8_t address, uint8_t regis, uint8_t
bool i2c_clear_reg_bits(I2C_TypeDef *I2C, uint8_t address, uint8_t regis, uint8_t bits) {
uint8_t value;
bool ret = i2c_read_reg(I2C, address, regis, &value);
if(ret) {
if (ret) {
ret = i2c_write_reg(I2C, address, regis, value & (uint8_t) (~bits));
}
return ret;
Expand All @@ -149,5 +162,6 @@ bool i2c_set_reg_mask(I2C_TypeDef *I2C, uint8_t address, uint8_t regis, uint8_t
void i2c_init(I2C_TypeDef *I2C) {
// 100kHz clock speed
I2C->TIMINGR = 0x107075B0;
I2C->CR1 = I2C_CR1_PE;
}

i2c_reset(I2C);
}
1 change: 1 addition & 0 deletions board/stm32h7/peripherals.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ void peripherals_init(void) {
RCC->APB2ENR |= RCC_APB2ENR_TIM1EN; // clock source timer
RCC->APB1LENR |= RCC_APB1LENR_TIM2EN; // main counter
RCC->APB1LENR |= RCC_APB1LENR_TIM3EN; // fan pwm
RCC->APB1LENR |= RCC_APB1LENR_TIM4EN; // beeper source
RCC->APB1LENR |= RCC_APB1LENR_TIM6EN; // interrupt timer
RCC->APB1LENR |= RCC_APB1LENR_TIM7EN; // DMA trigger timer
RCC->APB2ENR |= RCC_APB2ENR_TIM8EN; // tick timer
Expand Down
1 change: 0 additions & 1 deletion python/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,6 @@ def _cli_select_panda(self):
return None
if len(pandas) == 1:
print(f"INFO: connecting to panda {pandas[0]}")
time.sleep(1)
return pandas[0]
while True:
print("Multiple pandas available:")
Expand Down
Loading

0 comments on commit 4c4ef05

Please sign in to comment.