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:
@@ -43,6 +43,7 @@ embassy-executor = { path = "../../../../Rust/embassy/embassy-executor", feature
|
||||
# embassy-time = { git = "https://github.com/us-irs/embassy", branch = "local-cortex-ar", version = "0.4", features = ["tick-hz-1_000_000"] }
|
||||
embassy-time = { path = "../../../../Rust/embassy/embassy-time", version = "0.4", features = ["tick-hz-1_000_000"] }
|
||||
embassy-net = { path = "../../../../Rust/embassy/embassy-net", version = "0.7", features = ["dhcpv4"] }
|
||||
embassy-sync = { path = "../../../../Rust/embassy/embassy-sync", version = "0.7" }
|
||||
heapless = "0.8"
|
||||
axi-uartlite = { git = "https://egit.irs.uni-stuttgart.de/rust/axi-uartlite.git" }
|
||||
axi-uart16550 = { git = "https://egit.irs.uni-stuttgart.de/rust/axi-uart16550.git" }
|
||||
|
||||
@@ -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.
|
||||
}
|
||||
}
|
||||
|
||||
@@ -42,6 +42,7 @@ pub struct CopperControlRegister {
|
||||
}
|
||||
|
||||
#[bitbybit::bitenum(u1, exhaustive = true)]
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
pub enum LatchingLinkStatus {
|
||||
Up = 1,
|
||||
DownSinceLastRead = 0,
|
||||
|
||||
@@ -269,6 +269,8 @@ impl EthernetConfig {
|
||||
pub struct Ethernet {
|
||||
ll: ll::EthernetLowLevel,
|
||||
mdio: mdio::Mdio,
|
||||
current_speed: Speed,
|
||||
current_duplex: Duplex,
|
||||
}
|
||||
|
||||
const IRQ_CONTROL: InterruptControl = InterruptControl::builder()
|
||||
@@ -438,7 +440,12 @@ impl Ethernet {
|
||||
ll.configure_clock(config.init_clk_config, true);
|
||||
let mut mdio = mdio::Mdio::new(&ll, true);
|
||||
mdio.configure_clock_div(config.mdc_clk_div);
|
||||
let mut eth = Ethernet { ll, mdio };
|
||||
let mut eth = Ethernet {
|
||||
ll,
|
||||
mdio,
|
||||
current_speed: Speed::Mbps1000,
|
||||
current_duplex: Duplex::Full,
|
||||
};
|
||||
eth.set_rx_buf_descriptor_base_address(0);
|
||||
eth.set_tx_buf_descriptor_base_address(0);
|
||||
eth
|
||||
@@ -449,7 +456,22 @@ impl Ethernet {
|
||||
ll.configure_clock(config.init_clk_config, true);
|
||||
let mut mdio = mdio::Mdio::new(&ll, true);
|
||||
mdio.configure_clock_div(config.mdc_clk_div);
|
||||
Ethernet { ll, mdio }
|
||||
Ethernet {
|
||||
ll,
|
||||
mdio,
|
||||
current_speed: Speed::Mbps1000,
|
||||
current_duplex: Duplex::Full,
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn speed(&self) -> Speed {
|
||||
self.current_speed
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn duplex(&self) -> Duplex {
|
||||
self.current_duplex
|
||||
}
|
||||
|
||||
pub fn reset(&mut self) {
|
||||
@@ -462,14 +484,9 @@ impl Ethernet {
|
||||
ll.enable_peripheral_clock();
|
||||
ll.reset(3);
|
||||
ll.initialize(true);
|
||||
// Set these to sensible values, but these probably need to be set again after
|
||||
// Set speed and duplex to sensible values, but these probably need to be set again after
|
||||
// auto-negotiation has completed.
|
||||
ll.regs.modify_net_cfg(|mut net_cfg| {
|
||||
net_cfg.set_full_duplex(true);
|
||||
net_cfg.set_gigabit_enable(true);
|
||||
net_cfg.set_speed_mode(SpeedMode::High100Mbps);
|
||||
net_cfg
|
||||
});
|
||||
ll.set_speed_and_duplex(Speed::Mbps1000, Duplex::Full);
|
||||
let macaddr_msbs = (u32::from(mac_address[5]) << 8) | u32::from(mac_address[4]);
|
||||
let macaddr_lsbs = (u32::from(mac_address[3]) << 24)
|
||||
| (u32::from(mac_address[2]) << 16)
|
||||
@@ -526,6 +543,24 @@ impl Ethernet {
|
||||
&mut self.ll.regs
|
||||
}
|
||||
|
||||
/// This function checks whether new auto-negotiated settings require driver settings
|
||||
/// updates and perform them if necessary.
|
||||
pub fn configure_clock_and_speed_duplex(
|
||||
&mut self,
|
||||
speed: Speed,
|
||||
duplex: Duplex,
|
||||
clk_collection: &ClkConfigCollection,
|
||||
) {
|
||||
if speed == self.current_speed && duplex == self.current_duplex {
|
||||
// No change, do nothing.
|
||||
return;
|
||||
}
|
||||
self.ll.set_tx_rx_enable(false, false);
|
||||
self.ll
|
||||
.configure_clock_and_speed_duplex(speed, duplex, clk_collection);
|
||||
self.ll.set_tx_rx_enable(true, true);
|
||||
}
|
||||
|
||||
delegate::delegate! {
|
||||
to self.ll {
|
||||
#[inline]
|
||||
@@ -537,21 +572,6 @@ impl Ethernet {
|
||||
#[inline]
|
||||
pub fn set_tx_buf_descriptor_base_address(&mut self, addr: u32);
|
||||
|
||||
pub fn configure_clock_and_speed_duplex(
|
||||
&mut self,
|
||||
speed: Speed,
|
||||
duplex: Duplex,
|
||||
clk_collection: &ClkConfigCollection
|
||||
);
|
||||
|
||||
pub fn configure_clock_for_speed(
|
||||
&mut self,
|
||||
speed: Speed,
|
||||
clk_collection: &ClkConfigCollection,
|
||||
);
|
||||
|
||||
pub fn set_speed_and_duplex(&mut self, speed: Speed, duplex: Duplex);
|
||||
|
||||
#[inline]
|
||||
pub fn set_tx_rx_enable(&mut self, tx_enable: bool, rx_enable: bool);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user