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

RogerClark Wed Oct 18, 2017 8:56 pm

Thanks @bobc

That seems a sensible approach.

BTW.

The change to merge USB HID into the master seems just as contentious, so I would advise that the dust left to settle on that, before CAN could be merged.
However, as USB HID PR seems to currently be in the process of updating and bug fixing by its author, I think CAN may have to wait quite a while

My preference is to integrate HID before CAN as the number of people who want HID is higher than the number of people who want CAN, and the number of people with CAN hardware is a small percentage of members, where as most people use USB ( even if only for Serial CDCACM)

And_Ru Fri Oct 20, 2017 8:33 am

[bobc – Wed Oct 18, 2017 10:07 am] –
I think your PR has pulled in several other changes that are unnecessary or incorrect. I would start again with a clean branch of the current Arduino_STM32 repo, and apply only the changes needed for CAN.

Yes, I will do so. Just let me know wnen to start.

[bobc – Wed Oct 18, 2017 10:07 am] –
I think practically speaking, USB and CAN can not be used in the same application on F103.

Maybe I understand you wrong, but did you see this example: http://stm32duino.com/viewtopic.php?f=1 … =90#p33946 ?
I’m able either to start my usual CAN script, or start USB as UART Serial port. Suggest how should I test using USB and CAN in one application?

stevestrong Fri Oct 20, 2017 9:32 am

[And_Ru – Fri Oct 20, 2017 8:33 am] –
I’m able either to start my usual CAN script, or start USB as UART Serial port.

That is a nice feature. But…
Does anybody need it? I doubt. I think one uses one or the other, but I cannot imagine any application in which USB and CAN should be alternatively used.
So in order to keep the original version used by many users safe and intact, I can only suggest to use a separate branch for CAN.

jiauka Tue Jan 30, 2018 8:52 pm

Hi:
Fisrt, thanks you all guys that have done such a great work.

If anyone is still interested in this topic, I have done a library that uses the internal CAN controller.

https://github.com/jiauka/NMEA2000_stm32f1

It is working with a “Bluepil” stmF103c8 and a SN65HVD230 can driver, but it should work with any stm32Fxx MCU with maybe some minor change at the clock setting, I have to test wiht an STMF4xxx that I have laying around

NMEA2000_CAN.h need some tweaking to add the library

> #elif defined(STM32F103xB)||defined(STM32F103xB)
> #define USE_N2K_CAN USE_N2K_STM32F1xx_CAN
106a109,120
> #elif USE_N2K_CAN == USE_N2K_STM32F1xx_CAN
> // Use MBED devices
> #include <NMEA2000_stm32f1.h>
> tNMEA2000 &NMEA2000=*(new NMEA2000_stm32f1());
>
> #elif USE_N2K_CAN == USE_N2K_STM32F103_CAN
> // Use MBED devices
> #include <NMEA2000_mbed.h> // https://github.com/thomasonw/NMEA2000_mbed
> tNMEA2000 &NMEA2000=*(new tNMEA2000_mbed());
> tmbedStream serStream;
>
>
114,116c128,130
< #if defined(__STM32F1__) // Maple
< #include <MapleIntCompatibility.h>
< #endif
—
> #if defined(STM32F103xB) // Maple
> #include <NMEA2000_stm32f1.h>
> tNMEA2000 &NMEA2000=*(new NMEA2000_stm32f1());
117a132
> #endif

yours,

j.

michael_l Wed Jan 31, 2018 6:14 am

I did a stm32cube project with USB and CAN enabled for stm32f103c8 and it does not show any conflicts between USB and CAN. I guess I am missing something ?

Image
Image
Image

RogerClark Wed Jan 31, 2018 10:26 pm

I think the problem is shared interrupt or shared memory or both for the CAN and the USB sections inside the STM32F103
(I’m sure its been discussed in detail before)

coddingtonbear Tue Feb 06, 2018 3:45 am

This may not be useful to many of you, but I’ve posted a fork of the Arduino_STM32 repository here including a minimal set of changes from the above mishmash of branches and patches, but with added (working) support for polling mode. You can find it in my HardwareCAN branch here:
https://github.com/rogerclarkmelbourne/ … ardwareCAN.

