blob: 969fdd3ae62af1e7b9d3c37079f5bb4027941096 [file]
// Copyright lowRISC contributors.
// Licensed under the Apache License, Version 2.0, see LICENSE for details.
// SPDX-License-Identifier: Apache-2.0
use anyhow::Result;
use std::rc::Rc;
use crate::io::gpio::{GpioPin, PinMode, PullMode};
use crate::transport::hyperdebug::{Error, Hyperdebug, Inner};
pub struct HyperdebugGpioPin {
inner: Rc<Inner>,
pinname: String,
}
impl HyperdebugGpioPin {
pub fn open(hyperdebug: &Hyperdebug, pinname: &str) -> Result<Self> {
let result = Self {
inner: Rc::clone(&hyperdebug.inner),
pinname: pinname.to_string(),
};
Ok(result)
}
}
impl GpioPin for HyperdebugGpioPin {
/// Reads the value of the the GPIO pin `id`.
fn read(&self) -> Result<bool> {
let mut result: Result<bool> =
Err(Error::CommunicationError("No output from gpioget").into());
self.inner
.execute_command(&format!("gpioget {}", &self.pinname), |line| {
result = Ok(line.trim_start().starts_with("1"))
})?;
result
}
/// Sets the value of the GPIO pin `id` to `value`.
fn write(&self, value: bool) -> Result<()> {
self.inner.execute_command(
&format!("gpioset {} {}", &self.pinname, if value { 1 } else { 0 }),
|_| {},
)
}
fn set_mode(&self, mode: PinMode) -> Result<()> {
self.inner.execute_command(
&format!(
"gpiomode {} {}",
&self.pinname,
match mode {
PinMode::Input => "input",
PinMode::OpenDrain => "opendrain",
PinMode::PushPull => "pushpull",
}
),
|_| {},
)
}
fn set_pull_mode(&self, mode: PullMode) -> Result<()> {
self.inner.execute_command(
&format!(
"gpiopullmode {} {}",
&self.pinname,
match mode {
PullMode::None => "none",
PullMode::PullUp => "up",
PullMode::PullDown => "down",
}
),
|_| {},
)
}
}