artiq-zynq/src/runtime/src/main.rs

155 lines
4.2 KiB
Rust
Raw Normal View History

2020-04-11 20:19:39 +08:00
#![no_std]
#![no_main]
#![recursion_limit = "1024"] // for futures_util::select!
#![feature(alloc_error_handler)]
2020-07-25 12:12:56 +08:00
#![feature(const_btree_new)]
#![feature(panic_info_message)]
2020-04-11 20:19:39 +08:00
#[macro_use]
2020-04-11 20:19:39 +08:00
extern crate alloc;
#[cfg(all(feature = "target_kasli_soc", has_virtual_leds))]
use core::cell::RefCell;
2023-09-05 16:21:39 +08:00
use ksupport;
use libasync::task;
2023-10-11 17:56:30 +08:00
#[cfg(has_drtio_eem)]
use libboard_artiq::drtio_eem;
#[cfg(feature = "target_kasli_soc")]
use libboard_artiq::io_expander;
use libboard_artiq::{identifier_read, logger, pl};
use libboard_zynq::{gic, mpcore, timer::GlobalTimer};
use libconfig::Config;
use libcortex_a9::l2c::enable_l2_cache;
use libsupport_zynq::{exception_vectors, ram};
use log::{info, warn};
mod analyzer;
2020-04-13 13:48:08 +08:00
mod comms;
mod mgmt;
mod moninj;
mod panic;
mod proto_async;
mod rpc_async;
mod rtio_clocking;
mod rtio_dma;
2023-03-27 15:53:32 +08:00
mod rtio_mgt;
2023-08-31 17:36:01 +08:00
#[cfg(has_drtio)]
mod subkernel;
// linker symbols
extern "C" {
static __exceptions_start: u32;
}
#[cfg(all(feature = "target_kasli_soc", has_virtual_leds))]
async fn io_expanders_service(
i2c_bus: RefCell<&mut libboard_zynq::i2c::I2c>,
io_expander0: RefCell<io_expander::IoExpander>,
io_expander1: RefCell<io_expander::IoExpander>,
) {
loop {
task::r#yield().await;
io_expander0
.borrow_mut()
.service(&mut i2c_bus.borrow_mut())
.expect("I2C I/O expander #0 service failed");
io_expander1
.borrow_mut()
.service(&mut i2c_bus.borrow_mut())
.expect("I2C I/O expander #1 service failed");
}
}
#[cfg(has_grabber)]
mod grabber {
use libasync::delay;
use libboard_artiq::grabber;
use libboard_zynq::time::Milliseconds;
2023-10-18 11:56:38 +08:00
use crate::GlobalTimer;
pub async fn grabber_thread(timer: GlobalTimer) {
let mut countdown = timer.countdown();
loop {
grabber::tick();
delay(&mut countdown, Milliseconds(200)).await;
}
}
}
static mut LOG_BUFFER: [u8; 1 << 17] = [0; 1 << 17];
2020-07-13 14:59:56 +08:00
#[no_mangle]
pub fn main_core0() {
unsafe {
exception_vectors::set_vector_table(&__exceptions_start as *const u32 as u32);
}
2021-01-26 12:38:09 +08:00
enable_l2_cache(0x8);
let mut timer = GlobalTimer::start();
2020-07-13 14:59:56 +08:00
let buffer_logger = unsafe { logger::BufferLogger::new(&mut LOG_BUFFER[..]) };
2020-09-01 17:11:21 +08:00
buffer_logger.set_uart_log_level(log::LevelFilter::Info);
2020-07-13 14:59:56 +08:00
buffer_logger.register();
2020-09-01 17:11:21 +08:00
log::set_max_level(log::LevelFilter::Info);
2020-07-13 14:59:56 +08:00
info!("NAR3/Zynq7000 starting...");
ram::init_alloc_core0();
2020-08-18 01:17:15 +08:00
gic::InterruptController::gic(mpcore::RegisterBlock::mpcore()).enable_interrupts();
2022-10-21 12:08:11 +08:00
info!("gateware ident: {}", identifier_read(&mut [0; 64]));
2020-09-06 00:11:19 +08:00
2023-09-05 16:21:39 +08:00
ksupport::i2c::init();
#[cfg(feature = "target_kasli_soc")]
{
let i2c_bus = unsafe { (ksupport::i2c::I2C_BUS).as_mut().unwrap() };
let mut io_expander0 = io_expander::IoExpander::new(i2c_bus, 0).unwrap();
let mut io_expander1 = io_expander::IoExpander::new(i2c_bus, 1).unwrap();
io_expander0
.init(i2c_bus)
.expect("I2C I/O expander #0 initialization failed");
io_expander1
.init(i2c_bus)
.expect("I2C I/O expander #1 initialization failed");
// Drive CLK_SEL to true
#[cfg(has_si549)]
io_expander0.set(1, 7, true);
// Drive TX_DISABLE to false on SFP0..3
io_expander0.set(0, 1, false);
io_expander1.set(0, 1, false);
io_expander0.set(1, 1, false);
io_expander1.set(1, 1, false);
io_expander0.service(i2c_bus).unwrap();
io_expander1.service(i2c_bus).unwrap();
#[cfg(has_virtual_leds)]
task::spawn(io_expanders_service(
RefCell::new(i2c_bus),
RefCell::new(io_expander0),
RefCell::new(io_expander1),
));
}
2020-09-01 14:43:16 +08:00
let cfg = match Config::new() {
2020-07-08 19:24:26 +08:00
Ok(cfg) => cfg,
Err(err) => {
warn!("config initialization failed: {}", err);
2020-09-01 14:43:16 +08:00
Config::new_dummy()
2020-07-08 19:24:26 +08:00
}
};
rtio_clocking::init(&mut timer, &cfg);
#[cfg(has_drtio_eem)]
drtio_eem::init(&mut timer, &cfg);
#[cfg(has_grabber)]
task::spawn(grabber::grabber_thread(timer));
2023-09-05 16:21:39 +08:00
task::spawn(ksupport::report_async_rtio_errors());
comms::main(timer, cfg);
2020-04-11 20:19:39 +08:00
}