# Firmware using West

{% hint style="warning" %}
You can use the online GitPod.io if you want a simple way to generate a new firmware without setting up this whole build system. Click the Link below. You can make quick changes and test things without all the setup.\
\
If you plan on using a debugger or being able to use the `west flash` command to write to your board quickly then continue on.
{% endhint %}

{% content-ref url="/pages/LEZ6NKS384HUQFTReGRq" %}
[Firmware using GitPod.io](/head-tracker-v2.2/support/development/firmware-using-gitpod.io.md)
{% endcontent-ref %}

This guide mostly follows the instructions on the Zephyr Gettings started Guide. Some install locations have been modified so that code completion and debugging paths match what is set in **ht/firmware/src/headtracker.code-workspace** and **ht/firmware/src/.vscode/c\_cpp\_properties.json**

{% embed url="<https://docs.zephyrproject.org/latest/develop/getting_started/index.html>" %}

## Install Chocolatey + Required Packages

Follow the instructions here to install chocolatey. You could install all the requirements yourself but this makes the installation easy.

{% embed url="<https://chocolatey.org/install>" %}

Open a Administrative PowerShell Terminal. Use the **Win + X** keystroke and select **Windows PowerShell (Admin)** or **Terminal (Admin)** Depending on Windows version![](/files/P2Yuxh3wPuO97TPQPIDX) OR ![](/files/n0CbNrBCv2yDhqbeC3Qk)

Run the following commands to install the necessary files

```
choco feature enable -n allowGlobalConfirmation
```

```powershell
choco install cmake --installargs 'ADD_CMAKE_TO_PATH=System'
choco install ninja gperf python311 git dtc-msys2 wget 7zip
choco install vscode # Optional
choco install github-desktop # Optional
```

You can skip the optional ones if you don't want to use VSCode or Github Desktop

{% hint style="info" %}
Install Chocolatey GUI  as will if you want an easy way to work with Chocolatey Package manager\
`choco install chocolateygui`
{% endhint %}

## Install West using PIP

Open a new PowerShell terminal as a **regular user**. Use **Win + X** keystroke and choose Windows Powershell  ![](/files/3BaCdSd4moNG3UuFMcgs) or Terminal ![](/files/DjgT1xObsPskIUgxP7Fk)

```powershell
pip3 install -U west
```

{% hint style="info" %}
If you have an issue with `WARNING: Failed to write executable - trying to use .deleteme logic` Try running again in an admin user. Continue the remainder as a regular user
{% endhint %}

## Get the HeadTracker source + required submodules

```powershell
cd c:\
git clone https://github.com/dlktdr/HeadTracker.git ht
cd ht
git submodule update --init
```

## Get the Zephyr source & update&#x20;

{% hint style="success" %}
We are using Zephyr LTS v3.7.1 as of writing this. Please check in Dockerfile to verify the version in case this isn't updated. \
\
<https://github.com/dlktdr/HeadTracker/blob/master/Dockerfile>
{% endhint %}

```powershell
cd c:\ht\firmware
west init zephyr
cd zephyr\zephyr
git checkout v3.7.1
cd ..
west update
west zephyr-export
```

## Install the extra required packages using PIP

```
pip3 install -r c:\ht\firmware\zephyr\zephyr\scripts\requirements.txt
```

## Download the Zephyr SDK

{% hint style="success" %}
As of writing this we are using the 0.17.0 version of the Zephyr SDK. Please check Dockerfile for the current version in case this isn't updated.\
\
<https://github.com/dlktdr/HeadTracker/blob/master/Dockerfile>
{% endhint %}

Download the Zephyr SDK version listed above\
\
<https://github.com/zephyrproject-rtos/sdk-ng/releases/download/v0.17.0/zephyr-sdk-0.17.0_windows-x86_64.7z>\
\
This is all the build tools for ARM, RISC and other architectures.

After download completes, move the file to **c:\HT\firmware**

## **Extract SDK, Rename, Register cmake package**

Open PowerShell as a regular user

