Skip to content

Commit f8ab74a

Browse files
legonigelrbiasini
authored andcommitted
L-line relay (#166)
* 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
1 parent 11c4cdc commit f8ab74a

21 files changed

+208
-13
lines changed

VERSION

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
v1.2.0
1+
v1.2.1

board/drivers/can.h

+12-8
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
#define ALL_CAN_BUT_MAIN_SILENT 0xFE
44
#define ALL_CAN_LIVE 0
55

6+
#include "lline_relay.h"
7+
68
int can_live = 0, pending_can_live = 0, can_loopback = 0, can_silent = ALL_CAN_SILENT;
79

810
// ********************* instantiate queues *********************
@@ -453,14 +455,16 @@ void can_rx(uint8_t can_number) {
453455

454456
// forwarding (panda only)
455457
#ifdef PANDA
456-
int bus_fwd_num = can_forwarding[bus_number] != -1 ? can_forwarding[bus_number] : safety_fwd_hook(bus_number, &to_push);
457-
if (bus_fwd_num != -1) {
458-
CAN_FIFOMailBox_TypeDef to_send;
459-
to_send.RIR = to_push.RIR | 1; // TXRQ
460-
to_send.RDTR = to_push.RDTR;
461-
to_send.RDLR = to_push.RDLR;
462-
to_send.RDHR = to_push.RDHR;
463-
can_send(&to_send, bus_fwd_num);
458+
if ((get_lline_status() != 0) || !relay_control) { //Relay engaged or relay isn't controlled, allow fwd
459+
int bus_fwd_num = can_forwarding[bus_number] != -1 ? can_forwarding[bus_number] : safety_fwd_hook(bus_number, &to_push);
460+
if (bus_fwd_num != -1) {
461+
CAN_FIFOMailBox_TypeDef to_send;
462+
to_send.RIR = to_push.RIR | 1; // TXRQ
463+
to_send.RDTR = to_push.RDTR;
464+
to_send.RDLR = to_push.RDLR;
465+
to_send.RDHR = to_push.RDHR;
466+
can_send(&to_send, bus_fwd_num);
467+
}
464468
}
465469
#endif
466470

board/drivers/lline_relay.h

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
#ifdef PANDA
2+
3+
int relay_control = 0; // True if relay is controlled through l-line
4+
5+
/* Conrol a relay connected to l-line pin */
6+
7+
// 160us cycles, 1 high, 25 low
8+
9+
volatile int turn_on_relay = 0;
10+
volatile int on_cycles = 25;
11+
12+
//5s timeout
13+
#define LLINE_TIMEOUT_CYCLES 31250
14+
volatile int timeout_cycles = LLINE_TIMEOUT_CYCLES;
15+
16+
void TIM5_IRQHandler(void) {
17+
if (TIM5->SR & TIM_SR_UIF) {
18+
on_cycles--;
19+
timeout_cycles--;
20+
if (timeout_cycles == 0) {
21+
turn_on_relay = 0;
22+
}
23+
if (on_cycles > 0) {
24+
if (turn_on_relay) {
25+
set_gpio_output(GPIOC, 10, 0);
26+
}
27+
}
28+
else {
29+
set_gpio_output(GPIOC, 10, 1);
30+
on_cycles = 25;
31+
}
32+
}
33+
TIM5->ARR = 160-1;
34+
TIM5->SR = 0;
35+
}
36+
37+
void lline_relay_init (void) {
38+
set_lline_output(0);
39+
relay_control = 1;
40+
set_gpio_output(GPIOC, 10, 1);
41+
42+
// setup
43+
TIM5->PSC = 48-1; // tick on 1 us
44+
TIM5->CR1 = TIM_CR1_CEN; // enable
45+
TIM5->ARR = 50-1; // 50 us
46+
TIM5->DIER = TIM_DIER_UIE; // update interrupt
47+
TIM5->CNT = 0;
48+
49+
NVIC_EnableIRQ(TIM5_IRQn);
50+
51+
#ifdef DEBUG
52+
puts("INIT LLINE\n");
53+
puts(" SR ");
54+
putui(TIM5->SR);
55+
puts(" PSC ");
56+
putui(TIM5->PSC);
57+
puts(" CR1 ");
58+
putui(TIM5->CR1);
59+
puts(" ARR ");
60+
putui(TIM5->ARR);
61+
puts(" DIER ");
62+
putui(TIM5->DIER);
63+
puts(" SR ");
64+
putui(TIM5->SR);
65+
puts(" CNT ");
66+
putui(TIM5->CNT);
67+
puts("\n");
68+
#endif
69+
}
70+
71+
void lline_relay_release (void) {
72+
set_lline_output(0);
73+
relay_control = 0;
74+
puts("RELEASE LLINE\n");
75+
set_gpio_alternate(GPIOC, 10, GPIO_AF7_USART3);
76+
NVIC_DisableIRQ(TIM5_IRQn);
77+
}
78+
79+
void set_lline_output(int to_set) {
80+
timeout_cycles = LLINE_TIMEOUT_CYCLES;
81+
turn_on_relay = to_set;
82+
}
83+
84+
int get_lline_status() {
85+
return turn_on_relay;
86+
}
87+
88+
#endif

board/gpio.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ void periph_init() {
120120
RCC->APB1ENR |= RCC_APB1ENR_TIM2EN;
121121
RCC->APB1ENR |= RCC_APB1ENR_TIM3EN;
122122
RCC->APB1ENR |= RCC_APB1ENR_TIM4EN;
123+
RCC->APB1ENR |= RCC_APB1ENR_TIM5EN;
123124
RCC->APB1ENR |= RCC_APB1ENR_TIM6EN;
124125
RCC->APB2ENR |= RCC_APB2ENR_USART1EN;
125126
RCC->AHB2ENR |= RCC_AHB2ENR_OTGFSEN;
@@ -391,7 +392,9 @@ void gpio_init() {
391392
set_gpio_output(GPIOA, 14, 1);
392393

393394
// C10,C11: L-Line setup on USART 3
394-
set_gpio_alternate(GPIOC, 10, GPIO_AF7_USART3);
395+
// LLine now used for relay output
396+
set_gpio_output(GPIOC, 10, 1);
397+
//set_gpio_alternate(GPIOC, 10, GPIO_AF7_USART3);
395398
set_gpio_alternate(GPIOC, 11, GPIO_AF7_USART3);
396399
set_gpio_pullup(GPIOC, 11, PULL_UP);
397400
#endif

board/main.c

+18
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,14 @@ void usb_cb_ep3_out(uint8_t *usbdata, int len, int hardwired) {
163163

164164
uint8_t bus_number = (to_push.RDTR >> 4) & CAN_BUS_NUM_MASK;
165165
can_send(&to_push, bus_number);
166+
167+
#ifdef PANDA
168+
// Enable relay on can message if allowed.
169+
// Temporary until OP has support for relay
170+
if (safety_relay_hook()) {
171+
set_lline_output(1);
172+
}
173+
#endif
166174
}
167175
}
168176

@@ -444,6 +452,16 @@ int usb_cb_control_msg(USB_Setup_TypeDef *setup, uint8_t *resp, int hardwired) {
444452
}
445453
break;
446454
}
455+
// **** 0xf3: set l-line relay
456+
case 0xf3:
457+
{
458+
#ifdef PANDA
459+
if (safety_relay_hook()) {
460+
set_lline_output(setup->b.wValue.w == 1);
461+
}
462+
#endif
463+
break;
464+
}
447465
default:
448466
puts("NO HANDLER ");
449467
puth(setup->b.bRequest);

board/safety.h

+10
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ int driver_limit_check(int val, int val_last, struct sample_t *val_driver,
2929
int rt_rate_limit_check(int val, int val_last, const int MAX_RT_DELTA);
3030
#ifdef PANDA
3131
float interpolate(struct lookup_t xy, float x);
32+
33+
void lline_relay_init (void);
34+
void lline_relay_release (void);
35+
void set_lline_output(int to_set);
3236
#endif
3337

3438
typedef void (*safety_hook_init)(int16_t param);
@@ -37,6 +41,7 @@ typedef int (*tx_hook)(CAN_FIFOMailBox_TypeDef *to_send);
3741
typedef int (*tx_lin_hook)(int lin_num, uint8_t *data, int len);
3842
typedef int (*ign_hook)();
3943
typedef int (*fwd_hook)(int bus_num, CAN_FIFOMailBox_TypeDef *to_fwd);
44+
typedef int (*relay_hook)();
4045

4146
typedef struct {
4247
safety_hook_init init;
@@ -45,6 +50,7 @@ typedef struct {
4550
tx_hook tx;
4651
tx_lin_hook tx_lin;
4752
fwd_hook fwd;
53+
relay_hook relay;
4854
} safety_hooks;
4955

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

100+
int safety_relay_hook(void) {
101+
return current_hooks->relay();
102+
}
103+
94104
typedef struct {
95105
uint16_t id;
96106
const safety_hooks *hooks;

board/safety/safety_cadillac.h

+4
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,9 @@ static int cadillac_tx_hook(CAN_FIFOMailBox_TypeDef *to_send) {
115115
static void cadillac_init(int16_t param) {
116116
controls_allowed = 0;
117117
cadillac_ign = 0;
118+
#ifdef PANDA
119+
lline_relay_release();
120+
#endif
118121
}
119122

120123
static int cadillac_ign_hook() {
@@ -128,4 +131,5 @@ const safety_hooks cadillac_hooks = {
128131
.tx_lin = nooutput_tx_lin_hook,
129132
.ignition = cadillac_ign_hook,
130133
.fwd = alloutput_fwd_hook,
134+
.relay = nooutput_relay_hook,
131135
};

board/safety/safety_chrysler.h

+4
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,9 @@ static int chrysler_tx_hook(CAN_FIFOMailBox_TypeDef *to_send) {
127127

128128
static void chrysler_init(int16_t param) {
129129
chrysler_camera_detected = 0;
130+
#ifdef PANDA
131+
lline_relay_release();
132+
#endif
130133
}
131134

132135
static int chrysler_fwd_hook(int bus_num, CAN_FIFOMailBox_TypeDef *to_fwd) {
@@ -150,4 +153,5 @@ const safety_hooks chrysler_hooks = {
150153
.tx_lin = nooutput_tx_lin_hook,
151154
.ignition = default_ign_hook,
152155
.fwd = chrysler_fwd_hook,
156+
.relay = nooutput_relay_hook,
153157
};

board/safety/safety_defaults.h

+16
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ int default_ign_hook() {
88

99
static void nooutput_init(int16_t param) {
1010
controls_allowed = 0;
11+
#ifdef PANDA
12+
lline_relay_release();
13+
#endif
1114
}
1215

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

28+
static int nooutput_relay_hook(int to_set) {
29+
return false;
30+
}
31+
2532
const safety_hooks nooutput_hooks = {
2633
.init = nooutput_init,
2734
.rx = default_rx_hook,
2835
.tx = nooutput_tx_hook,
2936
.tx_lin = nooutput_tx_lin_hook,
3037
.ignition = default_ign_hook,
3138
.fwd = nooutput_fwd_hook,
39+
.relay = nooutput_relay_hook,
3240
};
3341

3442
// *** all output safety mode ***
3543

3644
static void alloutput_init(int16_t param) {
3745
controls_allowed = 1;
46+
#ifdef PANDA
47+
lline_relay_release();
48+
#endif
3849
}
3950

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

63+
static int alloutput_relay_hook(int to_set) {
64+
return true;
65+
}
66+
5267
const safety_hooks alloutput_hooks = {
5368
.init = alloutput_init,
5469
.rx = default_rx_hook,
5570
.tx = alloutput_tx_hook,
5671
.tx_lin = alloutput_tx_lin_hook,
5772
.ignition = default_ign_hook,
5873
.fwd = alloutput_fwd_hook,
74+
.relay = alloutput_relay_hook,
5975
};

board/safety/safety_elm327.h

+1
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,5 @@ const safety_hooks elm327_hooks = {
4242
.tx_lin = elm327_tx_lin_hook,
4343
.ignition = default_ign_hook,
4444
.fwd = elm327_fwd_hook,
45+
.relay = nooutput_relay_hook,
4546
};

board/safety/safety_ford.h

+1
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,5 @@ const safety_hooks ford_hooks = {
9090
.tx_lin = nooutput_tx_lin_hook,
9191
.ignition = default_ign_hook,
9292
.fwd = nooutput_fwd_hook,
93+
.relay = nooutput_relay_hook,
9394
};

board/safety/safety_gm.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,9 @@ static int gm_tx_hook(CAN_FIFOMailBox_TypeDef *to_send) {
228228
static void gm_init(int16_t param) {
229229
controls_allowed = 0;
230230
gm_ignition_started = 0;
231+
#ifdef PANDA
232+
lline_relay_release();
233+
#endif
231234
}
232235

233236
static int gm_ign_hook() {
@@ -241,5 +244,5 @@ const safety_hooks gm_hooks = {
241244
.tx_lin = nooutput_tx_lin_hook,
242245
.ignition = gm_ign_hook,
243246
.fwd = nooutput_fwd_hook,
247+
.relay = nooutput_relay_hook,
244248
};
245-

board/safety/safety_gm_ascm.h

+1
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,6 @@ const safety_hooks gm_ascm_hooks = {
4848
.tx_lin = nooutput_tx_lin_hook,
4949
.ignition = default_ign_hook,
5050
.fwd = gm_ascm_fwd_hook,
51+
.relay = nooutput_relay_hook,
5152
};
5253

board/safety/safety_honda.h

+8
Original file line numberDiff line numberDiff line change
@@ -136,13 +136,19 @@ static void honda_init(int16_t param) {
136136
controls_allowed = 0;
137137
bosch_hardware = false;
138138
honda_alt_brake_msg = false;
139+
#ifdef PANDA
140+
lline_relay_release();
141+
#endif
139142
}
140143

141144
static void honda_bosch_init(int16_t param) {
142145
controls_allowed = 0;
143146
bosch_hardware = true;
144147
// Checking for alternate brake override from safety parameter
145148
honda_alt_brake_msg = param == 1 ? true : false;
149+
#ifdef PANDA
150+
lline_relay_release();
151+
#endif
146152
}
147153

148154
static int honda_fwd_hook(int bus_num, CAN_FIFOMailBox_TypeDef *to_fwd) {
@@ -176,6 +182,7 @@ const safety_hooks honda_hooks = {
176182
.tx_lin = nooutput_tx_lin_hook,
177183
.ignition = default_ign_hook,
178184
.fwd = honda_fwd_hook,
185+
.relay = nooutput_relay_hook,
179186
};
180187

181188
const safety_hooks honda_bosch_hooks = {
@@ -185,4 +192,5 @@ const safety_hooks honda_bosch_hooks = {
185192
.tx_lin = nooutput_tx_lin_hook,
186193
.ignition = default_ign_hook,
187194
.fwd = honda_bosch_fwd_hook,
195+
.relay = nooutput_relay_hook,
188196
};

board/safety/safety_hyundai.h

+4
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,9 @@ static int hyundai_fwd_hook(int bus_num, CAN_FIFOMailBox_TypeDef *to_fwd) {
152152
static void hyundai_init(int16_t param) {
153153
controls_allowed = 0;
154154
hyundai_giraffe_switch_2 = 0;
155+
#ifdef PANDA
156+
lline_relay_release();
157+
#endif
155158
}
156159

157160
const safety_hooks hyundai_hooks = {
@@ -161,4 +164,5 @@ const safety_hooks hyundai_hooks = {
161164
.tx_lin = nooutput_tx_lin_hook,
162165
.ignition = default_ign_hook,
163166
.fwd = hyundai_fwd_hook,
167+
.relay = nooutput_relay_hook,
164168
};

0 commit comments

Comments
 (0)