TI中文支持网
TI专业的中文技术问题搜集分享网站

TMS320F2837xD ti官方例程can_external_transmit CANA和CANB之间无法正常通信的情况?

Other Parts Discussed in Thread:C2000WARE

在使用2837xD测试can通信时,发现官方例程can_external_transmit(cana_to_canb)无法正常通信,将官方例程导入ccs之后,只更改了相应的can引脚(开发板为创龙28379D)。相应引脚选择为:CANA(GPIO4,GPIO5),CANB (GPIO7,GPIO8),两个can口外部均接了收发器,CANAL和CANBL连接,CANAH和CANBH连接。在使用逻辑分析仪抓取cana口发出的数据波形没问题,但是一直不进canb中断,希望各位老师能给看看是什么原因?确定下是我程序的问题还是官方例程存在问题。

这个问题纠结我很多天了,一直没有进展,因为我看到网上其他人在28335上也有遇到类似问题的,希望能把这个问题彻底解决。

具体的程序如下:

//
// Included Files
//
#include "F28x_Project.h" // Device Headerfile and Examples Include File
#include <stdint.h>
#include <stdbool.h>
#include "inc/hw_types.h"
#include "inc/hw_memmap.h"
#include "inc/hw_can.h"
#include "driverlib/can.h"

//
// Defines
//
#define TXCOUNT 100
#define MSG_DATA_LENGTH 4
#define TX_MSG_OBJ_ID 1
#define RX_MSG_OBJ_ID 1

//
// Globals
//
volatile unsigned long i;
volatile uint32_t txMsgCount = 0;
volatile uint32_t rxMsgCount = 0;
volatile uint32_t errorFlag = 0;
unsigned char txMsgData[4];
unsigned char rxMsgData[4];
tCANMsgObject sTXCANMessage;
tCANMsgObject sRXCANMessage;

//
// Function Prototypes
//
__interrupt void canbISR(void);

