w5500 Ethernet Nucleo STM32F303RE crash

All about boards manufactured by ST
Post Reply
hardyn
Posts: 8
Joined: Sun Mar 28, 2021 5:33 am

w5500 Ethernet Nucleo STM32F303RE crash

Post by hardyn »

Hello,

I am trying to interface a W5500 ethernet PHY with the F303RE and am running into a great deal of difficulty. The SPI interface seems to go crazy after udp endpacket() command which appears like a hung/crashed state. Prior to the crashed state, there is data corruption in the values returned from the W5500, see below. By continuing to send UDP packets the combination will sometimes recovery temporarily. In this hung state, the clock pin starts oscillating like mad.

if the endpacket() line is commented out, the program is stable, but single direction traffic isn't helpful.

The exact same code run on a F103 Bluepill works perfectly, so it is not a case of damage to the W5500.

Any help would be appreciated.

Code: Select all

 
Received packet of size 11 
From 192.168.1.114, port 54041 
Contents : 
hello world 

Received packet of size 11 
From 192.168.1.114, port 54041 
Contents : 
hello world 

Received packet of size 11 
From 192.168.1.114, port 54041 
Contents : 
hello world 

Received packet of size 32 
From 114.108.100.32, port 49407 
Contents : 
hello world 

Received packet of size 32 
From 0.0.0.32, port 49407 
Contents : 
hello world 

Received packet of size 11 
From 192.168.1.114, port 54041 
Contents : 
hello world 

Received packet of size 11 
From 192.168.1.114, port 54041 
Contents : 
hello world 

Received packet of size 32 
From 0.0.0.32, port 49407 
Contents : 
hello world 

Received packet of size 32 
From 0.0.0.32, port 49407 
Contents : 
hello world 

Code: Select all

//#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.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);

unsigned int localPort = 8888;      // local port to listen on

// buffers for receiving and sending data
char packetBuffer[UDP_TX_PACKET_MAX_SIZE];  // buffer to hold incoming packet,
char ReplyBuffer[] = "acknowledged";        // a string to send back

// An EthernetUDP instance to let us send and receive packets over UDP
EthernetUDP Udp;

[img]
https://photos.app.goo.gl/zBXAkYagfXg3ioR78
[/img]


void setup() {
  // You can use Ethernet.init(pin) to configure the CS pin
  //Ethernet.init(10);  // Most Arduino shields
  //Ethernet.init(5);   // MKR ETH shield
  //Ethernet.init(0);   // Teensy 2.0
  //Ethernet.init(20);  // Teensy++ 2.0
  //Ethernet.init(15);  // ESP8266 with Adafruit Featherwing Ethernet
  //Ethernet.init(33);  // ESP32 with Adafruit Featherwing Ethernet

  //SPI.beginTransaction(SPISettings(1000000, MSBFIRST, SPI_MODE0));
  //SPI.endTransaction();

  // start the Ethernet
  Ethernet.begin(mac, ip);

  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  // 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 UDP
  Udp.begin(localPort);
}

void loop() {
  // if there's data available, read a packet
  int packetSize = Udp.parsePacket();
  if (packetSize) {
    Serial.print("Received packet of size ");
    Serial.println(packetSize);
    Serial.print("From ");
    IPAddress remote = Udp.remoteIP();
    for (int i=0; i < 4; i++) {
      Serial.print(remote[i], DEC);
      if (i < 3) {
        Serial.print(".");
      }
    }
    Serial.print(", port ");
    Serial.println(Udp.remotePort());

    // read the packet into packetBufffer
    Udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);
    Serial.println("Contents:");
    Serial.println(packetBuffer);

    // send a reply to the IP address and port that sent us the packet we received
    Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
    Udp.write(ReplyBuffer);
    Udp.endPacket();
  }
  
  delay(10);
}
stevestrong
Posts: 484
Joined: Fri Dec 27, 2019 4:53 pm
Answers: 8
Location: Munich, Germany
Contact:

Re: w5500 Ethernet Nucleo STM32F303RE crash

Post by stevestrong »

Don't you have some SPI pins of the F303 board shared with other on-board chips?
Which SPI interface do you use? CS pin?
Which core?
hardyn
Posts: 8
Joined: Sun Mar 28, 2021 5:33 am

Re: w5500 Ethernet Nucleo STM32F303RE crash

Post by hardyn »

Thanks for the response,

No, there are no other devices on the board. D13 is shared with the onboard LED (LD2) but this isn't causing a voltage issue on the pin (I have measured this), and with a ~500R resistor, its current draw (~3ma) should not be saturating the pin.

To my knowledge there is only one interface available that is hardcoded to pins D10-D13, which resolves to SPI interface 1. D10 as the CS pin. If that is what you mean? If there is an alternative SPI library for the STM32duino, I am unaware of it.

I believe I am using the Arduino_Core_STM32, I have made no effort to use others.
ag123
Posts: 867
Joined: Thu Dec 19, 2019 5:30 am
Answers: 5

Re: w5500 Ethernet Nucleo STM32F303RE crash

Post by ag123 »

stm32f303 is a little on the path less travelled.
there is one thing you may like to test for SPI at least, connect mosi to miso, my guess is you should get back your byte for

Code: Select all

byte_received = spi.send(byte_sent)
that's probably as raw a 'logic analyzer' as you can find
another goof is hardware goofs is sometimes more common than we think, check the wires if after all they are connect to the pins u'd want to connect.
other things are dig in to the ethernet library, check what are the pins used for SPI.begin(), you may be surprised if after all they are using different pins than you are expecting in the call to SPI.begin()

and one of those things is i'd normally keep a led blinking away, just to be sure that it's alive. if the led blinks in loop() while nothing happens, chances are that it isn't 'hang' and the problem is elsewhere. if the led doesn't blink, check for a possible hardfault (this one is hard, it may take some skills & in debug to find that out)

one of those things is take an st-link v2 and debug away sometimes tracing codes and checking the hardware registers e.g. gpio / spi etc could help to catch where the problem is. i almost wanted to setup a logic analyzer to check signals while messing with sdfat, then i noticed a curious piece of code that sends data across spi with a null pointer. something like

Code: Select all

spi.send( read_buffer, null);
so i think what the author intend to do is to read data into the buffer while sending null in return. the trouble is that would send a bunch of data from location zero had we not caught the 'bug'.
there are curious programming assumptions which are incorrect but simply used that way
stevestrong
Posts: 484
Joined: Fri Dec 27, 2019 4:53 pm
Answers: 8
Location: Munich, Germany
Contact:

Re: w5500 Ethernet Nucleo STM32F303RE crash

Post by stevestrong »

If it runs on bluepill correctly, then it must be related to the new board and/or STM derivate MCU, so you should check the supply voltages / pins / core settings...
hardyn
Posts: 8
Joined: Sun Mar 28, 2021 5:33 am

Re: w5500 Ethernet Nucleo STM32F303RE crash

Post by hardyn »

As it turns out, the Bluepill SPI interface appears to be much more robust than that of the F303RE. It was not a crash per-se but severe data corruption that appeared to halt the program. The wire length used on my test-setup for the BP was, apparently, too long for use on the F303RE. Shortening up the wires resolves the situation.

Sorry for the delay in response, I had back burner-ed this project and just picked it up again a little while ago. Thanks to everybody that contributed.
Post Reply

Return to “STM boards (Discovery, Eval, Nucleo, ...)”