Skip to content

Commit

Permalink
Merge pull request #202 from quartiq/rs/led-toggle-on-bus-nack
Browse files Browse the repository at this point in the history
Toggle main-board LEDs on I2C fan NACK
  • Loading branch information
jordens authored Feb 16, 2022
2 parents d4595fc + 6ef01bf commit 73ab7b9
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ authors = ["Ryan Summers <ryan.summers@vertigo-designs.com>"]
edition = "2018"
readme = "README.md"
name = "booster"
version = "0.3.0-rc.1"
version = "0.3.0-rc.3"
build = "build.rs"
resolver = "2"

Expand Down
23 changes: 18 additions & 5 deletions src/hardware/chassis_fans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
//! Copyright (C) 2020 QUARTIQ GmbH - All Rights Reserved
//! Unauthorized usage, editing, or copying is strictly prohibited.
//! Proprietary and confidential.
use super::{I2cError, I2cProxy};
use super::{I2cError, I2cProxy, MainboardLeds};
use embedded_hal::digital::v2::OutputPin;
use max6639::Max6639;

/// The default fan speed on power-up.
Expand All @@ -14,6 +15,7 @@ pub const DEFAULT_FAN_SPEED: f32 = 0.2;
pub struct ChassisFans {
fans: [Max6639<I2cProxy>; 3],
duty_cycle: f32,
leds: MainboardLeds,
}

impl ChassisFans {
Expand All @@ -25,10 +27,11 @@ impl ChassisFans {
///
/// # Returns
/// A new fan controller.
pub fn new(fans: [Max6639<I2cProxy>; 3]) -> Self {
pub fn new(fans: [Max6639<I2cProxy>; 3], leds: MainboardLeds) -> Self {
ChassisFans {
fans,
duty_cycle: DEFAULT_FAN_SPEED,
leds,
}
}

Expand Down Expand Up @@ -58,14 +61,24 @@ impl ChassisFans {
// Bound the duty cycle to a normalized range.
let duty_cycle = duty_cycle.clamp(0.0, 1.0);

let leds = &mut self.leds;

// Keep retrying until the configuration succeeds or the maximum number of retry
// attempts is exhausted.
let retry_set = |fan: &mut Max6639<I2cProxy>, subfan, duty_cycle| {
let mut retry_set = |fan: &mut Max6639<I2cProxy>, subfan, duty_cycle| {
for _ in 0..2 {
match fan.set_duty_cycle(subfan, duty_cycle) {
Err(max6639::Error::Interface(I2cError::NACK)) => {}
Err(max6639::Error::Interface(I2cError::NACK)) => {
leds.0.set_high().unwrap();
leds.1.set_high().unwrap();
leds.2.set_high().unwrap();
}
Err(e) => Err(e).unwrap(),
Ok(_) => break,
Ok(_) => {
leds.0.set_low().unwrap();
leds.1.set_low().unwrap();
leds.2.set_low().unwrap();
}
}
}
};
Expand Down
5 changes: 5 additions & 0 deletions src/hardware/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ pub type Spi = hal::spi::Spi<
),
>;

pub type Led1 = hal::gpio::gpioc::PC8<hal::gpio::Output<hal::gpio::PushPull>>;
pub type Led2 = hal::gpio::gpioc::PC9<hal::gpio::Output<hal::gpio::PushPull>>;
pub type Led3 = hal::gpio::gpioc::PC10<hal::gpio::Output<hal::gpio::PushPull>>;
pub type MainboardLeds = (Led1, Led2, Led3);

#[cfg(feature = "phy_enc424j600")]
pub type NetworkStack = smoltcp_nal::NetworkStack<
'static,
Expand Down
14 changes: 13 additions & 1 deletion src/hardware/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,18 @@ pub fn setup(
};

let mut fans = {
let main_board_leds = {
let mut led1 = gpioc.pc8.into_push_pull_output();
let mut led2 = gpioc.pc9.into_push_pull_output();
let mut led3 = gpioc.pc10.into_push_pull_output();

led1.set_low().unwrap();
led2.set_low().unwrap();
led3.set_low().unwrap();

(led1, led2, led3)
};

let fan1 =
max6639::Max6639::new(i2c_bus_manager.acquire_i2c(), max6639::AddressPin::Pulldown)
.unwrap();
Expand All @@ -341,7 +353,7 @@ pub fn setup(
max6639::Max6639::new(i2c_bus_manager.acquire_i2c(), max6639::AddressPin::Pullup)
.unwrap();

ChassisFans::new([fan1, fan2, fan3])
ChassisFans::new([fan1, fan2, fan3], main_board_leds)
};

assert!(fans.self_test(&mut delay));
Expand Down

0 comments on commit 73ab7b9

Please sign in to comment.