Skip to content

Commit

Permalink
Power saving refactor (#389)
Browse files Browse the repository at this point in the history
* Exit power saving also for CAN based ingition detection and replace interrup with simple state machine. a bit of delay it's fine

* bug fixes and also CDP set on start

* sorry misra

* move comment

* typo

* remove ignition interrupts

* Power saving state machine should be done by boardd, which also sets safety modes and usb power modes

* typo

* Added usb command for power save state setting

* Added power save enabled packet

* Added power_save_enabled to python lib too
  • Loading branch information
rbiasini authored Nov 21, 2019
1 parent d58d08f commit 1192d93
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 79 deletions.
7 changes: 0 additions & 7 deletions board/boards/white.h
Original file line number Diff line number Diff line change
Expand Up @@ -302,13 +302,6 @@ void white_grey_common_init(void) {
// Set normal CAN mode
white_set_can_mode(CAN_MODE_NORMAL);

// Setup ignition interrupts
SYSCFG->EXTICR[1] = SYSCFG_EXTICR1_EXTI1_PA;
EXTI->IMR |= (1U << 1);
EXTI->RTSR |= (1U << 1);
EXTI->FTSR |= (1U << 1);
NVIC_EnableIRQ(EXTI1_IRQn);

// Init usb power mode
uint32_t voltage = adc_get_voltage();
// Init in CDP mode only if panda is powered by 12V.
Expand Down
31 changes: 3 additions & 28 deletions board/drivers/harness.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ uint8_t car_harness_status = 0U;

struct harness_configuration {
const bool has_harness;
GPIO_TypeDef *GPIO_SBU1;
GPIO_TypeDef *GPIO_SBU1;
GPIO_TypeDef *GPIO_SBU2;
GPIO_TypeDef *GPIO_relay_normal;
GPIO_TypeDef *GPIO_relay_flipped;
Expand Down Expand Up @@ -52,28 +52,6 @@ bool harness_check_ignition(void) {
return ret;
}

// TODO: refactor to use harness config
void harness_setup_ignition_interrupts(void){
if(car_harness_status == HARNESS_STATUS_NORMAL){
SYSCFG->EXTICR[0] = SYSCFG_EXTICR1_EXTI3_PC;
EXTI->IMR |= (1U << 3);
EXTI->RTSR |= (1U << 3);
EXTI->FTSR |= (1U << 3);
puts("setup interrupts: normal\n");
} else if(car_harness_status == HARNESS_STATUS_FLIPPED) {
SYSCFG->EXTICR[0] = SYSCFG_EXTICR1_EXTI0_PC;
EXTI->IMR |= (1U << 0);
EXTI->RTSR |= (1U << 0);
EXTI->FTSR |= (1U << 0);
NVIC_EnableIRQ(EXTI1_IRQn);
puts("setup interrupts: flipped\n");
} else {
puts("tried to setup ignition interrupts without harness connected\n");
}
NVIC_EnableIRQ(EXTI0_IRQn);
NVIC_EnableIRQ(EXTI3_IRQn);
}

uint8_t harness_detect_orientation(void) {
uint8_t ret = HARNESS_STATUS_NC;

Expand Down Expand Up @@ -117,14 +95,11 @@ void harness_init(void) {
set_gpio_mode(current_board->harness_config->GPIO_SBU2, current_board->harness_config->pin_SBU2, MODE_INPUT);
} else {
set_gpio_mode(current_board->harness_config->GPIO_SBU1, current_board->harness_config->pin_SBU1, MODE_INPUT);
}
}

// keep busses connected by default
set_intercept_relay(false);

// setup ignition interrupts
harness_setup_ignition_interrupts();
} else {
puts("failed to detect car harness!\n");
}
}
}
62 changes: 20 additions & 42 deletions board/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,45 +72,6 @@ void debug_ring_callback(uart_ring *ring) {
}
}

// ***************************** started logic *****************************
void started_interrupt_handler(uint8_t interrupt_line) {
volatile unsigned int pr = EXTI->PR & (1U << interrupt_line);
if ((pr & (1U << interrupt_line)) != 0U) {
#ifdef DEBUG
puts("got started interrupt\n");
#endif

// jenky debounce
delay(100000);

#ifdef EON
// set power savings mode here if on EON build
int power_save_state = check_started() ? POWER_SAVE_STATUS_DISABLED : POWER_SAVE_STATUS_ENABLED;
set_power_save_state(power_save_state);
// set CDP usb power mode everytime that the car starts to make sure EON is charging
if (check_started()) {
current_board->set_usb_power_mode(USB_POWER_CDP);
}
#endif
}
EXTI->PR = (1U << interrupt_line);
}

