diff --git a/framework_lib/src/chromium_ec/command.rs b/framework_lib/src/chromium_ec/command.rs index 6c397870..a0205780 100644 --- a/framework_lib/src/chromium_ec/command.rs +++ b/framework_lib/src/chromium_ec/command.rs @@ -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, diff --git a/framework_lib/src/chromium_ec/commands.rs b/framework_lib/src/chromium_ec/commands.rs index 9a7f784b..82057165 100644 --- a/framework_lib/src/chromium_ec/commands.rs +++ b/framework_lib/src/chromium_ec/commands.rs @@ -174,12 +174,57 @@ pub struct EcResponsePwmGetKeyboardBacklight { pub enabled: u8, } +#[repr(u8)] +pub enum PwmType { + Generic = 0, + KbLight, + DisplayLight, +} + impl EcRequest 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 for EcRequestPwmGetDuty { + fn command_id() -> EcCommands { + EcCommands::PwmGetDuty + } +} + #[repr(C, packed)] pub struct EcRequestGpioGetV0 { pub name: [u8; 32], diff --git a/framework_lib/src/chromium_ec/mod.rs b/framework_lib/src/chromium_ec/mod.rs index 03039295..f86fa839 100644 --- a/framework_lib/src/chromium_ec/mod.rs +++ b/framework_lib/src/chromium_ec/mod.rs @@ -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 { - 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