2021-01-27 05:33:03 +00:00
#![ cfg_attr(target_os = " none " , no_std) ]
#![ cfg_attr(target_os = " none " , no_main) ]
2021-06-18 14:37:08 +00:00
#![ cfg_attr(not(target_os = " none " ), allow(dead_code)) ]
#![ cfg_attr(not(target_os = " none " ), allow(unused_imports)) ]
#![ cfg_attr(not(target_os = " none " ), allow(unused_variables)) ]
2021-01-27 05:33:03 +00:00
mod api ;
2021-04-06 11:32:35 +00:00
use api ::* ;
2021-03-15 16:11:23 +00:00
mod i2c ;
2021-01-27 05:33:03 +00:00
use log ::{ error , info } ;
2021-04-06 11:32:35 +00:00
2021-04-07 10:29:50 +00:00
use num_traits ::{ ToPrimitive , FromPrimitive } ;
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
2021-08-02 10:11:48 +00:00
#[ cfg(any(target_os = " none " , target_os = " xous " )) ]
2021-01-27 05:33:03 +00:00
mod implementation {
2021-04-06 11:32:35 +00:00
use crate ::api ::* ;
2021-01-27 18:56:00 +00:00
use log ::{ error , info } ;
2021-08-22 06:28:06 +00:00
use utralib ::{ generated ::* , utra ::gpio ::UARTSEL_UARTSEL } ;
2021-04-06 11:32:35 +00:00
use num_traits ::ToPrimitive ;
2021-04-29 15:15:47 +00:00
use susres ::{ RegManager , RegOrField , SuspendResume } ;
2021-01-27 05:33:03 +00:00
2021-04-07 18:30:57 +00:00
#[ allow(dead_code) ]
2021-01-27 05:33:03 +00:00
pub struct Llio {
crg_csr : utralib ::CSR < u32 > ,
gpio_csr : utralib ::CSR < u32 > ,
2021-04-29 15:15:47 +00:00
gpio_susres : RegManager ::< { utra ::gpio ::GPIO_NUMREGS } > ,
2021-01-27 05:33:03 +00:00
info_csr : utralib ::CSR < u32 > ,
identifier_csr : utralib ::CSR < u32 > ,
2021-03-16 08:37:38 +00:00
handler_conn : Option < xous ::CID > ,
2021-01-27 05:33:03 +00:00
event_csr : utralib ::CSR < u32 > ,
2021-04-29 15:15:47 +00:00
event_susres : RegManager ::< { utra ::btevents ::BTEVENTS_NUMREGS } > ,
2021-01-27 05:33:03 +00:00
power_csr : utralib ::CSR < u32 > ,
2021-04-29 15:15:47 +00:00
power_susres : RegManager ::< { utra ::power ::POWER_NUMREGS } > ,
2021-03-10 18:57:58 +00:00
xadc_csr : utralib ::CSR < u32 > , // be careful with this as XADC is shared with TRNG
2021-03-28 10:14:41 +00:00
ticktimer : ticktimer_server ::Ticktimer ,
2021-06-01 17:08:31 +00:00
activity_period : u32 , // 12mhz clock cycles over which to sample activity
2021-01-27 20:03:30 +00:00
destruct_armed : bool ,
2021-08-22 06:28:06 +00:00
uartmux_cache : u32 , // stash a value of the uartmux -- restore from override into kernel so we can record KPs on resume
2021-01-27 05:33:03 +00:00
}
fn handle_event_irq ( _irq_no : usize , arg : * mut usize ) {
let xl = unsafe { & mut * ( arg as * mut Llio ) } ;
// just clear the pending request for now and return
2021-04-07 10:29:50 +00:00
if xl . event_csr . rf ( utra ::btevents ::EV_PENDING_COM_INT ) ! = 0 {
if let Some ( conn ) = xl . handler_conn {
xous ::try_send_message ( conn ,
xous ::Message ::new_scalar ( Opcode ::EventComHappened . to_usize ( ) . unwrap ( ) , 0 , 0 , 0 , 0 ) ) . map ( | _ | ( ) ) . unwrap ( ) ;
} else {
log ::error! ( " |handle_event_irq: COM interrupt, but no connection for notification! " )
}
}
if xl . event_csr . rf ( utra ::btevents ::EV_PENDING_RTC_INT ) ! = 0 {
if let Some ( conn ) = xl . handler_conn {
xous ::try_send_message ( conn ,
xous ::Message ::new_scalar ( Opcode ::EventRtcHappened . to_usize ( ) . unwrap ( ) , 0 , 0 , 0 , 0 ) ) . map ( | _ | ( ) ) . unwrap ( ) ;
} else {
log ::error! ( " |handle_event_irq: RTC interrupt, but no connection for notification! " )
}
}
2021-01-27 05:33:03 +00:00
xl . event_csr
. wo ( utra ::btevents ::EV_PENDING , xl . event_csr . r ( utra ::btevents ::EV_PENDING ) ) ;
}
fn handle_gpio_irq ( _irq_no : usize , arg : * mut usize ) {
let xl = unsafe { & mut * ( arg as * mut Llio ) } ;
2021-04-07 10:29:50 +00:00
if let Some ( conn ) = xl . handler_conn {
xous ::try_send_message ( conn ,
xous ::Message ::new_scalar ( Opcode ::GpioIntHappened . to_usize ( ) . unwrap ( ) ,
xl . gpio_csr . r ( utra ::gpio ::EV_PENDING ) as _ , 0 , 0 , 0 ) ) . map ( | _ | ( ) ) . unwrap ( ) ;
} else {
log ::error! ( " |handle_event_irq: GPIO interrupt, but no connection for notification! " )
}
2021-01-27 05:33:03 +00:00
xl . gpio_csr
. wo ( utra ::gpio ::EV_PENDING , xl . gpio_csr . r ( utra ::gpio ::EV_PENDING ) ) ;
}
2021-04-07 10:29:50 +00:00
fn handle_power_irq ( _irq_no : usize , arg : * mut usize ) {
let xl = unsafe { & mut * ( arg as * mut Llio ) } ;
2021-06-01 17:08:31 +00:00
if xl . power_csr . rf ( utra ::power ::EV_PENDING_USB_ATTACH ) ! = 0 {
if let Some ( conn ) = xl . handler_conn {
xous ::try_send_message ( conn ,
xous ::Message ::new_scalar ( Opcode ::EventUsbHappened . to_usize ( ) . unwrap ( ) ,
0 , 0 , 0 , 0 ) ) . map ( | _ | ( ) ) . unwrap ( ) ;
} else {
log ::error! ( " |handle_event_irq: USB interrupt, but no connection for notification! " )
}
} else if xl . power_csr . rf ( utra ::power ::EV_PENDING_ACTIVITY_UPDATE ) ! = 0 {
if let Some ( conn ) = xl . handler_conn {
let activity = xl . power_csr . rf ( utra ::power ::ACTIVITY_RATE_COUNTS_AWAKE ) ;
xous ::try_send_message ( conn ,
xous ::Message ::new_scalar ( Opcode ::EventActivityHappened . to_usize ( ) . unwrap ( ) ,
activity as usize , 0 , 0 , 0 ) ) . map ( | _ | ( ) ) . unwrap ( ) ;
} else {
log ::error! ( " |handle_event_irq: activity interrupt, but no connection for notification! " )
}
2021-04-07 10:29:50 +00:00
}
xl . power_csr
. wo ( utra ::power ::EV_PENDING , xl . power_csr . r ( utra ::power ::EV_PENDING ) ) ;
}
2021-01-27 05:33:03 +00:00
2021-03-23 15:10:26 +00:00
pub fn log_init ( ) -> * mut u32 {
let gpio_base = xous ::syscall ::map_memory (
xous ::MemoryAddress ::new ( utra ::gpio ::HW_GPIO_BASE ) ,
None ,
4096 ,
xous ::MemoryFlags ::R | xous ::MemoryFlags ::W ,
)
. expect ( " couldn't map GPIO CSR range " ) ;
let mut gpio_csr = CSR ::new ( gpio_base . as_mut_ptr ( ) as * mut u32 ) ;
// setup the initial logging output
2021-06-24 05:52:44 +00:00
gpio_csr . wfo ( utra ::gpio ::UARTSEL_UARTSEL , crate ::api ::BOOT_UART ) ;
2021-03-23 15:10:26 +00:00
gpio_base . as_mut_ptr ( ) as * mut u32
}
2021-01-27 05:33:03 +00:00
impl Llio {
2021-03-23 15:10:26 +00:00
pub fn new ( handler_conn : xous ::CID , gpio_base : * mut u32 ) -> Llio {
2021-01-27 05:33:03 +00:00
let crg_csr = xous ::syscall ::map_memory (
xous ::MemoryAddress ::new ( utra ::crg ::HW_CRG_BASE ) ,
None ,
4096 ,
xous ::MemoryFlags ::R | xous ::MemoryFlags ::W ,
)
. expect ( " couldn't map CRG CSR range " ) ;
let info_csr = xous ::syscall ::map_memory (
xous ::MemoryAddress ::new ( utra ::info ::HW_INFO_BASE ) ,
None ,
4096 ,
xous ::MemoryFlags ::R | xous ::MemoryFlags ::W ,
)
. expect ( " couldn't map Info CSR range " ) ;
let identifier_csr = xous ::syscall ::map_memory (
xous ::MemoryAddress ::new ( utra ::identifier_mem ::HW_IDENTIFIER_MEM_BASE ) ,
None ,
4096 ,
xous ::MemoryFlags ::R | xous ::MemoryFlags ::W ,
)
. expect ( " couldn't map Identifier CSR range " ) ;
let event_csr = xous ::syscall ::map_memory (
xous ::MemoryAddress ::new ( utra ::btevents ::HW_BTEVENTS_BASE ) ,
None ,
4096 ,
xous ::MemoryFlags ::R | xous ::MemoryFlags ::W ,
)
. expect ( " couldn't map BtEvents CSR range " ) ;
let power_csr = xous ::syscall ::map_memory (
xous ::MemoryAddress ::new ( utra ::power ::HW_POWER_BASE ) ,
None ,
4096 ,
xous ::MemoryFlags ::R | xous ::MemoryFlags ::W ,
)
. expect ( " couldn't map Power CSR range " ) ;
2021-03-10 18:57:58 +00:00
let xadc_csr = xous ::syscall ::map_memory (
xous ::MemoryAddress ::new ( utra ::trng ::HW_TRNG_BASE ) ,
None ,
4096 ,
xous ::MemoryFlags ::R | xous ::MemoryFlags ::W ,
)
. expect ( " couldn't map Xadc CSR range " ) ; // note that Xadc is "in" the TRNG because TRNG can override Xadc in hardware
2021-01-27 20:03:30 +00:00
2021-03-28 10:14:41 +00:00
let ticktimer = ticktimer_server ::Ticktimer ::new ( ) . expect ( " Couldn't connect to Ticktimer " ) ;
2021-01-27 05:33:03 +00:00
let mut xl = Llio {
crg_csr : CSR ::new ( crg_csr . as_mut_ptr ( ) as * mut u32 ) ,
2021-03-23 15:10:26 +00:00
gpio_csr : CSR ::new ( gpio_base ) ,
2021-04-29 15:15:47 +00:00
gpio_susres : RegManager ::new ( gpio_base ) ,
2021-01-27 05:33:03 +00:00
info_csr : CSR ::new ( info_csr . as_mut_ptr ( ) as * mut u32 ) ,
identifier_csr : CSR ::new ( identifier_csr . as_mut_ptr ( ) as * mut u32 ) ,
2021-03-16 08:37:38 +00:00
handler_conn : Some ( handler_conn ) , // connection for messages from IRQ handler
2021-01-27 05:33:03 +00:00
event_csr : CSR ::new ( event_csr . as_mut_ptr ( ) as * mut u32 ) ,
2021-04-29 15:15:47 +00:00
event_susres : RegManager ::new ( event_csr . as_mut_ptr ( ) as * mut u32 ) ,
2021-01-27 05:33:03 +00:00
power_csr : CSR ::new ( power_csr . as_mut_ptr ( ) as * mut u32 ) ,
2021-04-29 15:15:47 +00:00
power_susres : RegManager ::new ( power_csr . as_mut_ptr ( ) as * mut u32 ) ,
2021-03-10 18:57:58 +00:00
xadc_csr : CSR ::new ( xadc_csr . as_mut_ptr ( ) as * mut u32 ) ,
2021-03-28 10:14:41 +00:00
ticktimer ,
2021-06-01 17:08:31 +00:00
activity_period : 24_000_000 , // 2 second interval initially
2021-01-27 20:03:30 +00:00
destruct_armed : false ,
2021-08-22 06:28:06 +00:00
uartmux_cache : BOOT_UART . into ( ) ,
2021-01-27 05:33:03 +00:00
} ;
xous ::claim_interrupt (
utra ::btevents ::BTEVENTS_IRQ ,
handle_event_irq ,
( & mut xl ) as * mut Llio as * mut usize ,
)
. expect ( " couldn't claim BtEvents irq " ) ;
xous ::claim_interrupt (
utra ::gpio ::GPIO_IRQ ,
handle_gpio_irq ,
( & mut xl ) as * mut Llio as * mut usize ,
)
. expect ( " couldn't claim GPIO irq " ) ;
2021-04-07 10:29:50 +00:00
xous ::claim_interrupt (
utra ::power ::POWER_IRQ ,
handle_power_irq ,
( & mut xl ) as * mut Llio as * mut usize ,
)
. expect ( " couldn't claim Power irq " ) ;
2021-04-29 15:15:47 +00:00
xl . gpio_susres . push ( RegOrField ::Reg ( utra ::gpio ::DRIVE ) , None ) ;
xl . gpio_susres . push ( RegOrField ::Reg ( utra ::gpio ::OUTPUT ) , None ) ;
xl . gpio_susres . push ( RegOrField ::Reg ( utra ::gpio ::INTPOL ) , None ) ;
xl . gpio_susres . push ( RegOrField ::Reg ( utra ::gpio ::INTENA ) , None ) ;
xl . gpio_susres . push_fixed_value ( RegOrField ::Reg ( utra ::gpio ::EV_PENDING ) , 0xFFFF_FFFF ) ;
xl . gpio_susres . push ( RegOrField ::Reg ( utra ::gpio ::EV_ENABLE ) , None ) ;
xl . gpio_susres . push ( RegOrField ::Field ( utra ::gpio ::UARTSEL_UARTSEL ) , None ) ;
2021-07-12 16:46:31 +00:00
xl . gpio_susres . push ( RegOrField ::Reg ( utra ::gpio ::USBDISABLE ) , None ) ;
2021-04-29 15:15:47 +00:00
xl . event_susres . push_fixed_value ( RegOrField ::Reg ( utra ::btevents ::EV_PENDING ) , 0xFFFF_FFFF ) ;
xl . event_susres . push ( RegOrField ::Reg ( utra ::btevents ::EV_ENABLE ) , None ) ;
2021-05-29 16:12:18 +00:00
xl . power_csr . rmwf ( utra ::power ::POWER_CRYPTO_ON , 0 ) ; // save power on crypto block
2021-04-29 15:15:47 +00:00
xl . power_susres . push ( RegOrField ::Reg ( utra ::power ::POWER ) , None ) ;
xl . power_susres . push ( RegOrField ::Reg ( utra ::power ::VIBE ) , None ) ;
2021-06-01 17:08:31 +00:00
xl . power_csr . wfo ( utra ::power ::SAMPLING_PERIOD_SAMPLE_PERIOD , xl . activity_period ) ; // 2 second sampling intervals
xl . power_susres . push ( RegOrField ::Reg ( utra ::power ::SAMPLING_PERIOD ) , None ) ;
xl . power_csr . wfo ( utra ::power ::EV_PENDING_ACTIVITY_UPDATE , 1 ) ;
xl . power_csr . rmwf ( utra ::power ::EV_ENABLE_ACTIVITY_UPDATE , 1 ) ;
2021-04-29 15:15:47 +00:00
xl . power_susres . push_fixed_value ( RegOrField ::Reg ( utra ::power ::EV_PENDING ) , 0xFFFF_FFFF ) ;
xl . power_susres . push ( RegOrField ::Reg ( utra ::power ::EV_ENABLE ) , None ) ;
2021-01-27 05:33:03 +00:00
xl
}
2021-04-29 15:15:47 +00:00
pub fn suspend ( & mut self ) {
2021-08-22 06:28:06 +00:00
self . uartmux_cache = self . gpio_csr . rf ( UARTSEL_UARTSEL ) . into ( ) ;
self . set_uart_mux ( UartType ::Kernel ) ;
2021-04-29 15:15:47 +00:00
self . gpio_susres . suspend ( ) ;
self . event_susres . suspend ( ) ;
self . power_susres . suspend ( ) ;
// this happens after suspend, so these disables are "lost" upon resume and replaced with the normal running values
self . gpio_csr . wo ( utra ::gpio ::EV_ENABLE , 0 ) ;
self . event_csr . wo ( utra ::btevents ::EV_ENABLE , 0 ) ;
self . power_csr . wo ( utra ::power ::EV_ENABLE , 0 ) ;
}
pub fn resume ( & mut self ) {
self . power_susres . resume ( ) ;
self . event_susres . resume ( ) ;
self . gpio_susres . resume ( ) ;
2021-08-22 06:28:06 +00:00
// add a short pause so we can capture KPs from other processes on resume
// this could cause some problems if there are llio-urgent activities to run after resume
// it's ok to remove this -- it's just to help capture debug logs on resume.
self . ticktimer . sleep_ms ( 500 ) . unwrap ( ) ;
self . set_uart_mux ( UartType ::from ( self . uartmux_cache as usize ) ) ;
2021-05-03 10:14:58 +00:00
}
2021-06-01 17:08:31 +00:00
#[ allow(dead_code) ]
pub fn activity_set_period ( & mut self , period : u32 ) {
self . activity_period = period ;
self . power_csr . wfo ( utra ::power ::SAMPLING_PERIOD_SAMPLE_PERIOD , period ) ;
}
pub fn activity_get_period ( & mut self ) -> u32 {
self . activity_period
}
2021-05-03 10:14:58 +00:00
2021-01-27 18:56:00 +00:00
pub fn gpio_dout ( & mut self , d : u32 ) {
self . gpio_csr . wfo ( utra ::gpio ::OUTPUT_OUTPUT , d ) ;
}
pub fn gpio_din ( & self ) -> u32 {
self . gpio_csr . rf ( utra ::gpio ::INPUT_INPUT )
}
pub fn gpio_drive ( & mut self , d : u32 ) {
self . gpio_csr . wfo ( utra ::gpio ::DRIVE_DRIVE , d ) ;
}
pub fn gpio_int_mask ( & mut self , d : u32 ) {
self . gpio_csr . wfo ( utra ::gpio ::INTENA_INTENA , d ) ;
}
pub fn gpio_int_as_falling ( & mut self , d : u32 ) {
self . gpio_csr . wfo ( utra ::gpio ::INTPOL_INTPOL , d ) ;
}
pub fn gpio_int_pending ( & self ) -> u32 {
self . gpio_csr . r ( utra ::gpio ::EV_PENDING ) & 0xff
}
pub fn gpio_int_ena ( & mut self , d : u32 ) {
self . gpio_csr . wo ( utra ::gpio ::EV_ENABLE , d & 0xff ) ;
}
pub fn set_uart_mux ( & mut self , mux : UartType ) {
match mux {
2021-08-04 14:40:53 +00:00
UartType ::Kernel = > {
log ::warn! ( " disabling WFI so that kernel console works as expected " ) ;
self . power_csr . rmwf ( utra ::power ::POWER_DISABLE_WFI , 1 ) ;
self . gpio_csr . wfo ( utra ::gpio ::UARTSEL_UARTSEL , 0 ) ;
} ,
UartType ::Log = > {
// this is a command mainly for debugging, so we'll accept the chance that we re-enabled WFI e.g.
// during a critical operation like SPINOR flashing because we swapped consoles at a bad time. Should be
// very rare and only affect devs...
log ::warn! ( " unsafe re-enabling WFI -- if you issued this command at a bad time, could have side effects " ) ;
self . power_csr . rmwf ( utra ::power ::POWER_DISABLE_WFI , 0 ) ;
self . gpio_csr . wfo ( utra ::gpio ::UARTSEL_UARTSEL , 1 )
} ,
UartType ::Application = > {
log ::warn! ( " disabling WFI so that app console works as expected " ) ;
self . power_csr . rmwf ( utra ::power ::POWER_DISABLE_WFI , 1 ) ;
self . gpio_csr . wfo ( utra ::gpio ::UARTSEL_UARTSEL , 2 )
} ,
2021-04-06 11:32:35 +00:00
_ = > info! ( " invalid UART type specified for mux, doing nothing. " ) ,
2021-01-27 18:56:00 +00:00
}
}
2021-01-27 20:03:30 +00:00
pub fn get_info_dna ( & self ) -> ( usize , usize ) {
( self . info_csr . r ( utra ::info ::DNA_ID0 ) as usize , self . info_csr . r ( utra ::info ::DNA_ID1 ) as usize )
}
pub fn get_info_git ( & self ) -> ( usize , usize ) {
(
( ( self . info_csr . rf ( utra ::info ::GIT_MAJOR_GIT_MAJOR ) as u32 ) < < 24 |
( self . info_csr . rf ( utra ::info ::GIT_MINOR_GIT_MINOR ) as u32 ) < < 16 |
( self . info_csr . rf ( utra ::info ::GIT_REVISION_GIT_REVISION ) as u32 ) < < 8 |
( self . info_csr . rf ( utra ::info ::GIT_GITEXTRA_GIT_GITEXTRA ) as u32 ) & 0xFF < < 0 ) as usize ,
self . info_csr . rf ( utra ::info ::GIT_GITREV_GIT_GITREV ) as usize
)
}
pub fn get_info_platform ( & self ) -> ( usize , usize ) {
( self . info_csr . r ( utra ::info ::PLATFORM_PLATFORM0 ) as usize , self . info_csr . r ( utra ::info ::PLATFORM_PLATFORM1 ) as usize )
}
pub fn get_info_target ( & self ) -> ( usize , usize ) {
( self . info_csr . r ( utra ::info ::PLATFORM_TARGET0 ) as usize , self . info_csr . r ( utra ::info ::PLATFORM_TARGET1 ) as usize )
}
pub fn power_audio ( & mut self , power_on : bool ) {
if power_on {
self . power_csr . rmwf ( utra ::power ::POWER_AUDIO , 1 ) ;
} else {
self . power_csr . rmwf ( utra ::power ::POWER_AUDIO , 0 ) ;
}
}
2021-05-29 16:12:18 +00:00
pub fn power_crypto ( & mut self , power_on : bool ) {
if power_on {
self . power_csr . rmwf ( utra ::power ::POWER_CRYPTO_ON , 1 ) ;
} else {
self . power_csr . rmwf ( utra ::power ::POWER_CRYPTO_ON , 0 ) ;
}
}
2021-06-01 09:02:30 +00:00
// apparently "override" is a reserved keyword in Rust???
pub fn wfi_override ( & mut self , override_ : bool ) {
if override_ {
self . power_csr . rmwf ( utra ::power ::POWER_DISABLE_WFI , 1 ) ;
} else {
self . power_csr . rmwf ( utra ::power ::POWER_DISABLE_WFI , 0 ) ;
}
}
pub fn debug_powerdown ( & mut self , ena : bool ) {
if ena {
self . power_csr . rmwf ( utra ::gpio ::DEBUG_WFI , 1 ) ;
} else {
self . power_csr . rmwf ( utra ::gpio ::DEBUG_WFI , 0 ) ;
}
}
pub fn debug_wakeup ( & mut self , ena : bool ) {
if ena {
self . power_csr . rmwf ( utra ::gpio ::DEBUG_WAKEUP , 1 ) ;
} else {
self . power_csr . rmwf ( utra ::gpio ::DEBUG_WAKEUP , 0 ) ;
}
}
2021-05-29 16:12:18 +00:00
pub fn power_crypto_status ( & self ) -> ( bool , bool , bool , bool ) {
let sha = if self . power_csr . rf ( utra ::power ::CLK_STATUS_SHA_ON ) = = 0 { false } else { true } ;
let engine = if self . power_csr . rf ( utra ::power ::CLK_STATUS_ENGINE_ON ) = = 0 { false } else { true } ;
let force = if self . power_csr . rf ( utra ::power ::CLK_STATUS_BTPOWER_ON ) = = 0 { false } else { true } ;
let overall = if self . power_csr . rf ( utra ::power ::CLK_STATUS_CRYPTO_ON ) = = 0 { false } else { true } ;
( overall , sha , engine , force )
}
2021-01-27 20:03:30 +00:00
pub fn power_self ( & mut self , power_on : bool ) {
if power_on {
2021-04-06 11:32:35 +00:00
info! ( " setting self-power state to on " ) ;
2021-01-27 20:03:30 +00:00
self . power_csr . rmwf ( utra ::power ::POWER_SELF , 1 ) ;
} else {
2021-04-06 11:32:35 +00:00
info! ( " setting self-power state to OFF " ) ;
2021-03-10 20:09:47 +00:00
self . power_csr . rmwf ( utra ::power ::POWER_STATE , 0 ) ;
2021-01-27 20:03:30 +00:00
self . power_csr . rmwf ( utra ::power ::POWER_SELF , 0 ) ;
}
}
debugging BOOST mode glitch issue
Here's an arcane bug for you:
When powering down, the Spartan 7 3.3V I/O drivers will
output a roughly 800us wide glitch as 3.3V falls below around
1.7V, if the 0.95V core voltage comes down first (which it does
because we have an active discharge on 0.95V, but not on 3.3V).
In this case, it's just wide and high enough to cause the
BOOST mode line to engage, which takes the BOOST output
and ties it to VBUS, which is just enough for the system to
think you're powering it back on again. Which is not what
you want when powering down.
The ultimate fix was to put a heftier pull-down on the BOOST
line so that it can't trip the BOOST output into the VBUS.
Another option could be more active discharge FETs, but that
would require a full board spin and we are already at certification,
so we'll stick with the stronger pull-down on BOOST. It seems to
be a robust fix in the worst corner cases we can find.
The downside is that when powering an external USB device,
you are burning an extra 30mW of power overcoming the pull-down,
but typically, any external USB devices plugged into Precursor
will burn far in excess of that, so the delta should be negligible.
2021-04-23 07:33:14 +00:00
pub fn power_boost_mode ( & mut self , boost_on : bool ) {
if boost_on {
2021-01-27 20:03:30 +00:00
self . power_csr . rmwf ( utra ::power ::POWER_BOOSTMODE , 1 ) ;
} else {
self . power_csr . rmwf ( utra ::power ::POWER_BOOSTMODE , 0 ) ;
}
}
pub fn ec_snoop_allow ( & mut self , allow : bool ) {
if allow {
self . power_csr . rmwf ( utra ::power ::POWER_EC_SNOOP , 1 ) ;
} else {
self . power_csr . rmwf ( utra ::power ::POWER_EC_SNOOP , 0 ) ;
}
}
pub fn ec_reset ( & mut self ) {
self . power_csr . rmwf ( utra ::power ::POWER_RESET_EC , 1 ) ;
2021-04-14 20:10:47 +00:00
self . ticktimer . sleep_ms ( 20 ) . unwrap ( ) ;
2021-01-27 20:03:30 +00:00
self . power_csr . rmwf ( utra ::power ::POWER_RESET_EC , 0 ) ;
}
pub fn ec_power_on ( & mut self ) {
self . power_csr . rmwf ( utra ::power ::POWER_UP5K_ON , 1 ) ;
}
pub fn self_destruct ( & mut self , code : u32 ) {
if self . destruct_armed & & code = = 0x3141_5926 {
2021-04-22 17:49:03 +00:00
self . ticktimer . sleep_ms ( 100 ) . unwrap ( ) ; // give a moment for any last words (like clearing the screen, powering down, etc.)
2021-01-27 20:03:30 +00:00
self . power_csr . rmwf ( utra ::power ::POWER_SELFDESTRUCT , 1 ) ;
} else if ! self . destruct_armed & & code = = 0x2718_2818 {
self . destruct_armed = true ;
} else {
self . destruct_armed = false ;
2021-04-06 11:32:35 +00:00
error! ( " self destruct attempted, but incorrect code sequence presented. " ) ;
2021-01-27 20:03:30 +00:00
}
}
pub fn vibe ( & mut self , pattern : VibePattern ) {
match pattern {
VibePattern ::Short = > {
self . power_csr . wfo ( utra ::power ::VIBE_VIBE , 1 ) ;
2021-04-16 11:27:48 +00:00
self . ticktimer . sleep_ms ( 80 ) . unwrap ( ) ;
2021-01-27 20:03:30 +00:00
self . power_csr . wfo ( utra ::power ::VIBE_VIBE , 0 ) ;
} ,
VibePattern ::Long = > {
self . power_csr . wfo ( utra ::power ::VIBE_VIBE , 1 ) ;
2021-03-28 10:14:41 +00:00
self . ticktimer . sleep_ms ( 1000 ) . unwrap ( ) ;
2021-01-27 20:03:30 +00:00
self . power_csr . wfo ( utra ::power ::VIBE_VIBE , 0 ) ;
} ,
VibePattern ::Double = > {
self . power_csr . wfo ( utra ::power ::VIBE_VIBE , 1 ) ;
2021-04-16 11:27:48 +00:00
self . ticktimer . sleep_ms ( 150 ) . unwrap ( ) ;
2021-01-27 20:03:30 +00:00
self . power_csr . wfo ( utra ::power ::VIBE_VIBE , 0 ) ;
2021-03-28 10:14:41 +00:00
self . ticktimer . sleep_ms ( 250 ) . unwrap ( ) ;
2021-01-27 20:03:30 +00:00
self . power_csr . wfo ( utra ::power ::VIBE_VIBE , 1 ) ;
2021-04-16 11:27:48 +00:00
self . ticktimer . sleep_ms ( 150 ) . unwrap ( ) ;
2021-01-27 20:03:30 +00:00
self . power_csr . wfo ( utra ::power ::VIBE_VIBE , 0 ) ;
} ,
}
}
2021-03-10 18:57:58 +00:00
pub fn xadc_vbus ( & self ) -> u16 {
self . xadc_csr . rf ( utra ::trng ::XADC_VBUS_XADC_VBUS ) as u16
}
pub fn xadc_vccint ( & self ) -> u16 {
self . xadc_csr . rf ( utra ::trng ::XADC_VCCINT_XADC_VCCINT ) as u16
}
pub fn xadc_vccaux ( & self ) -> u16 {
self . xadc_csr . rf ( utra ::trng ::XADC_VCCAUX_XADC_VCCAUX ) as u16
}
pub fn xadc_vccbram ( & self ) -> u16 {
self . xadc_csr . rf ( utra ::trng ::XADC_VCCBRAM_XADC_VCCBRAM ) as u16
}
pub fn xadc_usbn ( & self ) -> u16 {
self . xadc_csr . rf ( utra ::trng ::XADC_USB_N_XADC_USB_N ) as u16
}
pub fn xadc_usbp ( & self ) -> u16 {
self . xadc_csr . rf ( utra ::trng ::XADC_USB_P_XADC_USB_P ) as u16
}
pub fn xadc_temperature ( & self ) -> u16 {
self . xadc_csr . rf ( utra ::trng ::XADC_TEMPERATURE_XADC_TEMPERATURE ) as u16
}
pub fn xadc_gpio5 ( & self ) -> u16 {
self . xadc_csr . rf ( utra ::trng ::XADC_GPIO5_XADC_GPIO5 ) as u16
}
pub fn xadc_gpio2 ( & self ) -> u16 {
self . xadc_csr . rf ( utra ::trng ::XADC_GPIO2_XADC_GPIO2 ) as u16
}
2021-04-07 10:29:50 +00:00
pub fn rtc_int_ena ( & mut self , ena : bool ) {
let value = if ena { 1 } else { 0 } ;
self . event_csr . rmwf ( utra ::btevents ::EV_ENABLE_RTC_INT , value ) ;
}
pub fn com_int_ena ( & mut self , ena : bool ) {
let value = if ena { 1 } else { 0 } ;
self . event_csr . rmwf ( utra ::btevents ::EV_ENABLE_COM_INT , value ) ;
}
pub fn usb_int_ena ( & mut self , ena : bool ) {
let value = if ena { 1 } else { 0 } ;
self . power_csr . rmwf ( utra ::power ::EV_PENDING_USB_ATTACH , value ) ;
}
2021-07-12 16:46:31 +00:00
pub fn get_usb_disable ( & self ) -> bool {
if self . gpio_csr . rf ( utra ::gpio ::USBDISABLE_USBDISABLE ) ! = 0 {
true
} else {
false
}
}
pub fn set_usb_disable ( & mut self , state : bool ) {
if state {
self . gpio_csr . wfo ( utra ::gpio ::USBDISABLE_USBDISABLE , 1 ) ;
} else {
self . gpio_csr . wfo ( utra ::gpio ::USBDISABLE_USBDISABLE , 0 ) ;
}
}
2021-01-27 05:33:03 +00:00
}
}
// a stub to try to avoid breaking hosted mode for as long as possible.
2021-08-02 10:11:48 +00:00
#[ cfg(not(any(target_os = " none " , target_os = " xous " ))) ]
2021-01-27 05:33:03 +00:00
mod implementation {
2021-03-16 11:28:01 +00:00
use llio ::api ::* ;
2021-01-27 05:33:03 +00:00
use log ::{ error , info } ;
2021-04-07 20:38:01 +00:00
#[ derive(Copy, Clone, Debug) ]
2021-01-27 05:33:03 +00:00
pub struct Llio {
}
2021-03-23 15:10:26 +00:00
pub fn log_init ( ) -> * mut u32 { 0 as * mut u32 }
2021-01-27 05:33:03 +00:00
impl Llio {
2021-03-23 15:10:26 +00:00
pub fn new ( _handler_conn : xous ::CID , _gpio_base : * mut u32 ) -> Llio {
2021-01-27 05:33:03 +00:00
Llio {
}
}
2021-04-29 15:15:47 +00:00
pub fn suspend ( & self ) { }
pub fn resume ( & self ) { }
2021-03-10 17:45:54 +00:00
pub fn gpio_dout ( & self , _d : u32 ) { }
pub fn gpio_din ( & self , ) -> u32 { 0xDEAD_BEEF }
pub fn gpio_drive ( & self , _d : u32 ) { }
pub fn gpio_int_mask ( & self , _d : u32 ) { }
pub fn gpio_int_as_falling ( & self , _d : u32 ) { }
pub fn gpio_int_pending ( & self , ) -> u32 { 0x0 }
pub fn gpio_int_ena ( & self , _d : u32 ) { }
pub fn set_uart_mux ( & self , _mux : UartType ) { }
pub fn get_info_dna ( & self , ) -> ( usize , usize ) { ( 0 , 0 ) }
pub fn get_info_git ( & self , ) -> ( usize , usize ) { ( 0 , 0 ) }
pub fn get_info_platform ( & self , ) -> ( usize , usize ) { ( 0 , 0 ) }
pub fn get_info_target ( & self , ) -> ( usize , usize ) { ( 0 , 0 ) }
pub fn power_audio ( & self , _power_on : bool ) { }
2021-05-29 16:12:18 +00:00
pub fn power_crypto ( & self , _power_on : bool ) { }
pub fn power_crypto_status ( & self ) -> ( bool , bool , bool , bool ) {
( true , true , true , true )
}
2021-03-10 17:45:54 +00:00
pub fn power_self ( & self , _power_on : bool ) { }
pub fn power_boost_mode ( & self , _power_on : bool ) { }
pub fn ec_snoop_allow ( & self , _power_on : bool ) { }
pub fn ec_reset ( & self , ) { }
pub fn ec_power_on ( & self , ) { }
pub fn self_destruct ( & self , _code : u32 ) { }
2021-04-16 11:30:56 +00:00
pub fn vibe ( & self , pattern : VibePattern ) {
log ::info! ( " Imagine your keyboard vibrating: {:?} " , pattern ) ;
}
2021-03-10 18:57:58 +00:00
pub fn xadc_vbus ( & self ) -> u16 {
0
}
pub fn xadc_vccint ( & self ) -> u16 {
0
}
pub fn xadc_vccaux ( & self ) -> u16 {
0
}
pub fn xadc_vccbram ( & self ) -> u16 {
0
}
pub fn xadc_usbn ( & self ) -> u16 {
0
}
pub fn xadc_usbp ( & self ) -> u16 {
0
}
pub fn xadc_temperature ( & self ) -> u16 {
0
}
pub fn xadc_gpio5 ( & self ) -> u16 {
0
}
pub fn xadc_gpio2 ( & self ) -> u16 {
0
}
2021-04-07 10:29:50 +00:00
pub fn rtc_int_ena ( self , _ena : bool ) {
}
pub fn com_int_ena ( self , _ena : bool ) {
}
pub fn usb_int_ena ( self , _ena : bool ) {
}
2021-06-01 19:45:15 +00:00
pub fn debug_powerdown ( & mut self , _ena : bool ) {
}
pub fn debug_wakeup ( & mut self , _ena : bool ) {
}
pub fn activity_get_period ( & mut self ) -> u32 {
12_000_000
}
pub fn wfi_override ( & mut self , _override_ : bool ) {
}
2021-07-12 16:46:31 +00:00
pub fn get_usb_disable ( & self ) -> bool {
false
}
pub fn set_usb_disable ( & mut self , _state : bool ) {
}
2021-05-05 16:45:14 +00:00
}
}
2021-05-03 10:14:58 +00:00
2021-05-05 16:45:14 +00:00
fn i2c_thread ( sid0 : usize , sid1 : usize , sid2 : usize , sid3 : usize ) {
let i2c_sid = xous ::SID ::from_u32 ( sid0 as u32 , sid1 as u32 , sid2 as u32 , sid3 as u32 ) ;
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 " ) ;
let mut susres = susres ::Susres ::new ( & xns , I2cOpcode ::SuspendResume as u32 , sr_cid ) . expect ( " couldn't create suspend/resume object " ) ;
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 , _ , _ , _ , {
i2c . suspend ( ) ;
susres . suspend_until_resume ( token ) . expect ( " couldn't execute suspend/resume " ) ;
i2c . resume ( ) ;
} ) ,
Some ( I2cOpcode ::IrqI2cTxrxWriteDone ) = > msg_scalar_unpack! ( msg , _ , _ , _ , _ , {
// I2C state machine handler irq result
i2c . report_write_done ( ) ;
} ) ,
Some ( I2cOpcode ::IrqI2cTxrxReadDone ) = > msg_scalar_unpack! ( msg , _ , _ , _ , _ , {
// 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 ( ) -> ! {
use crate ::implementation ::Llio ;
2021-03-23 15:10:26 +00:00
// very early on map in the GPIO base so we can have the right logging enabled
let gpio_base = crate ::implementation ::log_init ( ) ;
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 ) ;
2021-04-06 11:32:35 +00:00
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-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)
let llio_sid = xns . register_name ( api ::SERVER_NAME_LLIO , Some ( 7 ) ) . 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
2021-06-15 09:41:14 +00:00
// each connection to llio also creates an i2c connection, so it has the same expectation list
2021-08-10 22:15:04 +00:00
let i2c_sid = xns . register_name ( api ::SERVER_NAME_I2C , Some ( 7 ) ) . expect ( " can't register I2C thread " ) ;
2021-05-05 16:45:14 +00:00
log ::trace! ( " registered I2C thread with NS -- {:?} " , i2c_sid ) ;
let ( sid0 , sid1 , sid2 , sid3 ) = i2c_sid . to_u32 ( ) ;
xous ::create_thread_4 ( i2c_thread , sid0 as usize , sid1 as usize , sid2 as usize , sid3 as usize ) . expect ( " couldn't start I2C handler thread " ) ;
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-03-15 16:11:23 +00:00
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 " ) ;
let mut susres = susres ::Susres ::new ( & 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
2021-04-06 11:32:35 +00:00
log ::trace! ( " starting main loop " ) ;
2021-01-27 05:33:03 +00:00
loop {
2021-05-05 16:45:14 +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 ( ) ;
susres . suspend_until_resume ( token ) . expect ( " couldn't execute suspend/resume " ) ;
llio . resume ( ) ;
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 , _ , _ , _ , _ , {
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 " ) ;
} ) ,
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 ;
} ) ,
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 = > {
error! ( " couldn't convert opcode: {:?} " , msg ) ;
}
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 {
error! ( " ran out of space registering callback " ) ;
}
}
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
xous ::send_message ( scb . server_to_cb_cid ,
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 )
) . unwrap ( ) ;
} ;
}
2021-05-20 03:01:00 +00:00
}