84 snprintf(thd_name, 10,
"can%d_rx", cas->
if_index);
85 chRegSetThreadName(thd_name);
88 chEvtRegister(&cas->
cand->error_event, &rxe, EVENT_MASK(1));
95 while(!chThdShouldTerminateX()) {
97 eventmask_t evts = chEvtWaitAnyTimeout(ALL_EVENTS, TIME_IMMEDIATE);
99 if (evts & EVENT_MASK(1)) {
100 chEvtGetAndClearFlags(&rxe);
105 msg_t
status = canReceiveTimeout(cas->
cand, CAN_ANY_MAILBOX, &rx_frame, chTimeMS2I(50));
109 if(rx_frame.common.XTD) {
112 id = rx_frame.std.SID;
114 if(rx_frame.common.RTR) {
117 if(rx_frame.common.ESI) {
135 .timestamp = TIME_I2US(chVTGetSystemTimeX())
142 if(rx_frame.common.ESI) {
147 memcpy(pprz_frame.
data, rx_frame.data8, pprz_frame.
len);
233 const uint32_t pclk = STM32_FDCANCLK;
237 static const int MaxBS1 = 16;
238 static const int MaxBS2 = 8;
251 const int max_quanta_per_bit = (cas->
can_baudrate >= 1000000) ? 10 : 17;
252 static const int MaxSamplePointLocation = 900;
267 uint8_t bs1_bs2_sum = max_quanta_per_bit - 1;
268 while ((prescaler_bs % (1 + bs1_bs2_sum)) != 0) {
269 if (bs1_bs2_sum <= 2) {
275 const uint32_t prescaler = prescaler_bs / (1 + bs1_bs2_sum);
276 if ((prescaler < 1U) || (prescaler > 1024U)) {
299 uint8_t bs1 = ((7 * bs1_bs2_sum - 1) + 4) / 8;
300 uint8_t bs2 = bs1_bs2_sum - bs1;
301 uint16_t sample_point_permill = 1000 * (1 + bs1) / (1 + bs1 + bs2);
304 if (sample_point_permill > MaxSamplePointLocation) {
305 bs1 = (7 * bs1_bs2_sum - 1) / 8;
306 bs2 = bs1_bs2_sum - bs1;
307 sample_point_permill = 1000 * (1 + bs1) / (1 + bs1 + bs2);
319 if ((cas->
can_baudrate != (pclk / (prescaler * (1 + bs1 + bs2)))) || (bs1 < 1) || (bs1 > MaxBS1) || (bs2 < 1)
327 cas->
cfg.op_mode = OPMODE_FDCAN;
329 cas->
cfg.op_mode = OPMODE_CAN;
331 cas->
cfg.RXGFC = FDCAN_CONFIG_GFC_ANFE_RX_0 | FDCAN_CONFIG_GFC_ANFS_RX_0;
332 cas->
cfg.NBTP = (0 << FDCAN_NBTP_NSJW_Pos) | ((bs1 - 1) << FDCAN_NBTP_NTSEG1_Pos) | ((
333 bs2 - 1) << FDCAN_NBTP_NTSEG2_Pos) | ((prescaler - 1) << FDCAN_NBTP_NBRP_Pos);
335 cas->
cfg.CCCR = FDCAN_CCCR_FDOE | FDCAN_CCCR_BRSE;
341 cas->
cfg.mcr = CAN_MCR_ABOM | CAN_MCR_AWUM | CAN_MCR_TXFP;
342 cas->
cfg.btr = CAN_BTR_SJW(0) | CAN_BTR_TS1(bs1 - 1) | CAN_BTR_TS2(bs2 - 1) | CAN_BTR_BRP(prescaler - 1);