Spark Logic

CAN Bus Library for STM32F103

watswat5 Mon May 04, 2015 9:19 pm

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

Replies

And_Ru Fri Nov 18, 2016 5:46 am

Phono wrote:to And_Ru : I am sorry, but the library is dedicated to the integral CAN interface of the STM32F103. Maybe one day it will be expanded to other interfaces.

Phono Fri Nov 18, 2016 7:02 pm

If you mean whether it is possible to connect only the CANL wire without connecting the CANH, it is not up to the library. The ISO 11898-compliant CAN transceivers requires that both wires be connected, and in addition, that the line be terminated with a 120 ohm resistor at both ends.
If you need only one wire, you may want to look at the NCV7356 for example.

And_Ru Mon Nov 28, 2016 5:10 pm

Phono wrote:If you mean whether it is possible to connect only the CANL wire without connecting the CANH, it is not up to the library. The ISO 11898-compliant CAN transceivers requires that both wires be connected, and in addition, that the line be terminated with a 120 ohm resistor at both ends.
If you need only one wire, you may want to look at the NCV7356 for example.

SyRus Sat Dec 10, 2016 2:36 pm

Hi guys…

I don’t know what’s the big deal here??? The MCP2515 shield for Arduino works perfectly with Iteadmaple 1.0 Arduino compatible board. The library compiles the board inits and then all runs smooth and easy like Arduino accept it’s much faster. All you need to do is a little bit rewire it. Not all of the CAN_BUS shield pins goes to the board. But if you really wants then you defanatelly figure it out. Program board through the Arduino IDE…

Also it works with Teensy 3.1 3.2.. Without any problem..

Phono Tue Dec 13, 2016 7:32 pm

Maybe you do not know, but the Olimexino STM32 board includes all the CAN hardware. You just have to connect your can bus to a terminal strip and you’re done. No need for an extra shield.

SyRus Thu Dec 15, 2016 4:36 am

I know if your answer was to me Phono . But then you have to write your own functions to maintain CAN ah? From this thread it seems people have wish to run that exactly shield by STM32 because STM much faster and second that the shield has very very convenient library with just a few very very convenient functions. Nothing more just what you need.
Though seems no body here actually tryd to hook up the PCM2515 CAN shield with STM. I did.. that’s why Im here to tall you about.

And_Ru Thu Dec 15, 2016 7:12 am

@SyRus, you’re right, MCP2515 works perfectly (correction – I mean not the shield, but the MCP2515 chip itself :) ).
My first mistake was to connect CAN bus directly to the board (CAN TX, RX).
Second fail was because TJA1050 that I tried was defective (infinite resistance between CAN Hi – CAN Lo).
Third – both TJA1050 and MCP2515 must be supplied with 5V to be able to send messages.

I’ve simplified the code by Phono to just send a message for test reasons:
// HardwareCAN_simplified_send1.ino
#include <HardwareCAN.h>
//#include "changes.h"
/*
* Example of use of the HardwareCAN library
* Please read the file changes.h to see the changes to be performed to the core in order to use this
*/

byte msgD0 ; // variable to be used in the example.

// Instanciation of CAN interface
HardwareCAN canBus(CAN1_BASE);
CanMsg msg ;

void CANSetup(void)
{
CAN_STATUS Stat ;

// Initialize CAN module
canBus.map(CAN_GPIO_PB8_PB9); // This setting is already wired in the Olimexino-STM32 board
Stat = canBus.begin(CAN_SPEED_125, CAN_MODE_NORMAL); // Other speeds go from 125 kbps to 1000 kbps. CAN allows even more choices.

canBus.filter(0, 0, 0);
canBus.set_irq_mode(); // Use irq mode (recommended), so the handling of incoming messages
// will be performed at ease in a task or in the loop. The software fifo is 16 cells long,
// allowing at least 15 ms before processing the fifo is needed at 125 kbps
Stat = canBus.status();
if (Stat != CAN_OK)
/* Your own error processing here */ ; // Initialization failed
}

// Send one frame. Parameter is a pointer to a frame structure (above), that has previously been updated with data.
// If no mailbox is available, wait until one becomes empty. There are 3 mailboxes.
CAN_TX_MBX CANsend(CanMsg *pmsg) // Should be moved to the library?!
{
CAN_TX_MBX mbx;

do
{
mbx = canBus.send(pmsg) ;
#ifdef USE_MULTITASK
vTaskDelay( 1 ) ; // Infinite loops are not multitasking-friendly
#endif
}
while(mbx == CAN_TX_NO_MBX) ; // Waiting outbound frames will eventually be sent, unless there is a CAN bus failure.
return mbx ;
}

