|
nros platform-cffi
Canonical C ABI for porting the nros platform abstraction
|
Canonical C ABI for the nros platform CAN frame driver. More...

Go to the source code of this file.
Data Structures | |
| struct | nros_platform_can_frame_t |
| struct | nros_platform_can_t |
Macros | |
| #define | NROS_PLATFORM_CAN_CLASSIC_MAX_LEN 8u |
| #define | NROS_PLATFORM_CAN_EAGAIN (-4) |
| #define | NROS_PLATFORM_CAN_EBUSOFF (-5) |
| #define | NROS_PLATFORM_CAN_EINVAL (-1) |
| #define | NROS_PLATFORM_CAN_EIO (-8) |
| #define | NROS_PLATFORM_CAN_ENODEV (-7) |
| #define | NROS_PLATFORM_CAN_ENOFD (-3) |
| #define | NROS_PLATFORM_CAN_ENOTOPEN (-2) |
| #define | NROS_PLATFORM_CAN_ENOTSUP (-6) |
| #define | NROS_PLATFORM_CAN_EXT_ID_MAX 0x1FFFFFFFu |
| #define | NROS_PLATFORM_CAN_FD_MAX_LEN 64u |
| #define | NROS_PLATFORM_CAN_FLAG_BRS 0x04u |
| #define | NROS_PLATFORM_CAN_FLAG_EXT 0x01u |
| #define | NROS_PLATFORM_CAN_FLAG_FD 0x02u |
| #define | NROS_PLATFORM_CAN_FLAGS_KNOWN 0x87u |
| #define | NROS_PLATFORM_CAN_OK 0 |
| #define | NROS_PLATFORM_CAN_OPAQUE_U64S 16u |
| #define | NROS_PLATFORM_CAN_SEND_WAIT_TX 0x80u |
| #define | NROS_PLATFORM_CAN_STD_ID_MAX 0x7FFu |
| #define | NROS_PLATFORM_CAN_STORAGE_FITS(type) |
Functions | |
| void | nros_platform_can_close (nros_platform_can_t *can) |
| uint8_t | nros_platform_can_fd_active (const nros_platform_can_t *can) |
| static int8_t | nros_platform_can_frame_check (const nros_platform_can_frame_t *frame, uint8_t fd_active) |
| int8_t | nros_platform_can_open (nros_platform_can_t *can, const char *device, uint8_t want_fd) |
| int8_t | nros_platform_can_recv (nros_platform_can_t *can, nros_platform_can_frame_t *frame, uint32_t timeout_us) |
| int8_t | nros_platform_can_send (nros_platform_can_t *can, const nros_platform_can_frame_t *frame, uint32_t timeout_us) |
Canonical C ABI for the nros platform CAN frame driver.
Companion to <nros/platform.h>. An application that sends its own CAN or CAN FD frames (an actuator command, a heartbeat) sends them through these five calls. Carved into a separate header because a board with no CAN never includes it.
The driver opens a device that the board already brought up and moves frames on it. It sets no bitrate, starts no controller and states no layer-1 fact. Frame ids, payload layout, classic or FD, and any end-to-end protection belong to the application.
Exactly one backend defines these symbols in an image, and it follows from the board: Zephyr's CAN API on a Zephyr board, SocketCAN on a Linux-process board, the board package's own driver on a "board"-stack board, or the host-only mock where the image selects it.
nros_platform_can_t is caller storage of a size this header fixes. A backend keeps its state inside it and proves at compile time that the state fits (NROS_PLATFORM_CAN_STORAGE_FITS). Zero-initialise a handle before its first open; close returns it to the closed state. An open handle must not move: a backend may keep a kernel object (a semaphore, a message queue) inside it.
0 (NROS_PLATFORM_CAN_OK) on success, a negative NROS_PLATFORM_CAN_E* code otherwise. timeout_us = 0 never blocks. send returning 0 means the controller accepted the frame within timeout_us; with NROS_PLATFORM_CAN_SEND_WAIT_TX set in the frame's flags it means the frame left the controller. A backend that cannot report completion returns NROS_PLATFORM_CAN_ENOTSUP for that flag and never downgrades it to acceptance.
| #define NROS_PLATFORM_CAN_CLASSIC_MAX_LEN 8u |
Largest payloads, in bytes.
| #define NROS_PLATFORM_CAN_EAGAIN (-4) |
The transmit queue stayed full, or nothing arrived, within timeout_us.
| #define NROS_PLATFORM_CAN_EBUSOFF (-5) |
The controller is bus-off.
| #define NROS_PLATFORM_CAN_EINVAL (-1) |
Invalid frame: identifier out of range for its width, a length that is not a valid classic or FD size, an unknown flag, or BRS without FD. Also an invalid argument (NULL).
| #define NROS_PLATFORM_CAN_EIO (-8) |
Any other backend failure.
| #define NROS_PLATFORM_CAN_ENODEV (-7) |
open: the named device does not exist or is not ready.
| #define NROS_PLATFORM_CAN_ENOFD (-3) |
CAN FD is not active: an FD frame sent on a handle whose fd_active is false, or open with want_fd on a device a backend can tell at open does not carry FD (SocketCAN reads the interface MTU).
| #define NROS_PLATFORM_CAN_ENOTOPEN (-2) |
The handle is not open.
| #define NROS_PLATFORM_CAN_ENOTSUP (-6) |
The backend does not provide this call or flag (recv, NROS_PLATFORM_CAN_SEND_WAIT_TX, or a board with no CAN backend).
| #define NROS_PLATFORM_CAN_EXT_ID_MAX 0x1FFFFFFFu |
| #define NROS_PLATFORM_CAN_FD_MAX_LEN 64u |
| #define NROS_PLATFORM_CAN_FLAG_BRS 0x04u |
FD bit-rate switch. Legal only together with NROS_PLATFORM_CAN_FLAG_FD.
| #define NROS_PLATFORM_CAN_FLAG_EXT 0x01u |
29-bit (extended) identifier; clear for an 11-bit identifier.
| #define NROS_PLATFORM_CAN_FLAG_FD 0x02u |
A CAN FD frame. Legal only on a handle whose fd_active is true; on any other send returns NROS_PLATFORM_CAN_ENOFD.
| #define NROS_PLATFORM_CAN_FLAGS_KNOWN 0x87u |
Every flag bit this ABI defines; any other bit makes a frame invalid.
| #define NROS_PLATFORM_CAN_OK 0 |
Sent, received, opened.
| #define NROS_PLATFORM_CAN_OPAQUE_U64S 16u |
Handle storage in uint64_t words. ABI: changing it is a header edit, a scripts/gen-abi-bindings.sh run and a commit of both. Measured state per backend (bytes, x86_64): mock 8, socketcan 12, zephyr 24.
| #define NROS_PLATFORM_CAN_SEND_WAIT_TX 0x80u |
send only: return once the frame left the controller, not when it was accepted. NROS_PLATFORM_CAN_ENOTSUP where the backend cannot tell.
| #define NROS_PLATFORM_CAN_STD_ID_MAX 0x7FFu |
Largest 11-bit and 29-bit identifiers.
| #define NROS_PLATFORM_CAN_STORAGE_FITS | ( | type | ) |
One line in a backend (in-tree or a board package's) that fails the build when its state outgrows the handle, naming the constant to raise.
| void nros_platform_can_close | ( | nros_platform_can_t * | can | ) |
Close the handle. Idempotent; a closed or never-opened handle is a no-op.
| uint8_t nros_platform_can_fd_active | ( | const nros_platform_can_t * | can | ) |
Non-zero when the handle was opened with want_fd and the device carries CAN FD; zero otherwise, a closed handle included.
|
inlinestatic |
NROS_PLATFORM_CAN_OK when frame is valid to send on a handle whose FD is active (fd_active non-zero) or not; NROS_PLATFORM_CAN_ENOFD for an FD frame on a handle whose FD is not active; NROS_PLATFORM_CAN_EINVAL otherwise. One definition, so every backend refuses the same frames.
| int8_t nros_platform_can_open | ( | nros_platform_can_t * | can, |
| const char * | device, | ||
| uint8_t | want_fd ) |
Open device into the caller's handle storage. device NULL opens the image's device (the one its board stack names); a non-NULL name opens that device instead. want_fd non-zero asks for CAN FD: afterwards nros_platform_can_fd_active says whether FD is active. A backend that can tell at open that the device does not carry FD (SocketCAN: the interface MTU) fails here with NROS_PLATFORM_CAN_ENOFD; one that learns it from the device's mode (Zephyr: the controller's CAN_MODE_FD, set at bring-up) opens and reports it through fd_active. Either way an FD send on a handle whose FD is not active returns NROS_PLATFORM_CAN_ENOFD.
| int8_t nros_platform_can_recv | ( | nros_platform_can_t * | can, |
| nros_platform_can_frame_t * | frame, | ||
| uint32_t | timeout_us ) |
Receive one frame, waiting at most timeout_us (0: poll). NROS_PLATFORM_CAN_EAGAIN when nothing arrived; NROS_PLATFORM_CAN_ENOTSUP on a backend without receive.
| int8_t nros_platform_can_send | ( | nros_platform_can_t * | can, |
| const nros_platform_can_frame_t * | frame, | ||
| uint32_t | timeout_us ) |
Send one frame, waiting at most timeout_us for the controller to take it (0: do not wait). See the file comment for what 0 means.