Skip to content

Commit

Permalink
Set fan target RPM
Browse files Browse the repository at this point in the history
  • Loading branch information
ChocolateLoverRaj committed May 18, 2024
1 parent 80c6bfb commit 24faae6
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
2 changes: 2 additions & 0 deletions crosec/src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ pub enum CrosEcCmd {
GetChipInfo = 0x0005,
GetBoardVersion = 0x0006,
GetCmdVersions = 0x0008,
SetFanTargetRpm = 0x0021,
}

pub mod get_chip_info;
pub mod hello;
pub mod version;
pub mod board_version;
pub mod set_fan_target_rpm;
pub mod get_cmd_versions;
41 changes: 41 additions & 0 deletions crosec/src/commands/set_fan_target_rpm.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use std::mem::size_of;
use bytemuck::{Pod, Zeroable};

use crate::{ec_command, EcCmdResult, EcInterface};
use crate::commands::CrosEcCmd;

#[repr(C)]
#[derive(Pod, Zeroable, Copy, Clone)]
struct EcParamsSetFanTargetRpmV0 {
rpm: u32,
}

struct EcParamsSetFanTargetRpmV1 {
rpm: u32,
fan_index: u8,
}

impl EcParamsSetFanTargetRpmV1 {
pub fn to_le_bytes(self) -> [u8; size_of::<u32>() + size_of::<u8>()] {
[self.rpm.to_le_bytes().to_vec(), self.fan_index.to_le_bytes().to_vec()].concat().try_into().unwrap()
}
}

pub fn ec_cmd_set_fan_target_rpm(rpm: u32, fan_index: Option<u8>) -> EcCmdResult<()> {
// v0 can only set the RPM for all fans
// v1 can set the RPM for a specific fan
match fan_index {
Some(index) => {
ec_command(CrosEcCmd::SetFanTargetRpm, 1, &EcParamsSetFanTargetRpmV1 {
rpm,
fan_index: index,
}.to_le_bytes(), EcInterface::Default)?;
}
None => {
ec_command(CrosEcCmd::SetFanTargetRpm, 0, &bytemuck::bytes_of(&EcParamsSetFanTargetRpmV0 {
rpm
}), EcInterface::Default)?;
}
};
Ok(())
}
18 changes: 18 additions & 0 deletions ectool/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crosec::commands::{CrosEcCmd, get_chip_info::ec_cmd_get_chip_info, hello::ec
use crosec::commands::board_version::ec_cmd_board_version;
use crosec::commands::get_cmd_versions::ec_cmd_get_cmd_versions;
use num_traits::cast::FromPrimitive;
use crosec::commands::set_fan_target_rpm::ec_cmd_set_fan_target_rpm;

#[derive(Parser)]
struct Cli {
Expand All @@ -25,6 +26,12 @@ enum Commands {
CmdVersions {
command: u32
},
/// Set target fan RPM
SetFanTargetRpm {
rpm: u32,
#[arg()]
index: Option<u8>
}
}

fn main() -> Result<()> {
Expand Down Expand Up @@ -70,6 +77,17 @@ fn main() -> Result<()> {
println!("Unknown Command");
}
}
},
Commands::SetFanTargetRpm {rpm, index} => {
ec_cmd_set_fan_target_rpm(rpm, index)?;
match index {
Some(index) => {
println!("Set RPM to {rpm} for fan {index}");
},
None => {
println!("Set RPM to {rpm} for all fans");
}
}
}
}

Expand Down

0 comments on commit 24faae6

Please sign in to comment.