2021-03-16 08:37:38 +00:00
use llio ::api ::* ;
2021-03-16 11:28:01 +00:00
#[ cfg(target_os = " none " ) ]
2021-03-16 07:52:30 +00:00
use utralib ::* ;
2021-04-06 11:32:35 +00:00
#[ cfg(target_os = " none " ) ]
use num_traits ::ToPrimitive ;
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
#[ cfg(not(target_os = " none " )) ]
pub struct I2cStateMachine {
}
#[ cfg(not(target_os = " none " )) ]
impl I2cStateMachine {
2021-03-31 19:47:10 +00:00
pub fn new ( _ticktimer : ticktimer_server ::Ticktimer , _i2c_base : * mut u32 ) -> Self {
2021-03-16 11:28:01 +00:00
I2cStateMachine { }
}
pub fn initiate ( & mut self , _transaction : I2cTransaction ) -> I2cStatus {
I2cStatus ::ResponseInProgress
}
pub fn handler ( & mut self ) {
}
2021-04-07 20:38:01 +00:00
pub fn is_busy ( & self ) -> bool { false }
2021-03-16 11:28:01 +00:00
}
#[ cfg(target_os = " none " ) ]
2021-03-16 08:37:38 +00:00
pub struct I2cStateMachine {
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-03-16 07:52:30 +00:00
i2c_csr : utralib ::CSR < u32 > ,
2021-04-07 18:30:57 +00:00
listener : Option < xous ::SID > ,
2021-03-16 07:52:30 +00:00
}
2021-04-06 11:32:35 +00:00
#[ cfg(target_os = " none " ) ]
2021-04-07 18:30:57 +00:00
fn send_i2c_response ( listener : xous ::SID , trans : I2cTransaction ) -> Result < ( ) , xous ::Error > {
let cid = xous ::connect ( listener ) . unwrap ( ) ;
2021-04-06 11:32:35 +00:00
let buf = xous_ipc ::Buffer ::into_buf ( trans ) . or ( Err ( xous ::Error ::InternalError ) ) ? ;
2021-04-07 18:30:57 +00:00
buf . lend ( cid , I2cCallback ::Result . to_u32 ( ) . unwrap ( ) ) . map ( | _ | ( ) ) ? ;
unsafe { xous ::disconnect ( cid ) }
2021-04-06 11:32:35 +00:00
}
2021-03-16 11:28:01 +00:00
#[ cfg(target_os = " none " ) ]
2021-03-16 07:52:30 +00:00
impl I2cStateMachine {
2021-03-28 10:14:41 +00:00
pub fn new ( ticktimer : ticktimer_server ::Ticktimer , i2c_base : * mut u32 ) -> Self {
2021-03-16 07:52:30 +00:00
I2cStateMachine {
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 ,
2021-03-16 08:37:38 +00:00
i2c_csr : CSR ::new ( i2c_base ) ,
2021-03-16 07:52:30 +00:00
index : 0 ,
2021-04-07 18:30:57 +00:00
listener : None ,
2021-03-16 07:52:30 +00:00
}
}
pub fn 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 ;
// now we're ready to move on and try a new transaction. We hope! Maybe the block should be reset?? TBD. Need to understand the nature of the timeouts better, if and when they do happen.
}
self . timestamp = now ;
self . transaction = transaction . clone ( ) ;
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-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-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-04-07 18:30:57 +00:00
if let Some ( listener ) = self . listener {
send_i2c_response ( listener , nack ) . expect ( " LLIO|I2C: couldn't send NACK to listeners " ) ;
} ;
2021-03-16 07:52:30 +00:00
}
fn report_timeout ( & mut self ) {
2021-04-08 15:07:02 +00:00
log ::trace! ( " timeout " ) ;
2021-03-16 07:52:30 +00:00
let mut timeout = I2cTransaction ::new ( ) ;
timeout . status = I2cStatus ::ResponseTimeout ;
2021-04-07 18:30:57 +00:00
if let Some ( listener ) = self . listener {
send_i2c_response ( listener , timeout ) . expect ( " LLIO|I2c: couldn't send timeout error to liseners " ) ;
} ;
2021-03-16 07:52:30 +00:00
}
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-04-07 18:30:57 +00:00
if let Some ( listener ) = self . listener {
send_i2c_response ( listener , ack ) . expect ( " LLIO|I2C: couldn't send write ACK to listeners " ) ;
} ;
2021-03-16 07:52:30 +00:00
}
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-04-07 18:30:57 +00:00
if let Some ( listener ) = self . listener {
send_i2c_response ( listener , self . transaction ) . expect ( " LLIO|I2C: 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-03-16 07:52:30 +00:00
pub fn handler ( & mut self ) {
2021-04-08 15:07:02 +00:00
self . trace ( ) ;
2021-03-16 07:52:30 +00:00
// check if the transaction had actually timed out
2021-03-28 20:35:11 +00:00
let now = self . ticktimer . elapsed_ms ( ) ;
2021-03-16 08:37:38 +00:00
if now - self . timestamp > self . transaction . timeout_ms as u64 {
2021-04-08 15:07:02 +00:00
log ::error! ( " I2C transaction timed out: {}ms elapsed " , now - self . timestamp ) ;
2021-03-16 07:52:30 +00:00
// previous transaction had timed out...
self . report_timeout ( ) ;
// reset our state parameter
self . state = I2cState ::Idle ;
self . index = 0 ;
self . timestamp = now ;
return ;
}
self . timestamp = now ;
match self . state {
I2cState ::Write = > {
2021-04-07 20:38:01 +00:00
log ::trace! ( " I2C handler: WRITE " ) ;
2021-03-16 07:52:30 +00:00
if let Some ( txbuf ) = self . transaction . txbuf {
2021-04-08 15:07:02 +00:00
// check ack bit--this actually doesn't seem to work, so excise the code
if false {
if self . i2c_csr . rf ( utra ::i2c ::STATUS_RXACK ) ! = 1 {
/*
self . state = I2cState ::Idle ;
self . transaction = I2cTransaction ::new ( ) ;
self . report_nack ( ) ; * /
log ::trace! ( " got NACK, ignoring " ) ;
}
2021-03-16 07:52:30 +00:00
}
// send next byte if there is one
if self . index < self . transaction . txlen {
2021-04-08 15:07:02 +00:00
log ::trace! ( " write->write " ) ;
2021-03-16 07:52:30 +00:00
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-04-08 15:07:02 +00:00
log ::trace! ( " write->read " ) ;
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 {
self . report_write_done ( ) ;
self . state = I2cState ::Idle ;
}
}
2021-04-08 15:07:02 +00:00
self . trace ( ) ;
2021-03-16 07:52:30 +00:00
} else {
// we should never get here, because txbuf was checked as Some() by the setup routine
log ::error! ( " LLIO|I2C: illegal write state " ) ;
}
} ,
I2cState ::Read = > {
2021-04-07 20:38:01 +00:00
log ::trace! ( " I2C handler: 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 15:07:02 +00:00
log ::trace! ( " READ got 0x{:02x} " , rxbuf [ self . index as usize - 1 ] ) ;
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-04-07 20:38:01 +00:00
log ::trace! ( " I2C handler: read done {:?} " , self . transaction ) ;
2021-03-16 07:52:30 +00:00
self . report_read_done ( ) ;
self . state = I2cState ::Idle ;
2021-04-07 18:30:57 +00:00
self . listener = None ;
2021-03-16 07:52:30 +00:00
}
} else {
// we should never get here, because rxbuf was checked as Some() by the setup routine
log ::error! ( " LLIO|I2C: illegal read state " ) ;
}
2021-04-08 15:07:02 +00:00
self . trace ( ) ;
2021-03-16 07:52:30 +00:00
} ,
I2cState ::Idle = > {
log ::error! ( " LLIO|I2C: received interrupt event when no transaciton pending! " ) ;
}
}
}
}