diff options
| author | Dario Nieuwenhuis <[email protected]> | 2021-08-02 12:40:01 +0200 |
|---|---|---|
| committer | Dario Nieuwenhuis <[email protected]> | 2021-08-02 19:55:04 +0200 |
| commit | 63ac7ac799b07c18a9a621b4d20ead2b39982ef5 (patch) | |
| tree | 714ed050cb55ef90bfa2d3a94e9318e8e2acc724 | |
| parent | af87031d62ca9ee5e7dd44cba297f3d171ec0708 (diff) | |
Mark `new`s as unsafe due to not being leak-safe.
| -rw-r--r-- | embassy-hal-common/src/usb/mod.rs | 5 | ||||
| -rw-r--r-- | embassy-stm32/src/eth/v2/mod.rs | 121 | ||||
| -rw-r--r-- | examples/stm32h7/src/bin/eth.rs | 10 |
3 files changed, 69 insertions, 67 deletions
diff --git a/embassy-hal-common/src/usb/mod.rs b/embassy-hal-common/src/usb/mod.rs index 0940e6b02..ae9f26075 100644 --- a/embassy-hal-common/src/usb/mod.rs +++ b/embassy-hal-common/src/usb/mod.rs | |||
| @@ -60,7 +60,8 @@ where | |||
| 60 | T: ClassSet<B>, | 60 | T: ClassSet<B>, |
| 61 | I: USBInterrupt, | 61 | I: USBInterrupt, |
| 62 | { | 62 | { |
| 63 | pub fn new<S: IntoClassSet<B, T>>( | 63 | /// safety: the returned instance is not leak-safe |
| 64 | pub unsafe fn new<S: IntoClassSet<B, T>>( | ||
| 64 | state: &'bus mut State<'bus, B, T, I>, | 65 | state: &'bus mut State<'bus, B, T, I>, |
| 65 | device: UsbDevice<'bus, B>, | 66 | device: UsbDevice<'bus, B>, |
| 66 | class_set: S, | 67 | class_set: S, |
| @@ -71,7 +72,7 @@ where | |||
| 71 | classes: class_set.into_class_set(), | 72 | classes: class_set.into_class_set(), |
| 72 | _interrupt: PhantomData, | 73 | _interrupt: PhantomData, |
| 73 | }; | 74 | }; |
| 74 | let mutex = unsafe { PeripheralMutex::new_unchecked(&mut state.0, initial_state, irq) }; | 75 | let mutex = PeripheralMutex::new_unchecked(&mut state.0, initial_state, irq); |
| 75 | Self { | 76 | Self { |
| 76 | inner: RefCell::new(mutex), | 77 | inner: RefCell::new(mutex), |
| 77 | } | 78 | } |
diff --git a/embassy-stm32/src/eth/v2/mod.rs b/embassy-stm32/src/eth/v2/mod.rs index 2c73e0d03..37bc9715a 100644 --- a/embassy-stm32/src/eth/v2/mod.rs +++ b/embassy-stm32/src/eth/v2/mod.rs | |||
| @@ -34,7 +34,8 @@ pub struct Ethernet<'d, P: PHY, const TX: usize, const RX: usize> { | |||
| 34 | } | 34 | } |
| 35 | 35 | ||
| 36 | impl<'d, P: PHY, const TX: usize, const RX: usize> Ethernet<'d, P, TX, RX> { | 36 | impl<'d, P: PHY, const TX: usize, const RX: usize> Ethernet<'d, P, TX, RX> { |
| 37 | pub fn new( | 37 | /// safety: the returned instance is not leak-safe |
| 38 | pub unsafe fn new( | ||
| 38 | state: &'d mut State<'d, TX, RX>, | 39 | state: &'d mut State<'d, TX, RX>, |
| 39 | peri: impl Unborrow<Target = peripherals::ETH> + 'd, | 40 | peri: impl Unborrow<Target = peripherals::ETH> + 'd, |
| 40 | interrupt: impl Unborrow<Target = crate::interrupt::ETH> + 'd, | 41 | 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> { | |||
| 55 | 56 | ||
| 56 | // Enable the necessary Clocks | 57 | // Enable the necessary Clocks |
| 57 | // NOTE(unsafe) We have exclusive access to the registers | 58 | // NOTE(unsafe) We have exclusive access to the registers |
| 58 | critical_section::with(|_| unsafe { | 59 | critical_section::with(|_| { |
| 59 | RCC.apb4enr().modify(|w| w.set_syscfgen(true)); | 60 | RCC.apb4enr().modify(|w| w.set_syscfgen(true)); |
| 60 | RCC.ahb1enr().modify(|w| { | 61 | RCC.ahb1enr().modify(|w| { |
| 61 | w.set_eth1macen(true); | 62 | w.set_eth1macen(true); |
| @@ -78,52 +79,52 @@ impl<'d, P: PHY, const TX: usize, const RX: usize> Ethernet<'d, P, TX, RX> { | |||
| 78 | tx_en.configure(); | 79 | tx_en.configure(); |
| 79 | 80 | ||
| 80 | let inner = Inner::new(peri); | 81 | let inner = Inner::new(peri); |
| 81 | let state = unsafe { PeripheralMutex::new_unchecked(&mut state.0, inner, interrupt) }; | ||
| 82 | 82 | ||
| 83 | // NOTE(unsafe) We have exclusive access to the registers | 83 | // NOTE(unsafe) We are ourselves not leak-safe. |
| 84 | unsafe { | 84 | let state = PeripheralMutex::new_unchecked(&mut state.0, inner, interrupt); |
| 85 | let dma = ETH.ethernet_dma(); | ||
| 86 | let mac = ETH.ethernet_mac(); | ||
| 87 | let mtl = ETH.ethernet_mtl(); | ||
| 88 | |||
| 89 | // Reset and wait | ||
| 90 | dma.dmamr().modify(|w| w.set_swr(true)); | ||
| 91 | while dma.dmamr().read().swr() {} | ||
| 92 | 85 | ||
| 93 | mac.maccr().modify(|w| { | 86 | // NOTE(unsafe) We have exclusive access to the registers |
| 94 | w.set_ipg(0b000); // 96 bit times | 87 | let dma = ETH.ethernet_dma(); |
| 95 | w.set_acs(true); | 88 | let mac = ETH.ethernet_mac(); |
| 96 | w.set_fes(true); | 89 | let mtl = ETH.ethernet_mtl(); |
| 97 | w.set_dm(true); | 90 | |
| 98 | // TODO: Carrier sense ? ECRSFD | 91 | // Reset and wait |
| 99 | }); | 92 | dma.dmamr().modify(|w| w.set_swr(true)); |
| 93 | while dma.dmamr().read().swr() {} | ||
| 94 | |||
| 95 | mac.maccr().modify(|w| { | ||
| 96 | w.set_ipg(0b000); // 96 bit times | ||
| 97 | w.set_acs(true); | ||
| 98 | w.set_fes(true); | ||
| 99 | w.set_dm(true); | ||
| 100 | // TODO: Carrier sense ? ECRSFD | ||
| 101 | }); | ||
| 100 | 102 | ||
| 101 | mac.maca0lr().write(|w| { | 103 | mac.maca0lr().write(|w| { |
| 102 | w.set_addrlo( | 104 | w.set_addrlo( |
| 103 | u32::from(mac_addr[0]) | 105 | u32::from(mac_addr[0]) |
| 104 | | (u32::from(mac_addr[1]) << 8) | 106 | | (u32::from(mac_addr[1]) << 8) |
| 105 | | (u32::from(mac_addr[2]) << 16) | 107 | | (u32::from(mac_addr[2]) << 16) |
| 106 | | (u32::from(mac_addr[3]) << 24), | 108 | | (u32::from(mac_addr[3]) << 24), |
| 107 | ) | 109 | ) |
| 108 | }); | 110 | }); |
| 109 | mac.maca0hr() | 111 | mac.maca0hr() |
| 110 | .modify(|w| w.set_addrhi(u16::from(mac_addr[4]) | (u16::from(mac_addr[5]) << 8))); | 112 | .modify(|w| w.set_addrhi(u16::from(mac_addr[4]) | (u16::from(mac_addr[5]) << 8))); |
| 111 | 113 | ||
| 112 | mac.macpfr().modify(|w| w.set_saf(true)); | 114 | mac.macpfr().modify(|w| w.set_saf(true)); |
| 113 | mac.macqtx_fcr().modify(|w| w.set_pt(0x100)); | 115 | mac.macqtx_fcr().modify(|w| w.set_pt(0x100)); |
| 114 | 116 | ||
| 115 | mtl.mtlrx_qomr().modify(|w| w.set_rsf(true)); | 117 | mtl.mtlrx_qomr().modify(|w| w.set_rsf(true)); |
| 116 | mtl.mtltx_qomr().modify(|w| w.set_tsf(true)); | 118 | mtl.mtltx_qomr().modify(|w| w.set_tsf(true)); |
| 117 | 119 | ||
| 118 | dma.dmactx_cr().modify(|w| w.set_txpbl(1)); // 32 ? | 120 | dma.dmactx_cr().modify(|w| w.set_txpbl(1)); // 32 ? |
| 119 | dma.dmacrx_cr().modify(|w| { | 121 | dma.dmacrx_cr().modify(|w| { |
| 120 | w.set_rxpbl(1); // 32 ? | 122 | w.set_rxpbl(1); // 32 ? |
| 121 | w.set_rbsz(MTU as u16); | 123 | w.set_rbsz(MTU as u16); |
| 122 | }); | 124 | }); |
| 123 | } | ||
| 124 | 125 | ||
| 125 | // NOTE(unsafe) We got the peripheral singleton, which means that `rcc::init` was called | 126 | // NOTE(unsafe) We got the peripheral singleton, which means that `rcc::init` was called |
| 126 | let hclk = unsafe { crate::rcc::get_freqs().ahb1 }; | 127 | let hclk = crate::rcc::get_freqs().ahb1; |
| 127 | let hclk_mhz = hclk.0 / 1_000_000; | 128 | let hclk_mhz = hclk.0 / 1_000_000; |
| 128 | 129 | ||
| 129 | // Set the MDC clock frequency in the range 1MHz - 2.5MHz | 130 | // Set the MDC clock frequency in the range 1MHz - 2.5MHz |
| @@ -165,27 +166,25 @@ impl<'d, P: PHY, const TX: usize, const RX: usize> Ethernet<'d, P, TX, RX> { | |||
| 165 | 166 | ||
| 166 | fence(Ordering::SeqCst); | 167 | fence(Ordering::SeqCst); |
| 167 | 168 | ||
| 168 | unsafe { | 169 | let mac = ETH.ethernet_mac(); |
| 169 | let mac = ETH.ethernet_mac(); | 170 | let mtl = ETH.ethernet_mtl(); |
| 170 | let mtl = ETH.ethernet_mtl(); | 171 | let dma = ETH.ethernet_dma(); |
| 171 | let dma = ETH.ethernet_dma(); | 172 | |
| 172 | 173 | mac.maccr().modify(|w| { | |
| 173 | mac.maccr().modify(|w| { | 174 | w.set_re(true); |
| 174 | w.set_re(true); | 175 | w.set_te(true); |
| 175 | w.set_te(true); | 176 | }); |
| 176 | }); | 177 | mtl.mtltx_qomr().modify(|w| w.set_ftq(true)); |
| 177 | mtl.mtltx_qomr().modify(|w| w.set_ftq(true)); | 178 | |
| 178 | 179 | dma.dmactx_cr().modify(|w| w.set_st(true)); | |
| 179 | dma.dmactx_cr().modify(|w| w.set_st(true)); | 180 | dma.dmacrx_cr().modify(|w| w.set_sr(true)); |
| 180 | dma.dmacrx_cr().modify(|w| w.set_sr(true)); | 181 | |
| 181 | 182 | // Enable interrupts | |
| 182 | // Enable interrupts | 183 | dma.dmacier().modify(|w| { |
| 183 | dma.dmacier().modify(|w| { | 184 | w.set_nie(true); |
| 184 | w.set_nie(true); | 185 | w.set_rie(true); |
| 185 | w.set_rie(true); | 186 | w.set_tie(true); |
| 186 | w.set_tie(true); | 187 | }); |
| 187 | }); | ||
| 188 | } | ||
| 189 | }); | 188 | }); |
| 190 | P::phy_reset(&mut this); | 189 | P::phy_reset(&mut this); |
| 191 | P::phy_init(&mut this); | 190 | P::phy_init(&mut this); |
diff --git a/examples/stm32h7/src/bin/eth.rs b/examples/stm32h7/src/bin/eth.rs index 5cf49e82f..e49a101bf 100644 --- a/examples/stm32h7/src/bin/eth.rs +++ b/examples/stm32h7/src/bin/eth.rs | |||
| @@ -135,10 +135,12 @@ fn main() -> ! { | |||
| 135 | let eth_int = interrupt_take!(ETH); | 135 | let eth_int = interrupt_take!(ETH); |
| 136 | let mac_addr = [0x10; 6]; | 136 | let mac_addr = [0x10; 6]; |
| 137 | let state = STATE.put(State::new()); | 137 | let state = STATE.put(State::new()); |
| 138 | let eth = ETH.put(Ethernet::new( | 138 | let eth = unsafe { |
| 139 | state, p.ETH, eth_int, p.PA1, p.PA2, p.PC1, p.PA7, p.PC4, p.PC5, p.PB12, p.PB13, p.PB11, | 139 | ETH.put(Ethernet::new( |
| 140 | LAN8742A, mac_addr, 1, | 140 | state, p.ETH, eth_int, p.PA1, p.PA2, p.PC1, p.PA7, p.PC4, p.PC5, p.PB12, p.PB13, |
| 141 | )); | 141 | p.PB11, LAN8742A, mac_addr, 1, |
| 142 | )) | ||
| 143 | }; | ||
| 142 | 144 | ||
| 143 | let config = StaticConfigurator::new(NetConfig { | 145 | let config = StaticConfigurator::new(NetConfig { |
| 144 | address: Ipv4Cidr::new(Ipv4Address::new(192, 168, 0, 61), 24), | 146 | address: Ipv4Cidr::new(Ipv4Address::new(192, 168, 0, 61), 24), |
