Skip to content

The firmware running on a ST microcontroller for interfacing with the DWM1000 UWB modules.

Notifications You must be signed in to change notification settings

shalabyma/uwb_firmware

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

UWB Module Firmware

A nice tutorial on getting started with STM32 and Eclipse, created by Mohammed Shalaby, can be found here.

Historically, and as also covered in that tutorial, development on an STM32 chip consists of first configuring the chip according to your specific PCB in CubeMX, followed by setting up a configuration within the Eclipse IDE to build and debug the project. However, it turns out that Eclipse is simply just generating a makefile and running a make command to build the project. To upload and debug, the fundamental tool involved is actually openocd, and GDB is the debugger.

In this branch, we will be using all those tools directly. That is, we will build, upload, and debug the code without involving any editor, doing it all through the terminal. Then, we can use any editor we want to view and edit the code, as well as getting it to run the terminal commands for us. We will still be using CubeMX to generate the HAL code, and as it turns out, the makefile as well!

The benefits of this editor-independent approach consist of a much more fundamental understanding of what is happening, as well as the ability for each developer to use whatever IDE they want on the same code base.

If you would like to start from the absolute beginning, switch to the blank branch, which contains nothing other than the config_stm32f4.ioc file as well as this README.

In order to program and debug directly on the chip, we need to use the ST-LINK interface provided by our Discovery board. There are two ST-LINK versions that require two different sets of commands, both of which are provided in this document. In order to identify which version your board is running, I would recommend trying out both and identifying which version works, then running the commands associated with that version.

Generating the starter code with CubeMX

Assuming you are on the blank branch, you will have only the following in your directory

uwb_firmware
β”œβ”€β”€ config_stm32f4.ioc
β”œβ”€β”€ README.md
  1. Open the CubeMX software.
  2. Use File > Load Project... to load the config_stm32f4.ioc file.
  3. Under the Project Manager tab, in the Project section, you should see a field called Toolchain / IDE. Here, you can choose Makefile!
  4. Leave everything else as-is, and click on GENERATE CODE.

This should populate the current directory with the following files.

