Adding updated telemetry implementation

This commit is contained in:
Ryan Summers 2021-05-05 16:46:53 +02:00
parent 8144b3acf2
commit 0c6935587e
6 changed files with 72 additions and 49 deletions

View File

@ -169,21 +169,9 @@ const APP: () = {
#[idle(resources=[network], spawn=[settings_update])]
fn idle(mut c: idle::Context) -> ! {
loop {
// Update the smoltcp network stack.
let poll_result = c
.resources
.network
.lock(|network| network.processor.update());
// Service the MQTT configuration client.
if c.resources
.network
.lock(|network| network.miniconf.update())
== UpdateState::Updated
{
c.spawn.settings_update().unwrap()
} else if poll_result == UpdateState::NoChange {
cortex_m::asm::wfi();
match c.resources.network.lock(|net| net.update()) {
UpdateState::Updated => c.spawn.settings_update().unwrap(),
UpdateState::NoChange => cortex_m::asm::wfi(),
}
}
}

View File

@ -211,21 +211,9 @@ const APP: () = {
#[idle(resources=[network], spawn=[settings_update])]
fn idle(mut c: idle::Context) -> ! {
loop {
// Update the smoltcp network stack.
let poll_result = c
.resources
.network
.lock(|network| network.processor.update());
// Service the MQTT configuration client.
if c.resources
.network
.lock(|network| network.miniconf.update())
== UpdateState::Updated
{
c.spawn.settings_update().unwrap()
} else if poll_result == UpdateState::NoChange {
cortex_m::asm::wfi();
match c.resources.network.lock(|net| net.update()) {
UpdateState::Updated => c.spawn.settings_update().unwrap(),
UpdateState::NoChange => cortex_m::asm::wfi(),
}
}
}

View File

@ -7,6 +7,9 @@ use stm32h7xx_hal::{
prelude::*,
};
const NUM_SOCKETS: usize = 4;
use heapless::{consts, Vec};
use smoltcp_nal::smoltcp;
use embedded_hal::digital::v2::{InputPin, OutputPin};
@ -19,13 +22,13 @@ use super::{
pub struct NetStorage {
pub ip_addrs: [smoltcp::wire::IpCidr; 1],
pub sockets: [Option<smoltcp::socket::SocketSetItem<'static>>; 2],
pub sockets:
[Option<smoltcp::socket::SocketSetItem<'static>>; NUM_SOCKETS + 1],
pub socket_storage: [SocketStorage; NUM_SOCKETS],
pub neighbor_cache:
[Option<(smoltcp::wire::IpAddress, smoltcp::iface::Neighbor)>; 8],
pub routes_cache:
[Option<(smoltcp::wire::IpCidr, smoltcp::iface::Route)>; 8],
pub tx_storage: [u8; 4096],
pub rx_storage: [u8; 4096],
pub dhcp_rx_metadata: [smoltcp::socket::RawPacketMetadata; 1],
pub dhcp_tx_metadata: [smoltcp::socket::RawPacketMetadata; 1],
@ -33,6 +36,21 @@ pub struct NetStorage {
pub dhcp_rx_storage: [u8; 600],
}
#[derive(Copy, Clone)]
pub struct SocketStorage {
rx_storage: [u8; 4096],
tx_storage: [u8; 4096],
}
impl SocketStorage {
const fn new() -> Self {
Self {
rx_storage: [0; 4096],
tx_storage: [0; 4096],
}
}
}
impl NetStorage {
pub fn new() -> Self {
NetStorage {
@ -42,9 +60,8 @@ impl NetStorage {
)],
neighbor_cache: [None; 8],
routes_cache: [None; 8],
sockets: [None, None],
tx_storage: [0; 4096],
rx_storage: [0; 4096],
sockets: [None, None, None, None, None],
socket_storage: [SocketStorage::new(); NUM_SOCKETS],
dhcp_tx_storage: [0; 600],
dhcp_rx_storage: [0; 600],
dhcp_rx_metadata: [smoltcp::socket::RawPacketMetadata::EMPTY; 1],
@ -572,19 +589,25 @@ pub fn setup(
let mut sockets =
smoltcp::socket::SocketSet::new(&mut store.sockets[..]);
let tcp_socket = {
let rx_buffer = smoltcp::socket::TcpSocketBuffer::new(
&mut store.rx_storage[..],
);
let tx_buffer = smoltcp::socket::TcpSocketBuffer::new(
&mut store.tx_storage[..],
);
let mut handles: Vec<smoltcp::socket::SocketHandle, consts::U64> =
Vec::new();
for storage in store.socket_storage.iter_mut() {
let tcp_socket = {
let rx_buffer = smoltcp::socket::TcpSocketBuffer::new(
&mut storage.rx_storage[..],
);
let tx_buffer = smoltcp::socket::TcpSocketBuffer::new(
&mut storage.tx_storage[..],
);
smoltcp::socket::TcpSocket::new(rx_buffer, tx_buffer)
};
smoltcp::socket::TcpSocket::new(rx_buffer, tx_buffer)
};
let handle = sockets.add(tcp_socket);
let handle = sockets.add(tcp_socket);
(sockets, [handle])
handles.push(handle).unwrap();
}
(sockets, handles)
};
let dhcp_client = {

View File

@ -43,7 +43,7 @@ pub const DDS_SYNC_CLK_DIV: u8 = 4;
// The number of ticks in the ADC sampling timer. The timer runs at 100MHz, so the step size is
// equal to 10ns per tick.
// Currently, the sample rate is equal to: Fsample = 100/128 MHz ~ 800 KHz
pub const ADC_SAMPLE_TICKS_LOG2: u8 = 7;
pub const ADC_SAMPLE_TICKS_LOG2: u8 = 12;
pub const ADC_SAMPLE_TICKS: u16 = 1 << ADC_SAMPLE_TICKS_LOG2;
// The desired ADC sample processing buffer size.
@ -51,4 +51,4 @@ pub const SAMPLE_BUFFER_SIZE_LOG2: u8 = 3;
pub const SAMPLE_BUFFER_SIZE: usize = 1 << SAMPLE_BUFFER_SIZE_LOG2;
// The MQTT broker IPv4 address
pub const MQTT_BROKER: [u8; 4] = [10, 34, 16, 10];
pub const MQTT_BROKER: [u8; 4] = [10, 35, 16, 10];

View File

@ -80,6 +80,19 @@ where
telemetry,
}
}
pub fn update(&mut self) -> UpdateState {
// Poll for incoming data.
let poll_result = self.processor.update();
// Update the MQTT clients.
self.telemetry.update();
match self.miniconf.update() {
UpdateState::Updated => UpdateState::Updated,
UpdateState::NoChange => poll_result,
}
}
}
fn get_client_id(

View File

@ -86,4 +86,15 @@ impl<T: Serialize> TelemetryClient<T> {
.publish(&self.telemetry_topic, &telemetry, QoS::AtMostOnce, &[])
.ok();
}
pub fn update(&mut self) {
match self.mqtt.poll(|_client, _topic, _message, _properties| {}) {
Err(minimq::Error::Network(
smoltcp_nal::NetworkError::NoIpAddress,
)) => {}
Err(error) => log::info!("Unexpected error: {:?}", error),
_ => {}
}
}
}