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

Skip to content

Commit 0ddee22

Browse files
bradjchudson-ayers
authored andcommitted
component: adc: add adcdedicated
1 parent ce3180b commit 0ddee22

File tree

7 files changed

+147
-185
lines changed

7 files changed

+147
-185
lines changed

boards/components/src/adc.rs

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//! Components for using ADC capsules.
22
3+
use capsules::adc::AdcDedicated;
34
use capsules::adc::AdcVirtualized;
45
use capsules::virtual_adc::{AdcDevice, MuxAdc};
56
use core::mem::MaybeUninit;
@@ -40,6 +41,18 @@ macro_rules! adc_syscall_component_helper {
4041
};};
4142
}
4243

44+
#[macro_export]
45+
macro_rules! adc_dedicated_component_static {
46+
($A:ty $(,)?) => {{
47+
let adc = kernel::static_buf!(capsules::adc::AdcDedicated<'static, $A>);
48+
let buffer1 = kernel::static_buf!([u16; capsules::adc::BUF_LEN]);
49+
let buffer2 = kernel::static_buf!([u16; capsules::adc::BUF_LEN]);
50+
let buffer3 = kernel::static_buf!([u16; capsules::adc::BUF_LEN]);
51+
52+
(adc, buffer1, buffer2, buffer3)
53+
};};
54+
}
55+
4356
pub struct AdcMuxComponent<A: 'static + adc::Adc> {
4457
adc: &'static A,
4558
}
@@ -127,3 +140,61 @@ impl Component for AdcVirtualComponent {
127140
adc
128141
}
129142
}
143+
144+
pub struct AdcDedicatedComponent<
145+
A: kernel::hil::adc::Adc + kernel::hil::adc::AdcHighSpeed + 'static,
146+
> {
147+
adc: &'static A,
148+
channels: &'static [A::Channel],
149+
board_kernel: &'static kernel::Kernel,
150+
driver_num: usize,
151+
}
152+
153+
impl<A: kernel::hil::adc::Adc + kernel::hil::adc::AdcHighSpeed + 'static> AdcDedicatedComponent<A> {
154+
pub fn new(
155+
adc: &'static A,
156+
channels: &'static [A::Channel],
157+
board_kernel: &'static kernel::Kernel,
158+
driver_num: usize,
159+
) -> AdcDedicatedComponent<A> {
160+
AdcDedicatedComponent {
161+
adc,
162+
channels,
163+
board_kernel,
164+
driver_num,
165+
}
166+
}
167+
}
168+
169+
impl<A: kernel::hil::adc::Adc + kernel::hil::adc::AdcHighSpeed + 'static> Component
170+
for AdcDedicatedComponent<A>
171+
{
172+
type StaticInput = (
173+
&'static mut MaybeUninit<AdcDedicated<'static, A>>,
174+
&'static mut MaybeUninit<[u16; capsules::adc::BUF_LEN]>,
175+
&'static mut MaybeUninit<[u16; capsules::adc::BUF_LEN]>,
176+
&'static mut MaybeUninit<[u16; capsules::adc::BUF_LEN]>,
177+
);
178+
type Output = &'static AdcDedicated<'static, A>;
179+
180+
unsafe fn finalize(self, s: Self::StaticInput) -> Self::Output {
181+
let grant_cap = create_capability!(capabilities::MemoryAllocationCapability);
182+
183+
let buffer1 = s.1.write([0; capsules::adc::BUF_LEN]);
184+
let buffer2 = s.2.write([0; capsules::adc::BUF_LEN]);
185+
let buffer3 = s.3.write([0; capsules::adc::BUF_LEN]);
186+
187+
let adc = s.0.write(AdcDedicated::new(
188+
&self.adc,
189+
self.board_kernel.create_grant(self.driver_num, &grant_cap),
190+
self.channels,
191+
buffer1,
192+
buffer2,
193+
buffer3,
194+
));
195+
self.adc.set_client(adc);
196+
self.adc.set_highspeed_client(adc);
197+
198+
adc
199+
}
200+
}

boards/hail/src/main.rs

Lines changed: 13 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ use kernel::platform::{KernelResources, SyscallDriverLookup};
2323
use kernel::scheduler::round_robin::RoundRobinSched;
2424
#[allow(unused_imports)]
2525
use kernel::{create_capability, debug, debug_gpio, static_init};
26-
use sam4l::adc::Channel;
2726
use sam4l::chip::Sam4lDefaultPeripherals;
2827

