Rebuild obu-firmware on Niklas Dathe's microbu-firmware
Replace the vanetza-idf port with his current station firmware (microbu-firmware e4e4bd8: main/, components/microbu_v2x, components/vanetza, external/vanetza) and re-apply the MicrOBU additions, each marked "MicrOBU:" in the sources: - V2X_RX (opcode 0x90): every received ITS message, signed or not, through gn_unwrap.c, because the stack drops unsigned and foreign-root traffic. 0x85 is the spec's RAW_FRAME and 0x86 is reserved. The body is built in v2x_rx.hpp. - Unsigned transmission (geonet.c) for security profile 1 or station security 0, from a random MAC per configuration. - Radio polled from STATION_CONFIGURE on; manual country policy, TX power read-back, static RX buffer, short frames ignored. - Battery guard (battery_policy.hpp): deep sleep below 3.30 V with no phone on USB. - No USB writes without a host; last_frame_ms() for the guard. - 80 KB NVS with the app at 0x20000, 8 MB flash, XIAO ESP32-C5 board, 20 dBm. Remove the old C firmware, the bench listener and the old vanetza-idf copy. Add docs/station-link.tex as revision 2 of the interface reference (V2X_RX, unsigned sending, battery rule, revision history). Add 39 host unit tests (make firmware in test/host) next to the existing chain, replay, fuzz and battery tests, and point tools/station_status_watch.py at the new CONFIRM-based status.
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
#pragma once
|
||||
// Cell-voltage policy for the XIAO ESP32-C5's LiPo: smoothing, the low-battery warning with
|
||||
// hysteresis, and the decision to shut down. Pure logic with no ESP-IDF dependency, so the host
|
||||
// tests (test/host/test_battery.cpp) compile this header unmodified. Kept out of BatteryMonitor, which
|
||||
// only samples the cell.
|
||||
#include <cstdint>
|
||||
|
||||
namespace microbu {
|
||||
|
||||
/// @brief Thresholds and timing of the battery policy. Millivolts at the cell, not at the ADC pin.
|
||||
struct BatteryPolicyConfig {
|
||||
std::uint16_t warn_mv = 3500; ///< low-battery warning below this
|
||||
std::uint16_t warn_clear_mv = 3600;///< ... and cleared again at or above this (hysteresis)
|
||||
std::uint16_t shutdown_mv = 3300; ///< shut down below this ...
|
||||
unsigned shutdown_samples = 6; ///< ... for this many consecutive samples (6 x 5 s = 30 s: rides out TX sag)
|
||||
std::uint32_t grace_ms = 60000; ///< no shutdown in the first minute after boot
|
||||
std::uint16_t resume_mv = 3600; ///< after a shutdown the board only runs again at or above this
|
||||
std::uint16_t valid_min_mv = 2500; ///< a reading outside valid_min..valid_max is "no cell / no sensor"
|
||||
std::uint16_t valid_max_mv = 4500;
|
||||
};
|
||||
|
||||
/// @brief Turns raw cell-voltage samples into a smoothed value, a warning flag and a shutdown decision.
|
||||
class BatteryPolicy {
|
||||
public:
|
||||
/// @param config Thresholds; the defaults suit a single LiPo cell.
|
||||
explicit BatteryPolicy(BatteryPolicyConfig config = {}) : config_(config) {}
|
||||
|
||||
/// @brief Feeds one sample, taken every few seconds.
|
||||
/// @param mv Cell voltage in millivolts, or 0 when the measurement failed.
|
||||
/// @param usb_active A phone is talking over USB, so the board is powered from the phone and
|
||||
/// the cell cannot be the reason it would stop.
|
||||
/// @param uptime_ms Milliseconds since boot.
|
||||
void update(std::uint16_t mv, bool usb_active, std::uint32_t uptime_ms) {
|
||||
if (mv < config_.valid_min_mv || mv > config_.valid_max_mv) {
|
||||
// No cell (or no sensor): report "not measured" and never shut down on it.
|
||||
valid_ = false; ema_ = 0; low_ = false; below_ = 0;
|
||||
return;
|
||||
}
|
||||
if (!valid_) { valid_ = true; ema_ = mv; } // first sample after a gap: no ramp from zero
|
||||
else ema_ = static_cast<std::uint16_t>(ema_ + (static_cast<int>(mv) - static_cast<int>(ema_)) / 4);
|
||||
if (ema_ < config_.warn_mv) low_ = true;
|
||||
else if (ema_ >= config_.warn_clear_mv) low_ = false;
|
||||
if (ema_ < config_.shutdown_mv && !usb_active && uptime_ms >= config_.grace_ms) ++below_;
|
||||
else below_ = 0;
|
||||
}
|
||||
|
||||
/// @return Smoothed cell voltage in millivolts, 0 while there is no valid reading.
|
||||
std::uint16_t millivolts() const { return valid_ ? ema_ : 0; }
|
||||
/// @return true once the cell fell below the warning threshold, until it is back above the clear threshold.
|
||||
bool low() const { return low_; }
|
||||
/// @return true while the smoothed voltage is below the shutdown threshold (not yet sustained).
|
||||
bool critical() const { return valid_ && ema_ < config_.shutdown_mv; }
|
||||
/// @return true once the cell has stayed below the shutdown threshold with no USB link for long enough.
|
||||
bool shutdown_due() const { return below_ >= config_.shutdown_samples; }
|
||||
|
||||
/// @param mv A single reading taken right after a wake-up.
|
||||
/// @return true if the board may run: the cell recovered, or there is no valid reading to judge by.
|
||||
bool resume_ok(std::uint16_t mv) const {
|
||||
if (mv < config_.valid_min_mv || mv > config_.valid_max_mv) return true;
|
||||
return mv >= config_.resume_mv;
|
||||
}
|
||||
|
||||
private:
|
||||
BatteryPolicyConfig config_;
|
||||
std::uint16_t ema_ = 0;
|
||||
bool valid_ = false;
|
||||
bool low_ = false;
|
||||
unsigned below_ = 0;
|
||||
};
|
||||
|
||||
} // namespace microbu
|
||||
Reference in New Issue
Block a user