|
| 1 | +//! Demo for STM32H735G-DK eval board using the Real Time for the Masses |
| 2 | +//! (RTIC) framework. |
| 3 | +//! |
| 4 | +//! This demo responds to pings on 192.168.1.99 (IP address hardcoded below) |
| 5 | +//! |
| 6 | +//! We use the SysTick timer to create a 1ms timebase for use with smoltcp. |
| 7 | +//! |
| 8 | +//! The ethernet ring buffers are placed in AXI SRAM, where they can be |
| 9 | +//! accessed by both the core and the Ethernet DMA. |
| 10 | +#![deny(warnings)] |
| 11 | +#![no_main] |
| 12 | +#![no_std] |
| 13 | + |
| 14 | +use cortex_m; |
| 15 | +use rtic::app; |
| 16 | + |
| 17 | +#[macro_use] |
| 18 | +#[allow(unused)] |
| 19 | +mod utilities; |
| 20 | +use log::info; |
| 21 | + |
| 22 | +use smoltcp::iface::{ |
| 23 | + EthernetInterface, EthernetInterfaceBuilder, Neighbor, NeighborCache, |
| 24 | + Route, Routes, |
| 25 | +}; |
| 26 | +use smoltcp::socket::{SocketSet, SocketSetItem}; |
| 27 | +use smoltcp::time::Instant; |
| 28 | +use smoltcp::wire::{EthernetAddress, IpAddress, IpCidr, Ipv6Cidr}; |
| 29 | + |
| 30 | +use gpio::Speed::*; |
| 31 | +use stm32h7xx_hal::gpio; |
| 32 | +use stm32h7xx_hal::hal::digital::v2::OutputPin; |
| 33 | +use stm32h7xx_hal::rcc::CoreClocks; |
| 34 | +use stm32h7xx_hal::{ethernet, ethernet::PHY}; |
| 35 | +use stm32h7xx_hal::{prelude::*, stm32}; |
| 36 | + |
| 37 | +use core::sync::atomic::{AtomicU32, Ordering}; |
| 38 | + |
| 39 | +/// Configure SYSTICK for 1ms timebase |
| 40 | +fn systick_init(mut syst: stm32::SYST, clocks: CoreClocks) { |
| 41 | + let c_ck_mhz = clocks.c_ck().0 / 1_000_000; |
| 42 | + |
| 43 | + let syst_calib = 0x3E8; |
| 44 | + |
| 45 | + syst.set_clock_source(cortex_m::peripheral::syst::SystClkSource::Core); |
| 46 | + syst.set_reload((syst_calib * c_ck_mhz) - 1); |
| 47 | + syst.enable_interrupt(); |
| 48 | + syst.enable_counter(); |
| 49 | +} |
| 50 | + |
| 51 | +/// TIME is an atomic u32 that counts milliseconds. |
| 52 | +static TIME: AtomicU32 = AtomicU32::new(0); |
| 53 | + |
| 54 | +/// Locally administered MAC address |
| 55 | +const MAC_ADDRESS: [u8; 6] = [0x02, 0x00, 0x11, 0x22, 0x33, 0x44]; |
| 56 | + |
| 57 | +/// Ethernet descriptor rings are a global singleton |
| 58 | +#[link_section = ".axisram.eth"] |
| 59 | +static mut DES_RING: ethernet::DesRing<4, 4> = ethernet::DesRing::new(); |
| 60 | + |
| 61 | +/// Net storage with static initialisation - another global singleton |
| 62 | +pub struct NetStorageStatic<'a> { |
| 63 | + ip_addrs: [IpCidr; 1], |
| 64 | + socket_set_entries: [Option<SocketSetItem<'a>>; 8], |
| 65 | + neighbor_cache_storage: [Option<(IpAddress, Neighbor)>; 8], |
| 66 | + routes_storage: [Option<(IpCidr, Route)>; 1], |
| 67 | +} |
| 68 | +static mut STORE: NetStorageStatic = NetStorageStatic { |
| 69 | + // Garbage |
| 70 | + ip_addrs: [IpCidr::Ipv6(Ipv6Cidr::SOLICITED_NODE_PREFIX)], |
| 71 | + socket_set_entries: [None, None, None, None, None, None, None, None], |
| 72 | + neighbor_cache_storage: [None; 8], |
| 73 | + routes_storage: [None; 1], |
| 74 | +}; |
| 75 | + |
| 76 | +pub struct Net<'a> { |
| 77 | + iface: EthernetInterface<'a, ethernet::EthernetDMA<'a, 4, 4>>, |
| 78 | + sockets: SocketSet<'a>, |
| 79 | +} |
| 80 | +impl<'a> Net<'a> { |
| 81 | + pub fn new( |
| 82 | + store: &'static mut NetStorageStatic<'a>, |
| 83 | + ethdev: ethernet::EthernetDMA<'a, 4, 4>, |
| 84 | + ethernet_addr: EthernetAddress, |
| 85 | + ) -> Self { |
| 86 | + // Set IP address |
| 87 | + store.ip_addrs = |
| 88 | + [IpCidr::new(IpAddress::v4(192, 168, 1, 99).into(), 0)]; |
| 89 | + |
| 90 | + let neighbor_cache = |
| 91 | + NeighborCache::new(&mut store.neighbor_cache_storage[..]); |
| 92 | + let routes = Routes::new(&mut store.routes_storage[..]); |
| 93 | + |
| 94 | + let iface = EthernetInterfaceBuilder::new(ethdev) |
| 95 | + .ethernet_addr(ethernet_addr) |
| 96 | + .neighbor_cache(neighbor_cache) |
| 97 | + .ip_addrs(&mut store.ip_addrs[..]) |
| 98 | + .routes(routes) |
| 99 | + .finalize(); |
| 100 | + let sockets = SocketSet::new(&mut store.socket_set_entries[..]); |
| 101 | + |
| 102 | + return Net { iface, sockets }; |
| 103 | + } |
| 104 | + |
| 105 | + /// Polls on the ethernet interface. You should refer to the smoltcp |
| 106 | + /// documentation for poll() to understand how to call poll efficiently |
| 107 | + pub fn poll(&mut self, now: i64) { |
| 108 | + let timestamp = Instant::from_millis(now); |
| 109 | + |
| 110 | + self.iface |
| 111 | + .poll(&mut self.sockets, timestamp) |
| 112 | + .map(|_| ()) |
| 113 | + .unwrap_or_else(|e| info!("Poll: {:?}", e)); |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +#[app(device = stm32h7xx_hal::stm32, peripherals = true)] |
| 118 | +const APP: () = { |
| 119 | + struct Resources { |
| 120 | + net: Net<'static>, |
| 121 | + lan8742a: ethernet::phy::LAN8742A<ethernet::EthernetMAC>, |
| 122 | + link_led: gpio::gpioc::PC3<gpio::Output<gpio::PushPull>>, |
| 123 | + } |
| 124 | + |
| 125 | + #[init] |
| 126 | + fn init(mut ctx: init::Context) -> init::LateResources { |
| 127 | + utilities::logger::init(); |
| 128 | + // Initialise power... |
| 129 | + let pwr = ctx.device.PWR.constrain(); |
| 130 | + let pwrcfg = pwr.smps().freeze(); |
| 131 | + |
| 132 | + // Initialise clocks... |
| 133 | + let rcc = ctx.device.RCC.constrain(); |
| 134 | + let ccdr = rcc |
| 135 | + .sys_ck(200.mhz()) |
| 136 | + .hclk(200.mhz()) |
| 137 | + .freeze(pwrcfg, &ctx.device.SYSCFG); |
| 138 | + |
| 139 | + // Initialise system... |
| 140 | + ctx.core.SCB.invalidate_icache(); |
| 141 | + ctx.core.SCB.enable_icache(); |
| 142 | + // TODO: ETH DMA coherence issues |
| 143 | + // ctx.core.SCB.enable_dcache(&mut ctx.core.CPUID); |
| 144 | + ctx.core.DWT.enable_cycle_counter(); |
| 145 | + |
| 146 | + // Initialise IO... |
| 147 | + let gpioa = ctx.device.GPIOA.split(ccdr.peripheral.GPIOA); |
| 148 | + let gpioc = ctx.device.GPIOC.split(ccdr.peripheral.GPIOC); |
| 149 | + let gpiob = ctx.device.GPIOB.split(ccdr.peripheral.GPIOB); |
| 150 | + let mut link_led = gpioc.pc3.into_push_pull_output(); // USR LED1 |
| 151 | + link_led.set_high().ok(); |
| 152 | + |
| 153 | + let _rmii_ref_clk = gpioa.pa1.into_alternate_af11().set_speed(VeryHigh); |
| 154 | + let _rmii_mdio = gpioa.pa2.into_alternate_af11().set_speed(VeryHigh); |
| 155 | + let _rmii_mdc = gpioc.pc1.into_alternate_af11().set_speed(VeryHigh); |
| 156 | + let _rmii_crs_dv = gpioa.pa7.into_alternate_af11().set_speed(VeryHigh); |
| 157 | + let _rmii_rxd0 = gpioc.pc4.into_alternate_af11().set_speed(VeryHigh); |
| 158 | + let _rmii_rxd1 = gpioc.pc5.into_alternate_af11().set_speed(VeryHigh); |
| 159 | + let _rmii_tx_en = gpiob.pb11.into_alternate_af11().set_speed(VeryHigh); |
| 160 | + let _rmii_txd0 = gpiob.pb12.into_alternate_af11().set_speed(VeryHigh); |
| 161 | + let _rmii_txd1 = gpiob.pb13.into_alternate_af11().set_speed(VeryHigh); |
| 162 | + |
| 163 | + // Initialise ethernet... |
| 164 | + assert_eq!(ccdr.clocks.hclk().0, 200_000_000); // HCLK 200MHz |
| 165 | + assert_eq!(ccdr.clocks.pclk1().0, 100_000_000); // PCLK 100MHz |
| 166 | + assert_eq!(ccdr.clocks.pclk2().0, 100_000_000); // PCLK 100MHz |
| 167 | + assert_eq!(ccdr.clocks.pclk4().0, 100_000_000); // PCLK 100MHz |
| 168 | + |
| 169 | + let mac_addr = smoltcp::wire::EthernetAddress::from_bytes(&MAC_ADDRESS); |
| 170 | + let (eth_dma, eth_mac) = unsafe { |
| 171 | + ethernet::new_unchecked( |
| 172 | + ctx.device.ETHERNET_MAC, |
| 173 | + ctx.device.ETHERNET_MTL, |
| 174 | + ctx.device.ETHERNET_DMA, |
| 175 | + &mut DES_RING, |
| 176 | + mac_addr.clone(), |
| 177 | + ccdr.peripheral.ETH1MAC, |
| 178 | + &ccdr.clocks, |
| 179 | + ) |
| 180 | + }; |
| 181 | + |
| 182 | + // Initialise ethernet PHY... |
| 183 | + let mut lan8742a = ethernet::phy::LAN8742A::new(eth_mac); |
| 184 | + lan8742a.phy_reset(); |
| 185 | + lan8742a.phy_init(); |
| 186 | + // The eth_dma should not be used until the PHY reports the link is up |
| 187 | + |
| 188 | + unsafe { |
| 189 | + ethernet::enable_interrupt(); |
| 190 | + } |
| 191 | + |
| 192 | + // unsafe: mutable reference to static storage, we only do this once |
| 193 | + let store = unsafe { &mut STORE }; |
| 194 | + let net = Net::new(store, eth_dma, mac_addr); |
| 195 | + |
| 196 | + // 1ms tick |
| 197 | + systick_init(ctx.core.SYST, ccdr.clocks); |
| 198 | + |
| 199 | + init::LateResources { |
| 200 | + net, |
| 201 | + lan8742a, |
| 202 | + link_led, |
| 203 | + } |
| 204 | + } |
| 205 | + |
| 206 | + #[idle(resources = [lan8742a, link_led])] |
| 207 | + fn idle(ctx: idle::Context) -> ! { |
| 208 | + loop { |
| 209 | + // Ethernet |
| 210 | + match ctx.resources.lan8742a.poll_link() { |
| 211 | + true => ctx.resources.link_led.set_low(), |
| 212 | + _ => ctx.resources.link_led.set_high(), |
| 213 | + } |
| 214 | + .ok(); |
| 215 | + } |
| 216 | + } |
| 217 | + |
| 218 | + #[task(binds = ETH, resources = [net])] |
| 219 | + fn ethernet_event(ctx: ethernet_event::Context) { |
| 220 | + unsafe { ethernet::interrupt_handler() } |
| 221 | + |
| 222 | + let time = TIME.load(Ordering::Relaxed); |
| 223 | + ctx.resources.net.poll(time as i64); |
| 224 | + } |
| 225 | + |
| 226 | + #[task(binds = SysTick, priority=15)] |
| 227 | + fn systick_tick(_: systick_tick::Context) { |
| 228 | + TIME.fetch_add(1, Ordering::Relaxed); |
| 229 | + } |
| 230 | +}; |
0 commit comments