Skip to content

Commit

Permalink
fix(nightly): convert make_static!() to (Const)StaticCell
Browse files Browse the repository at this point in the history
  • Loading branch information
kaspar030 committed Sep 10, 2024
1 parent c8657f2 commit 7aff7f8
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 33 deletions.
23 changes: 14 additions & 9 deletions examples/embassy-http-server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@
mod pins;
mod routes;

use riot_rs::{debug::log::*, network, Spawner};
use riot_rs::{debug::log::*, network, ConstStaticCell, Spawner, StaticCell};

use embassy_net::tcp::TcpSocket;
use embassy_time::Duration;
use picoserve::{io::Error, routing::get};
use static_cell::make_static;

#[cfg(feature = "button-readings")]
use embassy_nrf::gpio::{Input, Pin, Pull};
Expand Down Expand Up @@ -101,7 +100,9 @@ fn main(spawner: Spawner, peripherals: pins::Peripherals) {
button4: Input::new(buttons.btn4.degrade(), Pull::Up),
};

ButtonInputs(make_static!(Mutex::new(buttons)))
static BUTTON_INPUTS: StaticCell<Mutex<CriticalSectionRawMutex, Buttons>> =
StaticCell::new();
ButtonInputs(BUTTON_INPUTS.init(Mutex::new(buttons)))
};

