radio: rewrite the networking stack, and implement some BLE magic

This commit is contained in:
2026-01-05 13:06:23 +01:00
parent 3bf7ebc997
commit f2ff1914b1
7 changed files with 377 additions and 82 deletions

View File

@@ -7,13 +7,13 @@
)]
use core::ptr::addr_of_mut;
use core::{num::{self, Wrapping}, ptr::addr_of_mut};
use alloc::sync::Arc;
use alloc::{string::String, sync::Arc};
use embassy_executor::Spawner;
use embassy_time::{Instant, Timer};
use esp_hal::{gpio::{Output, OutputConfig, Pin}, time::Rate};
use esp_hal::{gpio::{Output, OutputConfig, Pin}, time::Rate, xtensa_lx::debug_break};
use esp_hal::{
clock::CpuClock, system::{AppCoreGuard, CpuControl, Stack}, timer::{systimer::SystemTimer, timg::{TimerGroup, Wdt}}
};
@@ -22,7 +22,7 @@ use embassy_sync::{
pubsub::PubSubChannel,
blocking_mutex::raw::NoopRawMutex
};
use static_cell::ConstStaticCell;
use log::*;
use renderbug_embassy::{events::Prediction, graphics::display::DisplayControls, logging::RenderbugLogger, tasks::{oled::{OledUI, OledUiSurfacePool, oled_ui}, safetyui::{SafetyUi, safety_ui_main}, ui::UiSurfacePool}};
use renderbug_embassy::events::Measurement;
@@ -45,9 +45,6 @@ extern crate alloc;
// For more information see: <https://docs.espressif.com/projects/esp-idf/en/stable/esp32/api-reference/system/app_image_format.html#application-description>
esp_bootloader_esp_idf::esp_app_desc!();
static STATIC_HI_EXEC: StaticCell<InterruptExecutor<2>> = StaticCell::new();
static CORE2_EXEC: StaticCell<Executor> = StaticCell::new();
static mut CORE2_STACK: Stack<16384> = Stack::new();
#[cfg(feature="radio")]
static WIFI_INIT: StaticCell<esp_radio::Controller<'static>> = StaticCell::new();
@@ -83,14 +80,14 @@ async fn main(spawner: Spawner) {
let timer0 = TimerGroup::new(peripherals.TIMG0);
let mut wdt = timer0.wdt;
wdt.set_timeout(esp_hal::timer::timg::MwdtStage::Stage0, esp_hal::time::Duration::from_secs(5));
//wdt.enable();
let swi = SoftwareInterruptControl::new(peripherals.SW_INTERRUPT);
//let hi_exec = STATIC_HI_EXEC.init(InterruptExecutor::new(swi.software_interrupt2));
//let hi_spawn = hi_exec.start(esp_hal::interrupt::Priority::max());
wdt.enable();
// Spawn the rendering task as soon as possible so it can start pushing pixels
spawner.must_spawn(renderbug_embassy::tasks::render::render(peripherals.RMT, peripherals.GPIO5.degrade(), surfaces, safety_surfaces, display_controls, wdt));
// Wait one scheduler tick for the rendering task to get initialized
Timer::after_ticks(1).await;
#[cfg(feature="motion")]
{
use embassy_embedded_hal::shared_bus::asynch::i2c::I2cDevice;
@@ -131,7 +128,7 @@ async fn main(spawner: Spawner) {
let mut storage = SharedFlash::new(FlashStorage::new());
let mut buf = [8; 1024];
let partitions = esp_bootloader_esp_idf::partitions::read_partition_table(&mut storage, &mut buf).unwrap();
for sim_data in SimDataTable::open(storage, partitions).expect("Could not find partition for sim data!") {
for sim_data in SimDataTable::open(storage, partitions).expect("Could not find sim data!") {
let srcid = sim_data.srcid();
info!("Found simulation data for {srcid:?}");
if spawner.spawn(renderbug_embassy::tasks::simulation::simulation_task(sim_data, motion_bus.dyn_sender())).is_err() {
@@ -141,15 +138,23 @@ async fn main(spawner: Spawner) {
}
#[cfg(feature="radio")]
let wifi_init = {
let (wifi, network_device, ble) = {
info!("Configuring wifi");
WIFI_INIT.init_with(|| {esp_radio::init().expect("Failed to initialize radio controller")})
esp_radio::wifi_set_log_verbose();
let wifi_init = WIFI_INIT.init_with(|| {esp_radio::init().expect("Failed to initialize radio controller")});
let ble = esp_radio::ble::controller::BleConnector::new(wifi_init, peripherals.BT, esp_radio::ble::Config::default()).unwrap();
let (wifi, interfaces) = esp_radio::wifi::new(wifi_init, peripherals.WIFI, esp_radio::wifi::Config::default())
.expect("Failed to initialize WIFI!");
(wifi, interfaces.sta, ble)
};
info!("Starting core 2");
let core2_main = |spawner: Spawner| {
static PREDICTIONS: StaticCell<PubSubChannel<NoopRawMutex, Prediction, 15, 5, 1>> = StaticCell::new();
info!("Starting application tasks");
static PREDICTIONS: StaticCell<PubSubChannel<NoopRawMutex, Prediction, 15, 6, 1>> = StaticCell::new();
let predictions = PREDICTIONS.init(PubSubChannel::new());
#[cfg(not(feature="demo"))]
@@ -173,8 +178,26 @@ async fn main(spawner: Spawner) {
#[cfg(feature="radio")]
{
info!("Launching networking stack");
spawner.must_spawn(renderbug_embassy::tasks::wifi::wireless_task(predictions.dyn_subscriber().unwrap(), wifi_init, peripherals.WIFI));
use embassy_net::StackResources;
use esp_hal::rng::Rng;
use static_cell::ConstStaticCell;
info!("Setting up networking stack");
static RESOURCES: ConstStaticCell<StackResources<5>> = ConstStaticCell::new(StackResources::new());
let config = embassy_net::Config::dhcpv4(Default::default());
let seed = Rng::new().random() as i32;
let (stack, runner) = embassy_net::new(network_device, config, RESOURCES.take(), seed as u64);
info!("Launching network services");
//spawner.must_spawn(renderbug_embassy::tasks::wifi::net_task(runner));
info!("Starting connectivity task");
//spawner.must_spawn(renderbug_embassy::tasks::wifi::wifi_connect_task(wifi, stack, motion_bus.dyn_sender()));
info!("Launching HTTP telemetry");
//spawner.must_spawn(renderbug_embassy::tasks::wifi::http_telemetry_task(predictions.dyn_subscriber().unwrap(), stack));
info!("Starting BLE services");
spawner.must_spawn(renderbug_embassy::tasks::ble::ble_task(ble, predictions.dyn_subscriber().unwrap(), spawner));
}
#[cfg(feature="dual-core")]
@@ -194,13 +217,21 @@ async fn main(spawner: Spawner) {
};
#[cfg(feature="dual-core")]
esp_rtos::start_second_core(peripherals.CPU_CTRL, swi.software_interrupt0, swi.software_interrupt1, unsafe { &mut *addr_of_mut!(CORE2_STACK) }, || {
let exec = CORE2_EXEC.init_with(|| { Executor::new() });
exec.run(core2_main);
});
{
static mut CORE2_STACK: Stack<16384> = Stack::new();
let swi = SoftwareInterruptControl::new(peripherals.SW_INTERRUPT);
esp_rtos::start_second_core(peripherals.CPU_CTRL, swi.software_interrupt0, swi.software_interrupt1, unsafe { &mut *addr_of_mut!(CORE2_STACK) }, || {
info!("Second CPU core started");
static CORE2_EXEC: StaticCell<Executor> = StaticCell::new();
let exec = CORE2_EXEC.init_with(|| { Executor::new() });
exec.run(core2_main);
});
}
#[cfg(not(feature="dual-core"))]
core2_main(spawner);
info!("Ready to rock and roll");
}
#[embassy_executor::task]
@@ -216,7 +247,11 @@ async fn wdt_task(mut wdt: Wdt<esp_hal::peripherals::TIMG1<'static>>) {
#[embassy_executor::task]
async fn print_telemetry(mut events: DynSubscriber<'static, Prediction>) {
info!("telemetry ready");
let mut num_events = Wrapping(0usize);
loop {
info!("predict={:?}", events.next_message_pure().await);
let next = events.next_message_pure().await;
trace!("idx={} predict={next:?}", num_events.0);
num_events += 1;
}
}