Skip to content

Commit

Permalink
fixing 735 example
Browse files Browse the repository at this point in the history
  • Loading branch information
Harishguna Satgunarajah committed Oct 2, 2023
1 parent 8e4fb54 commit cca418f
Showing 1 changed file with 88 additions and 29 deletions.
117 changes: 88 additions & 29 deletions examples/ethernet-rtic-nucleo-h723zg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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> {
Expand All @@ -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);
Expand Down Expand Up @@ -153,44 +174,82 @@ 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
assert_eq!(ccdr.clocks.pclk2().raw(), 100_000_000); // PCLK 100MHz
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());
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit cca418f

Please sign in to comment.