当我尝试在STM32F405 MCU上初始化bxCan时,它在离开请求后没有设置CAN_MSR_INAK
。
这是我的代码:
rcc_peripheral_enable_clock(&RCC_APB1ENR, RCC_APB1ENR_CAN2EN);
nvic_enable_irq(NVIC_CAN2_RX0_IRQ);
can_reset(CAN2);
if (can_init(CAN2, // Can
false, // Time triggered communication mode
true, // Automatic bus-off management
true, // Automatic wakeup mode
false, // No automatic retransmission
false, // Receive FIFO locked mode
true, // Transmit FIFO priority
CAN_BTR_SJW_1TQ, // Resynchronization time quanta jump width
CAN_BTR_TS1_11TQ, // Time segment 1 time quanta width
CAN_BTR_TS2_2TQ, // Time segment 2 time quanta width
3, // Baud rate prescaler (1 mbps)
false, // Loopback
false) != 0) // Silent mode
_exit(0);
这是can_init
函数:
int can_init(uint32_t canport, bool ttcm, bool abom, bool awum, bool nart,
bool rflm, bool txfp, uint32_t sjw, uint32_t ts1, uint32_t ts2,
uint32_t brp, bool loopback, bool silent)
{
volatile uint32_t wait_ack;
int ret = 0;
/* Exit from sleep mode. */
CAN_MCR(canport) &= ~CAN_MCR_SLEEP;
/* Request initialization "enter". */
CAN_MCR(canport) |= CAN_MCR_INRQ;
/* Wait for acknowledge. */
wait_ack = CAN_MSR_INAK_TIMEOUT;
while ((--wait_ack) &&
((CAN_MSR(canport) & CAN_MSR_INAK) != CAN_MSR_INAK));
/* Check the acknowledge. */
if ((CAN_MSR(canport) & CAN_MSR_INAK) != CAN_MSR_INAK) {
return 1;
}
/* clear can timing bits */
CAN_BTR(canport) = 0;
/* Set the automatic bus-off management. */
if (ttcm) {
CAN_MCR(canport) |= CAN_MCR_TTCM;
} else {
CAN_MCR(canport) &= ~CAN_MCR_TTCM;
}
if (abom) {
CAN_MCR(canport) |= CAN_MCR_ABOM;
} else {
CAN_MCR(canport) &= ~CAN_MCR_ABOM;
}
if (awum) {
CAN_MCR(canport) |= CAN_MCR_AWUM;
} else {
CAN_MCR(canport) &= ~CAN_MCR_AWUM;
}
if (nart) {
CAN_MCR(canport) |= CAN_MCR_NART;
} else {
CAN_MCR(canport) &= ~CAN_MCR_NART;
}
if (rflm) {
CAN_MCR(canport) |= CAN_MCR_RFLM;
} else {
CAN_MCR(canport) &= ~CAN_MCR_RFLM;
}
if (txfp) {
CAN_MCR(canport) |= CAN_MCR_TXFP;
} else {
CAN_MCR(canport) &= ~CAN_MCR_TXFP;
}
if (silent) {
CAN_BTR(canport) |= CAN_BTR_SILM;
} else {
CAN_BTR(canport) &= ~CAN_BTR_SILM;
}
if (loopback) {
CAN_BTR(canport) |= CAN_BTR_LBKM;
} else {
CAN_BTR(canport) &= ~CAN_BTR_LBKM;
}
/* Set bit timings. */
CAN_BTR(canport) |= sjw | ts2 | ts1 |
((brp - 1ul) & CAN_BTR_BRP_MASK);
/* Request initialization "leave". */
CAN_MCR(canport) &= ~CAN_MCR_INRQ;
/* Wait for acknowledge. */
wait_ack = CAN_MSR_INAK_TIMEOUT;
while ((--wait_ack) &&
((CAN_MSR(canport) & CAN_MSR_INAK) == CAN_MSR_INAK));
if ((CAN_MSR(canport) & CAN_MSR_INAK) == CAN_MSR_INAK) {
ret = 1;
}
return ret;
}
因此,此功能成功进入初始化模式,但不能保留。
我尝试做以下事情:
没有任何影响。它总是在同一点失败。
另外(只是为了确定),我试图禁用bxCan时钟 - 然后甚至无法进入初始化模式(如预期的那样)。
相同的代码适用于STM32F103,所以我不知道,这可能是错的。
答案 0 :(得分:2)
检查这些参数: (i)检查CAN时钟是否已启用(因为您正在进入初始化模式,这看起来很好。) (ii)检查是否启用了CAN GPIO端口时钟(在进入初始化模式之前,您应启用此功能) (iii)检查是否可以配置GPIO引脚(在进入初始化模式之前,您应该配置它)
答案 1 :(得分:0)
使用静默环回模式时,我遇到了同样的问题。解决方案是拉高RX引脚,以便在将模式从“初始化模式”更改为“普通模式”时可以接收11个隐性位。
答案 2 :(得分:0)
您是否正确配置了引脚?我认为F405需要在GPIO配置中进行替代映射