// Send message
// Prepare and send a frame containing some value
void SendCANmessage(long id=0x001, byte d0=0x00, byte d1=0x00, byte d2=0x00, byte d3=0x00, byte d4=0x00, byte d5=0x00, byte d6=0x00, byte d7=0x00)
{
// Initialize the message structure
// A CAN structure includes the following fields:
msg.IDE = CAN_ID_STD; // Indicates a standard identifier ; CAN_ID_EXT would mean this frame uses an extended identifier
msg.RTR = CAN_RTR_DATA; // Indicated this is a data frame, as opposed to a remote frame (would then be CAN_RTR_REMOTE)
msg.ID = id ; // Identifier of the frame : 0-2047 (0-0x3ff) for standard idenfiers; 0-0x1fffffff for extended identifiers
msg.DLC = 8; // Number of data bytes to follow

// Prepare frame : send something
msg.Data[0] = d0 ;
msg.Data[1] = d1 ;
msg.Data[2] = d2 ;
msg.Data[3] = d3 ;
msg.Data[4] = d4 ;
msg.Data[5] = d5 ;
msg.Data[6] = d6 ;
msg.Data[7] = d7 ;

digitalWrite(PC13, LOW); // turn the onboard LED on
CANsend(&msg) ; // Send this frame
delay(180);
digitalWrite(PC13, HIGH); // turn the LED off
delay(100);
}

// The application program starts here
void setup() {
// put your setup code here, to run once:
CANSetup() ; // Initialize the CAN module and prepare the message structures.
pinMode(PC13, OUTPUT);
msgD0 = 0x01;
}

void loop() {
delay(1000);
long msgID = 0x101 ;
SendCANmessage(msgID, msgD0) ;
msgD0++;
}

Phono Thu Dec 15, 2016 6:07 pm

@And_Ru:
to add new speeds, you have to change the code at two places as shown below.
I am unsure of the numeric values to achieve these speeds. At least, it should work with a good physical bus line, but fine tuning the values SJW, TS1 and TS2 may further improve the robustness of the communication.
i have never heard of these speeds. To satisfy my curisosity, where are they used?

1) in the file can.h, change the enum definition:
enum CAN_SPEED {
CAN_SPEED_125,
CAN_SPEED_250,
CAN_SPEED_500,
CAN_SPEED_1000,
};

And_Ru Fri Dec 16, 2016 5:51 am

Many thanks, Phono!
Another place found in HardwareCAN.cpp (otherwise Arduino compiler don’t pass it):
/**
* @brief Initialize a CAN peripheral
* @param freq frequency to run at, must one of the following values:
* - CAN_SPEED_1000
* - CAN_SPEED_500
* - CAN_SPEED_250
* - CAN_SPEED_125
* - CAN_SPEED_95
* - CAN_SPEED_33
*/!

zmemw16 Fri Dec 16, 2016 9:14 pm

ok i’ll bite. where’s the car?
stephen

Phono Sun Dec 18, 2016 1:59 pm

@RogerClark: Roger, could you eventually manage with my PR? Will you merge it with the current repository?
I also have ready a thread-safe version of the Ethernet library. Anyone interested?

RogerClark Sun Dec 18, 2016 9:41 pm

I don’t have any CAN bus hardware to test this, nor the time to test it even if I did.

So I’ve pulled your changes to a separate branch

https://github.com/rogerclarkmelbourne/ … og-patch-1

So if anyone wants to use them, they can use that branch

michael_l Mon Dec 19, 2016 7:39 am

Great job! I guess I’ll have to try this too. Will order some TJA1050 and try this also. Do PB8 (TX)_PB9 (RX) work also for the blue board ?

Phono: I’d be interested to try your library – if you can create a new branch for that or ?

Phono Mon Dec 19, 2016 7:27 pm

@michael_l
the schematics of the blue pill indicates CAN_RX and CAN_TX for PB8 and PB9, so I do not see why it would not work.
Yes, I will issue a PR for the ethernet library.

And_Ru Tue Dec 27, 2016 6:44 pm

I confirm that library works.

Here is the video [youtube]https://www.youtube.com/watch?v=GJgBOnZgmis[/youtube]
Here is thecode:
It sends OK button on my button press and turns on the LED when see “SETTINGS” button is pressed.
I had to add more CAN speed settings into the library, so I’ve cloned it.

Also there’s simple example how to send.

Edison517 Tue Jan 10, 2017 2:27 am

Hi folks,
I hope I can join in here. I just purchased an STM32F103 Nucelo dev board that I’m going to learn how to use. I come from a few years of Arduino background, tied with CANBUS and I’m hoping to switch over to STM32 since (a few of them) have CAN built in.

When I get my dev board I’ll start playing around with it and probably get the CAN library installed and see if I can get it to talk with my 2000 Saab 9-3 bus. I currently have an ATMEGA328P-AU talking via a MCP2515 and a SN65HVD231, so I think I can get things to work with only the 231 chip.

if I figure anything out I can post for others :)
Seth

jongjejung Fri Feb 03, 2017 2:51 am

@And_Ru : Thank you for your works on this Library. When I tried your simplified sending program to Arduino UNO-MCP 2515,
message sent was good only for firs time then message sending freezes. Have experienced same thing?

bilbolodz Fri Feb 17, 2017 1:08 pm

OK, I’ve managed to merge Phonog-Phonog-patch-1 with current sources of Arduino_STM32. In a next few weeks I will try to rewrote my old Arduino MCP2515 project to Arduino_STM32 using OLIMEXINO-STM32 and Olimex SHIELD-LCD16x2. I hope it will work.

And_Ru Fri Feb 17, 2017 6:55 pm

jongjejung wrote:@And_Ru : Thank you for your works on this Library. When I tried your simplified sending program to Arduino UNO-MCP 2515,
message sent was good only for firs time then message sending freezes. Have experienced same thing?

Sign in to reply