]> rtime.felk.cvut.cz Git - can-utils.git/blob - can-calc-bit-timing.c
95bc79655037ef75cf478726a210d5ababe5a99a
[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_rtcantl1(struct can_bittiming *bt, int hdr)
203 {
204         uint16_t bcr0, bcr1;
205
206         if (hdr) {
207                 printf("__BCR0 __BCR1");
208         } else {
209                 bcr1 = ((((bt->prop_seg + bt->phase_seg1 - 1) & 0x0F) << 12) |
210                         (((bt->phase_seg2 - 1) & 0x07) << 8) |
211                         (((bt->sjw - 1) & 0x03) << 4));
212                 bcr0 =  ((bt->brp - 1) & 0xFF);
213                 printf("0x%04x 0x%04x", bcr0, bcr1);
214         }
215 }
216
217 static struct can_bittiming_const can_calc_consts[] = {
218         {
219                 .name = "sja1000",
220                 .tseg1_min = 1,
221                 .tseg1_max = 16,
222                 .tseg2_min = 1,
223                 .tseg2_max = 8,
224                 .sjw_max = 4,
225                 .brp_min = 1,
226                 .brp_max = 64,
227                 .brp_inc = 1,
228
229                 .ref_clk = 8000000,
230                 .printf_btr = printf_btr_sja1000,
231         },
232         {
233                 .name = "mscan",
234                 .tseg1_min = 4,
235                 .tseg1_max = 16,
236                 .tseg2_min = 2,
237                 .tseg2_max = 8,
238                 .sjw_max = 4,
239                 .brp_min = 1,
240                 .brp_max = 64,
241                 .brp_inc = 1,
242
243                 .ref_clk = 32000000,
244                 .printf_btr = printf_btr_sja1000,
245         },
246         {
247                 .name = "mscan",
248                 .tseg1_min = 4,
249                 .tseg1_max = 16,
250                 .tseg2_min = 2,
251                 .tseg2_max = 8,
252                 .sjw_max = 4,
253                 .brp_min = 1,
254                 .brp_max = 64,
255                 .brp_inc = 1,
256
257                 .ref_clk = 33000000,
258                 .printf_btr = printf_btr_sja1000,
259         },
260         {
261                 .name = "mscan",
262                 .tseg1_min = 4,
263                 .tseg1_max = 16,
264                 .tseg2_min = 2,
265                 .tseg2_max = 8,
266                 .sjw_max = 4,
267                 .brp_min = 1,
268                 .brp_max = 64,
269                 .brp_inc = 1,
270
271                 .ref_clk = 33300000,
272                 .printf_btr = printf_btr_sja1000,
273         },
274         {
275                 .name = "mscan",
276                 .tseg1_min = 4,
277                 .tseg1_max = 16,
278                 .tseg2_min = 2,
279                 .tseg2_max = 8,
280                 .sjw_max = 4,
281                 .brp_min = 1,
282                 .brp_max = 64,
283                 .brp_inc = 1,
284
285                 .ref_clk = 33333333,
286                 .printf_btr = printf_btr_sja1000,
287         },
288         {
289                 .name = "at91",
290                 .tseg1_min = 4,
291                 .tseg1_max = 16,
292                 .tseg2_min = 2,
293                 .tseg2_max = 8,
294                 .sjw_max = 4,
295                 .brp_min = 2,
296                 .brp_max = 128,
297                 .brp_inc = 1,
298
299                 .ref_clk = 100000000,
300                 .printf_btr = printf_btr_at91,
301         },
302         {
303                 .name = "at91",
304                 .tseg1_min = 4,
305                 .tseg1_max = 16,
306                 .tseg2_min = 2,
307                 .tseg2_max = 8,
308                 .sjw_max = 4,
309                 .brp_min = 2,
310                 .brp_max = 128,
311                 .brp_inc = 1,
312
313                 /* real world clock as found on the ronetix PM9263 */
314                 .ref_clk = 99532800,
315                 .printf_btr = printf_btr_at91,
316         },
317         {
318                 .name = "flexcan",
319                 .tseg1_min = 4,
320                 .tseg1_max = 16,
321                 .tseg2_min = 2,
322                 .tseg2_max = 8,
323                 .sjw_max = 4,
324                 .brp_min = 1,
325                 .brp_max = 256,
326                 .brp_inc = 1,
327
328                 .ref_clk = 49875000,
329                 .printf_btr = printf_btr_flexcan,
330         },
331         {
332                 .name = "flexcan",
333                 .tseg1_min = 4,
334                 .tseg1_max = 16,
335                 .tseg2_min = 2,
336                 .tseg2_max = 8,
337                 .sjw_max = 4,
338                 .brp_min = 1,
339                 .brp_max = 256,
340                 .brp_inc = 1,
341
342                 .ref_clk = 66500000,
343                 .printf_btr = printf_btr_flexcan,
344         },
345         {
346                 .name = "mcp251x",
347                 .tseg1_min = 3,
348                 .tseg1_max = 16,
349                 .tseg2_min = 2,
350                 .tseg2_max = 8,
351                 .sjw_max = 4,
352                 .brp_min = 1,
353                 .brp_max = 64,
354                 .brp_inc = 1,
355
356                 .ref_clk = 8000000,
357                 .printf_btr = printf_btr_mcp251x,
358         },
359         {
360                 .name = "mcp251x",
361                 .tseg1_min = 3,
362                 .tseg1_max = 16,
363                 .tseg2_min = 2,
364                 .tseg2_max = 8,
365                 .sjw_max = 4,
366                 .brp_min = 1,
367                 .brp_max = 64,
368                 .brp_inc = 1,
369
370                 .ref_clk = 16000000,
371                 .printf_btr = printf_btr_mcp251x,
372         },
373         {
374                 .name = "rtcantl1",
375                 .tseg1_min = 4,
376                 .tseg1_max = 16,
377                 .tseg2_min = 2,
378                 .tseg2_max = 8,
379                 .sjw_max = 4,
380                 .brp_min = 1,
381                 .brp_max = 256,
382                 .brp_inc = 1,
383
384                 .ref_clk = 8000000,
385                 .printf_btr = printf_btr_rtcantl1,
386         },
387 };
388
389 static long common_bitrates[] = {
390         1000000,
391         800000,
392         500000,
393         250000,
394         125000,
395         100000,
396         50000,
397         20000,
398         10000,
399 };
400
401 #define CAN_CALC_MAX_ERROR 50 /* in one-tenth of a percent */
402
403 static int can_update_spt(const struct can_bittiming_const *btc,
404                           int sampl_pt, int tseg, int *tseg1, int *tseg2)
405 {
406         *tseg2 = tseg + 1 - (sampl_pt * (tseg + 1)) / 1000;
407         if (*tseg2 < btc->tseg2_min)
408                 *tseg2 = btc->tseg2_min;
409         if (*tseg2 > btc->tseg2_max)
410                 *tseg2 = btc->tseg2_max;
411         *tseg1 = tseg - *tseg2;
412         if (*tseg1 > btc->tseg1_max) {
413                 *tseg1 = btc->tseg1_max;
414                 *tseg2 = tseg - *tseg1;
415         }
416         return 1000 * (tseg + 1 - *tseg2) / (tseg + 1);
417 }
418
419 static int can_calc_bittiming(struct net_device *dev, struct can_bittiming *bt)
420 {
421         struct can_priv *priv = netdev_priv(dev);
422         const struct can_bittiming_const *btc = priv->bittiming_const;
423         long rate, best_rate = 0;
424         long best_error = 1000000000, error = 0;
425         int best_tseg = 0, best_brp = 0, brp = 0;
426         int tsegall, tseg = 0, tseg1 = 0, tseg2 = 0;
427         int spt_error = 1000, spt = 0, sampl_pt;
428         u64 v64;
429
430         if (!priv->bittiming_const)
431                 return -ENOTSUPP;
432
433         /* Use CIA recommended sample points */
434         if (bt->sample_point) {
435                 sampl_pt = bt->sample_point;
436         } else {
437                 if (bt->bitrate > 800000)
438                         sampl_pt = 750;
439                 else if (bt->bitrate > 500000)
440                         sampl_pt = 800;
441                 else
442                         sampl_pt = 875;
443         }
444
445         /* tseg even = round down, odd = round up */
446         for (tseg = (btc->tseg1_max + btc->tseg2_max) * 2 + 1;
447              tseg >= (btc->tseg1_min + btc->tseg2_min) * 2; tseg--) {
448                 tsegall = 1 + tseg / 2;
449                 /* Compute all possible tseg choices (tseg=tseg1+tseg2) */
450                 brp = priv->clock.freq / (tsegall * bt->bitrate) + tseg % 2;
451                 /* chose brp step which is possible in system */
452                 brp = (brp / btc->brp_inc) * btc->brp_inc;
453                 if ((brp < btc->brp_min) || (brp > btc->brp_max))
454                         continue;
455                 rate = priv->clock.freq / (brp * tsegall);
456                 error = bt->bitrate - rate;
457                 /* tseg brp biterror */
458                 if (error < 0)
459                         error = -error;
460                 if (error > best_error)
461                         continue;
462                 best_error = error;
463                 if (error == 0) {
464                         spt = can_update_spt(btc, sampl_pt, tseg / 2,
465                                              &tseg1, &tseg2);
466                         error = sampl_pt - spt;
467                         if (error < 0)
468                                 error = -error;
469                         if (error > spt_error)
470                                 continue;
471                         spt_error = error;
472                 }
473                 best_tseg = tseg / 2;
474                 best_brp = brp;
475                 best_rate = rate;
476                 if (error == 0)
477                         break;
478         }
479
480         if (best_error) {
481                 /* Error in one-tenth of a percent */
482                 error = (best_error * 1000) / bt->bitrate;
483                 if (error > CAN_CALC_MAX_ERROR) {
484                         dev_err(dev->dev.parent,
485                                 "bitrate error %ld.%ld%% too high\n",
486                                 error / 10, error % 10);
487                         return -EDOM;
488                 } else {
489                         dev_warn(dev->dev.parent, "bitrate error %ld.%ld%%\n",
490                                  error / 10, error % 10);
491                 }
492         }
493
494         /* real sample point */
495         bt->sample_point = can_update_spt(btc, sampl_pt, best_tseg,
496                                           &tseg1, &tseg2);
497
498         v64 = (u64)best_brp * 1000000000UL;
499         do_div(v64, priv->clock.freq);
500         bt->tq = (u32)v64;
501         bt->prop_seg = tseg1 / 2;
502         bt->phase_seg1 = tseg1 - bt->prop_seg;
503         bt->phase_seg2 = tseg2;
504         bt->sjw = 1;
505         bt->brp = best_brp;
506
507         /* real bit-rate */
508         bt->bitrate = priv->clock.freq / (bt->brp * (tseg1 + tseg2 + 1));
509
510         return 0;
511 }
512
513 static __u32 get_cia_sample_point(__u32 bitrate)
514 {
515         __u32 sampl_pt;
516
517         if (bitrate > 800000)
518                 sampl_pt = 750;
519         else if (bitrate > 500000)
520                 sampl_pt = 800;
521         else
522                 sampl_pt = 875;
523
524         return sampl_pt;
525 }
526
527 static void print_bit_timing(const struct can_bittiming_const *btc,
528                              __u32 bitrate, __u32 sample_point, __u32 ref_clk,
529                              int quiet)
530 {
531         struct net_device dev = {
532                 .priv.bittiming_const = btc,
533                 .priv.clock.freq = ref_clk,
534         };
535         struct can_bittiming bt = {
536                 .bitrate = bitrate,
537                 .sample_point = sample_point,
538         };
539         long rate_error, spt_error;
540
541         if (!quiet) {
542                 printf("Bit timing parameters for %s with %.6f MHz ref clock\n"
543                        "nominal                                 real Bitrt   nom  real SampP\n"
544                        "Bitrate TQ[ns] PrS PhS1 PhS2 SJW BRP Bitrate Error SampP SampP Error ",
545                        btc->name,
546                        ref_clk / 1000000.0);
547
548                 btc->printf_btr(&bt, 1);
549                 printf("\n");
550         }
551
552         if (can_calc_bittiming(&dev, &bt)) {
553                 printf("%7d ***bitrate not possible***\n", bitrate);
554                 return;
555         }
556
557         /* get nominal sample point */
558         if (!sample_point)
559                 sample_point = get_cia_sample_point(bitrate);
560
561         rate_error = abs((__s32)(bitrate - bt.bitrate));
562         spt_error = abs((__s32)(sample_point - bt.sample_point));
563
564         printf("%7d "
565                "%6d %3d %4d %4d "
566                "%3d %3d "
567                "%7d %4.1f%% "
568                "%4.1f%% %4.1f%% %4.1f%% ",
569                bitrate,
570                bt.tq, bt.prop_seg, bt.phase_seg1, bt.phase_seg2,
571                bt.sjw, bt.brp,
572
573                bt.bitrate,
574                100.0 * rate_error / bitrate,
575
576                sample_point / 10.0,
577                bt.sample_point / 10.0,
578                100.0 * spt_error / sample_point);
579
580         btc->printf_btr(&bt, 0);
581         printf("\n");
582 }
583
584 static void do_list(void)
585 {
586         unsigned int i;
587
588         for (i = 0; i < ARRAY_SIZE(can_calc_consts); i++)
589                 printf("%s\n", can_calc_consts[i].name);
590 }
591
592 int main(int argc, char *argv[])
593 {
594         __u32 bitrate = 0;
595         __u32 opt_ref_clk = 0, ref_clk;
596         int sampl_pt = 0;
597         int quiet = 0;
598         int list = 0;
599         char *name = NULL;
600         unsigned int i, j;
601         int opt, found = 0;
602
603         const struct can_bittiming_const *btc = NULL;
604
605         while ((opt = getopt(argc, argv, "b:c:lps:")) != -1) {
606                 switch (opt) {
607                 case 'b':
608                         bitrate = atoi(optarg);
609                         break;
610
611                 case 'c':
612                         opt_ref_clk = atoi(optarg);
613                         break;
614
615                 case 'l':
616                         list = 1;
617                         break;
618
619                 case 'q':
620                         quiet = 1;
621                         break;
622
623                 case 's':
624                         sampl_pt = atoi(optarg);
625                         break;
626
627                 default:
628                         print_usage(argv[0]);
629                         break;
630                 }
631         }
632
633         if (argc > optind + 1)
634                 print_usage(argv[0]);
635
636         if (argc == optind + 1)
637                 name = argv[optind];
638
639         if (list) {
640                 do_list();
641                 exit(EXIT_SUCCESS);
642         }
643
644         if (sampl_pt && (sampl_pt >= 1000 || sampl_pt < 100))
645                 print_usage(argv[0]);
646
647         for (i = 0; i < ARRAY_SIZE(can_calc_consts); i++) {
648                 if (name && strcmp(can_calc_consts[i].name, name))
649                         continue;
650
651                 found = 1;
652                 btc = &can_calc_consts[i];
653
654                 if (opt_ref_clk)
655                         ref_clk = opt_ref_clk;
656                 else
657                         ref_clk = btc->ref_clk;
658
659                 if (bitrate) {
660                         print_bit_timing(btc, bitrate, sampl_pt, ref_clk, quiet);
661                 } else {
662                         for (j = 0; j < ARRAY_SIZE(common_bitrates); j++)
663                                 print_bit_timing(btc, common_bitrates[j],
664                                                  sampl_pt, ref_clk, j);
665                 }
666                 printf("\n");
667         }
668
669         if (!found) {
670                 printf("error: unknown CAN controller '%s', try one of these:\n\n", name);
671                 do_list();
672                 exit(EXIT_FAILURE);
673         }
674
675         exit(EXIT_SUCCESS);
676 }