Read and Write to the flash memory

Updated on 3 September 2021
dev board RobotDyn M0 Mini
firmware Arduino
chip SAMD21
features flash read write
This tutorial is more than 1 year old. If the steps below do not work, then please check the latest versions and the documentations of the individual tools used.

Before starting

Dependancies

Ensure the following dependancies are downloaded and available:

Pre-requisites

Try these simpler or similar examples:

Buy the components

Code

Download code flash-storage-samd21.ino
#include <FlashStorage.h>

FlashStorage(my_flash_store, int);
String readString = "54";  // Change this number to store in flash

void setup() {
  SerialUSB.begin(9600);
  while (!SerialUSB) { }
  delay(100);

  int number;

  SerialUSB.print("Number read from flash before storing: ");
  number = my_flash_store.read();
  SerialUSB.println(number);

  SerialUSB.print("Number from user: ");
  SerialUSB.println(readString);

  SerialUSB.println("Number has been stored in flash!");
  my_flash_store.write(readString.toInt());

  SerialUSB.print("Number read from flash: ");
  number = my_flash_store.read();
  SerialUSB.println(number);
}

void loop() {}

Makefile

BOARD?=arduino:samd:mzero_bl
PORT := $(shell ls /dev/cu.usbmodem*)

.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) ./

flash:
	arduino-cli upload -p $(PORT) --fqbn $(BOARD) ./

clean:
	rm -f .*.bin
	rm -f .*.elf
	rm -f .*.hex

Serial console

Serial output from the firmware.

Read and Write to the flash memory serial console

Description

Read and write a single number to the flash memory of a SAMD21 board (E.g. Arduino M0 or RobotDyn SAMD21 M0-Mini).

References

Watch