Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 80d8c98

Browse files
committed
components: revamp adc components
1 parent f63de54 commit 80d8c98

File tree

17 files changed

+303
-306
lines changed

17 files changed

+303
-306
lines changed

boards/clue_nrf52840/src/main.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -515,7 +515,7 @@ pub unsafe fn main() {
515515
base_peripherals.adc.calibrate();
516516

517517
let adc_mux = components::adc::AdcMuxComponent::new(&base_peripherals.adc)
518-
.finalize(components::adc_mux_component_helper!(nrf52840::adc::Adc));
518+
.finalize(components::adc_mux_component_static!(nrf52840::adc::Adc));
519519

520520
let adc_syscall =
521521
components::adc::AdcVirtualComponent::new(board_kernel, capsules::adc::DRIVER_NUM)
@@ -525,43 +525,43 @@ pub unsafe fn main() {
525525
&adc_mux,
526526
nrf52840::adc::AdcChannelSetup::new(nrf52840::adc::AdcChannel::AnalogInput7)
527527
)
528-
.finalize(components::adc_component_helper!(nrf52840::adc::Adc)),
528+
.finalize(components::adc_component_static!(nrf52840::adc::Adc)),
529529
// A1
530530
components::adc::AdcComponent::new(
531531
&adc_mux,
532532
nrf52840::adc::AdcChannelSetup::new(nrf52840::adc::AdcChannel::AnalogInput5)
533533
)
534-
.finalize(components::adc_component_helper!(nrf52840::adc::Adc)),
534+
.finalize(components::adc_component_static!(nrf52840::adc::Adc)),
535535
// A2
536536
components::adc::AdcComponent::new(
537537
&adc_mux,
538538
nrf52840::adc::AdcChannelSetup::new(nrf52840::adc::AdcChannel::AnalogInput2)
539539
)
540-
.finalize(components::adc_component_helper!(nrf52840::adc::Adc)),
540+
.finalize(components::adc_component_static!(nrf52840::adc::Adc)),
541541
// A3
542542
components::adc::AdcComponent::new(
543543
&adc_mux,
544544
nrf52840::adc::AdcChannelSetup::new(nrf52840::adc::AdcChannel::AnalogInput3)
545545
)
546-
.finalize(components::adc_component_helper!(nrf52840::adc::Adc)),
546+
.finalize(components::adc_component_static!(nrf52840::adc::Adc)),
547547
// A4
548548
components::adc::AdcComponent::new(
549549
&adc_mux,
550550
nrf52840::adc::AdcChannelSetup::new(nrf52840::adc::AdcChannel::AnalogInput1)
551551
)
552-
.finalize(components::adc_component_helper!(nrf52840::adc::Adc)),
552+
.finalize(components::adc_component_static!(nrf52840::adc::Adc)),
553553
// A5
554554
components::adc::AdcComponent::new(
555555
&adc_mux,
556556
nrf52840::adc::AdcChannelSetup::new(nrf52840::adc::AdcChannel::AnalogInput4)
557557
)
558-
.finalize(components::adc_component_helper!(nrf52840::adc::Adc)),
558+
.finalize(components::adc_component_static!(nrf52840::adc::Adc)),
559559
// A6
560560
components::adc::AdcComponent::new(
561561
&adc_mux,
562562
nrf52840::adc::AdcChannelSetup::new(nrf52840::adc::AdcChannel::AnalogInput0)
563563
)
564-
.finalize(components::adc_component_helper!(nrf52840::adc::Adc)),
564+
.finalize(components::adc_component_static!(nrf52840::adc::Adc)),
565565
));
566566

567567
//--------------------------------------------------------------------------

boards/components/src/adc.rs

Lines changed: 20 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,18 @@ use kernel::capabilities;
55
use kernel::component::Component;
66
use kernel::create_capability;
77
use kernel::hil::adc;
8-
use kernel::static_init_half;
98

109
#[macro_export]
11-
macro_rules! adc_mux_component_helper {
10+
macro_rules! adc_mux_component_static {
1211
($A:ty $(,)?) => {{
13-
use capsules::virtual_adc::MuxAdc;
14-
use core::mem::MaybeUninit;
15-
static mut BUF: MaybeUninit<MuxAdc<'static, $A>> = MaybeUninit::uninit();
16-
&mut BUF
12+
kernel::static_buf!(capsules::virtual_adc::MuxAdc<'static, $A>)
1713
};};
1814
}
1915

2016
#[macro_export]
21-
macro_rules! adc_component_helper {
17+
macro_rules! adc_component_static {
2218
($A:ty $(,)?) => {{
23-
use capsules::virtual_adc::AdcDevice;
24-
use core::mem::MaybeUninit;
25-
static mut BUF: MaybeUninit<AdcDevice<'static, $A>> = MaybeUninit::uninit();
26-
&mut BUF
19+
kernel::static_buf!(capsules::virtual_adc::AdcDevice<'static, $A>)
2720
};};
2821
}
2922

@@ -53,35 +46,30 @@ pub struct AdcMuxComponent<A: 'static + adc::Adc> {
5346
adc: &'static A,
5447
}
5548

56-
pub struct AdcComponent<A: 'static + adc::Adc> {
57-
adc_mux: &'static MuxAdc<'static, A>,
58-
channel: A::Channel,
59-
}
60-
6149
impl<A: 'static + adc::Adc> AdcMuxComponent<A> {
6250
pub fn new(adc: &'static A) -> Self {
6351
AdcMuxComponent { adc: adc }
6452
}
6553
}
6654

67-
pub struct AdcVirtualComponent {
68-
board_kernel: &'static kernel::Kernel,
69-
driver_num: usize,
70-
}
71-
7255
impl<A: 'static + adc::Adc> Component for AdcMuxComponent<A> {
7356
type StaticInput = &'static mut MaybeUninit<MuxAdc<'static, A>>;
7457
type Output = &'static MuxAdc<'static, A>;
7558

7659
unsafe fn finalize(self, static_buffer: Self::StaticInput) -> Self::Output {
77-
let adc_mux = static_init_half!(static_buffer, MuxAdc<'static, A>, MuxAdc::new(self.adc));
60+
let adc_mux = static_buffer.write(MuxAdc::new(self.adc));
7861

7962
self.adc.set_client(adc_mux);
8063

8164
adc_mux
8265
}
8366
}
8467

68+
pub struct AdcComponent<A: 'static + adc::Adc> {
69+
adc_mux: &'static MuxAdc<'static, A>,
70+
channel: A::Channel,
71+
}
72+
8573
impl<A: 'static + adc::Adc> AdcComponent<A> {
8674
pub fn new(mux: &'static MuxAdc<'static, A>, channel: A::Channel) -> Self {
8775
AdcComponent {
@@ -96,18 +84,19 @@ impl<A: 'static + adc::Adc> Component for AdcComponent<A> {
9684
type Output = &'static AdcDevice<'static, A>;
9785

9886
unsafe fn finalize(self, static_buffer: Self::StaticInput) -> Self::Output {
99-
let adc_device = static_init_half!(
100-
static_buffer,
101-
AdcDevice<'static, A>,
102-
AdcDevice::new(self.adc_mux, self.channel)
103-
);
87+
let adc_device = static_buffer.write(AdcDevice::new(self.adc_mux, self.channel));
10488

10589
adc_device.add_to_mux();
10690

10791
adc_device
10892
}
10993
}
11094

95+
pub struct AdcVirtualComponent {
96+
board_kernel: &'static kernel::Kernel,
97+
driver_num: usize,
98+
}
99+
111100
impl AdcVirtualComponent {
112101
pub fn new(board_kernel: &'static kernel::Kernel, driver_num: usize) -> AdcVirtualComponent {
113102
AdcVirtualComponent {
@@ -128,11 +117,10 @@ impl Component for AdcVirtualComponent {
128117
let grant_cap = create_capability!(capabilities::MemoryAllocationCapability);
129118
let grant_adc = self.board_kernel.create_grant(self.driver_num, &grant_cap);
130119

131-
let adc = static_init_half!(
132-
static_buffer.0,
133-
capsules::adc::AdcVirtualized<'static>,
134-
capsules::adc::AdcVirtualized::new(static_buffer.1, grant_adc)
135-
);
120+
let adc = static_buffer.0.write(capsules::adc::AdcVirtualized::new(
121+
static_buffer.1,
122+
grant_adc,
123+
));
136124

137125
for driver in static_buffer.1 {
138126
kernel::hil::adc::AdcChannel::set_client(*driver, adc);

boards/components/src/adc_microphone.rs

Lines changed: 58 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -5,94 +5,91 @@
55
//!
66
//!
77
//! ```rust
8-
//! let adc_microphone = components::adc_microphone::AdcMicrophoneComponent::new().finalize(
9-
//! components::adc_microphone_component_helper!(
10-
//! // adc
11-
//! nrf52833::adc::Adc,
12-
//! // adc channel
13-
//! nrf52833::adc::AdcChannelSetup::setup(
14-
//! nrf52833::adc::AdcChannel::AnalogInput3,
15-
//! nrf52833::adc::AdcChannelGain::Gain4,
16-
//! nrf52833::adc::AdcChannelResistor::Bypass,
17-
//! nrf52833::adc::AdcChannelResistor::Pulldown,
18-
//! nrf52833::adc::AdcChannelSamplingTime::us3
19-
//! ),
20-
//! // adc mux
21-
//! adc_mux,
22-
//! // buffer size
23-
//! 50,
24-
//! // gpio
25-
//! nrf52833::gpio::GPIOPin,
26-
//! // optional gpio pin
27-
//! Some(&base_peripherals.gpio_port[LED_MICROPHONE_PIN])
28-
//! ),
29-
//! );
8+
//! let adc_microphone = components::adc_microphone::AdcMicrophoneComponent::new(
9+
//! adc_mux,
10+
//! nrf52833::adc::AdcChannelSetup::setup(
11+
//! nrf52833::adc::AdcChannel::AnalogInput3,
12+
//! nrf52833::adc::AdcChannelGain::Gain4,
13+
//! nrf52833::adc::AdcChannelResistor::Bypass,
14+
//! nrf52833::adc::AdcChannelResistor::Pulldown,
15+
//! nrf52833::adc::AdcChannelSamplingTime::us3,
16+
//! ),
17+
//! Some(&nrf52833_peripherals.gpio_port[LED_MICROPHONE_PIN]),
18+
//! )
19+
//! .finalize(components::adc_microphone_component_static!(
20+
//! // adc
21+
//! nrf52833::adc::Adc,
22+
//! // buffer size
23+
//! 50,
24+
//! // gpio
25+
//! nrf52833::gpio::GPIOPin
26+
//! ));
3027
//! ```
3128
3229
use capsules::adc_microphone::AdcMicrophone;
3330
use capsules::virtual_adc::AdcDevice;
34-
use core::marker::PhantomData;
3531
use core::mem::MaybeUninit;
3632
use kernel::component::Component;
3733
use kernel::hil::adc::{self, AdcChannel};
3834
use kernel::hil::gpio;
39-
use kernel::static_init_half;
4035

4136
#[macro_export]
42-
macro_rules! adc_microphone_component_helper {
43-
($A:ty, $channel:expr, $adc_mux:expr, $LEN:literal, $P: ty, $pin: expr $(,)?) => {{
44-
use capsules::adc_microphone::AdcMicrophone;
45-
use capsules::virtual_adc::AdcDevice;
46-
use core::mem::MaybeUninit;
47-
use kernel::hil::adc::Adc;
48-
use kernel::hil::gpio::Pin;
37+
macro_rules! adc_microphone_component_static {
38+
($A:ty, $LEN:literal, $P: ty $(,)?) => {{
39+
let adc_device = components::adc_component_static!($A);
40+
let buffer = kernel::static_buf!([u16; $LEN]);
41+
let adc_microphone =
42+
kernel::static_buf!(capsules::adc_microphone::AdcMicrophone<'static, $P>);
4943

50-
static mut BUFFER: [u16; $LEN] = [0; $LEN];
51-
52-
let mut adc_microphone_adc: &'static capsules::virtual_adc::AdcDevice<'static, $A> =
53-
components::adc::AdcComponent::new($adc_mux, $channel)
54-
.finalize(components::adc_component_helper!($A));
55-
static mut adc_microphone: MaybeUninit<AdcMicrophone<'static, $P>> = MaybeUninit::uninit();
56-
(
57-
&mut adc_microphone_adc,
58-
$pin,
59-
&mut BUFFER,
60-
&mut adc_microphone,
61-
)
44+
(adc_device, buffer, adc_microphone)
6245
};};
6346
}
6447

65-
pub struct AdcMicrophoneComponent<A: 'static + adc::Adc, P: 'static + gpio::Pin> {
66-
_adc: PhantomData<A>,
67-
_pin: PhantomData<P>,
48+
pub struct AdcMicrophoneComponent<
49+
A: 'static + adc::Adc,
50+
P: 'static + gpio::Pin,
51+
const BUF_LEN: usize,
52+
> {
53+
adc_mux: &'static capsules::virtual_adc::MuxAdc<'static, A>,
54+
adc_channel: A::Channel,
55+
pin: Option<&'static P>,
6856
}
6957

70-
impl<A: 'static + adc::Adc, P: 'static + gpio::Pin> AdcMicrophoneComponent<A, P> {
71-
pub fn new() -> AdcMicrophoneComponent<A, P> {
58+
impl<A: 'static + adc::Adc, P: 'static + gpio::Pin, const BUF_LEN: usize>
59+
AdcMicrophoneComponent<A, P, BUF_LEN>
60+
{
61+
pub fn new(
62+
adc_mux: &'static capsules::virtual_adc::MuxAdc<'static, A>,
63+
adc_channel: A::Channel,
64+
pin: Option<&'static P>,
65+
) -> AdcMicrophoneComponent<A, P, BUF_LEN> {
7266
AdcMicrophoneComponent {
73-
_adc: PhantomData,
74-
_pin: PhantomData,
67+
adc_mux,
68+
adc_channel,
69+
pin,
7570
}
7671
}
7772
}
7873

79-
impl<A: 'static + adc::Adc, P: 'static + gpio::Pin> Component for AdcMicrophoneComponent<A, P> {
74+
impl<A: 'static + adc::Adc, P: 'static + gpio::Pin, const BUF_LEN: usize> Component
75+
for AdcMicrophoneComponent<A, P, BUF_LEN>
76+
{
8077
type StaticInput = (
81-
&'static AdcDevice<'static, A>,
82-
Option<&'static P>,
83-
&'static mut [u16],
78+
&'static mut MaybeUninit<AdcDevice<'static, A>>,
79+
&'static mut MaybeUninit<[u16; BUF_LEN]>,
8480
&'static mut MaybeUninit<AdcMicrophone<'static, P>>,
8581
);
8682
type Output = &'static AdcMicrophone<'static, P>;
8783

88-
unsafe fn finalize(self, static_buffer: Self::StaticInput) -> Self::Output {
89-
let adc_microphone = static_init_half!(
90-
static_buffer.3,
91-
AdcMicrophone<'static, P>,
92-
AdcMicrophone::new(static_buffer.0, static_buffer.1, static_buffer.2)
93-
);
84+
unsafe fn finalize(self, s: Self::StaticInput) -> Self::Output {
85+
let adc_device =
86+
crate::adc::AdcComponent::new(self.adc_mux, self.adc_channel).finalize(s.0);
87+
88+
let buffer = s.1.write([0; BUF_LEN]);
89+
90+
let adc_microphone = s.2.write(AdcMicrophone::new(adc_device, self.pin, buffer));
9491

95-
static_buffer.0.set_client(adc_microphone);
92+
adc_device.set_client(adc_microphone);
9693

9794
adc_microphone
9895
}

0 commit comments

Comments
 (0)