```powershell
cd c:\ht\firmware
7z x zephyr-sdk-0.17.0_windows-x86_64.7z
move zephyr-sdk-0.17.0 zephyr-sdk  #Rename folder so default VScode setup can find compilers
rm zephyr-sdk-17.0_windows-x86_64.7z #optional, Remove the download
cd zephyr-sdk
.\setup.cmd /c  #Register Cmake Package
```

## Test Compiling the code

Open a PowerShell as a regular user. The environment variable **ZEPHYR\_BASE** must be set so west knows where to look for the zephyr source code. If you do this in VScode the variable is set on the terminal from the headtrackercode.workspace file so you can skip it if using the vscode terminal

```
cd c:\ht\firmware\src
$env:ZEPHYR_BASE = 'c:\ht\firmware\zephyr\zephyr'
west build -p -b arduino_nano_33_ble
```

## Open Visual Studio Code

From File Choose open Workspace From File and open C:\ht\firmware\src\headtracker.code-workspace file. This will load all the settings which will make building quicker.

<figure><img src="/files/Wg3DwKvgWKYGb7R2sgmw" alt=""><figcaption></figcaption></figure>

From VScode close any terminals that are open and start new ones. This is to be sure the ZEPHYR\_BASE environment variable gets set properly. It's set in the workspace file for you.<br>

<figure><img src="/files/QCKQwlq2A7MUhLAilMoH" alt=""><figcaption></figcaption></figure>

You can now build for various boards using the commands they are listed in the GitPod documentation here,

{% embed url="<https://headtracker.gitbook.io/head-tracker-v2.2/support/development/firmware-build-using-gitpod.io#build-commands>" %}

## Flashing the board

You can flash directly form the command line. Do this after you successfully compile the code for whatever board your working on. The flash utility will change depending on the selected board.

```
west flash 
```

For the Arduino boards it's also helpful to specify the com port. e.g. COM4. If you have multiple ports on your system west flash may not properly find it.

```
west flash --bossac-port COM4
```

To find the port. **Double tap the reset button to place the board in bootloader mode**. Check the device manager to see what port is listed. The port number will change when in bootloader mode and in normal run mode. \
![](/files/BCfxSDH5xLzsWQdnJeEq)

To see all the flashing runner options for the supported boards run,

```
west flash --context
```

## Debugging with a J-Link

Install Segger J-Link. ![](/files/Cte6Xc0D9ZthcJEfEbo5) <https://www.segger.com/downloads/jlink/>\
Install the cortex-debug extension in visual studio code ![](/files/EQwNOnbqKbdUxoLF0RH9)\
There are various launch tasks setup in **headtracker.code-workspace** to make debugging easy with a jlink. Highlight the board you want then press F5 to compile and upload

**\*\*Be sure to delete the build folder before switching from a release to a debug modes.**

<figure><img src="/files/V9RdODTX2FRQausBAzBp" alt=""><figcaption></figcaption></figure>

There is an example using OpenOCD for a ESP. Next time I go to use this I'll update the docs. Your on your own for this one.&#x20;

## Common Issues

* Issues with building can often be resolved by deleting the build folder under **c:\ht\firmware\src\build** and trying again. Build with the -**p** flag to start with a clean build. Required which switching between various boards.
* If you see the error **"unknown command "build";** Your ZEPHYR\_BASE environment variable isn't set properly. <img src="/files/0AACdBbZNUNN1q6nBONX" alt="" data-size="original">
* **ERROR: source directory "." does not contain a CMakeLists.txt;** You tried to build from the incorrect folder. You must build from c:\ht\firmware\src

  <figure><img src="/files/yj0R1SSB9pYPcxjatL1j" alt=""><figcaption></figcaption></figure>
* Could not find a package configuration file provided by "Zephyr-sdk".\
  You need to enter the zephyr-sdk folder and register the cmake package. \
  `cd firmware/zephry-sdk`\
  `./setup.cmd /c`\ <img src="/files/CRCAZqB6rlJaxzK2B9vO" alt="" data-size="original">

Others questions and issues please ask on Discord in the #dev channel. Link Below

{% embed url="<https://discord.com/channels/827622724565467196/900028126271242250>" %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://headtracker.gitbook.io/head-tracker-v2.2/support/development/firmware-using-west.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
