Skip to content

Commit 9ed0456

Browse files
committed
Added support for CAN2 and hardware with dual CAN. Added sensor port voltage change macro. This has no effect on hardware without these things.
1 parent 7d38621 commit 9ed0456

File tree

4 files changed

+87
-1
lines changed

4 files changed

+87
-1
lines changed

comm_can.c

+73
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,27 @@ void comm_can_init(void) {
137137
PAL_STM32_OTYPE_PUSHPULL |
138138
PAL_STM32_OSPEED_MID1);
139139

140+
#ifdef HW_CAN2_DEV
141+
palSetPadMode(HW_CAN2_RX_PORT, HW_CAN2_RX_PIN,
142+
PAL_MODE_ALTERNATE(HW_CAN2_GPIO_AF) |
143+
PAL_STM32_OTYPE_PUSHPULL |
144+
PAL_STM32_OSPEED_MID1);
145+
palSetPadMode(HW_CAN2_TX_PORT, HW_CAN2_TX_PIN,
146+
PAL_MODE_ALTERNATE(HW_CAN2_GPIO_AF) |
147+
PAL_STM32_OTYPE_PUSHPULL |
148+
PAL_STM32_OSPEED_MID1);
149+
150+
canStart(&CAND1, &cancfg);
151+
canStart(&CAND2, &cancfg);
152+
#else
153+
// CAND1 must be running for CAND2 to work
154+
CANDriver *cand = &HW_CAN_DEV;
155+
if (cand == &CAND2) {
156+
canStart(&CAND1, &cancfg);
157+
}
158+
140159
canStart(&HW_CAN_DEV, &cancfg);
160+
#endif
141161

142162
canard_driver_init();
143163

