Commit 7da29f97 authored by Marc Kleine-Budde's avatar Marc Kleine-Budde

can: dev: can-calc-bit-timing(): better sample point calculation

This patch optimizes the calculation of the sample point. To understand what it
does have a look at the original implementation.

If there is a combination of timing parameters where both the bitrate and
sample point error are 0 the current implementation will find it.

However if the reference clock doesn't allow an optimal bitrate (this means the
bitrate error is always != 0) there might be several timing parameter
combinations having the same bitrate error. The original implementation will
allways choose the one with the highest brp. The actual sample point error
isn't taken into account.

This patch changes the algorithm to minimize the sample point error, too. Now a
brp/tseg combination is accepted as better if one of these condition are
fulfilled:
1) the bit rate error must be smaller, or
2) the bit rate error must be equal and
   the sample point error must be equal or smaller

If a smaller bit rate error is found the sample point error is reset. This
ensures that we first optimize for small bit rate error and then for small
sample point errors.
Signed-off-by: default avatarMarc Kleine-Budde <mkl@pengutronix.de>
parent a20fadf8
...@@ -69,6 +69,7 @@ EXPORT_SYMBOL_GPL(can_len2dlc); ...@@ -69,6 +69,7 @@ EXPORT_SYMBOL_GPL(can_len2dlc);
#ifdef CONFIG_CAN_CALC_BITTIMING #ifdef CONFIG_CAN_CALC_BITTIMING
#define CAN_CALC_MAX_ERROR 50 /* in one-tenth of a percent */ #define CAN_CALC_MAX_ERROR 50 /* in one-tenth of a percent */
#define CAN_CALC_SYNC_SEG 1
/* /*
* Bit-timing calculation derived from: * Bit-timing calculation derived from:
...@@ -83,98 +84,126 @@ EXPORT_SYMBOL_GPL(can_len2dlc); ...@@ -83,98 +84,126 @@ EXPORT_SYMBOL_GPL(can_len2dlc);
* registers of the CAN controller. You can find more information * registers of the CAN controller. You can find more information
* in the header file linux/can/netlink.h. * in the header file linux/can/netlink.h.
*/ */
static int can_update_spt(const struct can_bittiming_const *btc, static int can_update_sample_point(const struct can_bittiming_const *btc,
int sampl_pt, int tseg, int *tseg1, int *tseg2) unsigned int sample_point_nominal, unsigned int tseg,
unsigned int *tseg1_ptr, unsigned int *tseg2_ptr,
unsigned int *sample_point_error_ptr)
{ {
*tseg2 = tseg + 1 - (sampl_pt * (tseg + 1)) / 1000; unsigned int sample_point_error, best_sample_point_error = UINT_MAX;
if (*tseg2 < btc->tseg2_min) unsigned int sample_point, best_sample_point = 0;
*tseg2 = btc->tseg2_min; unsigned int tseg1, tseg2;
if (*tseg2 > btc->tseg2_max) int i;
*tseg2 = btc->tseg2_max;
*tseg1 = tseg - *tseg2; for (i = 0; i <= 1; i++) {
if (*tseg1 > btc->tseg1_max) { tseg2 = tseg + CAN_CALC_SYNC_SEG - (sample_point_nominal * (tseg + CAN_CALC_SYNC_SEG)) / 1000 - i;
*tseg1 = btc->tseg1_max; tseg2 = clamp(tseg2, btc->tseg2_min, btc->tseg2_max);
*tseg2 = tseg - *tseg1; tseg1 = tseg - tseg2;
if (tseg1 > btc->tseg1_max) {
tseg1 = btc->tseg1_max;
tseg2 = tseg - tseg1;
}
sample_point = 1000 * (tseg + CAN_CALC_SYNC_SEG - tseg2) / (tseg + CAN_CALC_SYNC_SEG);
sample_point_error = abs(sample_point_nominal - sample_point);
if ((sample_point <= sample_point_nominal) && (sample_point_error < best_sample_point_error)) {
best_sample_point = sample_point;
best_sample_point_error = sample_point_error;
*tseg1_ptr = tseg1;
*tseg2_ptr = tseg2;
}
} }
return 1000 * (tseg + 1 - *tseg2) / (tseg + 1);
if (sample_point_error_ptr)
*sample_point_error_ptr = best_sample_point_error;
return best_sample_point;
} }
static int can_calc_bittiming(struct net_device *dev, struct can_bittiming *bt, static int can_calc_bittiming(struct net_device *dev, struct can_bittiming *bt,
const struct can_bittiming_const *btc) const struct can_bittiming_const *btc)
{ {
struct can_priv *priv = netdev_priv(dev); struct can_priv *priv = netdev_priv(dev);
long best_error = 1000000000, error = 0; unsigned int bitrate; /* current bitrate */
int best_tseg = 0, best_brp = 0, brp = 0; unsigned int bitrate_error; /* difference between current and nominal value */
int tsegall, tseg = 0, tseg1 = 0, tseg2 = 0; unsigned int best_bitrate_error = UINT_MAX;
int spt_error = 1000, spt = 0, sampl_pt; unsigned int sample_point_error; /* difference between current and nominal value */
long rate; unsigned int best_sample_point_error = UINT_MAX;
unsigned int sample_point_nominal; /* nominal 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;
u64 v64; u64 v64;
/* Use CiA recommended sample points */ /* Use CiA recommended sample points */
if (bt->sample_point) { if (bt->sample_point) {
sampl_pt = bt->sample_point; sample_point_nominal = bt->sample_point;
} else { } else {
if (bt->bitrate > 800000) if (bt->bitrate > 800000)
sampl_pt = 750; sample_point_nominal = 750;
else if (bt->bitrate > 500000) else if (bt->bitrate > 500000)
sampl_pt = 800; sample_point_nominal = 800;
else else
sampl_pt = 875; sample_point_nominal = 875;
} }
/* tseg even = round down, odd = round up */ /* tseg even = round down, odd = round up */
for (tseg = (btc->tseg1_max + btc->tseg2_max) * 2 + 1; for (tseg = (btc->tseg1_max + btc->tseg2_max) * 2 + 1;
tseg >= (btc->tseg1_min + btc->tseg2_min) * 2; tseg--) { tseg >= (btc->tseg1_min + btc->tseg2_min) * 2; tseg--) {
tsegall = 1 + tseg / 2; tsegall = CAN_CALC_SYNC_SEG + tseg / 2;
/* Compute all possible tseg choices (tseg=tseg1+tseg2) */ /* Compute all possible tseg choices (tseg=tseg1+tseg2) */
brp = priv->clock.freq / (tsegall * bt->bitrate) + tseg % 2; brp = priv->clock.freq / (tsegall * bt->bitrate) + tseg % 2;
/* chose brp step which is possible in system */
/* choose brp step which is possible in system */
brp = (brp / btc->brp_inc) * btc->brp_inc; brp = (brp / btc->brp_inc) * btc->brp_inc;
if ((brp < btc->brp_min) || (brp > btc->brp_max)) if ((brp < btc->brp_min) || (brp > btc->brp_max))
continue; continue;
rate = priv->clock.freq / (brp * tsegall);
error = bt->bitrate - rate; bitrate = priv->clock.freq / (brp * tsegall);
bitrate_error = abs(bt->bitrate - bitrate);
/* tseg brp biterror */ /* tseg brp biterror */
if (error < 0) if (bitrate_error > best_bitrate_error)
error = -error;
if (error > best_error)
continue; continue;
best_error = error;
if (error == 0) { /* reset sample point error if we have a better bitrate */
spt = can_update_spt(btc, sampl_pt, tseg / 2, if (bitrate_error < best_bitrate_error)
&tseg1, &tseg2); best_sample_point_error = UINT_MAX;
error = sampl_pt - spt;
if (error < 0) can_update_sample_point(btc, sample_point_nominal, tseg / 2, &tseg1, &tseg2, &sample_point_error);
error = -error; if (sample_point_error > best_sample_point_error)
if (error > spt_error) continue;
continue;
spt_error = error; best_sample_point_error = sample_point_error;
} best_bitrate_error = bitrate_error;
best_tseg = tseg / 2; best_tseg = tseg / 2;
best_brp = brp; best_brp = brp;
if (error == 0)
if (bitrate_error == 0 && sample_point_error == 0)
break; break;
} }
if (best_error) { if (best_bitrate_error) {
/* Error in one-tenth of a percent */ /* Error in one-tenth of a percent */
error = (best_error * 1000) / bt->bitrate; v64 = (u64)best_bitrate_error * 1000;
if (error > CAN_CALC_MAX_ERROR) { do_div(v64, bt->bitrate);
bitrate_error = (u32)v64;
if (bitrate_error > CAN_CALC_MAX_ERROR) {
netdev_err(dev, netdev_err(dev,
"bitrate error %ld.%ld%% too high\n", "bitrate error %d.%d%% too high\n",
error / 10, error % 10); bitrate_error / 10, bitrate_error % 10);
return -EDOM; return -EDOM;
} else {
netdev_warn(dev, "bitrate error %ld.%ld%%\n",
error / 10, error % 10);
} }
netdev_warn(dev, "bitrate error %d.%d%%\n",
bitrate_error / 10, bitrate_error % 10);
} }
/* real sample point */ /* real sample point */
bt->sample_point = can_update_spt(btc, sampl_pt, best_tseg, bt->sample_point = can_update_sample_point(btc, sample_point_nominal, best_tseg,
&tseg1, &tseg2); &tseg1, &tseg2, NULL);
v64 = (u64)best_brp * 1000000000UL; v64 = (u64)best_brp * 1000 * 1000 * 1000;
do_div(v64, priv->clock.freq); do_div(v64, priv->clock.freq);
bt->tq = (u32)v64; bt->tq = (u32)v64;
bt->prop_seg = tseg1 / 2; bt->prop_seg = tseg1 / 2;
...@@ -182,9 +211,9 @@ static int can_calc_bittiming(struct net_device *dev, struct can_bittiming *bt, ...@@ -182,9 +211,9 @@ static int can_calc_bittiming(struct net_device *dev, struct can_bittiming *bt,
bt->phase_seg2 = tseg2; bt->phase_seg2 = tseg2;
/* check for sjw user settings */ /* check for sjw user settings */
if (!bt->sjw || !btc->sjw_max) if (!bt->sjw || !btc->sjw_max) {
bt->sjw = 1; bt->sjw = 1;
else { } else {
/* bt->sjw is at least 1 -> sanitize upper bound to sjw_max */ /* bt->sjw is at least 1 -> sanitize upper bound to sjw_max */
if (bt->sjw > btc->sjw_max) if (bt->sjw > btc->sjw_max)
bt->sjw = btc->sjw_max; bt->sjw = btc->sjw_max;
...@@ -194,8 +223,9 @@ static int can_calc_bittiming(struct net_device *dev, struct can_bittiming *bt, ...@@ -194,8 +223,9 @@ static int can_calc_bittiming(struct net_device *dev, struct can_bittiming *bt,
} }
bt->brp = best_brp; bt->brp = best_brp;
/* real bit-rate */
bt->bitrate = priv->clock.freq / (bt->brp * (tseg1 + tseg2 + 1)); /* real bitrate */
bt->bitrate = priv->clock.freq / (bt->brp * (CAN_CALC_SYNC_SEG + tseg1 + tseg2));
return 0; return 0;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment