humpback-dds/examples/mqtt_client.rs

249 lines
7.7 KiB
Rust
Raw Normal View History

2020-09-04 13:30:44 +08:00
#![no_std]
#![no_main]
2020-09-17 17:05:09 +08:00
use log::{info, warn};
2020-09-10 14:35:11 +08:00
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-18 12:23:28 +08:00
use heapless::consts;
2020-09-04 13:30:44 +08:00
use cortex_m;
2020-09-18 12:23:28 +08:00
use cortex_m_rt::entry;
2020-09-04 13:30:44 +08:00
use rtic::cyccnt::{Instant, U32Ext};
use minimq::{
2020-09-18 12:23:28 +08:00
embedded_nal::{IpAddr, Ipv4Addr, TcpStack},
2020-09-15 12:17:42 +08:00
MqttClient, QoS
2020-09-04 13:30:44 +08:00
};
2020-09-18 12:23:28 +08:00
use firmware::nal_tcp_client::{NetworkStack, NetStorage};
2020-09-15 12:17:42 +08:00
use firmware::{
cpld::{
CPLD,
},
};
2020-09-17 17:05:09 +08:00
use firmware::Urukul;
2020-09-18 12:23:28 +08:00
use firmware::mqtt_mux::MqttMux;
2020-09-04 13:30:44 +08:00
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() -> ! {
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
.use_hse(8.mhz())
2020-09-10 14:35:11 +08:00
.sysclk(400.mhz())
.hclk(200.mhz())
.pll1_q_ck(48.mhz()) // for SPI
.pll1_r_ck(400.mhz()) // for TRACECK
2020-09-10 14:35:11 +08:00
.freeze(vos, &dp.SYSCFG);
unsafe {
logger::enable_itm(&dp.DBGMCU, &mut cp.DCB, &mut cp.ITM);
}
logger::init();
2020-09-18 12:23:28 +08:00
// 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);
// 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-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[..]);
2020-09-23 10:44:38 +08:00
let mut routes = net::iface::Routes::new(&mut store.routes_cache[..]);
let default_v4_gw = net::wire::Ipv4Address::new(192, 168, 1, 1);
routes.add_default_ipv4_route(default_v4_gw).unwrap();
2020-09-04 13:30:44 +08:00
let mut net_interface = net::iface::EthernetInterfaceBuilder::new(eth_dma)
.ethernet_addr(mac_addr)
.neighbor_cache(neighbor_cache)
.ip_addrs(&mut store.ip_addrs[..])
2020-09-23 10:44:38 +08:00
.routes(routes)
2020-09-04 13:30:44 +08:00
.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();
2020-09-18 12:23:28 +08:00
let urukul = Urukul::new(
2020-09-16 12:05:45 +08:00
parts.spi1, parts.spi2, parts.spi3, parts.spi4, parts.spi5, parts.spi6, parts.spi7
2020-09-04 13:30:44 +08:00
);
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-17 17:05:09 +08:00
let mut mqtt_mux = MqttMux::new(urukul);
2020-09-15 12:17:42 +08:00
// Time unit in ms
2020-09-04 13:30:44 +08:00
let mut time: u32 = 0;
// Cycle counter for 1 ms
// This effectively provides a conversion from rtic unit to ms
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);
2020-09-15 12:17:42 +08:00
// Case dealt: Ethernet connection break down, neither side has timeout
// Limitation: Timeout inequality will cause TCP socket state to desync
// Probably fixed in latest smoltcp commit
2020-09-04 13:30:44 +08:00
let mut client = MqttClient::<consts::U256, _>::new(
IpAddr::V4(Ipv4Addr::new(192, 168, 1, 125)),
2020-09-15 12:17:42 +08:00
"Nucleo",
2020-09-04 13:30:44 +08:00
tcp_stack,
)
.unwrap();
2020-09-15 12:17:42 +08:00
let mut tick = false;
2020-09-15 12:17:42 +08:00
let mut has_subscribed = false;
2020-09-10 14:35:11 +08:00
2020-09-04 13:30:44 +08:00
loop {
// Update time accumulator in ms
// Tick once every ms
if Instant::now() > next_ms {
tick = true;
2020-09-10 14:35:11 +08:00
time += 1;
next_ms += 400_000.cycles();
2020-09-10 14:35:11 +08:00
}
2020-09-15 12:17:42 +08:00
// eth Poll if necessary
// Do not poll if eth link is down
if tick && client.network_stack.update_delay(time) == 0 && eth_mac.phy_poll_link() {
client.network_stack.update(time);
}
2020-09-10 14:35:11 +08:00
2020-09-24 17:32:53 +08:00
// Process MQTT messages about Urukul/Control
let connection = client
2020-09-23 10:44:38 +08:00
.poll(|_client, topic, message, _properties| {
2020-09-24 17:32:53 +08:00
info!("On {:?}, received: {:?}", topic, message);
2020-09-23 10:44:38 +08:00
// Why is topic a string while message is a slice?
mqtt_mux.process_mqtt(topic, message);
}).is_ok();
2020-09-15 12:17:42 +08:00
if connection && !has_subscribed && tick {
2020-09-23 10:44:38 +08:00
match client.subscribe("Urukul/Control/#", &[]) {
2020-09-15 12:17:42 +08:00
Ok(()) => has_subscribed = true,
Err(minimq::Error::NotReady) => {},
e => warn!("{:?}", e),
};
}
2020-09-07 17:52:37 +08:00
if connection && tick && (time % 3000) == 0 {
info!("Feedback from print publish: {:?}", client
.publish("Channel1/Switch", "Hello, World!".as_bytes(), QoS::AtMostOnce, &[]));
2020-09-04 13:30:44 +08:00
}
// Reset tick flag
tick = false;
2020-09-04 13:30:44 +08:00
}
}