1.測(cè)試準(zhǔn)備工作
1.1. 開(kāi)發(fā)板硬件資源介紹:
1.2. 首先通過(guò)國(guó)民技術(shù)的技術(shù)資料網(wǎng)址,用瀏覽器打開(kāi)后如下所示:
選擇下載了N32L40XXX_V2.1.0.zip資料包后,解壓得到官方Demo,數(shù)據(jù)手冊(cè),評(píng)估板,軟件開(kāi)發(fā)套件,應(yīng)用筆記,測(cè)試資料等等。如下圖所示:
1.3. 引腳資源分配介紹
本例中CAN的引腳使用GPIOB的引腳8和引腳9配置CAN的接收和發(fā)送引腳。將GPIOC的引腳11和引腳10配置為USART3的接收和發(fā)送引腳,通過(guò)USB轉(zhuǎn)UART模塊,晶振等公共資源就不一一介紹。
1.4. 功能模塊介紹
本方案從軟件上主要分為三部份,CAN模塊,串口通訊模塊和日志輸出模塊。
CAN模塊:主要負(fù)責(zé)CAN報(bào)文的收發(fā),報(bào)文解析。
串口通訊模塊:將CAN模塊接收的CAN報(bào)文傳輸至上位機(jī),將上位機(jī)發(fā)送的CAN報(bào)文轉(zhuǎn)給CAN模塊發(fā)送到CAN網(wǎng)絡(luò)中。
日志輸出模塊:輸出系統(tǒng)運(yùn)行過(guò)程中的日志。
1.5. 性能指標(biāo)
本案例測(cè)試中,將測(cè)試波特率為500kbps,250kbps,125kbps三個(gè)波特率,具體指標(biāo)為:
錯(cuò)誤率:幀錯(cuò)誤率:99%
采樣率:85%
連續(xù)測(cè)試時(shí)長(zhǎng):72小時(shí)
2.環(huán)境搭建及創(chuàng)建工程。
2.1.首先到RT-Thread官方網(wǎng)站下載RT-Thread Studio,并安裝好,啟動(dòng)RT-Thread Studio.點(diǎn)擊SDK安裝按鈕,彈出下圖所示界面。
2.3.還需要安裝PyOCD,防止在下載程序時(shí)報(bào)錯(cuò)
2.4.在文件選擇新建,由于不支持RT-Thread Nano,所以只能選擇RT-Thread項(xiàng)目。
2.5. 在彈出菜單中,選擇項(xiàng)目存放路徑,輸入項(xiàng)目名,選擇基于開(kāi)發(fā)板,并選擇本次的測(cè)試開(kāi)發(fā)板,點(diǎn)擊確定,就創(chuàng)建了基于模板的工程。
2.6.在項(xiàng)目?jī)?nèi)的RT-Thread Settings中打開(kāi)CAN模塊功能,串口1用于日志打印。
3.RTThread的CAN驅(qū)動(dòng)機(jī)制介紹
3.1. rtconfig.h配置文件介紹
在RT-Thread Setting中,打開(kāi)USART3和CAN后,在rtconfig.h配置文件中可以看到定義了BSP_USING_CAN,RT_USING_PIN,RT_USING_SERIAL宏,表示啟動(dòng)了CAN驅(qū)動(dòng)與設(shè)備。
3.2.CAN Drive 驅(qū)動(dòng)介紹,
3.2.1.RT-Thread提供I/O設(shè)備管理接口來(lái)訪(fǎng)問(wèn)CAN硬件控制,接口如下:
3.2.2.通過(guò)INIT_BOARD_EXPORT將CAN的硬件驅(qū)動(dòng)加載到初始化列表中,通過(guò)rt_hw_can_register將CAN注冊(cè)到RT-Thread的設(shè)備列表中。
3.2.3.在調(diào)用rt_device_register函數(shù),將設(shè)備注冊(cè)到OS的設(shè)備列表中。
3.2.4.CAN波特率配置
3.3.測(cè)試代碼
#include
#include
#include
#include
/* defined the LED3 pin: PB5 /
#define LED3_PIN GET_PIN(B, 5)
#define LED2_PIN GET_PIN(B, 4)
int main(void)
{
/ set LED3 pin mode to output /
rt_pin_mode(LED3_PIN, PIN_MODE_OUTPUT);
rt_pin_mode(LED2_PIN, PIN_MODE_OUTPUT);
thread_CanDriver_Entry(RT_NULL);
while (1)
{
rt_pin_write(LED2_PIN, PIN_LOW);
rt_pin_write(LED3_PIN, PIN_HIGH);
rt_thread_mdelay(500);
rt_pin_write(LED3_PIN, PIN_LOW);
rt_pin_write(LED2_PIN, PIN_HIGH);
rt_thread_mdelay(500);
}
}
#include
/
Copyright (c) 2006-2021, RT-Thread Development Team
SPDX-License-Identifier: Apache-2.0
Change Logs:
Date Author Notes
2023-04-22 chunyang.jiang the first version
/
#include "rtdevice.h"
#include
#include
#include
#define CAN_DEV_NAME "bxcan" / CAN 設(shè)備名稱(chēng) /
struct rt_semaphore rx_sem; / 用于接收消息的信號(hào)量 /
static rt_device_t can_device; / CAN 設(shè)備名稱(chēng) */
struct rt_can_msg can_msg = {0};
struct rt_can_msg canrx_msg = {0};
static rt_thread_t thread_rec;
static rt_thread_t thread_send;
static void thread_can_send(void *parameter);
static void thread_can_Receiver(void *parameter);
static rt_err_t Can_ReceiverMessage_Call(rt_device_t dev, rt_size_t size)
{
rt_sem_release(&rx_sem);
// rt_kprintf("Can Receiver message!rn");
return RT_EOK;
}
static void thread_can_Receiver(void parameter)
{
rt_err_t ret;
int i;
/ 設(shè)置接收回調(diào)函數(shù) /
rt_device_set_rx_indicate(can_device, Can_ReceiverMessage_Call);
/
struct rt_can_filter_item items[5] =
{
{0x100, 0, 0, 0, 0x700, RT_NULL},
{0x300, 0, 0, 0, 0x700, RT_NULL},
{0x211, 0, 0, 0, 0x7ff, RT_NULL},
{0x486, 0, 0, 0, 0x7ff, RT_NULL},
{0x555, 0, 0, 0, 0x7ff, 7}
};
struct rt_can_filter_config cfg = {5,1,items};
ret = rt_device_control(can_device,RT_CAN_CMD_SET_FILTER,&cfg);
RT_ASSERT(ret == RT_EOK);
*/
while(1)
{
canrx_msg.hdr = -1;
rt_sem_take(&rx_sem, RT_WAITING_FOREVER);
rt_device_read(can_device, 0, &canrx_msg, sizeof(canrx_msg));
rt_kprintf("ID:%x",canrx_msg.id);
for(i=0; i <8; i++)
{
rt_kprintf(" %2x",canrx_msg.data[i]);
}
rt_kprintf("rn");
}
}
static void thread_can_send(void *parameter)
{
rt_size_t res;
static uint32_t i = 0;
static uint32_t a = 0xFFFFFFFF;
while(1)
{
i ++;
a --;
can_msg.id = 0x78;
can_msg.ide = RT_CAN_STDID;
can_msg.rtr = RT_CAN_DTR;
can_msg.len = 0x08;
can_msg.data[0] = i;
can_msg.data[1] = (i>>8);
can_msg.data[2] = (i>>16);
can_msg.data[3] = (i>>24);
can_msg.data[4] = 0x05;
can_msg.data[5] = 0x06;
can_msg.data[6] = 0x07;
can_msg.data[7] = 0x08;
res = rt_device_write(can_device, 0, &can_msg, sizeof(can_msg));
if(res == 0)
{
// rt_kprintf("Can Send a frame failed!rn");
}
else
{
// rt_kprintf("Can Send a frame success!rn");
}
can_msg.id = 0x778;
can_msg.ide = RT_CAN_STDID;
can_msg.rtr = RT_CAN_DTR;
can_msg.len = 0x08;
can_msg.data[0] = a;
can_msg.data[1] = (a>>8);
can_msg.data[2] = (a>>16);
can_msg.data[3] = (a>>24);
can_msg.data[4] = 0x05;
can_msg.data[5] = 0x06;
can_msg.data[6] = 0x07;
can_msg.data[7] = 0x08;
res = rt_device_write(can_device, 0, &can_msg, sizeof(can_msg));
if(res == 0)
{
// rt_kprintf("Can Send a frame failed!rn");
}
else
{
// rt_kprintf("Can Send a frame success!rn");
}
can_msg.id = 0x508;
can_msg.ide = RT_CAN_STDID;
can_msg.rtr = RT_CAN_DTR;
can_msg.len = 0x08;
can_msg.data[0] = 8;
can_msg.data[1] = 8;
can_msg.data[2] = 8;
can_msg.data[3] = 8;
can_msg.data[4] = 0x05;
can_msg.data[5] = 0x06;
can_msg.data[6] = 0x07;
can_msg.data[7] = 0x08;
res = rt_device_write(can_device, 0, &can_msg, sizeof(can_msg));
if(res == 0)
{
// rt_kprintf("Can Send a frame failed!rn");
}
else
{
// rt_kprintf("Can Send a frame success!rn");
}
rt_thread_mdelay(1);
}
}
int thread_CanDriver_Entry(void *parameter)
{
static char can_name[RT_NAME_MAX];
static uint32_t can_name_len = 0;
rt_err_t ret;
can_name_len = sizeof(CAN_DEV_NAME);
rt_strncpy(can_name, CAN_DEV_NAME, RT_NAME_MAX);
/ 在設(shè)備列表中查找設(shè)備 /
can_device = rt_device_find(can_name);
if(!can_device)
{
rt_kprintf("find %s failed!rn",can_name);
return RT_ERROR;
}
/ 初始化信號(hào)量 /
rt_sem_init(&rx_sem, "rx_sem", 0 ,RT_IPC_FLAG_FIFO);
/ 打開(kāi)設(shè)備 /
ret = rt_device_open(can_device, RT_DEVICE_FLAG_INT_TX | RT_DEVICE_FLAG_INT_RX);
RT_ASSERT(ret == RT_EOK);
thread_send = rt_thread_create("thread_send", thread_can_send,RT_NULL, 1024,25,1);
if(thread_send == NULL)
{
rt_kprintf("Create Can Send failed!rn");
return RT_ERROR;
}
else
{
rt_thread_startup(thread_send);
}
thread_rec = rt_thread_create("thread_Receiver", thread_can_Receiver,RT_NULL, 1024,25,1);
if(thread_send == NULL)
{
rt_kprintf("Create Can Send failed!rn");
return RT_ERROR;
}
else
{
rt_thread_startup(thread_rec);
}
}
MSH_CMD_EXPORT(thread_CanDriver_Entry, can device Driver sample);
4.測(cè)試效果圖
測(cè)試效果圖連接:
在測(cè)試時(shí),測(cè)試500Kbps,250bps,125bps波特率,報(bào)文發(fā)送時(shí)間間隔為10ms,開(kāi)發(fā)板發(fā)送3幀,接收一幀(由于測(cè)試CAN只支持1幀發(fā)送)
5.測(cè)試總結(jié)
在測(cè)試中發(fā)現(xiàn)在沒(méi)有安裝PyOCD,無(wú)法燒錄的問(wèn)題,同時(shí)還發(fā)現(xiàn)在發(fā)送CAN數(shù)據(jù)時(shí),執(zhí)行res = rt_device_write(can_device, 0, &can_msg, sizeof(can_msg));發(fā)送一幀CAN數(shù)據(jù)時(shí),通過(guò)CAN測(cè)試盒,明顯看到有報(bào)文發(fā)出,但返回狀態(tài)一直為0,于是跟蹤測(cè)試后發(fā)現(xiàn)在等待CAN發(fā)送后,沒(méi)有對(duì)發(fā)送結(jié)果進(jìn)行返回導(dǎo)致,于是修改代碼,如下圖所示:
另外也發(fā)現(xiàn)如果CAN的TX,RX不接CAN驅(qū)動(dòng)器時(shí),執(zhí)行發(fā)送數(shù)據(jù)后,代碼會(huì)進(jìn)入死循環(huán)。這個(gè)問(wèn)題是驅(qū)動(dòng)機(jī)制導(dǎo)致,如果遇到這個(gè)問(wèn)題可以查一下CAN驅(qū)動(dòng)接接線(xiàn),驅(qū)動(dòng)器是否工作。
總的說(shuō)來(lái),本次測(cè)試挺順利的,除了部分Bug外,CAN能夠?qū)崿F(xiàn)10ms多幀發(fā)送,并同時(shí)處理接收?qǐng)?bào)文。滿(mǎn)足日常設(shè)計(jì)中的CAN通訊需求。
-
驅(qū)動(dòng)器
+關(guān)注
關(guān)注
52文章
8226瀏覽量
146253 -
串口通訊
+關(guān)注
關(guān)注
1文章
258瀏覽量
24914 -
上位機(jī)
+關(guān)注
關(guān)注
27文章
941瀏覽量
54790 -
CAN模塊
+關(guān)注
關(guān)注
0文章
24瀏覽量
8747 -
RT-Thread
+關(guān)注
關(guān)注
31文章
1285瀏覽量
40085
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
評(píng)論