Sample code with CAN communication

Forum related to the FLEX boards

Moderator: paolo.gai

Locked
pau.marti@upc.edu
Newbie
Posts: 4
Joined: Mon Oct 13, 2008 7:25 am

Sample code with CAN communication

Post by pau.marti@upc.edu » Fri Nov 14, 2008 1:25 pm

We are trying to send/receive messages over CAN from ERIKA on Flex using the CAN modules of the multibus board. It seems not easy.. at least for us. Does anyone has a simple sample code to share?

Thanks

paolo.gai
Administrator
Posts: 875
Joined: Thu Dec 07, 2006 12:11 pm

Re:Sample code with CAN communication

Post by paolo.gai » Fri Nov 14, 2008 1:55 pm

Hi pau!

I hope everything is fine with you...

I know that Luigi from UNITN and Roberto from SUPSI have some CAN code working.

We are also trying to get it to put it into the default branch of EE.

I\'ll contact them, maybe we\'ll be able to have something to post soon...

Ciao!

PJ

moonckim
Newbie
Posts: 15
Joined: Fri Feb 04, 2011 9:18 pm

Re: Sample code with CAN communication

Post by moonckim » Fri Apr 01, 2011 6:20 pm

Any updates on this topic?

Another question: Does RT_Druid include any CAN device driver for the CAN adapter module on multibus daughter board? Or, are there any CAN drivers available for FLEX platform?

Thank you,
Moon

paolo.gai
Administrator
Posts: 875
Joined: Thu Dec 07, 2006 12:11 pm

Re: Sample code with CAN communication

Post by paolo.gai » Fri Apr 01, 2011 7:47 pm

As long as I know:

- some code is in the repository, please check
http://svn.tuxfamily.org/viewvc.cgi/eri ... iew=markup

- scicoslab blocks for CAN are available as blocks
http://svn.tuxfamily.org/viewvc.cgi/eri ... unication/

- The code was part of the training course last year in pisa. it wil be a topic of this year course in June as well...

Ciao,

PJ

moonckim
Newbie
Posts: 15
Joined: Fri Feb 04, 2011 9:18 pm

Re: Sample code with CAN communication

Post by moonckim » Wed Apr 20, 2011 6:36 pm

With the source code from the repository, I was able to run a loopback test (JP1 on CAN module) with CAN module on top of Multibus daughter board. I was able to receive the message I sent through the DMA configuration.

However, when I tried to test communication between two sets of Flex Full + Multibus daughter + CAN module, it failed. Wiring is CAN+ to CAN+ and CAN- to CAN-. I put 120ohm resistors at both ends (of the CAN modules). I also tested with a single terminator and no terminator, but neither worked.

I can't tell whether it's a problem of sender, receiver, or both until a CAN bus analyzer will be delivered in a week or so. Nevertheless, any comments or advice will be appreciated.

Thank you,
Moon

paolo.gai
Administrator
Posts: 875
Joined: Thu Dec 07, 2006 12:11 pm

Re: Sample code with CAN communication

Post by paolo.gai » Thu Apr 21, 2011 8:05 am

Quite strange... Please note that the CAN modules already has the terminators on the module (just put jumper JP1 on the CAN module...)

Ciao,

PJ

moonckim
Newbie
Posts: 15
Joined: Fri Feb 04, 2011 9:18 pm

Re: Sample code with CAN communication

Post by moonckim » Thu Apr 21, 2011 5:05 pm

I put jumpers on JP1 of both CAN modules and connected CAN+ & CAN- without any resistors. However, it's not working.

Please see below for my code snippet for init, send, and receive. One sender (FLEX Full+Multibus+CAN module) periodically calling Send_ECAN1() while one receiver periodically calling Receive_ECAN1(). The receiver is not blocked by calling EE_ecan1_Rx(), but the function just returns empty data (with all 0's as the associated DMA channel buffer is initialized). I also checked that both the sender task and the receiver task are invoked properly.

When I tested this code on a single board with a single task calling Send_ECAN1() and Receive_ECAN1() in a row, it worked fine. Any comments?

// init in main()
main()
{
...
EE_ecan1_Initialize();
...
}

unsigned char iter = 0;
ee_ecan_mID ecanMsg1, ecanMsg2;
void Send_ECAN1()
{
int i;
int data_len = 8;

ecanMsg1.id = 0x1FFF0000;
ecanMsg1.message_type = CAN_MSG_DATA;
ecanMsg1.frame_type = CAN_FRAME_EXT;
ecanMsg1.data_length = data_len;
for (i = 0; i < data_len; i++) {
ecanMsg1.data = iter++;
}

// send a CAN message
EE_ecan1_SendMessage(&ecanMsg1);
}

void Receive_ECAN1()
{
int i;

// receive a CAN message
EE_ecan1_Rx(&ecanMsg2);

if (ecanMsg2.data_length > 0)
{
for (i = 0; i < ecanMsg2.data_length; i++) {
EE_UART1_Send(ecanMsg2.data);
}
}
}

moonckim
Newbie
Posts: 15
Joined: Fri Feb 04, 2011 9:18 pm

Re: Sample code with CAN communication

Post by moonckim » Wed May 11, 2011 3:45 pm

Has anyone succeeded to receive a CAN message using the code posted above? It works for "loopback" test, but I am having trouble with a two board setup, one tx and another rx. I can see the traffic on the bus using a CAN bus analyzer but the receiver never gets the messages. It seems no data is written to DMA when I monitor the ecan1msgBuf.

Any comments?

Thanks in advance,
Moon

paolo.gai
Administrator
Posts: 875
Joined: Thu Dec 07, 2006 12:11 pm

Re: Sample code with CAN communication

Post by paolo.gai » Thu Nov 03, 2011 9:06 am

I know it is a little bit late as a response, but a demo on using CAN has been committed on the ERIKA repository and will be available in the next build.

The demo is on
examples/pic30/pic30_MultibusBoard_can_demo

and it has been committed in revision 1371

Ciao,

PJ

Locked