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