Skip to content

A Hardware Hacking Tool with Web-Based CLI That Speaks Every Protocol

add uart service method to detect baud based on pin activity

Geo39891265e44d
diff --git a/src/Services/UartService.cpp b/src/Services/UartService.cpp
index b1d39b7a..097cd385 100644
--- a/src/Services/UartService.cpp
+++ b/src/Services/UartService.cpp
@@ -372,4 +372,66 @@ std::vector<UartService::PinActivity> UartService::scanUartActivity(const std::v
});
return out;
+}
+
+uint32_t UartService::detectBaudByEdge(
+ uint8_t pin,
+ uint32_t totalMs,
+ uint32_t windowMs,
+ uint32_t minEdges,
+ bool pullup
+) {
+ std::map<uint32_t, uint32_t> votes;
+ uint32_t bestBaud = 0;
+ uint32_t bestVotes = 0;
+
+ uint32_t consecutiveBaud = 0;
+ uint32_t consecutiveCount = 0;
+
+ unsigned long start = millis();
+
+ while (millis() - start < totalMs) {
+ auto a = measureUartActivity(pin, windowMs, pullup);
+
+ // not enough edges or no valid baud
+ if (a.edges < minEdges || a.approxBaud == 0) {
+ consecutiveBaud = 0;
+ consecutiveCount = 0;
+ continue;
+ }
+
+ // to closest standard baudrate
+ uint32_t approx = a.approxBaud;
+ uint32_t snapped = kBaudRates[0];
+ uint32_t bestDiff = (approx > snapped) ? (approx - snapped) : (snapped - approx);
+
+ for (size_t i = 1; i < kBaudRatesCount; i++) {
+ uint32_t b = kBaudRates[i];
+ uint32_t diff = (approx > b) ? (approx - b) : (b - approx);
+ if (diff < bestDiff) {
+ bestDiff = diff;
+ snapped = b;
+ }
+ }
+
+ votes[snapped]++;
+
+ // track best
+ if (votes[snapped] > bestVotes) {
+ bestVotes = votes[snapped];
+ bestBaud = snapped;
+ }
+
+ // check for consecutive agreement
+ if (snapped == consecutiveBaud) consecutiveCount++;
+ else {
+ consecutiveBaud = snapped;
+ consecutiveCount = 1;
+ }
+
+ if (consecutiveCount >= 2) return consecutiveBaud;
+ if (bestVotes >= 3) return bestBaud;
+ }
+
+ return 0;
}
\ No newline at end of file
diff --git a/src/Services/UartService.h b/src/Services/UartService.h
index 5f6461d6..43b9211c 100644
--- a/src/Services/UartService.h
+++ b/src/Services/UartService.h
@@ -10,6 +10,7 @@
#include "soc/uart_periph.h"
#include "Models/ByteCode.h"
#include <SD.h>
+#include <map>
#define UART_PORT UART_NUM_1
@@ -53,6 +54,7 @@ public:
uint32_t windowMs = 100,
uint32_t minEdges = 10,
bool pullup = true);
+ uint32_t detectBaudByEdge(uint8_t pin, uint32_t totalMs = 5000, uint32_t windowMs = 300, uint32_t minEdges = 30, bool pullup = true);
private:
XModem xmodem;
@@ -66,4 +68,27 @@ private:
inline static volatile uint32_t lastEdgeTimeUs = 0;
inline static volatile uint32_t edgeIntervals[64] = {0};
inline static volatile uint8_t intervalCount = 0;
+
+ inline static constexpr uint32_t kBaudRates[] = {
+ // Legacy
+ 110, 300, 600, 1200, 1800, 2000, 2400, 3600, 4800, 7200,
+ 9600, 10400, 14400, 16000, 19200,
+
+ // Mid-range
+ 28800, 31250, 32000, 33600, 38400,
+ 56000, 57600, 64000, 76800,
+
+ // High
+ 100000, 115200, 125000, 128000, 153600,
+ 200000, 230400, 250000, 256000, 307200,
+
+ // Very high
+ 460800, 500000, 576000, 614400, 750000,
+ 921600, 1000000, 1152000, 1228800, 1500000,
+
+ // Extreme
+ 2000000, 2500000, 3000000, 3500000, 4000000
+ };
+
+ static constexpr size_t kBaudRatesCount = sizeof(kBaudRates) / sizeof(kBaudRates[0]);
};