Problem with CanBusExample1

15 May 2010 . Edited: 15 May 2010

I’m trying to following the CanBusExample1 at

http://mbed.org/projects/cookbook/wiki/CanBusExample1

The mbed is connected a MPC2551 Can transceiver. My Can network consist of the mbed and a car ECU.

I’ve modified the send() function so that it sends out a 0x7df 0x02 0x01 0x0c message which is a request of the engine’s rpm.

The problem is from the serial message I’m seeing two Rx messages. The first message is the Tx message somehow echoed back. The second message is the correct message reply from the ECU.

Below is the serial log:

 

CanTx--> id: 0x200  dlc: 1  data: 4a

CanRx--> id: 0x7df  dlc: 8  data: 2 1 c 0 0 0 0 0

CanRx--> id: 0x7e8  dlc: 8  data: 3 41 c 0 48 0 0 0

CanTx--> id: 0x200  dlc: 1  data: 4b

CanRx--> id: 0x7df  dlc: 8  data: 2 1 c 0 0 0 0 0

CanRx--> id: 0x7e8  dlc: 8  data: 3 41 c 0 30 0 0 0

Ignore the CanTx id, I’ve not updated the prinft.

With a CanBus analyzer I can only see one Rx message which is correct.

The question is how come can2.read() function returns a 1 to indicate a message has arrived but it is from the Tx message that was just send?

Any ideas?

 

17 May 2010 . Edited: 17 May 2010

Hi Sukkin,

All in all the CAN software seems to be good enough to start using the CAN bus, but doesn't let me do everything I want to - like having an unusual speed for example. However, a friend told me about a website www.coocox.com which has all sorts of software that can be used with mbed including some source code for CAN bus which might help - look under the 'Coocox components, NXP' section.

I don't know how to stop this happening and at first it was a bit of a problem. I did read through the datasheet (book) and it seems to be a feature you can turn on or off. Now I am thinking of using these repeated messages to check that the message is actually sent because you don't get these messages if there is a problem on the CAN connection.

Sophie x

 

17 May 2010 . Edited: 17 May 2010

Hi Sophie,

So you are seeing this repeated message is well? I’ve got another dev board using the MCP2515 and I don’t get this repeated message.

At the moment I’ve done a work round by reading the can2.read() twice and checking the Rx Can ID and ignoring the one I’ve just send.

I’m developing a mbed Can-bus demo board with LCD, uSD card holder and a GPS connector. Rev B of the PCB has just gone to the manufacturer in China and should be ready in about two weeks time.

Sukkin

mbed boards from the UK

18 May 2010 . Edited: 18 May 2010

Hi Sukkin,

this might be it; I found this description in the user manual...

Table 320. CAN Command Register (CAN1CMR - address 0x4004 4004, CAN2CMR - address 0x4004 8004) bit
description

4[1][6] SRR Self Reception Request. 0 0
0 (absent) No self reception request.
1 (present) The message, previously written to the CANxTFS, CANxTID, and
optionally the CANxTDA and CANxTDB registers, is queued for
transmission from the selected Transmit Buffer and received
simultaneously. This differs from the TR bit above in that the receiver is
not disabled during the transmission, so that it receives the message if its
Identifier is recognized by the Acceptance Filter.

[1] - Setting the command bits TR and AT simultaneously results in transmitting a message once. No re-transmission will be performed in
case of an error or arbitration lost (single shot transmission).
- Setting the command bits SRR and TR simultaneously results in sending the transmit message once using the self-reception feature.
No re-transmission will be performed in case of an error or arbitration lost.
- Setting the command bits TR, AT and SRR simultaneously results in transmitting a message once as described for TR and AT. The
moment the Transmit Status bit is set within the Status Register, the internal Transmission Request Bit is cleared automatically.
- Setting TR and SRR simultaneously will ignore the set SRR bit.

[6] Upon Self Reception Request, a message is transmitted and simultaneously received if the Acceptance Filter is set to the corresponding
identifier. A receive and a transmit interrupt will indicate correct self reception (see also Self Test Mode in Section 16–7.1 “CAN Mode
register (CAN1MOD - 0x4004 4000, CAN2MOD - 0x4004 8000)”).

I haven't tried changing it though so you would be the first to try this :-)

If you look in the CAN section of the this header file you will find names for these addresses:

http://mbed.org/projects/libraries/svn/mbed/trunk/LPC1768/LPC17xx.h

This is how I change the CAN speed for 615 k/bit:

LPC_CAN2->BTR = 0x370002;

I guess you will do something similar with the 'CMR' register:

LPC_CAN2->CMR = 0xCafeBabe;                  // obviously change CafeBabe to what ever you need it to be, and use LPC_CAN1 for the other CAN interface

Hope this helps,

Sophie x

19 May 2010

Hi Sophie,

I’ved tried clearing the SRR bit, which is bit 4 by doing a:

LPC_CAN2->CMR &= ~(1<<4);

Just after setting the Can speed but it didn’t make any different.

Do you know if we can see the source code for this Can library? I was hoping by using the mbed library it would make things easier but this is not the case.

 

20 May 2010 . Edited: 20 May 2010

Hi Sukkin,

Sorry my guess didn't help, I've just spotted this note which may be significant.

- Setting TR and SRR simultaneously will ignore the set SRR bit.

The user manual for the mbed chip has large sections on all of the mbed chip functions, chapter 16 is about CAN, there is a link to the user manual at the bottom of this page:

http://mbed.org/nxp/lpc1768/technical-reference/

The source mbed source code seems to be a closely guarded secret, but hope is not lost - a friend of mine told me about another website a few days ago:

http://www.coocox.com/

This page on their website has source code for the CAN chip on the mbed system (and a whole lot more):

http://www.coocox.com/chip_search.php?Vendor=NXP&Series=LPC17xx&Chip=LPC1768

From what I've seen, this isn't the same as the mbed library for CAN, actually it seems that maybe the mbed CAN library is a smaller, easier to use, but less 'controllable' (not the best description, but I mean the mbed library can do less) version of this code - just a guess.

Sophie x

 

31 May 2010

I've been playing with this a little bit and think I've got an understanding.

I think that I have been able to successfully transmit CAN messages without requiring an Ack by setting the self test mode bit of the CANxMOD register like so:

LPC_CAN2->MOD |= 0x1;  // Reset mode = 1
LPC_CAN2->MOD |= 0x4;  // Self Test Mode = 1
LPC_CAN2->MOD &= 0xFE; // Rest mode = 0 - operational mode

However, if my understanding is correct, then the CANxCMR register is used to trigger the sending of a CAN message and hence the flags are set at the time of transmission, so the CAN library will be setting bit 0 to 1 to request the transfer layer transmits the CAN frame in the send buffer.  At this time, I presume the SRR bit 4 is also being set true.

Therefore my first question is whether there exists already a way to control the value written to CANxCMR when transmitting a CAN message without having to re-invent the wheel and write my own CAN library.

My second question that should perhaps be directed toward another thread, is whether it would be possible to give the CAN library a function pointer to be called upon a CAN receive interrupt, rather than having the code sit in a loop polling the read() function?

 

As this is my first post, I'll also say that I'm blown away by the mbed project.  I've had an AT90CAN128 sat on my desk for weeks waiting for me to finally get around to learning how to use it for some proof of concepts I've had kicking around for a while, but having no experience with hardware and embedded systems was very much procrastinating.  Having heard about the mbed last week, I was up and running on an old breadboard within 5 minutes of opening the box and yesterday managed to write in a matter of an hour or two a simple analogue to CAN interface to try out some ideas.  Brilliant!