Skip to content

Commit 006b1d9

Browse files
committed
Merge upstream
2 parents fa24ef5 + 3bc8f2b commit 006b1d9

File tree

13 files changed

+552
-376
lines changed

13 files changed

+552
-376
lines changed

examples/rtc-demo/Cargo.toml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
[package]
2+
name = "rtc-demo"
3+
version = "0.1.0"
4+
authors = ["Timo Kröger"]
5+
edition = "2018"
6+
7+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
8+
9+
[dependencies]
10+
cortex-m = "0.6.2"
11+
cortex-m-rt = "0.6.12"
12+
rtt-target = {version = "0.2.0", features = ["cortex-m"] }
13+
nrf52840-hal = { features = ["rt"], path = "../../nrf52840-hal" }
14+
15+
[dependencies.embedded-hal]
16+
version = "0.2.3"
17+
features = ["unproven"]

examples/rtc-demo/Embed.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[default.rtt]
2+
enabled = true

examples/rtc-demo/README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# RTC demo
2+
3+
This example shows how to use the compare functionality of the real-time counter peripheral.
4+
Runs on the nRF52840_DK but can easily be adapted for other hardware.
5+
6+
## Set up with `cargo-embed`
7+
8+
Install `cargo-embed` if you don't have it already:
9+
10+
```console
11+
$ cargo install cargo-embed
12+
```
13+
14+
Then just `cd` to the example folder and run
15+
16+
```console
17+
$ cargo embed --target thumbv7em-none-eabihf
18+
```

examples/rtc-demo/src/main.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#![no_main]
2+
#![no_std]
3+
4+
use nrf52840_hal as hal;
5+
6+
use hal::rtc::{Rtc, RtcCompareReg, RtcInterrupt};
7+
use rtt_target::{rprintln, rtt_init_print};
8+
9+
#[panic_handler] // panicking behavior
10+
fn panic(_: &core::panic::PanicInfo) -> ! {
11+
loop {
12+
cortex_m::asm::bkpt();
13+
}
14+
}
15+
16+
#[cortex_m_rt::entry]
17+
fn main() -> ! {
18+
rtt_init_print!();
19+
20+
let p = hal::pac::Peripherals::take().unwrap();
21+
22+
// Enable LfClk which is required by the RTC.
23+
let clocks = hal::clocks::Clocks::new(p.CLOCK);
24+
let clocks = clocks.start_lfclk();
25+
26+
// Run RTC for 1 second
27+
let mut rtc = Rtc::new(p.RTC0);
28+
rtc.set_compare(RtcCompareReg::Compare0, 32_768).unwrap();
29+
rtc.enable_event(RtcInterrupt::Compare0);
30+
31+
rprintln!("Starting RTC");
32+
let rtc = rtc.enable_counter();
33+
34+
rprintln!("Waiting for compare match");
35+
while !rtc.is_event_triggered(RtcInterrupt::Compare0) {}
36+
rtc.reset_event(RtcInterrupt::Compare0);
37+
38+
rprintln!("Compare match, stopping RTC");
39+
let rtc = rtc.disable_counter();
40+
41+
rprintln!("Counter stopped at {} ticks", rtc.get_counter());
42+
43+
// Stop LfClk when RTC is not used anymore.
44+
rtc.release();
45+
clocks.stop_lfclk();
46+
47+
loop {
48+
cortex_m::asm::nop();
49+
}
50+
}

nrf-hal-common/src/gpio.rs

Lines changed: 84 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,13 @@ impl<MODE> Pin<MODE> {
133133
unsafe { &*ptr }
134134
}
135135

