opentitanlib/transport/chip_whisperer/
gpio.rs

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
5use super::board::Board;
6use anyhow::Result;
7use std::cell::RefCell;
8use std::rc::Rc;
9
10use crate::io::gpio::{GpioError, GpioPin, PinMode, PullMode};
11use crate::transport::chip_whisperer::usb::Backend;
12
13pub struct Pin<B: Board> {
14    device: Rc<RefCell<Backend<B>>>,
15    pinname: String,
16}
17
18impl<B: Board> Pin<B> {
19    pub fn open(backend: Rc<RefCell<Backend<B>>>, pinname: String) -> Result<Self> {
20        Ok(Self {
21            device: backend,
22            pinname,
23        })
24    }
25}
26
27impl<B: Board> GpioPin for Pin<B> {
28    fn read(&self) -> Result<bool> {
29        let usb = self.device.borrow();
30        let pin = usb.pin_get_state(&self.pinname)?;
31        Ok(pin != 0)
32    }
33
34    fn write(&self, value: bool) -> Result<()> {
35        let usb = self.device.borrow();
36        usb.pin_set_state(&self.pinname, value)?;
37        Ok(())
38    }
39
40    fn set_mode(&self, mode: PinMode) -> Result<()> {
41        let usb = self.device.borrow();
42        match mode {
43            PinMode::Input => usb.pin_set_output(&self.pinname, false)?,
44            PinMode::PushPull => usb.pin_set_output(&self.pinname, true)?,
45            _ => return Err(GpioError::UnsupportedPinMode(mode).into()),
46        }
47        Ok(())
48    }
49
50    fn set_pull_mode(&self, mode: PullMode) -> Result<()> {
51        match mode {
52            PullMode::None => Ok(()),
53            _ => Err(GpioError::UnsupportedPullMode(mode).into()),
54        }
55    }
56
57    fn get_internal_pin_name(&self) -> Option<&str> {
58        Some(&self.pinname)
59    }
60}