• Stars
    star
    203
  • Rank 192,890 (Top 4 %)
  • Language
    C++
  • Created over 4 years ago
  • Updated 8 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Can Example for Arduino Core STM32

Arduino-STM32-CAN

Basic Extended CAN(BxCan) communication example for Arduino Core STM32.

Hardware requirements

  • STM32F072 Development Board.
  • STM32F103 Development Board.
  • STM32F303 Development Board.
  • STM32F405 Development Board.
  • STM32F407 Development Board.
  • STM32F446 Development Board.
  • CAN Transceiver.
    MCP2551/2561/2562(5V)
    TJA1040/1050/1055(5V)
    SN65HVD230/231/232(3.3V)

NOTE
3V CAN Transceivers are fully interoperable with 5V CAN Transceivers.
Check here.

Software requirements

Arduino core support for STM32 based boards.
https://github.com/stm32duino/Arduino_Core_STM32


There is some core library for STM32.
It doesn't work with Arduino STM32.

Wirering

  • USB power supply (board with 5V pin)
    STM32 CAN RX GPIO has 5V tolerant.
    STM32F103-CAN-5V

  • Board without 5V pin, such as BlackPill, or ST-Link power supply
    STM32F103-CAN-3V3

Transmitter

  • from STM32F072
    STM32F072_Send

  • from STM32F103
    STM32F103_Send

  • from STM32F303
    STM32F303_Send

  • from STM32F407
    STM32F405_Send

  • from STM32F446
    stm32f446_transfer

Receiver

STM32-Receive

Communication with Arduino-UNO

You can use this library.

Communication with Arduino-DUE

You can use this library. STM32F103-CAN-DUO

Communication with ESP8266

You can use this library. STM32F103-CAN-ESP8266

Communication with ESP32

ESP-IDE has a CAN Network example.
https://github.com/espressif/esp-idf/tree/master/examples/peripherals/twai/twai_network
You can use it.
STM32F103-CAN-ESP32

Communication with Raspberry Pi

Edit /boot/config.txt and reboot.

dtparam=spi=on
dtoverlay=mcp2515-can0,oscillator=16000000,interrupt=25
dtoverlay=spi-bcm2835-overlay

Make sure the device is ready after reboot.

$ dmesg | grep mcp251x
[   19.992025] mcp251x spi0.0 can0: MCP2515 successfully initialized.

$ ls /sys/bus/spi/devices/spi0.0
driver  modalias  net  of_node  power  statistics  subsystem  uevent

$ ls /sys/bus/spi/devices/spi0.0/net
can0

$ sudo /sbin/ip link set can0 up type can bitrate 1000000

$ dmesg | grep can0
[   19.992025] mcp251x spi0.0 can0: MCP2515 successfully initialized.
[ 1309.525795] IPv6: ADDRCONF(NETDEV_CHANGE): can0: link becomes ready

$ sudo ifconfig can0
can0: flags=193<UP,RUNNING,NOARP>  mtu 16
        unspec 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00  txqueuelen 10  (UNSPEC)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

$ sudo ifconfig can0 txqueuelen 1000

Install can-utils.

$ sudo apt-get install can-utils

# Receiver
$ candump can0

# Transmitter
$ cansend can0 123#11223344AABBCCDD

STM32F103-CAN-RPI

Troubleshooting

There is a module of SN65HVD230 like this.
SN65HVD230-1

There is a 120 ohms terminating resistor on the left side.
SN65HVD230-22

A transmission fail will occur.
SendFail

I have removed the terminating resistor.
And I used a external resistance of 150 ohms.
A transmission fail is fixed.
SN65HVD230-33

If the transmission fails, these are the possible causes.

  • There is no receiving app on CanBus.
  • The speed does not match the receiver.
  • There is no terminating resistor on the CanBus.
  • There are three terminating resistors on the CanBus.
  • The resistance value of the terminating resistor is incorrect.
  • 3.3V transceiver and 5V transceiver are mixed.
    +1V Ground Shift using Split Termination.
    Check here.
  • Stub length in CAN bus is too long.
    Though the ISO11898 Standard specifies a maximum bus length of 40m and maximum stub length of 0.3m at 1Mbps. See here.
  • Noise effects.
    It occurs when the wire cable is thin or wire cable is crossing.
    It is preferable to use wires that are noise-proof.

I changed the single wire to twisted wire. It's fixed.
CAN-SendFail

