RT-Thread Studio上的CAN实践步骤说明

电子说

1.3w人已加入

描述

1.前言说明
CAN 是Controller Area Network 的缩写(以下称为CAN),是ISO*1 国际标准化的串行通信协议。
在当前的汽车产业中,出于对安全性、舒适性、方便性、低公害、低成本的要求,各种各样的电子控制系统
被开发了出来。由于这些系统之间通信所用的数据类型及对可靠性的要求不尽相同,由多条总线构成的情况很
多,线束的数量也随之增加。为适应“减少线束的数量”、“通过多个LAN,进行大量数据的高速通信”的需
要,1986 年德国电气商博世公司开发出面向汽车的CAN 通信协议。此后,CAN 通过ISO11898 及ISO11519 进行了标准化,现在在欧洲已是汽车网络的标准协议。现在,CAN 的高性能和可靠性已被认同,并被广泛地应用于工业自动化、船舶、医疗设备、工业设备等方面。
瑞萨在RA6M3系列芯片 集成CAN总线控制器。Controller Area Network (CAN) Module 详细参考《Renesas RA6M3 Group User’s Manual: Hardware》

1.1.本章内容
使用RT-Thread Studio来创建工程,配置CAN接口驱动,编写CAN接口测试程序,实现瑞萨RA6M3芯片和上 位机CAN通信(数据接收与发送)。

1.2.模块介绍
CAN接口图

RT-Thread

RT-Thread

1.3.开发软件
RT-Thread Studio , RA Smart Configurator

2.步骤说明
2.1.新建工程

RT-Thread

RT-Thread

2.2.编写测试程序

/*

Copyright (c) 2006-2021, RT-Thread Development Team

SPDX-License-Identifier: Apache-2.0

Change Logs:
Date Author Notes
2023-06-15 Administrator the first version
/
#include
#include "hal_data.h"
#include
#define CAN_DEV_NAME "can0" /
CAN 设备名称 /
static struct rt_semaphore rx_sem; /
用于接收消息的信号量 /
static rt_device_t can_dev; /
CAN 设备句柄 */
static int can_echo( rt_uint8_t p_buff );
/
接收数据回调函数 /
static rt_err_t can_rx_call(rt_device_t dev, rt_size_t size)
{
/
CAN 接收到数据后产生中断,调用此回调函数,然后发送接收信号量 */
rt_sem_release(&rx_sem);
return RT_EOK;
}
static void can_rx_thread(void parameter)
{
int i;
rt_err_t res;
struct rt_can_msg rxmsg = {0};
/
设置接收回调函数 /
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,设置默认过滤表 /
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,指定设置 7 号过滤表 /
};
struct rt_can_filter_config cfg = {5, 1, items}; /
一共有 5 个过滤表 /
/
设置硬件过滤表 /
res = rt_device_control(can_dev, RT_CAN_CMD_SET_FILTER, &cfg);
RT_ASSERT(res == RT_EOK);
#endif
while (1)
{
/
hdr 值为 - 1,表示直接从 uselist 链表读取数据 /
//rxmsg.hdr_index = -1;
/
阻塞等待接收信号量 /
rt_sem_take(&rx_sem, RT_WAITING_FOREVER);
/
从 CAN 读取一帧数据 /
rt_device_read(can_dev, 0, &rxmsg, sizeof(rxmsg));
/
打印数据 ID 及内容 */
rt_kprintf("ID:%x", rxmsg.id);
for (i = 0; i < 8; i++)
{
rt_kprintf("%2x", rxmsg.data[i]);
}
can_echo( &rxmsg.data[0]);
rt_kprintf("n");
}
}
static int can_echo( rt_uint8_t p_buff )
{
struct rt_can_msg msg = {0};
rt_size_t size;
msg.id = 0x03; /
ID 为 0x03 /
msg.ide = RT_CAN_STDID; /
标准格式 /
msg.rtr = RT_CAN_DTR; /
数据帧 /
msg.len = 8; /
数据长度为 8 /
rt_memcpy(&msg.data[0], p_buff, 8);
/
发送一帧 CAN 数据 */
size = rt_device_write(can_dev, 0, &msg, sizeof(msg) );
if (size == 0)
{
rt_kprintf("can dev write data failed!n");
}
}
int can_test(int argc, char argv[])
{
struct rt_can_msg msg = {0};
rt_err_t res;
rt_size_t size;
rt_thread_t thread;
char can_name[RT_NAME_MAX];
if (argc == 2)
{
rt_strncpy(can_name, argv[1], RT_NAME_MAX);
}
else
{
rt_strncpy(can_name, CAN_DEV_NAME, RT_NAME_MAX);
}
/
查找 CAN 设备 /
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);
/
以中断接收及发送方式打开 CAN 设备 /
res = rt_device_open(can_dev, RT_DEVICE_FLAG_INT_TX | RT_DEVICE_FLAG_INT_RX);
RT_ASSERT(res == RT_EOK);
/
设置 CAN 的工作模式为正常工作模式 /
//res = rt_device_control(can_dev, RT_CAN_CMD_SET_MODE, (void )RT_CAN_MODE_NORMAL);
/
设置 CAN 通信的波特率为 100kbit/s
/
//res = rt_device_control(can_dev, RT_CAN_CMD_SET_BAUD, (void )CAN100kBaud);
/
创建数据接收线程 /
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");
}
return res;
}
/
导出到 msh 命令列表中 */
MSH_CMD_EXPORT(can_test, can device sample);

RT-Thread

3.代码验证
编译程序并下载到开发板

RT-Thread

使用CANFD(USB转CAN )发送和接收开发板CAN通信数据

RT-Thread

4.章节总结
本章主要是验证了开发板和上位机CAN通信,RT-THREAD 开发板BSP 适配的很好,各种软件组件比较丰富,可以节省很多开发时间。

打开APP阅读更多精彩内容
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉

全部0条评论

快来发表一下你的评论吧 !

×
20
完善资料,
赚取积分