Skip to content

Commit f6b1353

Browse files
committed
ccgx: Add function to write to ccgx
Pass writes through via I2C passthrough on the EC Signed-off-by: Daniel Schaefer <dhs@frame.work>
1 parent b1169be commit f6b1353

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

framework_lib/src/ccgx/device.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,21 @@ impl PdController {
182182
)
183183
}
184184

185+
pub fn i2c_write(&self, addr: u16, data: &[u8]) -> EcResult<EcI2cPassthruResponse> {
186+
trace!(
187+
"I2C passthrough from I2C Port {} to I2C Addr {}",
188+
self.port.i2c_port()?,
189+
self.port.i2c_address()?
190+
);
191+
i2c_write(
192+
&self.ec,
193+
self.port.i2c_port()?,
194+
self.port.i2c_address()?,
195+
addr,
196+
data,
197+
)
198+
}
199+
185200
fn ccgx_read(&self, reg: ControlRegisters, len: u16) -> EcResult<Vec<u8>> {
186201
let mut data: Vec<u8> = Vec::with_capacity(len.into());
187202

@@ -204,6 +219,35 @@ impl PdController {
204219
Ok(data)
205220
}
206221

222+
fn ccgx_write(&self, reg: ControlRegisters, data: &[u8]) -> EcResult<()> {
223+
let addr = reg as u16;
224+
trace!(
225+
"ccgx_write(reg: {:?}, addr: {}, data.len(): {}",
226+
reg,
227+
addr,
228+
data.len()
229+
);
230+
let mut data_written = 0;
231+
232+
while data_written < data.len() {
233+
let chunk_len = std::cmp::min(MAX_I2C_CHUNK, data.len());
234+
let buffer = &data[data_written..data_written + chunk_len];
235+
let offset = addr + data_written as u16;
236+
237+
let i2c_response = self.i2c_write(offset, buffer)?;
238+
if let Err(EcError::DeviceError(err)) = i2c_response.is_successful() {
239+
return Err(EcError::DeviceError(format!(
240+
"I2C write was not successful: {:?}",
241+
err
242+
)));
243+
}
244+
245+
data_written += chunk_len;
246+
}
247+
248+
Ok(())
249+
}
250+
207251
pub fn get_silicon_id(&self) -> EcResult<u16> {
208252
let data = self.ccgx_read(ControlRegisters::SiliconId, 2)?;
209253
assert_win_len(data.len(), 2);

framework_lib/src/chromium_ec/i2c_passthrough.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ pub fn i2c_write(
155155

156156
let data = ec.send_command(EcCommands::I2cPassthrough as u16, 0, &buffer)?;
157157
let res: _EcI2cPassthruResponse = unsafe { std::ptr::read(data.as_ptr() as *const _) };
158-
assert_eq!(data.len(), size_of::<_EcI2cPassthruResponse>()); // No extra data other than the header
158+
util::assert_win_len(data.len(), size_of::<_EcI2cPassthruResponse>()); // No extra data other than the header
159159
debug_assert_eq!(res.messages as usize, messages.len());
160160
Ok(EcI2cPassthruResponse {
161161
i2c_status: res.i2c_status,

0 commit comments

Comments
 (0)