thermostat/src/net.rs

106 lines
3.4 KiB
Rust
Raw Normal View History

2019-03-13 05:52:39 +08:00
//! As there is only one peripheral, supporting data structures are
//! declared once and globally.
2019-03-15 05:02:08 +08:00
use core::cell::RefCell;
2020-10-30 22:03:57 +08:00
use cortex_m::interrupt::{CriticalSection, Mutex};
2019-03-13 05:52:39 +08:00
use stm32f4xx_hal::{
2020-09-04 03:38:56 +08:00
rcc::Clocks,
pac::{interrupt, Peripherals, ETHERNET_MAC, ETHERNET_DMA},
2019-03-13 05:52:39 +08:00
};
use smoltcp::wire::{EthernetAddress, Ipv4Address, Ipv4Cidr};
use smoltcp::iface::{
EthernetInterfaceBuilder, EthernetInterface,
NeighborCache, Routes,
};
use stm32_eth::{Eth, RingEntry, RxDescriptor, TxDescriptor};
use crate::command_parser::Ipv4Config;
2020-09-04 03:38:56 +08:00
use crate::pins::EthernetPins;
2019-03-13 05:52:39 +08:00
2019-03-19 03:02:57 +08:00
/// Not on the stack so that stack can be placed in CCMRAM (which the
/// ethernet peripheral cannot access)
2019-03-13 05:52:39 +08:00
static mut RX_RING: Option<[RingEntry<RxDescriptor>; 8]> = None;
2019-03-19 03:02:57 +08:00
/// Not on the stack so that stack can be placed in CCMRAM (which the
/// ethernet peripheral cannot access)
2019-03-13 05:52:39 +08:00
static mut TX_RING: Option<[RingEntry<TxDescriptor>; 2]> = None;
2019-03-19 04:41:51 +08:00
/// Interrupt pending flag: set by the `ETH` interrupt handler, should
/// be cleared before polling the interface.
2019-03-15 05:02:08 +08:00
static NET_PENDING: Mutex<RefCell<bool>> = Mutex::new(RefCell::new(false));
2019-03-13 05:52:39 +08:00
2019-03-19 04:41:51 +08:00
/// Run callback `f` with ethernet driver and TCP/IP stack
pub fn run<F>(
2020-09-04 03:38:56 +08:00
clocks: Clocks,
2020-03-09 07:07:56 +08:00
ethernet_mac: ETHERNET_MAC, ethernet_dma: ETHERNET_DMA,
2020-09-04 03:38:56 +08:00
eth_pins: EthernetPins,
ethernet_addr: EthernetAddress,
ipv4_config: Ipv4Config,
f: F
) where
2019-03-19 03:02:57 +08:00
F: FnOnce(EthernetInterface<&mut stm32_eth::Eth<'static, 'static>>),
2019-03-13 05:52:39 +08:00
{
let rx_ring = unsafe {
RX_RING.get_or_insert(Default::default())
};
let tx_ring = unsafe {
TX_RING.get_or_insert(Default::default())
};
2019-03-19 04:41:51 +08:00
// Ethernet driver
2019-03-13 05:52:39 +08:00
let mut eth_dev = Eth::new(
ethernet_mac, ethernet_dma,
2020-09-04 03:38:56 +08:00
&mut rx_ring[..], &mut tx_ring[..],
clocks,
eth_pins,
).unwrap();
2020-03-09 07:07:56 +08:00
eth_dev.enable_interrupt();
2019-03-13 05:52:39 +08:00
2019-03-19 04:41:51 +08:00
// IP stack
let (ipv4_cidr, gateway) = split_ipv4_config(ipv4_config);
let mut ip_addrs = [ipv4_cidr.into()];
2019-03-13 05:52:39 +08:00
let mut neighbor_storage = [None; 16];
let neighbor_cache = NeighborCache::new(&mut neighbor_storage[..]);
let mut routes_storage = [None; 1];
let mut routes = Routes::new(&mut routes_storage[..]);
gateway.map(|gateway| routes.add_default_ipv4_route(gateway).unwrap());
2019-03-13 05:52:39 +08:00
let iface = EthernetInterfaceBuilder::new(&mut eth_dev)
.ethernet_addr(ethernet_addr)
.ip_addrs(&mut ip_addrs[..])
.neighbor_cache(neighbor_cache)
.routes(routes)
2019-03-13 05:52:39 +08:00
.finalize();
2019-03-19 03:02:57 +08:00
f(iface);
2019-03-13 05:52:39 +08:00
}
2019-03-19 04:41:51 +08:00
/// Potentially wake up from `wfi()`, set the interrupt pending flag,
/// clear interrupt flags.
2019-03-13 05:52:39 +08:00
#[interrupt]
fn ETH() {
2019-03-15 05:02:08 +08:00
cortex_m::interrupt::free(|cs| {
*NET_PENDING.borrow(cs)
.borrow_mut() = true;
});
2019-03-13 05:52:39 +08:00
let p = unsafe { Peripherals::steal() };
stm32_eth::eth_interrupt_handler(&p.ETHERNET_DMA);
}
2019-03-15 05:02:08 +08:00
2019-03-19 04:41:51 +08:00
/// Has an interrupt occurred since last call to `clear_pending()`?
2019-03-15 05:02:08 +08:00
pub fn is_pending(cs: &CriticalSection) -> bool {
*NET_PENDING.borrow(cs)
.borrow()
}
2019-03-19 04:41:51 +08:00
/// Clear the interrupt pending flag before polling the interface for
/// data.
2019-03-15 05:02:08 +08:00
pub fn clear_pending(cs: &CriticalSection) {
*NET_PENDING.borrow(cs)
.borrow_mut() = false;
}
/// utility for destructuring into smoltcp types
pub fn split_ipv4_config(config: Ipv4Config) -> (Ipv4Cidr, Option<Ipv4Address>) {
let cidr = Ipv4Cidr::new(Ipv4Address(config.address), config.mask_len);
let gateway = config.gateway.map(Ipv4Address);
(cidr, gateway)
}