RT-Thread RTOS 1.2.0
An open source embedded real-time operating system
载入中...
搜索中...
未找到
CAN Driver
+ CAN Driver 的协作图:

结构体

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_device
 
struct  rt_can_msg
 
struct  rt_can_msg_list
 
struct  rt_can_rx_fifo
 
struct  rt_can_sndbxinx_list
 
struct  rt_can_tx_fifo
 
struct  rt_can_ops
 

宏定义

#define CAN_RX_FIFO0   (0x00000000U)
 
#define CAN_RX_FIFO1   (0x00000001U)
 
#define RT_CAN_FILTER_ITEM_INIT(id, ide, rtr, mode, mask)
 
#define RT_CAN_FILTER_STD_INIT(id)
 
#define RT_CAN_FILTER_EXT_INIT(id)
 
#define RT_CAN_STD_RMT_FILTER_INIT(id)
 
#define RT_CAN_EXT_RMT_FILTER_INIT(id)
 
#define RT_CAN_STD_RMT_DATA_FILTER_INIT(id)
 
#define RT_CAN_EXT_RMT_DATA_FILTER_INIT(id)
 
#define CANDEFAULTCONFIG
 
#define RT_CAN_CMD_SET_FILTER   0x13
 
#define RT_CAN_CMD_SET_BAUD   0x14
 
#define RT_CAN_CMD_SET_MODE   0x15
 
#define RT_CAN_CMD_SET_PRIV   0x16
 
#define RT_CAN_CMD_GET_STATUS   0x17
 
#define RT_CAN_CMD_SET_STATUS_IND   0x18
 
#define RT_CAN_CMD_SET_BUS_HOOK   0x19
 
#define RT_CAN_CMD_SET_CANFD   0x1A
 
#define RT_CAN_CMD_SET_BAUD_FD   0x1B
 
#define RT_CAN_CMD_SET_BITTIMING   0x1C
 
#define RT_CAN_CMD_START   0x1D
 
#define RT_DEVICE_CAN_INT_ERR   0x1000
 
#define RT_CAN_STDID   0
 
#define RT_CAN_EXTID   1
 
#define RT_CAN_DTR   0
 
#define RT_CAN_RTR   1
 
#define RT_CAN_SND_RESULT_OK   0
 
#define RT_CAN_SND_RESULT_ERR   1
 
#define RT_CAN_SND_RESULT_WAIT   2
 
#define RT_CAN_EVENT_RX_IND   0x01 /* Rx indication */
 
#define RT_CAN_EVENT_TX_DONE   0x02 /* Tx complete */
 
#define RT_CAN_EVENT_TX_FAIL   0x03 /* Tx fail */
 
#define RT_CAN_EVENT_RX_TIMEOUT   0x05 /* Rx timeout */
 
#define RT_CAN_EVENT_RXOF_IND   0x06 /* Rx overflow */
 

类型定义

typedef rt_err_t(* rt_canstatus_ind) (struct rt_can_device *, void *)
 
typedef struct rt_can_status_ind_typert_can_status_ind_type_t
 
typedef void(* rt_can_bus_hook) (struct rt_can_device *)
 
typedef struct rt_can_devicert_can_t
 
typedef struct rt_can_statusrt_can_status_t
 
typedef struct rt_can_msgrt_can_msg_t
 

枚举

enum  RT_CAN_STATUS_MODE { NORMAL = 0 , ERRWARNING = 1 , ERRPASSIVE = 2 , BUSOFF = 4 }
 
enum  RT_CAN_BUS_ERR {
  RT_CAN_BUS_NO_ERR = 0 , RT_CAN_BUS_BIT_PAD_ERR = 1 , RT_CAN_BUS_FORMAT_ERR = 2 , RT_CAN_BUS_ACK_ERR = 3 ,
  RT_CAN_BUS_IMPLICIT_BIT_ERR = 4 , RT_CAN_BUS_EXPLICIT_BIT_ERR = 5 , RT_CAN_BUS_CRC_ERR = 6
}
 

函数

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)
 

详细描述

CAN driver api

Example