I do, though, even when using the original unmodified patches, have interrupt-related problems that I’m not sure how to resolve — I’m embarrassed to say that the conversation from this point is going to reveal some shortcomings in my understanding of microcontrollers.

Specifically — when using IRQ mode (or, really, having any of the CAN interrupts — CANx->IER = (CAN_IER_FMPIE0 | CAN_IER_FMPIE1 | CAN_IER_TMEIE) — enabled), the moment any of those interrupts fire, the microcontroller appears to hang. That’s what inspired me to flesh out the polling mode described above. This doesn’t happen while debugging using an stlink, so I’m a little bit at a loss as to what I’m doing wrong and wonder if any of you folks have any input.

Given that I’m guessing it’s a problem in an ISR, I am very curious about is how the miscellaneous CAN_IER_FMPIE0/CAN_IER_TMEIE/etc derived interrupts are mapped to an actual ISR. I’ve had a look at page 673 in the manual (http://www.st.com/content/ccc/resource/ … 171190.pdf) and can see how those are mapped to a handful of interrupts in a general sense, but I’m not at all clear on what that might mean in Maple/Stm32duino as far as what those interrupts are called. I have, though, noticed the three interrupt functions at the bottom of can.c in the original supplied patches from Phonog —

uint8 __attribute__ ((interrupt)) CAN_RX0_IRQ_Handler(void)
{
if (can_active)
{
can_rx_copy_from_fifos();
}
return can_active; // return CAN active flag to USB handler
}

// Addition JMD: the messages stored in fifo1 must also trigger an interrupt.
void __irq_can_rx1(void)
{
CAN_RX0_IRQ_Handler() ;
}

void __attribute__ ((interrupt)) USB_HP_CAN_TX_IRQHandler(void)
{
if (can_active)
{
if (CAN1_BASE->TSR & CAN_TSR_RQCP0)
CAN1_BASE->TSR |= CAN_TSR_RQCP0; // reset request complete mbx 0
if (CAN1_BASE->TSR & CAN_TSR_RQCP1)
CAN1_BASE->TSR |= CAN_TSR_RQCP1; // reset request complete mbx 1
if (CAN1_BASE->TSR & CAN_TSR_RQCP2)
CAN1_BASE->TSR |= CAN_TSR_RQCP2; // reset request complete mbx 2
}
}

Nobby42 Sun Mar 11, 2018 7:17 pm

Hi,

I use the CAN Bus Library, Works fine. But I have problems with the extended ID an filtering.
The filter works only with the standard ID. How can I use it for extended IDs?
That’s my code:
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <HardwareCAN.h>
#define BPIN 0
#define SPIN 1

byte msgD0 ;
uint32_t lastSent = 0;
int count = 1;
int ledState = 0;
int extID = 1;

float cTemp;
float pressure;

union t_tag {byte b[4]; float tempVal;} t;
union p_tag {byte b[4]; float pressVal;} p;

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

// Set the LCD address to 0x3E for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x3E, 20, 4);

void CANSetup(void)
{
CAN_STATUS Stat ;

// Initialize CAN module
canBus.map(CAN_GPIO_PB8_PB9);
Stat = canBus.begin(CAN_SPEED_1000, CAN_MODE_NORMAL); // set speed to 1000 kbps
CAN_ID_EXT;

lcd.setCursor(0, 1);
lcd.print("Status: ");
lcd.println(Stat);
canBus.filter(0, 0x00ffee00, 0x1fffffff); // filter don't work for ID 0x00FFEE00
canBus.filter(1, 0x3f, 0x1fffffff); // filter work for ID 0x00FFEE00
canBus.set_irq_mode(); // Use irq mode (recommended), so the handling of incoming messages
Stat = canBus.status();

if (Stat != CAN_OK) {
lcd.println("CAN initialization Failed");
/* Your own error processing here */ ; // Initialization failed
}
}

vegfokic Sun Mar 11, 2018 8:03 pm

Hello
Can I found anywhere CAN BUS library and example for STM32F407?

vegfokic Thu Mar 15, 2018 11:16 am

Hi

I found a COIDE code for stm32discovery and it is works after changing. The board is changed to STM32F407VET6. The marked BLUE and RED leds are changed to PA6 and PA7 led. CAN pins are on PB8 and PB9, speed 100kb/s. I manage to upload onto the board. After the two leds are light. When I connect to 100kb/s CAN networks which push signal to the STM board, the two leds begin to blinking :) I use sn65hvd230 CAN interface.