uwb_firmware
β”œβ”€β”€ config_stm32f4.ioc
β”œβ”€β”€ config_stm32f4.xml
β”œβ”€β”€ Drivers
β”‚Β Β  β”œβ”€β”€ CMSIS
β”‚Β Β  β”‚Β Β  β”œβ”€β”€ Device
β”‚Β Β  β”‚Β Β  β”‚Β Β  └── ST
β”‚Β Β  β”‚Β Β  β”‚Β Β      └── STM32F4xx
β”‚Β Β  β”‚Β Β  β”‚Β Β          β”œβ”€β”€ Include
β”‚Β Β  β”‚Β Β  β”‚Β Β          β”‚Β Β  β”œβ”€β”€ stm32f405xx.h
β”‚Β Β  β”‚Β Β  β”‚Β Β          β”‚Β Β  β”œβ”€β”€ stm32f4xx.h
β”‚Β Β  β”‚Β Β  β”‚Β Β          β”‚Β Β  └── system_stm32f4xx.h
β”‚Β Β  β”‚Β Β  β”‚Β Β          └── Source
β”‚Β Β  β”‚Β Β  β”‚Β Β              └── Templates
β”‚Β Β  β”‚Β Β  └── Include
β”‚Β Β  β”‚Β Β      β”œβ”€β”€ cmsis_armcc.h
β”‚Β Β  β”‚Β Β      β”œβ”€β”€ cmsis_armclang.h
β”‚Β Β  β”‚Β Β      β”œβ”€β”€ cmsis_compiler.h
β”‚Β Β  β”‚Β Β      β”œβ”€β”€ cmsis_gcc.h
β”‚Β Β  β”‚Β Β      β”œβ”€β”€ cmsis_iccarm.h
β”‚Β Β  β”‚Β Β      β”œβ”€β”€ cmsis_version.h
β”‚Β Β  β”‚Β Β      β”œβ”€β”€ core_armv8mbl.h
β”‚Β Β  β”‚Β Β      β”œβ”€β”€ core_armv8mml.h
β”‚Β Β  β”‚Β Β      β”œβ”€β”€ core_cm0.h
β”‚Β Β  β”‚Β Β      β”œβ”€β”€ core_cm0plus.h
β”‚Β Β  β”‚Β Β      β”œβ”€β”€ core_cm1.h
β”‚Β Β  β”‚Β Β      β”œβ”€β”€ core_cm23.h
β”‚Β Β  β”‚Β Β      β”œβ”€β”€ core_cm33.h
β”‚Β Β  β”‚Β Β      β”œβ”€β”€ core_cm3.h
β”‚Β Β  β”‚Β Β      β”œβ”€β”€ core_cm4.h
β”‚Β Β  β”‚Β Β      β”œβ”€β”€ core_cm7.h
β”‚Β Β  β”‚Β Β      β”œβ”€β”€ core_sc000.h
β”‚Β Β  β”‚Β Β      β”œβ”€β”€ core_sc300.h
β”‚Β Β  β”‚Β Β      β”œβ”€β”€ mpu_armv7.h
β”‚Β Β  β”‚Β Β      β”œβ”€β”€ mpu_armv8.h
β”‚Β Β  β”‚Β Β      └── tz_context.h
β”‚Β Β  └── STM32F4xx_HAL_Driver
β”‚Β Β      β”œβ”€β”€ Inc
β”‚Β Β      β”‚Β Β  β”œβ”€β”€ Legacy
β”‚Β Β      β”‚Β Β  β”‚Β Β  └── stm32_hal_legacy.h
β”‚Β Β      β”‚Β Β  β”œβ”€β”€ stm32f4xx_hal_cortex.h
β”‚Β Β      β”‚Β Β  β”œβ”€β”€ stm32f4xx_hal_def.h
β”‚Β Β      β”‚Β Β  β”œβ”€β”€ stm32f4xx_hal_dma_ex.h
β”‚Β Β      β”‚Β Β  β”œβ”€β”€ stm32f4xx_hal_dma.h
β”‚Β Β      β”‚Β Β  β”œβ”€β”€ stm32f4xx_hal_exti.h
β”‚Β Β      β”‚Β Β  β”œβ”€β”€ stm32f4xx_hal_flash_ex.h
β”‚Β Β      β”‚Β Β  β”œβ”€β”€ stm32f4xx_hal_flash.h
β”‚Β Β      β”‚Β Β  β”œβ”€β”€ stm32f4xx_hal_flash_ramfunc.h
β”‚Β Β      β”‚Β Β  β”œβ”€β”€ stm32f4xx_hal_gpio_ex.h
β”‚Β Β      β”‚Β Β  β”œβ”€β”€ stm32f4xx_hal_gpio.h
β”‚Β Β      β”‚Β Β  β”œβ”€β”€ stm32f4xx_hal.h
β”‚Β Β      β”‚Β Β  β”œβ”€β”€ stm32f4xx_hal_i2c_ex.h
β”‚Β Β      β”‚Β Β  β”œβ”€β”€ stm32f4xx_hal_i2c.h
β”‚Β Β      β”‚Β Β  β”œβ”€β”€ stm32f4xx_hal_pcd_ex.h
β”‚Β Β      β”‚Β Β  β”œβ”€β”€ stm32f4xx_hal_pcd.h
β”‚Β Β      β”‚Β Β  β”œβ”€β”€ stm32f4xx_hal_pwr_ex.h
β”‚Β Β      β”‚Β Β  β”œβ”€β”€ stm32f4xx_hal_pwr.h
β”‚Β Β      β”‚Β Β  β”œβ”€β”€ stm32f4xx_hal_rcc_ex.h
β”‚Β Β      β”‚Β Β  β”œβ”€β”€ stm32f4xx_hal_rcc.h
β”‚Β Β      β”‚Β Β  β”œβ”€β”€ stm32f4xx_hal_spi.h
β”‚Β Β      β”‚Β Β  β”œβ”€β”€ stm32f4xx_hal_tim_ex.h
β”‚Β Β      β”‚Β Β  β”œβ”€β”€ stm32f4xx_hal_tim.h
β”‚Β Β      β”‚Β Β  └── stm32f4xx_ll_usb.h
β”‚Β Β      └── Src
β”‚Β Β          β”œβ”€β”€ stm32f4xx_hal.c
β”‚Β Β          β”œβ”€β”€ stm32f4xx_hal_cortex.c
β”‚Β Β          β”œβ”€β”€ stm32f4xx_hal_dma.c
β”‚Β Β          β”œβ”€β”€ stm32f4xx_hal_dma_ex.c
β”‚Β Β          β”œβ”€β”€ stm32f4xx_hal_exti.c
β”‚Β Β          β”œβ”€β”€ stm32f4xx_hal_flash.c
β”‚Β Β          β”œβ”€β”€ stm32f4xx_hal_flash_ex.c
β”‚Β Β          β”œβ”€β”€ stm32f4xx_hal_flash_ramfunc.c
β”‚Β Β          β”œβ”€β”€ stm32f4xx_hal_gpio.c
β”‚Β Β          β”œβ”€β”€ stm32f4xx_hal_i2c.c
β”‚Β Β          β”œβ”€β”€ stm32f4xx_hal_i2c_ex.c
β”‚Β Β          β”œβ”€β”€ stm32f4xx_hal_pcd.c
β”‚Β Β          β”œβ”€β”€ stm32f4xx_hal_pcd_ex.c
β”‚Β Β          β”œβ”€β”€ stm32f4xx_hal_pwr.c
β”‚Β Β          β”œβ”€β”€ stm32f4xx_hal_pwr_ex.c
β”‚Β Β          β”œβ”€β”€ stm32f4xx_hal_rcc.c
β”‚Β Β          β”œβ”€β”€ stm32f4xx_hal_rcc_ex.c
β”‚Β Β          β”œβ”€β”€ stm32f4xx_hal_spi.c
β”‚Β Β          β”œβ”€β”€ stm32f4xx_hal_tim.c
β”‚Β Β          β”œβ”€β”€ stm32f4xx_hal_tim_ex.c
β”‚Β Β          └── stm32f4xx_ll_usb.c
β”œβ”€β”€ Inc
β”‚Β Β  β”œβ”€β”€ FreeRTOSConfig.h
β”‚Β Β  β”œβ”€β”€ main.h
β”‚Β Β  β”œβ”€β”€ stm32f4xx_hal_conf.h
β”‚Β Β  β”œβ”€β”€ stm32f4xx_it.h
β”‚Β Β  β”œβ”€β”€ usbd_cdc_if.h
β”‚Β Β  β”œβ”€β”€ usbd_conf.h
β”‚Β Β  β”œβ”€β”€ usbd_desc.h
β”‚Β Β  └── usb_device.h
β”œβ”€β”€ Makefile
β”œβ”€β”€ Middlewares
β”‚Β Β  β”œβ”€β”€ ST
β”‚Β Β  β”‚Β Β  └── STM32_USB_Device_Library
β”‚Β Β  β”‚Β Β      β”œβ”€β”€ Class
β”‚Β Β  β”‚Β Β      β”‚Β Β  └── CDC
β”‚Β Β  β”‚Β Β      β”‚Β Β      β”œβ”€β”€ Inc
β”‚Β Β  β”‚Β Β      β”‚Β Β      β”‚Β Β  └── usbd_cdc.h
β”‚Β Β  β”‚Β Β      β”‚Β Β      └── Src
β”‚Β Β  β”‚Β Β      β”‚Β Β          └── usbd_cdc.c
β”‚Β Β  β”‚Β Β      └── Core
β”‚Β Β  β”‚Β Β          β”œβ”€β”€ Inc
β”‚Β Β  β”‚Β Β          β”‚Β Β  β”œβ”€β”€ usbd_core.h
β”‚Β Β  β”‚Β Β          β”‚Β Β  β”œβ”€β”€ usbd_ctlreq.h
β”‚Β Β  β”‚Β Β          β”‚Β Β  β”œβ”€β”€ usbd_def.h
β”‚Β Β  β”‚Β Β          β”‚Β Β  └── usbd_ioreq.h
β”‚Β Β  β”‚Β Β          └── Src
β”‚Β Β  β”‚Β Β              β”œβ”€β”€ usbd_core.c
β”‚Β Β  β”‚Β Β              β”œβ”€β”€ usbd_ctlreq.c
β”‚Β Β  β”‚Β Β              └── usbd_ioreq.c
β”‚Β Β  └── Third_Party
β”‚Β Β      └── FreeRTOS
β”‚Β Β          └── Source
β”‚Β Β              β”œβ”€β”€ CMSIS_RTOS
β”‚Β Β              β”‚Β Β  β”œβ”€β”€ cmsis_os.c
β”‚Β Β              β”‚Β Β  └── cmsis_os.h
β”‚Β Β              β”œβ”€β”€ croutine.c
β”‚Β Β              β”œβ”€β”€ event_groups.c
β”‚Β Β              β”œβ”€β”€ include
β”‚Β Β              β”‚Β Β  β”œβ”€β”€ atomic.h
β”‚Β Β              β”‚Β Β  β”œβ”€β”€ croutine.h
β”‚Β Β              β”‚Β Β  β”œβ”€β”€ deprecated_definitions.h
β”‚Β Β              β”‚Β Β  β”œβ”€β”€ event_groups.h
β”‚Β Β              β”‚Β Β  β”œβ”€β”€ FreeRTOS.h
β”‚Β Β              β”‚Β Β  β”œβ”€β”€ list.h
β”‚Β Β              β”‚Β Β  β”œβ”€β”€ message_buffer.h
β”‚Β Β              β”‚Β Β  β”œβ”€β”€ mpu_prototypes.h
β”‚Β Β              β”‚Β Β  β”œβ”€β”€ mpu_wrappers.h
β”‚Β Β              β”‚Β Β  β”œβ”€β”€ portable.h
β”‚Β Β              β”‚Β Β  β”œβ”€β”€ projdefs.h
β”‚Β Β              β”‚Β Β  β”œβ”€β”€ queue.h
β”‚Β Β              β”‚Β Β  β”œβ”€β”€ semphr.h
β”‚Β Β              β”‚Β Β  β”œβ”€β”€ StackMacros.h
β”‚Β Β              β”‚Β Β  β”œβ”€β”€ stack_macros.h
β”‚Β Β              β”‚Β Β  β”œβ”€β”€ stream_buffer.h
β”‚Β Β              β”‚Β Β  β”œβ”€β”€ task.h
β”‚Β Β              β”‚Β Β  └── timers.h
β”‚Β Β              β”œβ”€β”€ list.c
β”‚Β Β              β”œβ”€β”€ portable
β”‚Β Β              β”‚Β Β  β”œβ”€β”€ GCC
β”‚Β Β              β”‚Β Β  β”‚Β Β  └── ARM_CM4F
β”‚Β Β              β”‚Β Β  β”‚Β Β      β”œβ”€β”€ port.c
β”‚Β Β              β”‚Β Β  β”‚Β Β      └── portmacro.h
β”‚Β Β              β”‚Β Β  └── MemMang
β”‚Β Β              β”‚Β Β      └── heap_4.c
β”‚Β Β              β”œβ”€β”€ queue.c
β”‚Β Β              β”œβ”€β”€ stream_buffer.c
β”‚Β Β              β”œβ”€β”€ tasks.c
β”‚Β Β              └── timers.c
β”œβ”€β”€ README.md
β”œβ”€β”€ Src
β”‚Β Β  β”œβ”€β”€ freertos.c
β”‚Β Β  β”œβ”€β”€ main.c
β”‚Β Β  β”œβ”€β”€ stm32f4xx_hal_msp.c
β”‚Β Β  β”œβ”€β”€ stm32f4xx_hal_timebase_tim.c
β”‚Β Β  β”œβ”€β”€ stm32f4xx_it.c
β”‚Β Β  β”œβ”€β”€ syscalls.c
β”‚Β Β  β”œβ”€β”€ system_stm32f4xx.c
β”‚Β Β  β”œβ”€β”€ usbd_cdc_if.c
β”‚Β Β  β”œβ”€β”€ usbd_conf.c
β”‚Β Β  β”œβ”€β”€ usbd_desc.c
β”‚Β Β  └── usb_device.c
β”œβ”€β”€ startup
β”‚Β Β  └── startup_stm32f405xx.s
β”œβ”€β”€ startup_stm32f405xx.s
└── STM32F405RGTx_FLASH.ld

Notice the presence of a Makefile. Feel free to open this file and check it out.

Compiling the code

In the project directory,

make all

and thats it. This will create a ./build directory, with a bunch of stuff, including a .elf file, which is the compiled firmware that we will be uploading to our board.

Steven suggests the following very basic tutorial on using make: https://cs.colby.edu/maxwell/courses/tutorials/maketutor/.

Uploading with OpenOCD

Although OpenOCD can be downloaded explicitly, it is also possible to install it as a regular package

sudo apt-get install openocd

This will allow take care of creating a symbolic link to the openocd command, allowing us to just use the openocd command from any directory. Assuming you have built the code with make, that you are still in the uwb_firmware directory, and that the discovery board is plugged in with the appropriate jumpers removed, we can upload our firmware in one command. If your debugger is using ST-LINK V1, the command is

openocd -f board/stm32f4discovery.cfg -c "program ./build/config_stm32f4.elf verify reset exit"

If the debugger is using ST-LINK V2, then

openocd -f interface/stlink-v2-1.cfg -f target/stm32f4x.cfg -c "program ./build/config_stm32f4.elf verify reset exit"

Debugging with OpenOCD and GDB

First, make sure the board is connected by USB and start OpenOCD in V1

openocd -f board/stm32f4discovery.cfg

or in V2

openocd -f interface/stlink-v2-1.cfg -f target/stm32f4x.cfg

In a new terminal in the current uwb_firmware directory

arm-none-eabi-gdb ./build/config_stm32f4.elf

and you will enter a GDB command line. The above step assumes that you have installed the arm-none-eabi-gcc toolchain as per Mohammed's tutorial. To connect to the openocd server

(gdb) target remote localhost:3333

Then at this point you can use whatever GDB commands. You can directly load the firmware from here

(gdb) load

You can then use list to see where you are in the code, as well as continue or step. Theres a way to set breakpoints from the GDB terminal, but at this point, we will move to using the VSCode editor for debugging.

Setting up the same workflow in VS Code

At a minimum, you just need to open the uwb_firmware folder in VS Code and you can start editting the source code with some basic syntax highlighting already. However, it will probably be full of red warnings as a result of Intellisense not finding all the files. We will need to configure intellisense properly.

Configuring Intellisense

Create a ./.vscode/ subdirectory. Create a c_cpp_properties.json file and insert the following

{
	"version": 4,
	"configurations":
	[
		{	
			"name": "Linux: Embedded Development",
			"intelliSenseMode": "gcc-arm",
			"cStandard": "c99",
			"cppStandard": "c++17",
			"compilerPath": "/opt/gcc-arm-none-eabi-9-2020-q2-update/bin/arm-none-eabi-gcc"
			,
			"defines":
			[
				"USE_HAL_DRIVER", 
				"STM32F405xx"
			],
			"includePath":
			[
				"${workspaceFolder}/**",
				"/opt/gcc-arm-none-eabi-9-2020-q2-update/lib/gcc/arm-none-eabi/9.3.1/include",
				"Inc", 
				"Drivers/STM32F4xx_HAL_Driver/Inc", 
				"Drivers/STM32F4xx_HAL_Driver/Inc/Legacy", 
				"Middlewares/Third_Party/FreeRTOS/Source/include", 
				"Middlewares/Third_Party/FreeRTOS/Source/CMSIS_RTOS", 
				"Middlewares/Third_Party/FreeRTOS/Source/portable/GCC/ARM_CM4F", 
				"Middlewares/ST/STM32_USB_Device_Library/Core/Inc", 
				"Middlewares/ST/STM32_USB_Device_Library/Class/CDC/Inc", 
				"Drivers/CMSIS/Device/ST/STM32F4xx/Include", 
				"Drivers/CMSIS/Include"
			]
		}
	]
}