2928
/// Support routines for debugging I/O.
@@ -420,40 +419,21 @@ pub unsafe fn main() {
420419
let adc_channels = static_init!(
421420
[sam4l::adc::AdcChannel; 6],
422421
[
423-
sam4l::adc::AdcChannel::new(Channel::AD0), // A0
424-
sam4l::adc::AdcChannel::new(Channel::AD1), // A1
425-
sam4l::adc::AdcChannel::new(Channel::AD3), // A2
426-
sam4l::adc::AdcChannel::new(Channel::AD4), // A3
427-
sam4l::adc::AdcChannel::new(Channel::AD5), // A4
428-
sam4l::adc::AdcChannel::new(Channel::AD6), // A5
422+
sam4l::adc::AdcChannel::new(sam4l::adc::Channel::AD0), // A0
423+
sam4l::adc::AdcChannel::new(sam4l::adc::Channel::AD1), // A1
424+
sam4l::adc::AdcChannel::new(sam4l::adc::Channel::AD3), // A2
425+
sam4l::adc::AdcChannel::new(sam4l::adc::Channel::AD4), // A3
426+
sam4l::adc::AdcChannel::new(sam4l::adc::Channel::AD5), // A4
427+
sam4l::adc::AdcChannel::new(sam4l::adc::Channel::AD6), // A5
429428
]
430429
);
431-
// Capsule expects references inside array bc it was built assuming model in which
432-
// global structs are used, so this is a bit of a hack to pass it what it wants.
433-
let ref_channels = static_init!(
434-
[&sam4l::adc::AdcChannel; 6],
435-
[
436-
&adc_channels[0],
437-
&adc_channels[1],
438-
&adc_channels[2],
439-
&adc_channels[3],
440-
&adc_channels[4],
441-
&adc_channels[5],
442-
]
443-
);
444-
let adc = static_init!(
445-
capsules::adc::AdcDedicated<'static, sam4l::adc::Adc>,
446-
capsules::adc::AdcDedicated::new(
447-
&peripherals.adc,
448-
board_kernel.create_grant(capsules::adc::DRIVER_NUM, &memory_allocation_capability),
449-
ref_channels,
450-
&mut capsules::adc::ADC_BUFFER1,
451-
&mut capsules::adc::ADC_BUFFER2,
452-
&mut capsules::adc::ADC_BUFFER3
453-
)
454-
);
455-
hil::adc::Adc::set_client(&peripherals.adc, adc);
456-
hil::adc::AdcHighSpeed::set_highspeed_client(&peripherals.adc, adc);
430+
let adc = components::adc::AdcDedicatedComponent::new(
431+
&peripherals.adc,
432+
adc_channels,
433+
board_kernel,
434+
capsules::adc::DRIVER_NUM,
435+
)
436+
.finalize(components::adc_dedicated_component_static!(sam4l::adc::Adc));
457437

458438
// Setup RNG
459439
let rng = components::rng::RngComponent::new(

boards/imix/src/imix_components/adc.rs

Lines changed: 0 additions & 92 deletions
This file was deleted.
Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1 @@
1-
pub mod adc;
21
pub mod test;
3-
4-
pub use self::adc::AdcComponent;

boards/imix/src/main.rs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ use components::process_console::ProcessConsoleComponent;
5454
use components::rng::RngComponent;
5555
use components::si7021::SI7021Component;
5656
use components::spi::{SpiComponent, SpiSyscallComponent};
57-
use imix_components::adc::AdcComponent;
5857

5958
/// Support routines for debugging I/O.
6059
///
@@ -471,8 +470,26 @@ pub unsafe fn main() {
471470
)
472471
.finalize(components::rf233_component_static!(sam4l::spi::SpiHw));
473472

474-
let adc =
475-
AdcComponent::new(board_kernel, capsules::adc::DRIVER_NUM, &peripherals.adc).finalize(());
473+
// Setup ADC
474+
let adc_channels = static_init!(
475+
[sam4l::adc::AdcChannel; 6],
476+
[
477+
sam4l::adc::AdcChannel::new(sam4l::adc::Channel::AD1), // AD0
478+
sam4l::adc::AdcChannel::new(sam4l::adc::Channel::AD2), // AD1
479+
sam4l::adc::AdcChannel::new(sam4l::adc::Channel::AD3), // AD2
480+
sam4l::adc::AdcChannel::new(sam4l::adc::Channel::AD4), // AD3
481+
sam4l::adc::AdcChannel::new(sam4l::adc::Channel::AD5), // AD4
482+
sam4l::adc::AdcChannel::new(sam4l::adc::Channel::AD6), // AD5
483+
]
484+
);
485+
let adc = components::adc::AdcDedicatedComponent::new(
486+
&peripherals.adc,
487+
adc_channels,
488+
board_kernel,
489+
capsules::adc::DRIVER_NUM,
490+
)
491+
.finalize(components::adc_dedicated_component_static!(sam4l::adc::Adc));
492+
476493
let gpio = GpioComponent::new(
477494
board_kernel,
478495
capsules::gpio::DRIVER_NUM,

boards/msp_exp432p401r/src/main.rs

Lines changed: 34 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ use kernel::capabilities;
1313
use kernel::component::Component;
1414
use kernel::dynamic_deferred_call::DynamicDeferredCall;
1515
use kernel::dynamic_deferred_call::DynamicDeferredCallClientState;
16-
use kernel::hil;
1716
use kernel::hil::gpio::Configure;
1817
use kernel::platform::{KernelResources, SyscallDriverLookup};
1918
use kernel::scheduler::round_robin::RoundRobinSched;
@@ -369,54 +368,46 @@ pub unsafe fn main() {
369368
.finalize(components::alarm_component_static!(msp432::timer::TimerA));
370369

371370
// Setup ADC
372-
373371
setup_adc_pins(&peripherals.gpio);
374372

375373
let adc_channels = static_init!(
376-
[&'static msp432::adc::Channel; 24],
374+
[msp432::adc::Channel; 24],
377375
[
378-
&msp432::adc::Channel::Channel0, // A0
379-
&msp432::adc::Channel::Channel1, // A1
380-
&msp432::adc::Channel::Channel2, // A2
381-
&msp432::adc::Channel::Channel3, // A3
382-
&msp432::adc::Channel::Channel4, // A4
383-
&msp432::adc::Channel::Channel5, // A5
384-
&msp432::adc::Channel::Channel6, // A6
385-
&msp432::adc::Channel::Channel7, // A7
386-
&msp432::adc::Channel::Channel8, // A8
387-
&msp432::adc::Channel::Channel9, // A9
388-
&msp432::adc::Channel::Channel10, // A10
389-
&msp432::adc::Channel::Channel11, // A11
390-
&msp432::adc::Channel::Channel12, // A12
391-
&msp432::adc::Channel::Channel13, // A13
392-
&msp432::adc::Channel::Channel14, // A14
393-
&msp432::adc::Channel::Channel15, // A15
394-
&msp432::adc::Channel::Channel16, // A16
395-
&msp432::adc::Channel::Channel17, // A17
396-
&msp432::adc::Channel::Channel18, // A18
397-
&msp432::adc::Channel::Channel19, // A19
398-
&msp432::adc::Channel::Channel20, // A20
399-
&msp432::adc::Channel::Channel21, // A21
400-
&msp432::adc::Channel::Channel22, // A22
401-
&msp432::adc::Channel::Channel23, // A23
376+
msp432::adc::Channel::Channel0, // A0
377+
msp432::adc::Channel::Channel1, // A1
378+
msp432::adc::Channel::Channel2, // A2
379+
msp432::adc::Channel::Channel3, // A3
380+
msp432::adc::Channel::Channel4, // A4
381+
msp432::adc::Channel::Channel5, // A5
382+
msp432::adc::Channel::Channel6, // A6
383+
msp432::adc::Channel::Channel7, // A7
384+
msp432::adc::Channel::Channel8, // A8
385+
msp432::adc::Channel::Channel9, // A9
386+
msp432::adc::Channel::Channel10, // A10
387+
msp432::adc::Channel::Channel11, // A11
388+
msp432::adc::Channel::Channel12, // A12
389+
msp432::adc::Channel::Channel13, // A13
390+
msp432::adc::Channel::Channel14, // A14
391+
msp432::adc::Channel::Channel15, // A15
392+
msp432::adc::Channel::Channel16, // A16
393+
msp432::adc::Channel::Channel17, // A17
394+
msp432::adc::Channel::Channel18, // A18
395+
msp432::adc::Channel::Channel19, // A19
396+
msp432::adc::Channel::Channel20, // A20
397+
msp432::adc::Channel::Channel21, // A21
398+
msp432::adc::Channel::Channel22, // A22
399+
msp432::adc::Channel::Channel23, // A23
402400
]
403401
);
404-
405-
let grant_cap = create_capability!(capabilities::MemoryAllocationCapability);
406-
let grant_adc = board_kernel.create_grant(capsules::adc::DRIVER_NUM, &grant_cap);
407-
let adc = static_init!(
408-
capsules::adc::AdcDedicated<'static, msp432::adc::Adc>,
409-
capsules::adc::AdcDedicated::new(
410-
&peripherals.adc,
411-
grant_adc,
412-
adc_channels,
413-
&mut capsules::adc::ADC_BUFFER1,
414-
&mut capsules::adc::ADC_BUFFER2,
415-
&mut capsules::adc::ADC_BUFFER3
416-
)
417-
);
418-
hil::adc::Adc::set_client(&peripherals.adc, adc);
419-
hil::adc::AdcHighSpeed::set_highspeed_client(&peripherals.adc, adc);
402+
let adc = components::adc::AdcDedicatedComponent::new(
403+
&peripherals.adc,
404+
adc_channels,
405+
board_kernel,
406+
capsules::adc::DRIVER_NUM,
407+
)
408+
.finalize(components::adc_dedicated_component_static!(
409+
msp432::adc::Adc
410+
));
420411

421412
// Set the reference voltage for the ADC to 2.5V
422413
peripherals

0 commit comments

Comments
 (0)