Device dashboard on NUCLEO-F746ZG - CMSIS-RTOS v2, using Keil MDK

Overview

This tutorial shows how to implement a Web device dashboard using Mongoose Library over CMSIS-RTOS API v2 on an STM32 Nucleo-F746ZG development board, using the ARM Keil MDK development environment.

device dashboard login

Features of this implementation include:

  • Uses Keil RTX5, ARM CMSIS Core and device headers through Software Packs
  • Uses Mongoose's built-in TCP/IP stack, which includes an STM32 Ethernet driver
  • Does NOT use an external network stack like lwIP, or MDK (RL)
  • The Web dashboard provides:
    • User Authentication: login protection with multiple permission levels
    • The web UI is optimized for size and for TLS usage
    • Logged users can view/change device settings
    • The web UI is fully embedded into the firmware binary, and does not need a filesystem to serve it, making it resilient
interactive device dashboard

This example is a hardware adaptation of the Device Dashboard that can run on Mac/Linux/Windows. Mongoose Library, being cross-platform, allows to develop and run the same code on different platforms. That means: all functionality related to networking can be developed and debugged on a workstation, and then run as-is on an embedded device - and this example is a demonstration of that.

Take your time to navigate and study the Device Dashboard tutorial. Here, we concentrate on the features specific to this embedded platform in the Keil MDK environment.

The Keil RTX5 implementation of this project is at examples/stm32/nucleo-f746zg-keil-rtx5. Though we specifically refer to this implementation, most references in this tutorial are valid for any supported RTOS. However, to use an RTOS other than Keil RTX5, please check this section.

This example is a plain Keil MDK-based project with the following files of interest:

  • main.c - provides the main() entry point with hardware init, LED blinking and network init.
  • hal.h - provides an abstraction for MAC generation
  • syscalls.c - provides a low level function to redirect debug output to a UART.
  • mongoose.c, mongoose.h - Mongoose Library
  • net.c, net.h - part of the device dashboard example, contains the Web functionality
  • packed_fs.c - part of the device dashboard example, embeds the Web UI used by the dashboard

All these files have been grouped in Source Group 1 in the Project Explorer. Keil MDK can integrate with STM32CubeMX, and we use it. The device configuration file, used as the recipe to generate the code for device initialization, is handled by the IDE, we access it through the Run-Time Environment manager. For all auto-generated files, we've used those places designated for USER_CODE, so all files can be re-generated by STM32CubeMX for newer versions of the firmware packs.

References in this tutorial are for a Nucleo-F746ZG board

Below is a general process outline:

  • The board IP addressing will be provided by a DHCP server. If you want to set a static configuration, set IP address, network mask and gateway in main.c; see below
  • Build the example (see below) and run it on a development board
  • The firmware initializes the RTOS; see RTOS integration below
  • The firmware initializes the network
  • After initialization, the application starts Mongoose's event loop and blinks a blue LED
  • Once the blue LED starts blinking, the example is ready
  • Open your web browser and navigate to the board IP address, you should see a nice device dashboard

Build and run

  • Follow the Build Tools tutorial to setup your development environment.

  • In your project directory, clone the Mongoose Library repository using git

  • Start μVision and open the project; if you need a quick start on the ARM Keil MDK and μVision, follow this step by step tutorial.

  • In order to build this project, click the Build icon. To flash this firmware to your board, plug it in a USB port and click on the Load icon. When finished, you have to reset your board pressing its reset button. You should soon see the blue LED start blinking. As long as there is only one board plugged in, μVision will find it; though we need to know the serial port device to be able to get the log information. You'll need to dig for it in your computer.

    Keil MDK build project
  • When the firmware starts, the board should signal its state by blinking the blue LED. We now need to know the IP address of the board to connect to it. If we used DHCP, as it is the default, we can check our DHCP server logs or see the device logs. Let's do this.

  • To connect to the board, in this example we'll be using PuTTY; we configure it for 115200bps.

    0      2 main.c:44:server               MAC: 02:35:42:56:22:2e. Waiting for IP...
    ...
    133e   2 mongoose.c:7621:onstatechange  READY, IP: 192.168.69.234
    1344   2 mongoose.c:7622:onstatechange         GW: 192.168.69.1
    134b   2 mongoose.c:7625:onstatechange         Lease: 21600 sec
    1351   2 main.c:49:server               Initialising application...
    1358   3 mongoose.c:3607:mg_listen      1 0x0 http://0.0.0.0
    135e   2 main.c:53:server               Starting event loop
    
  • Now start a browser on http://IP_ADDRESS, where IP_ADDRESS is the board's IP address printed on the serial console. You should see a login screen as in the image above

  • From here on, if you want to try the dashboard features please go to the device dashboard tutorial and follow some of the steps depicted there.

