1 /* can-calc-bit-timing.c: Calculate CAN bit timing parameters
3 * Copyright (C) 2008 Wolfgang Grandegger <wg@grandegger.com>
6 * can_baud.c - CAN baudrate calculation
7 * Code based on LinCAN sources and H8S2638 project
8 * Copyright 2004-2006 Pavel Pisa - DCE FELK CVUT cz
9 * Copyright 2005 Stanislav Marek
10 * email:pisa@cmp.felk.cvut.cz
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
26 #include <linux/types.h>
27 #include <linux/can/netlink.h>
29 /* imported from kernel */
32 * abs - return absolute value of an argument
33 * @x: the value. If it is unsigned type, it is converted to signed type first.
34 * char is treated as if it was signed (regardless of whether it really is)
35 * but the macro's return type is preserved as char.
37 * Return: an absolute value of x.
39 #define abs(x) __abs_choose_expr(x, long long, \
40 __abs_choose_expr(x, long, \
41 __abs_choose_expr(x, int, \
42 __abs_choose_expr(x, short, \
43 __abs_choose_expr(x, char, \
44 __builtin_choose_expr( \
45 __builtin_types_compatible_p(typeof(x), char), \
46 (char)({ signed char __x = (x); __x<0?-__x:__x; }), \
49 #define __abs_choose_expr(x, type, other) __builtin_choose_expr( \
50 __builtin_types_compatible_p(typeof(x), signed type) || \
51 __builtin_types_compatible_p(typeof(x), unsigned type), \
52 ({ signed type __x = (x); __x < 0 ? -__x : __x; }), other)
55 * min()/max()/clamp() macros that also do
56 * strict type-checking.. See the
57 * "unnecessary" pointer comparison.
59 #define min(x, y) ({ \
60 typeof(x) _min1 = (x); \
61 typeof(y) _min2 = (y); \
62 (void) (&_min1 == &_min2); \
63 _min1 < _min2 ? _min1 : _min2; })
65 #define max(x, y) ({ \
66 typeof(x) _max1 = (x); \
67 typeof(y) _max2 = (y); \
68 (void) (&_max1 == &_max2); \
69 _max1 > _max2 ? _max1 : _max2; })
72 * clamp - return a value clamped to a given range with strict typechecking
74 * @lo: lowest allowable value
75 * @hi: highest allowable value
77 * This macro does strict typechecking of lo/hi to make sure they are of the
78 * same type as val. See the unnecessary pointer comparisons.
80 #define clamp(val, lo, hi) min((typeof(val))max(val, lo), hi)
82 # define do_div(n,base) ({ \
83 uint32_t __base = (base); \
85 __rem = ((uint64_t)(n)) % __base; \
86 (n) = ((uint64_t)(n)) / __base; \
92 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
94 /* we don't want to see these prints */
95 #define netdev_err(dev, format, arg...) do { } while (0)
96 #define netdev_warn(dev, format, arg...) do { } while (0)
98 /* define in-kernel-types */
102 struct calc_bittiming_const {
103 struct can_bittiming_const bittiming_const;
105 __u32 ref_clk; /* CAN system clock frequency in Hz */
106 void (*printf_btr)(struct can_bittiming *bt, bool hdr);
110 * minimal structs, just enough to be source level compatible
113 struct can_clock clock;
117 struct can_priv priv;
120 static inline void *netdev_priv(const struct net_device *dev)
122 return (void *)&dev->priv;
125 static void print_usage(char *cmd)
127 printf("Usage: %s [options] [<CAN-contoller-name>]\n"
129 "\t-q : don't print header line\n"
130 "\t-l : list all support CAN controller names\n"
131 "\t-b <bitrate> : bit-rate in bits/sec\n"
132 "\t-s <samp_pt> : sample-point in one-tenth of a percent\n"
133 "\t or 0 for CIA recommended sample points\n"
134 "\t-c <clock> : real CAN system clock in Hz\n",
140 static void printf_btr_sja1000(struct can_bittiming *bt, bool hdr)
147 btr0 = ((bt->brp - 1) & 0x3f) | (((bt->sjw - 1) & 0x3) << 6);
148 btr1 = ((bt->prop_seg + bt->phase_seg1 - 1) & 0xf) |
149 (((bt->phase_seg2 - 1) & 0x7) << 4);
150 printf("0x%02x 0x%02x", btr0, btr1);
154 static void printf_btr_at91(struct can_bittiming *bt, bool hdr)
157 printf("%10s", "CAN_BR");
159 uint32_t br = ((bt->phase_seg2 - 1) |
160 ((bt->phase_seg1 - 1) << 4) |
161 ((bt->prop_seg - 1) << 8) |
162 ((bt->sjw - 1) << 12) |
163 ((bt->brp - 1) << 16));
164 printf("0x%08x", br);
168 static void printf_btr_flexcan(struct can_bittiming *bt, bool hdr)
171 printf("%10s", "CAN_CTRL");
173 uint32_t ctrl = (((bt->brp - 1) << 24) |
174 ((bt->sjw - 1) << 22) |
175 ((bt->phase_seg1 - 1) << 19) |
176 ((bt->phase_seg2 - 1) << 16) |
177 ((bt->prop_seg - 1) << 0));
179 printf("0x%08x", ctrl);
183 static void printf_btr_mcp251x(struct can_bittiming *bt, bool hdr)
185 uint8_t cnf1, cnf2, cnf3;
188 printf("CNF1 CNF2 CNF3");
190 cnf1 = ((bt->sjw - 1) << 6) | (bt->brp - 1);
191 cnf2 = 0x80 | ((bt->phase_seg1 - 1) << 3) | (bt->prop_seg - 1);
192 cnf3 = bt->phase_seg2 - 1;
193 printf("0x%02x 0x%02x 0x%02x", cnf1, cnf2, cnf3);
197 static void printf_btr_ti_hecc(struct can_bittiming *bt, bool hdr)
200 printf("%10s", "CANBTC");
204 can_btc = (bt->phase_seg2 - 1) & 0x7;
205 can_btc |= ((bt->phase_seg1 + bt->prop_seg - 1)
207 can_btc |= ((bt->sjw - 1) & 0x3) << 8;
208 can_btc |= ((bt->brp - 1) & 0xFF) << 16;
210 printf("0x%08x", can_btc);
214 #define RCAR_CAN_BCR_TSEG1(x) (((x) & 0x0f) << 20)
215 #define RCAR_CAN_BCR_BPR(x) (((x) & 0x3ff) << 8)
216 #define RCAR_CAN_BCR_SJW(x) (((x) & 0x3) << 4)
217 #define RCAR_CAN_BCR_TSEG2(x) ((x) & 0x07)
219 static void printf_btr_rcar_can(struct can_bittiming *bt, bool hdr)
222 printf("%10s", "CiBCR");
226 bcr = RCAR_CAN_BCR_TSEG1(bt->phase_seg1 + bt->prop_seg - 1) |
227 RCAR_CAN_BCR_BPR(bt->brp - 1) |
228 RCAR_CAN_BCR_SJW(bt->sjw - 1) |
229 RCAR_CAN_BCR_TSEG2(bt->phase_seg2 - 1);
231 printf("0x%08x", bcr << 8);
235 static struct calc_bittiming_const can_calc_consts[] = {
249 .printf_btr = printf_btr_sja1000,
263 .printf_btr = printf_btr_sja1000,
277 .printf_btr = printf_btr_sja1000,
291 .printf_btr = printf_btr_sja1000,
305 .printf_btr = printf_btr_sja1000,
318 .ref_clk = 66660000, /* mpc5121 */
319 .printf_btr = printf_btr_sja1000,
332 .ref_clk = 66666666, /* mpc5121 */
333 .printf_btr = printf_btr_sja1000,
346 .ref_clk = 100000000,
347 .printf_btr = printf_btr_at91,
360 /* real world clock as found on the ronetix PM9263 */
362 .printf_btr = printf_btr_at91,
375 .ref_clk = 24000000, /* mx28 */
376 .printf_btr = printf_btr_flexcan,
389 .ref_clk = 30000000, /* mx6 */
390 .printf_btr = printf_btr_flexcan,
404 .printf_btr = printf_btr_flexcan,
418 .printf_btr = printf_btr_flexcan,
432 .printf_btr = printf_btr_flexcan,
446 .printf_btr = printf_btr_flexcan,
460 .printf_btr = printf_btr_flexcan, /* vybrid */
474 .printf_btr = printf_btr_mcp251x,
488 .printf_btr = printf_btr_mcp251x,
502 .printf_btr = printf_btr_ti_hecc,
516 .printf_btr = printf_btr_rcar_can,
520 static long common_bitrates[] = {
532 #define CAN_CALC_MAX_ERROR 50 /* in one-tenth of a percent */
533 #define CAN_CALC_SYNC_SEG 1
536 * Bit-timing calculation derived from:
538 * Code based on LinCAN sources and H8S2638 project
539 * Copyright 2004-2006 Pavel Pisa - DCE FELK CVUT cz
540 * Copyright 2005 Stanislav Marek
541 * email: pisa@cmp.felk.cvut.cz
543 * Calculates proper bit-timing parameters for a specified bit-rate
544 * and sample-point, which can then be used to set the bit-timing
545 * registers of the CAN controller. You can find more information
546 * in the header file linux/can/netlink.h.
548 static int can_update_spt(const struct can_bittiming_const *btc,
549 int sampl_pt, int tseg, int *tseg1, int *tseg2)
551 *tseg2 = tseg + CAN_CALC_SYNC_SEG -
552 (sampl_pt * (tseg + CAN_CALC_SYNC_SEG)) / 1000;
553 if (*tseg2 < btc->tseg2_min)
554 *tseg2 = btc->tseg2_min;
555 if (*tseg2 > btc->tseg2_max)
556 *tseg2 = btc->tseg2_max;
557 *tseg1 = tseg - *tseg2;
558 if (*tseg1 > btc->tseg1_max) {
559 *tseg1 = btc->tseg1_max;
560 *tseg2 = tseg - *tseg1;
562 return 1000 * (tseg + CAN_CALC_SYNC_SEG - *tseg2) / (tseg + CAN_CALC_SYNC_SEG);
565 static int can_calc_bittiming(struct net_device *dev, struct can_bittiming *bt,
566 const struct can_bittiming_const *btc)
568 struct can_priv *priv = netdev_priv(dev);
569 long best_error = 1000000000, error = 0;
570 int best_tseg = 0, best_brp = 0, brp = 0;
571 int tsegall, tseg = 0, tseg1 = 0, tseg2 = 0;
572 int spt_error = 1000, spt = 0, sampl_pt;
576 /* Use CiA recommended sample points */
577 if (bt->sample_point) {
578 sampl_pt = bt->sample_point;
580 if (bt->bitrate > 800000)
582 else if (bt->bitrate > 500000)
588 /* tseg even = round down, odd = round up */
589 for (tseg = (btc->tseg1_max + btc->tseg2_max) * 2 + 1;
590 tseg >= (btc->tseg1_min + btc->tseg2_min) * 2; tseg--) {
591 tsegall = CAN_CALC_SYNC_SEG + tseg / 2;
593 /* Compute all possible tseg choices (tseg=tseg1+tseg2) */
594 brp = priv->clock.freq / (tsegall * bt->bitrate) + tseg % 2;
595 /* chose brp step which is possible in system */
596 brp = (brp / btc->brp_inc) * btc->brp_inc;
597 if ((brp < btc->brp_min) || (brp > btc->brp_max))
599 rate = priv->clock.freq / (brp * tsegall);
600 error = bt->bitrate - rate;
601 /* tseg brp biterror */
604 if (error > best_error)
608 spt = can_update_spt(btc, sampl_pt, tseg / 2,
610 error = sampl_pt - spt;
613 if (error > spt_error)
617 best_tseg = tseg / 2;
624 /* Error in one-tenth of a percent */
625 error = (best_error * 1000) / bt->bitrate;
626 if (error > CAN_CALC_MAX_ERROR) {
628 "bitrate error %ld.%ld%% too high\n",
629 error / 10, error % 10);
632 netdev_warn(dev, "bitrate error %ld.%ld%%\n",
633 error / 10, error % 10);
636 /* real sample point */
637 bt->sample_point = can_update_spt(btc, sampl_pt, best_tseg,
640 v64 = (u64)best_brp * 1000000000UL;
641 do_div(v64, priv->clock.freq);
643 bt->prop_seg = tseg1 / 2;
644 bt->phase_seg1 = tseg1 - bt->prop_seg;
645 bt->phase_seg2 = tseg2;
647 /* check for sjw user settings */
648 if (!bt->sjw || !btc->sjw_max)
651 /* bt->sjw is at least 1 -> sanitize upper bound to sjw_max */
652 if (bt->sjw > btc->sjw_max)
653 bt->sjw = btc->sjw_max;
654 /* bt->sjw must not be higher than tseg2 */
661 bt->bitrate = priv->clock.freq / (bt->brp * (CAN_CALC_SYNC_SEG + tseg1 + tseg2));
666 static __u32 get_cia_sample_point(__u32 bitrate)
670 if (bitrate > 800000)
672 else if (bitrate > 500000)
680 static void print_bit_timing(const struct calc_bittiming_const *btc,
681 __u32 bitrate, __u32 sample_point, __u32 ref_clk,
684 struct net_device dev = {
685 .priv.clock.freq = ref_clk,
687 struct can_bittiming bt = {
689 .sample_point = sample_point,
691 long rate_error, spt_error;
694 printf("Bit timing parameters for %s with %.6f MHz ref clock\n"
695 "nominal real Bitrt nom real SampP\n"
696 "Bitrate TQ[ns] PrS PhS1 PhS2 SJW BRP Bitrate Error SampP SampP Error ",
697 btc->bittiming_const.name,
698 ref_clk / 1000000.0);
700 btc->printf_btr(&bt, true);
704 if (can_calc_bittiming(&dev, &bt, &btc->bittiming_const)) {
705 printf("%7d ***bitrate not possible***\n", bitrate);
709 /* get nominal sample point */
711 sample_point = get_cia_sample_point(bitrate);
713 rate_error = abs((__s32)(bitrate - bt.bitrate));
714 spt_error = abs((__s32)(sample_point - bt.sample_point));
720 "%4.1f%% %4.1f%% %4.1f%% ",
722 bt.tq, bt.prop_seg, bt.phase_seg1, bt.phase_seg2,
726 100.0 * rate_error / bitrate,
729 bt.sample_point / 10.0,
730 100.0 * spt_error / sample_point);
732 btc->printf_btr(&bt, false);
736 static void do_list(void)
740 for (i = 0; i < ARRAY_SIZE(can_calc_consts); i++)
741 printf("%s\n", can_calc_consts[i].bittiming_const.name);
744 int main(int argc, char *argv[])
747 __u32 opt_ref_clk = 0, ref_clk;
749 bool quiet = false, list = false, found = false;
754 const struct calc_bittiming_const *btc = NULL;
756 while ((opt = getopt(argc, argv, "b:c:lps:")) != -1) {
759 bitrate = atoi(optarg);
763 opt_ref_clk = atoi(optarg);
775 sampl_pt = atoi(optarg);
779 print_usage(argv[0]);
784 if (argc > optind + 1)
785 print_usage(argv[0]);
787 if (argc == optind + 1)
795 if (sampl_pt && (sampl_pt >= 1000 || sampl_pt < 100))
796 print_usage(argv[0]);
798 for (i = 0; i < ARRAY_SIZE(can_calc_consts); i++) {
799 if (name && strcmp(can_calc_consts[i].bittiming_const.name, name))
803 btc = &can_calc_consts[i];
806 ref_clk = opt_ref_clk;
808 ref_clk = btc->ref_clk;
811 print_bit_timing(btc, bitrate, sampl_pt, ref_clk, quiet);
813 for (j = 0; j < ARRAY_SIZE(common_bitrates); j++)
814 print_bit_timing(btc, common_bitrates[j],
815 sampl_pt, ref_clk, j);
821 printf("error: unknown CAN controller '%s', try one of these:\n\n", name);