STM32 Nucleo-F429Z
Overview
This example shows how to build an HTTP server using Mongoose Library and run it on an STM32 Nucleo-F429Z development board with FreeRTOS and lwIP.
The source code for this tutorial is located at https://github.com/mongoose-examples/stm32-nucleo-f429z
A big part of this example is a regular STM32CubeIDE project (a lot of generated
files, FreeRTOS and lwIP sources, etc.). Project structure will vary depending on the STM32CubeIDE version and how much of the project was generated using the STM32CubeMX tool. Old versions used to have the Inc
and Src
directories right on the root of the project, while newer versions put those under a Core
directory. Also, we may find specific configuration options like the IP address in Inc/main.h
, while newer auto-generated versions use specific directories; for example the IP address is inside the MX_LWIP_Init()
function, in file lwip.c
, under the LWIP
directory.
Mongoose-related functions are located in Src/web_server.c
Below is a general process outline:
- Set IP address, network mask and gateway in
Inc/main.h
- Build the example (see below) and run it on a development board
- The firmware initializes the network, and turns on a green LED if the initialization was successful, a red one otherwise
- If you see a red LED turned on, make sure that your Ethernet cable is properly connected to the board
- After initialization, the application starts Mongoose's event loop and blinks a blue LED
- Once the blue LED starts blinking - the web server is ready
- Open your web browser and navigate to the board IP address, you should see a "Hello, world" page
Build options
There are (at least) two options to build the example:
- Using the
make
command - Using the STM32CubeIDE IDE
Option 1: make
- Follow the Build Tools tutorial to setup your development environment.
- Start a terminal, navigate to the project folder, and from there run
make build
- When build finishes, run
make flash
Done! Now the firmware is flashed and the board should signal its state via an LED indicator (green or red).
Option 2: STM32CubeIDE
STM32CubeIDE is an IDE software provided by STMicroelectronics. We'll be importing the project so it has to be present in your filesystem, so first run make to clone the git repository; unless you've already tried Option 1 above.
- Start STM32CubeIDE
- Choose
File -> Import...
, then openGeneral
and selectExisting Projects into Workspace
: - Pick a project directory as
Root directory
, make sure thatNUCLEO-F429Zx-simple-web-server
is checked and clickFinish
: - After import, you'll probably have to close the "Information Center" window so you can actually see the project. Click on the project in the
Project Explorer
tab, now click on the hammer icon to build the project, then on the green arrow icon to flash the project. You can also right click on the project and selectRun As -> STM32 C/C++ Application
: - Done! The firmware should light the LEDs
Debug output
During execution, Mongoose can print debug information. There are several ways
to receive this information. This example re-routes stdout
to the SWV ITM Console.
This allows us to see the output of printf()
and similar functions directly in
STM32CubeIDE. For this, you need to do the following:
- Choose
Run
->Debug configurations...
: - Under
STM32 C/C++ Application
select theNUCLEO-F429Zx-simple-web-server
configuration. Note: if you don't see this configuration, close the dialog and start the debug session viaDebug as -> STM Application
to create it: - Go to the
Debugger
tab - Make sure you are using
SWD
mode (and not JTAG). CheckEnabled
in theSerial Wire Viewer
section - Update the
Core clock
setting, set it to 180MHz - Click
Apply
and then clickDebug
to start the debug session. You will probably be asked to switch to the "Debug" perspective, do it. - Once the application stops at the beginning of the
main
function, open the SWV Console (Window->Show View->SWV->SWV ITM Data Console
) - If there is not already a "port 0" tab, click
Add port
and add port 0 - Click
Configure trace
and verify port 0 is enabled, or enable it - Click
Start trace
- Resume program execution
- You should see something like this in the console window:
1970-01-01 00:00:00 I web_server.c:30:server Starting Mongoose v7.4 1970-01-01 00:00:00 I mongoose.c:3297:mg_listen 1 accepting on http://0.0.0.0:80 (port 80)
- Done! Now you can use the printf() function or the LOG() macro to output required information
Custom application
In order to create your own Mongoose-enabled application you have several ways:
The obvious way, is to add the required functionality to this example. The sample includes all standard HAL Drivers. As you can see in the
Src/web_server.c
file, theBSP_LED_On()
function is used to turn on an LED. In the same way, you can have access to all the HAL functions.If, for some reason, you can't use this example as a base (e.g.: you have your own big project to which you need to add Mongoose), you can do the following:
- Add FreeRTOS and lwIP to your project (via
Device Configuration tool
or manually, depending on your project type) - Initialize FreeRTOS and lwIP. Read this STM article.
- Add the
mongoose.c
(and.h
if you want) file(s) to your project - Add Mongoose-specific configuration flags
- Add the following defines in the
lwipopts.h
file:#define LWIP_SOCKET 1 #define LWIP_POSIX_SOCKETS_IO_NAMES 0 #define SO_REUSE 0
- Add
MG_ARCH=MG_ARCH_FREERTOS_LWIP
to the preprocessor symbols (Properties->C/C++ Build->Settings->Preprocessor
) - Enable float usage for printf (
Properties->C/C++ Build->Settings->MCU Settings->Use float with printf
) - Done! Now write code similar to that in
Src/web_server.c
(read Mongoose documentation) and let the magic begin
- Add FreeRTOS and lwIP to your project (via
If you want to evaluate STM and Mongoose as fast and simple as possible, but you have a board that is not covered by any of our examples and tutorials, try to use the
LwIP_HTTP_Server_Netconn_RTOS
example: it supports a lot of boards.- Copy the example to your workspace
- Add
mongoose.c
andweb_server.c
to your project - Add the same Mongoose-specific configuration flags as in point 2
- Comment out netconn initialization (probably in the
StartThread()
function) and callstart_mongoose()
instead - That's it! Your application should build and run Mongoose.