Skip to content

Commit

Permalink
Re-implement broken make_static! macro
Browse files Browse the repository at this point in the history
static-cell's `make_static!` macro is broken in latest nightly:
embassy-rs/static-cell#16

Re-implement it by dropping the automatic type deduction magic.
  • Loading branch information
kesyog committed Jul 6, 2024
1 parent e167211 commit d28da13
Show file tree
Hide file tree
Showing 12 changed files with 60 additions and 46 deletions.
7 changes: 2 additions & 5 deletions hangman/Cargo.lock

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

2 changes: 1 addition & 1 deletion hangman/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ num = { version = "0.4", default-features = false }
once_cell = { version = "1.18", default-features = false, features = ["critical-section"] }
panic-probe = { version = "0.3", features = ["print-defmt"] }
rand = { version = "0.8", default-features = false, features = ["nightly"] }
static_cell = { version = "1", features = ["nightly"] }
static_cell = { version = "2", features = ["nightly"] }
typenum = "1.17"

[features]
Expand Down
1 change: 0 additions & 1 deletion hangman/src/bin/blinky_p0.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ use embedded_alloc::Heap;
use hangman::{ble, pac};
use nrf_softdevice::{self as _, Softdevice};
use panic_probe as _;
// use static_cell::make_static;

#[allow(dead_code)]
enum Mode {
Expand Down
1 change: 0 additions & 1 deletion hangman/src/bin/blinky_p1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ use embedded_alloc::Heap;
use hangman::{ble, pac};
use nrf_softdevice::{self as _, Softdevice};
use panic_probe as _;
// use static_cell::make_static;

#[allow(dead_code)]
enum Mode {
Expand Down
9 changes: 5 additions & 4 deletions hangman/src/bin/calibration_p0.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,11 @@ use embassy_nrf::{
use embassy_sync::{blocking_mutex::raw::NoopRawMutex, channel::Channel, mutex::Mutex};
use embedded_alloc::Heap;
use hangman::{
ble, blocking_hal, pac,
ble, blocking_hal, make_static, pac,
weight::{self, average, Hx711},
};
use nrf_softdevice::{self as _, Softdevice};
use panic_probe as _;
use static_cell::make_static;

type SharedDelay = Mutex<NoopRawMutex, SysTickDelay>;

Expand Down Expand Up @@ -86,7 +85,8 @@ async fn main(spawner: Spawner) -> ! {

let p = embassy_nrf::init(config());
let syst = pac::CorePeripherals::take().unwrap().SYST;
let delay: &'static SharedDelay = make_static!(Mutex::new(SysTickDelay::new(syst)));
let delay: &'static SharedDelay =
make_static!(SharedDelay, Mutex::new(SysTickDelay::new(syst)));

let sd = ble::init_softdevice();
spawner.must_spawn(softdevice_task(sd));
Expand All @@ -105,7 +105,8 @@ async fn main(spawner: Spawner) -> ! {
);
let hx711 = Hx711::new(hx711_data, hx711_clock, delay);

let ch: &hangman::MeasureCommandChannel = make_static!(Channel::new());
let ch: &hangman::MeasureCommandChannel =
make_static!(hangman::MeasureCommandChannel, Channel::new());
spawner.must_spawn(weight::task_function(ch.receiver(), hx711, sd));

let mut button = gpio::Input::new(p.P1_06.degrade(), gpio::Pull::Up);
Expand Down
9 changes: 5 additions & 4 deletions hangman/src/bin/calibration_p1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,11 @@ use embassy_sync::{blocking_mutex::raw::NoopRawMutex, channel::Channel, mutex::M
use embassy_time::{Duration, Timer};
use embedded_alloc::Heap;
use hangman::{
ble, blocking_hal, pac,
ble, blocking_hal, make_static, pac,
weight::{self, average, Ads1230},
};
use nrf_softdevice::{self as _, Softdevice};
use panic_probe as _;
use static_cell::make_static;

type SharedDelay = Mutex<NoopRawMutex, SysTickDelay>;

Expand Down Expand Up @@ -87,7 +86,8 @@ async fn main(spawner: Spawner) -> ! {

let p = embassy_nrf::init(config());
let syst = pac::CorePeripherals::take().unwrap().SYST;
let delay: &'static SharedDelay = make_static!(Mutex::new(SysTickDelay::new(syst)));
let delay: &'static SharedDelay =
make_static!(SharedDelay, Mutex::new(SysTickDelay::new(syst)));

let sd = ble::init_softdevice();
spawner.must_spawn(softdevice_task(sd));
Expand Down Expand Up @@ -118,7 +118,8 @@ async fn main(spawner: Spawner) -> ! {
let mut adc = Ads1230::new(adc_data, adc_clock, vdda_on, delay);
adc.schedule_offset_calibration().await;

let ch: &hangman::MeasureCommandChannel = make_static!(Channel::new());
let ch: &hangman::MeasureCommandChannel =
make_static!(hangman::MeasureCommandChannel, Channel::new());
spawner.must_spawn(weight::task_function(ch.receiver(), adc, sd));

let mut button = gpio::Input::new(p.P0_09.degrade(), gpio::Pull::Up);
Expand Down
11 changes: 6 additions & 5 deletions hangman/src/bin/dongle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,12 @@ use hangman::console;
use hangman::{
battery_voltage, ble, blocking_hal,
button::{self, Button},
pac, util,
make_static, pac, util,
weight::{self, Hx711},
MeasureCommandChannel, SharedDelay,
};
use nrf_softdevice::{self as _, SocEvent, Softdevice};
use panic_probe as _;
use static_cell::make_static;

#[global_allocator]
/// Create a small heap. Not sure how to pass around closures without one.
Expand Down Expand Up @@ -106,7 +105,8 @@ async fn main(spawner: Spawner) -> ! {
// This will reset the GPIO latch signal
let p = embassy_nrf::init(config());
let syst = pac::CorePeripherals::take().unwrap().SYST;
let delay: &'static SharedDelay = make_static!(Mutex::new(SysTickDelay::new(syst)));
let delay: &'static SharedDelay =
make_static!(SharedDelay, Mutex::new(SysTickDelay::new(syst)));

// For debugging
let _green_led = gpio::Output::new(
Expand All @@ -129,7 +129,8 @@ async fn main(spawner: Spawner) -> ! {
// USB setup
// Hack: pretend USB is already connected. not a bad assumption since this is a dongle
// There might be a race condition at startup between USB init and SD init.
let usb_detect_ref: &SoftwareVbusDetect = make_static!(SoftwareVbusDetect::new(true, true));
let usb_detect_ref: &SoftwareVbusDetect =
make_static!(SoftwareVbusDetect, SoftwareVbusDetect::new(true, true));

let sd = ble::init_softdevice();
spawner.must_spawn(softdevice_task(sd, usb_detect_ref));
Expand All @@ -148,7 +149,7 @@ async fn main(spawner: Spawner) -> ! {
#[cfg(feature = "console")]
let (usb, class) = console::board::setup_usb(p.USBD, Irqs, usb_detect_ref);

let ch: &MeasureCommandChannel = make_static!(Channel::new());
let ch: &MeasureCommandChannel = make_static!(MeasureCommandChannel, Channel::new());
spawner.must_spawn(weight::task_function(ch.receiver(), hx711, sd));
// Sample battery voltage while sampling to get a reading under load
ch.sender()
Expand Down
11 changes: 6 additions & 5 deletions hangman/src/bin/proto0_0.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,12 @@ use embedded_alloc::Heap;
use hangman::{
battery_voltage, ble, blocking_hal,
button::{self, Button},
pac, util,
make_static, pac, util,
weight::{self, Hx711},
MeasureCommandChannel, SharedDelay,
};
use nrf_softdevice::{self as _, SocEvent, Softdevice};
use panic_probe as _;
use static_cell::make_static;

#[global_allocator]
/// Create a small heap. Not sure how to pass around closures without one.
Expand Down Expand Up @@ -96,7 +95,8 @@ async fn main(spawner: Spawner) -> ! {

let p = embassy_nrf::init(config());
let syst = pac::CorePeripherals::take().unwrap().SYST;
let delay: &'static SharedDelay = make_static!(Mutex::new(SysTickDelay::new(syst)));
let delay: &'static SharedDelay =
make_static!(SharedDelay, Mutex::new(SysTickDelay::new(syst)));

// orange DATA 0.17
let hx711_data = gpio::Input::new(p.P0_17.degrade(), gpio::Pull::None);
Expand All @@ -112,7 +112,8 @@ async fn main(spawner: Spawner) -> ! {
// USB setup
// Hack: pretend USB is already connected. not a bad assumption since this is a dongle
// There might be a race condition at startup between USB init and SD init.
let usb_detect_ref: &SoftwareVbusDetect = make_static!(SoftwareVbusDetect::new(true, true));
let usb_detect_ref: &SoftwareVbusDetect =
make_static!(SoftwareVbusDetect, SoftwareVbusDetect::new(true, true));

let sd = ble::init_softdevice();
spawner.must_spawn(softdevice_task(sd, usb_detect_ref));
Expand All @@ -128,7 +129,7 @@ async fn main(spawner: Spawner) -> ! {
)
};

let ch: &MeasureCommandChannel = make_static!(Channel::new());
let ch: &MeasureCommandChannel = make_static!(MeasureCommandChannel, Channel::new());
spawner.must_spawn(weight::task_function(ch.receiver(), hx711, sd));

// Sample battery voltage while sampling to get a reading under load
Expand Down
8 changes: 4 additions & 4 deletions hangman/src/bin/proto1_0.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,12 @@ use embedded_alloc::Heap;
use hangman::{
battery_voltage, ble, blocking_hal,
button::{self, Button},
pac, sleep, util,
make_static, pac, sleep, util,
weight::{self, Ads1230},
MeasureCommandChannel, SharedDelay,
};
use nrf_softdevice::{self as _, Softdevice};
use panic_probe as _;
use static_cell::make_static;

#[global_allocator]
/// Create a small heap. Not sure how to pass around closures without one.
Expand Down Expand Up @@ -96,7 +95,8 @@ async fn main(spawner: Spawner) -> ! {

let p = embassy_nrf::init(config());
let syst = pac::CorePeripherals::take().unwrap().SYST;
let delay: &'static SharedDelay = make_static!(Mutex::new(SysTickDelay::new(syst)));
let delay: &'static SharedDelay =
make_static!(SharedDelay, Mutex::new(SysTickDelay::new(syst)));

let sd = ble::init_softdevice();
spawner.must_spawn(softdevice_task(sd));
Expand Down Expand Up @@ -143,7 +143,7 @@ async fn main(spawner: Spawner) -> ! {
let mut adc = Ads1230::new(adc_data, adc_clock, vdda_on, delay);
adc.schedule_offset_calibration().await;

let ch: &MeasureCommandChannel = make_static!(Channel::new());
let ch: &MeasureCommandChannel = make_static!(MeasureCommandChannel, Channel::new());
spawner.must_spawn(weight::task_function(ch.receiver(), adc, sd));

// Sample battery voltage while sampling to get a reading under load
Expand Down
20 changes: 11 additions & 9 deletions hangman/src/console/board.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,11 @@
// limitations under the License.

use super::{Driver, UsbDriver};
use crate::Irqs;
use crate::{make_static, Irqs};
use embassy_nrf::peripherals::USBD;
use embassy_nrf::usb::vbus_detect::SoftwareVbusDetect;
use embassy_usb::class::cdc_acm::{CdcAcmClass, State};
use embassy_usb::UsbDevice;
use static_cell::make_static;

pub(crate) fn setup_usb(
usbd: USBD,
Expand Down Expand Up @@ -55,13 +54,16 @@ pub(crate) fn setup_usb(
control_buf: [u8; 64],
serial_state: State<'static>,
}
let res: &mut Resources = make_static!(Resources {
device_descriptor: [0; 256],
config_descriptor: [0; 256],
bos_descriptor: [0; 256],
control_buf: [0; 64],
serial_state: State::new(),
});
let res: &mut Resources = make_static!(
Resources,
Resources {
device_descriptor: [0; 256],
config_descriptor: [0; 256],
bos_descriptor: [0; 256],
control_buf: [0; 64],
serial_state: State::new(),
}
);

// Create embassy-usb DeviceBuilder using the driver and config.
let mut builder = embassy_usb::Builder::new(
Expand Down
12 changes: 12 additions & 0 deletions hangman/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,15 @@ pub type MeasureCommandChannel =
pub const MEASURE_COMMAND_CHANNEL_SIZE: usize = 5;
pub type MeasureCommandReceiver =
Receiver<'static, NoopRawMutex, weight::Command, MEASURE_COMMAND_CHANNEL_SIZE>;

/// Re-implementation of static_cell::make_static, which is broken on latest nightly
/// https://github.com/embassy-rs/static-cell/issues/16
#[macro_export]
macro_rules! make_static {
($t:ty, $val:expr) => ($crate::make_static!($t, $val,));
($t:ty, $val:expr, $(#[$m:meta])*) => {{
$(#[$m])*
static STATIC_CELL: static_cell::StaticCell<$t> = static_cell::StaticCell::new();
STATIC_CELL.init_with(|| $val)
}};
}
15 changes: 8 additions & 7 deletions hangman/src/weight/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,12 @@ use super::Ads1230;
#[cfg(feature = "nrf52840")]
use super::Hx711;
use super::{average, median::Median, Command, RawReading, Sample, SampleProducerMut, SampleType};
use crate::nonvolatile::Nvm;
use crate::MeasureCommandReceiver;
use crate::{make_static, nonvolatile::Nvm, MeasureCommandReceiver};
use embassy_sync::blocking_mutex::raw::NoopRawMutex;
use embassy_sync::mutex::Mutex;
use embassy_time::{Duration, Instant, Timer};
use hangman_utils::two_point_cal::{self, CalPoint, TwoPoint};
use nrf_softdevice::Softdevice;
use static_cell::make_static;

const THREAD_SLEEP_DELAY: Duration = Duration::from_millis(100);

Expand Down Expand Up @@ -174,15 +172,18 @@ async fn measure(context: &mut MeasurementContext) {
#[embassy_executor::task]
pub async fn task_function(rx: MeasureCommandReceiver, adc: Adc, sd: &'static Softdevice) {
defmt::debug!("Starting measurement task");
let adc: &SharedAdc = make_static!(Mutex::new(adc));
let median: &'static SharedFilteredAdc = make_static!(Mutex::new(Median::new(adc)));
let adc: &SharedAdc = make_static!(SharedAdc, Mutex::new(adc));
let median: &'static SharedFilteredAdc =
make_static!(SharedFilteredAdc, Mutex::new(Median::new(adc)));

let nvm = Nvm::new(sd);
let cal_m = nvm.read_cal_m();
let cal_b = nvm.read_cal_b();
defmt::info!("Loaded calibration: m={=f32} b={=i32}", cal_m, cal_b);
let calibrator: &SharedCalibrator =
make_static!(Mutex::new(Calibrator::new(median, cal_m, cal_b)));
let calibrator: &SharedCalibrator = make_static!(
SharedCalibrator,
Mutex::new(Calibrator::new(median, cal_m, cal_b))
);

let tarer = Tarer::new(calibrator);
let mut context = MeasurementContext {
Expand Down

0 comments on commit d28da13

Please sign in to comment.