-
Notifications
You must be signed in to change notification settings - Fork 792
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[opentitanlib] Add ftdi transport for voyager board
Signed-off-by: Douglas Reis <[email protected]>
- Loading branch information
Showing
8 changed files
with
254 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
sw/host/opentitanlib/src/app/config/opentitan_ftdi_voyager.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
{ | ||
"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", | ||
"alias_of": "bdbus7" | ||
}, | ||
{ | ||
"name": "IOC8", | ||
"alias_of": "adbus6" | ||
}, | ||
{ | ||
"name": "IOC5", | ||
"alias_of": "adbus7" | ||
} | ||
], | ||
"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 | ||
} | ||
] | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()?)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
// 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 serde_annotate::Annotate; | ||
use std::any::Any; | ||
use std::cell::RefCell; | ||
use std::collections::hash_map::Entry; | ||
use std::collections::HashMap; | ||
use std::rc::Rc; | ||
|
||
use crate::io::gpio::GpioPin; | ||
use crate::io::spi::Target; | ||
use crate::io::uart::Uart; | ||
use crate::io::uart::UartError; | ||
use crate::transport::common::uart::SerialPortUart; | ||
use crate::transport::{ | ||
Capabilities, Capability, Transport, TransportError, TransportInterfaceType, | ||
}; | ||
use crate::util::parse_int::ParseInt; | ||
use serialport::SerialPortType; | ||
|
||
use chip::Chip; | ||
use ftdi_embedded_hal as ftdi_hal; | ||
|
||
pub mod chip; | ||
|
||
#[derive(Default)] | ||
struct Inner { | ||
spi: Option<Rc<dyn Target>>, | ||
gpio: HashMap<String, Rc<dyn GpioPin>>, | ||
uart: HashMap<u32, Rc<dyn Uart>>, | ||
} | ||
|
||
pub struct Ftdi<C: Chip> { | ||
pub(crate) ftdi_interfaces: HashMap<ftdi::Interface, ftdi_hal::FtHal<ftdi::Device>>, | ||
inner: RefCell<Inner>, | ||
phantom: std::marker::PhantomData<C>, | ||
} | ||
|
||
impl<C: Chip> Ftdi<C> { | ||
pub fn new() -> anyhow::Result<Self> { | ||
let mut ftdi_interfaces = HashMap::new(); | ||
for interface in C::INTERFACES { | ||
let device = ftdi::find_by_vid_pid(C::VENDOR_ID, C::PRODUCT_ID) | ||
.interface(*interface) | ||
.open()?; | ||
ftdi_interfaces.insert(*interface, ftdi_hal::FtHal::init_freq(device, 8_000_000)?); | ||
} | ||
|
||
let ftdi_dev = Ftdi { | ||
ftdi_interfaces, | ||
inner: RefCell::default(), | ||
phantom: std::marker::PhantomData, | ||
}; | ||
Ok(ftdi_dev) | ||
} | ||
|
||
fn open_uart(&self, instance: u32) -> Result<SerialPortUart> { | ||
let mut ports = serialport::available_ports() | ||
.map_err(|e| UartError::EnumerationError(e.to_string()))?; | ||
|
||
ports.retain(|port| { | ||
if let SerialPortType::UsbPort(info) = &port.port_type { | ||
if info.vid == C::VENDOR_ID && info.pid == C::PRODUCT_ID { | ||
return true; | ||
} | ||
} | ||
false | ||
}); | ||
|
||
let port = ports.get(instance as usize).ok_or_else(|| { | ||
TransportError::InvalidInstance(TransportInterfaceType::Uart, instance.to_string()) | ||
})?; | ||
|
||
SerialPortUart::open(&port.port_name, C::UART_BAUD) | ||
} | ||
} | ||
|
||
impl<C: Chip> Transport for Ftdi<C> { | ||
fn capabilities(&self) -> Result<Capabilities> { | ||
Ok(Capabilities::new( | ||
Capability::SPI | Capability::GPIO | Capability::UART | Capability::UART_NONBLOCKING, | ||
)) | ||
} | ||
|
||
fn uart(&self, instance: &str) -> Result<Rc<dyn Uart>> { | ||
let mut inner = self.inner.borrow_mut(); | ||
let instance = u32::from_str(instance).ok().ok_or_else(|| { | ||
TransportError::InvalidInstance(TransportInterfaceType::Uart, instance.to_string()) | ||
})?; | ||
let uart = match inner.uart.entry(instance) { | ||
Entry::Vacant(v) => { | ||
let u = v.insert(Rc::new(self.open_uart(instance)?)); | ||
Rc::clone(u) | ||
} | ||
Entry::Occupied(o) => Rc::clone(o.get()), | ||
}; | ||
Ok(uart) | ||
} | ||
|
||
fn gpio_pin(&self, pinname: &str) -> Result<Rc<dyn GpioPin>> { | ||
Err(TransportError::UnsupportedOperation.into()) | ||
} | ||
|
||
fn spi(&self, _instance: &str) -> Result<Rc<dyn Target>> { | ||
Err(TransportError::UnsupportedOperation.into()) | ||
} | ||
|
||
fn dispatch(&self, _action: &dyn Any) -> Result<Option<Box<dyn Annotate>>> { | ||
Err(TransportError::UnsupportedOperation.into()) | ||
} | ||
} | ||
|
||
/// Command for Transport::dispatch(). | ||
pub struct SetPll {} | ||
|
||
/// Command for Transport::dispatch(). Resets the Chip whisperer board's SAM3X chip. | ||
pub struct ResetSam3x {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters