RT-Thread RTOS
An open source embedded real-time operating system
+ Collaboration diagram for CAN Driver:

Data Structures

struct  rt_can_filter_item
 
struct  rt_can_filter_config
 
struct  rt_can_bit_timing
 
struct  rt_can_bit_timing_config
 
struct  can_configure
 
struct  rt_can_status
 
struct  rt_can_status_ind_type
 
struct  rt_can_msg
 
struct  rt_can_device
 
struct  rt_can_ops
 

Macros

#define CAN_RX_FIFO0   (0x00000000U)
 
#define CAN_RX_FIFO1   (0x00000001U)
 
#define RT_CAN_FILTER_ITEM_INIT(id, ide, rtr, mode, mask)    {(id), (ide), (rtr), (mode), (mask), -1, CAN_RX_FIFO0 }
 

Typedefs

typedef rt_err_t(* rt_canstatus_ind) (struct rt_can_device *can, void *args)
 
typedef struct rt_can_status_ind_typert_can_status_ind_type_t
 
typedef void(* rt_can_bus_hook) (struct rt_can_device *can)
 

Functions

rt_err_t rt_hw_can_register (struct rt_can_device *can, const char *name, const struct rt_can_ops *ops, void *data)
 
void rt_hw_can_isr (struct rt_can_device *can, int event)
 

Detailed Description

CAN driver api.

Example: Demonstrating CAN RX, Filters, and Blocking/Non-Blocking TX

