humpback-dds/examples/mqtt_client.rs

262 lines
7.7 KiB
Rust
Raw Normal View History

2020-09-04 13:30:44 +08:00
#![no_std]
#![no_main]
2020-09-10 14:35:11 +08:00
#[macro_use]
extern crate log;
2020-09-04 13:30:44 +08:00
use smoltcp as net;
2020-09-07 14:05:13 +08:00
use stm32h7xx_hal::ethernet;
2020-09-04 13:30:44 +08:00
use stm32h7xx_hal::{gpio::Speed, prelude::*, spi, pac};
2020-09-07 17:52:37 +08:00
use embedded_hal::{
blocking::spi::Transfer,
digital::v2::OutputPin,
};
2020-09-04 13:30:44 +08:00
use heapless::{consts, String};
use cortex_m;
use cortex_m_rt::{
entry,
exception,
};
use cortex_m_semihosting::hprintln;
2020-09-10 14:35:11 +08:00
// use panic_halt as _;
2020-09-04 13:30:44 +08:00
use rtic::cyccnt::{Instant, U32Ext};
use minimq::{
embedded_nal::{IpAddr, Ipv4Addr, TcpStack, SocketAddr, Mode},
MqttClient, QoS,
};
use firmware::nal_tcp_client::{NetworkStack, NetStorage, NetworkInterface};
use firmware::{Urukul};
use firmware::cpld::{CPLD};
2020-09-10 14:35:11 +08:00
#[path = "util/logger.rs"]
mod logger;
2020-09-04 13:30:44 +08:00
static mut NET_STORE: NetStorage = NetStorage {
// Placeholder for the real IP address, which is initialized at runtime.
ip_addrs: [net::wire::IpCidr::Ipv6(
net::wire::Ipv6Cidr::SOLICITED_NODE_PREFIX,
)],
neighbor_cache: [None; 8],
routes_cache: [None; 8],
};
#[link_section = ".sram3.eth"]
static mut DES_RING: ethernet::DesRing = ethernet::DesRing::new();
macro_rules! add_socket {
($sockets:ident, $tx_storage:ident, $rx_storage:ident) => {
let mut $rx_storage = [0; 4096];
let mut $tx_storage = [0; 4096];
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)
};
let _handle = $sockets.add(tcp_socket);
};
}
#[entry]
fn main() -> ! {
2020-09-10 14:35:11 +08:00
// logger::semihosting_init();
2020-09-04 13:30:44 +08:00
let mut cp = cortex_m::Peripherals::take().unwrap();
let dp = pac::Peripherals::take().unwrap();
cp.DWT.enable_cycle_counter();
// Enable SRAM3 for the descriptor ring.
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());
2020-09-04 13:30:44 +08:00
let pwr = dp.PWR.constrain();
let vos = pwr.freeze();
2020-09-10 14:35:11 +08:00
let rcc = dp.RCC.constrain();
2020-09-04 13:30:44 +08:00
let ccdr = rcc
2020-09-10 14:35:11 +08:00
.use_hse(16.mhz())
.sysclk(400.mhz())
.hclk(200.mhz())
.per_ck(100.mhz())
.pll1_q_ck(48.mhz()) // for SPI
.pll2_p_ck(100.mhz())
.pll2_q_ck(100.mhz())
.freeze(vos, &dp.SYSCFG);
let mut delay = cp.SYST.delay(ccdr.clocks);
2020-09-04 13:30:44 +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);
let gpiod = dp.GPIOD.split(ccdr.peripheral.GPIOD);
let gpioe = dp.GPIOE.split(ccdr.peripheral.GPIOE);
let gpiof = dp.GPIOF.split(ccdr.peripheral.GPIOF);
let gpiog = dp.GPIOG.split(ccdr.peripheral.GPIOG);
2020-09-07 17:52:37 +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();
2020-09-10 14:35:11 +08:00
let mut green_led = gpiob.pb0.into_push_pull_output();
green_led.set_low().unwrap();
2020-09-07 17:52:37 +08:00
2020-09-04 13:30:44 +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, _eth_mac) = unsafe {
2020-09-07 14:05:13 +08:00
ethernet::new_unchecked(
2020-09-04 13:30:44 +08:00
dp.ETHERNET_MAC,
dp.ETHERNET_MTL,
dp.ETHERNET_DMA,
&mut DES_RING,
mac_addr.clone(),
)
};
unsafe { ethernet::enable_interrupt() }
let store = unsafe { &mut NET_STORE };
store.ip_addrs[0] = net::wire::IpCidr::new(net::wire::IpAddress::v4(192, 168, 1, 200), 24);
let neighbor_cache = net::iface::NeighborCache::new(&mut store.neighbor_cache[..]);
let default_v4_gw = net::wire::Ipv4Address::new(192, 168, 1, 1);
let mut routes = net::iface::Routes::new(&mut store.routes_cache[..]);
routes.add_default_ipv4_route(default_v4_gw).unwrap();
let mut net_interface = net::iface::EthernetInterfaceBuilder::new(eth_dma)
.ethernet_addr(mac_addr)
.neighbor_cache(neighbor_cache)
.ip_addrs(&mut store.ip_addrs[..])
// .routes(routes)
.finalize();
/*
* Using SPI1, AF5
* SCLK -> PA5
* MOSI -> PB5
* MISO -> PA6
* CS -> 0: PB12, 1: PA15, 2: PC7
* I/O_Update -> PB15
*/
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(),
);
let io_update = gpiob.pb15.into_push_pull_output();
let spi = dp.SPI1.spi(
(sclk, miso, mosi),
spi::MODE_0,
3.mhz(),
ccdr.peripheral.SPI1,
&ccdr.clocks,
);
let cpld = CPLD::new(spi, (cs0, cs1, cs2), io_update);
let parts = cpld.split();
let urukul = Urukul::new(
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-07 17:52:37 +08:00
cp.SCB.invalidate_icache();
2020-09-04 13:30:44 +08:00
cp.SCB.enable_icache();
2020-09-10 14:35:11 +08:00
// cp.SCB.clean_dcache(&mut cp.CPUID);
// cp.SCB.disable_dcache(&mut cp.CPUID);
// cp.SCB.enable_dcache(&mut cp.CPUID);
2020-09-04 13:30:44 +08:00
let mut time: u32 = 0;
let mut next_ms = Instant::now();
2020-09-07 17:52:37 +08:00
next_ms += 400_000.cycles();
2020-09-04 13:30:44 +08:00
let mut socket_set_entries: [_; 8] = Default::default();
let mut sockets = net::socket::SocketSet::new(&mut socket_set_entries[..]);
add_socket!(sockets, rx_storage, tx_storage);
let tcp_stack = NetworkStack::new(&mut net_interface, sockets);
let mut client = MqttClient::<consts::U256, _>::new(
IpAddr::V4(Ipv4Addr::new(192, 168, 1, 125)),
"nucleo",
tcp_stack,
)
.unwrap();
2020-09-10 14:35:11 +08:00
delay.delay_ms(1000_u16);
client.network_stack.update(time);
green_led.set_high().unwrap();
2020-09-04 13:30:44 +08:00
loop {
2020-09-10 14:35:11 +08:00
let tick = Instant::now() > next_ms;
if tick {
next_ms += 400_000.cycles();
time += 1;
}
client.network_stack.update(time);
client
.poll(|_client, topic, message, _properties| match topic {
_ => info!("On '{:?}', received: {:?}", topic, message),
})
.unwrap();
2020-09-07 17:52:37 +08:00
match client.is_connected() {
true => {
yellow_led.set_high().unwrap();
red_led.set_low().unwrap();
},
_ => {
yellow_led.set_low().unwrap();
red_led.set_high().unwrap();
},
};
2020-09-04 13:30:44 +08:00
if tick && (time % 1000) == 0 {
client
.publish("nucleo", "Hello, World!".as_bytes(), QoS::AtMostOnce, &[])
.unwrap();
}
// Update the TCP stack.
2020-09-10 14:35:11 +08:00
// let sleep = client.network_stack.update(time);
// if sleep {
// //cortex_m::asm::wfi();
// cortex_m::asm::nop();
// }
2020-09-04 13:30:44 +08:00
}
}