EmbeddedRelated.com
Forums

problem in CAN

Started by harisha eh November 29, 2006
hello group,

Im using lpc2129 in MCB2100 board.im facing problem in
CAN(Controller Area Network) communication in hardware.Actually i
connected two MCB2100 board,im just sending char in hypertermial
through UART1(but communication between two board is through CAN
only).i connected CANL to CANL & CANH to CANH.
Transmission is done through push message function in which
i'm just taking the datas from the structure and assigning to a
pointer. Using uart (hyperterminal) we are assigning data, to the data
fieid of the structure.
In the receiver code, we are just assigning the pointer
value from transmitter to another receiver pointer.We are not able to
check the received value in hyperterminal.
My transmitter and receiver code is given below

**************************************************************
Transmitter
***************************************************************
#include
#include

unsigned int a=0;

int CANAll_Init (unsigned int can_btr );
int CANAll_PushMessage (CANALL_MSG *pTransmitBuf);
int CANAll_PullMessage (CANALL_MSG *pReceiveBuf );

typedef struct
{
unsigned int Frame; // Bits 16..19: DLC - Data Length Counter
// Bit 30: Set if this is a RTR message
// Bit 31: Set if this is a 29-bit ID message
unsigned int MsgID; // CAN Message ID (11-bit or 29-bit)
unsigned int DatA; // CAN Message Data Bytes 0-3
unsigned int DatB; // CAN Message Data Bytes 4-7
} CANALL_MSG;
void delay(unsigned int n)
{
while(n--);
}

void serial_init(void)
{
PINSEL0 |= 0X00050000;
U1LCR = 0X83;
U1DLL = 97;
U1LCR = 0X03;

}
int CANAll_Init (unsigned int can_btr )
{
unsigned int *p;
p=&a;

AFMR = 0x00000002;
C1MOD = 0x01;
C1IER = 0x00;
C1GSR = 0x00;
C1BTR = can_btr;

// initialize destination pointer

*p = C1RFS; // Frame

p++;
*p = C1RID; // ID

p++;
*p = C1RDA; // Data A

p++;
*p = C1RDB; // Data B
C1CMR = 0x04; // release receive buffer
C1MOD = 0x00;

return 0;
}

int CANAll_PushMessage (CANALL_MSG *pTransmitBuf)
{
unsigned int *pAddr;
unsigned int *pCandata;

pAddr = (unsigned int *) &C1TFI1 ;
*pAddr = pTransmitBuf->Frame & 0xC00F0000;

// Write CAN ID
pAddr++;
*pAddr = pTransmitBuf->MsgID;

// Write first 4 data bytes
pCandata = (unsigned int *) &(pTransmitBuf->DatA);
pAddr++;
*pAddr = *pCandata;

// Write second 4 data bytes
pCandata++;
pAddr++;
*pAddr = *pCandata;
return 0;

}
void main(void)
{
CANALL_MSG MsgBuf; // Buffers one CAN message
unsigned char data;
PINSEL1 |= 0x00155000; // Set bits 14 and 16
serial_init();
CANAll_Init(CANBitrate125k_12MHz);

while(1)
{

while((U1LSR & 0x01)==0);
data=U1RBR;

MsgBuf.Frame = 0x80080000; // 29-bit, no RTR, DLC is 8 bytes
MsgBuf.MsgID = 0x00012345; // CAN ID
MsgBuf.DatA = data; // all zeros
MsgBuf.DatB = data; // all zeros

// Transmit initial message on CAN 1
CANAll_PushMessage(&MsgBuf);

U1THRa;
while((U1LSR & 0x20)==0);

delay(500);

}
}

*********************************************************
Receiver
*********************************************************
#include
#include

unsigned int a=0;
int CANAll_Init (unsigned int can_btr );
int CANAll_PushMessage (CANALL_MSG *pTransmitBuf);
int CANAll_PullMessage (CANALL_MSG *pReceiveBuf );

