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 software is released under the GPL-License.
21 #define do_div(a,b) a = (a) / (b)
23 static void print_usage(char* cmd)
25 printf("Usage: %s [options] [<CAN-contoller-name>]\n"
27 "\t-q : don't print header line\n"
28 "\t-l : list all support CAN controller names\n"
29 "\t-b <bitrate> : bit-rate in bits/sec\n"
30 "\t-s <samp_pt> : sample-point in one-tenth of a percent\n"
31 "\t or 0 for CIA recommended sample points\n"
32 "\t-c <clock> : real CAN system clock in Hz\n",
49 struct can_bittiming_const {
61 void (*printf_btr)(struct can_bittime *bt, int hdr);
64 static void printf_btr_sja1000(struct can_bittime *bt, int hdr)
71 btr0 = ((bt->brp - 1) & 0x3f) | (((bt->sjw - 1) & 0x3) << 6);
72 btr1 = ((bt->prop_seg + bt->phase_seg1 - 1) & 0xf) |
73 (((bt->phase_seg2 - 1) & 0x7) << 4);
74 printf("0x%02x 0x%02x", btr0, btr1);
78 static void printf_btr_at91(struct can_bittime *bt, int hdr)
83 uint32_t br = ((bt->phase_seg2 - 1) |
84 ((bt->phase_seg1 - 1) << 4) |
85 ((bt->prop_seg - 1) << 8) |
86 ((bt->sjw - 1) << 12) |
87 ((bt->brp - 1) << 16));
92 static void printf_btr_mcp2510(struct can_bittime *bt, int hdr)
94 uint8_t cnf1, cnf2, cnf3;
97 printf("CNF1 CNF2 CNF3");
99 cnf1 = ((bt->sjw - 1) << 6) | bt->brp;
100 cnf2 = 0x80 | ((bt->phase_seg1 - 1) << 3) | (bt->prop_seg - 1);
101 cnf3 = bt->phase_seg2 - 1;
102 printf("0x%02x 0x%02x 0x%02x", cnf1, cnf2, cnf3);
106 static void printf_btr_rtcantl1(struct can_bittime *bt, int hdr)
111 printf("__BCR0 __BCR1");
113 bcr1 = ((((bt->prop_seg + bt->phase_seg1 - 1) & 0x0F) << 12) |
114 (((bt->phase_seg2 - 1) & 0x07) << 8) |
115 (((bt->sjw - 1) & 0x03) << 4));
116 bcr0 = ((bt->brp - 1) & 0xFF);
117 printf("0x%04x 0x%04x", bcr0, bcr1);
121 struct can_bittiming_const can_calc_consts[] = {
124 /* Note: only prop_seg + bt->phase_seg1 matters */
126 .phase_seg1_max = 16,
133 .printf_btr = printf_btr_sja1000,
137 /* Note: only prop_seg + bt->phase_seg1 matters */
139 .phase_seg1_max = 16,
146 .printf_btr = printf_btr_sja1000,
160 .printf_btr = printf_btr_at91,
174 .printf_btr = printf_btr_mcp2510,
188 .printf_btr = printf_btr_rtcantl1,
192 static long common_bitrates[] = {
204 static int can_update_spt(const struct can_bittiming_const *btc,
205 int sampl_pt, int tseg, int *tseg1, int *tseg2)
207 *tseg2 = tseg + 1 - (sampl_pt * (tseg + 1)) / 1000;
208 if (*tseg2 < btc->phase_seg2_min)
209 *tseg2 = btc->phase_seg2_min;
210 if (*tseg2 > btc->phase_seg2_max)
211 *tseg2 = btc->phase_seg2_max;
212 *tseg1 = tseg - *tseg2;
213 if (*tseg1 > btc->prop_seg_max + btc->phase_seg1_max) {
214 *tseg1 = btc->prop_seg_max + btc->phase_seg1_max;
215 *tseg2 = tseg - *tseg1;
217 return 1000 * (tseg + 1 - *tseg2) / (tseg + 1);
220 int can_calc_bittiming(struct can_bittime *bt, long bitrate,
221 int sampl_pt, long clock,
222 const struct can_bittiming_const *btc)
224 long best_error = 1000000000, error;
225 int best_tseg = 0, best_brp = 0, brp = 0;
226 int spt_error = 1000, spt = 0;
227 long rate, best_rate = 0;
228 int tseg = 0, tseg1 = 0, tseg2 = 0;
232 /* Use CIA recommended sample points */
233 if (bitrate > 800000)
235 else if (bitrate > 500000)
242 printf("tseg brp bitrate biterror\n");
245 /* tseg even = round down, odd = round up */
246 for (tseg = (btc->prop_seg_max + btc->phase_seg1_max +
247 btc->phase_seg2_max) * 2 + 1;
248 tseg >= (btc->prop_seg_min + btc->phase_seg1_min +
249 btc->phase_seg2_min) * 2; tseg--) {
250 /* Compute all posibilities of tseg choices (tseg=tseg1+tseg2) */
251 brp = clock / ((1 + tseg / 2) * bitrate) + tseg % 2;
252 /* chose brp step which is possible in system */
253 brp = (brp / btc->brp_inc) * btc->brp_inc;
254 if ((brp < btc->brp_min) || (brp > btc->brp_max))
256 rate = clock / (brp * (1 + tseg / 2));
257 error = bitrate - rate;
258 /* tseg brp biterror */
260 printf("%4d %3d %7ld %8ld %03d\n", tseg, brp, rate, error,
261 can_update_spt(btc, sampl_pt, tseg / 2,
266 if (error > best_error)
270 spt = can_update_spt(btc, sampl_pt, tseg / 2,
272 error = sampl_pt - spt;
273 //printf("%d %d %d\n", sampl_pt, error, spt_error);
276 if (error > spt_error)
279 //printf("%d\n", spt_error);
281 //printf("error=%d\n", best_error);
282 best_tseg = tseg / 2;
289 if (best_error && (bitrate / best_error < 10))
292 spt = can_update_spt(btc, sampl_pt, best_tseg,
296 /* sample point < 50% */
297 bt->phase_seg1 = tseg1 / 2;
299 /* keep phase_seg{1,2} equal around the sample point */
300 bt->phase_seg1 = tseg2;
302 bt->prop_seg = tseg1 - bt->phase_seg1;
303 /* Check prop_seg range if necessary */
304 if (btc->prop_seg_min || btc->prop_seg_max) {
305 if (bt->prop_seg < btc->prop_seg_min)
306 bt->prop_seg = btc->prop_seg_min;
307 else if (bt->prop_seg > btc->prop_seg_max)
308 bt->prop_seg = btc->prop_seg_max;
309 bt->phase_seg1 = tseg1 - bt->prop_seg;
311 bt->phase_seg2 = tseg2;
314 bt->error = best_error;
316 v64 = (uint64_t)bt->brp * 1000000000UL;
323 void print_bit_timing(const struct can_bittiming_const *btc,
324 long bitrate, int sampl_pt, long ref_clk, int quiet)
326 struct can_bittime bt;
328 memset(&bt, 0, sizeof(bt));
331 printf("Bit timing parameters for %s using %ldHz\n",
333 printf("Bitrate TQ[ns] PrS PhS1 PhS2 SJW BRP SampP Error ");
334 btc->printf_btr(&bt, 1);
338 if (can_calc_bittiming(&bt, bitrate, sampl_pt, ref_clk, btc)) {
339 printf("%7ld ***bitrate not possible***\n", bitrate);
343 printf("%7ld %6d %3d %4d %4d %3d %3d %2d.%d%% %4.1f%% ",
344 bitrate, bt.tq, bt.prop_seg, bt.phase_seg1,
345 bt.phase_seg2, bt.sjw, bt.brp,
346 bt.sampl_pt / 10, bt.sampl_pt % 10,
347 (double)100 * bt.error / bitrate);
348 btc->printf_btr(&bt, 0);
352 int main(int argc, char *argv[])
355 long ref_clk = 8000000;
362 const struct can_bittiming_const *btc = NULL;
364 while ((opt = getopt(argc, argv, "b:c:lps:")) != -1) {
367 bitrate = atoi(optarg);
371 ref_clk = atoi(optarg);
383 sampl_pt = atoi(optarg);
387 print_usage(argv[0]);
392 if (argc > optind + 1)
393 print_usage(argv[0]);
395 if (argc == optind + 1)
399 for (i = 0; i < sizeof(can_calc_consts) /
400 sizeof(struct can_bittiming_const); i++)
401 printf("%s\n", can_calc_consts[i].name);
405 if (sampl_pt && (sampl_pt >= 1000 || sampl_pt < 100))
406 print_usage(argv[0]);
409 for (i = 0; i < sizeof(can_calc_consts) /
410 sizeof(struct can_bittiming_const); i++) {
411 if (!strcmp(can_calc_consts[i].name, name)) {
412 btc = &can_calc_consts[i];
417 print_usage(argv[0]);
420 btc = &can_calc_consts[0];
424 print_bit_timing(btc, bitrate, sampl_pt, ref_clk, quiet);
426 for (i = 0; i < sizeof(common_bitrates) / sizeof(long); i++)
427 print_bit_timing(btc, common_bitrates[i], sampl_pt,