Skip to content

Commit 2773da5

Browse files
committed
fdcan: Add support for enabling TDC (Transceiver Delay Compensation)
Transceiver Delay Compensation is required for reliable CAN FD data phase operation at high bitrates (5+ Mbps).
1 parent 185cbde commit 2773da5

6 files changed

Lines changed: 35 additions & 10 deletions

File tree

src/platform/stm32/stm32_fdcan.c

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

3+
#include "stm32_fdcan.h"
4+
35
#include <assert.h>
46

57
#include <mios/eventlog.h>
@@ -14,6 +16,8 @@
1416

1517
#include <mios/bytestream.h>
1618

19+
#define FDCAN_TDCR 0x048
20+
1721
#define FDCAN_FLSSA(x) (0x000 + 4 * (x))
1822
#define FDCAN_FLESA 0x070
1923
#define FDCAN_RXFIFO0(x,y) (0x0b0 + 72 * (x) + 4 * (y))
@@ -343,7 +347,8 @@ stm32_fdcan_init(fdcan_t *fc, const char *name,
343347
uint32_t nominal_bitrate, uint32_t data_bitrate,
344348
uint32_t clockfreq,
345349
const struct dsig_filter *dif,
346-
const struct dsig_filter *dof)
350+
const struct dsig_filter *dof,
351+
uint32_t flags)
347352
{
348353
int stdidx = 0;
349354

@@ -396,6 +401,7 @@ stm32_fdcan_init(fdcan_t *fc, const char *name,
396401

397402
can_timing_t nom, data;
398403
error_t err;
404+
399405
err = fdcan_calculate_timings(clockfreq, nominal_bitrate, 75,
400406
512, 256, 128, &nom, "nominal");
401407
if(err)
@@ -406,12 +412,22 @@ stm32_fdcan_init(fdcan_t *fc, const char *name,
406412
if(err)
407413
return err;
408414

409-
reg_wr(fc->reg_base + FDCAN_DBTP,
410-
((data.prescaler - 1) << 16) |
411-
((data.t1 - 1) << 8) |
412-
((data.t2 - 1) << 4) |
413-
((data.t2 - 1) << 0) |
414-
0);
415+
uint32_t dbtp =
416+
((data.prescaler - 1) << 16) |
417+
((data.t1 - 1) << 8) |
418+
((data.t2 - 1) << 4) |
419+
((data.t2 - 1) << 0) |
420+
0;
421+
422+
if(flags & FDCAN_ENABLE_TDC) {
423+
// TDCO: SSP offset in CAN clock cycles = sample point position
424+
const int tdco = (1 + data.t1) * data.prescaler;
425+
reg_wr(fc->reg_base + FDCAN_TDCR, tdco << 8);
426+
427+
dbtp |= (1 << 23); // TDC: Transceiver Delay Compensation;
428+
}
429+
430+
reg_wr(fc->reg_base + FDCAN_DBTP, dbtp);
415431

416432
reg_wr(fc->reg_base + FDCAN_NBTP,
417433
((nom.prescaler - 1) << 16) |

src/platform/stm32/stm32_fdcan.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#pragma once
2+
3+
#define FDCAN_ENABLE_TDC 0x1

src/platform/stm32h7/stm32h7_can.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,8 @@ stm32h7_can_init(int instance, gpio_t can_tx, gpio_t can_rx,
126126
nominal_bitrate, data_bitrate,
127127
clk_get_freq(CLK_FDCAN),
128128
input_filter,
129-
output_filter);
129+
output_filter,
130+
flags);
130131
if(err) {
131132
printf("%s: Failed to initialize\n", name);
132133
return NULL;

src/platform/stm32h7/stm32h7_can.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
struct dsig_filter;
66
struct can_netif;
77

8-
#define STM32H7_CAN_TIM3_TIMESTAMPING 0x1
8+
#include "platform/stm32/stm32_fdcan.h"
9+
10+
#define STM32H7_CAN_TIM3_TIMESTAMPING 0x10000
911

1012
struct can_netif *stm32h7_can_init(int instance, gpio_t can_tx, gpio_t can_rx,
1113
unsigned int nominal_bitrate,

src/platform/t234spe/t234spe_can.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,8 @@ tegra243_spe_can_init(unsigned int nominal_bitrate,
9191
nominal_bitrate, data_bitrate,
9292
50000000,
9393
input_filter,
94-
output_filter);
94+
output_filter,
95+
flags);
9596
if(err) {
9697
printf("%s: Failed to initialize\n", name);
9798
return NULL;

src/platform/t234spe/t234spe_can.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
struct dsig_filter;
66
struct can_netif;
77

8+
#include "platform/stm32/stm32_fdcan.h"
9+
810
struct can_netif *tegra243_spe_can_init(unsigned int nominal_bitrate,
911
unsigned int data_bitrate,
1012
const struct dsig_filter *input_filter,

0 commit comments

Comments
 (0)