Toggle navigation
首页
问答
文章
积分商城
专家
专区
更多专区...
文档中心
返回主站
搜索
提问
会员
中心
登录
注册
RT-Thread一般讨论
基于最新版CubeMx制作的bsp包,使用rt-thread 4.0.2,CAN设备不.....
发布于 2020-01-08 14:34:12 浏览:1926
订阅该版
制作过程: 1、cube配置。 ![110,0](https://www.rt-thread.org/qa/forum.php?mod=image&aid=13149&size=300x300&key=6daaad7dec098e16&nocache=yes&type=fixnone) 2、menuconfig menuconfig BSP_USING_CAN bool "Enable CAN" default n select RT_USING_CAN if BSP_USING_CAN config BSP_USING_CAN1 bool "Enable CAN1" default n endif 3、使用例程(官方例程不做改动)。 #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 while (1) { /* hdr值为-1,表示直接从uselist链表读取数据 */ rxmsg.hdr = -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*); } rt_kprintf("
"); } } 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_dev = rt_device_find(can_name); if (!can_dev) { rt_kprintf("find %s failed!
", 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!
"); } 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!
"); } return res; } /* 导出到 msh 命令列表中 */ MSH_CMD_EXPORT(can_sample, can device sample); 结果: CAN读写 写:can dev write data failed! 读:没有数据 4、使用裸机代码对比 #include
#include "rtdevice.h" #include
CAN_HandleTypeDef hcan1; CAN_TxHeaderTypeDef TxHeader; CAN_RxHeaderTypeDef RxHeader; uint8_t TxData[8]; uint8_t RxData[8]; uint8_t rx_flag = 0; /* 接收数据回调函数 */ void HAL_CAN_RxFifo0MsgPendingCallback ( CAN_HandleTypeDef* hcan ) { if ( hcan ==&hcan1 ) { /* Get RX message */ if ( HAL_CAN_GetRxMessage ( hcan, CAN_RX_FIFO0, &RxHeader, RxData ) != HAL_OK ) { /* Reception Error */ //Error_Handler(); } /* Display LEDx */ if ( ( RxHeader.StdId == 0x321 ) && ( RxHeader.IDE == CAN_ID_STD ) && ( RxHeader.DLC == 8 ) ) { rt_kprintf ( "rev
" ); } rx_flag = 1; } rt_kprintf ( "rev
" ); } static void can_rx_thread ( void* parameter ) { uint8_t i=0; uint32_t TxMailbox; uint8_t message[8]; while ( 1 ) { /* 阻塞等待接收信号量 */ if ( rx_flag == 1 ) { /* 打印数据ID及内容 */ rt_kprintf ( "ID:%x ",RxHeader.StdId ); for ( i = 0; i < 8; i++ ) { rt_kprintf ( "%2x ",RxData* ); } rt_kprintf ( "
" ); rx_flag = 0; } else { TxHeader.StdId=0X12; //标准标识符 TxHeader.ExtId=0x12; //扩展标识符(29位) TxHeader.IDE=CAN_ID_STD; //使用标准帧 TxHeader.RTR=CAN_RTR_DATA; //数据帧 TxHeader.DLC=8; for ( i=0; i<8; i++ ) { message*=i; } if ( HAL_CAN_AddTxMessage ( &hcan1, &TxHeader, message, &TxMailbox ) != HAL_OK ) //发送 { rt_kprintf ( "send fail!
" );; } while ( HAL_CAN_GetTxMailboxesFreeLevel ( &hcan1 ) != 3 ) { } rt_kprintf ( "test can
" ); } rt_thread_mdelay ( 1000 ); } } int can_test_sample ( void ) { CAN_FilterTypeDef sFilterConfig; rt_err_t res; rt_thread_t thread; hcan1.Instance = CAN1; hcan1.Init.Prescaler = 6; hcan1.Init.Mode = CAN_MODE_NORMAL; hcan1.Init.SyncJumpWidth = CAN_SJW_1TQ; hcan1.Init.TimeSeg1 = CAN_BS1_7TQ; hcan1.Init.TimeSeg2 = CAN_BS2_6TQ; hcan1.Init.TimeTriggeredMode = DISABLE; hcan1.Init.AutoBusOff = DISABLE; hcan1.Init.AutoWakeUp = DISABLE; hcan1.Init.AutoRetransmission = DISABLE; hcan1.Init.ReceiveFifoLocked = DISABLE; hcan1.Init.TransmitFifoPriority = DISABLE; if ( HAL_CAN_Init ( &hcan1 ) != HAL_OK ) { while ( 1 ) { } } /*##-2- Configure the CAN Filter ###########################################*/ sFilterConfig.FilterBank = 0; sFilterConfig.FilterMode = CAN_FILTERMODE_IDMASK; sFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT; sFilterConfig.FilterIdHigh = 0x0000; sFilterConfig.FilterIdLow = 0x0000; sFilterConfig.FilterMaskIdHigh = 0x0000; sFilterConfig.FilterMaskIdLow = 0x0000; sFilterConfig.FilterFIFOAssignment = CAN_RX_FIFO0; sFilterConfig.FilterActivation = ENABLE; sFilterConfig.SlaveStartFilterBank = 14; if ( HAL_CAN_ConfigFilter ( &hcan1, &sFilterConfig ) != HAL_OK ) { /* Filter configuration Error */ while ( 1 ) { } } /*##-3- Start the CAN peripheral ###########################################*/ if ( HAL_CAN_Start ( &hcan1 ) != HAL_OK ) { /* Start Error */ while ( 1 ) { } } /*##-4- Activate CAN RX notification #######################################*/ if ( HAL_CAN_ActivateNotification ( &hcan1, CAN_IT_RX_FIFO0_MSG_PENDING ) != HAL_OK ) { /* Notification Error */ while ( 1 ) { } } thread = rt_thread_create ( "can_test_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!
" ); } return res; } 结果: CAN设备收发 发:能发出数据并且·能收到 ![110,0](https://www.rt-thread.org/qa/forum.php?mod=image&aid=13150&size=300x300&key=4ec5b13878fecc20&nocache=yes&type=fixnone) 收:导致程序死机 5、遇到的问题 硬件:stm32f407VE cubemx:5.4.0 按照配置出来的can设备能注册能配置,但是并不能进行收发。但是用裸机代码直接用在系统上也是可以发送的,只在使用103的设备上用过 can设备,但是用在407上所做出来的can不能使用,各位有遇到过这一类的问题吗,请大家指点一下。 ![cube.png](https://oss-club.rt-thread.org/uploads/202001/08/141954ilyxl8313dzekplx.png) ![cannet.png](https://oss-club.rt-thread.org/uploads/202001/08/142813vq7oxzvhbxb69hvq.png)
查看更多
3
个回答
默认排序
按发布时间排序
小白白又白
2020-01-08
这家伙很懒,什么也没写!
在线等
小白白又白
2020-01-08
这家伙很懒,什么也没写!
改用老版本CAN驱动,新驱动就暂时不用。
billylv
2020-02-22
这家伙很懒,什么也没写!
[i=s] 本帖最后由 billylv 于 2020-2-22 20:50 编辑 [/i] 你可以看一下 波特率设置的部分 ``` #if defined (SOC_SERIES_STM32F1) static const struct stm_baud_rate_tab can_baud_rate_tab[] = { {CAN1MBaud, (CAN_SJW_2TQ | CAN_BS1_8TQ | CAN_BS2_3TQ | 3)}, {CAN800kBaud, (CAN_SJW_2TQ | CAN_BS1_5TQ | CAN_BS2_3TQ | 5)}, {CAN500kBaud, (CAN_SJW_2TQ | CAN_BS1_8TQ | CAN_BS2_3TQ | 6)}, {CAN250kBaud, (CAN_SJW_2TQ | CAN_BS1_8TQ | CAN_BS2_3TQ | 12)}, {CAN125kBaud, (CAN_SJW_2TQ | CAN_BS1_8TQ | CAN_BS2_3TQ | 24)}, {CAN100kBaud, (CAN_SJW_2TQ | CAN_BS1_8TQ | CAN_BS2_3TQ | 30)}, {CAN50kBaud, (CAN_SJW_2TQ | CAN_BS1_8TQ | CAN_BS2_3TQ | 60)}, {CAN20kBaud, (CAN_SJW_2TQ | CAN_BS1_8TQ | CAN_BS2_3TQ | 150)}, {CAN10kBaud, (CAN_SJW_2TQ | CAN_BS1_8TQ | CAN_BS2_3TQ | 300)} }; #elif defined (SOC_SERIES_STM32F4) static const struct stm_baud_rate_tab can_baud_rate_tab[] = { {CAN1MBaud, (CAN_SJW_2TQ | CAN_BS1_9TQ | CAN_BS2_4TQ | 3)}, {CAN800kBaud, (CAN_SJW_2TQ | CAN_BS1_8TQ | CAN_BS2_4TQ | 4)}, {CAN500kBaud, (CAN_SJW_2TQ | CAN_BS1_9TQ | CAN_BS2_4TQ | 6)}, {CAN250kBaud, (CAN_SJW_2TQ | CAN_BS1_9TQ | CAN_BS2_4TQ | 12)}, {CAN125kBaud, (CAN_SJW_2TQ | CAN_BS1_9TQ | CAN_BS2_4TQ | 24)}, {CAN100kBaud, (CAN_SJW_2TQ | CAN_BS1_9TQ | CAN_BS2_4TQ | 30)}, {CAN50kBaud, (CAN_SJW_2TQ | CAN_BS1_9TQ | CAN_BS2_4TQ | 60)}, {CAN20kBaud, (CAN_SJW_2TQ | CAN_BS1_9TQ | CAN_BS2_4TQ | 150)}, {CAN10kBaud, (CAN_SJW_2TQ | CAN_BS1_9TQ | CAN_BS2_4TQ | 300)} }; #endif ```
撰写答案
登录
注册新账号
关注者
0
被浏览
1.9k
关于作者
小白白又白
这家伙很懒,什么也没写!
提问
9
回答
18
被采纳
0
关注TA
发私信
相关问题
1
有关动态模块加载的一篇论文
2
最近的调程序总结
3
晕掉了,这么久都不见layer2的踪影啊
4
继续K9ii的历程
5
[GUI相关] FreeType 2
6
[GUI相关]嵌入式系统中文输入法的设计
7
20081101 RT-Thread开发者聚会总结
8
嵌入式系统基础
9
linux2.4.19在at91rm9200 上的寄存器设置
10
[转]基于嵌入式Linux的通用触摸屏校准程序
推荐文章
1
RT-Thread应用项目汇总
2
玩转RT-Thread系列教程
3
机器人操作系统 (ROS2) 和 RT-Thread 通信
4
国产MCU移植系列教程汇总,欢迎查看!
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组件
最新文章
1
新手教程-Pin设备使用
2
基于嵌入式的智能关怀系统#24嵌入式设计大赛
3
基于机器视觉识别自动跟踪器
4
[24嵌入式设计大赛]基于Vision-Board的智能应急环境监测控制小车
5
【24嵌入式设计大赛】基于RT-Thread VisionBoard开发版的AI识别宠物
热门标签
RT-Thread Studio
串口
Env
LWIP
SPI
AT
Bootloader
Hardfault
CAN总线
ART-Pi
FinSH
USB
文件系统
DMA
RT-Thread
SCons
RT-Thread Nano
线程
MQTT
STM32
RTC
rt-smart
FAL
ESP8266
I2C_IIC
ota在线升级
WIZnet_W5500
UART
flash
packages_软件包
cubemx
PWM
freemodbus
BSP
潘多拉开发板_Pandora
定时器
ADC
中断
flashDB
socket
Debug
GD32
编译报错
msh
keil_MDK
at_device
MicroPython
rt_mq_消息队列_msg_queue
ulog
C++_cpp
本月问答贡献
rv666
6
个答案
1
次被采纳
踩姑娘的小蘑菇
5
个答案
1
次被采纳
用户名由3_15位
5
个答案
1
次被采纳
xiaorui
2
个答案
1
次被采纳
张世争
2
个答案
1
次被采纳
本月文章贡献
jaffer
1
篇文章
5
次点赞
rtt_dmx
1
篇文章
4
次点赞
BRICK PORTER
1
篇文章
2
次点赞
不灬忘初心
1
篇文章
2
次点赞
flytianya2010
1
篇文章
1
次点赞
回到
顶部
发布
问题
分享
好友
手机
浏览
扫码手机浏览
投诉
建议
回到
底部