Now you should have the full amazing code navigation/editting functionality of VS Code, including syntax highlighting, Go to Definition, Go to References, code peeking and more.

Building from VS Code

VS Code provides functionality to run whatever terminal command as a "task" from within the editor. In the ./.vscode/ folder, create a tasks.json file with the following

{
	"version": "2.0.0",
	"tasks":
	[
		{
			"label": "Build Firmware",
			"group":
			{
				"kind": "build",
				"isDefault": true
			},
			"type": "shell",
			"command": "make all",
			"args":
			[
				
			],
			"problemMatcher":
			[
				"$gcc"
			],
			"presentation":
			{
				"focus": true
			}
		}
	]
}

The line "isDefault": true sets this task is the default build task. This means that all we need to do is press CTRL + SHIFT + B to run the same make command as before.

Alternatively, press CTRL + SHIFT + P to open the command palette, and select Run Task, it will then ask you which one to choose.

Uploading from VS Code

Just as before, we just need to create a VS Code task to run the upload command for us. Open tasks.json and add the following task just after the build task (seperated by a comma)

{
    "label": "Upload Firmware V2",
    "type": "shell",
    "command": "openocd",
    "args":
		[
			"-f","interface/stlink-v2-1.cfg",
			"-f","target/stm32f4x.cfg",
			"-c","'program build/config_stm32f4.elf verify reset exit'"
		]
},
{
	"label": "Upload Firmware V1",
	"type": "shell",
	"command": "openocd",
	"args":
	[
		"-f","board/stm32f4discovery.cfg",
		"-c","'program build/config_stm32f4.elf verify reset exit'"
	]
}

