This is the description of the C/C++ API bindings for the CAN Bricklet. General information and technical specifications for the CAN Bricklet are summarized in its hardware description.
An installation guide for the C/C++ API bindings is part of their general description.
The example code below is Public Domain (CC0 1.0).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | #include <stdio.h>
#include "ip_connection.h"
#include "bricklet_can.h"
#define HOST "localhost"
#define PORT 4223
#define UID "XYZ" // Change XYZ to the UID of your CAN Bricklet
// Callback function for frame read callback
void cb_frame_read(uint8_t frame_type, uint32_t identifier, uint8_t data[8],
uint8_t length, void *user_data) {
(void)user_data; // avoid unused parameter warning
if(frame_type == CAN_FRAME_TYPE_STANDARD_DATA) {
printf("Frame Type: Standard Data\n");
} else if(frame_type == CAN_FRAME_TYPE_STANDARD_REMOTE) {
printf("Frame Type: Standard Remote\n");
} else if(frame_type == CAN_FRAME_TYPE_EXTENDED_DATA) {
printf("Frame Type: Extended Data\n");
} else if(frame_type == CAN_FRAME_TYPE_EXTENDED_REMOTE) {
printf("Frame Type: Extended Remote\n");
}
printf("Identifier: %u\n", identifier);
printf("Data (Length: %d):", length);
uint8_t i;
for (i = 0; i < length && i < 8; ++i) {
printf(" %d", data[i]);
}
printf("\n");
printf("\n");
}
int main(void) {
// Create IP connection
IPConnection ipcon;
ipcon_create(&ipcon);
// Create device object
CAN can;
can_create(&can, UID, &ipcon);
// Connect to brickd
if(ipcon_connect(&ipcon, HOST, PORT) < 0) {
fprintf(stderr, "Could not connect\n");
return 1;
}
// Don't use device before ipcon is connected
// Configure transceiver for loopback mode
can_set_configuration(&can, CAN_BAUD_RATE_1000KBPS, CAN_TRANSCEIVER_MODE_LOOPBACK, 0);
// Register frame read callback to function cb_frame_read
can_register_callback(&can,
CAN_CALLBACK_FRAME_READ,
(void (*)(void))cb_frame_read,
NULL);
// Enable frame read callback
can_enable_frame_read_callback(&can);
// Write standard data frame with identifier 1742 and 3 bytes of data
uint8_t data[8] = {42, 23, 17, 0, 0, 0, 0, 0};
bool success;
can_write_frame(&can, CAN_FRAME_TYPE_STANDARD_DATA, 1742, data, 3, &success);
printf("Press key to exit\n");
getchar();
can_disable_frame_read_callback(&can);
can_destroy(&can);
ipcon_destroy(&ipcon); // Calls ipcon_disconnect internally
return 0;
}
|
Most functions of the C/C++ bindings return an error code (e_code
).
Data returned from the device, when a getter is called,
is handled via output parameters. These parameters are labeled with the
ret_
prefix.
Possible error codes are:
as defined in ip_connection.h
.
All functions listed below are thread-safe.
can_create
(CAN *can, const char *uid, IPConnection *ipcon)¶Parameters: |
|
---|
Creates the device object can
with the unique device ID uid
and adds
it to the IPConnection ipcon
:
CAN can;
can_create(&can, "YOUR_DEVICE_UID", &ipcon);
This device object can be used after the IP connection has been connected.
can_destroy
(CAN *can)¶Parameters: |
|
---|
Removes the device object can
from its IPConnection and destroys it.
The device object cannot be used anymore afterwards.
can_write_frame
(CAN *can, uint8_t frame_type, uint32_t identifier, uint8_t data[8], uint8_t length, bool *ret_success)¶Parameters: |
|
---|---|
Output Parameters: |
|
Returns: |
|
Writes a data or remote frame to the write buffer to be transmitted over the CAN transceiver.
The Bricklet supports the standard 11-bit (CAN 2.0A) and the additional extended
18-bit (CAN 2.0B) identifiers. For standard frames the Bricklet uses bit 0 to 10
from the identifier
parameter as standard 11-bit identifier. For extended
frames the Bricklet additionally uses bit 11 to 28 from the identifier
parameter as extended 18-bit identifier.
For remote frames the data
parameter is ignored.
Returns true if the frame was successfully added to the write buffer. Returns false if the frame could not be added because write buffer is already full.
The write buffer can overflow if frames are written to it at a higher rate
than the Bricklet can transmitted them over the CAN transceiver. This may
happen if the CAN transceiver is configured as read-only or is using a low baud
rate (see can_set_configuration()
). It can also happen if the CAN bus is
congested and the frame cannot be transmitted because it constantly loses
arbitration or because the CAN transceiver is currently disabled due to a high
write error level (see can_get_error_log()
).
The following constants are available for this function:
For frame_type:
can_read_frame
(CAN *can, bool *ret_success, uint8_t *ret_frame_type, uint32_t *ret_identifier, uint8_t ret_data[8], uint8_t *ret_length)¶Parameters: |
|
---|---|
Output Parameters: |
|
Returns: |
|
Tries to read the next data or remote frame from the read buffer and return it.
If a frame was successfully read, then the success
return value is set to
true and the other return values contain the frame. If the read buffer is
empty and no frame could be read, then the success
return value is set to
false and the other return values contain invalid data.
The identifier
return value follows the identifier format described for
can_write_frame()
.
For remote frames the data
return value always contains invalid data.
A configurable read filter can be used to define which frames should be
received by the CAN transceiver and put into the read buffer (see
can_set_read_filter()
).
Instead of polling with this function, you can also use callbacks. See the
can_enable_frame_read_callback()
function and the CAN_CALLBACK_FRAME_READ
callback.
The following constants are available for this function:
For ret_frame_type:
can_set_configuration
(CAN *can, uint8_t baud_rate, uint8_t transceiver_mode, int32_t write_timeout)¶Parameters: |
|
---|---|
Returns: |
|
Sets the configuration for the CAN bus communication.
The baud rate can be configured in steps between 10 and 1000 kbit/s.
The CAN transceiver has three different modes:
The write timeout has three different modes that define how a failed frame transmission should be handled:
The following constants are available for this function:
For baud_rate:
For transceiver_mode:
can_get_configuration
(CAN *can, uint8_t *ret_baud_rate, uint8_t *ret_transceiver_mode, int32_t *ret_write_timeout)¶Parameters: |
|
---|---|
Output Parameters: |
|
Returns: |
|
Returns the configuration as set by can_set_configuration()
.
The following constants are available for this function:
For ret_baud_rate:
For ret_transceiver_mode:
can_set_read_filter
(CAN *can, uint8_t mode, uint32_t mask, uint32_t filter1, uint32_t filter2)¶Parameters: |
|
---|---|
Returns: |
|
Set the read filter configuration. This can be used to define which frames should be received by the CAN transceiver and put into the read buffer.
The read filter has five different modes that define if and how the mask and the two filters are applied:
The mask and filters are used as bit masks. Their usage depends on the mode:
The mask and filters are applied in this way: The mask is used to select the identifier and data bits that should be compared to the corresponding filter bits. All unselected bits are automatically accepted. All selected bits have to match one of the filters to be accepted. If all bits for the selected mode are accepted then the frame is accepted and is added to the read buffer.
Mask Bit | Filter Bit | Identifier/Data Bit | Result |
---|---|---|---|
0 | X | X | Accept |
1 | 0 | 0 | Accept |
1 | 0 | 1 | Reject |
1 | 1 | 0 | Reject |
1 | 1 | 1 | Accept |
For example, to receive standard frames with identifier 0x123 only the mode can be set to Match-Standard with 0x7FF as mask and 0x123 as filter 1 and filter 2. The mask of 0x7FF selects all 11 identifier bits for matching so that the identifier has to be exactly 0x123 to be accepted.
To accept identifier 0x123 and identifier 0x456 at the same time, just set filter 2 to 0x456 and keep mask and filter 1 unchanged.
The following constants are available for this function:
For mode:
can_get_read_filter
(CAN *can, uint8_t *ret_mode, uint32_t *ret_mask, uint32_t *ret_filter1, uint32_t *ret_filter2)¶Parameters: |
|
---|---|
Output Parameters: |
|
Returns: |
|
Returns the read filter as set by can_set_read_filter()
.
The following constants are available for this function:
For ret_mode:
can_get_error_log
(CAN *can, uint8_t *ret_write_error_level, uint8_t *ret_read_error_level, bool *ret_transceiver_disabled, uint32_t *ret_write_timeout_count, uint32_t *ret_read_register_overflow_count, uint32_t *ret_read_buffer_overflow_count)¶Parameters: |
|
---|---|
Output Parameters: |
|
Returns: |
|
Returns information about different kinds of errors.
The write and read error levels indicate the current level of checksum, acknowledgement, form, bit and stuffing errors during CAN bus write and read operations.
When the write error level exceeds 255 then the CAN transceiver gets disabled and no frames can be transmitted or received anymore. The CAN transceiver will automatically be activated again after the CAN bus is idle for a while.
The write and read error levels are not available in read-only transceiver mode
(see can_set_configuration()
) and are reset to 0 as a side effect of changing
the configuration or the read filter.
The write timeout, read register and buffer overflow counts represents the number of these errors:
can_set_configuration()
).can_set_read_filter()
) can help to reduce the amount of received frames.
This count is not exact, but a lower bound, because the Bricklet might not
able detect all overflows if they occur in rapid succession.can_read_frame()
function. Using the CAN_CALLBACK_FRAME_READ
callback ensures that the read buffer
can not overflow.can_get_identity
(CAN *can, char ret_uid[8], char ret_connected_uid[8], char *ret_position, uint8_t ret_hardware_version[3], uint8_t ret_firmware_version[3], uint16_t *ret_device_identifier)¶Parameters: |
|
---|---|
Output Parameters: |
|
Returns: |
|
Returns the UID, the UID where the Bricklet is connected to, the position, the hardware and firmware version as well as the device identifier.
The position can be 'a', 'b', 'c', 'd', 'e', 'f', 'g' or 'h' (Bricklet Port). A Bricklet connected to an Isolator Bricklet is always at position 'z'.
The device identifier numbers can be found here. There is also a constant for the device identifier of this Bricklet.
can_register_callback
(CAN *can, int16_t callback_id, void (*function)(void), void *user_data)¶Parameters: |
|
---|
Registers the given function
with the given callback_id
. The
user_data
will be passed as the last parameter to the function
.
The available callback IDs with corresponding function signatures are listed below.
can_enable_frame_read_callback
(CAN *can)¶Parameters: |
|
---|---|
Returns: |
|
Enables the CAN_CALLBACK_FRAME_READ
callback.
By default the callback is disabled. Enabling this callback will disable the CAN_CALLBACK_FRAME_READABLE
callback.
can_disable_frame_read_callback
(CAN *can)¶Parameters: |
|
---|---|
Returns: |
|
Disables the CAN_CALLBACK_FRAME_READ
callback.
By default the callback is disabled.
can_is_frame_read_callback_enabled
(CAN *can, bool *ret_enabled)¶Parameters: |
|
---|---|
Output Parameters: |
|
Returns: |
|
Returns true if the CAN_CALLBACK_FRAME_READ
callback is enabled, false otherwise.
can_set_frame_readable_callback_configuration
(CAN *can, bool enabled)¶Parameters: |
|
---|---|
Returns: |
|
Enables/disables the CAN_CALLBACK_FRAME_READABLE
callback.
By default the callback is disabled. Enabling this callback will disable the CAN_CALLBACK_FRAME_READ
callback.
New in version 2.0.1 (Plugin).
can_get_frame_readable_callback_configuration
(CAN *can, bool *ret_enabled)¶Parameters: |
|
---|---|
Output Parameters: |
|
Returns: |
|
Returns true if the CAN_CALLBACK_FRAME_READABLE
callback is enabled, false otherwise.
New in version 2.0.1 (Plugin).
Callbacks can be registered to receive time critical or recurring data from the
device. The registration is done with the can_register_callback()
function:
void my_callback(int value, void *user_data) { printf("Value: %d\n", value); } can_register_callback(&can, CAN_CALLBACK_EXAMPLE, (void (*)(void))my_callback, NULL);
The available constants with corresponding function signatures are described below.
Note
Using callbacks for recurring events is always preferred compared to using getters. It will use less USB bandwidth and the latency will be a lot better, since there is no round trip time.
CAN_CALLBACK_FRAME_READ
¶void callback(uint8_t frame_type, uint32_t identifier, uint8_t data[8], uint8_t length, void *user_data)
Callback Parameters: |
|
---|
This callback is triggered if a data or remote frame was received by the CAN transceiver.
The identifier
return value follows the identifier format described for
can_write_frame()
.
For remote frames the data
return value always contains invalid values.
A configurable read filter can be used to define which frames should be
received by the CAN transceiver at all (see can_set_read_filter()
).
To enable this callback, use can_enable_frame_read_callback()
.
The following constants are available for this function:
For frame_type:
CAN_CALLBACK_FRAME_READABLE
¶void callback(void *user_data)
Callback Parameters: |
|
---|
This callback is triggered if a data or remote frame was received by the CAN
transceiver. The received frame can be read with can_read_frame()
.
If additional frames are received, but can_read_frame()
was not called yet, the callback
will not trigger again.
A configurable read filter can be used to define which frames should be
received by the CAN transceiver and put into the read queue (see
can_set_read_filter()
).
To enable this callback, use can_set_frame_readable_callback_configuration()
.
New in version 2.0.1 (Plugin).
Virtual functions don't communicate with the device itself, but operate only on the API bindings device object. They can be called without the corresponding IP Connection object being connected.
can_get_api_version
(CAN *can, uint8_t ret_api_version[3])¶Parameters: |
|
---|---|
Output Parameters: |
|
Returns: |
|
Returns the version of the API definition implemented by this API bindings. This is neither the release version of this API bindings nor does it tell you anything about the represented Brick or Bricklet.
can_get_response_expected
(CAN *can, uint8_t function_id, bool *ret_response_expected)¶Parameters: |
|
---|---|
Output Parameters: |
|
Returns: |
|
Returns the response expected flag for the function specified by the function ID parameter. It is true if the function is expected to send a response, false otherwise.
For getter functions this is enabled by default and cannot be disabled,
because those functions will always send a response. For callback configuration
functions it is enabled by default too, but can be disabled by
can_set_response_expected()
. For setter functions it is disabled by default
and can be enabled.
Enabling the response expected flag for a setter function allows to detect timeouts and other error conditions calls of this setter as well. The device will then send a response for this purpose. If this flag is disabled for a setter function then no response is sent and errors are silently ignored, because they cannot be detected.
The following constants are available for this function:
For function_id:
can_set_response_expected
(CAN *can, uint8_t function_id, bool response_expected)¶Parameters: |
|
---|---|
Returns: |
|
Changes the response expected flag of the function specified by the function ID parameter. This flag can only be changed for setter (default value: false) and callback configuration functions (default value: true). For getter functions it is always enabled.
Enabling the response expected flag for a setter function allows to detect timeouts and other error conditions calls of this setter as well. The device will then send a response for this purpose. If this flag is disabled for a setter function then no response is sent and errors are silently ignored, because they cannot be detected.
The following constants are available for this function:
For function_id:
can_set_response_expected_all
(CAN *can, bool response_expected)¶Parameters: |
|
---|---|
Returns: |
|
Changes the response expected flag for all setter and callback configuration functions of this device at once.
CAN_DEVICE_IDENTIFIER
¶This constant is used to identify a CAN Bricklet.
The can_get_identity()
function and the IPCON_CALLBACK_ENUMERATE
callback of the IP Connection have a device_identifier
parameter to specify
the Brick's or Bricklet's type.
CAN_DEVICE_DISPLAY_NAME
¶This constant represents the human readable name of a CAN Bricklet.