fn make_app() -> picoserve::Router<AppRouter, AppState> {
Expand All @@ -111,13 +112,17 @@ fn main(spawner: Spawner, peripherals: pins::Peripherals) {
router
}

let app = make_static!(make_app());
static APP: StaticCell<picoserve::Router<AppRouter, AppState>> = StaticCell::new();
let app = APP.init(make_app());

let config = make_static!(picoserve::Config::new(picoserve::Timeouts {
start_read_request: Some(Duration::from_secs(5)),
read_request: Some(Duration::from_secs(1)),
write: Some(Duration::from_secs(1)),
}));
static CONFIG: ConstStaticCell<picoserve::Config<Duration>> =
ConstStaticCell::new(picoserve::Config::new(picoserve::Timeouts {
start_read_request: Some(Duration::from_secs(5)),
read_request: Some(Duration::from_secs(1)),
write: Some(Duration::from_secs(1)),
}));

let config = &mut *CONFIG.take();

for id in 0..WEB_TASK_POOL_SIZE {
let app_state = AppState {
Expand Down
7 changes: 4 additions & 3 deletions examples/usb-serial/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@

use riot_rs::{
debug::log::info,
embassy_usb, make_static,
embassy_usb,
usb::{UsbBuilderHook, UsbDriver},
StaticCell,
};

use embassy_usb::{
Expand Down Expand Up @@ -36,11 +37,11 @@ fn usb_config() -> embassy_usb::Config<'static> {
async fn main() {
info!("Hello World!");

let state = make_static!(State::new());
static STATE: StaticCell<State> = StaticCell::new();

// Inject class on the system USB builder.
let mut class = USB_BUILDER_HOOK
.with(|builder| CdcAcmClass::new(builder, state, 64))
.with(|builder| CdcAcmClass::new(builder, STATE.init(State::new()), 64))
.await;

// Do stuff with the class!
Expand Down
4 changes: 2 additions & 2 deletions src/riot-rs-embassy/src/arch/stm32/usb_synopsis_otg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub fn driver(peripherals: &mut arch::OptionalPeripherals) -> UsbDriver {
// "An internal buffer used to temporarily store received packets.
// Must be large enough to fit all OUT endpoint max packet sizes.
// Endpoint allocation will fail if it is too small."
let ep_out_buffer = crate::make_static!([0u8; 256]);
static EP_OUT_BUFFER: [u8; 256] = ConstStaticCell::new();
let mut config = embassy_stm32::usb::Config::default();

// Enable vbus_detection
Expand All @@ -36,5 +36,5 @@ pub fn driver(peripherals: &mut arch::OptionalPeripherals) -> UsbDriver {
embassy_stm32::interrupt::OTG_FS.set_priority(Priority::P0);
}

Driver::new_fs(usb, Irqs, dp, dm, ep_out_buffer, config)
Driver::new_fs(usb, Irqs, dp, dm, EP_OUT_BUFFER.tale(), config)
}
40 changes: 26 additions & 14 deletions src/riot-rs-embassy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ use riot_rs_debug::log::debug;

// re-exports
pub use linkme::{self, distributed_slice};
pub use static_cell::make_static;
pub use static_cell::{ConstStaticCell, StaticCell};

// Used by a macro we provide
pub use embassy_executor;
Expand Down Expand Up @@ -122,8 +122,10 @@ fn init() -> ! {
debug!("riot-rs-embassy::init(): using single thread executor");
let p = arch::init();

let executor = make_static!(arch::Executor::new());
executor.run(|spawner| spawner.must_spawn(init_task(p)))
static EXECUTOR: arch::EXECUTOR = ConstStaticCell::new();
EXECUTOR
.init(arch::Executor::new())
.run(|spawner| spawner.must_spawn(init_task(p)))
}

#[cfg(feature = "executor-thread")]
Expand All @@ -147,8 +149,10 @@ fn init() {
debug!("riot-rs-embassy::init(): using thread executor");
let p = arch::init();

let executor = make_static!(thread_executor::Executor::new());
executor.run(|spawner| spawner.must_spawn(init_task(p)));
static EXECUTOR: arch::EXECUTOR = ConstStaticCell::new();
EXECUTOR
.init(thread_executor::Executor::new())
.run(|spawner| spawner.must_spawn(init_task(p)));
}

#[embassy_executor::task]
Expand Down Expand Up @@ -188,14 +192,18 @@ async fn init_task(mut peripherals: arch::OptionalPeripherals) {

let usb_driver = arch::usb::driver(&mut peripherals);

static CONFIG_DESC: ConstStaticCell<[u8; 256]> = ConstStaticCell::new([0; 256]);
static BOS_DESC: ConstStaticCell<[u8; 256]> = ConstStaticCell::new([0; 256]);
static MSOS_DESC: ConstStaticCell<[u8; 128]> = ConstStaticCell::new([0; 128]);
static CONTROL_BUF: ConstStaticCell<[u8; 128]> = ConstStaticCell::new([0; 128]);
// Create embassy-usb DeviceBuilder using the driver and config.
let builder = usb::UsbBuilder::new(
usb_driver,
usb_config,
&mut make_static!([0; 256])[..],
&mut make_static!([0; 256])[..],
&mut make_static!([0; 128])[..],
&mut make_static!([0; 128])[..],
&mut CONFIG_DESC.take()[..],
&mut BOS_DESC.take()[..],
&mut MSOS_DESC.take()[..],
&mut CONTROL_BUF.take()[..],
);

builder
Expand All @@ -211,18 +219,20 @@ async fn init_task(mut peripherals: arch::OptionalPeripherals) {
let host_mac_addr = [0x8A, 0x88, 0x88, 0x88, 0x88, 0x88];

// Create classes on the builder.
static CDC_ECM_STATE: StaticCell<CdcNcmState> = StaticCell::new();
let usb_cdc_ecm = CdcNcmClass::new(
&mut usb_builder,
make_static!(CdcNcmState::new()),
CDC_ECM_STATE.init(CdcNcmState::new()),
host_mac_addr,
64,
);

let our_mac_addr = [0xCA, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC];

static NET_STATE: StaticCell<NetState<{ network::ETHERNET_MTU }, 4, 4>> = StaticCell::new();
let (runner, device) = usb_cdc_ecm
.into_embassy_net_device::<{ network::ETHERNET_MTU }, 4, 4>(
make_static!(NetState::new()),
NET_STATE.init(NetState::new()),
our_mac_addr,
);

Expand Down Expand Up @@ -271,11 +281,13 @@ async fn init_task(mut peripherals: arch::OptionalPeripherals) {
let seed = 1234u64;

// Init network stack
let stack = &*make_static!(Stack::new(
static RESOURCES: StaticCell<StackResources<MAX_CONCURRENT_SOCKETS>> = StaticCell::new();
static _STACK: StaticCell<NetworkStack> = StaticCell::new();
let stack = &*_STACK.init(Stack::new(
device,
config,
make_static!(StackResources::<MAX_CONCURRENT_SOCKETS>::new()),
seed
RESOURCES.init(StackResources::new()),
seed,
));

spawner.spawn(network::net_task(stack)).unwrap();
Expand Down
7 changes: 4 additions & 3 deletions src/riot-rs-embassy/src/wifi/cyw43.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use embassy_rp::{
use riot_rs_debug::log::info;

use self::rpi_pico_w::{Cyw43Periphs, CywSpi, Irqs};
use crate::{arch::OptionalPeripherals, make_static};
use crate::{arch::OptionalPeripherals, StaticCell};

pub type NetworkDevice = cyw43::NetDriver<'static>;

Expand Down Expand Up @@ -68,8 +68,9 @@ pub async fn device<'a, 'b: 'a>(
pins.dma,
);

let state = make_static!(cyw43::State::new());
let (net_device, mut control, runner) = cyw43::new(state, pwr, spi, fw).await;
static STATE: StaticCell<cyw43::State> = StaticCell::new();
let (net_device, mut control, runner) =
cyw43::new(STATE.init(cyw43::State::new()), pwr, spi, fw).await;

// this needs to be spawned here (before using `control`)
spawner.spawn(wifi_cyw43_task(runner)).unwrap();
Expand Down
5 changes: 3 additions & 2 deletions src/riot-rs-threads/src/autostart_thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ macro_rules! autostart_thread {
#[$crate::macro_reexports::linkme::distributed_slice($crate::THREAD_FNS)]
#[linkme(crate = $crate::macro_reexports::linkme)]
fn [<__start_thread_ $fn_name>] () {
let stack = $crate::macro_reexports::static_cell::make_static!([0u8; $stacksize as usize]);
$crate::thread_create_noarg($fn_name, stack, $priority);
use $crate::macro_reexports::static_cell::ConstStaticCell;
static STACK: ConstStaticCell<[u8; $stacksize]> = ConstStaticCell::new([0u8; $stacksize]);
$crate::thread_create_noarg($fn_name, &mut *STACK.take(), $priority);
}
}
};
Expand Down

0 comments on commit 7aff7f8

Please sign in to comment.