diff options
| author | Dario Nieuwenhuis <[email protected]> | 2023-05-31 23:49:00 +0000 |
|---|---|---|
| committer | GitHub <[email protected]> | 2023-05-31 23:49:00 +0000 |
| commit | c036eab62c2221bd8f458bd6edd1562dfcdb8421 (patch) | |
| tree | 605b3eb96ea10120f7e8bf0741bcc34daab8e9ec /examples/nrf52840/src | |
| parent | d7d66bd74f2ef1bc8903b15df630ddbb8fe97df4 (diff) | |
| parent | 1d8321b821d114b369d5a087a1a7a6600228b032 (diff) | |
Merge pull request #1523 from embassy-rs/static-cell
Use make_static! from static-cell v1.1
Diffstat (limited to 'examples/nrf52840/src')
| -rw-r--r-- | examples/nrf52840/src/bin/usb_ethernet.rs | 32 | ||||
| -rw-r--r-- | examples/nrf52840/src/bin/usb_serial_multitask.rs | 23 |
2 files changed, 21 insertions, 34 deletions
diff --git a/examples/nrf52840/src/bin/usb_ethernet.rs b/examples/nrf52840/src/bin/usb_ethernet.rs index 786025c43..1065f5b5d 100644 --- a/examples/nrf52840/src/bin/usb_ethernet.rs +++ b/examples/nrf52840/src/bin/usb_ethernet.rs | |||
| @@ -16,7 +16,7 @@ use embassy_usb::class::cdc_ncm::embassy_net::{Device, Runner, State as NetState | |||
| 16 | use embassy_usb::class::cdc_ncm::{CdcNcmClass, State}; | 16 | use embassy_usb::class::cdc_ncm::{CdcNcmClass, State}; |
| 17 | use embassy_usb::{Builder, Config, UsbDevice}; | 17 | use embassy_usb::{Builder, Config, UsbDevice}; |
| 18 | use embedded_io::asynch::Write; | 18 | use embedded_io::asynch::Write; |
| 19 | use static_cell::StaticCell; | 19 | use static_cell::make_static; |
| 20 | use {defmt_rtt as _, panic_probe as _}; | 20 | use {defmt_rtt as _, panic_probe as _}; |
| 21 | 21 | ||
| 22 | bind_interrupts!(struct Irqs { | 22 | bind_interrupts!(struct Irqs { |
| @@ -27,15 +27,6 @@ bind_interrupts!(struct Irqs { | |||
| 27 | 27 | ||
| 28 | type MyDriver = Driver<'static, peripherals::USBD, HardwareVbusDetect>; | 28 | type MyDriver = Driver<'static, peripherals::USBD, HardwareVbusDetect>; |
| 29 | 29 | ||
| 30 | macro_rules! singleton { | ||
| 31 | ($val:expr) => {{ | ||
| 32 | type T = impl Sized; | ||
| 33 | static STATIC_CELL: StaticCell<T> = StaticCell::new(); | ||
| 34 | let (x,) = STATIC_CELL.init(($val,)); | ||
| 35 | x | ||
| 36 | }}; | ||
| 37 | } | ||
| 38 | |||
| 39 | const MTU: usize = 1514; | 30 | const MTU: usize = 1514; |
| 40 | 31 | ||
| 41 | #[embassy_executor::task] | 32 | #[embassy_executor::task] |
| @@ -83,11 +74,11 @@ async fn main(spawner: Spawner) { | |||
| 83 | let mut builder = Builder::new( | 74 | let mut builder = Builder::new( |
| 84 | driver, | 75 | driver, |
| 85 | config, | 76 | config, |
| 86 | &mut singleton!([0; 256])[..], | 77 | &mut make_static!([0; 256])[..], |
| 87 | &mut singleton!([0; 256])[..], | 78 | &mut make_static!([0; 256])[..], |
| 88 | &mut singleton!([0; 256])[..], | 79 | &mut make_static!([0; 256])[..], |
| 89 | &mut singleton!([0; 128])[..], | 80 | &mut make_static!([0; 128])[..], |
| 90 | &mut singleton!([0; 128])[..], | 81 | &mut make_static!([0; 128])[..], |
| 91 | ); | 82 | ); |
| 92 | 83 | ||
| 93 | // Our MAC addr. | 84 | // Our MAC addr. |
| @@ -96,14 +87,14 @@ async fn main(spawner: Spawner) { | |||
| 96 | let host_mac_addr = [0x88, 0x88, 0x88, 0x88, 0x88, 0x88]; | 87 | let host_mac_addr = [0x88, 0x88, 0x88, 0x88, 0x88, 0x88]; |
| 97 | 88 | ||
| 98 | // Create classes on the builder. | 89 | // Create classes on the builder. |
| 99 | let class = CdcNcmClass::new(&mut builder, singleton!(State::new()), host_mac_addr, 64); | 90 | let class = CdcNcmClass::new(&mut builder, make_static!(State::new()), host_mac_addr, 64); |
| 100 | 91 | ||
| 101 | // Build the builder. | 92 | // Build the builder. |
| 102 | let usb = builder.build(); | 93 | let usb = builder.build(); |
| 103 | 94 | ||
| 104 | unwrap!(spawner.spawn(usb_task(usb))); | 95 | unwrap!(spawner.spawn(usb_task(usb))); |
| 105 | 96 | ||
| 106 | let (runner, device) = class.into_embassy_net_device::<MTU, 4, 4>(singleton!(NetState::new()), our_mac_addr); | 97 | let (runner, device) = class.into_embassy_net_device::<MTU, 4, 4>(make_static!(NetState::new()), our_mac_addr); |
| 107 | unwrap!(spawner.spawn(usb_ncm_task(runner))); | 98 | unwrap!(spawner.spawn(usb_ncm_task(runner))); |
| 108 | 99 | ||
| 109 | let config = embassy_net::Config::Dhcp(Default::default()); | 100 | let config = embassy_net::Config::Dhcp(Default::default()); |
| @@ -120,7 +111,12 @@ async fn main(spawner: Spawner) { | |||
| 120 | let seed = u64::from_le_bytes(seed); | 111 | let seed = u64::from_le_bytes(seed); |
| 121 | 112 | ||
| 122 | // Init network stack | 113 | // Init network stack |
| 123 | let stack = &*singleton!(Stack::new(device, config, singleton!(StackResources::<2>::new()), seed)); | 114 | let stack = &*make_static!(Stack::new( |
| 115 | device, | ||
| 116 | config, | ||
| 117 | make_static!(StackResources::<2>::new()), | ||
| 118 | seed | ||
| 119 | )); | ||
| 124 | 120 | ||
| 125 | unwrap!(spawner.spawn(net_task(stack))); | 121 | unwrap!(spawner.spawn(net_task(stack))); |
| 126 | 122 | ||
diff --git a/examples/nrf52840/src/bin/usb_serial_multitask.rs b/examples/nrf52840/src/bin/usb_serial_multitask.rs index ac22d9499..cd4392903 100644 --- a/examples/nrf52840/src/bin/usb_serial_multitask.rs +++ b/examples/nrf52840/src/bin/usb_serial_multitask.rs | |||
| @@ -12,7 +12,7 @@ use embassy_nrf::{bind_interrupts, pac, peripherals, usb}; | |||
| 12 | use embassy_usb::class::cdc_acm::{CdcAcmClass, State}; | 12 | use embassy_usb::class::cdc_acm::{CdcAcmClass, State}; |
| 13 | use embassy_usb::driver::EndpointError; | 13 | use embassy_usb::driver::EndpointError; |
| 14 | use embassy_usb::{Builder, Config, UsbDevice}; | 14 | use embassy_usb::{Builder, Config, UsbDevice}; |
| 15 | use static_cell::StaticCell; | 15 | use static_cell::make_static; |
| 16 | use {defmt_rtt as _, panic_probe as _}; | 16 | use {defmt_rtt as _, panic_probe as _}; |
| 17 | 17 | ||
| 18 | bind_interrupts!(struct Irqs { | 18 | bind_interrupts!(struct Irqs { |
| @@ -20,15 +20,6 @@ bind_interrupts!(struct Irqs { | |||
| 20 | POWER_CLOCK => usb::vbus_detect::InterruptHandler; | 20 | POWER_CLOCK => usb::vbus_detect::InterruptHandler; |
| 21 | }); | 21 | }); |
| 22 | 22 | ||
| 23 | macro_rules! singleton { | ||
| 24 | ($val:expr) => {{ | ||
| 25 | type T = impl Sized; | ||
| 26 | static STATIC_CELL: StaticCell<T> = StaticCell::new(); | ||
| 27 | let (x,) = STATIC_CELL.init(($val,)); | ||
| 28 | x | ||
| 29 | }}; | ||
| 30 | } | ||
| 31 | |||
| 32 | type MyDriver = Driver<'static, peripherals::USBD, HardwareVbusDetect>; | 23 | type MyDriver = Driver<'static, peripherals::USBD, HardwareVbusDetect>; |
| 33 | 24 | ||
| 34 | #[embassy_executor::task] | 25 | #[embassy_executor::task] |
| @@ -73,17 +64,17 @@ async fn main(spawner: Spawner) { | |||
| 73 | config.device_protocol = 0x01; | 64 | config.device_protocol = 0x01; |
| 74 | config.composite_with_iads = true; | 65 | config.composite_with_iads = true; |
| 75 | 66 | ||
| 76 | let state = singleton!(State::new()); | 67 | let state = make_static!(State::new()); |
| 77 | 68 | ||
| 78 | // Create embassy-usb DeviceBuilder using the driver and config. | 69 | // Create embassy-usb DeviceBuilder using the driver and config. |
| 79 | let mut builder = Builder::new( | 70 | let mut builder = Builder::new( |
| 80 | driver, | 71 | driver, |
| 81 | config, | 72 | config, |
| 82 | &mut singleton!([0; 256])[..], | 73 | &mut make_static!([0; 256])[..], |
| 83 | &mut singleton!([0; 256])[..], | 74 | &mut make_static!([0; 256])[..], |
| 84 | &mut singleton!([0; 256])[..], | 75 | &mut make_static!([0; 256])[..], |
| 85 | &mut singleton!([0; 128])[..], | 76 | &mut make_static!([0; 128])[..], |
| 86 | &mut singleton!([0; 128])[..], | 77 | &mut make_static!([0; 128])[..], |
| 87 | ); | 78 | ); |
| 88 | 79 | ||
| 89 | // Create classes on the builder. | 80 | // Create classes on the builder. |