void delay(unsigned int n)
{
while(n--);
}

void serial_init(void)
{
PINSEL0 |= 0X00050000;
U1LCR = 0X83;
U1DLL = 97;
U1LCR = 0X03;

}

int CANAll_PullMessage (CANALL_MSG *pReceiveBuf )
{
unsigned int *pSrc; // Source pointer
unsigned int *pDst; // Destination pointer
// Initialize pointers
pSrc = &a;
pDst = (unsigned int *) &(pReceiveBuf->Frame);
*pDst = *pSrc; // Copy Frame

pSrc++;
pDst++;
*pDst = *pSrc; // Copy MsgID

pSrc++;
pDst++;
*pDst = *pSrc; // Copy DatA

pSrc++;
pDst++;
*pDst = *pSrc; // Copy DatB

C1CMR = 0x30;
return 0;
}

int CANAll_Init (unsigned int can_btr )
{
unsigned int *p;
p=&a;

AFMR = 0x00000002;
C1MOD = 0x01;
C1IER = 0x00;
C1GSR = 0x00;
C1BTR = can_btr;

// initialize destination pointer

*p = C1RFS; // Frame

p++;
*p = C1RID; // ID

p++;
*p = C1RDA; // Data A

p++;
*p = C1RDB; // Data B
C1CMR = 0x04; // release receive buffer
C1MOD = 0x00;

return 0;
}

void main(void)
{
CANALL_MSG MsgBuf; // Buffers one CAN message
unsigned char data;
PINSEL1 |= 0x00155000; // Set bits 14 and 16
serial_init();
CANAll_Init(CANBitrate125k_12MHz);

while(1)
{

// Transmit initial message on CAN 1
CANAll_PullMessage(&MsgBuf);

U1THRa;
while((U1LSR & 0x20)==0);

delay(500);

}
}

Please help me.

Thanks & Regards
Harish

An Engineer's Guide to the LPC2100 Series

At 11:05 AM 11/29/2006 +0530, harisha eh wrote:

>*********************************************************
>Receiver
>*********************************************************
>#include
>#includeunsigned int a=0;
>int CANAll_Init (unsigned int can_btr );
>int CANAll_PushMessage (CANALL_MSG *pTransmitBuf);
>int CANAll_PullMessage (CANALL_MSG *pReceiveBuf );



