DSPRelated.com
Forums

help needed on CAN

Started by saur...@yahoo.com December 3, 2007
I am trying to establish connection between two can board (ie. two cpu).I have written a CAn transmit code. According to me i feel all initializations are done properly. All register too reflect as i can see them using jtag emulator.My program wait for ACK so it get struck there. But i want to monitor waveform on my tranmit pin of CAN AS per my code i have written msg box data as high low so i can view then on DO.But i m unable to get any waveform.please guide how to go further and also if i m wrong in my code correct me.
MY CPU is 2808

saurabh parmar
s...@yahoo.co.in

below mentiond is my code.
#include "Edutech_280X.h"
#include "lcd.h"
#include "Gpio.h"
// Prototype statements for functions found within this file.
void mailbox_check(int32 T1, int32 T2, int32 T3);
void mailbox_read(int16 i);
extern void Edutech_Delay(long);

// Global variable for this example
struct ECAN_REGS ECanaShadow;

Uint32 ErrorCount;
Uint32 PassCount;
Uint32 MessageReceivedCount;
Uint32 TestMbox1 = 0;
Uint32 TestMbox2 = 0;
Uint32 TestMbox3 = 0;
Uint32 i;
void Init_CAN()
{
// step 2 Set the CANTX and the CANRX pins to CAN functions:
EALLOW;
ECanaShadow.CANMC.all = ECanaRegs.CANMC.all;
ECanaShadow.CANMC.bit.SRES = 1;
ECanaRegs.CANMC.all = ECanaShadow.CANMC.all;
EDIS;

EALLOW;
ECanaShadow.CANTIOC.all = ECanaRegs.CANTIOC.all;
ECanaShadow.CANTIOC.bit.TXFUNC = 1;
ECanaRegs.CANTIOC.all = ECanaShadow.CANTIOC.all;

ECanaShadow.CANRIOC.all = ECanaRegs.CANRIOC.all;
ECanaShadow.CANRIOC.bit.RXFUNC = 1;
ECanaRegs.CANRIOC.all = ECanaShadow.CANRIOC.all;
EDIS;

// extra

// TAn, RMPn, GIFn bits are all zero upon reset and are cleared again
// as a matter of precaution.

ECanaRegs.CANTA.all = 0xFFFFFFFF; /* Clear all TAn bits */

ECanaRegs.CANRMP.all = 0xFFFFFFFF; /* Clear all RMPn bits */

EALLOW;
ECanaRegs.CANGIM.all=0x00000000;

ECanaRegs.CANGIF0.all = 0xFFFFFFFF; /* Clear all interrupt flag bits */
ECanaRegs.CANGIF1.all = 0xFFFFFFFF;

//ECanaRegs.CANTA.all = 0xFFFFFFFF; // Clear all TAn

// step 3 After a reset, bit CCR (CANMC.12) and bit CCE (CANES.4) are set to 1.
//This allows the user to configure the bit-timing configuration register
//(CANBTC).If the CCE bit is set (CANES.4 = 1), proceed to next step; otherwise, set
//the CCR bit (CANMC.12 = 1) and wait until CCE bit is set (CANES.4 = 1).

EALLOW;
ECanaShadow.CANMC.all = ECanaRegs.CANMC.all;
ECanaShadow.CANMC.bit.CCR = 1;
ECanaRegs.CANMC.all = ECanaShadow.CANMC.all;
EDIS;

// Wait until the CPU has been granted permission to change the
// configuration registers
// Wait for CCE bit to be set..

// ECanaShadow.CANES.all = ECanaRegs.CANES.all;
LcdDisplayStringCenter("1 loop starts",ROW1);

while(ECanaRegs.CANES.bit.CCE != 1 ){

// ECanaShadow.CANES.all = ECanaRegs.CANES.all;
}
LcdDisplayStringCenter("CCE is 1 now",ROW1);

// Configure the eCAN timingProgram the CANBTC register with the appropriate timing values. Make
//sure that the values TSEG1 and TSEG2 are not 0. If they are 0, the module
//does not leave the initialization mode.

EALLOW;
ECanaShadow.CANBTC.all = ECanaRegs.CANBTC.all;
ECanaShadow.CANBTC.all = 0;
ECanaShadow.CANBTC.bit.BRPREG = 39; // (BRPREG + 1) = 10 feeds a 15 MHz CAN clock
ECanaShadow.CANBTC.bit.TSEG2REG = 1;
ECanaShadow.CANBTC.bit.TSEG1REG =6;
ECanaShadow.CANBTC.bit.SAM = 1;
ECanaRegs.CANBTC.all = ECanaShadow.CANBTC.all;
EDIS;
while(ECanaShadow.CANBTC.bit.TSEG2REG == 0)
{
EALLOW;
ECanaShadow.CANBTC.all = ECanaRegs.CANBTC.all;
ECanaShadow.CANBTC.bit.TSEG2REG = 1;
ECanaRegs.CANBTC.all = ECanaShadow.CANBTC.all;
EDIS;

}

while(ECanaShadow.CANBTC.bit.TSEG1REG == 0)
{
EALLOW;
ECanaShadow.CANBTC.all = ECanaRegs.CANBTC.all;
ECanaShadow.CANBTC.bit.TSEG1REG = 6;
ECanaRegs.CANBTC.all = ECanaShadow.CANBTC.all;
EDIS;

}

EALLOW;
ECanaShadow.CANMC.all = ECanaRegs.CANMC.all;
ECanaShadow.CANMC.bit.CCR = 0;
ECanaShadow.CANMC.bit.PDR = 0;
ECanaShadow.CANMC.bit.WUBA = 0;
ECanaShadow.CANMC.bit.CDR = 0;
ECanaShadow.CANMC.bit.ABO = 0;
ECanaShadow.CANMC.bit.STM = 0;
ECanaShadow.CANMC.bit.SRES = 0;
ECanaShadow.CANMC.bit.MBNR = 0;
ECanaRegs.CANMC.all = ECanaShadow.CANMC.all;
EDIS;

//Initialize all bits of MSGCTRLn registers to zero.
EALLOW;
ECanaMboxes.MBOX0.MSGCTRL.all = 0x00000000;
EDIS;

ECanaShadow.CANES.all = ECanaRegs.CANES.all;

while(ECanaShadow.CANES.bit.CCE != 0 )
{
ECanaShadow.CANES.all = ECanaRegs.CANES.all;
}
LcdDisplayStringCenter("init done..***",ROW1);

}

