oh god interrupt data sharing
Some checks failed
ci / Check build (push) Has been cancelled
ci / Check formatting (push) Has been cancelled
ci / Check Documentation Build (push) Has been cancelled
ci / Clippy (push) Has been cancelled
ci / Check build (pull_request) Has been cancelled
ci / Check formatting (pull_request) Has been cancelled
ci / Check Documentation Build (pull_request) Has been cancelled
ci / Clippy (pull_request) Has been cancelled
Some checks failed
ci / Check build (push) Has been cancelled
ci / Check formatting (push) Has been cancelled
ci / Check Documentation Build (push) Has been cancelled
ci / Clippy (push) Has been cancelled
ci / Check build (pull_request) Has been cancelled
ci / Check formatting (pull_request) Has been cancelled
ci / Check Documentation Build (pull_request) Has been cancelled
ci / Clippy (pull_request) Has been cancelled
This commit is contained in:
@@ -1,20 +1,23 @@
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
|
||||
use core::{mem::MaybeUninit, panic::PanicInfo};
|
||||
use core::{cell::RefCell, mem::MaybeUninit, panic::PanicInfo};
|
||||
use cortex_ar::asm::nop;
|
||||
use embassy_executor::Spawner;
|
||||
use embassy_time::{Duration, Ticker, WithTimeout};
|
||||
use embassy_time::{Duration, Ticker, Timer};
|
||||
use embedded_hal::digital::StatefulOutputPin;
|
||||
use embedded_io::Write;
|
||||
use log::{error, info};
|
||||
use rand::{RngCore, SeedableRng};
|
||||
use zedboard::PS_CLOCK_FREQUENCY;
|
||||
use zedboard::{PS_CLOCK_FREQUENCY, phy_marvell::LatchingLinkStatus};
|
||||
use zynq7000_hal::{
|
||||
BootMode,
|
||||
clocks::Clocks,
|
||||
configure_level_shifter,
|
||||
eth::{AlignedBuffer, ClkConfigCollection, EthernetConfig, EthernetLowLevel},
|
||||
eth::{
|
||||
AlignedBuffer, ClkConfigCollection, EthernetConfig, EthernetLowLevel,
|
||||
embassy_net::InterruptResult,
|
||||
},
|
||||
gic::{GicConfigurator, GicInterruptHelper, Interrupt},
|
||||
gpio::{GpioPins, Output, PinState},
|
||||
gtc::Gtc,
|
||||
@@ -45,6 +48,26 @@ static ETH_RX_BUFS: static_cell::ConstStaticCell<[AlignedBuffer; NUM_RX_BUFS]> =
|
||||
static ETH_TX_BUFS: static_cell::ConstStaticCell<[AlignedBuffer; NUM_TX_BUFS]> =
|
||||
static_cell::ConstStaticCell::new([AlignedBuffer([0; zynq7000_hal::eth::MTU]); NUM_TX_BUFS]);
|
||||
|
||||
static ETH_ERR_QUEUE: static_cell::ConstStaticCell<
|
||||
embassy_sync::channel::Channel<
|
||||
embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex,
|
||||
InterruptResult,
|
||||
8,
|
||||
>,
|
||||
> = static_cell::ConstStaticCell::new(embassy_sync::channel::Channel::new());
|
||||
static ETH_ERR_SENDER: critical_section::Mutex<
|
||||
RefCell<
|
||||
Option<
|
||||
embassy_sync::channel::Sender<
|
||||
'static,
|
||||
embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex,
|
||||
InterruptResult,
|
||||
8,
|
||||
>,
|
||||
>,
|
||||
>,
|
||||
> = critical_section::Mutex::new(RefCell::new(None));
|
||||
|
||||
/// See memory.x file. 1 MB starting at this address will be configured as uncached memory using the
|
||||
/// MMU.
|
||||
const UNCACHED_ADDR: u32 = 0x4000000;
|
||||
@@ -74,6 +97,14 @@ async fn led_task(mut mio_led: Output) -> ! {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
pub enum IpMode {
|
||||
LinkDown,
|
||||
AutoNegotiating,
|
||||
AwaitingIpConfig,
|
||||
StackReady,
|
||||
}
|
||||
|
||||
#[embassy_executor::main]
|
||||
#[unsafe(export_name = "main")]
|
||||
async fn main(spawner: Spawner) -> ! {
|
||||
@@ -215,11 +246,18 @@ async fn main(spawner: Spawner) -> ! {
|
||||
RESOURCES.init(embassy_net::StackResources::new()),
|
||||
rng.next_u64(),
|
||||
);
|
||||
let eth_err_queue = ETH_ERR_QUEUE.take();
|
||||
let sender = eth_err_queue.sender();
|
||||
let receiver = eth_err_queue.receiver();
|
||||
critical_section::with(|cs| {
|
||||
let mut sender_ref = ETH_ERR_SENDER.borrow(cs).borrow_mut();
|
||||
sender_ref.replace(sender);
|
||||
});
|
||||
spawner.spawn(embassy_net_task(runner)).unwrap();
|
||||
|
||||
let mut mio_led = Output::new_for_mio(gpio_pins.mio.mio7, PinState::Low);
|
||||
|
||||
spawner.spawn(led_task(mio_led)).unwrap();
|
||||
//spawner.spawn(led_task(mio_led)).unwrap();
|
||||
|
||||
let mut _emio_leds: [Output; 8] = [
|
||||
Output::new_for_emio(gpio_pins.emio.take(0).unwrap(), PinState::Low),
|
||||
@@ -231,23 +269,20 @@ async fn main(spawner: Spawner) -> ! {
|
||||
Output::new_for_emio(gpio_pins.emio.take(6).unwrap(), PinState::Low),
|
||||
Output::new_for_emio(gpio_pins.emio.take(7).unwrap(), PinState::Low),
|
||||
];
|
||||
let mut ip_mode = IpMode::LinkDown;
|
||||
loop {
|
||||
// TODO: We have to check our own link state boolean/atomic here instead of the stack
|
||||
// link. We have to periodically monitor the phy link state, and update the atomic from
|
||||
// that value.
|
||||
//
|
||||
// If the link goes from up to down, we update the atomic and have to periodically check
|
||||
// whether the link is up again.
|
||||
//
|
||||
// If it does from down to up, wait for auto-negotiation to complete, and update the
|
||||
// atomic from that value.
|
||||
if !stack.is_link_up() {
|
||||
loop {
|
||||
let config_up_fut = stack.wait_config_up();
|
||||
while let Ok(msg) = receiver.try_receive() {}
|
||||
match ip_mode {
|
||||
// Assuming that auto-negotiation is performed automatically.
|
||||
IpMode::LinkDown => {
|
||||
mio_led.set_low();
|
||||
zynq7000_hal::eth::embassy_net::update_link_state(
|
||||
embassy_net::driver::LinkState::Down,
|
||||
);
|
||||
ip_mode = IpMode::AutoNegotiating;
|
||||
}
|
||||
IpMode::AutoNegotiating => {
|
||||
let status = phy.read_copper_status();
|
||||
// TODO: How is this related to the ethernet stack link state? Should we only
|
||||
// update the link state to up once auto-negotiation is complete and the clock
|
||||
// was configured correctly?
|
||||
if status.auto_negotiation_complete() {
|
||||
let extended_status = phy.read_copper_specific_status_register_1();
|
||||
eth.configure_clock_and_speed_duplex(
|
||||
@@ -257,18 +292,33 @@ async fn main(spawner: Spawner) -> ! {
|
||||
extended_status.duplex().as_zynq7000_eth_duplex(),
|
||||
&clk_collection,
|
||||
);
|
||||
eth.set_tx_rx_enable(true, true);
|
||||
}
|
||||
match config_up_fut.with_timeout(Duration::from_millis(100)).await {
|
||||
Ok(_) => break,
|
||||
Err(e) => todo!(),
|
||||
zynq7000_hal::eth::embassy_net::update_link_state(
|
||||
embassy_net::driver::LinkState::Up,
|
||||
);
|
||||
ip_mode = IpMode::AwaitingIpConfig;
|
||||
} else {
|
||||
Timer::after_millis(100).await;
|
||||
}
|
||||
}
|
||||
IpMode::AwaitingIpConfig => {
|
||||
// TODO: Timeout, check for link down periodically.
|
||||
stack.wait_config_up().await;
|
||||
let network_config = stack.config_v4();
|
||||
info!("Network configuration is up: DHCP config: {network_config:?}!",);
|
||||
ip_mode = IpMode::StackReady;
|
||||
mio_led.set_high();
|
||||
}
|
||||
IpMode::StackReady => {
|
||||
let status = phy.read_copper_status();
|
||||
if status.copper_link_status() == LatchingLinkStatus::DownSinceLastRead {
|
||||
ip_mode = IpMode::LinkDown;
|
||||
continue;
|
||||
}
|
||||
// Now what? maybe UDP Server?
|
||||
info!("stack is ready");
|
||||
Timer::after_millis(100).await;
|
||||
}
|
||||
}
|
||||
let network_config = stack.config_v4();
|
||||
info!("Network configuration is up: DHCP config: {network_config:?}!",);
|
||||
//mio_led.toggle().unwrap();
|
||||
//ticker.next().await; // Wait for the next cycle of the ticker
|
||||
}
|
||||
}
|
||||
|
||||
@@ -290,6 +340,13 @@ pub extern "C" fn _irq_handler() {
|
||||
let result = zynq7000_hal::eth::embassy_net::on_interrupt(
|
||||
zynq7000_hal::eth::EthernetId::Eth0,
|
||||
);
|
||||
if result.has_anomalies() || result.has_errors() {
|
||||
critical_section::with(|cs| {
|
||||
let sender_ref = ETH_ERR_SENDER.borrow(cs).borrow_mut();
|
||||
let sender_mut = sender_ref.unwrap();
|
||||
sender_mut.try_send(result).ok();
|
||||
})
|
||||
}
|
||||
// TODO: Send the result structure back to the main thread.
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user