//
// Main
//
void main(void)
{

InitSysCtrl();

InitGpio();

//
// Setup GPIO pin mux for CAN-A TX/RX and CAN-B TX/RX
GPIO_SetupPinMux(5, GPIO_MUX_CPU1, 6); //GPIO5 – CANRXA
GPIO_SetupPinOptions(5, GPIO_INPUT, GPIO_ASYNC);
GPIO_SetupPinMux(4, GPIO_MUX_CPU1, 6); //GPIO4 – CANTXA
GPIO_SetupPinOptions(4, GPIO_OUTPUT, GPIO_PUSHPULL);
GPIO_SetupPinMux(4, GPIO_MUX_CPU1, 6); //GPIO7 – CANRXB
GPIO_SetupPinOptions(4, GPIO_INPUT, GPIO_ASYNC);
GPIO_SetupPinMux(8, GPIO_MUX_CPU1, 2); //GPIO8 – CANTXB
GPIO_SetupPinOptions(8, GPIO_OUTPUT, GPIO_PUSHPULL);

//
// Initialize the CAN controllers
CANInit(CANA_BASE);
CANInit(CANB_BASE);

//
// Setup CAN to be clocked off the PLL output clock
CANClkSourceSelect(CANA_BASE, 0); 
CANClkSourceSelect(CANB_BASE, 0);

//
// Set up the CAN bus bit rate to 500kHz for each module
// This function sets up the CAN bus timing for a nominal configuration.
// You can achieve more control over the CAN bus timing by using the
// function CANBitTimingSet() instead of this one, if needed.
// Additionally, consult the device data sheet for more information about
// the CAN module clocking.
//
CANBitRateSet(CANA_BASE, 200000000, 500000);
CANBitRateSet(CANB_BASE, 200000000, 500000);

//
// Enable interrupts on the CAN B peripheral.
//
CANIntEnable(CANB_BASE, CAN_INT_MASTER | CAN_INT_ERROR | CAN_INT_STATUS);

DINT;

InitPieCtrl();

IER = 0x0000;
IFR = 0x0000;

InitPieVectTable();

EALLOW;
PieVectTable.CANB0_INT = canbISR;
EDIS;

PieCtrlRegs.PIEIER9.bit.INTx7 = 1;
IER |= M_INT9;
EINT;

//
// Enable the CAN-B interrupt signal
CANGlobalIntEnable(CANB_BASE, CAN_GLB_INT_CANINT0);

//
// Initialize the transmit message object used for sending CAN messages.
// Message Object Parameters:
// Message Identifier: 0x5555
// Message ID Mask: 0x0
// Message Object Flags: None
// Message Data Length: 4 Bytes
// Message Transmit data: txMsgData
//
sTXCANMessage.ui32MsgID = 0x5555;
sTXCANMessage.ui32MsgIDMask = 0;
sTXCANMessage.ui32Flags = 0;
sTXCANMessage.ui32MsgLen = MSG_DATA_LENGTH;
sTXCANMessage.pucMsgData = txMsgData;

//
// Initialize the receive message object used for receiving CAN messages.
// Message Object Parameters:
// Message Identifier: 0x5555
// Message ID Mask: 0x0
// Message Object Flags: Receive Interrupt
// Message Data Length: 4 Bytes
// Message Receive data: rxMsgData
//
sRXCANMessage.ui32MsgID = 0x5555;
sRXCANMessage.ui32MsgIDMask = 0;
sRXCANMessage.ui32Flags = MSG_OBJ_RX_INT_ENABLE;
sRXCANMessage.ui32MsgLen = MSG_DATA_LENGTH;
sRXCANMessage.pucMsgData = rxMsgData;
CANMessageSet(CANB_BASE, RX_MSG_OBJ_ID, &sRXCANMessage,
MSG_OBJ_TYPE_RX);

//
// Initialize the transmit message object data buffer to be sent
//
txMsgData[0] = 0x12;
txMsgData[1] = 0x34;
txMsgData[2] = 0x56;
txMsgData[3] = 0x78;

//
// Start CAN module A and B operations
//
CANEnable(CANA_BASE);
CANEnable(CANB_BASE);

//
// Transmit messages from CAN-A to CAN-B
//
for(i = 0; i < TXCOUNT; i++)
{
//
// Check the error flag to see if errors occurred
//
if(errorFlag)
{
asm(" ESTOP0");
}

//
// Verify that the number of transmitted messages equal the number of
// messages received before sending a new message
//
if(txMsgCount == rxMsgCount)
{
//
// Transmit Message
//
CANMessageSet(CANA_BASE, TX_MSG_OBJ_ID, &sTXCANMessage,
MSG_OBJ_TYPE_TX);

txMsgCount++;
}
else
{
errorFlag = 1;
}

//
// Delay 0.25 second before continuing
//
DELAY_US(1000 * 250);

//
// Increment the value in the transmitted message data.
//
txMsgData[0] += 0x01;
txMsgData[1] += 0x01;
txMsgData[2] += 0x01;
txMsgData[3] += 0x01;
}

//
// Stop application
//
asm(" ESTOP0");
}

//
// CAN B ISR – The interrupt service routine called when a CAN interrupt is
// triggered on CAN module B.
//
__interrupt void
canbISR(void)
{
uint32_t status;

//
// Read the CAN-B interrupt status to find the cause of the interrupt
//
status = CANIntStatus(CANB_BASE, CAN_INT_STS_CAUSE);

//
// If the cause is a controller status interrupt, then get the status
//
if(status == CAN_INT_INT0ID_STATUS)
{
//
// Read the controller status. This will return a field of status
// error bits that can indicate various errors. Error processing
// is not done in this example for simplicity. Refer to the
// API documentation for details about the error status bits.
// The act of reading this status will clear the interrupt.
//
status = CANStatusGet(CANB_BASE, CAN_STS_CONTROL);

//
// Check to see if an error occurred.
//
if(((status & ~(CAN_ES_RXOK)) != 7) &&
((status & ~(CAN_ES_RXOK)) != 0))
{
//
// Set a flag to indicate some errors may have occurred.
//
errorFlag = 1;
}
}
//
// Check if the cause is the CAN-B receive message object 1
//
else if(status == RX_MSG_OBJ_ID)
{
//
// Get the received message
//
CANMessageGet(CANB_BASE, RX_MSG_OBJ_ID, &sRXCANMessage, true);

//
// Getting to this point means that the RX interrupt occurred on
// message object 1, and the message RX is complete. Clear the
// message object interrupt.
//
CANIntClear(CANB_BASE, RX_MSG_OBJ_ID);

//
// Increment a counter to keep track of how many messages have been
// received. In a real application this could be used to set flags to
// indicate when a message is received.
//
rxMsgCount++;

//
// Since the message was received, clear any error flags.
//
errorFlag = 0;
}
//
// If something unexpected caused the interrupt, this would handle it.
//
else
{
//
// Spurious interrupt handling can go here.
//
}

//
// Clear the global interrupt flag for the CAN interrupt line
//
CANGlobalIntClear(CANB_BASE, CAN_GLB_INT_CANINT0);

//
// Acknowledge this interrupt located in group 9
//
PieCtrlRegs.PIEACK.all = PIEACK_GROUP9;
}

