> ## Documentation Index
> Fetch the complete documentation index at: https://docs.thistle.tech/llms.txt
> Use this file to discover all available pages before exploring further.

# Thistle OTA Update on PSOC Edge AI Kit

> AI Model Update on PSoC Edge AI Kit

This guide will show you how to perform an over-the-air (OTA) update on a PSoC Edge AI Kit device using Thistle.
You can follow along with our video guide as well.

<div style={{ display: "flex", flexWrap: "wrap", justifyContent: "center", marginBottom: "20px", marginTop: "20px" }}>
  <div style={{ width: "50%", minWidth: "300px", padding: "0 10px" }}>
    <h3>Part 1. Prepare your PSoC Edge AI kit</h3>

    <iframe width="100%" height="315" src="https://www.youtube.com/embed/5FtUUOzyWbE" title="YouTube video player" frameBorder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" allowFullScreen />
  </div>

  <div style={{ width: "50%", minWidth: "300px", padding: "0 10px" }}>
    <h3>Part 2. AI Model update using Thistle</h3>

    <iframe width="100%" height="315" src="https://www.youtube.com/embed/a0AD40LTh9Y" title="YouTube video player" frameBorder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" allowFullScreen />
  </div>
</div>

Overview of Thistle OTA Update for PSoC Edge

<img src="https://mintcdn.com/thistletechnologies/eZOmvJGw8GrVYs9i/images/diagram-ctuc-psocedge-workshop-flow.png?fit=max&auto=format&n=eZOmvJGw8GrVYs9i&q=85&s=b95d19231eafec94e9a10619933b1257" alt="Thistle Control Center" width="1920" height="1080" data-path="images/diagram-ctuc-psocedge-workshop-flow.png" />

## Tools Needed for This Tutorial

### Hardware

