2021-05-05 21:13:23 +00:00
use crate ::api ::* ;
2021-03-16 11:28:01 +00:00
2021-03-16 07:52:30 +00:00
use utralib ::* ;
2021-04-06 11:32:35 +00:00
use num_traits ::ToPrimitive ;
2021-05-05 16:45:14 +00:00
use susres ::{ RegManager , RegOrField , SuspendResume } ;
2021-05-05 19:40:04 +00:00
use heapless ::spsc ::Queue ;
2021-04-06 11:32:35 +00:00
2021-03-16 08:37:38 +00:00
#[ derive(Eq, PartialEq) ]
2021-03-16 07:52:30 +00:00
enum I2cState {
Idle ,
Write ,
Read ,
}
2021-03-16 11:28:01 +00:00
2021-05-05 16:45:14 +00:00
// ASSUME: we are only ever handling txrx done interrupts. If implementing ARB interrupts, this needs to be refactored to read the source and dispatch accordingly.
fn handle_i2c_irq ( _irq_no : usize , arg : * mut usize ) {
let i2c = unsafe { & mut * ( arg as * mut I2cStateMachine ) } ;
if let Some ( conn ) = i2c . handler_conn {
match i2c . handler_i ( ) {
I2cHandlerReport ::WriteDone = > {
xous ::try_send_message ( conn ,
xous ::Message ::new_scalar ( I2cOpcode ::IrqI2cTxrxWriteDone . to_usize ( ) . unwrap ( ) , 0 , 0 , 0 , 0 ) ) . map ( | _ | ( ) ) . unwrap ( ) ;
} ,
I2cHandlerReport ::ReadDone = > {
xous ::try_send_message ( conn ,
xous ::Message ::new_scalar ( I2cOpcode ::IrqI2cTxrxReadDone . to_usize ( ) . unwrap ( ) , 0 , 0 , 0 , 0 ) ) . map ( | _ | ( ) ) . unwrap ( ) ;
} ,
_ = > { } , // don't send any message if we're in progress
}
} else {
panic! ( " |handle_i2c_irq: TXRX done interrupt, but no connection for notification! " ) ;
}
i2c . i2c_csr
. wo ( utra ::i2c ::EV_PENDING , i2c . i2c_csr . r ( utra ::i2c ::EV_PENDING ) ) ;
}
2021-05-03 10:53:17 +00:00
#[ derive(Debug, Eq, PartialEq) ]
pub ( crate ) enum I2cHandlerReport {
WriteDone ,
ReadDone ,
InProgress ,
}
pub ( crate ) struct I2cStateMachine {
2021-05-05 16:45:14 +00:00
i2c_csr : utralib ::CSR < u32 > ,
i2c_susres : RegManager ::< { utra ::i2c ::I2C_NUMREGS } > ,
handler_conn : Option < xous ::CID > ,
2021-03-16 07:52:30 +00:00
transaction : I2cTransaction ,
state : I2cState ,
index : u32 , // index of the current buffer in the state machine
timestamp : u64 , // timestamp of the last transaction
2021-03-28 10:14:41 +00:00
ticktimer : ticktimer_server ::Ticktimer , // a connection to the ticktimer so we can measure timeouts
2021-04-07 18:30:57 +00:00
listener : Option < xous ::SID > ,
2021-05-03 10:53:17 +00:00
error : bool , // set if the interrupt handler encountered some kind of error
2021-04-06 11:32:35 +00:00
2021-05-05 19:40:04 +00:00
workqueue : Queue < I2cTransaction , 8 > ,
2021-04-06 11:32:35 +00:00
}
2021-03-16 07:52:30 +00:00
impl I2cStateMachine {
2021-05-05 16:45:14 +00:00
pub fn new ( handler_conn : xous ::CID ) -> Self {
let ticktimer = ticktimer_server ::Ticktimer ::new ( ) . expect ( " Couldn't connect to Ticktimer " ) ;
let i2c_csr = xous ::syscall ::map_memory (
xous ::MemoryAddress ::new ( utra ::i2c ::HW_I2C_BASE ) ,
None ,
4096 ,
xous ::MemoryFlags ::R | xous ::MemoryFlags ::W ,
)
. expect ( " couldn't map I2C CSR range " ) ;
let mut i2c = I2cStateMachine {
i2c_csr : CSR ::new ( i2c_csr . as_mut_ptr ( ) as * mut u32 ) ,
i2c_susres : RegManager ::new ( i2c_csr . as_mut_ptr ( ) as * mut u32 ) ,
handler_conn : Some ( handler_conn ) ,
2021-03-16 07:52:30 +00:00
transaction : I2cTransaction ::new ( ) ,
state : I2cState ::Idle ,
2021-03-28 20:35:11 +00:00
timestamp : ticktimer . elapsed_ms ( ) ,
2021-03-16 07:52:30 +00:00
ticktimer ,
index : 0 ,
2021-04-07 18:30:57 +00:00
listener : None ,
2021-05-03 10:53:17 +00:00
error : false ,
2021-05-05 19:40:04 +00:00
workqueue : Queue ::new ( ) ,
2021-05-05 16:45:14 +00:00
} ;
// disable interrupt, just in case it's enabled from e.g. a warm boot
i2c . i2c_csr . wfo ( utra ::i2c ::EV_ENABLE_TXRX_DONE , 0 ) ;
xous ::claim_interrupt (
utra ::i2c ::I2C_IRQ ,
handle_i2c_irq ,
( & mut i2c ) as * mut I2cStateMachine as * mut usize ,
)
. expect ( " couldn't claim I2C irq " ) ;
// initialize i2c clocks
// set the prescale assuming 100MHz cpu operation: 100MHz / ( 5 * 100kHz ) - 1 = 199
let clkcode = ( utralib ::LITEX_CONFIG_CLOCK_FREQUENCY as u32 ) / ( 5 * 100_000 ) - 1 ;
i2c . i2c_csr . wfo ( utra ::i2c ::PRESCALE_PRESCALE , clkcode & 0xFFFF ) ;
// enable the block
i2c . i2c_csr . rmwf ( utra ::i2c ::CONTROL_EN , 1 ) ;
// clear any interrupts pending, just in case something went pear-shaped during initialization
i2c . i2c_csr . wo ( utra ::i2c ::EV_PENDING , i2c . i2c_csr . r ( utra ::i2c ::EV_PENDING ) ) ;
// now enable interrupts
i2c . i2c_csr . wfo ( utra ::i2c ::EV_ENABLE_TXRX_DONE , 1 ) ;
// setup suspend/resume manager
i2c . i2c_susres . push ( RegOrField ::Field ( utra ::i2c ::PRESCALE_PRESCALE ) , None ) ;
i2c . i2c_susres . push ( RegOrField ::Reg ( utra ::i2c ::CONTROL ) , None ) ;
i2c . i2c_susres . push_fixed_value ( RegOrField ::Reg ( utra ::i2c ::EV_PENDING ) , 0xFFFF_FFFF ) ; // clear pending interrupts
i2c . i2c_susres . push ( RegOrField ::Reg ( utra ::i2c ::EV_ENABLE ) , None ) ;
i2c
2021-03-16 07:52:30 +00:00
}
2021-05-05 16:45:14 +00:00
pub fn suspend ( & mut self ) {
self . i2c_susres . suspend ( ) ;
// this happens after suspend, so these disables are "lost" upon resume and replaced with the normal running values
self . i2c_csr . wo ( utra ::i2c ::EV_ENABLE , 0 ) ;
}
pub fn resume ( & mut self ) {
self . i2c_susres . resume ( ) ;
}
2021-03-16 07:52:30 +00:00
pub fn initiate ( & mut self , transaction : I2cTransaction ) -> I2cStatus {
2021-05-05 21:05:17 +00:00
// state idle means the transaction is done
// listener None means any callback notifications are also done
// workqueue empty means, we have a clear path to do our thing
if self . workqueue . is_empty ( ) & & self . state = = I2cState ::Idle & & self . listener = = None {
self . checked_initiate ( transaction )
} else {
2021-05-05 19:40:04 +00:00
match self . workqueue . enqueue ( transaction ) {
Ok ( _ ) = > return I2cStatus ::ResponseInProgress ,
_ = > return I2cStatus ::ResponseBusy ,
}
}
}
fn checked_initiate ( & mut self , transaction : I2cTransaction ) -> I2cStatus {
2021-04-08 15:07:02 +00:00
log ::trace! ( " I2C initated with {:x?} " , transaction ) ;
2021-03-17 20:00:38 +00:00
// sanity-check the bounds limits
if transaction . txlen > 258 | | transaction . rxlen > 258 {
2021-03-16 07:52:30 +00:00
return I2cStatus ::ResponseFormatError
}
2021-03-28 20:35:11 +00:00
let now = self . ticktimer . elapsed_ms ( ) ;
2021-03-16 08:37:38 +00:00
if self . state ! = I2cState ::Idle & & ( ( now - self . timestamp ) < self . transaction . timeout_ms as u64 ) {
2021-03-16 07:52:30 +00:00
// we're in a transaction that hadn't timed out, can't accept a new one
2021-04-08 15:07:02 +00:00
log ::trace! ( " bus is busy " ) ;
2021-03-16 07:52:30 +00:00
I2cStatus ::ResponseBusy
} else {
if self . state ! = I2cState ::Idle {
// we're in a transaction, but previous transaction had timed out...
self . report_timeout ( ) ;
// reset our state parameter
self . state = I2cState ::Idle ;
self . index = 0 ;
2021-05-03 13:24:37 +00:00
// reset the block - this resest just the state machine and not the prescaler or interrupt enable configs
self . i2c_csr . wfo ( utra ::i2c ::CORE_RESET_RESET , 1 ) ;
2021-03-16 07:52:30 +00:00
}
2021-05-03 10:53:17 +00:00
self . error = false ;
2021-03-16 07:52:30 +00:00
self . timestamp = now ;
self . transaction = transaction . clone ( ) ;
2021-05-05 21:05:17 +00:00
assert! ( self . listener = = None , " initiating when previous transaction is still in progress! " ) ;
2021-04-07 18:30:57 +00:00
match transaction . listener {
None = > self . listener = None ,
Some ( ( s0 , s1 , s2 , s3 ) ) = > self . listener = Some ( xous ::SID ::from_u32 ( s0 , s1 , s2 , s3 ) ) ,
}
2021-05-05 21:05:17 +00:00
log ::trace! ( " initiate with listener {:?} " , self . listener ) ;
2021-03-16 07:52:30 +00:00
if self . transaction . status = = I2cStatus ::RequestIncoming {
self . transaction . status = I2cStatus ::ResponseInProgress ;
// now do the BusAddr stuff, so that the we can get the irq response
2021-03-16 08:37:38 +00:00
if let Some ( _txbuf ) = self . transaction . txbuf {
// initiate bus address with write bit set
2021-03-16 07:52:30 +00:00
self . state = I2cState ::Write ;
self . i2c_csr . wfo ( utra ::i2c ::TXR_TXR , ( self . transaction . bus_addr < < 1 | 0 ) as u32 ) ;
self . index = 0 ;
self . i2c_csr . wo ( utra ::i2c ::COMMAND ,
self . i2c_csr . ms ( utra ::i2c ::COMMAND_WR , 1 ) |
self . i2c_csr . ms ( utra ::i2c ::COMMAND_STA , 1 )
) ;
2021-04-08 15:07:02 +00:00
self . trace ( ) ;
2021-03-16 07:52:30 +00:00
I2cStatus ::ResponseInProgress
2021-03-16 08:37:38 +00:00
} else if let Some ( _rxbuf ) = self . transaction . rxbuf {
// initiate bus address with read bit set
2021-03-16 07:52:30 +00:00
self . state = I2cState ::Read ;
self . i2c_csr . wfo ( utra ::i2c ::TXR_TXR , ( self . transaction . bus_addr < < 1 | 1 ) as u32 ) ;
self . index = 0 ;
self . i2c_csr . wo ( utra ::i2c ::COMMAND ,
self . i2c_csr . ms ( utra ::i2c ::COMMAND_WR , 1 ) |
self . i2c_csr . ms ( utra ::i2c ::COMMAND_STA , 1 )
) ;
2021-04-08 15:07:02 +00:00
self . trace ( ) ;
2021-03-16 07:52:30 +00:00
I2cStatus ::ResponseInProgress
} else {
// no buffers specified, erase everything and go to idle
self . state = I2cState ::Idle ;
self . transaction = I2cTransaction ::new ( ) ;
2021-04-08 15:07:02 +00:00
self . trace ( ) ;
2021-03-16 07:52:30 +00:00
I2cStatus ::ResponseFormatError
}
2021-03-16 08:37:38 +00:00
} else {
2021-04-08 15:07:02 +00:00
log ::trace! ( " initiation format error " ) ;
self . trace ( ) ;
2021-03-16 08:37:38 +00:00
I2cStatus ::ResponseFormatError // the status field was not formatted correctly to accept the transaction
2021-03-16 07:52:30 +00:00
}
}
}
2021-05-05 19:40:04 +00:00
fn i2c_followup ( & mut self , trans : I2cTransaction ) -> Result < ( ) , xous ::Error > {
if let Some ( listener ) = self . listener . take ( ) {
2021-05-05 21:05:17 +00:00
log ::trace! ( " followup to listener {:?}: {:?} " , listener , trans ) ;
2021-05-05 19:40:04 +00:00
let cid = xous ::connect ( listener ) . unwrap ( ) ;
let buf = xous_ipc ::Buffer ::into_buf ( trans ) . or ( Err ( xous ::Error ::InternalError ) ) ? ;
buf . lend ( cid , I2cCallback ::Result . to_u32 ( ) . unwrap ( ) ) . map ( | _ | ( ) ) ? ;
2021-05-05 20:16:31 +00:00
unsafe { xous ::disconnect ( cid ) . unwrap ( ) } ;
2021-05-05 21:05:17 +00:00
} else {
log ::trace! ( " completed with transaction, but no listener! {:?} " , trans ) ;
2021-05-05 19:40:04 +00:00
} ;
if let Some ( work ) = self . workqueue . dequeue ( ) {
if self . checked_initiate ( work ) ! = I2cStatus ::ResponseInProgress {
log ::error! ( " Unable to initiate I2C transaction even though machine should be idle: {:?}. " , work ) ;
log ::error! ( " Probably, the I2C engine is going off the rails from here... " ) ;
}
} ;
Ok ( ( ) )
}
2021-04-08 21:24:29 +00:00
#[ allow(dead_code) ] // keep this around in case we figure out the NACK issue
2021-03-16 07:52:30 +00:00
fn report_nack ( & mut self ) {
2021-04-08 15:07:02 +00:00
log ::trace! ( " NACK " ) ;
2021-04-07 18:30:57 +00:00
// report the NACK situation to the listener
2021-03-16 07:52:30 +00:00
let mut nack = I2cTransaction ::new ( ) ;
nack . status = I2cStatus ::ResponseNack ;
2021-05-05 19:40:04 +00:00
self . i2c_followup ( nack ) . expect ( " couldn't send NACK to listeners " ) ;
2021-03-16 07:52:30 +00:00
}
fn report_timeout ( & mut self ) {
2021-05-03 13:24:37 +00:00
log ::error! ( " I2C timeout on transaction {:?} " , self . transaction ) ;
2021-03-16 07:52:30 +00:00
let mut timeout = I2cTransaction ::new ( ) ;
timeout . status = I2cStatus ::ResponseTimeout ;
2021-05-05 19:40:04 +00:00
self . i2c_followup ( timeout ) . expect ( " couldn't send timeout error to liseners " ) ;
2021-03-16 07:52:30 +00:00
}
2021-05-03 10:53:17 +00:00
pub fn report_write_done ( & mut self ) {
2021-04-08 15:07:02 +00:00
log ::trace! ( " write_done " ) ;
2021-03-16 07:52:30 +00:00
// report the end of a write-only transaction to all the listeners
let mut ack = I2cTransaction ::new ( ) ;
ack . status = I2cStatus ::ResponseWriteOk ;
2021-05-05 19:40:04 +00:00
self . i2c_followup ( ack ) . expect ( " couldn't send write ACK to listeners " ) ;
2021-03-16 07:52:30 +00:00
}
2021-05-03 10:53:17 +00:00
pub fn report_read_done ( & mut self ) {
2021-04-08 15:07:02 +00:00
log ::trace! ( " read_done " ) ;
2021-03-16 07:52:30 +00:00
// report the result of a read transaction to all the listeners
self . transaction . status = I2cStatus ::ResponseReadOk ;
2021-05-05 19:40:04 +00:00
log ::trace! ( " Sending read done {:?} " , self . transaction ) ;
self . i2c_followup ( self . transaction ) . expect ( " couldn't send read response to listeners " ) ;
2021-03-16 07:52:30 +00:00
}
2021-04-07 18:30:57 +00:00
pub fn is_busy ( & self ) -> bool {
if self . state = = I2cState ::Idle {
false
2021-04-06 11:32:35 +00:00
} else {
2021-04-07 18:30:57 +00:00
true
2021-04-06 11:32:35 +00:00
}
2021-03-16 07:52:30 +00:00
}
2021-04-08 15:07:02 +00:00
fn trace ( & self ) {
log ::trace! ( " I2C trace: PENDING: {:x}, ENABLE: {:x}, CMD: {:x}, STATUS: {:x}, CONTROL: {:x}, PRESCALE: {:x} " ,
self . i2c_csr . r ( utra ::i2c ::EV_PENDING ) ,
self . i2c_csr . r ( utra ::i2c ::EV_ENABLE ) ,
self . i2c_csr . r ( utra ::i2c ::COMMAND ) ,
self . i2c_csr . r ( utra ::i2c ::STATUS ) ,
self . i2c_csr . r ( utra ::i2c ::CONTROL ) ,
self . i2c_csr . r ( utra ::i2c ::PRESCALE ) ,
) ;
}
2021-05-03 10:14:58 +00:00
// interrupt context-friendly handler
2021-05-03 10:53:17 +00:00
pub fn handler_i ( & mut self ) -> I2cHandlerReport {
let mut report = I2cHandlerReport ::InProgress ;
2021-03-16 07:52:30 +00:00
match self . state {
I2cState ::Write = > {
if let Some ( txbuf ) = self . transaction . txbuf {
// send next byte if there is one
if self . index < self . transaction . txlen {
self . i2c_csr . wfo ( utra ::i2c ::TXR_TXR , txbuf [ self . index as usize ] as u32 ) ;
if self . index = = ( self . transaction . txlen - 1 ) & & self . transaction . rxbuf . is_none ( ) {
// send a stop bit if this is the very last in the series
self . i2c_csr . wo ( utra ::i2c ::COMMAND ,
self . i2c_csr . ms ( utra ::i2c ::COMMAND_WR , 1 ) |
self . i2c_csr . ms ( utra ::i2c ::COMMAND_STO , 1 )
) ;
} else {
self . i2c_csr . wfo ( utra ::i2c ::COMMAND_WR , 1 ) ;
}
self . index + = 1 ;
} else {
2021-03-16 08:37:38 +00:00
if let Some ( _rxbuf ) = self . transaction . rxbuf {
// initiate bus address with read bit set
2021-03-16 07:52:30 +00:00
self . state = I2cState ::Read ;
self . i2c_csr . wfo ( utra ::i2c ::TXR_TXR , ( self . transaction . bus_addr < < 1 | 1 ) as u32 ) ;
self . index = 0 ;
self . i2c_csr . wo ( utra ::i2c ::COMMAND ,
self . i2c_csr . ms ( utra ::i2c ::COMMAND_WR , 1 ) |
self . i2c_csr . ms ( utra ::i2c ::COMMAND_STA , 1 )
) ;
} else {
2021-05-03 10:53:17 +00:00
report = I2cHandlerReport ::WriteDone ;
2021-03-16 07:52:30 +00:00
self . state = I2cState ::Idle ;
}
}
} else {
// we should never get here, because txbuf was checked as Some() by the setup routine
}
} ,
I2cState ::Read = > {
2021-03-16 08:37:38 +00:00
if let Some ( mut rxbuf ) = self . transaction . rxbuf {
2021-03-16 07:52:30 +00:00
if self . index > 0 {
// we are re-entering from a previous call, store the read value from the previous call
2021-03-16 08:37:38 +00:00
rxbuf [ self . index as usize - 1 ] = self . i2c_csr . rf ( utra ::i2c ::RXR_RXR ) as u8 ;
2021-04-08 21:24:29 +00:00
self . transaction . rxbuf = Some ( rxbuf ) ;
2021-03-16 07:52:30 +00:00
}
if self . index < self . transaction . rxlen {
if self . index = = ( self . transaction . rxlen - 1 ) {
self . i2c_csr . wo ( utra ::i2c ::COMMAND ,
self . i2c_csr . ms ( utra ::i2c ::COMMAND_RD , 1 ) |
self . i2c_csr . ms ( utra ::i2c ::COMMAND_STO , 1 ) |
self . i2c_csr . ms ( utra ::i2c ::COMMAND_ACK , 1 )
) ;
} else {
self . i2c_csr . wfo ( utra ::i2c ::COMMAND_RD , 1 ) ;
}
self . index + = 1 ;
} else {
2021-05-03 10:53:17 +00:00
report = I2cHandlerReport ::ReadDone ;
2021-03-16 07:52:30 +00:00
self . state = I2cState ::Idle ;
}
} else {
// we should never get here, because rxbuf was checked as Some() by the setup routine
}
} ,
I2cState ::Idle = > {
2021-05-03 10:53:17 +00:00
// this shouldn't happen, all we can do is flag an error
self . error = true ;
2021-03-16 07:52:30 +00:00
}
}
2021-05-03 10:53:17 +00:00
report
2021-03-16 07:52:30 +00:00
}
}