>int CANAll_PullMessage (CANALL_MSG *pReceiveBuf )
>{
> unsigned int *pSrc; // Source pointer
> unsigned int *pDst; // Destination pointer
> // Initialize pointers
> pSrc = &a;
> pDst = (unsigned int *) &(pReceiveBuf->Frame);
> *pDst = *pSrc; // Copy Frame
>
> pSrc++;
> pDst++;
> *pDst = *pSrc; // Copy MsgID
>
> pSrc++;
> pDst++;
> *pDst = *pSrc; // Copy DatA
I haven't used the LPC CAN implementation yet, but I cannot imagine this
ever working. All you are doing is copying 4 ints starting from wherever
'a' happens to be into your receive structure. You never actually read any
of the CAN buffers.

You do even odder things in the init, writing to areas of memory that you
have no idea what they are. The only thing you can say is they are outside
of 'a'. What you actually end up writing to will depend on where a ends up.

Robert

http://www.aeolusdevelopment.com/

From the Divided by a Common Language File (Edited to protect the guilty)
ME - "I'd like to get Price and delivery for connector Part # XXXXX"
Dist./Rep - "$X.XX Lead time 37 days"
ME - "Anything we can do about lead time? 37 days seems a bit high."
Dist./Rep - "that is the lead time given because our stock is live.... we
currently have stock."
What is the history of that code?

Did you write it from scratch
- so we cannot assume that any of it works

or is it based on (for example) BlinkyCAN in
the files section of this group
- in which case what have you changed that makes
it fit your hardware but somehow stops it from working?

What about your hardware?
Are you terminating the CAN cable at both ends?

How do you know the problem is in the CAN part of the
code and not (for example) that one of the PCs has
a dodgy COM port?

Does the transmitter think it has successfully transmitted
the packet or is it continually retrying until it fails
bus-off?

Have you looked at what the transmitter is putting out
with a commercial CAN analyser, or an oscilloscope.
(It's much harder with an oscilloscope but still
possible).

Have you read all the errata about the Philips CAN
peripheral, for example how you have to send a dummy frame
with all ID bits dominant and abort that before
sending any useful messages?

There are a lot of questions that need answering to
help us narrow down your problem.

Help us to help you.
Danish
hi Robert Adsett,
Thanks for the reply,Actually i download CAN test code Net & i modifed
that code(the code sent on previous mail).Im sending that downloaded
code.plz give me suggestions,actually im new to this field.
Thanks and Regards
Harish
Thanks for the reply Danish Ali ,
Regarding ur questions:

i did not written code from scratch

i downloaded test code from net(please see attchment).But i tested
this code also
in Hardware,im not getting output.

Actually i taken off all ISR & i just replace with content of ISR in
place of ISR .This ISR Copies the received message into the some array
.And i taken off Interrupt Service Routine for CAN Errors.

I have terminating reg at both ends in hardware

i clear checked my PCs the COM port are working fine.

I checked in both transmitter and receiver side using Multimeter in
both line i.e CANL & CANH ,it is showing some 2.3v.

Thanks and Regards
Harish
On 11/29/06, Danish Ali wrote:
> What is the history of that code?
>
> Did you write it from scratch
> - so we cannot assume that any of it works
>
> or is it based on (for example) BlinkyCAN in
> the files section of this group
> - in which case what have you changed that makes
> it fit your hardware but somehow stops it from working?
>
> What about your hardware?
> Are you terminating the CAN cable at both ends?
>
> How do you know the problem is in the CAN part of the
> code and not (for example) that one of the PCs has
> a dodgy COM port?
>
> Does the transmitter think it has successfully transmitted
> the packet or is it continually retrying until it fails
> bus-off?
>
> Have you looked at what the transmitter is putting out
> with a commercial CAN analyser, or an oscilloscope.
> (It's much harder with an oscilloscope but still
> possible).
>
> Have you read all the errata about the Philips CAN
> peripheral, for example how you have to send a dummy frame
> with all ID bits dominant and abort that before
> sending any useful messages?
>
> There are a lot of questions that need answering to
> help us narrow down your problem.
>
> Help us to help you.
> Danish
>
Hello, Harish. I haven't checked CAN-specific code, but there is a
programming mistake in your code. I think this can be the problem once your
error description seems to be data corruption. My comments follow inline:

> hello group,
>
> Im using lpc2129 in MCB2100 board.im facing problem in
> CAN(Controller Area Network) communication in hardware.Actually i
> connected two MCB2100 board,im just sending char in hypertermial
> through UART1(but communication between two board is through CAN
> only).i connected CANL to CANL & CANH to CANH.
> Transmission is done through push message function in which
> i'm just taking the datas from the structure and assigning to a
> pointer. Using uart (hyperterminal) we are assigning data, to the data
> fieid of the structure.
> In the receiver code, we are just assigning the pointer
> value from transmitter to another receiver pointer.We are not able to
> check the received value in hyperterminal.
> My transmitter and receiver code is given below
>
> **************************************************************
> Transmitter
> ***************************************************************
> #include
> #include unsigned int a=0;

You created a global int (32 Bits) 'a'. Semantics: use a more appropriated
name -- see below that you write outbounds this variable.

> int CANAll_Init (unsigned int can_btr );
> int CANAll_PushMessage (CANALL_MSG *pTransmitBuf);
> int CANAll_PullMessage (CANALL_MSG *pReceiveBuf );
>
> typedef struct
> {
> unsigned int Frame; // Bits 16..19: DLC - Data Length Counter
> // Bit 30: Set if this is a RTR message
> // Bit 31: Set if this is a 29-bit ID message
> unsigned int MsgID; // CAN Message ID (11-bit or 29-bit)
> unsigned int DatA; // CAN Message Data Bytes 0-3
> unsigned int DatB; // CAN Message Data Bytes 4-7
> } CANALL_MSG;
> void delay(unsigned int n)
> {
> while(n--);
> }
>
> void serial_init(void)
> {
> PINSEL0 |= 0X00050000;
> U1LCR = 0X83;
> U1DLL = 97;
> U1LCR = 0X03;
>
> }
> int CANAll_Init (unsigned int can_btr )
> {
> unsigned int *p;
> p=&a;

P points to a (obvious), no problem.

>
> AFMR = 0x00000002;
> C1MOD = 0x01;
> C1IER = 0x00;
> C1GSR = 0x00;
> C1BTR = can_btr;
>
> // initialize destination pointer
>
> *p = C1RFS; // Frame
>
> p++;

Incrementing p is a runtime error. Remember that 'a' is only 32 Bits wide?
Now p points to an unknown address.

> *p = C1RID; // ID

Here's the problem: now you write to an invalid address (@runtime).

> p++;
> *p = C1RDA; // Data A

Again..

> p++;
> *p = C1RDB; // Data B

And again.

The same applies to receiver code. After fixing that you can focus on CAN.

Wagner.
> C1CMR = 0x04; // release receive buffer
> C1MOD = 0x00;
>
> return 0;
> }
>
> int CANAll_PushMessage (CANALL_MSG *pTransmitBuf)
> {
> unsigned int *pAddr;
> unsigned int *pCandata;
>
> pAddr = (unsigned int *) &C1TFI1 ;
> *pAddr = pTransmitBuf->Frame & 0xC00F0000;
>
> // Write CAN ID
> pAddr++;
> *pAddr = pTransmitBuf->MsgID;
>
> // Write first 4 data bytes
> pCandata = (unsigned int *) &(pTransmitBuf->DatA);
> pAddr++;
> *pAddr = *pCandata;
>
> // Write second 4 data bytes
> pCandata++;
> pAddr++;
> *pAddr = *pCandata;
> return 0;
>
> }
> void main(void)
> {
> CANALL_MSG MsgBuf; // Buffers one CAN message
> unsigned char data;
> PINSEL1 |= 0x00155000; // Set bits 14 and 16
> serial_init();
> CANAll_Init(CANBitrate125k_12MHz);
>
> while(1)
> {
>
> while((U1LSR & 0x01)==0);
> data=U1RBR;
>
> MsgBuf.Frame = 0x80080000; // 29-bit, no RTR, DLC is 8 bytes
> MsgBuf.MsgID = 0x00012345; // CAN ID
> MsgBuf.DatA = data; // all zeros
> MsgBuf.DatB = data; // all zeros
>
> // Transmit initial message on CAN 1
> CANAll_PushMessage(&MsgBuf);
>
> U1THRa;
> while((U1LSR & 0x20)==0);
>
> delay(500);
>
> }
> }
>
> *********************************************************
> Receiver
> *********************************************************
> #include
> #include unsigned int a=0;
> int CANAll_Init (unsigned int can_btr );
> int CANAll_PushMessage (CANALL_MSG *pTransmitBuf);
> int CANAll_PullMessage (CANALL_MSG *pReceiveBuf );
>
> void delay(unsigned int n)
> {
> while(n--);
> }
>
> void serial_init(void)
> {
> PINSEL0 |= 0X00050000;
> U1LCR = 0X83;
> U1DLL = 97;
> U1LCR = 0X03;
>
> }
>
> int CANAll_PullMessage (CANALL_MSG *pReceiveBuf )
> {
> unsigned int *pSrc; // Source pointer
> unsigned int *pDst; // Destination pointer
> // Initialize pointers
> pSrc = &a;
> pDst = (unsigned int *) &(pReceiveBuf->Frame);
> *pDst = *pSrc; // Copy Frame
>
> pSrc++;
> pDst++;
> *pDst = *pSrc; // Copy MsgID
>
> pSrc++;
> pDst++;
> *pDst = *pSrc; // Copy DatA
>
> pSrc++;
> pDst++;
> *pDst = *pSrc; // Copy DatB
>
> C1CMR = 0x30;
> return 0;
> }
>
> int CANAll_Init (unsigned int can_btr )
> {
> unsigned int *p;
> p=&a;
>
> AFMR = 0x00000002;
> C1MOD = 0x01;
> C1IER = 0x00;
> C1GSR = 0x00;
> C1BTR = can_btr;
>
> // initialize destination pointer
>
> *p = C1RFS; // Frame
>
> p++;
> *p = C1RID; // ID
>
> p++;
> *p = C1RDA; // Data A
>
> p++;
> *p = C1RDB; // Data B
> C1CMR = 0x04; // release receive buffer
> C1MOD = 0x00;
>
> return 0;
> }
>
> void main(void)
> {
> CANALL_MSG MsgBuf; // Buffers one CAN message
> unsigned char data;
> PINSEL1 |= 0x00155000; // Set bits 14 and 16
> serial_init();
> CANAll_Init(CANBitrate125k_12MHz);
>
> while(1)
> {
> // Transmit initial message on CAN 1
> CANAll_PullMessage(&MsgBuf);
>
> U1THRa;
> while((U1LSR & 0x20)==0);
>
> delay(500);
>
> }
> }
>
> Please help me.
>
> Thanks & Regards
> Harish
I've mailed you directly a copy of an example project using KEIL that shows
you how to use CAN1 and CAN2 with IRQ's for the boards you're using MCB2100
(I'm guessing since you're using KEIL's demo boards you're probably using
their starter kit and compiler as well). It's basically the test code I
threw together ages ago to test some can modules on one of our boards and
should illustrate all the issues you need to deal with including the
acceptance filter.

Hope it helps

Andy

-----Original Message-----
From: l... [mailto:l...]On Behalf Of
harisha eh
Sent: 29 November 2006 09:34
To: l...
Subject: Re: [lpc2000] problem in CAN
hi Robert Adsett,
Thanks for the reply,Actually i download CAN test code Net & i modifed
that code(the code sent on previous mail).Im sending that downloaded
code.plz give me suggestions,actually im new to this field.

Thanks and Regards
Harish
Hello,

would it be possible to have a copy of the CAN example too.

i am having some problems too. originally i got CAN working on the at90can128,
2 of them are sending messages back and forth no problem,
with the lpc2129 i am able to listen but don't get all the messages (50/sec) instead 1 pops up every 5sec or so. i tried other timing/sampling point and no luck also.
any suggestions. thanks
regards,

daniel

Andrew Berney wrote: I've mailed you directly a copy of an example project using KEIL that shows
you how to use CAN1 and CAN2 with IRQ's for the boards you're using MCB2100
(I'm guessing since you're using KEIL's demo boards you're probably using
their starter kit and compiler as well). It's basically the test code I
threw together ages ago to test some can modules on one of our boards and
should illustrate all the issues you need to deal with including the
acceptance filter.

Hope it helps

Andy

-----Original Message-----
From: l... [mailto:l...]On Behalf Of
harisha eh
Sent: 29 November 2006 09:34
To: l...
Subject: Re: [lpc2000] problem in CAN

hi Robert Adsett,
Thanks for the reply,Actually i download CAN test code Net & i modifed
that code(the code sent on previous mail).Im sending that downloaded
code.plz give me suggestions,actually im new to this field.

Thanks and Regards
Harish





---------------------------------
All new Yahoo! Mail
---------------------------------
Get news delivered. Enjoy RSS feeds right on your Mail page.