Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into legacy
Browse files Browse the repository at this point in the history
  • Loading branch information
eFiniLan committed Jul 17, 2023
2 parents d2a537b + 5d87344 commit d98c3ac
Show file tree
Hide file tree
Showing 32 changed files with 1,594 additions and 168 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
*.tmp
*.pyc
.*.swp
.*.swo
Expand Down
1 change: 1 addition & 0 deletions __init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from .python.constants import McuType, BASEDIR, FW_PATH # noqa: F401
from .python.spi import PandaSpiException, PandaProtocolMismatch # noqa: F401
from .python.serial import PandaSerial # noqa: F401
from .python import (Panda, PandaDFU, # noqa: F401
pack_can_buffer, unpack_can_buffer, calculate_checksum, unpack_log,
Expand Down
2 changes: 1 addition & 1 deletion board/boards/board_declarations.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ typedef void (*board_set_ir_power)(uint8_t percentage);
typedef void (*board_set_fan_enabled)(bool enabled);
typedef void (*board_set_phone_power)(bool enabled);
typedef void (*board_set_siren)(bool enabled);
typedef void (*board_board_tick)(bool ignition, bool usb_enum, bool heartbeat_seen, bool harness_inserted);
typedef bool (*board_board_tick)(bool ignition, bool usb_enum, bool heartbeat_seen, bool harness_inserted);
typedef bool (*board_read_som_gpio)(void);

struct board {
Expand Down
5 changes: 4 additions & 1 deletion board/boards/dos.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,16 +73,19 @@ void dos_set_usb_power_mode(uint8_t mode) {
}
}

void dos_board_tick(bool ignition, bool usb_enum, bool heartbeat_seen, bool harness_inserted) {
bool dos_board_tick(bool ignition, bool usb_enum, bool heartbeat_seen, bool harness_inserted) {
bool ret = false;
if ((ignition && !usb_enum) || harness_inserted) {
// enable bootkick if ignition seen or if plugged into a harness
ret = true;
dos_set_bootkick(true);
} else if (heartbeat_seen) {
// disable once openpilot is up
dos_set_bootkick(false);
} else {

}
return ret;
}

void dos_set_can_mode(uint8_t mode){
Expand Down
3 changes: 2 additions & 1 deletion board/boards/red_v2.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,6 @@ const board board_red_v2 = {
.set_fan_enabled = unused_set_fan_enabled,
.set_ir_power = unused_set_ir_power,
.set_phone_power = unused_set_phone_power,
.set_siren = unused_set_siren
.set_siren = unused_set_siren,
.read_som_gpio = unused_read_som_gpio
};
5 changes: 4 additions & 1 deletion board/boards/tres.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@ void tres_set_bootkick(bool enabled){
}

bool tres_ignition_prev = false;
void tres_board_tick(bool ignition, bool usb_enum, bool heartbeat_seen, bool harness_inserted) {
bool tres_board_tick(bool ignition, bool usb_enum, bool heartbeat_seen, bool harness_inserted) {
UNUSED(usb_enum);
bool ret = false;
if ((ignition && !tres_ignition_prev) || harness_inserted) {
// enable bootkick on rising edge of ignition
ret = true;
tres_set_bootkick(true);
} else if (heartbeat_seen) {
// disable once openpilot is up
Expand All @@ -31,6 +33,7 @@ void tres_board_tick(bool ignition, bool usb_enum, bool heartbeat_seen, bool har

}
tres_ignition_prev = ignition;
return ret;
}

void tres_set_fan_enabled(bool enabled) {
Expand Down
3 changes: 2 additions & 1 deletion board/boards/uno.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ void uno_set_can_mode(uint8_t mode){
}
}

void uno_board_tick(bool ignition, bool usb_enum, bool heartbeat_seen, bool harness_inserted) {
bool uno_board_tick(bool ignition, bool usb_enum, bool heartbeat_seen, bool harness_inserted) {
UNUSED(ignition);
UNUSED(usb_enum);
UNUSED(heartbeat_seen);
Expand All @@ -158,6 +158,7 @@ void uno_board_tick(bool ignition, bool usb_enum, bool heartbeat_seen, bool harn
} else {
uno_set_bootkick(false);
}
return false;
}

bool uno_check_ignition(void){
Expand Down
3 changes: 2 additions & 1 deletion board/boards/unused_funcs.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@ uint32_t unused_read_current(void) {
return 0U;
}

void unused_board_tick(bool ignition, bool usb_enum, bool heartbeat_seen, bool harness_inserted) {
bool unused_board_tick(bool ignition, bool usb_enum, bool heartbeat_seen, bool harness_inserted) {
UNUSED(ignition);
UNUSED(usb_enum);
UNUSED(heartbeat_seen);
UNUSED(harness_inserted);
return false;
}

bool unused_read_som_gpio(void) {
Expand Down
2 changes: 0 additions & 2 deletions board/drivers/bxcan.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,7 @@ void update_can_health_pkt(uint8_t can_number, uint32_t ir_reg) {
// ***************************** CAN *****************************
// CANx_SCE IRQ Handler
void can_sce(uint8_t can_number) {
ENTER_CRITICAL();
update_can_health_pkt(can_number, 1U);
EXIT_CRITICAL();
}

// CANx_TX IRQ Handler
Expand Down
17 changes: 9 additions & 8 deletions board/drivers/fdcan.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ uint8_t can_irq_number[3][2] = {
{ FDCAN3_IT0_IRQn, FDCAN3_IT1_IRQn },
};

#define CAN_ACK_ERROR 3U

bool can_set_speed(uint8_t can_number) {
bool ret = true;
FDCAN_GlobalTypeDef *CANx = CANIF_FROM_CAN_NUM(can_number);
Expand All @@ -39,8 +41,6 @@ void can_set_gmlan(uint8_t bus) {
}

void update_can_health_pkt(uint8_t can_number, uint32_t ir_reg) {
ENTER_CRITICAL();

FDCAN_GlobalTypeDef *CANx = CANIF_FROM_CAN_NUM(can_number);
uint32_t psr_reg = CANx->PSR;
uint32_t ecr_reg = CANx->ECR;
Expand Down Expand Up @@ -68,20 +68,21 @@ void update_can_health_pkt(uint8_t can_number, uint32_t ir_reg) {


if (ir_reg != 0U) {
// Clear error interrupts
CANx->IR |= (FDCAN_IR_PED | FDCAN_IR_PEA | FDCAN_IR_EP | FDCAN_IR_BO | FDCAN_IR_RF0L);
can_health[can_number].total_error_cnt += 1U;
// Check for RX FIFO overflow
if ((ir_reg & (FDCAN_IR_RF0L)) != 0) {
can_health[can_number].total_rx_lost_cnt += 1U;
}
// Reset CAN core when CEL couter reaches at least 100 errors
if (((ir_reg & (FDCAN_IR_PED | FDCAN_IR_PEA)) != 0) && (((ecr_reg & FDCAN_ECR_CEL) >> FDCAN_ECR_CEL_Pos) >= 100U)) {
// While multiplexing between buses 1 and 3 we are getting ACK errors that overwhelm CAN core
// By resseting CAN core when no ACK is detected for a while(until TEC counter reaches 127) it can recover faster
if (((can_health[can_number].last_error == CAN_ACK_ERROR) || (can_health[can_number].last_data_error == CAN_ACK_ERROR)) && (can_health[can_number].transmit_error_cnt > 127U)) {
can_health[can_number].can_core_reset_cnt += 1U;
can_health[can_number].total_tx_lost_cnt += (FDCAN_TX_FIFO_EL_CNT - (FDCAN_TXFQS_TFFL & FDCAN_TXFQS_TFFL_Msk)); // TX FIFO msgs will be lost after reset
can_health[can_number].total_tx_lost_cnt += (FDCAN_TX_FIFO_EL_CNT - (CANx->TXFQS & FDCAN_TXFQS_TFFL)); // TX FIFO msgs will be lost after reset
llcan_clear_send(CANx);
}
// Clear error interrupts
CANx->IR |= (FDCAN_IR_PED | FDCAN_IR_PEA | FDCAN_IR_EP | FDCAN_IR_BO | FDCAN_IR_RF0L);
}
EXIT_CRITICAL();
}

// ***************************** CAN *****************************
Expand Down
5 changes: 3 additions & 2 deletions board/drivers/spi.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@

#include "crc.h"

#define SPI_BUF_SIZE 1024U
#define SPI_TIMEOUT_US 10000U

// got max rate from hitting a non-existent endpoint
// in a tight loop, plus some buffer
#define SPI_IRQ_RATE 16000U

#ifdef STM32H7
#define SPI_BUF_SIZE 2048U
__attribute__((section(".ram_d1"))) uint8_t spi_buf_rx[SPI_BUF_SIZE];
__attribute__((section(".ram_d1"))) uint8_t spi_buf_tx[SPI_BUF_SIZE];
__attribute__((section(".ram_d2"))) uint8_t spi_buf_tx[SPI_BUF_SIZE];
#else
#define SPI_BUF_SIZE 1024U
uint8_t spi_buf_rx[SPI_BUF_SIZE];
uint8_t spi_buf_tx[SPI_BUF_SIZE];
#endif
Expand Down
23 changes: 22 additions & 1 deletion board/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,8 @@ void __attribute__ ((noinline)) enable_fpu(void) {
// called at 8Hz
uint8_t loop_counter = 0U;
uint8_t previous_harness_status = HARNESS_STATUS_NC;
uint32_t waiting_to_boot_count = 0;
bool waiting_to_boot = false;
void tick_handler(void) {
if (TICK_TIMER->SR != 0) {
// siren
Expand Down Expand Up @@ -210,9 +212,28 @@ void tick_handler(void) {
logging_tick();

const bool recent_heartbeat = heartbeat_counter == 0U;
current_board->board_tick(check_started(), usb_enumerated, recent_heartbeat, ((harness.status != previous_harness_status) && (harness.status != HARNESS_STATUS_NC)));
const bool harness_inserted = (harness.status != previous_harness_status) && (harness.status != HARNESS_STATUS_NC);
const bool just_bootkicked = current_board->board_tick(check_started(), usb_enumerated, recent_heartbeat, harness_inserted);
previous_harness_status = harness.status;

// log device boot time
const bool som_running = current_board->read_som_gpio();
if (just_bootkicked && !som_running) {
log("bootkick");
waiting_to_boot = true;
}
if (waiting_to_boot) {
if (som_running) {
log("device booted");
waiting_to_boot = false;
} else if (waiting_to_boot_count == 10U) {
log("not booted after 10s");
} else {

}
waiting_to_boot_count += 1U;
}

// increase heartbeat counter and cap it at the uint32 limit
if (heartbeat_counter < __UINT32_MAX__) {
heartbeat_counter += 1U;
Expand Down
Loading

0 comments on commit d98c3ac

Please sign in to comment.