G120 CAN Bit timing II

Hi all, after using the can bit timings from :
http://www.tinyclr.com/forum/topic?id=10910

All my testing on receiving data went well with these settings.

I added a output thread today for sending data and I get “Bus Off” errors.
A minimal sample based on the wiki code:



using System;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using GHI.Premium.Hardware;

public class Program
{
    // Messages list
    static CAN.Message[] msgList;

    public static void Main()
    {
        int T1, T2, BRP;
        BRP = 20;
        T1 = 15;
        T2 = 8;


        // Create a message list of 100 messages
        msgList = new CAN.Message[100];
        for (int i = 0; i < msgList.Length; i++)
            msgList[i] = new CAN.Message();

        // Use channel 1
        CAN can = new CAN(CAN.Channel.Channel_1,(uint)(((T2 - 1) << 20)|((T1 - 1) << 16) |((BRP - 1) << 0)));

        // Subscribe to events
        can.DataReceivedEvent += new CANDataReceivedEventHandler(can_DataReceivedEvent);
        can.ErrorReceivedEvent += new CANErrorReceivedEventHandler(can_ErrorReceivedEvent);

         //Example for sending one message  msg ID
        msgList[0].ArbID = 0x55;
        msgList[0].Data[0] = 1;
        msgList[0].Data[1] = 2;
        msgList[0].Data[2] = 3;
        msgList[0].Data[3] = 4;
        msgList[0].Data[4] = 5;
        msgList[0].Data[5] = 6;
        msgList[0].Data[6] = 7;
        msgList[0].Data[7] = 8;
        msgList[0].DLC = 8;
        msgList[0].IsEID = false;
        msgList[0].IsRTR = false; 

        for (int t=0;t < 10;t++)
        {
            Debug.Print(can.PostMessages(msgList, 0, 1).ToString());
            Thread.Sleep(1000);
        }

    }

    static void can_DataReceivedEvent(CAN sender, CANDataReceivedEventArgs args)
    {
        Debug.Print(">>> can_DataReceivedEvent <<<");

        // read as many messages as possible
        int count = sender.GetMessages(msgList, 0, msgList.Length);
        for (int i = 0; i < count; i++)
        {
            Debug.Print("MSG: ID = " + msgList[i].ArbID + " at time = " + msgList[i].TimeStamp);
        }
    }

    static void can_ErrorReceivedEvent(CAN sender, CANErrorReceivedEventArgs args)
    {
        Debug.Print(">>> can_ErrorReceivedEvent <<<");

        switch (args.Error)
        {
            case CAN.Error.Overrun:
                Debug.Print("Overrun error. Message lost");
                break;

            case CAN.Error.RXOver:
                Debug.Print("RXOver error. Internal buffer is full. Message lost");
                break;

            case CAN.Error.BusOff:
                Debug.Print("BusOff error. Reset CAN controller.");
                sender.Reset();
                break;
        }
    }
}

If i run this, i get 10x

1

can_ErrorReceivedEvent <<<
BusOff error. Reset CAN controller.

In the debug window.

Ideas?

@ David@ Emrol -

I’m not sure if PostMessages is an Async call, but if yes, may be the post is locked by the thread sleep before all the message is sent (as it is the same thread for both)…Maybe creating a thread for the post should be an issue…?

I don’t know, as far as I understand this, the bus off state is a can mechanism to isolate “problem members” (like a jabber on a network) after 255 errors.

I’m not sure if a bad bit time setting could cause this behavior.
I just put this sample app up with a G120HDR a breadboard and a CAN transceiver attached to a CAN analyzer that reports to my PC over USB.
No messages are showing up in the output, just the bus off on the G120.

Today i grabbed a EMX board with the same sample code, changed BRP to 20, connected the EMX can port to the G120 can port, hit F5 and this works just fine.
Connected this to a real can data bus with 25% load, EMX receives data just fine, G120 receives data just fine, EMX send’s data just fine, G120 sends data = “bus off” on G120 and no messages on the can bus…

@ David@ Emrol - Canyou add the POstMessageSent event to sse if at least, ti send one message or crash at first try ?

The returnvalue can1.PostMessages(msgListOut1, 0, msgListOut1Pointer) = 1 (i only send 1, but this seems ok like 1 send from buffer suze)
If i read PostedMessagesSent as next statement i get a “True” and next pop’s the CAN.Error.BusOff

I also replaced the transceivers, and used the CAN2 from emx on the same transceivers, everything works except can postmessage from G120

OK, and if you add the CAN.Reset() when BusOff is thrown ?

This was already in the event :



                case CAN.Error.BusOff:
                    DebugOut("CAN1 BusOff error. Reset CAN controller.");
                    SysLogFileWriteLine(RealDateTime + " : " + "CAN1 BusOff error. Reset CAN controller.");
                    sender.Reset();
                    break;

Oups ! OK !

FYI

I’m still using 4.2.7 NOT 4.2.9
But i searched the forum and did not found any CAN related problems with this release so i dont think 4.2.9 would solve it.

For re-calculating the T1 and T2 values i need the speed were CAN controller is clocked with.
This value is based on µcontroller clock / BRP (this wat i read in the wiki)

So for the EMX this was 72Mhz / BRP12 = 6Mhz as CAN Clock
For the G120 we have 120Mhz / BRP20 = 6Mhz CAN Clock ( i assume this should be indeed the same)

So BRP=20 looks ok
Next the T1, T2 and TQ

Based on the Kvaser calculator (using 6Mhz CAN clock and 125Kb CAN speed) i get
T1=16 (now I’m using 15)
T2=8 (the same)
Tq=24 (16 or 24 in the wiki so ok I guess)

Is this calculation correct? Or did I missed something?

Did some more testing today, used a clean G120HDR (running 4.2.7) and 2 gadgeteer CAN DW 1.2 modules and a EMX dev board with the onboard CAN1

Run the sample app on the G120 -->> bus off error after each message send
Run the sample app on the EMX -->> can_DataReceivedEvent on the G120

dont realy know what to check now…

I go update to 4.2.9 to see if this makes a differce

I think i will go home now… 2 failures and now the device disconnects every minute or so and then reconnects… pongpong - pongpong
Will try update again tomorow morning.

@ GHI: is this sample app working on your side?

Yes we can update firmware of course. Did you use WinUSB at some point? If so, roll back to the NETMF drivers, no WinUSB.

Yes i did use the WinUSB drivers, will try in the morning.

@ Gus - Not the update the sample app on page 1 to send CAN msg on the G120 with that bit timing.

This morning i uninstalled the drivers (marked the checkbox to delete the driver) and then connected a new G120HDR.

Driver installed correct, device listed in the device manager, LDR0 and 1 low, reset, shows as COM27, run the updater, and after displaying “executing application” for about 2 minutes i get the error.

And also error using the old updater, can someone confirm the driver installed in the screenshot is the correct one?

Did not remember there was an “Execuring Application…” in m case ? But maybe I did not take care of it…