Skip to content

Commit

Permalink
Merge pull request #66 from FrameworkComputer/get-gpio
Browse files Browse the repository at this point in the history
framework_lib: Implement getting GPIO value
  • Loading branch information
JohnAZoidberg authored Dec 11, 2024
2 parents 96a7608 + edf5b39 commit fb43228
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 0 deletions.
1 change: 1 addition & 0 deletions framework_lib/src/chromium_ec/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ pub enum EcCommands {
FlashProtect = 0x15,
PwmGetKeyboardBacklight = 0x0022,
PwmSetKeyboardBacklight = 0x0023,
GpioGet = 0x93,
I2cPassthrough = 0x9e,
ConsoleSnapshot = 0x97,
ConsoleRead = 0x98,
Expand Down
19 changes: 19 additions & 0 deletions framework_lib/src/chromium_ec/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,25 @@ impl EcRequest<EcResponsePwmGetKeyboardBacklight> for EcRequestPwmGetKeyboardBac
}
}

#[repr(C, packed)]
pub struct EcRequestGpioGetV0 {
pub name: [u8; 32],
}

#[repr(C, packed)]
pub struct EcResponseGpioGetV0 {
pub val: u8,
}

impl EcRequest<EcResponseGpioGetV0> for EcRequestGpioGetV0 {
fn command_id() -> EcCommands {
EcCommands::GpioGet
}
fn command_version() -> u8 {
0
}
}

#[repr(C, packed)]
pub struct EcRequestReboot {}

Expand Down
11 changes: 11 additions & 0 deletions framework_lib/src/chromium_ec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -854,6 +854,17 @@ impl CrosEc {
}
.send_command(self)
}

pub fn get_gpio(&self, name: &str) -> EcResult<bool> {
const MAX_LEN: usize = 32;
let mut request = EcRequestGpioGetV0 { name: [0; MAX_LEN] };

let end = MAX_LEN.min(name.len());
request.name[..end].copy_from_slice(name[..end].as_bytes());

let res = request.send_command(self)?;
Ok(res.val == 1)
}
}

#[cfg_attr(not(feature = "uefi"), derive(clap::ValueEnum))]
Expand Down
5 changes: 5 additions & 0 deletions framework_lib/src/commandline/clap_std.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ struct ClapCli {
#[arg(long)]
charge_limit: Option<Option<u8>>,

/// Get GPIO value by name
#[arg(long)]
get_gpio: Option<String>,

/// Get or set fingerprint LED brightness
#[arg(long)]
fp_brightness: Option<Option<FpBrightnessArg>>,
Expand Down Expand Up @@ -251,6 +255,7 @@ pub fn parse(args: &[String]) -> Cli {
inputmodules: args.inputmodules,
input_deck_mode: args.input_deck_mode,
charge_limit: args.charge_limit,
get_gpio: args.get_gpio,
fp_brightness: args.fp_brightness,
kblight: args.kblight,
console: args.console,
Expand Down
9 changes: 9 additions & 0 deletions framework_lib/src/commandline/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ pub struct Cli {
pub inputmodules: bool,
pub input_deck_mode: Option<InputDeckModeArg>,
pub charge_limit: Option<Option<u8>>,
pub get_gpio: Option<String>,
pub fp_brightness: Option<Option<FpBrightnessArg>>,
pub kblight: Option<Option<u8>>,
pub console: Option<ConsoleArg>,
Expand Down Expand Up @@ -720,6 +721,13 @@ pub fn run_with_args(args: &Cli, _allupdate: bool) -> i32 {
ec.set_input_deck_mode((*mode).into()).unwrap();
} else if let Some(maybe_limit) = args.charge_limit {
print_err(handle_charge_limit(&ec, maybe_limit));
} else if let Some(gpio_name) = &args.get_gpio {
print!("Getting GPIO value {}: ", gpio_name);
if let Ok(value) = ec.get_gpio(gpio_name) {
println!("{:?}", value);
} else {
println!("Not found");
}
} else if let Some(maybe_brightness) = &args.fp_brightness {
print_err(handle_fp_brightness(&ec, *maybe_brightness));
} else if let Some(Some(kblight)) = args.kblight {
Expand Down Expand Up @@ -979,6 +987,7 @@ Options:
--inputmodules Show status of the input modules (Framework 16 only)
--input-deck-mode Set input deck power mode [possible values: auto, off, on] (Framework 16 only)
--charge-limit [<VAL>] Get or set battery charge limit (Percentage number as arg, e.g. '100')
--get-gpio <GET_GPIO> Get GPIO value by name
--fp-brightness [<VAL>]Get or set fingerprint LED brightness level [possible values: high, medium, low]
--kblight [<KBLIGHT>] Set keyboard backlight percentage or get, if no value provided
--console <CONSOLE> Get EC console, choose whether recent or to follow the output [possible values: recent, follow]
Expand Down
8 changes: 8 additions & 0 deletions framework_lib/src/commandline/uefi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ pub fn parse(args: &[String]) -> Cli {
inputmodules: false,
input_deck_mode: None,
charge_limit: None,
get_gpio: None,
fp_brightness: None,
kblight: None,
console: None,
Expand Down Expand Up @@ -187,6 +188,13 @@ pub fn parse(args: &[String]) -> Cli {
Some(None)
};
found_an_option = true;
} else if arg == "--get-gpio" {
cli.get_gpio = if args.len() > i + 1 {
Some(args[i + 1].clone())
} else {
None
};
found_an_option = true;
} else if arg == "--kblight" {
cli.kblight = if args.len() > i + 1 {
if let Ok(percent) = args[i + 1].parse::<u8>() {
Expand Down

0 comments on commit fb43228

Please sign in to comment.