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

jongjejung Tue Feb 28, 2017 2:21 am

Thank you for the reply. I did supply 5V to both MCP2515 and TJA, result was same transmitting only once at the bootup of STM32f103.
For refernce, I’ve used your simplified sending sketch and MCP2515 can module is attched to Arduino UNO on the receiving side.
Thank you again for your kind reply. :D

michael_l Tue Feb 28, 2017 8:19 am

Is there any kind of filtering supported ?

And_Ru Sun Mar 19, 2017 6:49 am

michael_l wrote:Is there any kind of filtering supported ?

And_Ru Sun Mar 19, 2017 6:53 am

@Phono, can you check the speed of sending messages?
I send two messages in series and they appear in cansniffer with 0.28 seconds delay. Is it normal for CAN speed 95 kbps?

Geekshow Wed Mar 22, 2017 12:28 am

Hi guys, I’ve read this post with mild encouragement. Nice work on the CANbus functionality so far! I had been beginning to lose hope of getting this working…. :-o

I have a load of Olimexino-STM32 boards (with onboard MCP2551 CAN transceiver) I’m planning to use for a home CAN network. I’ll happily help out with testing as required?

cheers,
Keith

ebaril72 Fri Mar 24, 2017 3:55 am

Great Work Phonog ! I’ve had a few of those in my drawer for a long time but will now start testing a bit.

I’ve been doing Lots of CANbus using the MCP2515 but always had to stick to standard Arduino Nano to avoid the dreaded 3.3V – 5V Issue.
Yes, transceivers do a great job but the higher the component count the lower is the reliability.

I’ve been using the Atmel SAMD21 (3.3V) but could not interface the MCP2515 directly. Now I can have more Mips/ More Memory

Can Tranceivers : Check into this: http://www.ebay.com/itm/SN65HVD230-CAN- … SwepJXbQj6

I’ve been using those on an arduino Due project.

Is there anything you’d like to be tested in perticular ?
My Ultimate dream : CANbus bootloader !
I was just about to lookinto it for the Arduino Nano or the SAMD21.
I did work an SDCard(SPI) bootloader for the SAMD21 and was about to look into a CANbus version …

But this 2$ PCB just tells me I should try on the STM… I’d be willing but I would like to know if someone else has interest to help out.
I could even work into the STM32Loader.py to support out work.

Imagine this, you seal your mini project in a proper casing to avoid environmental issues and you fidure out later you would like to add a new feature.
OKOK , I know you guys all do it best the first time around ! ;-)

michael_l Fri Mar 24, 2017 12:24 pm

What kind of possibilites there are to accomplish an automatic start / shutdown of the device ?

Basically I would like it to work so that when I go to car and turn ignition on then stm32 starts up. When I leave the car then stm32 would go to sleep and not drain my battery.

And_Ru Sat Mar 25, 2017 10:05 am

michael_l wrote:What kind of possibilites there are to accomplish an automatic start / shutdown of the device ?

Basically I would like it to work so that when I go to car and turn ignition on then stm32 starts up. When I leave the car then stm32 would go to sleep and not drain my battery.

michael_l Sat Mar 25, 2017 11:40 am

And_Ru wrote:michael_l wrote:What kind of possibilites there are to accomplish an automatic start / shutdown of the device ?

Basically I would like it to work so that when I go to car and turn ignition on then stm32 starts up. When I leave the car then stm32 would go to sleep and not drain my battery.

Phono Mon Apr 10, 2017 7:49 pm

@And_Ru: the delay between frames seem strange to me. I never observed it. My applications run at 125 kb/s and I can send quite a few frames at only a few milliseconds interval.

mobi123 Wed May 03, 2017 7:15 am

Phono wrote:@And_Ru: the delay between frames seem strange to me. I never observed it. My applications run at 125 kb/s and I can send quite a few frames at only a few milliseconds interval.

mobi123 Tue May 09, 2017 2:56 am

CAN_STATUS can_gpio_map(CAN_Port* CANx, CAN_GPIO_MAP map_mode)
{
rcc_clk_enable(RCC_AFIO);

status = CAN_INIT_FAILED;
if( CANx == CAN1_BASE)
{
switch(map_mode)
{
case CAN_GPIO_PB8_PB9:
rcc_clk_enable(RCC_GPIOB);
afio_remap(AFIO_MAPR_CAN_REMAP_PB8_PB9);
gpio_set_mode(GPIOB, 8, GPIO_INPUT_FLOATING);
gpio_set_mode(GPIOB, 9, GPIO_AF_OUTPUT_PP);
break;
case CAN_GPIO_PA11_PA12:
rcc_clk_enable(RCC_GPIOA);
afio_remap(AFIO_MAPR_CAN_REMAP_NONE);
gpio_set_mode(GPIOA, 11, GPIO_INPUT_FLOATING);
gpio_set_mode(GPIOA, 12, GPIO_AF_OUTPUT_PP);
break;
#if NR_GPIO_PORTS >= 4
case CAN_GPIO_PD0_PD1:
rcc_clk_enable(RCC_GPIOD);
afio_remap(AFIO_MAPR_CAN_REMAP_PD0_PD1);
gpio_set_mode(GPIOD, 0, GPIO_INPUT_FLOATING);
gpio_set_mode(GPIOD, 1, GPIO_AF_OUTPUT_PP);
break;
#endif
default:
return status;
}
status = CAN_OK;
}
return status;
}

