---
description: Follow this guide to flash firmware to your board on Linux.
---

# Linux - flashing

{% hint style="warning" %}
For VM and/or WSL users the USB functionalities might not work properly. For WSL user consult the[ WSL page ](../../development-tools/wsl)for instructions on how to set up the USB ports.&#x20;
{% endhint %}

## Setting up&#x20;

### Step 1 - Download and Install J-Link

First things first, we need to get the software that allows us to use the J-Link debugger. Below is the link to the install page. Choose the download corresponding to your system.

{% embed url="https://www.segger.com/downloads/jlink#J-LinkSoftwareAndDocumentationPack" %}

Just execute the downloaded file and an installer will open. Once you have finished installing go to the next step.

### Step 2 - Connect the J-Link to your PC

The image bellow shows the J-Link debugger used to connect to your PC. Connect the USB connection to your PC and the 7-pin JST GH is connected to the "DEBUG" port in your FMUK66. You will also need to connect a micro-USB cable to the FMU to power it.

<figure><img src="https://3991116448-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M8v3AGPa-j5DtYx8zqd%2Fuploads%2FSECxnO5yN2GWvgdX8sN1%2Fimage.png?alt=media&#x26;token=139ec00d-9f1f-41f1-8aed-ea4413bffd36" alt=""><figcaption></figcaption></figure>

Then you can connect both the USB cables displayed below to your computer.  The connection should look a bit like the photo below:

<figure><img src="https://3991116448-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M8v3AGPa-j5DtYx8zqd%2Fuploads%2FDkBZ0fO6QyEkV8T2Kxxs%2FWhatsApp%20Image%202023-02-24%20at%2010.19.51.jpeg?alt=media&#x26;token=b8cc1ba6-efc2-480a-9fd5-85f26ea808f2" alt=""><figcaption></figcaption></figure>

Now open a terminal window and type the following:

```
JLinkExe
```

You should get this output:

![](https://3991116448-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M8v3AGPa-j5DtYx8zqd%2F-M8vajMQaKBwd8BrpQo3%2F-M8vasws5qHDb4Sem8zC%2Fimage.png?alt=media\&token=31c8060e-64f1-423a-b428-4f7b030c2417)

Once that is done you can connect your debugger by typing:&#x20;

```
conect
```

This is the output you should get:

![](https://3991116448-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M8v3AGPa-j5DtYx8zqd%2F-M8vajMQaKBwd8BrpQo3%2F-M8vavLB7AxcXwlXj0rI%2Fimage.png?alt=media\&token=72edd168-75b7-4f11-818f-72c7ff98b48a)

The next step is to specify the target device. Follow the table below for the target device identifier needed for your specific board. In our case we are using the board FMUK66 with device name `MK66FN2M0XXX18`.

| Board Name                      | Device / Core  |
| ------------------------------- | -------------- |
| RDDRONE-FMUK66 (HoverGames FMU) | MK66FN2M0XXX18 |
| UCANS32K146                     | S32K146        |
| RDDRONE-BMS772                  | S32K144        |
| 8MMNavQ                         | ???            |

On Linux, you enter this by hand. But you can also copy the device name above and paste it into the terminal by pressing `Ctrl+Shift+V`. The selected device will be remembered in next sessions, meaning you can just press enter to select the saved default.

<figure><img src="https://3991116448-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M8v3AGPa-j5DtYx8zqd%2Fuploads%2FqdbM2VOHj0r1ERvErCDB%2Fimage.png?alt=media&#x26;token=4e944176-69c2-4772-8028-c17f309f9c6f" alt=""><figcaption></figcaption></figure>

If you're using a J-Link debugger, enter `s` to select SWD as the target interface.

<figure><img src="https://3991116448-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M8v3AGPa-j5DtYx8zqd%2Fuploads%2FqsxirrEr0KUgjjErxXzX%2Fimage.png?alt=media&#x26;token=04ab5539-021b-454e-8001-549571296b57" alt=""><figcaption></figcaption></figure>

For the speed entry, reference the table below to select the correct target interface speed for the board you are flashing. In our case it is `4000`.

| Board Name     | Target Device Speed (kHz) |
| -------------- | ------------------------- |
| RDDRONE-FMUK66 | 4000                      |
| UCANS32K146    | 1000                      |
| RDDRONE-BMS772 | 1000                      |

<figure><img src="https://3991116448-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M8v3AGPa-j5DtYx8zqd%2Fuploads%2FEdhBQWfvTRkOp7GqRtGR%2Fimage.png?alt=media&#x26;token=90fcd808-8102-45e2-9295-d91ac309fca1" alt=""><figcaption></figcaption></figure>

The J-Link will then connect to the target. You will get a pop up to accept the Term of Use. Accept those terms to continue. You should see something similar to this after:

<figure><img src="https://3991116448-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M8v3AGPa-j5DtYx8zqd%2Fuploads%2FqFlYTAwDUBNKl7lnwIL6%2Fimage.png?alt=media&#x26;token=005906cc-4270-42d1-b2cc-368a0f17ce35" alt=""><figcaption></figcaption></figure>

### Step 3 - Download the bootloader

Now you will need to download the bootloader. The FMU requires a bootloader to run. You will be able to flash this using the J-Link debugger. Please download the file from the [Downloads](../../../extra-content/downloads-and-links#bootloader-download-links) page. Make sure to remember the location of this file.

### Step 4 - Flashing the FMU (bootloader)

To flash a .bin file, the general format of `loadbin` is as follows:&#x20;

```
loadbin <path to binary file> <target memory address>
```

The path to the binary file is an absolute path, and the target memory address is a hexadecimal address.&#x20;

{% hint style="info" %}
When your path includes a whitespace, you will need quotation marks around you path, like so:

`loadbin "/another path/to/file.bin" 0x0`
{% endhint %}

Depending on the size of the binary, it might take a while to flash. The console will show the different steps. It will first check the contents of the binary and match it against the contents of the memory on the board. If they are equal, the binary doesn't have to be flashed. Otherwise, the old contents of the memory are first erased. Then the contents of the binary are programmed into the memory, and finally it is verified that it was done correctly.

![](https://3991116448-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M8v3AGPa-j5DtYx8zqd%2F-M8vajMQaKBwd8BrpQo3%2F-M8vb9rBM-1CRmYywX7T%2Fimage.png?alt=media\&token=600f7682-57e0-4355-9832-3c2f6d3bf9d2)

That is everything. You may need to press reset on the board, or power cycle for the new code to start running on your board.

#### J-Link help

To know what other commands the J-Link can accept you can type `?` to get a help list of commands that J-Link accepts.&#x20;

![](https://3991116448-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M8v3AGPa-j5DtYx8zqd%2F-M8vajMQaKBwd8BrpQo3%2F-M8vb5R7K7kiTiP1HCE6%2Fimage.png?alt=media\&token=c6514876-d881-4940-b789-343745c40d45)

### Step 5 - Flashing the FMU (firmware)

Now with the bootloader set up we can start flashing the firmware to the board. This process is very similar to the previous step with the bootloader. The biggest difference is that the location of the firmware file is in a different place and the target memory address is different.&#x20;

The file should be located at the location similar to this one:

```
~/src/PX4-Autopilot/build/nxp_fmuk66-v3/nxp_fmuk66-v3.bin
```

Now that you know where the location of the file is we can continue. Do the same steps as the bootloader:

1.  Go to the Terminal and write:

    `JLinkExe`
2. `connect`
3. `MK66FN2M0XXX18` or "Enter"
4. `S`
5. `4000`
6.  Now this part is different:

    `loadbin </path/to/firmware/file.bin> 0x6000`

The path for the firmware is the one mentioned in the beginning of the chapter.&#x20;

{% hint style="info" %}
It is very important that you get the correct offsets (`0x0`, `0x6000`), as the bootloader uses these to find the firmware on your FMU's flash chip.
{% endhint %}

Once you have finished flashing your board, you can press the RESET button on the side of the FMU to reboot it. It will now be running PX4-Autopilot!

## Uploading code

With all the flashing setup we can uplaod example codes to the board. To do so first, use the following code to go to the PX4 directory (this should be done in your WSL):

```
cd ~/src/PX4-Autopilot
```

Once in the correct directory, write the make command bellow:

```
make nxp_fmuk66-v3_default
```

You have just compiled the file and made it ready to upload. To upload these files, run the following command:

```
make nxp_fmuk66-v3_default upload
```

It might take a while and you will get a prompt to unplug and plug back is your USB. Follow those instructions until there is a progress bar uploading your files. Once finished your code will be uploaded and ready to be tested.
