Toggle navigation
首页
问答
文章
积分商城
专家
专区
更多专区...
文档中心
返回主站
搜索
提问
会员
中心
登录
注册
CAN总线
RT-Thread CAN驱动无法通讯的问题与解决
发布于 2022-08-15 10:41:33 浏览:1236
订阅该版
[tocm] 近几天在调试RT的CAN,发现了一些问题,会导致CAN通讯异常,整理了一下分享给大家 ## 1、针对于APB1总线为42M的时钟配置,会导致波特率异常 会有这样一种情况,比如使用了F429的芯片,但是外部配了个8M XTAL,所以过了锁相环之后一般是168M的sys_clock,这样会导致APB1总线为42M,而RT在做CAN的波特率的时候针对于APB1时钟42M和45M的配置是不一样的。 如下图 ```c #if defined(STM32F405xx) || defined(STM32F415xx) || defined(STM32F407xx)|| defined(STM32F417xx) ||\ defined(STM32F401xC) || defined(STM32F401xE) || 1 /* 42MHz(max) */ static const struct stm32_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_5TQ | 4)}, {CAN500kBaud, (CAN_SJW_2TQ | CAN_BS1_9TQ | CAN_BS2_4TQ | 6)}, {CAN250kBaud, (CAN_SJW_2TQ | CAN_BS1_9TQ | CAN_BS2_5TQ | 12)}, {CAN125kBaud, (CAN_SJW_2TQ | CAN_BS1_9TQ | CAN_BS2_5TQ | 24)}, {CAN100kBaud, (CAN_SJW_2TQ | CAN_BS1_9TQ | CAN_BS2_5TQ | 30)}, {CAN50kBaud, (CAN_SJW_2TQ | CAN_BS1_9TQ | CAN_BS2_5TQ | 60)}, {CAN20kBaud, (CAN_SJW_2TQ | CAN_BS1_9TQ | CAN_BS2_5TQ | 150)}, {CAN10kBaud, (CAN_SJW_2TQ | CAN_BS1_9TQ | CAN_BS2_5TQ | 300)} }; #else /* APB1 45MHz(max) */ static const struct stm32_baud_rate_tab can_baud_rate_tab[] = { {CAN1MBaud, (CAN_SJW_2TQ | CAN_BS1_9TQ | CAN_BS2_5TQ | 3)}, {CAN800kBaud, (CAN_SJW_2TQ | CAN_BS1_8TQ | CAN_BS2_5TQ | 4)}, {CAN500kBaud, (CAN_SJW_2TQ | CAN_BS1_9TQ | CAN_BS2_5TQ | 6)}, {CAN250kBaud, (CAN_SJW_2TQ | CAN_BS1_9TQ | CAN_BS2_5TQ | 12)}, {CAN125kBaud, (CAN_SJW_2TQ | CAN_BS1_9TQ | CAN_BS2_5TQ | 24)}, {CAN100kBaud, (CAN_SJW_2TQ | CAN_BS1_9TQ | CAN_BS2_5TQ | 30)}, {CAN50kBaud, (CAN_SJW_2TQ | CAN_BS1_9TQ | CAN_BS2_5TQ | 60)}, {CAN20kBaud, (CAN_SJW_2TQ | CAN_BS1_9TQ | CAN_BS2_5TQ | 150)}, {CAN10kBaud, (CAN_SJW_2TQ | CAN_BS1_9TQ | CAN_BS2_5TQ | 300)} }; #endif ``` 当然原来RT的42M的时钟配置不是这样的,(官方给的42M的时钟配置和45M的是完全一样的,我的RT是4.0.3)以上代码是改过后的42M的时钟配置 ## 2、RT的CAN设备的使用会导致当前线程会被阻塞,CAN设备有时也会被阻塞 该问题主要是由于drv_can.c里的底层的错误状态导致进入无效循坏。当然这个问题在论坛里好多大神进行过讨论,亲测注释掉错误标志是最为有限的方法,其余的包括开启自动重发等等,皆试过,没有任何用,改后的代码在下方,仅供参考 ```c static int _can_sendmsg(struct rt_can_device *can, const void *buf, rt_uint32_t box_num) { CAN_HandleTypeDef *hcan; hcan = &((struct stm32_can *) can->parent.user_data)->CanHandle; struct rt_can_msg *pmsg = (struct rt_can_msg *) buf; CAN_TxHeaderTypeDef txheader = {0}; HAL_CAN_StateTypeDef state = hcan->State; /* Check the parameters */ RT_ASSERT(IS_CAN_DLC(pmsg->len)); if ((state == HAL_CAN_STATE_READY) || (state == HAL_CAN_STATE_LISTENING)) { /*check select mailbox is empty */ switch (1 << box_num) { case CAN_TX_MAILBOX0: if (HAL_IS_BIT_SET(hcan->Instance->TSR, CAN_TSR_TME0) != SET) { /* Change CAN state */ // hcan->State = HAL_CAN_STATE_ERROR; /* Return function status */ return -RT_ERROR; } break; case CAN_TX_MAILBOX1: if (HAL_IS_BIT_SET(hcan->Instance->TSR, CAN_TSR_TME1) != SET) { /* Change CAN state */ // hcan->State = HAL_CAN_STATE_ERROR; /* Return function status */ return -RT_ERROR; } break; case CAN_TX_MAILBOX2: if (HAL_IS_BIT_SET(hcan->Instance->TSR, CAN_TSR_TME2) != SET) { /* Change CAN state */ // hcan->State = HAL_CAN_STATE_ERROR; /* Return function status */ return -RT_ERROR; } break; default: RT_ASSERT(0); break; } if (RT_CAN_STDID == pmsg->ide) { txheader.IDE = CAN_ID_STD; RT_ASSERT(IS_CAN_STDID(pmsg->id)); txheader.StdId = pmsg->id; } else { txheader.IDE = CAN_ID_EXT; RT_ASSERT(IS_CAN_EXTID(pmsg->id)); txheader.ExtId = pmsg->id; } if (RT_CAN_DTR == pmsg->rtr) { txheader.RTR = CAN_RTR_DATA; } else { txheader.RTR = CAN_RTR_REMOTE; } /* clear TIR */ hcan->Instance->sTxMailBox[box_num].TIR &= CAN_TI0R_TXRQ; /* Set up the Id */ if (RT_CAN_STDID == pmsg->ide) { hcan->Instance->sTxMailBox[box_num].TIR |= (txheader.StdId << CAN_TI0R_STID_Pos) | txheader.RTR; } else { hcan->Instance->sTxMailBox[box_num].TIR |= (txheader.ExtId << CAN_TI0R_EXID_Pos) | txheader.IDE | txheader.RTR; } /* Set up the DLC */ hcan->Instance->sTxMailBox[box_num].TDTR = pmsg->len & 0x0FU; /* Set up the data field */ WRITE_REG(hcan->Instance->sTxMailBox[box_num].TDHR, ((uint32_t)pmsg->data[7] << CAN_TDH0R_DATA7_Pos) | ((uint32_t)pmsg->data[6] << CAN_TDH0R_DATA6_Pos) | ((uint32_t)pmsg->data[5] << CAN_TDH0R_DATA5_Pos) | ((uint32_t)pmsg->data[4] << CAN_TDH0R_DATA4_Pos)); WRITE_REG(hcan->Instance->sTxMailBox[box_num].TDLR, ((uint32_t)pmsg->data[3] << CAN_TDL0R_DATA3_Pos) | ((uint32_t)pmsg->data[2] << CAN_TDL0R_DATA2_Pos) | ((uint32_t)pmsg->data[1] << CAN_TDL0R_DATA1_Pos) | ((uint32_t)pmsg->data[0] << CAN_TDL0R_DATA0_Pos)); /* Request transmission */ SET_BIT(hcan->Instance->sTxMailBox[box_num].TIR, CAN_TI0R_TXRQ); return RT_EOK; } else { /* Update error code */ hcan->ErrorCode |= HAL_CAN_ERROR_NOT_INITIALIZED; return -RT_ERROR; } } ```
2
条评论
默认排序
按发布时间排序
登录
注册新账号
关于作者
jia868
这家伙很懒,什么也没写!
文章
2
回答
2
被采纳
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组件
热门标签
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
UART
WIZnet_W5500
ota在线升级
PWM
cubemx
freemodbus
flash
packages_软件包
BSP
潘多拉开发板_Pandora
定时器
ADC
GD32
flashDB
socket
中断
编译报错
Debug
SFUD
msh
rt_mq_消息队列_msg_queue
keil_MDK
ulog
MicroPython
C++_cpp
本月问答贡献
出出啊
1517
个答案
342
次被采纳
小小李sunny
1443
个答案
289
次被采纳
张世争
807
个答案
174
次被采纳
crystal266
547
个答案
161
次被采纳
whj467467222
1222
个答案
148
次被采纳
本月文章贡献
出出啊
1
篇文章
4
次点赞
小小李sunny
1
篇文章
1
次点赞
张世争
1
篇文章
1
次点赞
crystal266
2
篇文章
2
次点赞
whj467467222
2
篇文章
1
次点赞
回到
顶部
发布
问题
投诉
建议
回到
底部