diff --git a/board/drivers/spi.h b/board/drivers/spi.h index 970a99869fa0a1..f31f20590d9add 100644 --- a/board/drivers/spi.h +++ b/board/drivers/spi.h @@ -28,8 +28,8 @@ void spi_init() { // setup interrupt on falling edge of SPI enable (on PA4) SYSCFG->EXTICR[2] = SYSCFG_EXTICR2_EXTI4_PA; - EXTI->IMR = (1 << 4); - EXTI->FTSR = (1 << 4); + EXTI->IMR |= (1 << 4); + EXTI->FTSR |= (1 << 4); NVIC_EnableIRQ(EXTI4_IRQn); } @@ -113,7 +113,7 @@ void DMA2_Stream3_IRQHandler(void) { } void EXTI4_IRQHandler(void) { - volatile int pr = EXTI->PR; + volatile int pr = EXTI->PR & (1 << 4); #ifdef DEBUG_SPI puts("exti4\n"); #endif diff --git a/board/gpio.h b/board/gpio.h index fe236dcbb812bc..79997c58387033 100644 --- a/board/gpio.h +++ b/board/gpio.h @@ -92,8 +92,6 @@ void periph_init() { //RCC->APB2ENR |= RCC_APB2ENR_TIM1EN; RCC->APB2ENR |= RCC_APB2ENR_ADC1EN; RCC->APB2ENR |= RCC_APB2ENR_SPI1EN; - - // needed? RCC->APB2ENR |= RCC_APB2ENR_SYSCFGEN; } diff --git a/board/main.c b/board/main.c index 256743c61ee96c..ef3c597606ba07 100644 --- a/board/main.c +++ b/board/main.c @@ -1,4 +1,4 @@ -//#define EON +#define EON #include "config.h" #include "obj/gitversion.h" @@ -7,7 +7,6 @@ #include "libc.h" -#include "safety.h" #include "provision.h" #include "drivers/llcan.h" @@ -18,13 +17,13 @@ #include "drivers/adc.h" #include "drivers/usb.h" #include "drivers/gmlan_alt.h" -#include "drivers/can.h" #include "drivers/spi.h" #include "drivers/timer.h" #include "drivers/clock.h" #include "power_saving.h" - +#include "safety.h" +#include "drivers/can.h" // ********************* serial debugging ********************* @@ -60,12 +59,43 @@ void debug_ring_callback(uart_ring *ring) { } } -// ***************************** USB port ***************************** +// ***************************** started logic ***************************** int is_gpio_started() { + // ignition is on PA1 return (GPIOA->IDR & (1 << 1)) == 0; } +void EXTI1_IRQHandler() { + volatile int pr = EXTI->PR & (1 << 1); + if (pr & (1 << 1)) { + #ifdef DEBUG + puts("got started interrupt\n"); + #endif + + // jenky debounce + delay(100000); + + // set power savings mode here + if (is_gpio_started() == 1) { + power_save_disable(); + } else { + power_save_enable(); + } + EXTI->PR = (1 << 1); + } +} + +void started_interrupt_init() { + SYSCFG->EXTICR[1] = SYSCFG_EXTICR1_EXTI1_PA; + EXTI->IMR |= (1 << 1); + EXTI->RTSR |= (1 << 1); + EXTI->FTSR |= (1 << 1); + NVIC_EnableIRQ(EXTI1_IRQn); +} + +// ***************************** USB port ***************************** + int get_health_pkt(void *dat) { struct __attribute__((packed)) { uint32_t voltage; @@ -525,6 +555,16 @@ int main() { adc_init(); spi_init(); +#ifdef EON + // have to save power + set_esp_mode(ESP_DISABLED); + if (is_gpio_started() == 0) { + power_save_enable(); + } + // interrupt on started line + started_interrupt_init(); +#endif + #ifdef DEBUG puts("DEBUG ENABLED\n"); #endif @@ -533,12 +573,6 @@ int main() { __enable_irq(); -#ifdef EON - // have to save power - power_save_enable(); - set_esp_mode(ESP_DISABLED); -#endif - // if the error interrupt is enabled to quickly when the CAN bus is active // something bad happens and you can't connect to the device over USB delay(10000000); @@ -628,10 +662,13 @@ int main() { // blink the red LED int div_mode = ((usb_power_mode == USB_POWER_DCP) ? 4 : 1); + // TODO: refactor this elsewhere for (int div_mode_loop = 0; div_mode_loop < div_mode; div_mode_loop++) { for (int fade = 0; fade < 1024; fade += 8) { for (int i = 0; i < (128/div_mode); i++) { - set_led(LED_RED, 1); + if (power_save_status == POWER_SAVE_STATUS_DISABLED) { + set_led(LED_RED, 1); + } if (fade < 512) { delay(fade); } else { delay(1024-fade); } set_led(LED_RED, 0); if (fade < 512) { delay(512-fade); } else { delay(fade-512); } @@ -639,19 +676,6 @@ int main() { } } - #ifdef EON - // save power if the car isn't on - if (safety_ignition_hook() == -1) { - if (is_gpio_started() == 1) { - power_save_disable(); - } else { - power_save_enable(); - } - } else { - power_save_disable(); - } - #endif - // turn off the blue LED, turned on by CAN set_led(LED_BLUE, 0); } diff --git a/board/power_saving.h b/board/power_saving.h index 10ec35f5088b69..7743a9b474e367 100644 --- a/board/power_saving.h +++ b/board/power_saving.h @@ -5,6 +5,7 @@ int power_save_status = POWER_SAVE_STATUS_DISABLED; void power_save_enable(void) { if (power_save_status == POWER_SAVE_STATUS_ENABLED) return; + puts("enable power savings\n"); // turn off can set_can_enable(CAN1, 0); @@ -30,6 +31,7 @@ void power_save_enable(void) { void power_save_disable(void) { if (power_save_status == POWER_SAVE_STATUS_DISABLED) return; + puts("disable power savings\n"); // turn on can set_can_enable(CAN1, 1); diff --git a/board/safety.h b/board/safety.h index 722ba3994fe030..52c65bb7397ae2 100644 --- a/board/safety.h +++ b/board/safety.h @@ -133,6 +133,11 @@ int safety_set_mode(uint16_t mode, int16_t param) { if (safety_hook_registry[i].id == mode) { current_hooks = safety_hook_registry[i].hooks; if (current_hooks->init) current_hooks->init(param); + 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(); + } return 0; } }