Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
726 changes: 547 additions & 179 deletions firmware/Cargo.lock

Large diffs are not rendered by default.

28 changes: 17 additions & 11 deletions firmware/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,14 @@ _mcu-f-esp32 = [

[dependencies]
# mcu-esp32 stuff
esp32-hal = { version = "0.10", optional = true, features = [
esp32-hal = { version = "0.16", optional = true, features = [
"embassy",
"embassy-time-timg0",
"async",
] }

# mcu-esp32c3 stuff
esp32c3-hal = { version = "0.7", optional = true, features = [
esp32c3-hal = { version = "0.13", optional = true, features = [
"embassy",
"embassy-time-timg0",
"async",
Expand All @@ -144,6 +144,8 @@ nrf52832-pac = { version = "0.12", optional = true }
# Async stuff
embassy-futures = "0.1.0"
embassy-executor = { version = "*", features = [
"executor-thread",
"arch-cortex-m",
"integrated-timers",
"nightly", # Needed for .spawn()
] }
Expand All @@ -163,16 +165,18 @@ esp-alloc = { version = "0.2", optional = true }
defmt_esp_println = { path = "crates/defmt_esp_println", optional = true }

# Wi-Fi
esp-wifi = { git = "https://github.com/esp-rs/esp-wifi.git", rev = "76ba312", features = [
esp-wifi = { git = "https://github.com/esp-rs/esp-wifi.git", rev = "9e44fe3", features = [
"embedded-svc",
"wifi",
"embassy-net",
], optional = true }
smoltcp = { version = "0.9", default-features = false, features = [
smoltcp = { version = "0.10", default-features = false, features = [
], optional = true }
embassy-net = { version = "*", optional = true, features = [
"nightly", "tcp", "udp", "dhcpv4", "medium-ethernet"
] }
atomic-waker = { version = "1.1", default-features = false, features = ["portable-atomic"] }
portable-atomic = { version = "1.5", default-features = false, features = ["require-cas", "unsafe-assume-single-core"]}

# Generic BLE
bleps = { git = "https://github.com/bjoernQ/bleps", rev = "33fde67", optional = true }
Expand All @@ -188,7 +192,7 @@ nrf-softdevice = { version = "*", default-features = false, features = [

# Platform independent traits
embedded-hal = "0.2"
embedded-svc = { version = "0.23", default-features = false, optional = true, features = [
embedded-svc = { version = "0.26", default-features = false, optional = true, features = [
# "defmt"
# "nightly",
] }
Expand Down Expand Up @@ -241,15 +245,17 @@ bitvec = { git = "https://github.com/arctic-alpaca/ferrilab.git", rev = "e13261a
# in [dependencies] because this ensures that we don't duplicate any dependencies like
# `embassy-time`. This avoids bugs like this one:
# https://github.com/embassy-rs/embassy/issues/1115
embassy-executor = { git = "https://github.com/embassy-rs/embassy", rev = "26474ce6eb759e5add1c137f3417845e0797df3a" }
embassy-time = { git = "https://github.com/embassy-rs/embassy", rev = "26474ce6eb759e5add1c137f3417845e0797df3a" }
embassy-nrf = { git = "https://github.com/embassy-rs/embassy", rev = "26474ce6eb759e5add1c137f3417845e0797df3a" }
embassy-usb = { git = "https://github.com/embassy-rs/embassy", rev = "26474ce6eb759e5add1c137f3417845e0797df3a" }
embassy-sync = { git = "https://github.com/embassy-rs/embassy", rev = "26474ce6eb759e5add1c137f3417845e0797df3a" }
embassy-net = { git = "https://github.com/embassy-rs/embassy", rev = "26474ce6eb759e5add1c137f3417845e0797df3a" }
embassy-executor = { git = "https://github.com/embassy-rs/embassy", rev = "e6462f1e88bc37b69af3b47cc22c492ef41d697c" }
embassy-time = { git = "https://github.com/embassy-rs/embassy", rev = "e6462f1e88bc37b69af3b47cc22c492ef41d697c" }
embassy-nrf = { git = "https://github.com/embassy-rs/embassy", rev = "e6462f1e88bc37b69af3b47cc22c492ef41d697c" }
embassy-usb = { git = "https://github.com/embassy-rs/embassy", rev = "e6462f1e88bc37b69af3b47cc22c492ef41d697c" }
embassy-sync = { git = "https://github.com/embassy-rs/embassy", rev = "e6462f1e88bc37b69af3b47cc22c492ef41d697c" }
embassy-net = { git = "https://github.com/embassy-rs/embassy", rev = "e6462f1e88bc37b69af3b47cc22c492ef41d697c" }

nrf-softdevice = { git = "https://github.com/embassy-rs/nrf-softdevice", rev = "8a3dbb7" }

atomic_enum = { git = "https://github.com/thebutlah/atomic_enum", rev = "3c90118" }

[profile.dev]
# lto doesnt work for esp-wifi
lto = false
Expand Down
2 changes: 1 addition & 1 deletion firmware/rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[toolchain]
channel = "nightly-2022-12-18"
channel = "nightly-2023-08-26"
# channel = "esp" # for when compiling to xtensa targets
targets = [
"riscv32imc-unknown-none-elf",
Expand Down
6 changes: 2 additions & 4 deletions firmware/src/aliases.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

#[cfg(feature = "mcu-esp32")]
pub mod ඞ {
pub use esp32_hal::ehal;
pub use esp32_hal::Delay as DelayConcrete;

pub type I2cConcrete<'a> = esp32_hal::i2c::I2C<'a, esp32_hal::peripherals::I2C0>;
Expand All @@ -23,7 +22,6 @@ pub mod ඞ {

#[cfg(feature = "mcu-esp32c3")]
pub mod ඞ {
pub use esp32c3_hal::ehal;
pub use esp32c3_hal::Delay as DelayConcrete;

pub type I2cConcrete<'a> =
Expand All @@ -32,11 +30,11 @@ pub mod ඞ {
pub type BbqPeripheral<'a> = ();

#[cfg(feature = "net-wifi")]
pub type NetStackConcrete = embassy_net::Stack<esp_wifi::wifi::WifiDevice>;
pub type NetStackConcrete = embassy_net::Stack<esp_wifi::wifi::WifiDevice<'static>>;

#[cfg(feature = "net-wifi")]
pub struct NetConcrete {
pub controller: esp_wifi::wifi::WifiController,
pub controller: esp_wifi::wifi::WifiController<'static>,
pub stack: &'static NetStackConcrete,
}
#[cfg(not(feature = "net-wifi"))]
Expand Down
1 change: 0 additions & 1 deletion firmware/src/imu/drivers/stubbed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use defmt::debug;
use embassy_time::{Duration, Ticker};
use embedded_hal::blocking::delay::DelayMs;
use firmware_protocol::ImuType;
use futures_util::StreamExt;

/// Fakes an IMU for easier testing.
struct FakeImu(Ticker);
Expand Down
2 changes: 1 addition & 1 deletion firmware/src/networking/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ pub async fn network_task(
async fn stubbed_network_task(packets: &Packets, _net: NetConcrete) -> ! {
loop {
// Dump network messages
let _ = packets.serverbound.recv().await;
let _ = packets.serverbound.receive().await;
}
}
2 changes: 1 addition & 1 deletion firmware/src/networking/protocol/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub async fn control_task(
debug!("Control task!");
async {
loop {
match select(packets.clientbound.recv(), quat.wait()).await {
match select(packets.clientbound.receive(), quat.wait()).await {
Either::First(cb_msg) => {
handle_cb_msg(cb_msg, &packets.serverbound).await
}
Expand Down
8 changes: 5 additions & 3 deletions firmware/src/networking/wifi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ impl State {
/// Returns num bytes of outbound buffer
fn on_send(&mut self, outbound: SbPacket, out_buf: &mut [u8]) -> Option<usize> {
// Serialize the packet based on our send sequence number
let Ok(len) = Packet::new(self.tx_seq().0, outbound).serialize_into(out_buf) else {
let Ok(len) = Packet::new(self.tx_seq().0, outbound).serialize_into(out_buf)
else {
warn!("Failed to serialize outgoing packet");
return None;
};
Expand Down Expand Up @@ -123,7 +124,7 @@ pub async fn network_task(
let stack = net.stack;

let ip = loop {
if let Some(ip) = stack.config().map(|c| c.address) {
if let Some(ip) = stack.config_v4().map(|c| c.address) {
break ip;
}
Timer::after(Duration::from_millis(500)).await;
Expand Down Expand Up @@ -154,7 +155,7 @@ pub async fn network_task(
// Either start sending or receive, if either is available
let net = select(
recv_bytes(&mut socket, &mut buffer),
packets.serverbound.recv(),
packets.serverbound.receive(),
)
.await;

Expand Down Expand Up @@ -193,6 +194,7 @@ async fn recv_bytes<'s>(
loop {
match socket.recv_from(buffer).await {
Ok(v) => return v,
Err(UdpError::SocketNotBound) => todo!("refactor go brr"),
Err(UdpError::NoRoute) => warn!("UdpError::NoRoute"),
}
}
Expand Down
16 changes: 1 addition & 15 deletions firmware/src/peripherals/esp32/esp32c3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub fn get_peripherals(
) -> Peripherals<I2cConcrete<'static>, DelayConcrete, (), (), NetConcrete> {
let p = esp32c3_hal::peripherals::Peripherals::take();

let mut system = p.SYSTEM.split();
let system = p.SYSTEM.split();
// The ESP-Wifi module requires 160MHz for cpu clock speeed
let clocks =
ClockControl::configure(system.clock_control, CpuClock::Clock160MHz).freeze();
Expand Down Expand Up @@ -55,25 +55,11 @@ pub fn get_peripherals(
map_pin!(io, env!("PIN_SDA")),
map_pin!(io, env!("PIN_SCL")),
400u32.kHz(),
&mut system.peripheral_clock_control,
&clocks,
);

let delay = esp32c3_hal::Delay::new(&clocks);

#[cfg(feature = "net-wifi")]
{
use esp32c3_hal::systimer::SystemTimer;
use esp32c3_hal::Rng;

esp_wifi::init_heap();

let systimer = SystemTimer::new(p.SYSTIMER);
let rng = Rng::new(p.RNG);
esp_wifi::initialize(systimer.alarm0, rng, &clocks)
.expect("failed to initialize esp-wifi");
}

#[allow(clippy::let_unit_value)]
let net = super::init_wifi_stack();

Expand Down
25 changes: 23 additions & 2 deletions firmware/src/peripherals/esp32/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ mod ඞ;
#[path = "esp32.rs"]
mod ඞ;

use esp32c3_hal::clock::ClockControl;
use esp_wifi::EspWifiInitFor;
pub use ඞ::get_peripherals;

use crate::aliases::ඞ::NetConcrete;
Expand All @@ -25,10 +27,29 @@ fn init_wifi_stack() -> NetConcrete {
#[cfg(feature = "net-wifi")]
{
use embassy_net::{Config, Stack, StackResources};
use esp32c3_hal::system::SystemExt;
use esp_wifi::wifi::{WifiDevice, WifiMode};
// TODO: don't do this lmao
let p = unsafe { esp32c3_hal::peripherals::Peripherals::steal() };
let rng = esp32c3_hal::rng::Rng::new(p.RNG);

let (wifi_interface, controller) = esp_wifi::wifi::new(WifiMode::Sta);
let config = Config::Dhcp(Default::default());
let system = p.SYSTEM.split();
let clocks = ClockControl::max(system.clock_control).freeze();
let timer = esp32c3_hal::systimer::SystemTimer::new(p.SYSTIMER).alarm0;

let initialization = esp_wifi::initialize(
EspWifiInitFor::Wifi,
timer,
rng,
system.radio_clock_control,
&clocks,
)
.expect("Failed to initialize esp wifi");

let (wifi_interface, controller) =
esp_wifi::wifi::new_with_mode(&initialization, p.WIFI, WifiMode::Sta)
.expect("failed to create new wifi");
let config = Config::dhcpv4(Default::default());

let seed = 1234; // very random, very secure seed

Expand Down