Toggle navigation
首页
问答
文章
积分商城
专家
专区
更多专区...
文档中心
返回主站
搜索
提问
会员
中心
登录
注册
CAN总线
求教,STM32F105的CAN2接收不到数据
发布于 2019-08-24 00:47:31 浏览:3177
订阅该版
最近用STM32F105,用到了双CAN CAN1能发能收,但是CAN2只能发送,收不到数据,求教是什么问题? CAN1与CAN2代码基本上一样的。 初始化代码 struct can_configure g_can2_config; g_can2_config.baud_rate = CAN50kBaud; g_can2_config.msgboxsz = 32; g_can2_config.sndboxnumber = 3; g_can2_config.mode = RT_CAN_MODE_NORMAL; g_can2_config.privmode = 0; g_can2_config.ticks = 50; //g_can2_config.reserved = 0; //g_can2_config.maxhdr = ; rt_sem_init(&l_can2_sem, "can2_sem", 0, RT_IPC_FLAG_FIFO); l_can2_dev = rt_device_find("can2"); if(!l_can2_dev) { rt_kprintf("Find can2 failed!
"); return; } ret = rt_device_init(l_can2_dev); //Init can1 if(ret != RT_EOK) { rt_kprintf("Init can2 failed!
"); } ret = rt_device_control(l_can2_dev, RT_DEVICE_CTRL_CONFIG, &g_can2_config); if(ret != RT_EOK) { rt_kprintf("Can2 config failed!
"); return; } ret = rt_device_open(l_can2_dev, RT_DEVICE_OFLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_INT_TX); if(ret == RT_EOK) { rt_device_set_rx_indicate(l_can2_dev, can2_input); } else { rt_kprintf("Open can2 failed!
"); return; } 下面是接收代码 static rt_err_t can2_input(rt_device_t dev, rt_size_t size) { rt_sem_release(&l_can2_sem); return RT_EOK; } void rt_can2_rx_thread_entry(void* parameter) { int i; struct rt_can_msg rx_msg; rt_uint8_t uart_buf[UART_FRAME_LEN]; while(1) { if (rt_sem_take(&l_can2_sem, RT_WAITING_FOREVER) != RT_EOK) { continue; } while (rt_device_read(l_can2_dev, 0, &rx_msg, sizeof(rx_msg)) == sizeof(rx_msg)) { rt_kprintf("CAN2 rece data id: 0x%08X ide:%d rtr:%d
", rx_msg.id, rx_msg.ide, rx_msg.rtr);
查看更多
5
个回答
默认排序
按发布时间排序
tyustli
2019-08-24
这家伙很懒,什么也没写!
看一下,硬件引脚初始化了吗?
Iasaka
2019-08-24
这家伙很懒,什么也没写!
>看一下,硬件引脚初始化了吗? --- 看GPIO的寄存器配置没问题。 我直接修改了滤波器的代码。不知道那里不对,不能使能CAN2的滤波器。
tyustli
2019-08-24
这家伙很懒,什么也没写!
[i=s] 本帖最后由 tyustli 于 2019-8-24 17:43 编辑 [/i] >看GPIO的寄存器配置没问题。 >我直接修改了滤波器的代码。不知道那里不对,不能使能CAN2的滤波器。 ... --- CAN1 和 CAN2 是共用一组 28/14 个验收筛选器的,数据手册中有写
Iasaka
2019-08-24
这家伙很懒,什么也没写!
>CAN1 和 CAN2 是共用一组 28/14 个验收筛选器的,数据手册中有写 --- 是的,我用的V3.1.3的版本。主要不明白的地方是,我配置了下CAN2的滤波器。调试的时候发现,CAN2的14滤波器并没有使能,不知道那里有问题,还有14的掩码也没有修改,我设置成全接收。0x00000000 后来我直接修改了库的代码,到时能用了,但是感觉不是很妥当。 原有代码: HAL_StatusTypeDef HAL_CAN_ConfigFilter(CAN_HandleTypeDef* hcan, CAN_FilterConfTypeDef* sFilterConfig) { uint32_t filternbrbitpos = 0U; /* Prevent unused argument(s) compilation warning */ UNUSED(hcan); /* Check the parameters */ assert_param(IS_CAN_FILTER_NUMBER(sFilterConfig->FilterNumber)); assert_param(IS_CAN_FILTER_MODE(sFilterConfig->FilterMode)); assert_param(IS_CAN_FILTER_SCALE(sFilterConfig->FilterScale)); assert_param(IS_CAN_FILTER_FIFO(sFilterConfig->FilterFIFOAssignment)); assert_param(IS_FUNCTIONAL_STATE(sFilterConfig->FilterActivation)); assert_param(IS_CAN_BANKNUMBER(sFilterConfig->BankNumber)); filternbrbitpos = (1U) << sFilterConfig->FilterNumber; /* Initialisation mode for the filter */ /* Select the start slave bank */ MODIFY_REG(hcan->Instance->FMR , CAN_FMR_CAN2SB , CAN_FMR_FINIT | (uint32_t)(sFilterConfig->BankNumber << 8U) ); /* Filter Deactivation */ CLEAR_BIT(hcan->Instance->FA1R, filternbrbitpos); /* Filter Scale */ if (sFilterConfig->FilterScale == CAN_FILTERSCALE_16BIT) { /* 16-bit scale for the filter */ CLEAR_BIT(hcan->Instance->FS1R, filternbrbitpos); /* First 16-bit identifier and First 16-bit mask */ /* Or First 16-bit identifier and Second 16-bit identifier */ hcan->Instance->sFilterRegister[sFilterConfig->FilterNumber].FR1 = ((0x0000FFFFU & (uint32_t)sFilterConfig->FilterMaskIdLow) << 16U) | (0x0000FFFFU & (uint32_t)sFilterConfig->FilterIdLow); /* Second 16-bit identifier and Second 16-bit mask */ /* Or Third 16-bit identifier and Fourth 16-bit identifier */ hcan->Instance->sFilterRegister[sFilterConfig->FilterNumber].FR2 = ((0x0000FFFFU & (uint32_t)sFilterConfig->FilterMaskIdHigh) << 16U) | (0x0000FFFFU & (uint32_t)sFilterConfig->FilterIdHigh); } if (sFilterConfig->FilterScale == CAN_FILTERSCALE_32BIT) { /* 32-bit scale for the filter */ SET_BIT(hcan->Instance->FS1R, filternbrbitpos); /* 32-bit identifier or First 32-bit identifier */ hcan->Instance->sFilterRegister[sFilterConfig->FilterNumber].FR1 = ((0x0000FFFFU & (uint32_t)sFilterConfig->FilterIdHigh) << 16U) | (0x0000FFFFU & (uint32_t)sFilterConfig->FilterIdLow); /* 32-bit mask or Second 32-bit identifier */ hcan->Instance->sFilterRegister[sFilterConfig->FilterNumber].FR2 = ((0x0000FFFFU & (uint32_t)sFilterConfig->FilterMaskIdHigh) << 16U) | (0x0000FFFFU & (uint32_t)sFilterConfig->FilterMaskIdLow); } /* Filter Mode */ if (sFilterConfig->FilterMode == CAN_FILTERMODE_IDMASK) { /*Id/Mask mode for the filter*/ CLEAR_BIT(hcan->Instance->FM1R, filternbrbitpos); } else /* CAN_FilterInitStruct->CAN_FilterMode == CAN_FilterMode_IdList */ { /*Identifier list mode for the filter*/ SET_BIT(hcan->Instance->FM1R, filternbrbitpos); } /* Filter FIFO assignment */ if (sFilterConfig->FilterFIFOAssignment == CAN_FILTER_FIFO0) { /* FIFO 0 assignation for the filter */ CLEAR_BIT(hcan->Instance->FFA1R, filternbrbitpos); } else { /* FIFO 1 assignation for the filter */ SET_BIT(hcan->Instance->FFA1R, filternbrbitpos); } /* Filter activation */ if (sFilterConfig->FilterActivation == ENABLE) { SET_BIT(hcan->Instance->FA1R, filternbrbitpos); } /* Leave the initialisation mode for the filter */ CLEAR_BIT(hcan->Instance->FMR, ((uint32_t)CAN_FMR_FINIT)); /* Return function status */ return HAL_OK; } 我直接定死了 HAL_StatusTypeDef HAL_CAN_ConfigFilterConfig(void) { /* Initialisation mode for the filter */ /* Select the start slave bank */ MODIFY_REG(CAN1->FMR , CAN_FMR_CAN2SB , CAN_FMR_FINIT | (uint32_t)(14 << 8U) ); /* Filter Deactivation */ CLEAR_BIT(CAN1->FA1R, 0x00004001); //1,14Â˲¨Æ÷ /* 32-bit scale for the filter */ SET_BIT(CAN1->FS1R, 0x00004001); /* 32-bit identifier or First 32-bit identifier */ CAN1->sFilterRegister[0].FR1 = 0x00000000; /* 32-bit mask or Second 32-bit identifier */ CAN1->sFilterRegister[0].FR2 = 0x00000000; /* 32-bit identifier or First 32-bit identifier */ CAN1->sFilterRegister[14].FR1 = 0x00000000; /* 32-bit mask or Second 32-bit identifier */ CAN1->sFilterRegister[14].FR2 = 0x00000000; /* Filter Mode */ /*Id/Mask mode for the filter*/ CLEAR_BIT(CAN1->FM1R, 0x00004001); /* Filter FIFO assignment */ /* FIFO 0 assignation for the filter */ CLEAR_BIT(CAN1->FFA1R, 0x00000001); /* FIFO 1 assignation for the filter */ SET_BIT(CAN1->FFA1R, 0x00004000); /* Filter activation */ SET_BIT(CAN1->FA1R, 0x00004001); /* Leave the initialisation mode for the filter */ CLEAR_BIT(CAN1->FMR, ((uint32_t)CAN_FMR_FINIT)); CLEAR_BIT(CAN2->FMR, ((uint32_t)CAN_FMR_FINIT)); /* Return function status */ return HAL_OK; } 调用的时候 static rt_err_t drv_configure(struct rt_can_device *dev_can, struct can_configure *cfg) { ******省略 /* Filter conf */ filterConf = &drv_can->FilterConfig; filterConf->FilterNumber = 0; filterConf->FilterMode = CAN_FILTERMODE_IDMASK; filterConf->FilterScale = CAN_FILTERSCALE_32BIT; filterConf->FilterIdHigh = 0x0000; filterConf->FilterIdLow = 0x0000; filterConf->FilterMaskIdHigh = 0x0000; filterConf->FilterMaskIdLow = 0x0000; filterConf->FilterFIFOAssignment = CAN_FIFO0; filterConf->FilterActivation = ENABLE; filterConf->BankNumber = 14; HAL_CAN_ConfigFilterConfig();//修改了代码,直接配置
whj467467222
认证专家
2019-08-25
开源,分享,交流,共同进步
先用裸机把CAN2调通了在去上RTOS,F105的CAN2注意是从滤波器14开始的。
撰写答案
登录
注册新账号
关注者
0
被浏览
3.2k
关于作者
Iasaka
这家伙很懒,什么也没写!
提问
2
回答
5
被采纳
0
关注TA
发私信
相关问题
1
我也来传一个CANOpen移植,RTT+STM32F107+CanOpenNode
2
谁有STM32裸跑的CANopen程序啊???
3
CAN驱动程序框架
4
CAN驱动接口如何规范一下
5
RTT无法进入线程.Cannot access Memory
6
编译提示arm-none-eabi/bin/ld: cannot find crt0.o: No such file o
7
rtt 2.1.0 正式版 mdk4 bsp/stm32 编译canapp.c错误
8
STM32F10XCAN驱动使用的问题
9
2.1版本stm32f10x分支bxcan驱动波特率设置的bug
10
rtthread2.1.0下,找不到can1设备
推荐文章
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组件
最新文章
1
freemodbus主机在freertos的适配,参考rtthread例程
2
开源共生 商业共赢 | RT-Thread 2024开发者大会议程正式发布!
3
【24嵌入式设计大赛】基于RT-Thread星火一号的智慧家居系统
4
RT-Thread EtherKit开源以太网硬件正式发布
5
还在担心bsp不好维护吗?快使用yml管理主线bsp
热门标签
RT-Thread Studio
串口
Env
LWIP
SPI
AT
Bootloader
Hardfault
CAN总线
FinSH
ART-Pi
USB
DMA
文件系统
RT-Thread
SCons
RT-Thread Nano
线程
MQTT
STM32
RTC
FAL
rt-smart
ESP8266
I2C_IIC
WIZnet_W5500
UART
ota在线升级
PWM
cubemx
freemodbus
flash
packages_软件包
BSP
潘多拉开发板_Pandora
定时器
ADC
GD32
flashDB
socket
中断
Debug
编译报错
msh
SFUD
rt_mq_消息队列_msg_queue
keil_MDK
ulog
MicroPython
C++_cpp
本月问答贡献
a1012112796
20
个答案
3
次被采纳
张世争
12
个答案
3
次被采纳
踩姑娘的小蘑菇
7
个答案
3
次被采纳
用户名由3_15位
14
个答案
2
次被采纳
rv666
9
个答案
2
次被采纳
本月文章贡献
程序员阿伟
9
篇文章
2
次点赞
hhart
3
篇文章
4
次点赞
RTT_逍遥
1
篇文章
8
次点赞
大龄码农
1
篇文章
5
次点赞
ThinkCode
1
篇文章
1
次点赞
回到
顶部
发布
问题
分享
好友
手机
浏览
扫码手机浏览
投诉
建议
回到
底部