Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

CAN-bus library for general CAN-bus controller chip MCP2515
Goto page 1, 2  Next
 
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: 235
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: 5916
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: 235
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: 5916
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: 971

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: 235
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: 537

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: 90
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
andro

Bascom Member



Joined: 12 Mar 2007
Posts: 94
Location: Ljubljana

slovenia.gif
PostPosted: Thu Feb 06, 2020 12:12 pm    Post subject: Reply with quote

Hi,

That's realy great job!
Question: does this library support Xmega familly?
Br
Andrej
Back to top
View user's profile
Per Svensson

Bascom Member



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

sweden.gif
PostPosted: Thu Feb 06, 2020 2:01 pm    Post subject: Reply with quote

It is not yet tested on Xmega.
However, I have not used any specific Xmega features,so I see no reason why it should not work.
It you test it on Xmega, please report the result here.
/P
Back to top
View user's profile Visit poster's website
andro

Bascom Member



Joined: 12 Mar 2007
Posts: 94
Location: Ljubljana

slovenia.gif
PostPosted: Thu Feb 06, 2020 7:41 pm    Post subject: Reply with quote

Hi
The major difference is in "SPI".
Therefore I would have to change the part off the library where SPI is actually in use.
Is it ok, if I try to change your lib?
Br
Andrej
Back to top
View user's profile
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 5916
Location: Holland

blank.gif
PostPosted: Thu Feb 06, 2020 7:54 pm    Post subject: Reply with quote

you can use _XMEGA constant :

#IF _XMEGA
config spic
#ELSE
config spi
#ENDIF

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

Bascom Member



Joined: 12 Mar 2007
Posts: 94
Location: Ljubljana

slovenia.gif
PostPosted: Thu Feb 06, 2020 9:30 pm    Post subject: Reply with quote

Hello!

Yes, Mark, I'll do as You suggested. Now I also see it is an .inc file, not a .lib, so the workaround is (for me) much simpler.
As far as I have 'investigated' in last few minutes, I (only) have to change these part of code:

Code:
'**************************************************************************
'********************** SUBS AND FUNCTIONS ********************************
'**************************************************************************

Sub Mcp2515_bitmodify(byval Reg_addr As Byte , Byval Reg_mask As Byte , Byval Reg_val As Byte)
'Write to certain bits in a MCP2515 register via SPI
'This command ensures that certain bits in a complete register can be changed without affecting the other bits.
'Unlike the "Write" command, this does not change the entire contents of the register. Only the masked bits.

   Local Cmd As Byte

   Reset Mcp2515_cs
   Cmd = Cmd_bit_modify
   Spiout Cmd , 1
   Spiout Reg_addr , 1
   Spiout Reg_mask , 1
   Spiout Reg_val , 1
   Set Mcp2515_cs
End Sub
'**************************************************************************

Function Mcp2515_read(byval Reg_addr As Byte ) As Byte
'Read from a MCP2515 register via SPI
   Local Cmd As Byte
   Reset Mcp2515_cs
   Cmd = Cmd_read
   Spiout Cmd , 1
   Spiout Reg_addr , 1
   Spiin Mcp2515_read , 1
   Set Mcp2515_cs
End Function
'**************************************************************************

Sub Mcp2515_write(byval Reg_addr As Byte , Byval Reg_val As Byte)
'Write to a MCP2515 register via SPI
'Some registers can only be written in CONFIGURATION MODE
'These are:
'  • CNF1, CNF2, CNF3 registers
'  • Txrtsctrl Register
'  • Filter Registers
'  • Mask Registers

   Local Cmd As Byte

   Reset Mcp2515_cs
   Cmd = Cmd_write
   Spiout Cmd , 1
   Spiout Reg_addr , 1
   Spiout Reg_val , 1
   Set Mcp2515_cs
End Sub
'**************************************************************************

Sub Mcp2515_write_array(byval Startaddr As Byte , Data() As Byte , Byval N As Byte)
'Write to N consecutive register addresses
   Local I As Byte

   Reset Mcp2515_cs
   I = Cmd_write
   Spiout I , 1
   Spiout Startaddr , 1
   Spiout Data(i) , N
   Set Mcp2515_cs
End Sub
'**************************************************************************


I have done this before, so I think in a few days I might post some results.
BR
Andrej.
Back to top
View user's profile
Per Svensson

Bascom Member



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

sweden.gif
PostPosted: Thu Feb 06, 2020 9:59 pm    Post subject: Reply with quote

Hi Andro!

Yes of course you are welcome to modify and contribute to the code.
Especially if you, like mark suggests, does so in a generic way by using compiler directives and keywords.
If modifications are done to make the code more adaptive to a particular use or project, then I suggest that
you make a drop off branching, but if you make it more versatile but still generic, I suggest that you (or anyone)
updates the Revision code.

Good spirit! More of same! Smile

/Per
Back to top
View user's profile Visit poster's website
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
Goto page 1, 2  Next
Page 1 of 2

 
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