From cca418f66fa45958c3083fbe501e93841ce175b9 Mon Sep 17 00:00:00 2001 From: Harishguna Satgunarajah Date: Mon, 2 Oct 2023 15:50:18 -0700 Subject: [PATCH] fixing 735 example --- examples/ethernet-rtic-nucleo-h723zg.rs | 117 ++++++++++++++++++------ 1 file changed, 88 insertions(+), 29 deletions(-) diff --git a/examples/ethernet-rtic-nucleo-h723zg.rs b/examples/ethernet-rtic-nucleo-h723zg.rs index fb13fb8d..77013ef7 100644 --- a/examples/ethernet-rtic-nucleo-h723zg.rs +++ b/examples/ethernet-rtic-nucleo-h723zg.rs @@ -25,8 +25,13 @@ use smoltcp::iface::{Config, Interface, SocketSet, SocketStorage}; use smoltcp::time::Instant; use smoltcp::wire::{HardwareAddress, IpAddress, IpCidr}; -use stm32h7xx_hal::{ethernet, rcc::CoreClocks, stm32}; - +use stm32h7xx_hal::{rcc::CoreClocks, stm32}; +use stm32h7xx_hal::{ + ethernet, + ethernet::{ + RxDescriptor, RxDescriptorRing, TxDescriptor, TxDescriptorRing, MTU, + }, +}; /// Configure SYSTICK for 1ms timebase fn systick_init(mut syst: stm32::SYST, clocks: CoreClocks) { let c_ck_mhz = clocks.c_ck().to_MHz(); @@ -45,9 +50,25 @@ static TIME: AtomicU32 = AtomicU32::new(0); /// Locally administered MAC address const MAC_ADDRESS: [u8; 6] = [0x02, 0x00, 0x11, 0x22, 0x33, 0x44]; +/// DesRing TD +const NUM_DESCRIPTORS: usize = 8; /// Ethernet descriptor rings are a global singleton -#[link_section = ".axisram.eth"] -static mut DES_RING: ethernet::DesRing<4, 4> = ethernet::DesRing::new(); +#[link_section = ".sram3.eth"] +/// Doc +static mut TX_DESCRIPTORS: [TxDescriptor; NUM_DESCRIPTORS] = + [TxDescriptor::new(); NUM_DESCRIPTORS]; +#[link_section = ".sram3.eth"] +/// Doc +static mut TX_BUFFERS: [[u8; MTU + 2]; NUM_DESCRIPTORS] = + [[0u8; MTU + 2]; NUM_DESCRIPTORS]; +#[link_section = ".sram3.eth"] +/// Doc +static mut RX_DESCRIPTORS: [RxDescriptor; NUM_DESCRIPTORS] = + [RxDescriptor::new(); NUM_DESCRIPTORS]; +#[link_section = ".sram3.eth"] +/// Doc +static mut RX_BUFFERS: [[u8; MTU + 2]; NUM_DESCRIPTORS] = + [[0u8; MTU + 2]; NUM_DESCRIPTORS]; /// Net storage with static initialisation - another global singleton pub struct NetStorageStatic<'a> { @@ -60,13 +81,13 @@ static mut STORE: NetStorageStatic = NetStorageStatic { pub struct Net<'a> { iface: Interface, - ethdev: ethernet::EthernetDMA<4, 4>, + ethdev: ethernet::EthernetDMA<'a, 'a>, sockets: SocketSet<'a>, } impl<'a> Net<'a> { pub fn new( store: &'a mut NetStorageStatic<'a>, - mut ethdev: ethernet::EthernetDMA<4, 4>, + mut ethdev: ethernet::EthernetDMA<'a, 'a>, ethernet_addr: HardwareAddress, ) -> Self { let config = Config::new(ethernet_addr); @@ -153,6 +174,17 @@ mod app { let rmii_txd0 = gpiob.pb12.into_alternate(); let rmii_txd1 = gpiob.pb13.into_alternate(); + let rmii_pins = ( + rmii_ref_clk, + rmii_mdio, + rmii_mdc, + rmii_crs_dv, + rmii_rxd0, + rmii_rxd1, + rmii_tx_en, + rmii_txd0, + rmii_txd1, + ); // Initialise ethernet... assert_eq!(ccdr.clocks.hclk().raw(), 200_000_000); // HCLK 200MHz assert_eq!(ccdr.clocks.pclk1().raw(), 100_000_000); // PCLK 100MHz @@ -160,37 +192,64 @@ mod app { assert_eq!(ccdr.clocks.pclk4().raw(), 100_000_000); // PCLK 100MHz let mac_addr = smoltcp::wire::EthernetAddress::from_bytes(&MAC_ADDRESS); - let (eth_dma, eth_mac) = unsafe { - ethernet::new( - ctx.device.ETHERNET_MAC, - ctx.device.ETHERNET_MTL, - ctx.device.ETHERNET_DMA, - ( - rmii_ref_clk, - rmii_mdio, - rmii_mdc, - rmii_crs_dv, - rmii_rxd0, - rmii_rxd1, - rmii_tx_en, - rmii_txd0, - rmii_txd1, - ), - &mut DES_RING, - mac_addr, - ccdr.peripheral.ETH1MAC, - &ccdr.clocks, + let (rx_ring, tx_ring) = { + // let tx_desc = unsafe { TX_DESCRIPTORS.write([TxDescriptor::new(); NUM_DESCRIPTORS]) }; + // let tx_buf = unsafe { TX_BUFFERS.write([[0u8; MTU + 2]; NUM_DESCRIPTORS]) }; + + // let rx_desc = unsafe { RX_DESCRIPTORS.write([RxDescriptor::new(); NUM_DESCRIPTORS]) }; + // let rx_buf = unsafe { RX_BUFFERS.write([[0u8; MTU + 2]; NUM_DESCRIPTORS]) }; + + ( + RxDescriptorRing::new(unsafe { &mut RX_DESCRIPTORS }, unsafe { + &mut RX_BUFFERS + }), + TxDescriptorRing::new(unsafe { &mut TX_DESCRIPTORS }, unsafe { + &mut TX_BUFFERS + }), ) }; + #[cfg(feature = "ptp")] + let ethernet::Parts { + dma: eth_dma, + mac: eth_mac, + ptp: _ptp, + } = ethernet::new( + ctx.device.ETHERNET_MAC, + ctx.device.ETHERNET_MTL, + ctx.device.ETHERNET_DMA, + rmii_pins, + rx_ring, + tx_ring, + mac_addr, + ccdr.peripheral.ETH1MAC, + &ccdr.clocks, + ); + + #[cfg(not(feature = "ptp"))] + let ethernet::Parts { + dma: eth_dma, + mac: eth_mac, + } = ethernet::new( + ctx.device.ETHERNET_MAC, + ctx.device.ETHERNET_MTL, + ctx.device.ETHERNET_DMA, + rmii_pins, + rx_ring, + tx_ring, + mac_addr, + ccdr.peripheral.ETH1MAC, + &ccdr.clocks, + ); + // let start_addend = ptp.addend(); + eth_dma.enable_interrupt(); + // Initialise ethernet PHY... let mut lan8742a = ethernet::phy::LAN8742A::new(eth_mac); lan8742a.phy_reset(); lan8742a.phy_init(); // The eth_dma should not be used until the PHY reports the link is up - unsafe { ethernet::enable_interrupt() }; - // unsafe: mutable reference to static storage, we only do this once let store = unsafe { &mut STORE }; let net = Net::new(store, eth_dma, mac_addr.into()); @@ -222,7 +281,7 @@ mod app { #[task(binds = ETH, local = [net])] fn ethernet_event(ctx: ethernet_event::Context) { - unsafe { ethernet::interrupt_handler() } + ethernet::eth_interrupt_handler(); let time = TIME.load(Ordering::Relaxed); ctx.local.net.poll(time as i64);