fix(safety,openxr,dvr,config): harden RC/firmware failsafes, OpenXR lifecycle, DVR StreamCopy, relay & persistent config

This commit is contained in:
bleack committed 2026-11-25 22:42:25 +01:00
1 parent 8028680f07
commit d82a65d9e6
24 files changed
+2246 -638

No files matched your search

+205 -103
View File
@@ -177,6 +177,8 @@ volatile uint32_t momentConnexionUSB_ms = 0;
volatile bool logMountRequis = false;
volatile int8_t statutEnvoiLed = 0;
volatile uint32_t heartbeatCore1_ms = 0; // Surveillance santé Cœur 1 (Watchdog dual-core)
volatile bool core1Demarre = false; // Passe à true dès le premier tour de loop1()
uint32_t momentDemarrageCore0_ms = 0; // Grâce de démarrage Cœur 1 (5 s max)
// Failsafe et diagnostic
volatile bool failsafeActif = true; // Écrit uniquement par le cœur 0
@@ -229,7 +231,7 @@ volatile uint16_t etatBoutonsCockpitPrec = 0xFFFF; // Ignore tout bouton mainten
volatile uint8_t etatDpadPrec = 0xFF;
bool cockpitEchoUSB = true; // Actif par défaut pour liaison directe USB-C (/dev/ttyACM0)
uint32_t dernierPaquetHT_ms = 0;
bool htActif = false;
volatile bool htActif = false;
// Pilotage externe (Steam Controller via SteamFrame FPV -> Radxa -> RP2350)
volatile uint8_t sourceActive = SRC_G29;
@@ -270,14 +272,21 @@ static inline void assignerCanalCRSF(uint8_t numeroCh, uint16_t valeur11bits) {
}
}
char serialBuffer[64];
char serialBuffer[96];
uint8_t serialBufferIdx = 0;
char serial2Buffer[64];
bool serialBufferOverflow = false;
char serial2Buffer[96];
uint8_t serial2BufferIdx = 0;
bool serial2BufferOverflow = false;
static inline bool estCanalReserve(uint8_t ch) {
return (ch == CANAL_ARM) || (ch == CANAL_HT_PAN) || (ch == CANAL_HT_TILT) || (ch == CANAL_HT_ROLL);
}
void reinitialiserCanauxCRSF() {
bool ht = __atomic_load_n(&htActif, __ATOMIC_ACQUIRE);
for (int i = 0; i < 16; i++) {
if (htActif && (i == (CANAL_HT_PAN - 1) || i == (CANAL_HT_TILT - 1) || i == (CANAL_HT_ROLL - 1))) {
if (ht && (i == (CANAL_HT_PAN - 1) || i == (CANAL_HT_TILT - 1) || i == (CANAL_HT_ROLL - 1))) {
continue;
}
crsfChannelStore((uint8_t)i, CRSF_VAL_MID);
@@ -355,8 +364,8 @@ void chargerConfiguration() {
EEPROM.get(0, cfgStockee);
memcpy((void*)&configSysteme, &cfgStockee, sizeof(ConfigG29));
bool conflitArm = configSysteme.volant == CANAL_ARM || configSysteme.accel == CANAL_ARM ||
configSysteme.frein == CANAL_ARM || configSysteme.embrayage == CANAL_ARM;
bool conflitReserve = estCanalReserve(configSysteme.volant) || estCanalReserve(configSysteme.accel) ||
estCanalReserve(configSysteme.frein) || estCanalReserve(configSysteme.embrayage);
bool canauxValides = (configSysteme.volant >= 1 && configSysteme.volant <= 16) &&
(configSysteme.accel >= 1 && configSysteme.accel <= 16) &&
@@ -379,15 +388,15 @@ void chargerConfiguration() {
bool vibValide = (configSysteme.intensiteVibration <= 100);
bool vitessesValides = (configSysteme.gazAvantMin >= CRSF_VAL_MID && configSysteme.gazAvantMin <= CRSF_VAL_MAX) &&
(configSysteme.gazAvantMax[0] >= configSysteme.gazAvantMin && configSysteme.gazAvantMax[0] <= CRSF_VAL_MAX) &&
(configSysteme.gazAvantMax[1] >= configSysteme.gazAvantMin && configSysteme.gazAvantMax[1] <= CRSF_VAL_MAX) &&
(configSysteme.gazAvantMax[2] >= configSysteme.gazAvantMin && configSysteme.gazAvantMax[2] <= CRSF_VAL_MAX) &&
(configSysteme.gazArriereMin >= CRSF_VAL_MIN && configSysteme.gazArriereMin <= CRSF_VAL_MID) &&
(configSysteme.gazArriereMax >= CRSF_VAL_MIN && configSysteme.gazArriereMax <= configSysteme.gazArriereMin);
bool vitessesValides = (configSysteme.gazAvantMin >= CRSF_VAL_MID && configSysteme.gazAvantMin < CRSF_VAL_MAX) &&
(configSysteme.gazAvantMax[0] > configSysteme.gazAvantMin && configSysteme.gazAvantMax[0] <= CRSF_VAL_MAX) &&
(configSysteme.gazAvantMax[1] > configSysteme.gazAvantMin && configSysteme.gazAvantMax[1] <= CRSF_VAL_MAX) &&
(configSysteme.gazAvantMax[2] > configSysteme.gazAvantMin && configSysteme.gazAvantMax[2] <= CRSF_VAL_MAX) &&
(configSysteme.gazArriereMin > CRSF_VAL_MIN && configSysteme.gazArriereMin <= CRSF_VAL_MID) &&
(configSysteme.gazArriereMax >= CRSF_VAL_MIN && configSysteme.gazArriereMax < configSysteme.gazArriereMin);
if (configSysteme.magicNumber != MAGIC_NUMBER_CONFIG || conflitArm || !canauxValides || !canauxDistincts || !expoValide || !angleValide || !vibValide || !vitessesValides) {
if (conflitArm) Serial.println("!! Un axe etait mappe sur le canal ARM : configuration reinitialisee !!");
if (configSysteme.magicNumber != MAGIC_NUMBER_CONFIG || conflitReserve || !canauxValides || !canauxDistincts || !expoValide || !angleValide || !vibValide || !vitessesValides) {
if (conflitReserve) Serial.println("!! Un axe etait mappe sur un canal reserve (ARM/HT) : configuration reinitialisee !!");
else if (!canauxValides || !canauxDistincts) Serial.println("!! Conflit ou canal invalide en EEPROM : configuration reinitialisee !!");
else if (!expoValide || !angleValide || !vibValide || !vitessesValides) Serial.println("!! Valeur invalide en EEPROM : configuration reinitialisee !!");
else Serial.println(">> Initialisation configuration d'usine par defaut...");
@@ -412,7 +421,33 @@ void demanderOuExecuterSauvegarde() {
}
}
void traiterLigneSerie(char* commande) {
static bool parseStrictInt(const char* s, int& outVal) {
if (!s || *s == '\0') return false;
char* endPtr = nullptr;
long v = strtol(s, &endPtr, 10);
if (endPtr == s || *endPtr != '\0') return false;
outVal = (int)v;
return true;
}
static bool parseStrictFloat(const char* s, float& outVal) {
if (!s || *s == '\0') return false;
char* endPtr = nullptr;
float v = strtof(s, &endPtr);
if (endPtr == s || *endPtr != '\0' || isnan(v) || isinf(v)) return false;
outVal = v;
return true;
}
uint8_t calculerCrc8Crsf(const uint8_t* data, size_t len);
void traiterLigneSerie(char* commande, bool depuisUsb) {
// Sécurité : les commandes CLI sans CRC (B=BOOTSEL, R=Reset, V=/A=/F=/E=...) ne sont
// acceptées QUE sur l'USB local (Serial), jamais sur l'UART Radxa / UDP (Serial2).
if (!depuisUsb && commande[0] != '$') {
return;
}
if (commande[0] == '?') {
afficherConfiguration();
return;
@@ -454,14 +489,14 @@ void traiterLigneSerie(char* commande) {
extEmbrayagePct = 0;
extVolantPct = 0;
extRcActif = false;
sourceActive = SRC_G29;
majLedRequise = true;
cockpitEtatModifie = true;
__atomic_store_n(&sourceActive, SRC_G29, __ATOMIC_RELEASE);
__atomic_store_n(&majLedRequise, true, __ATOMIC_RELEASE);
__atomic_store_n(&cockpitEtatModifie, true, __ATOMIC_RELEASE);
momentDernierChangementDurete_ms = 0;
sauvegardeDifferee = false;
rp2040.resumeOtherCore();
majRangeRequise = true;
majRessortRequise = true;
__atomic_store_n(&majRangeRequise, true, __ATOMIC_RELEASE);
__atomic_store_n(&majRessortRequise, true, __ATOMIC_RELEASE);
sauvegarderConfiguration();
afficherConfiguration();
return;
@@ -474,13 +509,14 @@ void traiterLigneSerie(char* commande) {
Serial.println("!! Remappage des canaux refuse pendant la conduite (desarmer d'abord) !!");
return;
}
int nouveauCanal = atoi(&commande[2]);
if (nouveauCanal < 1 || nouveauCanal > 16) {
Serial.println("!! Canal invalide : doit etre entre 1 et 16 !!");
int nouveauCanal = 0;
if (!parseStrictInt(&commande[2], nouveauCanal) || nouveauCanal < 1 || nouveauCanal > 16) {
Serial.println("!! Canal invalide : doit etre un entier entre 1 et 16 !!");
return;
}
if (nouveauCanal == CANAL_ARM) {
Serial.printf("!! Canal %d reserve a l'armement (R2) !!\n", CANAL_ARM);
if (estCanalReserve((uint8_t)nouveauCanal)) {
Serial.printf("!! Canal %d reserve (ARM=CH%d, HT=CH%d..CH%d) !!\n",
nouveauCanal, CANAL_ARM, CANAL_HT_PAN, CANAL_HT_ROLL);
return;
}
@@ -512,60 +548,63 @@ void traiterLigneSerie(char* commande) {
sauvegarderConfiguration();
afficherConfiguration();
} else if (periph == 'D' || periph == 'd') {
int val = atoi(&commande[2]);
if (val >= 0 && val <= 255) {
int val = 0;
if (parseStrictInt(&commande[2], val) && val >= 0 && val <= 255) {
configSysteme.forceRessort = (uint8_t)val; // Atomique sur ARM Cortex-M33
majRessortRequise = true;
__atomic_store_n(&majRessortRequise, true, __ATOMIC_RELEASE);
demanderOuExecuterSauvegarde();
afficherConfiguration();
} else {
Serial.println("!! Durete (D) doit etre entre 0 et 255 !!");
Serial.println("!! Durete (D) doit etre un entier entre 0 et 255 !!");
}
} else if (periph == 'P' || periph == 'p') {
if (estActuellementArme()) {
Serial.println("!! Reglage de plage (P) refuse pendant la conduite (desarmer d'abord) !!");
return;
}
int val = atoi(&commande[2]);
if (val >= 40 && val <= 900) {
int val = 0;
if (parseStrictInt(&commande[2], val) && val >= 40 && val <= 900) {
rp2040.idleOtherCore();
configSysteme.angleSouhaiteTotal = (uint16_t)val;
recalculerParametresVolant();
rp2040.resumeOtherCore();
majRangeRequise = true;
__atomic_store_n(&majRangeRequise, true, __ATOMIC_RELEASE);
sauvegarderConfiguration();
afficherConfiguration();
} else {
Serial.println("!! Plage d'angle (P) doit etre entre 40 et 900 degres !!");
Serial.println("!! Plage d'angle (P) doit etre un entier entre 40 et 900 degres !!");
}
} else if (periph == 'X' || periph == 'x') {
if (estActuellementArme()) {
Serial.println("!! Reglage d'expo (X) refuse pendant la conduite (desarmer d'abord) !!");
return;
}
float val = atof(&commande[2]);
if (val >= 0.0f && val <= 0.8f) {
float val = 0.0f;
if (parseStrictFloat(&commande[2], val) && val >= 0.0f && val <= 0.8f) {
rp2040.idleOtherCore();
configSysteme.expoVolant = val;
rp2040.resumeOtherCore();
sauvegarderConfiguration();
afficherConfiguration();
} else {
Serial.println("!! Expo volant (X) doit etre entre 0.0 et 0.8 !!");
Serial.println("!! Expo volant (X) doit etre un nombre entre 0.0 et 0.8 !!");
}
} else if (periph == 'T' || periph == 't') {
int val = atoi(&commande[2]);
if (val >= 0 && val <= 100) {
int val = 0;
if (parseStrictInt(&commande[2], val) && val >= 0 && val <= 100) {
configSysteme.intensiteVibration = (uint8_t)val; // Atomique sur ARM Cortex-M33
cockpitEtatModifie = true;
__atomic_store_n(&cockpitEtatModifie, true, __ATOMIC_RELEASE);
demanderOuExecuterSauvegarde();
afficherConfiguration();
} else {
Serial.println("!! Vibrations (T) doit etre entre 0 et 100% !!");
Serial.println("!! Vibrations (T) doit etre un entier entre 0 et 100% !!");
}
} else if (periph == 'W' || periph == 'w') {
cockpitEchoUSB = (atoi(&commande[2]) != 0);
Serial.printf(">> [COCKPIT] Echo trames $G29 sur USB : %s\n", cockpitEchoUSB ? "ON" : "OFF");
int val = 0;
if (parseStrictInt(&commande[2], val)) {
cockpitEchoUSB = (val != 0);
Serial.printf(">> [COCKPIT] Echo trames $G29 sur USB : %s\n", cockpitEchoUSB ? "ON" : "OFF");
}
}
return;
}
@@ -624,22 +663,24 @@ void traiterLigneSerie(char* commande) {
return;
}
rp2040.idleOtherCore();
sourceActive = cible;
__atomic_store_n(&sourceActive, cible, __ATOMIC_RELEASE);
extRcActif = false;
extArme = false;
extRapport = 0;
__atomic_store_n(&extArme, false, __ATOMIC_RELEASE);
__atomic_store_n(&extRapport, 0, __ATOMIC_RELEASE);
extAccelPct = 0;
extFreinPct = 0;
estArme = false;
rapportEngage = 0;
extEmbrayagePct = 0;
extVolantPct = 0;
__atomic_store_n(&estArme, false, __ATOMIC_RELEASE);
__atomic_store_n(&rapportEngage, 0, __ATOMIC_RELEASE);
boutonArmDejaEnfonce = true;
palettePlusDejaEnfoncee = true;
paletteMoinsDejaEnfoncee = true;
etatBoutonsCockpitPrec = 0xFFFF;
reinitialiserCanauxCRSF();
rp2040.resumeOtherCore();
majLedRequise = true;
cockpitEtatModifie = true;
__atomic_store_n(&majLedRequise, true, __ATOMIC_RELEASE);
__atomic_store_n(&cockpitEtatModifie, true, __ATOMIC_RELEASE);
Serial.printf(">> [SRC] Source active basculee sur : %s\n",
(sourceActive == SRC_EXT) ? "EXT (Steam Controller)" : "G29 (Volant)");
return;
@@ -650,12 +691,13 @@ void traiterLigneSerie(char* commande) {
if (strncmp(commande, "$GEARS,", 7) == 0) {
int fMin = 0, g1 = 0, g2 = 0, g3 = 0, rMin = 0, rMax = 0;
if (sscanf(commande + 7, "%d,%d,%d,%d,%d,%d", &fMin, &g1, &g2, &g3, &rMin, &rMax) == 6) {
uint16_t vFMin = (uint16_t)constrain(fMin, CRSF_VAL_MID, CRSF_VAL_MAX);
uint16_t vG1 = (uint16_t)constrain(g1, (int)vFMin, CRSF_VAL_MAX);
uint16_t vG2 = (uint16_t)constrain(g2, (int)vFMin, CRSF_VAL_MAX);
uint16_t vG3 = (uint16_t)constrain(g3, (int)vFMin, CRSF_VAL_MAX);
uint16_t vRMin = (uint16_t)constrain(rMin, CRSF_VAL_MIN, CRSF_VAL_MID);
uint16_t vRMax = (uint16_t)constrain(rMax, CRSF_VAL_MIN, (int)vRMin);
// Bornes strictes (max > min en AV, max < min en AR) pour éliminer toute division par zéro dans map().
uint16_t vFMin = (uint16_t)constrain(fMin, CRSF_VAL_MID, CRSF_VAL_MAX - 1);
uint16_t vG1 = (uint16_t)constrain(g1, (int)vFMin + 1, CRSF_VAL_MAX);
uint16_t vG2 = (uint16_t)constrain(g2, (int)vFMin + 1, CRSF_VAL_MAX);
uint16_t vG3 = (uint16_t)constrain(g3, (int)vFMin + 1, CRSF_VAL_MAX);
uint16_t vRMin = (uint16_t)constrain(rMin, CRSF_VAL_MIN + 1, CRSF_VAL_MID);
uint16_t vRMax = (uint16_t)constrain(rMax, CRSF_VAL_MIN, (int)vRMin - 1);
bool modifie = (configSysteme.gazAvantMin != vFMin) ||
(configSysteme.gazAvantMax[0] != vG1) ||
@@ -664,17 +706,32 @@ void traiterLigneSerie(char* commande) {
(configSysteme.gazArriereMin != vRMin) ||
(configSysteme.gazArriereMax != vRMax);
configSysteme.gazAvantMin = vFMin;
configSysteme.gazAvantMax[0] = vG1;
configSysteme.gazAvantMax[1] = vG2;
configSysteme.gazAvantMax[2] = vG3;
configSysteme.gazArriereMin = vRMin;
configSysteme.gazArriereMax = vRMax;
if (modifie) {
sauvegarderConfiguration();
Serial.printf(">> [VITESSES] AV_MIN=%u | 1=%u | 2=%u | 3=%u | AR_MIN=%u | AR_MAX=%u\n",
vFMin, vG1, vG2, vG3, vRMin, vRMax);
if (estActuellementArme()) {
// Sécurité : jamais d'idleOtherCore / EEPROM.commit() en conduite armée (gel Flash 50-100 ms).
// On applique les plafonds en RAM et on diffère l'écriture Flash au désarmement.
configSysteme.gazAvantMin = vFMin;
configSysteme.gazAvantMax[0] = vG1;
configSysteme.gazAvantMax[1] = vG2;
configSysteme.gazAvantMax[2] = vG3;
configSysteme.gazArriereMin = vRMin;
configSysteme.gazArriereMax = vRMax;
sauvegardeDifferee = true;
Serial.printf(">> [VITESSES] Maj RAM (Flash differee au desarmement) : AV_MIN=%u | 1=%u | 2=%u | 3=%u | AR_MIN=%u | AR_MAX=%u\n",
vFMin, vG1, vG2, vG3, vRMin, vRMax);
} else {
rp2040.idleOtherCore();
configSysteme.gazAvantMin = vFMin;
configSysteme.gazAvantMax[0] = vG1;
configSysteme.gazAvantMax[1] = vG2;
configSysteme.gazAvantMax[2] = vG3;
configSysteme.gazArriereMin = vRMin;
configSysteme.gazArriereMax = vRMax;
rp2040.resumeOtherCore();
sauvegarderConfiguration();
Serial.printf(">> [VITESSES] AV_MIN=%u | 1=%u | 2=%u | 3=%u | AR_MIN=%u | AR_MAX=%u\n",
vFMin, vG1, vG2, vG3, vRMin, vRMax);
}
}
}
return;
@@ -683,7 +740,7 @@ void traiterLigneSerie(char* commande) {
// Injection conduite externe (Steam Controller via SteamFrame FPV) :
// $RC,<steer>,<accel>,<brake>,<clutch>,<arm>,<gear>
if (strncmp(commande, "$RC,", 4) == 0) {
if (sourceActive != SRC_EXT) {
if (__atomic_load_n(&sourceActive, __ATOMIC_ACQUIRE) != SRC_EXT) {
return;
}
int steer = CRSF_VAL_MID, accel = CRSF_VAL_MID, brake = CRSF_VAL_MID, clutch = CRSF_VAL_MID;
@@ -699,38 +756,48 @@ void traiterLigneSerie(char* commande) {
// Sécurité : refuser un armement (0 -> 1) si rapport != N ou gaz non neutres
if (demandeArm && !extArme) {
if (vGear == 0 && vAccel == CRSF_VAL_MID) {
extArme = true;
__atomic_store_n(&extArme, true, __ATOMIC_RELEASE);
momentArmement_ms = millis();
momentDesarmement_ms = 0;
majLedRequise = true;
cockpitEtatModifie = true;
__atomic_store_n(&majLedRequise, true, __ATOMIC_RELEASE);
__atomic_store_n(&cockpitEtatModifie, true, __ATOMIC_RELEASE);
Serial.println(">> [ARM EXT] ARME (Steam Controller)");
}
} else if (!demandeArm && extArme) {
extArme = false;
__atomic_store_n(&extArme, false, __ATOMIC_RELEASE);
momentDesarmement_ms = millis();
momentArmement_ms = 0;
majLedRequise = true;
cockpitEtatModifie = true;
__atomic_store_n(&majLedRequise, true, __ATOMIC_RELEASE);
__atomic_store_n(&cockpitEtatModifie, true, __ATOMIC_RELEASE);
Serial.println(">> [ARM EXT] DESARME (Steam Controller)");
}
if (vGear != extRapport) {
extRapport = vGear;
majLedRequise = true;
cockpitEtatModifie = true;
__atomic_store_n(&extRapport, vGear, __ATOMIC_RELEASE);
__atomic_store_n(&majLedRequise, true, __ATOMIC_RELEASE);
__atomic_store_n(&cockpitEtatModifie, true, __ATOMIC_RELEASE);
}
// Défense en profondeur : appliquer aussi les plafonds CRSF par rapport côté RP2350
if (!extArme || extRapport == 0) {
vAccel = CRSF_VAL_MID;
} else if (extRapport >= 1 && extRapport <= 3) {
uint16_t maxV = configSysteme.gazAvantMax[extRapport - 1];
vAccel = (uint16_t)constrain((int)vAccel, CRSF_VAL_MID, (int)maxV);
} else if (extRapport == -1) {
vAccel = (uint16_t)constrain((int)vAccel, (int)configSysteme.gazArriereMax, CRSF_VAL_MID);
}
uint8_t accelPct = 0;
if (extArme && extRapport >= 1 && extRapport <= 3 && vAccel >= configSysteme.gazAvantMin) {
uint16_t maxV = configSysteme.gazAvantMax[extRapport - 1];
accelPct = (uint8_t)constrain(map(vAccel, configSysteme.gazAvantMin, maxV, 0, 100), 0, 100);
if (maxV > configSysteme.gazAvantMin) {
accelPct = (uint8_t)constrain(map(vAccel, configSysteme.gazAvantMin, maxV, 0, 100), 0, 100);
}
} else if (extArme && extRapport == -1 && vAccel <= configSysteme.gazArriereMin) {
accelPct = (uint8_t)constrain(map(vAccel, configSysteme.gazArriereMin, configSysteme.gazArriereMax, 0, 100), 0, 100);
if (configSysteme.gazArriereMin > configSysteme.gazArriereMax) {
accelPct = (uint8_t)constrain(map(vAccel, configSysteme.gazArriereMin, configSysteme.gazArriereMax, 0, 100), 0, 100);
}
}
uint8_t brakePct = (vBrake > CRSF_VAL_MID)
? (uint8_t)constrain(map(vBrake, CRSF_VAL_MID, CRSF_VAL_MAX, 0, 100), 0, 100)
@@ -756,34 +823,39 @@ void traiterLigneSerie(char* commande) {
dernierPaquetRC_ms = millis();
if (!extRcActif) {
extRcActif = true;
majLedRequise = true;
cockpitEtatModifie = true;
__atomic_store_n(&majLedRequise, true, __ATOMIC_RELEASE);
__atomic_store_n(&cockpitEtatModifie, true, __ATOMIC_RELEASE);
}
}
return;
}
}
void lireFluxSerie(Stream& port, char* buf, uint8_t& idx, size_t maxLen) {
while (port.available() > 0) {
void lireFluxSerie(Stream& port, char* buf, uint8_t& idx, bool& overflow, size_t maxLen, bool depuisUsb) {
int budget = 96; // Borner le temps passé dans loop() pour préserver la cadence CRSF 166 Hz
while (port.available() > 0 && budget-- > 0) {
char c = (char)port.read();
if (c == '\n' || c == '\r') {
if (idx > 0) {
if (!overflow && idx > 0) {
buf[idx] = '\0';
traiterLigneSerie(buf);
idx = 0;
traiterLigneSerie(buf, depuisUsb);
}
idx = 0;
overflow = false;
} else if (overflow) {
// Ignorer le reste de la ligne trop longue jusqu'au prochain retour à la ligne
} else if (idx < maxLen - 1) {
buf[idx++] = c;
} else {
idx = 0; // Ligne trop longue, reset
idx = 0;
overflow = true;
}
}
}
void verifierCommandesSerie() {
lireFluxSerie(Serial, serialBuffer, serialBufferIdx, sizeof(serialBuffer));
lireFluxSerie(Serial2, serial2Buffer, serial2BufferIdx, sizeof(serial2Buffer));
lireFluxSerie(Serial, serialBuffer, serialBufferIdx, serialBufferOverflow, sizeof(serialBuffer), true);
lireFluxSerie(Serial2, serial2Buffer, serial2BufferIdx, serial2BufferOverflow, sizeof(serial2Buffer), false);
}
// -------------------------------------------------------------------------
@@ -896,15 +968,19 @@ void setup() {
Serial1.setTX(0);
Serial1.setRX(1);
Serial1.setFIFOSize(256);
Serial1.begin(420000);
Serial2.setTX(PIN_UART_BRIDGE_TX);
Serial2.setRX(PIN_UART_BRIDGE_RX);
Serial2.setFIFOSize(512);
Serial2.begin(UART_BRIDGE_BAUD);
Serial.println("\n--- WAVESHARE RP2350-USB-A PRETE ---");
afficherConfiguration();
momentDemarrageCore0_ms = millis();
// Watchdog matériel : reboot si loop() ne le nourrit plus pendant WATCHDOG_TIMEOUT_MS
if (watchdog_caused_reboot()) {
Serial.println("!! [WATCHDOG] Redemarrage suite a un blocage !!");
@@ -912,15 +988,24 @@ void setup() {
rp2040.wdt_begin(WATCHDOG_TIMEOUT_MS);
}
static inline bool coeur1EstSain(uint32_t now) {
if (__atomic_load_n(&core1Demarre, __ATOMIC_ACQUIRE)) {
uint32_t hb = __atomic_load_n(&heartbeatCore1_ms, __ATOMIC_ACQUIRE);
return (now - hb) <= TIMEOUT_FAILSAFE_MS;
}
// Grâce de démarrage de 5 s pour laisser setup1() initialiser PIO-USB ; au-delà, faute Core 1.
return (now - momentDemarrageCore0_ms) <= 5000UL;
}
// Évalue le failsafe (cœur 0). Retourne true si les canaux doivent être forcés au neutre.
bool evaluerFailsafe() {
uint32_t now = millis();
// 1. Surveillance de la santé du Cœur 1 (Watchdog logique inter-cœur)
if (heartbeatCore1_ms > 0 && (now - heartbeatCore1_ms > TIMEOUT_FAILSAFE_MS)) {
if (!coeur1EstSain(now)) {
if (!failsafeActif) {
failsafeActif = true;
majLedRequise = true;
__atomic_store_n(&majLedRequise, true, __ATOMIC_RELEASE);
Serial.println(F("!! [FAILSAFE] Perte de reponse du Coeur 1 (Watchdog) !!"));
}
return true;
@@ -1089,10 +1174,24 @@ void traiterTelemetrieCRSF() {
}
void emettreLigneCockpit(const char* ligne) {
Serial2.println(ligne);
int needed = (int)strlen(ligne) + 2;
char framed[192];
const char* toSend = ligne;
// Ajouter un CRC-8 DVB-S2 (*XX) sur les trames $G29 et $G29_EVT (protège la liaison UART 460k)
if (strncmp(ligne, "$G29,", 5) == 0 || strncmp(ligne, "$G29_EVT,", 9) == 0) {
size_t len = strlen(ligne);
if (len + 4 < sizeof(framed)) {
uint8_t crc = calculerCrc8Crsf((const uint8_t*)ligne, len);
snprintf(framed, sizeof(framed), "%s*%02X", ligne, crc);
toSend = framed;
}
}
int needed = (int)strlen(toSend) + 2;
// SerialUART::availableForWrite() sur arduino-pico renvoie >= 1 si de la place est libre en FIFO
if (Serial2.availableForWrite() > 0) {
Serial2.println(toSend);
}
if (cockpitEchoUSB && Serial && Serial.availableForWrite() >= needed) {
Serial.println(ligne);
Serial.println(toSend);
}
}
@@ -1198,8 +1297,8 @@ void gererTimeoutRcExterne() {
}
void loop() {
// Watchdog dual-core : ne réarmer le watchdog matériel que si le Cœur 1 est également vivant
if (heartbeatCore1_ms == 0 || (millis() - heartbeatCore1_ms <= TIMEOUT_FAILSAFE_MS)) {
// Watchdog dual-core : ne réarmer le watchdog matériel que si le Cœur 1 est vivant (ou dans la grâce boot 5 s)
if (coeur1EstSain(millis())) {
rp2040.wdt_reset();
}
verifierCommandesSerie();
@@ -1213,8 +1312,8 @@ void loop() {
bool enAnimationArm = (momentArmement_ms > 0 && (millis() - momentArmement_ms < 850)) ||
(momentDesarmement_ms > 0 && (millis() - momentDesarmement_ms < 250));
if (majLedRequise || (enAnimationArm && (millis() - dernierTickAnimNeo_ms >= 50))) {
majLedRequise = false;
if (__atomic_exchange_n(&majLedRequise, false, __ATOMIC_ACQ_REL)
|| (enAnimationArm && (millis() - dernierTickAnimNeo_ms >= 50))) {
dernierTickAnimNeo_ms = millis();
actualiserLED();
int8_t rap = rapportActuel();
@@ -1596,7 +1695,8 @@ void setup1() {
}
void loop1() {
heartbeatCore1_ms = millis();
__atomic_store_n(&heartbeatCore1_ms, millis(), __ATOMIC_RELEASE);
__atomic_store_n(&core1Demarre, true, __ATOMIC_RELEASE);
USBHost.task();
if (!volant_est_pret) return;
@@ -1638,20 +1738,22 @@ void loop1() {
}
// 4. Envoi immédiat des trames prioritaires dès que le pipe USB est libre (sans bloquer loop1)
if (majRangeRequise) {
if (__atomic_load_n(&majRangeRequise, __ATOMIC_ACQUIRE)) {
if (envoyerPlageG29(configSysteme.angleSouhaiteTotal)) {
majRangeRequise = false;
__atomic_store_n(&majRangeRequise, false, __ATOMIC_RELEASE);
}
}
if (majRessortRequise) {
if (__atomic_load_n(&majRessortRequise, __ATOMIC_ACQUIRE)) {
if (envoyerRessortG29(configSysteme.forceRessort)) {
majRessortRequise = false;
__atomic_store_n(&majRessortRequise, false, __ATOMIC_RELEASE);
}
}
// Arrêt prioritaire du moteur de vibration dès le désarmement (ou si source externe active)
if ((sourceActive != SRC_G29 || !estArme) && !forceEstAuNeutre) {
// Arrêt prioritaire du moteur de vibration dès le désarmement (ou si source externe active),
// sauf si une butée logicielle (900°) est activement en cours pour éviter le battement à 25 Hz.
if ((__atomic_load_n(&sourceActive, __ATOMIC_ACQUIRE) != SRC_G29 || !estArme)
&& !murActifDroite && !murActifGauche && !forceEstAuNeutre) {
if (arreterForceG29()) {
forceEstAuNeutre = true;
return;