From: Oliver Hartkopp <socketcan@hartkopp.net>
To: linux-can@vger.kernel.org
Cc: Vincent Mailhol <mailhol@kernel.org>
Subject: [canxl v2 12/15] can: calc_bittiming: add can_calc_sample_point_nrz()
Date: Sat, 15 Nov 2025 17:37:37 +0100 [thread overview]
Message-ID: <20251115163740.7875-13-socketcan@hartkopp.net> (raw)
In-Reply-To: <20251115163740.7875-1-socketcan@hartkopp.net>
From: Vincent Mailhol <mailhol@kernel.org>
CAN XL optimal sample point for PWM encoding (when TMS is on) differs
from the NRZ optimal one. There is thus a need to calculate a
different sample point depending whether TMS is on or off.
This is a preparation change: move the sample point calculation from
can_calc_bittiming() into the new can_calc_sample_point_nrz()
function.
In an upcoming change, a function will be added to calculate the
sample point for PWM encoding.
Signed-off-by: Vincent Mailhol <mailhol@kernel.org>
---
drivers/net/can/dev/calc_bittiming.c | 25 +++++++++++++++----------
1 file changed, 15 insertions(+), 10 deletions(-)
diff --git a/drivers/net/can/dev/calc_bittiming.c b/drivers/net/can/dev/calc_bittiming.c
index 222117596704..9b2d0e458518 100644
--- a/drivers/net/can/dev/calc_bittiming.c
+++ b/drivers/net/can/dev/calc_bittiming.c
@@ -8,10 +8,22 @@
#include <linux/units.h>
#include <linux/can/dev.h>
#define CAN_CALC_MAX_ERROR 50 /* in one-tenth of a percent */
+/* CiA recommended sample points for Non Return to Zero encoding. */
+static int can_calc_sample_point_nrz(const struct can_bittiming *bt)
+{
+ if (bt->bitrate > 800 * KILO /* BPS */)
+ return 750;
+
+ if (bt->bitrate > 500 * KILO /* BPS */)
+ return 800;
+
+ return 875;
+}
+
/* Bit-timing calculation derived from:
*
* Code based on LinCAN sources and H8S2638 project
* Copyright 2004-2006 Pavel Pisa - DCE FELK CVUT cz
* Copyright 2005 Stanislav Marek
@@ -76,21 +88,14 @@ int can_calc_bittiming(const struct net_device *dev, struct can_bittiming *bt,
unsigned int best_brp = 0; /* current best value for brp */
unsigned int brp, tsegall, tseg, tseg1 = 0, tseg2 = 0;
u64 v64;
int err;
- /* Use CiA recommended sample points */
- if (bt->sample_point) {
+ if (bt->sample_point)
sample_point = bt->sample_point;
- } else {
- if (bt->bitrate > 800 * KILO /* BPS */)
- sample_point = 750;
- else if (bt->bitrate > 500 * KILO /* BPS */)
- sample_point = 800;
- else
- sample_point = 875;
- }
+ else
+ sample_point = can_calc_sample_point_nrz(bt);
/* tseg even = round down, odd = round up */
for (tseg = (btc->tseg1_max + btc->tseg2_max) * 2 + 1;
tseg >= (btc->tseg1_min + btc->tseg2_min) * 2; tseg--) {
tsegall = CAN_SYNC_SEG + tseg / 2;
--
2.47.3
next prev parent reply other threads:[~2025-11-15 16:38 UTC|newest]
Thread overview: 29+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-11-15 16:37 [canxl v2 00/15] CAN XL support for review (full series) Oliver Hartkopp
2025-11-15 16:37 ` [canxl v2 01/15] can: bittiming: apply NL_SET_ERR_MSG() to can_calc_bittiming() Oliver Hartkopp
2025-11-15 16:37 ` [canxl v2 02/15] can: dev: can_dev_dropped_skb: drop CAN FD skbs if FD is off Oliver Hartkopp
2025-11-15 16:37 ` [canxl v2 03/15] can: netlink: add CAN_CTRLMODE_RESTRICTED Oliver Hartkopp
2025-11-15 16:37 ` [canxl v2 04/15] can: netlink: add initial CAN XL support Oliver Hartkopp
2025-11-15 16:37 ` [canxl v2 05/15] can: netlink: add CAN_CTRLMODE_XL_TMS flag Oliver Hartkopp
2025-11-16 19:31 ` Oliver Hartkopp
2025-11-16 21:54 ` Vincent Mailhol
2025-11-16 22:53 ` Vincent Mailhol
2025-11-17 9:06 ` Oliver Hartkopp
2025-11-17 8:40 ` Oliver Hartkopp
2025-11-15 16:37 ` [canxl v2 06/15] can: dev: can_dev_dropped_skb: drop CC/FD frames in CANXL-only mode Oliver Hartkopp
2025-11-15 16:37 ` [canxl v2 07/15] can: bittiming: add PWM parameters Oliver Hartkopp
2025-11-15 16:37 ` [canxl v2 08/15] can: bittiming: add PWM validation Oliver Hartkopp
2025-11-15 16:37 ` [canxl v2 09/15] can: calc_bittiming: add PWM calculation Oliver Hartkopp
2025-11-15 16:37 ` [canxl v2 10/15] can: netlink: add PWM netlink interface Oliver Hartkopp
2025-11-15 16:37 ` [canxl v2 11/15] can: calc_bittiming: get rid of the incorrect "nominal" word Oliver Hartkopp
2025-11-15 16:37 ` Oliver Hartkopp [this message]
2025-11-15 16:37 ` [canxl v2 13/15] can: calc_bittiming: add can_calc_sample_point_pwm() Oliver Hartkopp
2025-11-16 20:05 ` Oliver Hartkopp
2025-11-16 22:18 ` Vincent Mailhol
2025-11-17 8:59 ` Oliver Hartkopp
2025-11-15 16:37 ` [canxl v2 14/15] can: add dummy_can driver Oliver Hartkopp
2025-11-15 16:37 ` [canxl v2 15/15] can: raw: instantly reject unsupported CAN frames Oliver Hartkopp
2025-11-19 18:17 ` Mainlining of [canxl v2 00/15] CAN XL support for review (full series) Oliver Hartkopp
2025-11-19 21:08 ` Marc Kleine-Budde
2025-11-20 11:27 ` Oliver Hartkopp
2025-11-20 10:34 ` Stéphane Grosjean
2025-11-20 11:21 ` Oliver Hartkopp
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20251115163740.7875-13-socketcan@hartkopp.net \
--to=socketcan@hartkopp.net \
--cc=linux-can@vger.kernel.org \
--cc=mailhol@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).