Skip to content

Commit a901456

Browse files
committed
static_cell
1 parent df69b65 commit a901456

File tree

10 files changed

+47
-41
lines changed

10 files changed

+47
-41
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ version = "0.5.0"
9393
optional = true
9494

9595
[dev-dependencies]
96+
static_cell = "2.1.1"
9697
defmt = "1.0.1"
9798
defmt-rtt = "1.0"
9899
panic-probe = { version = "1.0", features = ["print-defmt"] }

examples/ltdc-screen/main.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use embedded_graphics::{
1313
text::Text,
1414
};
1515

16+
use static_cell::ConstStaticCell;
1617
use stm32f4xx_hal::{
1718
ltdc::{BluePins, GreenPins, Layer, LtdcPins, PixelFormat, RedPins},
1819
pac,
@@ -28,7 +29,8 @@ const HEIGHT: u16 = 272;
2829

2930
// Graphics framebuffer
3031
const FB_GRAPHICS_SIZE: usize = (WIDTH as usize) * (HEIGHT as usize);
31-
static mut FB_LAYER1: [u16; FB_GRAPHICS_SIZE] = [0; FB_GRAPHICS_SIZE];
32+
pub static FB_LAYER1: ConstStaticCell<[u16; FB_GRAPHICS_SIZE]> =
33+
ConstStaticCell::new([0; FB_GRAPHICS_SIZE]);
3234

3335
#[entry]
3436
fn main() -> ! {
@@ -85,7 +87,7 @@ fn main() -> ! {
8587
let mut display = screen::Stm32F7DiscoDisplay::new(perif.LTDC, perif.DMA2D, pins);
8688
display
8789
.controller
88-
.config_layer(Layer::L1, unsafe { &mut FB_LAYER1 }, PixelFormat::RGB565);
90+
.config_layer(Layer::L1, FB_LAYER1.take(), PixelFormat::RGB565);
8991

9092
display.controller.enable_layer(Layer::L1);
9193
display.controller.reload();

examples/qspi-w25q.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ pub struct W25Q<PINS: QspiPins> {
1818
qspi: Qspi<PINS>,
1919
}
2020

21+
#[allow(unused)]
2122
pub struct DeviceId(u8);
2223

2324
impl<PINS> W25Q<PINS>

examples/rtic-usb-cdc-echo.rs

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use panic_halt as _;
55

66
#[rtic::app(device = stm32f4xx_hal::pac, peripherals = true, dispatchers = [USART1])]
77
mod app {
8+
use static_cell::{ConstStaticCell, StaticCell};
89
use stm32f4xx_hal::{
910
gpio::{Output, PC13},
1011
otg_fs::{UsbBus, UsbBusType, USB},
@@ -33,8 +34,9 @@ mod app {
3334

3435
#[init]
3536
fn init(ctx: init::Context) -> (Shared, Local, init::Monotonics) {
36-
static mut EP_MEMORY: [u32; 1024] = [0; 1024];
37-
static mut USB_BUS: Option<usb_device::bus::UsbBusAllocator<UsbBusType>> = None;
37+
static EP_MEMORY: ConstStaticCell<[u32; 1024]> = ConstStaticCell::new([0; 1024]);
38+
static USB_BUS: StaticCell<usb_device::bus::UsbBusAllocator<UsbBusType>> =
39+
StaticCell::new();
3840

3941
let dp = ctx.device;
4042

@@ -59,22 +61,17 @@ mod app {
5961
pin_dp: gpioa.pa12.into(),
6062
hclk: rcc.clocks.hclk(),
6163
};
62-
unsafe {
63-
USB_BUS.replace(UsbBus::new(usb, &mut EP_MEMORY));
64-
}
65-
66-
let usb_serial = usbd_serial::SerialPort::new(unsafe { USB_BUS.as_ref().unwrap() });
67-
let usb_dev = UsbDeviceBuilder::new(
68-
unsafe { USB_BUS.as_ref().unwrap() },
69-
UsbVidPid(0x16c0, 0x27dd),
70-
)
71-
.device_class(usbd_serial::USB_CLASS_CDC)
72-
.strings(&[StringDescriptors::default()
73-
.manufacturer("Fake Company")
74-
.product("Product")
75-
.serial_number("TEST")])
76-
.unwrap()
77-
.build();
64+
let usb_bus = USB_BUS.init(UsbBus::new(usb, EP_MEMORY.take()));
65+
66+
let usb_serial = usbd_serial::SerialPort::new(usb_bus);
67+
let usb_dev = UsbDeviceBuilder::new(usb_bus, UsbVidPid(0x16c0, 0x27dd))
68+
.device_class(usbd_serial::USB_CLASS_CDC)
69+
.strings(&[StringDescriptors::default()
70+
.manufacturer("Fake Company")
71+
.product("Product")
72+
.serial_number("TEST")])
73+
.unwrap()
74+
.build();
7875

7976
(
8077
Shared {

examples/spi-dma.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use core::cell::RefCell;
99
use cortex_m::interrupt::Mutex;
1010
use cortex_m_rt::entry;
1111
use embedded_hal_02::spi::{Mode, Phase, Polarity};
12+
use static_cell::ConstStaticCell;
1213
use stm32f4xx_hal::pac::interrupt;
1314
use stm32f4xx_hal::{
1415
dma::{config, MemoryToPeripheral, Stream4, StreamsTuple, Transfer},
@@ -105,12 +106,11 @@ fn DMA1_STREAM4() {
105106
// Its important to clear fifo errors as the transfer is paused until it is cleared
106107
transfer.clear_flags(DmaFlag::FifoError | DmaFlag::TransferComplete);
107108
if flags.is_transfer_complete() {
108-
unsafe {
109-
static mut BUFFER: [u8; ARRAY_SIZE] = [0; ARRAY_SIZE];
110-
for (i, b) in BUFFER.iter_mut().enumerate() {
111-
*b = (i + 1) as u8;
112-
}
113-
transfer.next_transfer(&mut BUFFER).unwrap();
109+
pub static BUFFER: ConstStaticCell<[u8; ARRAY_SIZE]> =
110+
ConstStaticCell::new([0; ARRAY_SIZE]);
111+
for (i, b) in BUFFER.take().iter_mut().enumerate() {
112+
*b = (i + 1) as u8;
114113
}
114+
transfer.next_transfer(BUFFER.take()).unwrap();
115115
}
116116
}

examples/uart-dma.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use stm32f4xx_hal::dma::{DmaFlag, PeripheralToMemory, Stream1};
88
use core::cell::RefCell;
99
use cortex_m::interrupt::Mutex;
1010
use cortex_m_rt::entry;
11+
use static_cell::ConstStaticCell;
1112
use stm32f4xx_hal::dma::config::DmaConfig;
1213
use stm32f4xx_hal::pac::Interrupt;
1314
use stm32f4xx_hal::pac::{interrupt, DMA1};
@@ -74,7 +75,8 @@ pub static G_UART3_TX: Mutex<RefCell<Option<serial::Tx<pac::USART3>>>> =
7475
Mutex::new(RefCell::new(None));
7576

7677
// dma buffer
77-
pub static mut RX_UART3_BUFFER: [u8; UART_BUFFER_SIZE] = [0; UART_BUFFER_SIZE];
78+
pub static RX_UART3_BUFFER: ConstStaticCell<[u8; UART_BUFFER_SIZE]> =
79+
ConstStaticCell::new([0; UART_BUFFER_SIZE]);
7880

7981
// a wrapper function that reads out of the uart ring buffer
8082
pub fn uart3_read_until(eol: u8) -> Option<[u8; UART_BUFFER_SIZE]> {
@@ -208,16 +210,14 @@ fn USART3() {
208210
if transfer.is_idle() {
209211
// Calc received bytes count
210212
let bytes_count = UART_BUFFER_SIZE - transfer.number_of_transfers() as usize;
211-
unsafe {
212-
let mut buffer = [0; UART_BUFFER_SIZE];
213-
match transfer.next_transfer(&mut RX_UART3_BUFFER) {
214-
Ok((b, _)) => buffer = *b,
215-
Err(_err) => {}
216-
}
217-
if let Some(ring_buffer) = G_UART3_BUFFER.borrow(cs).borrow_mut().as_mut() {
218-
for i in 0..bytes_count {
219-
ring_buffer.push(buffer[i]);
220-
}
213+
let mut buffer = [0; UART_BUFFER_SIZE];
214+
match transfer.next_transfer(RX_UART3_BUFFER.take()) {
215+
Ok((b, _)) => buffer = *b,
216+
Err(_err) => {}
217+
}
218+
if let Some(ring_buffer) = G_UART3_BUFFER.borrow(cs).borrow_mut().as_mut() {
219+
for i in 0..bytes_count {
220+
ring_buffer.push(buffer[i]);
221221
}
222222
}
223223
}

examples/usb-serial-poll.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@
66
use panic_halt as _;
77

88
use cortex_m_rt::entry;
9+
use static_cell::ConstStaticCell;
910
use stm32f4xx_hal::otg_fs::{UsbBus, USB};
1011
use stm32f4xx_hal::rcc::Config;
1112
use stm32f4xx_hal::{pac, prelude::*};
1213
use usb_device::prelude::*;
1314

14-
static mut EP_MEMORY: [u32; 1024] = [0; 1024];
15+
// Statically allocate memory for a `u32`.
16+
static EP_MEMORY: ConstStaticCell<[u32; 1024]> = ConstStaticCell::new([0; 1024]);
1517

1618
#[entry]
1719
fn main() -> ! {
@@ -29,7 +31,7 @@ fn main() -> ! {
2931
&rcc.clocks,
3032
);
3133

32-
let usb_bus = UsbBus::new(usb, unsafe { &mut EP_MEMORY });
34+
let usb_bus = UsbBus::new(usb, EP_MEMORY.take());
3335

3436
let mut serial = usbd_serial::SerialPort::new(&usb_bus);
3537

examples/ws2812-spi.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use hal::{
1010
pac::{self, SPI1},
1111
prelude::*,
1212
};
13-
use smart_leds::{brightness, RGB8, SmartLedsWrite};
13+
use smart_leds::{brightness, SmartLedsWrite, RGB8};
1414
use ws2812_spi as ws2812;
1515

1616
#[entry]

src/rng.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,4 +199,4 @@ impl rand_core_06::RngCore for Rng {
199199
self.try_fill_bytes(buffer)?;
200200
Ok(())
201201
}
202-
}
202+
}

src/timer.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,7 @@ mod sealed {
459459
fn start_pwm(&mut self);
460460
}
461461

462+
#[allow(unused)]
462463
pub trait WithCapture: WithChannel {
463464
fn preload_capture(&mut self, c: u8, mode: CaptureMode);
464465
fn prescaler_capture(&mut self, c: u8, psc: CapturePrescaler);
@@ -476,6 +477,7 @@ mod sealed {
476477
fn split() -> Self::Channels;
477478
}
478479

480+
#[allow(unused)]
479481
pub trait SplitCapture {
480482
type CaptureChannels;
481483
fn split_capture() -> Self::CaptureChannels;
@@ -488,6 +490,7 @@ pub trait Instance:
488490
{
489491
}
490492

493+
#[allow(unused)]
491494
use sealed::{Split, SplitCapture};
492495
macro_rules! split {
493496
($TIM:ty: 1) => {

0 commit comments

Comments
 (0)