Skip to content

Commit b5d62c3

Browse files
committed
adc: iio: hardware: introduce ChannelDesc type
This makes working with the channel constants a bit less unwieldy. Signed-off-by: Leonard Göhrs <l.goehrs@pengutronix.de>
1 parent 60a84f0 commit b5d62c3

File tree

1 file changed

+75
-46
lines changed

1 file changed

+75
-46
lines changed

src/adc/iio/hardware.rs

Lines changed: 75 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -35,49 +35,71 @@ use thread_priority::*;
3535

3636
use crate::measurement::{Measurement, Timestamp};
3737

38+
struct ChannelDesc {
39+
kernel_name: &'static str,
40+
calibration_path: &'static str,
41+
name: &'static str,
42+
}
43+
3844
// Hard coded list of channels using the internal STM32MP1 ADC.
3945
// Consists of the IIO channel name, the location of the calibration data
4046
// in the device tree and an internal name for the channel.
41-
const CHANNELS_STM32: &[(&str, &str, &str)] = &[
42-
(
43-
"voltage13",
44-
"baseboard-factory-data/usb-host-curr",
45-
"usb-host-curr",
46-
),
47-
(
48-
"voltage15",
49-
"baseboard-factory-data/usb-host1-curr",
50-
"usb-host1-curr",
51-
),
52-
(
53-
"voltage0",
54-
"baseboard-factory-data/usb-host2-curr",
55-
"usb-host2-curr",
56-
),
57-
(
58-
"voltage1",
59-
"baseboard-factory-data/usb-host3-curr",
60-
"usb-host3-curr",
61-
),
62-
("voltage2", "baseboard-factory-data/out0-volt", "out0-volt"),
63-
("voltage10", "baseboard-factory-data/out1-volt", "out1-volt"),
64-
(
65-
"voltage5",
66-
"baseboard-factory-data/iobus-curr",
67-
"iobus-curr",
68-
),
69-
(
70-
"voltage9",
71-
"baseboard-factory-data/iobus-volt",
72-
"iobus-volt",
73-
),
47+
const CHANNELS_STM32: &[ChannelDesc] = &[
48+
ChannelDesc {
49+
kernel_name: "voltage13",
50+
calibration_path: "baseboard-factory-data/usb-host-curr",
51+
name: "usb-host-curr",
52+
},
53+
ChannelDesc {
54+
kernel_name: "voltage15",
55+
calibration_path: "baseboard-factory-data/usb-host1-curr",
56+
name: "usb-host1-curr",
57+
},
58+
ChannelDesc {
59+
kernel_name: "voltage0",
60+
calibration_path: "baseboard-factory-data/usb-host2-curr",
61+
name: "usb-host2-curr",
62+
},
63+
ChannelDesc {
64+
kernel_name: "voltage1",
65+
calibration_path: "baseboard-factory-data/usb-host3-curr",
66+
name: "usb-host3-curr",
67+
},
68+
ChannelDesc {
69+
kernel_name: "voltage2",
70+
calibration_path: "baseboard-factory-data/out0-volt",
71+
name: "out0-volt",
72+
},
73+
ChannelDesc {
74+
kernel_name: "voltage10",
75+
calibration_path: "baseboard-factory-data/out1-volt",
76+
name: "out1-volt",
77+
},
78+
ChannelDesc {
79+
kernel_name: "voltage5",
80+
calibration_path: "baseboard-factory-data/iobus-curr",
81+
name: "iobus-curr",
82+
},
83+
ChannelDesc {
84+
kernel_name: "voltage9",
85+
calibration_path: "baseboard-factory-data/iobus-volt",
86+
name: "iobus-volt",
87+
},
7488
];
7589

7690
// The same as for the STM32MP1 channels but for the discrete ADC on the power
7791
// board.
78-
const CHANNELS_PWR: &[(&str, &str, &str)] = &[
79-
("voltage", "powerboard-factory-data/pwr-volt", "pwr-volt"),
80-
("current", "powerboard-factory-data/pwr-curr", "pwr-curr"),
92+
const CHANNELS_PWR: &[ChannelDesc] = &[
93+
ChannelDesc {
94+
kernel_name: "voltage",
95+
calibration_path: "powerboard-factory-data/pwr-volt",
96+
name: "pwr-volt",
97+
},
98+
ChannelDesc {
99+
kernel_name: "current",
100+
calibration_path: "powerboard-factory-data/pwr-curr",
101+
name: "pwr-curr",
102+
},
81103
];
82104

83105
#[derive(Clone, Copy)]
@@ -235,10 +257,10 @@ impl IioThread {
235257

236258
let stm32_channels: Vec<Channel> = CHANNELS_STM32
237259
.iter()
238-
.map(|(iio_name, _, _)| {
260+
.map(|ChannelDesc { kernel_name, .. }| {
239261
let ch = stm32_adc
240-
.find_channel(iio_name, false)
241-
.unwrap_or_else(|| panic!("Failed to open iio channel {}", iio_name));
262+
.find_channel(kernel_name, false)
263+
.unwrap_or_else(|| panic!("Failed to open iio channel {}", kernel_name));
242264

243265
ch.enable();
244266
ch
@@ -247,10 +269,10 @@ impl IioThread {
247269

248270
let pwr_channels: Vec<Channel> = CHANNELS_PWR
249271
.iter()
250-
.map(|(iio_name, _, _)| {
272+
.map(|ChannelDesc { kernel_name, .. }| {
251273
pwr_adc
252-
.find_channel(iio_name, false)
253-
.unwrap_or_else(|| panic!("Failed to open iio channel {}", iio_name))
274+
.find_channel(kernel_name, false)
275+
.unwrap_or_else(|| panic!("Failed to open iio channel {}", kernel_name))
254276
})
255277
.collect();
256278

@@ -371,10 +393,17 @@ impl IioThread {
371393
.iter()
372394
.chain(CHANNELS_PWR)
373395
.enumerate()
374-
.find(|(_, (_, _, name))| name == &ch_name)
396+
.find(|(_, ChannelDesc { name, .. })| name == &ch_name)
375397
.ok_or(anyhow!("Could not get adc channel {}", ch_name))
376-
.and_then(|(idx, (_, calib_name, _))| {
377-
CalibratedChannel::from_name(self.clone(), idx, calib_name)
378-
})
398+
.and_then(
399+
|(
400+
idx,
401+
ChannelDesc {
402+
calibration_path, ..
403+
},
404+
)| {
405+
CalibratedChannel::from_name(self.clone(), idx, calibration_path)
406+
},
407+
)
379408
}
380409
}

0 commit comments

Comments
 (0)