From 1192d934a08819a0be00661d5f27f35344c8f479 Mon Sep 17 00:00:00 2001 From: rbiasini Date: Thu, 21 Nov 2019 12:53:00 -0800 Subject: [PATCH] Power saving refactor (#389) * 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 --- board/boards/white.h | 7 ----- board/drivers/harness.h | 31 ++------------------- board/main.c | 62 +++++++++++++---------------------------- python/__init__.py | 5 ++-- 4 files changed, 26 insertions(+), 79 deletions(-) diff --git a/board/boards/white.h b/board/boards/white.h index 7b2f915..6598dae 100644 --- a/board/boards/white.h +++ b/board/boards/white.h @@ -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. diff --git a/board/drivers/harness.h b/board/drivers/harness.h index 17520a4..c996bff 100644 --- a/board/drivers/harness.h +++ b/board/drivers/harness.h @@ -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; @@ -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; @@ -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"); } -} \ No newline at end of file +} diff --git a/board/main.c b/board/main.c index ed305c1..b080fad 100644 --- a/board/main.c +++ b/board/main.c @@ -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 @@ -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(); @@ -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); } @@ -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) { @@ -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 diff --git a/python/__init__.py b/python/__init__.py index bdf748f..ab1b5d9 100644 --- a/python/__init__.py +++ b/python/__init__.py @@ -347,7 +347,7 @@ class Panda(object): 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], @@ -361,7 +361,8 @@ class Panda(object): "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 *******************