|
| 1 | +#![no_std] |
| 2 | +#![no_main] |
| 3 | + |
| 4 | +use defmt::*; |
| 5 | +use embassy_executor::Spawner; |
| 6 | +use embassy_net::tcp::TcpSocket; |
| 7 | +use embassy_net::{Ipv4Address, Stack, StackResources}; |
| 8 | +use embassy_net_wiznet::chip::W5500; |
| 9 | +use embassy_net_wiznet::{Device, Runner, State}; |
| 10 | +use embassy_stm32::exti::ExtiInput; |
| 11 | +use embassy_stm32::gpio::{Level, Output, Pull, Speed}; |
| 12 | +use embassy_stm32::mode::Async; |
| 13 | +use embassy_stm32::rng::Rng; |
| 14 | +use embassy_stm32::spi::Spi; |
| 15 | +use embassy_stm32::time::Hertz; |
| 16 | +use embassy_stm32::{bind_interrupts, peripherals, rng, spi, Config}; |
| 17 | +use embassy_time::{Delay, Timer}; |
| 18 | +use embedded_hal_bus::spi::ExclusiveDevice; |
| 19 | +use embedded_io_async::Write; |
| 20 | +use static_cell::StaticCell; |
| 21 | +use {defmt_rtt as _, panic_probe as _}; |
| 22 | + |
| 23 | +bind_interrupts!(struct Irqs { |
| 24 | + HASH_RNG => rng::InterruptHandler<peripherals::RNG>; |
| 25 | +}); |
| 26 | + |
| 27 | +type EthernetSPI = ExclusiveDevice<Spi<'static, Async>, Output<'static>, Delay>; |
| 28 | +#[embassy_executor::task] |
| 29 | +async fn ethernet_task(runner: Runner<'static, W5500, EthernetSPI, ExtiInput<'static>, Output<'static>>) -> ! { |
| 30 | + runner.run().await |
| 31 | +} |
| 32 | + |
| 33 | +#[embassy_executor::task] |
| 34 | +async fn net_task(stack: &'static Stack<Device<'static>>) -> ! { |
| 35 | + stack.run().await |
| 36 | +} |
| 37 | + |
| 38 | +#[embassy_executor::main] |
| 39 | +async fn main(spawner: Spawner) -> ! { |
| 40 | + let mut config = Config::default(); |
| 41 | + { |
| 42 | + use embassy_stm32::rcc::*; |
| 43 | + config.rcc.hse = Some(Hse { |
| 44 | + freq: Hertz(8_000_000), |
| 45 | + mode: HseMode::Bypass, |
| 46 | + }); |
| 47 | + config.rcc.pll_src = PllSource::HSE; |
| 48 | + config.rcc.pll = Some(Pll { |
| 49 | + prediv: PllPreDiv::DIV4, |
| 50 | + mul: PllMul::MUL180, |
| 51 | + divp: Some(PllPDiv::DIV2), // 8mhz / 4 * 180 / 2 = 180Mhz. |
| 52 | + divq: None, |
| 53 | + divr: None, |
| 54 | + }); |
| 55 | + config.rcc.ahb_pre = AHBPrescaler::DIV1; |
| 56 | + config.rcc.apb1_pre = APBPrescaler::DIV4; |
| 57 | + config.rcc.apb2_pre = APBPrescaler::DIV2; |
| 58 | + config.rcc.sys = Sysclk::PLL1_P; |
| 59 | + } |
| 60 | + let p = embassy_stm32::init(config); |
| 61 | + |
| 62 | + info!("Hello World!"); |
| 63 | + |
| 64 | + // Generate random seed |
| 65 | + let mut rng = Rng::new(p.RNG, Irqs); |
| 66 | + let mut seed = [0; 8]; |
| 67 | + unwrap!(rng.async_fill_bytes(&mut seed).await); |
| 68 | + let seed = u64::from_le_bytes(seed); |
| 69 | + |
| 70 | + let mut spi_cfg = spi::Config::default(); |
| 71 | + spi_cfg.frequency = Hertz(50_000_000); // up to 50m works |
| 72 | + let (miso, mosi, clk) = (p.PA6, p.PA7, p.PA5); |
| 73 | + let spi = Spi::new(p.SPI1, clk, mosi, miso, p.DMA2_CH3, p.DMA2_CH0, spi_cfg); |
| 74 | + let cs = Output::new(p.PA4, Level::High, Speed::VeryHigh); |
| 75 | + let spi = unwrap!(ExclusiveDevice::new(spi, cs, Delay)); |
| 76 | + |
| 77 | + let w5500_int = ExtiInput::new(p.PB0, p.EXTI0, Pull::Up); |
| 78 | + let w5500_reset = Output::new(p.PB1, Level::High, Speed::VeryHigh); |
| 79 | + |
| 80 | + let mac_addr = [0x02, 234, 3, 4, 82, 231]; |
| 81 | + static STATE: StaticCell<State<2, 2>> = StaticCell::new(); |
| 82 | + let state = STATE.init(State::<2, 2>::new()); |
| 83 | + let (device, runner) = embassy_net_wiznet::new(mac_addr, state, spi, w5500_int, w5500_reset).await; |
| 84 | + unwrap!(spawner.spawn(ethernet_task(runner))); |
| 85 | + |
| 86 | + let config = embassy_net::Config::dhcpv4(Default::default()); |
| 87 | + //let config = embassy_net::Config::ipv4_static(embassy_net::StaticConfigV4 { |
| 88 | + // address: Ipv4Cidr::new(Ipv4Address::new(10, 42, 0, 61), 24), |
| 89 | + // dns_servers: Vec::new(), |
| 90 | + // gateway: Some(Ipv4Address::new(10, 42, 0, 1)), |
| 91 | + //}); |
| 92 | + |
| 93 | + static STACK: StaticCell<Stack<Device>> = StaticCell::new(); |
| 94 | + static RESOURCES: StaticCell<StackResources<2>> = StaticCell::new(); |
| 95 | + let stack = &*STACK.init(Stack::new( |
| 96 | + device, |
| 97 | + config, |
| 98 | + RESOURCES.init(StackResources::<2>::new()), |
| 99 | + seed, |
| 100 | + )); |
| 101 | + |
| 102 | + // Launch network task |
| 103 | + unwrap!(spawner.spawn(net_task(stack))); |
| 104 | + |
| 105 | + // Ensure DHCP configuration is up before trying connect |
| 106 | + stack.wait_config_up().await; |
| 107 | + |
| 108 | + info!("Network task initialized"); |
| 109 | + |
| 110 | + // Then we can use it! |
| 111 | + let mut rx_buffer = [0; 1024]; |
| 112 | + let mut tx_buffer = [0; 1024]; |
| 113 | + |
| 114 | + loop { |
| 115 | + let mut socket = TcpSocket::new(stack, &mut rx_buffer, &mut tx_buffer); |
| 116 | + |
| 117 | + socket.set_timeout(Some(embassy_time::Duration::from_secs(10))); |
| 118 | + |
| 119 | + let remote_endpoint = (Ipv4Address::new(10, 42, 0, 1), 8000); |
| 120 | + info!("connecting..."); |
| 121 | + let r = socket.connect(remote_endpoint).await; |
| 122 | + if let Err(e) = r { |
| 123 | + info!("connect error: {:?}", e); |
| 124 | + Timer::after_secs(1).await; |
| 125 | + continue; |
| 126 | + } |
| 127 | + info!("connected!"); |
| 128 | + let buf = [0; 1024]; |
| 129 | + loop { |
| 130 | + let r = socket.write_all(&buf).await; |
| 131 | + if let Err(e) = r { |
| 132 | + info!("write error: {:?}", e); |
| 133 | + break; |
| 134 | + } |
| 135 | + Timer::after_secs(1).await; |
| 136 | + } |
| 137 | + } |
| 138 | +} |
0 commit comments