Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

CAN-bus library for general CAN-bus controller chip MCP2515

 
Post new topic   Reply to topic    www.mcselec.com Forum Index -> Share your working BASCOM-AVR code here
View previous topic :: View next topic  
Author Message
Per Svensson

Bascom Member



Joined: 03 Oct 2004
Posts: 181
Location: Gothenburg, Sweden

sweden.gif
PostPosted: Mon Jan 06, 2020 5:16 pm    Post subject: CAN-bus library for general CAN-bus controller chip MCP2515 Reply with quote

If you are interested in communication over a CAN-bus, this might be a thread for you.
My ambition has been to create a Bascom include library for a general CAN controller chip rather than the one that exists in a few of the AVR-chips.
Thus, making CAN available for virtually all members of the AVR family. I have chosen the MCP2515 controller as it seem to be widely spread and well proven
It is also rather cheap. Both as a chip and mounted on a PCB.

I have used an Arduino UNO with a CAN-shield from RobotDyn or Spark-fun during my development. This combination will have access so all necessary pins over the standard Arduino pinning,
so no extra wires are necessary. You can find these shields from Ali-baba, e-bay or Spark-fun. They are all very similar.

For bus testing I have used the "CAN bus Analyser" from Microchip. A simple tool that still can handle most CAN tasks and log the results.
If you don't want to invest in this there is also a possibility to put the MCP2515 in "loopback mode". It will allow testing of most functions without actually hooking up to a real bus at all.

The first library version 1.2 that I publish here is a little more than just an embryo for a finished and well proven lib, but certainly nothing you can trust yet. My hope is that we together can make it
better, and more solid.

I suggest that you start having a look in the functional description file "CAN library description - Revision 1.2.txt". Here is the syntax for the various function calls and a short description of what they do.
I also recommend that you skim through the MCP2515 data-sheet to get a good understanding of the internal chip structure and how buffers are handled. (there are three for transmission and two for reception. Message filtering is also a vital part of the communication.)

Next, try to compile the example file "MCP2515 Simple CAN R1.2.bas". It will call all necessary routines from the INC-file "CAN_MCP2515_package.inc"
This example will allow you to send and receive CAN messages in loopback or normal mode. The default baudrate is 125kbit/s but you can chose any of the rates recommended by the CAN_OPEN interest group "CiA". I will do all I can to make this lib as compatible as possible to CAN-OPEN. Type "?" for a menu of a few commands that will come in handy during your evaluation.

There is also an auto-baud function is the lib, that should make life easier as it tries to automatically hook up to an existing bus by listening to ongoing traffic (is there is any) and adapt to that rate.
This is invoked during boot-up, but is not really tested since I had no bus with already talking devices.

I hope this contribution will fill a gap and make CAN-communication easier and more understandable. I also hop that you will contribute to the refinement of this "embryo". Either by issuing your own updated lib, or by sending me new enhancements or bug reports so I can generate updates.

Have fun!
/Per
Back to top
View user's profile Visit poster's website
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 4858
Location: Holland

blank.gif
PostPosted: Wed Jan 08, 2020 11:52 am    Post subject: Reply with quote

Hi Per,

That is excellent work !
It seems a nice chip and indeed it is convenient when you can use any AVR without CAN hardware.
When i have time i will study this chip some more. When this matures i can have a look at possible integration when it makes sense.

Thanks for sharing !

_________________
Mark
Back to top
View user's profile Visit poster's website
Per Svensson

Bascom Member



Joined: 03 Oct 2004
Posts: 181
Location: Gothenburg, Sweden

sweden.gif
PostPosted: Wed Jan 08, 2020 12:44 pm    Post subject: Reply with quote

Thanks Mark!

I think it can be useful, but I also wonder how many Bascom'ers that really use CAN?
Besides from car diagnostics I have a feeling that it is not used that much among hobbyists.
It is a robust bustype so it deserves more attention.
Could be interesting to get some "hands up" here..

One interesting feature in this chip is the ability to send a message for "transmit request"
It will offer instant reply of data that is already loaded in the remote data registers (up to eight bytes).
This is not tested yet, but is on the todo list. However, It is in principle already supported by the CAN_TRANSMIT function by setting param RW to 1.

The chip is rather simple but the use of multiple bit mapping of certain flags maked it sometimes confusing.
The message masking and filtering is also rather complex so I will ty to hide as much of this handling and let the lib sort it out on the higher level.

