Skip to content

Commit

Permalink
refactor power savings to depend on car started bit
Browse files Browse the repository at this point in the history
  • Loading branch information
geohot committed May 23, 2019
1 parent 386d5df commit a74f001
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 30 deletions.
6 changes: 3 additions & 3 deletions board/drivers/spi.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions board/gpio.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
74 changes: 49 additions & 25 deletions board/main.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//#define EON
#define EON

#include "config.h"
#include "obj/gitversion.h"
Expand All @@ -7,7 +7,6 @@


#include "libc.h"
#include "safety.h"
#include "provision.h"

#include "drivers/llcan.h"
Expand All @@ -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 *********************

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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);
Expand Down Expand Up @@ -628,30 +662,20 @@ 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); }
}
}
}

#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);
}
Expand Down
2 changes: 2 additions & 0 deletions board/power_saving.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
5 changes: 5 additions & 0 deletions board/safety.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down

0 comments on commit a74f001

Please sign in to comment.