Skip to content

Commit

Permalink
L-line relay (#166)
Browse files Browse the repository at this point in the history
* Initial version of L-Line Relay

* lline relay fix build, add to health

* Add lline relay to safety

* Lline relay fix build

* Fix tests

* Add lline safety init. Dont fwd with relay closed

* Turn on relay with CAN

* relay hook

* More reliable lline relay

* Longer LLine timeout

* Only turn on wifi if not eon

* Dont disable ESP in early

* Allow CAN to be turned off

 - CAN is turned off via USB.
 - CAN is turned on when either try to transmit or can is received
 - If only transmit is asleep, all messages should send okay
 - If receive is alseep, will miss first message while waking up
 - Sometimes will report error on second message while CAN perif wakes up
 - Saves 130mW!

* Power Saver Mode

 - Gray Panda power consumption 650mw -> 325mW
 - Turns off CAN, GMLAN, LIN, GPS when no activity for 10s
 - No acitvity is no CAN send, CAN Recv, Write to GPS

* Fix power_saving to better turn off can

 - On some cars when the can is turned off, it triggers a wakeup.
 Delaying the automatic wakeup seems to fix this

* Don't save power in pedal

* Fix relay clicking on startup

* Fix duplicate include

* consistent relay setting

* relay_status can be added when needed, as it's started_alt was consumed in other places

* need to skip forwarding only if relay control is claimed

* unneded change

* make lline_relay.h not depending on can.h

* less spaghetti I guess

* less lines

* reset pedal changes

* no unused input

* update version
  • Loading branch information
legonigel authored and rbiasini committed Apr 12, 2019
1 parent 11c4cdc commit f8ab74a
Show file tree
Hide file tree
Showing 21 changed files with 208 additions and 13 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v1.2.0
v1.2.1
20 changes: 12 additions & 8 deletions board/drivers/can.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#define ALL_CAN_BUT_MAIN_SILENT 0xFE
#define ALL_CAN_LIVE 0

#include "lline_relay.h"

int can_live = 0, pending_can_live = 0, can_loopback = 0, can_silent = ALL_CAN_SILENT;

// ********************* instantiate queues *********************
Expand Down Expand Up @@ -453,14 +455,16 @@ void can_rx(uint8_t can_number) {

// forwarding (panda only)
#ifdef PANDA
int bus_fwd_num = can_forwarding[bus_number] != -1 ? can_forwarding[bus_number] : safety_fwd_hook(bus_number, &to_push);
if (bus_fwd_num != -1) {
CAN_FIFOMailBox_TypeDef to_send;
to_send.RIR = to_push.RIR | 1; // TXRQ
to_send.RDTR = to_push.RDTR;
to_send.RDLR = to_push.RDLR;
to_send.RDHR = to_push.RDHR;
can_send(&to_send, bus_fwd_num);
if ((get_lline_status() != 0) || !relay_control) { //Relay engaged or relay isn't controlled, allow fwd
int bus_fwd_num = can_forwarding[bus_number] != -1 ? can_forwarding[bus_number] : safety_fwd_hook(bus_number, &to_push);
if (bus_fwd_num != -1) {
CAN_FIFOMailBox_TypeDef to_send;
to_send.RIR = to_push.RIR | 1; // TXRQ
to_send.RDTR = to_push.RDTR;
to_send.RDLR = to_push.RDLR;
to_send.RDHR = to_push.RDHR;
can_send(&to_send, bus_fwd_num);
}
}
#endif

Expand Down
88 changes: 88 additions & 0 deletions board/drivers/lline_relay.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#ifdef PANDA

int relay_control = 0; // True if relay is controlled through l-line

/* Conrol a relay connected to l-line pin */

// 160us cycles, 1 high, 25 low

volatile int turn_on_relay = 0;
volatile int on_cycles = 25;

//5s timeout
#define LLINE_TIMEOUT_CYCLES 31250
volatile int timeout_cycles = LLINE_TIMEOUT_CYCLES;

void TIM5_IRQHandler(void) {
if (TIM5->SR & TIM_SR_UIF) {
on_cycles--;
timeout_cycles--;
if (timeout_cycles == 0) {
turn_on_relay = 0;
}
if (on_cycles > 0) {
if (turn_on_relay) {
set_gpio_output(GPIOC, 10, 0);
}
}
else {
set_gpio_output(GPIOC, 10, 1);
on_cycles = 25;
}
}
TIM5->ARR = 160-1;
TIM5->SR = 0;
}

void lline_relay_init (void) {
set_lline_output(0);
relay_control = 1;
set_gpio_output(GPIOC, 10, 1);

// setup
TIM5->PSC = 48-1; // tick on 1 us
TIM5->CR1 = TIM_CR1_CEN; // enable
TIM5->ARR = 50-1; // 50 us
TIM5->DIER = TIM_DIER_UIE; // update interrupt
TIM5->CNT = 0;

NVIC_EnableIRQ(TIM5_IRQn);

#ifdef DEBUG
puts("INIT LLINE\n");
puts(" SR ");
putui(TIM5->SR);
puts(" PSC ");
putui(TIM5->PSC);
puts(" CR1 ");
putui(TIM5->CR1);
puts(" ARR ");
putui(TIM5->ARR);
puts(" DIER ");
putui(TIM5->DIER);
puts(" SR ");
putui(TIM5->SR);
puts(" CNT ");
putui(TIM5->CNT);
puts("\n");
#endif
}

void lline_relay_release (void) {
set_lline_output(0);
relay_control = 0;
puts("RELEASE LLINE\n");
set_gpio_alternate(GPIOC, 10, GPIO_AF7_USART3);
NVIC_DisableIRQ(TIM5_IRQn);
}

void set_lline_output(int to_set) {
timeout_cycles = LLINE_TIMEOUT_CYCLES;
turn_on_relay = to_set;
}

int get_lline_status() {
return turn_on_relay;
}

#endif
5 changes: 4 additions & 1 deletion board/gpio.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ void periph_init() {
RCC->APB1ENR |= RCC_APB1ENR_TIM2EN;
RCC->APB1ENR |= RCC_APB1ENR_TIM3EN;
RCC->APB1ENR |= RCC_APB1ENR_TIM4EN;
RCC->APB1ENR |= RCC_APB1ENR_TIM5EN;
RCC->APB1ENR |= RCC_APB1ENR_TIM6EN;
RCC->APB2ENR |= RCC_APB2ENR_USART1EN;
RCC->AHB2ENR |= RCC_AHB2ENR_OTGFSEN;
Expand Down Expand Up @@ -391,7 +392,9 @@ void gpio_init() {
set_gpio_output(GPIOA, 14, 1);

// C10,C11: L-Line setup on USART 3
set_gpio_alternate(GPIOC, 10, GPIO_AF7_USART3);
// LLine now used for relay output
set_gpio_output(GPIOC, 10, 1);
//set_gpio_alternate(GPIOC, 10, GPIO_AF7_USART3);
set_gpio_alternate(GPIOC, 11, GPIO_AF7_USART3);
set_gpio_pullup(GPIOC, 11, PULL_UP);
#endif
Expand Down
18 changes: 18 additions & 0 deletions board/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,14 @@ void usb_cb_ep3_out(uint8_t *usbdata, int len, int hardwired) {

uint8_t bus_number = (to_push.RDTR >> 4) & CAN_BUS_NUM_MASK;
can_send(&to_push, bus_number);

#ifdef PANDA
// Enable relay on can message if allowed.
// Temporary until OP has support for relay
if (safety_relay_hook()) {
set_lline_output(1);
}
#endif
}
}

Expand Down Expand Up @@ -444,6 +452,16 @@ int usb_cb_control_msg(USB_Setup_TypeDef *setup, uint8_t *resp, int hardwired) {
}
break;
}
// **** 0xf3: set l-line relay
case 0xf3:
{
#ifdef PANDA
if (safety_relay_hook()) {
set_lline_output(setup->b.wValue.w == 1);
}
#endif
break;
}
default:
puts("NO HANDLER ");
puth(setup->b.bRequest);
Expand Down
10 changes: 10 additions & 0 deletions board/safety.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ int driver_limit_check(int val, int val_last, struct sample_t *val_driver,
int rt_rate_limit_check(int val, int val_last, const int MAX_RT_DELTA);
#ifdef PANDA
float interpolate(struct lookup_t xy, float x);

void lline_relay_init (void);
void lline_relay_release (void);
void set_lline_output(int to_set);
#endif

typedef void (*safety_hook_init)(int16_t param);
Expand All @@ -37,6 +41,7 @@ typedef int (*tx_hook)(CAN_FIFOMailBox_TypeDef *to_send);
typedef int (*tx_lin_hook)(int lin_num, uint8_t *data, int len);
typedef int (*ign_hook)();
typedef int (*fwd_hook)(int bus_num, CAN_FIFOMailBox_TypeDef *to_fwd);
typedef int (*relay_hook)();

typedef struct {
safety_hook_init init;
Expand All @@ -45,6 +50,7 @@ typedef struct {
tx_hook tx;
tx_lin_hook tx_lin;
fwd_hook fwd;
relay_hook relay;
} safety_hooks;

// This can be set by the safety hooks.
Expand Down Expand Up @@ -91,6 +97,10 @@ int safety_fwd_hook(int bus_num, CAN_FIFOMailBox_TypeDef *to_fwd) {
return current_hooks->fwd(bus_num, to_fwd);
}

int safety_relay_hook(void) {
return current_hooks->relay();
}

typedef struct {
uint16_t id;
const safety_hooks *hooks;
Expand Down
4 changes: 4 additions & 0 deletions board/safety/safety_cadillac.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ static int cadillac_tx_hook(CAN_FIFOMailBox_TypeDef *to_send) {
static void cadillac_init(int16_t param) {
controls_allowed = 0;
cadillac_ign = 0;
#ifdef PANDA
lline_relay_release();
#endif
}

static int cadillac_ign_hook() {
Expand All @@ -128,4 +131,5 @@ const safety_hooks cadillac_hooks = {
.tx_lin = nooutput_tx_lin_hook,
.ignition = cadillac_ign_hook,
.fwd = alloutput_fwd_hook,
.relay = nooutput_relay_hook,
};
4 changes: 4 additions & 0 deletions board/safety/safety_chrysler.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@ static int chrysler_tx_hook(CAN_FIFOMailBox_TypeDef *to_send) {

static void chrysler_init(int16_t param) {
chrysler_camera_detected = 0;
#ifdef PANDA
lline_relay_release();
#endif
}

static int chrysler_fwd_hook(int bus_num, CAN_FIFOMailBox_TypeDef *to_fwd) {
Expand All @@ -150,4 +153,5 @@ const safety_hooks chrysler_hooks = {
.tx_lin = nooutput_tx_lin_hook,
.ignition = default_ign_hook,
.fwd = chrysler_fwd_hook,
.relay = nooutput_relay_hook,
};
16 changes: 16 additions & 0 deletions board/safety/safety_defaults.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ int default_ign_hook() {

static void nooutput_init(int16_t param) {
controls_allowed = 0;
#ifdef PANDA
lline_relay_release();
#endif
}

static int nooutput_tx_hook(CAN_FIFOMailBox_TypeDef *to_send) {
Expand All @@ -22,19 +25,27 @@ static int nooutput_fwd_hook(int bus_num, CAN_FIFOMailBox_TypeDef *to_fwd) {
return -1;
}

static int nooutput_relay_hook(int to_set) {
return false;
}

const safety_hooks nooutput_hooks = {
.init = nooutput_init,
.rx = default_rx_hook,
.tx = nooutput_tx_hook,
.tx_lin = nooutput_tx_lin_hook,
.ignition = default_ign_hook,
.fwd = nooutput_fwd_hook,
.relay = nooutput_relay_hook,
};

// *** all output safety mode ***

static void alloutput_init(int16_t param) {
controls_allowed = 1;
#ifdef PANDA
lline_relay_release();
#endif
}

static int alloutput_tx_hook(CAN_FIFOMailBox_TypeDef *to_send) {
Expand All @@ -49,11 +60,16 @@ static int alloutput_fwd_hook(int bus_num, CAN_FIFOMailBox_TypeDef *to_fwd) {
return -1;
}

static int alloutput_relay_hook(int to_set) {
return true;
}

const safety_hooks alloutput_hooks = {
.init = alloutput_init,
.rx = default_rx_hook,
.tx = alloutput_tx_hook,
.tx_lin = alloutput_tx_lin_hook,
.ignition = default_ign_hook,
.fwd = alloutput_fwd_hook,
.relay = alloutput_relay_hook,
};
1 change: 1 addition & 0 deletions board/safety/safety_elm327.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,5 @@ const safety_hooks elm327_hooks = {
.tx_lin = elm327_tx_lin_hook,
.ignition = default_ign_hook,
.fwd = elm327_fwd_hook,
.relay = nooutput_relay_hook,
};
1 change: 1 addition & 0 deletions board/safety/safety_ford.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,5 @@ const safety_hooks ford_hooks = {
.tx_lin = nooutput_tx_lin_hook,
.ignition = default_ign_hook,
.fwd = nooutput_fwd_hook,
.relay = nooutput_relay_hook,
};
5 changes: 4 additions & 1 deletion board/safety/safety_gm.h
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,9 @@ static int gm_tx_hook(CAN_FIFOMailBox_TypeDef *to_send) {
static void gm_init(int16_t param) {
controls_allowed = 0;
gm_ignition_started = 0;
#ifdef PANDA
lline_relay_release();
#endif
}

static int gm_ign_hook() {
Expand All @@ -241,5 +244,5 @@ const safety_hooks gm_hooks = {
.tx_lin = nooutput_tx_lin_hook,
.ignition = gm_ign_hook,
.fwd = nooutput_fwd_hook,
.relay = nooutput_relay_hook,
};

1 change: 1 addition & 0 deletions board/safety/safety_gm_ascm.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,6 @@ const safety_hooks gm_ascm_hooks = {
.tx_lin = nooutput_tx_lin_hook,
.ignition = default_ign_hook,
.fwd = gm_ascm_fwd_hook,
.relay = nooutput_relay_hook,
};

8 changes: 8 additions & 0 deletions board/safety/safety_honda.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,13 +136,19 @@ static void honda_init(int16_t param) {
controls_allowed = 0;
bosch_hardware = false;
honda_alt_brake_msg = false;
#ifdef PANDA
lline_relay_release();
#endif
}

static void honda_bosch_init(int16_t param) {
controls_allowed = 0;
bosch_hardware = true;
// Checking for alternate brake override from safety parameter
honda_alt_brake_msg = param == 1 ? true : false;
#ifdef PANDA
lline_relay_release();
#endif
}

static int honda_fwd_hook(int bus_num, CAN_FIFOMailBox_TypeDef *to_fwd) {
Expand Down Expand Up @@ -176,6 +182,7 @@ const safety_hooks honda_hooks = {
.tx_lin = nooutput_tx_lin_hook,
.ignition = default_ign_hook,
.fwd = honda_fwd_hook,
.relay = nooutput_relay_hook,
};

const safety_hooks honda_bosch_hooks = {
Expand All @@ -185,4 +192,5 @@ const safety_hooks honda_bosch_hooks = {
.tx_lin = nooutput_tx_lin_hook,
.ignition = default_ign_hook,
.fwd = honda_bosch_fwd_hook,
.relay = nooutput_relay_hook,
};
4 changes: 4 additions & 0 deletions board/safety/safety_hyundai.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,9 @@ static int hyundai_fwd_hook(int bus_num, CAN_FIFOMailBox_TypeDef *to_fwd) {
static void hyundai_init(int16_t param) {
controls_allowed = 0;
hyundai_giraffe_switch_2 = 0;
#ifdef PANDA
lline_relay_release();
#endif
}

const safety_hooks hyundai_hooks = {
Expand All @@ -161,4 +164,5 @@ const safety_hooks hyundai_hooks = {
.tx_lin = nooutput_tx_lin_hook,
.ignition = default_ign_hook,
.fwd = hyundai_fwd_hook,
.relay = nooutput_relay_hook,
};
Loading

0 comments on commit f8ab74a

Please sign in to comment.