#include <rtthread.h>
#include "rtdevice.h"
#define CAN_DEV_NAME "can1" // CAN 设备名称
static struct rt_semaphore rx_sem; // 用于接收消息的信号量
static rt_device_t can_dev; // CAN 设备句柄
// 接收数据回调函数
static rt_err_t can_rx_call(rt_device_t dev, rt_size_t size)
{
// CAN 接收到数据后产生中断,调用此回调函数,然后发送接收信号量
rt_sem_release(&rx_sem);
return RT_EOK;
}
static void can_rx_thread(void *parameter)
{
int i;
rt_err_t res;
struct rt_can_msg rxmsg = {0};
// 设置接收回调函数
rt_device_set_rx_indicate(can_dev, can_rx_call);
#ifdef RT_CAN_USING_HDR
struct rt_can_filter_item items[5] =
{
RT_CAN_FILTER_ITEM_INIT(0x100, 0, 0, 0, 0x700, RT_NULL, RT_NULL), // std,match ID:0x100~0x1ff,hdr 为 - 1,设置默认过滤表
RT_CAN_FILTER_ITEM_INIT(0x300, 0, 0, 0, 0x700, RT_NULL, RT_NULL), // std,match ID:0x300~0x3ff,hdr 为 - 1
RT_CAN_FILTER_ITEM_INIT(0x211, 0, 0, 0, 0x7ff, RT_NULL, RT_NULL), // std,match ID:0x211,hdr 为 - 1
RT_CAN_FILTER_STD_INIT(0x486, RT_NULL, RT_NULL), // std,match ID:0x486,hdr 为 - 1
{0x555, 0, 0, 0, 0x7ff, 7,} // std,match ID:0x555,hdr 为 7,指定设置 7 号过滤表
};
struct rt_can_filter_config cfg = {5, 1, items}; // 一共有 5 个过滤表
// 设置硬件过滤表
RT_ASSERT(res == RT_EOK);
#endif
res = RT_TRUE;
res = rt_device_control(can_dev, RT_CAN_CMD_START, &res);
while (1)
{
// hdr 值为 - 1,表示直接从 uselist 链表读取数据
rxmsg.hdr = -1;
// 阻塞等待接收信号量
// 从 CAN 读取一帧数据
rt_device_read(can_dev, 0, &rxmsg, sizeof(rxmsg));
// 打印数据 ID 及内容
rt_kprintf("ID:%x", rxmsg.id);
for (i = 0; i < 8; i++)
{
rt_kprintf("%2x", rxmsg.data[i]);
}
rt_kprintf("\n");
}
}
int can_sample(int argc, char *argv[])
{
struct rt_can_msg msg = {0};
rt_err_t res;
rt_size_t size;
rt_thread_t thread;
char can_name[RT_NAME_MAX];
if (argc == 2)
{
rt_strncpy(can_name, argv[1], RT_NAME_MAX);
}
else
{
rt_strncpy(can_name, CAN_DEV_NAME, RT_NAME_MAX);
}
// 查找 CAN 设备
can_dev = rt_device_find(can_name);
if (!can_dev)
{
rt_kprintf("find %s failed!\n", can_name);
return -RT_ERROR;
}
// 初始化 CAN 接收信号量
rt_sem_init(&rx_sem, "rx_sem", 0, RT_IPC_FLAG_FIFO);
// 以中断接收及发送方式打开 CAN 设备
RT_ASSERT(res == RT_EOK);
// 创建数据接收线程
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");
}
msg.id = 0x78; // ID 为 0x78
msg.ide = RT_CAN_STDID; // 标准格式
msg.rtr = RT_CAN_DTR; // 数据帧
msg.len = 8; // 数据长度为 8
// 待发送的 8 字节数据
msg.data[0] = 0x00;
msg.data[1] = 0x11;
msg.data[2] = 0x22;
msg.data[3] = 0x33;
msg.data[4] = 0x44;
msg.data[5] = 0x55;
msg.data[6] = 0x66;
msg.data[7] = 0x77;
// 发送一帧 CAN 数据
size = rt_device_write(can_dev, 0, &msg, sizeof(msg));
if (size == 0)
{
rt_kprintf("can dev write data failed!\n");
}
return res;
}
// 导出到 msh 命令列表中
MSH_CMD_EXPORT(can_sample, can device sample);
#define RT_CAN_STDID
#define RT_CAN_FILTER_ITEM_INIT(id, ide, rtr, mode, mask)
#define RT_CAN_CMD_START
#define RT_CAN_FILTER_STD_INIT(id)
#define RT_CAN_DTR
#define RT_CAN_CMD_SET_FILTER
rt_err_t rt_device_set_rx_indicate(rt_device_t dev, rt_err_t(*rx_ind)(rt_device_t dev, rt_size_t size))
#define RT_DEVICE_FLAG_INT_RX
rt_err_t rt_device_control(rt_device_t dev, int cmd, void *arg)
rt_ssize_t rt_device_write(rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size)
rt_ssize_t rt_device_read(rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size)
rt_device_t rt_device_find(const char *name)
struct rt_device * rt_device_t
rt_err_t rt_device_open(rt_device_t dev, rt_uint16_t oflag)
#define RT_DEVICE_FLAG_INT_TX
#define RT_IPC_FLAG_FIFO
#define RT_WAITING_FOREVER
#define rt_kprintf(...)
#define RT_ASSERT(EX)
rt_err_t rt_thread_startup(rt_thread_t thread)
This function will start a thread and put it to system ready queue.
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.
struct rt_thread * rt_thread_t
#define MSH_CMD_EXPORT(...)
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.
定义 ipc.c:376
rt_err_t rt_sem_take(rt_sem_t sem, rt_int32_t time)
定义 ipc.c:644
rt_err_t rt_sem_release(rt_sem_t sem)
This function will release a semaphore. If there is thread suspended on the semaphore,...
定义 ipc.c:695
rt_base_t rt_err_t
#define RT_TRUE
rt_ubase_t rt_size_t
#define RT_NULL
CAN filter configuration
struct rt_can_filter_item * items
CAN filter item
rt_uint32_t id
rt_uint32_t ide
rt_uint32_t rtr
rt_uint32_t len
rt_uint8_t data[8]

