Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions framework_lib/src/chromium_ec/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ pub enum EcCommands {
FlashProtect = 0x15,
PwmGetKeyboardBacklight = 0x0022,
PwmSetKeyboardBacklight = 0x0023,
PwmSetFanDuty = 0x0024,
PwmSetDuty = 0x0025,
PwmGetDuty = 0x0026,
GpioGet = 0x93,
I2cPassthrough = 0x9e,
ConsoleSnapshot = 0x97,
Expand Down
45 changes: 45 additions & 0 deletions framework_lib/src/chromium_ec/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,12 +174,57 @@ pub struct EcResponsePwmGetKeyboardBacklight {
pub enabled: u8,
}

#[repr(u8)]
pub enum PwmType {
Generic = 0,
KbLight,
DisplayLight,
}

impl EcRequest<EcResponsePwmGetKeyboardBacklight> for EcRequestPwmGetKeyboardBacklight {
fn command_id() -> EcCommands {
EcCommands::PwmGetKeyboardBacklight
}
}

pub const PWM_MAX_DUTY: u16 = 0xFFFF;

#[repr(C, packed)]
pub struct EcRequestPwmSetDuty {
/// Duty cycle, min 0, max 0xFFFF
pub duty: u16,
/// See enum PwmType
pub pwm_type: u8,
/// Type-specific index, or 0 if unique
pub index: u8,
}

impl EcRequest<()> for EcRequestPwmSetDuty {
fn command_id() -> EcCommands {
EcCommands::PwmSetDuty
}
}

#[repr(C, packed)]
pub struct EcRequestPwmGetDuty {
/// See enum PwmType
pub pwm_type: u8,
/// Type-specific index, or 0 if unique
pub index: u8,
}

#[repr(C, packed)]
pub struct EcResponsePwmGetDuty {
/// Duty cycle, min 0, max 0xFFFF
pub duty: u16,
}

impl EcRequest<EcResponsePwmGetDuty> for EcRequestPwmGetDuty {
fn command_id() -> EcCommands {
EcCommands::PwmGetDuty
}
}

#[repr(C, packed)]
pub struct EcRequestGpioGetV0 {
pub name: [u8; 32],
Expand Down
19 changes: 11 additions & 8 deletions framework_lib/src/chromium_ec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -371,22 +371,25 @@ impl CrosEc {
/// * `percent` - An integer from 0 to 100. 0 being off, 100 being full brightness
pub fn set_keyboard_backlight(&self, percent: u8) {
debug_assert!(percent <= 100);
let res = EcRequestPwmSetKeyboardBacklight { percent }.send_command(self);
let res = EcRequestPwmSetDuty {
duty: percent as u16 * (PWM_MAX_DUTY / 100),
pwm_type: PwmType::KbLight as u8,
index: 0,
}
.send_command(self);
debug_assert!(res.is_ok());
}

/// Check the current brightness of the keyboard backlight
///
pub fn get_keyboard_backlight(&self) -> EcResult<u8> {
let kblight = EcRequestPwmGetKeyboardBacklight {}.send_command(self)?;

// The enabled field is deprecated and must always be 1
debug_assert_eq!(kblight.enabled, 1);
if !kblight.enabled == 0 {
error!("Should always be enabled, even if OFF");
let kblight = EcRequestPwmGetDuty {
pwm_type: PwmType::KbLight as u8,
index: 0,
}
.send_command(self)?;

Ok(kblight.percent)
Ok((kblight.duty / (PWM_MAX_DUTY / 100)) as u8)
}

/// Overwrite RO and RW regions of EC flash
Expand Down