linux-can.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Vincent Mailhol <mailhol.vincent@wanadoo.fr>
To: Marc Kleine-Budde <mkl@pengutronix.de>,
	linux-can@vger.kernel.org, Thomas.Kopp@microchip.com
Cc: Oliver Hartkopp <socketcan@hartkopp.net>,
	netdev@vger.kernel.org, marex@denx.de,
	Simon Horman <simon.horman@corigine.com>,
	linux-kernel@vger.kernel.org,
	Vincent Mailhol <mailhol.vincent@wanadoo.fr>
Subject: [PATCH v4 1/3] can: length: fix bitstuffing count
Date: Fri,  2 Jun 2023 01:56:23 +0900	[thread overview]
Message-ID: <20230601165625.100040-2-mailhol.vincent@wanadoo.fr> (raw)
In-Reply-To: <20230601165625.100040-1-mailhol.vincent@wanadoo.fr>

The Stuff Bit Count is always coded on 4 bits (ref [1]). Update the
Stuff Bit Count size accordingly.

In addition, the CRC fields of CAN FD Frames contain stuff bits at
fixed positions called fixed stuff bits [2]. The CRC field starts with
a fixed stuff bit and then has another fixed stuff bit after each
fourth bit [2], which allow us to derive this formula:

  FSB count = 1 + round_down(len(CRC field)/4)

The length of the CRC field is [1]:

  len(CRC field) = len(Stuff Bit Count) + len(CRC)
                 = 4 + len(CRC)

with len(CRC) either 17 or 21 bits depending of the payload length.

In conclusion, for CRC17:

  FSB count = 1 + round_down((4 + 17)/4)
            = 6

and for CRC 21:

  FSB count = 1 + round_down((4 + 21)/4)
            = 7

Add a Fixed Stuff bits (FSB) field with above values and update
CANFD_FRAME_OVERHEAD_SFF and CANFD_FRAME_OVERHEAD_EFF accordingly.

[1] ISO 11898-1:2015 section 10.4.2.6 "CRC field":

  The CRC field shall contain the CRC sequence followed by a recessive
  CRC delimiter. For FD Frames, the CRC field shall also contain the
  stuff count.

  Stuff count

  If FD Frames, the stuff count shall be at the beginning of the CRC
  field. It shall consist of the stuff bit count modulo 8 in a 3-bit
  gray code followed by a parity bit [...]

[2] ISO 11898-1:2015 paragraph 10.5 "Frame coding":

  In the CRC field of FD Frames, the stuff bits shall be inserted at
  fixed positions; they are called fixed stuff bits. There shall be a
  fixed stuff bit before the first bit of the stuff count, even if the
  last bits of the preceding field are a sequence of five consecutive
  bits of identical value, there shall be only the fixed stuff bit,
  there shall not be two consecutive stuff bits. A further fixed stuff
  bit shall be inserted after each fourth bit of the CRC field [...]

Fixes: 85d99c3e2a13 ("can: length: can_skb_get_frame_len(): introduce function to get data length of frame in data link layer")
Suggested-by: Thomas Kopp <Thomas.Kopp@microchip.com>
Signed-off-by: Vincent Mailhol <mailhol.vincent@wanadoo.fr>
Reviewed-by: Thomas Kopp <Thomas.Kopp@microchip.com>
---
 include/linux/can/length.h | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)

diff --git a/include/linux/can/length.h b/include/linux/can/length.h
index 69336549d24f..b8c12c83bc51 100644
--- a/include/linux/can/length.h
+++ b/include/linux/can/length.h
@@ -72,17 +72,18 @@
  * Error Status Indicator (ESI)		1
  * Data length code (DLC)		4
  * Data field				0...512
- * Stuff Bit Count (SBC)		0...16: 4 20...64:5
+ * Stuff Bit Count (SBC)		4
  * CRC					0...16: 17 20...64:21
  * CRC delimiter (CD)			1
+ * Fixed Stuff bits (FSB)		0...16: 6 20...64:7
  * ACK slot (AS)			1
  * ACK delimiter (AD)			1
  * End-of-frame (EOF)			7
  * Inter frame spacing			3
  *
- * assuming CRC21, rounded up and ignoring bitstuffing
+ * assuming CRC21, rounded up and ignoring dynamic bitstuffing
  */
