|
| 1 | +// Copyright lowRISC contributors (OpenTitan project). |
| 2 | +// Licensed under the Apache License, Version 2.0, see LICENSE for details. |
| 3 | +// SPDX-License-Identifier: Apache-2.0 |
| 4 | + |
| 5 | +use std::cell::{Cell, RefCell}; |
| 6 | +use std::io::{Read, Write}; |
| 7 | +use std::path::Path; |
| 8 | + |
| 9 | +use anyhow::Context; |
| 10 | +use anyhow::bail; |
| 11 | +use serialport::TTYPort; |
| 12 | + |
| 13 | +use crate::io::i2c::I2cError; |
| 14 | +use crate::io::i2c::{Bus, Transfer}; |
| 15 | +use crate::transport::TransportError; |
| 16 | + |
| 17 | +pub struct QemuI2c { |
| 18 | + pty: RefCell<TTYPort>, |
| 19 | + default_addr: Cell<Option<u8>>, |
| 20 | +} |
| 21 | + |
| 22 | +impl QemuI2c { |
| 23 | + pub fn new<P: AsRef<Path>>(pty: P) -> anyhow::Result<QemuI2c> { |
| 24 | + let pty = pty.as_ref().to_str().context("path not UTF-8")?; |
| 25 | + let pty = serialport::new(pty, 115200) |
| 26 | + .timeout(std::time::Duration::from_secs(1)) |
| 27 | + .open_native() |
| 28 | + .context("failed to open I2C PTY")?; |
| 29 | + let pty = RefCell::new(pty); |
| 30 | + |
| 31 | + Ok(QemuI2c { |
| 32 | + pty, |
| 33 | + default_addr: Cell::new(None), |
| 34 | + }) |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +fn check_response<R: Read>(pty: &mut R, s: &'static str) -> anyhow::Result<()> { |
| 39 | + let mut byte_buf = [0u8; 1]; |
| 40 | + pty.read_exact(&mut byte_buf)?; |
| 41 | + match byte_buf[0] { |
| 42 | + b'.' => Ok(()), |
| 43 | + b'!' => bail!(s), |
| 44 | + b'x' => bail!("Malformed command"), |
| 45 | + _ => unreachable!(), |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +impl Bus for QemuI2c { |
| 50 | + fn get_max_speed(&self) -> anyhow::Result<u32> { |
| 51 | + Err(TransportError::UnsupportedOperation.into()) |
| 52 | + } |
| 53 | + |
| 54 | + fn set_max_speed(&self, _max_speed: u32) -> anyhow::Result<()> { |
| 55 | + Err(TransportError::UnsupportedOperation.into()) |
| 56 | + } |
| 57 | + |
| 58 | + fn set_default_address(&self, addr: u8) -> anyhow::Result<()> { |
| 59 | + self.default_addr.set(Some(addr)); |
| 60 | + Ok(()) |
| 61 | + } |
| 62 | + |
| 63 | + fn run_transaction( |
| 64 | + &self, |
| 65 | + addr: Option<u8>, |
| 66 | + transaction: &mut [Transfer], |
| 67 | + ) -> anyhow::Result<()> { |
| 68 | + let target_addr = addr |
| 69 | + .or(self.default_addr.get()) |
| 70 | + .ok_or(I2cError::MissingAddress)?; |
| 71 | + |
| 72 | + let mut pty = self.pty.borrow_mut(); |
| 73 | + |
| 74 | + // Write and check protocol version |
| 75 | + let i2c_protocol_version = 1_u8; |
| 76 | + |
| 77 | + write!(pty, "i")?; |
| 78 | + pty.write_all(&[i2c_protocol_version])?; |
| 79 | + check_response(&mut *pty, "Protocol version mismatch")?; |
| 80 | + |
| 81 | + for transfer in transaction.iter_mut() { |
| 82 | + // Start/repeated start condition |
| 83 | + write!(pty, "S")?; |
| 84 | + |
| 85 | + match transfer { |
| 86 | + Transfer::Write(buf) => { |
| 87 | + // Write I2C address + write bit (0) |
| 88 | + let start_byte: u8 = target_addr << 1; |
| 89 | + pty.write_all(std::slice::from_ref(&start_byte))?; |
| 90 | + check_response(&mut *pty, "Write transfer NACKed")?; |
| 91 | + |
| 92 | + // Write data byte and check for no NACKs |
| 93 | + for byte in buf.iter() { |
| 94 | + write!(pty, "W")?; |
| 95 | + pty.write_all(std::slice::from_ref(byte))?; |
| 96 | + check_response(&mut *pty, "Written byte NACKed")?; |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + Transfer::Read(buf) => { |
| 101 | + // Write I2C address + read bit (1) |
| 102 | + let start_byte: u8 = (target_addr << 1) | 1; |
| 103 | + pty.write_all(std::slice::from_ref(&start_byte))?; |
| 104 | + check_response(&mut *pty, "Read transfer NACKed")?; |
| 105 | + |
| 106 | + // Issue read and read data byte |
| 107 | + for byte in buf.iter_mut() { |
| 108 | + write!(pty, "R")?; |
| 109 | + pty.read_exact(std::slice::from_mut(byte))?; |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + // Stop condition |
| 115 | + write!(pty, "P")?; |
| 116 | + |
| 117 | + Ok(()) |
| 118 | + } |
| 119 | +} |
0 commit comments