Skip to content

Commit 93c628f

Browse files
Merge pull request #72 from FrameworkComputer/pwm-commands
framwork_lib: Use EC_CMD_PWM_{S,G}ET_DUTY for kblight
2 parents 053abf3 + 7e33c8a commit 93c628f

File tree

3 files changed

+59
-8
lines changed

3 files changed

+59
-8
lines changed

framework_lib/src/chromium_ec/command.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ pub enum EcCommands {
3030
FlashProtect = 0x15,
3131
PwmGetKeyboardBacklight = 0x0022,
3232
PwmSetKeyboardBacklight = 0x0023,
33+
PwmSetFanDuty = 0x0024,
34+
PwmSetDuty = 0x0025,
35+
PwmGetDuty = 0x0026,
3336
GpioGet = 0x93,
3437
I2cPassthrough = 0x9e,
3538
ConsoleSnapshot = 0x97,

framework_lib/src/chromium_ec/commands.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,12 +174,57 @@ pub struct EcResponsePwmGetKeyboardBacklight {
174174
pub enabled: u8,
175175
}
176176

177+
#[repr(u8)]
178+
pub enum PwmType {
179+
Generic = 0,
180+
KbLight,
181+
DisplayLight,
182+
}
183+
177184
impl EcRequest<EcResponsePwmGetKeyboardBacklight> for EcRequestPwmGetKeyboardBacklight {
178185
fn command_id() -> EcCommands {
179186
EcCommands::PwmGetKeyboardBacklight
180187
}
181188
}
182189

190+
pub const PWM_MAX_DUTY: u16 = 0xFFFF;
191+
192+
#[repr(C, packed)]
193+
pub struct EcRequestPwmSetDuty {
194+
/// Duty cycle, min 0, max 0xFFFF
195+
pub duty: u16,
196+
/// See enum PwmType
197+
pub pwm_type: u8,
198+
/// Type-specific index, or 0 if unique
199+
pub index: u8,
200+
}
201+
202+
impl EcRequest<()> for EcRequestPwmSetDuty {
203+
fn command_id() -> EcCommands {
204+
EcCommands::PwmSetDuty
205+
}
206+
}
207+
208+
#[repr(C, packed)]
209+
pub struct EcRequestPwmGetDuty {
210+
/// See enum PwmType
211+
pub pwm_type: u8,
212+
/// Type-specific index, or 0 if unique
213+
pub index: u8,
214+
}
215+
216+
#[repr(C, packed)]
217+
pub struct EcResponsePwmGetDuty {
218+
/// Duty cycle, min 0, max 0xFFFF
219+
pub duty: u16,
220+
}
221+
222+
impl EcRequest<EcResponsePwmGetDuty> for EcRequestPwmGetDuty {
223+
fn command_id() -> EcCommands {
224+
EcCommands::PwmGetDuty
225+
}
226+
}
227+
183228
#[repr(C, packed)]
184229
pub struct EcRequestGpioGetV0 {
185230
pub name: [u8; 32],

framework_lib/src/chromium_ec/mod.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -371,22 +371,25 @@ impl CrosEc {
371371
/// * `percent` - An integer from 0 to 100. 0 being off, 100 being full brightness
372372
pub fn set_keyboard_backlight(&self, percent: u8) {
373373
debug_assert!(percent <= 100);
374-
let res = EcRequestPwmSetKeyboardBacklight { percent }.send_command(self);
374+
let res = EcRequestPwmSetDuty {
375+
duty: percent as u16 * (PWM_MAX_DUTY / 100),
376+
pwm_type: PwmType::KbLight as u8,
377+
index: 0,
378+
}
379+
.send_command(self);
375380
debug_assert!(res.is_ok());
376381
}
377382

378383
/// Check the current brightness of the keyboard backlight
379384
///
380385
pub fn get_keyboard_backlight(&self) -> EcResult<u8> {
381-
let kblight = EcRequestPwmGetKeyboardBacklight {}.send_command(self)?;
382-
383-
// The enabled field is deprecated and must always be 1
384-
debug_assert_eq!(kblight.enabled, 1);
385-
if !kblight.enabled == 0 {
386-
error!("Should always be enabled, even if OFF");
386+
let kblight = EcRequestPwmGetDuty {
387+
pwm_type: PwmType::KbLight as u8,
388+
index: 0,
387389
}
390+
.send_command(self)?;
388391

389-
Ok(kblight.percent)
392+
Ok((kblight.duty / (PWM_MAX_DUTY / 100)) as u8)
390393
}
391394

392395
/// Overwrite RO and RW regions of EC flash

0 commit comments

Comments
 (0)