Hi,
This is my first go with any GHI products. I recently ordered up a FEZ Cobra 2 REV B to demo the G120 SOC setup.
I’m trying to get the CAN bus to work. I’m using a MCP2551 Can Transceiver. I cannot receive or transmit. When I transmit I get a CAN Bus “Bus-Off error”. I’m running the latest firmware.
I can see data on both sides of the transceiver through both a scope and logic analyzer, which leads me to believe its a issue with code or the G120 itself.
I have also tried two different setups with the MCP2551 which I have included the schematics for.
The logicdata can be downloaded from (http://sdrv.ms/1gj7QKk) and the code is below.
Any help would be greatfully appreciated!
[em] Note: I have a 2 known good can analyzers on the same bus, once sending, and one is listen only mode. I can view the data being sent on my separate receiver no problem. (Both are based on the MCP2515 & MCP2551 chipsets.)[/em]
using System;
using System.Threading;
using GHI.Premium.Hardware;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
namespace MFConsoleApplication1
{
public class Program
{
private static CAN.Message[] msgList;
public static void Main()
{
//First lets set date/time
Utility.SetLocalTime(new DateTime(2013, 09, 15, 08, 55, 00));
uint bitRate = GetBitRateConfig_120MHZ(Bitrate.BITRATE_500k);
CAN can = new CAN(CAN.Channel.Channel_2, bitRate);
can.DataReceivedEvent += can_DataReceivedEvent;
can.ErrorReceivedEvent += can_ErrorReceivedEvent;
int clock = 1;
CAN.Message sendMessage = new CAN.Message();
while (true)
{
sendMessage.ArbID = 0x7E5;
sendMessage.Data[0] = (byte)clock;
sendMessage.Data[1] = 0x78;
sendMessage.Data[2] = 0x5F;
sendMessage.Data[3] = 0xFF;
sendMessage.Data[4] = 0x3B;
sendMessage.Data[5] = 0x00;
sendMessage.Data[6] = 0x00;
sendMessage.Data[7] = 0x4C;
if (clock == int.MaxValue - 1)
clock = 0;
clock ++;
CAN.Message[] messages = new[] {sendMessage};
int numberMessagesPosted = can.PostMessages(messages, 0, messages.Length);
var test = can.TransmitErrorCount;
if(test> 1)
Debug.Print(test.ToString());
Thread.Sleep(500);
}
}
private 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;
}
}
private 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++)
{
string m = ("Data: " + msgList[i].Data[0].ToString("X") + " " + msgList[i].Data[1].ToString("X") + " " + msgList[i].Data[2].ToString("X") + " " + msgList[i].Data[3].ToString("X") + " "
+ msgList[i].Data[3].ToString("X") + " " + msgList[i].Data[4].ToString("X") + " " + msgList[i].Data[5].ToString("X") + " " + msgList[i].Data[6].ToString("X") + " "
+ msgList[i].Data[7].ToString("X"));
Debug.Print("MSG: ID = " + msgList[i].ArbID + "DATA: " + m + " at time = " + msgList[i].TimeStamp);
}
}
public enum Bitrate
{
NONE = 0,
BITRATE_125k = 125000,
BITRATE_250k = 250000,
BITRATE_500k = 500000,
BITRATE_1000k = 1000000,
}
/// <summary>
/// Returns a bitrate for the internal CAN driver
/// </summary>
/// <param name="bitrate">Bitrate</param>
/// <returns>CAN driver bitrate for LPC17XX</returns>
public static uint GetBitRateConfig_120MHZ(Bitrate bitrate)
{
int t1 = 0;
int t2 = 0;
int brp = 0;
switch (bitrate)
{
case Bitrate.BITRATE_125k:
brp = 20;
t1 = 16 - 1;
t2 = 8;
break;
case Bitrate.BITRATE_250k:
brp = 10;
t1 = 16 - 1;
t2 = 8;
break;
case Bitrate.BITRATE_500k:
brp = 5;
t1 = 16 - 1;
t2 = 8;
break;
case Bitrate.BITRATE_1000k:
brp = 5;
t1 = 8 - 1;
t2 = 4;
break;
}
return (uint) (((t2 - 1) << 20) | ((t1 - 1) << 16) | ((brp - 1) << 0));
}
}
}