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:
Ashin Walpola
2026-11-11 00:13:57 +01:00
parent 1c3a66e4da
commit 50bbf6552d
10307 changed files with 53622 additions and 850451 deletions
+72
View File
@@ -0,0 +1,72 @@
#include "battery_monitor.hpp"
#include <driver/gpio.h>
#include <esp_adc/adc_cali_scheme.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <esp_log.h>
#include <esp_sleep.h>
namespace microbu {
BatteryMonitor::BatteryMonitor() {
#if (CONFIG_MICROBU_XIAO_C5_BATTERY || CONFIG_MICROBU_BOARD_MAINBOARD_V5) && CONFIG_IDF_TARGET_ESP32C5
adc_unit_t unit;
#if CONFIG_MICROBU_BOARD_MAINBOARD_V5
constexpr int battery_gpio = 1;
#else
constexpr int battery_gpio = 6;
#endif
if (adc_oneshot_io_to_channel(battery_gpio, &unit, &channel_) != ESP_OK) return;
adc_oneshot_unit_init_cfg_t config = {};
config.unit_id = unit;
if (adc_oneshot_new_unit(&config, &adc_) != ESP_OK) return;
adc_oneshot_chan_cfg_t input = {};
input.atten = ADC_ATTEN_DB_12;
input.bitwidth = ADC_BITWIDTH_DEFAULT;
if (adc_oneshot_config_channel(adc_, channel_, &input) != ESP_OK) return;
adc_cali_curve_fitting_config_t calibration = {};
calibration.unit_id = unit;
calibration.chan = channel_;
calibration.atten = input.atten;
calibration.bitwidth = input.bitwidth;
if (adc_cali_create_scheme_curve_fitting(&calibration, &calibration_) != ESP_OK) {
ESP_LOGW("battery", "Calibration unavailable; battery voltage stays unavailable");
return;
}
#if CONFIG_MICROBU_XIAO_C5_BATTERY
gpio_set_direction(GPIO_NUM_26, GPIO_MODE_OUTPUT);
gpio_set_level(GPIO_NUM_26, 0);
#endif
#endif
}
void BatteryMonitor::deep_sleep() {
ESP_LOGW("battery", "deep sleep for %d s", CONFIG_MICROBU_BATTERY_SLEEP_S);
esp_sleep_enable_timer_wakeup(static_cast<std::uint64_t>(CONFIG_MICROBU_BATTERY_SLEEP_S) * 1000000ULL);
esp_deep_sleep_start();
}
bool BatteryMonitor::woke_from_battery_sleep() {
return esp_sleep_get_wakeup_cause() == ESP_SLEEP_WAKEUP_TIMER;
}
std::uint16_t BatteryMonitor::sample_millivolts() {
if (!adc_ || !calibration_) return 0;
#if CONFIG_MICROBU_XIAO_C5_BATTERY
gpio_set_level(GPIO_NUM_26, 1);
#endif
vTaskDelay(pdMS_TO_TICKS(5)); // settle the switched 100k/100k divider
int sum = 0;
bool valid = true;
for (unsigned i = 0; i < 16; ++i) {
int raw = 0, mv = 0;
if (adc_oneshot_read(adc_, channel_, &raw) != ESP_OK ||
adc_cali_raw_to_voltage(calibration_, raw, &mv) != ESP_OK) { valid = false; break; }
sum += mv;
}
#if CONFIG_MICROBU_XIAO_C5_BATTERY
gpio_set_level(GPIO_NUM_26, 0);
#endif
const int voltage = (sum * 2 + 8) / 16;
return valid && voltage >= 2500 && voltage <= 4500 ? voltage : 0;
}
}