thermostat/src/net.rs

78 lines
2.5 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;
use cortex_m::interrupt::Mutex;
use bare_metal::CriticalSection;
2019-03-13 05:52:39 +08:00
use stm32f4xx_hal::{
stm32::{interrupt, Peripherals, NVIC, ETHERNET_MAC, ETHERNET_DMA},
};
use smoltcp::wire::{EthernetAddress, IpAddress, IpCidr};
use smoltcp::iface::{NeighborCache, EthernetInterfaceBuilder, EthernetInterface};
use stm32_eth::{Eth, RingEntry, RxDescriptor, TxDescriptor};
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;
// TODO: generate one from device id
const SRC_MAC: [u8; 6] = [0x00, 0x00, 0xDE, 0xAD, 0xBE, 0xEF];
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 03:02:57 +08:00
pub fn run<F>(nvic: &mut NVIC, ethernet_mac: ETHERNET_MAC, ethernet_dma: ETHERNET_DMA, f: F)
2019-03-13 05:52:39 +08:00
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())
};
let mut eth_dev = Eth::new(
ethernet_mac, ethernet_dma,
&mut rx_ring[..], &mut tx_ring[..]
);
eth_dev.enable_interrupt(nvic);
let local_addr = IpAddress::v4(192, 168, 69, 3);
let mut ip_addrs = [IpCidr::new(local_addr, 24)];
let mut neighbor_storage = [None; 16];
let neighbor_cache = NeighborCache::new(&mut neighbor_storage[..]);
let ethernet_addr = EthernetAddress(SRC_MAC);
let iface = EthernetInterfaceBuilder::new(&mut eth_dev)
.ethernet_addr(ethernet_addr)
.ip_addrs(&mut ip_addrs[..])
.neighbor_cache(neighbor_cache)
.finalize();
2019-03-19 03:02:57 +08:00
f(iface);
2019-03-13 05:52:39 +08:00
}
2019-03-19 03:02:57 +08:00
/// Wake up from `wfi()`, clear interrupt flags,
2019-03-13 05:52:39 +08:00
/// and TODO: set pending flag
#[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
pub fn is_pending(cs: &CriticalSection) -> bool {
*NET_PENDING.borrow(cs)
.borrow()
}
pub fn clear_pending(cs: &CriticalSection) {
*NET_PENDING.borrow(cs)
.borrow_mut() = false;
}