fix(eeprom): securiser la sauvegarde Flash en conduite et valider la configuration

- Differer la sauvegarde Flash automatique tant que le vehicule est arme pour eviter le gel et le faux failsafe
- Rejeter la sauvegarde Flash manuelle par le bouton central Enter en conduite
- Reinitialiser la structure ConfigG29 avec memset() pour un padding propre avant memcmp()
- Valider strictement les canaux (1-16, sans doublons), expoVolant et angleSouhaiteTotal en EEPROM
- Detecter les conflits de canaux sur les commandes serie (V=, A=, F=, E=)
- Remplacer la constante magique d'init FFB par ETAPES_INIT_FFB_TOTAL (2)
- Initialiser l'anti-rebond de tous les boutons au branchement USB
- Ne loguer le rapport engage sur la liaison serie que lors d'un reel changement
This commit is contained in:
bleackstorm committed 2026-11-11 22:55:47 +01:00
1 parent bcc8c08ec5
commit 6373ed4371
1 file changed
+72 -12
+72 -12
View File
@@ -57,6 +57,7 @@ const uint16_t GAZ_AVANT_MAX_PAR_VITESSE[3] = {
#define EXPO_DEFAUT 0.25f // 0.0f = Linéaire, 0.25f = Doux au centre (FPU RP2350) #define EXPO_DEFAUT 0.25f // 0.0f = Linéaire, 0.25f = Doux au centre (FPU RP2350)
#define FORCE_RESSORT_DEFAUT 140 // Force du retour au centre : 0 à 255 #define FORCE_RESSORT_DEFAUT 140 // Force du retour au centre : 0 à 255
#define PENTE_RESSORT_G29 7 // Raideur autour du centre : 1 à 7 #define PENTE_RESSORT_G29 7 // Raideur autour du centre : 1 à 7
#define ETAPES_INIT_FFB_TOTAL 2 // Nombre d'étapes d'init FFB (0=Range, 1=Spring)
Adafruit_NeoPixel rgb_led(NUM_PIXELS, PIN_LED_RGB, NEO_GRB + NEO_KHZ800); Adafruit_NeoPixel rgb_led(NUM_PIXELS, PIN_LED_RGB, NEO_GRB + NEO_KHZ800);
Adafruit_USBH_Host USBHost; Adafruit_USBH_Host USBHost;
@@ -143,6 +144,7 @@ void reinitialiserCanauxCRSF() {
} }
void reinitialiserConfigurationDefaut() { void reinitialiserConfigurationDefaut() {
memset((void*)&configSysteme, 0, sizeof(ConfigG29));
configSysteme.volant = 1; configSysteme.volant = 1;
configSysteme.accel = 2; configSysteme.accel = 2;
configSysteme.frein = 3; configSysteme.frein = 3;
@@ -165,7 +167,9 @@ void sauvegarderConfiguration() {
return; // Déjà synchronisé : évite un gel du Cœur 1 et préserve les cycles Flash return; // Déjà synchronisé : évite un gel du Cœur 1 et préserve les cycles Flash
} }
rp2040.wdt_reset();
rp2040.idleOtherCore(); rp2040.idleOtherCore();
memcpy(&cfgActuelle, (const void*)&configSysteme, sizeof(ConfigG29));
EEPROM.put(0, cfgActuelle); EEPROM.put(0, cfgActuelle);
EEPROM.commit(); EEPROM.commit();
rp2040.resumeOtherCore(); rp2040.resumeOtherCore();
@@ -193,9 +197,30 @@ void chargerConfiguration() {
bool conflitArm = configSysteme.volant == CANAL_ARM || configSysteme.accel == CANAL_ARM || bool conflitArm = configSysteme.volant == CANAL_ARM || configSysteme.accel == CANAL_ARM ||
configSysteme.frein == CANAL_ARM || configSysteme.embrayage == CANAL_ARM; configSysteme.frein == CANAL_ARM || configSysteme.embrayage == CANAL_ARM;
if (configSysteme.magicNumber != 0x47 || conflitArm ||
configSysteme.angleSouhaiteTotal < 40 || configSysteme.angleSouhaiteTotal > 900) { bool canauxValides = (configSysteme.volant >= 1 && configSysteme.volant <= 16) &&
(configSysteme.accel >= 1 && configSysteme.accel <= 16) &&
(configSysteme.frein >= 1 && configSysteme.frein <= 16) &&
(configSysteme.embrayage >= 1 && configSysteme.embrayage <= 16);
bool canauxDistincts = (configSysteme.volant != configSysteme.accel) &&
(configSysteme.volant != configSysteme.frein) &&
(configSysteme.volant != configSysteme.embrayage) &&
(configSysteme.accel != configSysteme.frein) &&
(configSysteme.accel != configSysteme.embrayage) &&
(configSysteme.frein != configSysteme.embrayage);
bool expoValide = !isnan(configSysteme.expoVolant) &&
(configSysteme.expoVolant >= 0.0f) &&
(configSysteme.expoVolant <= 0.8f);
bool angleValide = (configSysteme.angleSouhaiteTotal >= 40) &&
(configSysteme.angleSouhaiteTotal <= 900);
if (configSysteme.magicNumber != 0x47 || conflitArm || !canauxValides || !canauxDistincts || !expoValide || !angleValide) {
if (conflitArm) Serial.println("!! Un axe etait mappe sur le canal ARM : configuration reinitialisee !!"); if (conflitArm) Serial.println("!! Un axe etait mappe sur le canal ARM : configuration reinitialisee !!");
else if (!canauxValides || !canauxDistincts) Serial.println("!! Conflit ou canal invalide en EEPROM : configuration reinitialisee !!");
else if (!expoValide || !angleValide) Serial.println("!! Valeur d'angle ou d'expo invalide en EEPROM : configuration reinitialisee !!");
else Serial.println(">> Initialisation configuration d'usine par defaut..."); else Serial.println(">> Initialisation configuration d'usine par defaut...");
reinitialiserConfigurationDefaut(); reinitialiserConfigurationDefaut();
sauvegarderConfiguration(); sauvegarderConfiguration();
@@ -232,11 +257,31 @@ void traiterLigneSerie(char* commande) {
if (periph == 'V' || periph == 'v' || periph == 'A' || periph == 'a' || if (periph == 'V' || periph == 'v' || periph == 'A' || periph == 'a' ||
periph == 'F' || periph == 'f' || periph == 'E' || periph == 'e') { periph == 'F' || periph == 'f' || periph == 'E' || periph == 'e') {
int nouveauCanal = atoi(&commande[2]); int nouveauCanal = atoi(&commande[2]);
if (nouveauCanal < 1 || nouveauCanal > 16) {
Serial.println("!! Canal invalide : doit etre entre 1 et 16 !!");
return;
}
if (nouveauCanal == CANAL_ARM) { if (nouveauCanal == CANAL_ARM) {
Serial.printf("!! Canal %d reserve a l'armement (R2) !!\n", CANAL_ARM); Serial.printf("!! Canal %d reserve a l'armement (R2) !!\n", CANAL_ARM);
return; return;
} }
if (nouveauCanal >= 1 && nouveauCanal <= 16) {
uint8_t cV = configSysteme.volant;
uint8_t cA = configSysteme.accel;
uint8_t cF = configSysteme.frein;
uint8_t cE = configSysteme.embrayage;
bool conflit = false;
switch (periph) {
case 'V': case 'v': if (nouveauCanal == cA || nouveauCanal == cF || nouveauCanal == cE) conflit = true; break;
case 'A': case 'a': if (nouveauCanal == cV || nouveauCanal == cF || nouveauCanal == cE) conflit = true; break;
case 'F': case 'f': if (nouveauCanal == cV || nouveauCanal == cA || nouveauCanal == cE) conflit = true; break;
case 'E': case 'e': if (nouveauCanal == cV || nouveauCanal == cA || nouveauCanal == cF) conflit = true; break;
}
if (conflit) {
Serial.printf("!! Conflit : le canal %d est deja utilise par un autre axe !!\n", nouveauCanal);
return;
}
rp2040.idleOtherCore(); rp2040.idleOtherCore();
switch (periph) { switch (periph) {
case 'V': case 'v': configSysteme.volant = nouveauCanal; break; case 'V': case 'v': configSysteme.volant = nouveauCanal; break;
@@ -248,7 +293,6 @@ void traiterLigneSerie(char* commande) {
rp2040.resumeOtherCore(); rp2040.resumeOtherCore();
sauvegarderConfiguration(); sauvegarderConfiguration();
afficherConfiguration(); afficherConfiguration();
}
} else if (periph == 'D' || periph == 'd') { } else if (periph == 'D' || periph == 'd') {
int val = atoi(&commande[2]); int val = atoi(&commande[2]);
if (val >= 0 && val <= 255) { if (val >= 0 && val <= 255) {
@@ -434,11 +478,15 @@ void loop() {
rp2040.wdt_reset(); rp2040.wdt_reset();
verifierCommandesSerie(); verifierCommandesSerie();
static int8_t dernierRapportLog = 0xFF;
if (majLedRequise) { if (majLedRequise) {
majLedRequise = false; majLedRequise = false;
actualiserLED(); actualiserLED();
if (rapportEngage != dernierRapportLog) {
dernierRapportLog = rapportEngage;
Serial.printf("[BOITE] Rapport engage : %d\n", rapportEngage); Serial.printf("[BOITE] Rapport engage : %d\n", rapportEngage);
} }
}
if (logMountRequis) { if (logMountRequis) {
logMountRequis = false; logMountRequis = false;
@@ -466,19 +514,25 @@ void loop() {
configSysteme.forceRessort, (configSysteme.forceRessort * 100) / 255); configSysteme.forceRessort, (configSysteme.forceRessort * 100) / 255);
} }
// Sauvegarde automatique 2s après la fin du réglage à la molette // Sauvegarde automatique 2s après la fin du réglage à la molette.
// Sécurité : différée tant que le véhicule est armé pour éviter un gel Flash en conduite.
uint32_t momentChgt = momentDernierChangementDurete_ms; uint32_t momentChgt = momentDernierChangementDurete_ms;
if (momentChgt > 0 && (millis() - momentChgt >= 2000)) { if (!estArme && momentChgt > 0 && (millis() - momentChgt >= 2000)) {
momentDernierChangementDurete_ms = 0; momentDernierChangementDurete_ms = 0;
sauvegarderConfiguration(); sauvegarderConfiguration();
} }
// Sauvegarde immédiate lors d'un appui sur le bouton central Enter de la molette // Sauvegarde immédiate lors d'un appui sur le bouton central Enter de la molette
// (uniquement autorisée si désarmé pour des raisons de sécurité)
if (demandeSauvegardeEEPROM) { if (demandeSauvegardeEEPROM) {
demandeSauvegardeEEPROM = false; demandeSauvegardeEEPROM = false;
if (estArme) {
Serial.println("!! Sauvegarde Flash refusee pendant la conduite (desarmer d'abord) !!");
} else {
momentDernierChangementDurete_ms = 0; momentDernierChangementDurete_ms = 0;
sauvegarderConfiguration(); sauvegarderConfiguration();
} }
}
static uint32_t dernierHeartbeat = 0; static uint32_t dernierHeartbeat = 0;
static uint32_t dernierCompteOK = 0; static uint32_t dernierCompteOK = 0;
@@ -488,8 +542,8 @@ void loop() {
uint32_t compteOK = compteurRapportsOK; uint32_t compteOK = compteurRapportsOK;
uint32_t rapportsParSec = ((compteOK - dernierCompteOK) * 1000UL) / (dt ? dt : 1); uint32_t rapportsParSec = ((compteOK - dernierCompteOK) * 1000UL) / (dt ? dt : 1);
dernierCompteOK = compteOK; dernierCompteOK = compteOK;
Serial.printf("[ETAT] G29_Pret=%d (PID=%04X) | InitFFB=%d/6 | Durete=%d/255 | Plage=%d deg | Rapport=%d | Arme=%d | Statut_LED=%d | USB=%lu rap/s | Rejetes=%lu | Failsafe=%d\n", Serial.printf("[ETAT] G29_Pret=%d (PID=%04X) | InitFFB=%d/%d | Durete=%d/255 | Plage=%d deg | Rapport=%d | Arme=%d | Statut_LED=%d | USB=%lu rap/s | Rejetes=%lu | Failsafe=%d\n",
volant_est_pret, g29_pid, etapeInitMoteur, configSysteme.forceRessort, configSysteme.angleSouhaiteTotal, volant_est_pret, g29_pid, etapeInitMoteur, ETAPES_INIT_FFB_TOTAL, configSysteme.forceRessort, configSysteme.angleSouhaiteTotal,
rapportEngage, estArme, statutEnvoiLed, rapportEngage, estArme, statutEnvoiLed,
(unsigned long)rapportsParSec, (unsigned long)compteurRapportsRejetes, failsafeActif); (unsigned long)rapportsParSec, (unsigned long)compteurRapportsRejetes, failsafeActif);
} }
@@ -522,7 +576,7 @@ bool envoyerTrameG29_0x30(const uint8_t cmd7[7]) {
} }
void gererInitMoteurG29() { void gererInitMoteurG29() {
if (!volant_est_pret || etapeInitMoteur >= 6) return; if (!volant_est_pret || etapeInitMoteur >= ETAPES_INIT_FFB_TOTAL) return;
if (millis() - momentConnexionUSB_ms < 600) return; if (millis() - momentConnexionUSB_ms < 600) return;
uint16_t angle = configSysteme.angleSouhaiteTotal; uint16_t angle = configSysteme.angleSouhaiteTotal;
@@ -531,7 +585,7 @@ void gererInitMoteurG29() {
switch (etapeInitMoteur) { switch (etapeInitMoteur) {
case 0: if (envoyerTrameG29_0x30(cmdRange)) etapeInitMoteur++; break; case 0: if (envoyerTrameG29_0x30(cmdRange)) etapeInitMoteur++; break;
case 1: if (envoyerTrameG29_0x30(cmdSpring)) etapeInitMoteur = 6; break; case 1: if (envoyerTrameG29_0x30(cmdSpring)) etapeInitMoteur = ETAPES_INIT_FFB_TOTAL; break;
} }
} }
@@ -576,7 +630,7 @@ void loop1() {
if (volant_est_pret && (millis() - dernierTickG29 >= 50)) { if (volant_est_pret && (millis() - dernierTickG29 >= 50)) {
dernierTickG29 = millis(); dernierTickG29 = millis();
if (etapeInitMoteur < 6) { if (etapeInitMoteur < ETAPES_INIT_FFB_TOTAL) {
gererInitMoteurG29(); gererInitMoteurG29();
return; return;
} }
@@ -600,7 +654,8 @@ void loop1() {
uint8_t masqueCible = 0x00; uint8_t masqueCible = 0x00;
// Si la dureté a été modifiée il y a moins de 1.5s, afficher la jauge de dureté sur les LEDs // Si la dureté a été modifiée il y a moins de 1.5s, afficher la jauge de dureté sur les LEDs
if (momentDernierChangementDurete_ms > 0 && (millis() - momentDernierChangementDurete_ms < 1500)) { uint32_t momentDernierChg = momentDernierChangementDurete_ms;
if (momentDernierChg > 0 && (millis() - momentDernierChg < 1500)) {
uint8_t f = configSysteme.forceRessort; uint8_t f = configSysteme.forceRessort;
if (f >= 240) masqueCible = 0x1F; // 5 LEDs (Rouge) if (f >= 240) masqueCible = 0x1F; // 5 LEDs (Rouge)
else if (f >= 180) masqueCible = 0x0F; // 4 LEDs (Jaune haut) else if (f >= 180) masqueCible = 0x0F; // 4 LEDs (Jaune haut)
@@ -657,6 +712,11 @@ void tuh_hid_mount_cb(uint8_t dev_addr, uint8_t instance, uint8_t const* desc_re
demandeRetourNeutre = false; demandeRetourNeutre = false;
estArme = false; estArme = false;
boutonArmDejaEnfonce = true; // Ignorer R2 s'il est déjà enfoncé au branchement boutonArmDejaEnfonce = true; // Ignorer R2 s'il est déjà enfoncé au branchement
palettePlusDejaEnfoncee = true; // Exiger relâchement des palettes et molette au boot
paletteMoinsDejaEnfoncee= true;
dialPlusDejaEnfonce = true;
dialMoinsDejaEnfonce = true;
dialEnterDejaEnfonce = true;
momentConnexionUSB_ms = millis(); momentConnexionUSB_ms = millis();
dernierRapportUSB_ms = millis(); dernierRapportUSB_ms = millis();
volant_est_pret = true; volant_est_pret = true;