mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-15 08:30:02 +00:00
rtc first draft
This commit is contained in:
parent
01a0fcc73f
commit
45c90659e5
13 changed files with 522 additions and 265 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
|
@ -1152,6 +1152,7 @@ version = "0.1.0"
|
|||
dependencies = [
|
||||
"bitflags",
|
||||
"heapless",
|
||||
"llio",
|
||||
"log",
|
||||
"log-server",
|
||||
"num-derive",
|
||||
|
|
|
|||
|
|
@ -1328,10 +1328,10 @@ register to disable individual events.]]></description>
|
|||
<register>
|
||||
<name>GIT_GITREV</name>
|
||||
<description><![CDATA[First 32-bits of the git revision. This documentation was built from git rev
|
||||
``8561717c``, so this value is 2237755772, which should be enough to check out
|
||||
``79ae30c6``, so this value is 2041458886, which should be enough to check out
|
||||
the exact git version used to build this firmware.]]></description>
|
||||
<addressOffset>0x0014</addressOffset>
|
||||
<resetValue>0x8561717c</resetValue>
|
||||
<resetValue>0x79ae30c6</resetValue>
|
||||
<size>32</size>
|
||||
<fields>
|
||||
<field>
|
||||
|
|
@ -1348,7 +1348,7 @@ the exact git version used to build this firmware.]]></description>
|
|||
is ``1``, then the repository this was built from has one additional commit
|
||||
beyond the tag indicated in `MAJOR`, `MINOR`, and `REVISION`.]]></description>
|
||||
<addressOffset>0x0018</addressOffset>
|
||||
<resetValue>0x0f</resetValue>
|
||||
<resetValue>0x10</resetValue>
|
||||
<size>32</size>
|
||||
<fields>
|
||||
<field>
|
||||
|
|
@ -2550,6 +2550,10 @@ reset fashion]]></description>
|
|||
<size>0x14</size>
|
||||
<usage>registers</usage>
|
||||
</addressBlock>
|
||||
<interrupt>
|
||||
<name>power</name>
|
||||
<value>9</value>
|
||||
</interrupt>
|
||||
</peripheral>
|
||||
<peripheral>
|
||||
<name>SPINOR</name>
|
||||
|
|
@ -3044,7 +3048,7 @@ again until the interrupt is serviced.]]></description>
|
|||
</addressBlock>
|
||||
<interrupt>
|
||||
<name>keyboard</name>
|
||||
<value>9</value>
|
||||
<value>10</value>
|
||||
</interrupt>
|
||||
</peripheral>
|
||||
<peripheral>
|
||||
|
|
@ -3056,7 +3060,7 @@ again until the interrupt is serviced.]]></description>
|
|||
<name>SEED1</name>
|
||||
<description><![CDATA[Bits 32-63 of `SEED_SEED`. Seed used for the build]]></description>
|
||||
<addressOffset>0x0000</addressOffset>
|
||||
<resetValue>0x62e0ffce</resetValue>
|
||||
<resetValue>0x505c16c1</resetValue>
|
||||
<size>32</size>
|
||||
<fields>
|
||||
<field>
|
||||
|
|
@ -3071,7 +3075,7 @@ again until the interrupt is serviced.]]></description>
|
|||
<name>SEED0</name>
|
||||
<description><![CDATA[Bits 0-31 of `SEED_SEED`.]]></description>
|
||||
<addressOffset>0x0004</addressOffset>
|
||||
<resetValue>0x629a6ab2</resetValue>
|
||||
<resetValue>0x578f7014</resetValue>
|
||||
<size>32</size>
|
||||
<fields>
|
||||
<field>
|
||||
|
|
@ -3500,7 +3504,7 @@ I2S_FORMAT.I2S_STANDARD is I2S standard]]></description>
|
|||
</addressBlock>
|
||||
<interrupt>
|
||||
<name>audio</name>
|
||||
<value>10</value>
|
||||
<value>11</value>
|
||||
</interrupt>
|
||||
</peripheral>
|
||||
<peripheral>
|
||||
|
|
@ -3636,7 +3640,7 @@ I2S_FORMAT.I2S_STANDARD is I2S standard]]></description>
|
|||
</addressBlock>
|
||||
<interrupt>
|
||||
<name>trng_kernel</name>
|
||||
<value>11</value>
|
||||
<value>12</value>
|
||||
</interrupt>
|
||||
</peripheral>
|
||||
<peripheral>
|
||||
|
|
@ -3968,7 +3972,7 @@ to max field size]]></description>
|
|||
</addressBlock>
|
||||
<interrupt>
|
||||
<name>trng_server</name>
|
||||
<value>12</value>
|
||||
<value>13</value>
|
||||
</interrupt>
|
||||
</peripheral>
|
||||
<peripheral>
|
||||
|
|
@ -4776,7 +4780,7 @@ in bits]]></description>
|
|||
</addressBlock>
|
||||
<interrupt>
|
||||
<name>sha512</name>
|
||||
<value>13</value>
|
||||
<value>14</value>
|
||||
</interrupt>
|
||||
</peripheral>
|
||||
<peripheral>
|
||||
|
|
@ -5005,7 +5009,7 @@ exactly reflects the binary layout of an Engine instruction.]]></description>
|
|||
</addressBlock>
|
||||
<interrupt>
|
||||
<name>engine</name>
|
||||
<value>14</value>
|
||||
<value>15</value>
|
||||
</interrupt>
|
||||
</peripheral>
|
||||
<peripheral>
|
||||
|
|
@ -5223,7 +5227,7 @@ register to disable individual events.]]></description>
|
|||
</addressBlock>
|
||||
<interrupt>
|
||||
<name>wdt</name>
|
||||
<value>15</value>
|
||||
<value>16</value>
|
||||
</interrupt>
|
||||
</peripheral>
|
||||
</peripherals>
|
||||
|
|
@ -5294,21 +5298,22 @@ register to disable individual events.]]></description>
|
|||
<constant name="CONFIG_BUS_DATA_WIDTH" value="32" />
|
||||
<constant name="CONFIG_BUS_ADDRESS_WIDTH" value="32" />
|
||||
<constant name="APP_UART_INTERRUPT" value="4" />
|
||||
<constant name="AUDIO_INTERRUPT" value="10" />
|
||||
<constant name="AUDIO_INTERRUPT" value="11" />
|
||||
<constant name="BTEVENTS_INTERRUPT" value="7" />
|
||||
<constant name="COM_INTERRUPT" value="5" />
|
||||
<constant name="CONSOLE_INTERRUPT" value="3" />
|
||||
<constant name="ENGINE_INTERRUPT" value="14" />
|
||||
<constant name="ENGINE_INTERRUPT" value="15" />
|
||||
<constant name="GPIO_INTERRUPT" value="1" />
|
||||
<constant name="I2C_INTERRUPT" value="6" />
|
||||
<constant name="KEYBOARD_INTERRUPT" value="9" />
|
||||
<constant name="SHA512_INTERRUPT" value="13" />
|
||||
<constant name="KEYBOARD_INTERRUPT" value="10" />
|
||||
<constant name="POWER_INTERRUPT" value="9" />
|
||||
<constant name="SHA512_INTERRUPT" value="14" />
|
||||
<constant name="TICKTIMER_INTERRUPT" value="8" />
|
||||
<constant name="TIMER0_INTERRUPT" value="0" />
|
||||
<constant name="TRNG_KERNEL_INTERRUPT" value="11" />
|
||||
<constant name="TRNG_SERVER_INTERRUPT" value="12" />
|
||||
<constant name="TRNG_KERNEL_INTERRUPT" value="12" />
|
||||
<constant name="TRNG_SERVER_INTERRUPT" value="13" />
|
||||
<constant name="UART_INTERRUPT" value="2" />
|
||||
<constant name="WDT_INTERRUPT" value="15" />
|
||||
<constant name="WDT_INTERRUPT" value="16" />
|
||||
</constants>
|
||||
</vendorExtensions>
|
||||
</device>
|
||||
|
|
@ -75,12 +75,14 @@ pub struct I2cTransaction {
|
|||
pub txlen: u32,
|
||||
pub rxbuf: Option<[u8; 258]>,
|
||||
pub rxlen: u32,
|
||||
pub listener: Option<(u32, u32, u32, u32)>, // where Rx split transactions should be routed to
|
||||
pub timeout_ms: u32,
|
||||
pub status: I2cStatus,
|
||||
pub callback_id: u32, // used by callback routines to help identify a return value
|
||||
}
|
||||
impl I2cTransaction {
|
||||
pub fn new() -> Self {
|
||||
I2cTransaction{ bus_addr: 0, txbuf: None, txlen: 0, rxbuf: None, rxlen: 0, timeout_ms: 100, status: I2cStatus::Uninitialized }
|
||||
I2cTransaction{ bus_addr: 0, txbuf: None, txlen: 0, rxbuf: None, rxlen: 0, timeout_ms: 100, status: I2cStatus::Uninitialized, listener: None, callback_id: 0 }
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -195,10 +197,10 @@ pub(crate) enum Opcode {
|
|||
|
||||
/// not tested - I2C functions
|
||||
I2cTxRx, //(I2cTransaction), // type (tx or rx) encoded in struct
|
||||
/// callback repsonses from the I2C engine
|
||||
I2cRegisterCallback, //(String<64>), // if TxRx is issued without first subscribing to responses, the read data is just "lost"
|
||||
/// from i2c interrupt handler
|
||||
IrqI2cTxrxDone, // internal API only
|
||||
/// from i2c interrupt handler (internal API only)
|
||||
IrqI2cTxrxDone,
|
||||
/// checks if the I2C engine is currently busy, for polling implementations
|
||||
I2cIsBusy,
|
||||
|
||||
/// not tested -- events
|
||||
EventComSubscribe, //(String<64>),
|
||||
|
|
|
|||
|
|
@ -24,9 +24,6 @@ impl I2cStateMachine {
|
|||
pub fn initiate(&mut self, _transaction: I2cTransaction ) -> I2cStatus {
|
||||
I2cStatus::ResponseInProgress
|
||||
}
|
||||
pub fn register_listener(&mut self, _listener: xous::CID) -> Result<(), xous::CID> {
|
||||
Ok(())
|
||||
}
|
||||
pub fn handler(&mut self) {
|
||||
}
|
||||
}
|
||||
|
|
@ -38,13 +35,15 @@ pub struct I2cStateMachine {
|
|||
timestamp: u64, // timestamp of the last transaction
|
||||
ticktimer: ticktimer_server::Ticktimer, // a connection to the ticktimer so we can measure timeouts
|
||||
i2c_csr: utralib::CSR<u32>,
|
||||
listeners: [Option<xous::CID>; 32],
|
||||
listener: Option<xous::SID>,
|
||||
}
|
||||
|
||||
#[cfg(target_os = "none")]
|
||||
fn send_i2c_response(listener: xous::CID, trans: I2cTransaction) -> Result<(), xous::Error> {
|
||||
fn send_i2c_response(listener: xous::SID, trans: I2cTransaction) -> Result<(), xous::Error> {
|
||||
let cid = xous::connect(listener).unwrap();
|
||||
let buf = xous_ipc::Buffer::into_buf(trans).or(Err(xous::Error::InternalError))?;
|
||||
buf.lend(listener, I2cCallback::Result.to_u32().unwrap()).map(|_|())
|
||||
buf.lend(cid, I2cCallback::Result.to_u32().unwrap()).map(|_|())?;
|
||||
unsafe{xous::disconnect(cid)}
|
||||
}
|
||||
|
||||
#[cfg(target_os = "none")]
|
||||
|
|
@ -57,7 +56,7 @@ impl I2cStateMachine {
|
|||
ticktimer,
|
||||
i2c_csr: CSR::new(i2c_base),
|
||||
index: 0,
|
||||
listeners: [None; 32],
|
||||
listener: None,
|
||||
}
|
||||
}
|
||||
pub fn initiate(&mut self, transaction: I2cTransaction ) -> I2cStatus {
|
||||
|
|
@ -81,6 +80,10 @@ impl I2cStateMachine {
|
|||
}
|
||||
self.timestamp = now;
|
||||
self.transaction = transaction.clone();
|
||||
match transaction.listener {
|
||||
None => self.listener = None,
|
||||
Some((s0, s1, s2, s3)) => self.listener = Some(xous::SID::from_u32(s0, s1, s2, s3)),
|
||||
}
|
||||
|
||||
if self.transaction.status == I2cStatus::RequestIncoming {
|
||||
self.transaction.status = I2cStatus::ResponseInProgress;
|
||||
|
|
@ -117,57 +120,40 @@ impl I2cStateMachine {
|
|||
}
|
||||
}
|
||||
fn report_nack(&mut self) {
|
||||
// report the NACK situation to all the listeners
|
||||
// report the NACK situation to the listener
|
||||
let mut nack = I2cTransaction::new();
|
||||
nack.status = I2cStatus::ResponseNack;
|
||||
for &l in self.listeners.iter() {
|
||||
if let Some(listener) = l {
|
||||
send_i2c_response(listener, nack).expect("LLIO|I2C: couldn't send NACK to listeners");
|
||||
};
|
||||
}
|
||||
if let Some(listener) = self.listener {
|
||||
send_i2c_response(listener, nack).expect("LLIO|I2C: couldn't send NACK to listeners");
|
||||
};
|
||||
}
|
||||
fn report_timeout(&mut self) {
|
||||
let mut timeout = I2cTransaction::new();
|
||||
timeout.status = I2cStatus::ResponseTimeout;
|
||||
for &l in self.listeners.iter() {
|
||||
if let Some(listener) = l {
|
||||
send_i2c_response(listener, timeout).expect("LLIO|I2c: couldn't send timeout error to liseners");
|
||||
};
|
||||
}
|
||||
if let Some(listener) = self.listener {
|
||||
send_i2c_response(listener, timeout).expect("LLIO|I2c: couldn't send timeout error to liseners");
|
||||
};
|
||||
}
|
||||
fn report_write_done(&mut self) {
|
||||
// report the end of a write-only transaction to all the listeners
|
||||
let mut ack = I2cTransaction::new();
|
||||
ack.status = I2cStatus::ResponseWriteOk;
|
||||
for &l in self.listeners.iter() {
|
||||
if let Some(listener) = l {
|
||||
send_i2c_response(listener, ack).expect("LLIO|I2C: couldn't send write ACK to listeners");
|
||||
};
|
||||
}
|
||||
if let Some(listener) = self.listener {
|
||||
send_i2c_response(listener, ack).expect("LLIO|I2C: couldn't send write ACK to listeners");
|
||||
};
|
||||
}
|
||||
fn report_read_done(&mut self) {
|
||||
// report the result of a read transaction to all the listeners
|
||||
self.transaction.status = I2cStatus::ResponseReadOk;
|
||||
for &l in self.listeners.iter() {
|
||||
if let Some(listener) = l {
|
||||
send_i2c_response(listener, self.transaction).expect("LLIO|I2C: couldn't send read response to listeners");
|
||||
};
|
||||
}
|
||||
if let Some(listener) = self.listener {
|
||||
send_i2c_response(listener, self.transaction).expect("LLIO|I2C: couldn't send read response to listeners");
|
||||
};
|
||||
}
|
||||
pub fn register_listener(&mut self, listener: xous::CID) -> Result<(), xous::CID> {
|
||||
let mut found = false;
|
||||
for entry in self.listeners.iter_mut() {
|
||||
if *entry == None {
|
||||
*entry = Some(listener);
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
log::error!("I2cRegisterCallback ran out of space registering callback");
|
||||
Err(listener)
|
||||
pub fn is_busy(&self) -> bool {
|
||||
if self.state == I2cState::Idle {
|
||||
false
|
||||
} else {
|
||||
Ok(())
|
||||
true
|
||||
}
|
||||
}
|
||||
pub fn handler(&mut self) {
|
||||
|
|
@ -246,6 +232,7 @@ impl I2cStateMachine {
|
|||
} else {
|
||||
self.report_read_done();
|
||||
self.state = I2cState::Idle;
|
||||
self.listener = None;
|
||||
}
|
||||
} else {
|
||||
// we should never get here, because rxbuf was checked as Some() by the setup routine
|
||||
|
|
@ -258,17 +245,3 @@ impl I2cStateMachine {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(target_os = "none")]
|
||||
impl Drop for I2cStateMachine {
|
||||
fn drop(&mut self) {
|
||||
for &l in self.listeners.iter() {
|
||||
if let Some(listener) = l {
|
||||
let dummy = I2cTransaction::new();
|
||||
let buf = xous_ipc::Buffer::into_buf(dummy).or(Err(xous::Error::InternalError)).unwrap();
|
||||
buf.lend(listener, I2cCallback::Drop.to_u32().unwrap()).map(|_|()).unwrap();
|
||||
unsafe{xous::disconnect(listener).unwrap();}
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,13 +3,13 @@
|
|||
/// 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;
|
||||
use api::*;
|
||||
pub use api::*;
|
||||
|
||||
use xous::{send_message, CID, Message, msg_scalar_unpack};
|
||||
use xous_ipc::Buffer;
|
||||
use num_traits::{ToPrimitive, FromPrimitive};
|
||||
|
||||
// this hooks the responce of the I2C bus
|
||||
// this hooks the response of the I2C bus
|
||||
static mut I2C_CB: Option<fn(I2cTransaction)> = None;
|
||||
|
||||
#[derive(Debug)]
|
||||
|
|
@ -44,21 +44,41 @@ impl Llio {
|
|||
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();
|
||||
xous::send_message(self.conn,
|
||||
Message::new_scalar(Opcode::I2cRegisterCallback.to_usize().unwrap(),
|
||||
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
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
// used by other servers to request an I2C transaction
|
||||
pub fn send_i2c_request(&self, transaction: I2cTransaction) -> Result<I2cStatus, xous::Error> {
|
||||
let mut buf = Buffer::into_buf(transaction).or(Err(xous::Error::InternalError))?;
|
||||
// copy the transaction, and annotate with our private callback listener server address
|
||||
// the server is used only for this connection, and shared only to the LLIO server
|
||||
// it's sanitized on the callback response. It's not the end of the world if this server address is
|
||||
// discovered, just unhygenic.
|
||||
let mut local_transaction = transaction;
|
||||
match self.i2c_sid {
|
||||
None => local_transaction.listener = None,
|
||||
Some(sid) => local_transaction.listener = Some(sid.to_u32()),
|
||||
}
|
||||
let mut buf = Buffer::into_buf(local_transaction).or(Err(xous::Error::InternalError))?;
|
||||
buf.lend_mut(self.conn, Opcode::I2cTxRx.to_u32().unwrap()).or(Err(xous::Error::InternalError))?;
|
||||
|
||||
let status = buf.to_original::<I2cStatus, _>().unwrap();
|
||||
Ok(status)
|
||||
}
|
||||
pub fn poll_i2c_busy(&self) -> Result<bool, xous::Error> {
|
||||
let response = send_message(self.conn,
|
||||
Message::new_blocking_scalar(Opcode::I2cIsBusy.to_usize().unwrap(), 0, 0, 0, 0))?;
|
||||
if let xous::Result::Scalar1(val) = response {
|
||||
if val != 0 {
|
||||
Ok(true)
|
||||
} else {
|
||||
Ok(false)
|
||||
}
|
||||
} else {
|
||||
log::error!("LLIO: unexpected return value: {:#?}", response);
|
||||
Err(xous::Error::InternalError)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn allow_power_off(&self, allow: bool) -> Result<(), xous::Error> {
|
||||
let arg = if allow { 1 } else { 0 };
|
||||
|
|
@ -255,21 +275,29 @@ impl Llio {
|
|||
}
|
||||
}
|
||||
}
|
||||
fn drop_conn(sid: xous::SID) {
|
||||
let cid = xous::connect(sid).unwrap();
|
||||
xous::send_message(cid,
|
||||
Message::new_blocking_scalar(EventCallback::Drop.to_usize().unwrap(), 0, 0, 0, 0)).unwrap();
|
||||
unsafe{xous::disconnect(cid).unwrap();}
|
||||
xous::destroy_server(sid).unwrap();
|
||||
}
|
||||
impl Drop for Llio {
|
||||
fn drop(&mut self) {
|
||||
if let Some(sid) = self.i2c_sid.take() {
|
||||
let cid = xous::connect(sid).unwrap();
|
||||
xous::send_message(cid,
|
||||
Message::new_blocking_scalar(I2cCallback::Drop.to_usize().unwrap(), 0, 0, 0, 0)).unwrap();
|
||||
unsafe{xous::disconnect(cid).unwrap();}
|
||||
xous::destroy_server(sid).unwrap();
|
||||
drop_conn(sid);
|
||||
}
|
||||
if let Some(sid) = self.usb_sid.take() {
|
||||
let cid = xous::connect(sid).unwrap();
|
||||
xous::send_message(cid,
|
||||
Message::new_blocking_scalar(EventCallback::Drop.to_usize().unwrap(), 0, 0, 0, 0)).unwrap();
|
||||
unsafe{xous::disconnect(cid).unwrap();}
|
||||
xous::destroy_server(sid).unwrap();
|
||||
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);
|
||||
}
|
||||
unsafe{xous::disconnect(self.conn).unwrap();}
|
||||
}
|
||||
|
|
@ -283,7 +311,8 @@ fn i2c_cb_server(sid0: usize, sid1: usize, sid2: usize, sid3: usize) {
|
|||
match FromPrimitive::from_usize(msg.body.id()) {
|
||||
Some(I2cCallback::Result) => {
|
||||
let buffer = unsafe { Buffer::from_memory_message(msg.body.memory_message().unwrap()) };
|
||||
let i2cresult = buffer.to_original::<I2cTransaction, _>().unwrap();
|
||||
let mut i2cresult = buffer.to_original::<I2cTransaction, _>().unwrap();
|
||||
i2cresult.listener = None; // don't leak our local server address to the callback
|
||||
unsafe {
|
||||
if let Some(cb) = I2C_CB {
|
||||
cb(i2cresult)
|
||||
|
|
|
|||
|
|
@ -19,6 +19,7 @@ mod implementation {
|
|||
use utralib::generated::*;
|
||||
use num_traits::ToPrimitive;
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub struct Llio {
|
||||
reboot_csr: utralib::CSR<u32>,
|
||||
crg_csr: utralib::CSR<u32>,
|
||||
|
|
@ -684,10 +685,9 @@ fn xmain() -> ! {
|
|||
let status = i2c_machine.initiate(i2c_txrx);
|
||||
buffer.replace(status).unwrap();
|
||||
}
|
||||
Some(Opcode::I2cRegisterCallback) => msg_scalar_unpack!(msg, sid0, sid1, sid2, sid3, {
|
||||
let sid = xous::SID::from_u32(sid0 as _, sid1 as _, sid2 as _, sid3 as _);
|
||||
let cid = xous::connect(sid).unwrap();
|
||||
i2c_machine.register_listener(cid).unwrap();
|
||||
Some(Opcode::I2cIsBusy) => msg_blocking_scalar_unpack!(msg, _, _, _, _, {
|
||||
let busy = if i2c_machine.is_busy() {1} else {0};
|
||||
xous::return_scalar(msg.sender, busy as _).expect("couldn't return I2cIsBusy");
|
||||
}),
|
||||
Some(Opcode::EventUsbAttachSubscribe) => {
|
||||
let buffer = unsafe { Buffer::from_memory_message(msg.body.memory_message().unwrap()) };
|
||||
|
|
|
|||
|
|
@ -13,6 +13,7 @@ ticktimer-server = { path = "../ticktimer-server" }
|
|||
xous-names = { path = "../xous-names" }
|
||||
log = "0.4"
|
||||
bitflags = "1.2.1"
|
||||
llio = {path = "../llio"}
|
||||
|
||||
xous-ipc = {path = "../../xous-ipc"}
|
||||
num-derive = {version = "0.3", default-features = false}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,5 @@
|
|||
use xous::{Message, ScalarMessage};
|
||||
|
||||
#[derive(Debug, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)]
|
||||
#[derive(Debug, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize, Copy, Clone)]
|
||||
pub enum Weekday {
|
||||
Sunday,
|
||||
Monday,
|
||||
|
|
@ -32,18 +31,18 @@ pub struct DateTime {
|
|||
pub(crate) const SERVER_NAME_RTC: &str = "_Real time clock application server_";
|
||||
|
||||
#[derive(Debug, num_derive::FromPrimitive, num_derive::ToPrimitive)]
|
||||
pub enum Opcode {
|
||||
pub(crate) enum Opcode {
|
||||
/// register a callback for the datetime
|
||||
RegisterDateTimeCallback,
|
||||
|
||||
/// register a callback for the RTC alarm event
|
||||
RegisterAlarmCallback,
|
||||
/// sets the datetime
|
||||
SetDateTime, //(DateTime),
|
||||
|
||||
/// Get datetime. Causes users with registered callbacks to receive the current DateTime
|
||||
RequestDateTime,
|
||||
|
||||
/// sets the datetime
|
||||
SetDateTime, //(DateTime),
|
||||
/// the datetime response, used internally from the callback manager
|
||||
ResponseDateTime,
|
||||
|
||||
/// sets a wake-up alarm. This forces the SoC into power-on state, if it happens to be off.
|
||||
/// primarily used to trigger cold reboots, but could have other reasons
|
||||
|
|
|
|||
|
|
@ -2,14 +2,15 @@
|
|||
|
||||
pub mod api;
|
||||
|
||||
use api::{Return, Opcode}; // if you prefer to map the api into your local namespace
|
||||
use xous::{send_message, Error, CID, Message, msg_scalar_unpack};
|
||||
use xous_ipc::{String, Buffer};
|
||||
use num_traits::{ToPrimitive, FromPrimitive};
|
||||
use api::{Return, Opcode, DateTime}; // if you prefer to map the api into your local namespace
|
||||
use xous::{send_message, CID, Message};
|
||||
use xous_ipc::Buffer;
|
||||
use num_traits::ToPrimitive;
|
||||
|
||||
pub struct Rtc {
|
||||
conn: CID,
|
||||
callback_sid: Option<xous::SID>,
|
||||
llio: llio::Llio,
|
||||
}
|
||||
static mut RTC_CB: Option<fn(DateTime)> = None;
|
||||
impl Rtc {
|
||||
|
|
@ -18,6 +19,7 @@ impl Rtc {
|
|||
Ok(Rtc {
|
||||
conn,
|
||||
callback_sid: None,
|
||||
llio: llio::Llio::new(&xns).expect("Can't connect to LLIO on behalf of RTC library"),
|
||||
})
|
||||
}
|
||||
|
||||
|
|
@ -25,7 +27,7 @@ impl Rtc {
|
|||
let buf = Buffer::into_buf(dt).or(Err(xous::Error::InternalError))?;
|
||||
buf.lend(self.conn, Opcode::SetDateTime.to_u32().unwrap()).map(|_| ())
|
||||
}
|
||||
pub fn rtc_callback(&mut self, cb: fn(DateTime)) -> Result<(), xous::Error> {
|
||||
pub fn hook_rtc_callback(&mut self, cb: fn(DateTime)) -> Result<(), xous::Error> {
|
||||
if unsafe{RTC_CB}.is_some() {
|
||||
return Err(xous::Error::MemoryInUse)
|
||||
}
|
||||
|
|
@ -42,21 +44,38 @@ impl Rtc {
|
|||
}
|
||||
Ok(())
|
||||
}
|
||||
// this simply forwards the hook on to the LLIO library, which actually owns the Event peripheral where the interrupt is generated
|
||||
pub fn hook_rtc_alarm_callback(&mut self, id: u32, cid: CID) -> Result<(), xous::Error> {
|
||||
self.llio.hook_rtc_alarm_callback(id, cid)
|
||||
}
|
||||
|
||||
pub fn request_datetime(&self) -> Result<(), xous::Error> {
|
||||
send_message(self.conn,
|
||||
Message::new_scalar(Opcode::RequestDateTime.to_usize().unwrap(), 0, 0, 0, 0)
|
||||
).map(|_|())
|
||||
}
|
||||
pub fn set_alarm(&self, seconds_from_now: u8) -> Result<(), xous::Error> {
|
||||
/// wakeup alarm will force the system on if it is off, but does not trigger an interrupt on the CPU
|
||||
pub fn set_wakeup_alarm(&self, seconds_from_now: u8) -> Result<(), xous::Error> {
|
||||
send_message(self.conn,
|
||||
Message::new_scalar(Opcode::SetWakeupAlarm.to_usize().unwrap(), seconds_from_now as _, 0, 0, 0)
|
||||
).map(|_|())
|
||||
}
|
||||
pub fn clear_alarm(&self) -> Result<(), xous::Error> {
|
||||
pub fn clear_wakeup_alarm(&self) -> Result<(), xous::Error> {
|
||||
send_message(self.conn,
|
||||
Message::new_scalar(Opcode::ClearWakeupAlarm.to_usize().unwrap(), 0, 0, 0, 0)
|
||||
).map(|_|())
|
||||
}
|
||||
/// the rtc alarm will not turn the system on, but it will trigger an interrupt on the CPU
|
||||
pub fn set_rtc_alarm(&self, seconds_from_now: u8) -> Result<(), xous::Error> {
|
||||
send_message(self.conn,
|
||||
Message::new_scalar(Opcode::SetRtcAlarm.to_usize().unwrap(), seconds_from_now as _, 0, 0, 0)
|
||||
).map(|_|())
|
||||
}
|
||||
pub fn clear_rtc_alarm(&self) -> Result<(), xous::Error> {
|
||||
send_message(self.conn,
|
||||
Message::new_scalar(Opcode::ClearRtcAlarm.to_usize().unwrap(), 0, 0, 0, 0)
|
||||
).map(|_|())
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for Rtc {
|
||||
|
|
|
|||
|
|
@ -2,18 +2,21 @@
|
|||
#![cfg_attr(target_os = "none", no_main)]
|
||||
|
||||
mod api;
|
||||
use api::*;
|
||||
|
||||
use num_traits::FromPrimitive;
|
||||
use xous_ipc::{String, Buffer};
|
||||
use api::{Return, Opcode};
|
||||
use xous::{CID, msg_scalar_unpack, msg_blocking_scalar_unpack};
|
||||
use xous_ipc::Buffer;
|
||||
use api::{Return, Opcode, DateTime};
|
||||
use xous::{CID, msg_scalar_unpack};
|
||||
|
||||
#[cfg(target_os = "none")]
|
||||
mod implementation {
|
||||
use log::info;
|
||||
#[macro_use]
|
||||
#![allow(dead_code)]
|
||||
use bitflags::*;
|
||||
use crate::CB_TO_MAIN_CONN;
|
||||
use llio::{I2cStatus, I2cTransaction, Llio};
|
||||
use crate::api::{Opcode, DateTime, Weekday};
|
||||
use xous_ipc::Buffer;
|
||||
use num_traits::ToPrimitive;
|
||||
|
||||
const ABRTCMC_I2C_ADR: u8 = 0x68;
|
||||
const ABRTCMC_CONTROL1: u8 = 0x00;
|
||||
|
|
@ -194,144 +197,256 @@ mod implementation {
|
|||
(bcd & 0xf) + ((bcd >> 4) * 10)
|
||||
}
|
||||
|
||||
fn to_weekday(bcd: u8) -> Weekdays {
|
||||
fn to_weekday(bcd: u8) -> Weekday {
|
||||
match bcd {
|
||||
0 => Weekdays::SUNDAY,
|
||||
1 => Weekdays::MONDAY,
|
||||
2 => Weekdays::TUESDAY,
|
||||
3 => Weekdays::WEDNESDAY,
|
||||
4 => Weekdays::THURSDAY,
|
||||
5 => Weekdays::FRIDAY,
|
||||
6 => Weekdays::SATURDAY,
|
||||
_ => Weekdays::SUNDAY,
|
||||
0 => Weekday::Sunday,
|
||||
1 => Weekday::Monday,
|
||||
2 => Weekday::Tuesday,
|
||||
3 => Weekday::Wednesday,
|
||||
4 => Weekday::Thursday,
|
||||
5 => Weekday::Friday,
|
||||
6 => Weekday::Saturday,
|
||||
_ => Weekday::Sunday,
|
||||
}
|
||||
}
|
||||
|
||||
fn i2c_callback(trans: I2cTransaction) {
|
||||
if trans.status == I2cStatus::ResponseReadOk {
|
||||
if let Some(cb_to_main_conn) = unsafe{CB_TO_MAIN_CONN} {
|
||||
// we expect this to be 0, as we don't set it
|
||||
assert!(trans.callback_id == 0, "callback ID was incorrect!");
|
||||
if let Some(rxbuf) = trans.rxbuf {
|
||||
let dt = DateTime {
|
||||
seconds: to_binary(rxbuf[0] & 0x7f),
|
||||
minutes: to_binary(rxbuf[1] & 0x7f),
|
||||
hours: to_binary(rxbuf[2] & 0x3f),
|
||||
days: to_binary(rxbuf[3] & 0x3f),
|
||||
weekday: to_weekday(rxbuf[4] & 0x7f),
|
||||
months: to_binary(rxbuf[5] & 0x1f),
|
||||
years: to_binary(rxbuf[6]),
|
||||
};
|
||||
let buf = Buffer::into_buf(dt).unwrap();
|
||||
buf.send(cb_to_main_conn, Opcode::ResponseDateTime.to_u32().unwrap()).unwrap();
|
||||
} else {
|
||||
log::error!("i2c_callback: no rx data to unpack!")
|
||||
}
|
||||
} else {
|
||||
log::error!("i2c_callback happened, but no connection to the main server!");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Rtc {
|
||||
pub seconds: u8,
|
||||
pub minutes: u8,
|
||||
pub hours: u8,
|
||||
pub days: u8,
|
||||
pub months: u8,
|
||||
pub years: u8,
|
||||
pub weekday: Weekdays,
|
||||
updated_ticks: u64,
|
||||
llio: Llio,
|
||||
rtc_alarm_enabled: bool,
|
||||
wakeup_alarm_enabled: bool,
|
||||
}
|
||||
|
||||
impl Rtc {
|
||||
pub fn new() -> Rtc {
|
||||
info!("RTC: hardware initialized");
|
||||
|
||||
pub fn new(xns: &xous_names::XousNames) -> Rtc {
|
||||
log::trace!("hardware initialized");
|
||||
let mut llio = Llio::new(xns).expect("can't connect to LLIO");
|
||||
llio.hook_i2c_callback(i2c_callback).expect("can't hook I2C callback");
|
||||
Rtc {
|
||||
seconds: 0,
|
||||
minutes: 0,
|
||||
hours: 0,
|
||||
days: 0,
|
||||
months: 0,
|
||||
years: 0,
|
||||
weekday: Weekdays::SUNDAY,
|
||||
updated_ticks: 0,
|
||||
llio,
|
||||
rtc_alarm_enabled: false,
|
||||
wakeup_alarm_enabled: false,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// we only support 24 hour mode
|
||||
/// TODO: sanity check arguments
|
||||
/// TODO: write accesses should happen in a single block, to guarante atomicity of the operation
|
||||
pub fn rtc_set(&mut self, secs: u8, mins: u8, hours: u8, days: u8, months: u8, years: u8, d: Weekdays) -> bool {
|
||||
let mut txbuf: [u8; 2];
|
||||
pub fn rtc_set(&mut self, secs: u8, mins: u8, hours: u8, days: u8, months: u8, years: u8, day: Weekday)
|
||||
-> Result<bool, xous::Error> {
|
||||
let mut transaction = I2cTransaction::new();
|
||||
let mut txbuf: [u8; 258] = [0; 258];
|
||||
|
||||
if secs > 59 { return Ok(false); }
|
||||
if mins > 59 { return Ok(false); }
|
||||
if hours > 23 { return Ok(false); }
|
||||
if days > 31 { return Ok(false); }
|
||||
if months > 12 { return Ok(false); }
|
||||
if years > 99 { return Ok(false); }
|
||||
|
||||
// convert enum to bitfields
|
||||
let d = match day {
|
||||
Weekday::Monday => Weekdays::MONDAY,
|
||||
Weekday::Tuesday => Weekdays::TUESDAY,
|
||||
Weekday::Wednesday => Weekdays::WEDNESDAY,
|
||||
Weekday::Thursday => Weekdays::THURSDAY,
|
||||
Weekday::Friday => Weekdays::FRIDAY,
|
||||
Weekday::Saturday => Weekdays::SATURDAY,
|
||||
Weekday::Sunday => Weekdays::SUNDAY,
|
||||
};
|
||||
|
||||
// make sure battery switchover is enabled, otherwise we won't keep time when power goes off
|
||||
txbuf = [ABRTCMC_CONTROL3, (Control3::BATT_STD_BL_EN).bits()];
|
||||
i2c_controller(&self.p, ABRTCMC_I2C_ADR, Some(&txbuf), None, I2C_TIMEOUT);
|
||||
txbuf[0] = ABRTCMC_CONTROL3;
|
||||
txbuf[1] = (Control3::BATT_STD_BL_EN).bits();
|
||||
txbuf[2] = to_bcd(secs);
|
||||
txbuf[3] = to_bcd(mins);
|
||||
txbuf[4] = to_bcd(hours);
|
||||
txbuf[5] = to_bcd(days);
|
||||
txbuf[6] = to_bcd(d.bits);
|
||||
txbuf[7] = to_bcd(months);
|
||||
txbuf[8] = to_bcd(years);
|
||||
|
||||
txbuf = [ABRTCMC_SECONDS, to_bcd(secs)];
|
||||
i2c_controller(&self.p, ABRTCMC_I2C_ADR, Some(&txbuf), None, I2C_TIMEOUT);
|
||||
self.updated_ticks = get_ticks(&self.p);
|
||||
self.seconds = secs;
|
||||
|
||||
txbuf = [ABRTCMC_MINUTES, to_bcd(mins)];
|
||||
i2c_controller(&self.p, ABRTCMC_I2C_ADR, Some(&txbuf), None, I2C_TIMEOUT);
|
||||
self.minutes = mins;
|
||||
|
||||
txbuf = [ABRTCMC_HOURS, to_bcd(hours)];
|
||||
i2c_controller(&self.p, ABRTCMC_I2C_ADR, Some(&txbuf), None, I2C_TIMEOUT);
|
||||
self.hours = hours;
|
||||
|
||||
txbuf = [ABRTCMC_DAYS, to_bcd(days)];
|
||||
i2c_controller(&self.p, ABRTCMC_I2C_ADR, Some(&txbuf), None, I2C_TIMEOUT);
|
||||
self.days = days;
|
||||
|
||||
txbuf = [ABRTCMC_MONTHS, to_bcd(months)];
|
||||
i2c_controller(&self.p, ABRTCMC_I2C_ADR, Some(&txbuf), None, I2C_TIMEOUT);
|
||||
self.months = months;
|
||||
|
||||
txbuf = [ABRTCMC_YEARS, to_bcd(years)];
|
||||
i2c_controller(&self.p, ABRTCMC_I2C_ADR, Some(&txbuf), None, I2C_TIMEOUT);
|
||||
self.years = years;
|
||||
|
||||
txbuf = [ABRTCMC_WEEKDAYS, d.bits()];
|
||||
i2c_controller(&self.p, ABRTCMC_I2C_ADR, Some(&txbuf), None, I2C_TIMEOUT);
|
||||
self.weekday = d;
|
||||
|
||||
true // sanity check args would return false on fail
|
||||
}
|
||||
|
||||
pub fn rtc_update(&mut self) {
|
||||
let txbuf: [u8; 1];
|
||||
let mut rxbuf: [u8; 7] = [0; 7];
|
||||
|
||||
// only update from RTC if more than 1 second has passed since the last update
|
||||
if get_ticks(&self.p) - self.updated_ticks > 1000 {
|
||||
// read as a single block to make the time readout atomic
|
||||
txbuf = [ABRTCMC_SECONDS];
|
||||
i2c_controller(&self.p, ABRTCMC_I2C_ADR, Some(&txbuf), Some(&mut rxbuf), I2C_TIMEOUT);
|
||||
|
||||
self.seconds = to_binary(rxbuf[0] & 0x7f);
|
||||
self.minutes = to_binary(rxbuf[1] & 0x7f);
|
||||
self.hours = to_binary(rxbuf[2] & 0x3f);
|
||||
self.days = to_binary(rxbuf[3] & 0x3f);
|
||||
self.weekday = to_weekday(rxbuf[4] & 0x7f);
|
||||
self.months = to_binary(rxbuf[5] & 0x1f);
|
||||
self.years = to_binary(rxbuf[6]);
|
||||
|
||||
self.updated_ticks = get_ticks(&self.p);
|
||||
transaction.bus_addr = ABRTCMC_I2C_ADR;
|
||||
transaction.txbuf = Some(txbuf);
|
||||
transaction.txlen = 9;
|
||||
transaction.status = I2cStatus::RequestIncoming;
|
||||
match self.llio.send_i2c_request(transaction) {
|
||||
Ok(status) => {
|
||||
match status {
|
||||
I2cStatus::ResponseInProgress => Ok(true),
|
||||
I2cStatus::ResponseBusy => Err(xous::Error::ServerQueueFull),
|
||||
_ => Err(xous::Error::InternalError),
|
||||
}
|
||||
}
|
||||
_ => Err(xous::Error::InternalError)
|
||||
}
|
||||
}
|
||||
|
||||
/// testing-only routine -- wakeup self after designated number of seconds
|
||||
pub fn wakeup_alarm(&mut self, seconds: u8) {
|
||||
let mut txbuf: [u8; 2];
|
||||
|
||||
// make sure battery switchover is enabled
|
||||
txbuf = [ABRTCMC_CONTROL3, (Control3::BATT_STD_BL_EN).bits()];
|
||||
i2c_controller(&self.p, ABRTCMC_I2C_ADR, Some(&txbuf), None, I2C_TIMEOUT);
|
||||
|
||||
// set clock units to 1 second, output pulse length to ~218ms
|
||||
txbuf = [ABRTCMC_TIMERB_CLK, (TimerClk::CLK_1_S | TimerClk::PULSE_218_MS).bits()];
|
||||
i2c_controller(&self.p, ABRTCMC_I2C_ADR, Some(&txbuf), None, I2C_TIMEOUT);
|
||||
|
||||
// program elapsed time
|
||||
txbuf = [ABRTCMC_TIMERB, seconds];
|
||||
i2c_controller(&self.p, ABRTCMC_I2C_ADR, Some(&txbuf), None, I2C_TIMEOUT);
|
||||
|
||||
// enable timerb countdown interrupt, also clears any prior interrupt flag
|
||||
txbuf = [ABRTCMC_CONTROL2, (Control2::COUNTDOWN_B_INT).bits()];
|
||||
i2c_controller(&self.p, ABRTCMC_I2C_ADR, Some(&txbuf), None, I2C_TIMEOUT);
|
||||
|
||||
// turn on the timer proper -- the system will restart in 5...4..3....
|
||||
txbuf = [ABRTCMC_CONFIG, (Config::TIMER_B_ENABLE | Config::CLKOUT_DISABLE).bits()];
|
||||
i2c_controller(&self.p, ABRTCMC_I2C_ADR, Some(&txbuf), None, I2C_TIMEOUT);
|
||||
pub fn rtc_get(&mut self) -> Result<(), xous::Error> {
|
||||
let mut transaction = I2cTransaction::new();
|
||||
let mut txbuf = [0; 258];
|
||||
let rxbuf = [0; 258];
|
||||
txbuf[0] = ABRTCMC_SECONDS;
|
||||
transaction.bus_addr = ABRTCMC_I2C_ADR;
|
||||
transaction.txlen = 1;
|
||||
transaction.rxlen = 7;
|
||||
transaction.txbuf = Some(txbuf);
|
||||
transaction.rxbuf = Some(rxbuf);
|
||||
transaction.status = I2cStatus::RequestIncoming;
|
||||
match self.llio.send_i2c_request(transaction) {
|
||||
Ok(status) => {
|
||||
match status {
|
||||
I2cStatus::ResponseInProgress => Ok(()),
|
||||
I2cStatus::ResponseBusy => Err(xous::Error::ServerQueueFull),
|
||||
_ => Err(xous::Error::InternalError),
|
||||
}
|
||||
}
|
||||
_ => Err(xous::Error::InternalError)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn clear_alarm(&mut self) {
|
||||
// turn off RTC wakeup timer, in case previously set
|
||||
let mut txbuf : [u8; 2] = [ABRTCMC_CONFIG, (Config::CLKOUT_DISABLE).bits()];
|
||||
i2c_controller(&self.p, ABRTCMC_I2C_ADR, Some(&txbuf), None, I2C_TIMEOUT);
|
||||
fn blocking_i2c_write2(&self, tx: [u8; 2]) -> bool {
|
||||
let mut transaction = I2cTransaction::new();
|
||||
let mut txbuf: [u8; 258] = [0; 258];
|
||||
txbuf[0] = tx[0];
|
||||
txbuf[1] = tx[1];
|
||||
transaction.bus_addr = ABRTCMC_I2C_ADR;
|
||||
transaction.txbuf = Some(txbuf);
|
||||
transaction.txlen = 2;
|
||||
transaction.status = I2cStatus::RequestIncoming;
|
||||
|
||||
// clear all interrupts and flags
|
||||
txbuf = [ABRTCMC_CONTROL2, 0];
|
||||
i2c_controller(&self.p, ABRTCMC_I2C_ADR, Some(&txbuf), None, I2C_TIMEOUT);
|
||||
while self.llio.poll_i2c_busy().unwrap() {
|
||||
xous::yield_slice();
|
||||
}
|
||||
let mut sent = false;
|
||||
while !sent {
|
||||
match self.llio.send_i2c_request(transaction) {
|
||||
Ok(status) => {
|
||||
match status {
|
||||
I2cStatus::ResponseInProgress => sent = true,
|
||||
I2cStatus::ResponseBusy => sent = false,
|
||||
_ => {log::error!("try_send_i2c unhandled response"); return false;},
|
||||
}
|
||||
}
|
||||
_ => {log::error!("try_send_i2c unhandled error"); return false;}
|
||||
}
|
||||
xous::yield_slice();
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
/// wakeup self after designated number of seconds
|
||||
pub fn wakeup_alarm(&mut self, seconds: u8) {
|
||||
self.wakeup_alarm_enabled = true;
|
||||
|
||||
// make sure battery switchover is enabled, otherwise we won't keep time when power goes off
|
||||
self.blocking_i2c_write2([ABRTCMC_CONTROL3, (Control3::BATT_STD_BL_EN).bits()]);
|
||||
|
||||
// set clock units to 1 second, output pulse length to ~218ms
|
||||
self.blocking_i2c_write2([ABRTCMC_TIMERB_CLK, (TimerClk::CLK_1_S | TimerClk::PULSE_218_MS).bits()]);
|
||||
|
||||
// program elapsed time
|
||||
self.blocking_i2c_write2([ABRTCMC_TIMERB, seconds]);
|
||||
|
||||
// enable timerb countdown interrupt, also clears any prior interrupt flag
|
||||
let mut control2 = (Control2::COUNTDOWN_B_INT).bits();
|
||||
if self.rtc_alarm_enabled {
|
||||
control2 |= Control2::COUNTDOWN_A_INT.bits();
|
||||
}
|
||||
self.blocking_i2c_write2([ABRTCMC_CONTROL2, control2]);
|
||||
|
||||
// turn on the timer proper -- the system will wakeup in 5..4..3....
|
||||
let mut config = (Config::CLKOUT_DISABLE | Config::TIMER_B_ENABLE | Config::TIMERB_INT_PULSED).bits();
|
||||
if self.rtc_alarm_enabled {
|
||||
config |= (Config::TIMER_A_COUNTDWN | Config::TIMERA_SECONDS_INT_PULSED).bits();
|
||||
}
|
||||
self.blocking_i2c_write2([ABRTCMC_CONFIG, config]);
|
||||
}
|
||||
|
||||
pub fn clear_wakeup_alarm(&mut self) {
|
||||
self.wakeup_alarm_enabled = false;
|
||||
|
||||
let mut config = Config::CLKOUT_DISABLE.bits();
|
||||
if self.rtc_alarm_enabled {
|
||||
config |= (Config::TIMER_A_COUNTDWN | Config::TIMERA_SECONDS_INT_PULSED).bits();
|
||||
}
|
||||
// turn off RTC wakeup timer, in case previously set
|
||||
self.blocking_i2c_write2([ABRTCMC_CONFIG, config]);
|
||||
|
||||
// clear my interrupts and flags
|
||||
let mut control2 = 0;
|
||||
if self.rtc_alarm_enabled {
|
||||
control2 |= Control2::COUNTDOWN_A_INT.bits();
|
||||
}
|
||||
self.blocking_i2c_write2([ABRTCMC_CONTROL2, control2]);
|
||||
}
|
||||
|
||||
|
||||
|
||||
pub fn rtc_alarm(&mut self, seconds: u8) {
|
||||
self.rtc_alarm_enabled = true;
|
||||
// make sure battery switchover is enabled, otherwise we won't keep time when power goes off
|
||||
self.blocking_i2c_write2([ABRTCMC_CONTROL3, (Control3::BATT_STD_BL_EN).bits()]);
|
||||
|
||||
// set clock units to 1 second, output pulse length to ~218ms
|
||||
self.blocking_i2c_write2([ABRTCMC_TIMERA_CLK, (TimerClk::CLK_1_S | TimerClk::PULSE_218_MS).bits()]);
|
||||
|
||||
// program elapsed time
|
||||
self.blocking_i2c_write2([ABRTCMC_TIMERA, seconds]);
|
||||
|
||||
// enable timerb countdown interrupt, also clears any prior interrupt flag
|
||||
let mut control2 = (Control2::COUNTDOWN_A_INT).bits();
|
||||
if self.wakeup_alarm_enabled {
|
||||
control2 |= Control2::COUNTDOWN_B_INT.bits();
|
||||
}
|
||||
self.blocking_i2c_write2([ABRTCMC_CONTROL2, control2]);
|
||||
|
||||
// turn on the timer proper -- interrupt in 5..4..3....
|
||||
let mut config = (Config::CLKOUT_DISABLE | Config::TIMER_A_COUNTDWN | Config::TIMERA_SECONDS_INT_PULSED).bits();
|
||||
if self.wakeup_alarm_enabled {
|
||||
config |= (Config::TIMER_B_ENABLE | Config::TIMERB_INT_PULSED).bits();
|
||||
}
|
||||
self.blocking_i2c_write2([ABRTCMC_CONFIG, config]);
|
||||
}
|
||||
|
||||
pub fn clear_rtc_alarm(&mut self) {
|
||||
self.rtc_alarm_enabled = false;
|
||||
// turn off RTC wakeup timer, in case previously set
|
||||
let mut config = Config::CLKOUT_DISABLE.bits();
|
||||
if self.wakeup_alarm_enabled {
|
||||
config |= (Config::TIMER_B_ENABLE | Config::TIMERB_INT_PULSED).bits();
|
||||
}
|
||||
self.blocking_i2c_write2([ABRTCMC_CONFIG, config]);
|
||||
|
||||
// clear my interrupts and flags
|
||||
let mut control2 = 0;
|
||||
if self.wakeup_alarm_enabled {
|
||||
control2 |= Control2::COUNTDOWN_B_INT.bits();
|
||||
}
|
||||
self.blocking_i2c_write2([ABRTCMC_CONTROL2, control2]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -369,39 +484,146 @@ mod implementation {
|
|||
}
|
||||
}
|
||||
|
||||
static mut CB_TO_MAIN_CONN: Option<CID> = None;
|
||||
#[xous::xous_main]
|
||||
fn xmain() -> ! {
|
||||
use crate::implementation::Rtc;
|
||||
|
||||
log_server::init_wait().unwrap();
|
||||
log::set_max_level(log::LevelFilter::Trace);
|
||||
log::info!("RTC: my PID is {}", xous::process::id());
|
||||
log::info!("my PID is {}", xous::process::id());
|
||||
|
||||
let xns = xous_names::XousNames::new().unwrap();
|
||||
let rtc_sid = xns.register_name(api::SERVER_NAME_RTC).expect("RTC: can't register server");
|
||||
log::trace!("RTC: registered with NS -- {:?}", rtc_sid);
|
||||
let rtc_sid = xns.register_name(api::SERVER_NAME_RTC).expect("can't register server");
|
||||
log::trace!("registered with NS -- {:?}", rtc_sid);
|
||||
unsafe{CB_TO_MAIN_CONN = Some(xous::connect(rtc_sid).unwrap())};
|
||||
|
||||
#[cfg(target_os = "none")]
|
||||
let rtc = Rtc::new();
|
||||
let mut rtc = Rtc::new(&xns);
|
||||
|
||||
#[cfg(not(target_os = "none"))]
|
||||
let mut rtc = Rtc::new();
|
||||
|
||||
let mut cb_conns: [Option<CID>; 32] = [None; 32];
|
||||
log::trace!("RTC: ready to accept requests");
|
||||
let ticktimer = ticktimer_server::Ticktimer::new().expect("can't connect to ticktimer");
|
||||
let mut dt_cb_conns: [Option<CID>; 32] = [None; 32];
|
||||
log::trace!("ready to accept requests");
|
||||
loop {
|
||||
let msg = xous::receive_message(rtc_sid).unwrap();
|
||||
log::trace!("Message: {:?}", msg);
|
||||
match FromPrimitive::from_usize(msg.body.id()) {
|
||||
Some(Opcode::SetDateTime) => {
|
||||
let buffer = unsafe { Buffer::from_memory_message(msg.body.memory_message().unwrap()) };
|
||||
let dt = buffer.as_flat::<DateTime, _>().unwrap();
|
||||
let dt = buffer.to_original::<DateTime, _>().unwrap();
|
||||
let mut sent = false;
|
||||
while !sent {
|
||||
match rtc.rtc_set(
|
||||
dt.seconds,
|
||||
dt.minutes,
|
||||
dt.hours,
|
||||
dt.days,
|
||||
dt.months,
|
||||
dt.years,
|
||||
dt.weekday,
|
||||
) {
|
||||
Ok(true) => {sent = true;},
|
||||
Ok(false) => {sent = true; log::error!("badly formatted arguments setting RTC date and time");}
|
||||
Err(xous::Error::ServerQueueFull) => {
|
||||
sent = false;
|
||||
log::trace!("I2C interface was busy setting date and time, retrying");
|
||||
ticktimer.sleep_ms(1).unwrap(); // wait a quanta of time and then retry
|
||||
},
|
||||
_ => {
|
||||
sent = true;
|
||||
log::error!("error setting RTC date time");
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
Some(Opcode::RegisterDateTimeCallback) => msg_scalar_unpack!(msg, sid0, sid1, sid2, sid3, {
|
||||
let sid = xous::SID::from_u32(sid0 as _, sid1 as _, sid2 as _, sid3 as _);
|
||||
let cid = Some(xous::connect(sid).unwrap());
|
||||
let mut found = false;
|
||||
for entry in dt_cb_conns.iter_mut() {
|
||||
if *entry == None {
|
||||
*entry = cid;
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
log::error!("RegisterDateTimeCallback listener ran out of space registering callback");
|
||||
}
|
||||
}),
|
||||
Some(Opcode::ResponseDateTime) => {
|
||||
let incoming_buffer = unsafe { Buffer::from_memory_message(msg.body.memory_message().unwrap()) };
|
||||
let dt = incoming_buffer.to_original::<DateTime, _>().unwrap();
|
||||
let ret = Return::ReturnDateTime(dt);
|
||||
let outgoing_buf = Buffer::into_buf(ret).or(Err(xous::Error::InternalError)).unwrap();
|
||||
for maybe_conn in dt_cb_conns.iter_mut() {
|
||||
if let Some(conn) = maybe_conn {
|
||||
match outgoing_buf.lend(*conn, 0) { // the ID field is ignored on the callback server
|
||||
Err(xous::Error::ServerNotFound) => {
|
||||
*maybe_conn = None
|
||||
},
|
||||
Ok(_) => {},
|
||||
_ => panic!("unhandled error or result in callback processing")
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
Some(Opcode::RequestDateTime) => {
|
||||
let mut sent = false;
|
||||
while !sent {
|
||||
match rtc.rtc_get() {
|
||||
Ok(_) => sent = true,
|
||||
Err(xous::Error::ServerQueueFull) => {
|
||||
sent = false;
|
||||
log::trace!("I2C interface was busy getting date and time, retrying");
|
||||
ticktimer.sleep_ms(1).unwrap();
|
||||
},
|
||||
_ => {
|
||||
sent = true;
|
||||
log::error!("error requesting RTC date and time");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Some(Opcode::SetWakeupAlarm) => msg_scalar_unpack!(msg, delay, _, _, _, {
|
||||
rtc.wakeup_alarm(delay as u8); // this will block until finished, no callbacks used
|
||||
}),
|
||||
Some(Opcode::ClearWakeupAlarm) => msg_scalar_unpack!(msg, _, _, _, _, {
|
||||
rtc.clear_wakeup_alarm(); // blocks until transaction is finished
|
||||
}),
|
||||
Some(Opcode::SetRtcAlarm) => msg_scalar_unpack!(msg, delay, _, _, _, {
|
||||
rtc.rtc_alarm(delay as u8); // this will block until finished, no callbacks used
|
||||
}),
|
||||
Some(Opcode::ClearRtcAlarm) => msg_scalar_unpack!(msg, _, _, _, _, {
|
||||
rtc.clear_rtc_alarm(); // blocks until transaction is finished
|
||||
}),
|
||||
None => {
|
||||
log::error!("unknown opcode received, exiting");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
// clean up our program
|
||||
log::trace!("main loop exit, destroying servers");
|
||||
unsafe{
|
||||
if let Some(cb)= CB_TO_MAIN_CONN {
|
||||
xous::disconnect(cb).unwrap();
|
||||
}
|
||||
}
|
||||
for entry in dt_cb_conns.iter_mut() {
|
||||
if let Some(conn) = entry {
|
||||
let dropmsg = Return::Drop;
|
||||
let buf = Buffer::into_buf(dropmsg).unwrap();
|
||||
buf.lend(*conn, 0).unwrap(); // the ID is ignored for this server
|
||||
unsafe{xous::disconnect(*conn).unwrap();}
|
||||
}
|
||||
*entry = None;
|
||||
}
|
||||
xns.unregister_server(rtc_sid).unwrap();
|
||||
xous::destroy_server(rtc_sid).unwrap();
|
||||
log::trace!("quitting");
|
||||
xous::terminate_process(); loop {}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -21,7 +21,7 @@ utralib = {path = "../../utralib"}
|
|||
[features]
|
||||
debug-print = []
|
||||
watchdog = []
|
||||
# generally you want to disable watchdog if you're doing development.
|
||||
# generally you want to disable watchdog if you're doing development
|
||||
default = ["watchdog"]
|
||||
#default = []
|
||||
# default = []
|
||||
# default = ["debug-print"]
|
||||
|
|
|
|||
|
|
@ -1328,10 +1328,10 @@ register to disable individual events.]]></description>
|
|||
<register>
|
||||
<name>GIT_GITREV</name>
|
||||
<description><![CDATA[First 32-bits of the git revision. This documentation was built from git rev
|
||||
``8561717c``, so this value is 2237755772, which should be enough to check out
|
||||
``79ae30c6``, so this value is 2041458886, which should be enough to check out
|
||||
the exact git version used to build this firmware.]]></description>
|
||||
<addressOffset>0x0014</addressOffset>
|
||||
<resetValue>0x8561717c</resetValue>
|
||||
<resetValue>0x79ae30c6</resetValue>
|
||||
<size>32</size>
|
||||
<fields>
|
||||
<field>
|
||||
|
|
@ -1348,7 +1348,7 @@ the exact git version used to build this firmware.]]></description>
|
|||
is ``1``, then the repository this was built from has one additional commit
|
||||
beyond the tag indicated in `MAJOR`, `MINOR`, and `REVISION`.]]></description>
|
||||
<addressOffset>0x0018</addressOffset>
|
||||
<resetValue>0x0f</resetValue>
|
||||
<resetValue>0x10</resetValue>
|
||||
<size>32</size>
|
||||
<fields>
|
||||
<field>
|
||||
|
|
@ -2550,6 +2550,10 @@ reset fashion]]></description>
|
|||
<size>0x14</size>
|
||||
<usage>registers</usage>
|
||||
</addressBlock>
|
||||
<interrupt>
|
||||
<name>power</name>
|
||||
<value>9</value>
|
||||
</interrupt>
|
||||
</peripheral>
|
||||
<peripheral>
|
||||
<name>SPINOR</name>
|
||||
|
|
@ -3044,7 +3048,7 @@ again until the interrupt is serviced.]]></description>
|
|||
</addressBlock>
|
||||
<interrupt>
|
||||
<name>keyboard</name>
|
||||
<value>9</value>
|
||||
<value>10</value>
|
||||
</interrupt>
|
||||
</peripheral>
|
||||
<peripheral>
|
||||
|
|
@ -3056,7 +3060,7 @@ again until the interrupt is serviced.]]></description>
|
|||
<name>SEED1</name>
|
||||
<description><![CDATA[Bits 32-63 of `SEED_SEED`. Seed used for the build]]></description>
|
||||
<addressOffset>0x0000</addressOffset>
|
||||
<resetValue>0x62e0ffce</resetValue>
|
||||
<resetValue>0x505c16c1</resetValue>
|
||||
<size>32</size>
|
||||
<fields>
|
||||
<field>
|
||||
|
|
@ -3071,7 +3075,7 @@ again until the interrupt is serviced.]]></description>
|
|||
<name>SEED0</name>
|
||||
<description><![CDATA[Bits 0-31 of `SEED_SEED`.]]></description>
|
||||
<addressOffset>0x0004</addressOffset>
|
||||
<resetValue>0x629a6ab2</resetValue>
|
||||
<resetValue>0x578f7014</resetValue>
|
||||
<size>32</size>
|
||||
<fields>
|
||||
<field>
|
||||
|
|
@ -3500,7 +3504,7 @@ I2S_FORMAT.I2S_STANDARD is I2S standard]]></description>
|
|||
</addressBlock>
|
||||
<interrupt>
|
||||
<name>audio</name>
|
||||
<value>10</value>
|
||||
<value>11</value>
|
||||
</interrupt>
|
||||
</peripheral>
|
||||
<peripheral>
|
||||
|
|
@ -3636,7 +3640,7 @@ I2S_FORMAT.I2S_STANDARD is I2S standard]]></description>
|
|||
</addressBlock>
|
||||
<interrupt>
|
||||
<name>trng_kernel</name>
|
||||
<value>11</value>
|
||||
<value>12</value>
|
||||
</interrupt>
|
||||
</peripheral>
|
||||
<peripheral>
|
||||
|
|
@ -3968,7 +3972,7 @@ to max field size]]></description>
|
|||
</addressBlock>
|
||||
<interrupt>
|
||||
<name>trng_server</name>
|
||||
<value>12</value>
|
||||
<value>13</value>
|
||||
</interrupt>
|
||||
</peripheral>
|
||||
<peripheral>
|
||||
|
|
@ -4776,7 +4780,7 @@ in bits]]></description>
|
|||
</addressBlock>
|
||||
<interrupt>
|
||||
<name>sha512</name>
|
||||
<value>13</value>
|
||||
<value>14</value>
|
||||
</interrupt>
|
||||
</peripheral>
|
||||
<peripheral>
|
||||
|
|
@ -5005,7 +5009,7 @@ exactly reflects the binary layout of an Engine instruction.]]></description>
|
|||
</addressBlock>
|
||||
<interrupt>
|
||||
<name>engine</name>
|
||||
<value>14</value>
|
||||
<value>15</value>
|
||||
</interrupt>
|
||||
</peripheral>
|
||||
<peripheral>
|
||||
|
|
@ -5223,7 +5227,7 @@ register to disable individual events.]]></description>
|
|||
</addressBlock>
|
||||
<interrupt>
|
||||
<name>wdt</name>
|
||||
<value>15</value>
|
||||
<value>16</value>
|
||||
</interrupt>
|
||||
</peripheral>
|
||||
</peripherals>
|
||||
|
|
@ -5294,21 +5298,22 @@ register to disable individual events.]]></description>
|
|||
<constant name="CONFIG_BUS_DATA_WIDTH" value="32" />
|
||||
<constant name="CONFIG_BUS_ADDRESS_WIDTH" value="32" />
|
||||
<constant name="APP_UART_INTERRUPT" value="4" />
|
||||
<constant name="AUDIO_INTERRUPT" value="10" />
|
||||
<constant name="AUDIO_INTERRUPT" value="11" />
|
||||
<constant name="BTEVENTS_INTERRUPT" value="7" />
|
||||
<constant name="COM_INTERRUPT" value="5" />
|
||||
<constant name="CONSOLE_INTERRUPT" value="3" />
|
||||
<constant name="ENGINE_INTERRUPT" value="14" />
|
||||
<constant name="ENGINE_INTERRUPT" value="15" />
|
||||
<constant name="GPIO_INTERRUPT" value="1" />
|
||||
<constant name="I2C_INTERRUPT" value="6" />
|
||||
<constant name="KEYBOARD_INTERRUPT" value="9" />
|
||||
<constant name="SHA512_INTERRUPT" value="13" />
|
||||
<constant name="KEYBOARD_INTERRUPT" value="10" />
|
||||
<constant name="POWER_INTERRUPT" value="9" />
|
||||
<constant name="SHA512_INTERRUPT" value="14" />
|
||||
<constant name="TICKTIMER_INTERRUPT" value="8" />
|
||||
<constant name="TIMER0_INTERRUPT" value="0" />
|
||||
<constant name="TRNG_KERNEL_INTERRUPT" value="11" />
|
||||
<constant name="TRNG_SERVER_INTERRUPT" value="12" />
|
||||
<constant name="TRNG_KERNEL_INTERRUPT" value="12" />
|
||||
<constant name="TRNG_SERVER_INTERRUPT" value="13" />
|
||||
<constant name="UART_INTERRUPT" value="2" />
|
||||
<constant name="WDT_INTERRUPT" value="15" />
|
||||
<constant name="WDT_INTERRUPT" value="16" />
|
||||
</constants>
|
||||
</vendorExtensions>
|
||||
</device>
|
||||
|
|
@ -49,6 +49,7 @@ fn try_main() -> Result<(), DynError> {
|
|||
"keyboard",
|
||||
"trng",
|
||||
"llio",
|
||||
"rtc",
|
||||
];
|
||||
let fcc_pkgs = [
|
||||
"fcc-agent",
|
||||
|
|
|
|||
Loading…
Reference in a new issue