#include <rtthread.h>
#include <rtdevice.h>
#define CAN_DEV_NAME "can1" // The name of the CAN device
static rt_device_t can_dev; // CAN device handle
static struct rt_semaphore rx_sem; // Semaphore for message reception
// Callback function for CAN reception
static rt_err_t can_rx_callback(rt_device_t dev, rt_size_t size)
{
// The CAN interrupt calls this callback when data is received.
// Release the semaphore to notify the receiving thread.
rt_sem_release(&rx_sem);
return RT_EOK;
}
static void can_rx_thread(void *parameter)
{
rt_err_t res;
struct rt_can_msg rx_msg = {0};
// Set the receive callback function
rt_device_set_rx_indicate(can_dev, can_rx_callback);
#ifdef RT_CAN_USING_HDR
// Example of configuring multiple hardware filters
struct rt_can_filter_item items[] =
{
// Filter 1: Match standard frames with IDs from 0x100 to 0x1FF. hdr_index will be -1.
RT_CAN_FILTER_ITEM_INIT(0x100, 0, 0, 0, 0x700, RT_NULL, RT_NULL),
// Filter 2: Match standard frames with IDs from 0x300 to 0x3FF. hdr_index will be -1.
RT_CAN_FILTER_ITEM_INIT(0x300, 0, 0, 0, 0x700, RT_NULL, RT_NULL),
// Filter 3: Exactly match standard frame with ID 0x211. hdr_index will be -1.
RT_CAN_FILTER_ITEM_INIT(0x211, 0, 0, 0, 0x7FF, RT_NULL, RT_NULL),
// Filter 4: Exactly match standard frame with ID 0x486 using a helper macro. hdr_index will be -1.
RT_CAN_FILTER_STD_INIT(0x486, RT_NULL, RT_NULL),
// Filter 5: Exactly match standard frame with ID 0x555 and explicitly assign it to filter bank #7.
// This uses direct struct initialization: {id, ide, rtr, mode, mask, hdr_bank}.
{0x555, 0, 0, 0, 0x7FF, 7}
};
// Create the filter configuration structure with 5 active filters.
struct rt_can_filter_config cfg = {sizeof(items)/sizeof(items[0]), 1, items};
// Set the hardware filters for the CAN device.
res = rt_device_control(can_dev, RT_CAN_CMD_SET_FILTER, &cfg);
RT_ASSERT(res == RT_EOK);
#endif
// Some drivers might require an explicit start command.
// This is driver-specific.
rt_uint32_t cmd_arg = 1; // Argument to enable the controller
res = rt_device_control(can_dev, RT_CAN_CMD_START, &cmd_arg);
RT_ASSERT(res == RT_EOK);
while (1)
{
// Block and wait for the semaphore, which is released by the receive callback.
rt_sem_take(&rx_sem, RT_WAITING_FOREVER);
// Read one frame of data from the CAN device's general message queue.
rx_msg.hdr_index = -1;
rt_device_read(can_dev, 0, &rx_msg, sizeof(rx_msg));
// Print the received message's ID and data.
rt_kprintf("Received a message. ID: 0x%x, Data: ", rx_msg.id);
for (int i = 0; i < rx_msg.len; i++)
{
rt_kprintf("%02x ", rx_msg.data[i]);
}
rt_kprintf("\n");
}
}
int can_sample(int argc, char *argv[])
{
rt_err_t res;
rt_thread_t thread;
char can_name[RT_NAME_MAX];
// Allow specifying the CAN device name from the command line, e.g., "can_sample can2"
if (argc == 2)
{
rt_strncpy(can_name, argv[1], RT_NAME_MAX);
}
else
{
rt_strncpy(can_name, CAN_DEV_NAME, RT_NAME_MAX);
}
// Find the CAN device by name
can_dev = rt_device_find(can_name);
if (!can_dev)
{
rt_kprintf("find device %s failed!\n", can_name);
return -RT_ERROR;
}
// Initialize the receive semaphore
rt_sem_init(&rx_sem, "rx_sem", 0, RT_IPC_FLAG_FIFO);
// Open the CAN device in interrupt-driven TX/RX mode
res = rt_device_open(can_dev, RT_DEVICE_FLAG_INT_TX | RT_DEVICE_FLAG_INT_RX);
RT_ASSERT(res == RT_EOK);
// Create and start the data receiving thread
thread = rt_thread_create("can_rx", can_rx_thread, RT_NULL, 1024, 25, 10);
if (thread != RT_NULL)
{
}
else
{
rt_kprintf("create can_rx thread failed!\n");
return -RT_ERROR;
}
rt_kprintf("CAN device %s opened successfully.\n", can_name);
// --- Demonstrate Blocking Send ---
struct rt_can_msg blocking_msg = {0};
blocking_msg.id = 0x78;
blocking_msg.ide = RT_CAN_STDID;
blocking_msg.rtr = RT_CAN_DTR;
blocking_msg.len = 8;
// The `nonblocking` flag is 0 by default for blocking mode.
for(int i = 0; i < 8; i++) blocking_msg.data[i] = i;
rt_kprintf("Attempting to send a message in BLOCKING mode...\n");
if (rt_device_write(can_dev, 0, &blocking_msg, sizeof(blocking_msg)) == sizeof(blocking_msg))
{
rt_kprintf("Blocking message sent successfully.\n");
}
else
{
rt_kprintf("Blocking message send failed.\n");
}
rt_thread_mdelay(100); // Wait a moment for clarity in the log
// --- Demonstrate Non-Blocking Send ---
struct rt_can_msg nonblocking_msg = {0};
nonblocking_msg.id = 0x79;
nonblocking_msg.ide = RT_CAN_STDID;
nonblocking_msg.rtr = RT_CAN_DTR;
nonblocking_msg.len = 4;
nonblocking_msg.data[0] = 0xDE;
nonblocking_msg.data[1] = 0xAD;
nonblocking_msg.data[2] = 0xBE;
nonblocking_msg.data[3] = 0xEF;
nonblocking_msg.nonblocking = 1; // <-- Key: Set the non-blocking flag
rt_kprintf("Attempting to send a message in NON-BLOCKING mode...\n");
if (rt_device_write(can_dev, 0, &nonblocking_msg, sizeof(nonblocking_msg)) == sizeof(nonblocking_msg))
{
rt_kprintf("Non-blocking message was accepted (sent or enqueued).\n");
}
else
{
rt_kprintf("Non-blocking send failed (buffer was full).\n");
}
return res;
}
// Export the function to the MSH command line
MSH_CMD_EXPORT(can_sample, can device usage example);
#define RT_DEVICE_FLAG_INT_RX
Definition: rtdef.h:1293
#define RT_DEVICE_FLAG_INT_TX
Definition: rtdef.h:1295
#define RT_CAN_FILTER_ITEM_INIT(id, ide, rtr, mode, mask)
A helper macro to initialize a rt_can_filter_item structure for Mask Mode.
Definition: dev_can.h:317
#define MSH_CMD_EXPORT(...)
Exports a command to module shell.
Definition: finsh.h:153
rt_err_t rt_sem_init(rt_sem_t sem, const char *name, rt_uint32_t value, rt_uint8_t flag)
This function will initialize a static semaphore object.
Definition: ipc.c:376
rt_err_t rt_sem_release(rt_sem_t sem)
This function will release a semaphore. If there is thread suspended on the semaphore,...
Definition: ipc.c:696
#define RT_IPC_FLAG_FIFO
Definition: rtdef.h:968
#define RT_WAITING_FOREVER
Definition: rtdef.h:976
rt_err_t rt_thread_startup(rt_thread_t thread)
This function will start a thread and put it to system ready queue.
Definition: thread.c:406
rt_err_t rt_thread_mdelay(rt_int32_t ms)
This function will let current thread delay for some milliseconds.
Definition: thread.c:784
rt_thread_t rt_thread_create(const char *name, void(*entry)(void *parameter), void *parameter, rt_uint32_t stack_size, rt_uint8_t priority, rt_uint32_t tick)
This function will create a thread object and allocate thread object memory. and stack.
Definition: thread.c:550
CAN hardware filter configuration structure. This structure is passed to the driver via rt_device_con...
Definition: dev_can.h:339
struct rt_can_filter_item * items
Definition: dev_can.h:342
CAN filter item structure.
Definition: dev_can.h:257
The CAN message structure.
Definition: dev_can.h:502
rt_uint32_t id
Definition: dev_can.h:503
rt_uint32_t ide
Definition: dev_can.h:504
rt_uint32_t rtr
Definition: dev_can.h:505
rt_uint32_t len
Definition: dev_can.h:507
rt_uint8_t data[8]
Definition: dev_can.h:523
rt_int32_t hdr_index
Definition: dev_can.h:509
rt_uint32_t nonblocking
Definition: dev_can.h:519
Definition: rtdef.h:1366
Definition: rtdef.h:999
Thread Control Block.
Definition: rtdef.h:852

