Skip to content

Commit fb43228

Browse files
Merge pull request #66 from FrameworkComputer/get-gpio
framework_lib: Implement getting GPIO value
2 parents 96a7608 + edf5b39 commit fb43228

File tree

6 files changed

+53
-0
lines changed

6 files changed

+53
-0
lines changed

framework_lib/src/chromium_ec/command.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ pub enum EcCommands {
3030
FlashProtect = 0x15,
3131
PwmGetKeyboardBacklight = 0x0022,
3232
PwmSetKeyboardBacklight = 0x0023,
33+
GpioGet = 0x93,
3334
I2cPassthrough = 0x9e,
3435
ConsoleSnapshot = 0x97,
3536
ConsoleRead = 0x98,

framework_lib/src/chromium_ec/commands.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,25 @@ impl EcRequest<EcResponsePwmGetKeyboardBacklight> for EcRequestPwmGetKeyboardBac
180180
}
181181
}
182182

183+
#[repr(C, packed)]
184+
pub struct EcRequestGpioGetV0 {
185+
pub name: [u8; 32],
186+
}
187+
188+
#[repr(C, packed)]
189+
pub struct EcResponseGpioGetV0 {
190+
pub val: u8,
191+
}
192+
193+
impl EcRequest<EcResponseGpioGetV0> for EcRequestGpioGetV0 {
194+
fn command_id() -> EcCommands {
195+
EcCommands::GpioGet
196+
}
197+
fn command_version() -> u8 {
198+
0
199+
}
200+
}
201+
183202
#[repr(C, packed)]
184203
pub struct EcRequestReboot {}
185204

framework_lib/src/chromium_ec/mod.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -854,6 +854,17 @@ impl CrosEc {
854854
}
855855
.send_command(self)
856856
}
857+
858+
pub fn get_gpio(&self, name: &str) -> EcResult<bool> {
859+
const MAX_LEN: usize = 32;
860+
let mut request = EcRequestGpioGetV0 { name: [0; MAX_LEN] };
861+
862+
let end = MAX_LEN.min(name.len());
863+
request.name[..end].copy_from_slice(name[..end].as_bytes());
864+
865+
let res = request.send_command(self)?;
866+
Ok(res.val == 1)
867+
}
857868
}
858869

859870
#[cfg_attr(not(feature = "uefi"), derive(clap::ValueEnum))]

framework_lib/src/commandline/clap_std.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,10 @@ struct ClapCli {
128128
#[arg(long)]
129129
charge_limit: Option<Option<u8>>,
130130

131+
/// Get GPIO value by name
132+
#[arg(long)]
133+
get_gpio: Option<String>,
134+
131135
/// Get or set fingerprint LED brightness
132136
#[arg(long)]
133137
fp_brightness: Option<Option<FpBrightnessArg>>,
@@ -251,6 +255,7 @@ pub fn parse(args: &[String]) -> Cli {
251255
inputmodules: args.inputmodules,
252256
input_deck_mode: args.input_deck_mode,
253257
charge_limit: args.charge_limit,
258+
get_gpio: args.get_gpio,
254259
fp_brightness: args.fp_brightness,
255260
kblight: args.kblight,
256261
console: args.console,

framework_lib/src/commandline/mod.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ pub struct Cli {
148148
pub inputmodules: bool,
149149
pub input_deck_mode: Option<InputDeckModeArg>,
150150
pub charge_limit: Option<Option<u8>>,
151+
pub get_gpio: Option<String>,
151152
pub fp_brightness: Option<Option<FpBrightnessArg>>,
152153
pub kblight: Option<Option<u8>>,
153154
pub console: Option<ConsoleArg>,
@@ -720,6 +721,13 @@ pub fn run_with_args(args: &Cli, _allupdate: bool) -> i32 {
720721
ec.set_input_deck_mode((*mode).into()).unwrap();
721722
} else if let Some(maybe_limit) = args.charge_limit {
722723
print_err(handle_charge_limit(&ec, maybe_limit));
724+
} else if let Some(gpio_name) = &args.get_gpio {
725+
print!("Getting GPIO value {}: ", gpio_name);
726+
if let Ok(value) = ec.get_gpio(gpio_name) {
727+
println!("{:?}", value);
728+
} else {
729+
println!("Not found");
730+
}
723731
} else if let Some(maybe_brightness) = &args.fp_brightness {
724732
print_err(handle_fp_brightness(&ec, *maybe_brightness));
725733
} else if let Some(Some(kblight)) = args.kblight {
@@ -979,6 +987,7 @@ Options:
979987
--inputmodules Show status of the input modules (Framework 16 only)
980988
--input-deck-mode Set input deck power mode [possible values: auto, off, on] (Framework 16 only)
981989
--charge-limit [<VAL>] Get or set battery charge limit (Percentage number as arg, e.g. '100')
990+
--get-gpio <GET_GPIO> Get GPIO value by name
982991
--fp-brightness [<VAL>]Get or set fingerprint LED brightness level [possible values: high, medium, low]
983992
--kblight [<KBLIGHT>] Set keyboard backlight percentage or get, if no value provided
984993
--console <CONSOLE> Get EC console, choose whether recent or to follow the output [possible values: recent, follow]

framework_lib/src/commandline/uefi.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ pub fn parse(args: &[String]) -> Cli {
8282
inputmodules: false,
8383
input_deck_mode: None,
8484
charge_limit: None,
85+
get_gpio: None,
8586
fp_brightness: None,
8687
kblight: None,
8788
console: None,
@@ -187,6 +188,13 @@ pub fn parse(args: &[String]) -> Cli {
187188
Some(None)
188189
};
189190
found_an_option = true;
191+
} else if arg == "--get-gpio" {
192+
cli.get_gpio = if args.len() > i + 1 {
193+
Some(args[i + 1].clone())
194+
} else {
195+
None
196+
};
197+
found_an_option = true;
190198
} else if arg == "--kblight" {
191199
cli.kblight = if args.len() > i + 1 {
192200
if let Ok(percent) = args[i + 1].parse::<u8>() {

0 commit comments

Comments
 (0)