// cppcheck-suppress unusedFunction ; used in headers not included in cppcheck
void EXTI0_IRQHandler(void) {
started_interrupt_handler(0);
}

// cppcheck-suppress unusedFunction ; used in headers not included in cppcheck
void EXTI1_IRQHandler(void) {
started_interrupt_handler(1);
}

// cppcheck-suppress unusedFunction ; used in headers not included in cppcheck
void EXTI3_IRQHandler(void) {
started_interrupt_handler(3);
}

// ****************************** safety mode ******************************

// this is the only way to leave silent mode
Expand Down Expand Up @@ -180,6 +141,7 @@ int get_health_pkt(void *dat) {
uint8_t usb_power_mode_pkt;
uint8_t safety_mode_pkt;
uint8_t fault_status_pkt;
uint8_t power_save_enabled_pkt;
} *health = dat;

health->voltage_pkt = adc_get_voltage();
Expand All @@ -198,6 +160,7 @@ int get_health_pkt(void *dat) {
health->usb_power_mode_pkt = usb_power_mode;
health->safety_mode_pkt = (uint8_t)(current_safety_mode);
health->fault_status_pkt = 0U; // TODO: populate this field
health->power_save_enabled_pkt = (uint8_t)(power_save_status == POWER_SAVE_STATUS_ENABLED);

return sizeof(*health);
}
Expand Down Expand Up @@ -549,6 +512,10 @@ int usb_cb_control_msg(USB_Setup_TypeDef *setup, uint8_t *resp, bool hardwired)
case 0xe6:
current_board->set_usb_power_mode(setup->b.wValue.w);
break;
// **** 0xe7: set power save state
case 0xe7:
set_power_save_state(setup->b.wValue.w);
break;
// **** 0xf0: do k-line wValue pulse on uart2 for Acura
case 0xf0:
if (setup->b.wValue.w == 1U) {
Expand Down Expand Up @@ -706,13 +673,24 @@ void TIM1_BRK_TIM9_IRQHandler(void) {
heartbeat_counter += 1U;
}

// check heartbeat counter if we are running EON code. If the heartbeat has been gone for a while, go to SILENT safety mode.
#ifdef EON
// check heartbeat counter if we are running EON code.
// if the heartbeat has been gone for a while, go to SILENT safety mode and enter power save
if (heartbeat_counter >= (check_started() ? EON_HEARTBEAT_IGNITION_CNT_ON : EON_HEARTBEAT_IGNITION_CNT_OFF)) {
puts("EON hasn't sent a heartbeat for 0x"); puth(heartbeat_counter); puts(" seconds. Safety is set to SILENT mode.\n");
if(current_safety_mode != SAFETY_SILENT){
puts("EON hasn't sent a heartbeat for 0x");
puth(heartbeat_counter);
puts(" seconds. Safety is set to SILENT mode.\n");
if (current_safety_mode != SAFETY_SILENT) {
set_safety_mode(SAFETY_SILENT, 0U);
}
if (power_save_status != POWER_SAVE_STATUS_ENABLED) {
set_power_save_state(POWER_SAVE_STATUS_ENABLED);
}
}

// enter CDP mode when car starts to ensure we are charging a turned off EON
if (check_started() && (usb_power_mode != USB_POWER_CDP)) {
current_board->set_usb_power_mode(USB_POWER_CDP);
}
#endif

Expand Down
5 changes: 3 additions & 2 deletions python/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ def call_control_api(self, msg):

def health(self):
dat = self._handle.controlRead(Panda.REQUEST_IN, 0xd2, 0, 0, 28)
a = struct.unpack("IIIIIBBBBBBBB", dat)
a = struct.unpack("IIIIIBBBBBBBBB", dat)
return {
"voltage": a[0],
"current": a[1],
Expand All @@ -361,7 +361,8 @@ def health(self):
"car_harness_status": a[9],
"usb_power_mode": a[10],
"safety_mode": a[11],
"fault_status": a[12]
"fault_status": a[12],
"power_save_enabled": a[13]
}

# ******************* control *******************
Expand Down

0 comments on commit 1192d93

Please sign in to comment.