Created Thu, 29 Jan 2015 08:23:14 +0000 by unexpectedly
Thu, 29 Jan 2015 08:23:14 +0000
Hey everyone,
Are there any real examples of working code for the CAN bus?
I've got MCP2551 can transceiver set up as in the image and finally it isn't interfering with the diagnostic equipment and the vehicle being tested. (ie, I'm in the middle and wanting to sniff and before now, my connection was interfering)
I'm using the code from the CAN example. In fact I have only made a couple small changes...
Here's my setup:
void setup() {
Serial.begin(9600);
Serial.println("Init:");
/* Init each CAN controller module for use. */
initCan1(node1can1);
initCan2(node1can2);
/* Install the interrupt service routines. */
canMod1.attachInterrupt(doCan1Interrupt);
canMod2.attachInterrupt(doCan2Interrupt);
Serial.println("can bus modules initialized.");
}
And only just now I'm actually getting the "can bus modules initialized." text printed to the serial port. So my current configuration is working better for the whole system... meaning that when it was set up without the 80-something ohm resistor, I wasn't getting the initialized text but also the diagnostic equipment couldn't talk to the vehicle's CANBUS.
And here's my epic loop:
void loop() {
delay(100); //wait so that the character has time to be delivered
rxCAN1();
rxCAN2();
}
And I've got the CAN Rx line connected to pin 15, which is CAN1 Rx on the max32.
The function rxCAN1() has this in it:
Serial.print(byte(message->data[0]));
So given the simplicity of it all, I'd like to think that if the sample code works, I'd at least get something bothering me on the serial monitor. But I only get "Init:" and the "initialized" messages.
The example I'm using is literally from the MPIDE CAN example. That's the only chipKIT / PIC32 CAN code I've found yet on the net.
So -- I'd really appreciate guidance, ideas, or even dumb ideas of what to try next.
Thanks! Chris
PS - I would probably already be going with parallel development on the crappier all-Arduino side but the postman chose to lose my SparkFun CANbus shields. For those, there is working code. But they utilize external CAN controllers as well as the MCP2551 CAN transceiver. I'm ass-u-ME-ing that interacting with an external CAN controller is different from the built in pair of CAN controllers in the max32...
Thu, 29 Jan 2015 12:14:56 +0000
Before you go too far, ensure that you have both TX and RX lines connected. You can put the CAN interface in a listen only mode on the Max32. If you don't do this, you may end up with further problems, as it's not quite a simple serial interface. Also, add a resistor around 10K between the RS pin and ground. I had no end of problems until I hooked things up fully.
It would also be good to know what configuration you are applying to the CAN module!
One thing that you might want to consider is setting up the two CAN interfaces on your Max32 and sending from one to the other (like the chipKit example) to check your code works properly. Don't forget the 120 ohm terminating resistors on both ends though.
I ended up doing this so I could check that my code works, before I let loose on a test harness. (Although I was using the Fubarino SD, rather than Max32) .There are CAN settings that you'll need to make sure match, otherwise you'll not get much. For my test harness, I ended up using an off the shelf ODBII adapter, and simulated the PID responses to the PID requests made by the ODBII adapter. Sadly, I haven't been able to take this further, due to limitations on accessing CAN on a vehicle...
Back to the termination resistors - the CAN bus should only need to be terminated at either end - you shouldn't need to terminate it on your device if your node is not at the either end of the bus. However, do be aware that in NORMAL_OPERATION mode, your node will participate fully on the bus. That includes error detection and signalling. If any CAN receiver node detects an error (frame check, bit stuffing etc), they will transmit an error frame to obliterate the message and highlight to the transmitter that it's message has been damaged. You can check the error counters in C1TREC for CAN1. There's a fair bit of information in the Section 34. Controller Area Network (CAN) documentation.
I've attached the last version I had of my CANbussimulator. I can't remember what state it was in. It was based on the MPIDE CAN example, as like you, it's the only chipKIT CAN code I found...
Hope that helps!
Tue, 03 Feb 2015 21:22:10 +0000
Thanks for your reply and your source code, too.
Things I know:
Having said that, I really picked through "Step 2: Configure the CAN Module Clock."
The PIC32 CAN data sheet is slightly worse than useless... (61154C.pdf). It was referenced in another forum post here. I ended up finding a downloadable calculator that saved my bacon. I changed to these settings, uploaded and instant output:
canBitConfig.phaseSeg2Tq = CAN::BIT_8TQ;
canBitConfig.phaseSeg1Tq = CAN::BIT_8TQ;
canBitConfig.propagationSegTq = CAN::BIT_3TQ;
canBitConfig.phaseSeg2TimeSelect = CAN::TRUE;
canBitConfig.sample3Time = CAN::TRUE;
canBitConfig.syncJumpWidth = CAN::BIT_1TQ;
Woot!
Wed, 04 Feb 2015 11:37:13 +0000
Nice find on the timing tool - glad you've been able to get things up and running.