导入例程can_loopback,这是一个外回环模式的程序,运行正常,示波器上可以观测到Tx引脚上的信号。
然后改成 SILENT模式,可以接收到Can传感器发出的信号,正常接收,Can传感器是隔一段时间自动发数据的。
但是改成Normal Mode ,我的做法是删掉这两句话
// HWREG(CAN0_BASE + CAN_O_CTL) |= CAN_CTL_TEST;
// HWREG(CAN0_BASE + CAN_O_TEST) = CAN_TEST_EXL;
然后程序就不能运行,ES寄存器的值是0x65,芯片技术手册上说是Bit0 ERROR,如下
Bit0 Error: During the transmission of a message (or acknowledge bit, or active error flag, or
overload flag), the device wanted to send a dominant level (logical value '0'), but the monitored bus
level was recessive. During Bus-Off recovery, this status is set each time a sequence of 11
recessive bits has been monitored. This enables the CPU to monitor the proceeding of the Bus-Off
recovery sequence (indicating the bus is not stuck at dominant or continuously disturbed).
求路过的大神看看,是不是初始化哪里还有问题,个人感觉和邮箱设置无关。技术手册也看了好久,感觉初始化没问题,但是运行又不成功,对这个Bit0 error没什么办法,困扰了好多天,求大神解答,谢谢。
下面是主函数
int main(void)
{
HWREG(SYSCTL_MWRALLOW) = 0xA5A5A5A5;
// Setup main clock tree for 75MHz – M3 and 150MHz – C28x
SysCtlClockConfigSet(SYSCTL_SYSDIV_1 | SYSCTL_M3SSDIV_2 | SYSCTL_USE_PLL |
(SYSCTL_SPLLIMULT_M & 0x0F));
#ifdef _FLASH
//memcpy(&RamfuncsRunStart, &RamfuncsLoadStart, (size_t)&RamfuncsLoadSize);
FlashInit();
#endif
InitConsole(); //初始化串口
//接下来初始化 Can
//时钟 引脚配置
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD);
GPIOPinConfigure(GPIO_PD0_CAN0RX);
GPIOPinConfigure(GPIO_PD1_CAN0TX);
GPIOPinTypeCAN(GPIO_PORTD_BASE, GPIO_PIN_0 | GPIO_PIN_1);
SysCtlPeripheralEnable(SYSCTL_PERIPH_CAN0);
// Initialize the CAN controller
CANInit(CAN0_BASE);
// Setup CAN to be clocked off the M3/Master subsystem clock
CANClkSourceSelect(CAN0_BASE, CAN_CLK_M3);
// Set up the bit rate for the CAN bus.
CANBitRateSet(CAN0_BASE, SysCtlClockGet(SYSTEM_CLOCK_SPEED), 500000);
// Enable interrupts on the CAN peripheral.
CANIntEnable(CAN0_BASE, CAN_INT_MASTER | CAN_INT_ERROR | CAN_INT_STATUS);
// Register interrupt handler in RAM vector table
IntRegister(INT_CAN0INT0, CANIntHandler);
// Enable the CAN interrupt on the processor (NVIC).
IntEnable(INT_CAN0INT0);
IntMasterEnable();
// Enable test mode and select external loopback
// HWREG(CAN0_BASE + CAN_O_CTL) |= CAN_CTL_TEST;
// HWREG(CAN0_BASE + CAN_O_TEST) = CAN_TEST_EXL; //CAN_TEST_SILENT;
// Initialize the message object that will be used for sending CAN
// messages. The message will be 8 bytes that will contain an incrementing
// value.
ucTXMsgData[0] = 0x8B;
ucTXMsgData[1] = 0xFB;
ucTXMsgData[2] = 0x00;
ucTXMsgData[3] = 0xEC;
ucTXMsgData[4] = 0x00;
ucTXMsgData[5] = 0x00;
ucTXMsgData[6] = 0x00;
ucTXMsgData[7] = 0x1D;
sTXCANMessage.ulMsgID = 0; // CAN message ID – use 1
sTXCANMessage.ulMsgIDMask = 0; // no mask needed for TX
sTXCANMessage.ulFlags = MSG_OBJ_TX_INT_ENABLE; //MSG_OBJ_TX_INT_ENABLE;
sTXCANMessage.ulMsgLen = sizeof(ucTXMsgData); // size of message is 8
sTXCANMessage.pucMsgData = ucTXMsgData; // ptr to message content
CANMessageSet(CAN0_BASE, 1, &sTXCANMessage, MSG_OBJ_TYPE_TX);
// Initialize the message object that will be used for receiving CAN
// messages.
ucRXMsgData[0] = 0x00;
ucRXMsgData[1] = 0x00;
ucRXMsgData[2] = 0x00;
ucRXMsgData[3] = 0x00;
ucRXMsgData[4] = 0x00;
ucRXMsgData[5] = 0x00;
ucRXMsgData[6] = 0x00;
ucRXMsgData[7] = 0x00;
sRXCANMessage.ulMsgID = 0x18F01D48; // CAN message ID – use 1
sRXCANMessage.ulMsgIDMask = 0; // no mask needed for TX
sRXCANMessage.ulFlags = MSG_OBJ_RX_INT_ENABLE; //MSG_OBJ_RX_INT_ENABLE;
sRXCANMessage.ulMsgLen = sizeof(ucRXMsgData); // size of message is 8
sRXCANMessage.pucMsgData = ucRXMsgData; // ptr to message content
CANMessageSet(CAN0_BASE, 2, &sRXCANMessage, MSG_OBJ_TYPE_RX);
// Enable the CAN for operation.
CANEnable(CAN0_BASE);
// Enter loop to send messages. A new message will be sent once per
// second.
for(;;)
{
// Now wait 1 second before continuing
SimpleDelay();
// Check the error flag to see if errors occurred
if(g_bErrFlag)
{
UARTprintf(" error – cable connected?\n");
UARTprintf("ulStatus : 0x%02X %02X %02X %02X\n",
ucCanStatus[0], ucCanStatus[1], ucCanStatus[2],
ucCanStatus[3]);
}
else
{
UARTprintf("Transmit msg: 0x%02X %02X %02X %02X %02X %02X %02X %02X\n",
ucTXMsgData[0], ucTXMsgData[1], ucTXMsgData[2],ucTXMsgData[3],
ucTXMsgData[4], ucTXMsgData[5], ucTXMsgData[6],ucTXMsgData[7]);
UARTprintf("Receiving msg: 0x%02X %02X %02X %02X %02X %02X %02X %02X\n",
ucRXMsgData[0], ucRXMsgData[1], ucRXMsgData[2],ucRXMsgData[3],
ucRXMsgData[4], ucRXMsgData[5], ucRXMsgData[6],ucRXMsgData[7]);
// If no errors then print the count of message sent
UARTprintf(" total count = %u\n", g_ulMsgCount);
}
}
}