From ec3d0386c2f6e546cc2e5ee8cc855086e8afdbf6 Mon Sep 17 00:00:00 2001 From: Riccardo Date: Wed, 26 Jun 2019 18:24:21 -0700 Subject: [PATCH] refactor set_power_save_state --- board/drivers/llgpio.h | 4 +-- board/gpio.h | 2 +- board/main.c | 25 ++++++--------- board/power_saving.h | 70 ++++++++++++++++-------------------------- 4 files changed, 40 insertions(+), 61 deletions(-) diff --git a/board/drivers/llgpio.h b/board/drivers/llgpio.h index c9f11b43..172776eb 100644 --- a/board/drivers/llgpio.h +++ b/board/drivers/llgpio.h @@ -14,8 +14,8 @@ void set_gpio_mode(GPIO_TypeDef *GPIO, int pin, int mode) { GPIO->MODER = tmp; } -void set_gpio_output(GPIO_TypeDef *GPIO, int pin, int val) { - if (val != 0) { +void set_gpio_output(GPIO_TypeDef *GPIO, int pin, bool enabled) { + if (enabled) { GPIO->ODR |= (1 << pin); } else { GPIO->ODR &= ~(1 << pin); diff --git a/board/gpio.h b/board/gpio.h index 2894a763..37ff6c3f 100644 --- a/board/gpio.h +++ b/board/gpio.h @@ -97,7 +97,7 @@ void periph_init(void) { // ********************* setters ********************* -void set_can_enable(CAN_TypeDef *CAN, int enabled) { +void set_can_enable(CAN_TypeDef *CAN, bool enabled) { // enable CAN busses if (CAN == CAN1) { #ifdef PANDA diff --git a/board/main.c b/board/main.c index c56577cc..e12f4495 100644 --- a/board/main.c +++ b/board/main.c @@ -64,7 +64,7 @@ void debug_ring_callback(uart_ring *ring) { // ***************************** started logic ***************************** -int is_gpio_started(void) { +bool is_gpio_started(void) { // ignition is on PA1 return (GPIOA->IDR & (1U << 1)) == 0; } @@ -80,11 +80,8 @@ void EXTI1_IRQHandler(void) { delay(100000); // set power savings mode here - if (is_gpio_started() == 1) { - power_save_disable(); - } else { - power_save_enable(); - } + int power_save_state = is_gpio_started() ? POWER_SAVE_STATUS_DISABLED : POWER_SAVE_STATUS_ENABLED; + set_power_save_state(power_save_state); EXTI->PR = (1U << 1); } } @@ -153,12 +150,10 @@ int usb_cb_ep1_in(uint8_t *usbdata, int len, bool hardwired) { // send on serial, first byte to select the ring void usb_cb_ep2_out(uint8_t *usbdata, int len, bool hardwired) { UNUSED(hardwired); - if (len != 0) { - uart_ring *ur = get_ring_by_number(usbdata[0]); - if (ur != NULL) { - if ((usbdata[0] < 2) || safety_tx_lin_hook(usbdata[0]-2, usbdata+1, len-1)) { - for (int i = 1; i < len; i++) while (!putc(ur, usbdata[i])); - } + uart_ring *ur = get_ring_by_number(usbdata[0]); + if ((len != 0) && (ur != NULL)) { + if ((usbdata[0] < 2) || safety_tx_lin_hook(usbdata[0]-2, usbdata+1, len-1)) { + for (int i = 1; i < len; i++) while (!putc(ur, usbdata[i])); } } } @@ -296,7 +291,7 @@ int usb_cb_control_msg(USB_Setup_TypeDef *setup, uint8_t *resp, bool hardwired) if (safety_ignition_hook() != -1) { // if the ignition hook depends on something other than the started GPIO // we have to disable power savings (fix for GM and Tesla) - power_save_disable(); + set_power_save_state(POWER_SAVE_STATUS_DISABLED); } #ifndef EON // always LIVE on EON @@ -678,8 +673,8 @@ int main(void) { set_esp_mode(ESP_DISABLED); } // only enter power save after the first cycle - /*if (is_gpio_started() == 0) { - power_save_enable(); + /*if (is_gpio_started()) { + set_power_save_state(POWER_SAVE_STATUS_ENABLED); }*/ // interrupt on started line started_interrupt_init(); diff --git a/board/power_saving.h b/board/power_saving.h index e1b20768..986adf3c 100644 --- a/board/power_saving.h +++ b/board/power_saving.h @@ -3,57 +3,41 @@ int power_save_status = POWER_SAVE_STATUS_DISABLED; -void power_save_enable(void) { - if (power_save_status != POWER_SAVE_STATUS_ENABLED) { - puts("enable power savings\n"); +void set_power_save_state(int state) { - // turn off can - set_can_enable(CAN1, 0); - set_can_enable(CAN2, 0); - set_can_enable(CAN3, 0); - - // turn off GMLAN - set_gpio_output(GPIOB, 14, 0); - set_gpio_output(GPIOB, 15, 0); - - // turn off LIN - set_gpio_output(GPIOB, 7, 0); - set_gpio_output(GPIOA, 14, 0); - - if (is_grey_panda) { - char UBLOX_SLEEP_MSG[] = "\xb5\x62\x06\x04\x04\x00\x01\x00\x08\x00\x17\x78"; - uart_ring *ur = get_ring_by_number(1); - for (unsigned int i = 0; i < sizeof(UBLOX_SLEEP_MSG)-1; i++) while (!putc(ur, UBLOX_SLEEP_MSG[i])); + bool is_valid_state = (state == POWER_SAVE_STATUS_ENABLED) || (state == POWER_SAVE_STATUS_DISABLED); + if (is_valid_state && (state != power_save_status)) { + bool enable = false; + if (state == POWER_SAVE_STATUS_ENABLED) { + puts("enable power savings\n"); + if (is_grey_panda) { + char UBLOX_SLEEP_MSG[] = "\xb5\x62\x06\x04\x04\x00\x01\x00\x08\x00\x17\x78"; + uart_ring *ur = get_ring_by_number(1); + for (unsigned int i = 0; i < sizeof(UBLOX_SLEEP_MSG) - 1; i++) while (!putc(ur, UBLOX_SLEEP_MSG[i])); + } + } else { + puts("disable power savings\n"); + if (is_grey_panda) { + char UBLOX_WAKE_MSG[] = "\xb5\x62\x06\x04\x04\x00\x01\x00\x09\x00\x18\x7a"; + uart_ring *ur = get_ring_by_number(1); + for (unsigned int i = 0; i < sizeof(UBLOX_WAKE_MSG) - 1; i++) while (!putc(ur, UBLOX_WAKE_MSG[i])); + } + enable = true; } - power_save_status = POWER_SAVE_STATUS_ENABLED; - } -} - -void power_save_disable(void) { - if (power_save_status != POWER_SAVE_STATUS_DISABLED) { - puts("disable power savings\n"); - // turn on can - set_can_enable(CAN1, 1); - set_can_enable(CAN2, 1); - set_can_enable(CAN3, 1); + set_can_enable(CAN1, enable); + set_can_enable(CAN2, enable); + set_can_enable(CAN3, enable); // turn on GMLAN - set_gpio_output(GPIOB, 14, 1); - set_gpio_output(GPIOB, 15, 1); + set_gpio_output(GPIOB, 14, enable); + set_gpio_output(GPIOB, 15, enable); // turn on LIN - set_gpio_output(GPIOB, 7, 1); - set_gpio_output(GPIOA, 14, 1); + set_gpio_output(GPIOB, 7, enable); + set_gpio_output(GPIOA, 14, enable); - if (is_grey_panda) { - char UBLOX_WAKE_MSG[] = "\xb5\x62\x06\x04\x04\x00\x01\x00\x09\x00\x18\x7a"; - uart_ring *ur = get_ring_by_number(1); - for (unsigned int i = 0; i < sizeof(UBLOX_WAKE_MSG)-1; i++) while (!putc(ur, UBLOX_WAKE_MSG[i])); - } - - power_save_status = POWER_SAVE_STATUS_DISABLED; + power_save_status = state; } } -