refactor i2c to be in the implementation block

This commit is contained in:
bunnie 2021-05-03 18:14:58 +08:00
parent 8e8b515a36
commit d65e91f792
2 changed files with 132 additions and 23 deletions

View file

@ -13,21 +13,6 @@ enum I2cState {
Read,
}
#[cfg(not(target_os = "none"))]
pub struct I2cStateMachine {
}
#[cfg(not(target_os = "none"))]
impl I2cStateMachine {
pub fn new(_ticktimer: ticktimer_server::Ticktimer, _i2c_base: *mut u32) -> Self {
I2cStateMachine {}
}
pub fn initiate(&mut self, _transaction: I2cTransaction ) -> I2cStatus {
I2cStatus::ResponseInProgress
}
pub fn handler(&mut self) {
}
pub fn is_busy(&self) -> bool { false }
}
#[cfg(target_os = "none")]
pub struct I2cStateMachine {
transaction: I2cTransaction,
@ -282,4 +267,109 @@ impl I2cStateMachine {
}
}
}
// interrupt context-friendly handler
pub fn handler_i(&mut self) {
self.trace();
// check if the transaction had actually timed out
let now = self.ticktimer.elapsed_ms();
if now - self.timestamp > self.transaction.timeout_ms as u64 {
log::error!("I2C transaction timed out: {}ms elapsed", now - self.timestamp);
// 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 => {
log::trace!("I2C handler: WRITE");
if let Some(txbuf) = self.transaction.txbuf {
// 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");
}
}
// send next byte if there is one
if self.index < self.transaction.txlen {
log::trace!("write->write");
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 {
if let Some(_rxbuf) = self.transaction.rxbuf {
// initiate bus address with read bit set
log::trace!("write->read");
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;
}
}
self.trace();
} 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 => {
log::trace!("I2C handler: READ");
if let Some(mut rxbuf) = self.transaction.rxbuf {
if self.index > 0 {
// we are re-entering from a previous call, store the read value from the previous call
rxbuf[self.index as usize - 1] = self.i2c_csr.rf(utra::i2c::RXR_RXR) as u8;
log::trace!("READ got 0x{:02x}", rxbuf[self.index as usize - 1]);
self.transaction.rxbuf = Some(rxbuf);
}
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 {
log::trace!("I2C handler: read done {:?}", self.transaction);
self.report_read_done();
self.state = I2cState::Idle;
self.listener = None;
}
} else {
// we should never get here, because rxbuf was checked as Some() by the setup routine
log::error!("LLIO|I2C: illegal read state");
}
self.trace();
},
I2cState::Idle => {
log::error!("LLIO|I2C: received interrupt event when no transaciton pending!");
}
}
}
}

View file

@ -28,6 +28,7 @@ mod implementation {
use utralib::generated::*;
use num_traits::ToPrimitive;
use susres::{RegManager, RegOrField, SuspendResume};
use crate::i2c::*;
#[allow(dead_code)]
pub struct Llio {
@ -46,6 +47,7 @@ mod implementation {
xadc_csr: utralib::CSR<u32>, // be careful with this as XADC is shared with TRNG
ticktimer: ticktimer_server::Ticktimer,
destruct_armed: bool,
i2c_machine: I2cStateMachine,
}
fn handle_event_irq(_irq_no: usize, arg: *mut usize) {
@ -177,6 +179,7 @@ mod implementation {
.expect("couldn't map Xadc CSR range"); // note that Xadc is "in" the TRNG because TRNG can override Xadc in hardware
let ticktimer = ticktimer_server::Ticktimer::new().expect("Couldn't connect to Ticktimer");
let i2c_ticktimer = ticktimer_server::Ticktimer::new().expect("Couldn't connect to Ticktimer");
let mut xl = Llio {
crg_csr: CSR::new(crg_csr.as_mut_ptr() as *mut u32),
@ -194,6 +197,7 @@ mod implementation {
xadc_csr: CSR::new(xadc_csr.as_mut_ptr() as *mut u32),
ticktimer,
destruct_armed: false,
i2c_machine: I2cStateMachine::new(i2c_ticktimer, i2c_csr.as_mut_ptr() as *mut u32),
};
xous::claim_interrupt(
@ -280,6 +284,16 @@ mod implementation {
self.i2c_susres.resume();
}
pub fn i2c_handler(&mut self) {
self.i2c_machine.handler();
}
pub fn i2c_initiate(&mut self, i2c_txrx: llio::I2cTransaction) -> llio::I2cStatus {
self.i2c_machine.initiate(i2c_txrx)
}
pub fn i2c_is_busy(&mut self) -> bool {
self.i2c_machine.is_busy()
}
pub fn gpio_dout(&mut self, d: u32) {
self.gpio_csr.wfo(utra::gpio::OUTPUT_OUTPUT, d);
}
@ -523,6 +537,16 @@ mod implementation {
}
pub fn usb_int_ena(self, _ena: bool) {
}
pub fn i2c_handler(&mut self) {
}
pub fn i2c_initiate(&mut self, i2c_txrx: I2cTransaction) -> I2cStatus {
I2cStatus::ResponseInProgress
}
pub fn i2c_is_busy(&mut self) -> bool {
false
}
}
}
@ -552,11 +576,6 @@ fn xmain() -> ! {
let handler_conn = xous::connect(llio_sid).expect("can't create IRQ handler connection");
let mut llio = Llio::new(handler_conn, gpio_base);
// ticktimer is a well-known server
let ticktimer = ticktimer_server::Ticktimer::new().expect("Couldn't connect to Ticktimer");
// create an i2c state machine handler
let mut i2c_machine = I2cStateMachine::new(ticktimer, llio.get_i2c_base());
// 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");
@ -688,16 +707,16 @@ fn xmain() -> ! {
}),
Some(Opcode::IrqI2cTxrxDone) => msg_scalar_unpack!(msg, _, _, _, _, {
// I2C state machine handler irq received
i2c_machine.handler();
llio.i2c_handler();
}),
Some(Opcode::I2cTxRx) => {
let mut buffer = unsafe { Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap()) };
let i2c_txrx = buffer.to_original::<llio::api::I2cTransaction, _>().unwrap();
let status = i2c_machine.initiate(i2c_txrx);
let status = llio.i2c_initiate(i2c_txrx);
buffer.replace(status).unwrap();
}
Some(Opcode::I2cIsBusy) => msg_blocking_scalar_unpack!(msg, _, _, _, _, {
let busy = if i2c_machine.is_busy() {1} else {0};
let busy = if llio.i2c_is_busy() {1} else {0};
xous::return_scalar(msg.sender, busy as _).expect("couldn't return I2cIsBusy");
}),
Some(Opcode::EventUsbAttachSubscribe) => {