An Arduino sketch is used to create the TRNG sending code that runs on the Trinkey QT2040. Scroll to the bottom of this page for the complete code listing.

You can customize the TRNG sending behavior by modifying these lines found at the top of the sketch:

//--| User Config |-----------------------------------------------
#define TRNG_FORMAT   2         // 0=raw, 1=CSV, 2=JSON
#define TRNG_LENGTH   8         // random number length in bytes, 8 to 256
#define TRNG_RATE     100       // generate new number ever X ms
#define BEAT_RATE     1000      // neopixel heart beat rate in ms, 0=none
#define BEAT_COLOR    0xADAF00  // neopixel heart beat color
//----------------------------------------------------------------

Here is a summary of the options:

  • TRNG_FORMAT - sets the desired output format on the serial port
  • TRNG_LENGTH - sets the desired length in bytes
  • TRNG_RATE - sets how often new values are sent over serial
  • BEAT_RATE - sets NeoPixel heart beat rate
  • BEAT_COLOR - sets NeoPixel heart beat color

The UF2 files provided earlier in this guide are simply pre-compiled versions of this sketch with different settings.

Infineon Trust M Arduino Library

The Arduino library used is provided by Infineon and can be found here:

It can be installed via the Library Manager. Just search for "optiga trust m" and it should show up:

Manually Installing Trust M Arduino Libary

At the time of this guide writing, there is a needed update to the Infineon Trust M library. While this change has been added (PR merged) into the library code, it has not been released.  With the current release version (v1.1.0), a compile time error will occur:

error: no return statement in function returning non-void [-Werror=return-type]

Therefore, manually installing the library is required.

To download a zip of the library:

  1. Go to the libary repo
  2. Click the Code button
  3. Click Download ZIP
  4. In the Arduino IDE, install via Sketch > Include Library > Add .ZIP Library...

Arduino Sketch

Here is the code listing for the Arduino sketch used.

#include <Adafruit_NeoPixel.h>
#include "OPTIGATrustM.h"

//--| User Config |-----------------------------------------------
#define TRNG_FORMAT   2         // 0=raw, 1=CSV, 2=JSON
#define TRNG_LENGTH   8         // random number length in bytes, 8 to 256
#define TRNG_RATE     100       // generate new number ever X ms
#define BEAT_RATE     1000      // neopixel heart beat rate in ms, 0=none
#define BEAT_COLOR    0xADAF00  // neopixel heart beat color
//----------------------------------------------------------------

uint8_t trng[TRNG_LENGTH];
int current_time, last_trng, last_beat;

Adafruit_NeoPixel neopixel(1, PIN_NEOPIXEL, NEO_GRB + NEO_KHZ800);

void setup() 
{
  Serial.begin(115200); // USB CDC doesn't really care about baud rate
  
  if (trustM.begin()) {
    Serial.println("Failed to initialize Trust M.");
    neoPanic();
  }

  neopixel.begin();
  neopixel.fill(0);
  neopixel.show();

  last_trng = last_beat = millis();
}

void loop()
{
  current_time = millis();

  if (current_time - last_trng > TRNG_RATE) {
    trustM.getRandom(TRNG_LENGTH, trng);
    sendTRNG();
    last_trng = current_time;   
  }

  if ((BEAT_RATE) && (current_time - last_beat > BEAT_RATE)) {
    if (neopixel.getPixelColor(0)) {
      neopixel.fill(0);
    } else {
      neopixel.fill(BEAT_COLOR);
    }
    neopixel.show();
    last_beat = current_time;
  }
}

void sendTRNG() {
  if (TRNG_FORMAT) {
    // formatted string output (CSV, JSON)
    if (TRNG_FORMAT==2) Serial.print("{\"trng\": \"");
    for (uint16_t i=0; i<TRNG_LENGTH; i++) {
      Serial.print(trng[i]);
      if (i != TRNG_LENGTH - 1) Serial.print(", "); 
    }
    if (TRNG_FORMAT==2) Serial.print("\"}");
    Serial.println();
  } else {
    // raw output (bytes)
    Serial.write(trng, TRNG_LENGTH);
  }
}


void neoPanic() {
  while (1) {
    neopixel.fill(0xFF0000); neopixel.show(); delay(100);
    neopixel.fill(0x000000); neopixel.show(); delay(100);
  }
}

This guide was first published on Dec 09, 2021. It was last updated on Dec 09, 2021.

This page (Customizing) was last updated on Dec 31, 2021.

Text editor powered by tinymce.