aboutsummaryrefslogtreecommitdiff
path: root/embassy-nrf/src/uarte.rs
diff options
context:
space:
mode:
authorDario Nieuwenhuis <[email protected]>2025-03-26 16:01:37 +0100
committerDario Nieuwenhuis <[email protected]>2025-03-27 15:18:06 +0100
commitd41eeeae79388f219bf6a84e2f7bde9f6b532516 (patch)
tree678b6fc732216e529dc38e6f65b72a309917ac32 /embassy-nrf/src/uarte.rs
parent9edf5b7f049f95742b60b041e4443967d8a6b708 (diff)
Remove Peripheral trait, rename PeripheralRef->Peri.
Diffstat (limited to 'embassy-nrf/src/uarte.rs')
-rw-r--r--embassy-nrf/src/uarte.rs124
1 files changed, 49 insertions, 75 deletions
diff --git a/embassy-nrf/src/uarte.rs b/embassy-nrf/src/uarte.rs
index ebb4dd941..b44edfe84 100644
--- a/embassy-nrf/src/uarte.rs
+++ b/embassy-nrf/src/uarte.rs
@@ -19,7 +19,7 @@ use core::sync::atomic::{compiler_fence, AtomicU8, Ordering};
19use core::task::Poll; 19use core::task::Poll;
20 20
21use embassy_hal_internal::drop::OnDrop; 21use embassy_hal_internal::drop::OnDrop;
22use embassy_hal_internal::{into_ref, PeripheralRef}; 22use embassy_hal_internal::{Peri, PeripheralType};
23use embassy_sync::waitqueue::AtomicWaker; 23use embassy_sync::waitqueue::AtomicWaker;
24// Re-export SVD variants to allow user to directly set values. 24// Re-export SVD variants to allow user to directly set values.
25pub use pac::uarte::vals::{Baudrate, ConfigParity as Parity}; 25pub use pac::uarte::vals::{Baudrate, ConfigParity as Parity};
@@ -32,7 +32,7 @@ use crate::pac::uarte::vals;
32use crate::ppi::{AnyConfigurableChannel, ConfigurableChannel, Event, Ppi, Task}; 32use crate::ppi::{AnyConfigurableChannel, ConfigurableChannel, Event, Ppi, Task};
33use crate::timer::{Frequency, Instance as TimerInstance, Timer}; 33use crate::timer::{Frequency, Instance as TimerInstance, Timer};
34use crate::util::slice_in_ram_or; 34use crate::util::slice_in_ram_or;
35use crate::{interrupt, pac, Peripheral}; 35use crate::{interrupt, pac};
36 36
37/// UARTE config. 37/// UARTE config.
38#[derive(Clone)] 38#[derive(Clone)]
@@ -141,56 +141,54 @@ pub struct Uarte<'d, T: Instance> {
141/// 141///
142/// This can be obtained via [`Uarte::split`], or created directly. 142/// This can be obtained via [`Uarte::split`], or created directly.
143pub struct UarteTx<'d, T: Instance> { 143pub struct UarteTx<'d, T: Instance> {
144 _p: PeripheralRef<'d, T>, 144 _p: Peri<'d, T>,
145} 145}
146 146
147/// Receiver part of the UARTE driver. 147/// Receiver part of the UARTE driver.
148/// 148///
149/// This can be obtained via [`Uarte::split`], or created directly. 149/// This can be obtained via [`Uarte::split`], or created directly.
150pub struct UarteRx<'d, T: Instance> { 150pub struct UarteRx<'d, T: Instance> {
151 _p: PeripheralRef<'d, T>, 151 _p: Peri<'d, T>,
152} 152}
153 153
154impl<'d, T: Instance> Uarte<'d, T> { 154impl<'d, T: Instance> Uarte<'d, T> {
155 /// Create a new UARTE without hardware flow control 155 /// Create a new UARTE without hardware flow control
156 pub fn new( 156 pub fn new(
157 uarte: impl Peripheral<P = T> + 'd, 157 uarte: Peri<'d, T>,
158 _irq: impl interrupt::typelevel::Binding<T::Interrupt, InterruptHandler<T>> + 'd, 158 _irq: impl interrupt::typelevel::Binding<T::Interrupt, InterruptHandler<T>> + 'd,
159 rxd: impl Peripheral<P = impl GpioPin> + 'd, 159 rxd: Peri<'d, impl GpioPin>,
160 txd: impl Peripheral<P = impl GpioPin> + 'd, 160 txd: Peri<'d, impl GpioPin>,
161 config: Config, 161 config: Config,
162 ) -> Self { 162 ) -> Self {
163 into_ref!(uarte, rxd, txd); 163 Self::new_inner(uarte, rxd.into(), txd.into(), None, None, config)
164 Self::new_inner(uarte, rxd.map_into(), txd.map_into(), None, None, config)
165 } 164 }
166 165
167 /// Create a new UARTE with hardware flow control (RTS/CTS) 166 /// Create a new UARTE with hardware flow control (RTS/CTS)
168 pub fn new_with_rtscts( 167 pub fn new_with_rtscts(
169 uarte: impl Peripheral<P = T> + 'd, 168 uarte: Peri<'d, T>,
170 _irq: impl interrupt::typelevel::Binding<T::Interrupt, InterruptHandler<T>> + 'd, 169 _irq: impl interrupt::typelevel::Binding<T::Interrupt, InterruptHandler<T>> + 'd,
171 rxd: impl Peripheral<P = impl GpioPin> + 'd, 170 rxd: Peri<'d, impl GpioPin>,
172 txd: impl Peripheral<P = impl GpioPin> + 'd, 171 txd: Peri<'d, impl GpioPin>,
173 cts: impl Peripheral<P = impl GpioPin> + 'd, 172 cts: Peri<'d, impl GpioPin>,
174 rts: impl Peripheral<P = impl GpioPin> + 'd, 173 rts: Peri<'d, impl GpioPin>,
175 config: Config, 174 config: Config,
176 ) -> Self { 175 ) -> Self {
177 into_ref!(uarte, rxd, txd, cts, rts);
178 Self::new_inner( 176 Self::new_inner(
179 uarte, 177 uarte,
180 rxd.map_into(), 178 rxd.into(),
181 txd.map_into(), 179 txd.into(),
182 Some(cts.map_into()), 180 Some(cts.into()),
183 Some(rts.map_into()), 181 Some(rts.into()),
184 config, 182 config,
185 ) 183 )
186 } 184 }
187 185
188 fn new_inner( 186 fn new_inner(
189 uarte: PeripheralRef<'d, T>, 187 uarte: Peri<'d, T>,
190 rxd: PeripheralRef<'d, AnyPin>, 188 rxd: Peri<'d, AnyPin>,
191 txd: PeripheralRef<'d, AnyPin>, 189 txd: Peri<'d, AnyPin>,
192 cts: Option<PeripheralRef<'d, AnyPin>>, 190 cts: Option<Peri<'d, AnyPin>>,
193 rts: Option<PeripheralRef<'d, AnyPin>>, 191 rts: Option<Peri<'d, AnyPin>>,
194 config: Config, 192 config: Config,
195 ) -> Self { 193 ) -> Self {
196 let r = T::regs(); 194 let r = T::regs();
@@ -239,9 +237,9 @@ impl<'d, T: Instance> Uarte<'d, T> {
239 /// This is useful to concurrently transmit and receive from independent tasks. 237 /// This is useful to concurrently transmit and receive from independent tasks.
240 pub fn split_with_idle<U: TimerInstance>( 238 pub fn split_with_idle<U: TimerInstance>(
241 self, 239 self,
242 timer: impl Peripheral<P = U> + 'd, 240 timer: Peri<'d, U>,
243 ppi_ch1: impl Peripheral<P = impl ConfigurableChannel + 'd> + 'd, 241 ppi_ch1: Peri<'d, impl ConfigurableChannel + 'd>,
244 ppi_ch2: impl Peripheral<P = impl ConfigurableChannel + 'd> + 'd, 242 ppi_ch2: Peri<'d, impl ConfigurableChannel + 'd>,
245 ) -> (UarteTx<'d, T>, UarteRxWithIdle<'d, T, U>) { 243 ) -> (UarteTx<'d, T>, UarteRxWithIdle<'d, T, U>) {
246 (self.tx, self.rx.with_idle(timer, ppi_ch1, ppi_ch2)) 244 (self.tx, self.rx.with_idle(timer, ppi_ch1, ppi_ch2))
247 } 245 }
@@ -283,11 +281,7 @@ impl<'d, T: Instance> Uarte<'d, T> {
283 } 281 }
284} 282}
285 283
286pub(crate) fn configure_tx_pins( 284pub(crate) fn configure_tx_pins(r: pac::uarte::Uarte, txd: Peri<'_, AnyPin>, cts: Option<Peri<'_, AnyPin>>) {
287 r: pac::uarte::Uarte,
288 txd: PeripheralRef<'_, AnyPin>,
289 cts: Option<PeripheralRef<'_, AnyPin>>,
290) {
291 txd.set_high(); 285 txd.set_high();
292 txd.conf().write(|w| { 286 txd.conf().write(|w| {
293 w.set_dir(gpiovals::Dir::OUTPUT); 287 w.set_dir(gpiovals::Dir::OUTPUT);
@@ -306,11 +300,7 @@ pub(crate) fn configure_tx_pins(
306 r.psel().cts().write_value(cts.psel_bits()); 300 r.psel().cts().write_value(cts.psel_bits());
307} 301}
308 302
309pub(crate) fn configure_rx_pins( 303pub(crate) fn configure_rx_pins(r: pac::uarte::Uarte, rxd: Peri<'_, AnyPin>, rts: Option<Peri<'_, AnyPin>>) {
310 r: pac::uarte::Uarte,
311 rxd: PeripheralRef<'_, AnyPin>,
312 rts: Option<PeripheralRef<'_, AnyPin>>,
313) {
314 rxd.conf().write(|w| { 304 rxd.conf().write(|w| {
315 w.set_dir(gpiovals::Dir::INPUT); 305 w.set_dir(gpiovals::Dir::INPUT);
316 w.set_input(gpiovals::Input::CONNECT); 306 w.set_input(gpiovals::Input::CONNECT);
@@ -356,33 +346,26 @@ pub(crate) fn configure(r: pac::uarte::Uarte, config: Config, hardware_flow_cont
356impl<'d, T: Instance> UarteTx<'d, T> { 346impl<'d, T: Instance> UarteTx<'d, T> {
357 /// Create a new tx-only UARTE without hardware flow control 347 /// Create a new tx-only UARTE without hardware flow control
358 pub fn new( 348 pub fn new(
359 uarte: impl Peripheral<P = T> + 'd, 349 uarte: Peri<'d, T>,
360 _irq: impl interrupt::typelevel::Binding<T::Interrupt, InterruptHandler<T>> + 'd, 350 _irq: impl interrupt::typelevel::Binding<T::Interrupt, InterruptHandler<T>> + 'd,
361 txd: impl Peripheral<P = impl GpioPin> + 'd, 351 txd: Peri<'d, impl GpioPin>,
362 config: Config, 352 config: Config,
363 ) -> Self { 353 ) -> Self {
364 into_ref!(uarte, txd); 354 Self::new_inner(uarte, txd.into(), None, config)
365 Self::new_inner(uarte, txd.map_into(), None, config)
366 } 355 }
367 356
368 /// Create a new tx-only UARTE with hardware flow control (RTS/CTS) 357 /// Create a new tx-only UARTE with hardware flow control (RTS/CTS)
369 pub fn new_with_rtscts( 358 pub fn new_with_rtscts(
370 uarte: impl Peripheral<P = T> + 'd, 359 uarte: Peri<'d, T>,
371 _irq: impl interrupt::typelevel::Binding<T::Interrupt, InterruptHandler<T>> + 'd, 360 _irq: impl interrupt::typelevel::Binding<T::Interrupt, InterruptHandler<T>> + 'd,
372 txd: impl Peripheral<P = impl GpioPin> + 'd, 361 txd: Peri<'d, impl GpioPin>,
373 cts: impl Peripheral<P = impl GpioPin> + 'd, 362 cts: Peri<'d, impl GpioPin>,
374 config: Config, 363 config: Config,
375 ) -> Self { 364 ) -> Self {
376 into_ref!(uarte, txd, cts); 365 Self::new_inner(uarte, txd.into(), Some(cts.into()), config)
377 Self::new_inner(uarte, txd.map_into(), Some(cts.map_into()), config)
378 } 366 }
379 367
380 fn new_inner( 368 fn new_inner(uarte: Peri<'d, T>, txd: Peri<'d, AnyPin>, cts: Option<Peri<'d, AnyPin>>, config: Config) -> Self {
381 uarte: PeripheralRef<'d, T>,
382 txd: PeripheralRef<'d, AnyPin>,
383 cts: Option<PeripheralRef<'d, AnyPin>>,
384 config: Config,
385 ) -> Self {
386 let r = T::regs(); 369 let r = T::regs();
387 370
388 configure(r, config, cts.is_some()); 371 configure(r, config, cts.is_some());
@@ -539,25 +522,23 @@ impl<'a, T: Instance> Drop for UarteTx<'a, T> {
539impl<'d, T: Instance> UarteRx<'d, T> { 522impl<'d, T: Instance> UarteRx<'d, T> {
540 /// Create a new rx-only UARTE without hardware flow control 523 /// Create a new rx-only UARTE without hardware flow control
541 pub fn new( 524 pub fn new(
542 uarte: impl Peripheral<P = T> + 'd, 525 uarte: Peri<'d, T>,
543 _irq: impl interrupt::typelevel::Binding<T::Interrupt, InterruptHandler<T>> + 'd, 526 _irq: impl interrupt::typelevel::Binding<T::Interrupt, InterruptHandler<T>> + 'd,
544 rxd: impl Peripheral<P = impl GpioPin> + 'd, 527 rxd: Peri<'d, impl GpioPin>,
545 config: Config, 528 config: Config,
546 ) -> Self { 529 ) -> Self {
547 into_ref!(uarte, rxd); 530 Self::new_inner(uarte, rxd.into(), None, config)
548 Self::new_inner(uarte, rxd.map_into(), None, config)
549 } 531 }
550 532
551 /// Create a new rx-only UARTE with hardware flow control (RTS/CTS) 533 /// Create a new rx-only UARTE with hardware flow control (RTS/CTS)
552 pub fn new_with_rtscts( 534 pub fn new_with_rtscts(
553 uarte: impl Peripheral<P = T> + 'd, 535 uarte: Peri<'d, T>,
554 _irq: impl interrupt::typelevel::Binding<T::Interrupt, InterruptHandler<T>> + 'd, 536 _irq: impl interrupt::typelevel::Binding<T::Interrupt, InterruptHandler<T>> + 'd,
555 rxd: impl Peripheral<P = impl GpioPin> + 'd, 537 rxd: Peri<'d, impl GpioPin>,
556 rts: impl Peripheral<P = impl GpioPin> + 'd, 538 rts: Peri<'d, impl GpioPin>,
557 config: Config, 539 config: Config,
558 ) -> Self { 540 ) -> Self {
559 into_ref!(uarte, rxd, rts); 541 Self::new_inner(uarte, rxd.into(), Some(rts.into()), config)
560 Self::new_inner(uarte, rxd.map_into(), Some(rts.map_into()), config)
561 } 542 }
562 543
563 /// Check for errors and clear the error register if an error occured. 544 /// Check for errors and clear the error register if an error occured.
@@ -568,12 +549,7 @@ impl<'d, T: Instance> UarteRx<'d, T> {
568 ErrorSource::from_bits_truncate(err_bits.0).check() 549 ErrorSource::from_bits_truncate(err_bits.0).check()
569 } 550 }
570 551
571 fn new_inner( 552 fn new_inner(uarte: Peri<'d, T>, rxd: Peri<'d, AnyPin>, rts: Option<Peri<'d, AnyPin>>, config: Config) -> Self {
572 uarte: PeripheralRef<'d, T>,
573 rxd: PeripheralRef<'d, AnyPin>,
574 rts: Option<PeripheralRef<'d, AnyPin>>,
575 config: Config,
576 ) -> Self {
577 let r = T::regs(); 553 let r = T::regs();
578 554
579 configure(r, config, rts.is_some()); 555 configure(r, config, rts.is_some());
@@ -592,14 +568,12 @@ impl<'d, T: Instance> UarteRx<'d, T> {
592 /// Upgrade to an instance that supports idle line detection. 568 /// Upgrade to an instance that supports idle line detection.
593 pub fn with_idle<U: TimerInstance>( 569 pub fn with_idle<U: TimerInstance>(
594 self, 570 self,
595 timer: impl Peripheral<P = U> + 'd, 571 timer: Peri<'d, U>,
596 ppi_ch1: impl Peripheral<P = impl ConfigurableChannel + 'd> + 'd, 572 ppi_ch1: Peri<'d, impl ConfigurableChannel + 'd>,
597 ppi_ch2: impl Peripheral<P = impl ConfigurableChannel + 'd> + 'd, 573 ppi_ch2: Peri<'d, impl ConfigurableChannel + 'd>,
598 ) -> UarteRxWithIdle<'d, T, U> { 574 ) -> UarteRxWithIdle<'d, T, U> {
599 let timer = Timer::new(timer); 575 let timer = Timer::new(timer);
600 576
601 into_ref!(ppi_ch1, ppi_ch2);
602
603 let r = T::regs(); 577 let r = T::regs();
604 578
605 // BAUDRATE register values are `baudrate * 2^32 / 16000000` 579 // BAUDRATE register values are `baudrate * 2^32 / 16000000`
@@ -617,7 +591,7 @@ impl<'d, T: Instance> UarteRx<'d, T> {
617 timer.cc(0).short_compare_stop(); 591 timer.cc(0).short_compare_stop();
618 592
619 let mut ppi_ch1 = Ppi::new_one_to_two( 593 let mut ppi_ch1 = Ppi::new_one_to_two(
620 ppi_ch1.map_into(), 594 ppi_ch1.into(),
621 Event::from_reg(r.events_rxdrdy()), 595 Event::from_reg(r.events_rxdrdy()),
622 timer.task_clear(), 596 timer.task_clear(),
623 timer.task_start(), 597 timer.task_start(),
@@ -625,7 +599,7 @@ impl<'d, T: Instance> UarteRx<'d, T> {
625 ppi_ch1.enable(); 599 ppi_ch1.enable();
626 600
627 let mut ppi_ch2 = Ppi::new_one_to_one( 601 let mut ppi_ch2 = Ppi::new_one_to_one(
628 ppi_ch2.map_into(), 602 ppi_ch2.into(),
629 timer.cc(0).event_compare(), 603 timer.cc(0).event_compare(),
630 Task::from_reg(r.tasks_stoprx()), 604 Task::from_reg(r.tasks_stoprx()),
631 ); 605 );
@@ -992,7 +966,7 @@ pub(crate) trait SealedInstance {
992 966
993/// UARTE peripheral instance. 967/// UARTE peripheral instance.
994#[allow(private_bounds)] 968#[allow(private_bounds)]
995pub trait Instance: Peripheral<P = Self> + SealedInstance + 'static + Send { 969pub trait Instance: SealedInstance + PeripheralType + 'static + Send {
996 /// Interrupt for this peripheral. 970 /// Interrupt for this peripheral.
997 type Interrupt: interrupt::typelevel::Interrupt; 971 type Interrupt: interrupt::typelevel::Interrupt;
998} 972}