2021-01-27 05:33:03 +00:00
#![ cfg_attr(target_os = " none " , no_std) ]
#![ cfg_attr(target_os = " none " , no_main) ]
mod api ;
2021-04-06 11:32:35 +00:00
use api ::* ;
2021-03-15 16:11:23 +00:00
mod i2c ;
2022-03-23 18:19:26 +00:00
#[ cfg(any(target_os = " none " , target_os = " xous " )) ]
mod llio_hw ;
#[ cfg(any(target_os = " none " , target_os = " xous " )) ]
use llio_hw ::* ;
#[ cfg(not(any(target_os = " none " , target_os = " xous " ))) ]
mod llio_hosted ;
#[ cfg(not(any(target_os = " none " , target_os = " xous " ))) ]
use llio_hosted ::* ;
2021-01-27 05:33:03 +00:00
2022-01-11 07:45:20 +00:00
use num_traits ::* ;
2021-04-06 16:48:48 +00:00
use xous_ipc ::Buffer ;
2021-04-06 11:32:35 +00:00
use xous ::{ CID , msg_scalar_unpack , msg_blocking_scalar_unpack } ;
2021-01-27 05:33:03 +00:00
2022-01-11 07:45:20 +00:00
use std ::thread ;
fn i2c_thread ( i2c_sid : xous ::SID ) {
2021-05-05 16:45:14 +00:00
let xns = xous_names ::XousNames ::new ( ) . unwrap ( ) ;
let handler_conn = xous ::connect ( i2c_sid ) . expect ( " couldn't make handler connection for i2c " ) ;
let mut i2c = i2c ::I2cStateMachine ::new ( handler_conn ) ;
2021-05-03 10:14:58 +00:00
2021-05-05 16:45:14 +00:00
// register a suspend/resume listener
let sr_cid = xous ::connect ( i2c_sid ) . expect ( " couldn't create suspend callback connection " ) ;
2022-01-10 05:45:56 +00:00
let mut susres = susres ::Susres ::new ( None , & xns , I2cOpcode ::SuspendResume as u32 , sr_cid ) . expect ( " couldn't create suspend/resume object " ) ;
2021-05-05 16:45:14 +00:00
another fix for issue #75: llio resume failure
Found the following KP on resume. This time it looks like it's
failing in a syscall, during a send_message call. There's no
more visibility into what causes this, so this commit tries to clean
up some loose ends in the resume path that could send messages before
the system is ready. Namely, the console mux call got condensed into
an abridged version that doesn't send log messages (you couldn't see
them anyways...), and the I2C engine now pauses suspend until
the current transaction at hand completes. I suspect it's quite
likely that it's the I2C engine that's causing the issues...
LLIO -> PID 14
Jumping to loader...
PROGRAM HALT: CPU Exception on PID 14: Store page fault of 0x00000000 at 0x0003a02e
Current thread 2:
Thread 2:
PC:0003a02e SP:7ffff170 RA:00036290
GP:00000000 TP:60000000
T0:00000000 T1:000000f0 T2:00000030
T3:00000080 T4:000000f4 T5:ffffffff T6:0000008f
S0:7ffff1d0 S1:7ffff228 S2:00000007 S3:00000007
S4:00000001 S5:00000004 S6:00037996 S7:00010e34
S8:00011548 S9:000101bc S10:7fffff4c S11:0003c000
A0:0000000e A1:00000000 A2:00000004 A3:00000000
A4:00000000 A5:00000000 A6:00000000 A7:00000000
0003a028 <_xous_syscall>:
3a028: 00000073 ecall
3a02c: 4282 lw t0,0(sp)
3a02e: 00a2a023 sw a0,0(t0)
3a032: 00b2a223 sw a1,4(t0)
3a036: 00c2a423 sw a2,8(t0)
3a03a: 00d2a623 sw a3,12(t0)
3a03e: 00e2a823 sw a4,16(t0)
3a042: 00f2aa23 sw a5,20(t0)
3a046: 0102ac23 sw a6,24(t0)
3a04a: 0112ae23 sw a7,28(t0)
3a04e: 8082 ret
0003623c <_ZN4xous7syscall12send_message17h44786a68889a5f1dE>:
pub fn send_message(connection: CID, message: Message) -> core::result::Result<Result, Error> {
3623c: 7175 addi sp,sp,-144
3623e: c706 sw ra,140(sp)
36240: c522 sw s0,136(sp)
36242: c326 sw s1,132(sp)
36244: 84aa mv s1,a0
36246: 4549 li a0,18
crate::arch::syscall(call)
36248: 4214 lw a3,0(a2)
3624a: 4258 lw a4,4(a2)
3624c: 04a10023 sb a0,64(sp)
36250: c2ae sw a1,68(sp)
36252: c4b6 sw a3,72(sp)
36254: c6ba sw a4,76(sp)
36256: 4608 lw a0,8(a2)
36258: 464c lw a1,12(a2)
3625a: 4a14 lw a3,16(a2)
3625c: 4a50 lw a2,20(a2)
3625e: c8aa sw a0,80(sp)
36260: caae sw a1,84(sp)
36262: ccb6 sw a3,88(sp)
36264: ceb2 sw a2,92(sp)
let mut ret = crate::Result::Ok;
36266: d082 sw zero,96(sp)
let args = call.as_args();
36268: 0828 addi a0,sp,24
3626a: 008c addi a1,sp,64
3626c: 00000097 auipc ra,0x0
36270: 8da080e7 jalr -1830(ra) # 35b46 <_ZN4xous7syscall7SysCall7as_args17h7e6c318e8a926fa8E>
args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], &mut ret,
36274: 4562 lw a0,24(sp)
36276: 45f2 lw a1,28(sp)
36278: 5602 lw a2,32(sp)
3627a: 5692 lw a3,36(sp)
3627c: 5722 lw a4,40(sp)
3627e: 57b2 lw a5,44(sp)
36280: 5842 lw a6,48(sp)
36282: 58d2 lw a7,52(sp)
36284: 1080 addi s0,sp,96
_xous_syscall(
36286: c022 sw s0,0(sp)
36288: 00004097 auipc ra,0x4
3628c: da0080e7 jalr -608(ra) # 3a028 <_xous_syscall>
crate::Result::Error(e) => Err(e),
36290: 5586 lw a1,96(sp)
36292: 5516 lw a0,100(sp)
36294: 4605 li a2,1
36296: 00c59563 bne a1,a2,362a0 <_ZN4xous7syscall12send_message17h44786a68889a5f1dE+0x64>
Err(e) => Err(e),
3629a: c0c8 sw a0,4(s1)
2021-08-23 17:39:48 +00:00
let mut suspend_pending_token : Option < usize > = None ;
2021-06-24 19:41:42 +00:00
log ::trace! ( " starting i2c main loop " ) ;
2021-05-05 16:45:14 +00:00
loop {
let mut msg = xous ::receive_message ( i2c_sid ) . unwrap ( ) ;
2021-06-24 19:41:42 +00:00
log ::trace! ( " i2c message: {:?} " , msg ) ;
2021-05-05 16:45:14 +00:00
match FromPrimitive ::from_usize ( msg . body . id ( ) ) {
Some ( I2cOpcode ::SuspendResume ) = > xous ::msg_scalar_unpack! ( msg , token , _ , _ , _ , {
another fix for issue #75: llio resume failure
Found the following KP on resume. This time it looks like it's
failing in a syscall, during a send_message call. There's no
more visibility into what causes this, so this commit tries to clean
up some loose ends in the resume path that could send messages before
the system is ready. Namely, the console mux call got condensed into
an abridged version that doesn't send log messages (you couldn't see
them anyways...), and the I2C engine now pauses suspend until
the current transaction at hand completes. I suspect it's quite
likely that it's the I2C engine that's causing the issues...
LLIO -> PID 14
Jumping to loader...
PROGRAM HALT: CPU Exception on PID 14: Store page fault of 0x00000000 at 0x0003a02e
Current thread 2:
Thread 2:
PC:0003a02e SP:7ffff170 RA:00036290
GP:00000000 TP:60000000
T0:00000000 T1:000000f0 T2:00000030
T3:00000080 T4:000000f4 T5:ffffffff T6:0000008f
S0:7ffff1d0 S1:7ffff228 S2:00000007 S3:00000007
S4:00000001 S5:00000004 S6:00037996 S7:00010e34
S8:00011548 S9:000101bc S10:7fffff4c S11:0003c000
A0:0000000e A1:00000000 A2:00000004 A3:00000000
A4:00000000 A5:00000000 A6:00000000 A7:00000000
0003a028 <_xous_syscall>:
3a028: 00000073 ecall
3a02c: 4282 lw t0,0(sp)
3a02e: 00a2a023 sw a0,0(t0)
3a032: 00b2a223 sw a1,4(t0)
3a036: 00c2a423 sw a2,8(t0)
3a03a: 00d2a623 sw a3,12(t0)
3a03e: 00e2a823 sw a4,16(t0)
3a042: 00f2aa23 sw a5,20(t0)
3a046: 0102ac23 sw a6,24(t0)
3a04a: 0112ae23 sw a7,28(t0)
3a04e: 8082 ret
0003623c <_ZN4xous7syscall12send_message17h44786a68889a5f1dE>:
pub fn send_message(connection: CID, message: Message) -> core::result::Result<Result, Error> {
3623c: 7175 addi sp,sp,-144
3623e: c706 sw ra,140(sp)
36240: c522 sw s0,136(sp)
36242: c326 sw s1,132(sp)
36244: 84aa mv s1,a0
36246: 4549 li a0,18
crate::arch::syscall(call)
36248: 4214 lw a3,0(a2)
3624a: 4258 lw a4,4(a2)
3624c: 04a10023 sb a0,64(sp)
36250: c2ae sw a1,68(sp)
36252: c4b6 sw a3,72(sp)
36254: c6ba sw a4,76(sp)
36256: 4608 lw a0,8(a2)
36258: 464c lw a1,12(a2)
3625a: 4a14 lw a3,16(a2)
3625c: 4a50 lw a2,20(a2)
3625e: c8aa sw a0,80(sp)
36260: caae sw a1,84(sp)
36262: ccb6 sw a3,88(sp)
36264: ceb2 sw a2,92(sp)
let mut ret = crate::Result::Ok;
36266: d082 sw zero,96(sp)
let args = call.as_args();
36268: 0828 addi a0,sp,24
3626a: 008c addi a1,sp,64
3626c: 00000097 auipc ra,0x0
36270: 8da080e7 jalr -1830(ra) # 35b46 <_ZN4xous7syscall7SysCall7as_args17h7e6c318e8a926fa8E>
args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], &mut ret,
36274: 4562 lw a0,24(sp)
36276: 45f2 lw a1,28(sp)
36278: 5602 lw a2,32(sp)
3627a: 5692 lw a3,36(sp)
3627c: 5722 lw a4,40(sp)
3627e: 57b2 lw a5,44(sp)
36280: 5842 lw a6,48(sp)
36282: 58d2 lw a7,52(sp)
36284: 1080 addi s0,sp,96
_xous_syscall(
36286: c022 sw s0,0(sp)
36288: 00004097 auipc ra,0x4
3628c: da0080e7 jalr -608(ra) # 3a028 <_xous_syscall>
crate::Result::Error(e) => Err(e),
36290: 5586 lw a1,96(sp)
36292: 5516 lw a0,100(sp)
36294: 4605 li a2,1
36296: 00c59563 bne a1,a2,362a0 <_ZN4xous7syscall12send_message17h44786a68889a5f1dE+0x64>
Err(e) => Err(e),
3629a: c0c8 sw a0,4(s1)
2021-08-23 17:39:48 +00:00
if ! i2c . is_busy ( ) {
i2c . suspend ( ) ;
susres . suspend_until_resume ( token ) . expect ( " couldn't execute suspend/resume " ) ;
i2c . resume ( ) ;
} else {
// stash the token, and we'll do the suspend once the I2C transaction is done.
suspend_pending_token = Some ( token ) ;
}
2021-05-05 16:45:14 +00:00
} ) ,
Some ( I2cOpcode ::IrqI2cTxrxWriteDone ) = > msg_scalar_unpack! ( msg , _ , _ , _ , _ , {
another fix for issue #75: llio resume failure
Found the following KP on resume. This time it looks like it's
failing in a syscall, during a send_message call. There's no
more visibility into what causes this, so this commit tries to clean
up some loose ends in the resume path that could send messages before
the system is ready. Namely, the console mux call got condensed into
an abridged version that doesn't send log messages (you couldn't see
them anyways...), and the I2C engine now pauses suspend until
the current transaction at hand completes. I suspect it's quite
likely that it's the I2C engine that's causing the issues...
LLIO -> PID 14
Jumping to loader...
PROGRAM HALT: CPU Exception on PID 14: Store page fault of 0x00000000 at 0x0003a02e
Current thread 2:
Thread 2:
PC:0003a02e SP:7ffff170 RA:00036290
GP:00000000 TP:60000000
T0:00000000 T1:000000f0 T2:00000030
T3:00000080 T4:000000f4 T5:ffffffff T6:0000008f
S0:7ffff1d0 S1:7ffff228 S2:00000007 S3:00000007
S4:00000001 S5:00000004 S6:00037996 S7:00010e34
S8:00011548 S9:000101bc S10:7fffff4c S11:0003c000
A0:0000000e A1:00000000 A2:00000004 A3:00000000
A4:00000000 A5:00000000 A6:00000000 A7:00000000
0003a028 <_xous_syscall>:
3a028: 00000073 ecall
3a02c: 4282 lw t0,0(sp)
3a02e: 00a2a023 sw a0,0(t0)
3a032: 00b2a223 sw a1,4(t0)
3a036: 00c2a423 sw a2,8(t0)
3a03a: 00d2a623 sw a3,12(t0)
3a03e: 00e2a823 sw a4,16(t0)
3a042: 00f2aa23 sw a5,20(t0)
3a046: 0102ac23 sw a6,24(t0)
3a04a: 0112ae23 sw a7,28(t0)
3a04e: 8082 ret
0003623c <_ZN4xous7syscall12send_message17h44786a68889a5f1dE>:
pub fn send_message(connection: CID, message: Message) -> core::result::Result<Result, Error> {
3623c: 7175 addi sp,sp,-144
3623e: c706 sw ra,140(sp)
36240: c522 sw s0,136(sp)
36242: c326 sw s1,132(sp)
36244: 84aa mv s1,a0
36246: 4549 li a0,18
crate::arch::syscall(call)
36248: 4214 lw a3,0(a2)
3624a: 4258 lw a4,4(a2)
3624c: 04a10023 sb a0,64(sp)
36250: c2ae sw a1,68(sp)
36252: c4b6 sw a3,72(sp)
36254: c6ba sw a4,76(sp)
36256: 4608 lw a0,8(a2)
36258: 464c lw a1,12(a2)
3625a: 4a14 lw a3,16(a2)
3625c: 4a50 lw a2,20(a2)
3625e: c8aa sw a0,80(sp)
36260: caae sw a1,84(sp)
36262: ccb6 sw a3,88(sp)
36264: ceb2 sw a2,92(sp)
let mut ret = crate::Result::Ok;
36266: d082 sw zero,96(sp)
let args = call.as_args();
36268: 0828 addi a0,sp,24
3626a: 008c addi a1,sp,64
3626c: 00000097 auipc ra,0x0
36270: 8da080e7 jalr -1830(ra) # 35b46 <_ZN4xous7syscall7SysCall7as_args17h7e6c318e8a926fa8E>
args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], &mut ret,
36274: 4562 lw a0,24(sp)
36276: 45f2 lw a1,28(sp)
36278: 5602 lw a2,32(sp)
3627a: 5692 lw a3,36(sp)
3627c: 5722 lw a4,40(sp)
3627e: 57b2 lw a5,44(sp)
36280: 5842 lw a6,48(sp)
36282: 58d2 lw a7,52(sp)
36284: 1080 addi s0,sp,96
_xous_syscall(
36286: c022 sw s0,0(sp)
36288: 00004097 auipc ra,0x4
3628c: da0080e7 jalr -608(ra) # 3a028 <_xous_syscall>
crate::Result::Error(e) => Err(e),
36290: 5586 lw a1,96(sp)
36292: 5516 lw a0,100(sp)
36294: 4605 li a2,1
36296: 00c59563 bne a1,a2,362a0 <_ZN4xous7syscall12send_message17h44786a68889a5f1dE+0x64>
Err(e) => Err(e),
3629a: c0c8 sw a0,4(s1)
2021-08-23 17:39:48 +00:00
if let Some ( token ) = suspend_pending_token . take ( ) {
i2c . suspend ( ) ;
susres . suspend_until_resume ( token ) . expect ( " couldn't execute suspend/resume " ) ;
i2c . resume ( ) ;
}
2021-05-05 16:45:14 +00:00
// I2C state machine handler irq result
i2c . report_write_done ( ) ;
} ) ,
Some ( I2cOpcode ::IrqI2cTxrxReadDone ) = > msg_scalar_unpack! ( msg , _ , _ , _ , _ , {
another fix for issue #75: llio resume failure
Found the following KP on resume. This time it looks like it's
failing in a syscall, during a send_message call. There's no
more visibility into what causes this, so this commit tries to clean
up some loose ends in the resume path that could send messages before
the system is ready. Namely, the console mux call got condensed into
an abridged version that doesn't send log messages (you couldn't see
them anyways...), and the I2C engine now pauses suspend until
the current transaction at hand completes. I suspect it's quite
likely that it's the I2C engine that's causing the issues...
LLIO -> PID 14
Jumping to loader...
PROGRAM HALT: CPU Exception on PID 14: Store page fault of 0x00000000 at 0x0003a02e
Current thread 2:
Thread 2:
PC:0003a02e SP:7ffff170 RA:00036290
GP:00000000 TP:60000000
T0:00000000 T1:000000f0 T2:00000030
T3:00000080 T4:000000f4 T5:ffffffff T6:0000008f
S0:7ffff1d0 S1:7ffff228 S2:00000007 S3:00000007
S4:00000001 S5:00000004 S6:00037996 S7:00010e34
S8:00011548 S9:000101bc S10:7fffff4c S11:0003c000
A0:0000000e A1:00000000 A2:00000004 A3:00000000
A4:00000000 A5:00000000 A6:00000000 A7:00000000
0003a028 <_xous_syscall>:
3a028: 00000073 ecall
3a02c: 4282 lw t0,0(sp)
3a02e: 00a2a023 sw a0,0(t0)
3a032: 00b2a223 sw a1,4(t0)
3a036: 00c2a423 sw a2,8(t0)
3a03a: 00d2a623 sw a3,12(t0)
3a03e: 00e2a823 sw a4,16(t0)
3a042: 00f2aa23 sw a5,20(t0)
3a046: 0102ac23 sw a6,24(t0)
3a04a: 0112ae23 sw a7,28(t0)
3a04e: 8082 ret
0003623c <_ZN4xous7syscall12send_message17h44786a68889a5f1dE>:
pub fn send_message(connection: CID, message: Message) -> core::result::Result<Result, Error> {
3623c: 7175 addi sp,sp,-144
3623e: c706 sw ra,140(sp)
36240: c522 sw s0,136(sp)
36242: c326 sw s1,132(sp)
36244: 84aa mv s1,a0
36246: 4549 li a0,18
crate::arch::syscall(call)
36248: 4214 lw a3,0(a2)
3624a: 4258 lw a4,4(a2)
3624c: 04a10023 sb a0,64(sp)
36250: c2ae sw a1,68(sp)
36252: c4b6 sw a3,72(sp)
36254: c6ba sw a4,76(sp)
36256: 4608 lw a0,8(a2)
36258: 464c lw a1,12(a2)
3625a: 4a14 lw a3,16(a2)
3625c: 4a50 lw a2,20(a2)
3625e: c8aa sw a0,80(sp)
36260: caae sw a1,84(sp)
36262: ccb6 sw a3,88(sp)
36264: ceb2 sw a2,92(sp)
let mut ret = crate::Result::Ok;
36266: d082 sw zero,96(sp)
let args = call.as_args();
36268: 0828 addi a0,sp,24
3626a: 008c addi a1,sp,64
3626c: 00000097 auipc ra,0x0
36270: 8da080e7 jalr -1830(ra) # 35b46 <_ZN4xous7syscall7SysCall7as_args17h7e6c318e8a926fa8E>
args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], &mut ret,
36274: 4562 lw a0,24(sp)
36276: 45f2 lw a1,28(sp)
36278: 5602 lw a2,32(sp)
3627a: 5692 lw a3,36(sp)
3627c: 5722 lw a4,40(sp)
3627e: 57b2 lw a5,44(sp)
36280: 5842 lw a6,48(sp)
36282: 58d2 lw a7,52(sp)
36284: 1080 addi s0,sp,96
_xous_syscall(
36286: c022 sw s0,0(sp)
36288: 00004097 auipc ra,0x4
3628c: da0080e7 jalr -608(ra) # 3a028 <_xous_syscall>
crate::Result::Error(e) => Err(e),
36290: 5586 lw a1,96(sp)
36292: 5516 lw a0,100(sp)
36294: 4605 li a2,1
36296: 00c59563 bne a1,a2,362a0 <_ZN4xous7syscall12send_message17h44786a68889a5f1dE+0x64>
Err(e) => Err(e),
3629a: c0c8 sw a0,4(s1)
2021-08-23 17:39:48 +00:00
if let Some ( token ) = suspend_pending_token . take ( ) {
i2c . suspend ( ) ;
susres . suspend_until_resume ( token ) . expect ( " couldn't execute suspend/resume " ) ;
i2c . resume ( ) ;
}
2021-05-05 16:45:14 +00:00
// I2C state machine handler irq result
i2c . report_read_done ( ) ;
} ) ,
Some ( I2cOpcode ::I2cTxRx ) = > {
let mut buffer = unsafe { Buffer ::from_memory_message_mut ( msg . body . memory_message_mut ( ) . unwrap ( ) ) } ;
2021-05-05 21:13:23 +00:00
let i2c_txrx = buffer . to_original ::< api ::I2cTransaction , _ > ( ) . unwrap ( ) ;
2021-05-05 16:45:14 +00:00
let status = i2c . initiate ( i2c_txrx ) ;
buffer . replace ( status ) . unwrap ( ) ;
2021-05-05 19:40:04 +00:00
} ,
2021-05-05 16:45:14 +00:00
Some ( I2cOpcode ::I2cIsBusy ) = > msg_blocking_scalar_unpack! ( msg , _ , _ , _ , _ , {
let busy = if i2c . is_busy ( ) { 1 } else { 0 } ;
xous ::return_scalar ( msg . sender , busy as _ ) . expect ( " couldn't return I2cIsBusy " ) ;
} ) ,
Some ( I2cOpcode ::Quit ) = > {
log ::info! ( " Received quit opcode, exiting! " ) ;
break ;
}
None = > {
log ::error! ( " Received unknown opcode: {:?} " , msg ) ;
}
}
2021-01-27 05:33:03 +00:00
}
2021-05-05 16:45:14 +00:00
xns . unregister_server ( i2c_sid ) . unwrap ( ) ;
xous ::destroy_server ( i2c_sid ) . unwrap ( ) ;
2021-01-27 05:33:03 +00:00
}
2021-05-05 16:45:14 +00:00
2021-04-06 11:32:35 +00:00
#[ derive(Copy, Clone, Debug) ]
struct ScalarCallback {
server_to_cb_cid : CID ,
cb_to_client_cid : CID ,
cb_to_client_id : u32 ,
}
2021-01-27 05:33:03 +00:00
#[ xous::xous_main ]
fn xmain ( ) -> ! {
2021-03-23 15:10:26 +00:00
// very early on map in the GPIO base so we can have the right logging enabled
2022-03-23 18:19:26 +00:00
let gpio_base = crate ::log_init ( ) ;
2021-03-23 15:10:26 +00:00
2021-01-27 05:33:03 +00:00
log_server ::init_wait ( ) . unwrap ( ) ;
2021-05-05 21:05:17 +00:00
log ::set_max_level ( log ::LevelFilter ::Info ) ;
2022-01-11 07:45:20 +00:00
log ::info! ( " my PID is {} " , xous ::process ::id ( ) ) ;
2021-01-27 05:33:03 +00:00
2021-03-29 18:13:23 +00:00
let xns = xous_names ::XousNames ::new ( ) . unwrap ( ) ;
2021-06-15 09:41:14 +00:00
// connections expected:
// - codec
// - GAM
// - keyboard
// - shellchat/sleep
// - shellchat/environment
2021-10-04 09:25:49 +00:00
// - shellchat/autoupdater
2021-07-17 18:36:31 +00:00
// - spinor (for turning off wfi during writes)
2021-08-10 22:15:04 +00:00
// - rootkeys (for reboots)
2021-09-18 17:06:21 +00:00
// - oqc-test (for testing the vibe motor)
2021-10-08 12:01:13 +00:00
// - net (for COM interrupt dispatch)
2021-11-08 20:57:12 +00:00
// - pddb also allocates a connection, but then releases it, to read the DNA field.
2022-01-11 04:02:27 +00:00
// We've migrated the I2C function out (which is arguably the most sensitive bit), so we can now set this more safely to unrestriced connection counts.
2021-10-29 18:14:31 +00:00
let llio_sid = xns . register_name ( api ::SERVER_NAME_LLIO , None ) . expect ( " can't register server " ) ;
2021-04-06 11:32:35 +00:00
log ::trace! ( " registered with NS -- {:?} " , llio_sid ) ;
2021-01-27 05:33:03 +00:00
2021-05-05 16:45:14 +00:00
// create the I2C handler thread
2022-01-11 04:02:27 +00:00
// - codec
2022-03-26 20:08:36 +00:00
// - time server
2022-03-23 18:19:26 +00:00
// - llio
2022-01-11 04:02:27 +00:00
// I2C can be used to set time, which can have security implications; we are more strict on counting who can have access to this resource.
2022-01-11 08:19:31 +00:00
let i2c_sid = xns . register_name ( api ::SERVER_NAME_I2C , Some ( 3 ) ) . expect ( " can't register I2C thread " ) ;
2021-05-05 16:45:14 +00:00
log ::trace! ( " registered I2C thread with NS -- {:?} " , i2c_sid ) ;
2022-01-11 07:45:20 +00:00
let _ = thread ::spawn ( {
let i2c_sid = i2c_sid . clone ( ) ;
move | | {
i2c_thread ( i2c_sid ) ;
}
} ) ;
2021-05-05 16:45:14 +00:00
2021-03-15 16:11:23 +00:00
// Create a new llio object
2021-04-06 11:32:35 +00:00
let handler_conn = xous ::connect ( llio_sid ) . expect ( " can't create IRQ handler connection " ) ;
2021-03-23 15:10:26 +00:00
let mut llio = Llio ::new ( handler_conn , gpio_base ) ;
2021-09-14 12:15:03 +00:00
llio . ec_power_on ( ) ; // ensure this is set correctly; if we're on, we always want the EC on.
2021-03-15 16:11:23 +00:00
2021-10-10 21:21:01 +00:00
if cfg! ( feature = " wfi_off " ) {
log ::warn! ( " WFI is overridden at boot -- automatic power savings is OFF! " ) ;
llio . wfi_override ( true ) ;
}
2021-04-29 15:15:47 +00:00
// register a suspend/resume listener
let sr_cid = xous ::connect ( llio_sid ) . expect ( " couldn't create suspend callback connection " ) ;
2022-01-27 14:05:38 +00:00
let mut susres = susres ::Susres ::new ( Some ( susres ::SuspendOrder ::Late ) , & xns , Opcode ::SuspendResume as u32 , sr_cid ) . expect ( " couldn't create suspend/resume object " ) ;
2021-06-01 17:08:31 +00:00
let mut latest_activity = 0 ;
2021-04-29 15:15:47 +00:00
2021-04-06 11:32:35 +00:00
let mut usb_cb_conns : [ Option < ScalarCallback > ; 32 ] = [ None ; 32 ] ;
2021-04-07 10:29:50 +00:00
let mut com_cb_conns : [ Option < ScalarCallback > ; 32 ] = [ None ; 32 ] ;
let mut rtc_cb_conns : [ Option < ScalarCallback > ; 32 ] = [ None ; 32 ] ;
let mut gpio_cb_conns : [ Option < ScalarCallback > ; 32 ] = [ None ; 32 ] ;
2021-04-06 11:32:35 +00:00
2021-07-11 10:47:19 +00:00
let mut lockstatus_force_update = true ; // some state to track if we've been through a susupend/resume, to help out the status thread with its UX update after a restart-from-cold
2022-03-23 18:19:26 +00:00
// create a self-connection to I2C to handle the public, non-security sensitive RTC API calls
let mut i2c = llio ::I2c ::new ( & xns ) ;
let mut rtc_alarm_enabled = false ;
let mut wakeup_alarm_enabled = false ;
2021-04-06 11:32:35 +00:00
log ::trace! ( " starting main loop " ) ;
2021-01-27 05:33:03 +00:00
loop {
2022-03-23 18:19:26 +00:00
let msg = xous ::receive_message ( llio_sid ) . unwrap ( ) ;
2021-04-06 11:32:35 +00:00
log ::trace! ( " Message: {:?} " , msg ) ;
2021-04-29 17:55:38 +00:00
match FromPrimitive ::from_usize ( msg . body . id ( ) ) {
Some ( Opcode ::SuspendResume ) = > xous ::msg_scalar_unpack! ( msg , token , _ , _ , _ , {
llio . suspend ( ) ;
2022-03-17 09:56:42 +00:00
#[ cfg(feature= " tts " ) ]
llio . tts_sleep_indicate ( ) ; // this happens after the suspend call because we don't want the sleep indicator to be restored on resume
2021-04-29 17:55:38 +00:00
susres . suspend_until_resume ( token ) . expect ( " couldn't execute suspend/resume " ) ;
llio . resume ( ) ;
2022-03-17 09:56:42 +00:00
#[ cfg(feature= " tts " ) ]
llio . vibe ( VibePattern ::Double ) ;
2021-07-11 10:47:19 +00:00
lockstatus_force_update = true ; // notify the status bar that yes, it does need to redraw the lock status, even if the value hasn't changed since the last read
2021-04-29 17:55:38 +00:00
} ) ,
Some ( Opcode ::CrgMode ) = > msg_scalar_unpack! ( msg , _mode , _ , _ , _ , {
todo! ( " CrgMode opcode not yet implemented. " ) ;
} ) ,
Some ( Opcode ::GpioDataOut ) = > msg_scalar_unpack! ( msg , d , _ , _ , _ , {
llio . gpio_dout ( d as u32 ) ;
} ) ,
Some ( Opcode ::GpioDataIn ) = > msg_blocking_scalar_unpack! ( msg , _ , _ , _ , _ , {
xous ::return_scalar ( msg . sender , llio . gpio_din ( ) as usize ) . expect ( " couldn't return gpio data in " ) ;
} ) ,
Some ( Opcode ::GpioDataDrive ) = > msg_scalar_unpack! ( msg , d , _ , _ , _ , {
llio . gpio_drive ( d as u32 ) ;
} ) ,
Some ( Opcode ::GpioIntMask ) = > msg_scalar_unpack! ( msg , d , _ , _ , _ , {
llio . gpio_int_mask ( d as u32 ) ;
} ) ,
Some ( Opcode ::GpioIntAsFalling ) = > msg_scalar_unpack! ( msg , d , _ , _ , _ , {
llio . gpio_int_as_falling ( d as u32 ) ;
} ) ,
Some ( Opcode ::GpioIntPending ) = > msg_blocking_scalar_unpack! ( msg , _ , _ , _ , _ , {
xous ::return_scalar ( msg . sender , llio . gpio_int_pending ( ) as usize ) . expect ( " couldn't return gpio pending vector " ) ;
} ) ,
Some ( Opcode ::GpioIntEna ) = > msg_scalar_unpack! ( msg , d , _ , _ , _ , {
llio . gpio_int_ena ( d as u32 ) ;
} ) ,
2021-06-01 09:02:30 +00:00
Some ( Opcode ::DebugPowerdown ) = > msg_scalar_unpack! ( msg , arg , _ , _ , _ , {
let ena = if arg = = 0 { false } else { true } ;
llio . debug_powerdown ( ena ) ;
} ) ,
Some ( Opcode ::DebugWakeup ) = > msg_scalar_unpack! ( msg , arg , _ , _ , _ , {
let ena = if arg = = 0 { false } else { true } ;
llio . debug_wakeup ( ena ) ;
} ) ,
2021-04-29 17:55:38 +00:00
Some ( Opcode ::UartMux ) = > msg_scalar_unpack! ( msg , mux , _ , _ , _ , {
llio . set_uart_mux ( mux . into ( ) ) ;
} ) ,
Some ( Opcode ::InfoDna ) = > msg_blocking_scalar_unpack! ( msg , _ , _ , _ , _ , {
let ( val1 , val2 ) = llio . get_info_dna ( ) ;
xous ::return_scalar2 ( msg . sender , val1 , val2 ) . expect ( " couldn't return DNA " ) ;
} ) ,
Some ( Opcode ::InfoGit ) = > msg_blocking_scalar_unpack! ( msg , _ , _ , _ , _ , {
let ( val1 , val2 ) = llio . get_info_git ( ) ;
xous ::return_scalar2 ( msg . sender , val1 , val2 ) . expect ( " couldn't return Git " ) ;
} ) ,
Some ( Opcode ::InfoPlatform ) = > msg_blocking_scalar_unpack! ( msg , _ , _ , _ , _ , {
let ( val1 , val2 ) = llio . get_info_platform ( ) ;
xous ::return_scalar2 ( msg . sender , val1 , val2 ) . expect ( " couldn't return Platform " ) ;
} ) ,
Some ( Opcode ::InfoTarget ) = > msg_blocking_scalar_unpack! ( msg , _ , _ , _ , _ , {
let ( val1 , val2 ) = llio . get_info_target ( ) ;
xous ::return_scalar2 ( msg . sender , val1 , val2 ) . expect ( " couldn't return Target " ) ;
} ) ,
2021-05-03 18:22:35 +00:00
Some ( Opcode ::PowerAudio ) = > msg_blocking_scalar_unpack! ( msg , power_on , _ , _ , _ , {
2021-04-29 17:55:38 +00:00
if power_on = = 0 {
llio . power_audio ( false ) ;
} else {
llio . power_audio ( true ) ;
2021-04-06 11:32:35 +00:00
}
2021-05-03 18:22:35 +00:00
xous ::return_scalar ( msg . sender , 0 ) . expect ( " couldn't confirm audio power was set " ) ;
2021-05-29 16:12:18 +00:00
} ) ,
Some ( Opcode ::PowerCrypto ) = > msg_blocking_scalar_unpack! ( msg , power_on , _ , _ , _ , {
if power_on = = 0 {
llio . power_crypto ( false ) ;
} else {
llio . power_crypto ( true ) ;
}
xous ::return_scalar ( msg . sender , 0 ) . expect ( " couldn't confirm crypto power was set " ) ;
} ) ,
2021-06-01 09:02:30 +00:00
Some ( Opcode ::WfiOverride ) = > msg_blocking_scalar_unpack! ( msg , override_ , _ , _ , _ , {
if override_ = = 0 {
llio . wfi_override ( false ) ;
} else {
llio . wfi_override ( true ) ;
}
xous ::return_scalar ( msg . sender , 0 ) . expect ( " couldn't confirm wfi override was updated " ) ;
} ) ,
2021-05-29 16:12:18 +00:00
Some ( Opcode ::PowerCryptoStatus ) = > msg_blocking_scalar_unpack! ( msg , _ , _ , _ , _ , {
let ( _ , sha , engine , force ) = llio . power_crypto_status ( ) ;
let mut ret = 0 ;
if sha { ret | = 1 } ;
if engine { ret | = 2 } ;
if force { ret | = 4 } ;
xous ::return_scalar ( msg . sender , ret ) . expect ( " couldn't return crypto unit power status " ) ;
2021-04-29 17:55:38 +00:00
} ) ,
Some ( Opcode ::PowerSelf ) = > msg_scalar_unpack! ( msg , power_on , _ , _ , _ , {
if power_on = = 0 {
llio . power_self ( false ) ;
} else {
llio . power_self ( true ) ;
2021-04-06 11:32:35 +00:00
}
2021-04-29 17:55:38 +00:00
} ) ,
Some ( Opcode ::PowerBoostMode ) = > msg_scalar_unpack! ( msg , power_on , _ , _ , _ , {
if power_on = = 0 {
llio . power_boost_mode ( false ) ;
} else {
llio . power_boost_mode ( true ) ;
2021-04-07 10:29:50 +00:00
}
2021-04-29 17:55:38 +00:00
} ) ,
Some ( Opcode ::EcSnoopAllow ) = > msg_scalar_unpack! ( msg , power_on , _ , _ , _ , {
if power_on = = 0 {
llio . ec_snoop_allow ( false ) ;
} else {
llio . ec_snoop_allow ( true ) ;
2021-04-07 10:29:50 +00:00
}
2021-04-29 17:55:38 +00:00
} ) ,
Some ( Opcode ::EcReset ) = > msg_scalar_unpack! ( msg , _ , _ , _ , _ , {
llio . ec_reset ( ) ;
} ) ,
Some ( Opcode ::EcPowerOn ) = > msg_scalar_unpack! ( msg , _ , _ , _ , _ , {
llio . ec_power_on ( ) ;
} ) ,
Some ( Opcode ::SelfDestruct ) = > msg_scalar_unpack! ( msg , code , _ , _ , _ , {
llio . self_destruct ( code as u32 ) ;
} ) ,
Some ( Opcode ::Vibe ) = > msg_scalar_unpack! ( msg , pattern , _ , _ , _ , {
llio . vibe ( pattern . into ( ) ) ;
} ) ,
Some ( Opcode ::AdcVbus ) = > msg_blocking_scalar_unpack! ( msg , _ , _ , _ , _ , {
xous ::return_scalar ( msg . sender , llio . xadc_vbus ( ) as _ ) . expect ( " couldn't return Xadc " ) ;
} ) ,
Some ( Opcode ::AdcVccInt ) = > msg_blocking_scalar_unpack! ( msg , _ , _ , _ , _ , {
xous ::return_scalar ( msg . sender , llio . xadc_vccint ( ) as _ ) . expect ( " couldn't return Xadc " ) ;
} ) ,
Some ( Opcode ::AdcVccAux ) = > msg_blocking_scalar_unpack! ( msg , _ , _ , _ , _ , {
xous ::return_scalar ( msg . sender , llio . xadc_vccaux ( ) as _ ) . expect ( " couldn't return Xadc " ) ;
} ) ,
Some ( Opcode ::AdcVccBram ) = > msg_blocking_scalar_unpack! ( msg , _ , _ , _ , _ , {
xous ::return_scalar ( msg . sender , llio . xadc_vccbram ( ) as _ ) . expect ( " couldn't return Xadc " ) ;
} ) ,
Some ( Opcode ::AdcUsbN ) = > msg_blocking_scalar_unpack! ( msg , _ , _ , _ , _ , {
xous ::return_scalar ( msg . sender , llio . xadc_usbn ( ) as _ ) . expect ( " couldn't return Xadc " ) ;
} ) ,
Some ( Opcode ::AdcUsbP ) = > msg_blocking_scalar_unpack! ( msg , _ , _ , _ , _ , {
xous ::return_scalar ( msg . sender , llio . xadc_usbp ( ) as _ ) . expect ( " couldn't return Xadc " ) ;
} ) ,
Some ( Opcode ::AdcTemperature ) = > msg_blocking_scalar_unpack! ( msg , _ , _ , _ , _ , {
xous ::return_scalar ( msg . sender , llio . xadc_temperature ( ) as _ ) . expect ( " couldn't return Xadc " ) ;
} ) ,
Some ( Opcode ::AdcGpio5 ) = > msg_blocking_scalar_unpack! ( msg , _ , _ , _ , _ , {
xous ::return_scalar ( msg . sender , llio . xadc_gpio5 ( ) as _ ) . expect ( " couldn't return Xadc " ) ;
} ) ,
Some ( Opcode ::AdcGpio2 ) = > msg_blocking_scalar_unpack! ( msg , _ , _ , _ , _ , {
xous ::return_scalar ( msg . sender , llio . xadc_gpio2 ( ) as _ ) . expect ( " couldn't return Xadc " ) ;
} ) ,
Some ( Opcode ::EventUsbAttachSubscribe ) = > {
let buffer = unsafe { Buffer ::from_memory_message ( msg . body . memory_message ( ) . unwrap ( ) ) } ;
let hookdata = buffer . to_original ::< ScalarHook , _ > ( ) . unwrap ( ) ;
do_hook ( hookdata , & mut usb_cb_conns ) ;
}
Some ( Opcode ::EventComSubscribe ) = > {
let buffer = unsafe { Buffer ::from_memory_message ( msg . body . memory_message ( ) . unwrap ( ) ) } ;
let hookdata = buffer . to_original ::< ScalarHook , _ > ( ) . unwrap ( ) ;
do_hook ( hookdata , & mut com_cb_conns ) ;
}
Some ( Opcode ::EventRtcSubscribe ) = > {
let buffer = unsafe { Buffer ::from_memory_message ( msg . body . memory_message ( ) . unwrap ( ) ) } ;
let hookdata = buffer . to_original ::< ScalarHook , _ > ( ) . unwrap ( ) ;
do_hook ( hookdata , & mut rtc_cb_conns ) ;
}
Some ( Opcode ::GpioIntSubscribe ) = > {
let buffer = unsafe { Buffer ::from_memory_message ( msg . body . memory_message ( ) . unwrap ( ) ) } ;
let hookdata = buffer . to_original ::< ScalarHook , _ > ( ) . unwrap ( ) ;
do_hook ( hookdata , & mut gpio_cb_conns ) ;
}
Some ( Opcode ::EventComEnable ) = > msg_scalar_unpack! ( msg , ena , _ , _ , _ , {
if ena = = 0 {
llio . com_int_ena ( false ) ;
} else {
llio . com_int_ena ( true ) ;
2021-04-07 10:29:50 +00:00
}
2021-04-29 17:55:38 +00:00
} ) ,
Some ( Opcode ::EventRtcEnable ) = > msg_scalar_unpack! ( msg , ena , _ , _ , _ , {
if ena = = 0 {
llio . rtc_int_ena ( false ) ;
} else {
llio . rtc_int_ena ( true ) ;
2021-04-07 10:29:50 +00:00
}
2021-04-29 17:55:38 +00:00
} ) ,
Some ( Opcode ::EventUsbAttachEnable ) = > msg_scalar_unpack! ( msg , ena , _ , _ , _ , {
if ena = = 0 {
llio . usb_int_ena ( false ) ;
} else {
llio . usb_int_ena ( true ) ;
2021-04-06 11:32:35 +00:00
}
2021-04-29 17:55:38 +00:00
} ) ,
Some ( Opcode ::EventComHappened ) = > {
send_event ( & com_cb_conns , 0 ) ;
} ,
Some ( Opcode ::EventRtcHappened ) = > {
send_event ( & rtc_cb_conns , 0 ) ;
} ,
Some ( Opcode ::EventUsbHappened ) = > {
send_event ( & usb_cb_conns , 0 ) ;
} ,
Some ( Opcode ::GpioIntHappened ) = > msg_scalar_unpack! ( msg , channel , _ , _ , _ , {
send_event ( & gpio_cb_conns , channel as usize ) ;
} ) ,
2021-06-01 17:08:31 +00:00
Some ( Opcode ::EventActivityHappened ) = > msg_scalar_unpack! ( msg , activity , _ , _ , _ , {
log ::debug! ( " activity: {} " , activity ) ;
latest_activity = activity as u32 ;
} ) ,
Some ( Opcode ::GetActivity ) = > msg_blocking_scalar_unpack! ( msg , _ , _ , _ , _ , {
2022-01-22 10:25:39 +00:00
#[ cfg(any(target_os = " none " , target_os = " xous " )) ]
{
let period = llio . activity_get_period ( ) as u32 ;
// log::debug!("activity/period: {}/{}, {:.2}%", latest_activity, period, (latest_activity as f32 / period as f32) * 100.0);
xous ::return_scalar2 ( msg . sender , latest_activity as usize , period as usize ) . expect ( " couldn't return activity " ) ;
}
#[ cfg(not(any(target_os = " none " , target_os = " xous " ))) ] // fake an activity
{
let period = 12_000 ;
xous ::return_scalar2 ( msg . sender , latest_activity as usize , period as usize ) . expect ( " couldn't return activity " ) ;
latest_activity + = period / 20 ;
latest_activity % = period ;
}
2021-06-01 17:08:31 +00:00
} ) ,
2021-07-12 16:46:31 +00:00
Some ( Opcode ::DebugUsbOp ) = > msg_blocking_scalar_unpack! ( msg , update_req , new_state , _ , _ , {
2021-07-11 10:47:19 +00:00
if update_req ! = 0 {
2021-07-12 16:46:31 +00:00
// if new_state is true (not 0), then try to lock the USB port
2021-07-11 10:47:19 +00:00
// if false, try to unlock the USB port
2021-07-12 16:46:31 +00:00
if new_state ! = 0 {
llio . set_usb_disable ( true ) ;
} else {
llio . set_usb_disable ( false ) ;
}
2021-07-11 10:47:19 +00:00
}
// at this point, *read back* the new state -- don't assume it "took". The readback is always based on
// a real hardware value and not the requested value. for now, always false.
2021-07-12 16:46:31 +00:00
let is_locked = if llio . get_usb_disable ( ) {
1
} else {
0
} ;
2021-07-11 10:47:19 +00:00
// this is a performance optimization. we could always redraw the status, but, instead we only redraw when
// the status has changed. However, there is an edge case: on a resume from suspend, the status needs a redraw,
// even if nothing has changed. Thus, we have this separate boolean we send back to force an update in the
// case that we have just come out of a suspend.
let force_update = if lockstatus_force_update {
1
} else {
0
} ;
xous ::return_scalar2 ( msg . sender , is_locked , force_update ) . expect ( " couldn't return status " ) ;
lockstatus_force_update = false ;
} ) ,
2022-03-23 18:19:26 +00:00
Some ( Opcode ::SetWakeupAlarm ) = > msg_blocking_scalar_unpack! ( msg , delay , _ , _ , _ , {
if delay > u8 ::MAX as usize {
log ::error! ( " Wakeup must be no longer than {} secs in the future " , u8 ::MAX ) ;
xous ::return_scalar ( msg . sender , 1 ) . expect ( " couldn't return to caller " ) ;
continue ;
}
let seconds = delay as u8 ;
wakeup_alarm_enabled = true ;
// make sure battery switchover is enabled, otherwise we won't keep time when power goes off
i2c . i2c_write ( ABRTCMC_I2C_ADR , ABRTCMC_CONTROL3 , & [ ( Control3 ::BATT_STD_BL_EN ) . bits ( ) ] ) . expect ( " RTC access error " ) ;
// set clock units to 1 second, output pulse length to ~218ms
i2c . i2c_write ( ABRTCMC_I2C_ADR , ABRTCMC_TIMERB_CLK , & [ ( TimerClk ::CLK_1_S | TimerClk ::PULSE_218_MS ) . bits ( ) ] ) . expect ( " RTC access error " ) ;
// program elapsed time
i2c . i2c_write ( ABRTCMC_I2C_ADR , ABRTCMC_TIMERB , & [ seconds ] ) . expect ( " RTC access error " ) ;
// enable timerb countdown interrupt, also clears any prior interrupt flag
let mut control2 = ( Control2 ::COUNTDOWN_B_INT ) . bits ( ) ;
if rtc_alarm_enabled {
control2 | = Control2 ::COUNTDOWN_A_INT . bits ( ) ;
}
i2c . i2c_write ( ABRTCMC_I2C_ADR , ABRTCMC_CONTROL2 , & [ control2 ] ) . expect ( " RTC access error " ) ;
// turn on the timer proper -- the system will wakeup in 5..4..3....
let mut config = ( Config ::CLKOUT_DISABLE | Config ::TIMER_B_ENABLE ) . bits ( ) ;
if rtc_alarm_enabled {
config | = ( Config ::TIMER_A_COUNTDWN | Config ::TIMERA_SECONDS_INT_PULSED ) . bits ( ) ;
}
i2c . i2c_write ( ABRTCMC_I2C_ADR , ABRTCMC_CONFIG , & [ config ] ) . expect ( " RTC access error " ) ;
xous ::return_scalar ( msg . sender , 0 ) . expect ( " couldn't return to caller " ) ;
} ) ,
Some ( Opcode ::ClearWakeupAlarm ) = > msg_blocking_scalar_unpack! ( msg , _ , _ , _ , _ , {
wakeup_alarm_enabled = false ;
// make sure battery switchover is enabled, otherwise we won't keep time when power goes off
i2c . i2c_write ( ABRTCMC_I2C_ADR , ABRTCMC_CONTROL3 , & [ ( Control3 ::BATT_STD_BL_EN ) . bits ( ) ] ) . expect ( " RTC access error " ) ;
let mut config = Config ::CLKOUT_DISABLE . bits ( ) ;
if rtc_alarm_enabled {
config | = ( Config ::TIMER_A_COUNTDWN | Config ::TIMERA_SECONDS_INT_PULSED ) . bits ( ) ;
}
// turn off RTC wakeup timer, in case previously set
i2c . i2c_write ( ABRTCMC_I2C_ADR , ABRTCMC_CONFIG , & [ config ] ) . expect ( " RTC access error " ) ;
// clear my interrupts and flags
let mut control2 = 0 ;
if rtc_alarm_enabled {
control2 | = Control2 ::COUNTDOWN_A_INT . bits ( ) ;
}
i2c . i2c_write ( ABRTCMC_I2C_ADR , ABRTCMC_CONTROL2 , & [ control2 ] ) . expect ( " RTC access error " ) ;
xous ::return_scalar ( msg . sender , 0 ) . expect ( " couldn't return to caller " ) ;
} ) ,
Some ( Opcode ::SetRtcAlarm ) = > msg_blocking_scalar_unpack! ( msg , delay , _ , _ , _ , {
if delay > u8 ::MAX as usize {
log ::error! ( " Alarm must be no longer than {} secs in the future " , u8 ::MAX ) ;
xous ::return_scalar ( msg . sender , 1 ) . expect ( " couldn't return to caller " ) ;
continue ;
}
let seconds = delay as u8 ;
rtc_alarm_enabled = true ;
// make sure battery switchover is enabled, otherwise we won't keep time when power goes off
i2c . i2c_write ( ABRTCMC_I2C_ADR , ABRTCMC_CONTROL3 , & [ ( Control3 ::BATT_STD_BL_EN ) . bits ( ) ] ) . expect ( " RTC access error " ) ;
// set clock units to 1 second, output pulse length to ~218ms
i2c . i2c_write ( ABRTCMC_I2C_ADR , ABRTCMC_TIMERA_CLK , & [ ( TimerClk ::CLK_1_S | TimerClk ::PULSE_218_MS ) . bits ( ) ] ) . expect ( " RTC access error " ) ;
// program elapsed time
i2c . i2c_write ( ABRTCMC_I2C_ADR , ABRTCMC_TIMERA , & [ seconds ] ) . expect ( " RTC access error " ) ;
// enable timerb countdown interrupt, also clears any prior interrupt flag
let mut control2 = ( Control2 ::COUNTDOWN_A_INT ) . bits ( ) ;
if wakeup_alarm_enabled {
control2 | = Control2 ::COUNTDOWN_B_INT . bits ( ) ;
}
i2c . i2c_write ( ABRTCMC_I2C_ADR , ABRTCMC_CONTROL2 , & [ control2 ] ) . expect ( " RTC access error " ) ;
// 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 wakeup_alarm_enabled {
config | = ( Config ::TIMER_B_ENABLE ) . bits ( ) ;
}
i2c . i2c_write ( ABRTCMC_I2C_ADR , ABRTCMC_CONFIG , & [ config ] ) . expect ( " RTC access error " ) ;
xous ::return_scalar ( msg . sender , 0 ) . expect ( " couldn't return to caller " ) ;
} ) ,
Some ( Opcode ::ClearRtcAlarm ) = > msg_blocking_scalar_unpack! ( msg , _ , _ , _ , _ , {
rtc_alarm_enabled = false ;
// turn off RTC wakeup timer, in case previously set
let mut config = Config ::CLKOUT_DISABLE . bits ( ) ;
if wakeup_alarm_enabled {
config | = ( Config ::TIMER_B_ENABLE | Config ::TIMERB_INT_PULSED ) . bits ( ) ;
}
i2c . i2c_write ( ABRTCMC_I2C_ADR , ABRTCMC_CONFIG , & [ config ] ) . expect ( " RTC access error " ) ;
// clear my interrupts and flags
let mut control2 = 0 ;
if wakeup_alarm_enabled {
control2 | = Control2 ::COUNTDOWN_B_INT . bits ( ) ;
}
i2c . i2c_write ( ABRTCMC_I2C_ADR , ABRTCMC_CONTROL2 , & [ control2 ] ) . expect ( " RTC access error " ) ;
xous ::return_scalar ( msg . sender , 0 ) . expect ( " couldn't return to caller " ) ;
} ) ,
2022-03-24 10:44:06 +00:00
Some ( Opcode ::GetRtcValue ) = > msg_blocking_scalar_unpack! ( msg , _ , _ , _ , _ , {
// There is a possibility that the RTC hardware is actually in an invalid state.
// Thus, this will return a u64 which is formatted as follows:
// [63] - invalid (0 for valid, 1 for invalid)
// [62:0] - time in seconds
// This is okay because 2^63 is much larger than the total number of seconds trackable by the RTC hardware.
// The RTC hardware can only count up to 100 years before rolling over, which is 3.1*10^9 seconds.
// Note that we start the RTC at somewhere between 0-10 years, so in practice, a user can expect between 90-100 years
// of continuous uptime service out of the RTC.
let mut settings = [ 0 u8 ; 8 ] ;
match i2c . i2c_read ( ABRTCMC_I2C_ADR , ABRTCMC_CONTROL3 , & mut settings , None ) {
Ok ( llio ::I2cStatus ::ResponseReadOk ) = > { } ,
_ = > {
log ::error! ( " Couldn't read seconds from RTC! " ) ;
xous ::return_scalar2 ( msg . sender , 0x8000_0000 , 0 ) . expect ( " couldn't return to caller " ) ;
continue ;
} ,
} ;
2022-03-26 20:08:36 +00:00
log ::debug! ( " GetRtcValue regs: {:?} " , settings ) ;
2022-03-24 10:44:06 +00:00
let total_secs = match rtc_to_seconds ( & settings ) {
Some ( s ) = > s ,
None = > {
xous ::return_scalar2 ( msg . sender , 0x8000_0000 , 0 ) . expect ( " couldn't return to caller " ) ;
continue ;
}
} ;
xous ::return_scalar2 ( msg . sender ,
( ( total_secs > > 32 ) & 0xFFFF_FFFF ) as usize ,
( total_secs & 0xFFFF_FFFF ) as usize ,
) . expect ( " couldn't return to caller " ) ;
2022-03-23 18:19:26 +00:00
} ) ,
2021-05-05 16:45:14 +00:00
Some ( Opcode ::Quit ) = > {
log ::info! ( " Received quit opcode, exiting. " ) ;
let dropconn = xous ::connect ( i2c_sid ) . unwrap ( ) ;
xous ::send_message ( dropconn ,
xous ::Message ::new_scalar ( I2cOpcode ::Quit . to_usize ( ) . unwrap ( ) , 0 , 0 , 0 , 0 ) ) . unwrap ( ) ;
unsafe { xous ::disconnect ( dropconn ) . unwrap ( ) ; }
2021-04-29 17:55:38 +00:00
break ;
2021-04-06 11:32:35 +00:00
}
2021-05-05 16:45:14 +00:00
None = > {
2022-01-11 07:45:20 +00:00
log ::error! ( " couldn't convert opcode: {:?} " , msg ) ;
2021-05-05 16:45:14 +00:00
}
2021-01-27 05:33:03 +00:00
}
}
2021-04-05 15:50:00 +00:00
log ::trace! ( " main loop exit, destroying servers " ) ;
2021-04-07 10:29:50 +00:00
unhook ( & mut com_cb_conns ) ;
unhook ( & mut rtc_cb_conns ) ;
unhook ( & mut usb_cb_conns ) ;
unhook ( & mut gpio_cb_conns ) ;
2021-04-05 15:50:00 +00:00
xns . unregister_server ( llio_sid ) . unwrap ( ) ;
xous ::destroy_server ( llio_sid ) . unwrap ( ) ;
log ::trace! ( " quitting " ) ;
2021-05-20 03:01:00 +00:00
xous ::terminate_process ( 0 )
2021-01-27 05:33:03 +00:00
}
2021-04-07 10:29:50 +00:00
fn do_hook ( hookdata : ScalarHook , cb_conns : & mut [ Option < ScalarCallback > ; 32 ] ) {
let ( s0 , s1 , s2 , s3 ) = hookdata . sid ;
let sid = xous ::SID ::from_u32 ( s0 , s1 , s2 , s3 ) ;
let server_to_cb_cid = xous ::connect ( sid ) . unwrap ( ) ;
let cb_dat = Some ( ScalarCallback {
server_to_cb_cid ,
cb_to_client_cid : hookdata . cid ,
cb_to_client_id : hookdata . id ,
} ) ;
let mut found = false ;
for entry in cb_conns . iter_mut ( ) {
if entry . is_none ( ) {
* entry = cb_dat ;
found = true ;
break ;
}
}
if ! found {
2022-01-11 07:45:20 +00:00
log ::error! ( " ran out of space registering callback " ) ;
2021-04-07 10:29:50 +00:00
}
}
fn unhook ( cb_conns : & mut [ Option < ScalarCallback > ; 32 ] ) {
for entry in cb_conns . iter_mut ( ) {
if let Some ( scb ) = entry {
xous ::send_message ( scb . server_to_cb_cid ,
xous ::Message ::new_blocking_scalar ( EventCallback ::Drop . to_usize ( ) . unwrap ( ) , 0 , 0 , 0 , 0 )
) . unwrap ( ) ;
unsafe { xous ::disconnect ( scb . server_to_cb_cid ) . unwrap ( ) ; }
}
* entry = None ;
}
}
fn send_event ( cb_conns : & [ Option < ScalarCallback > ; 32 ] , which : usize ) {
for entry in cb_conns . iter ( ) {
if let Some ( scb ) = entry {
// note that the "which" argument is only used for GPIO events, to indicate which pin had the event
2021-10-20 16:03:08 +00:00
match xous ::try_send_message ( scb . server_to_cb_cid ,
2021-04-07 10:29:50 +00:00
xous ::Message ::new_scalar ( EventCallback ::Event . to_usize ( ) . unwrap ( ) ,
scb . cb_to_client_cid as usize , scb . cb_to_client_id as usize , which , 0 )
2021-10-20 16:03:08 +00:00
) {
Ok ( _ ) = > { } ,
Err ( e ) = > {
match e {
xous ::Error ::ServerQueueFull = > {
// this triggers if an interrupt storm happens. This could be perfectly natural and/or
// "expected", and the "best" behavior is probably to drop the events, but leave a warning.
// Examples of this would be a ping flood overwhelming the network stack.
log ::warn! ( " Attempted to send event, but destination queue is full. Event was dropped: {:?} " , scb ) ;
}
xous ::Error ::ServerNotFound = > {
log ::warn! ( " Event callback subscriber has died. Event was dropped: {:?} " , scb ) ;
}
_ = > {
log ::error! ( " Callback error {:?}: {:?} " , e , scb ) ;
}
}
}
}
2021-04-07 10:29:50 +00:00
} ;
}
2021-05-20 03:01:00 +00:00
}
2022-03-24 10:44:06 +00:00
// run with `cargo test -- --nocapture --test-threads=1`:
// --nocapture to see the print output (while debugging)
// --test-threads=1 so we can see the output in sequence
#[ cfg(test) ]
mod tests {
use super ::* ;
use chrono ::{ DateTime , Utc } ;
use llio ::rtc_to_seconds ;
use rand ::Rng ;
2022-03-26 20:08:36 +00:00
fn to_bcd ( binary : u8 ) -> u8 {
let mut lsd : u8 = binary % 10 ;
if lsd > 9 {
lsd = 9 ;
}
let mut msd : u8 = binary / 10 ;
if msd > 9 {
msd = 9 ;
}
( msd < < 4 ) | lsd
}
2022-03-24 10:44:06 +00:00
#[ test ]
fn test_rtc_to_secs ( ) {
let mut rng = rand ::thread_rng ( ) ;
let rtc_base = DateTime ::< Utc > ::from_utc ( chrono ::NaiveDate ::from_ymd ( 2000 , 1 , 1 )
. and_hms ( 0 , 0 , 0 ) , Utc ) ;
// test every year, every month, every day, with a random time stamp
for year in 0 .. 99 {
for month in 1 ..= 12 {
let days = match month {
1 = > 1 ..= 31 ,
2 = > if ( year % 4 ) = = 0 {
1 ..= 29
} else {
1 ..= 28
}
3 = > 1 ..= 31 ,
4 = > 1 ..= 30 ,
5 = > 1 ..= 31 ,
6 = > 1 ..= 30 ,
7 = > 1 ..= 31 ,
8 = > 1 ..= 31 ,
9 = > 1 ..= 30 ,
10 = > 1 ..= 31 ,
11 = > 1 ..= 30 ,
12 = > 1 ..= 31 ,
_ = > { panic! ( " invalid month " ) } ,
} ;
for day in days {
let h = rng . gen_range ( 0 .. 24 ) ;
let m = rng . gen_range ( 0 .. 60 ) ;
let s = rng . gen_range ( 0 .. 60 ) ;
let rtc_test = DateTime ::< Utc > ::from_utc (
chrono ::NaiveDate ::from_ymd ( 2000 + year , month , day )
. and_hms ( h , m , s ) , Utc ) ;
let diff = rtc_test . signed_duration_since ( rtc_base ) ;
let settings = [
( Control3 ::BATT_STD_BL_EN ) . bits ( ) ,
2022-03-26 20:08:36 +00:00
to_bcd ( s as u8 ) ,
to_bcd ( m as u8 ) ,
to_bcd ( h as u8 ) ,
to_bcd ( day as u8 ) ,
2022-03-24 10:44:06 +00:00
0 ,
2022-03-26 20:08:36 +00:00
to_bcd ( month as u8 ) ,
to_bcd ( year as u8 ) ,
2022-03-24 10:44:06 +00:00
] ;
if diff . num_seconds ( ) ! = rtc_to_seconds ( & settings ) . unwrap ( ) as i64 {
println! ( " {} vs {} " , diff . num_seconds ( ) , rtc_to_seconds ( & settings ) . unwrap ( ) ) ;
println! ( " Duration to {} / {} / {} - {} : {} : {} -- {} " ,
2000 + year ,
month ,
day ,
h , m , s ,
diff . num_seconds ( )
) ;
}
assert! ( diff . num_seconds ( ) = = rtc_to_seconds ( & settings ) . unwrap ( ) as i64 ) ;
}
}
}
}
}