Using PlatformIO

Arduino-IDE only supports ST-LINK V2.1 adapters, but OpenOCD used by PlatformIO supports both V2.0 and V2.1.
With ST-LINK, there is no need to change boot mode when writing firmware.
PlatformIO allows you to use cheap Chinese ST-LINK adapters like this one.
You can get it at a low price (about $2).
ST-LINK-1 ST-LINK-2

Connect the ST-LINK adapter and the STM32 development board as follows.

ST-LINK STM32
3.3V 3.3V
GND GND
SWDIO SWIO(=PA13)
SWCLK SWCLK(=PA14)

platformio-2

Advanced use of BxCan

Skip if you don't need advanced usage.
Advanced usage has different purposes depending on the requirements of the application.
So I can't give you a concrete code example.

About transmission

STM32 has three transmit(TX) mailboxes.
Three TX mailboxes are provided to the software for setting up messages.
This sample code uses only the first TX mailbox and blocks the application until the send is successfully completed.
There are two ways to know if the send was successful without blocking.
Read the reference manual carefully and modify the source code as needed.

  • Uses a transmission completion interrupt
    You need to set the CAN interrupt enable register (CAN_IER) appropriately.

  • Check TX mailbox space before sending
    An application can store transmission requests in three TX mailboxes simultaneously.
    When there are requests for multiple TX mailboxes, the transmission scheduler decides which mailbox has to be transmitted first.
    You can specify which mailbox has priority using the CAN Master Control Register (CAN_MCR).
    You can tell if a mailbox is free using the CAN Transmit Status Register (CAN_TSR).
    When all three TX mailboxes are pending, no new messages can be sent until the transmit is canceled or completed.
    When all three TX mailboxes are pending, you can choose to cancel the pending transmission, wait until the transmission is complete, or give up on the new transmission.
    This choice depends on your application requirements.
    When all three TX mailboxes are pending for a long time, it could be a transmission error.
    CAN Error Status Register (CAN_ESR) should be checked.

About reception

STM32 has two receive(RX) mailboxes.
Each RX Mailbox allows access to a 3-level depth FIFO, the access being offered only to the oldest received message in the FIFO.
This sample code uses only the first RX mailbox.
The RX mailbox is closely related to the receive filter settings.
By properly setting the receive filter, you can sort the received messages into two RX Mailboxes.
For example, high priority messages can be stored in the first RX MailBox and low priority messages can be stored in the second RX MailBox.
Read the reference manual carefully and modify the source code as needed.

Reference

https://github.com/nopnop2002/Robotell-USB-CAN-Python

https://github.com/nopnop2002/esp-idf-CANBus-Monitor

More Repositories

1

esp-idf-st7789

ST7789 Driver for esp-idf
C
207
star
2

esp-idf-ssd1306

SSD1306/SH1106 Driver for esp-idf
C
187
star
3

esp-idf-ili9340

SPI TFT and XPT2046 touch screen controller driver for esp-idf
C
128
star
4

esp-idf-CANBus-Monitor

Monitor Canbus traffic
C
114
star
5

esp-idf-mqtt-broker

MQTT Broker for esp-idf
C
113
star
6

esp-idf-parallel-tft

8bit parallel TFT & 4-line resistance touch screen Driver for esp-idf using i2s paralell mode
C
100
star
7

esp-idf-json

Example of JSON Serialize and Deserialize in ESP-IDF
C
96
star
8

esp-idf-sx126x

SX1262/SX1268/LLCC68 Low Power Long Range Transceiver driver for esp-idf
C
83
star
9

esp-idf-sx127x

SX1276/77/78/79 Low Power Long Range Transceiver driver for esp-idf
C
73
star
10

STM32_TFT_8bit

STM32F103 8bit parallel TFT Library for Arduino_STM32
C++
53
star
11

esp-idf-mirf

nRF24L01 Driver for esp-idf
C
51
star
12

Raspberry-ili9325

Parallel TFT Shield Library for wiringPi
C
49
star
13

esp-idf-mpu6050-dmp

A demo showing the pose of the mpu6050 in 3D using esp-idf
C++
43
star
14

esp-idf-cc1101

CC1101 Low-Power Sub-1 GHz RF Transceiver driver for esp-idf
C
42
star
15

esp-idf-video-streaming

Capture video from a USB camera using ESP-IDF
C
42
star
16

