Windows Quick Start: Difference between revisions

From Miosix Wiki
Jump to navigation Jump to search
mNo edit summary
mNo edit summary
 
(13 intermediate revisions by 3 users not shown)
Line 1: Line 1:
=== Before you begin ===
= Installing the required software =


Before you can install the Miosix Toolchain on Windows, you will need to install [[https://en.wikipedia.org/wiki/Git_%28software%29 git]], which is necessary to download the Miosix kernel and keep it up to date, [[https://en.wikipedia.org/wiki/Perl Perl]], which is a scripting language used when compiling the kernel, and the STLink drivers if you want to program stm32 boards that have an USB-based STLinkV2 programmer/debugger.
== Miosix Toolchain ==


* For git, it is recomended to install it from [[http://www.git-scm.com/download/win git-scm.com]]. Please '''do not''' uncheck the ''Windows Explorer integration'' feature during the installation, as you will need it to install the kernel sources.
To be able to compile the Miosix kernel and your application, you need a patched version of the GCC compiler called Miosix toolchain, you can find the link to download it [[Miosix Toolchain|here]]. At the end of the installation you will need to reboot your computer (sorry, it's Windows...)
* [[http://strawberryperl.com Strawberry perl]] is the recomended perl version for Miosix on Windows
* The STLink drivers can be found on the [[http://www.st.com/web/en/catalog/tools/PF258168# ST website]].


Finally, it is recomended to download a better text editor than Notepad or WordPad, as you will need it to edit the Miosix configuration files. [[http://www.notepad-plus-plus.org/ Notepad++]] is a good option, but many other options exist. Just '''don't use notepad''', because it does not recognize Unix [[https://en.wikipedia.org/wiki/Line_ending line-endings]] and will show you Miosix source files as if they were composed of a single line of text.
== Git ==


Once you have downloaded and installed the aforementioned software, you can proceed with the installation of the Miosix Toolchain.
To download the Miosix kernel and keep it up to date, you need the [https://en.wikipedia.org/wiki/Git_%28software%29 git] version control system.


=== Install the Miosix Toolchain ===
It is recommended to install it from [http://www.git-scm.com/download/win git-scm.com]. Please '''do not''' uncheck the ''Windows Explorer integration'' feature during the installation, as you will need it to install the kernel sources.


Download the [[http://miosix.org/toolchain/MiosixToolchainInstaller.exe Miosix Toolchain installer]] and run it. At the end you will need to reboot your computer (sorry, it's Windows...)
== Perl ==


=== Get the Miosix kernel sources ===
The [https://en.wikipedia.org/wiki/Perl Perl] programming language is used by the Miosix build system. Unlike on GNU/Linux computers where Perl is pre-installed, for Windows you'll need to install it separately.


Go to the directory where you want to dowload the Miosix kernel, for example the Documents directory, and right-click on it. Select ''Git bash'' to open a git shell. There you can type the commands to download the kernel.
[http://strawberryperl.com Strawberry perl] is the recomended Perl version for Miosix on Windows.


[[File:Gitbashwindows.png]]
== Flashing tool ==


Currently, Miosix 2.0 is in the ''testing'' branch of the git repository, while the default one, the ''master'' branch, contains Miosix 1.x. So, you need to explicitly switch to the testing branch after downloading the kernel. To do so, type the following commands in the git shell.
With the installed Miosix toolchain you'll be able to compile the kernel, but you'll also need a way to transfer it to your microcontroller. This operation is usually called 'flashing' the microcontroller. Miosix can work with any flashing utility that accepts as input raw binary files, and on Windows you'll usually want to install the flashing tool provided by your chip vendor.
 
For STM32 chips, you can use the STLink utility available [http://www.st.com/web/en/catalog/tools/PF258168# here].
 
== Text editor or IDE ==
 
It is recommended to download a better text editor than Notepad, as you will need it to edit the Miosix configuration files. [http://www.notepad-plus-plus.org/ Notepad++] is a good option, but many other options exist. Just '''don't use Notepad''', because it does not recognize Unix [https://en.wikipedia.org/wiki/Line_ending line-endings] and will show you Miosix source files as if they were composed of a single line of text.
 
As an alternative to Notepad++, you may want to use an [[IDEs|IDE]].
 
== Serial port viewer ==
 
Miosix redirects ''stdin''/''stdout'' to a serial port by default on most boards, so to see the boot log and the output of ''printf()'' in your application you need a program to interact with the serial port. [https://putty.org/index.html PuTTY] is an option, although old fashioned. TODO: There are likely better options for Windows too.
 
= Downloading and configuring the Miosix kernel =
 
This section is a stripped down guide with the bare minimum to get you started, we also have a full guide about [[Miosix and git workflow]]. Following the tutorial in this page, your project directory will '''not''' be under version control (git). The [[Miosix and git workflow]] page shows how to create a git repository for your project and add the Miosix kernel as a git submodule.
 
Before downloading the kernel, however, you first have to create '''a directory for your project''' (in this case, the hello world), and then clone the kernel '''as a subdirectory'''.
 
Using the Windows explorer, create a '''hello''' directory, double click on it. Then right-click on the empty directory. Select ''Git bash'' to open a git shell. This opens a shell where you can type the commands to download the kernel.
 
[[File:Git-bash-windows.png]]
 
Note that it is possible to paste commands in the shell (so as to avoid typing them, which can be tedious and lead to typos), but you have to do it one line of text at a time, and to paste you need to use the ''Shift+Ins'' shortcut, not the usual ''Ctrl+V'' one. The last command, ''exit'', will close the shell.


<source lang="bash">
<source lang="bash">
git clone https://git.gitorious.org/miosix-kernel/miosix-kernel.git
git clone https://github.com/fedetft/miosix-kernel.git
cd miosix-kernel
git fetch origin
git checkout -b testing origin/testing
exit
exit
</source>
</source>


Note that it is possible to paste commands in the shell (so as to avoid typing them, which can be tedious and lead to typos), but you have to do it one line of text at a time, and to paste you need to use the ''Shift+Ins'' shortcut, not the usual ''Ctrl+v'' one. The last command, ''exit'', will close the shell.
Note that there is also a ''Git GUI'' in the right click menu, but git is best used from a command line interface, you're on your own if you choose to use git from the GUI.
 
== Initializing a Miosix project ==
 
For reasons explained in depth in the [[Miosix and git workflow]] page, you'll need to copy the Miosix configuration directory from the kernel sources to your project directory, as well as create a template project where you can start writing code.
This can be automated with a [https://en.wikipedia.org/wiki/Perl Perl] script called ''init_project_out_of_git_tree.pl''. To run it, you need to open a Windows terminal (which is a '''different''' terminal than the git bash we used before), which can be done by ''Shift+Right click'' and selecting ''Open command window here'':
 
[[File:Open-terminal-windows.png]]
 
Then, type the following command:
 
<source lang="bash">
perl miosix-kernel/tools/init_project_out_of_git_tree.pl
</source>
 
Leave the terminal open as you'll need it later.


Note that there is also a 'Git GUI' in the right click menu, but since I don't know how to use it, you're on your own if you choose that route.
You should run the script from the directory where you want to initialize your empty project, such as the '''hello''' directory in this tutorial. A successful run of the script produces the following output:


=== Configuring and compiling the kernel ===
<source lang="bash">
Successfully created Miosix project
Target directory: hello
Kernel directory: hello/miosix-kernel/miosix
</source>


More in-depth information on how th configure the kernel for your board can be found in the [[Board List]], but for now we will assume you have an stm32f4discovery, which is a common board, and briefly show how to get to a blinking LED example.
Your project directory should now look like the following picture, with the ''miosix-kernel'' directory containing the kernel sources, the ''config'' directory with the kernel configuration, the empty ''main.cpp'' and the build files for the two alternative build systems supported by Miosix: Makefile and CMake:


'''Top-level directory'''
[[File:Initialized-miosix-project-windows.png]]


First of all the miosix-kernel directory is often referred to as the '''top-level directory''' of the kernel. It contains, among other, the ''main.cpp'' file which is where you can start writing your application code, the ''Makefile'' where you can add additional C++ and C source files to be compiled, and the ''miosix'' directory which contains the kernel.
For this tutorial, we'll use the Makefile build system, have a look at the [[CMake]] page to learn how to use CMake instead.


[[File:Miosixtopleveldirectorywindows.png]]
== Configuring the kernel ==


All paths in this wiki, unless they start with a '/' (for Linux) or 'C:\' (For Windows), are intended relative to Miosix's top-level directory, so if we're talking about the 'miosix/config/Makefile.inc' file you can find it within the directory where you have downloaded the kernel from git.
The minimum configuration required to compile the kernel is to '''select a board''', as the kernel needs to build the appropriate board support package for it.


'''Configuring the kernel'''
The Miosix kernel has several other build-time configuration options, to enable optional components (filesystem, userspace processes), select algorithms such as the scheduler and trade feature support for code size, but these won't be covered here.
The kernel is configured by editing files in the config directory, the two most important ones are [https://github.com/fedetft/miosix-kernel/blob/master/miosix/config/Makefile.inc Makefile.inc] and [https://github.com/fedetft/miosix-kernel/blob/master/miosix/config/miosix_settings.h miosix_settings.h].


The kernel is configured by editing two files, named [[Makefile.inc|miosix/config/Makefile.inc]] and [[miosix_settingls.h|miosix/config/miosix_settings.h]]. Open the first one in Notepad++, and select your board. To do so, look for the ''OPT_BOARD'' section of the file, which looks like this:
To select a board, using Notepad++ open the first one, ''config/Makefile.inc''. Look for the ''OPT_BOARD'' section of the file, which contains the list of boards officially supported by the kernel. In this section, all boards appear commented out (the ''#'' sign is a comment in Makefile syntax), so you need to uncomment the line corrsponding to your board. [[Porting]] the kernel to a new board is an advanced topic not covered here, so we'll assume you'll want to use an officially supported board. As an example, in this tutorial we'll use the stm32f746zg_nucleo board:


<source lang="bash">
<source lang="bash">
##
...
## Target board, choose one. This also implicitly select the target
#OPT_BOARD := stm32f469ni_stm32f469i-disco
## architecture
OPT_BOARD := stm32f746zg_nucleo
##
#OPT_BOARD := stm32f765ii_marco_ram_board
#OPT_BOARD := lpc2138_miosix_board
...
OPT_BOARD := stm32f103ze_stm3210e-eval
#OPT_BOARD := stm32f103ve_mp3v2
#OPT_BOARD := stm32f100rb_stm32vldiscovery
#OPT_BOARD := stm32f103ve_strive_mini
#OPT_BOARD := stm32f103ze_redbull_v2
#OPT_BOARD := stm32f407vg_stm32f4discovery
#OPT_BOARD := stm32f207ig_stm3220g-eval
#OPT_BOARD := stm32f207zg_ethboard_v2
#OPT_BOARD := stm32f207ze_als_camboard
#OPT_BOARD := stm32l151_als_mainboard
#OPT_BOARD := stm32f407vg_bitsboard
#OPT_BOARD := stm32f205rg_sony-newman
</source>
</source>
In Makefile syntax a '#' sign denotes a comment, so to select a board you have to comment out (by prepending a '#') the default board selected, and uncomment (by removing the '#' at the start of the line) your board, which in this example is the ''stm32f407vg_stm32f4discovery''.


Next, edit the ''miosix_settings.h'' file using Notepad++ and uncomment (by removing the '//' at the start of the line) the following line
If you do not yet have a board, you can follow along and get to the compiled firmware anyway. If you're looking for suggestions on which board to get, consider that most boards currently supported by Miosix are for STM32 microcontrollers. Here's a basic guide:
<source lang="CPP">
* '''Nucleo''' STM32 boards produced by ST are good for beginners, as they allow to power the board, flash it, debug it and access the serial port all through a single USB connection with your computer. The stm32f401re_nucleo and stm32f411re_nucleo are realtively low cost.
//#define JTAG_DISABLE_SLEEP
* '''Discovery''' STM32 boards are also good, but they don't include an USB to serial adapter so you'll have to buy it separately and you'll end up having to connect two USB cables, one for power/programming/debugging and one for the serial port.
</source>
* '''Third party''' STM32 boards such as the stm32f411ce_blackpill are cheaper and great to embed in your hardware project, but you'll have to [[https://en.wikipedia.org/wiki/Soldering solder]] the pin header yourself, and you'll need to attach a separate USB to serial adapter, and if you want in-circuit debugging you'll need an STLink adapter, so you'll end up having to run up to three separate USB cables from your computer to the board for power/programming, serial port and debugging.
(which is towards the end of the file). This is a C++ header file, so the comment syntax is '//'. More information on the meaning of this line can be found in [[miosix_settings.h]], but a short summary is that this line prevents the kernel from shutting down the CPU when it has nothing to do, which saves power but interferes with the programming/debugging protocol used to program the board via QSTLink2.
* Venturing outside of STM32, the '''RP2040''' is a good choice. Also in this case, you'll need a separate USB to serial adapter and some soldering skills, though.
 
= Writing your first Miosix program =


'''Blink a LED'''
== Hello world ==


Open the ''main.cpp'' file in the top-level directory using Notepad++, and replace its content with the following program:
With Notepad++ open the ''main.cpp'' file with your [[IDEs | IDE]] or text editor of choice, and replace its content with the following program:
<source lang="CPP">
<source lang="CPP">
#include <cstdio>
#include <thread>
#include <miosix.h>
#include <miosix.h>
#include <interfaces/bsp.h>
using namespace std;
using namespace miosix;
using namespace miosix;
int main()
int main()
{
{
     for(;;)
     for(;;)
     {
     {
        iprintf("Hello world\n");
         ledOn();
         ledOn();
         Thread::sleep(1000);
         this_thread::sleep_for(500ms);
         ledOff();
         ledOff();
         Thread::sleep(1000);
         this_thread::sleep_for(500ms);
     }
     }
}
}
</source>
</source>
The Miosix board support package defines the ''ledOn()'' and ''ledOff()'' functions to control a LED on the board for all the boards that have at least one software-accessible LED.


'''Compiling'''
At this point, it's good to have a quick walkthrough of this hello world program. Miosix is based on a [[Fluid kernel]] architecture: in a nutshell it's a kernel that supports writing applications both inside the kernel, and inside userspace programs. In this tutorial, we're using it as a unikernel. This means when power is applied to the board, the kernel will boot, and then start running the ''main()'' function you just wrote ''in kernelspace''. This means you have both support for C/C++ standard libraries and unrestricted hardware access.
 
This main() function will print "Hello world" and blink an LED approximately every second.
The printing is done with the ''iprintf()'' function. If you expected just ''printf()'' without the ''i'', the reason is that Miosix uses the [https://sourceware.org/newlib/ Newlib] C standard library that provides both ''printf()'' and ''iprintf()'', an optimized version that is smaller in code size but incapable of printing floating point numbers. So you can also use ''printf()'' if you wish, but this optimization is worth knowing from the start.
 
Sleeping to blink the LED is done with the [https://en.cppreference.com/cpp/thread/sleep_for sleep_for()] function from the C++ standard library. Miosix supports other sleeping APIs, such as [https://pubs.opengroup.org/onlinepubs/9699919799/functions/nanosleep.html nanosleep()] from POSIX, or ''Thread::sleep()'', a native Miosix function. All these functions do the same thing: they tell the scheduler to stop the currently running thread, run some other thread, and resume execution after a given time interval. Miosix is a preemptive multithreading operating system, so you should get comfortable with the fact that the code you're writing lives in a thread and shares the CPU with other threads.
 
Many (but not all!) board support packages define the ''ledOn()'' and ''ledOff()'' functions to control one of the LED on the board. This should be true for all the boards that have at least one software-accessible LED. This is true for the ''stm32f746zg_nucleo'' board we selected, but if you selected a different board with no LEDs, then compiling will fail with an error related to ''ledOn()'' and ''ledOff()''. You can comment out those two lines in this case, your hello world will only print to the serial port.
 
== Compiling ==
 
In the same terminal you used to run Perl, type ''make'' to build the kernel.
 
<source lang="bash">
make
</source>
 
If all goes well, the result should look like this.


To compile the kernel, open a DOS shell in the Miosix top-level directory (you can 'Shift+Right click' in the top-level directory and choose 'Open command window here' in modern versions of Windows), otherwise you will have to ''cd'' your way into that directory.
[[File:Make-output-windows.png]]


[[File:Openterminalwindows.png]]
Otherwise, compiler errors will appear in the shell. The number that appears under ''text'' in the make output is the size in bytes of your application plus the kernel. Don't worry about the 42KBytes code size, you're compiling with the default Miosix options, if you want you can play around with the options later to  [[Miosix code size optimization|reduce the code size]].


To compile the kernel type ''make'' in the DOS prompt. If all goes well, the result should look like this.
== Flashing ==


[[File:Makeoutputwindows.png]]
The procedure to flash the microcontroller board on Windows depends on the vendor-provided flashing tool. They usually provide a GUI where you can load the ''.bin'' file and then connect to the board and flash it. For Miosix the file is named main.bin if you're not using processes, or image.bin if you are using processes. For this tutorial, look for ''main.bin'' in the '''hello''' directory.


Otherwise, compiler errors will appear in the DOS prompt. The number that appears under ''text'' in the make output is the size in bytes of your application plus the kernel. If you think that 90KBytes is a bit too much for a blinking led, don't worry. The kernel by default includes support for C stdio functions, filesystem code including Unicode support and the C++ exception handling runtime, all of which can be disabled to significantly [[Miosix code size optimization|reduce code size]].
If you correctly flashed the board, the LED will start blinking.


'''Programming'''
== Accessing the serial port ==


There are two ways to program the stm32f4discovery board. One is to use QSTLink2 directly from the DOS prompt you already have open by typing 'make program' (connect the USB cable first!), the other is through QSTLink2's GUI. You can find QSTLink2 in the start menu
The USB to serial port adapter will appear on Windows as a randomly numbered COM port. To figure out the right number, you'll have to open ''Devices'' from the control panel:


[[File:Qstlink2startmenuwindows.png]]
[[File:Windows-list-devices.png]]


Once you start it, you have to click on ''Connect'', and it should find your ''stm32f4discovery'' board. After that, click on ''Send'' and select the ''main.bin'' file in the Miosix top-level directory.
Today, on my computer, Windows decided to assign the STM32 board to COM3.


[[File:Qstlink2flashingwindows.png]]
Next up, open PuTTY and configure it to open the right COM port. Starting from Miosix v3.00, all boards use by default a baud rate of 115200bit/s, so input 115200 in the ''Speed'' field:


Note that, regardless of using QSTLink2 form the DOS prompt or the GUI, there is a bug that in some circumstances blocks the microcontroller until the next powercycle. Therefore, after having programmed the microcontroller, it is recomended to unplug and reconnect the USB cable to powercycle the ''stm32f4discovery'' board. At that point, you shuold see the red LED blinking.
[[File:Windows-putty-configuration.png]]


=== What's next? ===
Finally, click ''Open'' and you should see the serial port messages from the microcontroller. Since the board will be already running when you connect, you can press the RESET button on the board to reboot it and see the boot logs:


You have finished the installation of the Miosix Toolchain. You may want to [[Quick start#IDE Configuration|install and configure an IDE]], or the [[Quick start#In-circuit debugger|debugger]].
[[File:Windows-putty-serial.png]]


=== Uninstall the Miosix Toolchain ===
= A note on uninstalling software =


To uninstall the Miosix Toolchain, you can find the uninstaller in the start menu under ''Miosix Toolchain''. Note that Git, Perl and the STLink drivers have their own uninstallers. Also, the ''miosix-kernel'' in the ''Documents'' directory will not be removed.
To uninstall the Miosix Toolchain, you can find the uninstaller in the start menu under ''Miosix Toolchain''. Note that the other tools such as Git, Perl and the STLink utility have their own uninstallers.


[[Category:Installation and Configuration]]
[[Category:Installation and Configuration]]

Latest revision as of 21:03, 10 May 2026

Installing the required software

Miosix Toolchain

To be able to compile the Miosix kernel and your application, you need a patched version of the GCC compiler called Miosix toolchain, you can find the link to download it here. At the end of the installation you will need to reboot your computer (sorry, it's Windows...)

Git

To download the Miosix kernel and keep it up to date, you need the git version control system.

It is recommended to install it from git-scm.com. Please do not uncheck the Windows Explorer integration feature during the installation, as you will need it to install the kernel sources.

Perl

The Perl programming language is used by the Miosix build system. Unlike on GNU/Linux computers where Perl is pre-installed, for Windows you'll need to install it separately.

Strawberry perl is the recomended Perl version for Miosix on Windows.

Flashing tool

With the installed Miosix toolchain you'll be able to compile the kernel, but you'll also need a way to transfer it to your microcontroller. This operation is usually called 'flashing' the microcontroller. Miosix can work with any flashing utility that accepts as input raw binary files, and on Windows you'll usually want to install the flashing tool provided by your chip vendor.

For STM32 chips, you can use the STLink utility available here.

Text editor or IDE

It is recommended to download a better text editor than Notepad, as you will need it to edit the Miosix configuration files. Notepad++ is a good option, but many other options exist. Just don't use Notepad, because it does not recognize Unix line-endings and will show you Miosix source files as if they were composed of a single line of text.

As an alternative to Notepad++, you may want to use an IDE.

Serial port viewer

Miosix redirects stdin/stdout to a serial port by default on most boards, so to see the boot log and the output of printf() in your application you need a program to interact with the serial port. PuTTY is an option, although old fashioned. TODO: There are likely better options for Windows too.

Downloading and configuring the Miosix kernel

This section is a stripped down guide with the bare minimum to get you started, we also have a full guide about Miosix and git workflow. Following the tutorial in this page, your project directory will not be under version control (git). The Miosix and git workflow page shows how to create a git repository for your project and add the Miosix kernel as a git submodule.

Before downloading the kernel, however, you first have to create a directory for your project (in this case, the hello world), and then clone the kernel as a subdirectory.

Using the Windows explorer, create a hello directory, double click on it. Then right-click on the empty directory. Select Git bash to open a git shell. This opens a shell where you can type the commands to download the kernel.

Note that it is possible to paste commands in the shell (so as to avoid typing them, which can be tedious and lead to typos), but you have to do it one line of text at a time, and to paste you need to use the Shift+Ins shortcut, not the usual Ctrl+V one. The last command, exit, will close the shell.

git clone https://github.com/fedetft/miosix-kernel.git
exit

Note that there is also a Git GUI in the right click menu, but git is best used from a command line interface, you're on your own if you choose to use git from the GUI.

Initializing a Miosix project

For reasons explained in depth in the Miosix and git workflow page, you'll need to copy the Miosix configuration directory from the kernel sources to your project directory, as well as create a template project where you can start writing code. This can be automated with a Perl script called init_project_out_of_git_tree.pl. To run it, you need to open a Windows terminal (which is a different terminal than the git bash we used before), which can be done by Shift+Right click and selecting Open command window here:

Then, type the following command:

perl miosix-kernel/tools/init_project_out_of_git_tree.pl

Leave the terminal open as you'll need it later.

You should run the script from the directory where you want to initialize your empty project, such as the hello directory in this tutorial. A successful run of the script produces the following output:

Successfully created Miosix project
Target directory: hello
Kernel directory: hello/miosix-kernel/miosix

Your project directory should now look like the following picture, with the miosix-kernel directory containing the kernel sources, the config directory with the kernel configuration, the empty main.cpp and the build files for the two alternative build systems supported by Miosix: Makefile and CMake:

For this tutorial, we'll use the Makefile build system, have a look at the CMake page to learn how to use CMake instead.

Configuring the kernel

The minimum configuration required to compile the kernel is to select a board, as the kernel needs to build the appropriate board support package for it.

The Miosix kernel has several other build-time configuration options, to enable optional components (filesystem, userspace processes), select algorithms such as the scheduler and trade feature support for code size, but these won't be covered here. The kernel is configured by editing files in the config directory, the two most important ones are Makefile.inc and miosix_settings.h.

To select a board, using Notepad++ open the first one, config/Makefile.inc. Look for the OPT_BOARD section of the file, which contains the list of boards officially supported by the kernel. In this section, all boards appear commented out (the # sign is a comment in Makefile syntax), so you need to uncomment the line corrsponding to your board. Porting the kernel to a new board is an advanced topic not covered here, so we'll assume you'll want to use an officially supported board. As an example, in this tutorial we'll use the stm32f746zg_nucleo board:

...
#OPT_BOARD := stm32f469ni_stm32f469i-disco
OPT_BOARD := stm32f746zg_nucleo
#OPT_BOARD := stm32f765ii_marco_ram_board
...

If you do not yet have a board, you can follow along and get to the compiled firmware anyway. If you're looking for suggestions on which board to get, consider that most boards currently supported by Miosix are for STM32 microcontrollers. Here's a basic guide:

  • Nucleo STM32 boards produced by ST are good for beginners, as they allow to power the board, flash it, debug it and access the serial port all through a single USB connection with your computer. The stm32f401re_nucleo and stm32f411re_nucleo are realtively low cost.
  • Discovery STM32 boards are also good, but they don't include an USB to serial adapter so you'll have to buy it separately and you'll end up having to connect two USB cables, one for power/programming/debugging and one for the serial port.
  • Third party STM32 boards such as the stm32f411ce_blackpill are cheaper and great to embed in your hardware project, but you'll have to [solder] the pin header yourself, and you'll need to attach a separate USB to serial adapter, and if you want in-circuit debugging you'll need an STLink adapter, so you'll end up having to run up to three separate USB cables from your computer to the board for power/programming, serial port and debugging.
  • Venturing outside of STM32, the RP2040 is a good choice. Also in this case, you'll need a separate USB to serial adapter and some soldering skills, though.

Writing your first Miosix program

Hello world

With Notepad++ open the main.cpp file with your IDE or text editor of choice, and replace its content with the following program:

#include <cstdio>
#include <thread>
#include <miosix.h>
#include <interfaces/bsp.h>

using namespace std;
using namespace miosix;

int main()
{
    for(;;)
    {
        iprintf("Hello world\n");
        ledOn();
        this_thread::sleep_for(500ms);
        ledOff();
        this_thread::sleep_for(500ms);
    }
}

At this point, it's good to have a quick walkthrough of this hello world program. Miosix is based on a Fluid kernel architecture: in a nutshell it's a kernel that supports writing applications both inside the kernel, and inside userspace programs. In this tutorial, we're using it as a unikernel. This means when power is applied to the board, the kernel will boot, and then start running the main() function you just wrote in kernelspace. This means you have both support for C/C++ standard libraries and unrestricted hardware access.

This main() function will print "Hello world" and blink an LED approximately every second. The printing is done with the iprintf() function. If you expected just printf() without the i, the reason is that Miosix uses the Newlib C standard library that provides both printf() and iprintf(), an optimized version that is smaller in code size but incapable of printing floating point numbers. So you can also use printf() if you wish, but this optimization is worth knowing from the start.

Sleeping to blink the LED is done with the sleep_for() function from the C++ standard library. Miosix supports other sleeping APIs, such as nanosleep() from POSIX, or Thread::sleep(), a native Miosix function. All these functions do the same thing: they tell the scheduler to stop the currently running thread, run some other thread, and resume execution after a given time interval. Miosix is a preemptive multithreading operating system, so you should get comfortable with the fact that the code you're writing lives in a thread and shares the CPU with other threads.

Many (but not all!) board support packages define the ledOn() and ledOff() functions to control one of the LED on the board. This should be true for all the boards that have at least one software-accessible LED. This is true for the stm32f746zg_nucleo board we selected, but if you selected a different board with no LEDs, then compiling will fail with an error related to ledOn() and ledOff(). You can comment out those two lines in this case, your hello world will only print to the serial port.

Compiling

In the same terminal you used to run Perl, type make to build the kernel.

make

If all goes well, the result should look like this.

Otherwise, compiler errors will appear in the shell. The number that appears under text in the make output is the size in bytes of your application plus the kernel. Don't worry about the 42KBytes code size, you're compiling with the default Miosix options, if you want you can play around with the options later to reduce the code size.

Flashing

The procedure to flash the microcontroller board on Windows depends on the vendor-provided flashing tool. They usually provide a GUI where you can load the .bin file and then connect to the board and flash it. For Miosix the file is named main.bin if you're not using processes, or image.bin if you are using processes. For this tutorial, look for main.bin in the hello directory.

If you correctly flashed the board, the LED will start blinking.

Accessing the serial port

The USB to serial port adapter will appear on Windows as a randomly numbered COM port. To figure out the right number, you'll have to open Devices from the control panel:

Today, on my computer, Windows decided to assign the STM32 board to COM3.

Next up, open PuTTY and configure it to open the right COM port. Starting from Miosix v3.00, all boards use by default a baud rate of 115200bit/s, so input 115200 in the Speed field:

Finally, click Open and you should see the serial port messages from the microcontroller. Since the board will be already running when you connect, you can press the RESET button on the board to reboot it and see the boot logs:

A note on uninstalling software

To uninstall the Miosix Toolchain, you can find the uninstaller in the start menu under Miosix Toolchain. Note that the other tools such as Git, Perl and the STLink utility have their own uninstallers.