2019-05-05 20:56:23 +08:00
|
|
|
#![no_std]
|
|
|
|
#![no_main]
|
2020-07-28 12:36:16 +08:00
|
|
|
#![feature(const_in_array_repeat_expressions)]
|
2021-01-28 10:58:17 +08:00
|
|
|
#![feature(naked_functions)]
|
|
|
|
#![feature(asm)]
|
2019-05-05 20:56:23 +08:00
|
|
|
|
2020-03-31 07:16:58 +08:00
|
|
|
extern crate alloc;
|
|
|
|
|
2020-07-19 15:39:08 +08:00
|
|
|
use alloc::collections::BTreeMap;
|
2020-06-05 11:48:41 +08:00
|
|
|
use libasync::{
|
|
|
|
delay,
|
|
|
|
smoltcp::{Sockets, TcpStream},
|
|
|
|
task,
|
|
|
|
};
|
2020-03-26 05:23:30 +08:00
|
|
|
use libboard_zynq::{
|
2020-06-05 11:48:41 +08:00
|
|
|
self as zynq,
|
|
|
|
clocks::source::{ArmPll, ClockSource, IoPll},
|
|
|
|
clocks::Clocks,
|
2020-09-09 20:13:13 +08:00
|
|
|
println, stdio,
|
2020-08-03 11:24:47 +08:00
|
|
|
mpcore,
|
|
|
|
gic,
|
2020-03-26 05:23:30 +08:00
|
|
|
smoltcp::{
|
2020-06-05 11:48:41 +08:00
|
|
|
iface::{EthernetInterfaceBuilder, NeighborCache, Routes},
|
2020-03-26 05:23:30 +08:00
|
|
|
time::Instant,
|
2020-06-05 11:48:41 +08:00
|
|
|
wire::{EthernetAddress, IpAddress, IpCidr},
|
2020-03-26 05:23:30 +08:00
|
|
|
},
|
2020-04-25 07:18:49 +08:00
|
|
|
time::Milliseconds,
|
2020-03-26 05:23:30 +08:00
|
|
|
};
|
2020-09-09 21:30:56 +08:00
|
|
|
#[cfg(feature = "target_zc706")]
|
|
|
|
use libboard_zynq::print;
|
2020-06-05 11:48:41 +08:00
|
|
|
use libcortex_a9::{
|
|
|
|
mutex::Mutex,
|
2020-08-20 11:57:47 +08:00
|
|
|
l2c::enable_l2_cache,
|
2020-08-03 11:24:47 +08:00
|
|
|
sync_channel::{Sender, Receiver},
|
2020-07-28 12:36:16 +08:00
|
|
|
sync_channel,
|
2020-08-03 13:24:25 +08:00
|
|
|
regs::{MPIDR, SP},
|
2020-08-04 13:50:42 +08:00
|
|
|
spin_lock_yield, notify_spin_lock,
|
2021-01-28 10:58:17 +08:00
|
|
|
asm, interrupt_handler
|
2020-06-05 11:48:41 +08:00
|
|
|
};
|
2020-08-03 13:24:25 +08:00
|
|
|
use libregister::{RegisterR, RegisterW};
|
2020-01-31 05:54:48 +08:00
|
|
|
use libsupport_zynq::{
|
2020-06-05 11:48:41 +08:00
|
|
|
boot, ram,
|
2019-12-18 07:06:10 +08:00
|
|
|
};
|
2020-06-22 08:06:11 +08:00
|
|
|
use log::{info, warn};
|
2020-08-03 13:24:25 +08:00
|
|
|
use core::sync::atomic::{AtomicBool, Ordering};
|
2019-05-05 20:56:23 +08:00
|
|
|
|
2019-07-05 06:44:53 +08:00
|
|
|
const HWADDR: [u8; 6] = [0, 0x23, 0xde, 0xea, 0xbe, 0xef];
|
|
|
|
|
2020-08-03 11:24:47 +08:00
|
|
|
static mut CORE1_REQ: (Sender<usize>, Receiver<usize>) = sync_channel!(usize, 10);
|
|
|
|
static mut CORE1_RES: (Sender<usize>, Receiver<usize>) = sync_channel!(usize, 10);
|
|
|
|
|
2020-08-03 13:24:25 +08:00
|
|
|
extern "C" {
|
|
|
|
static mut __stack1_start: u32;
|
|
|
|
}
|
|
|
|
|
|
|
|
static CORE1_RESTART: AtomicBool = AtomicBool::new(false);
|
|
|
|
|
2021-01-28 11:57:52 +08:00
|
|
|
interrupt_handler!(IRQ, irq, __irq_stack0_start, __irq_stack1_start, {
|
2020-08-03 13:24:25 +08:00
|
|
|
if MPIDR.read().cpu_id() == 1{
|
2020-08-13 13:39:04 +08:00
|
|
|
let mpcore = mpcore::RegisterBlock::mpcore();
|
2020-08-12 16:27:17 +08:00
|
|
|
let mut gic = gic::InterruptController::gic(mpcore);
|
2020-08-03 13:24:25 +08:00
|
|
|
let id = gic.get_interrupt_id();
|
|
|
|
if id.0 == 0 {
|
|
|
|
gic.end_interrupt(id);
|
|
|
|
asm::exit_irq();
|
|
|
|
SP.write(&mut __stack1_start as *mut _ as u32);
|
|
|
|
asm::enable_irq();
|
|
|
|
CORE1_RESTART.store(false, Ordering::Relaxed);
|
2020-08-04 13:50:42 +08:00
|
|
|
notify_spin_lock();
|
2020-08-03 13:24:25 +08:00
|
|
|
main_core1();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
stdio::drop_uart();
|
|
|
|
println!("IRQ");
|
|
|
|
loop {}
|
2021-01-28 10:58:17 +08:00
|
|
|
});
|
2020-08-03 13:24:25 +08:00
|
|
|
|
|
|
|
pub fn restart_core1() {
|
2020-08-13 13:39:04 +08:00
|
|
|
let mut interrupt_controller = gic::InterruptController::gic(mpcore::RegisterBlock::mpcore());
|
2020-08-03 13:24:25 +08:00
|
|
|
CORE1_RESTART.store(true, Ordering::Relaxed);
|
|
|
|
interrupt_controller.send_sgi(gic::InterruptId(0), gic::CPUCore::Core1.into());
|
|
|
|
while CORE1_RESTART.load(Ordering::Relaxed) {
|
2020-08-04 13:50:42 +08:00
|
|
|
spin_lock_yield();
|
2020-08-03 13:24:25 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-18 07:06:10 +08:00
|
|
|
#[no_mangle]
|
|
|
|
pub fn main_core0() {
|
2019-12-17 08:07:46 +08:00
|
|
|
// zynq::clocks::CpuClocks::enable_io(1_250_000_000);
|
2021-01-26 12:53:26 +08:00
|
|
|
enable_l2_cache(0x8);
|
2020-09-09 21:29:25 +08:00
|
|
|
println!("\nZynq experiments");
|
2020-08-13 13:39:04 +08:00
|
|
|
let mut interrupt_controller = gic::InterruptController::gic(mpcore::RegisterBlock::mpcore());
|
2020-08-03 13:24:25 +08:00
|
|
|
interrupt_controller.enable_interrupts();
|
2020-04-21 05:40:01 +08:00
|
|
|
|
2020-05-01 07:33:00 +08:00
|
|
|
libboard_zynq::logger::init().unwrap();
|
2020-04-21 05:40:01 +08:00
|
|
|
log::set_max_level(log::LevelFilter::Trace);
|
|
|
|
|
2020-06-05 11:48:41 +08:00
|
|
|
info!(
|
|
|
|
"Boot mode: {:?}",
|
2020-08-13 13:39:04 +08:00
|
|
|
zynq::slcr::RegisterBlock::slcr()
|
2020-06-05 11:48:41 +08:00
|
|
|
.boot_mode
|
|
|
|
.read()
|
|
|
|
.boot_mode_pins()
|
|
|
|
);
|
2019-12-10 09:50:44 +08:00
|
|
|
|
2020-11-20 02:41:38 +08:00
|
|
|
#[cfg(any(
|
|
|
|
feature = "target_zc706",
|
|
|
|
feature = "target_redpitaya",
|
|
|
|
feature = "target_kasli_soc",
|
|
|
|
))]
|
2020-01-24 05:44:10 +08:00
|
|
|
const CPU_FREQ: u32 = 800_000_000;
|
2020-11-14 00:56:47 +08:00
|
|
|
#[cfg(feature = "target_coraz7")]
|
2020-01-24 05:44:10 +08:00
|
|
|
const CPU_FREQ: u32 = 650_000_000;
|
|
|
|
|
2020-05-01 07:45:52 +08:00
|
|
|
info!("Setup clock sources...");
|
2020-01-24 05:44:10 +08:00
|
|
|
ArmPll::setup(2 * CPU_FREQ);
|
|
|
|
Clocks::set_cpu_freq(CPU_FREQ);
|
2020-11-20 02:41:38 +08:00
|
|
|
IoPll::setup(1_000_000_000);
|
|
|
|
libboard_zynq::stdio::drop_uart();
|
2020-05-01 07:45:52 +08:00
|
|
|
info!("PLLs set up");
|
2020-01-24 05:44:10 +08:00
|
|
|
let clocks = zynq::clocks::Clocks::get();
|
2020-06-05 11:48:41 +08:00
|
|
|
info!(
|
|
|
|
"CPU Clocks: {}/{}/{}/{}",
|
|
|
|
clocks.cpu_6x4x(),
|
|
|
|
clocks.cpu_3x2x(),
|
|
|
|
clocks.cpu_2x(),
|
|
|
|
clocks.cpu_1x()
|
|
|
|
);
|
2020-01-24 05:44:10 +08:00
|
|
|
|
2020-04-25 08:59:48 +08:00
|
|
|
let timer = libboard_zynq::timer::GlobalTimer::start();
|
2020-04-25 07:18:49 +08:00
|
|
|
|
2020-08-12 16:27:17 +08:00
|
|
|
let mut ddr = zynq::ddr::DdrRam::ddrram();
|
2019-12-17 08:07:46 +08:00
|
|
|
#[cfg(not(feature = "target_zc706"))]
|
2019-10-26 05:19:34 +08:00
|
|
|
ddr.memtest();
|
2020-04-27 10:06:55 +08:00
|
|
|
ram::init_alloc_ddr(&mut ddr);
|
2019-10-31 08:41:10 +08:00
|
|
|
|
2020-08-03 11:24:47 +08:00
|
|
|
boot::Core1::start(false);
|
2019-12-17 08:07:46 +08:00
|
|
|
|
2020-08-03 11:24:47 +08:00
|
|
|
let core1_req = unsafe { &mut CORE1_REQ.0 };
|
|
|
|
let core1_res = unsafe { &mut CORE1_RES.1 };
|
2020-04-13 07:24:37 +08:00
|
|
|
task::block_on(async {
|
|
|
|
for i in 0..10 {
|
2020-08-03 13:24:25 +08:00
|
|
|
restart_core1();
|
2020-04-13 07:24:37 +08:00
|
|
|
core1_req.async_send(i).await;
|
|
|
|
let j = core1_res.async_recv().await;
|
|
|
|
println!("{} -> {}", i, j);
|
2020-04-09 08:49:24 +08:00
|
|
|
}
|
2020-04-13 07:24:37 +08:00
|
|
|
});
|
2020-08-05 15:29:28 +08:00
|
|
|
unsafe {
|
|
|
|
core1_req.drop_elements();
|
|
|
|
}
|
2020-08-20 11:57:47 +08:00
|
|
|
|
2020-08-07 10:32:37 +08:00
|
|
|
// Test I2C
|
2020-08-10 11:49:21 +08:00
|
|
|
#[cfg(feature = "target_zc706")]
|
|
|
|
{
|
2020-08-12 16:27:17 +08:00
|
|
|
let mut i2c = zynq::i2c::I2c::i2c0();
|
2020-09-06 00:17:59 +08:00
|
|
|
i2c.init().unwrap();
|
2020-08-10 11:49:21 +08:00
|
|
|
println!("I2C bit-banging enabled");
|
|
|
|
let mut eeprom = zynq::i2c::eeprom::EEPROM::new(&mut i2c, 16);
|
|
|
|
// Write to 0x00 and 0x08
|
|
|
|
let eeprom_buffer: [u8; 22] = [
|
2020-08-20 11:57:47 +08:00
|
|
|
0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb,
|
|
|
|
0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee,
|
2020-08-10 11:49:21 +08:00
|
|
|
0xef, 0xcd, 0xab, 0x89, 0x67, 0x45, 0x23, 0x01,
|
|
|
|
];
|
2020-09-06 00:17:59 +08:00
|
|
|
eeprom.write(0x00, &eeprom_buffer[0..6]).unwrap();
|
|
|
|
eeprom.write(0x08, &eeprom_buffer[6..22]).unwrap();
|
2020-08-10 11:49:21 +08:00
|
|
|
println!("Data written to EEPROM");
|
|
|
|
let mut eeprom_buffer = [0u8; 24];
|
|
|
|
// Read from 0x00
|
2020-09-06 00:17:59 +08:00
|
|
|
eeprom.read(0x00, &mut eeprom_buffer).unwrap();
|
2020-08-10 11:49:21 +08:00
|
|
|
print!("Data read from EEPROM @ 0x00: (hex) ");
|
|
|
|
for i in 0..6 {
|
|
|
|
print!("{:02x} ", eeprom_buffer[i]);
|
|
|
|
}
|
|
|
|
println!("");
|
|
|
|
// Read from 0x08
|
2020-09-06 00:17:59 +08:00
|
|
|
eeprom.read(0x08, &mut eeprom_buffer).unwrap();
|
2020-08-10 11:49:21 +08:00
|
|
|
print!("Data read from EEPROM @ 0x08: (hex) ");
|
|
|
|
for i in 0..16 {
|
|
|
|
print!("{:02x} ", eeprom_buffer[i]);
|
|
|
|
}
|
|
|
|
println!("");
|
2020-08-07 10:32:37 +08:00
|
|
|
}
|
2019-12-17 08:07:46 +08:00
|
|
|
|
2022-08-26 11:43:23 +08:00
|
|
|
#[cfg(feature = "target_kasli_soc")]
|
|
|
|
{
|
|
|
|
let mut err_cdwn = timer.countdown();
|
|
|
|
let mut err_state = true;
|
|
|
|
let mut led = zynq::error_led::ErrorLED::error_led();
|
|
|
|
task::spawn( async move {
|
|
|
|
loop {
|
|
|
|
led.toggle(err_state);
|
|
|
|
err_state = !err_state;
|
|
|
|
delay(&mut err_cdwn, Milliseconds(1000)).await;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-08-12 16:27:17 +08:00
|
|
|
let eth = zynq::eth::Eth::eth0(HWADDR.clone());
|
2019-06-20 06:30:18 +08:00
|
|
|
println!("Eth on");
|
2019-05-08 01:28:33 +08:00
|
|
|
|
2020-06-22 08:06:11 +08:00
|
|
|
const RX_LEN: usize = 4096;
|
2019-09-29 07:39:57 +08:00
|
|
|
// Number of transmission buffers (minimum is two because with
|
|
|
|
// one, duplicate packet transmission occurs)
|
2020-06-22 08:06:11 +08:00
|
|
|
const TX_LEN: usize = 4096;
|
2020-06-11 05:20:43 +08:00
|
|
|
let eth = eth.start_rx(RX_LEN);
|
|
|
|
let mut eth = eth.start_tx(TX_LEN);
|
2020-05-01 07:17:53 +08:00
|
|
|
|
2019-07-05 06:44:53 +08:00
|
|
|
let ethernet_addr = EthernetAddress(HWADDR);
|
|
|
|
// IP stack
|
2019-11-13 23:02:56 +08:00
|
|
|
let local_addr = IpAddress::v4(192, 168, 1, 51);
|
2019-07-05 06:44:53 +08:00
|
|
|
let mut ip_addrs = [IpCidr::new(local_addr, 24)];
|
2020-05-01 07:17:53 +08:00
|
|
|
let routes = Routes::new(BTreeMap::new());
|
|
|
|
let neighbor_cache = NeighborCache::new(BTreeMap::new());
|
2019-07-05 06:44:53 +08:00
|
|
|
let mut iface = EthernetInterfaceBuilder::new(&mut eth)
|
|
|
|
.ethernet_addr(ethernet_addr)
|
|
|
|
.ip_addrs(&mut ip_addrs[..])
|
2020-03-31 07:16:58 +08:00
|
|
|
.routes(routes)
|
2019-07-05 06:44:53 +08:00
|
|
|
.neighbor_cache(neighbor_cache)
|
|
|
|
.finalize();
|
2020-03-31 07:16:58 +08:00
|
|
|
|
|
|
|
Sockets::init(32);
|
2020-04-01 04:34:32 +08:00
|
|
|
|
2020-07-19 15:34:32 +08:00
|
|
|
const TCP_PORT: u16 = 19;
|
2020-06-22 08:06:11 +08:00
|
|
|
// (rx, tx)
|
|
|
|
let stats = alloc::rc::Rc::new(core::cell::RefCell::new((0, 0)));
|
|
|
|
let stats_tx = stats.clone();
|
2020-04-17 02:28:40 +08:00
|
|
|
task::spawn(async move {
|
2020-06-22 08:06:11 +08:00
|
|
|
while let Ok(stream) = TcpStream::accept(TCP_PORT, 0x10_0000, 0x10_0000).await {
|
|
|
|
let stats_tx = stats_tx.clone();
|
2020-04-17 02:28:40 +08:00
|
|
|
task::spawn(async move {
|
2021-01-15 16:47:31 +08:00
|
|
|
let tx_data = (0..=255).cycle().take(4096).collect::<alloc::vec::Vec<u8>>();
|
2020-06-22 08:06:11 +08:00
|
|
|
loop {
|
|
|
|
// const CHUNK_SIZE: usize = 65536;
|
|
|
|
// match stream.send((0..=255).cycle().take(CHUNK_SIZE)).await {
|
|
|
|
match stream.send_slice(&tx_data[..]).await {
|
2020-11-20 03:27:05 +08:00
|
|
|
Ok(_len) => stats_tx.borrow_mut().1 += tx_data.len(), //CHUNK_SIZE,
|
2020-06-22 08:06:11 +08:00
|
|
|
Err(e) => {
|
|
|
|
warn!("tx: {:?}", e);
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
let stats_rx = stats.clone();
|
|
|
|
task::spawn(async move {
|
|
|
|
while let Ok(stream) = TcpStream::accept(TCP_PORT+1, 0x10_0000, 0x10_0000).await {
|
|
|
|
let stats_rx = stats_rx.clone();
|
|
|
|
task::spawn(async move {
|
|
|
|
loop {
|
2020-07-19 15:34:32 +08:00
|
|
|
match stream.recv(|buf| (buf.len(), buf.len())).await {
|
2020-06-22 08:06:11 +08:00
|
|
|
Ok(len) => stats_rx.borrow_mut().0 += len,
|
|
|
|
Err(e) => {
|
|
|
|
warn!("rx: {:?}", e);
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-04-17 02:28:40 +08:00
|
|
|
});
|
|
|
|
}
|
2020-03-31 07:16:58 +08:00
|
|
|
});
|
2019-12-17 08:07:46 +08:00
|
|
|
|
2020-04-25 07:18:49 +08:00
|
|
|
let mut countdown = timer.countdown();
|
|
|
|
task::spawn(async move {
|
|
|
|
loop {
|
|
|
|
delay(&mut countdown, Milliseconds(1000)).await;
|
2020-04-25 09:01:19 +08:00
|
|
|
|
2020-07-23 05:47:57 +08:00
|
|
|
let timestamp = timer.get_us().0;
|
2020-06-05 11:48:41 +08:00
|
|
|
let seconds = timestamp / 1_000_000;
|
|
|
|
let micros = timestamp % 1_000_000;
|
2020-06-22 08:06:11 +08:00
|
|
|
let (rx, tx) = {
|
|
|
|
let mut stats = stats.borrow_mut();
|
|
|
|
let result = *stats;
|
|
|
|
*stats = (0, 0);
|
|
|
|
result
|
|
|
|
};
|
|
|
|
info!("time: {:6}.{:06}s, rx: {}k/s, tx: {}k/s", seconds, micros, rx / 1024, tx / 1024);
|
2020-04-25 07:18:49 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-04-03 06:18:04 +08:00
|
|
|
Sockets::run(&mut iface, || {
|
2020-04-25 07:18:49 +08:00
|
|
|
Instant::from_millis(timer.get_time().0 as i64)
|
2020-04-17 02:42:21 +08:00
|
|
|
})
|
2019-05-28 06:28:35 +08:00
|
|
|
}
|
2019-05-31 02:30:19 +08:00
|
|
|
|
2019-12-17 08:07:46 +08:00
|
|
|
static DONE: Mutex<bool> = Mutex::new(false);
|
|
|
|
|
2019-12-18 07:06:10 +08:00
|
|
|
#[no_mangle]
|
2019-11-16 07:21:57 +08:00
|
|
|
pub fn main_core1() {
|
2019-11-21 00:00:57 +08:00
|
|
|
println!("Hello from core1!");
|
2020-08-13 13:39:04 +08:00
|
|
|
let mut interrupt_controller = gic::InterruptController::gic(mpcore::RegisterBlock::mpcore());
|
2020-08-03 11:24:47 +08:00
|
|
|
interrupt_controller.enable_interrupts();
|
|
|
|
let req = unsafe { &mut CORE1_REQ.1 };
|
|
|
|
let res = unsafe { &mut CORE1_RES.0 };
|
2020-04-09 08:49:24 +08:00
|
|
|
|
2020-04-13 07:24:37 +08:00
|
|
|
for i in req {
|
2020-07-28 12:36:16 +08:00
|
|
|
res.send(i * i);
|
2019-12-17 08:07:46 +08:00
|
|
|
}
|
2020-04-09 08:49:24 +08:00
|
|
|
|
2019-12-17 08:07:46 +08:00
|
|
|
println!("core1 done!");
|
|
|
|
*DONE.lock() = true;
|
|
|
|
|
2019-11-16 07:21:57 +08:00
|
|
|
loop {}
|
|
|
|
}
|