Mark news as unsafe due to not being leak-safe.

This commit is contained in:
Dario Nieuwenhuis 2021-08-02 12:40:01 +02:00
parent af87031d62
commit 63ac7ac799
3 changed files with 64 additions and 62 deletions

View File

@ -60,7 +60,8 @@ where
T: ClassSet<B>, T: ClassSet<B>,
I: USBInterrupt, I: USBInterrupt,
{ {
pub fn new<S: IntoClassSet<B, T>>( /// safety: the returned instance is not leak-safe
pub unsafe fn new<S: IntoClassSet<B, T>>(
state: &'bus mut State<'bus, B, T, I>, state: &'bus mut State<'bus, B, T, I>,
device: UsbDevice<'bus, B>, device: UsbDevice<'bus, B>,
class_set: S, class_set: S,
@ -71,7 +72,7 @@ where
classes: class_set.into_class_set(), classes: class_set.into_class_set(),
_interrupt: PhantomData, _interrupt: PhantomData,
}; };
let mutex = unsafe { PeripheralMutex::new_unchecked(&mut state.0, initial_state, irq) }; let mutex = PeripheralMutex::new_unchecked(&mut state.0, initial_state, irq);
Self { Self {
inner: RefCell::new(mutex), inner: RefCell::new(mutex),
} }

View File

@ -34,7 +34,8 @@ pub struct Ethernet<'d, P: PHY, const TX: usize, const RX: usize> {
} }
impl<'d, P: PHY, const TX: usize, const RX: usize> Ethernet<'d, P, TX, RX> { impl<'d, P: PHY, const TX: usize, const RX: usize> Ethernet<'d, P, TX, RX> {
pub fn new( /// safety: the returned instance is not leak-safe
pub unsafe fn new(
state: &'d mut State<'d, TX, RX>, state: &'d mut State<'d, TX, RX>,
peri: impl Unborrow<Target = peripherals::ETH> + 'd, peri: impl Unborrow<Target = peripherals::ETH> + 'd,
interrupt: impl Unborrow<Target = crate::interrupt::ETH> + 'd, interrupt: impl Unborrow<Target = crate::interrupt::ETH> + 'd,
@ -55,7 +56,7 @@ impl<'d, P: PHY, const TX: usize, const RX: usize> Ethernet<'d, P, TX, RX> {
// Enable the necessary Clocks // Enable the necessary Clocks
// NOTE(unsafe) We have exclusive access to the registers // NOTE(unsafe) We have exclusive access to the registers
critical_section::with(|_| unsafe { critical_section::with(|_| {
RCC.apb4enr().modify(|w| w.set_syscfgen(true)); RCC.apb4enr().modify(|w| w.set_syscfgen(true));
RCC.ahb1enr().modify(|w| { RCC.ahb1enr().modify(|w| {
w.set_eth1macen(true); w.set_eth1macen(true);
@ -78,10 +79,11 @@ impl<'d, P: PHY, const TX: usize, const RX: usize> Ethernet<'d, P, TX, RX> {
tx_en.configure(); tx_en.configure();
let inner = Inner::new(peri); let inner = Inner::new(peri);
let state = unsafe { PeripheralMutex::new_unchecked(&mut state.0, inner, interrupt) };
// NOTE(unsafe) We are ourselves not leak-safe.
let state = PeripheralMutex::new_unchecked(&mut state.0, inner, interrupt);
// NOTE(unsafe) We have exclusive access to the registers // NOTE(unsafe) We have exclusive access to the registers
unsafe {
let dma = ETH.ethernet_dma(); let dma = ETH.ethernet_dma();
let mac = ETH.ethernet_mac(); let mac = ETH.ethernet_mac();
let mtl = ETH.ethernet_mtl(); let mtl = ETH.ethernet_mtl();
@ -120,10 +122,9 @@ impl<'d, P: PHY, const TX: usize, const RX: usize> Ethernet<'d, P, TX, RX> {
w.set_rxpbl(1); // 32 ? w.set_rxpbl(1); // 32 ?
w.set_rbsz(MTU as u16); w.set_rbsz(MTU as u16);
}); });
}
// NOTE(unsafe) We got the peripheral singleton, which means that `rcc::init` was called // NOTE(unsafe) We got the peripheral singleton, which means that `rcc::init` was called
let hclk = unsafe { crate::rcc::get_freqs().ahb1 }; let hclk = crate::rcc::get_freqs().ahb1;
let hclk_mhz = hclk.0 / 1_000_000; let hclk_mhz = hclk.0 / 1_000_000;
// Set the MDC clock frequency in the range 1MHz - 2.5MHz // Set the MDC clock frequency in the range 1MHz - 2.5MHz
@ -165,7 +166,6 @@ impl<'d, P: PHY, const TX: usize, const RX: usize> Ethernet<'d, P, TX, RX> {
fence(Ordering::SeqCst); fence(Ordering::SeqCst);
unsafe {
let mac = ETH.ethernet_mac(); let mac = ETH.ethernet_mac();
let mtl = ETH.ethernet_mtl(); let mtl = ETH.ethernet_mtl();
let dma = ETH.ethernet_dma(); let dma = ETH.ethernet_dma();
@ -185,7 +185,6 @@ impl<'d, P: PHY, const TX: usize, const RX: usize> Ethernet<'d, P, TX, RX> {
w.set_rie(true); w.set_rie(true);
w.set_tie(true); w.set_tie(true);
}); });
}
}); });
P::phy_reset(&mut this); P::phy_reset(&mut this);
P::phy_init(&mut this); P::phy_init(&mut this);

View File

@ -135,10 +135,12 @@ fn main() -> ! {
let eth_int = interrupt_take!(ETH); let eth_int = interrupt_take!(ETH);
let mac_addr = [0x10; 6]; let mac_addr = [0x10; 6];
let state = STATE.put(State::new()); let state = STATE.put(State::new());
let eth = ETH.put(Ethernet::new( let eth = unsafe {
state, p.ETH, eth_int, p.PA1, p.PA2, p.PC1, p.PA7, p.PC4, p.PC5, p.PB12, p.PB13, p.PB11, ETH.put(Ethernet::new(
LAN8742A, mac_addr, 1, state, p.ETH, eth_int, p.PA1, p.PA2, p.PC1, p.PA7, p.PC4, p.PC5, p.PB12, p.PB13,
)); p.PB11, LAN8742A, mac_addr, 1,
))
};
let config = StaticConfigurator::new(NetConfig { let config = StaticConfigurator::new(NetConfig {
address: Ipv4Cidr::new(Ipv4Address::new(192, 168, 0, 61), 24), address: Ipv4Cidr::new(Ipv4Address::new(192, 168, 0, 61), 24),