Arm Mbed OS support forum

Ethernet interface problem on stm32f407


I’m proting mbed os to a custom stm32f407 board, and follow the guide from TM32F407VET6 black board project. The link is :

Now I encounter a problem on ethernet interface. The pin connection of my board is the same as the ethernet module mentioned in that page. I run the demo code of TCP socket, and find the net.connect hangs up for a few seconds and return “-3004”. The socked connection is not established. Is there anyone know how to solve it?



#include “mbed.h”
#include “EthernetInterface.h”

// Network interface
EthernetInterface net;

uint8_t ip[4] = {192, 168, 25, 5};
uint8_t subnet[4] = {255, 255, 255, 0};
uint8_t gateway[4] = {192, 168, 25, 1};

const SocketAddress IP(ip, NSAPI_IPv4);
const SocketAddress SUBNET(subnet, NSAPI_IPv4);
const SocketAddress GATEWAY(gateway, NSAPI_IPv4);

// Socket demo
int main()
// Bring up the ethernet interface
printf(“Ethernet socket example\n”);
// net.connect();
net.set_network(IP, SUBNET, GATEWAY);
nsapi_error_t status = net.connect();

// Show the network address
SocketAddress a;
printf("IP address: %s\n", a.get_ip_address() ? a.get_ip_address() : "None");

// Open a socket on the network interface, and create a TCP connection to
TCPSocket socket;;

net.gethostbyname("", &a);

// Send a simple http request
char sbuffer[] = "GET / HTTP/1.1\r\nHost:\r\n\r\n";
int scount = socket.send(sbuffer, sizeof sbuffer);
printf("sent %d [%.*s]\n", scount, strstr(sbuffer, "\r\n") - sbuffer, sbuffer);

// Recieve a simple http response and print out the response line
char rbuffer[64];
int rcount = socket.recv(rbuffer, sizeof rbuffer);
printf("recv %d [%.*s]\n", rcount, strstr(rbuffer, "\r\n") - rbuffer, rbuffer);

// Close the socket to return its memory and bring down the network interface

// Bring down the ethernet interface


Hello Li,

Sorry. I have to update the site. Adding an mbed_app.json file with the following content to the project should fix the problem:

    "target_overrides": {
        "*": {
            "stm32-emac.eth-phy-AutoNegotiation": "ETH_AUTONEGOTIATION_DISABLE"

Best regards, Zoltan

that problem was fixed already, but depending on the Phy chip, changing the phy-address is necessary:

            "stm32-emac.eth-phy-address" : 1 

This is necessary for the LAN8720 boards.

hi Zoltan,

is the json file added under the project folder? It seems doesn’t work…
I also modified mbed_lib.json under mbed-os\connectivity\drivers\emac\TARGET_STM, as shown in the attached pic, is it correct?

no, not ok, always avoid to change something in mbed-os. Then you are no longer in sync with the remote branch. An update command will complain and you have to merge your changes again.
The correct way to use the config system is explained in the Mbed docs, create a mbed_app.json file in your project root (there should be a sample already) and add some content like Zoltan has shown. But only for the eth-phy-address.
The build process will create a macro MBED_CONF_… and that is used in the code. You can verify this in the BUILD dir, a file mbed_config.h is generated and will contain this macro definition.


Johannes is right. Do not make modifications to the mbed_lib.json. That file will be automatically updated in the new revision of mbed-os and you loose your settings. Rather create a mbed_app.json file in the root directory of your project (if no such exists yet) with content as suggested by Johannes:

    "target_overrides": {
        "*": {
            "stm32-emac.eth-phy-address": 1

That worked for me with LAN8720 using Mbed OS 6.4.0 and ARCH_MAX as target.

Edit: I have noticed that the value of eth-phy-address is already overriden to 1 for the ARCH_MAX target in the mbed_lib.json file of the stm32-emac library. So I tried to compile without any mbed_app.json file. And the connection to Ethernet worked fine. So I’d suggest to double check your wiring. Also try to reset your board after being programmed.


Thank you Johannes for sharing the info regarding the eth-phy-address and eth-phy-AutoNegotiation!

@JojoS @hudakz
Thanks for the suggestions. I added a Json file in the project root directory and rebuild the project. However, it seems doesn’t work…I checked the macro MBED_CONF…, the values are correct. the code executes to net->connect, then returns -3004. As to the hardware, the attached schematic is the same as the pin connection of ARCH_MAX. I’m sure the ethernet port hardware is OK since I tested RT-Thread( another embedded os) on my borad, and the ethernet port works…

so a bit confusing now…is there any other possible reason?


It seems there is an issue with online compiler. So I’d suggest to import this demo project into the Mbed StudioIDE and build it offline for the ARCH_MAX target using Mbed OS 6.4.0. It worked fine for me.
Please notice that it prints to UART1 (pins PA_9, PA_10) available on the UART header next to the JTAG/SWD header .