33#define ALL_CAN_BUT_MAIN_SILENT 0xFE
44#define ALL_CAN_LIVE 0
55
6+ #include "lline_relay.h"
7+
68int can_live = 0 , pending_can_live = 0 , can_loopback = 0 , can_silent = ALL_CAN_SILENT ;
79
810// ********************* instantiate queues *********************
@@ -23,6 +25,11 @@ can_buffer(tx2_q, 0x100)
2325 can_ring * can_queues [] = {& can_tx1_q , & can_tx2_q };
2426#endif
2527
28+ #ifdef PANDA
29+ // Forward declare
30+ void power_save_reset_timer ();
31+ #endif
32+
2633// ********************* interrupt safe queue *********************
2734
2835int can_pop (can_ring * q , CAN_FIFOMailBox_TypeDef * elem ) {
@@ -213,7 +220,7 @@ void can_init(uint8_t can_number) {
213220 CAN -> FMR &= ~(CAN_FMR_FINIT );
214221
215222 // enable certain CAN interrupts
216- CAN -> IER |= CAN_IER_TMEIE | CAN_IER_FMPIE0 ;
223+ CAN -> IER |= CAN_IER_TMEIE | CAN_IER_FMPIE0 | CAN_IER_WKUIE ;
217224
218225 switch (can_number ) {
219226 case 0 :
@@ -293,7 +300,6 @@ void can_set_gmlan(int bus) {
293300void can_sce (CAN_TypeDef * CAN ) {
294301 enter_critical_section ();
295302
296- can_err_cnt += 1 ;
297303 #ifdef DEBUG
298304 if (CAN == CAN1 ) puts ("CAN1: " );
299305 if (CAN == CAN2 ) puts ("CAN2: " );
@@ -315,23 +321,42 @@ void can_sce(CAN_TypeDef *CAN) {
315321
316322 uint8_t can_number = CAN_NUM_FROM_CANIF (CAN );
317323 uint8_t bus_number = BUS_NUM_FROM_CAN_NUM (can_number );
318- if (can_autobaud_enabled [bus_number ] && (CAN -> ESR & CAN_ESR_LEC )) {
319- can_autobaud_speed_increment (can_number );
320- can_set_speed (can_number );
321- }
322324
323- // clear current send
324- CAN -> TSR |= CAN_TSR_ABRQ0 ;
325- CAN -> MSR &= ~(CAN_MSR_ERRI );
326- CAN -> MSR = CAN -> MSR ;
325+ if (CAN -> MSR & CAN_MSR_WKUI ) {
326+ //Waking from sleep
327+ #ifdef DEBUG
328+ puts ("WAKE\n" );
329+ #endif
330+ set_can_enable (CAN , 1 );
331+ CAN -> MSR &= ~(CAN_MSR_WKUI );
332+ CAN -> MSR = CAN -> MSR ;
333+ #ifdef PANDA
334+ power_save_reset_timer ();
335+ #endif
336+ } else {
337+ can_err_cnt += 1 ;
338+
327339
340+ if (can_autobaud_enabled [bus_number ] && (CAN -> ESR & CAN_ESR_LEC )) {
341+ can_autobaud_speed_increment (can_number );
342+ can_set_speed (can_number );
343+ }
344+
345+ // clear current send
346+ CAN -> TSR |= CAN_TSR_ABRQ0 ;
347+ CAN -> MSR &= ~(CAN_MSR_ERRI );
348+ CAN -> MSR = CAN -> MSR ;
349+ }
328350 exit_critical_section ();
329351}
330352
331353// ***************************** CAN *****************************
332354
333355void process_can (uint8_t can_number ) {
334356 if (can_number == 0xff ) return ;
357+ #ifdef PANDA
358+ power_save_reset_timer ();
359+ #endif
335360
336361 enter_critical_section ();
337362
@@ -375,6 +400,13 @@ void process_can(uint8_t can_number) {
375400 }
376401
377402 if (can_pop (can_queues [bus_number ], & to_send )) {
403+ if (CAN -> MCR & CAN_MCR_SLEEP ) {
404+ set_can_enable (CAN , 1 );
405+ CAN -> MCR &= ~(CAN_MCR_SLEEP );
406+ CAN -> MCR |= CAN_MCR_INRQ ;
407+ while ((CAN -> MSR & CAN_MSR_INAK ) != CAN_MSR_INAK );
408+ CAN -> MCR &= ~(CAN_MCR_INRQ );
409+ }
378410 can_tx_cnt += 1 ;
379411 // only send if we have received a packet
380412 CAN -> sTxMailBox [0 ].TDLR = to_send .RDLR ;
@@ -390,6 +422,9 @@ void process_can(uint8_t can_number) {
390422// CAN receive handlers
391423// blink blue when we are receiving CAN messages
392424void can_rx (uint8_t can_number ) {
425+ #ifdef PANDA
426+ power_save_reset_timer ();
427+ #endif
393428 CAN_TypeDef * CAN = CANIF_FROM_CAN_NUM (can_number );
394429 uint8_t bus_number = BUS_NUM_FROM_CAN_NUM (can_number );
395430 while (CAN -> RF0R & CAN_RF0R_FMP0 ) {
@@ -420,14 +455,16 @@ void can_rx(uint8_t can_number) {
420455
421456 // forwarding (panda only)
422457 #ifdef PANDA
423- int bus_fwd_num = can_forwarding [bus_number ] != -1 ? can_forwarding [bus_number ] : safety_fwd_hook (bus_number , & to_push );
424- if (bus_fwd_num != -1 ) {
425- CAN_FIFOMailBox_TypeDef to_send ;
426- to_send .RIR = to_push .RIR | 1 ; // TXRQ
427- to_send .RDTR = to_push .RDTR ;
428- to_send .RDLR = to_push .RDLR ;
429- to_send .RDHR = to_push .RDHR ;
430- 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+ }
431468 }
432469 #endif
433470
0 commit comments