Importing example in Espressif framework in Arduino framework

Hi guys…

I am interested in the following example for the Ethernet connection…

They use the espressif framework… Is there a simple way to use this example in the ArduinoFramework…

II tried to import the header files missing but still have problem during linking stage…

What am I doing wrong ?

It is possible to do that ?

Thanks a lot

Possible? Yes. Simple? Oh no. The ESP-IDF example makes a modification to the SDK settings (here). The Arduino uses a precompiled version of the SDK with this setting set the opposite way. So, integrating it into Arduino-ESP32 would require a recompilation of the base libraries which I presonally found to be extremely tedious (GitHub - espressif/esp32-arduino-lib-builder).

The ENC28J60 is a very common Ethernet chip with good general library support. Do the libraries linked at Ethernet test on esp32 not work?

1 Like

Thanks for the quick reply.
I tried to use

But I really don’t find useful examples about how to use the library…
Any idea where can I find them?

Thanks a lot

The library is a supplement for Ethernet.h that would usually be used for the e.g. Arduino AVR core. Hence, all sketches like Ethernet/examples at master · arduino-libraries/Ethernet · GitHub should work with it. As a starting point, you can e.g. use WebClient.ino, with the Ethernet.init(x); line adapted for x = Chip Select pin to the W5500 module.

1 Like

Ok… But I have a enc28j60 not a W5500 module… is the example working also for that module?

Thanks again

Sorry yes, I misspoke, the jandrassy/EthernetENC is for the ENC28J60, it is the “usual” Arduino-provided Ethernet.h library that’s implemented for W5500 (and similiar) chips. But the EthernetENC library is exposing the same APIs as the usual Arduino library, just under the hood it’s having the code to talk to a ENC28J60 chip. So yes, you can use those examples.

1 Like

Hi Max…

I just tried one example and It didn’t work,

#include <SPI.h>
#include <EthernetENC.h>

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
IPAddress ip(192, 168, 1, 177);

// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):

EthernetServer server_ethernet(80);
EthernetClient client_ethernet;

// Variables to measure the speed
unsigned long beginMicros, endMicros;
unsigned long byteCount = 0;
bool printWebData = true;  // set to false for better speed measurement


void Ethernet_Init(void)
{

// You can use Ethernet.init(pin) to configure the CS pin
Ethernet.init(15);  // Most Arduino shields

// Open serial communications and wait for port to open:
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.println("Ethernet WebServer Example");

// start the Ethernet connection and the server:
Ethernet.begin(mac, ip);

// Check for Ethernet hardware present
if (Ethernet.hardwareStatus() == EthernetNoHardware) 
{
Serial.println("Ethernet shield was not found.  Sorry, can't run without hardware. :(");
while (true) 
{
  delay(1); // do nothing, no point running without Ethernet hardware
}
}
if (Ethernet.linkStatus() == LinkOFF) 
{
Serial.println("Ethernet cable is not connected.");
}

// start the server
server_ethernet.begin();
Serial.print("server is at ");
Serial.println(Ethernet.localIP());

}

Each time it tells that it cannot find the hw… It does not recognize the shield…
My concern is that If have the following Hw configuration
image

From the code it seems that I can only set the CS pin… I need to set all the pins correctly or verify that they are using the right one

Any idea of what, how or where Can I verify this ?

Thanks a lot

Since the library is using SPI library with default settings

it will be expecting the SPI bus pins to be standard, i.e., for MOSI, MISO, SCLK see ESP32 Thing Hookup Guide - SparkFun Learn (i.e., it uses VSPI, not HSPI).

Since the library is hardcoded for SPI.begin(), I suggest you fork the library / copy it in the lib/ folder of your project and modify the .begin() call to be as in this example.

Also, the SPI bus speed of 20MHz is hardcoded here. As far as I can see, it does not use the interrupt pin.

1 Like

Ok max…
So changing the begin function and doing the init of the spi I should be able to configure the hw for my scenario.
In the example for the espressif framework is straighforward …

I need to change a bit but I think it should work

Thanks

Hi Max …
I have tried to modify the code in the following way …

void New_SPI_init (void)
{
  hspi = new SPIClass(HSPI);

  hspi->begin(HSPI_SCLK, HSPI_MISO, HSPI_MOSI, HSPI_SS); //SCLK, MISO, MOSI, SS
  hspi->setFrequency(spiClk);

  pinMode(HSPI_SCLK, OUTPUT);
  pinMatrixOutAttach(HSPI_SCLK, 0, false, false);

  pinMode(HSPI_MISO, INPUT);
  pinMatrixInAttach(HSPI_MISO, 0, false);

  pinMode(HSPI_MOSI, OUTPUT);
  pinMatrixOutAttach(HSPI_MOSI, 0, false, false);

}

