How to run NuttX on STM32 Blue Pill ?

Board image

Introduction

I will show you how I installed NuttX on my STM32. The tutorial above has been done for some time and some paths have been changed. I'm going to show them and hope that some mistakes I made can help you configure and install NuttX. First of all, see some important resume after to perform some important steps (it took me two days to discover this error).

You can see the tutorial created by Alan C. Assis on youtube Youtube to remove some doubts.

What is Nuttx ?

Apache NuttX (Incubating) is a real-time operating system (RTOS) with an emphasis on standards compliance and small footprint. Scalable from 8-bit to 32-bit microcontroller environments, the primary governing standards in NuttX are POSIX and ANSI standards. Additional standard APIs from Unix and other common RTOSs (such as VxWorks) are adopted for functionality not available under these standards, or for functionality that is not appropriate for deeply-embedded environments (such as fork()).

What is a STM32 Blue Pill ?

It's a ARM 32-bits Cortex M3, running 72MHz, 64KB or 128KB flash memory and 20KB SRAM. Power supply in 3V. The STM32F103C8 is powerfull and cheap board compared to others like Arduino Uno , for example.

Table of Content

  1. What you need ?
  2. How to setup this components ?
  3. Install the Openocd
  4. Compiling the NuttX OS
    1. Install Kfrontends (if you do not have)
    2. Compliting
  5. Loading the binary
  6. Frequent Errors

What you will need (Hardware) ?

  1. STM32F103C8T6 Board image

  2. ST-LINK

  3. USB to TTL Serial Cable

  4. PC running Linux Linux image penguim

How to setup this components ?

Please, make sure that connect the STM32 pin A9 to RXD USB to TTL Serial Cable and STM32 pin A10 to TX USB to TTL Serial Cable, ground to STM32 ground and +5V to STM32 5V.

After do it, connect :

At the end we'll have something like this:

Now, connect the STLINK and USB serial Cable on USB Port.
Check if it's everything ok through USB port using the command:

$ lsusb  

The output should be like the below image, with USB STLINK is connected :

Install the Openocd

Open the linux terminal and execute the following commands for install the openocd:

  1. $ sudo apt-get install automake bison build-essential flex gcc-arm-none-eabi gperf git libncurses5-dev libtool libusb-dev libusb-1.0.0-dev pkg-config minicom
  2. $ mkdir ~/nuttxspace
  3. $ cd ~/nuttxspace 
  4. $ cd ~/nuttxspace/openocd
  5. $./bootstrap
  6. $ ./configure --enable-internal-jimtcl --enable-maintainer-mode --disable-werror --disable-shared --enable-stlink --enable-jlink --enable-rlink --enable-vslink --enable-ti-icdi --enable-remote-bitbang
  7. $ make -j4
  8. $ sudo make install
  9. $ cd ..

Compiling the NuttX OS

Install Kfrontends (if you do not have)

I didn't need to install kfrotends, but try to install it in case you need to install.
Now try:

  1. $  sudo apt-get install kconfig-frontends 

For compiling the Nuttx OS execute the commands below :

  1. $ git clone https://github.com/apache/incubator-nuttx.git nuttx
  2. $ git clone https://github.com/apache/incubator-nuttx-apps.git apps

Compiling

If there was no error when executing the previous commands, run :

  1. $ git clone https://bitbucket.org/nuttx/tools.git
  2. $ cd ~/nuttxspace/tools/kconfig-frontends
  3. $ ./configure --enable-mconf
  4. $ make -j4

    If occours the error WARNING: 'aclocal-1.15' is missing on your system after run step 4:

    Run the following command:

    $ autoreconf -f -i ; make -j4
  5. $ sudo make install
  6. $ sudo ldconfig 
  7. $ cd .. 

Preparing the binary:

  1. $ cd ~/nuttxspace/nuttx/tools  
  2. $ ./configure.sh stm32f103-minimum/nsh 
  3. $ cd .. 
  4. This step you don't need edit nothing. But, we can if you want. Just select exit.

     Menu config nuttx
    $ make menuconfig
  5. $ make -j4 
  6. $ ls -l nuttx.bin  
  7. $ cd ..

Load the binary at STM32:

  1. $ cd openocd/contrib/ 
  2. $ sudo cp 60-openocd.rules /etc/udev/rules.d/ 
  3. $ sudo udevadm trigger 
  4. $ cd ../..
  5. $ cd nuttx 
  6. $ openocd -f interface/stlink-v2.cfg -f target/stm32f1x.cfg  
  7.  Stop the execution using Ctrl + C  
  8. Now flash the binary:

    $ openocd -f interface/stlink-v2.cfg -f target/stm32f1x.cfg -c init -c "reset halt" -c "flash write_image erase nuttx.bin 0x08000000"
  9.  Stop the execution using Ctrl + C  
  10. Checking the serial port, searching for something like ttyUSB0:

    log dmesg

  11. Set serial port on minicom:

    $ sudo minicom -s 

    log dmesg log dmesg

  12. Save as default:

    log dmesg

  13. Press the reset button on STM32 and see NuttX shell using minicom:

    $ sudo minicom 

Frequent errors:

In case errors like:

Info : STLINK V2J29S7 (API v2) VID:PID 0483:3748
Info : Target voltage: 3.164341
Warn : UNEXPECTED idcode: 0x1ba01477
Error: expected 1 of 1: 0x2ba01477

If this error occour, you should create a new .cfg:

There's a lot of fake boards on the market and you can get one of them. The error above, it's caused by UNEXPECTED idcode: 0x1ba01477 .

  1. Go to the folder and create new file based on stm32f1x.cfg. In this case I called stm32f1x_fake.cfg.

    $ cat  ~/nuttxspace/openocd/tcl/target/stm32f1x.cfg > ~/nuttxspace/openocd/tcl/target/stm32f1x_fake.cfg
  2. Now, open the file and replace the idcode 0x2ba01477 by 0x1ba01477 . I prefer open the file using Vim editor.

    $ vim ~/nuttxspace/openocd/tcl/target/stm32f1x_fake.cfg
  3. Edit the file by changing the CPU_ID:

    Now, run the following commands again:

  4. $  cd ~/nuttxspace/openocd 
  5. $ ./bootstrap 
  6. $ ./configure --enable-internal-jimtcl --enable-maintainer-mode --disable-werror --disable-shared --enable-stlink --enable-jlink --enable-rlink --enable-vslink --enable-ti-icdi --enable-remote-bitbang
  7. $ make -j4
  8. $ sudo make install 
  9. Now try flash the binary again:

  10. $ cd ~/nuttxspace/nuttx/ 
  11. $ openocd -f interface/stlink-v2.cfg -f target/stm32f1x_fake.cfg   
  12.  Stop the execution using Ctrl + C  
  13. $ openocd -f interface/stlink-v2.cfg -f target/stm32f1x_fake.cfg -c init -c "reset halt" -c "flash write_image erase nuttx.bin 0x08000000"

At this point, just go to minicom and execute the needed steps.