Table of Contents
Running PX4 Firmware on the STM Discovery Development Kit
This guide provides all necessary steps to get the PX4 FMU firmware up and running on an STM32F4 Discovery development kit (shown below), and connecting to the NuttX terminal via UART. This process was developed and verified on a Windows 7 machine with 64-bit architecture, however, the same process should work on other versions of Windows. Though some of the software works well on both Linux and OSX, the ST-LINK tool used to flash the PX4 bootloader to the STM Discovery is Windows only. Though it may be possible to flash the bootloader without ST-LINK, you must have a Windows machine for this particular process.
The following hardware is required to get the firmware running, and to connect to the NuttX terminal via UART on the Discovery development board:
- STM32F4DISCOVERY development kit
- USB mini cable
- USB micro cable
- USB to serial device (FTDI) or other serial connection capable of 3.3V logic levels
IMPORTANT: Ensure that the serial connection/hardware setup you are using uses 3.3V levels on the Discovery board side. Many serial (RS232) ports/devices use a voltage level that is too high (such as -12V to +12V) and can damage the chip.
You will need to download and install the following software:
- PX4FMU & PX4FLOW drivers (wait until after the PX4 bootloader has been flashed to install this)
Additionally, Eclipse (included with the PX4 Toolchain installation) requires having Java installed. Also, if you prefer having a GUI for interactions with the repository, Git or TortoiseGIT are both viable options, but not required.
Building the PX4 Firmware
This step will prepare the PX4 firmware to be flashed to the Discovery board. You won't need any hardware for this step, but you must download and install the PX4 tool chain (PX4 Toolchain Installer v0.14) before you can build the Firmware.
- First, you will need to get a fresh copy of the source files. To do this find PX4 Toolchain»PX4 Software Download under the Windows Start menu. This process may take a few minutes, depending on connection. Once completed, click the OK on the popup menu indicating that this process only needs to be ran the first time.
- Now, open the PX4 console by clicking PX4 Toolchain»PX4 Console in the Windows Start menu. It should begin in the top directory of PX4 (C:\px4). Change to the firmware directory cd Firmware
- Initialize the submodules - this step is only required once:
git submodule init
- Update the submodules: This is required after each pull from master:
git submodule update
- After the submodule files have been cloned, it's time to build the project. To do this, type make archives. This process generally takes a few minutes to complete.
When the build process has finished, the firmware is ready for uploading to the Discovery board via the PX4 bootloader. You can leave the console open, as you will need it for uploading the firmware in a few minutes.
Flashing the PX4 Bootloader
The following steps will guide you through the process of flashing the PX4 bootloader (Discovery version) to the STM Discovery board. For this, you will need the USB mini cable, USB micro cable, and the Discovery board.
- First, connect the STM Discovery to your computer using the USB mini cable. If you installed the ST-LINK drivers correctly, it should show up in Device Manager under Universal Serial Bus controllers.
- Open STM32 ST-LINK Utility, found under STMicroelectronics in the start menu, and click Target»Connect (alternatively, there are shortcut buttons for connecting, disconnecting, erasing, and etc.). The Discovery board will connect and memory contents will be displayed starting at address 0x08000000. If the board does not connect, try a different USB port and/or double check driver installation.
- It is recommended that you erase the chip upon flashing the bootloader. Do this by going to Target»Erase Chip.
- Once the chip has been erased, it's time to flash the PX4 bootloader. Click Target»Program & Verify and enter the file path for the bootloader binary file “px4discovery_bl.bin”. This file will be located in the Bootloader directory in the PX4 Toolchain installation directory. By default, this would be C:\px4\Bootloader. Once the file path has been selected, click Start.
- The flashing process will verify the program as it is stored to memory and will print the status in the STM32 ST-LINK Utility. If the option to reset the board upon finishing was selected, you should now see an orange LED blinking next to the STM32F407 chip on the development kit. If not, press the black reset button and verify that the LED is blinking. You can now disconnect the device by Target»Disconnect , and close STM32 ST-LINK Utility.
- Now plug in the USB micro cable to the end of the Discovery board opposite the USB mini port (leave the USB mini cable connected for device power). Open Windows Device Manager and find the unknown device. From here, right click for properties and update/install the driver for the PX4 FMU. You will need to point Windows to the location you downloaded the PX4FMU & PX4FLOW drivers earlier. Once the drivers have finished installing, the Discovery board will show up as a PX4 FMU COM port in Device Manager.
Uploading the PX4 Firmware
To upload the firmware to the discovery board you will be using the PX4 Console, the Discovery board, and both the USB mini and micro cables.
- Connect the Discovery board via USB mini, but leave the USB micro cable disconnected for now (however, do keep the USB micro cable handy).
- Open PX4 Console (if you closed it after building the firmware) and navigate to the Firmware directory cd Firmware. Here, type make px4-stm32f4discovery_default upload. This will take a few seconds to complete.
- When the upload build is finished running, it will begin searching the COM ports for the PX4 FMU. Now plug in the USB micro cable to the STM Discovery board. The upload process will recognize the board and begin programming and verifying the firmware.
Connecting to the NuttX Terminal via UART
The final step is to connect to the NuttX terminal via UART. Please recall that you MUST use a UART device that interfaces with the STM Discovery board at 3.3V levels, typical UART operation voltages are too high. Using a device such as an FTDI serial to USB (again, interfaces at 3.3V) or an equivalent level shifter will suffice. The NuttX terminal communicates through USART 2 (pins PA2/PA3 for TX/RX) on the STM Discovery board. The Discovery board will need both the USB mini and USB micro cables for this setup, though, keep them unplugged while physically connecting to the UART pins.
- Connect to the STM Discovery board via pin PA2 for USART 2 TX, and pin PA3 for USART 2 RX. Keep in mind that you may have to swap these depending on the device you are using to interface with the PC. Discovery TX will connect to PC RX, and Discovery RX will connect to PC TX (through the appropriate hardware). You may now plug in the USB mini and micro cables, or wait until after the next step.
- The PX4 Toolchain comes with a useful terminal application called Tera Term. It can be found in the Windows Start menu under PX4 Toolchain»Tera Term. Once Tera Term is open, select Setup»Serial port… and select the correct COM port (you may have to check Device Manager to verify). Set the serial connection to 57600 baud, 8 bit, no parity, and 1 stop bit (57600 8N1). Click OK to connect.
* If you haven't already connected the USB mini and micro cables, do so now. The board will power on and the orange LED will flash for a few seconds (starts in bootloader). After a few seconds the NuttX terminal will appear. If you've already powered on the device before setting up the connection, simply hit return to bring up the nsh> prompt, or press the reset button on the STM Discovery board.
- You can type help to bring up the terminal help listing
- You can type ver all to list HW and Software Version
* You can also get an Nuttx Terminal on USART6, PC6 (TX) and PC7 (RX). You have to write “nshterm /dev/ttyS1” in the Nuttx Terminal and then change your Cables to the USART6 pins.
* To get back to USART2 write “nshterm /dev/ttyS0” in the Nuttx Terminal and change your Cables back to the USART2 pins.
* You could also get an Nuttx Terminal over the USB Bootloader Port. You have to write “nshterm /dev/ttyACM0” in the Nuttx Terminal, open a second teraterm and connect to the Bootlaoder USB Comport (you may have to check Device Manager to verify).
* You could do also some “tests”, but most will fail. One who works the following:
change your Nuttx Terminal to the USB Bootloader Port,
write “tests uart_baudchange” on the Nuttx Terminal which will write to your USART6
the following string “UART2 #” 100 times
Congratulations! The PX4 firmware is now running on your STM32F4 Discovery kit.
Debugging PX4 using OpenOCD and Eclipse
You can also debug the PX4 firmware using OpenOCD and Eclipse so you can step through your code and see variables and all that.
Close the ST-LINK utility to free up the port so that it can be used by OpenOCD. Then Install OpenOCD from https://github.com/gnuarmeclipse/openocd/releases.
Unfortunately the VID/PID pair they have for this board is wrong, so edit this file: “%ProgramFiles%\GNU ARM Eclipse\OpenOCD\0.10.0-201601101000-dev\scripts\interface\stlink-v2.cfg”
It looks like it might even be a typo, change the 8 to a B on this line:
- hla_vid_pid 0x0483 0x3748
So it reads:
- hla_vid_pid 0x0483 0x374B
Now open a new command prompt and (make sure ~\px4\toolchain\bin is in your PATH) because OpenOcd uses gdb.exe which lives there (see http://dev.px4.io/starting-installing-windows.html for instructions on installing the PX4 toolchain).
- OpenOcd -f “%PROGRAMFILES%\GNU ARM Eclipse\OpenOCD\0.10.0-201601101000-dev\scripts\board\stm32f4discovery.cfg”
You should see this output which means it is ready for business:
When you are done debugging, just press CTRL+C to stop it and it will release the COM Port so that you can use ST-LINK utility again to reflash the board.
Next step is setting up eclipse to use OpenOCD, which is more involved…
Debugging with Eclipse
Launch the PX4 Eclipse project (using ~\px4\toolchain\msys\1.0\px4_eclipse.bat) from the px4 toolchain.
- Open the Run menu and select “Debug Configurations”.
- Select “GDB Hardware Debugging” and click the “+” button to create a new debug configuration.
- Enter the full path of the firmware_nuttx elf file into the “C/C++ Application:” field, it should be someplace like this:
~\px4\src\Firmware\build_px4-stm32f4discovery_default\src\firmware\nuttx\firmware_nuttx(namely, the output of the build for the px4-stm32f4discovery_default configuration).
- Click Debugger tab, change “gdb” command to “arm-none-eabi-gdb” and change the port number to 3333.
- Click the Startup tab, and enter this string into the text box:
*monitor reset halt*
- Click the “Common” tab and check the “Debug” checkbox and click the “Select other…” link at the bottom of this page.
- Click on the “Change Workspace Settings” link, find the GDB Hardware Debugging Launch Type and select “Standard GDB Hardware Debugging Launcher” like this:
- Then Apply/OK your way out of all that and you should see a new option under the Debug toolbar item:
If you have already flashed the board using ST-LINK, and if you started OpenOCD correctly, then this will launch the debugger and stop you on the first line of code in the NuttX chip specific start routine, void __start(void) in stm32_start.c.
You should see some spew in the openocd window as you step through the code, that is normal. You can also configure the debugger to “skip” doing a Build from eclipse and that way compile and flash from command line, and only use eclipse to do debugging.