Oak

🌳 An outdoor LoRa-GPS tracker with an E-Ink display 🔑

power 18650
wireless LoRa,
sensor GPS, E-Ink
mcu SAMD21G18
bom items 82
bom cost USD $78.93
vendors 6
completed July 2022

Reading and writing a struct to the flash

flash storage read write struct easy

Reading and writing a struct to the non-volatile flash memory.


Before starting

Dependancies

FlashStorage

Details

Write a set of config to the flash storage and read it back.

struct Config {
  byte localAddress;
  byte destinationAddress;
  char eink_version[4];
  char hardware_model[2];
};

Steps

  1. Plug in the Oak PCB to the computer
  2. (Option A) Ensure the PCB can be detected with ls -al /dev/cu.usbmodem and arduino-cli board list. Run make to compile and upload the code to the board. (Option A) Ensure the PCB can be detected with <code>ls -al /dev/cu.usbmodem</code> and <code>arduino-cli board list</code>. Run <code>make</code> to compile and upload the code to the board.
  3. (Option B) Ensure the board can be detected with Arduino IDE. Compile and upload the code to the board. (Option B) Ensure the board can be detected with Arduino IDE. Compile and upload the code to the board.
  4. Watch the Blinky LED the board is battery-powered or USB-powered.

Serial console

Serial output from the firmware.

Code

Download code
#include <FlashStorage.h>

struct Config {
  byte localAddress;
  byte destinationAddress;
  char eink_version[4];
  char hardware_model[2];
};

FlashStorage(oak_flash_store, Config);

void setup() {
  SerialUSB.begin(9600);
  while (!SerialUSB) {}
  SerialUSB.println("Reading and writing a struct into the flash...");

  Config oak_config = {0xBB, 0xAA, "1.0", "B"};
  Config oak_config_read;

  oak_flash_store.write(oak_config);
  oak_config_read = oak_flash_store.read();
  SerialUSB.println("\nWrite Oak config");

  SerialUSB.println("\nRead Oak config: ");
  SerialUSB.println(" localAddress: "
    + String(oak_config_read.localAddress, HEX));
  SerialUSB.println(" destinationAddress: "
    + String(oak_config_read.destinationAddress, HEX));
  SerialUSB.println(" eink_version: "
    + String(oak_config_read.eink_version));
  SerialUSB.println(" hardware_model: "
    + String(oak_config_read.hardware_model));
}

void loop() { }

Makefile

BOARD?=hutscape:samd:oak
PORT := $(shell ls /dev/cu.usbmodem*)
BUILD=build

.PHONY: default lint all flash clean

default: lint all flash clean

lint:
	cpplint --extensions=ino --filter=-legal/copyright *.ino

all:
	arduino-cli compile --fqbn $(BOARD) --output-dir $(BUILD) ./

flash:
	arduino-cli upload -p $(PORT) --fqbn $(BOARD) --input-dir $(BUILD) --verbose

clean:
	rm -r build

References