宏定义说明

◆ CAN_RX_FIFO0

#define CAN_RX_FIFO0   (0x00000000U)

CAN receive FIFO 0

在文件 dev_can.h211 行定义.

◆ CAN_RX_FIFO1

#define CAN_RX_FIFO1   (0x00000001U)

CAN receive FIFO 1

在文件 dev_can.h212 行定义.

◆ 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 }/*0:CAN_RX_FIFO0*/
#define CAN_RX_FIFO0

在文件 dev_can.h250 行定义.

250#define RT_CAN_FILTER_ITEM_INIT(id,ide,rtr,mode,mask) \
251 {(id), (ide), (rtr), (mode), (mask), -1, CAN_RX_FIFO0 }/*0:CAN_RX_FIFO0*/

◆ RT_CAN_FILTER_STD_INIT

#define RT_CAN_FILTER_STD_INIT ( id)
值:
RT_CAN_FILTER_ITEM_INIT(id,0,0,0,0xFFFFFFFF)

在文件 dev_can.h252 行定义.

252#define RT_CAN_FILTER_STD_INIT(id) \
253 RT_CAN_FILTER_ITEM_INIT(id,0,0,0,0xFFFFFFFF)

◆ RT_CAN_FILTER_EXT_INIT

#define RT_CAN_FILTER_EXT_INIT ( id)
值:
RT_CAN_FILTER_ITEM_INIT(id,1,0,0,0xFFFFFFFF)

在文件 dev_can.h254 行定义.

254#define RT_CAN_FILTER_EXT_INIT(id) \
255 RT_CAN_FILTER_ITEM_INIT(id,1,0,0,0xFFFFFFFF)

◆ RT_CAN_STD_RMT_FILTER_INIT

#define RT_CAN_STD_RMT_FILTER_INIT ( id)
值:
RT_CAN_FILTER_ITEM_INIT(id,0,1,0,0xFFFFFFFF)

在文件 dev_can.h256 行定义.

256#define RT_CAN_STD_RMT_FILTER_INIT(id) \
257 RT_CAN_FILTER_ITEM_INIT(id,0,1,0,0xFFFFFFFF)

◆ RT_CAN_EXT_RMT_FILTER_INIT