Susan Yang:

您直接使用TI例程can_ex3_external_transmit,只修改印引脚,可以成功通信吗?

C2000Ware_3_04_00_00\driverlib\f2837xd\examples\cpu1\can
GPIO_setPinConfig(DEVICE_GPIO_CFG_CANRXA);GPIO_setPinConfig(DEVICE_GPIO_CFG_CANTXA);GPIO_setPinConfig(DEVICE_GPIO_CFG_CANRXB);GPIO_setPinConfig(DEVICE_GPIO_CFG_CANTXB);

若是不能成功的话,使用loopback(can_ex1_loopback)时可以成功吗?

关于物理层: 1 CAN总线两端是否正确连接120Ω电阻?2 较低的比特率的话仍然有问题吗?3 如果总线上只有1个节点并让其传输,它应该会收到ACK错误并永远传输,您是否在示波器上看到此总线活动?

建议您也看一下下面的文档:www.ti.com/…/slla270.pdf

,

user4921973:

您好,非常高兴收到您的回答,按照您的解答,我重新测试了一遍,直接使用TI例程can_ex3_external_transmit,只修改印引脚,具体修改如下:
#defineDEVICE_GPIO_CFG_CANRXAGPIO_5_CANRXA
#defineDEVICE_GPIO_CFG_CANTXAGPIO_4_CANTXA
#defineDEVICE_GPIO_CFG_CANRXBGPIO_7_CANRXA
#defineDEVICE_GPIO_CFG_CANTXBGPIO_8_CANTXA
测试发现仍然通信不成功,用逻辑分析仪抓取CANA口波形发现,一直在重复的发送第一组数据。降低比特率为100k,现象是一样的。

为确定是不是我设置的CANA(GPIO4-GPIO5)和CANB(GPIO7-GPIO8)引脚存在问题(设置问题或开发板焊接问题),我使用(can_ex1_loopback)例程分别测试了CANA和CANB自回环,用逻辑分析仪抓取波形,数据传输正常,没有问题。证明CANA和CANB引脚没问题。

物理层:
1.CANA和CANB口均外接了收发器,此收发器是购买的现成的模块,模块输出端口自带120欧姆电阻,使用万用表测量,阻值吻合。
2.较低的比特率can_ex3_external_transmit仍然存在问题,其他几个例程:can_ex1_loopback,can_ex2_loopback_interrupts在比特率为20k/50k/100k/125k/250k/500k/1000k均可正常工作。
3.can_ex3_external_transmit例程为CANA_TO_CANB,和上述一样,CANA和CANB之间程序运行时,我用逻辑分析仪抓取CANA口输出数据,通过观察逻辑分析仪上的解析,每段数据之后都有一个NAK信号,不知道此信号是否是NOT-ACK的简称.

麻烦您这边再次帮我分析下是什么问题,谢谢啦,我这边也是刚开始使用28377的can模块,所以有很多知识不是很理解,再次感谢您。

,

user4921973:

不好意思,上面引脚我编写错误啦,应该是
#define DEVICE_GPIO_CFG_CANRXA GPIO_5_CANRXA
#define DEVICE_GPIO_CFG_CANTXA GPIO_4_CANTXA
#define DEVICE_GPIO_CFG_CANRXB GPIO_7_CANRXB
#define DEVICE_GPIO_CFG_CANTXB GPIO_8_CANTXB

,

user4921973:

问题已解决,接线问题,换根杜邦线就好了

,

Susan Yang:

谢谢您的反馈

赞(0)
未经允许不得转载:TI中文支持网 » TMS320F2837xD ti官方例程can_external_transmit CANA和CANB之间无法正常通信的情况?
分享到: 更多 (0)