humpback-dds/examples/tcp_client.rs

293 lines
8.7 KiB
Rust
Raw Normal View History

2020-09-04 13:31:11 +08:00
#![no_std]
2020-09-10 14:35:11 +08:00
#![no_main]
#[macro_use]
extern crate log;
#[macro_use]
extern crate lazy_static;
use smoltcp as net;
use stm32h7xx_hal::ethernet;
use stm32h7xx_hal::{gpio::Speed, prelude::*, spi, pac};
use embedded_hal::{
blocking::spi::Transfer,
digital::v2::OutputPin,
};
2020-09-02 11:00:04 +08:00
2020-09-04 13:31:11 +08:00
use core::sync::atomic::{AtomicU32, Ordering};
2020-09-10 14:35:11 +08:00
use core::fmt::Write;
use core::str;
2020-09-02 11:00:04 +08:00
2020-09-10 14:35:11 +08:00
// use heapless::{consts, String};
2020-09-02 11:00:04 +08:00
use cortex_m;
2020-09-10 14:35:11 +08:00
use cortex_m::iprintln;
2020-09-02 11:00:04 +08:00
use cortex_m_rt::{
entry,
exception,
};
2020-09-10 14:35:11 +08:00
// use cortex_m_semihosting::hprintln;
2020-09-02 11:00:04 +08:00
2020-09-10 14:35:11 +08:00
// use panic_halt as _;
2020-09-10 16:22:12 +08:00
use panic_itm as _;
2020-09-02 11:00:04 +08:00
2020-09-10 14:35:11 +08:00
use rtic::cyccnt::{Instant, U32Ext};
2020-09-02 11:00:04 +08:00
2020-09-10 14:35:11 +08:00
use log::info;
use log::debug;
use log::trace;
2020-09-11 17:36:41 +08:00
use log::warn;
2020-09-02 11:00:04 +08:00
2020-09-10 14:35:11 +08:00
use nb::block;
2020-09-02 11:00:04 +08:00
2020-09-10 14:35:11 +08:00
use minimq::{
embedded_nal::{IpAddr, Ipv4Addr, TcpStack, SocketAddr, Mode},
MqttClient, QoS,
};
2020-09-04 13:31:11 +08:00
2020-09-10 14:35:11 +08:00
use firmware::nal_tcp_client::{NetworkStack, NetStorage, NetworkInterface};
use firmware::{Urukul};
use firmware::cpld::{CPLD};
2020-09-02 11:00:04 +08:00
2020-09-04 13:31:11 +08:00
#[link_section = ".sram3.eth"]
static mut DES_RING: ethernet::DesRing = ethernet::DesRing::new();
2020-09-10 14:35:11 +08:00
// Logging setup
#[path = "util/logger.rs"]
mod logger;
#[path = "util/clock.rs"]
mod clock;
// End of logging setup
2020-09-02 11:00:04 +08:00
2020-09-10 14:35:11 +08:00
// static TIME: AtomicU32 = AtomicU32::new(0);
2020-09-04 13:31:11 +08:00
2020-09-02 11:00:04 +08:00
#[entry]
fn main() -> ! {
2020-09-10 14:35:11 +08:00
// logger::semihosting_init();
let clock = clock::Clock::new();
2020-09-04 13:31:11 +08:00
2020-09-02 11:00:04 +08:00
let mut cp = cortex_m::Peripherals::take().unwrap();
let dp = pac::Peripherals::take().unwrap();
2020-09-10 14:35:11 +08:00
cp.DWT.enable_cycle_counter();
2020-09-02 11:00:04 +08:00
2020-09-10 14:35:11 +08:00
// Enable SRAM3 for the descriptor ring.
2020-09-02 11:00:04 +08:00
dp.RCC.ahb2enr.modify(|_, w| w.sram3en().set_bit());
2020-09-10 14:35:11 +08:00
// Reset RCC clock
dp.RCC.rsr.write(|w| w.rmvf().set_bit());
let pwr = dp.PWR.constrain();
let vos = pwr.freeze();
2020-09-02 11:00:04 +08:00
let rcc = dp.RCC.constrain();
let ccdr = rcc
2020-09-10 14:35:11 +08:00
.use_hse(8.mhz())
.sysclk(400.mhz())
.hclk(200.mhz())
2020-09-10 17:15:29 +08:00
// .per_ck(100.mhz())
2020-09-10 14:35:11 +08:00
.pll1_q_ck(48.mhz()) // for SPI
2020-09-10 16:22:12 +08:00
.pll1_r_ck(400.mhz()) // for TRACECK
2020-09-10 17:15:29 +08:00
// .pll2_p_ck(100.mhz())
// .pll2_q_ck(100.mhz())
2020-09-10 14:35:11 +08:00
.freeze(vos, &dp.SYSCFG);
2020-09-10 16:22:12 +08:00
unsafe {
logger::enable_itm(&dp.DBGMCU, &mut cp.DCB, &mut cp.ITM);
}
2020-09-10 14:35:11 +08:00
let mut delay = cp.SYST.delay(ccdr.clocks);
2020-09-02 11:00:04 +08:00
let gpioa = dp.GPIOA.split(ccdr.peripheral.GPIOA);
let gpiob = dp.GPIOB.split(ccdr.peripheral.GPIOB);
let gpioc = dp.GPIOC.split(ccdr.peripheral.GPIOC);
2020-09-10 14:35:11 +08:00
let gpiod = dp.GPIOD.split(ccdr.peripheral.GPIOD);
let gpioe = dp.GPIOE.split(ccdr.peripheral.GPIOE);
let gpiof = dp.GPIOF.split(ccdr.peripheral.GPIOF);
2020-09-02 11:00:04 +08:00
let gpiog = dp.GPIOG.split(ccdr.peripheral.GPIOG);
2020-09-10 14:35:11 +08:00
let mut yellow_led = gpioe.pe1.into_push_pull_output();
yellow_led.set_low().unwrap();
let mut red_led = gpiob.pb14.into_push_pull_output();
red_led.set_high().unwrap();
let mut green_led = gpiob.pb0.into_push_pull_output();
green_led.set_low().unwrap();
2020-09-11 17:36:41 +08:00
// gpiob.pb3.into_alternate_af0().set_speed(Speed::VeryHigh);
2020-09-10 16:22:12 +08:00
logger::init();
2020-09-10 14:35:11 +08:00
// Configure ethernet IO
{
let _rmii_refclk = gpioa.pa1.into_alternate_af11().set_speed(Speed::VeryHigh);
let _rmii_mdio = gpioa.pa2.into_alternate_af11().set_speed(Speed::VeryHigh);
let _rmii_mdc = gpioc.pc1.into_alternate_af11().set_speed(Speed::VeryHigh);
let _rmii_crs_dv = gpioa.pa7.into_alternate_af11().set_speed(Speed::VeryHigh);
let _rmii_rxd0 = gpioc.pc4.into_alternate_af11().set_speed(Speed::VeryHigh);
let _rmii_rxd1 = gpioc.pc5.into_alternate_af11().set_speed(Speed::VeryHigh);
let _rmii_tx_en = gpiog.pg11.into_alternate_af11().set_speed(Speed::VeryHigh);
let _rmii_txd0 = gpiog.pg13.into_alternate_af11().set_speed(Speed::VeryHigh);
let _rmii_txd1 = gpiob.pb13.into_alternate_af11().set_speed(Speed::VeryHigh);
}
// Configure ethernet
let mac_addr = net::wire::EthernetAddress([0xAC, 0x6F, 0x7A, 0xDE, 0xD6, 0xC8]);
let (eth_dma, mut eth_mac) = unsafe {
2020-09-10 14:35:11 +08:00
ethernet::new_unchecked(
dp.ETHERNET_MAC,
dp.ETHERNET_MTL,
dp.ETHERNET_DMA,
&mut DES_RING,
mac_addr.clone(),
)
};
unsafe { ethernet::enable_interrupt() }
2020-09-04 13:31:11 +08:00
2020-09-10 14:35:11 +08:00
let mut ip_addrs = [net::wire::IpCidr::new(net::wire::IpAddress::v4(192, 168, 1, 200), 24)];
let mut neighbor_cache_entries = [None; 8];
let mut neighbor_cache = net::iface::NeighborCache::new(&mut neighbor_cache_entries[..]);
2020-09-11 17:36:41 +08:00
neighbor_cache.fill(
net::wire::IpAddress::v4(192, 168, 1, 125),
net::wire::EthernetAddress([0x2C, 0xF0, 0x5D, 0x26, 0xB8, 0x2F]),
clock.elapsed(),
);
2020-09-10 14:35:11 +08:00
let mut net_interface = net::iface::EthernetInterfaceBuilder::new(eth_dma)
.ethernet_addr(mac_addr)
.neighbor_cache(neighbor_cache)
.ip_addrs(&mut ip_addrs[..])
.finalize();
2020-09-02 11:00:04 +08:00
2020-09-10 14:35:11 +08:00
/*
* Using SPI1, AF5
* SCLK -> PA5
* MOSI -> PB5
* MISO -> PA6
* CS -> 0: PB12, 1: PA15, 2: PC7
* I/O_Update -> PB15
*/
2020-09-02 11:00:04 +08:00
let sclk = gpioa.pa5.into_alternate_af5();
let mosi = gpiob.pb5.into_alternate_af5();
let miso = gpioa.pa6.into_alternate_af5();
let (cs0, cs1, cs2) = (
gpiob.pb12.into_push_pull_output(),
gpioa.pa15.into_push_pull_output(),
gpioc.pc7.into_push_pull_output(),
);
2020-09-10 14:35:11 +08:00
let io_update = gpiob.pb15.into_push_pull_output();
2020-09-02 11:00:04 +08:00
let spi = dp.SPI1.spi(
(sclk, miso, mosi),
spi::MODE_0,
3.mhz(),
ccdr.peripheral.SPI1,
&ccdr.clocks,
);
2020-09-10 14:35:11 +08:00
let cpld = CPLD::new(spi, (cs0, cs1, cs2), io_update);
let parts = cpld.split();
let urukul = Urukul::new(
2020-09-02 11:00:04 +08:00
parts.spi1, parts.spi2, parts.spi3, parts.spi4, parts.spi5, parts.spi6, parts.spi7,
[25_000_000, 25_000_000, 25_000_000, 25_000_000]
);
2020-09-10 14:35:11 +08:00
cp.SCB.invalidate_icache();
cp.SCB.enable_icache();
2020-09-04 13:31:11 +08:00
2020-09-10 14:35:11 +08:00
// let mut time: u32 = 0;
// let mut next_ms = Instant::now();
2020-09-04 13:31:11 +08:00
2020-09-10 14:35:11 +08:00
// next_ms += 400_000.cycles();
2020-09-04 13:31:11 +08:00
2020-09-10 14:35:11 +08:00
let mut socket_set_entries: [_; 8] = Default::default();
let mut sockets = net::socket::SocketSet::new(&mut socket_set_entries[..]);
let mut rx_storage = [0; 4096];
let mut tx_storage = [0; 4096];
2020-09-04 13:31:11 +08:00
2020-09-10 14:35:11 +08:00
let tcp_socket = {
let tx_buffer = net::socket::TcpSocketBuffer::new(&mut tx_storage[..]);
let rx_buffer = net::socket::TcpSocketBuffer::new(&mut rx_storage[..]);
net::socket::TcpSocket::new(tx_buffer, rx_buffer)
2020-09-04 13:31:11 +08:00
};
2020-09-10 14:35:11 +08:00
let handle = sockets.add(tcp_socket);
// delay.delay_ms(2000_u16);
2020-09-10 14:35:11 +08:00
green_led.set_high().unwrap();
2020-09-04 13:31:11 +08:00
2020-09-10 14:35:11 +08:00
{
let mut socket = sockets.get::<net::socket::TcpSocket>(handle);
socket.connect((net::wire::IpAddress::v4(192, 168, 1, 125), 1883), 49500).unwrap();
socket.set_timeout(Some(net::time::Duration::from_millis(2000)));
2020-09-10 14:35:11 +08:00
debug!("connect!");
}
2020-09-04 13:31:11 +08:00
2020-09-10 14:35:11 +08:00
yellow_led.set_low().unwrap();
red_led.set_high().unwrap();
let mut green = true;
let mut connected = false;
debug!("Poll link status: {}", eth_mac.phy_poll_link());
while !eth_mac.phy_poll_link() {}
debug!("Poll link status: {}", eth_mac.phy_poll_link());
2020-09-02 11:00:04 +08:00
loop {
2020-09-10 14:35:11 +08:00
// let timestamp = net::time::Instant::from_millis(TIME.load(Ordering::Relaxed) as i64);
while !eth_mac.phy_poll_link() {}
2020-09-10 14:35:11 +08:00
match net_interface.poll(&mut sockets, clock.elapsed()) {
Ok(_) => {},
2020-09-04 13:31:11 +08:00
Err(e) => {
2020-09-10 14:35:11 +08:00
debug!("poll error: {}", e);
2020-09-04 13:31:11 +08:00
}
2020-09-10 14:35:11 +08:00
}
2020-09-04 13:31:11 +08:00
2020-09-10 14:35:11 +08:00
{
let mut socket = sockets.get::<net::socket::TcpSocket>(handle);
2020-09-04 13:31:11 +08:00
info!("Socket state: {} at time {}", socket.state(), clock.elapsed());
2020-09-10 14:35:11 +08:00
if socket.may_recv() {
yellow_led.set_high().unwrap();
red_led.set_low().unwrap();
let data = socket.recv(|data| {
2020-09-04 13:31:11 +08:00
(data.len(), data)
2020-09-10 14:35:11 +08:00
}).unwrap();
if socket.can_send() {
socket.send_slice("response".as_bytes()).unwrap();
}
2020-09-04 13:31:11 +08:00
}
2020-09-10 14:35:11 +08:00
if socket.may_send() {
yellow_led.set_high().unwrap();
red_led.set_low().unwrap();
debug!("close");
2020-09-04 13:31:11 +08:00
socket.close();
}
2020-09-10 14:35:11 +08:00
}
2020-09-04 13:31:11 +08:00
2020-09-10 14:35:11 +08:00
match net_interface.poll_delay(&sockets, clock.elapsed()) {
2020-09-10 16:22:12 +08:00
Some(net::time::Duration {millis :0}) => {
clock.advance(net::time::Duration::from_millis(1));
2020-09-10 16:22:12 +08:00
continue;
}
2020-09-10 14:35:11 +08:00
Some(time_delay) => {
2020-09-11 17:36:41 +08:00
green_led.set_low().unwrap();
delay.delay_ms(time_delay.total_millis() as u32);
green_led.set_high().unwrap();
2020-09-10 14:35:11 +08:00
clock.advance(time_delay)
},
None => {
// delay.delay_ms(1_u32);
clock.advance(net::time::Duration::from_millis(1))
},
2020-09-02 11:00:04 +08:00
}
2020-09-04 13:31:11 +08:00
}
}