Toggle navigation
首页
问答
文章
积分商城
专家
专区
更多专区...
文档中心
返回主站
搜索
提问
会员
中心
登录
注册
CAN总线
NXP 微控制器
MCAX-156 CAN测评
发布于 2025-02-26 21:20:06 浏览:19
订阅该版
[tocm] # FRDM-MCXA156开发板介绍  FRDM-MCXA156开发板基于 Arm® Cortex®-M33 核心,提供高达 1MB 的闪存 和 128KB 的 RAM,其中还包括 8KB 的 ECC 内存保护,同时支持CAN-FD, FlexIO, FS USB, I3C, LPLUART, LPSPI, LPI2C, DMA 和 LDO,支持板载的CMSIS-DAP以及JTAG/SWD 本文主要测评的开发板 FlexCan 采用TJA1057CAN 收发器。 # 开发资料 RT-Thread源码: https://github.com/RT-Thread/rt-thread RT-Thread Env工具: https://www.rt-thread.org/download.html 官方SDK需要注册NXP官网账号,进行下载。 # CAN总线  # 操作步骤 RT-Thread Packes地址:https://gitee.com/rtthread/rt-thread ,使用gitee下载 FRDM-CMXA BSP文件地址:F:\rt-thread-master\bsp\nxp\mcx\mcxa\frdm-mcxa156 使用env工具打开 使用menuconfig命令对工程进行配置:   配置完成后保存配置,使用`scons --target=mdk5`构建工程 打开`F:\rt-thread-master\bsp\nxp\mcx\mcxa\frdm-mcxa156`目录下的project工程 ```c #include
#include "rtdevice.h" #define CAN_DEV_NAME "can1" // CAN 设备名称 static struct rt_semaphore rx_sem; // 用于接收消息的信号量 static rt_device_t can_dev; // CAN 设备句柄 // 接收数据回调函数 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:0x100~0x1ff,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 res = RT_TRUE; res = rt_device_control(can_dev, RT_CAN_CMD_START, &res); 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]); } rt_kprintf("\n"); } } int can_sample(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); // 创建数据接收线程 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; // 标准格式 msg.rtr = RT_CAN_DTR; // 数据帧 msg.len = 8; // 数据长度为 8 // 待发送的 8 字节数据 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; // 发送一帧 CAN 数据 size = rt_device_write(can_dev, 0, &msg, sizeof(msg)); if (size == 0) { rt_kprintf("can dev write data failed!\n"); } return res; } MSH_CMD_EXPORT(can_sample, can device sample); ``` 使用该代码进行测试,编译烧录程序,进行测试。 使用can_sample命令后发现设备打开失败,原因是,Rt-Thread官方还未对can进行配置驱动,由于制作drv驱动耗时较久,小编需要在本周六完成文章,因此下篇文章再对其进行适配,本文先使用NXP官方demo测试该开发板的CAN总线。  测试过程中需要注意,必须如果使能CAN FD必须使用CAN FD盒子才能侦测到CAN数据,这也是汽车电子开发过程中经常遇到的。
0
条评论
默认排序
按发布时间排序
登录
注册新账号
关于作者
UVUE_4247
这家伙很懒,什么也没写!
文章
1
回答
0
被采纳
0
关注TA
发私信
相关文章
1
试贴-消灭0主题
2
LPC M4的一些资料
3
LPC4088的临时分支
4
lpc1788 ad 不稳定
5
1788 LCD控制器缓冲区字节问题
6
一起来学习LPC4088吧
7
上传LPC4088的realtouch主工程
8
RealBoard 4088预定帖 [第一批板子不多,预定提前结束]
9
晒RealBoard LPC4088开箱照啦,速带小板凳前来围观
10
4088主程序需要的SD卡资源
推荐文章
1
RT-Thread应用项目汇总
2
玩转RT-Thread系列教程
3
国产MCU移植系列教程汇总,欢迎查看!
4
机器人操作系统 (ROS2) 和 RT-Thread 通信
5
五分钟玩转RT-Thread新社区
6
【技术三千问】之《玩转ART-Pi》,看这篇就够了!干货汇总
7
关于STM32H7开发板上使用SDIO接口驱动SD卡挂载文件系统的问题总结
8
STM32的“GPU”——DMA2D实例详解
9
RT-Thread隐藏的宝藏之completion
10
【ART-PI】RT-Thread 开启RTC 与 Alarm组件
热门标签
RT-Thread Studio
串口
Env
LWIP
SPI
AT
Bootloader
Hardfault
CAN总线
FinSH
ART-Pi
DMA
USB
文件系统
RT-Thread
SCons
RT-Thread Nano
线程
MQTT
STM32
RTC
rt-smart
FAL
I2C_IIC
UART
ESP8266
cubemx
WIZnet_W5500
ota在线升级
PWM
BSP
flash
freemodbus
packages_软件包
潘多拉开发板_Pandora
定时器
ADC
GD32
flashDB
编译报错
socket
中断
Debug
rt_mq_消息队列_msg_queue
keil_MDK
ulog
SFUD
msh
C++_cpp
MicroPython
本月问答贡献
RTT_逍遥
12
个答案
3
次被采纳
xiaorui
3
个答案
3
次被采纳
踩姑娘的小蘑菇
6
个答案
2
次被采纳
winfeng
2
个答案
2
次被采纳
三世执戟
11
个答案
1
次被采纳
本月文章贡献
catcatbing
3
篇文章
5
次点赞
lizimu
2
篇文章
9
次点赞
jinchanchan
2
篇文章
2
次点赞
alight
1
篇文章
5
次点赞
swet123
1
篇文章
4
次点赞
回到
顶部
发布
问题
投诉
建议
回到
底部