From c2e85aad043a4200b5f3c130760715dbdb505384 Mon Sep 17 00:00:00 2001 From: Comma Device Date: Fri, 15 Nov 2024 04:32:45 +0000 Subject: [PATCH 1/3] works --- board/boards/cuatro.h | 1 + board/main.c | 43 ++++++++++++++++++++---------- board/stm32h7/interrupt_handlers.h | 2 +- board/stm32h7/peripherals.h | 16 ++++++++++- 4 files changed, 46 insertions(+), 16 deletions(-) diff --git a/board/boards/cuatro.h b/board/boards/cuatro.h index 4a6b5da096..da50c1234f 100644 --- a/board/boards/cuatro.h +++ b/board/boards/cuatro.h @@ -7,6 +7,7 @@ // ////////////////////////// // static void cuatro_set_led(uint8_t color, bool enabled) { + if (enabled) enabled = false; switch (color) { case LED_RED: set_gpio_output(GPIOD, 15, !enabled); diff --git a/board/main.c b/board/main.c index aaea95d089..60602b552f 100644 --- a/board/main.c +++ b/board/main.c @@ -28,6 +28,19 @@ #include "can_comms.h" #include "main_comms.h" +#ifdef STM32H7 +void LPTIM1_IRQHandler(void) { + static uint8_t cnt = 0; + if (LPTIM1->ISR & LPTIM_ISR_ARRM) { + LPTIM1->ICR |= LPTIM_ICR_ARRMCF; + current_board->set_siren((cnt++ % 2 == 0U)); + } else { + assert_fatal(false, "shouldn't happen"); + } +} +#endif + + // ********************* Serial debugging ********************* @@ -151,7 +164,7 @@ static void tick_handler(void) { if (TICK_TIMER->SR != 0U) { // siren - current_board->set_siren((loop_counter & 1U) && (siren_enabled || (siren_countdown > 0U))); + //current_board->set_siren((loop_counter & 1U) && (siren_enabled || (siren_countdown > 0U))); // tick drivers at 8Hz fan_tick(); @@ -171,8 +184,17 @@ static void tick_handler(void) { set_power_save_state(power_save_status); } + if (loop_counter == 0U) { + + if (siren_countdown > 0U) { + siren_countdown -= 1U; + } + + } + + // decimated to 1Hz - if (loop_counter == 0U) { + if (true) { can_live = pending_can_live; //puth(usart1_dma); print(" "); puth(DMA2_Stream5->M0AR); print(" "); puth(DMA2_Stream5->NDTR); print("\n"); @@ -211,10 +233,6 @@ static void tick_handler(void) { heartbeat_disabled = false; } - if (siren_countdown > 0U) { - siren_countdown -= 1U; - } - if (controls_allowed || heartbeat_engaged) { controls_allowed_countdown = 30U; } else if (controls_allowed_countdown > 0U) { @@ -233,22 +251,19 @@ static void tick_handler(void) { heartbeat_engaged_mismatches = 0U; } - if (!heartbeat_disabled) { + if (!heartbeat_lost) { // if the heartbeat has been gone for a while, go to SILENT safety mode and enter power save - if (heartbeat_counter >= (check_started() ? HEARTBEAT_IGNITION_CNT_ON : HEARTBEAT_IGNITION_CNT_OFF)) { + if (current_board->read_voltage_mV() < 150) { print("device hasn't sent a heartbeat for 0x"); puth(heartbeat_counter); print(" seconds. Safety is set to SILENT mode.\n"); - if (controls_allowed_countdown > 0U) { - siren_countdown = 3U; + if (true) { + siren_countdown = 1U; controls_allowed_countdown = 0U; } - // set flag to indicate the heartbeat was lost - if (is_car_safety_mode(current_safety_mode)) { - heartbeat_lost = true; - } + heartbeat_lost = true; // clear heartbeat engaged state heartbeat_engaged = false; diff --git a/board/stm32h7/interrupt_handlers.h b/board/stm32h7/interrupt_handlers.h index 8148021d69..4de03de9fc 100644 --- a/board/stm32h7/interrupt_handlers.h +++ b/board/stm32h7/interrupt_handlers.h @@ -88,7 +88,7 @@ void LTDC_IRQHandler(void) {handle_interrupt(LTDC_IRQn);} void LTDC_ER_IRQHandler(void) {handle_interrupt(LTDC_ER_IRQn);} void DMA2D_IRQHandler(void) {handle_interrupt(DMA2D_IRQn);} void OCTOSPI1_IRQHandler(void) {handle_interrupt(OCTOSPI1_IRQn);} -void LPTIM1_IRQHandler(void) {handle_interrupt(LPTIM1_IRQn);} +//void LPTIM1_IRQHandler(void) {handle_interrupt(LPTIM1_IRQn);} void CEC_IRQHandler(void) {handle_interrupt(CEC_IRQn);} void I2C4_EV_IRQHandler(void) {handle_interrupt(I2C4_EV_IRQn);} void I2C4_ER_IRQHandler(void) {handle_interrupt(I2C4_ER_IRQn);} diff --git a/board/stm32h7/peripherals.h b/board/stm32h7/peripherals.h index f0bd727fdf..d2a75217cb 100644 --- a/board/stm32h7/peripherals.h +++ b/board/stm32h7/peripherals.h @@ -127,7 +127,8 @@ void peripherals_init(void) { RCC->APB4ENR |= RCC_APB4ENR_SAI4EN; // SAI4 // Timers - RCC->APB2ENR |= RCC_APB2ENR_TIM1EN; // clock source timer + RCC->APB2ENR |= RCC_APB2ENR_TIM1EN; // clock source timer + RCC->APB1LENR |= RCC_APB1LENR_LPTIM1EN; // low power siren timer RCC->APB1LENR |= RCC_APB1LENR_TIM2EN; // main counter RCC->APB1LENR |= RCC_APB1LENR_TIM3EN; // fan pwm RCC->APB1LENR |= RCC_APB1LENR_TIM4EN; // beeper source @@ -136,6 +137,19 @@ void peripherals_init(void) { RCC->APB2ENR |= RCC_APB2ENR_TIM8EN; // tick timer RCC->APB1LENR |= RCC_APB1LENR_TIM12EN; // slow loop + // *** LPTIM *** + // prescaler 1:128 + LPTIM1->CFGR &= ~LPTIM_CFGR_PRESC; + LPTIM1->CFGR |= (7 << LPTIM_CFGR_PRESC_Pos); + + LPTIM1->CR |= LPTIM_CR_ENABLE; + LPTIM1->CMP = 0; + LPTIM1->ARR = 58875; + LPTIM1->CR |= LPTIM_CR_CNTSTRT; + + LPTIM1->IER |= LPTIM_IER_ARRMIE; + NVIC_EnableIRQ(LPTIM1_IRQn); + #ifdef PANDA_JUNGLE RCC->AHB3ENR |= RCC_AHB3ENR_SDMMC1EN; // SDMMC RCC->AHB4ENR |= RCC_AHB4ENR_ADC3EN; // Enable ADC3 clocks From 465a1bb8c5479c47f2c86fd5d71e02e072ce5a4b Mon Sep 17 00:00:00 2001 From: Comma Device Date: Fri, 15 Nov 2024 06:10:19 +0000 Subject: [PATCH 2/3] switch to lsi --- board/main.c | 13 +++++++++++-- board/stm32h7/peripherals.h | 7 ++++++- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/board/main.c b/board/main.c index 60602b552f..01f82f666d 100644 --- a/board/main.c +++ b/board/main.c @@ -32,10 +32,11 @@ void LPTIM1_IRQHandler(void) { static uint8_t cnt = 0; if (LPTIM1->ISR & LPTIM_ISR_ARRM) { + uptime_cnt++; LPTIM1->ICR |= LPTIM_ICR_ARRMCF; current_board->set_siren((cnt++ % 2 == 0U)); } else { - assert_fatal(false, "shouldn't happen"); + //assert_fatal(false, "shouldn't happen"); } } #endif @@ -296,7 +297,7 @@ static void tick_handler(void) { } // on to the next one - uptime_cnt += 1U; + //uptime_cnt += 1U; safety_mode_cnt += 1U; ignition_can_cnt += 1U; @@ -380,7 +381,15 @@ int main(void) { enable_interrupts(); // LED should keep on blinking all the time + NVIC_DisableIRQ(TICK_TIMER_IRQ); while (true) { + __WFI(); + SCB->SCR &= ~SCB_SCR_SLEEPDEEP_Msk; + /* + SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk; + */ + continue; + if (power_save_status == POWER_SAVE_STATUS_DISABLED) { #ifdef DEBUG_FAULTS if (fault_status == FAULT_STATUS_NONE) { diff --git a/board/stm32h7/peripherals.h b/board/stm32h7/peripherals.h index d2a75217cb..c3685d27be 100644 --- a/board/stm32h7/peripherals.h +++ b/board/stm32h7/peripherals.h @@ -138,13 +138,18 @@ void peripherals_init(void) { RCC->APB1LENR |= RCC_APB1LENR_TIM12EN; // slow loop // *** LPTIM *** + RCC->CSR |= RCC_CSR_LSION; + while (!(RCC->CSR & RCC_CSR_LSIRDY)); + RCC->D2CCIP2R &= ~RCC_D2CCIP2R_LPTIM1SEL; + RCC->D2CCIP2R |= RCC_D2CCIP2R_LPTIM1SEL_2; + // prescaler 1:128 LPTIM1->CFGR &= ~LPTIM_CFGR_PRESC; LPTIM1->CFGR |= (7 << LPTIM_CFGR_PRESC_Pos); LPTIM1->CR |= LPTIM_CR_ENABLE; LPTIM1->CMP = 0; - LPTIM1->ARR = 58875; + LPTIM1->ARR = 30; LPTIM1->CR |= LPTIM_CR_CNTSTRT; LPTIM1->IER |= LPTIM_IER_ARRMIE; From 68e3aee3e359ca85e21fd8110fb5b2b408a549db Mon Sep 17 00:00:00 2001 From: Comma Device Date: Sun, 17 Nov 2024 00:02:44 +0000 Subject: [PATCH 3/3] wip --- board/SConscript | 2 +- board/boards/cuatro.h | 58 --------------------------------- board/boards/red_chiplet.h | 48 --------------------------- board/main.c | 31 ++++++++++++------ board/stm32h7/peripherals.h | 31 ++---------------- tests/reflash_internal_panda.py | 2 +- 6 files changed, 25 insertions(+), 147 deletions(-) diff --git a/board/SConscript b/board/SConscript index 3ff77648ba..d2f91fdc2b 100644 --- a/board/SConscript +++ b/board/SConscript @@ -4,7 +4,7 @@ import copy Import('build_project', 'base_project_f4', 'base_project_h7') build_projects = { - "panda": base_project_f4, + #"panda": base_project_f4, "panda_h7": base_project_h7, } diff --git a/board/boards/cuatro.h b/board/boards/cuatro.h index da50c1234f..1df2d1ed65 100644 --- a/board/boards/cuatro.h +++ b/board/boards/cuatro.h @@ -83,67 +83,9 @@ static void cuatro_set_amp_enabled(bool 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(GPIOB, 2, OUTPUT_TYPE_OPEN_DRAIN); - set_gpio_output_type(GPIOD, 15, OUTPUT_TYPE_OPEN_DRAIN); - - // Power readout - set_gpio_mode(GPIOC, 5, MODE_ANALOG); - set_gpio_mode(GPIOA, 6, MODE_ANALOG); - - // CAN transceiver enables - set_gpio_pullup(GPIOB, 7, PULL_NONE); - set_gpio_mode(GPIOB, 7, MODE_OUTPUT); - set_gpio_pullup(GPIOD, 8, PULL_NONE); - set_gpio_mode(GPIOD, 8, MODE_OUTPUT); - - // FDCAN3, different pins on this package than the rest of the reds - set_gpio_pullup(GPIOD, 12, PULL_NONE); - set_gpio_alternate(GPIOD, 12, GPIO_AF5_FDCAN3); - set_gpio_pullup(GPIOD, 13, PULL_NONE); - set_gpio_alternate(GPIOD, 13, GPIO_AF5_FDCAN3); - - // C2: SOM GPIO used as input (fan control at boot) - set_gpio_mode(GPIOC, 2, MODE_INPUT); - set_gpio_pullup(GPIOC, 2, PULL_DOWN); - - // SOM bootkick + reset lines - cuatro_set_bootkick(BOOT_BOOTKICK); - - // SOM debugging UART - gpio_uart7_init(); - uart_init(&uart_ring_som_debug, 115200); - - // SPI init - gpio_spi_init(); - - // fan setup - set_gpio_alternate(GPIOC, 8, GPIO_AF2_TIM3); - register_set_bits(&(GPIOC->OTYPER), GPIO_OTYPER_OT8); // open drain - - // Initialize IR PWM and set to 0% - set_gpio_alternate(GPIOC, 9, GPIO_AF2_TIM3); - pwm_init(TIM3, 4); - tres_set_ir_power(0U); - - // 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 = { diff --git a/board/boards/red_chiplet.h b/board/boards/red_chiplet.h index e5d1aa097f..833f02b91f 100644 --- a/board/boards/red_chiplet.h +++ b/board/boards/red_chiplet.h @@ -86,54 +86,6 @@ static void red_chiplet_set_fan_or_usb_load_switch(bool enabled) { } static void red_chiplet_init(void) { - common_init_gpio(); - - // A8, A3: OBD_SBU1_RELAY, OBD_SBU2_RELAY - set_gpio_output_type(GPIOA, 8, OUTPUT_TYPE_OPEN_DRAIN); - set_gpio_pullup(GPIOA, 8, PULL_NONE); - set_gpio_output(GPIOA, 8, 1); - set_gpio_mode(GPIOA, 8, MODE_OUTPUT); - - set_gpio_output_type(GPIOA, 3, OUTPUT_TYPE_OPEN_DRAIN); - set_gpio_pullup(GPIOA, 3, PULL_NONE); - set_gpio_output(GPIOA, 3, 1); - set_gpio_mode(GPIOA, 3, MODE_OUTPUT); - - // G11,B10,D7,B11: transceiver enable - set_gpio_pullup(GPIOG, 11, PULL_NONE); - set_gpio_mode(GPIOG, 11, MODE_OUTPUT); - - set_gpio_pullup(GPIOB, 10, PULL_NONE); - set_gpio_mode(GPIOB, 10, MODE_OUTPUT); - - set_gpio_pullup(GPIOD, 7, PULL_NONE); - set_gpio_mode(GPIOD, 7, MODE_OUTPUT); - - set_gpio_pullup(GPIOB, 11, PULL_NONE); - set_gpio_mode(GPIOB, 11, MODE_OUTPUT); - - // D3: usb load switch - set_gpio_pullup(GPIOD, 3, PULL_NONE); - set_gpio_mode(GPIOD, 3, MODE_OUTPUT); - - // B0: 5VOUT_S - set_gpio_pullup(GPIOB, 0, PULL_NONE); - set_gpio_mode(GPIOB, 0, MODE_ANALOG); - - // Initialize harness - harness_init(); - - - // Enable CAN transceivers - red_chiplet_enable_can_transceivers(true); - - // Disable LEDs - red_set_led(LED_RED, false); - red_set_led(LED_GREEN, false); - red_set_led(LED_BLUE, false); - - // Set normal CAN mode - red_chiplet_set_can_mode(CAN_MODE_NORMAL); } static harness_configuration red_chiplet_harness_config = { diff --git a/board/main.c b/board/main.c index 01f82f666d..6b95ac6684 100644 --- a/board/main.c +++ b/board/main.c @@ -30,19 +30,20 @@ #ifdef STM32H7 void LPTIM1_IRQHandler(void) { - static uint8_t cnt = 0; + //static uint8_t cnt = 0; + static bool on = true; if (LPTIM1->ISR & LPTIM_ISR_ARRM) { - uptime_cnt++; LPTIM1->ICR |= LPTIM_ICR_ARRMCF; - current_board->set_siren((cnt++ % 2 == 0U)); + + //uptime_cnt++; + on = !on; + //current_board->set_siren(on); } else { //assert_fatal(false, "shouldn't happen"); } } #endif - - // ********************* Serial debugging ********************* static bool check_started(void) { @@ -322,10 +323,6 @@ int main(void) { clock_init(); peripherals_init(); detect_board_type(); - // red+green leds enabled until succesful USB/SPI init, as a debug indicator - current_board->set_led(LED_RED, true); - current_board->set_led(LED_GREEN, true); - adc_init(); // print hello print("\n\n\n************************ MAIN START ************************\n"); @@ -337,7 +334,21 @@ int main(void) { print(" Board type: 0x"); puth(hw_type); print("\n"); // init board + current_board->init(); + //enable_interrupts(); + //NVIC_EnableIRQ(LPTIM1_IRQn); + while (true) { + current_board->set_siren(true); + delay(512000U); + current_board->set_siren(false); + delay(512000U); + continue; + + //SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk; + __WFI(); + SCB->SCR &= ~SCB_SCR_SLEEPDEEP_Msk; + } // panda has an FPU, let's use it! enable_fpu(); @@ -383,10 +394,10 @@ int main(void) { // LED should keep on blinking all the time NVIC_DisableIRQ(TICK_TIMER_IRQ); while (true) { + //SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk; __WFI(); SCB->SCR &= ~SCB_SCR_SLEEPDEEP_Msk; /* - SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk; */ continue; diff --git a/board/stm32h7/peripherals.h b/board/stm32h7/peripherals.h index c3685d27be..4866832c3d 100644 --- a/board/stm32h7/peripherals.h +++ b/board/stm32h7/peripherals.h @@ -102,40 +102,14 @@ void peripherals_init(void) { RCC->AHB4ENR |= RCC_AHB4ENR_GPIOGEN; // Enable CPU access to SRAMs for DMA - RCC->AHB2ENR |= RCC_AHB2ENR_SRAM1EN | RCC_AHB2ENR_SRAM2EN; + //RCC->AHB2ENR |= RCC_AHB2ENR_SRAM1EN | RCC_AHB2ENR_SRAM2EN; // Supplemental - RCC->AHB1ENR |= RCC_AHB1ENR_DMA1EN; // DAC DMA - RCC->AHB1ENR |= RCC_AHB1ENR_DMA2EN; // SPI DMA RCC->APB4ENR |= RCC_APB4ENR_SYSCFGEN; - RCC->AHB4ENR |= RCC_AHB4ENR_BDMAEN; // Audio DMA - - // Connectivity - RCC->APB2ENR |= RCC_APB2ENR_SPI4EN; // SPI - RCC->APB1LENR |= RCC_APB1LENR_I2C5EN; // codec I2C - RCC->AHB1ENR |= RCC_AHB1ENR_USB1OTGHSEN; // USB - RCC->AHB1LPENR |= RCC_AHB1LPENR_USB1OTGHSLPEN; // USB LP needed for CSleep state(__WFI()) - RCC->AHB1LPENR &= ~(RCC_AHB1LPENR_USB1OTGHSULPILPEN); // disable USB ULPI - RCC->APB1LENR |= RCC_APB1LENR_UART7EN; // SOM uart - RCC->APB1HENR |= RCC_APB1HENR_FDCANEN; // FDCAN core enable - - // Analog - RCC->AHB1ENR |= RCC_AHB1ENR_ADC12EN; // Enable ADC12 clocks - RCC->APB1LENR |= RCC_APB1LENR_DAC12EN; // DAC - - // Audio - RCC->APB4ENR |= RCC_APB4ENR_SAI4EN; // SAI4 // Timers - RCC->APB2ENR |= RCC_APB2ENR_TIM1EN; // clock source timer RCC->APB1LENR |= RCC_APB1LENR_LPTIM1EN; // low power siren 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 - RCC->APB1LENR |= RCC_APB1LENR_TIM12EN; // slow loop + RCC->APB1LENR |= RCC_APB1LENR_TIM4EN; // *** LPTIM *** RCC->CSR |= RCC_CSR_LSION; @@ -153,7 +127,6 @@ void peripherals_init(void) { LPTIM1->CR |= LPTIM_CR_CNTSTRT; LPTIM1->IER |= LPTIM_IER_ARRMIE; - NVIC_EnableIRQ(LPTIM1_IRQn); #ifdef PANDA_JUNGLE RCC->AHB3ENR |= RCC_AHB3ENR_SDMMC1EN; // SDMMC diff --git a/tests/reflash_internal_panda.py b/tests/reflash_internal_panda.py index c2ad9f8964..ee89b5aab3 100755 --- a/tests/reflash_internal_panda.py +++ b/tests/reflash_internal_panda.py @@ -33,7 +33,7 @@ def gpio_set(pin, high): time.sleep(1) gpio_set(GPIO.STM_RST_N, 0) gpio_set(GPIO.STM_BOOT0, 0) - time.sleep(1) + time.sleep(0.2) print("flashing bootstub") PandaDFU(None).recover()