mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-19 09:00:54 +00:00
resource tracking implementation
This commit is contained in:
parent
8d84345444
commit
35cdc0c0f5
2 changed files with 121 additions and 10 deletions
|
|
@ -1,6 +1,5 @@
|
||||||
use std::marker::PhantomData;
|
|
||||||
|
|
||||||
use bao1x_api::bio::*;
|
use bao1x_api::bio::*;
|
||||||
|
use bao1x_api::bio_resources::*;
|
||||||
use num_traits::*;
|
use num_traits::*;
|
||||||
use rkyv::option::ArchivedOption;
|
use rkyv::option::ArchivedOption;
|
||||||
use utralib::*;
|
use utralib::*;
|
||||||
|
|
@ -19,15 +18,14 @@ impl Bio {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct CoreCsr<'a> {
|
pub struct CoreCsr {
|
||||||
pub csr: CSR<u32>,
|
pub csr: CSR<u32>,
|
||||||
_lifetime: PhantomData<&'a ()>,
|
|
||||||
}
|
}
|
||||||
impl<'a> CoreCsr<'a> {
|
impl CoreCsr {
|
||||||
pub fn from_handle(handle: CoreHandle<'a>) -> Self {
|
pub fn from_handle(handle: &CoreHandle) -> Self {
|
||||||
let (pointer, lifetime) = unsafe { handle.handle() };
|
let pointer = unsafe { handle.handle() };
|
||||||
// safety: this structure tracks the lifetime of the handle, and is therefore safe.
|
// safety: this structure tracks the lifetime of the handle, and is therefore safe.
|
||||||
Self { csr: CSR::new(pointer as *mut u32), _lifetime: lifetime }
|
Self { csr: CSR::new(pointer as *mut u32) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -81,7 +79,7 @@ impl<'a> BioApi<'a> for Bio {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe fn get_core_handle(&'a self, fifo: Fifo) -> Result<Option<CoreHandle<'a>>, BioError> {
|
unsafe fn get_core_handle(&self, fifo: Fifo) -> Result<Option<CoreHandle>, BioError> {
|
||||||
match send_message(
|
match send_message(
|
||||||
self.conn,
|
self.conn,
|
||||||
Message::new_blocking_scalar(BioOp::GetCoreHandle.to_usize().unwrap(), fifo as usize, 0, 0, 0),
|
Message::new_blocking_scalar(BioOp::GetCoreHandle.to_usize().unwrap(), fifo as usize, 0, 0, 0),
|
||||||
|
|
@ -221,3 +219,116 @@ impl<'a> BioApi<'a> for Bio {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "std")]
|
||||||
|
impl BioResources for Bio {
|
||||||
|
fn claim_resources(&self, spec: &ResourceSpec) -> Result<ResourceGrant, ResourceError> {
|
||||||
|
let request = ClaimResourcesRequest { spec: spec.clone(), grant: None, error: ResourceError::None };
|
||||||
|
let mut buf = Buffer::into_buf(request).unwrap();
|
||||||
|
buf.lend_mut(self.conn, BioOp::ClaimResources.to_u32().unwrap())
|
||||||
|
.map_err(|_| ResourceError::InternalError)?;
|
||||||
|
let response = buf.to_original::<ClaimResourcesRequest, _>().unwrap();
|
||||||
|
match response.error {
|
||||||
|
ResourceError::None => response.grant.ok_or(ResourceError::InternalError),
|
||||||
|
e => Err(e),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn release_resources(&self, grant_id: u32) -> Result<(), ResourceError> {
|
||||||
|
match send_message(
|
||||||
|
self.conn,
|
||||||
|
Message::new_blocking_scalar(
|
||||||
|
BioOp::ReleaseResources.to_usize().unwrap(),
|
||||||
|
grant_id as usize,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
),
|
||||||
|
) {
|
||||||
|
Ok(xous::Result::Scalar5(_, result, _, _, _)) => {
|
||||||
|
if result == 0 {
|
||||||
|
Ok(())
|
||||||
|
} else {
|
||||||
|
Err(ResourceError::InvalidGrantId(grant_id))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => Err(ResourceError::InternalError),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn resource_availability(&self) -> Result<ResourceAvailability, BioError> {
|
||||||
|
let response = ResourceAvailabilityResponse { availability: ResourceAvailability::default() };
|
||||||
|
let mut buf = Buffer::into_buf(response).unwrap();
|
||||||
|
buf.lend_mut(self.conn, BioOp::ResourceAvailability.to_u32().unwrap())
|
||||||
|
.map_err(|e| <xous::Error as Into<BioError>>::into(e))?;
|
||||||
|
let response = buf.to_original::<ResourceAvailabilityResponse, _>().unwrap();
|
||||||
|
Ok(response.availability)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn check_resources(&self, spec: &ResourceSpec) -> Result<(), ResourceError> {
|
||||||
|
let request = ClaimResourcesRequest { spec: spec.clone(), grant: None, error: ResourceError::None };
|
||||||
|
let mut buf = Buffer::into_buf(request).unwrap();
|
||||||
|
buf.lend_mut(self.conn, BioOp::CheckResources.to_u32().unwrap())
|
||||||
|
.map_err(|_| ResourceError::InternalError)?;
|
||||||
|
let response = buf.to_original::<ClaimResourcesRequest, _>().unwrap();
|
||||||
|
match response.error {
|
||||||
|
ResourceError::None => Ok(()),
|
||||||
|
e => Err(e),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn check_resources_batch(&self, specs: &[ResourceSpec]) -> Result<(), ResourceError> {
|
||||||
|
let request = CheckResourcesBatchRequest { specs: specs.to_vec(), error: ResourceError::None };
|
||||||
|
let mut buf = Buffer::into_buf(request).unwrap();
|
||||||
|
buf.lend_mut(self.conn, BioOp::CheckResourcesBatch.to_u32().unwrap())
|
||||||
|
.map_err(|_| ResourceError::InternalError)?;
|
||||||
|
let response = buf.to_original::<CheckResourcesBatchRequest, _>().unwrap();
|
||||||
|
match response.error {
|
||||||
|
ResourceError::None => Ok(()),
|
||||||
|
e => Err(e),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn claim_dynamic_pin(&self, pin: u8, claimer: &str) -> Result<(), ResourceError> {
|
||||||
|
let request = DynamicPinRequest { pin, claimer: claimer.to_string(), error: ResourceError::None };
|
||||||
|
let mut buf = Buffer::into_buf(request).unwrap();
|
||||||
|
buf.lend_mut(self.conn, BioOp::ClaimDynamicPin.to_u32().unwrap())
|
||||||
|
.map_err(|_| ResourceError::InternalError)?;
|
||||||
|
let response = buf.to_original::<DynamicPinRequest, _>().unwrap();
|
||||||
|
match response.error {
|
||||||
|
ResourceError::None => Ok(()),
|
||||||
|
e => Err(e),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn release_dynamic_pin(&self, pin: u8, claimer: &str) -> Result<(), ResourceError> {
|
||||||
|
let request = DynamicPinRequest { pin, claimer: claimer.to_string(), error: ResourceError::None };
|
||||||
|
let mut buf = Buffer::into_buf(request).unwrap();
|
||||||
|
buf.lend_mut(self.conn, BioOp::ReleaseDynamicPin.to_u32().unwrap())
|
||||||
|
.map_err(|_| ResourceError::InternalError)?;
|
||||||
|
let response = buf.to_original::<DynamicPinRequest, _>().unwrap();
|
||||||
|
match response.error {
|
||||||
|
ResourceError::None => Ok(()),
|
||||||
|
e => Err(e),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn set_core_run_state(&self, grant: &ResourceGrant, start: bool) {
|
||||||
|
let mut run_states = [CoreRunSetting::Unchanged; 4];
|
||||||
|
for core in grant.cores.iter() {
|
||||||
|
run_states[*core as usize] = if start { CoreRunSetting::Start } else { CoreRunSetting::Stop };
|
||||||
|
}
|
||||||
|
|
||||||
|
send_message(
|
||||||
|
self.conn,
|
||||||
|
Message::new_blocking_scalar(
|
||||||
|
BioOp::CoreState.to_usize().unwrap(),
|
||||||
|
run_states[0] as usize,
|
||||||
|
run_states[1] as usize,
|
||||||
|
run_states[2] as usize,
|
||||||
|
run_states[3] as usize,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -453,7 +453,7 @@ impl<'a> BioApi<'a> for BioSharedState {
|
||||||
prev_freq
|
prev_freq
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe fn get_core_handle(&'a self, _fifo: Fifo) -> Result<Option<CoreHandle<'a>>, BioError> {
|
unsafe fn get_core_handle(&self, _fifo: Fifo) -> Result<Option<CoreHandle>, BioError> {
|
||||||
unimplemented!("This is managed by the main loop server, not the hardware interface");
|
unimplemented!("This is managed by the main loop server, not the hardware interface");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue