satman: replaced clock with GlobalTimer completely

This commit is contained in:
mwojcik 2021-07-23 13:15:07 +02:00
parent 5e76f7c0b4
commit 0502737481
3 changed files with 40 additions and 37 deletions

View File

@ -2,11 +2,13 @@ use core::slice;
use crc; use crc;
use io::{ProtoRead, ProtoWrite, Cursor, Error as IoError}; use io::{ProtoRead, ProtoWrite, Cursor, Error as IoError};
use board_misoc::{csr::DRTIOAUX, mem::DRTIOAUX_MEM}; // <- port //use board_misoc::{mem::DRTIOAUX_MEM}; // <- port
use proto_artiq::drtioaux_proto::Error as ProtocolError; //^ uses generated files (like csr, but mem) - todo check after initial generation
use pl::csr::DRTIOAUX;
use drtioaux_proto::Error as ProtocolError;
use libboard_zynq::{timer::GlobalTimer, time::Milliseconds}; use libboard_zynq::{timer::GlobalTimer, time::Milliseconds};
pub use proto_artiq::drtioaux_proto::Packet; pub use drtioaux_proto::Packet;
// this is parametric over T because there's no impl Fail for !. // this is parametric over T because there's no impl Fail for !.
#[derive(Fail, Debug)] #[derive(Fail, Debug)]

View File

@ -5,12 +5,12 @@
extern crate log; extern crate log;
use core::convert::TryFrom; use core::convert::TryFrom;
use board_misoc::{csr, irq, ident, clock, i2c}; // <- port, use libboard_zynq use board_misoc::{csr, irq, ident, i2c}; // <- port, use libboard_zynq
use libboard_zynq::timer::GlobalTimer; use libboard_zynq::timer::GlobalTimer;
#[cfg(has_si5324)] #[cfg(has_si5324)]
use libboard_artiqzynq::si5324; use libboard_artiq::si5324;
use board_artiq::spi; // <- port?, use libboard_zynq (if spi available/necessary) use board_artiq::spi; // <- port?, use libboard_zynq (if spi available/necessary)
use libboard_artiqzynq::{drtio_routing, drtioaux, logger}; use libboard_artiq::{drtio_routing, drtioaux, logger};
mod repeater; mod repeater;
@ -357,11 +357,11 @@ fn drtiosat_process_errors() {
#[cfg(has_rtio_crg)] #[cfg(has_rtio_crg)]
fn init_rtio_crg() { fn init_rtio_crg(timer: GlobalTimer) {
unsafe { unsafe {
csr::rtio_crg::pll_reset_write(0); csr::rtio_crg::pll_reset_write(0);
} }
clock::spin_us(150); timer.delay_us(150);
let locked = unsafe { csr::rtio_crg::pll_locked_read() != 0 }; let locked = unsafe { csr::rtio_crg::pll_locked_read() != 0 };
if !locked { if !locked {
error!("RTIO clock failed"); error!("RTIO clock failed");
@ -369,13 +369,11 @@ fn init_rtio_crg() {
} }
#[cfg(not(has_rtio_crg))] #[cfg(not(has_rtio_crg))]
fn init_rtio_crg() { } fn init_rtio_crg(timer: GlobalTimer) { }
fn hardware_tick(ts: &mut u64) { fn hardware_tick(ts: &mut u64, timer: GlobalTimer) {
let now = clock::get_ms(); let now = timer.get_time();
if now > *ts { if now > *ts {
#[cfg(has_grabber)]
board_artiq::grabber::tick();
*ts = now + 200; *ts = now + 200;
} }
} }
@ -476,7 +474,7 @@ pub extern fn main() -> i32 {
while !drtiosat_link_rx_up() { while !drtiosat_link_rx_up() {
drtiosat_process_errors(); drtiosat_process_errors();
for mut rep in repeaters.iter_mut() { for mut rep in repeaters.iter_mut() {
rep.service(&routing_table, rank); rep.service(&routing_table, rank, timer);
} }
#[cfg(all(soc_platform = "kasli", hw_rev = "v2.0"))] #[cfg(all(soc_platform = "kasli", hw_rev = "v2.0"))]
{ {
@ -508,11 +506,11 @@ pub extern fn main() -> i32 {
io_expander0.service().expect("I2C I/O expander #0 service failed"); io_expander0.service().expect("I2C I/O expander #0 service failed");
io_expander1.service().expect("I2C I/O expander #1 service failed"); io_expander1.service().expect("I2C I/O expander #1 service failed");
} }
hardware_tick(&mut hardware_tick_ts); hardware_tick(&mut hardware_tick_ts, timer);
if drtiosat_tsc_loaded() { if drtiosat_tsc_loaded() {
info!("TSC loaded from uplink"); info!("TSC loaded from uplink");
for rep in repeaters.iter() { for rep in repeaters.iter() {
if let Err(e) = rep.sync_tsc() { if let Err(e) = rep.sync_tsc(timer) {
error!("failed to sync TSC ({})", e); error!("failed to sync TSC ({})", e);
} }
} }

View File

@ -1,6 +1,8 @@
use board_artiqzynq::{drtioaux, drtio_routing}; use libboard_artiq::{drtioaux, drtio_routing};
#[cfg(has_drtio_routing)] #[cfg(has_drtio_routing)]
use board_artiqzynq::{pl::csr, clock}; use libboard_artiq::{pl::csr};
#[cfg(has_drtio_routing)]
use libboard_zynq::timer::{GlobalTimer, Milliseconds};
#[cfg(has_drtio_routing)] #[cfg(has_drtio_routing)]
fn rep_link_rx_up(repno: u8) -> bool { fn rep_link_rx_up(repno: u8) -> bool {
@ -48,7 +50,8 @@ impl Repeater {
self.state == RepeaterState::Up self.state == RepeaterState::Up
} }
pub fn service(&mut self, routing_table: &drtio_routing::RoutingTable, rank: u8) { pub fn service(&mut self, routing_table: &drtio_routing::RoutingTable, rank: u8,
timer: GlobalTimer) {
self.process_local_errors(); self.process_local_errors();
match self.state { match self.state {
@ -63,7 +66,7 @@ impl Repeater {
drtioaux::send(self.auxno, &drtioaux::Packet::EchoRequest).unwrap(); drtioaux::send(self.auxno, &drtioaux::Packet::EchoRequest).unwrap();
self.state = RepeaterState::WaitPingReply { self.state = RepeaterState::WaitPingReply {
ping_count: ping_count + 1, ping_count: ping_count + 1,
timeout: clock::get_ms() + 100 timeout: timer.get_time() + Milliseconds(100)
} }
} else { } else {
error!("[REP#{}] link RX went down during ping", self.repno); error!("[REP#{}] link RX went down during ping", self.repno);
@ -91,7 +94,7 @@ impl Repeater {
return; return;
} }
} else { } else {
if clock::get_ms() > timeout { if timer.get_time() > timeout {
if ping_count > 200 { if ping_count > 200 {
error!("[REP#{}] ping failed", self.repno); error!("[REP#{}] ping failed", self.repno);
self.state = RepeaterState::Failed; self.state = RepeaterState::Failed;
@ -162,13 +165,13 @@ impl Repeater {
} }
} }
fn recv_aux_timeout(&self, timeout: u32) -> Result<drtioaux::Packet, drtioaux::Error<!>> { fn recv_aux_timeout(&self, timeout: u32, timer: GlobalTimer) -> Result<drtioaux::Packet, drtioaux::Error<!>> {
let max_time = clock::get_ms() + timeout as u64; let max_time = timer.get_time() + timeout as u64;
loop { loop {
if !rep_link_rx_up(self.repno) { if !rep_link_rx_up(self.repno) {
return Err(drtioaux::Error::LinkDown); return Err(drtioaux::Error::LinkDown);
} }
if clock::get_ms() > max_time { if timer.get_time() > max_time {
return Err(drtioaux::Error::TimedOut); return Err(drtioaux::Error::TimedOut);
} }
match drtioaux::recv(self.auxno) { match drtioaux::recv(self.auxno) {
@ -179,17 +182,17 @@ impl Repeater {
} }
} }
pub fn aux_forward(&self, request: &drtioaux::Packet) -> Result<(), drtioaux::Error<!>> { pub fn aux_forward(&self, request: &drtioaux::Packet, timer: GlobalTimer) -> Result<(), drtioaux::Error<!>> {
if self.state != RepeaterState::Up { if self.state != RepeaterState::Up {
return Err(drtioaux::Error::LinkDown); return Err(drtioaux::Error::LinkDown);
} }
drtioaux::send(self.auxno, request).unwrap(); drtioaux::send(self.auxno, request).unwrap();
let reply = self.recv_aux_timeout(200)?; let reply = self.recv_aux_timeout(200, timer)?;
drtioaux::send(0, &reply).unwrap(); drtioaux::send(0, &reply).unwrap();
Ok(()) Ok(())
} }
pub fn sync_tsc(&self) -> Result<(), drtioaux::Error<!>> { pub fn sync_tsc(&self, timer: GlobalTimer) -> Result<(), drtioaux::Error<!>> {
if self.state != RepeaterState::Up { if self.state != RepeaterState::Up {
return Ok(()); return Ok(());
} }
@ -202,7 +205,7 @@ impl Repeater {
// TSCAck is the only aux packet that is sent spontaneously // TSCAck is the only aux packet that is sent spontaneously
// by the satellite, in response to a TSC set on the RT link. // by the satellite, in response to a TSC set on the RT link.
let reply = self.recv_aux_timeout(10000)?; let reply = self.recv_aux_timeout(10000, timer)?;
if reply == drtioaux::Packet::TSCAck { if reply == drtioaux::Packet::TSCAck {
return Ok(()); return Ok(());
} else { } else {
@ -210,7 +213,7 @@ impl Repeater {
} }
} }
pub fn set_path(&self, destination: u8, hops: &[u8; drtio_routing::MAX_HOPS]) -> Result<(), drtioaux::Error<!>> { pub fn set_path(&self, destination: u8, hops: &[u8; drtio_routing::MAX_HOPS], timer: GlobalTimer) -> Result<(), drtioaux::Error<!>> {
if self.state != RepeaterState::Up { if self.state != RepeaterState::Up {
return Ok(()); return Ok(());
} }
@ -219,7 +222,7 @@ impl Repeater {
destination: destination, destination: destination,
hops: *hops hops: *hops
}).unwrap(); }).unwrap();
let reply = self.recv_aux_timeout(200)?; let reply = self.recv_aux_timeout(200, timer)?;
if reply != drtioaux::Packet::RoutingAck { if reply != drtioaux::Packet::RoutingAck {
return Err(drtioaux::Error::UnexpectedReply); return Err(drtioaux::Error::UnexpectedReply);
} }
@ -233,24 +236,24 @@ impl Repeater {
Ok(()) Ok(())
} }
pub fn set_rank(&self, rank: u8) -> Result<(), drtioaux::Error<!>> { pub fn set_rank(&self, rank: u8, timer: GlobalTimer) -> Result<(), drtioaux::Error<!>> {
if self.state != RepeaterState::Up { if self.state != RepeaterState::Up {
return Ok(()); return Ok(());
} }
drtioaux::send(self.auxno, &drtioaux::Packet::RoutingSetRank { drtioaux::send(self.auxno, &drtioaux::Packet::RoutingSetRank {
rank: rank rank: rank
}).unwrap(); }).unwrap();
let reply = self.recv_aux_timeout(200)?; let reply = self.recv_aux_timeout(200, timer)?;
if reply != drtioaux::Packet::RoutingAck { if reply != drtioaux::Packet::RoutingAck {
return Err(drtioaux::Error::UnexpectedReply); return Err(drtioaux::Error::UnexpectedReply);
} }
Ok(()) Ok(())
} }
pub fn rtio_reset(&self) -> Result<(), drtioaux::Error<!>> { pub fn rtio_reset(&self, timer: GlobalTimer) -> Result<(), drtioaux::Error<!>> {
let repno = self.repno as usize; let repno = self.repno as usize;
unsafe { (csr::DRTIOREP[repno].reset_write)(1); } unsafe { (csr::DRTIOREP[repno].reset_write)(1); }
clock::spin_us(100); timer.delay_us(100);
unsafe { (csr::DRTIOREP[repno].reset_write)(0); } unsafe { (csr::DRTIOREP[repno].reset_write)(0); }
if self.state != RepeaterState::Up { if self.state != RepeaterState::Up {
@ -258,7 +261,7 @@ impl Repeater {
} }
drtioaux::send(self.auxno, &drtioaux::Packet::ResetRequest).unwrap(); drtioaux::send(self.auxno, &drtioaux::Packet::ResetRequest).unwrap();
let reply = self.recv_aux_timeout(200)?; let reply = self.recv_aux_timeout(200, timer)?;
if reply != drtioaux::Packet::ResetAck { if reply != drtioaux::Packet::ResetAck {
return Err(drtioaux::Error::UnexpectedReply); return Err(drtioaux::Error::UnexpectedReply);
} }
@ -275,9 +278,9 @@ pub struct Repeater {
impl Repeater { impl Repeater {
pub fn new(_repno: u8) -> Repeater { Repeater::default() } pub fn new(_repno: u8) -> Repeater { Repeater::default() }
pub fn service(&self, _routing_table: &drtio_routing::RoutingTable, _rank: u8) { } pub fn service(&self, _routing_table: &drtio_routing::RoutingTable, _rank: u8, timer: GlobalTimer) { }
pub fn sync_tsc(&self) -> Result<(), drtioaux::Error<!>> { Ok(()) } pub fn sync_tsc(&self) -> Result<(), drtioaux::Error<!>> { Ok(()) }
pub fn rtio_reset(&self) -> Result<(), drtioaux::Error<!>> { Ok(()) } pub fn rtio_reset(&self, timer: GlobalTimer) -> Result<(), drtioaux::Error<!>> { Ok(()) }
} }