沁恒CH32x RT-Thread驅(qū)動(dòng)添加適配CH32V208驅(qū)動(dòng)-電子發(fā)燒友網(wǎng) (elecfans.com)
在這個(gè)基礎(chǔ)上,我增加了適配CAN的驅(qū)動(dòng),接著就可以篇寫CAN驅(qū)動(dòng),來(lái)實(shí)現(xiàn)數(shù)據(jù)收發(fā)了。
配置
- 打開env工具,輸入menuconfig,打開圖形配置界面,選擇Hardware Drivers Conifg---->On-chip Peripheral Drivers-->Enable CAN-->啟動(dòng)Using CAN1
- 添加 can測(cè)試文件。用vscode打開rtthread目錄添加can_sample.c函數(shù)如下:
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2023-02-01 Administrator the first version
*/
/*
* 程序清單:這是一個(gè) CAN 設(shè)備使用例程
* 例程導(dǎo)出了 can_sample 命令到控制終端
* 命令調(diào)用格式:can_sample can1
* 命令解釋:命令第二個(gè)參數(shù)是要使用的 CAN 設(shè)備名稱,為空則使用默認(rèn)的 CAN 設(shè)備
* 程序功能:通過(guò) CAN 設(shè)備發(fā)送一幀,并創(chuàng)建一個(gè)線程接收數(shù)據(jù)然后打印輸出。
*/
#include
#include "rtdevice.h"
#define CAN_DEV_NAME "can1" /* CAN 設(shè)備名稱 */
static struct rt_semaphore rx_sem; /* 用于接收消息的信號(hào)量 */
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ā)送接收信號(hào)量 */
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ù)長(zhǎng)度為 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!
");
}
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:0x100~0x1ff,hdr 為 - 1,設(shè)置默認(rèn)過(guò)濾表 */
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,指定設(shè)置 7 號(hào)過(guò)濾表 */
};
struct rt_can_filter_config cfg = {5, 1, items}; /* 一共有 5 個(gè)過(guò)濾表 */
/* 設(shè)置硬件過(guò)濾表 */
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_index = -1;
/* 阻塞等待接收信號(hào)量 */
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("recv end
");
}
}
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!
", can_name);
return RT_ERROR;
}
/* 初始化 CAN 接收信號(hào)量 */
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, 2056, 25, 10);
if (thread != RT_NULL)
{
rt_thread_startup(thread);
}
else
{
rt_kprintf("create can_rx thread failed!
");
}
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ù)長(zhǎng)度為 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!
");
}
rt_kprintf("can dev write data OK !
");
return res;
}
/* 導(dǎo)出到 msh 命令列表中 */
MSH_CMD_EXPORT(can_sample, can device sample);
- 在env工具中輸入scons執(zhí)行編譯:
- 然后打開wchisp工具把rtthread.bin下載到開發(fā)板:
- 用CAN轉(zhuǎn)TTL連接到CAN分析儀上,打開調(diào)試軟件,以及msh終端:
【總結(jié)】
雖然wch在rtthread的驅(qū)動(dòng)沒有更新,但是經(jīng)過(guò)我的修復(fù),順利的完成CAN的測(cè)試工作。
審核編輯:湯梓紅
-
CAN
+關(guān)注
關(guān)注
57文章
2754瀏覽量
463697 -
開發(fā)板
+關(guān)注
關(guān)注
25文章
5050瀏覽量
97455 -
RTT
+關(guān)注
關(guān)注
0文章
65瀏覽量
17126 -
RT-Thread
+關(guān)注
關(guān)注
31文章
1289瀏覽量
40122
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
評(píng)論