diff options
| -rw-r--r-- | embassy-net/Cargo.toml | 6 | ||||
| -rw-r--r-- | embassy-net/src/device.rs | 2 | ||||
| -rw-r--r-- | embassy-net/src/lib.rs | 2 | ||||
| -rw-r--r-- | embassy-net/src/packet_pool.rs | 14 | ||||
| -rw-r--r-- | embassy-stm32/Cargo.toml | 4 | ||||
| -rw-r--r-- | embassy-stm32/src/eth/lan8742a.rs | 103 | ||||
| -rw-r--r-- | embassy-stm32/src/eth/mod.rs | 34 | ||||
| -rw-r--r-- | embassy-stm32/src/eth/v1.rs | 1 | ||||
| -rw-r--r-- | embassy-stm32/src/eth/v2/descriptors.rs | 395 | ||||
| -rw-r--r-- | embassy-stm32/src/eth/v2/mod.rs | 477 | ||||
| -rw-r--r-- | embassy-stm32/src/lib.rs | 2 | ||||
| -rw-r--r-- | examples/stm32h7/.cargo/config | 10 | ||||
| -rw-r--r-- | examples/stm32h7/Cargo.toml | 17 | ||||
| -rw-r--r-- | examples/stm32h7/build.rs | 21 | ||||
| -rw-r--r-- | examples/stm32h7/memory.x | 2 | ||||
| -rw-r--r-- | examples/stm32h7/src/bin/eth.rs | 167 |
16 files changed, 1252 insertions, 5 deletions
diff --git a/embassy-net/Cargo.toml b/embassy-net/Cargo.toml index c64075b93..0f4571761 100644 --- a/embassy-net/Cargo.toml +++ b/embassy-net/Cargo.toml | |||
| @@ -5,6 +5,7 @@ authors = ["Dario Nieuwenhuis <[email protected]>"] | |||
| 5 | edition = "2018" | 5 | edition = "2018" |
| 6 | 6 | ||
| 7 | [features] | 7 | [features] |
| 8 | default = ["pool-4"] | ||
| 8 | std = [] | 9 | std = [] |
| 9 | defmt-trace = [] | 10 | defmt-trace = [] |
| 10 | defmt-debug = [] | 11 | defmt-debug = [] |
| @@ -17,6 +18,11 @@ dhcpv4 = ["medium-ethernet", "smoltcp/socket-dhcpv4"] | |||
| 17 | medium-ethernet = ["smoltcp/medium-ethernet"] | 18 | medium-ethernet = ["smoltcp/medium-ethernet"] |
| 18 | medium-ip = ["smoltcp/medium-ip"] | 19 | medium-ip = ["smoltcp/medium-ip"] |
| 19 | 20 | ||
| 21 | pool-4 = [] | ||
| 22 | pool-8 = [] | ||
| 23 | pool-16 = [] | ||
| 24 | pool-32 = [] | ||
| 25 | |||
| 20 | [dependencies] | 26 | [dependencies] |
| 21 | 27 | ||
| 22 | defmt = { version = "0.2.0", optional = true } | 28 | defmt = { version = "0.2.0", optional = true } |
diff --git a/embassy-net/src/device.rs b/embassy-net/src/device.rs index 5fcb94ac8..fc9d08947 100644 --- a/embassy-net/src/device.rs +++ b/embassy-net/src/device.rs | |||
| @@ -43,8 +43,8 @@ impl<'a> SmolDevice<'a> for DeviceAdapter { | |||
| 43 | type TxToken = TxToken<'a>; | 43 | type TxToken = TxToken<'a>; |
| 44 | 44 | ||
| 45 | fn receive(&'a mut self) -> Option<(Self::RxToken, Self::TxToken)> { | 45 | fn receive(&'a mut self) -> Option<(Self::RxToken, Self::TxToken)> { |
| 46 | let tx_pkt = PacketBox::new(Packet::new())?; | ||
| 46 | let rx_pkt = self.device.receive()?; | 47 | let rx_pkt = self.device.receive()?; |
| 47 | let tx_pkt = PacketBox::new(Packet::new()).unwrap(); // TODO: not sure about unwrap | ||
| 48 | let rx_token = RxToken { pkt: rx_pkt }; | 48 | let rx_token = RxToken { pkt: rx_pkt }; |
| 49 | let tx_token = TxToken { | 49 | let tx_token = TxToken { |
| 50 | device: self.device, | 50 | device: self.device, |
diff --git a/embassy-net/src/lib.rs b/embassy-net/src/lib.rs index 88dcf0aa5..51eb97a2e 100644 --- a/embassy-net/src/lib.rs +++ b/embassy-net/src/lib.rs | |||
| @@ -13,7 +13,7 @@ pub use config::DhcpConfigurator; | |||
| 13 | pub use config::{Config, Configurator, Event as ConfigEvent, StaticConfigurator}; | 13 | pub use config::{Config, Configurator, Event as ConfigEvent, StaticConfigurator}; |
| 14 | 14 | ||
| 15 | pub use device::{Device, LinkState}; | 15 | pub use device::{Device, LinkState}; |
| 16 | pub use packet_pool::{Packet, PacketBox, PacketBoxExt, PacketBuf}; | 16 | pub use packet_pool::{Packet, PacketBox, PacketBoxExt, PacketBuf, MTU}; |
| 17 | pub use stack::{init, is_config_up, is_init, is_link_up, run}; | 17 | pub use stack::{init, is_config_up, is_init, is_link_up, run}; |
| 18 | 18 | ||
| 19 | #[cfg(feature = "tcp")] | 19 | #[cfg(feature = "tcp")] |
diff --git a/embassy-net/src/packet_pool.rs b/embassy-net/src/packet_pool.rs index 2c27d4013..b43ae2eb2 100644 --- a/embassy-net/src/packet_pool.rs +++ b/embassy-net/src/packet_pool.rs | |||
| @@ -3,12 +3,24 @@ use core::ops::{Deref, DerefMut, Range}; | |||
| 3 | 3 | ||
| 4 | use atomic_pool::{pool, Box}; | 4 | use atomic_pool::{pool, Box}; |
| 5 | 5 | ||
| 6 | pub const MTU: usize = 1514; | 6 | pub const MTU: usize = 1516; |
| 7 | |||
| 8 | #[cfg(feature = "pool-4")] | ||
| 7 | pub const PACKET_POOL_SIZE: usize = 4; | 9 | pub const PACKET_POOL_SIZE: usize = 4; |
| 8 | 10 | ||
| 11 | #[cfg(feature = "pool-8")] | ||
| 12 | pub const PACKET_POOL_SIZE: usize = 8; | ||
| 13 | |||
| 14 | #[cfg(feature = "pool-16")] | ||
| 15 | pub const PACKET_POOL_SIZE: usize = 16; | ||
| 16 | |||
| 17 | #[cfg(feature = "pool-32")] | ||
| 18 | pub const PACKET_POOL_SIZE: usize = 32; | ||
| 19 | |||
| 9 | pool!(pub PacketPool: [Packet; PACKET_POOL_SIZE]); | 20 | pool!(pub PacketPool: [Packet; PACKET_POOL_SIZE]); |
| 10 | pub type PacketBox = Box<PacketPool>; | 21 | pub type PacketBox = Box<PacketPool>; |
| 11 | 22 | ||
| 23 | #[repr(align(4))] | ||
| 12 | pub struct Packet(pub [u8; MTU]); | 24 | pub struct Packet(pub [u8; MTU]); |
| 13 | 25 | ||
| 14 | impl Packet { | 26 | impl Packet { |
diff --git a/embassy-stm32/Cargo.toml b/embassy-stm32/Cargo.toml index 682ebaefc..8163624f3 100644 --- a/embassy-stm32/Cargo.toml +++ b/embassy-stm32/Cargo.toml | |||
| @@ -10,6 +10,7 @@ embassy = { version = "0.1.0", path = "../embassy" } | |||
| 10 | embassy-macros = { version = "0.1.0", path = "../embassy-macros", features = ["stm32"] } | 10 | embassy-macros = { version = "0.1.0", path = "../embassy-macros", features = ["stm32"] } |
| 11 | embassy-extras = {version = "0.1.0", path = "../embassy-extras" } | 11 | embassy-extras = {version = "0.1.0", path = "../embassy-extras" } |
| 12 | embassy-traits = {version = "0.1.0", path = "../embassy-traits" } | 12 | embassy-traits = {version = "0.1.0", path = "../embassy-traits" } |
| 13 | embassy-net = { version = "0.1.0", path = "../embassy-net", default-features = false, optional = true } | ||
| 13 | 14 | ||
| 14 | defmt = { version = "0.2.0", optional = true } | 15 | defmt = { version = "0.2.0", optional = true } |
| 15 | log = { version = "0.4.11", optional = true } | 16 | log = { version = "0.4.11", optional = true } |
| @@ -24,6 +25,8 @@ critical-section = "0.2.1" | |||
| 24 | bare-metal = "1.0.0" | 25 | bare-metal = "1.0.0" |
| 25 | atomic-polyfill = "0.1.2" | 26 | atomic-polyfill = "0.1.2" |
| 26 | stm32-metapac = { version = "0.1.0", path = "../stm32-metapac", features = ["rt"] } | 27 | stm32-metapac = { version = "0.1.0", path = "../stm32-metapac", features = ["rt"] } |
| 28 | vcell = { version = "0.1.3", optional = true } | ||
| 29 | |||
| 27 | cfg-if = "1.0.0" | 30 | cfg-if = "1.0.0" |
| 28 | 31 | ||
| 29 | [build-dependencies] | 32 | [build-dependencies] |
| @@ -38,6 +41,7 @@ defmt-info = [ ] | |||
| 38 | defmt-warn = [ ] | 41 | defmt-warn = [ ] |
| 39 | defmt-error = [ ] | 42 | defmt-error = [ ] |
| 40 | sdmmc-rs = ["embedded-sdmmc"] | 43 | sdmmc-rs = ["embedded-sdmmc"] |
| 44 | net = ["embassy-net", "vcell"] | ||
| 41 | 45 | ||
| 42 | # BEGIN GENERATED FEATURES | 46 | # BEGIN GENERATED FEATURES |
| 43 | # Generated by gen_features.py. DO NOT EDIT. | 47 | # Generated by gen_features.py. DO NOT EDIT. |
diff --git a/embassy-stm32/src/eth/lan8742a.rs b/embassy-stm32/src/eth/lan8742a.rs new file mode 100644 index 000000000..74d0ca5de --- /dev/null +++ b/embassy-stm32/src/eth/lan8742a.rs | |||
| @@ -0,0 +1,103 @@ | |||
| 1 | //! SMSC LAN8742A Ethernet PHY | ||
| 2 | |||
| 3 | use super::{StationManagement, PHY}; | ||
| 4 | |||
| 5 | #[allow(dead_code)] | ||
| 6 | mod phy_consts { | ||
| 7 | pub const PHY_REG_BCR: u8 = 0x00; | ||
| 8 | pub const PHY_REG_BSR: u8 = 0x01; | ||
| 9 | pub const PHY_REG_ID1: u8 = 0x02; | ||
| 10 | pub const PHY_REG_ID2: u8 = 0x03; | ||
| 11 | pub const PHY_REG_ANTX: u8 = 0x04; | ||
| 12 | pub const PHY_REG_ANRX: u8 = 0x05; | ||
| 13 | pub const PHY_REG_ANEXP: u8 = 0x06; | ||
| 14 | pub const PHY_REG_ANNPTX: u8 = 0x07; | ||
| 15 | pub const PHY_REG_ANNPRX: u8 = 0x08; | ||
| 16 | pub const PHY_REG_SSR: u8 = 0x1F; // Special Status Register | ||
| 17 | pub const PHY_REG_CTL: u8 = 0x0D; // Ethernet PHY Register Control | ||
| 18 | pub const PHY_REG_ADDAR: u8 = 0x0E; // Ethernet PHY Address or Data | ||
| 19 | |||
| 20 | pub const PHY_REG_WUCSR: u16 = 0x8010; | ||
| 21 | |||
| 22 | pub const PHY_REG_BCR_COLTEST: u16 = 1 << 7; | ||
| 23 | pub const PHY_REG_BCR_FD: u16 = 1 << 8; | ||
| 24 | pub const PHY_REG_BCR_ANRST: u16 = 1 << 9; | ||
| 25 | pub const PHY_REG_BCR_ISOLATE: u16 = 1 << 10; | ||
| 26 | pub const PHY_REG_BCR_POWERDN: u16 = 1 << 11; | ||
| 27 | pub const PHY_REG_BCR_AN: u16 = 1 << 12; | ||
| 28 | pub const PHY_REG_BCR_100M: u16 = 1 << 13; | ||
| 29 | pub const PHY_REG_BCR_LOOPBACK: u16 = 1 << 14; | ||
| 30 | pub const PHY_REG_BCR_RESET: u16 = 1 << 15; | ||
| 31 | |||
| 32 | pub const PHY_REG_BSR_JABBER: u16 = 1 << 1; | ||
| 33 | pub const PHY_REG_BSR_UP: u16 = 1 << 2; | ||
| 34 | pub const PHY_REG_BSR_FAULT: u16 = 1 << 4; | ||
| 35 | pub const PHY_REG_BSR_ANDONE: u16 = 1 << 5; | ||
| 36 | |||
| 37 | pub const PHY_REG_SSR_ANDONE: u16 = 1 << 12; | ||
| 38 | pub const PHY_REG_SSR_SPEED: u16 = 0b111 << 2; | ||
| 39 | pub const PHY_REG_SSR_10BASE_HD: u16 = 0b001 << 2; | ||
| 40 | pub const PHY_REG_SSR_10BASE_FD: u16 = 0b101 << 2; | ||
| 41 | pub const PHY_REG_SSR_100BASE_HD: u16 = 0b010 << 2; | ||
| 42 | pub const PHY_REG_SSR_100BASE_FD: u16 = 0b110 << 2; | ||
| 43 | } | ||
| 44 | use self::phy_consts::*; | ||
| 45 | |||
| 46 | /// SMSC LAN8742A Ethernet PHY | ||
| 47 | pub struct LAN8742A; | ||
| 48 | |||
| 49 | unsafe impl PHY for LAN8742A { | ||
| 50 | /// Reset PHY and wait for it to come out of reset. | ||
| 51 | fn phy_reset<S: StationManagement>(sm: &mut S) { | ||
| 52 | sm.smi_write(PHY_REG_BCR, PHY_REG_BCR_RESET); | ||
| 53 | while sm.smi_read(PHY_REG_BCR) & PHY_REG_BCR_RESET == PHY_REG_BCR_RESET {} | ||
| 54 | } | ||
| 55 | |||
| 56 | /// PHY initialisation. | ||
| 57 | fn phy_init<S: StationManagement>(sm: &mut S) { | ||
| 58 | // Clear WU CSR | ||
| 59 | Self::smi_write_ext(sm, PHY_REG_WUCSR, 0); | ||
| 60 | |||
| 61 | // Enable auto-negotiation | ||
| 62 | sm.smi_write( | ||
| 63 | PHY_REG_BCR, | ||
| 64 | PHY_REG_BCR_AN | PHY_REG_BCR_ANRST | PHY_REG_BCR_100M, | ||
| 65 | ); | ||
| 66 | } | ||
| 67 | |||
| 68 | fn poll_link<S: StationManagement>(sm: &mut S) -> bool { | ||
| 69 | let bsr = sm.smi_read(PHY_REG_BSR); | ||
| 70 | let ssr = sm.smi_read(PHY_REG_SSR); | ||
| 71 | |||
| 72 | // No link without autonegotiate | ||
| 73 | if bsr & PHY_REG_BSR_ANDONE == 0 { | ||
| 74 | return false; | ||
| 75 | } | ||
| 76 | // No link if link is down | ||
| 77 | if bsr & PHY_REG_BSR_UP == 0 { | ||
| 78 | return false; | ||
| 79 | } | ||
| 80 | // No link if autonegotiate incomplete | ||
| 81 | if ssr & PHY_REG_SSR_ANDONE == 0 { | ||
| 82 | return false; | ||
| 83 | } | ||
| 84 | // No link if other side isn't 100Mbps full duplex | ||
| 85 | if ssr & PHY_REG_SSR_SPEED != PHY_REG_SSR_100BASE_FD { | ||
| 86 | return false; | ||
| 87 | } | ||
| 88 | |||
| 89 | // Got link | ||
| 90 | true | ||
| 91 | } | ||
| 92 | } | ||
| 93 | |||
| 94 | /// Public functions for the LAN8742A | ||
| 95 | impl LAN8742A { | ||
| 96 | // Writes a value to an extended PHY register in MMD address space | ||
| 97 | fn smi_write_ext<S: StationManagement>(sm: &mut S, reg_addr: u16, reg_data: u16) { | ||
| 98 | sm.smi_write(PHY_REG_CTL, 0x0003); // set address | ||
| 99 | sm.smi_write(PHY_REG_ADDAR, reg_addr); | ||
| 100 | sm.smi_write(PHY_REG_CTL, 0x4003); // set data | ||
| 101 | sm.smi_write(PHY_REG_ADDAR, reg_data); | ||
| 102 | } | ||
| 103 | } | ||
diff --git a/embassy-stm32/src/eth/mod.rs b/embassy-stm32/src/eth/mod.rs new file mode 100644 index 000000000..e41ebf4d0 --- /dev/null +++ b/embassy-stm32/src/eth/mod.rs | |||
| @@ -0,0 +1,34 @@ | |||
| 1 | #![macro_use] | ||
| 2 | |||
| 3 | #[cfg_attr(eth_v1, path = "v1.rs")] | ||
| 4 | #[cfg_attr(eth_v2, path = "v2/mod.rs")] | ||
| 5 | mod _version; | ||
| 6 | pub mod lan8742a; | ||
| 7 | |||
| 8 | pub use _version::*; | ||
| 9 | |||
| 10 | /// Station Management Interface (SMI) on an ethernet PHY | ||
| 11 | /// | ||
| 12 | /// # Safety | ||
| 13 | /// | ||
| 14 | /// The methods cannot move out of self | ||
| 15 | pub unsafe trait StationManagement { | ||
| 16 | /// Read a register over SMI. | ||
| 17 | fn smi_read(&mut self, reg: u8) -> u16; | ||
| 18 | /// Write a register over SMI. | ||
| 19 | fn smi_write(&mut self, reg: u8, val: u16); | ||
| 20 | } | ||
| 21 | |||
| 22 | /// Traits for an Ethernet PHY | ||
| 23 | /// | ||
| 24 | /// # Safety | ||
| 25 | /// | ||
| 26 | /// The methods cannot move S | ||
| 27 | pub unsafe trait PHY { | ||
| 28 | /// Reset PHY and wait for it to come out of reset. | ||
| 29 | fn phy_reset<S: StationManagement>(sm: &mut S); | ||
| 30 | /// PHY initialisation. | ||
| 31 | fn phy_init<S: StationManagement>(sm: &mut S); | ||
| 32 | /// Poll link to see if it is up and FD with 100Mbps | ||
| 33 | fn poll_link<S: StationManagement>(sm: &mut S) -> bool; | ||
| 34 | } | ||
diff --git a/embassy-stm32/src/eth/v1.rs b/embassy-stm32/src/eth/v1.rs new file mode 100644 index 000000000..8b1378917 --- /dev/null +++ b/embassy-stm32/src/eth/v1.rs | |||
| @@ -0,0 +1 @@ | |||
diff --git a/embassy-stm32/src/eth/v2/descriptors.rs b/embassy-stm32/src/eth/v2/descriptors.rs new file mode 100644 index 000000000..23b11857c --- /dev/null +++ b/embassy-stm32/src/eth/v2/descriptors.rs | |||
| @@ -0,0 +1,395 @@ | |||
| 1 | use core::sync::atomic::{fence, Ordering}; | ||
| 2 | |||
| 3 | use embassy_net::{Packet, PacketBox, PacketBoxExt, PacketBuf}; | ||
| 4 | use vcell::VolatileCell; | ||
| 5 | |||
| 6 | use crate::pac::ETH; | ||
| 7 | |||
| 8 | #[non_exhaustive] | ||
| 9 | #[derive(Debug, Copy, Clone)] | ||
| 10 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] | ||
| 11 | pub enum Error { | ||
| 12 | NoBufferAvailable, | ||
| 13 | // TODO: Break down this error into several others | ||
| 14 | TransmissionError, | ||
| 15 | } | ||
| 16 | |||
| 17 | /// Transmit and Receive Descriptor fields | ||
| 18 | #[allow(dead_code)] | ||
| 19 | mod emac_consts { | ||
| 20 | pub const EMAC_DES3_OWN: u32 = 0x8000_0000; | ||
| 21 | pub const EMAC_DES3_CTXT: u32 = 0x4000_0000; | ||
| 22 | pub const EMAC_DES3_FD: u32 = 0x2000_0000; | ||
| 23 | pub const EMAC_DES3_LD: u32 = 0x1000_0000; | ||
| 24 | pub const EMAC_DES3_ES: u32 = 0x0000_8000; | ||
| 25 | pub const EMAC_DES0_BUF1AP: u32 = 0xFFFF_FFFF; | ||
| 26 | |||
| 27 | pub const EMAC_TDES2_IOC: u32 = 0x8000_0000; | ||
| 28 | pub const EMAC_TDES2_B1L: u32 = 0x0000_3FFF; | ||
| 29 | |||
| 30 | pub const EMAC_RDES3_IOC: u32 = 0x4000_0000; | ||
| 31 | pub const EMAC_RDES3_PL: u32 = 0x0000_7FFF; | ||
| 32 | pub const EMAC_RDES3_BUF1V: u32 = 0x0100_0000; | ||
| 33 | pub const EMAC_RDES3_PKTLEN: u32 = 0x0000_7FFF; | ||
| 34 | } | ||
| 35 | use emac_consts::*; | ||
| 36 | |||
| 37 | /// Transmit Descriptor representation | ||
| 38 | /// | ||
| 39 | /// * tdes0: transmit buffer address | ||
| 40 | /// * tdes1: | ||
| 41 | /// * tdes2: buffer lengths | ||
| 42 | /// * tdes3: control and payload/frame length | ||
| 43 | #[repr(C)] | ||
| 44 | struct TDes { | ||
| 45 | tdes0: VolatileCell<u32>, | ||
| 46 | tdes1: VolatileCell<u32>, | ||
| 47 | tdes2: VolatileCell<u32>, | ||
| 48 | tdes3: VolatileCell<u32>, | ||
| 49 | } | ||
| 50 | |||
| 51 | impl TDes { | ||
| 52 | pub const fn new() -> Self { | ||
| 53 | Self { | ||
| 54 | tdes0: VolatileCell::new(0), | ||
| 55 | tdes1: VolatileCell::new(0), | ||
| 56 | tdes2: VolatileCell::new(0), | ||
| 57 | tdes3: VolatileCell::new(0), | ||
| 58 | } | ||
| 59 | } | ||
| 60 | |||
| 61 | /// Return true if this TDes is not currently owned by the DMA | ||
| 62 | pub fn available(&self) -> bool { | ||
| 63 | self.tdes3.get() & EMAC_DES3_OWN == 0 | ||
| 64 | } | ||
| 65 | } | ||
| 66 | |||
| 67 | pub(crate) struct TDesRing<const N: usize> { | ||
| 68 | td: [TDes; N], | ||
| 69 | buffers: [Option<PacketBuf>; N], | ||
| 70 | tdidx: usize, | ||
| 71 | } | ||
| 72 | |||
| 73 | impl<const N: usize> TDesRing<N> { | ||
| 74 | pub const fn new() -> Self { | ||
| 75 | const TDES: TDes = TDes::new(); | ||
| 76 | const BUFFERS: Option<PacketBuf> = None; | ||
| 77 | |||
| 78 | Self { | ||
| 79 | td: [TDES; N], | ||
| 80 | buffers: [BUFFERS; N], | ||
| 81 | tdidx: 0, | ||
| 82 | } | ||
| 83 | } | ||
| 84 | |||
| 85 | /// Initialise this TDesRing. Assume TDesRing is corrupt | ||
| 86 | /// | ||
| 87 | /// The current memory address of the buffers inside this TDesRing | ||
| 88 | /// will be stored in the descriptors, so ensure the TDesRing is | ||
| 89 | /// not moved after initialisation. | ||
| 90 | pub(crate) fn init(&mut self) { | ||
| 91 | assert!(N > 0); | ||
| 92 | |||
| 93 | for td in self.td.iter_mut() { | ||
| 94 | *td = TDes::new(); | ||
| 95 | } | ||
| 96 | self.tdidx = 0; | ||
| 97 | |||
| 98 | // Initialize the pointers in the DMA engine. (There will be a memory barrier later | ||
| 99 | // before the DMA engine is enabled.) | ||
| 100 | // NOTE (unsafe) Used for atomic writes | ||
| 101 | unsafe { | ||
| 102 | let dma = ETH.ethernet_dma(); | ||
| 103 | |||
| 104 | dma.dmactx_dlar() | ||
| 105 | .write(|w| w.0 = &self.td as *const _ as u32); | ||
| 106 | dma.dmactx_rlr().write(|w| w.set_tdrl((N as u16) - 1)); | ||
| 107 | dma.dmactx_dtpr() | ||
| 108 | .write(|w| w.0 = &self.td[0] as *const _ as u32); | ||
| 109 | } | ||
| 110 | } | ||
| 111 | |||
| 112 | /// Return true if a TDes is available for use | ||
| 113 | pub(crate) fn available(&self) -> bool { | ||
| 114 | self.td[self.tdidx].available() | ||
| 115 | } | ||
| 116 | |||
| 117 | pub(crate) fn transmit(&mut self, pkt: PacketBuf) -> Result<(), Error> { | ||
| 118 | if !self.available() { | ||
| 119 | return Err(Error::NoBufferAvailable); | ||
| 120 | } | ||
| 121 | let x = self.tdidx; | ||
| 122 | let td = &mut self.td[x]; | ||
| 123 | |||
| 124 | let pkt_len = pkt.len(); | ||
| 125 | assert!(pkt_len as u32 <= EMAC_TDES2_B1L); | ||
| 126 | let address = pkt.as_ptr() as u32; | ||
| 127 | |||
| 128 | // Read format | ||
| 129 | td.tdes0.set(address); | ||
| 130 | td.tdes2 | ||
| 131 | .set(pkt_len as u32 & EMAC_TDES2_B1L | EMAC_TDES2_IOC); | ||
| 132 | |||
| 133 | // FD: Contains first buffer of packet | ||
| 134 | // LD: Contains last buffer of packet | ||
| 135 | // Give the DMA engine ownership | ||
| 136 | td.tdes3.set(EMAC_DES3_FD | EMAC_DES3_LD | EMAC_DES3_OWN); | ||
| 137 | |||
| 138 | self.buffers[x].replace(pkt); | ||
| 139 | |||
| 140 | // Ensure changes to the descriptor are committed before DMA engine sees tail pointer store. | ||
| 141 | // This will generate an DMB instruction. | ||
| 142 | // "Preceding reads and writes cannot be moved past subsequent writes." | ||
| 143 | fence(Ordering::Release); | ||
| 144 | |||
| 145 | // Move the tail pointer (TPR) to the next descriptor | ||
| 146 | let x = (x + 1) % N; | ||
| 147 | // NOTE(unsafe) Atomic write | ||
| 148 | unsafe { | ||
| 149 | ETH.ethernet_dma() | ||
| 150 | .dmactx_dtpr() | ||
| 151 | .write(|w| w.0 = &self.td[x] as *const _ as u32); | ||
| 152 | } | ||
| 153 | self.tdidx = x; | ||
| 154 | Ok(()) | ||
| 155 | } | ||
| 156 | |||
| 157 | pub(crate) fn on_interrupt(&mut self) -> Result<(), Error> { | ||
| 158 | let previous = (self.tdidx + N - 1) % N; | ||
| 159 | let td = &self.td[previous]; | ||
| 160 | |||
| 161 | // DMB to ensure that we are reading an updated value, probably not needed at the hardware | ||
| 162 | // level, but this is also a hint to the compiler that we're syncing on the buffer. | ||
| 163 | fence(Ordering::SeqCst); | ||
| 164 | |||
| 165 | let tdes3 = td.tdes3.get(); | ||
| 166 | |||
| 167 | if tdes3 & EMAC_DES3_OWN != 0 { | ||
| 168 | // Transmission isn't done yet, probably a receive interrupt that fired this | ||
| 169 | return Ok(()); | ||
| 170 | } | ||
| 171 | assert!(tdes3 & EMAC_DES3_CTXT == 0); | ||
| 172 | |||
| 173 | // Release the buffer | ||
| 174 | self.buffers[previous].take(); | ||
| 175 | |||
| 176 | if tdes3 & EMAC_DES3_ES != 0 { | ||
| 177 | Err(Error::TransmissionError) | ||
| 178 | } else { | ||
| 179 | Ok(()) | ||
| 180 | } | ||
| 181 | } | ||
| 182 | } | ||
| 183 | |||
| 184 | /// Receive Descriptor representation | ||
| 185 | /// | ||
| 186 | /// * rdes0: recieve buffer address | ||
| 187 | /// * rdes1: | ||
| 188 | /// * rdes2: | ||
| 189 | /// * rdes3: OWN and Status | ||
| 190 | #[repr(C)] | ||
| 191 | struct RDes { | ||
| 192 | rdes0: VolatileCell<u32>, | ||
| 193 | rdes1: VolatileCell<u32>, | ||
| 194 | rdes2: VolatileCell<u32>, | ||
| 195 | rdes3: VolatileCell<u32>, | ||
| 196 | } | ||
| 197 | |||
| 198 | impl RDes { | ||
| 199 | pub const fn new() -> Self { | ||
| 200 | Self { | ||
| 201 | rdes0: VolatileCell::new(0), | ||
| 202 | rdes1: VolatileCell::new(0), | ||
| 203 | rdes2: VolatileCell::new(0), | ||
| 204 | rdes3: VolatileCell::new(0), | ||
| 205 | } | ||
| 206 | } | ||
| 207 | |||
| 208 | /// Return true if this RDes is acceptable to us | ||
| 209 | #[inline(always)] | ||
| 210 | pub fn valid(&self) -> bool { | ||
| 211 | // Write-back descriptor is valid if: | ||
| 212 | // | ||
| 213 | // Contains first buffer of packet AND contains last buf of | ||
| 214 | // packet AND no errors AND not a context descriptor | ||
| 215 | self.rdes3.get() & (EMAC_DES3_FD | EMAC_DES3_LD | EMAC_DES3_ES | EMAC_DES3_CTXT) | ||
| 216 | == (EMAC_DES3_FD | EMAC_DES3_LD) | ||
| 217 | } | ||
| 218 | |||
| 219 | /// Return true if this RDes is not currently owned by the DMA | ||
| 220 | #[inline(always)] | ||
| 221 | pub fn available(&self) -> bool { | ||
| 222 | self.rdes3.get() & EMAC_DES3_OWN == 0 // Owned by us | ||
| 223 | } | ||
| 224 | |||
| 225 | #[inline(always)] | ||
| 226 | pub fn set_ready(&mut self, buf_addr: u32) { | ||
| 227 | self.rdes0.set(buf_addr); | ||
| 228 | self.rdes3 | ||
| 229 | .set(EMAC_RDES3_BUF1V | EMAC_RDES3_IOC | EMAC_DES3_OWN); | ||
| 230 | } | ||
| 231 | } | ||
| 232 | |||
| 233 | /// Rx ring of descriptors and packets | ||
| 234 | /// | ||
| 235 | /// This ring has three major locations that work in lock-step. The DMA will never write to the tail | ||
| 236 | /// index, so the `read_index` must never pass the tail index. The `next_tail_index` is always 1 | ||
| 237 | /// slot ahead of the real tail index, and it must never pass the `read_index` or it could overwrite | ||
| 238 | /// a packet still to be passed to the application. | ||
| 239 | /// | ||
| 240 | /// nt can't pass r (no alloc) | ||
| 241 | /// +---+---+---+---+ Read ok +---+---+---+---+ No Read +---+---+---+---+ | ||
| 242 | /// | | | | | ------------> | | | | | ------------> | | | | | | ||
| 243 | /// +---+---+---+---+ Allocation ok +---+---+---+---+ +---+---+---+---+ | ||
| 244 | /// ^ ^t ^t ^ ^t ^ | ||
| 245 | /// |r |r |r | ||
| 246 | /// |nt |nt |nt | ||
| 247 | /// | ||
| 248 | /// | ||
| 249 | /// +---+---+---+---+ Read ok +---+---+---+---+ Can't read +---+---+---+---+ | ||
| 250 | /// | | | | | ------------> | | | | | ------------> | | | | | | ||
| 251 | /// +---+---+---+---+ Allocation fail +---+---+---+---+ Allocation ok +---+---+---+---+ | ||
| 252 | /// ^ ^t ^ ^t ^ ^ ^ ^t | ||
| 253 | /// |r | |r | | |r | ||
| 254 | /// |nt |nt |nt | ||
| 255 | /// | ||
| 256 | pub(crate) struct RDesRing<const N: usize> { | ||
| 257 | rd: [RDes; N], | ||
| 258 | buffers: [Option<PacketBox>; N], | ||
| 259 | read_idx: usize, | ||
| 260 | next_tail_idx: usize, | ||
| 261 | } | ||
| 262 | |||
| 263 | impl<const N: usize> RDesRing<N> { | ||
| 264 | pub const fn new() -> Self { | ||
| 265 | const RDES: RDes = RDes::new(); | ||
| 266 | const BUFFERS: Option<PacketBox> = None; | ||
| 267 | |||
| 268 | Self { | ||
| 269 | rd: [RDES; N], | ||
| 270 | buffers: [BUFFERS; N], | ||
| 271 | read_idx: 0, | ||
| 272 | next_tail_idx: 0, | ||
| 273 | } | ||
| 274 | } | ||
| 275 | |||
| 276 | pub(crate) fn init(&mut self) { | ||
| 277 | assert!(N > 1); | ||
| 278 | |||
| 279 | for desc in self.rd.iter_mut() { | ||
| 280 | *desc = RDes::new(); | ||
| 281 | } | ||
| 282 | |||
| 283 | let mut last_index = 0; | ||
| 284 | for (index, buf) in self.buffers.iter_mut().enumerate() { | ||
| 285 | let pkt = match PacketBox::new(Packet::new()) { | ||
| 286 | Some(p) => p, | ||
| 287 | None => { | ||
| 288 | if index == 0 { | ||
| 289 | panic!("Could not allocate at least one buffer for Ethernet receiving"); | ||
| 290 | } else { | ||
| 291 | break; | ||
| 292 | } | ||
| 293 | } | ||
| 294 | }; | ||
| 295 | let addr = pkt.as_ptr() as u32; | ||
| 296 | *buf = Some(pkt); | ||
| 297 | self.rd[index].set_ready(addr); | ||
| 298 | last_index = index; | ||
| 299 | } | ||
| 300 | self.next_tail_idx = (last_index + 1) % N; | ||
| 301 | |||
| 302 | unsafe { | ||
| 303 | let dma = ETH.ethernet_dma(); | ||
| 304 | |||
| 305 | dma.dmacrx_dlar().write(|w| w.0 = self.rd.as_ptr() as u32); | ||
| 306 | dma.dmacrx_rlr().write(|w| w.set_rdrl((N as u16) - 1)); | ||
| 307 | |||
| 308 | // We manage to allocate all buffers, set the index to the last one, that means | ||
| 309 | // that the DMA won't consider the last one as ready, because it (unfortunately) | ||
| 310 | // stops at the tail ptr and wraps at the end of the ring, which means that we | ||
| 311 | // can't tell it to stop after the last buffer. | ||
| 312 | let tail_ptr = &self.rd[last_index] as *const _ as u32; | ||
| 313 | fence(Ordering::Release); | ||
| 314 | |||
| 315 | dma.dmacrx_dtpr().write(|w| w.0 = tail_ptr); | ||
| 316 | } | ||
| 317 | } | ||
| 318 | |||
| 319 | pub(crate) fn on_interrupt(&mut self) { | ||
| 320 | // XXX: Do we need to do anything here ? Maybe we should try to advance the tail ptr, but it | ||
| 321 | // would soon hit the read ptr anyway, and we will wake smoltcp's stack on the interrupt | ||
| 322 | // which should try to pop a packet... | ||
| 323 | } | ||
| 324 | |||
| 325 | pub(crate) fn pop_packet(&mut self) -> Option<PacketBuf> { | ||
| 326 | // Not sure if the contents of the write buffer on the M7 can affects reads, so we are using | ||
| 327 | // a DMB here just in case, it also serves as a hint to the compiler that we're syncing the | ||
| 328 | // buffer (I think .-.) | ||
| 329 | fence(Ordering::SeqCst); | ||
| 330 | |||
| 331 | let read_available = self.rd[self.read_idx].available(); | ||
| 332 | let tail_index = (self.next_tail_idx + N - 1) % N; | ||
| 333 | |||
| 334 | let pkt = if read_available && self.read_idx != tail_index { | ||
| 335 | let pkt = self.buffers[self.read_idx].take(); | ||
| 336 | let len = (self.rd[self.read_idx].rdes3.get() & EMAC_RDES3_PKTLEN) as usize; | ||
| 337 | |||
| 338 | assert!(pkt.is_some()); | ||
| 339 | let valid = self.rd[self.read_idx].valid(); | ||
| 340 | |||
| 341 | self.read_idx = (self.read_idx + 1) % N; | ||
| 342 | if valid { | ||
| 343 | pkt.map(|p| p.slice(0..len)) | ||
| 344 | } else { | ||
| 345 | None | ||
| 346 | } | ||
| 347 | } else { | ||
| 348 | None | ||
| 349 | }; | ||
| 350 | |||
| 351 | // Try to advance the tail_idx | ||
| 352 | if self.next_tail_idx != self.read_idx { | ||
| 353 | match PacketBox::new(Packet::new()) { | ||
| 354 | Some(b) => { | ||
| 355 | let addr = b.as_ptr() as u32; | ||
| 356 | self.buffers[self.next_tail_idx].replace(b); | ||
| 357 | self.rd[self.next_tail_idx].set_ready(addr); | ||
| 358 | |||
| 359 | // "Preceding reads and writes cannot be moved past subsequent writes." | ||
| 360 | fence(Ordering::Release); | ||
| 361 | |||
| 362 | // NOTE(unsafe) atomic write | ||
| 363 | unsafe { | ||
| 364 | ETH.ethernet_dma() | ||
| 365 | .dmacrx_dtpr() | ||
| 366 | .write(|w| w.0 = &self.rd[self.next_tail_idx] as *const _ as u32); | ||
| 367 | } | ||
| 368 | |||
| 369 | self.next_tail_idx = (self.next_tail_idx + 1) % N; | ||
| 370 | } | ||
| 371 | None => {} | ||
| 372 | } | ||
| 373 | } | ||
| 374 | pkt | ||
| 375 | } | ||
| 376 | } | ||
| 377 | |||
| 378 | pub struct DescriptorRing<const T: usize, const R: usize> { | ||
| 379 | pub(crate) tx: TDesRing<T>, | ||
| 380 | pub(crate) rx: RDesRing<R>, | ||
| 381 | } | ||
| 382 | |||
| 383 | impl<const T: usize, const R: usize> DescriptorRing<T, R> { | ||
| 384 | pub const fn new() -> Self { | ||
| 385 | Self { | ||
| 386 | tx: TDesRing::new(), | ||
| 387 | rx: RDesRing::new(), | ||
| 388 | } | ||
| 389 | } | ||
| 390 | |||
| 391 | pub fn init(&mut self) { | ||
| 392 | self.tx.init(); | ||
| 393 | self.rx.init(); | ||
| 394 | } | ||
| 395 | } | ||
diff --git a/embassy-stm32/src/eth/v2/mod.rs b/embassy-stm32/src/eth/v2/mod.rs new file mode 100644 index 000000000..a8a361dfe --- /dev/null +++ b/embassy-stm32/src/eth/v2/mod.rs | |||
| @@ -0,0 +1,477 @@ | |||
| 1 | use core::marker::PhantomData; | ||
| 2 | use core::pin::Pin; | ||
| 3 | use core::sync::atomic::{fence, Ordering}; | ||
| 4 | use core::task::Waker; | ||
| 5 | |||
| 6 | use embassy::util::{AtomicWaker, Unborrow}; | ||
| 7 | use embassy_extras::peripheral::{PeripheralMutex, PeripheralState}; | ||
| 8 | use embassy_extras::unborrow; | ||
| 9 | use embassy_net::{Device, DeviceCapabilities, LinkState, PacketBuf, MTU}; | ||
| 10 | |||
| 11 | use crate::gpio::sealed::Pin as __GpioPin; | ||
| 12 | use crate::gpio::AnyPin; | ||
| 13 | use crate::gpio::Pin as GpioPin; | ||
| 14 | use crate::pac::gpio::vals::Ospeedr; | ||
| 15 | use crate::pac::{ETH, RCC, SYSCFG}; | ||
| 16 | use crate::peripherals; | ||
| 17 | |||
| 18 | mod descriptors; | ||
| 19 | use super::{StationManagement, PHY}; | ||
| 20 | use descriptors::DescriptorRing; | ||
| 21 | |||
| 22 | pub struct Ethernet<'d, P: PHY, const TX: usize, const RX: usize> { | ||
| 23 | state: PeripheralMutex<Inner<'d, TX, RX>>, | ||
| 24 | pins: [AnyPin; 9], | ||
| 25 | _phy: P, | ||
| 26 | clock_range: u8, | ||
| 27 | phy_addr: u8, | ||
| 28 | mac_addr: [u8; 6], | ||
| 29 | } | ||
| 30 | |||
| 31 | impl<'d, P: PHY, const TX: usize, const RX: usize> Ethernet<'d, P, TX, RX> { | ||
| 32 | pub fn new( | ||
| 33 | peri: impl Unborrow<Target = peripherals::ETH> + 'd, | ||
| 34 | interrupt: impl Unborrow<Target = crate::interrupt::ETH> + 'd, | ||
| 35 | ref_clk: impl Unborrow<Target = impl RefClkPin> + 'd, | ||
| 36 | mdio: impl Unborrow<Target = impl MDIOPin> + 'd, | ||
| 37 | mdc: impl Unborrow<Target = impl MDCPin> + 'd, | ||
| 38 | crs: impl Unborrow<Target = impl CRSPin> + 'd, | ||
| 39 | rx_d0: impl Unborrow<Target = impl RXD0Pin> + 'd, | ||
| 40 | rx_d1: impl Unborrow<Target = impl RXD1Pin> + 'd, | ||
| 41 | tx_d0: impl Unborrow<Target = impl TXD0Pin> + 'd, | ||
| 42 | tx_d1: impl Unborrow<Target = impl TXD1Pin> + 'd, | ||
| 43 | tx_en: impl Unborrow<Target = impl TXEnPin> + 'd, | ||
| 44 | phy: P, | ||
| 45 | mac_addr: [u8; 6], | ||
| 46 | phy_addr: u8, | ||
| 47 | ) -> Self { | ||
| 48 | unborrow!(interrupt, ref_clk, mdio, mdc, crs, rx_d0, rx_d1, tx_d0, tx_d1, tx_en); | ||
| 49 | |||
| 50 | // Enable the necessary Clocks | ||
| 51 | // NOTE(unsafe) We have exclusive access to the registers | ||
| 52 | critical_section::with(|_| unsafe { | ||
| 53 | RCC.apb4enr().modify(|w| w.set_syscfgen(true)); | ||
| 54 | RCC.ahb1enr().modify(|w| { | ||
| 55 | w.set_eth1macen(true); | ||
| 56 | w.set_eth1txen(true); | ||
| 57 | w.set_eth1rxen(true); | ||
| 58 | }); | ||
| 59 | |||
| 60 | // RMII | ||
| 61 | SYSCFG.pmcr().modify(|w| w.set_epis(0b100)); | ||
| 62 | }); | ||
| 63 | |||
| 64 | ref_clk.configure(); | ||
| 65 | mdio.configure(); | ||
| 66 | mdc.configure(); | ||
| 67 | crs.configure(); | ||
| 68 | rx_d0.configure(); | ||
| 69 | rx_d1.configure(); | ||
| 70 | tx_d0.configure(); | ||
| 71 | tx_d1.configure(); | ||
| 72 | tx_en.configure(); | ||
| 73 | |||
| 74 | let inner = Inner::new(peri); | ||
| 75 | let state = PeripheralMutex::new(inner, interrupt); | ||
| 76 | |||
| 77 | // NOTE(unsafe) We have exclusive access to the registers | ||
| 78 | unsafe { | ||
| 79 | let dma = ETH.ethernet_dma(); | ||
| 80 | let mac = ETH.ethernet_mac(); | ||
| 81 | let mtl = ETH.ethernet_mtl(); | ||
| 82 | |||
| 83 | // Reset and wait | ||
| 84 | dma.dmamr().modify(|w| w.set_swr(true)); | ||
| 85 | while dma.dmamr().read().swr() {} | ||
| 86 | |||
| 87 | mac.maccr().modify(|w| { | ||
| 88 | w.set_ipg(0b000); // 96 bit times | ||
| 89 | w.set_acs(true); | ||
| 90 | w.set_fes(true); | ||
| 91 | w.set_dm(true); | ||
| 92 | // TODO: Carrier sense ? ECRSFD | ||
| 93 | }); | ||
| 94 | |||
| 95 | mac.maca0lr().write(|w| { | ||
| 96 | w.set_addrlo( | ||
| 97 | u32::from(mac_addr[0]) | ||
| 98 | | (u32::from(mac_addr[1]) << 8) | ||
| 99 | | (u32::from(mac_addr[2]) << 16) | ||
| 100 | | (u32::from(mac_addr[3]) << 24), | ||
| 101 | ) | ||
| 102 | }); | ||
| 103 | mac.maca0hr() | ||
| 104 | .modify(|w| w.set_addrhi(u16::from(mac_addr[4]) | (u16::from(mac_addr[5]) << 8))); | ||
| 105 | |||
| 106 | mac.macpfr().modify(|w| w.set_saf(true)); | ||
| 107 | mac.macqtx_fcr().modify(|w| w.set_pt(0x100)); | ||
| 108 | |||
| 109 | mtl.mtlrx_qomr().modify(|w| w.set_rsf(true)); | ||
| 110 | mtl.mtltx_qomr().modify(|w| w.set_tsf(true)); | ||
| 111 | |||
| 112 | dma.dmactx_cr().modify(|w| w.set_txpbl(1)); // 32 ? | ||
| 113 | dma.dmacrx_cr().modify(|w| { | ||
| 114 | w.set_rxpbl(1); // 32 ? | ||
| 115 | w.set_rbsz(MTU as u16); | ||
| 116 | }); | ||
| 117 | } | ||
| 118 | |||
| 119 | // NOTE(unsafe) We got the peripheral singleton, which means that `rcc::init` was called | ||
| 120 | let hclk = unsafe { crate::rcc::get_freqs().ahb1 }; | ||
| 121 | let hclk_mhz = hclk.0 / 1_000_000; | ||
| 122 | |||
| 123 | // Set the MDC clock frequency in the range 1MHz - 2.5MHz | ||
| 124 | let clock_range = match hclk_mhz { | ||
| 125 | 0..=34 => 2, // Divide by 16 | ||
| 126 | 35..=59 => 3, // Divide by 26 | ||
| 127 | 60..=99 => 0, // Divide by 42 | ||
| 128 | 100..=149 => 1, // Divide by 62 | ||
| 129 | 150..=249 => 4, // Divide by 102 | ||
| 130 | 250..=310 => 5, // Divide by 124 | ||
| 131 | _ => { | ||
| 132 | panic!("HCLK results in MDC clock > 2.5MHz even for the highest CSR clock divider") | ||
| 133 | } | ||
| 134 | }; | ||
| 135 | |||
| 136 | let pins = [ | ||
| 137 | ref_clk.degrade(), | ||
| 138 | mdio.degrade(), | ||
| 139 | mdc.degrade(), | ||
| 140 | crs.degrade(), | ||
| 141 | rx_d0.degrade(), | ||
| 142 | rx_d1.degrade(), | ||
| 143 | tx_d0.degrade(), | ||
| 144 | tx_d1.degrade(), | ||
| 145 | tx_en.degrade(), | ||
| 146 | ]; | ||
| 147 | |||
| 148 | Self { | ||
| 149 | state, | ||
| 150 | pins, | ||
| 151 | _phy: phy, | ||
| 152 | clock_range, | ||
| 153 | phy_addr, | ||
| 154 | mac_addr, | ||
| 155 | } | ||
| 156 | } | ||
| 157 | |||
| 158 | pub fn init(self: Pin<&mut Self>) { | ||
| 159 | // NOTE(unsafe) We won't move this | ||
| 160 | let this = unsafe { self.get_unchecked_mut() }; | ||
| 161 | let mut mutex = unsafe { Pin::new_unchecked(&mut this.state) }; | ||
| 162 | mutex.as_mut().register_interrupt(); | ||
| 163 | |||
| 164 | mutex.with(|s, _| { | ||
| 165 | s.desc_ring.init(); | ||
| 166 | |||
| 167 | fence(Ordering::SeqCst); | ||
| 168 | |||
| 169 | unsafe { | ||
| 170 | let mac = ETH.ethernet_mac(); | ||
| 171 | let mtl = ETH.ethernet_mtl(); | ||
| 172 | let dma = ETH.ethernet_dma(); | ||
| 173 | |||
| 174 | mac.maccr().modify(|w| { | ||
| 175 | w.set_re(true); | ||
| 176 | w.set_te(true); | ||
| 177 | }); | ||
| 178 | mtl.mtltx_qomr().modify(|w| w.set_ftq(true)); | ||
| 179 | |||
| 180 | dma.dmactx_cr().modify(|w| w.set_st(true)); | ||
| 181 | dma.dmacrx_cr().modify(|w| w.set_sr(true)); | ||
| 182 | |||
| 183 | // Enable interrupts | ||
| 184 | dma.dmacier().modify(|w| { | ||
| 185 | w.set_nie(true); | ||
| 186 | w.set_rie(true); | ||
| 187 | w.set_tie(true); | ||
| 188 | }); | ||
| 189 | } | ||
| 190 | }); | ||
| 191 | P::phy_reset(this); | ||
| 192 | P::phy_init(this); | ||
| 193 | } | ||
| 194 | } | ||
| 195 | |||
| 196 | unsafe impl<'d, P: PHY, const TX: usize, const RX: usize> StationManagement | ||
| 197 | for Ethernet<'d, P, TX, RX> | ||
| 198 | { | ||
| 199 | fn smi_read(&mut self, reg: u8) -> u16 { | ||
| 200 | // NOTE(unsafe) These registers aren't used in the interrupt and we have `&mut self` | ||
| 201 | unsafe { | ||
| 202 | let mac = ETH.ethernet_mac(); | ||
| 203 | |||
| 204 | mac.macmdioar().modify(|w| { | ||
| 205 | w.set_pa(self.phy_addr); | ||
| 206 | w.set_rda(reg); | ||
| 207 | w.set_goc(0b11); // read | ||
| 208 | w.set_cr(self.clock_range); | ||
| 209 | w.set_mb(true); | ||
| 210 | }); | ||
| 211 | while mac.macmdioar().read().mb() {} | ||
| 212 | mac.macmdiodr().read().md() | ||
| 213 | } | ||
| 214 | } | ||
| 215 | |||
| 216 | fn smi_write(&mut self, reg: u8, val: u16) { | ||
| 217 | // NOTE(unsafe) These registers aren't used in the interrupt and we have `&mut self` | ||
| 218 | unsafe { | ||
| 219 | let mac = ETH.ethernet_mac(); | ||
| 220 | |||
| 221 | mac.macmdiodr().write(|w| w.set_md(val)); | ||
| 222 | mac.macmdioar().modify(|w| { | ||
| 223 | w.set_pa(self.phy_addr); | ||
| 224 | w.set_rda(reg); | ||
| 225 | w.set_goc(0b01); // write | ||
| 226 | w.set_cr(self.clock_range); | ||
| 227 | w.set_mb(true); | ||
| 228 | }); | ||
| 229 | while mac.macmdioar().read().mb() {} | ||
| 230 | } | ||
| 231 | } | ||
| 232 | } | ||
| 233 | |||
| 234 | impl<'d, P: PHY, const TX: usize, const RX: usize> Device for Pin<&mut Ethernet<'d, P, TX, RX>> { | ||
| 235 | fn is_transmit_ready(&mut self) -> bool { | ||
| 236 | // NOTE(unsafe) We won't move out of self | ||
| 237 | let this = unsafe { self.as_mut().get_unchecked_mut() }; | ||
| 238 | let mutex = unsafe { Pin::new_unchecked(&mut this.state) }; | ||
| 239 | |||
| 240 | mutex.with(|s, _| s.desc_ring.tx.available()) | ||
| 241 | } | ||
| 242 | |||
| 243 | fn transmit(&mut self, pkt: PacketBuf) { | ||
| 244 | // NOTE(unsafe) We won't move out of self | ||
| 245 | let this = unsafe { self.as_mut().get_unchecked_mut() }; | ||
| 246 | let mutex = unsafe { Pin::new_unchecked(&mut this.state) }; | ||
| 247 | |||
| 248 | mutex.with(|s, _| unwrap!(s.desc_ring.tx.transmit(pkt))); | ||
| 249 | } | ||
| 250 | |||
| 251 | fn receive(&mut self) -> Option<PacketBuf> { | ||
| 252 | // NOTE(unsafe) We won't move out of self | ||
| 253 | let this = unsafe { self.as_mut().get_unchecked_mut() }; | ||
| 254 | let mutex = unsafe { Pin::new_unchecked(&mut this.state) }; | ||
| 255 | |||
| 256 | mutex.with(|s, _| s.desc_ring.rx.pop_packet()) | ||
| 257 | } | ||
| 258 | |||
| 259 | fn register_waker(&mut self, waker: &Waker) { | ||
| 260 | WAKER.register(waker); | ||
| 261 | } | ||
| 262 | |||
| 263 | fn capabilities(&mut self) -> DeviceCapabilities { | ||
| 264 | let mut caps = DeviceCapabilities::default(); | ||
| 265 | caps.max_transmission_unit = MTU; | ||
| 266 | caps.max_burst_size = Some(TX.min(RX)); | ||
| 267 | caps | ||
| 268 | } | ||
| 269 | |||
| 270 | fn link_state(&mut self) -> LinkState { | ||
| 271 | // NOTE(unsafe) We won't move out of self | ||
| 272 | let this = unsafe { self.as_mut().get_unchecked_mut() }; | ||
| 273 | |||
| 274 | if P::poll_link(this) { | ||
| 275 | LinkState::Up | ||
| 276 | } else { | ||
| 277 | LinkState::Down | ||
| 278 | } | ||
| 279 | } | ||
| 280 | |||
| 281 | fn ethernet_address(&mut self) -> [u8; 6] { | ||
| 282 | // NOTE(unsafe) We won't move out of self | ||
| 283 | let this = unsafe { self.as_mut().get_unchecked_mut() }; | ||
| 284 | |||
| 285 | this.mac_addr | ||
| 286 | } | ||
| 287 | } | ||
| 288 | |||
| 289 | impl<'d, P: PHY, const TX: usize, const RX: usize> Drop for Ethernet<'d, P, TX, RX> { | ||
| 290 | fn drop(&mut self) { | ||
| 291 | // NOTE(unsafe) We have `&mut self` and the interrupt doesn't use this registers | ||
| 292 | unsafe { | ||
| 293 | let dma = ETH.ethernet_dma(); | ||
| 294 | let mac = ETH.ethernet_mac(); | ||
| 295 | let mtl = ETH.ethernet_mtl(); | ||
| 296 | |||
| 297 | // Disable the TX DMA and wait for any previous transmissions to be completed | ||
| 298 | dma.dmactx_cr().modify(|w| w.set_st(false)); | ||
| 299 | while { | ||
| 300 | let txqueue = mtl.mtltx_qdr().read(); | ||
| 301 | txqueue.trcsts() == 0b01 || txqueue.txqsts() | ||
| 302 | } {} | ||
| 303 | |||
| 304 | // Disable MAC transmitter and receiver | ||
| 305 | mac.maccr().modify(|w| { | ||
| 306 | w.set_re(false); | ||
| 307 | w.set_te(false); | ||
| 308 | }); | ||
| 309 | |||
| 310 | // Wait for previous receiver transfers to be completed and then disable the RX DMA | ||
| 311 | while { | ||
| 312 | let rxqueue = mtl.mtlrx_qdr().read(); | ||
| 313 | rxqueue.rxqsts() != 0b00 || rxqueue.prxq() != 0 | ||
| 314 | } {} | ||
| 315 | dma.dmacrx_cr().modify(|w| w.set_sr(false)); | ||
| 316 | } | ||
| 317 | |||
| 318 | for pin in self.pins.iter_mut() { | ||
| 319 | // NOTE(unsafe) Exclusive access to the regs | ||
| 320 | critical_section::with(|_| unsafe { | ||
| 321 | pin.set_as_analog(); | ||
| 322 | pin.block() | ||
| 323 | .ospeedr() | ||
| 324 | .modify(|w| w.set_ospeedr(pin.pin() as usize, Ospeedr::LOWSPEED)); | ||
| 325 | }) | ||
| 326 | } | ||
| 327 | } | ||
| 328 | } | ||
| 329 | |||
| 330 | //---------------------------------------------------------------------- | ||
| 331 | |||
| 332 | struct Inner<'d, const TX: usize, const RX: usize> { | ||
| 333 | _peri: PhantomData<&'d mut peripherals::ETH>, | ||
| 334 | desc_ring: DescriptorRing<TX, RX>, | ||
| 335 | } | ||
| 336 | |||
| 337 | impl<'d, const TX: usize, const RX: usize> Inner<'d, TX, RX> { | ||
| 338 | pub fn new(_peri: impl Unborrow<Target = peripherals::ETH> + 'd) -> Self { | ||
| 339 | Self { | ||
| 340 | _peri: PhantomData, | ||
| 341 | desc_ring: DescriptorRing::new(), | ||
| 342 | } | ||
| 343 | } | ||
| 344 | } | ||
| 345 | |||
| 346 | impl<'d, const TX: usize, const RX: usize> PeripheralState for Inner<'d, TX, RX> { | ||
| 347 | type Interrupt = crate::interrupt::ETH; | ||
| 348 | |||
| 349 | fn on_interrupt(&mut self) { | ||
| 350 | unwrap!(self.desc_ring.tx.on_interrupt()); | ||
| 351 | self.desc_ring.rx.on_interrupt(); | ||
| 352 | |||
| 353 | WAKER.wake(); | ||
| 354 | |||
| 355 | // TODO: Check and clear more flags | ||
| 356 | unsafe { | ||
| 357 | let dma = ETH.ethernet_dma(); | ||
| 358 | |||
| 359 | dma.dmacsr().modify(|w| { | ||
| 360 | w.set_ti(true); | ||
| 361 | w.set_ri(true); | ||
| 362 | w.set_nis(true); | ||
| 363 | }); | ||
| 364 | // Delay two peripheral's clock | ||
| 365 | dma.dmacsr().read(); | ||
| 366 | dma.dmacsr().read(); | ||
| 367 | } | ||
| 368 | } | ||
| 369 | } | ||
| 370 | |||
| 371 | mod sealed { | ||
| 372 | use super::*; | ||
| 373 | |||
| 374 | pub trait RefClkPin: GpioPin { | ||
| 375 | fn configure(&mut self); | ||
| 376 | } | ||
| 377 | |||
| 378 | pub trait MDIOPin: GpioPin { | ||
| 379 | fn configure(&mut self); | ||
| 380 | } | ||
| 381 | |||
| 382 | pub trait MDCPin: GpioPin { | ||
| 383 | fn configure(&mut self); | ||
| 384 | } | ||
| 385 | |||
| 386 | pub trait CRSPin: GpioPin { | ||
| 387 | fn configure(&mut self); | ||
| 388 | } | ||
| 389 | |||
| 390 | pub trait RXD0Pin: GpioPin { | ||
| 391 | fn configure(&mut self); | ||
| 392 | } | ||
| 393 | |||
| 394 | pub trait RXD1Pin: GpioPin { | ||
| 395 | fn configure(&mut self); | ||
| 396 | } | ||
| 397 | |||
| 398 | pub trait TXD0Pin: GpioPin { | ||
| 399 | fn configure(&mut self); | ||
| 400 | } | ||
| 401 | |||
| 402 | pub trait TXD1Pin: GpioPin { | ||
| 403 | fn configure(&mut self); | ||
| 404 | } | ||
| 405 | |||
| 406 | pub trait TXEnPin: GpioPin { | ||
| 407 | fn configure(&mut self); | ||
| 408 | } | ||
| 409 | } | ||
| 410 | |||
| 411 | pub trait RefClkPin: sealed::RefClkPin + 'static {} | ||
| 412 | |||
| 413 | pub trait MDIOPin: sealed::MDIOPin + 'static {} | ||
| 414 | |||
| 415 | pub trait MDCPin: sealed::MDCPin + 'static {} | ||
| 416 | |||
| 417 | pub trait CRSPin: sealed::CRSPin + 'static {} | ||
| 418 | |||
| 419 | pub trait RXD0Pin: sealed::RXD0Pin + 'static {} | ||
| 420 | |||
| 421 | pub trait RXD1Pin: sealed::RXD1Pin + 'static {} | ||
| 422 | |||
| 423 | pub trait TXD0Pin: sealed::TXD0Pin + 'static {} | ||
| 424 | |||
| 425 | pub trait TXD1Pin: sealed::TXD1Pin + 'static {} | ||
| 426 | |||
| 427 | pub trait TXEnPin: sealed::TXEnPin + 'static {} | ||
| 428 | |||
| 429 | static WAKER: AtomicWaker = AtomicWaker::new(); | ||
| 430 | |||
| 431 | macro_rules! impl_pin { | ||
| 432 | ($pin:ident, $signal:ident, $af:expr) => { | ||
| 433 | impl sealed::$signal for peripherals::$pin { | ||
| 434 | fn configure(&mut self) { | ||
| 435 | // NOTE(unsafe) Exclusive access to the registers | ||
| 436 | critical_section::with(|_| unsafe { | ||
| 437 | self.set_as_af($af); | ||
| 438 | self.block() | ||
| 439 | .ospeedr() | ||
| 440 | .modify(|w| w.set_ospeedr(self.pin() as usize, Ospeedr::VERYHIGHSPEED)); | ||
| 441 | }) | ||
| 442 | } | ||
| 443 | } | ||
| 444 | |||
| 445 | impl $signal for peripherals::$pin {} | ||
| 446 | }; | ||
| 447 | } | ||
| 448 | |||
| 449 | crate::pac::peripheral_pins!( | ||
| 450 | ($inst:ident, eth, ETH, $pin:ident, REF_CLK) => { | ||
| 451 | impl_pin!($pin, RefClkPin, 11); | ||
| 452 | }; | ||
| 453 | ($inst:ident, eth, ETH, $pin:ident, MDIO, $af:expr) => { | ||
| 454 | impl_pin!($pin, MDIOPin, $af); | ||
| 455 | }; | ||
| 456 | ($inst:ident, eth, ETH, $pin:ident, MDC, $af:expr) => { | ||
| 457 | impl_pin!($pin, MDCPin, $af); | ||
| 458 | }; | ||
| 459 | ($inst:ident, eth, ETH, $pin:ident, CRS_DV, $af:expr) => { | ||
| 460 | impl_pin!($pin, CRSPin, $af); | ||
| 461 | }; | ||
| 462 | ($inst:ident, eth, ETH, $pin:ident, RXD0, $af:expr) => { | ||
| 463 | impl_pin!($pin, RXD0Pin, $af); | ||
| 464 | }; | ||
| 465 | ($inst:ident, eth, ETH, $pin:ident, RXD1, $af:expr) => { | ||
| 466 | impl_pin!($pin, RXD1Pin, $af); | ||
| 467 | }; | ||
| 468 | ($inst:ident, eth, ETH, $pin:ident, TXD0, $af:expr) => { | ||
| 469 | impl_pin!($pin, TXD0Pin, $af); | ||
| 470 | }; | ||
| 471 | ($inst:ident, eth, ETH, $pin:ident, TXD1, $af:expr) => { | ||
| 472 | impl_pin!($pin, TXD1Pin, $af); | ||
| 473 | }; | ||
| 474 | ($inst:ident, eth, ETH, $pin:ident, TX_EN, $af:expr) => { | ||
| 475 | impl_pin!($pin, TXEnPin, $af); | ||
| 476 | }; | ||
| 477 | ); | ||
diff --git a/embassy-stm32/src/lib.rs b/embassy-stm32/src/lib.rs index 6a08fc580..39ccd54f0 100644 --- a/embassy-stm32/src/lib.rs +++ b/embassy-stm32/src/lib.rs | |||
| @@ -29,6 +29,8 @@ pub mod clock; | |||
| 29 | pub mod dac; | 29 | pub mod dac; |
| 30 | #[cfg(dma)] | 30 | #[cfg(dma)] |
| 31 | pub mod dma; | 31 | pub mod dma; |
| 32 | #[cfg(all(eth, feature = "net"))] | ||
| 33 | pub mod eth; | ||
| 32 | #[cfg(i2c)] | 34 | #[cfg(i2c)] |
| 33 | pub mod i2c; | 35 | pub mod i2c; |
| 34 | #[cfg(pwr)] | 36 | #[cfg(pwr)] |
diff --git a/examples/stm32h7/.cargo/config b/examples/stm32h7/.cargo/config new file mode 100644 index 000000000..6b6a9f506 --- /dev/null +++ b/examples/stm32h7/.cargo/config | |||
| @@ -0,0 +1,10 @@ | |||
| 1 | [target.thumbv7em-none-eabihf] | ||
| 2 | runner = 'probe-run --chip STM32H743ZITx' | ||
| 3 | rustflags = [ | ||
| 4 | # LLD (shipped with the Rust toolchain) is used as the default linker | ||
| 5 | "-C", "link-arg=-Tlink.x", | ||
| 6 | "-C", "link-arg=-Tdefmt.x", | ||
| 7 | ] | ||
| 8 | |||
| 9 | [build] | ||
| 10 | target = "thumbv7em-none-eabihf" # Cortex-M4F and Cortex-M7F (with FPU) | ||
diff --git a/examples/stm32h7/Cargo.toml b/examples/stm32h7/Cargo.toml index d7288e4c7..ebaa4e5db 100644 --- a/examples/stm32h7/Cargo.toml +++ b/examples/stm32h7/Cargo.toml | |||
| @@ -19,8 +19,11 @@ defmt-error = [] | |||
| 19 | [dependencies] | 19 | [dependencies] |
| 20 | embassy = { version = "0.1.0", path = "../../embassy", features = ["defmt", "defmt-trace"] } | 20 | embassy = { version = "0.1.0", path = "../../embassy", features = ["defmt", "defmt-trace"] } |
| 21 | embassy-traits = { version = "0.1.0", path = "../../embassy-traits", features = ["defmt"] } | 21 | embassy-traits = { version = "0.1.0", path = "../../embassy-traits", features = ["defmt"] } |
| 22 | embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["defmt", "defmt-trace", "stm32h743zi"] } | 22 | embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["defmt", "defmt-trace", "stm32h743zi", "net"] } |
| 23 | embassy-extras = {version = "0.1.0", path = "../../embassy-extras" } | 23 | embassy-extras = {version = "0.1.0", path = "../../embassy-extras" } |
| 24 | embassy-net = { path = "../../embassy-net", default-features = false, features = ["defmt-debug", "defmt", "tcp", "medium-ethernet", "pool-16"] } | ||
| 25 | stm32-metapac = { path = "../../stm32-metapac", features = ["stm32h743zi"] } | ||
| 26 | embassy-macros = { path = "../../embassy-macros" } | ||
| 24 | stm32h7 = { version = "0.13", features = ["stm32h743"]} | 27 | stm32h7 = { version = "0.13", features = ["stm32h743"]} |
| 25 | stm32h7xx-hal = { version = "0.9.0", features = ["stm32h743"] } | 28 | stm32h7xx-hal = { version = "0.9.0", features = ["stm32h743"] } |
| 26 | 29 | ||
| @@ -34,6 +37,18 @@ panic-probe = { version = "0.2.0", features= ["print-defmt"] } | |||
| 34 | futures = { version = "0.3.8", default-features = false, features = ["async-await"] } | 37 | futures = { version = "0.3.8", default-features = false, features = ["async-await"] } |
| 35 | rtt-target = { version = "0.3", features = ["cortex-m"] } | 38 | rtt-target = { version = "0.3", features = ["cortex-m"] } |
| 36 | heapless = { version = "0.7.1", default-features = false } | 39 | heapless = { version = "0.7.1", default-features = false } |
| 40 | rand_core = { version = "0.6.2" } | ||
| 41 | critical-section = "0.2.1" | ||
| 37 | 42 | ||
| 38 | micromath = "2.0.0" | 43 | micromath = "2.0.0" |
| 39 | 44 | ||
| 45 | [dependencies.smoltcp] | ||
| 46 | git = "https://github.com/smoltcp-rs/smoltcp" | ||
| 47 | rev = "e4241510337e095b9d21136c5f58b2eaa1b78479" | ||
| 48 | default-features = false | ||
| 49 | features = [ | ||
| 50 | "proto-ipv4", | ||
| 51 | "socket", | ||
| 52 | "async", | ||
| 53 | "defmt", | ||
| 54 | ] | ||
diff --git a/examples/stm32h7/build.rs b/examples/stm32h7/build.rs new file mode 100644 index 000000000..555cdf687 --- /dev/null +++ b/examples/stm32h7/build.rs | |||
| @@ -0,0 +1,21 @@ | |||
| 1 | use std::env; | ||
| 2 | use std::fs::File; | ||
| 3 | use std::io::Write; | ||
| 4 | use std::path::PathBuf; | ||
| 5 | |||
| 6 | fn main() { | ||
| 7 | // Put `memory.x` in our output directory and ensure it's | ||
| 8 | // on the linker search path. | ||
| 9 | let out = &PathBuf::from(env::var_os("OUT_DIR").unwrap()); | ||
| 10 | File::create(out.join("memory.x")) | ||
| 11 | .unwrap() | ||
| 12 | .write_all(include_bytes!("memory.x")) | ||
| 13 | .unwrap(); | ||
| 14 | println!("cargo:rustc-link-search={}", out.display()); | ||
| 15 | |||
| 16 | // By default, Cargo will re-run a build script whenever | ||
| 17 | // any file in the project changes. By specifying `memory.x` | ||
| 18 | // here, we ensure the build script is only re-run when | ||
| 19 | // `memory.x` is changed. | ||
| 20 | println!("cargo:rerun-if-changed=memory.x"); | ||
| 21 | } | ||
diff --git a/examples/stm32h7/memory.x b/examples/stm32h7/memory.x index 48f58e36b..ef9485d12 100644 --- a/examples/stm32h7/memory.x +++ b/examples/stm32h7/memory.x | |||
| @@ -1,5 +1,5 @@ | |||
| 1 | MEMORY | 1 | MEMORY |
| 2 | { | 2 | { |
| 3 | FLASH : ORIGIN = 0x08000000, LENGTH = 2048K | 3 | FLASH : ORIGIN = 0x08000000, LENGTH = 2048K |
| 4 | RAM : ORIGIN = 0x20000000, LENGTH = 128K | 4 | RAM : ORIGIN = 0x24000000, LENGTH = 384K |
| 5 | } | 5 | } |
diff --git a/examples/stm32h7/src/bin/eth.rs b/examples/stm32h7/src/bin/eth.rs new file mode 100644 index 000000000..57997da0a --- /dev/null +++ b/examples/stm32h7/src/bin/eth.rs | |||
| @@ -0,0 +1,167 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | #![feature(trait_alias)] | ||
| 4 | #![feature(min_type_alias_impl_trait)] | ||
| 5 | #![feature(impl_trait_in_bindings)] | ||
| 6 | #![feature(type_alias_impl_trait)] | ||
| 7 | |||
| 8 | use core::pin::Pin; | ||
| 9 | use core::sync::atomic::{AtomicUsize, Ordering}; | ||
| 10 | |||
| 11 | use cortex_m_rt::entry; | ||
| 12 | use defmt::{info, unwrap}; | ||
| 13 | use defmt_rtt as _; // global logger | ||
| 14 | use embassy::executor::{Executor, Spawner}; | ||
| 15 | use embassy::io::AsyncWriteExt; | ||
| 16 | use embassy::time::{Duration, Timer}; | ||
| 17 | use embassy::util::Forever; | ||
| 18 | use embassy_macros::interrupt_take; | ||
| 19 | use embassy_net::{Config as NetConfig, Ipv4Address, Ipv4Cidr, StaticConfigurator, TcpSocket}; | ||
| 20 | use embassy_stm32::clock::{Alarm, Clock}; | ||
| 21 | use embassy_stm32::eth::lan8742a::LAN8742A; | ||
| 22 | use embassy_stm32::eth::Ethernet; | ||
| 23 | use embassy_stm32::rcc::{Config as RccConfig, Rcc}; | ||
| 24 | use embassy_stm32::rng::Random; | ||
| 25 | use embassy_stm32::time::Hertz; | ||
| 26 | use embassy_stm32::{interrupt, peripherals, Config}; | ||
| 27 | use heapless::Vec; | ||
| 28 | use panic_probe as _; | ||
| 29 | use peripherals::{RNG, TIM2}; | ||
| 30 | |||
| 31 | defmt::timestamp! {"{=u64}", { | ||
| 32 | static COUNT: AtomicUsize = AtomicUsize::new(0); | ||
| 33 | // NOTE(no-CAS) `timestamps` runs with interrupts disabled | ||
| 34 | let n = COUNT.load(Ordering::Relaxed); | ||
| 35 | COUNT.store(n + 1, Ordering::Relaxed); | ||
| 36 | n as u64 | ||
| 37 | } | ||
| 38 | } | ||
| 39 | |||
| 40 | #[embassy::task] | ||
| 41 | async fn main_task( | ||
| 42 | device: &'static mut Pin<&'static mut Ethernet<'static, LAN8742A, 4, 4>>, | ||
| 43 | config: &'static mut StaticConfigurator, | ||
| 44 | spawner: Spawner, | ||
| 45 | ) { | ||
| 46 | // Init network stack | ||
| 47 | embassy_net::init(device, config); | ||
| 48 | |||
| 49 | // Launch network task | ||
| 50 | unwrap!(spawner.spawn(net_task())); | ||
| 51 | |||
| 52 | info!("Network task initialized"); | ||
| 53 | |||
| 54 | // Then we can use it! | ||
| 55 | let mut rx_buffer = [0; 1024]; | ||
| 56 | let mut tx_buffer = [0; 1024]; | ||
| 57 | let mut socket = TcpSocket::new(&mut rx_buffer, &mut tx_buffer); | ||
| 58 | |||
| 59 | socket.set_timeout(Some(embassy_net::SmolDuration::from_secs(10))); | ||
| 60 | |||
| 61 | let remote_endpoint = (Ipv4Address::new(192, 168, 0, 10), 8000); | ||
| 62 | let r = socket.connect(remote_endpoint).await; | ||
| 63 | if let Err(e) = r { | ||
| 64 | info!("connect error: {:?}", e); | ||
| 65 | return; | ||
| 66 | } | ||
| 67 | info!("connected!"); | ||
| 68 | loop { | ||
| 69 | let r = socket.write_all(b"Hello\n").await; | ||
| 70 | if let Err(e) = r { | ||
| 71 | info!("write error: {:?}", e); | ||
| 72 | return; | ||
| 73 | } | ||
| 74 | Timer::after(Duration::from_secs(1)).await; | ||
| 75 | } | ||
| 76 | } | ||
| 77 | |||
| 78 | #[embassy::task] | ||
| 79 | async fn net_task() { | ||
| 80 | embassy_net::run().await | ||
| 81 | } | ||
| 82 | |||
| 83 | #[no_mangle] | ||
| 84 | fn _embassy_rand(buf: &mut [u8]) { | ||
| 85 | use rand_core::RngCore; | ||
| 86 | |||
| 87 | critical_section::with(|_| unsafe { | ||
| 88 | unwrap!(RNG_INST.as_mut()).fill_bytes(buf); | ||
| 89 | }); | ||
| 90 | } | ||
| 91 | |||
| 92 | static mut RNG_INST: Option<Random<RNG>> = None; | ||
| 93 | |||
| 94 | static EXECUTOR: Forever<Executor> = Forever::new(); | ||
| 95 | static TIMER_RTC: Forever<Clock<TIM2>> = Forever::new(); | ||
| 96 | static ALARM: Forever<Alarm<TIM2>> = Forever::new(); | ||
| 97 | static ETH: Forever<Ethernet<'static, LAN8742A, 4, 4>> = Forever::new(); | ||
| 98 | static DEVICE: Forever<Pin<&'static mut Ethernet<'static, LAN8742A, 4, 4>>> = Forever::new(); | ||
| 99 | static CONFIG: Forever<StaticConfigurator> = Forever::new(); | ||
| 100 | |||
| 101 | #[entry] | ||
| 102 | fn main() -> ! { | ||
| 103 | use stm32_metapac::RCC; | ||
| 104 | |||
| 105 | info!("Hello World!"); | ||
| 106 | |||
| 107 | info!("Setup RCC..."); | ||
| 108 | let mut rcc_config = RccConfig::default(); | ||
| 109 | rcc_config.sys_ck = Some(Hertz(400_000_000)); | ||
| 110 | rcc_config.pll1.q_ck = Some(Hertz(100_000_000)); | ||
| 111 | let config = Config::default().rcc(rcc_config); | ||
| 112 | |||
| 113 | let mut p = embassy_stm32::init(config); | ||
| 114 | |||
| 115 | // Constrain and Freeze clock | ||
| 116 | |||
| 117 | let mut rcc = Rcc::new(&mut p.RCC, RccConfig::default()); | ||
| 118 | rcc.enable_debug_wfe(&mut p.DBGMCU, true); | ||
| 119 | |||
| 120 | unsafe { | ||
| 121 | RCC.ahb4enr().modify(|w| { | ||
| 122 | w.set_gpioaen(true); | ||
| 123 | w.set_gpioben(true); | ||
| 124 | w.set_gpiocen(true); | ||
| 125 | w.set_gpioden(true); | ||
| 126 | w.set_gpioien(true); | ||
| 127 | }); | ||
| 128 | } | ||
| 129 | |||
| 130 | let rtc_int = interrupt_take!(TIM2); | ||
| 131 | let rtc = TIMER_RTC.put(Clock::new(p.TIM2, rtc_int)); | ||
| 132 | rtc.start(); | ||
| 133 | let alarm = ALARM.put(rtc.alarm1()); | ||
| 134 | |||
| 135 | unsafe { embassy::time::set_clock(rtc) }; | ||
| 136 | |||
| 137 | let rng = Random::new(p.RNG); | ||
| 138 | unsafe { | ||
| 139 | RNG_INST.replace(rng); | ||
| 140 | } | ||
| 141 | |||
| 142 | let eth_int = interrupt_take!(ETH); | ||
| 143 | let mac_addr = [0x10; 6]; | ||
| 144 | let eth = ETH.put(Ethernet::new( | ||
| 145 | p.ETH, eth_int, p.PA1, p.PA2, p.PC1, p.PA7, p.PC4, p.PC5, p.PB12, p.PB13, p.PB11, LAN8742A, | ||
| 146 | mac_addr, 1, | ||
| 147 | )); | ||
| 148 | |||
| 149 | // NOTE(unsafe) This thing is a &'static | ||
| 150 | let net_device = DEVICE.put(unsafe { Pin::new_unchecked(eth) }); | ||
| 151 | net_device.as_mut().init(); | ||
| 152 | |||
| 153 | let config = StaticConfigurator::new(NetConfig { | ||
| 154 | address: Ipv4Cidr::new(Ipv4Address::new(192, 168, 0, 61), 24), | ||
| 155 | dns_servers: Vec::new(), | ||
| 156 | gateway: Some(Ipv4Address::new(192, 168, 0, 1)), | ||
| 157 | }); | ||
| 158 | |||
| 159 | let config = CONFIG.put(config); | ||
| 160 | |||
| 161 | let executor = EXECUTOR.put(Executor::new()); | ||
| 162 | executor.set_alarm(alarm); | ||
| 163 | |||
| 164 | executor.run(move |spawner| { | ||
| 165 | unwrap!(spawner.spawn(main_task(net_device, config, spawner))); | ||
| 166 | }) | ||
| 167 | } | ||
