Skip to content

Commit 8aac4ee

Browse files
Merge pull request #9 from marckleinebudde/can-fd
libsocketcan: add functions to better configure CAN-FD bit timing
2 parents c717cea + 84b9835 commit 8aac4ee

File tree

2 files changed

+117
-0
lines changed

2 files changed

+117
-0
lines changed

include/libsocketcan.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,16 @@ int can_set_canfd_bittiming(const char *name, struct can_bittiming *bt, struct c
4444
int can_set_ctrlmode(const char *name, struct can_ctrlmode *cm);
4545
int can_set_bitrate(const char *name, __u32 bitrate);
4646
int can_set_bitrate_samplepoint(const char *name, __u32 bitrate, __u32 sample_point);
47+
int can_set_canfd_bitrates_samplepoint(const char *name, __u32 bitrate, __u32 sample_point, __u32 dbitrate, __u32 dsample_point);
4748

4849
int can_get_restart_ms(const char *name, __u32 *restart_ms);
4950
int can_get_bittiming(const char *name, struct can_bittiming *bt);
51+
int can_get_data_bittiming(const char *name, struct can_bittiming *dbt);
5052
int can_get_ctrlmode(const char *name, struct can_ctrlmode *cm);
5153
int can_get_state(const char *name, int *state);
5254
int can_get_clock(const char *name, struct can_clock *clock);
5355
int can_get_bittiming_const(const char *name, struct can_bittiming_const *btc);
56+
int can_get_data_bittiming_const(const char *name, struct can_bittiming_const *dbtc);
5457
int can_get_berr_counter(const char *name, struct can_berr_counter *bc);
5558
int can_get_device_stats(const char *name, struct can_device_stats *cds);
5659
int can_get_link_stats(const char *name, struct rtnl_link_stats64 *rls);

src/libsocketcan.c

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@
6666
#define GET_BERR_COUNTER 7
6767
#define GET_XSTATS 8
6868
#define GET_LINK_STATS 9
69+
#define GET_DATA_BITTIMING 10
70+
#define GET_DATA_BITTIMING_CONST 11
6971

7072
struct get_req {
7173
struct nlmsghdr n;
@@ -529,7 +531,26 @@ static int do_get_nl_link(int fd, __u8 acquire, const char *name, void *res)
529531
fprintf(stderr, "no berr_counter data found\n");
530532

531533
break;
534+
case GET_DATA_BITTIMING:
535+
if (can_attr[IFLA_CAN_DATA_BITTIMING]) {
536+
memcpy(res,
537+
RTA_DATA(can_attr[IFLA_CAN_DATA_BITTIMING]),
538+
sizeof(struct can_bittiming));
539+
ret = 0;
540+
} else
541+
fprintf(stderr, "no bittiming data found\n");
542+
543+
break;
544+
case GET_DATA_BITTIMING_CONST:
545+
if (can_attr[IFLA_CAN_DATA_BITTIMING_CONST]) {
546+
memcpy(res,
547+
RTA_DATA(can_attr[IFLA_CAN_DATA_BITTIMING_CONST]),
548+
sizeof(struct can_bittiming_const));
549+
ret = 0;
550+
} else
551+
fprintf(stderr, "no bittiming_const data found\n");
532552

553+
break;
533554
default:
534555
fprintf(stderr, "unknown acquire mode\n");
535556
}
@@ -1034,6 +1055,42 @@ int can_set_bitrate_samplepoint(const char *name, __u32 bitrate,
10341055
return can_set_bittiming(name, &bt);
10351056
}
10361057

1058+
/**
1059+
* @ingroup extern
1060+
* can_set_canfd_bitrates_samplepoint - setup the bitrate.
1061+
*
1062+
* @param name name of the can device. This is the netdev name, as ifconfig -a shows
1063+
* in your system. usually it contains prefix "can" and the numer of the can
1064+
* line. e.g. "can0"
1065+
* @param bitrate bitrate of the can bus
1066+
* @param sample_point sample point value
1067+
* @param dbitrate dbitrate of the can bus
1068+
* @param dsample_point dsample point value
1069+
*
1070+
* This one is similar to can_set_bitrate, only you can additionally set up the
1071+
* time point for sampling (sample point) customly instead of using the
1072+
* CIA recommended value. sample_point can be a value between 0 and 999.
1073+
*
1074+
* @return 0 if success
1075+
* @return -1 if failed
1076+
*/
1077+
int can_set_canfd_bitrates_samplepoint(const char *name, __u32 bitrate,
1078+
__u32 sample_point, __u32 dbitrate, __u32 dsample_point)
1079+
{
1080+
struct can_bittiming bt;
1081+
struct can_bittiming dbt;
1082+
1083+
memset(&bt, 0, sizeof(bt));
1084+
bt.bitrate = bitrate;
1085+
bt.sample_point = sample_point;
1086+
1087+
memset(&dbt, 0, sizeof(dbt));
1088+
dbt.bitrate = dbitrate;
1089+
dbt.sample_point = dsample_point;
1090+
1091+
return can_set_canfd_bittiming(name, &bt, &dbt);
1092+
}
1093+
10371094
/**
10381095
* @ingroup extern
10391096
* can_get_state - get the current state of the device
@@ -1112,6 +1169,27 @@ int can_get_bittiming(const char *name, struct can_bittiming *bt)
11121169
return get_link(name, GET_BITTIMING, bt);
11131170
}
11141171

1172+
/**
1173+
* @ingroup extern
1174+
* can_get_data_bittiming - get the current bittimnig configuration.
1175+
*
1176+
* @param name name of the can device. This is the netdev name, as ifconfig -a shows
1177+
* in your system. usually it contains prefix "can" and the numer of the can
1178+
* line. e.g. "can0"
1179+
* @param dbt pointer to the bittiming struct.
1180+
*
1181+
* This one stores the current bittiming configuration.
1182+
*
1183+
* Please see can_set_bittiming for more information about bit timing.
1184+
*
1185+
* @return 0 if success
1186+
* @return -1 if failed
1187+
*/
1188+
int can_get_data_bittiming(const char *name, struct can_bittiming *dbt)
1189+
{
1190+
return get_link(name, GET_DATA_BITTIMING, dbt);
1191+
}
1192+
11151193
/**
11161194
* @ingroup extern
11171195
* can_get_ctrlmode - get the current control mode.
@@ -1192,6 +1270,42 @@ int can_get_bittiming_const(const char *name, struct can_bittiming_const *btc)
11921270
return get_link(name, GET_BITTIMING_CONST, btc);
11931271
}
11941272

1273+
/**
1274+
* @ingroup extern
1275+
* can_get_data_bittiming_const - get the current bittimnig constant.
1276+
*
1277+
* @param name name of the can device. This is the netdev name, as ifconfig -a shows
1278+
* in your system. usually it contains prefix "can" and the numer of the can
1279+
* line. e.g. "can0"
1280+
* @param dbtc pointer to the bittiming constant struct.
1281+
*
1282+
* This one stores the hardware dependent bittiming constant. The
1283+
* can_bittiming_const struct consists:
1284+
*
1285+
* @code
1286+
* struct can_bittiming_const {
1287+
* char name[16];
1288+
* __u32 tseg1_min;
1289+
* __u32 tseg1_max;
1290+
* __u32 tseg2_min;
1291+
* __u32 tseg2_max;
1292+
* __u32 sjw_max;
1293+
* __u32 brp_min;
1294+
* __u32 brp_max;
1295+
* __u32 brp_inc;
1296+
* };
1297+
* @endcode
1298+
*
1299+
* The information in this struct is used to calculate the bus bit timing
1300+
* automatically.
1301+
*
1302+
* @return 0 if success
1303+
* @return -1 if failed
1304+
*/
1305+
int can_get_data_bittiming_const(const char *name, struct can_bittiming_const *dbtc)
1306+
{
1307+
return get_link(name, GET_BITTIMING_CONST, dbtc);
1308+
}
11951309

11961310
/**
11971311
* @ingroup extern

0 commit comments

Comments
 (0)