mobi123 Tue May 09, 2017 3:01 am

Now there is a gateway to receive data to send the mailbox to the TXRQ bit after the MCU is dead.
If there is no gateway to the case can always send data, how to solve this?

And_Ru Sun May 28, 2017 7:26 am

Phono wrote:@And_Ru: the delay between frames seem strange to me. I never observed it. My applications run at 125 kb/s and I can send quite a few frames at only a few milliseconds interval.

And_Ru Sun May 28, 2017 12:10 pm

…

And_Ru Thu Jun 01, 2017 7:56 pm

Hello.
I try to use two CAN buses and switch between them via remap. But it does not work. Could you analyze?
Code is below.
First it sends out the message with ID 0x100 to the bus on A11A12 pins, but then nothing there. On the B8B9 pins then I get working cycle with messages of ID 0x111. And all further messages with ID 0x100 go also here with speed 33.
What am I doing wrong?
Not possible to remap on-the-fly?

#include <HardwareCAN.h>
//#include "changes.h"
/*
*
*/

#define T_DELAY 10
// Instanciation of CAN interface
HardwareCAN canBus(CAN1_BASE);
CanMsg msg ;

void CAN_a_33_Setup(void)
{
CAN_STATUS Stat ;
canBus.map(CAN_GPIO_PA11_PA12);
Stat = canBus.begin(CAN_SPEED_33, CAN_MODE_NORMAL);
canBus.filter(0, 0, 0);
canBus.set_irq_mode();
Stat = canBus.status();
if (Stat != CAN_OK)
{digitalWrite(PC13, LOW);
delay(10000);}
// /* Your own error processing here */ ; // Initialization failed
// delay(T_DELAY);
}

void CAN_b_95_Setup(void)
{
canBus.map(CAN_GPIO_PB8_PB9);
Stat = canBus.begin(CAN_SPEED_95, CAN_MODE_NORMAL);
canBus.filter(0, 0, 0);
canBus.set_irq_mode();
Stat = canBus.status();
if (Stat != CAN_OK)
{digitalWrite(PC13, LOW);
delay(10000);}
// /* Your own error processing here */ ; // Initialization failed
// delay(T_DELAY);
}

CAN_TX_MBX CANsend(CanMsg *pmsg)
{
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 dlength=8, 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 = dlength; // 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
digitalWrite(PC13, HIGH); // turn the LED off
delay(T_DELAY);
}

// The application program starts here
byte msgD0 = 0x00;
void setup() { // Initialize the CAN module and prepare the message structures.
pinMode(PC13, OUTPUT);
digitalWrite(PC13, HIGH);
delay(10);
digitalWrite(PC13, LOW);
delay(1000);
digitalWrite(PC13, HIGH);
delay(1000);

}

void loop() {
CAN_a_33_Setup();
delay(T_DELAY);
SendCANmessage(0x100,1,msgD0);
delay(T_DELAY);
CAN_b_95_Setup();
delay(T_DELAY);
SendCANmessage(0x111,1,msgD0);
delay(T_DELAY);
msgD0++;
}

And_Ru Wed Jun 07, 2017 9:49 pm

So, the answer is
afio_init();
Before switching back to A pins.

macbeth Wed Jun 14, 2017 11:38 am

I’m just looking at some CAN issues at the moment. I had CAN working on a “maple”-clone with the libmaple toolchain. Now I’d like to switch to STM32duino (or rather platformIO).

Could you tell me what the status of the HardwareCAN integration is? I saw phono’s great branch with HardwareCAN (thanks, man!!). Would it be a big problem to pull these changes into master? Are the modifications (rcc.c, rcc.h, …) breaking anything else?

Looking at https://github.com/rogerclarkmelbourne/ … og-patch-1 it seems that there’s not too much to do. But then… I’m not in the position to judge the side effects. I usually don’t look much under the hood.

Phono Wed Jul 19, 2017 4:12 pm

Please users of this library: note that as of today, I have committed a new version that fixes a bug that caused received frames to be occasionnally dropped or corrupted.
In my case, in my system with about 100 frames per second, lost frames occurred several times a day, and one or two corrupted frames a day.
Please update your programming environment for a more robust operation of CAN.

Phono Thu Jul 20, 2017 12:02 pm

@RogerClark: I understand that you are not experienced enough to support the HardwareCAN library, and thus, you did not include it in your last version.
However, I would like to request you to include in the next version of the core the few changes that are required to be able to include the library in a project. These changes are simple and cannot add a threat to the stability of the core.
Thanks in advance.

Sign in to reply