-#define CANFD_FRAME_OVERHEAD_SFF DIV_ROUND_UP(61, 8)
+#define CANFD_FRAME_OVERHEAD_SFF DIV_ROUND_UP(67, 8)
 
 /*
  * Size of a CAN-FD Extended Frame
@@ -101,17 +102,18 @@
  * Error Status Indicator (ESI)		1
  * Data length code (DLC)		4
  * Data field				0...512
- * Stuff Bit Count (SBC)		0...16: 4 20...64:5
+ * Stuff Bit Count (SBC)		4
  * CRC					0...16: 17 20...64:21
  * CRC delimiter (CD)			1
+ * Fixed Stuff bits (FSB)		0...16: 6 20...64:7
  * ACK slot (AS)			1
  * ACK delimiter (AD)			1
  * End-of-frame (EOF)			7
  * Inter frame spacing			3
  *
- * assuming CRC21, rounded up and ignoring bitstuffing
+ * assuming CRC21, rounded up and ignoring dynamic bitstuffing
  */
-#define CANFD_FRAME_OVERHEAD_EFF DIV_ROUND_UP(80, 8)
+#define CANFD_FRAME_OVERHEAD_EFF DIV_ROUND_UP(86, 8)
 
 /*
  * Maximum size of a Classical CAN frame
-- 
2.39.3


  reply	other threads:[~2023-06-01 16:57 UTC|newest]

Thread overview: 36+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-05-07 15:55 [PATCH] can: length: add definitions for frame lengths in bits Vincent Mailhol
2023-05-08  8:54 ` Thomas.Kopp
2023-05-08 12:14   ` Marc Kleine-Budde
2023-05-09  4:16     ` Vincent MAILHOL
2023-05-09  6:43       ` Marc Kleine-Budde
2023-05-09  8:14         ` Vincent MAILHOL
2023-05-09  6:19     ` Thomas.Kopp
2023-05-08 12:20 ` Marc Kleine-Budde
2023-05-09  4:58   ` Vincent MAILHOL
2023-05-09  7:12     ` Thomas.Kopp
2023-05-09  8:06       ` Vincent MAILHOL
2023-05-23  6:52 ` [PATCH v2 0/3] can: length: fix definitions and add bit length calculation Vincent Mailhol
2023-05-23  6:52   ` [PATCH v2 1/3] can: length: fix bitstuffing count Vincent Mailhol
2023-05-23  6:52   ` [PATCH v2 2/3] can: length: fix description of the RRS field Vincent Mailhol
2023-05-23  6:52   ` [PATCH v2 3/3] can: length: refactor frame lengths definition to add size in bits Vincent Mailhol
2023-05-23  7:13     ` Vincent MAILHOL
2023-05-23 11:14     ` Simon Horman
2023-05-30 14:46 ` [PATCH v3 0/3] can: length: fix definitions and add bit length calculation Vincent Mailhol
2023-05-30 14:46   ` [PATCH v3 1/3] can: length: fix bitstuffing count Vincent Mailhol
2023-05-30 14:46   ` [PATCH v3 2/3] can: length: fix description of the RRS field Vincent Mailhol
2023-05-30 14:46   ` [PATCH v3 3/3] can: length: refactor frame lengths definition to add size in bits Vincent Mailhol
2023-05-30 15:51     ` Simon Horman
2023-05-30 17:29       ` Vincent MAILHOL
2023-05-30 19:49         ` Simon Horman
2023-05-31  9:45           ` Vincent MAILHOL
2023-06-01 10:31     ` Thomas.Kopp
2023-06-01 11:00       ` Vincent MAILHOL
2023-06-01 11:26         ` Thomas.Kopp
2023-06-01 16:56 ` [PATCH v4 0/3] can: length: fix definitions and add bit length calculation Vincent Mailhol
2023-06-01 16:56   ` Vincent Mailhol [this message]
2023-06-01 16:56   ` [PATCH v4 2/3] can: length: fix description of the RRS field Vincent Mailhol
2023-06-01 16:56   ` [PATCH v4 3/3] can: length: refactor frame lengths definition to add size in bits Vincent Mailhol
2023-06-11  2:57 ` [PATCH v5 0/3] can: length: fix definitions and add bit length calculation Vincent Mailhol
2023-06-11  2:57   ` [PATCH v5 1/3] can: length: fix bitstuffing count Vincent Mailhol
2023-06-11  2:57   ` [PATCH v5 2/3] can: length: fix description of the RRS field Vincent Mailhol
2023-06-11  2:57   ` [PATCH v5 3/3] can: length: refactor frame lengths definition to add size in bits Vincent Mailhol

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230601165625.100040-2-mailhol.vincent@wanadoo.fr \
    --to=mailhol.vincent@wanadoo.fr \
    --cc=Thomas.Kopp@microchip.com \
    --cc=linux-can@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=marex@denx.de \
    --cc=mkl@pengutronix.de \
    --cc=netdev@vger.kernel.org \
    --cc=simon.horman@corigine.com \
    --cc=socketcan@hartkopp.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).