My ambition is also to stick to CAN_OPEN as much as possible, as there are lots of profiles for various equipment already defined. Especially for talking
to sensors of all kinds.

Did you try to compile the example I attached?

/Per

//PS. How do I apply for HTML code to be allowed? I cannot format my text. Can you fix it? //DS
Back to top
View user's profile Visit poster's website
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 4858
Location: Holland

blank.gif
PostPosted: Wed Jan 08, 2020 2:15 pm    Post subject: Reply with quote

I also think that CAN is more for the profi. But there are lot of profi bascom users.

your code compiles ok but there are some things you can improve.
for example : Data() , the code explorer will flag this because DATA is also a reserved word.

I also needed to change the absolute inc path to relative. but no problem
Very Happy

_________________
Mark
Back to top
View user's profile Visit poster's website
tomi

Administrator



Joined: 22 Feb 2005
Posts: 155
Location: Universe

blank.gif
PostPosted: Wed Jan 08, 2020 2:52 pm    Post subject: Reply with quote

Per Svensson wrote:

//PS. How do I apply for HTML code to be allowed? I cannot format my text. Can you fix it? //DS


HTML tags are not allowed but for formatting text you can use BBtags in editor, they provide enough options to format your post.

_________________
Best regards,
/Tomi
Back to top
View user's profile Visit poster's website
EDC

Bascom Expert



Joined: 26 Mar 2014
Posts: 631

poland.gif
PostPosted: Wed Jan 08, 2020 4:01 pm    Post subject: Reply with quote

I was download the library and take a look for the code. I`ve saw the two links. First was for polish forum where in 2012 this Bascom related CAN topic was started and some part of the your library is ported. You are onest man.

My previous job was mechatronic so I have plenty of car cables Very Happy and steel sentiment for cars electronics.
So I have two VCDS cable. One is broken and maybe I dont try to fix it by simple repair because it have Mega162, FT323BL and topic chip MCP2515 Very Happy
Ofcourse every CAN needs transceiver like AVR needs too. They are two popular and this cable have PCA82C250.
Maybe someone will be interrested how proffesionall cable was made so I attach PDF with schematic. GAL on this circuit is for some protocol/pin switching.
Also it is good to look how K-Line can be driven with specialized chip L9637.

To the point.
I have some CAN gateway devices and car ECU`s . Maybe with your work I try to ask them with Bascom language Very Happy

I appreciate your work. I always want to do this but nobody have time for everything Very Happy
Back to top
View user's profile Visit poster's website
Per Svensson

Bascom Member



Joined: 03 Oct 2004
Posts: 181
Location: Gothenburg, Sweden

sweden.gif
PostPosted: Wed Jan 08, 2020 10:50 pm    Post subject: Reply with quote

Thanks Tomi
I'll try to prettify my future text from now. Smile

EDC
Thanks for your CAN design contribution. Now we can make our own CAN-links if we like. Smile
However, I suppose most people would avoid the hazzle of soldering and get an Arduino shield. (just like I did)
They are really cheap and you get a lot of extras with them. Like SD-card, joystick and GPS-plug in my case.
Nevertheless, your L-line is extra so that is a bonus. I have never tried that bus though...

I hope your enthusiasm will spred. Thanks again!

/Per
Back to top
View user's profile Visit poster's website
enniom

Bascom Member



Joined: 20 Oct 2009
Posts: 467

PostPosted: Fri Jan 10, 2020 6:34 pm    Post subject: Reply with quote

For the purpose of CAN interface to OBD, I have used ELM323/327 chips with AVR.

Very easy.

E
Back to top
View user's profile
nicofer

Bascom Member



Joined: 01 May 2013
Posts: 83
Location: GRJ

southafrica.gif
PostPosted: Wed Jan 15, 2020 11:33 am    Post subject: Reply with quote

Hi

I have a project that require 2 CAN bus 'ports'

One way is to use more powerful dual can port ARM chips .

But I prefer the BASCOM route with one channel a AT90 CAN 128 - the other channel the MCP 2515 .

Is the above library available ?

Regards
Nico
Back to top
View user's profile
Display posts from previous:   
Post new topic   Reply to topic    www.mcselec.com Forum Index -> Share your working BASCOM-AVR code here All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum