add a reset to an i2c core

This commit is contained in:
bunnie 2021-05-03 21:24:37 +08:00
parent 01e2c7edca
commit a4ca725c2a
7 changed files with 92 additions and 157 deletions

8
Cargo.lock generated
View file

@ -297,6 +297,7 @@ name = "codec"
version = "0.1.0"
dependencies = [
"heapless",
"llio",
"log",
"log-server",
"num-derive",
@ -304,6 +305,7 @@ dependencies = [
"susres",
"ticktimer-server",
"utralib",
"volatile",
"xous",
"xous-names",
]
@ -1558,6 +1560,12 @@ version = "0.9.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b5a972e5669d67ba988ce3dc826706fb0a8b01471c088cb0b6110b805cc36aed"
[[package]]
name = "volatile"
version = "0.4.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e4c2dbd44eb8b53973357e6e207e370f0c1059990df850aca1eca8947cf464f0"
[[package]]
name = "wasi"
version = "0.10.2+wasi-snapshot-preview1"

View file

@ -3,7 +3,7 @@
<device schemaVersion="1.1" xmlns:xs="http://www.w3.org/2001/XMLSchema-instance" xs:noNamespaceSchemaLocation="CMSIS-SVD.xsd" >
<vendor>litex</vendor>
<name>SOC</name>
<description><![CDATA[Litex SoC 2021-05-02 02:18:31]]></description>
<description><![CDATA[Litex SoC 2021-05-03 18:58:54]]></description>
<addressUnitBits>8</addressUnitBits>
<width>32</width>
@ -1070,10 +1070,10 @@ register to disable individual events.]]></description>
<register>
<name>GIT_GITREV</name>
<description><![CDATA[First 32-bits of the git revision. This documentation was built from git rev
``64fe00d9``, so this value is 1694367961, which should be enough to check out
``5129ab27``, so this value is 1361685287, which should be enough to check out
the exact git version used to build this firmware.]]></description>
<addressOffset>0x0014</addressOffset>
<resetValue>0x64fe00d9</resetValue>
<resetValue>0x5129ab27</resetValue>
<size>32</size>
<fields>
<field>
@ -1090,7 +1090,7 @@ the exact git version used to build this firmware.]]></description>
is ``1``, then the repository this was built from has one additional commit
beyond the tag indicated in `MAJOR`, `MINOR`, and `REVISION`.]]></description>
<addressOffset>0x0018</addressOffset>
<resetValue>0x05</resetValue>
<resetValue>0x06</resetValue>
<size>32</size>
<fields>
<field>
@ -1697,10 +1697,26 @@ upon the completion of one byte of data transfer.]]></description>
</field>
</fields>
</register>
<register>
<name>CORE_RESET</name>
<addressOffset>0x0018</addressOffset>
<resetValue>0x00</resetValue>
<size>32</size>
<fields>
<field>
<name>reset</name>
<msb>0</msb>
<bitRange>[0:0]</bitRange>
<lsb>0</lsb>
<description><![CDATA[Write `1` for a synchronous reset of the I2C core. Does not reset the prescale
value. This signal is outside of the OpenCores spec.]]></description>
</field>
</fields>
</register>
<register>
<name>EV_STATUS</name>
<description><![CDATA[Triggered on the falling edge of TIP]]></description>
<addressOffset>0x0018</addressOffset>
<addressOffset>0x001c</addressOffset>
<resetValue>0x00</resetValue>
<size>32</size>
<fields>
@ -1723,7 +1739,7 @@ upon the completion of one byte of data transfer.]]></description>
<register>
<name>EV_PENDING</name>
<description><![CDATA[Triggered on the falling edge of TIP]]></description>
<addressOffset>0x001c</addressOffset>
<addressOffset>0x0020</addressOffset>
<resetValue>0x00</resetValue>
<size>32</size>
<fields>
@ -1747,7 +1763,7 @@ clear or else it will re-trigger.]]></description>
<register>
<name>EV_ENABLE</name>
<description><![CDATA[Triggered on the falling edge of TIP]]></description>
<addressOffset>0x0020</addressOffset>
<addressOffset>0x0024</addressOffset>
<resetValue>0x00</resetValue>
<size>32</size>
<fields>
@ -1770,7 +1786,7 @@ clear or else it will re-trigger.]]></description>
</registers>
<addressBlock>
<offset>0</offset>
<size>0x24</size>
<size>0x28</size>
<usage>registers</usage>
</addressBlock>
<interrupt>
@ -3101,7 +3117,7 @@ inject too fast.]]></description>
<name>SEED1</name>
<description><![CDATA[Bits 32-63 of `SEED_SEED`. Seed used for the build]]></description>
<addressOffset>0x0000</addressOffset>
<resetValue>0x6ec50873</resetValue>
<resetValue>0x5f5e319d</resetValue>
<size>32</size>
<fields>
<field>
@ -3116,7 +3132,7 @@ inject too fast.]]></description>
<name>SEED0</name>
<description><![CDATA[Bits 0-31 of `SEED_SEED`.]]></description>
<addressOffset>0x0004</addressOffset>
<resetValue>0x7ff788bf</resetValue>
<resetValue>0x1a7dec69</resetValue>
<size>32</size>
<fields>
<field>

View file

@ -3,7 +3,7 @@
<device schemaVersion="1.1" xmlns:xs="http://www.w3.org/2001/XMLSchema-instance" xs:noNamespaceSchemaLocation="CMSIS-SVD.xsd" >
<vendor>litex</vendor>
<name>SOC</name>
<description><![CDATA[Litex SoC 2021-05-02 02:18:31]]></description>
<description><![CDATA[Litex SoC 2021-05-03 18:58:54]]></description>
<addressUnitBits>8</addressUnitBits>
<width>32</width>
@ -1070,10 +1070,10 @@ register to disable individual events.]]></description>
<register>
<name>GIT_GITREV</name>
<description><![CDATA[First 32-bits of the git revision. This documentation was built from git rev
``64fe00d9``, so this value is 1694367961, which should be enough to check out
``5129ab27``, so this value is 1361685287, which should be enough to check out
the exact git version used to build this firmware.]]></description>
<addressOffset>0x0014</addressOffset>
<resetValue>0x64fe00d9</resetValue>
<resetValue>0x5129ab27</resetValue>
<size>32</size>
<fields>
<field>
@ -1090,7 +1090,7 @@ the exact git version used to build this firmware.]]></description>
is ``1``, then the repository this was built from has one additional commit
beyond the tag indicated in `MAJOR`, `MINOR`, and `REVISION`.]]></description>
<addressOffset>0x0018</addressOffset>
<resetValue>0x05</resetValue>
<resetValue>0x06</resetValue>
<size>32</size>
<fields>
<field>
@ -1697,10 +1697,26 @@ upon the completion of one byte of data transfer.]]></description>
</field>
</fields>
</register>
<register>
<name>CORE_RESET</name>
<addressOffset>0x0018</addressOffset>
<resetValue>0x00</resetValue>
<size>32</size>
<fields>
<field>
<name>reset</name>
<msb>0</msb>
<bitRange>[0:0]</bitRange>
<lsb>0</lsb>
<description><![CDATA[Write `1` for a synchronous reset of the I2C core. Does not reset the prescale
value. This signal is outside of the OpenCores spec.]]></description>
</field>
</fields>
</register>
<register>
<name>EV_STATUS</name>
<description><![CDATA[Triggered on the falling edge of TIP]]></description>
<addressOffset>0x0018</addressOffset>
<addressOffset>0x001c</addressOffset>
<resetValue>0x00</resetValue>
<size>32</size>
<fields>
@ -1723,7 +1739,7 @@ upon the completion of one byte of data transfer.]]></description>
<register>
<name>EV_PENDING</name>
<description><![CDATA[Triggered on the falling edge of TIP]]></description>
<addressOffset>0x001c</addressOffset>
<addressOffset>0x0020</addressOffset>
<resetValue>0x00</resetValue>
<size>32</size>
<fields>
@ -1747,7 +1763,7 @@ clear or else it will re-trigger.]]></description>
<register>
<name>EV_ENABLE</name>
<description><![CDATA[Triggered on the falling edge of TIP]]></description>
<addressOffset>0x0020</addressOffset>
<addressOffset>0x0024</addressOffset>
<resetValue>0x00</resetValue>
<size>32</size>
<fields>
@ -1770,7 +1786,7 @@ clear or else it will re-trigger.]]></description>
</registers>
<addressBlock>
<offset>0</offset>
<size>0x24</size>
<size>0x28</size>
<usage>registers</usage>
</addressBlock>
<interrupt>
@ -3101,7 +3117,7 @@ inject too fast.]]></description>
<name>SEED1</name>
<description><![CDATA[Bits 32-63 of `SEED_SEED`. Seed used for the build]]></description>
<addressOffset>0x0000</addressOffset>
<resetValue>0x6ec50873</resetValue>
<resetValue>0x5f5e319d</resetValue>
<size>32</size>
<fields>
<field>
@ -3116,7 +3132,7 @@ inject too fast.]]></description>
<name>SEED0</name>
<description><![CDATA[Bits 0-31 of `SEED_SEED`.]]></description>
<addressOffset>0x0004</addressOffset>
<resetValue>0x7ff788bf</resetValue>
<resetValue>0x1a7dec69</resetValue>
<size>32</size>
<fields>
<field>

Binary file not shown.

View file

@ -72,7 +72,8 @@ impl I2cStateMachine {
// 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.
// 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);
}
self.error = false;
self.timestamp = now;
@ -132,7 +133,7 @@ impl I2cStateMachine {
};
}
fn report_timeout(&mut self) {
log::trace!("timeout");
log::error!("I2C timeout on transaction {:?}", self.transaction);
let mut timeout = I2cTransaction::new();
timeout.status = I2cStatus::ResponseTimeout;
if let Some(listener) = self.listener {
@ -176,128 +177,9 @@ impl I2cStateMachine {
self.i2c_csr.r(utra::i2c::PRESCALE),
);
}
#[cfg(feature = "non_irq_handler")]
pub fn handler(&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!");
}
}
}
// interrupt context-friendly handler
pub fn handler_i(&mut self) -> I2cHandlerReport {
/* // can't check timeouts in an interrupt handler
// 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;*/
let mut report = I2cHandlerReport::InProgress;
match self.state {

View file

@ -4,7 +4,6 @@
mod api;
use api::*;
mod i2c;
use i2c::*;
use log::{error, info};
@ -135,8 +134,6 @@ mod implementation {
}
impl Llio {
pub fn get_i2c_base(&self) -> *mut u32 { self.i2c_csr.base }
pub fn new(handler_conn: xous::CID, gpio_base: *mut u32) -> Llio {
let crg_csr = xous::syscall::map_memory(
xous::MemoryAddress::new(utra::crg::HW_CRG_BASE),
@ -488,8 +485,6 @@ mod implementation {
Llio {
}
}
pub fn get_i2c_base(&self) -> *mut u32 { 0 as *mut u32 }
pub fn suspend(&self) {}
pub fn resume(&self) {}
pub fn gpio_dout(&self, _d: u32) {}
@ -551,7 +546,9 @@ mod implementation {
pub fn usb_int_ena(self, _ena: bool) {
}
pub fn i2c_handler(&mut self) {
pub fn i2c_report_write_done(&mut self) {
}
pub fn i2c_report_read_done(&mut self) {
}
pub fn i2c_initiate(&mut self, i2c_txrx: I2cTransaction) -> I2cStatus {
I2cStatus::ResponseInProgress

View file

@ -3,7 +3,7 @@
<device schemaVersion="1.1" xmlns:xs="http://www.w3.org/2001/XMLSchema-instance" xs:noNamespaceSchemaLocation="CMSIS-SVD.xsd" >
<vendor>litex</vendor>
<name>SOC</name>
<description><![CDATA[Litex SoC 2021-05-02 02:18:31]]></description>
<description><![CDATA[Litex SoC 2021-05-03 18:58:54]]></description>
<addressUnitBits>8</addressUnitBits>
<width>32</width>
@ -1070,10 +1070,10 @@ register to disable individual events.]]></description>
<register>
<name>GIT_GITREV</name>
<description><![CDATA[First 32-bits of the git revision. This documentation was built from git rev
``64fe00d9``, so this value is 1694367961, which should be enough to check out
``5129ab27``, so this value is 1361685287, which should be enough to check out
the exact git version used to build this firmware.]]></description>
<addressOffset>0x0014</addressOffset>
<resetValue>0x64fe00d9</resetValue>
<resetValue>0x5129ab27</resetValue>
<size>32</size>
<fields>
<field>
@ -1090,7 +1090,7 @@ the exact git version used to build this firmware.]]></description>
is ``1``, then the repository this was built from has one additional commit
beyond the tag indicated in `MAJOR`, `MINOR`, and `REVISION`.]]></description>
<addressOffset>0x0018</addressOffset>
<resetValue>0x05</resetValue>
<resetValue>0x06</resetValue>
<size>32</size>
<fields>
<field>
@ -1697,10 +1697,26 @@ upon the completion of one byte of data transfer.]]></description>
</field>
</fields>
</register>
<register>
<name>CORE_RESET</name>
<addressOffset>0x0018</addressOffset>
<resetValue>0x00</resetValue>
<size>32</size>
<fields>
<field>
<name>reset</name>
<msb>0</msb>
<bitRange>[0:0]</bitRange>
<lsb>0</lsb>
<description><![CDATA[Write `1` for a synchronous reset of the I2C core. Does not reset the prescale
value. This signal is outside of the OpenCores spec.]]></description>
</field>
</fields>
</register>
<register>
<name>EV_STATUS</name>
<description><![CDATA[Triggered on the falling edge of TIP]]></description>
<addressOffset>0x0018</addressOffset>
<addressOffset>0x001c</addressOffset>
<resetValue>0x00</resetValue>
<size>32</size>
<fields>
@ -1723,7 +1739,7 @@ upon the completion of one byte of data transfer.]]></description>
<register>
<name>EV_PENDING</name>
<description><![CDATA[Triggered on the falling edge of TIP]]></description>
<addressOffset>0x001c</addressOffset>
<addressOffset>0x0020</addressOffset>
<resetValue>0x00</resetValue>
<size>32</size>
<fields>
@ -1747,7 +1763,7 @@ clear or else it will re-trigger.]]></description>
<register>
<name>EV_ENABLE</name>
<description><![CDATA[Triggered on the falling edge of TIP]]></description>
<addressOffset>0x0020</addressOffset>
<addressOffset>0x0024</addressOffset>
<resetValue>0x00</resetValue>
<size>32</size>
<fields>
@ -1770,7 +1786,7 @@ clear or else it will re-trigger.]]></description>
</registers>
<addressBlock>
<offset>0</offset>
<size>0x24</size>
<size>0x28</size>
<usage>registers</usage>
</addressBlock>
<interrupt>
@ -3101,7 +3117,7 @@ inject too fast.]]></description>
<name>SEED1</name>
<description><![CDATA[Bits 32-63 of `SEED_SEED`. Seed used for the build]]></description>
<addressOffset>0x0000</addressOffset>
<resetValue>0x6ec50873</resetValue>
<resetValue>0x5f5e319d</resetValue>
<size>32</size>
<fields>
<field>
@ -3116,7 +3132,7 @@ inject too fast.]]></description>
<name>SEED0</name>
<description><![CDATA[Bits 0-31 of `SEED_SEED`.]]></description>
<addressOffset>0x0004</addressOffset>
<resetValue>0x7ff788bf</resetValue>
<resetValue>0x1a7dec69</resetValue>
<size>32</size>
<fields>
<field>