void Enc28J60Network::initSPI()
{
  if (spiInitialized)
    return;
  pinMode(csPin, OUTPUT);
  CSPASSIVE;
  //SPI.begin();
  New_SPI_init();
  spiInitialized = true;
}

where the variables are the following

#define HSPI_MISO          12

#define HSPI_MOSI          13

#define HSPI_SCLK          14

#define HSPI_SS            15

#define HSPI_INTERRUPT     26

static const int spiClk = 6000000; // 6 MHz

//uninitalised pointers to SPI objects

SPIClass * hspi = NULL;
// set CS to 0 = active

#define CSACTIVE digitalWrite(csPin, LOW)

// set CS to 1 = passive

#define CSPASSIVE digitalWrite(csPin, HIGH)

#define SPI_ETHERNET_SETTINGS SPISettings(spiClk, MSBFIRST, SPI_MODE0)

But still getting not connection with the HW shield…

In your opinion what I am doing wrong… ?

Simone

Can you link to which hardware shield you’re using exactly?

That’s the raw chip, but do you have that chip on a specific breakout board / carrier board that you baught from somewhere? Or did you just plug the DIP version into a breadboard and connected it to a clock and ethernet plug and all that?

The shield is working and tested on a custom embedded board.
The code working is the following…

Using this example on the same board it is working…

I see. I have a ENC28J80 breakout board, I will connect it up to an ESP32 with the same pin mapping and see which library modifications are needed to get it working and report back.

Max just as reminder my pin mapping is the following…

image

Thanks for your time and effort I don’t know how to say thank you

Max…
I did a small improvement.
I changed the SPI.cpp file in line 295

I forced this command

SPIClass SPI(HSPI);   

It was SPIClass SPI(VSPI) and i think it is fixed unfortunatly

And I changed here

void Enc28J60Network::initSPI()

{

  if (spiInitialized)

    return;

  pinMode(csPin, OUTPUT);

  CSPASSIVE;

  SPI.begin(HSPI_SCLK, HSPI_MISO, HSPI_MOSI, HSPI_SS);

  SPI.setFrequency(spiClk);

  spiInitialized = true;

}

Now when I run the following init function

void Ethernet_Init(void)

{

  // You can use Ethernet.init(pin) to configure the CS pin

  Ethernet.init(15);  // Most Arduino shields

  // Open serial communications and wait for port to open:

  while (!Serial) {

    ; // wait for serial port to connect. Needed for native USB port only

  }

  Serial.println("Ethernet WebServer Example");

  // start the Ethernet connection and the server:

  Ethernet.begin(mac, ip);

  // Check for Ethernet hardware present

  if (Ethernet.hardwareStatus() == EthernetNoHardware)

  {

    Serial.println("Ethernet shield was not found.  Sorry, can't run without hardware. :(");

    while (true)

    {

      delay(1); // do nothing, no point running without Ethernet hardware

    }

  }

  if (Ethernet.linkStatus() == LinkOFF)

  {

    delay(500);

     if (Ethernet.linkStatus() == LinkOFF)

     {

        Serial.println("Ethernet cable is not connected.");

     }  

  }

  // start the server

  server_ethernet.begin();

  Serial.print("server is at ");

  Serial.println(Ethernet.localIP());

}

It is ok i don’t have the issues of hw not found and it is able to detect if the ethernet cable is connected from my laptop to the ethernet port…
But i would like to test the transmission between pc and the esp32 module…
How can I do that ?

void Ethernet_Task(void)
{   
    ?????????
}

Moreover many settings are still obscure from the SPI setting… I need a test with a real communication in order to validate that

void SPIClass::setClockDivider(uint32_t clockDiv)

{

    _div = clockDiv;

    spiSetClockDiv(_spi, _div);

}

uint32_t SPIClass::getClockDivider()

{

    return spiGetClockDiv(_spi);

}

void SPIClass::setDataMode(uint8_t dataMode)

{

    spiSetDataMode(_spi, dataMode);

}

void SPIClass::setBitOrder(uint8_t bitOrder)

{

    spiSetBitOrder(_spi, bitOrder);

}

Any idea for the test ?

In any case big progress…

The problem is that although this creates and initializes a new hspi object, the entire rest of the file is still using the global SPI object which uses VSPI. The idea was that you modify SPI.begin() to include the right pins, not create a new SPI object that later goes unused. I’ll show you a more correct library fix for this in a moment (allowing you to pass a SPIClass* into the Ethernet class, for example.)

Just use the Ethernet/examples/WebServer/WebServer.ino at master · arduino-libraries/Ethernet · GitHub sketch and try to access the created webserver from the PC’s browser. This tests bidirectional communication capability.

Yes you are right…
That is the reason why I changed the SPI.cpp file…