nvm-consignes.ino

Arduino Nano Every · File : nvm-consignes.ino · Language : Arduino · Use case : Stocker des consignes dans la NVM du 4809

The ATmega4809 has no silicon EEPROM anymore: the megaAVR core EEPROM library emulates it in NVM flash. This sketch loads then saves serial-adjustable setpoints, with a signature validating the structure — the expected behaviour of a controller.

Hardware

An Arduino Nano Every (ATmega4809 at 20 MHz, same footprint as the Nano, micro-B USB, separate UART chip, 5 V pins, drop-in compatibility with the classic Nano). Bare Nano Every; the NVM is in-chip (256-byte application section).

Wiring

No wiring; test by sending s400 then p30 on the serial monitor.

How it works

EEPROM.get/put handle a whole struct; the 0xC0FFEE signature detects virgin or corrupted flash. Writing only happens on serial command, sparing NVM cycles.

Running it

Install the Arduino megaAVR Boards package in the IDE, select Arduino Nano Every, upload. The USB Serial is separate from hardware Serial1 (RX/TX pins): classic serial code works as is.

Variants

Extend the struct with more fields (it must fit the section size), or add a full dump command for maintenance.

nvm-consignes.ino

// Le 4809 n'a pas d'EEPROM silicium : la lib EEPROM emule en flash.
// Conserve des consignes via la section applicative NVM.
#include <EEPROM.h>

struct Consignes {
  uint16_t seuil;
  uint16_t periode;
  uint32_t signature; // 0xC0FFEE pour valider la structure
};

Consignes cfg;
const int ADRESSE = 0;
const uint32_t SIG = 0xC0FFEE;

void setup() {
  Serial.begin(9600);
  EEPROM.get(ADRESSE, cfg);
  if (cfg.signature != SIG) {
    Serial.println(F("consignes par defaut"));
    cfg.seuil = 512; cfg.periode = 60; cfg.signature = SIG;
    EEPROM.put(ADRESSE, cfg); // premier enregistrement
  }
  Serial.print(F("seuil=")); Serial.print(cfg.seuil);
  Serial.print(F(" periode=")); Serial.println(cfg.periode);
}

void loop() {
  // Reglages par serie : "s123" change le seuil
  static String cmd = "";
  while (Serial.available()) {
    char c = Serial.read();
    if (c == '\n') {
      if (cmd.startsWith("s")) {
        cfg.seuil = cmd.substring(1).toInt();
        EEPROM.put(ADRESSE, cfg);
        Serial.println(F("seuil enregistre"));
      }
      cmd = "";
    } else cmd += c;
  }
}

← Back to the board