Skip to content

Commit 1c47fa6

Browse files
vincent-mailholmarckleinebudde
authored andcommitted
can: dev: add a helper function to calculate the duration of one bit
Rename macro CAN_CALC_SYNC_SEG to CAN_SYNC_SEG and make it available through include/linux/can/dev.h Add an helper function can_bit_time() which returns the duration (in time quanta) of one CAN bit. Rationale for this patch: the sync segment and the bit time are two concepts which are defined in the CAN ISO standard. Device drivers for CAN might need those. Please refer to ISO 11898-1:2015, section 11.3.1.1 "Bit time" for additional information. Signed-off-by: Vincent Mailhol <[email protected]> Link: https://lore.kernel.org/r/[email protected] [mkl: Let can_bit_time() return an unsinged int, make argument const] Signed-off-by: Marc Kleine-Budde <[email protected]>
1 parent f55a52b commit 1c47fa6

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed

drivers/net/can/dev.c

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ EXPORT_SYMBOL_GPL(can_len2dlc);
6060

6161
#ifdef CONFIG_CAN_CALC_BITTIMING
6262
#define CAN_CALC_MAX_ERROR 50 /* in one-tenth of a percent */
63-
#define CAN_CALC_SYNC_SEG 1
6463

6564
/* Bit-timing calculation derived from:
6665
*
@@ -86,8 +85,8 @@ can_update_sample_point(const struct can_bittiming_const *btc,
8685
int i;
8786

8887
for (i = 0; i <= 1; i++) {
89-
tseg2 = tseg + CAN_CALC_SYNC_SEG -
90-
(sample_point_nominal * (tseg + CAN_CALC_SYNC_SEG)) /
88+
tseg2 = tseg + CAN_SYNC_SEG -
89+
(sample_point_nominal * (tseg + CAN_SYNC_SEG)) /
9190
1000 - i;
9291
tseg2 = clamp(tseg2, btc->tseg2_min, btc->tseg2_max);
9392
tseg1 = tseg - tseg2;
@@ -96,8 +95,8 @@ can_update_sample_point(const struct can_bittiming_const *btc,
9695
tseg2 = tseg - tseg1;
9796
}
9897

99-
sample_point = 1000 * (tseg + CAN_CALC_SYNC_SEG - tseg2) /
100-
(tseg + CAN_CALC_SYNC_SEG);
98+
sample_point = 1000 * (tseg + CAN_SYNC_SEG - tseg2) /
99+
(tseg + CAN_SYNC_SEG);
101100
sample_point_error = abs(sample_point_nominal - sample_point);
102101

103102
if (sample_point <= sample_point_nominal &&
@@ -145,7 +144,7 @@ static int can_calc_bittiming(struct net_device *dev, struct can_bittiming *bt,
145144
/* tseg even = round down, odd = round up */
146145
for (tseg = (btc->tseg1_max + btc->tseg2_max) * 2 + 1;
147146
tseg >= (btc->tseg1_min + btc->tseg2_min) * 2; tseg--) {
148-
tsegall = CAN_CALC_SYNC_SEG + tseg / 2;
147+
tsegall = CAN_SYNC_SEG + tseg / 2;
149148

150149
/* Compute all possible tseg choices (tseg=tseg1+tseg2) */
151150
brp = priv->clock.freq / (tsegall * bt->bitrate) + tseg % 2;
@@ -223,7 +222,7 @@ static int can_calc_bittiming(struct net_device *dev, struct can_bittiming *bt,
223222

224223
/* real bitrate */
225224
bt->bitrate = priv->clock.freq /
226-
(bt->brp * (CAN_CALC_SYNC_SEG + tseg1 + tseg2));
225+
(bt->brp * (CAN_SYNC_SEG + tseg1 + tseg2));
227226

228227
return 0;
229228
}

include/linux/can/dev.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,21 @@ struct can_priv {
8282
#endif
8383
};
8484

85+
#define CAN_SYNC_SEG 1
86+
87+
/*
88+
* can_bit_time() - Duration of one bit
89+
*
90+
* Please refer to ISO 11898-1:2015, section 11.3.1.1 "Bit time" for
91+
* additional information.
92+
*
93+
* Return: the number of time quanta in one bit.
94+
*/
95+
static inline unsigned int can_bit_time(const struct can_bittiming *bt)
96+
{
97+
return CAN_SYNC_SEG + bt->prop_seg + bt->phase_seg1 + bt->phase_seg2;
98+
}
99+
85100
/*
86101
* get_can_dlc(value) - helper macro to cast a given data length code (dlc)
87102
* to u8 and ensure the dlc value to be max. 8 bytes.

0 commit comments

Comments
 (0)