CAN controller class, for CAN and CAN FD.
More...
|
| #define | PBL_CAN_STD_ID_MASK 0x7ffU |
| | Largest standard (11-bit) identifier.
|
| |
| #define | PBL_CAN_EXT_ID_MASK 0x1fffffffU |
| | Largest extended (29-bit) identifier.
|
| |
| #define | PBL_CAN_MAX_LEN 8U |
| | Most data bytes of a CAN frame.
|
| |
| #define | PBL_CAN_FD_MAX_LEN 64U |
| | Most data bytes of a CAN FD frame.
|
| |
| #define | PBL_CAN_STATE_DEFINE(sym, _tx_slots) |
| | Define the class state of controller sym, for the PBL_CAN_*_DEFINE() macro of a driver.
|
| |
| #define | PBL_CAN_INIT(sym, _name, _parent, _ops, _limits, _data_limits, _modes, _num_filters) |
| | Initializer for the struct pbl_can of controller sym.
|
| |
|
| typedef void(* | pbl_can_rx_callback_t) (const struct pbl_can *can, const struct pbl_can_frame *frame, void *arg) |
| | Receive callback, called from the controller's interrupt for each frame accepted.
|
| |
| typedef void(* | pbl_can_tx_callback_t) (const struct pbl_can *can, int status, void *arg) |
| | Transmit callback, called from the controller's interrupt once a frame is done.
|
| |
| typedef void(* | pbl_can_state_callback_t) (const struct pbl_can *can, enum pbl_can_bus_state state, struct pbl_can_err_cnt cnt, void *arg) |
| | State callback, called from the controller's interrupt when its state changes.
|
| |
|
| enum | pbl_can_bus_state {
PBL_CAN_STATE_STOPPED
, PBL_CAN_STATE_ERROR_ACTIVE
, PBL_CAN_STATE_ERROR_WARNING
, PBL_CAN_STATE_ERROR_PASSIVE
,
PBL_CAN_STATE_BUS_OFF
} |
| | Fault confinement state. More...
|
| |
| enum | pbl_can_error {
PBL_CAN_ERROR_BIT
, PBL_CAN_ERROR_STUFF
, PBL_CAN_ERROR_FORM
, PBL_CAN_ERROR_ACK
,
PBL_CAN_ERROR_CRC
, PBL_CAN_ERROR_ARBITRATION_LOST
, PBL_CAN_ERROR_RX_OVERRUN
, PBL_CAN_ERROR_COUNT
} |
| | Kinds of errors the controller counts. More...
|
| |
|
| int | pbl_can_dev_init (const struct pbl_device *dev) |
| | Device init of every controller: calls the driver's init.
|
| |
| void | pbl_can_receive (const struct pbl_can *can, const struct pbl_can_frame *frame) |
| | Hand a received frame to the receive callback.
|
| |
| void | pbl_can_tx_done (const struct pbl_can *can, pbl_can_tx_callback_t cb, void *arg, int status) |
| | Complete a frame given to the driver's send().
|
| |
| void | pbl_can_report_state (const struct pbl_can *can, enum pbl_can_bus_state state, struct pbl_can_err_cnt cnt) |
| | Report a state change.
|
| |
| void | pbl_can_report_error (const struct pbl_can *can, enum pbl_can_error error) |
| | Count an error.
|
| |
| int | pbl_can_calc_timing (const struct pbl_can_timing_limits *limits, uint32_t clock, uint32_t bitrate, uint16_t sample_point, struct pbl_can_timing *timing) |
| | Compute the bit timing of one phase.
|
| |
| uint8_t | pbl_can_dlc_to_len (uint8_t dlc) |
| | Get the bytes of a frame of a data length code.
|
| |
| uint8_t | pbl_can_len_to_dlc (uint8_t len) |
| | Get the data length code of a frame of len bytes.
|
| |
| int | pbl_can_configure (const struct pbl_can *can, const struct pbl_can_config *cfg) |
| | Set the bit rates and mode, while stopped.
|
| |
| int | pbl_can_config_get (const struct pbl_can *can, struct pbl_can_config *cfg) |
| | Get the configuration.
|
| |
| int | pbl_can_add_filter (const struct pbl_can *can, const struct pbl_can_filter *filter) |
| | Add an acceptance filter, while stopped.
|
| |
| int | pbl_can_remove_filter (const struct pbl_can *can, int idx) |
| | Remove an acceptance filter, while stopped.
|
| |
| void | pbl_can_set_rx_callback (const struct pbl_can *can, pbl_can_rx_callback_t cb, void *arg) |
| | Set the receive callback.
|
| |
| void | pbl_can_set_state_callback (const struct pbl_can *can, pbl_can_state_callback_t cb, void *arg) |
| | Set the state callback.
|
| |
| int | pbl_can_start (const struct pbl_can *can) |
| | Join the bus, with the configuration and filters given while stopped.
|
| |
| int | pbl_can_stop (const struct pbl_can *can) |
| | Leave the bus.
|
| |
| int | pbl_can_send (const struct pbl_can *can, const struct pbl_can_frame *frame, pbl_timeout_t timeout, pbl_can_tx_callback_t cb, void *arg) |
| | Send a frame.
|
| |
| int | pbl_can_get_state (const struct pbl_can *can, enum pbl_can_bus_state *state, struct pbl_can_err_cnt *cnt) |
| | Get the state and error counters.
|
| |
| int | pbl_can_recover (const struct pbl_can *can) |
| | Start the recovery from bus off, with PBL_CAN_MODE_MANUAL_RECOVERY.
|
| |
| int | pbl_can_get_stats (const struct pbl_can *can, struct pbl_can_stats *stats) |
| | Get the error counts.
|
| |
CAN controller class, for CAN and CAN FD.
A CAN controller is a struct pbl_can device. It starts stopped: it is given its bit rates and mode with pbl_can_configure(), and its acceptance filters with pbl_can_add_filter(), while stopped, then joins the bus with pbl_can_start(). Received frames are handed to a callback from the controller's interrupt, and frames are sent with pbl_can_send(), either waiting for the transmission or with a callback for its outcome:
}
const struct pbl_can_frame frame = {.
id = 0x123, .len = 2, .data = {0xbe, 0xef}};
uint32_t bitrate
Nominal bit rate, in bits per second.
Definition can.h:114
uint32_t id
Identifier, standard or extended as PBL_CAN_FRAME_IDE says.
Definition can.h:99
Bus configuration.
Definition can.h:112
A CAN or CAN FD frame.
Definition can.h:97
A CAN controller.
Definition can.h:318
void pbl_can_set_rx_callback(const struct pbl_can *can, pbl_can_rx_callback_t cb, void *arg)
Set the receive callback.
int pbl_can_configure(const struct pbl_can *can, const struct pbl_can_config *cfg)
Set the bit rates and mode, while stopped.
int pbl_can_start(const struct pbl_can *can)
Join the bus, with the configuration and filters given while stopped.
int pbl_can_send(const struct pbl_can *can, const struct pbl_can_frame *frame, pbl_timeout_t timeout, pbl_can_tx_callback_t cb, void *arg)
Send a frame.
#define PBL_MSEC(ms)
Timeout in milliseconds, rounded down to whole ticks.
Definition types.h:63
The class computes the bit timing from the bit rates, sample points and the controller's clock, within the limits the driver gives, and only accepts bit rates the clock divides exactly. Without filters, the controller accepts every frame.
Calls return 0 or a negative errno: -ENODEV if the controller is not ready, -ENOTSUP for a mode it does not support, -EBUSY for a call that needs it stopped and -ENETDOWN for one that needs it started.
◆ pbl_can_frame
| Data Fields |
|
union pbl_can_frame.__unnamed6__ |
__unnamed__ |
Data. |
|
uint8_t |
flags |
Frame flags, PBL_CAN_FRAME_*. |
|
uint32_t |
id |
Identifier, standard or extended as PBL_CAN_FRAME_IDE says. |
|
uint8_t |
len |
Data bytes: up to 8, or for CAN FD 12, 16, 20, 24, 32, 48 or 64. |
◆ pbl_can_config
| Data Fields |
|
uint32_t |
bitrate |
Nominal bit rate, in bits per second. |
|
uint32_t |
data_bitrate |
Data phase bit rate of CAN FD frames, in bits per second, with PBL_CAN_MODE_FD. |
|
uint16_t |
data_sample_point |
Data phase sample point, in tenths of a percent, or 0 for the class default. |
|
uint32_t |
mode |
Mode, PBL_CAN_MODE_* flags. |
|
uint16_t |
sample_point |
Nominal sample point, in tenths of a percent, or 0 for the class default. |
◆ pbl_can_filter
Acceptance filter: frames whose identifier matches id in the bits of mask.
| Data Fields |
|
uint8_t |
flags |
Frame flags: PBL_CAN_FRAME_IDE for extended identifiers, or none for standard ones. |
|
uint32_t |
id |
Identifier. |
|
uint32_t |
mask |
Bits of the identifier that have to match. |
◆ pbl_can_timing
Bit timing of one phase, in time quanta.
| Data Fields |
|
uint16_t |
prescaler |
Clock periods per time quantum. |
|
uint16_t |
sjw |
Synchronization jump width. |
|
uint16_t |
tseg1 |
Propagation and phase segment 1. |
|
uint16_t |
tseg2 |
Phase segment 2. |
◆ pbl_can_timing_limits
| struct pbl_can_timing_limits |
Bit timing limits of one phase.
| Data Fields |
|
uint16_t |
prescaler_max |
Largest prescaler. |
|
uint16_t |
prescaler_min |
Smallest prescaler. |
|
uint16_t |
sjw_max |
Largest synchronization jump width. |
|
uint16_t |
tseg1_max |
Longest propagation and phase segment 1. |
|
uint16_t |
tseg1_min |
Shortest propagation and phase segment 1. |
|
uint16_t |
tseg2_max |
Longest phase segment 2. |
|
uint16_t |
tseg2_min |
Shortest phase segment 2. |
◆ pbl_can_err_cnt
| Data Fields |
|
uint8_t |
rx |
Receive error counter. |
|
uint8_t |
tx |
Transmit error counter. |
◆ pbl_can_stats
Counts of errors and bus off events, since the controller was initialized.
◆ pbl_can
| Data Fields |
|
const struct pbl_can_timing_limits * |
data_limits |
Data phase bit timing limits, or NULL without CAN FD. |
|
struct pbl_device |
dev |
Device. |
|
const struct pbl_can_timing_limits * |
limits |
Nominal bit timing limits. |
|
uint32_t |
modes |
Supported modes, PBL_CAN_MODE_* flags. |
|
uint8_t |
num_filters |
Acceptance filters. |
|
const struct pbl_can_ops * |
ops |
Driver operations. |
|
struct pbl_can_state * |
state |
Runtime state. |
◆ pbl_can_frame.__unnamed6__
| union pbl_can_frame.__unnamed6__ |
◆ PBL_CAN_EXT_ID_MASK
| #define PBL_CAN_EXT_ID_MASK 0x1fffffffU |
Largest extended (29-bit) identifier.
◆ PBL_CAN_FD_MAX_LEN
| #define PBL_CAN_FD_MAX_LEN 64U |
Most data bytes of a CAN FD frame.
◆ PBL_CAN_FRAME_BRS
| #define PBL_CAN_FRAME_BRS PBL_BIT(3) |
CAN FD frame whose data phase goes at the data bit rate.
◆ PBL_CAN_FRAME_ESI
| #define PBL_CAN_FRAME_ESI PBL_BIT(4) |
CAN FD frame from an error passive node, on reception.
◆ PBL_CAN_FRAME_FDF
| #define PBL_CAN_FRAME_FDF PBL_BIT(2) |
◆ PBL_CAN_FRAME_IDE
| #define PBL_CAN_FRAME_IDE PBL_BIT(0) |
Extended (29-bit) identifier.
◆ PBL_CAN_FRAME_RTR
| #define PBL_CAN_FRAME_RTR PBL_BIT(1) |
◆ PBL_CAN_INIT
| #define PBL_CAN_INIT |
( |
|
sym, |
|
|
|
_name, |
|
|
|
_parent, |
|
|
|
_ops, |
|
|
|
_limits, |
|
|
|
_data_limits, |
|
|
|
_modes, |
|
|
|
_num_filters |
|
) |
| |
Value: { \
.ops = (_ops), \
.state = &sym##_can_state, \
.limits = (_limits), \
.data_limits = (_data_limits), \
.modes = (_modes), \
.num_filters = (_num_filters), \
}
int pbl_can_dev_init(const struct pbl_device *dev)
Device init of every controller: calls the driver's init.
const struct pbl_device_class pbl_can_class
Class of the CAN controllers.
#define PBL_DEVICE_INIT_CLASS(sym, _name, _init, _parent, _cls)
Initializer for the struct pbl_device of device instance sym, in class _cls.
Definition device.h:153
Initializer for the struct pbl_can of controller sym.
- Parameters
-
| sym | Symbol of the controller instance, with its state defined by PBL_CAN_STATE_DEFINE(). |
| _name | Name. |
| _parent | Parent device, or NULL. |
| _ops | Driver operations. |
| _limits | Nominal bit timing limits. |
| _data_limits | Data phase bit timing limits, or NULL without CAN FD. |
| _modes | Supported modes. |
| _num_filters | Acceptance filters. |
◆ PBL_CAN_MAX_LEN
| #define PBL_CAN_MAX_LEN 8U |
Most data bytes of a CAN frame.
◆ PBL_CAN_MODE_FD
Send and receive CAN FD frames, as ISO 11898-1:2015 specifies them.
◆ PBL_CAN_MODE_FD_NON_ISO
| #define PBL_CAN_MODE_FD_NON_ISO PBL_BIT(3) |
With PBL_CAN_MODE_FD, CAN FD frames as first specified, without the stuff count.
◆ PBL_CAN_MODE_LISTEN_ONLY
| #define PBL_CAN_MODE_LISTEN_ONLY PBL_BIT(1) |
Receive only, without acknowledging frames or signalling errors; sending is refused.
◆ PBL_CAN_MODE_LOOPBACK
| #define PBL_CAN_MODE_LOOPBACK PBL_BIT(0) |
Receive the frames sent, without driving the bus.
◆ PBL_CAN_MODE_MANUAL_RECOVERY
| #define PBL_CAN_MODE_MANUAL_RECOVERY PBL_BIT(5) |
◆ PBL_CAN_MODE_ONE_SHOT
| #define PBL_CAN_MODE_ONE_SHOT PBL_BIT(4) |
Send each frame once, without retransmitting it after an error or a lost arbitration.
◆ PBL_CAN_STATE_DEFINE
| #define PBL_CAN_STATE_DEFINE |
( |
|
sym, |
|
|
|
_tx_slots |
|
) |
| |
Value:
static struct pbl_can_state sym##_can_state = { \
}
#define PBL_DEVICE_STATE_DEFINE(sym)
Define the runtime state of device instance sym.
Definition device.h:132
#define PBL_MUTEX_INITIALIZER
Static initializer for an unlocked mutex.
Definition mutex.h:50
#define PBL_SEM_INITIALIZER(init, lim)
Static initializer for a semaphore.
Definition sem.h:56
Define the class state of controller sym, for the PBL_CAN_*_DEFINE() macro of a driver.
- Parameters
-
| sym | Symbol of the controller instance. |
| _tx_slots | Frames the controller can have in flight. |
◆ PBL_CAN_STD_ID_MASK
| #define PBL_CAN_STD_ID_MASK 0x7ffU |
Largest standard (11-bit) identifier.
◆ pbl_can_rx_callback_t
| typedef void(* pbl_can_rx_callback_t) (const struct pbl_can *can, const struct pbl_can_frame *frame, void *arg) |
Receive callback, called from the controller's interrupt for each frame accepted.
- Parameters
-
| can | Controller. |
| frame | Frame, valid during the call. |
| arg | Callback argument. |
◆ pbl_can_state_callback_t
State callback, called from the controller's interrupt when its state changes.
- Parameters
-
| can | Controller. |
| state | New state. |
| cnt | Error counters. |
| arg | Callback argument. |
◆ pbl_can_tx_callback_t
| typedef void(* pbl_can_tx_callback_t) (const struct pbl_can *can, int status, void *arg) |
Transmit callback, called from the controller's interrupt once a frame is done.
- Parameters
-
| can | Controller. |
| status | 0 if the frame was sent, -ECANCELED if it was aborted, -ENETDOWN if the controller stopped or went bus off first, -EIO if it failed with PBL_CAN_MODE_ONE_SHOT. |
| arg | Callback argument. |
◆ pbl_can_bus_state
Fault confinement state.
| Enumerator |
|---|
| PBL_CAN_STATE_STOPPED | Stopped, off the bus.
|
| PBL_CAN_STATE_ERROR_ACTIVE | Error active: both error counters below 96.
|
| PBL_CAN_STATE_ERROR_WARNING | Error warning: an error counter at 96 or more.
|
| PBL_CAN_STATE_ERROR_PASSIVE | Error passive: an error counter above 127.
|
| PBL_CAN_STATE_BUS_OFF | Bus off: the transmit error counter went past 255.
|
◆ pbl_can_error
Kinds of errors the controller counts.
| Enumerator |
|---|
| PBL_CAN_ERROR_BIT | Bit error: the node sent a bit and read back another.
|
| PBL_CAN_ERROR_STUFF | Stuff error: six equal bits in a row.
|
| PBL_CAN_ERROR_FORM | Form error: a fixed-form field with a wrong bit.
|
| PBL_CAN_ERROR_ACK | Acknowledgment error: no node acknowledged a frame sent.
|
| PBL_CAN_ERROR_CRC | CRC error.
|
| PBL_CAN_ERROR_ARBITRATION_LOST | Arbitration lost to another node, not an error on the bus.
|
| PBL_CAN_ERROR_RX_OVERRUN | Frames received with no room left for them, dropped.
|
| PBL_CAN_ERROR_COUNT | Number of kinds of errors.
|
◆ pbl_can_add_filter()
Add an acceptance filter, while stopped.
Once a filter is added, the controller only accepts the frames some filter matches.
- Parameters
-
| can | Controller. |
| filter | Filter. |
- Returns
- Filter index, to remove it with,
-ENOSPC if every filter is in use, or another negative errno.
◆ pbl_can_calc_timing()
Compute the bit timing of one phase.
Picks the smallest prescaler that gives the bit rate exactly and the sample point closest to the one asked, with the synchronization jump width as long as both phase segments allow.
- Parameters
-
| limits | Bit timing limits. |
| clock | Clock the time quanta are counted in, in Hz. |
| bitrate | Bit rate, in bits per second. |
| sample_point | Sample point, in tenths of a percent. |
| [out] | timing | Bit timing. |
- Returns
- 0, or
-EINVAL if no timing within limits gives the bit rate.
◆ pbl_can_config_get()
Get the configuration.
- Parameters
-
| can | Controller. |
| [out] | cfg | Configuration. |
- Returns
- 0,
-EINVAL if the controller was never configured, or another negative errno.
◆ pbl_can_configure()
Set the bit rates and mode, while stopped.
- Parameters
-
| can | Controller. |
| cfg | Configuration. |
- Returns
- 0,
-EINVAL if the controller's clock cannot give a bit rate, or another negative errno.
◆ pbl_can_dev_init()
| int pbl_can_dev_init |
( |
const struct pbl_device * |
dev | ) |
|
Device init of every controller: calls the driver's init.
- Parameters
-
- Returns
- 0 or a negative errno.
◆ pbl_can_dlc_to_len()
| uint8_t pbl_can_dlc_to_len |
( |
uint8_t |
dlc | ) |
|
Get the bytes of a frame of a data length code.
- Parameters
-
| dlc | Data length code, 0 to 15. |
- Returns
- Data bytes, as a CAN FD frame has them.
◆ pbl_can_get_state()
Get the state and error counters.
- Parameters
-
| can | Controller. |
| [out] | state | State. |
| [out] | cnt | Error counters, or NULL. |
- Returns
- 0 or a negative errno.
◆ pbl_can_get_stats()
Get the error counts.
- Parameters
-
| can | Controller. |
| [out] | stats | Error counts. |
- Returns
- 0 or a negative errno.
◆ pbl_can_len_to_dlc()
| uint8_t pbl_can_len_to_dlc |
( |
uint8_t |
len | ) |
|
Get the data length code of a frame of len bytes.
- Parameters
-
| len | Data bytes, one a CAN FD frame can have. |
- Returns
- Data length code.
◆ pbl_can_receive()
Hand a received frame to the receive callback.
Called by the controller driver from its interrupt.
- Parameters
-
| can | Controller. |
| frame | Frame. |
◆ pbl_can_recover()
| int pbl_can_recover |
( |
const struct pbl_can * |
can | ) |
|
Start the recovery from bus off, with PBL_CAN_MODE_MANUAL_RECOVERY.
The controller rejoins the bus once it has seen 128 sequences of 11 recessive bits, and the state callback reports it. Does nothing if the controller is not bus off.
- Parameters
-
- Returns
- 0,
-ENOTSUP without PBL_CAN_MODE_MANUAL_RECOVERY, or another negative errno.
◆ pbl_can_remove_filter()
| int pbl_can_remove_filter |
( |
const struct pbl_can * |
can, |
|
|
int |
idx |
|
) |
| |
Remove an acceptance filter, while stopped.
- Parameters
-
- Returns
- 0 or a negative errno.
◆ pbl_can_report_error()
Count an error.
Called by the controller driver from its interrupt.
- Parameters
-
| can | Controller. |
| error | Kind of error. |
◆ pbl_can_report_state()
Report a state change.
Called by the controller driver from its interrupt.
- Parameters
-
| can | Controller. |
| state | New state. |
| cnt | Error counters. |
◆ pbl_can_send()
Send a frame.
Waits up to timeout for room in the controller. With a callback, returns once the frame is queued, and the callback gets its outcome. Without one, also waits up to timeout for the frame to go, aborting it past then, and returns its outcome.
- Parameters
-
| can | Controller. |
| frame | Frame. |
| timeout | How long to wait, PBL_NO_WAIT from interrupt context. |
| cb | Transmit callback, or NULL to wait for the frame to go. Required from interrupt context. |
| arg | Callback argument. |
- Returns
- 0,
-EPERM with PBL_CAN_MODE_LISTEN_ONLY, -EINVAL for a frame the configuration does not allow, -EAGAIN if the controller had no room in time, -ETIMEDOUT if the frame did not go in time, or the outcome of the frame as given to pbl_can_tx_callback_t.
◆ pbl_can_set_rx_callback()
Set the receive callback.
- Parameters
-
| can | Controller. |
| cb | Receive callback, or NULL to drop the frames received. |
| arg | Callback argument. |
◆ pbl_can_set_state_callback()
Set the state callback.
- Parameters
-
| can | Controller. |
| cb | State callback, or NULL. |
| arg | Callback argument. |
◆ pbl_can_start()
| int pbl_can_start |
( |
const struct pbl_can * |
can | ) |
|
Join the bus, with the configuration and filters given while stopped.
- Parameters
-
- Returns
- 0,
-EINVAL if the controller was never configured, or another negative errno.
◆ pbl_can_stop()
| int pbl_can_stop |
( |
const struct pbl_can * |
can | ) |
|
Leave the bus.
The frames in flight complete with -ENETDOWN.
- Parameters
-
- Returns
- 0 or a negative errno.
◆ pbl_can_tx_done()
Complete a frame given to the driver's send().
Called by the controller driver from its interrupt, or from its stop().
- Parameters
-
| can | Controller. |
| cb | Transmit callback given to send(). |
| arg | Callback argument given to send(). |
| status | 0, -ECANCELED or -ENETDOWN, as for pbl_can_tx_callback_t. |
◆ CAN_BUS
| const struct pbl_can* const CAN_BUS |
|
extern |
The controller of the board's CAN bus connector.
◆ pbl_can_class
Class of the CAN controllers.