More ChibiOS

I’m really enjoying ChibiOS.  The hardware abstraction layer is beautiful and the OS is rich, fast, and compact.  I am using it for a wireless IP gateway application.  I’m doing some pretty low-level stuff (interrupt driven SPI communications with a transceiver) and I have not had to write any processor-specific code!  I’m still getting to know ChibiOS, but I already have an application running on development boards that is communicating via the wireless transceiver, pulling an IP address via DHCP, serving a web page and a command shell via TCP, and also a separate serial command shell.  It fits in 80K.  By the end of next week, I hope to have a first cut of fully operational application software and move on to the custom hardware design.


Using NetBeans and Gnu ARM tools, I was able to build and run the ChibiOS/RT demos for the STM32F4 discovery board.  I downloaded the zip file containing the latest version of ChibiOS/RT and extracted it to my NetBeansProjects folder.  To build a project:

  • File -> New Project -> C/C++ -> C/C++ Project with Existing Sources
  • Browse to the appropriate directory e.g:
    • …./NetBeansProjects/ChibiOS_2.6.7/demos/ARMCM4-STM32F407-DISCOVERY
  • Select Tool Collection as GNU ARM
  • Select Automatic Configuration Mode

That’s it, the project is imported and automatically builds!  You can load it onto the target using st-flash and debug it (if desired) using st-util:

  • st-flash write \Users\dalbert\Documents\NetBeansProjects\ChibiOS_2.6.7\demos\ARMCM4-STM32F407-DISCOVERY\build\ch.bin 0x8000000
  • st-util  (make sure you’ve configured build/ch.elf as the project Properties->Build->Make->Build Result)

I’ve built and tested the three STM32F4-DISCOVERY demos and they all ran successfully including the VCP USB comm port with command interpreter in the MEMS demo.

I’ve also built the ChibiOS/RT + lwIP + PPP application here which required a few fixes and must be linked with ChibiOS/RT 3.x but then runs nicely using the serial port (PA2, PA3) to serve a PPP connection.  I have two directories under NetBeansProjects:
Then I create a symlink under stm32f4-chibi-…-master/lib called ChibiOS-RT that links to ../../ChibiOS_3.0.x.

On the remote terminal (linux box) side, I configured /etc/ppp/options including the serial port I was connected to (/dev/ttyUSB0) and the baudrate (38400); the whole file looks like this:

# Prevent pppd from forking into the background
# wait for connection if none initially found
# no hardware flow control
# don’t use modem control lines
# create a default route for this connection in the routing table
# no authorization required
# ensure exclusive access to the serial device
# do not set up any escaped control sequences
asyncmap 0
# use a maximum transmission packet size of 552 bytes
mtu 552
# use a maximum receive packet size of 552 bytes
mru 552
# IP stack configuration

Launch pppd specifying localip:remoteip: which generate the ppp0 network interface (shows on ifconfig):

sudo pppd

Reset the discovery board and the PPP connection is up!  You can ping the discovery board (ping and can bring up a remote shell connection using netcat: nc 25  (note to send a CR requires Ctrl-V <enter><enter>).


Cortex Cross Development Environment

I’ve been using ARM processors with C/C++ for more than a decade for nearly all of my embedded development.  I’ve usually used Em::Blocks on Windows platforms which works nicely but doesn’t fully work on linux, doesn’t support integrated version control, and hides some details that I want to have control over.

I’m working on some new projects using Cortex M3 and M4 processors and wanted to explore the available tools.  My favorite IDE is still NetBeans.  Netbeans was meant for Java, but has a C/C++ plugin and is fairly easy to configure for gcc cross-compilation using gnu tools for arm (linux/mac/windows).  I’d really like to get the environment working both on windows and linux since I routinely use both.  This post details the linux setup:

1) Download and install the GNU tools for ARM in /usr/local/gcc-arm-none-eabi-…:

cd /usr/local
sudo tar xjf ~dalbert/Downloads/gcc-arm-none-eabi-*.bz2

Add the tools to the search path (add the following to ~/.bashrc):


2) Install the C++ plugin for Netbeans and configure NetBeans to use add the new arm cross-compilation toolchain:

Tools->Options->C/C++->Add the configure the base directory (e.g. /usr/local/gcc-arm-none-eabi-4_9-2014q4/bin) and the c, c++, assembler, and debugger programs to arm-none-eabi-gcc, arm-none-eabi-g++, arm-none-eabi-as, arm-none-eabi-gdb.  I add the cross tools as GNU_ARM

3) Install the GDB Server plugin for Netbeans.  This allows you to attach NetBeans debugger to the remote target using the configured gdb debugger (arm-none-eabi-gdb per the above step).  To debug, Debug->Attach Debugger, choose debugger type as “gdbserver”, choose target as “remote:4242”, and set the project to your project.

4) For hardware assisted debugging, the STMlinkv2 software works with the STLink USB JTAG/SWD debug hardware ($30 from Digikey and included free with any ST Discovery board; I like this one that costs $10 at Digikey); documentation is here.

Install texane stmlinkv2 (for linux, install the packages required to build stmlinkv2: autotools autoconf autogen libusb-1.0.0 and maybe libusb-1.0.0-dev, intltool) see here and here for more info.  Windows binaries are here.  Included is st-flash for flashing the processors, st-info, st-term, and st-util: a GDB server.  For Windows, just unzip the folder to a convenient location and run st-util.  For linux, after installing the dependencies (listed above), build STMLinkV2 and install it (installs in /usr/local/bin):

sudo make install

Grant permissions for the tools (e.g. st-util) to access the USB ST-Link V2 hardware:

sudo install -m 644 49-stlinkv2.rules /etc/udev/rules.d/49-stlinkv2.rules
sudo udevadm control –reload-rules

More info on permissions for st-util

5) Download sample code and create a project for one of the examples.  I keep my NetBeans projects in my home directory in a NetBeansProjects directory:

File->New Project->C/C++ Project with Existing Sources->~/NetBeansProjects/kkstm32_base-master/example/32l_lcd
Set the Tool Collection to GNU_ARM and select Automatic configuration mode then press finish.
Note: the STM32 library in the downloaded example code I’ve linked to contains an error in core_cm3.c that must be fixed (add the ‘&’ in front of result r); see diffs below:

dalbert@mypc ~/NetBeansProjects/kkstm32_base-master/libs_stm/inc/core_support $ diff core_cm3.c.orig core_cm3.c
<    __ASM volatile (“strexb %0, %2, [%1]” : “=r” (result) : “r” (addr), “r” (value) );

>    __ASM volatile (“strexb %0, %2, [%1]” : “=&r” (result) : “r” (addr), “r” (value) );
<    __ASM volatile (“strexh %0, %2, [%1]” : “=r” (result) : “r” (addr), “r” (value) );

>    __ASM volatile (“strexh %0, %2, [%1]” : “=&r” (result) : “r” (addr), “r” (value) );

cd kkstm32_base-master/libs_stm/build
make     (to build the STM32 peripheral library)
cd kkstm32_base-master/example/32l_lcd
make     (to build the example code – builds lcd.elf, lcd.bin)

6) Build and load one of the examples built above into your STM32L Discovery eval board using the CLI first to confirm that everything is working:

st-flash write lcd.bin 0x8000000
test the other nifty st tools: st-info, st-term, st-util

7) Test debug interface from CLI:

st-util   (runs gdb-server which listens on localhost:4242)
arm-none-eabi-gdb -q lcd.elf
(gdb) target remote localhost:4242

8) Optionally install openocd (gdb server compatible) and test it for debugging:

sudo apt-get install openocd       (installs to /usr/share/openocd)
openocd –version
sudo openocd -f /usr/share/openocd/scripts/interface/stlink-v2.cfg -f /usr/share/openocd/scripts/target/stm32lx_stlink.cfg
in another window: telnet localhost 4444

9) Import one of the example projects into NetBeans and test operation in the IDE!

File->New Project->C/C++->C/C++ Project with Existing Sources
Choose source folder and GNU_ARM tool collection
Select Automatic Configuration Mode
Right click the project->Properties->Build->Make->Build Result = build/ch.elf

Make sure the Makefile is compiling the project with debugging enabled (-g)

Other links related to setting up a gnu/arm cross development environment:

  •   (wiki for the stlink tools)
  •  (detailed discussion of stlink tools)
  •  (very detailed article on eclipse, openocd, and a wide range of cross tools)
  • LibOpenCM3
  •   (STM32 peripheral library)

Next steps: