nuttx/can: add message alignment

This adds ability for read and write operations to work with messages
aligned to configured number of bytes. This has few different use
cases.

The alignment is specified as unsigned integer and can be changed with
ioctl command CANIOC_SET_MSGALIGN. The current value can be queried by
CANIOC_GET_MSGALIGN command.

The default value for the message alignment is 1. This will provide
behavior consistent with current one. Thus messages are placed to the
buffer right after data of the previous one. The same applies for
writes.

The special alignment value 0 disables read and write of multiple frames. Thus
read will always return at most one message and write will always write
only one message even if larger buffer size is provided.

Another use case is if message alignment is set to exactly message
representation size (`sizeof(struct can_msg_s)`). This allows writing
and reading arrays of messages.

Other values provide even more advanced and specialized use cases, such
as optimizations if architecture has to emulate some non-aligned
accesses, there alignment of for example 4 bytes could provide
performance boost.

The original motivation behind this is was compatibility with socket
CAN. It is easier to port applications to NuttX's CAN driver if only one
frame is provided at the time. This solution was suggested by Pavel Pisa
<pisa@fel.cvut.cz> as a more versatile variant of plain boolean
disabling the multiple frame retrieval.

Signed-off-by: Karel Kočí <kkoci@elektroline.cz>
This commit is contained in:
Karel Kočí
2025-11-04 14:38:48 +01:00
committed by Xiang Xiao
parent 8230c44517
commit c61d7c7e8d
4 changed files with 179 additions and 44 deletions

View File

@@ -305,6 +305,31 @@
* is returned with the errno variable set to indicate the
* nature of the error.
* Dependencies: None
*
* CANIOC_SET_MSGALIGN
* Description: Set messages alignment. Read and written messages can be
* configured to be aligned to multiple of given bytes by
* this. The default value is 1. The alignment affects both
* read and write operation. The value 0 has a special
* meaning where both write will always write only a single
* message and read will always provide only a single
* message.
*
* Argument: A pointer to an unsigned int type with alignment value.
* returned Value: Zero (OK) is returned on success. Otherwise -1 (ERROR)
* is returned with the errno variable set to indicate the
* nature of the error.
* Dependencies: None
*
* CANIOC_GET_MSGALIGN
* Description: Get messages alignment. See CANIOC_SET_MSGALIGN for
* explanation.
*
* Argument: A pointer to an unsigned int type for alignment value.
* returned Value: Zero (OK) is returned on success. Otherwise -1 (ERROR)
* is returned with the errno variable set to indicate the
* nature of the error.
* Dependencies: None
*/
#define CANIOC_RTR _CANIOC(1)
@@ -326,9 +351,11 @@
#define CANIOC_GET_STATE _CANIOC(17)
#define CANIOC_SET_TRANSVSTATE _CANIOC(18)
#define CANIOC_GET_TRANSVSTATE _CANIOC(19)
#define CANIOC_SET_MSGALIGN _CANIOC(20)
#define CANIOC_GET_MSGALIGN _CANIOC(21)
#define CAN_FIRST 0x0001 /* First common command */
#define CAN_NCMDS 19 /* 20 common commands */
#define CAN_NCMDS 21 /* 21 common commands */
/* User defined ioctl commands are also supported. These will be forwarded
* by the upper-half CAN driver to the lower-half CAN driver via the
@@ -796,13 +823,14 @@ struct can_ops_s
*
* The elements of 'cd_ops', and 'cd_priv'
*
* The common logic will initialize all semaphores.
* The common logic will initialize all semaphores and set 'msgalign' to '1'.
*/
struct can_reader_s
{
struct list_node list;
struct can_rxfifo_s fifo; /* Describes receive FIFO */
unsigned int msgalign;
FAR struct pollfd *cd_fds;
};