Skip to content

Commit 7973ba4

Browse files
CAN driver cleanup (#2276)
* it's all the same thing * not live * less macro * lil more * bool * lil more
1 parent a7f31bd commit 7973ba4

File tree

14 files changed

+59
-124
lines changed

14 files changed

+59
-124
lines changed

board/can.h

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,24 @@
11
#pragma once
2-
#include "can_declarations.h"
32

4-
static const uint8_t PANDA_CAN_CNT = 3U;
5-
static const uint8_t PANDA_BUS_CNT = 3U;
3+
#define PANDA_CAN_CNT 3U
64

75
static const unsigned char dlc_to_len[] = {0U, 1U, 2U, 3U, 4U, 5U, 6U, 7U, 8U, 12U, 16U, 20U, 24U, 32U, 48U, 64U};
6+
7+
#define CANPACKET_HEAD_SIZE 6U // non-data portion of CANPacket_t
8+
#define CANPACKET_DATA_SIZE_MAX 64U
9+
10+
// bump this when changing the CAN packet
11+
#define CAN_PACKET_VERSION 4
12+
typedef struct {
13+
unsigned char fd : 1;
14+
unsigned char bus : 3;
15+
unsigned char data_len_code : 4; // lookup length with dlc_to_len
16+
unsigned char rejected : 1;
17+
unsigned char returned : 1;
18+
unsigned char extended : 1;
19+
unsigned int addr : 29;
20+
unsigned char checksum;
21+
unsigned char data[CANPACKET_DATA_SIZE_MAX];
22+
} __attribute__((packed, aligned(4))) CANPacket_t;
23+
24+
#define GET_LEN(msg) (dlc_to_len[(msg)->data_len_code])

board/can_declarations.h

Lines changed: 0 additions & 24 deletions
This file was deleted.

board/drivers/can_common.h

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,13 @@ uint32_t safety_rx_invalid = 0;
55
uint32_t tx_buffer_overflow = 0;
66
uint32_t rx_buffer_overflow = 0;
77

8-
can_health_t can_health[CAN_HEALTH_ARRAY_SIZE] = {{0}, {0}, {0}};
8+
can_health_t can_health[PANDA_CAN_CNT] = {{0}, {0}, {0}};
99

1010
// Ignition detected from CAN meessages
1111
bool ignition_can = false;
1212
uint32_t ignition_can_cnt = 0U;
1313

14-
int can_live = 0;
15-
int pending_can_live = 0;
16-
int can_silent = ALL_CAN_SILENT;
14+
bool can_silent = true;
1715
bool can_loopback = false;
1816

1917
// ********************* instantiate queues *********************
@@ -39,7 +37,7 @@ can_buffer(tx3_q, CAN_TX_BUFFER_SIZE)
3937

4038
// FIXME:
4139
// cppcheck-suppress misra-c2012-9.3
42-
can_ring *can_queues[CAN_QUEUES_ARRAY_SIZE] = {&can_tx1_q, &can_tx2_q, &can_tx3_q};
40+
can_ring *can_queues[PANDA_CAN_CNT] = {&can_tx1_q, &can_tx2_q, &can_tx3_q};
4341

4442
// ********************* interrupt safe queue *********************
4543
bool can_pop(can_ring *q, CANPacket_t *elem) {
@@ -130,11 +128,10 @@ void can_clear(can_ring *q) {
130128

131129
// Helpers
132130
// Panda: Bus 0=CAN1 Bus 1=CAN2 Bus 2=CAN3
133-
bus_config_t bus_config[BUS_CONFIG_ARRAY_SIZE] = {
131+
bus_config_t bus_config[PANDA_CAN_CNT] = {
134132
{ .bus_lookup = 0U, .can_num_lookup = 0U, .forwarding_bus = -1, .can_speed = 5000U, .can_data_speed = 20000U, .canfd_auto = false, .canfd_enabled = false, .brs_enabled = false, .canfd_non_iso = false },
135133
{ .bus_lookup = 1U, .can_num_lookup = 1U, .forwarding_bus = -1, .can_speed = 5000U, .can_data_speed = 20000U, .canfd_auto = false, .canfd_enabled = false, .brs_enabled = false, .canfd_non_iso = false },
136134
{ .bus_lookup = 2U, .can_num_lookup = 2U, .forwarding_bus = -1, .can_speed = 5000U, .can_data_speed = 20000U, .canfd_auto = false, .canfd_enabled = false, .brs_enabled = false, .canfd_non_iso = false },
137-
{ .bus_lookup = 0xFFU, .can_num_lookup = 0xFFU, .forwarding_bus = -1, .can_speed = 333U, .can_data_speed = 333U, .canfd_auto = false, .canfd_enabled = false, .brs_enabled = false, .canfd_non_iso = false },
138135
};
139136

140137
void can_init_all(void) {
@@ -158,20 +155,18 @@ void can_set_forwarding(uint8_t from, uint8_t to) {
158155
#endif
159156

160157
void ignition_can_hook(CANPacket_t *msg) {
161-
int bus = GET_BUS(msg);
162-
if (bus == 0) {
163-
int addr = GET_ADDR(msg);
158+
if (msg->bus == 0U) {
164159
int len = GET_LEN(msg);
165160

166161
// GM exception
167-
if ((addr == 0x1F1) && (len == 8)) {
162+
if ((msg->addr == 0x1F1U) && (len == 8)) {
168163
// SystemPowerMode (2=Run, 3=Crank Request)
169164
ignition_can = (msg->data[0] & 0x2U) != 0U;
170165
ignition_can_cnt = 0U;
171166
}
172167

173168
// Rivian R1S/T GEN1 exception
174-
if ((addr == 0x152) && (len == 8)) {
169+
if ((msg->addr == 0x152U) && (len == 8)) {
175170
// 0x152 overlaps with Subaru pre-global which has this bit as the high beam
176171
int counter = msg->data[1] & 0xFU; // max is only 14
177172

@@ -185,7 +180,7 @@ void ignition_can_hook(CANPacket_t *msg) {
185180
}
186181

187182
// Tesla Model 3/Y exception
188-
if ((addr == 0x221) && (len == 8)) {
183+
if ((msg->addr == 0x221U) && (len == 8)) {
189184
// 0x221 overlaps with Rivian which has random data on byte 0
190185
int counter = msg->data[6] >> 4;
191186

@@ -200,7 +195,7 @@ void ignition_can_hook(CANPacket_t *msg) {
200195
}
201196

202197
// Mazda exception
203-
if ((addr == 0x9E) && (len == 8)) {
198+
if ((msg->addr == 0x9EU) && (len == 8)) {
204199
ignition_can = (msg->data[0] >> 5) == 0x6U;
205200
ignition_can_cnt = 0U;
206201
}
@@ -234,7 +229,7 @@ bool can_check_checksum(CANPacket_t *packet) {
234229

235230
void can_send(CANPacket_t *to_push, uint8_t bus_number, bool skip_tx_hook) {
236231
if (skip_tx_hook || safety_tx_hook(to_push) != 0) {
237-
if (bus_number < PANDA_BUS_CNT) {
232+
if (bus_number < PANDA_CAN_CNT) {
238233
// add CAN packet to send queue
239234
tx_buffer_overflow += can_push(can_queues[bus_number], to_push) ? 0U : 1U;
240235
process_can(CAN_NUM_FROM_BUS_NUM(bus_number));

board/drivers/can_common_declarations.h

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#pragma once
22

3+
#include "board/can.h"
4+
35
typedef struct {
46
volatile uint32_t w_ptr;
57
volatile uint32_t r_ptr;
@@ -24,28 +26,21 @@ extern uint32_t safety_rx_invalid;
2426
extern uint32_t tx_buffer_overflow;
2527
extern uint32_t rx_buffer_overflow;
2628

27-
#define CAN_HEALTH_ARRAY_SIZE 3
28-
extern can_health_t can_health[CAN_HEALTH_ARRAY_SIZE];
29+
extern can_health_t can_health[PANDA_CAN_CNT];
2930

3031
// Ignition detected from CAN meessages
3132
extern bool ignition_can;
3233
extern uint32_t ignition_can_cnt;
3334

34-
#define ALL_CAN_SILENT 0xFF
35-
#define ALL_CAN_LIVE 0
36-
37-
extern int can_live;
38-
extern int pending_can_live;
39-
extern int can_silent;
35+
extern bool can_silent;
4036
extern bool can_loopback;
4137

4238
// ******************* functions prototypes *********************
4339
bool can_init(uint8_t can_number);
4440
void process_can(uint8_t can_number);
4541

4642
// ********************* instantiate queues *********************
47-
#define CAN_QUEUES_ARRAY_SIZE 3
48-
extern can_ring *can_queues[CAN_QUEUES_ARRAY_SIZE];
43+
extern can_ring *can_queues[PANDA_CAN_CNT];
4944

5045
// helpers
5146
#define WORD_TO_BYTE_ARRAY(dst8, src32) 0[dst8] = ((src32) & 0xFFU); 1[dst8] = (((src32) >> 8U) & 0xFFU); 2[dst8] = (((src32) >> 16U) & 0xFFU); 3[dst8] = (((src32) >> 24U) & 0xFFU)
@@ -55,20 +50,7 @@ extern can_ring *can_queues[CAN_QUEUES_ARRAY_SIZE];
5550
bool can_pop(can_ring *q, CANPacket_t *elem);
5651
bool can_push(can_ring *q, const CANPacket_t *elem);
5752
uint32_t can_slots_empty(const can_ring *q);
58-
59-
// assign CAN numbering
60-
// bus num: CAN Bus numbers in panda, sent to/from USB
61-
// Min: 0; Max: 127; Bit 7 marks message as receipt (bus 129 is receipt for but 1)
62-
// cans: Look up MCU can interface from bus number
63-
// can number: numeric lookup for MCU CAN interfaces (0 = CAN1, 1 = CAN2, etc);
64-
// bus_lookup: Translates from 'can number' to 'bus number'.
65-
// can_num_lookup: Translates from 'bus number' to 'can number'.
66-
// forwarding bus: If >= 0, forward all messages from this bus to the specified bus.
67-
68-
// Helpers
69-
// Panda: Bus 0=CAN1 Bus 1=CAN2 Bus 2=CAN3
70-
#define BUS_CONFIG_ARRAY_SIZE 4
71-
extern bus_config_t bus_config[BUS_CONFIG_ARRAY_SIZE];
53+
extern bus_config_t bus_config[PANDA_CAN_CNT];
7254

7355
#define CANIF_FROM_CAN_NUM(num) (cans[num])
7456
#define BUS_NUM_FROM_CAN_NUM(num) (bus_config[num].bus_lookup)

board/drivers/fdcan.h

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include "fdcan_declarations.h"
22

3-
FDCAN_GlobalTypeDef *cans[CANS_ARRAY_SIZE] = {FDCAN1, FDCAN2, FDCAN3};
3+
FDCAN_GlobalTypeDef *cans[PANDA_CAN_CNT] = {FDCAN1, FDCAN2, FDCAN3};
44

55
static bool can_set_speed(uint8_t can_number) {
66
bool ret = true;
@@ -13,7 +13,7 @@ static bool can_set_speed(uint8_t can_number) {
1313
bus_config[bus_number].can_data_speed,
1414
bus_config[bus_number].canfd_non_iso,
1515
can_loopback,
16-
(unsigned int)(can_silent) & (1U << can_number)
16+
can_silent
1717
);
1818
return ret;
1919
}
@@ -32,7 +32,7 @@ void can_clear_send(FDCAN_GlobalTypeDef *FDCANx, uint8_t can_number) {
3232
}
3333

3434
void update_can_health_pkt(uint8_t can_number, uint32_t ir_reg) {
35-
uint8_t can_irq_number[3][2] = {
35+
uint8_t can_irq_number[PANDA_CAN_CNT][2] = {
3636
{ FDCAN1_IT0_IRQn, FDCAN1_IT1_IRQn },
3737
{ FDCAN2_IT0_IRQn, FDCAN2_IT1_IRQn },
3838
{ FDCAN3_IT0_IRQn, FDCAN3_IT1_IRQn },
@@ -63,7 +63,6 @@ void update_can_health_pkt(uint8_t can_number, uint32_t ir_reg) {
6363
can_health[can_number].irq0_call_rate = interrupts[can_irq_number[can_number][0]].call_rate;
6464
can_health[can_number].irq1_call_rate = interrupts[can_irq_number[can_number][1]].call_rate;
6565

66-
6766
if (ir_reg != 0U) {
6867
// Clear error interrupts
6968
FDCANx->IR |= (FDCAN_IR_PED | FDCAN_IR_PEA | FDCAN_IR_EP | FDCAN_IR_BO | FDCAN_IR_RF0L);
@@ -158,17 +157,13 @@ void can_rx(uint8_t can_number) {
158157

159158
// Clear all new messages from Rx FIFO 0
160159
FDCANx->IR |= FDCAN_IR_RF0N;
161-
while((FDCANx->RXF0S & FDCAN_RXF0S_F0FL) != 0U) {
160+
while ((FDCANx->RXF0S & FDCAN_RXF0S_F0FL) != 0U) {
162161
can_health[can_number].total_rx_cnt += 1U;
163-
164-
// can is live
165-
pending_can_live = 1;
166-
167162
// get the index of the next RX FIFO element (0 to FDCAN_RX_FIFO_0_EL_CNT - 1)
168163
uint32_t rx_fifo_idx = (uint8_t)((FDCANx->RXF0S >> FDCAN_RXF0S_F0GI_Pos) & 0x3FU);
169164

170165
// Recommended to offset get index by at least +1 if RX FIFO is in overwrite mode and full (datasheet)
171-
if((FDCANx->RXF0S & FDCAN_RXF0S_F0F) == FDCAN_RXF0S_F0F) {
166+
if ((FDCANx->RXF0S & FDCAN_RXF0S_F0F) == FDCAN_RXF0S_F0F) {
172167
rx_fifo_idx = ((rx_fifo_idx + 1U) >= FDCAN_RX_FIFO_0_EL_CNT) ? 0U : (rx_fifo_idx + 1U);
173168
can_health[can_number].total_rx_lost_cnt += 1U; // At least one message was lost
174169
}

board/drivers/fdcan_declarations.h

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,19 @@
11
#pragma once
22

3-
// IRQs: FDCAN1_IT0, FDCAN1_IT1
4-
// FDCAN2_IT0, FDCAN2_IT1
5-
// FDCAN3_IT0, FDCAN3_IT1
3+
#include "board/can.h"
64

75
typedef struct {
86
volatile uint32_t header[2];
97
volatile uint32_t data_word[CANPACKET_DATA_SIZE_MAX/4U];
108
} canfd_fifo;
119

12-
#define CANS_ARRAY_SIZE 3
13-
extern FDCAN_GlobalTypeDef *cans[CANS_ARRAY_SIZE];
10+
extern FDCAN_GlobalTypeDef *cans[PANDA_CAN_CNT];
1411

1512
#define CAN_ACK_ERROR 3U
1613

1714
void can_clear_send(FDCAN_GlobalTypeDef *FDCANx, uint8_t can_number);
1815
void update_can_health_pkt(uint8_t can_number, uint32_t ir_reg);
1916

20-
// ***************************** CAN *****************************
21-
// FDFDCANx_IT1 IRQ Handler (TX)
2217
void process_can(uint8_t can_number);
23-
// FDFDCANx_IT0 IRQ Handler (RX and errors)
24-
// blink blue when we are receiving CAN messages
2518
void can_rx(uint8_t can_number);
2619
bool can_init(uint8_t can_number);

board/faults_declarations.h

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,10 @@
1111
#define FAULT_INTERRUPT_RATE_CAN_2 (1UL << 3)
1212
#define FAULT_INTERRUPT_RATE_CAN_3 (1UL << 4)
1313
#define FAULT_INTERRUPT_RATE_TACH (1UL << 5)
14-
#define FAULT_INTERRUPT_RATE_GMLAN (1UL << 6) // deprecated
1514
#define FAULT_INTERRUPT_RATE_INTERRUPTS (1UL << 7)
1615
#define FAULT_INTERRUPT_RATE_SPI_DMA (1UL << 8)
17-
#define FAULT_INTERRUPT_RATE_SPI_CS (1UL << 9)
18-
#define FAULT_INTERRUPT_RATE_UART_1 (1UL << 10)
19-
#define FAULT_INTERRUPT_RATE_UART_2 (1UL << 11)
20-
#define FAULT_INTERRUPT_RATE_UART_3 (1UL << 12)
21-
#define FAULT_INTERRUPT_RATE_UART_5 (1UL << 13)
22-
#define FAULT_INTERRUPT_RATE_UART_DMA (1UL << 14)
2316
#define FAULT_INTERRUPT_RATE_USB (1UL << 15)
24-
#define FAULT_INTERRUPT_RATE_TIM1 (1UL << 16)
25-
#define FAULT_INTERRUPT_RATE_TIM3 (1UL << 17)
2617
#define FAULT_REGISTER_DIVERGENT (1UL << 18)
27-
#define FAULT_INTERRUPT_RATE_KLINE_INIT (1UL << 19)
2818
#define FAULT_INTERRUPT_RATE_CLOCK_SOURCE (1UL << 20)
2919
#define FAULT_INTERRUPT_RATE_TICK (1UL << 21)
3020
#define FAULT_INTERRUPT_RATE_EXTI (1UL << 22)

board/jungle/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ int main(void) {
175175
print("**** INTERRUPTS ON ****\n");
176176
enable_interrupts();
177177

178-
can_silent = ALL_CAN_LIVE;
178+
can_silent = false;
179179
set_safety_hooks(SAFETY_ALLOUTPUT, 0U);
180180

181181
can_init_all();

board/jungle/main_comms.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ int comms_control_handler(ControlPacket_t *req, uint8_t *resp) {
189189
break;
190190
// **** 0xde: set can bitrate
191191
case 0xde:
192-
if ((req->param1 < PANDA_BUS_CNT) && is_speed_valid(req->param2, speeds, sizeof(speeds)/sizeof(speeds[0]))) {
192+
if ((req->param1 < PANDA_CAN_CNT) && is_speed_valid(req->param2, speeds, sizeof(speeds)/sizeof(speeds[0]))) {
193193
bus_config[req->param1].can_speed = req->param2;
194194
bool ret = can_init(CAN_NUM_FROM_BUS_NUM(req->param1));
195195
UNUSED(ret);
@@ -212,7 +212,7 @@ int comms_control_handler(ControlPacket_t *req, uint8_t *resp) {
212212
if (req->param1 == 0xFFFFU) {
213213
print("Clearing CAN Rx queue\n");
214214
can_clear(&can_rx_q);
215-
} else if (req->param1 < PANDA_BUS_CNT) {
215+
} else if (req->param1 < PANDA_CAN_CNT) {
216216
print("Clearing CAN Tx queue\n");
217217
can_clear(can_queues[req->param1]);
218218
} else {
@@ -225,7 +225,7 @@ int comms_control_handler(ControlPacket_t *req, uint8_t *resp) {
225225
break;
226226
// **** 0xf5: Set CAN silent mode
227227
case 0xf5:
228-
can_silent = (req->param1 > 0U) ? ALL_CAN_SILENT : ALL_CAN_LIVE;
228+
can_silent = (req->param1 > 0U);
229229
can_init_all();
230230
break;
231231
// **** 0xf7: enable/disable header pin by number

board/main.c

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,12 @@ void set_safety_mode(uint16_t mode, uint16_t param) {
5555
case SAFETY_SILENT:
5656
set_intercept_relay(false, false);
5757
current_board->set_can_mode(CAN_MODE_NORMAL);
58-
can_silent = ALL_CAN_SILENT;
58+
can_silent = true;
5959
break;
6060
case SAFETY_NOOUTPUT:
6161
set_intercept_relay(false, false);
6262
current_board->set_can_mode(CAN_MODE_NORMAL);
63-
can_silent = ALL_CAN_LIVE;
63+
can_silent = false;
6464
break;
6565
case SAFETY_ELM327:
6666
set_intercept_relay(false, false);
@@ -75,14 +75,14 @@ void set_safety_mode(uint16_t mode, uint16_t param) {
7575
} else {
7676
current_board->set_can_mode(CAN_MODE_NORMAL);
7777
}
78-
can_silent = ALL_CAN_LIVE;
78+
can_silent = false;
7979
break;
8080
default:
8181
set_intercept_relay(true, false);
8282
heartbeat_counter = 0U;
8383
heartbeat_lost = false;
8484
current_board->set_can_mode(CAN_MODE_NORMAL);
85-
can_silent = ALL_CAN_LIVE;
85+
can_silent = false;
8686
break;
8787
}
8888
can_init_all();
@@ -143,14 +143,7 @@ static void tick_handler(void) {
143143

144144
// decimated to 1Hz
145145
if (loop_counter == 0U) {
146-
can_live = pending_can_live;
147-
148146
//puth(usart1_dma); print(" "); puth(DMA2_Stream5->M0AR); print(" "); puth(DMA2_Stream5->NDTR); print("\n");
149-
150-
// reset this every 16th pass
151-
if ((uptime_cnt & 0xFU) == 0U) {
152-
pending_can_live = 0;
153-
}
154147
#ifdef DEBUG
155148
print("** blink ");
156149
print("rx:"); puth4(can_rx_q.r_ptr); print("-"); puth4(can_rx_q.w_ptr); print(" ");

0 commit comments

Comments
 (0)