Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

low power beeping #2081

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion board/SConscript
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}

Expand Down
59 changes: 1 addition & 58 deletions board/boards/cuatro.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -82,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 = {
Expand Down
48 changes: 0 additions & 48 deletions board/boards/red_chiplet.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
73 changes: 54 additions & 19 deletions board/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,21 @@
#include "can_comms.h"
#include "main_comms.h"

#ifdef STM32H7
void LPTIM1_IRQHandler(void) {
//static uint8_t cnt = 0;
static bool on = true;
if (LPTIM1->ISR & LPTIM_ISR_ARRM) {
LPTIM1->ICR |= LPTIM_ICR_ARRMCF;

//uptime_cnt++;
on = !on;
//current_board->set_siren(on);
} else {
//assert_fatal(false, "shouldn't happen");
}
}
#endif

// ********************* Serial debugging *********************

Expand Down Expand Up @@ -151,7 +166,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();
Expand All @@ -171,8 +186,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");
Expand Down Expand Up @@ -211,10 +235,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) {
Expand All @@ -233,22 +253,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;
Expand Down Expand Up @@ -281,7 +298,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;

Expand All @@ -306,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");
Expand All @@ -321,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();
Expand Down Expand Up @@ -365,7 +392,15 @@ int main(void) {
enable_interrupts();

// 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;
/*
*/
continue;

if (power_save_status == POWER_SAVE_STATUS_DISABLED) {
#ifdef DEBUG_FAULTS
if (fault_status == FAULT_STATUS_NONE) {
Expand Down
2 changes: 1 addition & 1 deletion board/stm32h7/interrupt_handlers.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);}
Expand Down
44 changes: 18 additions & 26 deletions board/stm32h7/peripherals.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,39 +102,31 @@ 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
// Timers
RCC->APB1LENR |= RCC_APB1LENR_LPTIM1EN; // low power siren timer
RCC->APB1LENR |= RCC_APB1LENR_TIM4EN;

// Analog
RCC->AHB1ENR |= RCC_AHB1ENR_ADC12EN; // Enable ADC12 clocks
RCC->APB1LENR |= RCC_APB1LENR_DAC12EN; // DAC
// *** LPTIM ***
RCC->CSR |= RCC_CSR_LSION;
while (!(RCC->CSR & RCC_CSR_LSIRDY));
RCC->D2CCIP2R &= ~RCC_D2CCIP2R_LPTIM1SEL;
RCC->D2CCIP2R |= RCC_D2CCIP2R_LPTIM1SEL_2;

// Audio
RCC->APB4ENR |= RCC_APB4ENR_SAI4EN; // SAI4
// prescaler 1:128
LPTIM1->CFGR &= ~LPTIM_CFGR_PRESC;
LPTIM1->CFGR |= (7 << LPTIM_CFGR_PRESC_Pos);

// Timers
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
RCC->APB1LENR |= RCC_APB1LENR_TIM12EN; // slow loop
LPTIM1->CR |= LPTIM_CR_ENABLE;
LPTIM1->CMP = 0;
LPTIM1->ARR = 30;
LPTIM1->CR |= LPTIM_CR_CNTSTRT;

LPTIM1->IER |= LPTIM_IER_ARRMIE;

#ifdef PANDA_JUNGLE
RCC->AHB3ENR |= RCC_AHB3ENR_SDMMC1EN; // SDMMC
Expand Down
2 changes: 1 addition & 1 deletion tests/reflash_internal_panda.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down