Compilation options

  • MG_ARCH=MG_ARCH_CMSIS_RTOS2 - configures Mongoose to work with a supported RTOS using CMSIS-RTOS API v2
  • MG_ENABLE_CUSTOM_RANDOM=1 - lets the firmware code override mg_random() to use the device hardware RNG
  • MG_ENABLE_TCPIP=1 - enables the built-in TCP/IP stack
  • MG_ENABLE_PACKED_FS=1 - enables the embedded filesystem support
  • To enable the proper built-in driver for your board:
    Board Compilation option
    NUCLEO-F429ZI MG_ENABLE_DRIVER_STM32=1
    NUCLEO-F746ZG MG_ENABLE_DRIVER_STM32=1
    NUCLEO-H743ZI MG_ENABLE_DRIVER_STM32H=1
    NUCLEO-H743ZI2 MG_ENABLE_DRIVER_STM32H=1

These are grouped in the file mongoose_custom.h. This file has been written using scripting extensions, so you can just select the Configuration Wizard tab at the bottom of the editor to see the file contents in a friendly way. Keil MDK configure Mongoose

main.c overview

This example can be divided in the following blocks:

  1. CMSIS-RTOS v2 integration
  2. Initialize the microcontroller for this particular board and take advantage of the true RNG in the microcontroller
  3. Start the scheduler to run the desired tasks
  4. Initialize Mongoose
  5. Initialize the networking stack
  6. Run Mongoose

RTOS integration

Mongoose supports a number of well-known architectures, among them CMSIS-RTOS v2. To tell Mongoose in which architecture it is running, we need to define the macro MG_ARCH as we've seen above; we do this in mongoose_custom.h.

Network operations need a time base to calculate timeouts; this will be provided by the CMSIS-RTOS v2 API and Mongoose now knows how to call it. We need a 1000 Hz rate to provide a 1ms time base.

  • We need to add the proper libraries in our Run-Time Environment configuration; in the Run-Time Environment management window select CMSIS -> RTOS2 (API) -> Keil RTX5 and its Variant as Library (for other RTOS, skip this and the next bullets, and check this section).

    Keil MDK Manage Run-Time Environment As you can see, we take advantage of the Keil packs.
  • We configure RTX5 by opening its configuration file, CMSIS/RTX_Config.h in the Project Explorer, and choosing the Configuration Wizard tab in the lower left of the editor window. Most defaults are OK for this project, we set:

    • Global Dynamic Memory size 65536 (64 KB) - ample space for Mongoose and any other project task stack
    • Kernel Tick Frequency 1000 Hz
  • One last thing to consider is the time base for the RTOS and the HAL. ARM suggests overriding the HAL timing call, ST in general suggests using separate timers; we chose the latter. As the RTOS uses the SWI, PendSV and SysTick IRQ handlers, we instructed Cube to not generate code for those at project creation, and to use one of the device timers as time base for the HAL. Of course, this can be changed later

Custom mg_random()

Some network operations require the generation of random numbers, from simple port numbers that should be different on every reset to complex TLS operations. Mongoose supports a number of well-known architectures, but since here we are working at the bare-metal level, we need to provide our own custom function or default to the standard pseudo-random number generator. The necessary actions are:

  1. Define MG_ENABLE_CUSTOM_RANDOM=1 which is done in mongoose_custom.h
  2. Provide a custom void mg_random(void *buf, size_t len) function:

In this example, this function uses the microcontroller's built-in RNG through the STM32Cube HAL provided in the Keil packs

MCU and board initialization

