expose pca954x_select api #167

Merged
sb10q merged 4 commits from mwojcik/artiq-zynq:pca954x_select_api into master 2022-03-02 10:52:27 +08:00
4 changed files with 35 additions and 0 deletions
Showing only changes of commit f60893c656 - Show all commits

View File

@ -47,6 +47,7 @@ pub enum Packet {
I2cReadRequest { destination: u8, busno: u8, ack: bool },
I2cReadReply { succeeded: bool, data: u8 },
I2cBasicReply { succeeded: bool },
I2cPca954xSelectRequest { destination: u8, busno: u8, address: u8, channel: u8, clear: bool },
SpiSetConfigRequest { destination: u8, busno: u8, flags: u8, length: u8, div: u8, cs: u8 },
SpiWriteRequest { destination: u8, busno: u8, data: u32 },
@ -152,6 +153,13 @@ impl Packet {
0x87 => Packet::I2cBasicReply {
succeeded: reader.read_bool()?
},
0x88 => Packet::I2cPca954xSelectRequest {
destination: reader.read_u8()?,
busno: reader.read_u8()?,
address: reader.read_u8()?,
channel: reader.read_u8()?,
clear: reader.read_bool()?
},
0x90 => Packet::SpiSetConfigRequest {
destination: reader.read_u8()?,
@ -301,6 +309,14 @@ impl Packet {
writer.write_u8(0x87)?;
writer.write_bool(succeeded)?;
},
Packet::I2cPca954xSelectRequest { destination, busno, address, channel, clear } => {
writer.write_u8(0x88)?;
writer.write_u8(destination)?;
writer.write_u8(busno)?;
writer.write_u8(address)?;
writer.write_u8(channel)?;
writer.write_bool(clear)?;
},
Packet::SpiSetConfigRequest { destination, busno, flags, length, div, cs } => {
writer.write_u8(0x90)?;

View File

@ -60,6 +60,18 @@ pub extern fn read(busno: i32, ack: bool) -> i32 {
}
}
pub extern fn pca954x_select(busno: i32, address: i32, channel: i32, clear: bool) {
if busno > 0 {
artiq_raise!("I2CError", "I2C bus could not be accessed");
}
unsafe {
Outdated
Review

At least the match statement does not need to be in unsafe.

At least the match statement does not need to be in unsafe.
let ch = if clear { None } else { Some(channel as u8) };
if (&mut I2C_BUS).as_mut().unwrap().pca954x_select(address as u8, ch).is_err() {
artiq_raise!("I2CError", "PCA954X select failed");
}
}
}
pub fn init() {
let mut i2c = libboard_zynq::i2c::I2c::i2c0();
i2c.init().expect("I2C bus initialization failed");

View File

@ -116,6 +116,7 @@ pub fn resolve(required: &[u8]) -> Option<u32> {
api!(i2c_stop = i2c::stop),
api!(i2c_write = i2c::write),
api!(i2c_read = i2c::read),
api!(i2c_pca954x_select = i2c::pca954x_select),
// Double-precision floating-point arithmetic helper functions
// RTABI chapter 4.1.2, Table 2

View File

@ -282,6 +282,12 @@ fn process_aux_packet(_repeaters: &mut [repeater::Repeater],
&drtioaux::Packet::I2cReadReply { succeeded: false, data: 0xff })
}
}
drtioaux::Packet::I2cStartRequest { destination: _destination, busno: _busno, address, channel, clear } => {
forward!(_routing_table, _destination, *_rank, _repeaters, &packet, timer);
let ch = if clear { None } else { Some(channel as u8) };
let succeeded = i2c.pca954x_select(address, channel).is_ok();
drtioaux::send(0, &drtioaux::Packet::I2cBasicReply { succeeded: succeeded })
}
drtioaux::Packet::SpiSetConfigRequest { destination: _destination, busno: _busno,
flags: _flags, length: _length, div: _div, cs: _cs } => {