Macro Definition Documentation

◆ CAN_RX_FIFO0

#define CAN_RX_FIFO0   (0x00000000U)

CAN receive FIFO 0

◆ CAN_RX_FIFO1

#define CAN_RX_FIFO1   (0x00000001U)

CAN receive FIFO 1

◆ RT_CAN_FILTER_ITEM_INIT

#define RT_CAN_FILTER_ITEM_INIT (   id,
  ide,
  rtr,
  mode,
  mask 
)     {(id), (ide), (rtr), (mode), (mask), -1, CAN_RX_FIFO0 }

A helper macro to initialize a rt_can_filter_item structure for Mask Mode.

Parameters
[in]idThe CAN ID for the filter.
[in]ideIdentifier type (0 for Standard, 1 for Extended).
[in]rtrFrame type (0 for Data, 1 for Remote).
[in]modeFilter mode (0 for Mask, 1 for List).
[in]maskThe mask to be applied.

Typedef Documentation

◆ rt_canstatus_ind

typedef rt_err_t(* rt_canstatus_ind) (struct rt_can_device *can, void *args)

Typedef for the CAN status indication callback function.

Parameters
[in]canA pointer to the CAN device.
[in]argsUser-provided arguments.
Returns
The operation status.

◆ rt_can_status_ind_type_t

Structure to hold the status indication callback and its arguments.

◆ rt_can_bus_hook

typedef void(* rt_can_bus_hook) (struct rt_can_device *can)

Typedef for the periodic bus hook function.

Parameters
[in]canA pointer to the CAN device.
Returns
void

Function Documentation

◆ rt_hw_can_register()

rt_err_t rt_hw_can_register ( struct rt_can_device can,
const char *  name,
const struct rt_can_ops ops,
void *  data 
)

This function registers a CAN device with the device framework.

Parameters
[in]canA pointer to the CAN device object to be registered.
[in]nameThe name that the device will be registered with.
[in]opsA pointer to the structure containing the low-level CAN driver operations.
[in]dataA pointer to a user-defined data structure, which can be accessed via can->parent.user_data.
Returns
RT_EOK on successful registration, or a negative error code on failure.

◆ rt_hw_can_isr()

void rt_hw_can_isr ( struct rt_can_device can,
int  event 
)

The framework-level ISR handler for CAN devices.

This function is called by the low-level BSP ISR and acts as the central dispatcher for all CAN-related interrupt events. It handles both receive events and transmission-complete events.

Parameters
[in]canA pointer to the CAN device structure.
[in]eventThe interrupt event mask, indicating the cause of the interrupt.