#define RT_CAN_EXT_RMT_FILTER_INIT ( id)
值:
RT_CAN_FILTER_ITEM_INIT(id,1,1,0,0xFFFFFFFF)

在文件 dev_can.h258 行定义.

258#define RT_CAN_EXT_RMT_FILTER_INIT(id) \
259 RT_CAN_FILTER_ITEM_INIT(id,1,1,0,0xFFFFFFFF)

◆ RT_CAN_STD_RMT_DATA_FILTER_INIT

#define RT_CAN_STD_RMT_DATA_FILTER_INIT ( id)
值:
RT_CAN_FILTER_ITEM_INIT(id,0,0,1,0xFFFFFFFF)

在文件 dev_can.h260 行定义.

260#define RT_CAN_STD_RMT_DATA_FILTER_INIT(id) \
261 RT_CAN_FILTER_ITEM_INIT(id,0,0,1,0xFFFFFFFF)

◆ RT_CAN_EXT_RMT_DATA_FILTER_INIT

#define RT_CAN_EXT_RMT_DATA_FILTER_INIT ( id)
值:
RT_CAN_FILTER_ITEM_INIT(id,1,0,1,0xFFFFFFFF)

在文件 dev_can.h262 行定义.

262#define RT_CAN_EXT_RMT_DATA_FILTER_INIT(id) \
263 RT_CAN_FILTER_ITEM_INIT(id,1,0,1,0xFFFFFFFF)

◆ CANDEFAULTCONFIG

#define CANDEFAULTCONFIG
值:
{\
};
#define RT_CANMSG_BOX_SZ
#define RT_CANSND_BOX_NUM
#define RT_CAN_MODE_NORMAL
@ CAN1MBaud

在文件 dev_can.h330 行定义.

330#define CANDEFAULTCONFIG \
331{\
332 CAN1MBaud,\
333 RT_CANMSG_BOX_SZ,\
334 RT_CANSND_BOX_NUM,\
335 RT_CAN_MODE_NORMAL,\
336};

◆ RT_CAN_CMD_SET_FILTER

#define RT_CAN_CMD_SET_FILTER   0x13

在文件 dev_can.h339 行定义.

◆ RT_CAN_CMD_SET_BAUD

#define RT_CAN_CMD_SET_BAUD   0x14

在文件 dev_can.h340 行定义.

◆ RT_CAN_CMD_SET_MODE

#define RT_CAN_CMD_SET_MODE   0x15

在文件 dev_can.h341 行定义.

◆ RT_CAN_CMD_SET_PRIV

#define RT_CAN_CMD_SET_PRIV   0x16

在文件 dev_can.h342 行定义.

◆ RT_CAN_CMD_GET_STATUS

#define RT_CAN_CMD_GET_STATUS   0x17

在文件 dev_can.h343 行定义.

◆ RT_CAN_CMD_SET_STATUS_IND

#define RT_CAN_CMD_SET_STATUS_IND   0x18

在文件 dev_can.h344 行定义.

◆ RT_CAN_CMD_SET_BUS_HOOK

#define RT_CAN_CMD_SET_BUS_HOOK   0x19

在文件 dev_can.h345 行定义.

◆ RT_CAN_CMD_SET_CANFD

#define RT_CAN_CMD_SET_CANFD   0x1A

在文件 dev_can.h346 行定义.

◆ RT_CAN_CMD_SET_BAUD_FD

#define RT_CAN_CMD_SET_BAUD_FD   0x1B

在文件 dev_can.h347 行定义.

◆ RT_CAN_CMD_SET_BITTIMING

#define RT_CAN_CMD_SET_BITTIMING   0x1C

在文件 dev_can.h348 行定义.

◆ RT_CAN_CMD_START

#define RT_CAN_CMD_START   0x1D

在文件 dev_can.h349 行定义.

◆ RT_DEVICE_CAN_INT_ERR

#define RT_DEVICE_CAN_INT_ERR   0x1000

在文件 dev_can.h351 行定义.

◆ RT_CAN_STDID

#define RT_CAN_STDID   0

在文件 dev_can.h435 行定义.

◆ RT_CAN_EXTID

#define RT_CAN_EXTID   1

