Skip to content

Commit

Permalink
Merge remote-tracking branch 'commaai/panda/master' into hyundai-fwd-…
Browse files Browse the repository at this point in the history
…hook
  • Loading branch information
sunnyhaibin committed Dec 7, 2024
2 parents 98ac433 + c7cc2de commit dff3976
Show file tree
Hide file tree
Showing 46 changed files with 674 additions and 174 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
19 changes: 16 additions & 3 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 Expand Up @@ -86,8 +88,8 @@ jobs:
scons -j$(nproc) ${{ matrix.flags }} && \
tests/safety/test.sh"
misra:
name: MISRA C:2012
misra_linter:
name: MISRA C:2012 Linter
runs-on: ubuntu-latest
timeout-minutes: 20
steps:
Expand All @@ -97,8 +99,19 @@ jobs:
- name: Build FW
run: ${{ env.RUN }} "scons -j$(nproc)"
- name: Run MISRA C:2012 analysis
timeout-minutes: 1
timeout-minutes: 2
run: ${{ env.RUN }} "cd tests/misra && ./test_misra.sh"

misra_mutation:
name: MISRA C:2012 Mutation
runs-on: ubuntu-latest
timeout-minutes: 20
steps:
- uses: actions/checkout@v2
- name: Build Docker image
run: eval "$BUILD"
- name: Build FW
run: ${{ env.RUN }} "scons -j$(nproc)"
- name: MISRA mutation tests
timeout-minutes: 5
run: ${{ env.RUN }} "cd tests/misra && pytest -n8 test_mutation.py"
Expand Down
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ repos:
additional_dependencies: ['numpy', 'types-requests', 'types-atomicwrites',
'types-pycurl']
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.6.3
rev: v0.6.8
hooks:
- id: ruff
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ RUN pip3 install --break-system-packages --no-cache-dir $PYTHONPATH/panda/[dev]

# TODO: this should be a "pip install" or not even in this repo at all
RUN git config --global --add safe.directory $PYTHONPATH/panda
ENV OPENDBC_REF="5ed7a834a4e0e24c3968dd1e98ceb4b9d5f9791a"
ENV OPENDBC_REF="e1ce3619a5db661ef2b406ccf258a253baf6eebc"
RUN cd /tmp/ && \
git clone --depth 1 https://github.com/commaai/opendbc opendbc_repo && \
cd opendbc_repo && git fetch origin $OPENDBC_REF && git checkout FETCH_HEAD && rm -rf .git/ && \
Expand Down
3 changes: 2 additions & 1 deletion board/boards/black.h
Original file line number Diff line number Diff line change
Expand Up @@ -182,5 +182,6 @@ board board_black = {
.set_fan_enabled = unused_set_fan_enabled,
.set_ir_power = unused_set_ir_power,
.set_siren = unused_set_siren,
.read_som_gpio = unused_read_som_gpio
.read_som_gpio = unused_read_som_gpio,
.set_amp_enabled = unused_set_amp_enabled
};
2 changes: 2 additions & 0 deletions board/boards/board_declarations.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ typedef void (*board_set_fan_enabled)(bool enabled);
typedef void (*board_set_siren)(bool enabled);
typedef void (*board_set_bootkick)(BootState state);
typedef bool (*board_read_som_gpio)(void);
typedef void (*board_set_amp_enabled)(bool enabled);