136+
pub(crate) fn conf(&self) -> &gpio::PIN_CNF {
137+
&self.block().pin_cnf[self.pin() as usize]
138+
}
139+
136140
/// Convert the pin to be a floating input
137141
pub fn into_floating_input(self) -> Pin<Input<Floating>> {
138-
self.block().pin_cnf[self.pin() as usize].write(|w| {
142+
self.conf().write(|w| {
139143
w.dir().input();
140144
w.input().connect();
141145
w.pull().disabled();
@@ -150,7 +154,7 @@ impl<MODE> Pin<MODE> {
150154
}
151155
}
152156
pub fn into_pullup_input(self) -> Pin<Input<PullUp>> {
153-
self.block().pin_cnf[self.pin() as usize].write(|w| {
157+
self.conf().write(|w| {
154158
w.dir().input();
155159
w.input().connect();
156160
w.pull().pullup();
@@ -165,7 +169,7 @@ impl<MODE> Pin<MODE> {
165169
}
166170
}
167171
pub fn into_pulldown_input(self) -> Pin<Input<PullDown>> {
168-
self.block().pin_cnf[self.pin() as usize].write(|w| {
172+
self.conf().write(|w| {
169173
w.dir().input();
170174
w.input().connect();
171175
w.pull().pulldown();
@@ -192,7 +196,7 @@ impl<MODE> Pin<MODE> {
192196
Level::High => pin.set_high().unwrap(),
193197
}
194198

195-
self.block().pin_cnf[self.pin() as usize].write(|w| {
199+
self.conf().write(|w| {
196200
w.dir().output();
197201
w.input().connect(); // AJM - hack for SPI
198202
w.pull().disabled();
@@ -224,8 +228,7 @@ impl<MODE> Pin<MODE> {
224228
}
225229

226230
// This is safe, as we restrict our access to the dedicated register for this pin.
227-
let pin_cnf = &self.block().pin_cnf[self.pin() as usize];
228-
pin_cnf.write(|w| {
231+
self.conf().write(|w| {
229232
w.dir().output();
230233
w.input().disconnect();
231234
w.pull().disabled();
@@ -236,6 +239,20 @@ impl<MODE> Pin<MODE> {
236239

237240
pin
238241
}
242+
243+
/// Disconnects the pin.
244+
///
245+
/// In disconnected mode the pin cannot be used as input or output.
246+
/// It is primarily useful to reduce power usage.
247+
pub fn into_disconnected(self) -> Pin<Disconnected> {
248+
// Reset value is disconnected.
249+
self.conf().reset();
250+
251+
Pin {
252+
_mode: PhantomData,
253+
pin_port: self.pin_port,
254+
}
255+
}
239256
}
240257

241258
impl<MODE> InputPin for Pin<Input<MODE>> {
@@ -489,6 +506,19 @@ macro_rules! gpio {
489506
pin
490507
}
491508

509+
/// Disconnects the pin.
510+
///
511+
/// In disconnected mode the pin cannot be used as input or output.
512+
/// It is primarily useful to reduce power usage.
513+
pub fn into_disconnected(self) -> $PXi<Disconnected> {
514+
// Reset value is disconnected.
515+
unsafe { &(*$PX::ptr()).pin_cnf[$i] }.reset();
516+
517+
$PXi {
518+
_mode: PhantomData,
519+
}
520+
}
521+
492522
/// Degrade to a generic pin struct, which can be used with peripherals
493523
pub fn degrade(self) -> Pin<MODE> {
494524
Pin::new($port_value, $i)
@@ -552,58 +582,58 @@ macro_rules! gpio {
552582
// 32-bit GPIO port (P0)
553583
// ===========================================================================
554584
gpio!(P0, p0, p0, Port::Port0, [
555-
P0_00: (p0_00, 0, Input<Disconnected>),
556-
P0_01: (p0_01, 1, Input<Disconnected>),
557-
P0_02: (p0_02, 2, Input<Disconnected>),
558-
P0_03: (p0_03, 3, Input<Disconnected>),
559-
P0_04: (p0_04, 4, Input<Disconnected>),
560-
P0_05: (p0_05, 5, Input<Disconnected>),
561-
P0_06: (p0_06, 6, Input<Disconnected>),
562-
P0_07: (p0_07, 7, Input<Disconnected>),
563-
P0_08: (p0_08, 8, Input<Disconnected>),
564-
P0_09: (p0_09, 9, Input<Disconnected>),
565-
P0_10: (p0_10, 10, Input<Disconnected>),
566-
P0_11: (p0_11, 11, Input<Disconnected>),
567-
P0_12: (p0_12, 12, Input<Disconnected>),
568-
P0_13: (p0_13, 13, Input<Disconnected>),
569-
P0_14: (p0_14, 14, Input<Disconnected>),
570-
P0_15: (p0_15, 15, Input<Disconnected>),
571-
P0_16: (p0_16, 16, Input<Disconnected>),
572-
P0_17: (p0_17, 17, Input<Disconnected>),
573-
P0_18: (p0_18, 18, Input<Disconnected>),
574-
P0_19: (p0_19, 19, Input<Disconnected>),
575-
P0_20: (p0_20, 20, Input<Disconnected>),
576-
P0_21: (p0_21, 21, Input<Disconnected>),
577-
P0_22: (p0_22, 22, Input<Disconnected>),
578-
P0_23: (p0_23, 23, Input<Disconnected>),
579-
P0_24: (p0_24, 24, Input<Disconnected>),
580-
P0_25: (p0_25, 25, Input<Disconnected>),
581-
P0_26: (p0_26, 26, Input<Disconnected>),
582-
P0_27: (p0_27, 27, Input<Disconnected>),
583-
P0_28: (p0_28, 28, Input<Disconnected>),
584-
P0_29: (p0_29, 29, Input<Disconnected>),
585-
P0_30: (p0_30, 30, Input<Disconnected>),
586-
P0_31: (p0_31, 31, Input<Disconnected>),
585+
P0_00: (p0_00, 0, Disconnected),
586+
P0_01: (p0_01, 1, Disconnected),
587+
P0_02: (p0_02, 2, Disconnected),
588+
P0_03: (p0_03, 3, Disconnected),
589+
P0_04: (p0_04, 4, Disconnected),
590+
P0_05: (p0_05, 5, Disconnected),
591+
P0_06: (p0_06, 6, Disconnected),
592+
P0_07: (p0_07, 7, Disconnected),
593+
P0_08: (p0_08, 8, Disconnected),
594+
P0_09: (p0_09, 9, Disconnected),
595+
P0_10: (p0_10, 10, Disconnected),
596+
P0_11: (p0_11, 11, Disconnected),
597+
P0_12: (p0_12, 12, Disconnected),
598+
P0_13: (p0_13, 13, Disconnected),
599+
P0_14: (p0_14, 14, Disconnected),
600+
P0_15: (p0_15, 15, Disconnected),
601+
P0_16: (p0_16, 16, Disconnected),
602+
P0_17: (p0_17, 17, Disconnected),
603+
P0_18: (p0_18, 18, Disconnected),
604+
P0_19: (p0_19, 19, Disconnected),
605+
P0_20: (p0_20, 20, Disconnected),
606+
P0_21: (p0_21, 21, Disconnected),
607+
P0_22: (p0_22, 22, Disconnected),
608+
P0_23: (p0_23, 23, Disconnected),
609+
P0_24: (p0_24, 24, Disconnected),
610+
P0_25: (p0_25, 25, Disconnected),
611+
P0_26: (p0_26, 26, Disconnected),
612+
P0_27: (p0_27, 27, Disconnected),
613+
P0_28: (p0_28, 28, Disconnected),
614+
P0_29: (p0_29, 29, Disconnected),
615+
P0_30: (p0_30, 30, Disconnected),
616+
P0_31: (p0_31, 31, Disconnected),
587617
]);
588618

589619
// The p1 types are present in the p0 module generated from the
590620
// svd, but we want to export them in a p1 module from this crate.
591621
#[cfg(any(feature = "52833", feature = "52840"))]
592622
gpio!(P1, p0, p1, Port::Port1, [
593-
P1_00: (p1_00, 0, Input<Disconnected>),
594-
P1_01: (p1_01, 1, Input<Disconnected>),
595-
P1_02: (p1_02, 2, Input<Disconnected>),
596-
P1_03: (p1_03, 3, Input<Disconnected>),
597-
P1_04: (p1_04, 4, Input<Disconnected>),
598-
P1_05: (p1_05, 5, Input<Disconnected>),
599-
P1_06: (p1_06, 6, Input<Disconnected>),
600-
P1_07: (p1_07, 7, Input<Disconnected>),
601-
P1_08: (p1_08, 8, Input<Disconnected>),
602-
P1_09: (p1_09, 9, Input<Disconnected>),
603-
P1_10: (p1_10, 10, Input<Disconnected>),
604-
P1_11: (p1_11, 11, Input<Disconnected>),
605-
P1_12: (p1_12, 12, Input<Disconnected>),
606-
P1_13: (p1_13, 13, Input<Disconnected>),
607-
P1_14: (p1_14, 14, Input<Disconnected>),
608-
P1_15: (p1_15, 15, Input<Disconnected>),
623+
P1_00: (p1_00, 0, Disconnected),
624+
P1_01: (p1_01, 1, Disconnected),
625+
P1_02: (p1_02, 2, Disconnected),
626+
P1_03: (p1_03, 3, Disconnected),
627+
P1_04: (p1_04, 4, Disconnected),
628+
P1_05: (p1_05, 5, Disconnected),
629+
P1_06: (p1_06, 6, Disconnected),
630+
P1_07: (p1_07, 7, Disconnected),
631+
P1_08: (p1_08, 8, Disconnected),
632+
P1_09: (p1_09, 9, Disconnected),
633+
P1_10: (p1_10, 10, Disconnected),
634+
P1_11: (p1_11, 11, Disconnected),
635+
P1_12: (p1_12, 12, Disconnected),
636+
P1_13: (p1_13, 13, Disconnected),
637+
P1_14: (p1_14, 14, Disconnected),
638+
P1_15: (p1_15, 15, Disconnected),
609639
]);

nrf-hal-common/src/gpiote.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,15 @@ fn config_channel_task_pin<P: GpioteOutputPin>(
361361
TaskOutPolarity::Clear => w.polarity().hi_to_lo(),
362362
TaskOutPolarity::Toggle => w.polarity().toggle(),
363363
};
364+
365+
#[cfg(any(feature = "52833", feature = "52840"))]
366+
{
367+
match pin.port() {
368+
Port::Port0 => w.port().clear_bit(),
369+
Port::Port1 => w.port().set_bit(),
370+
};
371+
}
372+
364373
unsafe { w.psel().bits(pin.pin()) }
365374
});
366375
}
@@ -421,16 +430,23 @@ impl GpioteInputPin for Pin<Input<Floating>> {
421430
/// Trait to represent task output pin.
422431
pub trait GpioteOutputPin {
423432
fn pin(&self) -> u8;
433+
fn port(&self) -> Port;
424434
}
425435

426436
impl GpioteOutputPin for Pin<Output<OpenDrain>> {
427437
fn pin(&self) -> u8 {
428438
self.pin()
429439
}
440+
fn port(&self) -> Port {
441+
self.port()
442+
}
430443
}
431444

432445
impl GpioteOutputPin for Pin<Output<PushPull>> {
433446
fn pin(&self) -> u8 {
434447
self.pin()
435448
}
449+
fn port(&self) -> Port {
450+
self.port()
451+
}
436452
}

nrf-hal-common/src/i2s.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ impl I2S {
267267
pub fn rx<W, B>(mut self, mut buffer: B) -> Result<Transfer<B>, Error>
268268
where
269269
W: SupportedWordSize,
270-
B: WriteBuffer<Word = W>,
270+
B: WriteBuffer<Word = W> + 'static,
271271
{
272272
let (ptr, len) = unsafe { buffer.write_buffer() };
273273
if ptr as u32 % 4 != 0 {
@@ -300,8 +300,8 @@ impl I2S {
300300
) -> Result<TransferFullDuplex<TxB, RxB>, Error>
301301
where
302302
W: SupportedWordSize,
303-
TxB: ReadBuffer<Word = W>,
304-
RxB: WriteBuffer<Word = W>,
303+
TxB: ReadBuffer<Word = W> + 'static,
304+
RxB: WriteBuffer<Word = W> + 'static,
305305
{
306306
let (rx_ptr, rx_len) = unsafe { rx_buffer.write_buffer() };
307307
let (tx_ptr, tx_len) = unsafe { tx_buffer.read_buffer() };
@@ -345,7 +345,7 @@ impl I2S {
345345
pub fn tx<W, B>(mut self, buffer: B) -> Result<Transfer<B>, Error>
346346
where
347347
W: SupportedWordSize,
348-
B: ReadBuffer<Word = W>,
348+
B: ReadBuffer<Word = W> + 'static,
349349
{
350350
let (ptr, len) = unsafe { buffer.read_buffer() };
351351
if ptr as u32 % 4 != 0 {
@@ -602,6 +602,7 @@ pub enum I2SEvent {
602602

603603
/// A DMA transfer
604604
pub struct Transfer<B> {
605+
// FIXME: Always `Some`, only using `Option` here to allow moving fields out of `inner`.
605606
inner: Option<Inner<B>>,
606607
}
607608

@@ -635,6 +636,7 @@ impl<B> Drop for Transfer<B> {
635636
}
636637
/// A full duplex DMA transfer
637638
pub struct TransferFullDuplex<TxB, RxB> {
639+
// FIXME: Always `Some`, only using `Option` here to allow moving fields out of `inner`.
638640
inner: Option<InnerFullDuplex<TxB, RxB>>,
639641
}
640642

nrf-hal-common/src/ppi/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ pub trait Ppi {
8282
}
8383

8484
/// Traits that extends the [Ppi](trait.Ppi.html) trait, marking a channel as fully configurable.
85-
pub trait ConfigurablePpi {
85+
pub trait ConfigurablePpi: Ppi {
8686
/// Sets the task that must be triggered when the configured event occurs. The user must provide
8787
/// a reference to the task.
8888
fn set_task_endpoint<T: Task>(&mut self, task: &T);

0 commit comments

Comments
 (0)