Skip to content

Class hal::can

ClassList > hal > can

Controller Area Network (CAN bus) hardware abstraction interface.

  • #include <can.hpp>

Inherited by the following classes: hal::lpc40::can, hal::mock::can

Classes

Type Name
struct message_t
A CAN message.
struct send_t
Feedback from sending data over the CAN BUS.
struct settings
Generic settings for a can peripheral.

Public Types

Type Name
typedef void(const message_t &p_message) handler
Receive handler for can messages.
typedef uint32_t id_t
Can message ID type trait.

Public Functions

Type Name
status bus_on ()
Transition the CAN device from "bus-off" to "bus-on".
status configure (const settings & p_settings)
Configure this can bus port to match the settings supplied.
void on_receive (hal::callback< handler > p_handler)
Set the message reception handler.
result< send_t > send (const message_t & p_message)
Send a can message.
virtual ~can () = default

Public Types Documentation

typedef handler

using hal::can::handler =  void(const message_t& p_message);

typedef id_t

using hal::can::id_t =  uint32_t;

Public Functions Documentation

function bus_on

Transition the CAN device from "bus-off" to "bus-on".

inline status hal::can::bus_on () 

WARNING: Calling this function when the device is already in "bus-on" will have no effect. This function is not necessary to call after creating the CAN driver as the driver should already be "bus-on" on creation.

Can devices have two counters to determine system health. These two counters are the "transmit error counter" and the "receive error counter". Transmission errors can occur when the device attempts to communicate on the bus and either does not get an acknowledge or sees an unexpected or erroneous signal on the bus during its own transmission. When transmission errors reach 255 counts, the device will go into the "bus-off" state.

In the "bus-off" state, the CAN peripheral can no longer communicate on the bus. Any calls to send() will throw the error std::errc::network_down. If this occurs, this function must be called to re-enable bus communication.

Returns:

status - success or failure. In the case this function fails repeatedly, it is advised to simply not use the bus anymore as something is critical wrong and may not be recoverable.

function configure

Configure this can bus port to match the settings supplied.

inline status hal::can::configure (
    const settings & p_settings
) 

Parameters:

  • p_settings - settings to apply to can driver

Returns:

status - success or failure

Exception:

  • std::errc::invalid_argument if the settings could not be achieved.

function on_receive

Set the message reception handler.

inline void hal::can::on_receive (
    hal::callback < handler > p_handler
) 

All messages received before a message handler is installed are dropped.

Parameters:

  • p_handler - this handler will be called when a message has been received.

function send

Send a can message.

inline result< send_t > hal::can::send (
    const message_t & p_message
) 

Parameters:

  • p_message - the message to be sent

Returns:

result<send_t> - success or failure

Exception:

  • std::errc::network_down - if the can device is in the "bus-off" state. This can happen if a critical fault in the bus has occurred. A call to bus_on() will need to be issued to attempt to talk on the bus again. See bus_on() for more details.

function ~can

virtual hal::can::~can () = default

The documentation for this class was generated from the following file libraries/include/libhal/can.hpp