esp8266_ethernet

Example of using SPI Ethernet module with esp8266
C++
42
star
17

esp-idf-net-logging

Redirect esp-idf logging to the network
C
41
star
18

Raspberry-ssd1306

ssd1306 Command Line Tool for Raspberry Pi
C
40
star
19

esp-idf-can2mqtt

CANbus to mqtt bridge using esp32
C
40
star
20

Raspberry-W25Q64

SPI Flash Memory W25Q64 Access Library for RaspberryPi
C
39
star
21

esp-idf-ftpClient

ftp client for esp-idf
C
37
star
22

esp-idf-w25q64

SPI Flash Memory W25Q64 Access Library for esp-idf
C
36
star
23

esp-idf-can2http

CANbus to http bridge using esp32
C
36
star
24

Arduino-ESPAT-TCP

TCP/UDP Applicaton for Arduino using ESP8266's AT firmware.
C++
35
star
25

wiringpi-tft-tool

TFT Command Line Tool for Raspberry Pi
C
35
star
26

esp-idf-ftpServer

ftp server for esp-idf using FAT file system
C
34
star
27

Raspberry-ili9340spi

ILI9340 SPI TFT Library & XPT2046 Touch Screen Library for Raspberry
C
33
star
28

Arduino-LoRa-Ra01S

An Arduino Library for LoRa Communication using SX1262/1268
C++
33
star
29

Arduino-STM32-Ethernet-LAN8720

Ethernet on STM32 using external PHY
C++
32
star
30

esp-idf-protocol-buffer

Example of Google Protocol Buffers Serialize and Deserialize with ESP-IDF
C
30
star
31

esp-idf-espnow-gateway

Gateway between esp-now and MQTT using esp-idf
C
27
star
32

esp-idf-m5stickC

M5StickC ST7735S Driver for esp-idf
C
27
star
33

esp-idf-ds3231

DS3231 RTC Driver for esp-idf
C
26
star
34

Arduino-CANBus-Monitor

mcp2515 canbus module to Monitor Canbus traffic
C++
26
star
35

esp-idf-can2usb

CANbus to USB bridge using esp32
C
26
star
36

esp-idf-uart2bt

UART to Bluetooth bridge for ESP-IDF
C
25
star
37

SC16IS752

Driver for SC16IS752
C
24
star
38

Robotell-USB-CAN-Python

Python program for USB-CAN Adapter
Python
24
star
39

esp-idf-web-form

WEB Form example for ESP-IDF
C
23
star
40

esp-idf-http-camera

Take a picture and Publish it via HTTP
C
23
star
41

esp-idf-rc-switch

315/433MHz ASK RF driver for esp-idf
C
21
star
42

esp-idf-smb-client

SMB client example for esp-idf
C
21
star
43

OrangePi-ZERO-FAN-HAT

Cooling FAN HAT for OrangePi ZERO
20
star
44

esp-idf-GPS-Repeater

Transfer GPS NMAE messages over Wifi
C
19
star
45

esp-idf-24c

Two-Wire Serial EEPROM Access Library for esp-idf
C
18
star
46

Raspberry-ili9225spi

ILI9225 SPI TFT Library for RaspberryPi/OrangePi
C
18
star
47

esp-idf-wifi-apsta

WIFI_MODE_APSTA example with esp-idf
C
18
star
48

esp-idf-GPS-View

GPS NMEA Viewer for M5Stack
C
17
star
49

Arduino-STM32-8bitTFT

8bit parallel TFT Library for Arduino_Core_STM32
C++
17
star
50

esp-idf-pwm-slider

PWM Slider Bar Control using ESP-IDF
C
16
star
51

esp-idf-mqtt-camera

Take a picture and Publish it via MQTT
C
16
star
52

esp-idf-ftp-camera

Take a picture and Publish it via FTP
C
15
star
53

esp-idf-web-chart

Real-time data visualization using esp-idf
C
15
star
54

esp-idf-benchmark

Dhrystone/Whetstone benchmark for esp-idf
C
15
star
55

esp-idf-ssh-client

ssh client for esp-idf
C
15
star
56

esp-idf-m5stickC-Plus

M5StickC-Plus ST7789V2 Driver for ESP-IDF
C++
15
star
57

esp-idf-web-serial

Serial Monitor for esp-idf
C
14
star
58

esp-idf-a2dp-source