How can it implement into the generic or dunio repo?

Phono Sat Mar 24, 2018 4:45 pm

Hi all,
The CAN library lacks proper information for use.
Regarding the filter mechanism, one must understand how the value passed as an argument for the identifier and the mask are used by the library.
It is because the identifier and mask registers should not be written directly with the value of the identifier. Instead, the identifier must be processed to match the bit allocation described in the figure below (refer to STM32 user’s manual RM0008 page 640, figure 229).

Method Argument [31:24] : [23:16] : [15:8] : [7:0] Mapping STID[10:3] : STID[2:0] EXID[17:13] : EXID[12:5] : EXID[4:0] IDE RTR 0

vegfokic Mon Mar 26, 2018 7:35 pm

Phono
Its too difficult for Me. I have a library for arduino stm32F103, it is work but I can not use it for STM32F407.
The linked “demo 59 CAN” example works in COIDE for STM32F407 (not use hal library).
Now Im trying to setup CAN module in DOIDE using hal library. The CUBEMX library has some info but not fully about CAN.

Lukas Sun May 27, 2018 10:15 pm

Hi,

I did some can stuff with the UNO and car diagnostic hardware.
I bought on ebay some STM32F103C8T6 boards to use them as fast can communicator.

I connected a PCA82C250 transceiver to PB 8 and 9.
A USB TTL is connected to A9 A10.

The hardware is connected to some car modules on desk i know about that they are sending constantly.
Confirmed also with uno…

I just want to print can data out over the usb ttl.

Speed 125 Extended ID

I tested some librarys here for the board but lost the overview—
phonog compiles but crashes when i connect data lines to can bus
megadrifter has some errors in his filter the functions are declared different that i wasn’t able to reconstruct the filter function.

I found a third library in the last pages of the matching together discusion, it complies but also crashes…

It seems the interrupt crashes often within Serial.println i lose connection to chip.

Strange is also.
Serial1 is used in much librarys.
I only get output on Serial.

On the 3rd serial port i’m not able to program the stm32 right?

Can somebody offer a complete library he is sure about that it works on extended ids with usb ttl?

Phono Tue Jun 05, 2018 3:05 pm

@lukas:
the CAN interface of the STM32F103 has a shared interrupt vector with the USB interface.
This is taken care of in the Arduino for STM32 kernel (at least in the version I have committed) and handled via weak identifier declarations in the library.
Please check whether the CAN interrupt vector is connected to the CAN interrupt service routine of the library.

Lukas Sat Jun 09, 2018 11:27 pm

Thanks for the reply,
i downloaded this library: https://github.com/Phonog/Arduino_STM32 … og-patch-1

and pasted all files over the arduino 1.85 installation i have done with download from ide.

So i should have all necessary files but the board crashes after connecting to can, maybe i have done something wrong with connecting the transceiver or the transceiver is broken but guess not

Further i did the same with this library https://github.com/megadrifter/Arduino_ … GMLAN-test
but here the filter setting in hardwarecan.cpp is wrong it should return with the output of some other filter function from can.c but the function is defined wrong, after uncommenting i nearly get the same result.

With some normal uno i’m able to receive data from the 125 extended id can bus so there is something on bus…

Phono Sun Jun 24, 2018 3:27 pm

@Lukas: I have no further clue so far, but something looks strange. I am not familiar with OBD, but I read on wikipedia that the bit rate should be either 250 kb/s or 500 kb/s. You mention using 125 kb/s. Are you sure?

michelkeijzers Tue Jun 26, 2018 9:10 pm

I downloaded both the library from Phono (https://github.com/Phonog/Arduino_STM32) and Megadrifter (https://github.com/megadrifter/Arduino_ … GMLAN-test), but when I try to install it (with Arduino IDE, Sketch, Include Library, Add .ZIP library) and selecting the library, I get the error:

exit status 1
Error compiling for board Generic STM32F103C series
Specified folder/zip file does not contain a valid library.

What should I do to install the library (on an Windows system) ?

Pito Tue Jun 26, 2018 9:18 pm

Those links are not libraries, but entire stm32duino cores..

Sign in to reply