nros platform-cffi
Canonical C ABI for porting the nros platform abstraction
Loading...
Searching...
No Matches
platform_can.h File Reference

Canonical C ABI for the nros platform CAN frame driver. More...

#include <stdint.h>
#include <stddef.h>
#include "nros/platform.h"
Include dependency graph for platform_can.h:

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)

Detailed Description

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.

What the driver does and does not do

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.

The backend

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.

Handle storage

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.

Return values

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.

Macro Definition Documentation

◆ NROS_PLATFORM_CAN_CLASSIC_MAX_LEN

#define NROS_PLATFORM_CAN_CLASSIC_MAX_LEN   8u

Largest payloads, in bytes.

◆ NROS_PLATFORM_CAN_EAGAIN

#define NROS_PLATFORM_CAN_EAGAIN   (-4)

The transmit queue stayed full, or nothing arrived, within timeout_us.

◆ NROS_PLATFORM_CAN_EBUSOFF

#define NROS_PLATFORM_CAN_EBUSOFF   (-5)

The controller is bus-off.

◆ NROS_PLATFORM_CAN_EINVAL

#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).

◆ NROS_PLATFORM_CAN_EIO

#define NROS_PLATFORM_CAN_EIO   (-8)

Any other backend failure.

◆ NROS_PLATFORM_CAN_ENODEV

#define NROS_PLATFORM_CAN_ENODEV   (-7)

open: the named device does not exist or is not ready.

◆ NROS_PLATFORM_CAN_ENOFD

#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).

◆ NROS_PLATFORM_CAN_ENOTOPEN

#define NROS_PLATFORM_CAN_ENOTOPEN   (-2)

The handle is not open.

◆ NROS_PLATFORM_CAN_ENOTSUP

#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).

◆ NROS_PLATFORM_CAN_EXT_ID_MAX

#define NROS_PLATFORM_CAN_EXT_ID_MAX   0x1FFFFFFFu

◆ NROS_PLATFORM_CAN_FD_MAX_LEN

#define NROS_PLATFORM_CAN_FD_MAX_LEN   64u

◆ NROS_PLATFORM_CAN_FLAG_BRS

#define NROS_PLATFORM_CAN_FLAG_BRS   0x04u

FD bit-rate switch. Legal only together with NROS_PLATFORM_CAN_FLAG_FD.

◆ NROS_PLATFORM_CAN_FLAG_EXT

#define NROS_PLATFORM_CAN_FLAG_EXT   0x01u

29-bit (extended) identifier; clear for an 11-bit identifier.

◆ NROS_PLATFORM_CAN_FLAG_FD

#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.

◆ NROS_PLATFORM_CAN_FLAGS_KNOWN

#define NROS_PLATFORM_CAN_FLAGS_KNOWN   0x87u

Every flag bit this ABI defines; any other bit makes a frame invalid.

◆ NROS_PLATFORM_CAN_OK

#define NROS_PLATFORM_CAN_OK   0

Sent, received, opened.

◆ NROS_PLATFORM_CAN_OPAQUE_U64S

#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.

◆ NROS_PLATFORM_CAN_SEND_WAIT_TX

#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.

◆ NROS_PLATFORM_CAN_STD_ID_MAX

#define NROS_PLATFORM_CAN_STD_ID_MAX   0x7FFu

Largest 11-bit and 29-bit identifiers.

◆ NROS_PLATFORM_CAN_STORAGE_FITS

#define NROS_PLATFORM_CAN_STORAGE_FITS ( type)
Value:
_Static_assert(sizeof(type) <= sizeof(nros_platform_can_t) && \
_Alignof(type) <= _Alignof(nros_platform_can_t), \
"NROS_PLATFORM_CAN_OPAQUE_U64S too small for " #type)
Definition platform_can.h:128

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.

Function Documentation

◆ nros_platform_can_close()

void nros_platform_can_close ( nros_platform_can_t * can)

Close the handle. Idempotent; a closed or never-opened handle is a no-op.

◆ nros_platform_can_fd_active()

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.

◆ nros_platform_can_frame_check()

int8_t nros_platform_can_frame_check ( const nros_platform_can_frame_t * frame,
uint8_t fd_active )
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.

◆ nros_platform_can_open()

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.

◆ nros_platform_can_recv()

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.

◆ nros_platform_can_send()

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.