Microcontroller support is provided by Keil firmware packs, which are based on STM32Cube firmware packages. The microcontroller, its clock, and all peripherals we use are initialized by HAL functions according to the configuration, code like this is generated by STM32CubeMX. In fact, to reproduce all the configuration steps, follow the step by step tutorial up to and including step 5. In particular, step 1 describes the additions we need to do to a Cube-generated main file to call the generated initialization functions, this mx_init() function we call at the start of our main() function.

Since this is a CMSIS-RTOS v2 project, task stack allocation will be done through it; dynamic memory allocation still will be done through system calls so we need a system heap. We've left the initial stack allocation at a small value (1KB), and setup enough heap room at project creation; that can be seen/modified by opening the assembly startup file (Device/startup_<devicefamilyname>.s) in μVision and then use the Configuration Wizard. This setting affects the linker process.

Create tasks and start scheduler

We create the necessary tasks and call the function that starts the scheduler. Then, it takes over and we have a server task, where we initialize and run Mongoose, and a blinker task to blink an LED

Note we provide ample stack space for the Mongoose task. It doesn't actually need to be that big for such a simple example, but more complex interfaces will need plenty of room.

Mongoose initialization

In the server task we initialize Mongoose; this is no different from what we always do in any example; we are a task in an RTOS and we can run an infinite loop.

TCP/IP initialization

The built-in TCP/IP stack has to be enabled to be compiled in, and so Mongoose will work in association with it. This is done in mongoose_custom.h by defining MG_ENABLE_TCPIP=1.

Then this networking stack has to be configured and initialized. This is done by calling mg_tcpip_init() and passing it a pointer to a struct mg_tcpip_if. Inside this structure:

  • have pointers to a struct mg_tcpip_driver and any extra data that it could need
  • For DHCP: set ip as zero
  • For a static configuration, specify ip, mask, and gw in network byte order

In this example we use DHCP, but you can remove the comments and set a static configuration if you want:

Note that, we also need to specify a unique MAC address; this example provides a macro to transform the chip built-in unique ID into a unicast locally administered address; for production runs you'll have to consider among several options, from adding a MAC address chip in your hardware design to registering with the IEEE Registration Authority.

Some drivers, as you have probably noticed, require extra data. In this case the STM32 driver can accept the setting for the divider that generates the MDIO clock. You can pass a null pointer in the driver data or a negative value for this parameter and the driver will calculate it for you, based on the clock configuration.

As you can see, there are no multi-threading issues to worry about, just follow Mongoose documentation as usual and call all mg_* API functions from the same task where Mongoose is running.

Run Mongoose

Then we run Mongoose. This is no different from what we always do in any example, though note that it should be run after network initialization. The logic is standard: initialize the event manager (as we already did), start a listener, and fall into an infinite event loop:

In this case, the listener is started by web_init(), the device dashboard initialization function. The URL is configured by the macro HTTP_URL, which we defined as a preprocessor symbol.

Blinker task

This is a simple task that toggles the GPIO and loops, to blink the blue LED

We have covered those aspects that are specific to the STM32 implementation, for the details on the application and the UI, please see the Device Dashboard tutorial.

Differences among supported RTOSes

The FreeRTOS implementation is at examples/stm32/nucleo-f746zg-keil-freertos_cmsis2. In this example only configuration files change.

  • To build for FreeRTOS you need its Software Pack, ARM::CMSIS-FreeRTOS. If you don't have already installed it, you'll see a requester asking you permission to do so. The Pack Installer will open and install the pack.

  • In the Run-Time Environment management window

    • select CMSIS -> RTOS2 (API) -> FreeRTOS.
    • select RTOS, select its Variant as FreeRTOS, then select RTOS -> Config and its Variant as FreeRTOS (we are using the native FreeRTOS API with no abstraction layer), RTOS -> Core and its Variant as Cortex-M, RTOS -> Event Groups, RTOS -> Heap with its Variant as Heap_4, and RTOS -> Timers. If you need additional functionality, add it and then solve dependenciesKeil MDK Manage Run-Time Environment
  • Configure FreeRTOS by opening its configuration file, RTOS/FreeRTOSConfig.h in the Project Explorer, and choosing the Configuration Wizard tab in the lower left of the editor window. Most defaults are OK for this project, we set:

    • Total heap size 65536 - ample space for Mongoose and any other project task stack
    • Kernel Tick Frequency 1000 Hz
    • in Port Specific Features uncheck Use TrustZone Security Extension