@@ -220,7 +240,18 @@ void comm_can_transmit_eid_replace(uint32_t id, const uint8_t *data, uint8_t len
220240
memcpy(txmsg.data8, data, len);
221241

222242
chMtxLock(&can_mtx);
243+
#ifdef HW_CAN2_DEV
244+
for (int i = 0;i < 10;i++) {
245+
msg_t ok = canTransmit(&HW_CAN_DEV, CAN_ANY_MAILBOX, &txmsg, TIME_IMMEDIATE);
246+
msg_t ok2 = canTransmit(&HW_CAN2_DEV, CAN_ANY_MAILBOX, &txmsg, TIME_IMMEDIATE);
247+
if (ok == MSG_OK || ok2 == MSG_OK) {
248+
break;
249+
}
250+
chThdSleepMicroseconds(500);
251+
}
252+
#else
223253
canTransmit(&HW_CAN_DEV, CAN_ANY_MAILBOX, &txmsg, MS2ST(5));
254+
#endif
224255
chMtxUnlock(&can_mtx);
225256
#else
226257
(void)id;
@@ -252,7 +283,18 @@ void comm_can_transmit_sid(uint32_t id, uint8_t *data, uint8_t len) {
252283
memcpy(txmsg.data8, data, len);
253284

254285
chMtxLock(&can_mtx);
286+
#ifdef HW_CAN2_DEV
287+
for (int i = 0;i < 10;i++) {
288+
msg_t ok = canTransmit(&HW_CAN_DEV, CAN_ANY_MAILBOX, &txmsg, TIME_IMMEDIATE);
289+
msg_t ok2 = canTransmit(&HW_CAN2_DEV, CAN_ANY_MAILBOX, &txmsg, TIME_IMMEDIATE);
290+
if (ok == MSG_OK || ok2 == MSG_OK) {
291+
break;
292+
}
293+
chThdSleepMicroseconds(500);
294+
}
295+
#else
255296
canTransmit(&HW_CAN_DEV, CAN_ANY_MAILBOX, &txmsg, MS2ST(5));
297+
#endif
256298
chMtxUnlock(&can_mtx);
257299
#else
258300
(void)id;
@@ -1073,6 +1115,10 @@ static THD_FUNCTION(cancom_read_thread, arg) {
10731115
CANRxFrame rxmsg;
10741116

10751117
chEvtRegister(&HW_CAN_DEV.rxfull_event, &el, 0);
1118+
#ifdef HW_CAN2_DEV
1119+
event_listener_t el2;
1120+
chEvtRegister(&HW_CAN2_DEV.rxfull_event, &el2, 0);
1121+
#endif
10761122

10771123
while(!chThdShouldTerminateX()) {
10781124
// Feed watchdog
@@ -1096,9 +1142,29 @@ static THD_FUNCTION(cancom_read_thread, arg) {
10961142

10971143
result = canReceive(&HW_CAN_DEV, CAN_ANY_MAILBOX, &rxmsg, TIME_IMMEDIATE);
10981144
}
1145+
1146+
#ifdef HW_CAN2_DEV
1147+
result = canReceive(&HW_CAN2_DEV, CAN_ANY_MAILBOX, &rxmsg, TIME_IMMEDIATE);
1148+
1149+
while (result == MSG_OK) {
1150+
chMtxLock(&can_rx_mtx);
1151+
rx_frames[rx_frame_write++] = rxmsg;
1152+
if (rx_frame_write == RX_FRAMES_SIZE) {
1153+
rx_frame_write = 0;
1154+
}
1155+
chMtxUnlock(&can_rx_mtx);
1156+
1157+
chEvtSignal(process_tp, (eventmask_t) 1);
1158+
1159+
result = canReceive(&HW_CAN2_DEV, CAN_ANY_MAILBOX, &rxmsg, TIME_IMMEDIATE);
1160+
}
1161+
#endif
10991162
}
11001163

11011164
chEvtUnregister(&HW_CAN_DEV.rxfull_event, &el);
1165+
#ifdef HW_CAN2_DEV
1166+
chEvtUnregister(&HW_CAN2_DEV.rxfull_event, &el2);
1167+
#endif
11021168
}
11031169

11041170
static THD_FUNCTION(cancom_process_thread, arg) {
@@ -1823,6 +1889,13 @@ static void set_timing(int brp, int ts1, int ts2) {
18231889
cancfg.btr = CAN_BTR_SJW(3) | CAN_BTR_TS2(ts2) |
18241890
CAN_BTR_TS1(ts1) | CAN_BTR_BRP(brp);
18251891

1892+
#ifdef HW_CAN2_DEV
1893+
canStop(&CAND1);
1894+
canStart(&CAND1, &cancfg);
1895+
canStop(&CAND2);
1896+
canStart(&CAND2, &cancfg);
1897+
#else
18261898
canStop(&HW_CAN_DEV);
18271899
canStart(&HW_CAN_DEV, &cancfg);
1900+
#endif
18281901
}

hwconf/hw.h

+7
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,13 @@
252252
#define CURRENT_FILTER_OFF()
253253
#endif
254254

255+
#ifndef SENSOR_PORT_5V
256+
#define SENSOR_PORT_5V()
257+
#endif
258+
#ifndef SENSOR_PORT_3V3
259+
#define SENSOR_PORT_3V3()
260+
#endif
261+
255262
// VCC net voltage
256263
#ifndef V_REG
257264
#define V_REG 3.3

mc_interface.c

+6
Original file line numberDiff line numberDiff line change
@@ -210,10 +210,12 @@ void mc_interface_init(void) {
210210
// Initialize encoder
211211
switch (motor_now()->m_conf.m_sensor_port_mode) {
212212
case SENSOR_PORT_MODE_ABI:
213+
SENSOR_PORT_3V3();
213214
encoder_init_abi(motor_now()->m_conf.m_encoder_counts);
214215
break;
215216

216217
case SENSOR_PORT_MODE_AS5047_SPI:
218+
SENSOR_PORT_3V3();
217219
encoder_init_as5047p_spi();
218220
break;
219221

@@ -247,6 +249,7 @@ void mc_interface_init(void) {
247249
} break;
248250

249251
default:
252+
SENSOR_PORT_5V();
250253
break;
251254
}
252255

@@ -340,10 +343,12 @@ void mc_interface_set_configuration(mc_configuration *configuration) {
340343
encoder_deinit();
341344
switch (configuration->m_sensor_port_mode) {
342345
case SENSOR_PORT_MODE_ABI:
346+
SENSOR_PORT_3V3();
343347
encoder_init_abi(configuration->m_encoder_counts);
344348
break;
345349

346350
case SENSOR_PORT_MODE_AS5047_SPI:
351+
SENSOR_PORT_3V3();
347352
encoder_init_as5047p_spi();
348353
break;
349354

@@ -378,6 +383,7 @@ void mc_interface_set_configuration(mc_configuration *configuration) {
378383
} break;
379384

380385
default:
386+
SENSOR_PORT_5V();
381387
break;
382388
}
383389
}

mcuconf.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@
121121
* CAN driver system settings.
122122
*/
123123
#define STM32_CAN_USE_CAN1 TRUE
124-
#define STM32_CAN_USE_CAN2 FALSE
124+
#define STM32_CAN_USE_CAN2 TRUE
125125
#define STM32_CAN_CAN1_IRQ_PRIORITY 11
126126
#define STM32_CAN_CAN2_IRQ_PRIORITY 11
127127

0 commit comments

Comments
 (0)