Skip to contentSkip to conversation

can: bittiming: add CAN_KBPS, CAN_MBPS and CAN_MHZ macros

PATCH In review Vincent Mailhol 2 participants 2 messages Mar 6, 2021 – Mar 8, 2021 CAN network drivers

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 can: bittiming: add CAN_KBPS, CAN_MBPS and CAN_MHZ macros

Add three macro to simplify the readibility of big bittiming numbers:
- CAN_KBPS: kilobits per second (one thousand)
- CAN_MBPS: megabits per second (one million)
- CAN_MHZ: megahertz per second (one million)

Example:

	u32 bitrate_max = 8 * CAN_MBPS;
	struct can_clock clock = {.freq = 80 * CAN_MHZ};
instead of:
	u32 bitrate_max = 8000000;
	struct can_clock clock = {.freq = 80000000};

Apply the new macro to driver/net/can/dev/bittiming.c.

---
 drivers/net/can/dev/bittiming.c | 4 ++--
 include/linux/can/bittiming.h   | 7 +++++++
 2 files changed, 9 insertions(+), 2 deletions(-)
drivers/net/can/dev/bittiming.c +2 -2
index 5d6c2f217210..853b0ffb443c 100644
@@ -81,9 +81,9 @@ int can_calc_bittiming(struct net_device *dev, struct can_bittiming *bt,
 	if (bt->sample_point) {
 		sample_point_nominal = bt->sample_point;
 	} else {
-		if (bt->bitrate > 800000)
+		if (bt->bitrate > 800 * CAN_KBPS)
 			sample_point_nominal = 750;
-		else if (bt->bitrate > 500000)
+		else if (bt->bitrate > 500 * CAN_KBPS)
 			sample_point_nominal = 800;
 		else
 			sample_point_nominal = 875;
include/linux/can/bittiming.h +7 -0
index 3c4cad7b52c0..2450b94f535c 100644
@@ -11,6 +11,13 @@
 
 #define CAN_SYNC_SEG 1
 
+/* Kilobits and Megabits per second */
+#define CAN_KBPS 1000UL
+#define CAN_MBPS 1000000UL
+
+/* Megahertz */
+#define CAN_MHZ 1000000UL
+
 /*
  * struct can_tdc - CAN FD Transmission Delay Compensation parameters
  *

base-commit: 69436537f3461627d1ad3beccb19a4dc772cbd6a

Signature · 1 line
2.26.2
#1 lore raw
Quoted 1 line · Vincent Mailhol

Add three macro to simplify the readibility of big bittiming numbers:

readability

Quoted 11 lines

- CAN_KBPS: kilobits per second (one thousand)
- CAN_MBPS: megabits per second (one million)
- CAN_MHZ: megahertz per second (one million)

Example:

	u32 bitrate_max = 8 * CAN_MBPS;
	struct can_clock clock = {.freq = 80 * CAN_MHZ};
instead of:
	u32 bitrate_max = 8000000;
	struct can_clock clock = {.freq = 80000000};
Quoted 3 lines

Apply the new macro to driver/net/can/dev/bittiming.c.

Applied with the typo-fix to linux-can-next/testing.

regards,
Marc

Signature · 4 lines
Pengutronix e.K.                 | Marc Kleine-Budde           |
Embedded Linux                   | https://www.pengutronix.de  |
Vertretung West/Dortmund         | Phone: +49-231-2826-924     |
Amtsgericht Hildesheim, HRA 2686 | Fax:   +49-5121-206917-5555 |