]> rtime.felk.cvut.cz Git - can-utils.git/blob - can-calc-bit-timing.c
add 'v' command response
[can-utils.git] / can-calc-bit-timing.c
1 /* can-calc-bit-timing.c: Calculate CAN bit timing parameters
2  *
3  * Copyright (C) 2008 Wolfgang Grandegger <wg@grandegger.com>
4  *
5  * Derived from:
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
11  *
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
15  * any later version.
16  */
17
18 #include <errno.h>
19 #include <getopt.h>
20 #include <stdint.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24
25 #include <linux/types.h>
26
27 /* seems not to be defined in errno.h */
28 #ifndef ENOTSUPP
29 #define ENOTSUPP        524     /* Operation is not supported */
30 #endif
31
32 /* useful defines */
33 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
34
35 #define do_div(a,b) a = (a) / (b)
36
37 #define abs(x) ({                               \
38                 long __x = (x);                 \
39                 (__x < 0) ? -__x : __x;         \
40         })
41
42 /**
43  * clamp - return a value clamped to a given range with strict typechecking
44  * @val: current value
45  * @min: minimum allowable value
46  * @max: maximum allowable value
47  *
48  * This macro does strict typechecking of min/max to make sure they are of the
49  * same type as val.  See the unnecessary pointer comparisons.
50  */
51 #define clamp(val, min, max) ({                 \
52         typeof(val) __val = (val);              \
53         typeof(min) __min = (min);              \
54         typeof(max) __max = (max);              \
55         (void) (&__val == &__min);              \
56         (void) (&__val == &__max);              \
57         __val = __val < __min ? __min: __val;   \
58         __val > __max ? __max: __val; })
59
60 /* we don't want to see these prints */
61 #define dev_err(dev, format, arg...)    do { } while (0)
62 #define dev_warn(dev, format, arg...)   do { } while (0)
63
64 /* define in-kernel-types */
65 typedef __u64 u64;
66 typedef __u32 u32;
67
68
69 /*
70  * CAN bit-timing parameters
71  *
72  * For futher information, please read chapter "8 BIT TIMING
73  * REQUIREMENTS" of the "Bosch CAN Specification version 2.0"
74  * at http://www.semiconductors.bosch.de/pdf/can2spec.pdf.
75  */
76 struct can_bittiming {
77         __u32 bitrate;          /* Bit-rate in bits/second */
78         __u32 sample_point;     /* Sample point in one-tenth of a percent */
79         __u32 tq;               /* Time quanta (TQ) in nanoseconds */
80         __u32 prop_seg;         /* Propagation segment in TQs */
81         __u32 phase_seg1;       /* Phase buffer segment 1 in TQs */
82         __u32 phase_seg2;       /* Phase buffer segment 2 in TQs */
83         __u32 sjw;              /* Synchronisation jump width in TQs */
84         __u32 brp;              /* Bit-rate prescaler */
85 };
86
87 /*
88  * CAN harware-dependent bit-timing constant
89  *
90  * Used for calculating and checking bit-timing parameters
91  */
92 struct can_bittiming_const {
93         char name[16];          /* Name of the CAN controller hardware */
94         __u32 tseg1_min;        /* Time segement 1 = prop_seg + phase_seg1 */
95         __u32 tseg1_max;
96         __u32 tseg2_min;        /* Time segement 2 = phase_seg2 */
97         __u32 tseg2_max;
98         __u32 sjw_max;          /* Synchronisation jump width */
99         __u32 brp_min;          /* Bit-rate prescaler */
100         __u32 brp_max;
101         __u32 brp_inc;
102
103         /* added for can-calc-bit-timing utility */
104         __u32 ref_clk;          /* CAN system clock frequency in Hz */
105         void (*printf_btr)(struct can_bittiming *bt, int hdr);
106 };
107
108 /*
109  * CAN clock parameters
110  */
111 struct can_clock {
112         __u32 freq;             /* CAN system clock frequency in Hz */
113 };
114
115
116 /*
117  * minimal structs, just enough to be source level compatible
118  */
119 struct can_priv {
120         const struct can_bittiming_const *bittiming_const;
121         struct can_clock clock;
122 };
123
124 struct net_device {
125         struct can_priv priv;
126 };
127
128 static inline void *netdev_priv(const struct net_device *dev)
129 {
130         return (void *)&dev->priv;
131 }
132
133 static void print_usage(char* cmd)
134 {
135         printf("Usage: %s [options] [<CAN-contoller-name>]\n"
136                "\tOptions:\n"
137                "\t-q           : don't print header line\n"
138                "\t-l           : list all support CAN controller names\n"
139                "\t-b <bitrate> : bit-rate in bits/sec\n"
140                "\t-s <samp_pt> : sample-point in one-tenth of a percent\n"
141                "\t               or 0 for CIA recommended sample points\n"
142                "\t-c <clock>   : real CAN system clock in Hz\n",
143                cmd);
144
145         exit(EXIT_FAILURE);
146 }
147
148 static void printf_btr_sja1000(struct can_bittiming *bt, int hdr)
149 {
150         uint8_t btr0, btr1;
151
152         if (hdr) {
153                 printf("BTR0 BTR1");
154         } else {
155                 btr0 = ((bt->brp - 1) & 0x3f) | (((bt->sjw - 1) & 0x3) << 6);
156                 btr1 = ((bt->prop_seg + bt->phase_seg1 - 1) & 0xf) |
157                         (((bt->phase_seg2 - 1) & 0x7) << 4);
158                 printf("0x%02x 0x%02x", btr0, btr1);
159         }
160 }
161
162 static void printf_btr_at91(struct can_bittiming *bt, int hdr)
163 {
164         if (hdr) {
165                 printf("%10s", "CAN_BR");
166         } else {
167                 uint32_t br = ((bt->phase_seg2 - 1) |
168                                ((bt->phase_seg1 - 1) << 4) |
169                                ((bt->prop_seg - 1) << 8) |
170                                ((bt->sjw - 1) << 12) |
171                                ((bt->brp - 1) << 16));
172                 printf("0x%08x", br);
173         }
174 }
175
176 static void printf_btr_flexcan(struct can_bittiming *bt, int hdr)
177 {
178         if (hdr) {
179                 printf("%10s", "CAN_CTRL");
180         } else {
181                 uint32_t ctrl = (((bt->brp        - 1) << 24) |
182                                  ((bt->sjw        - 1) << 22) |
183                                  ((bt->phase_seg1 - 1) << 19) |
184                                  ((bt->phase_seg2 - 1) << 16) |
185                                  ((bt->prop_seg   - 1) <<  0));
186
187                 printf("0x%08x", ctrl);
188         }
189 }
190
191 static void printf_btr_mcp251x(struct can_bittiming *bt, int hdr)
192 {
193         uint8_t cnf1, cnf2, cnf3;
194
195         if (hdr) {
196                 printf("CNF1 CNF2 CNF3");
197         } else {
198                 cnf1 = ((bt->sjw - 1) << 6) | (bt->brp - 1);
199                 cnf2 = 0x80 | ((bt->phase_seg1 - 1) << 3) | (bt->prop_seg - 1);
200                 cnf3 = bt->phase_seg2 - 1;
201                 printf("0x%02x 0x%02x 0x%02x", cnf1, cnf2, cnf3);
202         }
203 }
204
205 static void printf_btr_ti_hecc(struct can_bittiming *bt, int hdr)
206 {
207         if (hdr) {
208                 printf("%10s", "CANBTC");
209         } else {
210                 uint32_t can_btc;
211
212                 can_btc = (bt->phase_seg2 - 1) & 0x7;
213                 can_btc |= ((bt->phase_seg1 + bt->prop_seg - 1)
214                             & 0xF) << 3;
215                 can_btc |= ((bt->sjw - 1) & 0x3) << 8;
216                 can_btc |= ((bt->brp - 1) & 0xFF) << 16;
217
218                 printf("0x%08x", can_btc);
219         }
220 }
221
222 static struct can_bittiming_const can_calc_consts[] = {
223         {
224                 .name = "sja1000",
225                 .tseg1_min = 1,
226                 .tseg1_max = 16,
227                 .tseg2_min = 1,
228                 .tseg2_max = 8,
229                 .sjw_max = 4,
230                 .brp_min = 1,
231                 .brp_max = 64,
232                 .brp_inc = 1,
233
234                 .ref_clk = 8000000,
235                 .printf_btr = printf_btr_sja1000,
236         },
237         {
238                 .name = "mscan",
239                 .tseg1_min = 4,
240                 .tseg1_max = 16,
241                 .tseg2_min = 2,
242                 .tseg2_max = 8,
243                 .sjw_max = 4,
244                 .brp_min = 1,
245                 .brp_max = 64,
246                 .brp_inc = 1,
247
248                 .ref_clk = 32000000,
249                 .printf_btr = printf_btr_sja1000,
250         },
251         {
252                 .name = "mscan",
253                 .tseg1_min = 4,
254                 .tseg1_max = 16,
255                 .tseg2_min = 2,
256                 .tseg2_max = 8,
257                 .sjw_max = 4,
258                 .brp_min = 1,
259                 .brp_max = 64,
260                 .brp_inc = 1,
261
262                 .ref_clk = 33000000,
263                 .printf_btr = printf_btr_sja1000,
264         },
265         {
266                 .name = "mscan",
267                 .tseg1_min = 4,
268                 .tseg1_max = 16,
269                 .tseg2_min = 2,
270                 .tseg2_max = 8,
271                 .sjw_max = 4,
272                 .brp_min = 1,
273                 .brp_max = 64,
274                 .brp_inc = 1,
275
276                 .ref_clk = 33300000,
277                 .printf_btr = printf_btr_sja1000,
278         },
279         {
280                 .name = "mscan",
281                 .tseg1_min = 4,
282                 .tseg1_max = 16,
283                 .tseg2_min = 2,
284                 .tseg2_max = 8,
285                 .sjw_max = 4,
286                 .brp_min = 1,
287                 .brp_max = 64,
288                 .brp_inc = 1,
289
290                 .ref_clk = 33333333,
291                 .printf_btr = printf_btr_sja1000,
292         },
293         {
294                 .name = "mscan",
295                 .tseg1_min = 4,
296                 .tseg1_max = 16,
297                 .tseg2_min = 2,
298                 .tseg2_max = 8,
299                 .sjw_max = 4,
300                 .brp_min = 1,
301                 .brp_max = 64,
302                 .brp_inc = 1,
303
304                 .ref_clk = 66660000,    /* mpc5121 */
305                 .printf_btr = printf_btr_sja1000,
306         },
307         {
308                 .name = "at91",
309                 .tseg1_min = 4,
310                 .tseg1_max = 16,
311                 .tseg2_min = 2,
312                 .tseg2_max = 8,
313                 .sjw_max = 4,
314                 .brp_min = 2,
315                 .brp_max = 128,
316                 .brp_inc = 1,
317
318                 .ref_clk = 100000000,
319                 .printf_btr = printf_btr_at91,
320         },
321         {
322                 .name = "at91",
323                 .tseg1_min = 4,
324                 .tseg1_max = 16,
325                 .tseg2_min = 2,
326                 .tseg2_max = 8,
327                 .sjw_max = 4,
328                 .brp_min = 2,
329                 .brp_max = 128,
330                 .brp_inc = 1,
331
332                 /* real world clock as found on the ronetix PM9263 */
333                 .ref_clk = 99532800,
334                 .printf_btr = printf_btr_at91,
335         },
336         {
337                 .name = "flexcan",
338                 .tseg1_min = 4,
339                 .tseg1_max = 16,
340                 .tseg2_min = 2,
341                 .tseg2_max = 8,
342                 .sjw_max = 4,
343                 .brp_min = 1,
344                 .brp_max = 256,
345                 .brp_inc = 1,
346
347                 .ref_clk = 24000000,    /* mx28 */
348                 .printf_btr = printf_btr_flexcan,
349         },
350         {
351                 .name = "flexcan",
352                 .tseg1_min = 4,
353                 .tseg1_max = 16,
354                 .tseg2_min = 2,
355                 .tseg2_max = 8,
356                 .sjw_max = 4,
357                 .brp_min = 1,
358                 .brp_max = 256,
359                 .brp_inc = 1,
360
361                 .ref_clk = 49875000,
362                 .printf_btr = printf_btr_flexcan,
363         },
364         {
365                 .name = "flexcan",
366                 .tseg1_min = 4,
367                 .tseg1_max = 16,
368                 .tseg2_min = 2,
369                 .tseg2_max = 8,
370                 .sjw_max = 4,
371                 .brp_min = 1,
372                 .brp_max = 256,
373                 .brp_inc = 1,
374
375                 .ref_clk = 66000000,
376                 .printf_btr = printf_btr_flexcan,
377         },
378         {
379                 .name = "flexcan",
380                 .tseg1_min = 4,
381                 .tseg1_max = 16,
382                 .tseg2_min = 2,
383                 .tseg2_max = 8,
384                 .sjw_max = 4,
385                 .brp_min = 1,
386                 .brp_max = 256,
387                 .brp_inc = 1,
388
389                 .ref_clk = 66500000,
390                 .printf_btr = printf_btr_flexcan,
391         },
392         {
393                 .name = "mcp251x",
394                 .tseg1_min = 3,
395                 .tseg1_max = 16,
396                 .tseg2_min = 2,
397                 .tseg2_max = 8,
398                 .sjw_max = 4,
399                 .brp_min = 1,
400                 .brp_max = 64,
401                 .brp_inc = 1,
402
403                 .ref_clk = 8000000,
404                 .printf_btr = printf_btr_mcp251x,
405         },
406         {
407                 .name = "mcp251x",
408                 .tseg1_min = 3,
409                 .tseg1_max = 16,
410                 .tseg2_min = 2,
411                 .tseg2_max = 8,
412                 .sjw_max = 4,
413                 .brp_min = 1,
414                 .brp_max = 64,
415                 .brp_inc = 1,
416
417                 .ref_clk = 16000000,
418                 .printf_btr = printf_btr_mcp251x,
419         },
420         {
421                 .name = "ti_hecc",
422                 .tseg1_min = 1,
423                 .tseg1_max = 16,
424                 .tseg2_min = 1,
425                 .tseg2_max = 8,
426                 .sjw_max = 4,
427                 .brp_min = 1,
428                 .brp_max = 256,
429                 .brp_inc = 1,
430
431                 .ref_clk = 13000000,
432                 .printf_btr = printf_btr_ti_hecc,
433         }
434 };
435
436 static long common_bitrates[] = {
437         1000000,
438         800000,
439         500000,
440         250000,
441         125000,
442         100000,
443         50000,
444         20000,
445         10000,
446 };
447
448 #define CAN_CALC_MAX_ERROR 50 /* in one-tenth of a percent */
449
450 static int can_update_spt(const struct can_bittiming_const *btc,
451                           int sampl_pt, int tseg, int *tseg1, int *tseg2)
452 {
453         *tseg2 = tseg + 1 - (sampl_pt * (tseg + 1)) / 1000;
454         if (*tseg2 < btc->tseg2_min)
455                 *tseg2 = btc->tseg2_min;
456         if (*tseg2 > btc->tseg2_max)
457                 *tseg2 = btc->tseg2_max;
458         *tseg1 = tseg - *tseg2;
459         if (*tseg1 > btc->tseg1_max) {
460                 *tseg1 = btc->tseg1_max;
461                 *tseg2 = tseg - *tseg1;
462         }
463         return 1000 * (tseg + 1 - *tseg2) / (tseg + 1);
464 }
465
466 static int can_calc_bittiming(struct net_device *dev, struct can_bittiming *bt)
467 {
468         struct can_priv *priv = netdev_priv(dev);
469         const struct can_bittiming_const *btc = priv->bittiming_const;
470         long rate = 0;
471         long best_error = 1000000000, error = 0;
472         int best_tseg = 0, best_brp = 0, brp = 0;
473         int tsegall, tseg = 0, tseg1 = 0, tseg2 = 0;
474         int spt_error = 1000, spt = 0, sampl_pt;
475         u64 v64;
476
477         if (!priv->bittiming_const)
478                 return -ENOTSUPP;
479
480         /* Use CIA recommended sample points */
481         if (bt->sample_point) {
482                 sampl_pt = bt->sample_point;
483         } else {
484                 if (bt->bitrate > 800000)
485                         sampl_pt = 750;
486                 else if (bt->bitrate > 500000)
487                         sampl_pt = 800;
488                 else
489                         sampl_pt = 875;
490         }
491
492         /* tseg even = round down, odd = round up */
493         for (tseg = (btc->tseg1_max + btc->tseg2_max) * 2 + 1;
494              tseg >= (btc->tseg1_min + btc->tseg2_min) * 2; tseg--) {
495                 tsegall = 1 + tseg / 2;
496                 /* Compute all possible tseg choices (tseg=tseg1+tseg2) */
497                 brp = priv->clock.freq / (tsegall * bt->bitrate) + tseg % 2;
498                 /* chose brp step which is possible in system */
499                 brp = (brp / btc->brp_inc) * btc->brp_inc;
500                 if ((brp < btc->brp_min) || (brp > btc->brp_max))
501                         continue;
502                 rate = priv->clock.freq / (brp * tsegall);
503                 error = bt->bitrate - rate;
504                 /* tseg brp biterror */
505                 if (error < 0)
506                         error = -error;
507                 if (error > best_error)
508                         continue;
509                 best_error = error;
510                 if (error == 0) {
511                         spt = can_update_spt(btc, sampl_pt, tseg / 2,
512                                              &tseg1, &tseg2);
513                         error = sampl_pt - spt;
514                         if (error < 0)
515                                 error = -error;
516                         if (error > spt_error)
517                                 continue;
518                         spt_error = error;
519                 }
520                 best_tseg = tseg / 2;
521                 best_brp = brp;
522                 if (error == 0)
523                         break;
524         }
525
526         if (best_error) {
527                 /* Error in one-tenth of a percent */
528                 error = (best_error * 1000) / bt->bitrate;
529                 if (error > CAN_CALC_MAX_ERROR) {
530                         dev_err(dev->dev.parent,
531                                 "bitrate error %ld.%ld%% too high\n",
532                                 error / 10, error % 10);
533                         return -EDOM;
534                 } else {
535                         dev_warn(dev->dev.parent, "bitrate error %ld.%ld%%\n",
536                                  error / 10, error % 10);
537                 }
538         }
539
540         /* real sample point */
541         bt->sample_point = can_update_spt(btc, sampl_pt, best_tseg,
542                                           &tseg1, &tseg2);
543
544         v64 = (u64)best_brp * 1000000000UL;
545         do_div(v64, priv->clock.freq);
546         bt->tq = (u32)v64;
547         bt->prop_seg = tseg1 / 2;
548         bt->phase_seg1 = tseg1 - bt->prop_seg;
549         bt->phase_seg2 = tseg2;
550         bt->sjw = 1;
551         bt->brp = best_brp;
552
553         /* real bit-rate */
554         bt->bitrate = priv->clock.freq / (bt->brp * (tseg1 + tseg2 + 1));
555
556         return 0;
557 }
558
559 static __u32 get_cia_sample_point(__u32 bitrate)
560 {
561         __u32 sampl_pt;
562
563         if (bitrate > 800000)
564                 sampl_pt = 750;
565         else if (bitrate > 500000)
566                 sampl_pt = 800;
567         else
568                 sampl_pt = 875;
569
570         return sampl_pt;
571 }
572
573 static void print_bit_timing(const struct can_bittiming_const *btc,
574                              __u32 bitrate, __u32 sample_point, __u32 ref_clk,
575                              int quiet)
576 {
577         struct net_device dev = {
578                 .priv.bittiming_const = btc,
579                 .priv.clock.freq = ref_clk,
580         };
581         struct can_bittiming bt = {
582                 .bitrate = bitrate,
583                 .sample_point = sample_point,
584         };
585         long rate_error, spt_error;
586
587         if (!quiet) {
588                 printf("Bit timing parameters for %s with %.6f MHz ref clock\n"
589                        "nominal                                 real Bitrt   nom  real SampP\n"
590                        "Bitrate TQ[ns] PrS PhS1 PhS2 SJW BRP Bitrate Error SampP SampP Error ",
591                        btc->name,
592                        ref_clk / 1000000.0);
593
594                 btc->printf_btr(&bt, 1);
595                 printf("\n");
596         }
597
598         if (can_calc_bittiming(&dev, &bt)) {
599                 printf("%7d ***bitrate not possible***\n", bitrate);
600                 return;
601         }
602
603         /* get nominal sample point */
604         if (!sample_point)
605                 sample_point = get_cia_sample_point(bitrate);
606
607         rate_error = abs((__s32)(bitrate - bt.bitrate));
608         spt_error = abs((__s32)(sample_point - bt.sample_point));
609
610         printf("%7d "
611                "%6d %3d %4d %4d "
612                "%3d %3d "
613                "%7d %4.1f%% "
614                "%4.1f%% %4.1f%% %4.1f%% ",
615                bitrate,
616                bt.tq, bt.prop_seg, bt.phase_seg1, bt.phase_seg2,
617                bt.sjw, bt.brp,
618
619                bt.bitrate,
620                100.0 * rate_error / bitrate,
621
622                sample_point / 10.0,
623                bt.sample_point / 10.0,
624                100.0 * spt_error / sample_point);
625
626         btc->printf_btr(&bt, 0);
627         printf("\n");
628 }
629
630 static void do_list(void)
631 {
632         unsigned int i;
633
634         for (i = 0; i < ARRAY_SIZE(can_calc_consts); i++)
635                 printf("%s\n", can_calc_consts[i].name);
636 }
637
638 int main(int argc, char *argv[])
639 {
640         __u32 bitrate = 0;
641         __u32 opt_ref_clk = 0, ref_clk;
642         int sampl_pt = 0;
643         int quiet = 0;
644         int list = 0;
645         char *name = NULL;
646         unsigned int i, j;
647         int opt, found = 0;
648
649         const struct can_bittiming_const *btc = NULL;
650
651         while ((opt = getopt(argc, argv, "b:c:lps:")) != -1) {
652                 switch (opt) {
653                 case 'b':
654                         bitrate = atoi(optarg);
655                         break;
656
657                 case 'c':
658                         opt_ref_clk = atoi(optarg);
659                         break;
660
661                 case 'l':
662                         list = 1;
663                         break;
664
665                 case 'q':
666                         quiet = 1;
667                         break;
668
669                 case 's':
670                         sampl_pt = atoi(optarg);
671                         break;
672
673                 default:
674                         print_usage(argv[0]);
675                         break;
676                 }
677         }
678
679         if (argc > optind + 1)
680                 print_usage(argv[0]);
681
682         if (argc == optind + 1)
683                 name = argv[optind];
684
685         if (list) {
686                 do_list();
687                 exit(EXIT_SUCCESS);
688         }
689
690         if (sampl_pt && (sampl_pt >= 1000 || sampl_pt < 100))
691                 print_usage(argv[0]);
692
693         for (i = 0; i < ARRAY_SIZE(can_calc_consts); i++) {
694                 if (name && strcmp(can_calc_consts[i].name, name))
695                         continue;
696
697                 found = 1;
698                 btc = &can_calc_consts[i];
699
700                 if (opt_ref_clk)
701                         ref_clk = opt_ref_clk;
702                 else
703                         ref_clk = btc->ref_clk;
704
705                 if (bitrate) {
706                         print_bit_timing(btc, bitrate, sampl_pt, ref_clk, quiet);
707                 } else {
708                         for (j = 0; j < ARRAY_SIZE(common_bitrates); j++)
709                                 print_bit_timing(btc, common_bitrates[j],
710                                                  sampl_pt, ref_clk, j);
711                 }
712                 printf("\n");
713         }
714
715         if (!found) {
716                 printf("error: unknown CAN controller '%s', try one of these:\n\n", name);
717                 do_list();
718                 exit(EXIT_FAILURE);
719         }
720
721         exit(EXIT_SUCCESS);
722 }