struct board {
harness_configuration *harness_config;
Expand All @@ -49,6 +50,7 @@ struct board {
board_set_siren set_siren;
board_set_bootkick set_bootkick;
board_read_som_gpio read_som_gpio;
board_set_amp_enabled set_amp_enabled;
};

// ******************* Definitions ********************
Expand Down
38 changes: 33 additions & 5 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,14 +71,26 @@ 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_set_amp_enabled(bool enabled){
set_gpio_output(GPIOA, 5, 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);

// more open drain
set_gpio_output_type(GPIOD, 3, OUTPUT_TYPE_OPEN_DRAIN); // FAN_EN
set_gpio_output_type(GPIOC, 12, OUTPUT_TYPE_OPEN_DRAIN); // VBAT_EN

// Power readout
set_gpio_mode(GPIOC, 5, MODE_ANALOG);
set_gpio_mode(GPIOA, 6, MODE_ANALOG);
Expand Down Expand Up @@ -120,6 +132,21 @@ static void cuatro_init(void) {

// Clock source
clock_source_init();

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

// Sound codec
cuatro_set_amp_enabled(false);
set_gpio_alternate(GPIOA, 2, GPIO_AF8_SAI4); // SAI4_SCK_B
set_gpio_alternate(GPIOC, 0, GPIO_AF8_SAI4); // SAI4_FS_B
set_gpio_alternate(GPIOD, 11, GPIO_AF10_SAI4); // SAI4_SD_A
set_gpio_alternate(GPIOE, 3, GPIO_AF8_SAI4); // SAI4_SD_B
set_gpio_alternate(GPIOE, 4, GPIO_AF2_SAI1); // SAI1_D2
set_gpio_alternate(GPIOE, 5, GPIO_AF2_SAI1); // SAI1_CK2
set_gpio_alternate(GPIOE, 6, GPIO_AF10_SAI4); // SAI4_MCLK_B
sound_init();
}

board board_cuatro = {
Expand All @@ -142,8 +169,9 @@ board board_cuatro = {
.read_voltage_mV = cuatro_read_voltage_mV,
.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_ir_power = unused_set_ir_power,
.set_siren = cuatro_set_siren,
.set_bootkick = cuatro_set_bootkick,
.read_som_gpio = tres_read_som_gpio
.read_som_gpio = tres_read_som_gpio,
.set_amp_enabled = cuatro_set_amp_enabled
};
3 changes: 2 additions & 1 deletion board/boards/dos.h
Original file line number Diff line number Diff line change
Expand Up @@ -211,5 +211,6 @@ board board_dos = {
.set_ir_power = dos_set_ir_power,
.set_siren = dos_set_siren,
.set_bootkick = dos_set_bootkick,
.read_som_gpio = dos_read_som_gpio
.read_som_gpio = dos_read_som_gpio,
.set_amp_enabled = unused_set_amp_enabled
};
3 changes: 2 additions & 1 deletion board/boards/grey.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,6 @@ board board_grey = {
.set_fan_enabled = unused_set_fan_enabled,
.set_ir_power = unused_set_ir_power,
.set_siren = unused_set_siren,
.read_som_gpio = unused_read_som_gpio
.read_som_gpio = unused_read_som_gpio,
.set_amp_enabled = unused_set_amp_enabled
};
3 changes: 2 additions & 1 deletion board/boards/red.h
Original file line number Diff line number Diff line change
Expand Up @@ -194,5 +194,6 @@ board board_red = {
.set_fan_enabled = unused_set_fan_enabled,
.set_ir_power = unused_set_ir_power,
.set_siren = unused_set_siren,
.read_som_gpio = unused_read_som_gpio
.read_som_gpio = unused_read_som_gpio,
.set_amp_enabled = unused_set_amp_enabled
};
4 changes: 2 additions & 2 deletions 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 Expand Up @@ -98,5 +97,6 @@ board board_tres = {
.set_ir_power = tres_set_ir_power,
.set_siren = fake_siren_set,
.set_bootkick = tres_set_bootkick,
.read_som_gpio = tres_read_som_gpio
.read_som_gpio = tres_read_som_gpio,
.set_amp_enabled = unused_set_amp_enabled
};
3 changes: 2 additions & 1 deletion board/boards/uno.h
Original file line number Diff line number Diff line change
Expand Up @@ -218,5 +218,6 @@ board board_uno = {
.set_ir_power = uno_set_ir_power,
.set_siren = unused_set_siren,
.set_bootkick = uno_set_bootkick,
.read_som_gpio = unused_read_som_gpio
.read_som_gpio = unused_read_som_gpio,
.set_amp_enabled = unused_set_amp_enabled
};
4 changes: 4 additions & 0 deletions board/boards/unused_funcs.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,7 @@ void unused_set_bootkick(BootState state) {
bool unused_read_som_gpio(void) {
return false;
}

void unused_set_amp_enabled(bool enabled) {
UNUSED(enabled);
}
3 changes: 2 additions & 1 deletion board/boards/white.h
Original file line number Diff line number Diff line change
Expand Up @@ -211,5 +211,6 @@ board board_white = {
.set_fan_enabled = unused_set_fan_enabled,
.set_ir_power = unused_set_ir_power,
.set_siren = unused_set_siren,
.read_som_gpio = unused_read_som_gpio
.read_som_gpio = unused_read_som_gpio,
.set_amp_enabled = unused_set_amp_enabled
};
2 changes: 1 addition & 1 deletion board/can_declarations.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#endif

typedef struct {
unsigned char reserved : 1;
unsigned char fd : 1;
unsigned char bus : 3;
unsigned char data_len_code : 4; // lookup length with dlc_to_len
unsigned char rejected : 1;
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;
}
3 changes: 3 additions & 0 deletions board/drivers/bxcan.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ void process_can(uint8_t can_number) {
if ((CANx->TSR & CAN_TSR_RQCP0) == CAN_TSR_RQCP0) {
if ((CANx->TSR & CAN_TSR_TXOK0) == CAN_TSR_TXOK0) {
CANPacket_t to_push;
to_push.fd = 0U;
to_push.returned = 1U;
to_push.rejected = 0U;
to_push.extended = (CANx->sTxMailBox[0].TIR >> 2) & 0x1U;
Expand Down Expand Up @@ -144,6 +145,7 @@ void can_rx(uint8_t can_number) {
// add to my fifo
CANPacket_t to_push;

to_push.fd = 0U;
to_push.returned = 0U;
to_push.rejected = 0U;
to_push.extended = (CANx->sFIFOMailBox[0].RIR >> 2) & 0x1U;
Expand All @@ -159,6 +161,7 @@ void can_rx(uint8_t can_number) {
if (bus_fwd_num != -1) {
CANPacket_t to_send;

to_send.fd = 0U;
to_send.returned = 0U;
to_send.rejected = 0U;
to_send.extended = to_push.extended; // TXRQ
Expand Down
10 changes: 5 additions & 5 deletions board/drivers/can_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,10 @@ void can_clear(can_ring *q) {
// Helpers
// Panda: Bus 0=CAN1 Bus 1=CAN2 Bus 2=CAN3
bus_config_t bus_config[BUS_CONFIG_ARRAY_SIZE] = {
{ .bus_lookup = 0U, .can_num_lookup = 0U, .forwarding_bus = -1, .can_speed = 5000U, .can_data_speed = 20000U, .canfd_enabled = false, .brs_enabled = false, .canfd_non_iso = false },
{ .bus_lookup = 1U, .can_num_lookup = 1U, .forwarding_bus = -1, .can_speed = 5000U, .can_data_speed = 20000U, .canfd_enabled = false, .brs_enabled = false, .canfd_non_iso = false },
{ .bus_lookup = 2U, .can_num_lookup = 2U, .forwarding_bus = -1, .can_speed = 5000U, .can_data_speed = 20000U, .canfd_enabled = false, .brs_enabled = false, .canfd_non_iso = false },
{ .bus_lookup = 0xFFU, .can_num_lookup = 0xFFU, .forwarding_bus = -1, .can_speed = 333U, .can_data_speed = 333U, .canfd_enabled = false, .brs_enabled = false, .canfd_non_iso = false },
{ .bus_lookup = 0U, .can_num_lookup = 0U, .forwarding_bus = -1, .can_speed = 5000U, .can_data_speed = 20000U, .canfd_auto = false, .canfd_enabled = false, .brs_enabled = false, .canfd_non_iso = false },
{ .bus_lookup = 1U, .can_num_lookup = 1U, .forwarding_bus = -1, .can_speed = 5000U, .can_data_speed = 20000U, .canfd_auto = false, .canfd_enabled = false, .brs_enabled = false, .canfd_non_iso = false },
{ .bus_lookup = 2U, .can_num_lookup = 2U, .forwarding_bus = -1, .can_speed = 5000U, .can_data_speed = 20000U, .canfd_auto = false, .canfd_enabled = false, .brs_enabled = false, .canfd_non_iso = false },
{ .bus_lookup = 0xFFU, .can_num_lookup = 0xFFU, .forwarding_bus = -1, .can_speed = 333U, .can_data_speed = 333U, .canfd_auto = false, .canfd_enabled = false, .brs_enabled = false, .canfd_non_iso = false },
};

void can_init_all(void) {
Expand Down Expand Up @@ -165,7 +165,7 @@ void ignition_can_hook(CANPacket_t *to_push) {
if (bus == 0) {
int addr = GET_ADDR(to_push);
int len = GET_LEN(to_push);

// GM exception
if ((addr == 0x1F1) && (len == 8)) {
// SystemPowerMode (2=Run, 3=Crank Request)
Expand Down
1 change: 1 addition & 0 deletions board/drivers/can_common_declarations.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ typedef struct {
int8_t forwarding_bus;
uint32_t can_speed;
uint32_t can_data_speed;
bool canfd_auto;
bool canfd_enabled;
bool brs_enabled;
bool canfd_non_iso;
Expand Down
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
15 changes: 11 additions & 4 deletions board/drivers/fdcan.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,11 @@ void process_can(uint8_t can_number) {
fifo = (canfd_fifo *)(TxFIFOSA + (tx_index * FDCAN_TX_FIFO_EL_SIZE));

fifo->header[0] = (to_send.extended << 30) | ((to_send.extended != 0U) ? (to_send.addr) : (to_send.addr << 18));
uint32_t canfd_enabled_header = bus_config[can_number].canfd_enabled ? (1UL << 21) : 0UL;

// If canfd_auto is set, outgoing packets will be automatically sent as CAN-FD if an incoming CAN-FD packet was seen
bool fd = bus_config[can_number].canfd_auto ? bus_config[can_number].canfd_enabled : (bool)(to_send.fd > 0U);
uint32_t canfd_enabled_header = fd ? (1UL << 21) : 0UL;

uint32_t brs_enabled_header = bus_config[can_number].brs_enabled ? (1UL << 20) : 0UL;
fifo->header[1] = (to_send.data_len_code << 16) | canfd_enabled_header | brs_enabled_header;

Expand All @@ -115,6 +119,7 @@ void process_can(uint8_t can_number) {
// Send back to USB
CANPacket_t to_push;

to_push.fd = fd;
to_push.returned = 1U;
to_push.rejected = 0U;
to_push.extended = to_send.extended;
Expand Down Expand Up @@ -168,16 +173,17 @@ void can_rx(uint8_t can_number) {
// getting address
fifo = (canfd_fifo *)(RxFIFO0SA + (rx_fifo_idx * FDCAN_RX_FIFO_0_EL_SIZE));

bool canfd_frame = ((fifo->header[1] >> 21) & 0x1U);
bool brs_frame = ((fifo->header[1] >> 20) & 0x1U);

to_push.fd = canfd_frame;
to_push.returned = 0U;
to_push.rejected = 0U;
to_push.extended = (fifo->header[0] >> 30) & 0x1U;
to_push.addr = ((to_push.extended != 0U) ? (fifo->header[0] & 0x1FFFFFFFU) : ((fifo->header[0] >> 18) & 0x7FFU));
to_push.bus = bus_number;
to_push.data_len_code = ((fifo->header[1] >> 16) & 0xFU);

bool canfd_frame = ((fifo->header[1] >> 21) & 0x1U);
bool brs_frame = ((fifo->header[1] >> 20) & 0x1U);

uint8_t data_len_w = (dlc_to_len[to_push.data_len_code] / 4U);
data_len_w += ((dlc_to_len[to_push.data_len_code] % 4U) > 0U) ? 1U : 0U;
for (unsigned int i = 0; i < data_len_w; i++) {
Expand All @@ -193,6 +199,7 @@ void can_rx(uint8_t can_number) {
if (bus_fwd_num != -1) {
CANPacket_t to_send;

to_send.fd = to_push.fd;
to_send.returned = 0U;
to_send.rejected = 0U;
to_send.extended = to_push.extended;
Expand Down
Loading

0 comments on commit dff3976

Please sign in to comment.