can: calc_bittiming: add sample point calculation for PWM encoding
Vincent Mailhol
maintainercover can: calc_bittiming: add sample point calculation for PWM encoding
I wrote this series in response to Oliver's feedback in [1]. I am sending this as an RFC based on top of [2] for the moment so that we can discuss this separately. The plan is to merge this to the CAN XL branch once we reach a consensus on this part.
Patch #1 and #2 are refactors, patch #3 introduces a dedicated function for the PWM sample point calculation.
[1] https://lore.kernel.org/linux-can/[email protected]/
[2] [PATCH v2 00/10] can: netlink: add CAN XL
---
Vincent Mailhol (3):
can: calc_bittiming: get rid of the incorrect "nominal" word
can: calc_bittiming: add can_calc_sample_point_nrz()
can: calc_bittiming: add can_calc_sample_point_pwm()
drivers/net/can/dev/calc_bittiming.c | 68 ++++++++++++++++++++++++------------ 1 file changed, 45 insertions(+), 23 deletions(-)
---
base-commit: ffee675aceb9f44b0502a8bec912abb0c4f4af62
change-id: 20251102-pwm_sample_point-8823cb3cd459
prerequisite-change-id: 20241229-canxl-netlink-bc640af10673:v2
prerequisite-patch-id: 6b3294205bd76b38257516c63b7001ab242c9b62
prerequisite-patch-id: 56431d12edcc0f325cf5204bb6868742c462c0ed
prerequisite-patch-id: 1547fd7ea8f1937f0491cfc0996b09890f850991
prerequisite-patch-id: 1dae270b0454352e46b927f71d1b47ff2bf7a49e
prerequisite-patch-id: e4d43de873dfdefc023a0b86e397b37ea2b9e9a3
prerequisite-patch-id: 4f3db477ff411effe70075c59ae6eac04fc65600
prerequisite-patch-id: 148dbfce9d3bb09537087ee93e60bb7819bdadee
prerequisite-patch-id: 7996539e26d449e8db260425c7287b4dce8cdf35
prerequisite-patch-id: 42215044df6a63fff07c7a7d771d7dc375cc8b0e
prerequisite-patch-id: 640ebf8ac8a1d114dcb91e6c05b9414bd09416fc
prerequisite-patch-id: 84ee5e4f937f8e4cd97833d601affea78fe55914
Best regards,
Signature · 1 line
Vincent Mailhol <[email protected]>
Vincent Mailhol
maintainerRFC PATCH 1/3 can: calc_bittiming: get rid of the incorrect "nominal" word
The functions can_update_sample_point() and can_calc_bittiming() are generic and meant to be used for both the nominal and the data bittiming calculation.
However, those functions use terminologies such as "bitrate nominal" or "sample point nominal". This is a leftover from when only Classical CAN was supported and now became incorrect.
Remove or replace any occurrences of the word "nominal" with something more accurate.
--- drivers/net/can/dev/calc_bittiming.c | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-)
drivers/net/can/dev/calc_bittiming.c +14 -16
@@ -24,7 +24,7 @@*/ static int can_update_sample_point (const struct can_bittiming_const *btc, -const unsigned int sample_point_nominal,const unsigned int tseg, +unsigned int sp_origin,unsigned int tseg,unsigned int *tseg1_ptr,unsigned int *tseg2_ptr,unsigned int *sample_point_error_ptr) {
@@ -35,8 +35,7 @@ can_update_sample_point(const struct can_bittiming_const *btc,for (i =0 ; i <=1 ; i++) { tseg2 = tseg +CAN_SYNC_SEG - - (sample_point_nominal * (tseg +CAN_SYNC_SEG )) / -1000 - i; + (sp_origin * (tseg +CAN_SYNC_SEG )) /1000 - i; tseg2 =clamp (tseg2, btc->tseg2_min, btc->tseg2_max); tseg1 = tseg - tseg2;if (tseg1 > btc->tseg1_max) {
@@ -46,9 +45,9 @@ can_update_sample_point(const struct can_bittiming_const *btc, sample_point =1000 * (tseg +CAN_SYNC_SEG - tseg2) / (tseg +CAN_SYNC_SEG ); - sample_point_error =abs (sample_point_nominal - sample_point); + sample_point_error =abs (sp_origin - sample_point); -if (sample_point <= sample_point_nominal && +if (sample_point <= sp_origin && sample_point_error < best_sample_point_error) { best_sample_point = sample_point; best_sample_point_error = sample_point_error;
@@ -68,11 +67,11 @@ int can_calc_bittiming(const struct net_device *dev, struct can_bittiming *bt, {struct can_priv *priv =netdev_priv (dev);unsigned int bitrate;/* current bitrate */ -unsigned int bitrate_error;/* difference between current and nominal value */ +unsigned int bitrate_error;/* difference between current and calculated value */ unsigned int best_bitrate_error =UINT_MAX ; -unsigned int sample_point_error;/* difference between current and nominal value */ +unsigned int sample_point_error;/* difference between current and calculated value */ unsigned int best_sample_point_error =UINT_MAX ; -unsigned int sample_point_nominal;/* nominal sample point */ +unsigned int sample_point;unsigned int best_tseg =0 ;/* current best value for tseg */ unsigned int best_brp =0 ;/* current best value for brp */ unsigned int brp, tsegall, tseg, tseg1 =0 , tseg2 =0 ;
@@ -81,14 +80,14 @@ int can_calc_bittiming(const struct net_device *dev, struct can_bittiming *bt,/* Use CiA recommended sample points */ if (bt->sample_point) { - sample_point_nominal = bt->sample_point; + sample_point = bt->sample_point; }else {if (bt->bitrate >800 *KILO /* BPS */ ) - sample_point_nominal =750 ; + sample_point =750 ;else if (bt->bitrate >500 *KILO /* BPS */ ) - sample_point_nominal =800 ; + sample_point =800 ;else - sample_point_nominal =875 ; + sample_point =875 ; }/* tseg even = round down, odd = round up */
@@ -115,7 +114,7 @@ int can_calc_bittiming(const struct net_device *dev, struct can_bittiming *bt,if (bitrate_error < best_bitrate_error) best_sample_point_error =UINT_MAX ; -can_update_sample_point (btc, sample_point_nominal, tseg /2 , +can_update_sample_point (btc, sample_point, tseg /2 , &tseg1, &tseg2, &sample_point_error);if (sample_point_error >= best_sample_point_error)continue ;
@@ -146,9 +145,8 @@ int can_calc_bittiming(const struct net_device *dev, struct can_bittiming *bt, }/* real sample point */ - bt->sample_point =can_update_sample_point (btc, sample_point_nominal, - best_tseg, &tseg1, &tseg2, -NULL ); + bt->sample_point =can_update_sample_point (btc, sample_point, best_tseg, + &tseg1, &tseg2,NULL ); v64 = (u64 )best_brp *1000 *1000 *1000 ;do_div (v64, priv->clock.freq);
Signature · 1 line
2.51.0
Marc Kleine-Budde
maintainerRe: [PATCH RFC 1/3] can: calc_bittiming: get rid of the incorrect "nominal" word
Quoted 3 lines · Vincent Mailhol
The functions
can_update_sample_point()andcan_calc_bittiming()are generic and meant to be used for both the nominal and the data bittiming calculation.
""There are 2 hard problems in computer science: cache invalidation,
naming things, and off-by-1 errors.""
Here it's naming things. Back in the days, in commit 7da29f97d6c8 ("can:
dev: can-calc-bit-timing(): better sample point calculation"), I wanted to distinguish between the sample point the user requested and the current sample point.
I was thinking about the signal that goes into a control loops, but at university the lecture was in German, so I picked the wrong term. I think "set point" or "reference value" are better terms.
Quoted 6 lines
However, those functions use terminologies such as "bitrate nominal" or "sample point nominal". This is a leftover from when only Classical CAN was supported and now became incorrect.
Remove or replace any occurrences of the word "nominal" with something more accurate.
What about replacing "nominal" with "reference"
Quoted 10 lines
Signed-off-by Vincent Mailhol <[email protected]>--- drivers/net/can/dev/calc_bittiming.c | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-)drivers/net/can/dev/calc_bittiming.c +0
-0
drivers/net/can/dev/calc_bittiming.c line 27
Quoted 5 lines
drivers/net/can/dev/calc_bittiming.c +1
-1@@ -24,7 +24,7 @@*/ static int can_update_sample_point(const struct can_bittiming_const *btc, - const unsigned int sample_point_nominal, const unsigned int tseg, + unsigned int sp_origin, unsigned int tseg,
Please don't remove the "const".
Quoted 26 lines
drivers/net/can/dev/calc_bittiming.c +3
-4unsigned int *tseg1_ptr, unsigned int *tseg2_ptr, unsigned int *sample_point_error_ptr) {@@ -35,8 +35,7 @@ can_update_sample_point(const struct can_bittiming_const *btc,for (i =0 ; i <=1 ; i++) { tseg2 = tseg +CAN_SYNC_SEG - - (sample_point_nominal * (tseg +CAN_SYNC_SEG )) / -1000 - i; + (sp_origin * (tseg +CAN_SYNC_SEG )) /1000 - i; tseg2 =clamp (tseg2, btc->tseg2_min, btc->tseg2_max); tseg1 = tseg - tseg2;if (tseg1 > btc->tseg1_max) {@@ -46,9 +45,9 @@ can_update_sample_point(const struct can_bittiming_const *btc, sample_point =1000 * (tseg +CAN_SYNC_SEG - tseg2) / (tseg +CAN_SYNC_SEG ); - sample_point_error =abs (sample_point_nominal - sample_point); + sample_point_error =abs (sp_origin - sample_point); -if (sample_point <= sample_point_nominal && +if (sample_point <= sp_origin && sample_point_error < best_sample_point_error) { best_sample_point = sample_point; best_sample_point_error = sample_point_error;
drivers/net/can/dev/calc_bittiming.c int can_calc_bittiming(const struct net_device *dev, struct can_bittiming *bt, · line 70
Quoted 5 lines
drivers/net/can/dev/calc_bittiming.c +1
-1@@ -68,11 +67,11 @@ int can_calc_bittiming(const struct net_device *dev, struct can_bittiming *bt, {struct can_priv *priv =netdev_priv (dev);unsigned int bitrate;/* current bitrate */ -unsigned int bitrate_error;/* difference between current and nominal value */ +unsigned int bitrate_error;/* difference between current and calculated value */
What about: "difference between reference and calculated value"
Quoted 39 lines
drivers/net/can/dev/calc_bittiming.c +7
-7unsigned int best_bitrate_error =UINT_MAX ; -unsigned int sample_point_error;/* difference between current and nominal value */ +unsigned int sample_point_error;/* difference between current and calculated value */ unsigned int best_sample_point_error =UINT_MAX ; -unsigned int sample_point_nominal;/* nominal sample point */ +unsigned int sample_point;unsigned int best_tseg =0 ;/* current best value for tseg */ unsigned int best_brp =0 ;/* current best value for brp */ unsigned int brp, tsegall, tseg, tseg1 =0 , tseg2 =0 ;@@ -81,14 +80,14 @@ int can_calc_bittiming(const struct net_device *dev, struct can_bittiming *bt,/* Use CiA recommended sample points */ if (bt->sample_point) { - sample_point_nominal = bt->sample_point; + sample_point = bt->sample_point; }else {if (bt->bitrate >800 *KILO /* BPS */ ) - sample_point_nominal =750 ; + sample_point =750 ;else if (bt->bitrate >500 *KILO /* BPS */ ) - sample_point_nominal =800 ; + sample_point =800 ;else - sample_point_nominal =875 ; + sample_point =875 ; }/* tseg even = round down, odd = round up */ @@ -115,7 +114,7 @@ int can_calc_bittiming(const struct net_device *dev, struct can_bittiming *bt,if (bitrate_error < best_bitrate_error) best_sample_point_error =UINT_MAX ; -can_update_sample_point (btc, sample_point_nominal, tseg /2 , +can_update_sample_point (btc, sample_point, tseg /2 , &tseg1, &tseg2, &sample_point_error);if (sample_point_error >= best_sample_point_error)continue ;@@ -146,9 +145,8 @@ int can_calc_bittiming(const struct net_device *dev, struct can_bittiming *bt, }
drivers/net/can/dev/calc_bittiming.c int can_calc_bittiming(const struct net_device *dev, struct can_bittiming *bt, · line 152
Quoted 10 lines
drivers/net/can/dev/calc_bittiming.c +2
-3/* real sample point */ - bt->sample_point =can_update_sample_point (btc, sample_point_nominal, - best_tseg, &tseg1, &tseg2, -NULL ); + bt->sample_point =can_update_sample_point (btc, sample_point, best_tseg, + &tseg1, &tseg2,NULL ); v64 = (u64 )best_brp *1000 *1000 *1000 ;do_div (v64, priv->clock.freq);
Marc
--
Pengutronix e.K. | Marc Kleine-Budde | Embedded Linux | https://www.pengutronix.de |
Vertretung Nürnberg | Phone: +49-5121-206917-129 |
Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-9 |
Vincent Mailhol
maintainerRe: [PATCH RFC 1/3] can: calc_bittiming: get rid of the incorrect "nominal" word
Quoted 7 lines · Marc Kleine-Budde
Quoted 3 lines · Vincent Mailhol
The functions
can_update_sample_point()andcan_calc_bittiming()are generic and meant to be used for both the nominal and the data bittiming calculation.""There are 2 hard problems in computer science: cache invalidation,
naming things, and off-by-1 errors.""
:D
Quoted 8 lines
Here it's naming things. Back in the days, in commit
7da29f97d6c8("can:
dev: can-calc-bit-timing(): better sample point calculation"), I wanted to distinguish between the sample point the user requested and the current sample point.I was thinking about the signal that goes into a control loops, but at university the lecture was in German, so I picked the wrong term. I think "set point" or "reference value" are better terms.
OK. Thanks for the clarification, now it makes more sense.
Quoted 8 lines
Quoted 6 lines
However, those functions use terminologies such as "bitrate nominal" or "sample point nominal". This is a leftover from when only Classical CAN was supported and now became incorrect.
Remove or replace any occurrences of the word "nominal" with something more accurate.
What about replacing "nominal" with "reference"
Ack. I will also fully rewrite the patch description. The new title will become:
can: calc_bittiming: get rid of the confusing "nominal" wording
Quoted 5 lines
Quoted 5 lines
Signed-off-by Vincent Mailhol <[email protected]>--- drivers/net/can/dev/calc_bittiming.c | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-)
Quoted 12 lines
drivers/net/can/dev/calc_bittiming.c line 27
Quoted 10 lines
drivers/net/can/dev/calc_bittiming.c +1
-1@@ -24,7 +24,7 @@*/ static int can_update_sample_point(const struct can_bittiming_const *btc, - const unsigned int sample_point_nominal, const unsigned int tseg, + unsigned int sp_origin, unsigned int tseg,Please don't remove the "const".
I always considered it silly to tag a scalar parameter as const. Because C passes the function argument by value, it is pretty meaningless. But I guess this change is out of scope. I will restore this in the next version.
Quoted 20 lines
Quoted 20 lines
drivers/net/can/dev/calc_bittiming.c +2
-3unsigned int *tseg1_ptr, unsigned int *tseg2_ptr, unsigned int *sample_point_error_ptr) {@@ -35,8 +35,7 @@ can_update_sample_point(const struct can_bittiming_const *btc,for (i =0 ; i <=1 ; i++) { tseg2 = tseg +CAN_SYNC_SEG - - (sample_point_nominal * (tseg +CAN_SYNC_SEG )) / -1000 - i; + (sp_origin * (tseg +CAN_SYNC_SEG )) /1000 - i; tseg2 =clamp (tseg2, btc->tseg2_min, btc->tseg2_max); tseg1 = tseg - tseg2;if (tseg1 > btc->tseg1_max) {@@ -46,9 +45,9 @@ can_update_sample_point(const struct can_bittiming_const *btc, sample_point =1000 * (tseg +CAN_SYNC_SEG - tseg2) / (tseg +CAN_SYNC_SEG ); - sample_point_error =abs (sample_point_nominal - sample_point); + sample_point_error =abs (sp_origin - sample_point);
Quoted 13 lines
drivers/net/can/dev/calc_bittiming.c int can_calc_bittiming(const struct net_device *dev, struct can_bittiming *bt, · line 70
Quoted 11 lines
drivers/net/can/dev/calc_bittiming.c +2
-2-if (sample_point <= sample_point_nominal && +if (sample_point <= sp_origin && sample_point_error < best_sample_point_error) { best_sample_point = sample_point; best_sample_point_error = sample_point_error;@@ -68,11 +67,11 @@ int can_calc_bittiming(const struct net_device *dev, struct can_bittiming *bt, {struct can_priv *priv =netdev_priv (dev);unsigned int bitrate;/* current bitrate */ -unsigned int bitrate_error;/* difference between current and nominal value */ +unsigned int bitrate_error;/* difference between current and calculated value */ What about: "difference between reference and calculated value"
Ack. Applied to next version.
Yours sincerely,
Vincent Mailhol
Marc Kleine-Budde
maintainerRe: [PATCH RFC 1/3] can: calc_bittiming: get rid of the incorrect "nominal" word
Quoted 17 lines · Vincent Mailhol
Quoted 4 lines
Quoted 4 lines
--- drivers/net/can/dev/calc_bittiming.c | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-)Quoted 12 lines
drivers/net/can/dev/calc_bittiming.c line 27
Quoted 10 lines
drivers/net/can/dev/calc_bittiming.c +1
-1@@ -24,7 +24,7 @@*/ static int can_update_sample_point(const struct can_bittiming_const *btc, - const unsigned int sample_point_nominal, const unsigned int tseg, + unsigned int sp_origin, unsigned int tseg,Please don't remove the "const".
Quoted 3 lines
I always considered it silly to tag a scalar parameter as const. Because C passes the function argument by value, it is pretty meaningless. But I guess this change is out of scope. I will restore this in the next version.
Here they are both the reference values in the right format. I know I don't want to change them. So I've marked them as const, to document this.
regards,
Marc
Signature · 4 lines
Pengutronix e.K. | Marc Kleine-Budde | Embedded Linux | https://www.pengutronix.de | Vertretung Nürnberg | Phone: +49-5121-206917-129 | Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-9 |
Re: [PATCH RFC 1/3] can: calc_bittiming: get rid of the incorrect "nominal" word
In reply to Vincent MailholHello again Vincent,
(Sorry all, I can only slowly work through the list of emails from this weekend.)
Quoted 10 lines
Quoted 7 lines
drivers/net/can/dev/calc_bittiming.c line 27
Quoted 5 lines
static int
can_update_sample_point(const structcan_bittiming_const*btc,(quoted diff) +0
-1- const unsigned int sample_point_nominal, const unsigned inttseg,
drivers/net/can/dev/calc_bittiming.c +1
-0+ unsigned int sp_origin, unsigned int tseg,Please don't remove the "const".
I always considered it silly to tag a scalar parameter as const. Because C
passes the function argument by value, it is pretty meaningless.
The "const" attribute means that the parameter cannot be used on the left side of an assignment in its block. Even if this parameter is not an input/output parameter, without “const” it can be modified within the function, just like a local variable (which it is, since it is theoretically also declared on the stack). Explicitly stating “const” is a strong indicator that the value cannot be modified in the block imho.
Quoted 3 lines
Yours sincerely,
Vincent Mailhol
Best regards,
-- Stéphane
Marc Kleine-Budde
maintainerRe: [PATCH RFC 1/3] can: calc_bittiming: get rid of the incorrect "nominal" word
Quoted 11 lines · Stéphane Grosjean
Quoted 10 lines
Quoted 7 lines
drivers/net/can/dev/calc_bittiming.c line 27
Quoted 5 lines
static int
can_update_sample_point(const structcan_bittiming_const*btc,(quoted diff) +0
-1- const unsigned int sample_point_nominal, const unsigned inttseg,
drivers/net/can/dev/calc_bittiming.c +1
-0+ unsigned int sp_origin, unsigned int tseg,Please don't remove the "const".
I always considered it silly to tag a scalar parameter as const. Because C
passes the function argument by value, it is pretty meaningless.
Quoted 7 lines
The "const" attribute means that the parameter cannot be used on the left side of an assignment in its block. Even if this parameter is not an input/output parameter, without “const” it can be modified within the function, just like a local variable (which it is, since it is theoretically also declared on the stack). Explicitly stating “const” is a strong indicator that the value cannot be modified in the block imho.
...and this is exactly what I want to express, because it's the reference value.
regards,
Marc
Signature · 4 lines
Pengutronix e.K. | Marc Kleine-Budde | Embedded Linux | https://www.pengutronix.de | Vertretung Nürnberg | Phone: +49-5121-206917-129 | Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-9 |
Vincent Mailhol
maintainerRFC PATCH 2/3 can: calc_bittiming: add can_calc_sample_point_nrz()
In reply to Vincent MailholCAN 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.
--- drivers/net/can/dev/calc_bittiming.c | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-)
drivers/net/can/dev/calc_bittiming.c +15 -10
@@ -10,6 +10,18 @@#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
@@ -78,17 +90,10 @@ int can_calc_bittiming(const struct net_device *dev, struct can_bittiming *bt,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;
Signature · 1 line
2.51.0
Vincent Mailhol
maintainerRFC PATCH 3/3 can: calc_bittiming: add can_calc_sample_point_pwm()
In reply to Vincent MailholThe optimum sample point value depends on the bit symmetry. The more asymmetric the bit is, the more the sample point would be located towards the end of the bit. On the contrary, if the transceiver only has a small asymmetry, the optimal sample point would be slightly after the centre of the bit.
For NRZ encoding (used by Classical CAN, CAN FD and CAN XL with TMS off), the optimum sample points values are above 70% as implemented in can_calc_sample_point_nrz().
When TMS is on, CAN XL optimum sample points are near to 50% or 60% [1]. Add can_calc_sample_point_pwm() which returns a sample point which is suitable for PWM encoding. We crafted the formula to make it return the same values as below table (source: table 3 of [1]).
Bit rate (Mbits/s) Sample point
-------------------------------------
2.0 51.3%
5.0 53.1%
8.0 55.0%
10.0 56.3%
12.3 53.8%
13.3 58.3%
14.5 54.5%
16.0 60.0%
17.7 55.6%
20.0 62.5%The calculation simply consists of setting a slightly too high sample point and then letting can_update_sample_point() correct the values.
For now, it is just a formula up our sleeves which matches the empirical observations of [1]. Once CiA recommendations become available, can_calc_sample_point_pwm() should be updated accordingly.
[1] CAN XL system design: Clock tolerances and edge deviations edge deviations
--- drivers/net/can/dev/calc_bittiming.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+)
drivers/net/can/dev/calc_bittiming.c +19 -0
@@ -22,6 +22,21 @@ static int can_calc_sample_point_nrz(const struct can_bittiming *bt)return 875 ; } +/* Sample points for Pulse-Width Modulation encoding. */ +static int can_calc_sample_point_pwm (const struct can_bittiming *bt) +{ +if (bt->bitrate >15 *MEGA /* BPS */ ) +return 625 ; + +if (bt->bitrate >9 *MEGA /* BPS */ ) +return 600 ; + +if (bt->bitrate >4 *MEGA /* BPS */ ) +return 560 ; + +return 520 ; +} +/* Bit-timing calculation derived from: * * Code based on LinCAN sources and H8S2638 project
@@ -92,6 +107,10 @@ int can_calc_bittiming(const struct net_device *dev, struct can_bittiming *bt,if (bt->sample_point) sample_point = bt->sample_point; + +else if (btc == priv->xl.data_bittiming_const && + (priv->ctrlmode &CAN_CTRLMODE_XL_TMS )) + sample_point =can_calc_sample_point_pwm (bt);else sample_point =can_calc_sample_point_nrz (bt);
Signature · 1 line
2.51.0
Quoted 8 lines · Vincent Mailhol
I wrote this series in response to Oliver's feedback in [1]. I am sending this as an RFC based on top of [2] for the moment so that we can discuss this separately. The plan is to merge this to the CAN XL branch once we reach a consensus on this part.
Patch #1 and #2 are refactors, patch #3 introduces a dedicated function for the PWM sample point calculation.
Quoted 6 lines
[1] https://lore.kernel.org/linux-can/[email protected]/
[2] [PATCH v2 00/10] can: netlink: add CAN XL
Signed-off-by Vincent Mailhol <[email protected]>
Quoted 25 lines
---
Vincent Mailhol (3):
can:calc_bittiming: get rid of the incorrect "nominal" word
can:calc_bittiming: addcan_calc_sample_point_nrz()
can:calc_bittiming: addcan_calc_sample_point_pwm()drivers/net/can/dev/calc_bittiming.c | 68 ++++++++++++++++++++++++------------ 1 file changed, 45 insertions(+), 23 deletions(-)---
base-commit:ffee675aceb9f44b0502a8bec912abb0c4f4af62
change-id: 20251102-pwm_sample_point-8823cb3cd459
prerequisite-change-id: 20241229-canxl-netlink-bc640af10673:v2
prerequisite-patch-id:6b3294205bd76b38257516c63b7001ab242c9b62
prerequisite-patch-id:56431d12edcc0f325cf5204bb6868742c462c0ed
prerequisite-patch-id:1547fd7ea8f1937f0491cfc0996b09890f850991
prerequisite-patch-id:1dae270b0454352e46b927f71d1b47ff2bf7a49e
prerequisite-patch-id:e4d43de873dfdefc023a0b86e397b37ea2b9e9a3
prerequisite-patch-id:4f3db477ff411effe70075c59ae6eac04fc65600
prerequisite-patch-id:148dbfce9d3bb09537087ee93e60bb7819bdadee
prerequisite-patch-id:7996539e26d449e8db260425c7287b4dce8cdf35
prerequisite-patch-id:42215044df6a63fff07c7a7d771d7dc375cc8b0e
prerequisite-patch-id:640ebf8ac8a1d114dcb91e6c05b9414bd09416fc
prerequisite-patch-id:84ee5e4f937f8e4cd97833d601affea78fe55914Best regards,
Vincent Mailhol
maintainerQuoted 4 lines · Vincent Mailhol
I wrote this series in response to Oliver's feedback in [1]. I am sending this as an RFC based on top of [2] for the moment so that we can discuss this separately. The plan is to merge this to the CAN XL branch once we reach a consensus on this part.
I changed my mind. I will keep this as a separate series. v2 is ready locally, I will share everything together once the next can-xl series is ready.
Yours sincerely,
Vincent Mailhol
Hi Vincent,
I was not sure when you would show up again and therefore I started to rework the discussed points this morning
- error-signalling / mixed-mode / CANXL-only mode
- restricted mode warning removal
- TMS as a mandatory setting (now default off)
for your convenience and for an inspiration/review.
I'll send the patch set as-is without further documentation.
The PWM sample-point and CAN_RAW changes are missing.
Best regards,
Oliver
Quoted 14 lines · Vincent Mailhol
Quoted 4 lines · Vincent Mailhol
I wrote this series in response to Oliver's feedback in [1]. I am sending this as an RFC based on top of [2] for the moment so that we can discuss this separately. The plan is to merge this to the CAN XL branch once we reach a consensus on this part.
I changed my mind. I will keep this as a separate series. v2 is ready locally, I will share everything together once the next can-xl series is ready.
Yours sincerely,
Vincent Mailhol