1.测试准备工作
1.1. 开发板硬件资源介绍:
1.2. 首先通过国民技术的技术资料网址,用浏览器打开后如下所示:
选择下载了N32L40XXX_V2.1.0.zip资料包后,解压得到官方Demo,数据手册,评估板,软件开发套件,应用笔记,测试资料等等。如下图所示:
1.3. 引脚资源分配介绍
本例中CAN的引脚使用GPIOB的引脚8和引脚9配置CAN的接收和发送引脚。将GPIOC的引脚11和引脚10配置为USART3的接收和发送引脚,通过USB转UART模块,晶振等公共资源就不一一介绍。
1.4. 功能模块介绍
本方案从软件上主要分为三部份,CAN模块,串口通讯模块和日志输出模块。
CAN模块:主要负责CAN报文的收发,报文解析。
串口通讯模块:将CAN模块接收的CAN报文传输至上位机,将上位机发送的CAN报文转给CAN模块发送到CAN网络中。
日志输出模块:输出系统运行过程中的日志。
1.5. 性能指标
本案例测试中,将测试波特率为500kbps,250kbps,125kbps三个波特率,具体指标为:
错误率:帧错误率:99%
采样率:85%
连续测试时长:72小时
2.环境搭建及创建工程。
2.1.首先到RT-Thread官方网站下载RT-Thread Studio,并安装好,启动RT-Thread Studio.点击SDK安装按钮,弹出下图所示界面。
2.3.还需要安装PyOCD,防止在下载程序时报错
2.4.在文件选择新建,由于不支持RT-Thread Nano,所以只能选择RT-Thread项目。
2.5. 在弹出菜单中,选择项目存放路径,输入项目名,选择基于开发板,并选择本次的测试开发板,点击确定,就创建了基于模板的工程。
2.6.在项目内的RT-Thread Settings中打开CAN模块功能,串口1用于日志打印。
3.RTThread的CAN驱动机制介绍
3.1. rtconfig.h配置文件介绍
在RT-Thread Setting中,打开USART3和CAN后,在rtconfig.h配置文件中可以看到定义了BSP_USING_CAN,RT_USING_PIN,RT_USING_SERIAL宏,表示启动了CAN驱动与设备。
3.2.CAN Drive 驱动介绍,
3.2.1.RT-Thread提供I/O设备管理接口来访问CAN硬件控制,接口如下:
3.2.2.通过INIT_BOARD_EXPORT将CAN的硬件驱动加载到初始化列表中,通过rt_hw_can_register将CAN注册到RT-Thread的设备列表中。
3.2.3.在调用rt_device_register函数,将设备注册到OS的设备列表中。
3.2.4.CAN波特率配置
3.3.测试代码
#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 设备名称 /
struct rt_semaphore rx_sem; / 用于接收消息的信号量 /
static rt_device_t can_device; / CAN 设备名称 */
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;
/ 设置接收回调函数 /
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);
/ 在设备列表中查找设备 /
can_device = rt_device_find(can_name);
if(!can_device)
{
rt_kprintf("find %s failed!rn",can_name);
return RT_ERROR;
}
/ 初始化信号量 /
rt_sem_init(&rx_sem, "rx_sem", 0 ,RT_IPC_FLAG_FIFO);
/ 打开设备 /
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.测试效果图
测试效果图连接:
在测试时,测试500Kbps,250bps,125bps波特率,报文发送时间间隔为10ms,开发板发送3帧,接收一帧(由于测试CAN只支持1帧发送)
5.测试总结
在测试中发现在没有安装PyOCD,无法烧录的问题,同时还发现在发送CAN数据时,执行res = rt_device_write(can_device, 0, &can_msg, sizeof(can_msg));发送一帧CAN数据时,通过CAN测试盒,明显看到有报文发出,但返回状态一直为0,于是跟踪测试后发现在等待CAN发送后,没有对发送结果进行返回导致,于是修改代码,如下图所示:
另外也发现如果CAN的TX,RX不接CAN驱动器时,执行发送数据后,代码会进入死循环。这个问题是驱动机制导致,如果遇到这个问题可以查一下CAN驱动接接线,驱动器是否工作。
总的说来,本次测试挺顺利的,除了部分Bug外,CAN能够实现10ms多帧发送,并同时处理接收报文。满足日常设计中的CAN通讯需求。
-
驱动器
+关注
关注
52文章
8208浏览量
146227 -
串口通讯
+关注
关注
1文章
258浏览量
24908 -
上位机
+关注
关注
27文章
941浏览量
54785 -
CAN模块
+关注
关注
0文章
24浏览量
8746 -
RT-Thread
+关注
关注
31文章
1285浏览量
40060
发布评论请先 登录
相关推荐
评论