在文件 dev_can.h436 行定义.

◆ RT_CAN_DTR

#define RT_CAN_DTR   0

在文件 dev_can.h437 行定义.

◆ RT_CAN_RTR

#define RT_CAN_RTR   1

在文件 dev_can.h438 行定义.

◆ RT_CAN_SND_RESULT_OK

#define RT_CAN_SND_RESULT_OK   0

在文件 dev_can.h487 行定义.

◆ RT_CAN_SND_RESULT_ERR

#define RT_CAN_SND_RESULT_ERR   1

在文件 dev_can.h488 行定义.

◆ RT_CAN_SND_RESULT_WAIT

#define RT_CAN_SND_RESULT_WAIT   2

在文件 dev_can.h489 行定义.

◆ RT_CAN_EVENT_RX_IND

#define RT_CAN_EVENT_RX_IND   0x01 /* Rx indication */

在文件 dev_can.h491 行定义.

◆ RT_CAN_EVENT_TX_DONE

#define RT_CAN_EVENT_TX_DONE   0x02 /* Tx complete */

在文件 dev_can.h492 行定义.

◆ RT_CAN_EVENT_TX_FAIL

#define RT_CAN_EVENT_TX_FAIL   0x03 /* Tx fail */

在文件 dev_can.h493 行定义.

◆ RT_CAN_EVENT_RX_TIMEOUT

#define RT_CAN_EVENT_RX_TIMEOUT   0x05 /* Rx timeout */

在文件 dev_can.h494 行定义.

◆ RT_CAN_EVENT_RXOF_IND

#define RT_CAN_EVENT_RXOF_IND   0x06 /* Rx overflow */

在文件 dev_can.h495 行定义.

类型定义说明

◆ rt_canstatus_ind

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

在文件 dev_can.h403 行定义.

◆ rt_can_status_ind_type_t

◆ rt_can_bus_hook

typedef void(* rt_can_bus_hook) (struct rt_can_device *)

在文件 dev_can.h410 行定义.

◆ rt_can_t

typedef struct rt_can_device* rt_can_t

在文件 dev_can.h433 行定义.

◆ rt_can_status_t

typedef struct rt_can_status* rt_can_status_t

在文件 dev_can.h440 行定义.

◆ rt_can_msg_t

typedef struct rt_can_msg* rt_can_msg_t

在文件 dev_can.h466 行定义.

枚举类型说明

◆ RT_CAN_STATUS_MODE

枚举值
NORMAL 
ERRWARNING 
ERRPASSIVE 
BUSOFF 

在文件 dev_can.h353 行定义.

354{
355 NORMAL = 0,
356 ERRWARNING = 1,
357 ERRPASSIVE = 2,
358 BUSOFF = 4,
359};
@ BUSOFF
@ ERRWARNING
@ NORMAL
@ ERRPASSIVE

◆ RT_CAN_BUS_ERR

枚举值
RT_CAN_BUS_NO_ERR 
RT_CAN_BUS_BIT_PAD_ERR 
RT_CAN_BUS_FORMAT_ERR 
RT_CAN_BUS_ACK_ERR 
RT_CAN_BUS_IMPLICIT_BIT_ERR 
RT_CAN_BUS_EXPLICIT_BIT_ERR 
RT_CAN_BUS_CRC_ERR 

在文件 dev_can.h360 行定义.

361{
369};
@ RT_CAN_BUS_CRC_ERR
@ RT_CAN_BUS_ACK_ERR
@ RT_CAN_BUS_NO_ERR
@ RT_CAN_BUS_BIT_PAD_ERR
@ RT_CAN_BUS_EXPLICIT_BIT_ERR
@ RT_CAN_BUS_IMPLICIT_BIT_ERR
@ RT_CAN_BUS_FORMAT_ERR

函数说明

◆ 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 )

Register a CAN device to device list

参数
canthe CAN device object
namethe name of CAN device
opsthe CAN device operators
datathe private data of CAN device
返回
the error code, RT_EOK on successfully

◆ rt_hw_can_isr()

void rt_hw_can_isr ( struct rt_can_device * can,
int event )

CAN interrupt service routine

参数
canthe CAN device
eventthe event mask