Skip to content

Commit

Permalink
Remove StaticCell in favour of using boxed allocations
Browse files Browse the repository at this point in the history
  • Loading branch information
zargony committed Sep 30, 2024
1 parent 5a1eaba commit c0d232e
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 19 deletions.
1 change: 0 additions & 1 deletion firmware/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion firmware/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,4 @@ reqwless = { version = "0.12", default-features = false, features = ["alloc", "e
serde = { version = "1.0", default-features = false, features = ["alloc", "derive"] }
serde_json = { version = "1.0", default-features = false, features = ["alloc"] }
ssd1306 = { version = "0.9", features = ["async"] }
static_cell = "2.1"
u8g2-fonts = "0.4"
31 changes: 14 additions & 17 deletions firmware/src/wifi.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use alloc::boxed::Box;
use core::cell::Cell;
use core::fmt;
use embassy_executor::{task, Spawner};
Expand All @@ -16,7 +17,6 @@ use esp_wifi::wifi::{
use esp_wifi::{EspWifiInitFor, EspWifiTimerSource};
use log::{debug, info, warn};
use rand_core::RngCore;
use static_cell::StaticCell;

/// Delay after Wifi disconnect or connection failure before trying to reconnect
const CONNECT_RETRY_DELAY: Duration = Duration::from_millis(5000);
Expand All @@ -33,18 +33,6 @@ const TX_BUFFER_SIZE: usize = 1024;
/// Wifi initialization error
pub use esp_wifi::InitializationError;

/// Static network stack
static STACK: StaticCell<Stack<WifiDevice<'_, WifiStaDevice>>> = StaticCell::new();

/// Static network stack resources (sockets, inflight dns queries)
// Needs at least one socket for DHCP, one socket for DNS, plus additional sockets for connections
static RESOURCES: StaticCell<StackResources<{ 2 + NUM_TCP_SOCKETS }>> = StaticCell::new();

/// Static TCP client state
static TCP_CLIENT_STATE: StaticCell<
TcpClientState<NUM_TCP_SOCKETS, TX_BUFFER_SIZE, RX_BUFFER_SIZE>,
> = StaticCell::new();

/// Option display helper
struct DisplayOption<T: fmt::Display>(Option<T>);

Expand Down Expand Up @@ -178,10 +166,18 @@ impl Wifi {
Err(err) => panic!("Failed to spawn Wifi connection task: {:?}", err),
}

// Initialize static network stack
// Following some resources are allocated and leaked to get a `&'static mut` reference.
// This is ok, since only one instance of `Wifi` can exist and it'll never be dropped.

// Initialize network stack resources (sockets, inflight dns queries). Needs at least one
// socket for DHCP, one socket for DNS, plus additional sockets for connections.
let resources = Box::new(StackResources::<{ 2 + NUM_TCP_SOCKETS }>::new());
let resources = Box::leak(resources);

// Initialize network stack
let config = Config::dhcpv4(DhcpConfig::default());
let resources = RESOURCES.init(StackResources::new());
let stack = STACK.init(Stack::new(device, config, resources, random_seed));
let stack = Box::new(Stack::new(device, config, resources, random_seed));
let stack = Box::leak(stack);

// Spawn task for running network stack
debug!("Wifi: Spawning network task...");
Expand All @@ -192,7 +188,8 @@ impl Wifi {
}

// Initialize TCP client state (reserves tx/rx buffers for TCP sockets)
let tcp_client_state = TCP_CLIENT_STATE.init(TcpClientState::new());
let tcp_client_state = Box::new(TcpClientState::new());
let tcp_client_state = Box::leak(tcp_client_state);

info!("Wifi: Controller initialized");
Ok(Self {
Expand Down

0 comments on commit c0d232e

Please sign in to comment.