can-calc-bit-timing.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722
  1. /* can-calc-bit-timing.c: Calculate CAN bit timing parameters
  2. *
  3. * Copyright (C) 2008 Wolfgang Grandegger <wg@grandegger.com>
  4. * Copyright (C) 2016 Marc Kleine-Budde <mkl@pengutronix.de>
  5. *
  6. * Derived from:
  7. * can_baud.c - CAN baudrate calculation
  8. * Code based on LinCAN sources and H8S2638 project
  9. * Copyright 2004-2006 Pavel Pisa - DCE FELK CVUT cz
  10. * Copyright 2005 Stanislav Marek
  11. * email:pisa@cmp.felk.cvut.cz
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation; either version 2 of the License, or
  16. * any later version.
  17. */
  18. #include <errno.h>
  19. #include <getopt.h>
  20. #include <limits.h>
  21. #include <stdbool.h>
  22. #include <stdint.h>
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include <linux/types.h>
  27. #include <linux/can/netlink.h>
  28. /* imported from kernel */
  29. /**
  30. * abs - return absolute value of an argument
  31. * @x: the value. If it is unsigned type, it is converted to signed type first.
  32. * char is treated as if it was signed (regardless of whether it really is)
  33. * but the macro's return type is preserved as char.
  34. *
  35. * Return: an absolute value of x.
  36. */
  37. #define abs(x) __abs_choose_expr(x, long long, \
  38. __abs_choose_expr(x, long, \
  39. __abs_choose_expr(x, int, \
  40. __abs_choose_expr(x, short, \
  41. __abs_choose_expr(x, char, \
  42. __builtin_choose_expr( \
  43. __builtin_types_compatible_p(typeof(x), char), \
  44. (char)({ signed char __x = (x); __x<0?-__x:__x; }), \
  45. ((void)0)))))))
  46. #define __abs_choose_expr(x, type, other) __builtin_choose_expr( \
  47. __builtin_types_compatible_p(typeof(x), signed type) || \
  48. __builtin_types_compatible_p(typeof(x), unsigned type), \
  49. ({ signed type __x = (x); __x < 0 ? -__x : __x; }), other)
  50. /*
  51. * min()/max()/clamp() macros that also do
  52. * strict type-checking.. See the
  53. * "unnecessary" pointer comparison.
  54. */
  55. #define min(x, y) ({ \
  56. typeof(x) _min1 = (x); \
  57. typeof(y) _min2 = (y); \
  58. (void) (&_min1 == &_min2); \
  59. _min1 < _min2 ? _min1 : _min2; })
  60. #define max(x, y) ({ \
  61. typeof(x) _max1 = (x); \
  62. typeof(y) _max2 = (y); \
  63. (void) (&_max1 == &_max2); \
  64. _max1 > _max2 ? _max1 : _max2; })
  65. /**
  66. * clamp - return a value clamped to a given range with strict typechecking
  67. * @val: current value
  68. * @lo: lowest allowable value
  69. * @hi: highest allowable value
  70. *
  71. * This macro does strict typechecking of lo/hi to make sure they are of the
  72. * same type as val. See the unnecessary pointer comparisons.
  73. */
  74. #define clamp(val, lo, hi) min((typeof(val))max(val, lo), hi)
  75. # define do_div(n,base) ({ \
  76. uint32_t __base = (base); \
  77. uint32_t __rem; \
  78. __rem = ((uint64_t)(n)) % __base; \
  79. (n) = ((uint64_t)(n)) / __base; \
  80. __rem; \
  81. })
  82. /* */
  83. #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
  84. /* we don't want to see these prints */
  85. #define netdev_err(dev, format, arg...) do { } while (0)
  86. #define netdev_warn(dev, format, arg...) do { } while (0)
  87. /* define in-kernel-types */
  88. typedef __u64 u64;
  89. typedef __u32 u32;
  90. struct calc_ref_clk {
  91. __u32 clk; /* CAN system clock frequency in Hz */
  92. char *name;
  93. };
  94. struct calc_bittiming_const {
  95. struct can_bittiming_const bittiming_const;
  96. const struct calc_ref_clk ref_clk[16];
  97. void (*printf_btr)(struct can_bittiming *bt, bool hdr);
  98. };
  99. /*
  100. * minimal structs, just enough to be source level compatible
  101. */
  102. struct can_priv {
  103. struct can_clock clock;
  104. };
  105. struct net_device {
  106. struct can_priv priv;
  107. };
  108. static inline void *netdev_priv(const struct net_device *dev)
  109. {
  110. return (void *)&dev->priv;
  111. }
  112. static void print_usage(char *cmd)
  113. {
  114. printf("Usage: %s [options] [<CAN-contoller-name>]\n"
  115. "\tOptions:\n"
  116. "\t-q : don't print header line\n"
  117. "\t-l : list all support CAN controller names\n"
  118. "\t-b <bitrate> : bit-rate in bits/sec\n"
  119. "\t-s <samp_pt> : sample-point in one-tenth of a percent\n"
  120. "\t or 0 for CIA recommended sample points\n"
  121. "\t-c <clock> : real CAN system clock in Hz\n",
  122. cmd);
  123. exit(EXIT_FAILURE);
  124. }
  125. static void printf_btr_sja1000(struct can_bittiming *bt, bool hdr)
  126. {
  127. uint8_t btr0, btr1;
  128. if (hdr) {
  129. printf("BTR0 BTR1");
  130. } else {
  131. btr0 = ((bt->brp - 1) & 0x3f) | (((bt->sjw - 1) & 0x3) << 6);
  132. btr1 = ((bt->prop_seg + bt->phase_seg1 - 1) & 0xf) |
  133. (((bt->phase_seg2 - 1) & 0x7) << 4);
  134. printf("0x%02x 0x%02x", btr0, btr1);
  135. }
  136. }
  137. static void printf_btr_at91(struct can_bittiming *bt, bool hdr)
  138. {
  139. if (hdr) {
  140. printf("%10s", "CAN_BR");
  141. } else {
  142. uint32_t br = ((bt->phase_seg2 - 1) |
  143. ((bt->phase_seg1 - 1) << 4) |
  144. ((bt->prop_seg - 1) << 8) |
  145. ((bt->sjw - 1) << 12) |
  146. ((bt->brp - 1) << 16));
  147. printf("0x%08x", br);
  148. }
  149. }
  150. static void printf_btr_flexcan(struct can_bittiming *bt, bool hdr)
  151. {
  152. if (hdr) {
  153. printf("%10s", "CAN_CTRL");
  154. } else {
  155. uint32_t ctrl = (((bt->brp - 1) << 24) |
  156. ((bt->sjw - 1) << 22) |
  157. ((bt->phase_seg1 - 1) << 19) |
  158. ((bt->phase_seg2 - 1) << 16) |
  159. ((bt->prop_seg - 1) << 0));
  160. printf("0x%08x", ctrl);
  161. }
  162. }
  163. static void printf_btr_mcp251x(struct can_bittiming *bt, bool hdr)
  164. {
  165. uint8_t cnf1, cnf2, cnf3;
  166. if (hdr) {
  167. printf("CNF1 CNF2 CNF3");
  168. } else {
  169. cnf1 = ((bt->sjw - 1) << 6) | (bt->brp - 1);
  170. cnf2 = 0x80 | ((bt->phase_seg1 - 1) << 3) | (bt->prop_seg - 1);
  171. cnf3 = bt->phase_seg2 - 1;
  172. printf("0x%02x 0x%02x 0x%02x", cnf1, cnf2, cnf3);
  173. }
  174. }
  175. static void printf_btr_ti_hecc(struct can_bittiming *bt, bool hdr)
  176. {
  177. if (hdr) {
  178. printf("%10s", "CANBTC");
  179. } else {
  180. uint32_t can_btc;
  181. can_btc = (bt->phase_seg2 - 1) & 0x7;
  182. can_btc |= ((bt->phase_seg1 + bt->prop_seg - 1)
  183. & 0xF) << 3;
  184. can_btc |= ((bt->sjw - 1) & 0x3) << 8;
  185. can_btc |= ((bt->brp - 1) & 0xFF) << 16;
  186. printf("0x%08x", can_btc);
  187. }
  188. }
  189. #define RCAR_CAN_BCR_TSEG1(x) (((x) & 0x0f) << 20)
  190. #define RCAR_CAN_BCR_BPR(x) (((x) & 0x3ff) << 8)
  191. #define RCAR_CAN_BCR_SJW(x) (((x) & 0x3) << 4)
  192. #define RCAR_CAN_BCR_TSEG2(x) ((x) & 0x07)
  193. static void printf_btr_rcar_can(struct can_bittiming *bt, bool hdr)
  194. {
  195. if (hdr) {
  196. printf("%10s", "CiBCR");
  197. } else {
  198. uint32_t bcr;
  199. bcr = RCAR_CAN_BCR_TSEG1(bt->phase_seg1 + bt->prop_seg - 1) |
  200. RCAR_CAN_BCR_BPR(bt->brp - 1) |
  201. RCAR_CAN_BCR_SJW(bt->sjw - 1) |
  202. RCAR_CAN_BCR_TSEG2(bt->phase_seg2 - 1);
  203. printf("0x%08x", bcr << 8);
  204. }
  205. }
  206. static struct calc_bittiming_const can_calc_consts[] = {
  207. {
  208. .bittiming_const = {
  209. .name = "sja1000",
  210. .tseg1_min = 1,
  211. .tseg1_max = 16,
  212. .tseg2_min = 1,
  213. .tseg2_max = 8,
  214. .sjw_max = 4,
  215. .brp_min = 1,
  216. .brp_max = 64,
  217. .brp_inc = 1,
  218. },
  219. .ref_clk = {
  220. { .clk = 8000000, },
  221. },
  222. .printf_btr = printf_btr_sja1000,
  223. }, {
  224. .bittiming_const = {
  225. .name = "mscan",
  226. .tseg1_min = 4,
  227. .tseg1_max = 16,
  228. .tseg2_min = 2,
  229. .tseg2_max = 8,
  230. .sjw_max = 4,
  231. .brp_min = 1,
  232. .brp_max = 64,
  233. .brp_inc = 1,
  234. },
  235. .ref_clk = {
  236. { .clk = 32000000, },
  237. { .clk = 33000000, },
  238. { .clk = 33300000, },
  239. { .clk = 33333333, },
  240. { .clk = 66660000, .name = "mpc5121", },
  241. { .clk = 66666666, .name = "mpc5121" },
  242. },
  243. .printf_btr = printf_btr_sja1000,
  244. }, {
  245. .bittiming_const = {
  246. .name = "at91",
  247. .tseg1_min = 4,
  248. .tseg1_max = 16,
  249. .tseg2_min = 2,
  250. .tseg2_max = 8,
  251. .sjw_max = 4,
  252. .brp_min = 2,
  253. .brp_max = 128,
  254. .brp_inc = 1,
  255. },
  256. .ref_clk = {
  257. { .clk = 99532800, .name = "ronetix PM9263", },
  258. { .clk = 100000000, },
  259. },
  260. .printf_btr = printf_btr_at91,
  261. }, {
  262. .bittiming_const = {
  263. .name = "flexcan",
  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 = 256,
  271. .brp_inc = 1,
  272. },
  273. .ref_clk = {
  274. { .clk = 24000000, .name = "mx28" },
  275. { .clk = 30000000, .name = "mx6" },
  276. { .clk = 49875000, },
  277. { .clk = 66000000, },
  278. { .clk = 66500000, },
  279. { .clk = 66666666, },
  280. { .clk = 83368421, .name = "vybrid" },
  281. },
  282. .printf_btr = printf_btr_flexcan,
  283. }, {
  284. .bittiming_const = {
  285. .name = "mcp251x",
  286. .tseg1_min = 3,
  287. .tseg1_max = 16,
  288. .tseg2_min = 2,
  289. .tseg2_max = 8,
  290. .sjw_max = 4,
  291. .brp_min = 1,
  292. .brp_max = 64,
  293. .brp_inc = 1,
  294. },
  295. .ref_clk = {
  296. { .clk = 8000000, },
  297. { .clk = 16000000, },
  298. },
  299. .printf_btr = printf_btr_mcp251x,
  300. }, {
  301. .bittiming_const = {
  302. .name = "ti_hecc",
  303. .tseg1_min = 1,
  304. .tseg1_max = 16,
  305. .tseg2_min = 1,
  306. .tseg2_max = 8,
  307. .sjw_max = 4,
  308. .brp_min = 1,
  309. .brp_max = 256,
  310. .brp_inc = 1,
  311. },
  312. .ref_clk = {
  313. { .clk = 13000000, },
  314. },
  315. .printf_btr = printf_btr_ti_hecc,
  316. }, {
  317. .bittiming_const = {
  318. .name = "rcar_can",
  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 = 1024,
  326. .brp_inc = 1,
  327. },
  328. .ref_clk = {
  329. { .clk = 65000000, },
  330. },
  331. .printf_btr = printf_btr_rcar_can,
  332. },
  333. };
  334. static long common_bitrates[] = {
  335. 1000000,
  336. 800000,
  337. 500000,
  338. 250000,
  339. 125000,
  340. 100000,
  341. 50000,
  342. 20000,
  343. 10000,
  344. };
  345. #define CAN_CALC_MAX_ERROR 50 /* in one-tenth of a percent */
  346. #define CAN_CALC_SYNC_SEG 1
  347. /*
  348. * Bit-timing calculation derived from:
  349. *
  350. * Code based on LinCAN sources and H8S2638 project
  351. * Copyright 2004-2006 Pavel Pisa - DCE FELK CVUT cz
  352. * Copyright 2005 Stanislav Marek
  353. * email: pisa@cmp.felk.cvut.cz
  354. *
  355. * Calculates proper bit-timing parameters for a specified bit-rate
  356. * and sample-point, which can then be used to set the bit-timing
  357. * registers of the CAN controller. You can find more information
  358. * in the header file linux/can/netlink.h.
  359. */
  360. static int can_update_spt(const struct can_bittiming_const *btc,
  361. unsigned int spt_nominal, unsigned int tseg,
  362. unsigned int *tseg1_ptr, unsigned int *tseg2_ptr,
  363. unsigned int *spt_error_ptr)
  364. {
  365. unsigned int spt_error, best_spt_error = UINT_MAX;
  366. unsigned int spt, best_spt = 0;
  367. unsigned int tseg1, tseg2;
  368. int i;
  369. for (i = 0; i <= 1; i++) {
  370. tseg2 = tseg + CAN_CALC_SYNC_SEG - (spt_nominal * (tseg + CAN_CALC_SYNC_SEG)) / 1000 - i;
  371. tseg2 = clamp(tseg2, btc->tseg2_min, btc->tseg2_max);
  372. tseg1 = tseg - tseg2;
  373. if (tseg1 > btc->tseg1_max) {
  374. tseg1 = btc->tseg1_max;
  375. tseg2 = tseg - tseg1;
  376. }
  377. spt = 1000 * (tseg + CAN_CALC_SYNC_SEG - tseg2) / (tseg + CAN_CALC_SYNC_SEG);
  378. spt_error = abs(spt_nominal - spt);
  379. if ((spt <= spt_nominal) && (spt_error < best_spt_error)) {
  380. best_spt = spt;
  381. best_spt_error = spt_error;
  382. *tseg1_ptr = tseg1;
  383. *tseg2_ptr = tseg2;
  384. }
  385. }
  386. if (spt_error_ptr)
  387. *spt_error_ptr = best_spt_error;
  388. return best_spt;
  389. }
  390. static int can_calc_bittiming(struct net_device *dev, struct can_bittiming *bt,
  391. const struct can_bittiming_const *btc)
  392. {
  393. struct can_priv *priv = netdev_priv(dev);
  394. unsigned int rate; /* current bitrate */
  395. unsigned int rate_error; /* difference between current and nominal value */
  396. unsigned int best_rate_error = UINT_MAX;
  397. unsigned int spt_error; /* difference between current and nominal value */
  398. unsigned int best_spt_error = UINT_MAX;
  399. unsigned int spt_nominal; /* nominal sample point */
  400. unsigned int best_tseg = 0; /* current best value for tseg */
  401. unsigned int best_brp = 0; /* current best value for brp */
  402. unsigned int brp, tsegall, tseg, tseg1, tseg2;
  403. u64 v64;
  404. /* Use CiA recommended sample points */
  405. if (bt->sample_point) {
  406. spt_nominal = bt->sample_point;
  407. } else {
  408. if (bt->bitrate > 800000)
  409. spt_nominal = 750;
  410. else if (bt->bitrate > 500000)
  411. spt_nominal = 800;
  412. else
  413. spt_nominal = 875;
  414. }
  415. /* tseg even = round down, odd = round up */
  416. for (tseg = (btc->tseg1_max + btc->tseg2_max) * 2 + 1;
  417. tseg >= (btc->tseg1_min + btc->tseg2_min) * 2; tseg--) {
  418. tsegall = CAN_CALC_SYNC_SEG + tseg / 2;
  419. /* Compute all possible tseg choices (tseg=tseg1+tseg2) */
  420. brp = priv->clock.freq / (tsegall * bt->bitrate) + tseg % 2;
  421. /* choose brp step which is possible in system */
  422. brp = (brp / btc->brp_inc) * btc->brp_inc;
  423. if ((brp < btc->brp_min) || (brp > btc->brp_max))
  424. continue;
  425. rate = priv->clock.freq / (brp * tsegall);
  426. rate_error = abs(bt->bitrate - rate);
  427. /* tseg brp biterror */
  428. if (rate_error > best_rate_error)
  429. continue;
  430. /* reset sample point error if we have a better bitrate */
  431. if (rate_error < best_rate_error)
  432. best_spt_error = UINT_MAX;
  433. can_update_spt(btc, spt_nominal, tseg / 2, &tseg1, &tseg2, &spt_error);
  434. if (spt_error > best_spt_error)
  435. continue;
  436. best_spt_error = spt_error;
  437. best_rate_error = rate_error;
  438. best_tseg = tseg / 2;
  439. best_brp = brp;
  440. if (rate_error == 0 && spt_error == 0)
  441. break;
  442. }
  443. if (best_rate_error) {
  444. /* Error in one-tenth of a percent */
  445. rate_error = (best_rate_error * 1000) / bt->bitrate;
  446. if (rate_error > CAN_CALC_MAX_ERROR) {
  447. netdev_err(dev,
  448. "bitrate error %ld.%ld%% too high\n",
  449. rate_error / 10, rate_error % 10);
  450. return -EDOM;
  451. }
  452. netdev_warn(dev, "bitrate error %ld.%ld%%\n",
  453. rate_error / 10, rate_error % 10);
  454. }
  455. /* real sample point */
  456. bt->sample_point = can_update_spt(btc, spt_nominal, best_tseg,
  457. &tseg1, &tseg2, NULL);
  458. v64 = (u64)best_brp * 1000 * 1000 * 1000;
  459. do_div(v64, priv->clock.freq);
  460. bt->tq = (u32)v64;
  461. bt->prop_seg = tseg1 / 2;
  462. bt->phase_seg1 = tseg1 - bt->prop_seg;
  463. bt->phase_seg2 = tseg2;
  464. /* check for sjw user settings */
  465. if (!bt->sjw || !btc->sjw_max) {
  466. bt->sjw = 1;
  467. } else {
  468. /* bt->sjw is at least 1 -> sanitize upper bound to sjw_max */
  469. if (bt->sjw > btc->sjw_max)
  470. bt->sjw = btc->sjw_max;
  471. /* bt->sjw must not be higher than tseg2 */
  472. if (tseg2 < bt->sjw)
  473. bt->sjw = tseg2;
  474. }
  475. bt->brp = best_brp;
  476. /* real bit-rate */
  477. bt->bitrate = priv->clock.freq / (bt->brp * (CAN_CALC_SYNC_SEG + tseg1 + tseg2));
  478. return 0;
  479. }
  480. static __u32 get_cia_sample_point(__u32 bitrate)
  481. {
  482. __u32 sampl_pt;
  483. if (bitrate > 800000)
  484. sampl_pt = 750;
  485. else if (bitrate > 500000)
  486. sampl_pt = 800;
  487. else
  488. sampl_pt = 875;
  489. return sampl_pt;
  490. }
  491. static void print_bit_timing(const struct calc_bittiming_const *btc,
  492. const struct calc_ref_clk *ref_clk,
  493. unsigned int bitrate_nominal,
  494. unsigned int spt_nominal,
  495. bool quiet)
  496. {
  497. struct net_device dev = {
  498. .priv.clock.freq = ref_clk->clk,
  499. };
  500. struct can_bittiming bt = {
  501. .bitrate = bitrate_nominal,
  502. .sample_point = spt_nominal,
  503. };
  504. long rate_error, spt_error;
  505. if (!quiet) {
  506. printf("Bit timing parameters for %s%s%s%s with %.6f MHz ref clock\n"
  507. "nominal real Bitrt nom real SampP\n"
  508. "Bitrate TQ[ns] PrS PhS1 PhS2 SJW BRP Bitrate Error SampP SampP Error ",
  509. btc->bittiming_const.name,
  510. ref_clk->name ? " (" : "",
  511. ref_clk->name ? ref_clk->name : "",
  512. ref_clk->name ? ")" : "",
  513. ref_clk->clk / 1000000.0);
  514. btc->printf_btr(&bt, true);
  515. printf("\n");
  516. }
  517. if (can_calc_bittiming(&dev, &bt, &btc->bittiming_const)) {
  518. printf("%7d ***bitrate not possible***\n", bitrate_nominal);
  519. return;
  520. }
  521. /* get nominal sample point */
  522. if (!spt_nominal)
  523. spt_nominal = get_cia_sample_point(bitrate_nominal);
  524. rate_error = abs(bitrate_nominal - bt.bitrate);
  525. spt_error = abs(spt_nominal - bt.sample_point);
  526. printf("%7d "
  527. "%6d %3d %4d %4d "
  528. "%3d %3d "
  529. "%7d %4.1f%% "
  530. "%4.1f%% %4.1f%% %4.1f%% ",
  531. bitrate_nominal,
  532. bt.tq, bt.prop_seg, bt.phase_seg1, bt.phase_seg2,
  533. bt.sjw, bt.brp,
  534. bt.bitrate,
  535. 100.0 * rate_error / bitrate_nominal,
  536. spt_nominal / 10.0,
  537. bt.sample_point / 10.0,
  538. 100.0 * spt_error / spt_nominal);
  539. btc->printf_btr(&bt, false);
  540. printf("\n");
  541. }
  542. static void do_list(void)
  543. {
  544. unsigned int i;
  545. for (i = 0; i < ARRAY_SIZE(can_calc_consts); i++)
  546. printf("%s\n", can_calc_consts[i].bittiming_const.name);
  547. }
  548. int main(int argc, char *argv[])
  549. {
  550. __u32 bitrate_nominal = 0;
  551. struct calc_ref_clk opt_ref_clk = {
  552. .name = "cmd-line",
  553. };
  554. const struct calc_ref_clk *ref_clk;
  555. unsigned int spt_nominal = 0;
  556. bool quiet = false, list = false, found = false;
  557. char *name = NULL;
  558. unsigned int i, j, k;
  559. int opt;
  560. const struct calc_bittiming_const *btc;
  561. while ((opt = getopt(argc, argv, "b:c:lqs:")) != -1) {
  562. switch (opt) {
  563. case 'b':
  564. bitrate_nominal = atoi(optarg);
  565. break;
  566. case 'c':
  567. opt_ref_clk.clk = strtoul(optarg, NULL, 10);
  568. break;
  569. case 'l':
  570. list = true;
  571. break;
  572. case 'q':
  573. quiet = true;
  574. break;
  575. case 's':
  576. spt_nominal = strtoul(optarg, NULL, 10);
  577. break;
  578. default:
  579. print_usage(argv[0]);
  580. break;
  581. }
  582. }
  583. if (argc > optind + 1)
  584. print_usage(argv[0]);
  585. if (argc == optind + 1)
  586. name = argv[optind];
  587. if (list) {
  588. do_list();
  589. exit(EXIT_SUCCESS);
  590. }
  591. if (spt_nominal && (spt_nominal >= 1000 || spt_nominal < 100))
  592. print_usage(argv[0]);
  593. for (i = 0; i < ARRAY_SIZE(can_calc_consts); i++) {
  594. if (name && strcmp(can_calc_consts[i].bittiming_const.name, name))
  595. continue;
  596. found = true;
  597. btc = &can_calc_consts[i];
  598. for (j = 0; j < ARRAY_SIZE(btc->ref_clk); j++) {
  599. if (opt_ref_clk.clk)
  600. ref_clk = &opt_ref_clk;
  601. else
  602. ref_clk = &btc->ref_clk[j];
  603. if (!ref_clk->clk)
  604. break;
  605. if (bitrate_nominal) {
  606. print_bit_timing(btc, ref_clk, bitrate_nominal,
  607. spt_nominal, quiet);
  608. } else {
  609. for (k = 0; k < ARRAY_SIZE(common_bitrates); k++)
  610. print_bit_timing(btc, ref_clk,
  611. common_bitrates[k],
  612. spt_nominal, k);
  613. }
  614. printf("\n");
  615. if (opt_ref_clk.clk)
  616. break;
  617. }
  618. }
  619. if (!found) {
  620. printf("error: unknown CAN controller '%s', try one of these:\n\n", name);
  621. do_list();
  622. exit(EXIT_FAILURE);
  623. }
  624. exit(EXIT_SUCCESS);
  625. }