And thats it! You can run this from the command palette by pressing CTRL + SHIFT + P and typing in Run Task, after which the suggestions will prompt you for which task to choose.

Debugging with VS Code

For this step, the easiest thing to do is to install the Cortex-Debug extension for VS Code.

Then, create a launch.json file inside the .vscode folder with the following contents.

{
    "version": "0.2.0",
    "configurations":
    [
        {
            "name": "Build and Debug V2",
            "type": "cortex-debug",
            "request": "launch",
            "servertype": "openocd",
            "cwd": "${workspaceFolder}",
            "executable": "./build/config_stm32f4.elf",
            "device": "STM32F405RGT6",
            "svdFile": "${workspaceFolder}/.vscode/STM32F405.svd",
            "configFiles":
            [
                "/usr/share/openocd/scripts/interface/stlink-v2-1.cfg",
                "/usr/share/openocd/scripts/target/stm32f4x.cfg"
            ],
            "preLaunchTask": "Build Firmware",
            "overrideGDBServerStartedRegex": "Info\\s:\\s([^\\n\\.]*)\\.cpu([^\\n]*)"
        },
		{
			"name": "Build and Debug V1",
			"type": "cortex-debug",
			"request": "launch",
			"servertype": "openocd",
			"cwd": "${workspaceFolder}",
			"executable": "build/config_stm32f4.elf",
			"device": "STM32F405RGT6",
            "svdFile": "${workspaceFolder}/.vscode/STM32F405.svd",
			"configFiles":
			[
				"/usr/share/openocd/scripts/board/stm32f4discovery.cfg"
			],
			"preLaunchTask": "Build Firmware",
			"overrideGDBServerStartedRegex": "Info\\s:\\s([^\\n\\.]*)\\.cpu([^\\n]*)"
		}
    ]
},

Create a settings.json file inside the .vscode folder with the following contents.

{
	"cortex-debug.armToolchainPath": "",
	"cortex-debug.openocdPath": "/usr/bin/openocd"
}

You should now be able to go to the debug tab in VS Code and see a Build and Debug option.

Steven's Magic Links

Makefile tutorial: https://cs.colby.edu/maxwell/courses/tutorials/maketutor/

FreeRTOS tutorial: https://freertos.org/fr-content-src/uploads/2018/07/161204_Mastering_the_FreeRTOS_Real_Time_Kernel-A_Hands-On_Tutorial_Guide.pdf

About

The firmware running on a ST microcontroller for interfacing with the DWM1000 UWB modules.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages