mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-15 08:30:02 +00:00
add a "skeleton" server which is a template for other servers
This commit is contained in:
parent
4854cc98ee
commit
dcb9956bcc
5 changed files with 181 additions and 0 deletions
23
services/skeleton/Cargo.toml
Normal file
23
services/skeleton/Cargo.toml
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
[package]
|
||||
name = "codec"
|
||||
version = "0.1.0"
|
||||
authors = ["bunnie <bunnie@kosagi.com>"]
|
||||
edition = "2018"
|
||||
description = "Audio CODEC server"
|
||||
|
||||
[dependencies]
|
||||
xous = { path = "../../xous-rs" }
|
||||
heapless = "0.5.6"
|
||||
log-server = { path = "../log-server" }
|
||||
ticktimer-server = { path = "../ticktimer-server" }
|
||||
xous-names = { path = "../xous-names" }
|
||||
log = "0.4"
|
||||
num-derive = {version = "0.3", default-features = false}
|
||||
num-traits = {version = "0.2", default-features = false}
|
||||
susres = {path = "../susres"}
|
||||
|
||||
[target.'cfg(not(any(windows,unix)))'.dependencies]
|
||||
utralib = { path = "../../utralib"}
|
||||
|
||||
[features]
|
||||
default = []
|
||||
3
services/skeleton/README.md
Normal file
3
services/skeleton/README.md
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
A skeleton server, based on a snapshot of the Codec server before
|
||||
it was fleshed out. Search and replace the word "codec" with your
|
||||
server name and use it as a starting template.
|
||||
10
services/skeleton/src/api.rs
Normal file
10
services/skeleton/src/api.rs
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
pub(crate) const SERVER_NAME_CODEC: &str = "_Low-level Audio Codec Server_";
|
||||
|
||||
#[allow(dead_code)]
|
||||
#[derive(num_derive::FromPrimitive, num_derive::ToPrimitive, Debug)]
|
||||
pub(crate) enum Opcode {
|
||||
|
||||
|
||||
/// Suspend/resume callback
|
||||
SuspendResume,
|
||||
}
|
||||
25
services/skeleton/src/lib.rs
Normal file
25
services/skeleton/src/lib.rs
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
#![cfg_attr(target_os = "none", no_std)]
|
||||
|
||||
pub mod api;
|
||||
use xous::{CID, send_message};
|
||||
use num_traits::ToPrimitive;
|
||||
|
||||
pub struct Codec {
|
||||
conn: CID,
|
||||
}
|
||||
impl Codec {
|
||||
pub fn new(xns: &xous_names::XousNames) -> Result<Self, xous::Error> {
|
||||
let conn = xns.request_connection_blocking(api::SERVER_NAME_CODEC).expect("Can't connect to Codec server");
|
||||
Ok(Codec {
|
||||
conn
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for Codec {
|
||||
fn drop(&mut self) {
|
||||
// de-allocate myself. It's unsafe because we are responsible to make sure nobody else is using the connection.
|
||||
unsafe{xous::disconnect(self.conn).unwrap();}
|
||||
|
||||
}
|
||||
}
|
||||
120
services/skeleton/src/main.rs
Normal file
120
services/skeleton/src/main.rs
Normal file
|
|
@ -0,0 +1,120 @@
|
|||
#![cfg_attr(target_os = "none", no_std)]
|
||||
#![cfg_attr(target_os = "none", no_main)]
|
||||
|
||||
mod api;
|
||||
|
||||
use num_traits::FromPrimitive;
|
||||
|
||||
use log::info;
|
||||
|
||||
|
||||
#[cfg(target_os = "none")]
|
||||
mod implementation {
|
||||
use utralib::generated::*;
|
||||
// use crate::api::*;
|
||||
use log::info;
|
||||
use susres::{RegManager, RegOrField, SuspendResume};
|
||||
|
||||
pub struct Codec {
|
||||
csr: utralib::CSR<u32>,
|
||||
fifo: xous::MemoryRange,
|
||||
susres_manager: RegManager::<{utra::audio::AUDIO_NUMREGS}>,
|
||||
}
|
||||
|
||||
impl Codec {
|
||||
pub fn new() -> Codec {
|
||||
let csr = xous::syscall::map_memory(
|
||||
xous::MemoryAddress::new(utra::audio::HW_AUDIO_BASE),
|
||||
None,
|
||||
4096,
|
||||
xous::MemoryFlags::R | xous::MemoryFlags::W,
|
||||
)
|
||||
.expect("couldn't map Audio CSR range");
|
||||
let fifo = xous::syscall::map_memory(
|
||||
xous::MemoryAddress::new(utralib::HW_AUDIO_MEM),
|
||||
None,
|
||||
utralib::HW_AUDIO_MEM_LEN,
|
||||
xous::MemoryFlags::R | xous::MemoryFlags::W,
|
||||
)
|
||||
.expect("couldn't map Audio CSR range");
|
||||
|
||||
let mut codec = Codec {
|
||||
csr: CSR::new(csr.as_mut_ptr() as *mut u32),
|
||||
susres_manager: RegManager::new(csr.as_mut_ptr() as *mut u32),
|
||||
fifo,
|
||||
};
|
||||
|
||||
codec
|
||||
}
|
||||
|
||||
pub fn suspend(&mut self) {
|
||||
self.susres_manager.suspend();
|
||||
}
|
||||
pub fn resume(&mut self) {
|
||||
self.susres_manager.resume();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// a stub to try to avoid breaking hosted mode for as long as possible.
|
||||
#[cfg(not(target_os = "none"))]
|
||||
mod implementation {
|
||||
use log::info;
|
||||
|
||||
pub struct Codec {
|
||||
}
|
||||
|
||||
impl Codec {
|
||||
pub fn new() -> Codec {
|
||||
Codec {
|
||||
}
|
||||
}
|
||||
pub fn suspend(&self) {
|
||||
}
|
||||
pub fn resume(&self) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[xous::xous_main]
|
||||
fn xmain() -> ! {
|
||||
use crate::implementation::Codec;
|
||||
|
||||
log_server::init_wait().unwrap();
|
||||
log::set_max_level(log::LevelFilter::Info);
|
||||
info!("my PID is {}", xous::process::id());
|
||||
|
||||
let xns = xous_names::XousNames::new().unwrap();
|
||||
let codec_sid = xns.register_name(api::SERVER_NAME_CODEC).expect("can't register server");
|
||||
log::trace!("registered with NS -- {:?}", codec_sid);
|
||||
|
||||
let mut codec = Codec::new();
|
||||
|
||||
log::trace!("ready to accept requests");
|
||||
|
||||
// register a suspend/resume listener
|
||||
let sr_cid = xous::connect(codec_sid).expect("couldn't create suspend callback connection");
|
||||
let mut susres = susres::Susres::new(&xns, api::Opcode::SuspendResume as u32, sr_cid).expect("couldn't create suspend/resume object");
|
||||
|
||||
loop {
|
||||
let msg = xous::receive_message(codec_sid).unwrap();
|
||||
match FromPrimitive::from_usize(msg.body.id()) {
|
||||
Some(api::Opcode::SuspendResume) => xous::msg_scalar_unpack!(msg, token, _, _, _, {
|
||||
codec.suspend();
|
||||
susres.suspend_until_resume(token).expect("couldn't execute suspend/resume");
|
||||
codec.resume();
|
||||
}),
|
||||
None => {
|
||||
log::error!("couldn't convert opcode");
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
// clean up our program
|
||||
log::trace!("main loop exit, destroying servers");
|
||||
xns.unregister_server(codec_sid).unwrap();
|
||||
xous::destroy_server(codec_sid).unwrap();
|
||||
log::trace!("quitting");
|
||||
xous::terminate_process(); loop {}
|
||||
}
|
||||
Loading…
Reference in a new issue