在applicantions文件夾下新建test_can.c,內(nèi)容如下:
/*
程序清單:這是一個 CAN 設(shè)備使用例程
例程導(dǎo)出了 can_sample 命令到控制終端
命令調(diào)用格式:can_sample bxcna
命令解釋:命令第二個參數(shù)是要使用的 CAN 設(shè)備名稱,為空則使用默認(rèn)的 CAN 設(shè)備
程序功能:通過 CAN 設(shè)備發(fā)送一幀,并創(chuàng)建一個線程接收數(shù)據(jù)然后打印輸出。
/
#include "can_dewen.h"
#include
#include "rtdevice.h"
#define CAN_DEV_NAME "bxcan" / CAN 設(shè)備名稱 /
static struct rt_semaphore rx_sem; / 用于接收消息的信號量 /
static rt_device_t can_dev; / CAN 設(shè)備句柄 /
/ 接收數(shù)據(jù)回調(diào)函數(shù) /
static rt_err_t can_rx_call(rt_device_t dev, rt_size_t size)
{
/ CAN 接收到數(shù)據(jù)后產(chǎn)生中斷,調(diào)用此回調(diào)函數(shù),然后發(fā)送接收信號量 /
rt_sem_release(&rx_sem);
return RT_EOK;
}
int can_send(rt_uint32_t humi,rt_uint32_t temp)
{
struct rt_can_msg msg = {0};
rt_err_t res;
rt_size_t size;
msg.id = 0x78; / ID 為 0x78 /
msg.ide = RT_CAN_STDID; / 標(biāo)準(zhǔn)格式 /
msg.rtr = RT_CAN_DTR; / 數(shù)據(jù)幀 /
msg.len = 8; / 數(shù)據(jù)長度為 8 /
/ 待發(fā)送的 8 字節(jié)數(shù)據(jù) /
msg.data[0] = (uint8_t)(temp/100);
msg.data[1] = (uint8_t)(temp%100);
msg.data[2] = (uint8_t)(humi/100);
msg.data[3] = (uint8_t)(humi%100);
msg.data[4] = 0x44;
msg.data[5] = 0x55;
msg.data[6] = 0x66;
msg.data[7] = 0x77;
/ 發(fā)送一幀 CAN 數(shù)據(jù) /
size = rt_device_write(can_dev, 0, &msg, sizeof(msg));
if (size == 0)
{
rt_kprintf("can dev write data failed!n");
}
return res;
}
static void can_rx_thread(void parameter)
{
int i;
rt_err_t res;
struct rt_can_msg rxmsg = {0};
/ 設(shè)置接收回調(diào)函數(shù) /
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:0x1000x1ff,hdr 為 - 1,設(shè)置默認(rèn)過濾表 /0x3ff,hdr 為 - 1 /
RT_CAN_FILTER_ITEM_INIT(0x300, 0, 0, 0, 0x700, RT_NULL, RT_NULL), / std,match ID:0x300
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,指定設(shè)置 7 號過濾表 /
};
struct rt_can_filter_config cfg = {5, 1, items}; / 一共有 5 個過濾表 /
/ 設(shè)置硬件過濾表 /
res = rt_device_control(can_dev, RT_CAN_CMD_SET_FILTER, &cfg);
RT_ASSERT(res == RT_EOK);
#endif
while (1)
{
/ hdr 值為 - 1,表示直接從 uselist 鏈表讀取數(shù)據(jù) /
rxmsg.hdr = -1;
/ 阻塞等待接收信號量 /
rt_sem_take(&rx_sem, RT_WAITING_FOREVER);
/ 從 CAN 讀取一幀數(shù)據(jù) /
rt_device_read(can_dev, 0, &rxmsg, sizeof(rxmsg));
/ 打印數(shù)據(jù) ID 及內(nèi)容 /
rt_kprintf("ID:%x", rxmsg.id);
for (i = 0; i < 8; i++)
{
rt_kprintf("%2x", rxmsg.data[i]);
}
rt_kprintf("n");
}
}
int can_sample(void)
{
struct rt_can_msg msg = {0};
rt_err_t res;
rt_size_t size;
rt_thread_t thread;
char can_name[RT_NAME_MAX];
rt_strncpy(can_name, CAN_DEV_NAME, RT_NAME_MAX);
/ 查找 CAN 設(shè)備 /
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);
/ 設(shè)置 CAN 通信的波特率為 500kbit/s /
res = rt_device_control(can_dev, RT_CAN_CMD_SET_BAUD, (void )CAN500kBaud);
/ 以中斷接收及發(fā)送方式打開 CAN 設(shè)備 /
res = rt_device_open(can_dev, RT_DEVICE_FLAG_INT_TX | RT_DEVICE_FLAG_INT_RX);
RT_ASSERT(res == RT_EOK);
/ 創(chuàng)建數(shù)據(jù)接收線程 /
thread = rt_thread_create("can_rx", can_rx_thread, RT_NULL, 1024, 25, 10);
if (thread != RT_NULL)
{
rt_thread_startup(thread);
}
else
{
rt_kprintf("create can_rx thread failed!n");
}
msg.id = 0x78; / ID 為 0x78 /
msg.ide = RT_CAN_STDID; / 標(biāo)準(zhǔn)格式 /
msg.rtr = RT_CAN_DTR; / 數(shù)據(jù)幀 /
msg.len = 8; / 數(shù)據(jù)長度為 8 /
/ 待發(fā)送的 8 字節(jié)數(shù)據(jù) /
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;
/ 發(fā)送一幀 CAN 數(shù)據(jù) /
size = rt_device_write(can_dev, 0, &msg, sizeof(msg));
if (size == 0)
{
rt_kprintf("can dev write data failed!n");
}
return res;
}
/ 導(dǎo)出到 msh 命令列表中 */
MSH_CMD_EXPORT(can_sample, can device sample);
然后編譯下載到開發(fā)板。
硬件準(zhǔn)備
CAN分析儀
CAN——TTL模塊
調(diào)試步驟
硬件接好 TTL——CAN TX ————開發(fā)板PB9,TTL——CAN RX————開發(fā)板PB8
通過集線器把CAN分析儀CAN——H 與TTL的CAN——H 相接 CAN——L與CAN——L接好。
通過串品終端執(zhí)行can_sample:
打開CAN分析儀上位機(jī),自動OBD識別,識別到了500K的CAN總,同時也接收到了開發(fā)板發(fā)送的認(rèn)證數(shù)據(jù)。發(fā)送數(shù)據(jù),在開發(fā)板的串口終端也成功接收并打印出來:
【總結(jié)】
國民技術(shù)在RT-Thread上的生態(tài)非常好,可以快速的實(shí)現(xiàn)對外設(shè)的驅(qū)動。
-
CAN總線
+關(guān)注
關(guān)注
145文章
1911瀏覽量
130570 -
TTL電路
+關(guān)注
關(guān)注
2文章
60瀏覽量
15056 -
上位機(jī)
+關(guān)注
關(guān)注
27文章
930瀏覽量
54696 -
CAN分析儀
+關(guān)注
關(guān)注
0文章
10瀏覽量
5410 -
RT-Thread
+關(guān)注
關(guān)注
31文章
1261瀏覽量
39839
發(fā)布評論請先 登錄
相關(guān)推薦
評論