ESP32 Bluetooth A2DP-SOURCE for esp-idf
C
14
star
59

Arduino-SI4432

Arduino library for SILICON LABS SI443x
C++
14
star
60

Arduino-STM32-nRF24L01

Arduino stm32 support files for nRF24L01 RF modules
C++
13
star
61

esp-idf-multipart-upload

A multipart file upload example for esp-idf
C
13
star
62

esp-idf-irSend

M5Stick and M5StickC(+) as a remote control transmitter
C
12
star
63

esp-idf-can2websocket

Brows CAN-Frame using esp-idf
C
12
star
64

esp-idf-ds1302

DS1302 RTC Driver for esp-idf
C
12
star
65

Raspberry-pcd8544

pcd8544 Command Line Tool for Raspberry Pi / Orange Pi
C
12
star
66

esp-idf-nrf905

nRF905 Single chip 433/868/915MHz Transceiver Driver for esp-idf
C
11
star
67

esp-idf-candump

CAN Dump for esp-idf
C
11
star
68

esp-idf-sh1107

sh1107 Driver for esp-idf
C
11
star
69

esp-idf-bmi160

A demo showing the pose of the bmi160 6DoF IMU sensor in 3D using esp-idf
C
11
star
70

esp-idf-vs1053

VS1053 Driver for esp-idf
C
10
star
71

Arduino-STM32-Ethernet

Arduino stm32 example for W5x00 ethernet modules
C++
10
star
72

esp-idf-ultrasonic

Ultrasonic distance sensor for ESP-IDF
C
10
star
73

esp-idf-lsm6ds3

A demo showing the pose of the lsm6ds3 6DoF IMU sensor in 3D using esp-idf
C
10
star
74

esp-idf-zlib

Example compression and decompression using zlib
C
10
star
75

esp-idf-pcf8563

PCF8563 RTC Driver for esp-idf
C
10
star
76

STM32_GD2

FT800/FT81X TFT Library for Arduino_STM32
C
9
star
77

esp-idf-Bluetooth-SPP

Classic Bluetooth SPP example for esp-idf
C
9
star
78

esp-idf-web-gpio

GPIO control using web browser
C
9
star
79

ESP8266-UART-to-UART-Bridge

C++
9
star
80

esp-idf-qr-code-generator

QR Code generator for esp-idf
C
9
star
81

esp-idf-rf69

RFM69 ISM Transceiver driver for esp-idf
C
9
star
82

esp8266-mpd-client

MPD client example for ESP8266/ESP32.
C++
9
star
83

esp-idf-cc2500

CC2500 Low-Cost Low-Power 2.4 GHz RF Transceiver driver for esp-idf
C
8
star
84

esp-idf-video-snapshot

Capture still images from a USB camera using ESP-IDF
C
8
star
85

esp-idf-sql-client

SQL database access example for esp-idf
C
8
star
86

pwmlib-opi

PWM C Library for H3/H2+ Sunxi SoC
C
8
star
87

esp-idf-DFPlayerMini

DFPlayer - A Mini MP3 Player For ESP-IDF
C
8
star
88

Raspberry-at24c

Two-Wire Serial EEPROM Access Library for RaspberryPi
C
8
star
89

esp-idf-mqtt-client

GUI MQTT Client for esp-idf
C
8
star
90

esp-idf-voice-control

Demonstration of voice control of esp32
C
7
star
91

esp-idf-mpr121

MPR121 Capacitive Touch Driver for esp-idf
C
7
star
92

esp-idf-smtp-camera

Take a picture and Publish it via SMTP.
C
7
star
93

esp-idf-93Cx6

Three-Wire Serial EEPROM Access Library for esp-idf
C
7
star
94

esp-idf-sc16is750

sc16is750/752 driver for esp-idf
C
7
star
95

esp-idf-world-weather

Display the weather forecast on M5STACK
C
7
star
96

esp-idf-ds1307

DS1307 RTC Driver for esp-idf
C
6
star
97

esp-idf-mcp3002

Driver for A/D converter with SPI serial interface
C
6
star
98

Raspberry-93Cx6

Three-Wire Serial EEPROM Access Library for RaspberryPi
C
6
star
99

Raspberry-HT1621B

HT1621B Segment LED Example Code for RaspberryPi
C
6
star
100

esp-idf-irAEHA

M5Stick and M5StickC as a remote control transmitter
C
6
star