Site Information

 Loading... Please wait...

Blog

The ESP32 Processor and Programming MCP2515 and MCP2517/18 Per SPI Port

Posted by Wilfried Voss on

The ESP32 Processor and ProgrammingIntroduction

The ESP32 is a powerful, low-cost microcontroller developed by Espressif Systems, widely used in IoT applications. It features integrated Wi-Fi and Bluetooth, making it a popular choice for embedded systems. Among its various interfaces, the Serial Peripheral Interface (SPI) plays a crucial role in facilitating communication with peripherals such as sensors, displays, and external memory devices. This paper explores the architecture of the ESP32, the functionality of its SPI ports, and programming techniques, including interfacing with the MCP2515 and MCP2517/18 CAN controllers.

ESP32 Architecture Overview

The ESP32 is built around a dual-core Tensilica LX6 microprocessor with a clock speed of up to 240 MHz. It includes 520 KB of SRAM, integrated flash memory, and an extensive set of peripherals such as ADCs, DACs, UARTs, I2C, and SPI interfaces. The chip supports deep sleep and ultra-low-power operation modes, making it ideal for battery-powered applications.

The SPI Port on ESP32

SPI is a high-speed, full-duplex communication protocol used to connect microcontrollers with peripheral devices. The ESP32 features multiple SPI buses, including:

  1. SPI0 and SPI1 - Used internally for flash memory.

  2. SPI2 (HSPI) and SPI3 (VSPI) - Available for external device communication.

Each SPI bus supports up to three configurable Chip Select (CS) lines, enabling multiple devices to be connected to a single bus. The standard SPI configuration consists of four lines:

  • MOSI (Master Out, Slave In)

  • MISO (Master In, Slave Out)

  • SCK (Serial Clock)

  • CS (Chip Select)

SPI Programming on ESP32

The ESP32 SPI interface is programmable using the Arduino IDE, ESP-IDF, or MicroPython. The SPI library provided in the Arduino environment simplifies communication with SPI devices.

Basic SPI Communication Example

#include 

#define CS_PIN 5

void setup() {
  SPI.begin();  // Initialize SPI bus
  pinMode(CS_PIN, OUTPUT);
  digitalWrite(CS_PIN, HIGH);
}

void loop() {
  digitalWrite(CS_PIN, LOW); // Select device
  SPI.transfer(0x55);        // Send data
  digitalWrite(CS_PIN, HIGH); // Deselect device
  delay(1000);
}

Connecting MCP2515 and MCP2517/18 to ESP32

The MCP2515 and MCP2517/18 are CAN (Controller Area Network) controllers that enable the ESP32 to interface with CAN networks. The MCP2515 operates over SPI, while the MCP2517/18 offers an enhanced SPI interface with FIFO buffering for higher performance.

MCP2515 Connection with ESP32

The MCP2515 requires an external CAN transceiver (e.g., TJA1050) and is connected as follows:

  • MOSI → ESP32 MOSI (e.g., GPIO 23)

  • MISO → ESP32 MISO (e.g., GPIO 19)

  • SCK → ESP32 SCK (e.g., GPIO 18)

  • CS → ESP32 CS (e.g., GPIO 5)

  • INT → ESP32 interrupt pin (e.g., GPIO 4)

The Arduino mcp_can library simplifies communication with the MCP2515.

MCP2517/18 Connection with ESP32

The MCP2517/18 offers an advanced interface with a larger FIFO buffer and improved performance. The wiring is similar to the MCP2515:

  • MOSI → ESP32 MOSI

  • MISO → ESP32 MISO

  • SCK → ESP32 SCK

  • CS → ESP32 CS

  • INT → ESP32 interrupt pin

For programming, the MCP_CAN_lib or a dedicated ESP32-compatible driver can be used.

A great example of using the MCP2515 with the ESP32 is the ESP32 Development Board with Dual Isolated CAN Bus HAT from Copperhill Technologies (link). Additionally, an MCP2517/18 version of this board is currently in preparation, further expanding the ESP32’s capabilities in CAN applications.

Conclusion

The ESP32 provides a versatile and high-performance platform for embedded applications. Its SPI interface enables seamless communication with various peripherals, including CAN controllers such as the MCP2515 and MCP2517/18. Proper wiring and SPI configuration ensure reliable operation, making the ESP32 a suitable choice for CAN-enabled IoT and automotive applications.

References

  1. Espressif Systems. "ESP32 Technical Reference Manual." Available at: https://www.espressif.com

  2. MCP2515 Datasheet, Microchip Technology. Available at: https://www.microchip.com

  3. MCP2517/18 Datasheet, Microchip Technology. Available at: https://www.microchip.com

  4. Arduino SPI Library Documentation. Available at: https://www.arduino.cc/reference/en/libraries/spi/

  5. "Interfacing ESP32 with MCP2515 CAN Bus Module," Available at: https://randomnerdtutorials.com

  6. Copperhill Technologies. "ESP32 Development Board with Dual Isolated CAN Bus HAT." Available at: https://copperhilltech.com/esp32-development-board-with-dual-isolated-can-bus-hat/


ESP32 Development Board with Dual Isolated CAN Bus HATESP32 Development Board with Dual Isolated CAN Bus HAT

The espBerry DevBoard seamlessly integrates the ESP32-DevKitC with any Raspberry Pi HAT through its onboard RPi-compatible 40-pin GPIO header. Rather than serving as a Raspberry Pi alternative, the espBerry enhances the ESP32’s capabilities by leveraging the extensive ecosystem of RPi HATs, providing expanded functionality and versatile hardware options for a wide range of applications.

The Dual Channel CAN Bus Expansion HAT for Raspberry Pi fully supports the CAN 2.0 Standard and is engineered for high reliability with multi-layer onboard protection circuits and strong anti-interference capabilities. Its robust design makes it ideal for applications in automotive systems, industrial automation, and other mission-critical environments requiring stable and efficient CAN communication. More information...