• Vincent Mailhol's avatar
    can: length: refactor frame lengths definition to add size in bits · 80a2fbce
    Vincent Mailhol authored
    Introduce a method to calculate the exact size in bits of a CAN(-FD)
    frame with or without dynamic bitstuffing.
    
    These are all the possible combinations taken into account:
    
      - Classical CAN or CAN-FD
      - Standard or Extended frame format
      - CAN-FD CRC17 or CRC21
      - Include or not intermission
    
    Instead of doing several individual macro definitions, declare the
    can_frame_bits() function-like macro. To this extent, do a full
    refactoring of the length definitions.
    
    In addition add the can_frame_bytes(). This function-like macro
    replaces the existing macro:
    
      - CAN_FRAME_OVERHEAD_SFF: can_frame_bytes(false, false, 0)
      - CAN_FRAME_OVERHEAD_EFF: can_frame_bytes(false, true, 0)
      - CANFD_FRAME_OVERHEAD_SFF: can_frame_bytes(true, false, 0)
      - CANFD_FRAME_OVERHEAD_EFF: can_frame_bytes(true, true, 0)
    
    Function-like macros were chosen over inline functions because they
    can be used to initialize const struct fields.
    
    The different maximum frame lengths (maximum data length, including
    intermission) are as follow:
    
       Frame type				bits	bytes
      -------------------------------------------------------
       Classic CAN SFF no bitstuffing	111	14
       Classic CAN EFF no bitstuffing	131	17
       Classic CAN SFF bitstuffing		135	17
       Classic CAN EFF bitstuffing		160	20
       CAN-FD SFF no bitstuffing		579	73
       CAN-FD EFF no bitstuffing		598	75
       CAN-FD SFF bitstuffing		712	89
       CAN-FD EFF bitstuffing		736	92
    
    The macro CAN_FRAME_LEN_MAX and CANFD_FRAME_LEN_MAX are kept as an
    alias to, respectively, can_frame_bytes(false, true, CAN_MAX_DLEN) and
    can_frame_bytes(true, true, CANFD_MAX_DLEN).
    
    In addition to the above:
    
     - Use ISO 11898-1:2015 definitions for the names of the CAN frame
       fields.
     - Include linux/bits.h for use of BITS_PER_BYTE.
     - Include linux/math.h for use of mult_frac() and
       DIV_ROUND_UP(). N.B: the use of DIV_ROUND_UP() is not new to this
       patch, but the include was previously omitted.
     - Add copyright 2023 for myself.
    Suggested-by: default avatarThomas Kopp <Thomas.Kopp@microchip.com>
    Signed-off-by: default avatarVincent Mailhol <mailhol.vincent@wanadoo.fr>
    Reviewed-by: default avatarThomas Kopp <Thomas.Kopp@microchip.com>
    Link: https://lore.kernel.org/all/20230611025728.450837-4-mailhol.vincent@wanadoo.frSigned-off-by: default avatarMarc Kleine-Budde <mkl@pengutronix.de>
    80a2fbce
length.c 2.5 KB