* One [PSoC Edge AI Kit](https://www.infineon.com/evaluation-board/kit-pse84-ai) Evaluation board with PSOC™ E84
  for Artificial Intelligence <br />
  (the board has multiple sensing capabilities: Voice, Radar, Audio, Movement, etc.)
* One USB-C cable
* One desktop/laptop PC running MacOS X, Windows, or Linux for publishing OTA updates,
  programming the PSoC Edge AI kit, and observing its logs using a terminal application.

### Software

* The [ModusToolbox Programmer](https://softwaretools.infineon.com/tools/com.ifx.tb.tool.modustoolboxprogtools) to flash the PSoC Edge AI Kit
* A Device Enrollment Token from the [Thistle Control Center](https://app.thistle.tech/projects)
* The C Thistle Update Client (CTUC) for the PSoC Edge AI kit:
  [download CTUC here](https://downloads.thistle.tech/psoc-edge/CTUC-PSOC-EDGE-BETA.hex)
* A terminal application like minicom

## Demo Preparation

### Connect PC and PSoC Edge AI Kit over USB Port

Connect the USB-C cable to the USB port of the PSoC Edge AI Kit that is further away from the pin header (see picture below).
Plug the other end of the USB-Ccable to the PC.

<img src="https://mintcdn.com/thistletechnologies/eZOmvJGw8GrVYs9i/images/diagram-psocedge-aikit-usb.png?fit=max&auto=format&n=eZOmvJGw8GrVYs9i&q=85&s=140cc14e0185a5aeec89c581b3e67b8f" alt="Connect PC to PSoC Edge AI Kit" width="1280" height="960" data-path="images/diagram-psocedge-aikit-usb.png" />

### Quick Start

1. Install the [ModusToolbox Programmer](https://softwaretools.infineon.com/tools/com.ifx.tb.tool.modustoolboxprogtools)

2. Download our CTUC binary for PSoC Edge

3. Flash the CTUC binary to your PSoC Edge AI Kit using the ModusToolbox Programmer

4. Start minicom or your preffered terminal application

* Press the black `Reset button` and observe:

```
Total Flash Size:67108864 bytes
Thistle Update Client for MCUs on the PSoC Edge AI kit
=== This demo CTUC will always flash a new firmware regardless of Release versioning ===
                Current CTUC mode: Enroll (First Run)

=== Thistle Setup ===
Enter your Thistle Device Enrollment Token (max 64 chars): 
```

## Device Enrollment Token

The Device Enrollment Token is unique per project and can be found in your Thistle Control Center (TCC).
Create a new project by pressing the `+Project` button in the top right of TCC:

<img src="https://mintcdn.com/thistletechnologies/eZOmvJGw8GrVYs9i/images/diagram-thistle-create-project.png?fit=max&auto=format&n=eZOmvJGw8GrVYs9i&q=85&s=e77c656bcd03263fccf6071a525c0921" alt="Create a project in TCC" width="729" height="539" data-path="images/diagram-thistle-create-project.png" />

**Enter your new project** and and go into `settings` (it is the last menu on the left),
open the `access` submenu and press the `copy` button next to the `Device Enrollment Token`:

<img src="https://mintcdn.com/thistletechnologies/eZOmvJGw8GrVYs9i/images/diagram-thistle-device-enrollment-token.png?fit=max&auto=format&n=eZOmvJGw8GrVYs9i&q=85&s=6707107b97e4b4ba9ef996c038bf6d38" alt="Create a project in TCC" width="903" height="1077" data-path="images/diagram-thistle-device-enrollment-token.png" />

Go back to the terminal and paste the value of the `Device Enrollment Token` and press "Enter".
The next prompt is for the `Thistle Public Verification Key` that is available in the same settings page.
Copy the `OTA Public Verification Key`, then paste it into the terminal.

```
Enter your Thistle Public Key (max 94 chars): 
```

Last prompt asks for WiFi settings, so that the PSoC Edge AI Kit
can download OTA updates from your TCC project.

```
=== WiFi Setup ===
Enter WiFi SSID (max 32 chars): wifi6
Enter WiFi Password (max 63 chars): halloween
```

OTA starts now and you should be able to see an output likes this:

```
Wi-Fi Connection Manager initialized.
Successfully connected to Wi-Fi network 'wifi6'.
IPv4 address assigned: 192.168.68.107
Enrolling

Connecting to Thistle Control Center ...
Connected to Thistle Control Center
Response Status: 200
Enrolling to Thistle Control Center
Response Status: 200
Found device_token: 64dc12a986f08a...
```

<Note>
  Thistle Update Client enrolls the device to TCC once and acquires a unique device id
</Note>

Soon after enrolling, the device will reboot to download the latest project manifest:

```
...
Checking for a new firmware update

Connecting to Thistle Control Center ...
Connected to Thistle Control Center

Fetching the latest manifest for this project
Response Status: 204

No Releases found for this project - Add at least one Release in your project.
Use the white button to trigger OTA again - Rebooting back to normal operations.
```

<Note>
  We need to have at least one Release published in our project before continuing further.
</Note>

## OTA Release

Go to your project in TCC and choose the second menu on the right named `Releases`.
In the same place where we had the `+Project` button, now we have the `+Release` button.

Create a new release by pressing the `+Release` button in the top right of TCC and
you should a similar window:

<img src="https://mintcdn.com/thistletechnologies/eZOmvJGw8GrVYs9i/images/diagram-thistle-mcu-firmware-release.png?fit=max&auto=format&n=eZOmvJGw8GrVYs9i&q=85&s=7f6cc18271586d93e887ee290db504b5" alt="Create a MCU Firmware Release in TCC" width="1280" height="1081" data-path="images/diagram-thistle-mcu-firmware-release.png" />

As shown on the picture above, perform the following three steps:

1. Select the "Infineon PSoC" from the drop-down menu

2. Press the "Select Files" button and upload the DEEPCRAFT-AlarmModel.bin (link below)

3. Press the "Deploy Release" button. That's it!

This prebuilt Alarm Detection Model is based on
the [DEEPCRAFT Ready Models for PSoC Edge](https://github.com/Infineon/mtb-example-psoc-edge-ml-deepcraft-deploy-ready-model/)
by Imagimob (an Infineon Technologies company): [download](https://downloads.thistle.tech/psoc-edge/DEEPCRAFT-AlarmSound.bin)

## Start OTA Update

Press the white `User button` on the PSoC Edge AI Kit

```
Checking for a new firmware update

Connecting to Thistle Control Center ...
Connected to Thistle Control Center

Fetching the latest manifest for this project
...
Manifest is genuine
Inform Thistle Control Center of started event

Downloading the latest release for this device
...
```

🎉 Congratulations! OTA is now under way

<Note>
  OTA update on PSoC Edge can take up to 5 minutes depending on the size of your AI Model.
</Note>

Once ready CTUC will perform one final reboot for an extra integrity check of the updated firmware
and then start the new application. Here is how this looks:

```
New Firmware Image (427368 bytes) is stored
Latest release is downloaded
****************** DEEPCRAFT Ready Model: alarm ****************** 

........................
```

Play an Alarm sound next to the PSoC Edge AI Kit to test the AI Model detection.

For example, here is a [School Alarm sound from Youtube](https://www.youtube.com/shorts/GxSO_E2hNoA).

## Conclusion

💡 Thistle OTA on PSoC Edge can be ran as many times as you want.
Add a new Release to your TCC project with a different AI Model
and press the white `User button` to start OTA.

This tutorial shows how to perform an update on the PSoC Edge AI Kit using the C Thistle Update Client (CTUC).
This process can be automated to update a fleet of devices. We support different enrollment setups,
either based on Trust On First Use (TOFU), or only accepting pre-enrolled devices.

Contact us for more information
