As a followup to my previous post on adding support for Olimex STM32-E407 to Mbed OS, I have created a tutorial on how to setup and run a basic networking example with the offline Mbed CLI.
Prerequisites
- git
- mercurial
- python3
- dfu-util
Python 2.7.x is mentioned in docs as of 2019-10-19, but I have used Python3 and it works fine for me. As Python 2 is not maintained past 2020, we will use Python3.
On Ubuntu you can install the prerequisites with the following command
sudo apt install python3 python3-pip git mercurial dfu-util
Installation
NOTE: Mbed recommends you install mbed-cli in an virtual environment (like virtualenv) but I have installed it globally for convenience and laziness.
sudo pip3 install mbed-cli
Next, we need to install a compiler for ARM. I chose GCC ARM
NOTE: installing gcc-arm-none-eabi
using via apt package repositories is NOT recommended
Download the toolchain from https://developer.arm.com/tools-and-software/open-source-software/developer-tools/gnu-toolchain/gnu-rm/downloads
Install the toolchain to /opt
(or somewhere else)
cd ~/Downloads
tar xvf gcc-arm-none-eabi-8-2019-q3-update-linux.tar.bz2
sudo mv gcc-arm-none-eabi-8-2019-q3-update-linux /opt
sudo ln -s /opt/gcc-arm-none-eabi-8-2019-q3-update-linux /opt/gcc-arm-none-eabi
Configure Mbed to use your toolchain
mbed config --global GCC_ARM_PATH /opt/gcc-arm-none-eabi/bin
Setup Mbed CLI
Create a new project
mbed new ethernet-example
cd ethernet-example
You will need to update to Mbed 5.15.0 or later
(you can tell if STM32-E407 support is present by running find mbed-os/targets/ -iname "*OLIMEX*"
)
cd mbed-os
# checkout the commit where STM32-E407 was merged to master
mbed update latest
cd ..
Create main.cpp
inside the ethernet-example/
directory with the following code:
#include "mbed.h"
#include "EthernetInterface.h"
// Network interface
EthernetInterface net;
DigitalOut led1(LED1);
// Socket demo
int main() {
// LED on while the request is being made
led1 = 0;
// Bring up the ethernet interface
printf("Ethernet socket example\r\n");
net.connect();
// Show the network address
const char *ip = net.get_ip_address();
printf("IP address is: %s\r\n", ip ? ip : "No IP");
// Open a socket on the network interface, and create a TCP connection to mbed.org
TCPSocket socket;
socket.open(&net);
socket.connect("www.arm.com", 80);
// Send a simple http request
char sbuffer[] = "GET / HTTP/1.1\r\nHost: www.arm.com\r\n\r\n";
int scount = socket.send(sbuffer, sizeof sbuffer);
printf("sent %d [%.*s]\r\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]\r\n", rcount, strstr(rbuffer, "\r\n")-rbuffer, rbuffer);
// Close the socket to return its memory and bring down the network interface
socket.close();
// Bring down the ethernet interface
net.disconnect();
printf("Done\n");
// LED off
led1 = 1;
}
The code basically sets up an Ethernet connection using DHCP to obtain an address, then creates an HTTP request to www.arm.com, reads the response, then closes the connection and Ethernet interface.
Compiling
mbed compile -t GCC_ARM -m OLIMEX_STM32E407_F407ZG
Programming
Set the STM32-E407 board to DFU mode (move jumper B0_1 / B0_0
to B0_1
position)
Then run the following command to flash the board
sudo dfu-util -a 0 -s 0x08000000 -D BUILD/OLIMEX_STM32E407_F407ZG/GCC_ARM/ethernet-example.bin
Running
The following is required to see the output
- Disable DFU mode (
B0_1
->B0_0
) - Connect a serial port to the BOOT header (pinout is on the back of the board)
- Connect an Ethernet cable from the board to your network.
- Open the serial port at 9600 baud. On Linux I use
picocom
like so:
sudo picocom -b 9600 /dev/ttyUSB0
- Push the RESET button (by the DC barrel connector) to boot the code
You should get something like this on the serial port
IP address is: 10.42.0.193
sent 38 [GET / HTTP/1.1]
recv 64 [HTTP/1.1 301 Moved Permanently]
Done
Congrats you have Mbed OS running on the STM32-E407!
Changing the serial port baud
By default, stdout is written to USART3
on PB10
/ PB11
at 9600 baud. If you want
to use another port or baud rate for debugging (such as the UART on the UEXT header),
add the following to mbed-app.json
in the project directory.
{
"target_overrides": {
"OLIMEX_STM32E407_F407ZG": {
"target.stdio_uart_tx": "PC_6",
"target.stdio_uart_rx": "PC_7",
"platform.stdio-baud-rate": 115200,
"platform.default-serial-baud-rate": 115200
}
}
}