Spark Logic

CAN Bus Library for STM32F103

watswat5

I’m looking for a working CAN library for the stm32f103. I know it has the hardware for it, but looking at some example code (found here: https://www.olimex.com/Products/Duino/S … e-hardware), I’m totally lost.

Can someone create a CAN library similar to the MCP2515 library for Arduino? That library is intuitive and easy to use, even for users who know almost nothing about CAN.

The MCP2515 Library can be found here: https://github.com/coryjfowler/MCP_CAN_lib

Replies19

michael_l Tue Sep 04, 2018 3:35 pm

I’m planning to try to port this library on F4 with stevestrongs repo. In F4 there are no conflicts between USB and CAN.

Where can I find the latest version ? Phonog: is it here: https://github.com/Phonog/Arduino_STM32 … og-patch-1 ? Thanks.

Phono Thu Sep 13, 2018 7:56 pm

@michael_l : yes, this is the right place.

tjlynchny Mon Oct 29, 2018 10:40 pm

[Phono – Sat Mar 24, 2018 4:45 pm] –

For an extended identifier, it should be
( ( ( id & 0x7FF ) << 21 ) | ( ( id & 0x1FFFF800 ) >> 8 ) | 4 )

Phono Wed Oct 31, 2018 8:42 pm

If you insert these formulas in the code of the library, you will force the library to only recognize extended identifiers.
What I meant is that you must pass these expressions as th arguments to the call of the filter method. In your case, you might want to write
canBus.filter(0, ( ( id & 0x7FF ) << 21 ) | ( ( id & 0x1FFFF800 ) >> 8 ) | 4, 0xFFFFFFFF) ; //

And_Ru Sat Nov 03, 2018 8:40 pm

[Lukas – Sun May 27, 2018 10:15 pm] –
It seems the interrupt crashes often within Serial.println i lose connection to chip.

Hi!
Just for information, in my sketches I use
Serial1 = pins A9-A10
Serial2 = pins A2-A3
and this works good.

[michelkeijzers – Tue Jun 26, 2018 9:10 pm] –
Megadrifter (https://github.com/megadrifter/Arduino_ … GMLAN-test)

Please do not use this, I started to make some changes but now it seems to be abandoned.

If You have troubles installing the library, You can download the zip archive of installed Arduino IDE with working library inside.
https://cloud.mail.ru/public/Eqai/jAoXWVYnC (I made this snapshot for myself).

Phono Fri Nov 09, 2018 9:39 am

Hi,
I have ported the HardwareCAN library to the newest core I took from the repository.
It crashed on initialization of the CAN interface.
I found out that the code of the initialisation in HardwareCAN.cpp

CAN_STATUS HardwareCAN::begin(CAN_SPEED speed, uint32 mode)

included a
Serial.end();
On earlier versions of the core, it used to work. Now it crashes. As a quick and dirty solution I commented it out.
In this state, my sketch works, but when its execution starts, my PC complains that an unrecognized USB device has been detected. It must be because the USB serial needs to be properly closed.
Who can tell me what is wrong now? What is the correct syntax for closing the USB serial interface?

Phono Fri Nov 16, 2018 1:09 pm

I eventually found that the statement
Serial.end();
may be dangerous, if Serial is not defined at this time. I changed the code to

if ( Serial ) Serial.end() ;

and this cured the problem.
Please update your library accordingly.

metrobcn Wed Nov 28, 2018 7:19 am

Hi Phono!

Might you be so kind to give me a little hint about where i can find your ported can library so that i know where to apply you latest serial closing patch?

Your advice will be much appreciated!!

Phono Thu Nov 29, 2018 7:45 am

I actually updated the repository today to include this change. You will find it at
https://github.com/Phonog

Vicolaships Sat Feb 02, 2019 8:28 pm

Hi there!
I would like to start a project with 25 devices (STM32 Blue Pills) communicating through a CAN bus.
I need a CAN bus because I have pretty long cable distances (~ 2 meters) and SPI / I2C are very hard to get working in these conditions.

– Do I need external hardware or can I use the PB8/PB9 and PA11/PA12 pins of the Blue-Pill ? (which one should I use ?)
– What library should I use ? Raw CAN is fine (I don’t need CANopen for example)
– What is the state of this library ? Is there some missing functionality ?

I’ve read a little bit but I’m confused at what software is available and before starting anything I’d like to have more information :)
Thank you for you insights!

RogerClark Sun Feb 03, 2019 2:15 am

The LibMaple core does not have support for CAN but there is a PR for it, which you could try

I don’t know if ST’s official core has high level support for CAN, you’d need to PM @fpiSTM and ask

You can always directly access the CAN hardware, but the problem is that its shared with USB, and by default the LibMaple core has USB support enabled unless you upload via hardware Serial

scoace Sun Feb 03, 2019 11:26 am

The repository for can support is the link above your post. Works well.

Vicolaships Mon Feb 04, 2019 1:02 pm

Ok for the software side.

So I only need a CAN transceiver and the Blue Pill has the CAN controller (used through the HardwareCAN library) within the STM32?

I will do a complete tutorial (wiring + code) on how to communicate between 2 STM32 through a CAN bus when I get the MCP2551 chips and I get everything working.

RogerL Mon Feb 04, 2019 3:53 pm

[Vicolaships – Mon Feb 04, 2019 1:02 pm] –
I will do a complete tutorial (wiring + code) on how to communicate between 2 STM32 through a CAN bus when I get the MCP2551 chips and I get everything working.

I am planning to do a CAN bus project later in the year, so am looking forward to your tutorial. I have ordered some MCP2551 chips.

Vicolaships Thu Feb 07, 2019 9:48 pm

[stevestrong – Thu Feb 07, 2019 10:02 am] –
I just received my 3 modules with MCP2515 +TJA1050, ordered from here: https://www.aliexpress.com/item/MCP2515 … 72090.html
Now I have to find a project for them 8-)

Cool!

Why did you order such a module (CAN controller and CAN transceiver) if the Blue-Pill (and other STM32s) already have the CAN controller?
Why not order a TJA1050 or MCP2551 directly?

stevestrong Fri Feb 08, 2019 8:51 am

Simply because I use the USB serial, so CAN is then unavailable.
And the price for a transceiver alone is not much lower than a complete module, so why to bother with native CAN software?
Nevertheless, I still appreciate the effort to bring CAN to work on bp, just that I find much simpler to use a lib for MCP2515.

Sign in to reply