Skip to content

Commit 55c2c9e

Browse files
committed
Add I2C support.
OS can now talk to I2C devices.
1 parent b036b9a commit 55c2c9e

File tree

1 file changed

+42
-7
lines changed

1 file changed

+42
-7
lines changed

src/main.rs

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2086,18 +2086,53 @@ unsafe extern "C" fn video_set_whole_palette(
20862086
}
20872087
}
20882088

2089-
extern "C" fn i2c_bus_get_info(_i2c_bus: u8) -> FfiOption<common::i2c::BusInfo> {
2090-
FfiOption::None
2089+
extern "C" fn i2c_bus_get_info(i2c_bus: u8) -> FfiOption<common::i2c::BusInfo> {
2090+
match i2c_bus {
2091+
0 => FfiOption::Some(common::i2c::BusInfo {
2092+
name: "RP2040".into(),
2093+
}),
2094+
1 => FfiOption::Some(common::i2c::BusInfo {
2095+
name: "Pico-BMC".into(),
2096+
}),
2097+
_ => FfiOption::None,
2098+
}
20912099
}
20922100

20932101
extern "C" fn i2c_write_read(
2094-
_i2c_bus: u8,
2095-
_i2c_device_address: u8,
2096-
_tx: FfiByteSlice,
2097-
_tx2: FfiByteSlice,
2098-
_rx: FfiBuffer,
2102+
i2c_bus: u8,
2103+
i2c_device_address: u8,
2104+
tx: FfiByteSlice,
2105+
tx2: FfiByteSlice,
2106+
mut rx: FfiBuffer,
20992107
) -> ApiResult<()> {
2108+
let mut lock = HARDWARE.lock();
2109+
let hw = lock.as_mut().unwrap();
2110+
match i2c_bus {
2111+
0 => {
2112+
// Use RP2040 bus
2113+
let mut i2c = hw.i2c.acquire_i2c();
2114+
let tx_bytes = tx.as_slice();
2115+
let tx2_bytes = tx2.as_slice();
2116+
let tx_iter = tx_bytes.iter().chain(tx2_bytes).cloned();
2117+
let r = if let Some(rx_buf) = rx.as_mut_slice() {
2118+
i2c.write_iter_read(i2c_device_address, tx_iter, rx_buf)
2119+
} else {
2120+
i2c.write(i2c_device_address, tx_iter)
2121+
};
2122+
match r {
2123+
Ok(()) => ApiResult::Ok(()),
2124+
Err(e) => {
2125+
defmt::warn!("Error executing I2C: {:?}", e);
2126+
ApiResult::Err(CError::DeviceError(0))
2127+
}
2128+
}
2129+
}
2130+
1 => {
2131+
// TODO talk to Pico-BMC over SPI
21002132
ApiResult::Err(CError::Unimplemented)
2133+
}
2134+
_ => ApiResult::Err(CError::Unimplemented),
2135+
}
21012136
}
21022137

21032138
extern "C" fn audio_mixer_channel_get_info(

0 commit comments

Comments
 (0)