Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Opentitantool] Add a ftdi transport to the opentitanlib. #25645

Merged
merged 4 commits into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions sw/host/opentitanlib/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ rust_library(
"src/app/mod.rs",
"src/app/spi.rs",
"src/backend/chip_whisperer.rs",
"src/backend/ftdi.rs",
"src/backend/hyperdebug.rs",
"src/backend/mod.rs",
"src/backend/proxy.rs",
Expand Down Expand Up @@ -182,6 +183,10 @@ rust_library(
"src/transport/dediprog/mod.rs",
"src/transport/dediprog/spi.rs",
"src/transport/errors.rs",
"src/transport/ftdi/chip.rs",
"src/transport/ftdi/gpio.rs",
"src/transport/ftdi/mod.rs",
"src/transport/ftdi/spi.rs",
"src/transport/hyperdebug/c2d2.rs",
"src/transport/hyperdebug/dfu.rs",
"src/transport/hyperdebug/gpio.rs",
Expand Down Expand Up @@ -300,9 +305,12 @@ rust_library(
"@crate_index//:deser-hjson",
"@crate_index//:directories",
"@crate_index//:ecdsa",
"@crate_index//:embedded-hal",
"@crate_index//:env_logger",
"@crate_index//:erased-serde",
"@crate_index//:ftdi",
"@crate_index//:ftdi-embedded-hal",
"@crate_index//:ftdi-mpsse",
"@crate_index//:hex",
"@crate_index//:humantime",
"@crate_index//:humantime-serde",
Expand Down
1 change: 1 addition & 0 deletions sw/host/opentitanlib/src/app/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ static BUILTINS: Lazy<HashMap<&'static str, &'static str>> = Lazy::new(|| {
"/__builtin__/ti50emulator.json" => include_str!("ti50emulator.json"),
"/__builtin__/opentitan_cw310.json" => include_str!("opentitan_cw310.json"),
"/__builtin__/opentitan_cw340.json" => include_str!("opentitan_cw340.json"),
"/__builtin__/opentitan_ftdi_voyager.json" => include_str!("opentitan_ftdi_voyager.json"),
"/__builtin__/opentitan.json" => include_str!("opentitan.json"),
"/__builtin__/hyperdebug.json" => include_str!("hyperdebug.json"),
"/__builtin__/hyperdebug_chipwhisperer.json" => include_str!("hyperdebug_chipwhisperer.json"),
Expand Down
87 changes: 87 additions & 0 deletions sw/host/opentitanlib/src/app/config/opentitan_ftdi_voyager.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
{
"includes": ["/__builtin__/opentitan.json"],
"interface": "ftdi",
"pins": [
{
"name": "RESET",
"mode": "PushPull",
"invert": false,
"alias_of": "adbus5",
"pull_mode": "None"
},
{
"name": "TRST",
"mode": "PushPull",
"level": false,
"alias_of": "adbus4",
"pull_mode": "None"
},
{
"name": "IOC0",
"alias_of": "bdbus4"
},
{
"name": "IOC1",
"alias_of": "bdbus5"
},
{
"name": "IOC2",
"alias_of": "bdbus6"
},
{
"name": "MUX_FTDI",
"mode": "PushPull",
"alias_of": "bdbus7"
},
{
"name": "IOC8",
"alias_of": "adbus6"
},
{
"name": "IOC5",
"alias_of": "adbus7"
},
{
"name": "SPI_DEV_CSB",
"alias_of": "bdbus3"
}
],
"spi": [
{
"name": "BOOTSTRAP"
}
],
"uarts": [
{
"name": "console",
"alias_of": "0"
},
{
"name": "dut",
"alias_of": "3"
}
],
"strappings": [
{
"name": "ROM_BOOTSTRAP",
"pins": [
{
"name": "SW_STRAP0",
"level": true
},
{
"name": "SW_STRAP1",
"level": true
},
{
"name": "SW_STRAP2",
"level": true
},
{
"name": "MUX_FTDI",
"level": true
}
]
}
]
}
14 changes: 14 additions & 0 deletions sw/host/opentitanlib/src/backend/ftdi.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright lowRISC contributors (OpenTitan project).
// Licensed under the Apache License, Version 2.0, see LICENSE for details.
// SPDX-License-Identifier: Apache-2.0

use anyhow::Result;

use crate::backend::BackendOpts;
use crate::transport::ftdi::chip::Chip;
use crate::transport::ftdi::Ftdi;
use crate::transport::Transport;

pub fn create<C: Chip + 'static>(_args: &BackendOpts) -> Result<Box<dyn Transport>> {
Ok(Box::new(Ftdi::<C>::new()?))
}
6 changes: 6 additions & 0 deletions sw/host/opentitanlib/src/backend/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ use crate::app::config::process_config_file;
use crate::app::{TransportWrapper, TransportWrapperBuilder};
use crate::transport::chip_whisperer::board::{Cw310, Cw340};
use crate::transport::dediprog::Dediprog;
use crate::transport::ftdi::chip::Ft4232hq;
use crate::transport::hyperdebug::{
C2d2Flavor, ChipWhispererFlavor, ServoMicroFlavor, StandardFlavor, Ti50Flavor,
};
use crate::transport::{EmptyTransport, Transport};
use crate::util::parse_int::ParseInt;

mod chip_whisperer;
mod ftdi;
mod hyperdebug;
mod proxy;
mod ti50emulator;
Expand Down Expand Up @@ -131,6 +133,10 @@ pub fn create(args: &BackendOpts) -> Result<TransportWrapper> {
chip_whisperer::create::<Cw340>(args)?,
Some(Path::new("/__builtin__/opentitan_cw340.json")),
),
"ftdi" => (
ftdi::create::<Ft4232hq>(args)?,
Some(Path::new("/__builtin__/opentitan_ftdi_voyager.json")),
),
"dediprog" => {
let dediprog: Box<dyn Transport> = Box::new(Dediprog::new(
args.usb_vid,
Expand Down
24 changes: 24 additions & 0 deletions sw/host/opentitanlib/src/transport/ftdi/chip.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright lowRISC contributors (OpenTitan project).
// Licensed under the Apache License, Version 2.0, see LICENSE for details.
// SPDX-License-Identifier: Apache-2.0

pub trait Chip {
const VENDOR_ID: u16 = 0x0403;
const PRODUCT_ID: u16;

const UART_BAUD: u32 = 115200;

const INTERFACES: &'static [ftdi::Interface];
}

pub struct Ft4232hq {}

impl Chip for Ft4232hq {
const PRODUCT_ID: u16 = 0x6011;
const INTERFACES: &'static [ftdi::Interface] = &[
ftdi::Interface::A,
ftdi::Interface::B,
// ftdi::Interface::C,
ftdi::Interface::D,
];
}
140 changes: 140 additions & 0 deletions sw/host/opentitanlib/src/transport/ftdi/gpio.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
// Copyright lowRISC contributors (OpenTitan project).
// Licensed under the Apache License, Version 2.0, see LICENSE for details.
// SPDX-License-Identifier: Apache-2.0

use anyhow::Result;
use std::cell::RefCell;
use std::collections::HashMap;
use std::rc::Rc;

use embedded_hal::digital::{InputPin, OutputPin};
use ftdi_embedded_hal as ftdi_hal;

use crate::io::gpio::{GpioError, GpioPin, PinMode, PullMode};
use crate::transport::ftdi::Chip;

#[derive(Default)]
enum PinType {
Output(ftdi_hal::OutputPin<ftdi::Device>),
Input(ftdi_hal::InputPin<ftdi::Device>),
#[default]
None,
}

pub struct Pin {
pin: RefCell<PinType>,
ftdi: Rc<HashMap<ftdi::Interface, ftdi_hal::FtHal<ftdi::Device>>>,
pinname: String,
}

impl Pin {
pub fn open<C: Chip>(
ftdi: &Rc<HashMap<ftdi::Interface, ftdi_hal::FtHal<ftdi::Device>>>,
pinname: String,
) -> Result<Self> {
Ok(Self {
pin: RefCell::new(PinType::None),
ftdi: Rc::clone(ftdi),
pinname,
})
}

fn map_outpin(&self) -> Result<ftdi_hal::OutputPin<ftdi::Device>> {
let pinname = self.pinname.to_lowercase();
let interface = match &pinname[0..1] {
"a" => ftdi::Interface::A,
"b" => ftdi::Interface::B,
"c" => ftdi::Interface::C,
"d" => ftdi::Interface::D,
&_ => return Err(GpioError::InvalidPinName(pinname).into()),
};
let hal = self
.ftdi
.get(&interface)
.ok_or_else(|| GpioError::InvalidPinName(pinname.clone()))?;

let pin = match &pinname[1..] {
"dbus0" => hal.ad0(),
"dbus1" => hal.ad1(),
"dbus2" => hal.ad2(),
"dbus3" => hal.ad3(),
"dbus4" => hal.ad4(),
"dbus5" => hal.ad5(),
"dbus6" => hal.ad6(),
"dbus7" => hal.ad7(),
_ => return Err(GpioError::InvalidPinName(self.pinname.to_owned()).into()),
}?;
Ok(pin)
}

fn map_inpin(&self) -> Result<ftdi_hal::InputPin<ftdi::Device>> {
let pinname = self.pinname.to_lowercase();
let interface = match &pinname[0..1] {
"a" => ftdi::Interface::A,
"b" => ftdi::Interface::B,
"c" => ftdi::Interface::C,
"d" => ftdi::Interface::D,
&_ => return Err(GpioError::InvalidPinName(pinname).into()),
};
let hal = self
.ftdi
.get(&interface)
.ok_or_else(|| GpioError::InvalidPinName(pinname.clone()))?;

let pin = match &pinname[1..] {
"dbus0" => hal.adi0(),
"dbus1" => hal.adi1(),
"dbus2" => hal.adi2(),
"dbus3" => hal.adi3(),
"dbus4" => hal.adi4(),
"dbus5" => hal.adi5(),
"dbus6" => hal.adi6(),
"dbus7" => hal.adi7(),
_ => return Err(GpioError::InvalidPinName(self.pinname.to_owned()).into()),
}?;
Ok(pin)
}
}

impl GpioPin for Pin {
fn read(&self) -> Result<bool> {
let mut pin = self.pin.borrow_mut();
match *pin {
PinType::Input(ref mut pin) => Ok(pin.is_high()?),
_ => Err(GpioError::InvalidPinMode(0).into()),
}
}

fn write(&self, value: bool) -> Result<()> {
let mut pin = self.pin.borrow_mut();
if let PinType::Output(ref mut pin) = *pin {
if value {
pin.set_high()?;
} else {
pin.set_low()?;
}
return Ok(());
};
Err(GpioError::InvalidPinMode(0).into())
}

fn set_mode(&self, mode: PinMode) -> Result<()> {
let mut pin = self.pin.borrow_mut();
*pin = match (&*pin, mode) {
(PinType::None, PinMode::Input) => PinType::Input(self.map_inpin()?),
(PinType::None, PinMode::PushPull | PinMode::OpenDrain) => {
PinType::Output(self.map_outpin()?)
}
_ => return Ok(()),
};
Ok(())
}

fn set_pull_mode(&self, _mode: PullMode) -> Result<()> {
Ok(())
}

fn get_internal_pin_name(&self) -> Option<&str> {
Some(&self.pinname)
}
}
Loading
Loading