void main()
{

Uint16 j;
struct ECAN_REGS ECanaShadow;

MessageReceivedCount = 0;
ErrorCount = 0;
PassCount = 0;

Edutech_Intdsp();
DisableDog();
GpioSelection(0x007E0000,PORTA);
GpioPeripheral(0x50000000,PORTA,MUX2);
Edutech_Delay(25);
LcdInit();

LcdDisplayStringCenter("1 check",ROW1);

// step 1 Enable clock to the CAN module.

EALLOW;
SysCtrlRegs.PCLKCR0.bit.ECANAENCLK = 1;
EDIS;
Init_CAN();

//1) Clear the appropriate bit in the CANTRS register to 0:

ECanaRegs.CANTRR.bit.TRR0=1;
while(ECanaRegs.CANTRS.bit.TRS0 !=0){};
LcdDisplayStringCenter("3 loop",ROW1);
//2) Disable the mailbox by clearing the corresponding bit in the mailbox
//enable (CANME) register.

ECanaRegs.CANME.bit.ME0 = 0;

// Load the message identifier (MSGID) register of the mailbox.

ECanaMboxes.MBOX0.MSGID.all = 0x05555555;
// ECanaMboxes.MBOX0.MSGID.all = 0x15AC0000;

EALLOW;
ECanaMboxes.MBOX0.MSGCTRL.all = 0;
ECanaMboxes.MBOX0.MSGCTRL.bit.DLC = 8;
ECanaMboxes.MBOX0.MSGCTRL.bit.RTR = 0;
EDIS;

//Set the mailbox direction by clearing the corresponding bit in the CANMD
//register.

ECanaRegs.CANMD.bit.MD0 = 0;

//Set the mailbox enable by setting the corresponding bit in the CANME
//register

ECanaRegs.CANME.bit.ME0 = 1;

// 1) Write the message data into the mailbox data field.

ECanaMboxes.MBOX0.MDL.all = 0x05555555;
ECanaMboxes.MBOX0.MDH.all = 0x05555555;

//Set the corresponding flag in the transmit request register
//(CANTRS.1 = 1) to start the transmission of the message. The CAN
//module now handles the complete transmission of the CAN message.
//ECanaRegs.CANTRR.bit.TRR0=1;
i=0;
for(;;)
{

i++;
LcdDisplayStringCenter("tx srt",ROW1);
ECanaRegs.CANTRS.bit.TRS0 = 1;

// Set TRS for all transmit mailboxes
//Wait until the transmit-acknowledge flag of the corresponding mailbox is
//set (TA.1 = 1). After a successful transmission, this flag is set by the CAN
//module.
while(ECanaRegs.CANTA.bit.TA0 != 1 ){} // Wait for all TAn bits to be set..
LcdDisplayStringCenter("tx compl",ROW1);

ECanaRegs.CANTA.bit.TA0= 1;

}

}

//==========================================================================// No more.
//==========================================================================