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 */
535 * Bit-timing calculation derived from:
537 * Code based on LinCAN sources and H8S2638 project
538 * Copyright 2004-2006 Pavel Pisa - DCE FELK CVUT cz
539 * Copyright 2005 Stanislav Marek
540 * email: pisa@cmp.felk.cvut.cz
542 * Calculates proper bit-timing parameters for a specified bit-rate
543 * and sample-point, which can then be used to set the bit-timing
544 * registers of the CAN controller. You can find more information
545 * in the header file linux/can/netlink.h.
547 static int can_update_spt(const struct can_bittiming_const *btc,
548 int sampl_pt, int tseg, int *tseg1, int *tseg2)
550 *tseg2 = tseg + 1 - (sampl_pt * (tseg + 1)) / 1000;
551 if (*tseg2 < btc->tseg2_min)
552 *tseg2 = btc->tseg2_min;
553 if (*tseg2 > btc->tseg2_max)
554 *tseg2 = btc->tseg2_max;
555 *tseg1 = tseg - *tseg2;
556 if (*tseg1 > btc->tseg1_max) {
557 *tseg1 = btc->tseg1_max;
558 *tseg2 = tseg - *tseg1;
560 return 1000 * (tseg + 1 - *tseg2) / (tseg + 1);
563 static int can_calc_bittiming(struct net_device *dev, struct can_bittiming *bt,
564 const struct can_bittiming_const *btc)
566 struct can_priv *priv = netdev_priv(dev);
567 long best_error = 1000000000, error = 0;
568 int best_tseg = 0, best_brp = 0, brp = 0;
569 int tsegall, tseg = 0, tseg1 = 0, tseg2 = 0;
570 int spt_error = 1000, spt = 0, sampl_pt;
574 /* Use CiA recommended sample points */
575 if (bt->sample_point) {
576 sampl_pt = bt->sample_point;
578 if (bt->bitrate > 800000)
580 else if (bt->bitrate > 500000)
586 /* tseg even = round down, odd = round up */
587 for (tseg = (btc->tseg1_max + btc->tseg2_max) * 2 + 1;
588 tseg >= (btc->tseg1_min + btc->tseg2_min) * 2; tseg--) {
589 tsegall = 1 + tseg / 2;
590 /* Compute all possible tseg choices (tseg=tseg1+tseg2) */
591 brp = priv->clock.freq / (tsegall * bt->bitrate) + tseg % 2;
592 /* chose brp step which is possible in system */
593 brp = (brp / btc->brp_inc) * btc->brp_inc;
594 if ((brp < btc->brp_min) || (brp > btc->brp_max))
596 rate = priv->clock.freq / (brp * tsegall);
597 error = bt->bitrate - rate;
598 /* tseg brp biterror */
601 if (error > best_error)
605 spt = can_update_spt(btc, sampl_pt, tseg / 2,
607 error = sampl_pt - spt;
610 if (error > spt_error)
614 best_tseg = tseg / 2;
621 /* Error in one-tenth of a percent */
622 error = (best_error * 1000) / bt->bitrate;
623 if (error > CAN_CALC_MAX_ERROR) {
625 "bitrate error %ld.%ld%% too high\n",
626 error / 10, error % 10);
629 netdev_warn(dev, "bitrate error %ld.%ld%%\n",
630 error / 10, error % 10);
634 /* real sample point */
635 bt->sample_point = can_update_spt(btc, sampl_pt, best_tseg,
638 v64 = (u64)best_brp * 1000000000UL;
639 do_div(v64, priv->clock.freq);
641 bt->prop_seg = tseg1 / 2;
642 bt->phase_seg1 = tseg1 - bt->prop_seg;
643 bt->phase_seg2 = tseg2;
645 /* check for sjw user settings */
646 if (!bt->sjw || !btc->sjw_max)
649 /* bt->sjw is at least 1 -> sanitize upper bound to sjw_max */
650 if (bt->sjw > btc->sjw_max)
651 bt->sjw = btc->sjw_max;
652 /* bt->sjw must not be higher than tseg2 */
659 bt->bitrate = priv->clock.freq / (bt->brp * (tseg1 + tseg2 + 1));
664 static __u32 get_cia_sample_point(__u32 bitrate)
668 if (bitrate > 800000)
670 else if (bitrate > 500000)
678 static void print_bit_timing(const struct calc_bittiming_const *btc,
679 __u32 bitrate, __u32 sample_point, __u32 ref_clk,
682 struct net_device dev = {
683 .priv.clock.freq = ref_clk,
685 struct can_bittiming bt = {
687 .sample_point = sample_point,
689 long rate_error, spt_error;
692 printf("Bit timing parameters for %s with %.6f MHz ref clock\n"
693 "nominal real Bitrt nom real SampP\n"
694 "Bitrate TQ[ns] PrS PhS1 PhS2 SJW BRP Bitrate Error SampP SampP Error ",
695 btc->bittiming_const.name,
696 ref_clk / 1000000.0);
698 btc->printf_btr(&bt, true);
702 if (can_calc_bittiming(&dev, &bt, &btc->bittiming_const)) {
703 printf("%7d ***bitrate not possible***\n", bitrate);
707 /* get nominal sample point */
709 sample_point = get_cia_sample_point(bitrate);
711 rate_error = abs((__s32)(bitrate - bt.bitrate));
712 spt_error = abs((__s32)(sample_point - bt.sample_point));
718 "%4.1f%% %4.1f%% %4.1f%% ",
720 bt.tq, bt.prop_seg, bt.phase_seg1, bt.phase_seg2,
724 100.0 * rate_error / bitrate,
727 bt.sample_point / 10.0,
728 100.0 * spt_error / sample_point);
730 btc->printf_btr(&bt, false);
734 static void do_list(void)
738 for (i = 0; i < ARRAY_SIZE(can_calc_consts); i++)
739 printf("%s\n", can_calc_consts[i].bittiming_const.name);
742 int main(int argc, char *argv[])
745 __u32 opt_ref_clk = 0, ref_clk;
747 bool quiet = false, list = false, found = false;
752 const struct calc_bittiming_const *btc = NULL;
754 while ((opt = getopt(argc, argv, "b:c:lps:")) != -1) {
757 bitrate = atoi(optarg);
761 opt_ref_clk = atoi(optarg);
773 sampl_pt = atoi(optarg);
777 print_usage(argv[0]);
782 if (argc > optind + 1)
783 print_usage(argv[0]);
785 if (argc == optind + 1)
793 if (sampl_pt && (sampl_pt >= 1000 || sampl_pt < 100))
794 print_usage(argv[0]);
796 for (i = 0; i < ARRAY_SIZE(can_calc_consts); i++) {
797 if (name && strcmp(can_calc_consts[i].bittiming_const.name, name))
801 btc = &can_calc_consts[i];
804 ref_clk = opt_ref_clk;
806 ref_clk = btc->ref_clk;
809 print_bit_timing(btc, bitrate, sampl_pt, ref_clk, quiet);
811 for (j = 0; j < ARRAY_SIZE(common_bitrates); j++)
812 print_bit_timing(btc, common_bitrates[j],
813 sampl_pt, ref_clk, j);
819 printf("error: unknown CAN controller '%s', try one of these:\n\n", name);