2020-08-07 13:36:00 +08:00
|
|
|
#![no_main]
|
|
|
|
#![no_std]
|
2020-09-25 14:24:33 +08:00
|
|
|
#![feature(core_intrinsics)]
|
2020-09-28 14:15:37 +08:00
|
|
|
#![feature(assoc_char_funcs)]
|
2020-12-18 17:46:56 +08:00
|
|
|
#![feature(alloc_error_handler)]
|
2020-09-25 14:24:33 +08:00
|
|
|
|
2020-09-28 14:15:37 +08:00
|
|
|
use log::{ trace, warn };
|
2020-09-24 17:32:53 +08:00
|
|
|
use stm32h7xx_hal::gpio::Speed;
|
2020-12-18 17:46:56 +08:00
|
|
|
use stm32h7xx_hal::rng::Rng;
|
2020-09-24 17:32:53 +08:00
|
|
|
use stm32h7xx_hal::{pac, prelude::*, spi};
|
|
|
|
use stm32h7xx_hal::ethernet;
|
2020-08-07 13:36:00 +08:00
|
|
|
|
2020-09-24 17:32:53 +08:00
|
|
|
use smoltcp as net;
|
2021-01-29 16:46:07 +08:00
|
|
|
use SaiTLS as tls;
|
2020-12-18 17:46:56 +08:00
|
|
|
|
|
|
|
use minimq::{ MqttClient, QoS };
|
2020-08-07 13:36:00 +08:00
|
|
|
|
|
|
|
use cortex_m;
|
|
|
|
use cortex_m_rt::entry;
|
2020-12-18 17:46:56 +08:00
|
|
|
use alloc_cortex_m::CortexMHeap;
|
2020-09-24 17:32:53 +08:00
|
|
|
use rtic::cyccnt::{Instant, U32Ext};
|
2020-12-18 17:46:56 +08:00
|
|
|
use rand_core::{RngCore, CryptoRng};
|
2020-08-07 13:36:00 +08:00
|
|
|
|
2020-12-18 17:46:56 +08:00
|
|
|
use tls::TlsRng;
|
|
|
|
use tls::tls::TlsSocket;
|
|
|
|
use tls::tcp_stack::NetworkStack;
|
2020-09-29 17:02:15 +08:00
|
|
|
use heapless::{ String, consts, consts::* };
|
2020-12-18 17:46:56 +08:00
|
|
|
use core::alloc::Layout;
|
2020-09-24 17:32:53 +08:00
|
|
|
|
|
|
|
#[macro_use]
|
|
|
|
pub mod bitmask_macro;
|
|
|
|
pub mod spi_slave;
|
|
|
|
pub mod cpld;
|
|
|
|
use crate::cpld::CPLD;
|
|
|
|
pub mod config_register;
|
|
|
|
pub mod attenuator;
|
|
|
|
pub mod dds;
|
2020-12-18 17:46:56 +08:00
|
|
|
pub mod net_store;
|
|
|
|
use crate::net_store::NetStorage;
|
2020-10-06 15:03:53 +08:00
|
|
|
pub mod fpga;
|
|
|
|
use crate::fpga::flash_ice40_fpga;
|
2020-09-24 17:32:53 +08:00
|
|
|
pub mod mqtt_mux;
|
|
|
|
use crate::mqtt_mux::MqttMux;
|
|
|
|
pub mod urukul;
|
|
|
|
use crate::urukul::Urukul;
|
2020-10-06 15:03:53 +08:00
|
|
|
pub mod flash;
|
2020-12-18 17:46:56 +08:00
|
|
|
pub mod config;
|
|
|
|
use crate::config::get_net_config;
|
|
|
|
pub mod flash_store;
|
|
|
|
use crate::flash_store::init_flash;
|
2020-08-07 13:36:00 +08:00
|
|
|
|
2020-09-14 17:33:50 +08:00
|
|
|
mod logger;
|
|
|
|
|
2020-12-18 17:46:56 +08:00
|
|
|
#[global_allocator]
|
|
|
|
static ALLOCATOR: CortexMHeap = CortexMHeap::empty();
|
|
|
|
|
|
|
|
#[alloc_error_handler]
|
|
|
|
fn oom(_: Layout) -> ! {
|
|
|
|
warn!("Out of memory!");
|
|
|
|
loop {}
|
|
|
|
}
|
|
|
|
|
2020-09-24 17:32:53 +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();
|
2021-01-29 16:46:07 +08:00
|
|
|
#[link_section = ".sram3.tx_store"]
|
|
|
|
static mut TX_STORAGE: [u8; 8192] = [0; 8192];
|
|
|
|
#[link_section = ".sram3.rx_store"]
|
|
|
|
static mut RX_STORAGE: [u8; 8192] = [0; 8192];
|
2020-09-24 17:32:53 +08:00
|
|
|
|
2020-12-18 17:46:56 +08:00
|
|
|
struct RngStruct {
|
|
|
|
rng: Rng
|
|
|
|
}
|
|
|
|
|
|
|
|
impl RngCore for RngStruct {
|
|
|
|
fn next_u32(&mut self) -> u32 {
|
|
|
|
self.rng.gen().unwrap()
|
|
|
|
}
|
2020-09-24 17:32:53 +08:00
|
|
|
|
2020-12-18 17:46:56 +08:00
|
|
|
fn next_u64(&mut self) -> u64 {
|
|
|
|
(u64::from(self.next_u32()) << 32) | u64::from(self.next_u32())
|
|
|
|
}
|
2020-09-24 17:32:53 +08:00
|
|
|
|
2020-12-18 17:46:56 +08:00
|
|
|
fn fill_bytes(&mut self, dest: &mut [u8]) {
|
|
|
|
self.rng.fill(dest).unwrap();
|
|
|
|
}
|
2020-09-24 17:32:53 +08:00
|
|
|
|
2020-12-18 17:46:56 +08:00
|
|
|
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), rand_core::Error> {
|
|
|
|
Ok(self.fill_bytes(dest))
|
|
|
|
}
|
2020-09-24 17:32:53 +08:00
|
|
|
}
|
|
|
|
|
2020-12-18 17:46:56 +08:00
|
|
|
impl CryptoRng for RngStruct {}
|
|
|
|
|
|
|
|
impl TlsRng for RngStruct {}
|
|
|
|
|
2020-08-07 13:36:00 +08:00
|
|
|
#[entry]
|
|
|
|
fn main() -> ! {
|
|
|
|
|
2020-12-18 17:46:56 +08:00
|
|
|
// Initialize the allocator BEFORE you use it
|
|
|
|
let start = cortex_m_rt::heap_start() as usize;
|
|
|
|
let size = 32768; // in bytes
|
|
|
|
unsafe { ALLOCATOR.init(start, size) }
|
|
|
|
|
2020-09-25 14:24:33 +08:00
|
|
|
let mut cp = cortex_m::Peripherals::take().unwrap();
|
|
|
|
let dp = pac::Peripherals::take().unwrap();
|
2020-08-07 13:36:00 +08:00
|
|
|
|
2020-09-25 14:24:33 +08:00
|
|
|
unsafe {
|
|
|
|
logger::enable_itm(&dp.DBGMCU, &mut cp.DCB, &mut cp.ITM);
|
|
|
|
}
|
|
|
|
logger::init();
|
2020-09-24 17:32:53 +08:00
|
|
|
|
2021-01-29 16:46:07 +08:00
|
|
|
// Enable SRAM3 for the descriptor ring and smoltcp buffers.
|
2020-09-24 17:32:53 +08:00
|
|
|
dp.RCC.ahb2enr.modify(|_, w| w.sram3en().set_bit());
|
2021-01-29 16:46:07 +08:00
|
|
|
// Enable SRAM2 for the RAM management buffer
|
|
|
|
dp.RCC.ahb2enr.modify(|_, w| w.sram2en().set_bit());
|
2020-09-25 14:24:33 +08:00
|
|
|
|
|
|
|
let pwr = dp.PWR.constrain();
|
|
|
|
let vos = pwr.freeze();
|
|
|
|
|
|
|
|
let rcc = dp.RCC.constrain();
|
|
|
|
let ccdr = rcc
|
|
|
|
.use_hse(8.mhz())
|
|
|
|
.sys_ck(400.mhz())
|
|
|
|
.hclk(200.mhz())
|
|
|
|
.pll1_q_ck(48.mhz())
|
|
|
|
.pll1_r_ck(400.mhz())
|
|
|
|
.freeze(vos, &dp.SYSCFG);
|
|
|
|
|
2020-09-25 10:57:50 +08:00
|
|
|
let delay = cp.SYST.delay(ccdr.clocks);
|
|
|
|
|
|
|
|
cp.SCB.invalidate_icache();
|
|
|
|
cp.SCB.enable_icache();
|
|
|
|
|
|
|
|
cp.DWT.enable_cycle_counter();
|
2020-08-07 13:36:00 +08:00
|
|
|
|
2020-12-18 17:46:56 +08:00
|
|
|
// Instantiate random number generator
|
|
|
|
let mut rng = RngStruct {
|
|
|
|
rng: dp.RNG.constrain(ccdr.peripheral.RNG, &ccdr.clocks)
|
2020-10-06 15:03:53 +08:00
|
|
|
};
|
|
|
|
|
2020-12-18 17:46:56 +08:00
|
|
|
// Create sfkv store and flash storage manager
|
|
|
|
let (flash, mut flash_store) = init_flash(dp.FLASH);
|
|
|
|
|
|
|
|
// Acquire client/broker IP Address, client MAC address from flash memory
|
|
|
|
let net_config = get_net_config(&mut flash_store);
|
|
|
|
|
2020-09-25 14:24:33 +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);
|
2020-09-25 10:24:18 +08:00
|
|
|
let gpiog = dp.GPIOG.split(ccdr.peripheral.GPIOG);
|
2020-08-07 13:36:00 +08:00
|
|
|
|
2020-09-25 10:57:50 +08:00
|
|
|
// Note: ITM doesn't work beyond this, due to a pin conflict between:
|
|
|
|
// - FPGA_SPI: SCK (af5)
|
|
|
|
// - ST_LINK SWO (af0)
|
|
|
|
// Both demands PB3
|
2020-09-25 14:24:33 +08:00
|
|
|
trace!("Flashing configuration bitstream to iCE40 HX8K on Humpback.");
|
|
|
|
|
|
|
|
// Using SPI_1 alternate functions (af5)
|
|
|
|
let fpga_sck = gpiob.pb3.into_alternate_af5();
|
|
|
|
let fpga_sdo = gpiob.pb4.into_alternate_af5();
|
|
|
|
let fpga_sdi = gpiob.pb5.into_alternate_af5();
|
|
|
|
|
|
|
|
// Setup SPI_SS_B and CRESET_B
|
|
|
|
let fpga_ss = gpioa.pa4.into_push_pull_output();
|
|
|
|
let fpga_creset = gpiof.pf3.into_open_drain_output();
|
|
|
|
|
|
|
|
// Setup CDONE
|
|
|
|
let fpga_cdone = gpiod.pd15.into_pull_up_input();
|
|
|
|
|
|
|
|
// Setup SPI interface
|
|
|
|
let fpga_cfg_spi = dp.SPI1.spi(
|
|
|
|
(fpga_sck, fpga_sdo, fpga_sdi),
|
|
|
|
spi::MODE_3,
|
|
|
|
12.mhz(),
|
|
|
|
ccdr.peripheral.SPI1,
|
|
|
|
&ccdr.clocks,
|
|
|
|
);
|
2020-09-24 17:32:53 +08:00
|
|
|
|
2020-09-25 14:24:33 +08:00
|
|
|
flash_ice40_fpga(fpga_cfg_spi, fpga_ss, fpga_creset, fpga_cdone, delay).unwrap();
|
2020-09-25 10:57:50 +08:00
|
|
|
|
2020-09-24 17:32:53 +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
|
2020-10-05 15:20:54 +08:00
|
|
|
// let mac_addr = net::wire::EthernetAddress([0xAC, 0x6F, 0x7A, 0xDE, 0xD6, 0xC8]);
|
2020-09-24 17:32:53 +08:00
|
|
|
let (eth_dma, mut eth_mac) = unsafe {
|
|
|
|
ethernet::new_unchecked(
|
|
|
|
dp.ETHERNET_MAC,
|
|
|
|
dp.ETHERNET_MTL,
|
|
|
|
dp.ETHERNET_DMA,
|
|
|
|
&mut DES_RING,
|
2020-12-18 17:46:56 +08:00
|
|
|
net_config.eth_addr.clone(),
|
2020-09-24 17:32:53 +08:00
|
|
|
)
|
|
|
|
};
|
|
|
|
|
|
|
|
unsafe { ethernet::enable_interrupt() }
|
|
|
|
|
|
|
|
let store = unsafe { &mut NET_STORE };
|
|
|
|
|
2020-12-18 17:46:56 +08:00
|
|
|
store.ip_addrs[0] = net_config.ip_cidr;
|
2020-09-24 17:32:53 +08:00
|
|
|
|
|
|
|
let neighbor_cache = net::iface::NeighborCache::new(&mut store.neighbor_cache[..]);
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
let mut net_interface = net::iface::EthernetInterfaceBuilder::new(eth_dma)
|
2020-12-18 17:46:56 +08:00
|
|
|
.ethernet_addr(net_config.eth_addr)
|
2020-09-24 17:32:53 +08:00
|
|
|
.neighbor_cache(neighbor_cache)
|
|
|
|
.ip_addrs(&mut store.ip_addrs[..])
|
|
|
|
.routes(routes)
|
|
|
|
.finalize();
|
2020-08-07 13:36:00 +08:00
|
|
|
|
2020-09-25 14:24:33 +08:00
|
|
|
/*
|
|
|
|
* Using SPI6
|
|
|
|
* SCLK -> PA5 (af8)
|
|
|
|
* MOSI -> PG14 (af5)
|
|
|
|
* MISO -> PA6 (af8)
|
|
|
|
* CS -> 0: PB12, 1: PA15, 2: PC7
|
|
|
|
*/
|
|
|
|
let sclk = gpioa.pa5.into_alternate_af8().set_speed(Speed::VeryHigh);
|
|
|
|
let mosi = gpiog.pg14.into_alternate_af5().set_speed(Speed::VeryHigh);
|
|
|
|
let miso = gpioa.pa6.into_alternate_af8().set_speed(Speed::VeryHigh);
|
|
|
|
let (cs0, cs1, cs2) = (
|
|
|
|
gpiob.pb12.into_push_pull_output(),
|
|
|
|
gpioa.pa15.into_push_pull_output(),
|
|
|
|
gpioc.pc7.into_push_pull_output(),
|
|
|
|
);
|
|
|
|
|
|
|
|
/*
|
2020-09-25 17:06:33 +08:00
|
|
|
* I/O_Update -> PB15
|
2020-09-25 14:24:33 +08:00
|
|
|
*/
|
|
|
|
let io_update = gpiob.pb15.into_push_pull_output();
|
|
|
|
|
|
|
|
let spi = dp.SPI6.spi(
|
|
|
|
(sclk, miso, mosi),
|
|
|
|
spi::MODE_0,
|
|
|
|
2.mhz(),
|
|
|
|
ccdr.peripheral.SPI6,
|
|
|
|
&ccdr.clocks,
|
|
|
|
);
|
|
|
|
|
|
|
|
let switch = CPLD::new(spi, (cs0, cs1, cs2), io_update);
|
|
|
|
let parts = switch.split();
|
2020-08-11 00:07:07 +08:00
|
|
|
|
2020-12-18 17:46:56 +08:00
|
|
|
let urukul = Urukul::new(
|
2020-09-24 17:32:53 +08:00
|
|
|
parts.spi1, parts.spi2, parts.spi3, parts.spi4, parts.spi5, parts.spi6, parts.spi7
|
2021-01-29 16:46:07 +08:00
|
|
|
);
|
2020-09-24 17:32:53 +08:00
|
|
|
|
2020-12-18 17:46:56 +08:00
|
|
|
let mut mqtt_mux = MqttMux::new(urukul, flash, flash_store, net_config.name.as_str());
|
2020-09-24 17:32:53 +08:00
|
|
|
|
|
|
|
// Time unit in ms
|
|
|
|
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();
|
|
|
|
next_ms += 400_000.cycles();
|
|
|
|
|
2020-12-18 17:46:56 +08:00
|
|
|
let mut tls_socket_entries: [_; 1] = Default::default();
|
2021-01-29 16:46:07 +08:00
|
|
|
let mut tls_socket_set = tls::set::TlsSocketSet::new(
|
2020-12-18 17:46:56 +08:00
|
|
|
&mut tls_socket_entries[..]
|
|
|
|
);
|
2020-09-24 17:32:53 +08:00
|
|
|
|
2021-01-29 16:46:07 +08:00
|
|
|
let tx_buffer = net::socket::TcpSocketBuffer::new(unsafe { &mut TX_STORAGE[..] });
|
|
|
|
let rx_buffer = net::socket::TcpSocketBuffer::new(unsafe { &mut RX_STORAGE[..] });
|
2020-12-18 17:46:56 +08:00
|
|
|
let mut tcp_socket = net::socket::TcpSocket::new(rx_buffer, tx_buffer);
|
|
|
|
tcp_socket.set_keep_alive(
|
|
|
|
Some(net::time::Duration::from_secs(2))
|
|
|
|
);
|
|
|
|
|
|
|
|
let tls_socket = TlsSocket::new(
|
|
|
|
tcp_socket,
|
|
|
|
&mut rng,
|
|
|
|
None
|
|
|
|
);
|
|
|
|
let _ = tls_socket_set.add(tls_socket);
|
|
|
|
|
|
|
|
let tls_stack = NetworkStack::new(
|
|
|
|
tls_socket_set
|
|
|
|
);
|
2020-09-24 17:32:53 +08:00
|
|
|
|
2021-01-29 16:46:07 +08:00
|
|
|
let mut client = MqttClient::<consts::U2048, _>::new(
|
2020-12-18 17:46:56 +08:00
|
|
|
net_config.broker_ip,
|
|
|
|
net_config.name.as_str(),
|
|
|
|
tls_stack,
|
2020-09-24 17:32:53 +08:00
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let mut tick = false;
|
|
|
|
let mut has_subscribed = false;
|
|
|
|
|
|
|
|
loop {
|
|
|
|
// Update time accumulator in ms
|
|
|
|
// Tick once every ms
|
|
|
|
if Instant::now() > next_ms {
|
|
|
|
tick = true;
|
|
|
|
time += 1;
|
|
|
|
next_ms += 400_000.cycles();
|
|
|
|
}
|
|
|
|
|
|
|
|
// eth Poll if necessary
|
|
|
|
// Do not poll if eth link is down
|
2020-12-18 17:46:56 +08:00
|
|
|
while !eth_mac.phy_poll_link() {}
|
|
|
|
client.network_stack.poll(&mut net_interface, net::time::Instant::from_millis(time));
|
2020-09-24 17:32:53 +08:00
|
|
|
|
|
|
|
// Process MQTT messages about Urukul/Control
|
2020-09-28 14:15:37 +08:00
|
|
|
let connection = match client
|
2020-09-24 17:32:53 +08:00
|
|
|
.poll(|_client, topic, message, _properties| {
|
2020-09-28 14:15:37 +08:00
|
|
|
mqtt_mux.process_mqtt_ingress(topic, message);
|
|
|
|
}) {
|
|
|
|
Ok(_) => true,
|
|
|
|
Err(e) => {
|
2020-12-18 17:46:56 +08:00
|
|
|
log::info!("Warn: {:?}", e);
|
2020-09-28 14:15:37 +08:00
|
|
|
false
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
// Process MQTT response messages about Urukul
|
2020-09-29 17:02:15 +08:00
|
|
|
for (topic, message) in mqtt_mux.process_mqtt_egress().unwrap() {
|
|
|
|
client.publish(
|
|
|
|
topic.as_str(),
|
2020-09-28 14:15:37 +08:00
|
|
|
message.as_bytes(),
|
|
|
|
QoS::AtMostOnce,
|
|
|
|
&[]
|
2020-09-29 17:02:15 +08:00
|
|
|
).unwrap();
|
2020-09-28 14:15:37 +08:00
|
|
|
}
|
2020-09-24 17:32:53 +08:00
|
|
|
|
|
|
|
if connection && !has_subscribed && tick {
|
2020-12-18 17:46:56 +08:00
|
|
|
let mut str_builder: String<U128> = String::from(net_config.name.as_str());
|
2020-09-29 17:02:15 +08:00
|
|
|
str_builder.push_str("/Control/#").unwrap();
|
|
|
|
match client.subscribe(str_builder.as_str(), &[]) {
|
2020-09-24 17:32:53 +08:00
|
|
|
Ok(()) => has_subscribed = true,
|
2020-09-25 10:57:50 +08:00
|
|
|
Err(minimq::Error::NotReady) => {},
|
|
|
|
_e => {},
|
2020-09-24 17:32:53 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reset tick flag
|
|
|
|
tick = false;
|
|
|
|
}
|
2020-08-07 13:36:00 +08:00
|
|
|
}
|
|
|
|
|