Skip to content

Commit 11f1f5a

Browse files
authored
I2C codegen dedupe (#2569)
Closes #2552 This is the most straightforward de-duplication approach, essentially "pushing down" the knowledge of whether a device requires a rail or not. This change *matches* the current behavior, and that of the system prior to #2538, and replaces the pre-existing by-rail-name accessor with the newer by-rail-name accessor (giving it the pre-existing accessor's nicer name). It *feels* like this change might not go far enough yet: for all of the devices we are creating, it is probably statically knowable whether that device SHOULD or SHOULD NOT have a rail index. For example, `Bmr491` and `Tps546B24A` do not, and previously had a "dead" rail parameter that I've removed. For correctness' sake, it feels like we should ideally fail out at compile time if a device SHOULD have multiple rails, and doesn't (according to the app toml), OR if a device SHOULDN'T have multiple rails, but does (according to the app toml).
1 parent 6e93059 commit 11f1f5a

14 files changed

Lines changed: 77 additions & 104 deletions

File tree

build/i2c/src/lib.rs

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1386,31 +1386,14 @@ impl ConfigGenerator {
13861386
for (rail, (device, index)) in &all {
13871387
let raw_bank = index.unwrap_or(0);
13881388

1389-
// ----
1390-
// First accessor, returns `(I2cDevice, u8)`
1391-
1392-
// if we update this code to be more clever than just
1393-
// to-lowercase'ing the rail names, you might need to go update
1394-
// the mapping in `control-plane-agent`!
1395-
write!(
1396-
&mut self.output,
1397-
r##"
1398-
#[allow(dead_code)]
1399-
pub fn {}(task: TaskId) -> (I2cDevice, u8) {{"##,
1400-
rail.to_lowercase(),
1401-
)?;
1402-
1403-
let out = self.generate_device(device, 16);
1404-
writeln!(&mut self.output, "({out}, {raw_bank})\n }}")?;
1405-
14061389
// ---
1407-
// Second accessor, returns `(I2cDevice, Option<u8>)`
1390+
// Accessor, returns `(I2cDevice, Option<u8>)`
14081391

14091392
write!(
14101393
&mut self.output,
14111394
r##"
14121395
#[allow(dead_code)]
1413-
pub fn {}_with_opt_page_idx(task: TaskId)"##,
1396+
pub fn {}(task: TaskId)"##,
14141397
rail.to_lowercase(),
14151398
)?;
14161399
write!(&mut self.output, " -> (I2cDevice, Option<u8>) {{")?;

drv/cosmo-seq-server/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ fn main() -> ! {
195195
use drv_i2c_devices::bmr491::{Bmr491, ExternalInputVoltageProtection};
196196

197197
let dev = i2c_config::devices::bmr491_u80(I2C.get_task_id());
198-
let driver = Bmr491::new(&dev, 0);
198+
let driver = Bmr491::new(&dev);
199199

200200
// Cosmo provides external undervoltage protection that kicks in at a
201201
// lower voltage than we'd like to tolerate, so, request additional

drv/cosmo-seq-server/src/vcore.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,10 +190,12 @@ const VCORE_NSAMPLES: usize = 25;
190190

191191
impl VCore {
192192
pub fn new(i2c: TaskId) -> Self {
193-
let (device, rail) = i2c_config::pmbus::vddcr_cpu0_a0(i2c);
193+
let (device, opt_rail) = i2c_config::pmbus::vddcr_cpu0_a0(i2c);
194+
let rail = opt_rail.unwrap_or(0);
194195
let vddcr_cpu0 = Raa229620A::new(&device, rail);
195196

196-
let (device, rail) = i2c_config::pmbus::vddcr_cpu1_a0(i2c);
197+
let (device, opt_rail) = i2c_config::pmbus::vddcr_cpu1_a0(i2c);
198+
let rail = opt_rail.unwrap_or(0);
197199
let vddcr_cpu1 = Raa229620A::new(&device, rail);
198200

199201
Self {

drv/gimlet-seq-server/src/main.rs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,7 @@ impl<S: SpiServer + Clone> ServerImpl<S> {
462462
};
463463

464464
let dev = i2c_config::devices::bmr491_u431(I2C.get_task_id());
465-
let driver = Bmr491::new(&dev, 0);
465+
let driver = Bmr491::new(&dev);
466466

467467
// Gimlet provides external undervoltage protection that is better
468468
// than what we'd get from the 491, so we rely on that.
@@ -528,7 +528,9 @@ impl<S: SpiServer + Clone> ServerImpl<S> {
528528
// Turn on the chassis LED once we reach A2
529529
sys.gpio_set(CHASSIS_LED);
530530

531-
let (device, rail) = i2c_config::pmbus::vdd_vcore(I2C.get_task_id());
531+
let (device, opt_rail) =
532+
i2c_config::pmbus::vdd_vcore(I2C.get_task_id());
533+
let rail = opt_rail.unwrap_or(0);
532534

533535
let mut server = Self {
534536
state: PowerState::A2,
@@ -1578,10 +1580,12 @@ cfg_if::cfg_if! {
15781580
use drv_i2c_devices::raa229618::Raa229618;
15791581
let i2c = I2C.get_task_id();
15801582

1581-
let (device, rail) = i2c_config::pmbus::vdd_vcore(i2c);
1583+
let (device, opt_rail) = i2c_config::pmbus::vdd_vcore(i2c);
1584+
let rail = opt_rail.unwrap_or(0);
15821585
let mut vdd_vcore = Raa229618::new(&device, rail);
15831586

1584-
let (device, rail) = i2c_config::pmbus::vddcr_soc(i2c);
1587+
let (device, opt_rail) = i2c_config::pmbus::vddcr_soc(i2c);
1588+
let rail = opt_rail.unwrap_or(0);
15851589
let mut vddcr_soc = Raa229618::new(&device, rail);
15861590

15871591
retry_i2c_txn(I2cTxn::VCoreOff, || vdd_vcore.turn_off())?;
@@ -1593,10 +1597,12 @@ cfg_if::cfg_if! {
15931597
use drv_i2c_devices::raa229618::Raa229618;
15941598
let i2c = I2C.get_task_id();
15951599

1596-
let (device, rail) = i2c_config::pmbus::vdd_vcore(i2c);
1600+
let (device, opt_rail) = i2c_config::pmbus::vdd_vcore(i2c);
1601+
let rail = opt_rail.unwrap_or(0);
15971602
let mut vdd_vcore = Raa229618::new(&device, rail);
15981603

1599-
let (device, rail) = i2c_config::pmbus::vddcr_soc(i2c);
1604+
let (device, opt_rail) = i2c_config::pmbus::vddcr_soc(i2c);
1605+
let rail = opt_rail.unwrap_or(0);
16001606
let mut vddcr_soc = Raa229618::new(&device, rail);
16011607

16021608
retry_i2c_txn(I2cTxn::VCoreOn, || vdd_vcore.turn_on())?;
@@ -1620,8 +1626,8 @@ cfg_if::cfg_if! {
16201626

16211627
let i2c = I2C.get_task_id();
16221628

1623-
let (device, rail) = i2c_config::pmbus::v3p3_sys_a0(i2c);
1624-
let v3p3_sys_a0 = Tps546B24A::new(&device, rail);
1629+
let (device, _rail) = i2c_config::pmbus::v3p3_sys_a0(i2c);
1630+
let v3p3_sys_a0 = Tps546B24A::new(&device);
16251631

16261632
ringbuf_entry!(
16271633
Trace::V3P3SysA0VOut(v3p3_sys_a0.read_vout().unwrap_lite())

drv/i2c-devices/src/bmr491.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ pub struct MitigationFailure {
133133
}
134134

135135
impl Bmr491 {
136-
pub fn new(device: &I2cDevice, _rail: u8) -> Self {
136+
pub fn new(device: &I2cDevice) -> Self {
137137
Bmr491 {
138138
device: *device,
139139
mode: Cell::new(None),

drv/i2c-devices/src/tps546b24a.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ impl From<Error> for ResponseCode {
5555
}
5656

5757
impl Tps546B24A {
58-
pub fn new(device: &I2cDevice, _rail: u8) -> Self {
58+
pub fn new(device: &I2cDevice) -> Self {
5959
Tps546B24A {
6060
device: *device,
6161
mode: Cell::new(None),

drv/psc-seq-server/src/bsp/observer_a.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
44

55
use super::{PSU_COUNT, i2c_config, notifications};
6-
use drv_i2c_api::I2cDevice;
76
use drv_stm32xx_sys_api as sys_api;
8-
use userlib::TaskId;
97

108
pub use drv_i2c_devices::mwocp6x::Mwocp67 as Mwocp6x;
119

@@ -49,8 +47,11 @@ pub const PSU_PWR_OK_NOTIF: [u32; PSU_COUNT] = [
4947
notifications::PSU_PWR_OK_5_MASK,
5048
];
5149

50+
/// Type returned by generated pmbus rail functions
51+
pub type SummonFn = fn(userlib::TaskId) -> (drv_i2c_api::I2cDevice, Option<u8>);
52+
5253
/// In order to get the PMBus devices by PSU index, we need a little lookup table.
53-
pub const PSU_PMBUS_DEVS: [fn(TaskId) -> (I2cDevice, u8); PSU_COUNT] = [
54+
pub const PSU_PMBUS_DEVS: [SummonFn; PSU_COUNT] = [
5455
i2c_config::pmbus::v50_main_psu0,
5556
i2c_config::pmbus::v50_main_psu1,
5657
i2c_config::pmbus::v50_main_psu2,

drv/psc-seq-server/src/bsp/psc_bc.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
44

55
use super::{PSU_COUNT, i2c_config, notifications};
6-
use drv_i2c_api::I2cDevice;
76
use drv_stm32xx_sys_api as sys_api;
8-
use userlib::TaskId;
97

108
pub use drv_i2c_devices::mwocp6x::Mwocp68 as Mwocp6x;
119

@@ -51,8 +49,11 @@ pub const PSU_PWR_OK_NOTIF: [u32; PSU_COUNT] = [
5149
notifications::PSU_PWR_OK_6_MASK,
5250
];
5351

52+
/// Type returned by generated pmbus rail functions
53+
pub type SummonFn = fn(userlib::TaskId) -> (drv_i2c_api::I2cDevice, Option<u8>);
54+
5455
/// In order to get the PMBus devices by PSU index, we need a little lookup table.
55-
pub const PSU_PMBUS_DEVS: [fn(TaskId) -> (I2cDevice, u8); PSU_COUNT] = [
56+
pub const PSU_PMBUS_DEVS: [SummonFn; PSU_COUNT] = [
5657
i2c_config::pmbus::v54_psu0,
5758
i2c_config::pmbus::v54_psu1,
5859
i2c_config::pmbus::v54_psu2,

drv/psc-seq-server/src/main.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,8 @@ fn main() -> ! {
509509
let dev = {
510510
let i2c = I2C.get_task_id();
511511
let make_dev = bsp::PSU_PMBUS_DEVS[i];
512-
let (dev, rail) = make_dev(i2c);
512+
let (dev, opt_rail) = make_dev(i2c);
513+
let rail = opt_rail.unwrap_or(0);
513514
bsp::Mwocp6x::new(&dev, rail)
514515
};
515516
let slot = PSU_SLOTS[i];

drv/sidecar-seq-server/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -906,7 +906,7 @@ fn main() -> ! {
906906
use drv_i2c_devices::bmr491::{Bmr491, ExternalInputVoltageProtection};
907907

908908
let dev = i2c_config::devices::bmr491_u12(I2C.get_task_id());
909-
let driver = Bmr491::new(&dev, 0);
909+
let driver = Bmr491::new(&dev);
910910

911911
// Sidecar provides external undervoltage protection that is better than
912912
// what we'd get from the 491, so we rely on that.

0 commit comments

Comments
 (0)