2021-01-27 05:33:03 +00:00
|
|
|
#![cfg_attr(target_os = "none", no_std)]
|
|
|
|
|
|
|
|
|
|
/// This is the API that other servers use to call the COM. Read this code as if you
|
|
|
|
|
/// are calling these functions inside a different process.
|
|
|
|
|
pub mod api;
|
2021-04-07 18:30:57 +00:00
|
|
|
pub use api::*;
|
2021-01-27 05:33:03 +00:00
|
|
|
|
2021-04-06 16:48:48 +00:00
|
|
|
use xous::{send_message, CID, Message, msg_scalar_unpack};
|
|
|
|
|
use xous_ipc::Buffer;
|
2021-04-06 11:32:35 +00:00
|
|
|
use num_traits::{ToPrimitive, FromPrimitive};
|
2021-01-27 05:33:03 +00:00
|
|
|
|
2021-04-07 18:30:57 +00:00
|
|
|
// this hooks the response of the I2C bus
|
2021-04-06 11:32:35 +00:00
|
|
|
static mut I2C_CB: Option<fn(I2cTransaction)> = None;
|
2021-03-15 16:11:23 +00:00
|
|
|
|
2021-05-05 19:40:04 +00:00
|
|
|
use core::sync::atomic::{AtomicBool, Ordering};
|
|
|
|
|
static I2C_IN_PROGRESS_MUTEX: AtomicBool = AtomicBool::new(false);
|
|
|
|
|
static mut I2C_RX_HANDOFF: [u8; I2C_MAX_LEN] = [0; I2C_MAX_LEN]; // this is protected by the above mutex
|
|
|
|
|
|
|
|
|
|
fn sync_i2c_cb(transaction: I2cTransaction) {
|
|
|
|
|
if let Some(rxbuf) = transaction.rxbuf {
|
|
|
|
|
unsafe {
|
|
|
|
|
for i in 0..transaction.rxlen as usize {
|
|
|
|
|
I2C_RX_HANDOFF[i] = rxbuf[i];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-05-05 20:16:31 +00:00
|
|
|
unsafe{I2C_CB = None;} // break-before-make, ensures that the I2C_RX_HANDOFF data can't be overwritten by another callback
|
2021-05-05 19:40:04 +00:00
|
|
|
I2C_IN_PROGRESS_MUTEX.store(false, Ordering::Relaxed);
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-06 11:32:35 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
|
pub struct Llio {
|
|
|
|
|
conn: CID,
|
2021-05-05 16:45:14 +00:00
|
|
|
i2c_conn: CID,
|
2021-04-06 11:32:35 +00:00
|
|
|
com_sid: Option<xous::SID>,
|
|
|
|
|
i2c_sid: Option<xous::SID>,
|
|
|
|
|
rtc_sid: Option<xous::SID>,
|
|
|
|
|
usb_sid: Option<xous::SID>,
|
2021-04-07 10:29:50 +00:00
|
|
|
gpio_sid: Option<xous::SID>,
|
2021-05-05 19:40:04 +00:00
|
|
|
i2c_timeout_ms: u32,
|
2021-03-15 16:11:23 +00:00
|
|
|
}
|
2021-04-06 11:32:35 +00:00
|
|
|
impl Llio {
|
|
|
|
|
pub fn new(xns: &xous_names::XousNames) -> Result<Self, xous::Error> {
|
2021-05-21 08:24:30 +00:00
|
|
|
REFCOUNT.store(REFCOUNT.load(Ordering::Relaxed) + 1, Ordering::Relaxed);
|
2021-04-26 16:46:24 +00:00
|
|
|
let conn = xns.request_connection_blocking(api::SERVER_NAME_LLIO).expect("Can't connect to LLIO");
|
2021-05-05 16:45:14 +00:00
|
|
|
let i2c_conn = xns.request_connection_blocking(api::SERVER_NAME_I2C).expect("Can't connect to I2C");
|
2021-04-06 11:32:35 +00:00
|
|
|
Ok(Llio {
|
|
|
|
|
conn,
|
|
|
|
|
com_sid: None,
|
|
|
|
|
i2c_sid: None,
|
|
|
|
|
rtc_sid: None,
|
|
|
|
|
usb_sid: None,
|
2021-04-07 10:29:50 +00:00
|
|
|
gpio_sid: None,
|
2021-05-05 16:45:14 +00:00
|
|
|
i2c_conn,
|
2021-05-05 19:40:04 +00:00
|
|
|
i2c_timeout_ms: 10,
|
2021-04-06 11:32:35 +00:00
|
|
|
})
|
|
|
|
|
}
|
2021-04-16 11:27:48 +00:00
|
|
|
pub fn vibe(&self, pattern: VibePattern) -> Result<(), xous::Error> {
|
|
|
|
|
send_message(self.conn,
|
|
|
|
|
Message::new_scalar(Opcode::Vibe.to_usize().unwrap(), pattern.into(), 0, 0, 0)
|
|
|
|
|
).map(|_|())
|
|
|
|
|
}
|
2021-05-05 16:45:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////// I2C ///////////////
|
2021-04-06 11:32:35 +00:00
|
|
|
// used to hook a callback for I2c responses
|
2021-05-05 19:40:04 +00:00
|
|
|
fn check_cb_init(&mut self) {
|
|
|
|
|
if self.i2c_sid.is_none() {
|
|
|
|
|
let sid = xous::create_server().unwrap();
|
|
|
|
|
self.i2c_sid = Some(sid);
|
|
|
|
|
let sid_tuple = sid.to_u32();
|
|
|
|
|
xous::create_thread_4(i2c_cb_server, sid_tuple.0 as usize, sid_tuple.1 as usize, sid_tuple.2 as usize, sid_tuple.3 as usize).unwrap();
|
|
|
|
|
// note: we don't register a callback, because we hand our SID directly to the i2c request for a 1:1 message return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
pub fn i2c_set_timeout(&mut self, timeout: u32) {
|
|
|
|
|
self.i2c_timeout_ms = timeout;
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-06 06:03:06 +00:00
|
|
|
/// initiate an i2c write. if async_cb is `None`, one will be provided and the routine will synchronously block until write is complete.
|
|
|
|
|
/// if you want to "fire and forget" the write and don't care when or how it finishes, simply provide a dummy callback.
|
|
|
|
|
pub fn i2c_write(&mut self, dev: u8, adr: u8, data: &[u8], async_cb: Option<fn(I2cTransaction)>) -> Result<I2cStatus, xous::Error> {
|
2021-05-05 19:40:04 +00:00
|
|
|
if data.len() > I2C_MAX_LEN - 1 {
|
|
|
|
|
return Err(xous::Error::OutOfMemory)
|
|
|
|
|
}
|
2021-05-06 06:03:06 +00:00
|
|
|
let synchronous: bool;
|
2021-05-05 19:40:04 +00:00
|
|
|
let mut transaction = I2cTransaction::new();
|
2021-05-06 06:03:06 +00:00
|
|
|
let cb = if let Some(callback) = async_cb {
|
|
|
|
|
synchronous = false;
|
|
|
|
|
callback
|
|
|
|
|
} else {
|
|
|
|
|
synchronous = true;
|
|
|
|
|
if I2C_IN_PROGRESS_MUTEX.load(Ordering::Relaxed) {
|
|
|
|
|
log::error!("entering a synchronous write routine, but somehow a synchronous operation was already in progress!");
|
|
|
|
|
return Err(xous::Error::InternalError);
|
2021-05-05 19:40:04 +00:00
|
|
|
}
|
2021-05-06 06:03:06 +00:00
|
|
|
sync_i2c_cb
|
|
|
|
|
};
|
2021-05-05 19:40:04 +00:00
|
|
|
self.check_cb_init();
|
|
|
|
|
unsafe {
|
|
|
|
|
if let Some(old_cb) = I2C_CB {
|
2021-05-06 06:03:06 +00:00
|
|
|
if old_cb != cb {
|
|
|
|
|
log::warn!("Multiple outstanding write transactions, with different callbacks. You are probably making an error!");
|
2021-05-05 19:40:04 +00:00
|
|
|
}
|
|
|
|
|
}
|
2021-05-06 06:03:06 +00:00
|
|
|
I2C_CB = Some(cb);
|
|
|
|
|
}
|
|
|
|
|
if synchronous {
|
|
|
|
|
I2C_IN_PROGRESS_MUTEX.store(true, Ordering::Relaxed);
|
2021-05-05 19:40:04 +00:00
|
|
|
}
|
|
|
|
|
match self.i2c_sid {
|
|
|
|
|
Some(sid) => transaction.listener = Some(sid.to_u32()),
|
|
|
|
|
None => log::error!("We requested a local listener, but somehow it's not there!"),
|
|
|
|
|
}
|
|
|
|
|
let mut txbuf = [0; I2C_MAX_LEN];
|
|
|
|
|
txbuf[0] = adr;
|
|
|
|
|
for i in 0..data.len() {
|
|
|
|
|
txbuf[i+1] = data[i];
|
|
|
|
|
}
|
|
|
|
|
transaction.bus_addr = dev;
|
|
|
|
|
transaction.txbuf = Some(txbuf);
|
|
|
|
|
transaction.txlen = (data.len() + 1) as u32;
|
|
|
|
|
transaction.status = I2cStatus::RequestIncoming;
|
|
|
|
|
transaction.timeout_ms = self.i2c_timeout_ms;
|
2021-05-05 16:45:14 +00:00
|
|
|
|
2021-05-05 19:40:04 +00:00
|
|
|
let mut buf = Buffer::into_buf(transaction).or(Err(xous::Error::InternalError))?;
|
|
|
|
|
buf.lend_mut(self.i2c_conn, I2cOpcode::I2cTxRx.to_u32().unwrap()).or(Err(xous::Error::InternalError))?;
|
|
|
|
|
let result = buf.to_original::<I2cStatus, _>().unwrap();
|
2021-05-06 06:03:06 +00:00
|
|
|
if synchronous {
|
|
|
|
|
if result != I2cStatus::ResponseInProgress {
|
|
|
|
|
return Err(xous::Error::OutOfMemory);
|
|
|
|
|
}
|
|
|
|
|
while I2C_IN_PROGRESS_MUTEX.load(Ordering::Relaxed) {
|
|
|
|
|
xous::yield_slice();
|
|
|
|
|
}
|
|
|
|
|
Ok(I2cStatus::ResponseWriteOk)
|
|
|
|
|
} else {
|
|
|
|
|
Ok(result)
|
2021-05-05 19:40:04 +00:00
|
|
|
}
|
|
|
|
|
}
|
2021-05-06 06:03:06 +00:00
|
|
|
|
|
|
|
|
/// initiate an i2c read. if asyncread_cb is `None`, one will be provided and the routine will synchronously block until read is complete.
|
|
|
|
|
/// synchronous reads will return the data in &mut `data`. Asynchronous reads will provide the result in the `rxbuf` field of the `I2cTransaction`
|
|
|
|
|
/// returned via the callback. Note that the callback API may be revised to return a smaller, more targeted structure in the future.
|
|
|
|
|
pub fn i2c_read(&mut self, dev: u8, adr: u8, data: &mut[u8], asyncread_cb: Option<fn(I2cTransaction)>) -> Result<I2cStatus, xous::Error> {
|
2021-05-05 20:16:31 +00:00
|
|
|
if data.len() > I2C_MAX_LEN - 1 {
|
|
|
|
|
return Err(xous::Error::OutOfMemory)
|
|
|
|
|
}
|
2021-05-06 06:03:06 +00:00
|
|
|
let synchronous: bool;
|
|
|
|
|
let read_cb = if let Some(cb) = asyncread_cb {
|
|
|
|
|
// user is supplying an async callback
|
|
|
|
|
synchronous = false;
|
|
|
|
|
cb
|
|
|
|
|
} else {
|
|
|
|
|
// user hasn't provided a callback, we'll provide one and return synchronously
|
|
|
|
|
synchronous = true;
|
|
|
|
|
if I2C_IN_PROGRESS_MUTEX.load(Ordering::Relaxed) {
|
|
|
|
|
log::error!("trying a synchronous read, but somehow a synchronous operation was already in progress!");
|
|
|
|
|
return Err(xous::Error::InternalError);
|
|
|
|
|
}
|
|
|
|
|
sync_i2c_cb // supply a default callback
|
|
|
|
|
};
|
2021-05-05 19:40:04 +00:00
|
|
|
let mut transaction = I2cTransaction::new();
|
|
|
|
|
self.check_cb_init();
|
|
|
|
|
unsafe {
|
|
|
|
|
if let Some(old_cb) = I2C_CB {
|
2021-05-06 06:03:06 +00:00
|
|
|
if old_cb != read_cb {
|
|
|
|
|
log::warn!("Multiple outstanding read transactions, with different callbacks. Hope you know what you are doing!");
|
2021-05-05 19:40:04 +00:00
|
|
|
}
|
|
|
|
|
}
|
2021-05-06 06:03:06 +00:00
|
|
|
I2C_CB = Some(read_cb);
|
|
|
|
|
}
|
|
|
|
|
if synchronous {
|
|
|
|
|
I2C_IN_PROGRESS_MUTEX.store(true, Ordering::Relaxed);
|
2021-05-05 19:40:04 +00:00
|
|
|
}
|
|
|
|
|
match self.i2c_sid {
|
|
|
|
|
Some(sid) => transaction.listener = Some(sid.to_u32()),
|
|
|
|
|
None => log::error!("We requested a local listener, but somehow it's not there!"),
|
|
|
|
|
}
|
|
|
|
|
let mut txbuf = [0; I2C_MAX_LEN];
|
|
|
|
|
txbuf[0] = adr;
|
|
|
|
|
let rxbuf = [0; I2C_MAX_LEN];
|
|
|
|
|
transaction.bus_addr = dev;
|
|
|
|
|
transaction.txbuf = Some(txbuf);
|
|
|
|
|
transaction.txlen = 1;
|
|
|
|
|
transaction.rxbuf = Some(rxbuf);
|
|
|
|
|
transaction.rxlen = data.len() as u32;
|
|
|
|
|
transaction.status = I2cStatus::RequestIncoming;
|
|
|
|
|
transaction.timeout_ms = self.i2c_timeout_ms;
|
2021-05-05 16:45:14 +00:00
|
|
|
|
2021-05-05 19:40:04 +00:00
|
|
|
let mut buf = Buffer::into_buf(transaction).or(Err(xous::Error::InternalError))?;
|
|
|
|
|
buf.lend_mut(self.i2c_conn, I2cOpcode::I2cTxRx.to_u32().unwrap()).or(Err(xous::Error::InternalError))?;
|
|
|
|
|
let result = buf.to_original::<I2cStatus, _>().unwrap();
|
2021-05-06 06:03:06 +00:00
|
|
|
if synchronous {
|
|
|
|
|
if result != I2cStatus::ResponseInProgress {
|
|
|
|
|
return Err(xous::Error::OutOfMemory);
|
|
|
|
|
}
|
|
|
|
|
while I2C_IN_PROGRESS_MUTEX.load(Ordering::Relaxed) {
|
|
|
|
|
xous::yield_slice();
|
|
|
|
|
}
|
|
|
|
|
unsafe {
|
|
|
|
|
for (&src, dst) in I2C_RX_HANDOFF.iter().zip(data.iter_mut()) {
|
|
|
|
|
*dst = src;
|
|
|
|
|
}
|
2021-05-05 19:40:04 +00:00
|
|
|
}
|
2021-05-06 06:03:06 +00:00
|
|
|
Ok(I2cStatus::ResponseReadOk)
|
|
|
|
|
} else {
|
|
|
|
|
Ok(result)
|
2021-05-05 19:40:04 +00:00
|
|
|
}
|
|
|
|
|
}
|
2021-05-06 06:03:06 +00:00
|
|
|
// used by async callback handlers to indicate their completion, allowing e.g. later synchronous operations
|
|
|
|
|
pub fn i2c_async_done(&self) {
|
|
|
|
|
unsafe{I2C_CB = None};
|
|
|
|
|
}
|
|
|
|
|
///////////////////////// end I2C ///////////////
|
2021-05-05 16:45:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-04-06 11:32:35 +00:00
|
|
|
pub fn allow_power_off(&self, allow: bool) -> Result<(), xous::Error> {
|
2021-04-09 09:31:36 +00:00
|
|
|
let arg = if allow { 0 } else { 1 };
|
2021-04-06 11:32:35 +00:00
|
|
|
send_message(self.conn,
|
|
|
|
|
Message::new_scalar(Opcode::PowerSelf.to_usize().unwrap(), arg, 0, 0, 0)
|
|
|
|
|
).map(|_| ())
|
|
|
|
|
}
|
2021-03-10 18:57:58 +00:00
|
|
|
|
2021-04-06 11:32:35 +00:00
|
|
|
pub fn allow_ec_snoop(&self, allow: bool) -> Result<(), xous::Error> {
|
|
|
|
|
let arg = if allow { 1 } else { 0 };
|
|
|
|
|
send_message(self.conn,
|
|
|
|
|
Message::new_scalar(Opcode::EcSnoopAllow.to_usize().unwrap(), arg, 0, 0, 0)
|
|
|
|
|
).map(|_| ())
|
|
|
|
|
}
|
2021-03-10 18:57:58 +00:00
|
|
|
|
2021-04-06 11:32:35 +00:00
|
|
|
pub fn adc_vbus(&self) -> Result<u16, xous::Error> {
|
|
|
|
|
let response = send_message(self.conn,
|
|
|
|
|
Message::new_blocking_scalar(Opcode::AdcVbus.to_usize().unwrap(), 0, 0, 0, 0))?;
|
|
|
|
|
if let xous::Result::Scalar1(val) = response {
|
|
|
|
|
Ok(val as u16)
|
|
|
|
|
} else {
|
|
|
|
|
log::error!("LLIO: unexpected return value: {:#?}", response);
|
|
|
|
|
Err(xous::Error::InternalError)
|
|
|
|
|
}
|
2021-03-10 18:57:58 +00:00
|
|
|
}
|
2021-04-06 11:32:35 +00:00
|
|
|
pub fn adc_vccint(&self) -> Result<u16, xous::Error> {
|
|
|
|
|
let response = send_message(self.conn,
|
|
|
|
|
Message::new_blocking_scalar(Opcode::AdcVccInt.to_usize().unwrap(), 0, 0, 0, 0))?;
|
|
|
|
|
if let xous::Result::Scalar1(val) = response {
|
|
|
|
|
Ok(val as u16)
|
|
|
|
|
} else {
|
|
|
|
|
log::error!("LLIO: unexpected return value: {:#?}", response);
|
|
|
|
|
Err(xous::Error::InternalError)
|
|
|
|
|
}
|
2021-03-10 18:57:58 +00:00
|
|
|
}
|
2021-04-06 11:32:35 +00:00
|
|
|
pub fn adc_vccaux(&self) -> Result<u16, xous::Error> {
|
|
|
|
|
let response = send_message(self.conn,
|
|
|
|
|
Message::new_blocking_scalar(Opcode::AdcVccAux.to_usize().unwrap(), 0, 0, 0, 0))?;
|
|
|
|
|
if let xous::Result::Scalar1(val) = response {
|
|
|
|
|
Ok(val as u16)
|
|
|
|
|
} else {
|
|
|
|
|
log::error!("LLIO: unexpected return value: {:#?}", response);
|
|
|
|
|
Err(xous::Error::InternalError)
|
|
|
|
|
}
|
2021-03-10 18:57:58 +00:00
|
|
|
}
|
2021-04-06 11:32:35 +00:00
|
|
|
pub fn adc_vccbram(&self) -> Result<u16, xous::Error> {
|
|
|
|
|
let response = send_message(self.conn,
|
|
|
|
|
Message::new_blocking_scalar(Opcode::AdcVccBram.to_usize().unwrap(), 0, 0, 0, 0))?;
|
|
|
|
|
if let xous::Result::Scalar1(val) = response {
|
|
|
|
|
Ok(val as u16)
|
|
|
|
|
} else {
|
|
|
|
|
log::error!("LLIO: unexpected return value: {:#?}", response);
|
|
|
|
|
Err(xous::Error::InternalError)
|
|
|
|
|
}
|
2021-03-10 18:57:58 +00:00
|
|
|
}
|
2021-04-06 11:32:35 +00:00
|
|
|
pub fn adc_usb_n(&self) -> Result<u16, xous::Error> {
|
|
|
|
|
let response = send_message(self.conn,
|
|
|
|
|
Message::new_blocking_scalar(Opcode::AdcUsbN.to_usize().unwrap(), 0, 0, 0, 0))?;
|
|
|
|
|
if let xous::Result::Scalar1(val) = response {
|
|
|
|
|
Ok(val as u16)
|
|
|
|
|
} else {
|
|
|
|
|
log::error!("LLIO: unexpected return value: {:#?}", response);
|
|
|
|
|
Err(xous::Error::InternalError)
|
|
|
|
|
}
|
2021-03-10 18:57:58 +00:00
|
|
|
}
|
2021-04-06 11:32:35 +00:00
|
|
|
pub fn adc_usb_p(&self) -> Result<u16, xous::Error> {
|
|
|
|
|
let response = send_message(self.conn,
|
|
|
|
|
Message::new_blocking_scalar(Opcode::AdcUsbP.to_usize().unwrap(), 0, 0, 0, 0))?;
|
|
|
|
|
if let xous::Result::Scalar1(val) = response {
|
|
|
|
|
Ok(val as u16)
|
|
|
|
|
} else {
|
|
|
|
|
log::error!("LLIO: unexpected return value: {:#?}", response);
|
|
|
|
|
Err(xous::Error::InternalError)
|
|
|
|
|
}
|
2021-03-10 18:57:58 +00:00
|
|
|
}
|
2021-04-06 11:32:35 +00:00
|
|
|
pub fn adc_temperature(&self) -> Result<u16, xous::Error> {
|
|
|
|
|
let response = send_message(self.conn,
|
|
|
|
|
Message::new_blocking_scalar(Opcode::AdcTemperature.to_usize().unwrap(), 0, 0, 0, 0))?;
|
|
|
|
|
if let xous::Result::Scalar1(val) = response {
|
|
|
|
|
Ok(val as u16)
|
|
|
|
|
} else {
|
|
|
|
|
log::error!("LLIO: unexpected return value: {:#?}", response);
|
|
|
|
|
Err(xous::Error::InternalError)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
pub fn adc_gpio5(&self) -> Result<u16, xous::Error> {
|
|
|
|
|
let response = send_message(self.conn,
|
|
|
|
|
Message::new_blocking_scalar(Opcode::AdcGpio5.to_usize().unwrap(), 0, 0, 0, 0))?;
|
|
|
|
|
if let xous::Result::Scalar1(val) = response {
|
|
|
|
|
Ok(val as u16)
|
|
|
|
|
} else {
|
|
|
|
|
log::error!("LLIO: unexpected return value: {:#?}", response);
|
|
|
|
|
Err(xous::Error::InternalError)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
pub fn adc_gpio2(&self) -> Result<u16, xous::Error> {
|
|
|
|
|
let response = send_message(self.conn,
|
|
|
|
|
Message::new_blocking_scalar(Opcode::AdcGpio2.to_usize().unwrap(), 0, 0, 0, 0))?;
|
|
|
|
|
if let xous::Result::Scalar1(val) = response {
|
|
|
|
|
Ok(val as u16)
|
|
|
|
|
} else {
|
|
|
|
|
log::error!("LLIO: unexpected return value: {:#?}", response);
|
|
|
|
|
Err(xous::Error::InternalError)
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-04-07 10:29:50 +00:00
|
|
|
// USB hooks
|
2021-04-06 16:48:48 +00:00
|
|
|
pub fn hook_usb_callback(&mut self, id: u32, cid: CID) -> Result<(), xous::Error> {
|
2021-04-06 11:32:35 +00:00
|
|
|
if self.usb_sid.is_none() {
|
|
|
|
|
let sid = xous::create_server().unwrap();
|
|
|
|
|
self.usb_sid = Some(sid);
|
|
|
|
|
let sid_tuple = sid.to_u32();
|
|
|
|
|
xous::create_thread_4(usb_cb_server, sid_tuple.0 as usize, sid_tuple.1 as usize, sid_tuple.2 as usize, sid_tuple.3 as usize).unwrap();
|
|
|
|
|
let hookdata = ScalarHook {
|
|
|
|
|
sid: sid_tuple,
|
|
|
|
|
id,
|
|
|
|
|
cid,
|
|
|
|
|
};
|
|
|
|
|
let buf = Buffer::into_buf(hookdata).or(Err(xous::Error::InternalError))?;
|
|
|
|
|
buf.lend(self.conn, Opcode::EventUsbAttachSubscribe.to_u32().unwrap()).map(|_|())
|
|
|
|
|
} else {
|
|
|
|
|
Err(xous::Error::MemoryInUse) // can't hook it twice
|
|
|
|
|
}
|
2021-03-10 18:57:58 +00:00
|
|
|
}
|
2021-04-07 10:29:50 +00:00
|
|
|
pub fn usb_event_enable(&self, ena: bool) -> Result<(), xous::Error> {
|
|
|
|
|
let arg = if ena { 1 } else { 0 };
|
|
|
|
|
send_message(self.conn,
|
|
|
|
|
Message::new_scalar(Opcode::EventUsbAttachEnable.to_usize().unwrap(), arg, 0, 0, 0)
|
|
|
|
|
).map(|_| ())
|
|
|
|
|
}
|
|
|
|
|
// RTC alarm hooks
|
|
|
|
|
pub fn hook_rtc_alarm_callback(&mut self, id: u32, cid: CID) -> Result<(), xous::Error> {
|
|
|
|
|
if self.rtc_sid.is_none() {
|
|
|
|
|
let sid = xous::create_server().unwrap();
|
|
|
|
|
self.rtc_sid = Some(sid);
|
|
|
|
|
let sid_tuple = sid.to_u32();
|
|
|
|
|
xous::create_thread_4(rtc_cb_server, sid_tuple.0 as usize, sid_tuple.1 as usize, sid_tuple.2 as usize, sid_tuple.3 as usize).unwrap();
|
|
|
|
|
let hookdata = ScalarHook {
|
|
|
|
|
sid: sid_tuple,
|
|
|
|
|
id,
|
|
|
|
|
cid,
|
|
|
|
|
};
|
|
|
|
|
let buf = Buffer::into_buf(hookdata).or(Err(xous::Error::InternalError))?;
|
|
|
|
|
buf.lend(self.conn, Opcode::EventRtcSubscribe.to_u32().unwrap()).map(|_|())
|
|
|
|
|
} else {
|
|
|
|
|
Err(xous::Error::MemoryInUse) // can't hook it twice
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
pub fn rtc_alarm_enable(&self, ena: bool) -> Result<(), xous::Error> {
|
|
|
|
|
let arg = if ena { 1 } else { 0 };
|
|
|
|
|
send_message(self.conn,
|
|
|
|
|
Message::new_scalar(Opcode::EventRtcEnable.to_usize().unwrap(), arg, 0, 0, 0)
|
|
|
|
|
).map(|_| ())
|
|
|
|
|
}
|
|
|
|
|
// COM IRQ hooks
|
|
|
|
|
pub fn hook_com_event_callback(&mut self, id: u32, cid: CID) -> Result<(), xous::Error> {
|
|
|
|
|
if self.com_sid.is_none() {
|
|
|
|
|
let sid = xous::create_server().unwrap();
|
|
|
|
|
self.com_sid = Some(sid);
|
|
|
|
|
let sid_tuple = sid.to_u32();
|
|
|
|
|
xous::create_thread_4(com_cb_server, sid_tuple.0 as usize, sid_tuple.1 as usize, sid_tuple.2 as usize, sid_tuple.3 as usize).unwrap();
|
|
|
|
|
let hookdata = ScalarHook {
|
|
|
|
|
sid: sid_tuple,
|
|
|
|
|
id,
|
|
|
|
|
cid,
|
|
|
|
|
};
|
|
|
|
|
let buf = Buffer::into_buf(hookdata).or(Err(xous::Error::InternalError))?;
|
|
|
|
|
buf.lend(self.conn, Opcode::EventComSubscribe.to_u32().unwrap()).map(|_|())
|
|
|
|
|
} else {
|
|
|
|
|
Err(xous::Error::MemoryInUse) // can't hook it twice
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
pub fn com_event_enable(&self, ena: bool) -> Result<(), xous::Error> {
|
|
|
|
|
let arg = if ena { 1 } else { 0 };
|
|
|
|
|
send_message(self.conn,
|
|
|
|
|
Message::new_scalar(Opcode::EventComEnable.to_usize().unwrap(), arg, 0, 0, 0)
|
|
|
|
|
).map(|_| ())
|
|
|
|
|
}
|
|
|
|
|
// GPIO IRQ hooks
|
|
|
|
|
pub fn hook_gpio_event_callback(&mut self, id: u32, cid: CID) -> Result<(), xous::Error> {
|
|
|
|
|
if self.gpio_sid.is_none() {
|
|
|
|
|
let sid = xous::create_server().unwrap();
|
|
|
|
|
self.gpio_sid = Some(sid);
|
|
|
|
|
let sid_tuple = sid.to_u32();
|
|
|
|
|
xous::create_thread_4(gpio_cb_server, sid_tuple.0 as usize, sid_tuple.1 as usize, sid_tuple.2 as usize, sid_tuple.3 as usize).unwrap();
|
|
|
|
|
let hookdata = ScalarHook {
|
|
|
|
|
sid: sid_tuple,
|
|
|
|
|
id,
|
|
|
|
|
cid,
|
|
|
|
|
};
|
|
|
|
|
let buf = Buffer::into_buf(hookdata).or(Err(xous::Error::InternalError))?;
|
|
|
|
|
buf.lend(self.conn, Opcode::GpioIntSubscribe.to_u32().unwrap()).map(|_|())
|
|
|
|
|
} else {
|
|
|
|
|
Err(xous::Error::MemoryInUse) // can't hook it twice
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-04-14 20:10:47 +00:00
|
|
|
pub fn ec_reset(&self) -> Result<(), xous::Error> {
|
|
|
|
|
send_message(self.conn,
|
|
|
|
|
Message::new_scalar(Opcode::EcReset.to_usize().unwrap(), 0, 0, 0, 0)
|
|
|
|
|
).map(|_| ())
|
|
|
|
|
}
|
2021-04-22 10:08:19 +00:00
|
|
|
pub fn self_destruct(&self, code: usize) -> Result<(), xous::Error> {
|
|
|
|
|
// it's up to the caller to know the code sequence, which is:
|
|
|
|
|
// 0x2718_2818
|
|
|
|
|
// followed by
|
|
|
|
|
// 0x3141_5926
|
|
|
|
|
send_message(self.conn,
|
|
|
|
|
Message::new_scalar(Opcode::SelfDestruct.to_usize().unwrap(), code, 0, 0, 0)
|
|
|
|
|
).map(|_| ())
|
|
|
|
|
}
|
debugging BOOST mode glitch issue
Here's an arcane bug for you:
When powering down, the Spartan 7 3.3V I/O drivers will
output a roughly 800us wide glitch as 3.3V falls below around
1.7V, if the 0.95V core voltage comes down first (which it does
because we have an active discharge on 0.95V, but not on 3.3V).
In this case, it's just wide and high enough to cause the
BOOST mode line to engage, which takes the BOOST output
and ties it to VBUS, which is just enough for the system to
think you're powering it back on again. Which is not what
you want when powering down.
The ultimate fix was to put a heftier pull-down on the BOOST
line so that it can't trip the BOOST output into the VBUS.
Another option could be more active discharge FETs, but that
would require a full board spin and we are already at certification,
so we'll stick with the stronger pull-down on BOOST. It seems to
be a robust fix in the worst corner cases we can find.
The downside is that when powering an external USB device,
you are burning an extra 30mW of power overcoming the pull-down,
but typically, any external USB devices plugged into Precursor
will burn far in excess of that, so the delta should be negligible.
2021-04-23 07:33:14 +00:00
|
|
|
pub fn boost_on(&self, ena: bool) -> Result<(), xous::Error> {
|
|
|
|
|
let arg = if ena { 1 } else { 0 };
|
|
|
|
|
send_message(self.conn,
|
|
|
|
|
Message::new_scalar(Opcode::PowerBoostMode.to_usize().unwrap(), arg, 0, 0, 0)
|
|
|
|
|
).map(|_| ())
|
|
|
|
|
}
|
|
|
|
|
pub fn audio_on(&self, ena: bool) -> Result<(), xous::Error> {
|
|
|
|
|
let arg = if ena { 1 } else { 0 };
|
|
|
|
|
send_message(self.conn,
|
2021-05-03 18:22:35 +00:00
|
|
|
Message::new_blocking_scalar(Opcode::PowerAudio.to_usize().unwrap(), arg, 0, 0, 0)
|
debugging BOOST mode glitch issue
Here's an arcane bug for you:
When powering down, the Spartan 7 3.3V I/O drivers will
output a roughly 800us wide glitch as 3.3V falls below around
1.7V, if the 0.95V core voltage comes down first (which it does
because we have an active discharge on 0.95V, but not on 3.3V).
In this case, it's just wide and high enough to cause the
BOOST mode line to engage, which takes the BOOST output
and ties it to VBUS, which is just enough for the system to
think you're powering it back on again. Which is not what
you want when powering down.
The ultimate fix was to put a heftier pull-down on the BOOST
line so that it can't trip the BOOST output into the VBUS.
Another option could be more active discharge FETs, but that
would require a full board spin and we are already at certification,
so we'll stick with the stronger pull-down on BOOST. It seems to
be a robust fix in the worst corner cases we can find.
The downside is that when powering an external USB device,
you are burning an extra 30mW of power overcoming the pull-down,
but typically, any external USB devices plugged into Precursor
will burn far in excess of that, so the delta should be negligible.
2021-04-23 07:33:14 +00:00
|
|
|
).map(|_| ())
|
|
|
|
|
}
|
2021-05-29 16:12:18 +00:00
|
|
|
// -149mA @ 4152mV crypto on // -143mA @ 4149mV crypto off
|
|
|
|
|
pub fn crypto_on(&self, ena: bool) -> Result<(), xous::Error> {
|
|
|
|
|
let arg = if ena { 1 } else { 0 };
|
|
|
|
|
send_message(self.conn,
|
|
|
|
|
Message::new_blocking_scalar(Opcode::PowerCrypto.to_usize().unwrap(), arg, 0, 0, 0)
|
|
|
|
|
).map(|_| ())
|
|
|
|
|
}
|
2021-06-01 09:02:30 +00:00
|
|
|
// setting this to true turns off WFI capabilities, forcing power always on
|
|
|
|
|
pub fn wfi_override(&self, ena: bool) -> Result<(), xous::Error> {
|
|
|
|
|
let arg = if ena { 1 } else { 0 };
|
|
|
|
|
send_message(self.conn,
|
|
|
|
|
Message::new_blocking_scalar(Opcode::WfiOverride.to_usize().unwrap(), arg, 0, 0, 0)
|
|
|
|
|
).map(|_| ())
|
|
|
|
|
}
|
2021-05-29 16:12:18 +00:00
|
|
|
pub fn crypto_power_status(&self) -> Result<(bool, bool, bool), xous::Error> { // sha, engine, override status
|
|
|
|
|
let response = send_message(self.conn,
|
|
|
|
|
Message::new_blocking_scalar(Opcode::PowerCryptoStatus.to_usize().unwrap(), 0, 0, 0, 0)
|
|
|
|
|
)?;
|
|
|
|
|
if let xous::Result::Scalar1(val) = response {
|
|
|
|
|
let sha = if (val & 1) == 0 { false } else { true };
|
|
|
|
|
let engine = if (val & 2) == 0 { false } else { true };
|
|
|
|
|
let force = if (val & 4) == 0 { false } else { true };
|
|
|
|
|
Ok((sha, engine, force))
|
|
|
|
|
} else {
|
|
|
|
|
Err(xous::Error::InternalError)
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-04-25 17:11:45 +00:00
|
|
|
pub fn soc_gitrev(&self) -> Result<(u8, u8, u8, u8, u32), xous::Error> {
|
|
|
|
|
let response = send_message(self.conn,
|
|
|
|
|
Message::new_blocking_scalar(Opcode::InfoGit.to_usize().unwrap(), 0, 0, 0, 0))?;
|
|
|
|
|
if let xous::Result::Scalar2(val1, val2) = response {
|
|
|
|
|
Ok(
|
|
|
|
|
(
|
|
|
|
|
((val1 >> 24) as u8), // major
|
|
|
|
|
((val1 >> 16) as u8), // minor
|
|
|
|
|
((val1 >> 8) as u8), // rev
|
|
|
|
|
(val1 >> 0) as u8, // gitextra
|
|
|
|
|
val2 as u32 // gitrev
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
} else {
|
|
|
|
|
log::error!("LLIO: unexpected return value: {:#?}", response);
|
|
|
|
|
Err(xous::Error::InternalError)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
pub fn soc_dna(&self) -> Result<u64, xous::Error> {
|
|
|
|
|
let response = send_message(self.conn,
|
|
|
|
|
Message::new_blocking_scalar(Opcode::InfoDna.to_usize().unwrap(), 0, 0, 0, 0))?;
|
|
|
|
|
if let xous::Result::Scalar2(val1, val2) = response {
|
|
|
|
|
Ok(
|
|
|
|
|
(val1 as u64) | ((val2 as u64) << 32)
|
|
|
|
|
)
|
|
|
|
|
} else {
|
|
|
|
|
log::error!("LLIO: unexpected return value: {:#?}", response);
|
|
|
|
|
Err(xous::Error::InternalError)
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-06-01 09:02:30 +00:00
|
|
|
pub fn gpio_data_direction(&self, dir: u8) -> Result<(), xous::Error> {
|
|
|
|
|
send_message(self.conn,
|
|
|
|
|
Message::new_scalar(Opcode::GpioDataDrive.to_usize().unwrap(), dir as usize, 0, 0, 0)
|
|
|
|
|
).map(|_| ())
|
|
|
|
|
}
|
|
|
|
|
pub fn gpio_debug_powerdown(&self, ena: bool) -> Result<(), xous::Error> {
|
|
|
|
|
let arg = if ena { 1 } else { 0 };
|
|
|
|
|
send_message(self.conn,
|
|
|
|
|
Message::new_scalar(Opcode::DebugPowerdown.to_usize().unwrap(), arg, 0, 0, 0)
|
|
|
|
|
).map(|_| ())
|
|
|
|
|
}
|
|
|
|
|
pub fn gpio_debug_wakeup(&self, ena: bool) -> Result<(), xous::Error> {
|
|
|
|
|
let arg = if ena { 1 } else { 0 };
|
|
|
|
|
send_message(self.conn,
|
|
|
|
|
Message::new_scalar(Opcode::DebugWakeup.to_usize().unwrap(), arg, 0, 0, 0)
|
|
|
|
|
).map(|_| ())
|
|
|
|
|
}
|
2021-06-01 17:08:31 +00:00
|
|
|
pub fn activity_instantaneous(&self) -> Result<(u32, u32), xous::Error> {
|
|
|
|
|
let response = send_message(self.conn,
|
|
|
|
|
Message::new_blocking_scalar(Opcode::GetActivity.to_usize().unwrap(), 0, 0, 0, 0))?;
|
|
|
|
|
if let xous::Result::Scalar2(active, total) = response {
|
|
|
|
|
Ok(
|
|
|
|
|
(active as u32, total as u32)
|
|
|
|
|
)
|
|
|
|
|
} else {
|
|
|
|
|
log::error!("LLIO: unexpected return value: {:#?}", response);
|
|
|
|
|
Err(xous::Error::InternalError)
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-06-12 20:40:50 +00:00
|
|
|
pub fn debug_usb_unlocked(&self) -> Result<bool, xous::Error> {
|
|
|
|
|
Ok(true) // for now, always return true, because we don't have the hardware yet to lock out debug USB
|
|
|
|
|
}
|
2021-03-10 18:57:58 +00:00
|
|
|
}
|
2021-04-14 20:10:47 +00:00
|
|
|
|
|
|
|
|
|
2021-04-07 18:30:57 +00:00
|
|
|
fn drop_conn(sid: xous::SID) {
|
|
|
|
|
let cid = xous::connect(sid).unwrap();
|
|
|
|
|
xous::send_message(cid,
|
2021-04-09 10:53:41 +00:00
|
|
|
Message::new_scalar(EventCallback::Drop.to_usize().unwrap(), 0, 0, 0, 0)).unwrap();
|
2021-04-07 18:30:57 +00:00
|
|
|
unsafe{xous::disconnect(cid).unwrap();}
|
|
|
|
|
}
|
2021-05-21 08:24:30 +00:00
|
|
|
use core::sync::atomic::AtomicU32;
|
|
|
|
|
static REFCOUNT: AtomicU32 = AtomicU32::new(0);
|
2021-04-06 11:32:35 +00:00
|
|
|
impl Drop for Llio {
|
|
|
|
|
fn drop(&mut self) {
|
|
|
|
|
if let Some(sid) = self.i2c_sid.take() {
|
2021-04-07 18:30:57 +00:00
|
|
|
drop_conn(sid);
|
2021-04-06 11:32:35 +00:00
|
|
|
}
|
|
|
|
|
if let Some(sid) = self.usb_sid.take() {
|
2021-04-07 18:30:57 +00:00
|
|
|
drop_conn(sid);
|
|
|
|
|
}
|
|
|
|
|
if let Some(sid) = self.rtc_sid.take() {
|
|
|
|
|
drop_conn(sid);
|
|
|
|
|
}
|
|
|
|
|
if let Some(sid) = self.com_sid.take() {
|
|
|
|
|
drop_conn(sid);
|
|
|
|
|
}
|
|
|
|
|
if let Some(sid) = self.gpio_sid.take() {
|
|
|
|
|
drop_conn(sid);
|
2021-04-06 11:32:35 +00:00
|
|
|
}
|
2021-05-21 08:24:30 +00:00
|
|
|
if REFCOUNT.load(Ordering::Relaxed) == 0 {
|
|
|
|
|
unsafe{xous::disconnect(self.conn).unwrap();}
|
|
|
|
|
}
|
2021-03-10 18:57:58 +00:00
|
|
|
}
|
|
|
|
|
}
|
2021-04-06 11:32:35 +00:00
|
|
|
|
|
|
|
|
/// handles callback messages from I2C server, in the library user's process space.
|
|
|
|
|
fn i2c_cb_server(sid0: usize, sid1: usize, sid2: usize, sid3: usize) {
|
|
|
|
|
let sid = xous::SID::from_u32(sid0 as u32, sid1 as u32, sid2 as u32, sid3 as u32);
|
|
|
|
|
loop {
|
|
|
|
|
let msg = xous::receive_message(sid).unwrap();
|
|
|
|
|
match FromPrimitive::from_usize(msg.body.id()) {
|
|
|
|
|
Some(I2cCallback::Result) => {
|
|
|
|
|
let buffer = unsafe { Buffer::from_memory_message(msg.body.memory_message().unwrap()) };
|
2021-04-07 18:30:57 +00:00
|
|
|
let mut i2cresult = buffer.to_original::<I2cTransaction, _>().unwrap();
|
|
|
|
|
i2cresult.listener = None; // don't leak our local server address to the callback
|
2021-04-06 11:32:35 +00:00
|
|
|
unsafe {
|
|
|
|
|
if let Some(cb) = I2C_CB {
|
|
|
|
|
cb(i2cresult)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
Some(I2cCallback::Drop) => {
|
|
|
|
|
break; // this exits the loop and kills the thread
|
|
|
|
|
}
|
|
|
|
|
None => (),
|
|
|
|
|
}
|
2021-03-10 18:57:58 +00:00
|
|
|
}
|
2021-04-09 10:51:43 +00:00
|
|
|
xous::destroy_server(sid).unwrap();
|
2021-03-26 12:22:33 +00:00
|
|
|
}
|
2021-04-06 11:32:35 +00:00
|
|
|
|
|
|
|
|
/// handles callback messages that indicate a USB interrupt has happened, in the library user's process space.
|
|
|
|
|
fn usb_cb_server(sid0: usize, sid1: usize, sid2: usize, sid3: usize) {
|
|
|
|
|
let sid = xous::SID::from_u32(sid0 as u32, sid1 as u32, sid2 as u32, sid3 as u32);
|
|
|
|
|
loop {
|
|
|
|
|
let msg = xous::receive_message(sid).unwrap();
|
|
|
|
|
match FromPrimitive::from_usize(msg.body.id()) {
|
|
|
|
|
Some(EventCallback::Event) => msg_scalar_unpack!(msg, cid, id, _, _, {
|
|
|
|
|
// directly pass the scalar message onto the CID with the ID memorized in the original hook
|
|
|
|
|
send_message(cid as u32,
|
|
|
|
|
Message::new_scalar(id, 0, 0, 0, 0)
|
|
|
|
|
).unwrap();
|
|
|
|
|
}),
|
|
|
|
|
Some(EventCallback::Drop) => {
|
|
|
|
|
break; // this exits the loop and kills the thread
|
|
|
|
|
}
|
|
|
|
|
None => (),
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-04-09 10:51:43 +00:00
|
|
|
xous::destroy_server(sid).unwrap();
|
2021-04-07 10:29:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// handles callback messages that indicate a RTC interrupt has happened, in the library user's process space.
|
|
|
|
|
fn rtc_cb_server(sid0: usize, sid1: usize, sid2: usize, sid3: usize) {
|
|
|
|
|
let sid = xous::SID::from_u32(sid0 as u32, sid1 as u32, sid2 as u32, sid3 as u32);
|
|
|
|
|
loop {
|
|
|
|
|
let msg = xous::receive_message(sid).unwrap();
|
|
|
|
|
match FromPrimitive::from_usize(msg.body.id()) {
|
|
|
|
|
Some(EventCallback::Event) => msg_scalar_unpack!(msg, cid, id, _, _, {
|
|
|
|
|
// directly pass the scalar message onto the CID with the ID memorized in the original hook
|
|
|
|
|
send_message(cid as u32,
|
|
|
|
|
Message::new_scalar(id, 0, 0, 0, 0)
|
|
|
|
|
).unwrap();
|
|
|
|
|
}),
|
|
|
|
|
Some(EventCallback::Drop) => {
|
|
|
|
|
break; // this exits the loop and kills the thread
|
|
|
|
|
}
|
|
|
|
|
None => (),
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-04-09 10:51:43 +00:00
|
|
|
xous::destroy_server(sid).unwrap();
|
2021-04-07 10:29:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// handles callback messages that indicate a COM interrupt has happened, in the library user's process space.
|
|
|
|
|
fn com_cb_server(sid0: usize, sid1: usize, sid2: usize, sid3: usize) {
|
|
|
|
|
let sid = xous::SID::from_u32(sid0 as u32, sid1 as u32, sid2 as u32, sid3 as u32);
|
|
|
|
|
loop {
|
|
|
|
|
let msg = xous::receive_message(sid).unwrap();
|
|
|
|
|
match FromPrimitive::from_usize(msg.body.id()) {
|
|
|
|
|
Some(EventCallback::Event) => msg_scalar_unpack!(msg, cid, id, _, _, {
|
|
|
|
|
// directly pass the scalar message onto the CID with the ID memorized in the original hook
|
|
|
|
|
send_message(cid as u32,
|
|
|
|
|
Message::new_scalar(id, 0, 0, 0, 0)
|
|
|
|
|
).unwrap();
|
|
|
|
|
}),
|
|
|
|
|
Some(EventCallback::Drop) => {
|
|
|
|
|
break; // this exits the loop and kills the thread
|
|
|
|
|
}
|
|
|
|
|
None => (),
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-04-09 10:51:43 +00:00
|
|
|
xous::destroy_server(sid).unwrap();
|
2021-04-07 10:29:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// handles callback messages that indicate a GPIO interrupt has happened, in the library user's process space.
|
|
|
|
|
fn gpio_cb_server(sid0: usize, sid1: usize, sid2: usize, sid3: usize) {
|
|
|
|
|
let sid = xous::SID::from_u32(sid0 as u32, sid1 as u32, sid2 as u32, sid3 as u32);
|
|
|
|
|
loop {
|
|
|
|
|
let msg = xous::receive_message(sid).unwrap();
|
|
|
|
|
match FromPrimitive::from_usize(msg.body.id()) {
|
|
|
|
|
Some(EventCallback::Event) => msg_scalar_unpack!(msg, cid, id, _, _, {
|
|
|
|
|
// directly pass the scalar message onto the CID with the ID memorized in the original hook
|
|
|
|
|
send_message(cid as u32,
|
|
|
|
|
Message::new_scalar(id, 0, 0, 0, 0)
|
|
|
|
|
).unwrap();
|
|
|
|
|
}),
|
|
|
|
|
Some(EventCallback::Drop) => {
|
|
|
|
|
break; // this exits the loop and kills the thread
|
|
|
|
|
}
|
|
|
|
|
None => (),
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-04-09 10:51:43 +00:00
|
|
|
xous::destroy_server(sid).unwrap();
|
2021-04-06 11:32:35 +00:00
|
|
|
}
|