From 6373ed4371f72febce1ebf4e8093517ed439ad14 Mon Sep 17 00:00:00 2001 From: bleackstorm <49142331+bleackstorm@users.noreply.github.com> Date: Wed, 11 Nov 2026 22:55:47 +0100 Subject: [PATCH] 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 --- g29_crsf.ino | 112 +++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 86 insertions(+), 26 deletions(-) diff --git a/g29_crsf.ino b/g29_crsf.ino index 50b23c7..ee3c35f 100644 --- a/g29_crsf.ino +++ b/g29_crsf.ino @@ -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 FORCE_RESSORT_DEFAUT 140 // Force du retour au centre : 0 à 255 #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_USBH_Host USBHost; @@ -143,6 +144,7 @@ void reinitialiserCanauxCRSF() { } void reinitialiserConfigurationDefaut() { + memset((void*)&configSysteme, 0, sizeof(ConfigG29)); configSysteme.volant = 1; configSysteme.accel = 2; 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 } + rp2040.wdt_reset(); rp2040.idleOtherCore(); + memcpy(&cfgActuelle, (const void*)&configSysteme, sizeof(ConfigG29)); EEPROM.put(0, cfgActuelle); EEPROM.commit(); rp2040.resumeOtherCore(); @@ -193,9 +197,30 @@ void chargerConfiguration() { bool conflitArm = configSysteme.volant == CANAL_ARM || configSysteme.accel == 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 !!"); + 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..."); reinitialiserConfigurationDefaut(); sauvegarderConfiguration(); @@ -232,23 +257,42 @@ void traiterLigneSerie(char* commande) { if (periph == 'V' || periph == 'v' || periph == 'A' || periph == 'a' || periph == 'F' || periph == 'f' || periph == 'E' || periph == 'e') { 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) { Serial.printf("!! Canal %d reserve a l'armement (R2) !!\n", CANAL_ARM); return; } - if (nouveauCanal >= 1 && nouveauCanal <= 16) { - rp2040.idleOtherCore(); - switch (periph) { - case 'V': case 'v': configSysteme.volant = nouveauCanal; break; - case 'A': case 'a': configSysteme.accel = nouveauCanal; break; - case 'F': case 'f': configSysteme.frein = nouveauCanal; break; - case 'E': case 'e': configSysteme.embrayage = nouveauCanal; break; - } - reinitialiserCanauxCRSF(); - rp2040.resumeOtherCore(); - sauvegarderConfiguration(); - afficherConfiguration(); + + 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(); + switch (periph) { + case 'V': case 'v': configSysteme.volant = nouveauCanal; break; + case 'A': case 'a': configSysteme.accel = nouveauCanal; break; + case 'F': case 'f': configSysteme.frein = nouveauCanal; break; + case 'E': case 'e': configSysteme.embrayage = nouveauCanal; break; + } + reinitialiserCanauxCRSF(); + rp2040.resumeOtherCore(); + sauvegarderConfiguration(); + afficherConfiguration(); } else if (periph == 'D' || periph == 'd') { int val = atoi(&commande[2]); if (val >= 0 && val <= 255) { @@ -434,10 +478,14 @@ void loop() { rp2040.wdt_reset(); verifierCommandesSerie(); + static int8_t dernierRapportLog = 0xFF; if (majLedRequise) { majLedRequise = false; actualiserLED(); - Serial.printf("[BOITE] Rapport engage : %d\n", rapportEngage); + if (rapportEngage != dernierRapportLog) { + dernierRapportLog = rapportEngage; + Serial.printf("[BOITE] Rapport engage : %d\n", rapportEngage); + } } if (logMountRequis) { @@ -466,18 +514,24 @@ void loop() { 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; - if (momentChgt > 0 && (millis() - momentChgt >= 2000)) { + if (!estArme && momentChgt > 0 && (millis() - momentChgt >= 2000)) { momentDernierChangementDurete_ms = 0; sauvegarderConfiguration(); } // 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) { demandeSauvegardeEEPROM = false; - momentDernierChangementDurete_ms = 0; - sauvegarderConfiguration(); + if (estArme) { + Serial.println("!! Sauvegarde Flash refusee pendant la conduite (desarmer d'abord) !!"); + } else { + momentDernierChangementDurete_ms = 0; + sauvegarderConfiguration(); + } } static uint32_t dernierHeartbeat = 0; @@ -488,8 +542,8 @@ void loop() { uint32_t compteOK = compteurRapportsOK; uint32_t rapportsParSec = ((compteOK - dernierCompteOK) * 1000UL) / (dt ? dt : 1); 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", - volant_est_pret, g29_pid, etapeInitMoteur, configSysteme.forceRessort, configSysteme.angleSouhaiteTotal, + 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, ETAPES_INIT_FFB_TOTAL, configSysteme.forceRessort, configSysteme.angleSouhaiteTotal, rapportEngage, estArme, statutEnvoiLed, (unsigned long)rapportsParSec, (unsigned long)compteurRapportsRejetes, failsafeActif); } @@ -522,7 +576,7 @@ bool envoyerTrameG29_0x30(const uint8_t cmd7[7]) { } 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; uint16_t angle = configSysteme.angleSouhaiteTotal; @@ -531,7 +585,7 @@ void gererInitMoteurG29() { switch (etapeInitMoteur) { 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)) { dernierTickG29 = millis(); - if (etapeInitMoteur < 6) { + if (etapeInitMoteur < ETAPES_INIT_FFB_TOTAL) { gererInitMoteurG29(); return; } @@ -600,7 +654,8 @@ void loop1() { 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 - if (momentDernierChangementDurete_ms > 0 && (millis() - momentDernierChangementDurete_ms < 1500)) { + uint32_t momentDernierChg = momentDernierChangementDurete_ms; + if (momentDernierChg > 0 && (millis() - momentDernierChg < 1500)) { uint8_t f = configSysteme.forceRessort; if (f >= 240) masqueCible = 0x1F; // 5 LEDs (Rouge) else if (f >= 180) masqueCible = 0x0F; // 4 LEDs (Jaune haut) @@ -656,7 +711,12 @@ void tuh_hid_mount_cb(uint8_t dev_addr, uint8_t instance, uint8_t const* desc_re rapportEngage = 0; demandeRetourNeutre = 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(); dernierRapportUSB_ms = millis(); volant_est_pret = true;