I have a Colibri iMX7D module on an Aster eval board. I have an external CAN transceiver connected to another CAN device. I am using pins 90 & 92 for Rx and Tx and am configuring these in my application.
The Flexcan appears to be configured correctly with these pins because on the 'scope I can see that messages from the external device are being acknowledged by the Colibri. I can also see that when I transmit a message, it appears on the 'scope and is acknowledged by my external device.
The problem is that when I call either of the following,
I have also noticed that when sending a message, although I can see it on the 'scope and it is acknowledged, the call
can.Can_Write(hCAN, ptr)
always returns 0.
I have attached my test project which is an adaption of your demo project.
Another thing I noticed is that in TdxAllLibraries.cs, the structure of the tCANMsg may not be correct.
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto) ]
public struct tCanMsg
{
public UInt32 id; ///< ID for CAN Bus Message Buffer (11 or 29bits used)
public UInt32 canMsgFlags; ///< message specific flags.Currently only 2 bits are used:\n * @ref IDE "CanMsgFlags_IDE"\n * @ref RTR "CanMsgFlags_RTR" (read only)
public UInt32 dataLen; ///< Message Data Length (0...8). This matches the DLC field of a CAN message.
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
public char[] data; ///< Message Data (0...8 bytes used). This matches the data field of a CAN message.
};
Shouldn’t the public char[] data at the end actually be public byte[]data? chars are a 16-bit representation in c#/.net.
At Toradex we rarely use C# except for writing the kind of interfaces for the library, so we don’t have too much experience.
Most of the library testing is done with C applications only - this explains why we didn’t find this problem so far.
Your error reports and suggestions for fixing seem to be very reasonable to me. It will take me some days before I will find time to do any tests on this.
To not be blocked I recommend to do the modifications in your local copy of the library. As the .NET interface part is in source code, it will directly affect your application.
I’ve now managed to test this with a C application adapted from the demo in the toradexcelibraries_2.3-20181011 and I am getting the same results as in my C# application. So the problem seems to be either in your CAN driver or in the way I have used it. I have attached my adapted project.
Thank you for your patience and efforts to report the bug.
Could you please enable debug message and capture the messages during the CAN transfer and let us know if it prints any messages.
Could you confirm that CAN message transmission is successful?
Just I checked the code, implementation seems to be proper. FlexCAN APIs returns 0 if there are any errors like CAN_ERR_TRANSFER_TIMEOUT or CAN_ERR_INSUFFICIENT_SPACE or CAN_ERR_TX_OVERFLOW or other errors detected by CAN controller.
Please let us know if you are seeing message transfer successful but FlexCAN APIs still returns the wrong value.
I can confirm that transmit is successful, returning 1 and I can see the data on my CAN analyser.
For recieve, it just times out, the return from Can_Read(hCan, &canBuf) is 0.
I have modified the recieve part to add a timer:
DWORD start;
// Configure timeout to 5 sec, by default it is set to 1 sec
Can_SetConfigInt(hCan, L"Timeout", 5000, StoreVolatile); // optional
// 4. Use CAN
canBuf.dataLen = 8;
// Record start time
start = GetTickCount();
printf("\nStart Time: %dms", start);
rxRes = Can_Read(hCan, &canBuf);
//Record duration
start = GetTickCount() - start;
printf("\nCAN Recieve Result: 0x%x Time taken: %dms", rxRes, start);
So I can see from this that it is taking 5002ms.
So although the response of 0 is correct for a timeout, why is it timing out and not getting the data that has been sent from my CAN analyser?
I am confident that our hardware is correct as I have built my own CAN driver by using MapMem to access the FlexCAN module of the iMX7. With this I have correct operation.
Any chance, Do you have our Colibri Evaluation board? I quickly tested iMX7D with Colibri Eval board, the CAN interface is working flawlessly.
Hence it seems we need to verify our HW and SW thoroughly.
Is it a 120-ohm termination resistor placed?
If possible, could you go through this documentation https://developer.toradex.com/knowledge-base/how-to-use-can-library and let us know your setup is similar
Could you confirm SODIMM_90 and 92 not connected to any other place and it is connected to CAN transceiver chip only?
Maybe could you draw a connection diagram on the paper from IMX7D module to till the PC? it would trigger a point to find the issue. The timeout can occur due to hardware connection problem or software configuration problem. Let us first verify the hardware connection is perfect since the default CAN_Demo application works I don’t expect the software configuration problem.