Skip to content

Commit 374da3d

Browse files
committed
add can driver sample
1 parent dfd495d commit 374da3d

File tree

2 files changed

+145
-0
lines changed

2 files changed

+145
-0
lines changed

SConscript

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ include_path = [cwd]
77
if GetDepend('PERIPHERAL_SAMPLES_USING_ADC'):
88
src += ['adc_vol_sample.c']
99

10+
if GetDepend('PERIPHERAL_SAMPLES_USING_CAN'):
11+
src += ['can_sample.c']
12+
1013
if GetDepend('PERIPHERAL_SAMPLES_USING_HWTIMER'):
1114
src += ['hwtimer_sample.c']
1215

can_sample.c

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
/*
2+
* Copyright (c) 2006-2018, RT-Thread Development Team
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
* Change Logs:
7+
* Date Author Notes
8+
* 2019-06-25 misonyo first implementation.
9+
*/
10+
/*
11+
* 程序清单:这是一个 CAN 设备使用例程
12+
* 例程导出了 can_sample 命令到控制终端
13+
* 命令调用格式:can_sample can2
14+
* 命令解释:命令第二个参数是要使用的 CAN 设备名称,为空则使用默认的 CAN 设备
15+
* 程序功能:通过 CAN 设备发送一帧,并创建一个线程接收数据然后打印输出。
16+
*/
17+
18+
#include <rtthread.h>
19+
#include "rtdevice.h"
20+
21+
#define CAN_DEV_NAME "can2" /* CAN 设备名称 */
22+
23+
static struct rt_semaphore rx_sem; /* 用于接收消息的信号量 */
24+
static rt_device_t can_dev; /* CAN 设备句柄 */
25+
26+
/* 接收数据回调函数 */
27+
static rt_err_t can_rx_call(rt_device_t dev, rt_size_t size)
28+
{
29+
/* CAN 接收到数据后产生中断,调用此回调函数,然后发送接收信号量 */
30+
rt_sem_release(&rx_sem);
31+
32+
return RT_EOK;
33+
}
34+
35+
static void can_rx_thread(void *parameter)
36+
{
37+
int i;
38+
rt_err_t res;
39+
struct rt_can_msg rxmsg = {0};
40+
41+
/* 设置接收回调函数 */
42+
rt_device_set_rx_indicate(can_dev, can_rx_call);
43+
44+
#ifdef RT_CAN_USING_HDR
45+
struct rt_can_filter_item items[5] =
46+
{
47+
RT_CAN_FILTER_ITEM_INIT(0x100, 0, 0, 0, 0x700, RT_NULL, RT_NULL), /* std,match ID:0x100~0x1ff,hdr为-1,设置默认过滤表 */
48+
RT_CAN_FILTER_ITEM_INIT(0x300, 0, 0, 0, 0x700, RT_NULL, RT_NULL), /* std,match ID:0x300~0x3ff,hdr为-1 */
49+
RT_CAN_FILTER_ITEM_INIT(0x211, 0, 0, 0, 0x7ff, RT_NULL, RT_NULL), /* std,match ID:0x211,hdr为-1 */
50+
RT_CAN_FILTER_STD_INIT(0x486, RT_NULL, RT_NULL), /* std,match ID:0x486,hdr为-1 */
51+
{0x555, 0, 0, 0, 0x7ff, 7,} /* std,match ID:0x555,hdr为7,指定设置7号过滤表 */
52+
};
53+
struct rt_can_filter_config cfg = {5, 1, items}; /* 一共有5个过滤表 */
54+
/* 设置硬件过滤表 */
55+
res = rt_device_control(can_dev, RT_CAN_CMD_SET_FILTER, &cfg);
56+
RT_ASSERT(res == RT_EOK);
57+
#endif
58+
59+
while (1)
60+
{
61+
/* hdr值为-1,表示直接从uselist链表读取数据 */
62+
rxmsg.hdr = -1;
63+
/* 阻塞等待接收信号量 */
64+
rt_sem_take(&rx_sem, RT_WAITING_FOREVER);
65+
/* 从CAN读取一帧数据 */
66+
rt_device_read(can_dev, 0, &rxmsg, sizeof(rxmsg));
67+
/* 打印数据ID及内容 */
68+
rt_kprintf("ID:%x ", rxmsg.id);
69+
for (i = 0; i < 8; i++)
70+
{
71+
rt_kprintf("%2x ", rxmsg.data[i]);
72+
}
73+
74+
rt_kprintf("\n");
75+
}
76+
}
77+
78+
int can_sample(int argc, char *argv[])
79+
{
80+
struct rt_can_msg msg = {0};
81+
rt_err_t res;
82+
rt_size_t size;
83+
rt_thread_t thread;
84+
char can_name[RT_NAME_MAX];
85+
86+
if (argc == 2)
87+
{
88+
rt_strncpy(can_name, argv[1], RT_NAME_MAX);
89+
}
90+
else
91+
{
92+
rt_strncpy(can_name, CAN_DEV_NAME, RT_NAME_MAX);
93+
}
94+
95+
can_dev = rt_device_find(can_name);
96+
if (!can_dev)
97+
{
98+
rt_kprintf("find %s failed!\n", can_name);
99+
return RT_ERROR;
100+
}
101+
102+
/* 初始化CAN接收信号量 */
103+
rt_sem_init(&rx_sem, "rx_sem", 0, RT_IPC_FLAG_FIFO);
104+
105+
/* 以中断接收及发送方式打开CAN设备 */
106+
res = rt_device_open(can_dev, RT_DEVICE_FLAG_INT_TX | RT_DEVICE_FLAG_INT_RX);
107+
RT_ASSERT(res == RT_EOK);
108+
109+
thread = rt_thread_create("can_rx", can_rx_thread, RT_NULL, 1024, 25, 10);
110+
if (thread != RT_NULL)
111+
{
112+
rt_thread_startup(thread);
113+
}
114+
else
115+
{
116+
rt_kprintf("create can_rx thread failed!\n");
117+
}
118+
119+
msg.id = 0x78; /* ID为0x78 */
120+
msg.ide = RT_CAN_STDID; /* 标准格式 */
121+
msg.rtr = RT_CAN_DTR; /* 数据帧 */
122+
msg.len = 8; /* 数据长度为8 */
123+
/* 待发送的8字节数据 */
124+
msg.data[0] = 0x00;
125+
msg.data[1] = 0x11;
126+
msg.data[2] = 0x22;
127+
msg.data[3] = 0x33;
128+
msg.data[4] = 0x44;
129+
msg.data[5] = 0x55;
130+
msg.data[6] = 0x66;
131+
msg.data[7] = 0x77;
132+
/* 发送一帧CAN数据 */
133+
size = rt_device_write(can_dev, 0, &msg, sizeof(msg));
134+
if (size == 0)
135+
{
136+
rt_kprintf("can dev write data failed!\n");
137+
}
138+
139+
return res;
140+
}
141+
/* 导出到 msh 命令列表中 */
142+
MSH_CMD_EXPORT(can_sample, can device sample);

0 commit comments

Comments
 (0)