From a2d8ad9486fb429a2ce27abb5879b9ca8f26dfa4 Mon Sep 17 00:00:00 2001 From: Robbe Derks Date: Wed, 9 Apr 2025 15:21:46 +0200 Subject: [PATCH 1/5] Log divergent registers once (#2182) log divergent reg --- board/drivers/registers.h | 11 ++++------- board/drivers/registers_declarations.h | 1 + 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/board/drivers/registers.h b/board/drivers/registers.h index 92b6b0fa..ce4be14a 100644 --- a/board/drivers/registers.h +++ b/board/drivers/registers.h @@ -49,13 +49,10 @@ void check_registers(void){ if((uint32_t) register_map[i].address != 0U){ ENTER_CRITICAL() if((*(register_map[i].address) & register_map[i].check_mask) != (register_map[i].value & register_map[i].check_mask)){ - #ifdef DEBUG_FAULTS - print("Register at address 0x"); puth((uint32_t) register_map[i].address); print(" is divergent!"); - print(" Map: 0x"); puth(register_map[i].value); - print(" Register: 0x"); puth(*(register_map[i].address)); - print(" Mask: 0x"); puth(register_map[i].check_mask); - print("\n"); - #endif + if(!register_map[i].logged_fault){ + print("Register 0x"); puth((uint32_t) register_map[i].address); print(" divergent! Map: 0x"); puth(register_map[i].value); print(" Reg: 0x"); puth(*(register_map[i].address)); print("\n"); + register_map[i].logged_fault = true; + } fault_occurred(FAULT_REGISTER_DIVERGENT); } EXIT_CRITICAL() diff --git a/board/drivers/registers_declarations.h b/board/drivers/registers_declarations.h index 13180117..90ee66df 100644 --- a/board/drivers/registers_declarations.h +++ b/board/drivers/registers_declarations.h @@ -4,6 +4,7 @@ typedef struct reg { volatile uint32_t *address; uint32_t value; uint32_t check_mask; + bool logged_fault; } reg; // 10 bit hash with 23 as a prime From 165dd6614e6cce9687a2d9db83f5120eeaa0e099 Mon Sep 17 00:00:00 2001 From: Robbe Derks Date: Wed, 9 Apr 2025 16:00:28 +0200 Subject: [PATCH 2/5] 1min is a bit tight --- .github/workflows/drivers.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/drivers.yaml b/.github/workflows/drivers.yaml index b787e3a5..f8389173 100644 --- a/.github/workflows/drivers.yaml +++ b/.github/workflows/drivers.yaml @@ -9,7 +9,7 @@ jobs: build_socketcan: name: socketcan build runs-on: ubuntu-latest - timeout-minutes: 1 + timeout-minutes: 2 steps: - uses: actions/checkout@v2 - name: Install dependencies From b14c47eb79dac1172c9f844d9eaf46d215553c9d Mon Sep 17 00:00:00 2001 From: Robbe Derks Date: Wed, 9 Apr 2025 16:09:19 +0200 Subject: [PATCH 3/5] Cleanup common and useless debug logs (#2183) * cleanup common logs * cleanup puth2 --- board/drivers/harness.h | 7 +------ board/drivers/spi.h | 18 +++++++++++------- board/drivers/uart.h | 6 +----- board/drivers/uart_declarations.h | 1 - board/drivers/usb.h | 17 +++++++++++------ board/main_declarations.h | 1 - 6 files changed, 24 insertions(+), 26 deletions(-) diff --git a/board/drivers/harness.h b/board/drivers/harness.h index efe319d0..1d8c5806 100644 --- a/board/drivers/harness.h +++ b/board/drivers/harness.h @@ -100,13 +100,8 @@ void harness_init(void) { set_gpio_output(current_board->harness_config->GPIO_relay_SBU1, current_board->harness_config->pin_relay_SBU1, 1); set_gpio_output(current_board->harness_config->GPIO_relay_SBU2, current_board->harness_config->pin_relay_SBU2, 1); - // try to detect orientation + // detect initial orientation harness.status = harness_detect_orientation(); - if (harness.status != HARNESS_STATUS_NC) { - print("detected car harness with orientation "); puth2(harness.status); print("\n"); - } else { - print("failed to detect car harness!\n"); - } // keep buses connected by default set_intercept_relay(false, false); diff --git a/board/drivers/spi.h b/board/drivers/spi.h index 36f4dd29..469243db 100644 --- a/board/drivers/spi.h +++ b/board/drivers/spi.h @@ -108,7 +108,9 @@ void spi_rx_done(void) { response_len = 1U; } else { // response: NACK and reset state machine - print("- incorrect header sync or checksum "); hexdump(spi_buf_rx, SPI_HEADER_SIZE); + #ifdef DEBUG_SPI + print("- incorrect header sync or checksum "); hexdump(spi_buf_rx, SPI_HEADER_SIZE); + #endif spi_buf_tx[0] = SPI_NACK; next_rx_state = SPI_STATE_HEADER_NACK; response_len = 1U; @@ -160,12 +162,14 @@ void spi_rx_done(void) { } else { // Checksum was incorrect response_ack = false; - print("- incorrect data checksum "); - puth4(spi_data_len_mosi); - print("\n"); - hexdump(spi_buf_rx, SPI_HEADER_SIZE); - hexdump(&(spi_buf_rx[SPI_HEADER_SIZE]), MIN(spi_data_len_mosi, 64)); - print("\n"); + #ifdef DEBUG_SPI + print("- incorrect data checksum "); + puth4(spi_data_len_mosi); + print("\n"); + hexdump(spi_buf_rx, SPI_HEADER_SIZE); + hexdump(&(spi_buf_rx[SPI_HEADER_SIZE]), MIN(spi_data_len_mosi, 64)); + print("\n"); + #endif } if (!response_ack) { diff --git a/board/drivers/uart.h b/board/drivers/uart.h index 737539c9..ee15999a 100644 --- a/board/drivers/uart.h +++ b/board/drivers/uart.h @@ -145,10 +145,6 @@ void puth(unsigned int i) { puthx(i, 8U); } -void puth2(unsigned int i) { - puthx(i, 2U); -} - #if defined(ENABLE_SPI) || defined(BOOTSTUB) || defined(DEBUG) void puth4(unsigned int i) { puthx(i, 4U); @@ -160,7 +156,7 @@ void hexdump(const void *a, int l) { if (a != NULL) { for (int i=0; i < l; i++) { if ((i != 0) && ((i & 0xf) == 0)) print("\n"); - puth2(((const unsigned char*)a)[i]); + puthx(((const unsigned char*)a)[i], 2U); print(" "); } } diff --git a/board/drivers/uart_declarations.h b/board/drivers/uart_declarations.h index 041fd377..f2775d3f 100644 --- a/board/drivers/uart_declarations.h +++ b/board/drivers/uart_declarations.h @@ -33,7 +33,6 @@ void putch(const char a); void print(const char *a); void puthx(uint32_t i, uint8_t len); void puth(unsigned int i); -void puth2(unsigned int i); #if defined(ENABLE_SPI) || defined(BOOTSTUB) || defined(DEBUG) void puth4(unsigned int i); #endif diff --git a/board/drivers/usb.h b/board/drivers/usb.h index 8fd3d8c4..1f715fee 100644 --- a/board/drivers/usb.h +++ b/board/drivers/usb.h @@ -538,21 +538,26 @@ void usb_irqhandler(void) { } if ((gintsts & USB_OTG_GINTSTS_USBRST) != 0U) { - print("USB reset\n"); + #ifdef DEBUG_USB + print("USB reset\n"); + #endif usb_reset(); } if ((gintsts & USB_OTG_GINTSTS_ENUMDNE) != 0U) { - print("enumeration done"); + #ifdef DEBUG_USB + print("enumeration done\n"); + #endif // Full speed, ENUMSPD //puth(USBx_DEVICE->DSTS); - print("\n"); } if ((gintsts & USB_OTG_GINTSTS_OTGINT) != 0U) { - print("OTG int:"); - puth(USBx->GOTGINT); - print("\n"); + #ifdef DEBUG_USB + print("OTG int:"); + puth(USBx->GOTGINT); + print("\n"); + #endif // getting ADTOCHG //USBx->GOTGINT = USBx->GOTGINT; diff --git a/board/main_declarations.h b/board/main_declarations.h index 2704a01a..3b4a807d 100644 --- a/board/main_declarations.h +++ b/board/main_declarations.h @@ -3,7 +3,6 @@ // ******************** Prototypes ******************** void print(const char *a); void puth(unsigned int i); -void puth2(unsigned int i); void puth4(unsigned int i); void hexdump(const void *a, int l); typedef struct board board; From 0dc95a89dd7df1e27c36d7560c39666151af81df Mon Sep 17 00:00:00 2001 From: Robbe Derks Date: Fri, 11 Apr 2025 00:55:54 +0200 Subject: [PATCH 4/5] Cuatro: changes for new board rev (#2176) changes for new board rev Co-authored-by: Comma Device --- board/boards/cuatro.h | 18 ++++++++++++++++-- board/boards/dos.h | 2 +- board/boards/tres.h | 2 +- board/drivers/clock_source.h | 7 +++++-- board/drivers/clock_source_declarations.h | 2 +- 5 files changed, 24 insertions(+), 7 deletions(-) diff --git a/board/boards/cuatro.h b/board/boards/cuatro.h index 4407903b..d9e219c0 100644 --- a/board/boards/cuatro.h +++ b/board/boards/cuatro.h @@ -91,7 +91,7 @@ static void cuatro_init(void) { tres_set_ir_power(0U); // Clock source - clock_source_init(); + clock_source_init(true); // Sound codec cuatro_set_amp_enabled(false); @@ -105,8 +105,22 @@ static void cuatro_init(void) { sound_init(); } +static harness_configuration cuatro_harness_config = { + .has_harness = true, + .GPIO_SBU1 = GPIOC, + .GPIO_SBU2 = GPIOA, + .GPIO_relay_SBU1 = GPIOA, + .GPIO_relay_SBU2 = GPIOA, + .pin_SBU1 = 4, + .pin_SBU2 = 1, + .pin_relay_SBU1 = 9, + .pin_relay_SBU2 = 3, + .adc_channel_SBU1 = 4, // ADC12_INP4 + .adc_channel_SBU2 = 17 // ADC1_INP17 +}; + board board_cuatro = { - .harness_config = &tres_harness_config, + .harness_config = &cuatro_harness_config, .has_spi = true, .has_canfd = true, .fan_max_rpm = 12500U, diff --git a/board/boards/dos.h b/board/boards/dos.h index 11cc8b5d..661f91fb 100644 --- a/board/boards/dos.h +++ b/board/boards/dos.h @@ -106,7 +106,7 @@ static void dos_init(void) { dos_set_bootkick(true); // Init clock source (camera strobe) using PWM - clock_source_init(); + clock_source_init(false); } static harness_configuration dos_harness_config = { diff --git a/board/boards/tres.h b/board/boards/tres.h index 585487b2..811314a5 100644 --- a/board/boards/tres.h +++ b/board/boards/tres.h @@ -129,7 +129,7 @@ static void tres_init(void) { register_set_bits(&(GPIOC->OTYPER), GPIO_OTYPER_OT10 | GPIO_OTYPER_OT11); // open drain // Clock source - clock_source_init(); + clock_source_init(false); } static harness_configuration tres_harness_config = { diff --git a/board/drivers/clock_source.h b/board/drivers/clock_source.h index 18d12d57..9fb818aa 100644 --- a/board/drivers/clock_source.h +++ b/board/drivers/clock_source.h @@ -4,7 +4,7 @@ void clock_source_set_period(uint8_t period) { register_set(&(TIM1->ARR), ((period*10U) - 1U), 0xFFFFU); } -void clock_source_init(void) { +void clock_source_init(bool enable_channel1) { // Setup timer register_set(&(TIM1->PSC), ((APB2_TIMER_FREQ*100U)-1U), 0xFFFFU); // Tick on 0.1 ms register_set(&(TIM1->ARR), ((CLOCK_SOURCE_PERIOD_MS*10U) - 1U), 0xFFFFU); // Period @@ -21,11 +21,14 @@ void clock_source_init(void) { NVIC_DisableIRQ(TIM1_CC_IRQn); // Set GPIO as timer channels + if (enable_channel1) { + set_gpio_alternate(GPIOA, 8, GPIO_AF1_TIM1); + } set_gpio_alternate(GPIOB, 14, GPIO_AF1_TIM1); set_gpio_alternate(GPIOB, 15, GPIO_AF1_TIM1); // Set PWM mode - register_set(&(TIM1->CCMR1), (0b110UL << TIM_CCMR1_OC2M_Pos), 0xFFFFU); + register_set(&(TIM1->CCMR1), (0b110UL << TIM_CCMR1_OC1M_Pos) | (0b110UL << TIM_CCMR1_OC2M_Pos), 0xFFFFU); register_set(&(TIM1->CCMR2), (0b110UL << TIM_CCMR2_OC3M_Pos), 0xFFFFU); // Enable output diff --git a/board/drivers/clock_source_declarations.h b/board/drivers/clock_source_declarations.h index 013f7530..b230b4d6 100644 --- a/board/drivers/clock_source_declarations.h +++ b/board/drivers/clock_source_declarations.h @@ -4,4 +4,4 @@ #define CLOCK_SOURCE_PULSE_LEN_MS 2U void clock_source_set_period(uint8_t period); -void clock_source_init(void); +void clock_source_init(bool enable_channel1); From d319ea2bbae4ee0f5dea12d7ca5456cd34288d55 Mon Sep 17 00:00:00 2001 From: Shane Smiskol Date: Mon, 14 Apr 2025 21:06:01 -0700 Subject: [PATCH 5/5] CAN ignition: fix unscoped prev counter (#2191) scoped prev_counter --- board/drivers/can_common.h | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/board/drivers/can_common.h b/board/drivers/can_common.h index eeaf8738..fa69c698 100644 --- a/board/drivers/can_common.h +++ b/board/drivers/can_common.h @@ -166,9 +166,6 @@ void ignition_can_hook(CANPacket_t *to_push) { int addr = GET_ADDR(to_push); int len = GET_LEN(to_push); - // Check counter position on cars with overlap - static int prev_counter = -1; - // GM exception if ((addr == 0x1F1) && (len == 8)) { // SystemPowerMode (2=Run, 3=Crank Request) @@ -181,6 +178,7 @@ void ignition_can_hook(CANPacket_t *to_push) { // 0x152 overlaps with Subaru pre-global which has this bit as the high beam int counter = GET_BYTE(to_push, 1) & 0xFU; // max is only 14 + static int prev_counter = -1; if ((counter == ((prev_counter + 1) % 15)) && (prev_counter != -1)) { // VDM_OutputSignals->VDM_EpasPowerMode ignition_can = ((GET_BYTE(to_push, 7) >> 4U) & 0x3U) == 1U; // VDM_EpasPowerMode_Drive_On=1 @@ -194,6 +192,7 @@ void ignition_can_hook(CANPacket_t *to_push) { // 0x221 overlaps with Rivian which has random data on byte 0 int counter = GET_BYTE(to_push, 6) >> 4; + static int prev_counter = -1; if ((counter == ((prev_counter + 1) % 16)) && (prev_counter != -1)) { // VCFRONT_LVPowerState->VCFRONT_vehiclePowerState int power_state = (GET_BYTE(to_push, 0) >> 5U) & 0x3U;