1 /* can-calc-bit-timing.c: Calculate CAN bit timing parameters
3 * Copyright (C) 2008 Wolfgang Grandegger <wg@grandegger.com>
4 * Copyright (C) 2016 Marc Kleine-Budde <mkl@pengutronix.de>
7 * can_baud.c - CAN baudrate calculation
8 * Code based on LinCAN sources and H8S2638 project
9 * Copyright 2004-2006 Pavel Pisa - DCE FELK CVUT cz
10 * Copyright 2005 Stanislav Marek
11 * email:pisa@cmp.felk.cvut.cz
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
28 #include <linux/types.h>
29 #include <linux/can/netlink.h>
31 /* imported from kernel */
34 * abs - return absolute value of an argument
35 * @x: the value. If it is unsigned type, it is converted to signed type first.
36 * char is treated as if it was signed (regardless of whether it really is)
37 * but the macro's return type is preserved as char.
39 * Return: an absolute value of x.
41 #define abs(x) __abs_choose_expr(x, long long, \
42 __abs_choose_expr(x, long, \
43 __abs_choose_expr(x, int, \
44 __abs_choose_expr(x, short, \
45 __abs_choose_expr(x, char, \
46 __builtin_choose_expr( \
47 __builtin_types_compatible_p(typeof(x), char), \
48 (char)({ signed char __x = (x); __x<0?-__x:__x; }), \
51 #define __abs_choose_expr(x, type, other) __builtin_choose_expr( \
52 __builtin_types_compatible_p(typeof(x), signed type) || \
53 __builtin_types_compatible_p(typeof(x), unsigned type), \
54 ({ signed type __x = (x); __x < 0 ? -__x : __x; }), other)
57 * min()/max()/clamp() macros that also do
58 * strict type-checking.. See the
59 * "unnecessary" pointer comparison.
61 #define min(x, y) ({ \
62 typeof(x) _min1 = (x); \
63 typeof(y) _min2 = (y); \
64 (void) (&_min1 == &_min2); \
65 _min1 < _min2 ? _min1 : _min2; })
67 #define max(x, y) ({ \
68 typeof(x) _max1 = (x); \
69 typeof(y) _max2 = (y); \
70 (void) (&_max1 == &_max2); \
71 _max1 > _max2 ? _max1 : _max2; })
74 * clamp - return a value clamped to a given range with strict typechecking
76 * @lo: lowest allowable value
77 * @hi: highest allowable value
79 * This macro does strict typechecking of lo/hi to make sure they are of the
80 * same type as val. See the unnecessary pointer comparisons.
82 #define clamp(val, lo, hi) min((typeof(val))max(val, lo), hi)
84 # define do_div(n,base) ({ \
85 uint32_t __base = (base); \
87 __rem = ((uint64_t)(n)) % __base; \
88 (n) = ((uint64_t)(n)) / __base; \
94 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
96 /* we don't want to see these prints */
97 #define netdev_err(dev, format, arg...) do { } while (0)
98 #define netdev_warn(dev, format, arg...) do { } while (0)
100 /* define in-kernel-types */
104 struct calc_ref_clk {
105 __u32 clk; /* CAN system clock frequency in Hz */
109 struct calc_bittiming_const {
110 struct can_bittiming_const bittiming_const;
112 const struct calc_ref_clk ref_clk[16];
113 void (*printf_btr)(struct can_bittiming *bt, bool hdr);
117 * minimal structs, just enough to be source level compatible
120 struct can_clock clock;
124 struct can_priv priv;
127 static inline void *netdev_priv(const struct net_device *dev)
129 return (void *)&dev->priv;
132 static void print_usage(char *cmd)
134 printf("Usage: %s [options] [<CAN-contoller-name>]\n"
136 "\t-q : don't print header line\n"
137 "\t-l : list all support CAN controller names\n"
138 "\t-b <bitrate> : bit-rate in bits/sec\n"
139 "\t-s <samp_pt> : sample-point in one-tenth of a percent\n"
140 "\t or 0 for CIA recommended sample points\n"
141 "\t-c <clock> : real CAN system clock in Hz\n",
147 static void printf_btr_sja1000(struct can_bittiming *bt, bool hdr)
154 btr0 = ((bt->brp - 1) & 0x3f) | (((bt->sjw - 1) & 0x3) << 6);
155 btr1 = ((bt->prop_seg + bt->phase_seg1 - 1) & 0xf) |
156 (((bt->phase_seg2 - 1) & 0x7) << 4);
157 printf("0x%02x 0x%02x", btr0, btr1);
161 static void printf_btr_at91(struct can_bittiming *bt, bool hdr)
164 printf("%10s", "CAN_BR");
166 uint32_t br = ((bt->phase_seg2 - 1) |
167 ((bt->phase_seg1 - 1) << 4) |
168 ((bt->prop_seg - 1) << 8) |
169 ((bt->sjw - 1) << 12) |
170 ((bt->brp - 1) << 16));
171 printf("0x%08x", br);
175 static void printf_btr_flexcan(struct can_bittiming *bt, bool hdr)
178 printf("%10s", "CAN_CTRL");
180 uint32_t ctrl = (((bt->brp - 1) << 24) |
181 ((bt->sjw - 1) << 22) |
182 ((bt->phase_seg1 - 1) << 19) |
183 ((bt->phase_seg2 - 1) << 16) |
184 ((bt->prop_seg - 1) << 0));
186 printf("0x%08x", ctrl);
190 static void printf_btr_mcp251x(struct can_bittiming *bt, bool hdr)
192 uint8_t cnf1, cnf2, cnf3;
195 printf("CNF1 CNF2 CNF3");
197 cnf1 = ((bt->sjw - 1) << 6) | (bt->brp - 1);
198 cnf2 = 0x80 | ((bt->phase_seg1 - 1) << 3) | (bt->prop_seg - 1);
199 cnf3 = bt->phase_seg2 - 1;
200 printf("0x%02x 0x%02x 0x%02x", cnf1, cnf2, cnf3);
204 static void printf_btr_ti_hecc(struct can_bittiming *bt, bool hdr)
207 printf("%10s", "CANBTC");
211 can_btc = (bt->phase_seg2 - 1) & 0x7;
212 can_btc |= ((bt->phase_seg1 + bt->prop_seg - 1)
214 can_btc |= ((bt->sjw - 1) & 0x3) << 8;
215 can_btc |= ((bt->brp - 1) & 0xFF) << 16;
217 printf("0x%08x", can_btc);
221 #define RCAR_CAN_BCR_TSEG1(x) (((x) & 0x0f) << 20)
222 #define RCAR_CAN_BCR_BPR(x) (((x) & 0x3ff) << 8)
223 #define RCAR_CAN_BCR_SJW(x) (((x) & 0x3) << 4)
224 #define RCAR_CAN_BCR_TSEG2(x) ((x) & 0x07)
226 static void printf_btr_rcar_can(struct can_bittiming *bt, bool hdr)
229 printf("%10s", "CiBCR");
233 bcr = RCAR_CAN_BCR_TSEG1(bt->phase_seg1 + bt->prop_seg - 1) |
234 RCAR_CAN_BCR_BPR(bt->brp - 1) |
235 RCAR_CAN_BCR_SJW(bt->sjw - 1) |
236 RCAR_CAN_BCR_TSEG2(bt->phase_seg2 - 1);
238 printf("0x%08x", bcr << 8);
242 static struct calc_bittiming_const can_calc_consts[] = {
258 .printf_btr = printf_btr_sja1000,
272 { .clk = 32000000, },
273 { .clk = 33000000, },
274 { .clk = 33300000, },
275 { .clk = 33333333, },
276 { .clk = 66660000, .name = "mpc5121", },
277 { .clk = 66666666, .name = "mpc5121" },
279 .printf_btr = printf_btr_sja1000,
293 { .clk = 99532800, .name = "ronetix PM9263", },
294 { .clk = 100000000, },
296 .printf_btr = printf_btr_at91,
310 { .clk = 24000000, .name = "mx28" },
311 { .clk = 30000000, .name = "mx6" },
312 { .clk = 49875000, },
313 { .clk = 66000000, },
314 { .clk = 66500000, },
315 { .clk = 66666666, },
316 { .clk = 83368421, .name = "vybrid" },
318 .printf_btr = printf_btr_flexcan,
333 { .clk = 16000000, },
335 .printf_btr = printf_btr_mcp251x,
349 { .clk = 13000000, },
351 .printf_btr = printf_btr_ti_hecc,
365 { .clk = 65000000, },
367 .printf_btr = printf_btr_rcar_can,
371 static long common_bitrates[] = {
383 #define CAN_CALC_MAX_ERROR 50 /* in one-tenth of a percent */
384 #define CAN_CALC_SYNC_SEG 1
387 * Bit-timing calculation derived from:
389 * Code based on LinCAN sources and H8S2638 project
390 * Copyright 2004-2006 Pavel Pisa - DCE FELK CVUT cz
391 * Copyright 2005 Stanislav Marek
392 * email: pisa@cmp.felk.cvut.cz
394 * Calculates proper bit-timing parameters for a specified bit-rate
395 * and sample-point, which can then be used to set the bit-timing
396 * registers of the CAN controller. You can find more information
397 * in the header file linux/can/netlink.h.
399 static int can_update_spt(const struct can_bittiming_const *btc,
400 unsigned int spt_nominal, unsigned int tseg,
401 unsigned int *tseg1_ptr, unsigned int *tseg2_ptr,
402 unsigned int *spt_error_ptr)
404 unsigned int spt_error, best_spt_error = UINT_MAX;
405 unsigned int spt, best_spt = 0;
406 unsigned int tseg1, tseg2;
409 for (i = 0; i <= 1; i++) {
410 tseg2 = tseg + CAN_CALC_SYNC_SEG - (spt_nominal * (tseg + CAN_CALC_SYNC_SEG)) / 1000 - i;
411 tseg2 = clamp(tseg2, btc->tseg2_min, btc->tseg2_max);
412 tseg1 = tseg - tseg2;
413 if (tseg1 > btc->tseg1_max) {
414 tseg1 = btc->tseg1_max;
415 tseg2 = tseg - tseg1;
418 spt = 1000 * (tseg + CAN_CALC_SYNC_SEG - tseg2) / (tseg + CAN_CALC_SYNC_SEG);
419 spt_error = abs(spt_nominal - spt);
421 if ((spt <= spt_nominal) && (spt_error < best_spt_error)) {
423 best_spt_error = spt_error;
430 *spt_error_ptr = best_spt_error;
435 static int can_calc_bittiming(struct net_device *dev, struct can_bittiming *bt,
436 const struct can_bittiming_const *btc)
438 struct can_priv *priv = netdev_priv(dev);
439 unsigned int rate; /* current bitrate */
440 unsigned int rate_error; /* difference between current and nominal value */
441 unsigned int best_rate_error = UINT_MAX;
442 unsigned int spt_error; /* difference between current and nominal value */
443 unsigned int best_spt_error = UINT_MAX;
444 unsigned int spt_nominal; /* nominal sample point */
445 unsigned int best_tseg = 0; /* current best value for tseg */
446 unsigned int best_brp = 0; /* current best value for brp */
447 unsigned int brp, tsegall, tseg, tseg1, tseg2;
450 /* Use CiA recommended sample points */
451 if (bt->sample_point) {
452 spt_nominal = bt->sample_point;
454 if (bt->bitrate > 800000)
456 else if (bt->bitrate > 500000)
462 /* tseg even = round down, odd = round up */
463 for (tseg = (btc->tseg1_max + btc->tseg2_max) * 2 + 1;
464 tseg >= (btc->tseg1_min + btc->tseg2_min) * 2; tseg--) {
465 tsegall = CAN_CALC_SYNC_SEG + tseg / 2;
467 /* Compute all possible tseg choices (tseg=tseg1+tseg2) */
468 brp = priv->clock.freq / (tsegall * bt->bitrate) + tseg % 2;
470 /* choose brp step which is possible in system */
471 brp = (brp / btc->brp_inc) * btc->brp_inc;
472 if ((brp < btc->brp_min) || (brp > btc->brp_max))
475 rate = priv->clock.freq / (brp * tsegall);
476 rate_error = abs(bt->bitrate - rate);
478 /* tseg brp biterror */
479 if (rate_error > best_rate_error)
482 /* reset sample point error if we have a better bitrate */
483 if (rate_error < best_rate_error)
484 best_spt_error = UINT_MAX;
486 can_update_spt(btc, spt_nominal, tseg / 2, &tseg1, &tseg2, &spt_error);
487 if (spt_error > best_spt_error)
490 best_spt_error = spt_error;
491 best_rate_error = rate_error;
492 best_tseg = tseg / 2;
495 if (rate_error == 0 && spt_error == 0)
499 if (best_rate_error) {
500 /* Error in one-tenth of a percent */
501 rate_error = (best_rate_error * 1000) / bt->bitrate;
502 if (rate_error > CAN_CALC_MAX_ERROR) {
504 "bitrate error %ld.%ld%% too high\n",
505 rate_error / 10, rate_error % 10);
508 netdev_warn(dev, "bitrate error %ld.%ld%%\n",
509 rate_error / 10, rate_error % 10);
512 /* real sample point */
513 bt->sample_point = can_update_spt(btc, spt_nominal, best_tseg,
514 &tseg1, &tseg2, NULL);
516 v64 = (u64)best_brp * 1000 * 1000 * 1000;
517 do_div(v64, priv->clock.freq);
519 bt->prop_seg = tseg1 / 2;
520 bt->phase_seg1 = tseg1 - bt->prop_seg;
521 bt->phase_seg2 = tseg2;
523 /* check for sjw user settings */
524 if (!bt->sjw || !btc->sjw_max) {
527 /* bt->sjw is at least 1 -> sanitize upper bound to sjw_max */
528 if (bt->sjw > btc->sjw_max)
529 bt->sjw = btc->sjw_max;
530 /* bt->sjw must not be higher than tseg2 */
538 bt->bitrate = priv->clock.freq / (bt->brp * (CAN_CALC_SYNC_SEG + tseg1 + tseg2));
543 static __u32 get_cia_sample_point(__u32 bitrate)
547 if (bitrate > 800000)
549 else if (bitrate > 500000)
557 static void print_bit_timing(const struct calc_bittiming_const *btc,
558 const struct calc_ref_clk *ref_clk,
559 unsigned int bitrate_nominal,
560 unsigned int spt_nominal,
563 struct net_device dev = {
564 .priv.clock.freq = ref_clk->clk,
566 struct can_bittiming bt = {
567 .bitrate = bitrate_nominal,
568 .sample_point = spt_nominal,
570 long rate_error, spt_error;
573 printf("Bit timing parameters for %s%s%s%s with %.6f MHz ref clock\n"
574 "nominal real Bitrt nom real SampP\n"
575 "Bitrate TQ[ns] PrS PhS1 PhS2 SJW BRP Bitrate Error SampP SampP Error ",
576 btc->bittiming_const.name,
577 ref_clk->name ? " (" : "",
578 ref_clk->name ? ref_clk->name : "",
579 ref_clk->name ? ")" : "",
580 ref_clk->clk / 1000000.0);
582 btc->printf_btr(&bt, true);
586 if (can_calc_bittiming(&dev, &bt, &btc->bittiming_const)) {
587 printf("%7d ***bitrate not possible***\n", bitrate_nominal);
591 /* get nominal sample point */
593 spt_nominal = get_cia_sample_point(bitrate_nominal);
595 rate_error = abs(bitrate_nominal - bt.bitrate);
596 spt_error = abs(spt_nominal - bt.sample_point);
602 "%4.1f%% %4.1f%% %4.1f%% ",
604 bt.tq, bt.prop_seg, bt.phase_seg1, bt.phase_seg2,
608 100.0 * rate_error / bitrate_nominal,
611 bt.sample_point / 10.0,
612 100.0 * spt_error / spt_nominal);
614 btc->printf_btr(&bt, false);
618 static void do_list(void)
622 for (i = 0; i < ARRAY_SIZE(can_calc_consts); i++)
623 printf("%s\n", can_calc_consts[i].bittiming_const.name);
626 int main(int argc, char *argv[])
628 __u32 bitrate_nominal = 0;
629 struct calc_ref_clk opt_ref_clk = {
632 const struct calc_ref_clk *ref_clk;
633 unsigned int spt_nominal = 0;
634 bool quiet = false, list = false, found = false;
636 unsigned int i, j, k;
639 const struct calc_bittiming_const *btc;
641 while ((opt = getopt(argc, argv, "b:c:lqs:")) != -1) {
644 bitrate_nominal = atoi(optarg);
648 opt_ref_clk.clk = strtoul(optarg, NULL, 10);
660 spt_nominal = strtoul(optarg, NULL, 10);
664 print_usage(argv[0]);
669 if (argc > optind + 1)
670 print_usage(argv[0]);
672 if (argc == optind + 1)
680 if (spt_nominal && (spt_nominal >= 1000 || spt_nominal < 100))
681 print_usage(argv[0]);
683 for (i = 0; i < ARRAY_SIZE(can_calc_consts); i++) {
684 if (name && strcmp(can_calc_consts[i].bittiming_const.name, name))
688 btc = &can_calc_consts[i];
690 for (j = 0; j < ARRAY_SIZE(btc->ref_clk); j++) {
692 ref_clk = &opt_ref_clk;
694 ref_clk = &btc->ref_clk[j];
699 if (bitrate_nominal) {
700 print_bit_timing(btc, ref_clk, bitrate_nominal,
703 for (k = 0; k < ARRAY_SIZE(common_bitrates); k++)
704 print_bit_timing(btc, ref_clk,
716 printf("error: unknown CAN controller '%s', try one of these:\n\n", name);