diff options
| author | Dario Nieuwenhuis <[email protected]> | 2025-09-27 18:37:33 +0200 |
|---|---|---|
| committer | Dario Nieuwenhuis <[email protected]> | 2025-09-28 21:05:37 +0200 |
| commit | b07192079f0fc6ce210104786540aa7be8938d40 (patch) | |
| tree | 659f336f177367fb82757c5b775c0f6c63894eb9 /embassy-nrf | |
| parent | e5328c78259c7e288bf54c83bc80c2d2311abdf2 (diff) | |
nrf/uart,timer: erase instance generics.
Diffstat (limited to 'embassy-nrf')
| -rw-r--r-- | embassy-nrf/src/buffered_uarte.rs | 229 | ||||
| -rw-r--r-- | embassy-nrf/src/timer.rs | 82 | ||||
| -rw-r--r-- | embassy-nrf/src/twim.rs | 2 | ||||
| -rw-r--r-- | embassy-nrf/src/uarte.rs | 148 |
4 files changed, 272 insertions, 189 deletions
diff --git a/embassy-nrf/src/buffered_uarte.rs b/embassy-nrf/src/buffered_uarte.rs index 29e126903..40c679190 100644 --- a/embassy-nrf/src/buffered_uarte.rs +++ b/embassy-nrf/src/buffered_uarte.rs | |||
| @@ -23,6 +23,7 @@ pub use pac::uarte::vals::{Baudrate, ConfigParity as Parity}; | |||
| 23 | 23 | ||
| 24 | use crate::gpio::{AnyPin, Pin as GpioPin}; | 24 | use crate::gpio::{AnyPin, Pin as GpioPin}; |
| 25 | use crate::interrupt::typelevel::Interrupt; | 25 | use crate::interrupt::typelevel::Interrupt; |
| 26 | use crate::interrupt::InterruptExt; | ||
| 26 | use crate::ppi::{ | 27 | use crate::ppi::{ |
| 27 | self, AnyConfigurableChannel, AnyGroup, Channel, ConfigurableChannel, Event, Group, Ppi, PpiGroup, Task, | 28 | self, AnyConfigurableChannel, AnyGroup, Channel, ConfigurableChannel, Event, Group, Ppi, PpiGroup, Task, |
| 28 | }; | 29 | }; |
| @@ -207,21 +208,21 @@ impl<U: UarteInstance> interrupt::typelevel::Handler<U::Interrupt> for Interrupt | |||
| 207 | } | 208 | } |
| 208 | 209 | ||
| 209 | /// Buffered UARTE driver. | 210 | /// Buffered UARTE driver. |
| 210 | pub struct BufferedUarte<'d, U: UarteInstance, T: TimerInstance> { | 211 | pub struct BufferedUarte<'d> { |
| 211 | tx: BufferedUarteTx<'d, U>, | 212 | tx: BufferedUarteTx<'d>, |
| 212 | rx: BufferedUarteRx<'d, U, T>, | 213 | rx: BufferedUarteRx<'d>, |
| 213 | } | 214 | } |
| 214 | 215 | ||
| 215 | impl<'d, U: UarteInstance, T: TimerInstance> Unpin for BufferedUarte<'d, U, T> {} | 216 | impl<'d> Unpin for BufferedUarte<'d> {} |
| 216 | 217 | ||
| 217 | impl<'d, U: UarteInstance, T: TimerInstance> BufferedUarte<'d, U, T> { | 218 | impl<'d> BufferedUarte<'d> { |
| 218 | /// Create a new BufferedUarte without hardware flow control. | 219 | /// Create a new BufferedUarte without hardware flow control. |
| 219 | /// | 220 | /// |
| 220 | /// # Panics | 221 | /// # Panics |
| 221 | /// | 222 | /// |
| 222 | /// Panics if `rx_buffer.len()` is odd. | 223 | /// Panics if `rx_buffer.len()` is odd. |
| 223 | #[allow(clippy::too_many_arguments)] | 224 | #[allow(clippy::too_many_arguments)] |
| 224 | pub fn new( | 225 | pub fn new<U: UarteInstance, T: TimerInstance>( |
| 225 | uarte: Peri<'d, U>, | 226 | uarte: Peri<'d, U>, |
| 226 | timer: Peri<'d, T>, | 227 | timer: Peri<'d, T>, |
| 227 | ppi_ch1: Peri<'d, impl ConfigurableChannel>, | 228 | ppi_ch1: Peri<'d, impl ConfigurableChannel>, |
| @@ -256,7 +257,7 @@ impl<'d, U: UarteInstance, T: TimerInstance> BufferedUarte<'d, U, T> { | |||
| 256 | /// | 257 | /// |
| 257 | /// Panics if `rx_buffer.len()` is odd. | 258 | /// Panics if `rx_buffer.len()` is odd. |
| 258 | #[allow(clippy::too_many_arguments)] | 259 | #[allow(clippy::too_many_arguments)] |
| 259 | pub fn new_with_rtscts( | 260 | pub fn new_with_rtscts<U: UarteInstance, T: TimerInstance>( |
| 260 | uarte: Peri<'d, U>, | 261 | uarte: Peri<'d, U>, |
| 261 | timer: Peri<'d, T>, | 262 | timer: Peri<'d, T>, |
| 262 | ppi_ch1: Peri<'d, impl ConfigurableChannel>, | 263 | ppi_ch1: Peri<'d, impl ConfigurableChannel>, |
| @@ -288,7 +289,7 @@ impl<'d, U: UarteInstance, T: TimerInstance> BufferedUarte<'d, U, T> { | |||
| 288 | } | 289 | } |
| 289 | 290 | ||
| 290 | #[allow(clippy::too_many_arguments)] | 291 | #[allow(clippy::too_many_arguments)] |
| 291 | fn new_inner( | 292 | fn new_inner<U: UarteInstance, T: TimerInstance>( |
| 292 | peri: Peri<'d, U>, | 293 | peri: Peri<'d, U>, |
| 293 | timer: Peri<'d, T>, | 294 | timer: Peri<'d, T>, |
| 294 | ppi_ch1: Peri<'d, AnyConfigurableChannel>, | 295 | ppi_ch1: Peri<'d, AnyConfigurableChannel>, |
| @@ -302,30 +303,33 @@ impl<'d, U: UarteInstance, T: TimerInstance> BufferedUarte<'d, U, T> { | |||
| 302 | rx_buffer: &'d mut [u8], | 303 | rx_buffer: &'d mut [u8], |
| 303 | tx_buffer: &'d mut [u8], | 304 | tx_buffer: &'d mut [u8], |
| 304 | ) -> Self { | 305 | ) -> Self { |
| 305 | configure(U::regs(), config, cts.is_some()); | 306 | let r = U::regs(); |
| 307 | let irq = U::Interrupt::IRQ; | ||
| 308 | let state = U::state(); | ||
| 309 | |||
| 310 | configure(r, config, cts.is_some()); | ||
| 306 | 311 | ||
| 307 | let tx = BufferedUarteTx::new_innerer(unsafe { peri.clone_unchecked() }, txd, cts, tx_buffer); | 312 | let tx = BufferedUarteTx::new_innerer(unsafe { peri.clone_unchecked() }, txd, cts, tx_buffer); |
| 308 | let rx = BufferedUarteRx::new_innerer(peri, timer, ppi_ch1, ppi_ch2, ppi_group, rxd, rts, rx_buffer); | 313 | let rx = BufferedUarteRx::new_innerer(peri, timer, ppi_ch1, ppi_ch2, ppi_group, rxd, rts, rx_buffer); |
| 309 | 314 | ||
| 310 | U::regs().enable().write(|w| w.set_enable(vals::Enable::ENABLED)); | 315 | r.enable().write(|w| w.set_enable(vals::Enable::ENABLED)); |
| 311 | U::Interrupt::pend(); | 316 | irq.pend(); |
| 312 | unsafe { U::Interrupt::enable() }; | 317 | unsafe { irq.enable() }; |
| 313 | 318 | ||
| 314 | U::state().tx_rx_refcount.store(2, Ordering::Relaxed); | 319 | state.tx_rx_refcount.store(2, Ordering::Relaxed); |
| 315 | 320 | ||
| 316 | Self { tx, rx } | 321 | Self { tx, rx } |
| 317 | } | 322 | } |
| 318 | 323 | ||
| 319 | /// Adjust the baud rate to the provided value. | 324 | /// Adjust the baud rate to the provided value. |
| 320 | pub fn set_baudrate(&mut self, baudrate: Baudrate) { | 325 | pub fn set_baudrate(&mut self, baudrate: Baudrate) { |
| 321 | let r = U::regs(); | 326 | self.tx.set_baudrate(baudrate); |
| 322 | r.baudrate().write(|w| w.set_baudrate(baudrate)); | ||
| 323 | } | 327 | } |
| 324 | 328 | ||
| 325 | /// Split the UART in reader and writer parts. | 329 | /// Split the UART in reader and writer parts. |
| 326 | /// | 330 | /// |
| 327 | /// This allows reading and writing concurrently from independent tasks. | 331 | /// This allows reading and writing concurrently from independent tasks. |
| 328 | pub fn split(self) -> (BufferedUarteRx<'d, U, T>, BufferedUarteTx<'d, U>) { | 332 | pub fn split(self) -> (BufferedUarteRx<'d>, BufferedUarteTx<'d>) { |
| 329 | (self.rx, self.tx) | 333 | (self.rx, self.tx) |
| 330 | } | 334 | } |
| 331 | 335 | ||
| @@ -333,7 +337,7 @@ impl<'d, U: UarteInstance, T: TimerInstance> BufferedUarte<'d, U, T> { | |||
| 333 | /// | 337 | /// |
| 334 | /// The returned halves borrow from `self`, so you can drop them and go back to using | 338 | /// The returned halves borrow from `self`, so you can drop them and go back to using |
| 335 | /// the "un-split" `self`. This allows temporarily splitting the UART. | 339 | /// the "un-split" `self`. This allows temporarily splitting the UART. |
| 336 | pub fn split_by_ref(&mut self) -> (&mut BufferedUarteRx<'d, U, T>, &mut BufferedUarteTx<'d, U>) { | 340 | pub fn split_by_ref(&mut self) -> (&mut BufferedUarteRx<'d>, &mut BufferedUarteTx<'d>) { |
| 337 | (&mut self.rx, &mut self.tx) | 341 | (&mut self.rx, &mut self.tx) |
| 338 | } | 342 | } |
| 339 | 343 | ||
| @@ -369,13 +373,17 @@ impl<'d, U: UarteInstance, T: TimerInstance> BufferedUarte<'d, U, T> { | |||
| 369 | } | 373 | } |
| 370 | 374 | ||
| 371 | /// Reader part of the buffered UARTE driver. | 375 | /// Reader part of the buffered UARTE driver. |
| 372 | pub struct BufferedUarteTx<'d, U: UarteInstance> { | 376 | pub struct BufferedUarteTx<'d> { |
| 373 | _peri: Peri<'d, U>, | 377 | r: pac::uarte::Uarte, |
| 378 | _irq: interrupt::Interrupt, | ||
| 379 | state: &'static crate::uarte::State, | ||
| 380 | buffered_state: &'static State, | ||
| 381 | _p: PhantomData<&'d ()>, | ||
| 374 | } | 382 | } |
| 375 | 383 | ||
| 376 | impl<'d, U: UarteInstance> BufferedUarteTx<'d, U> { | 384 | impl<'d> BufferedUarteTx<'d> { |
| 377 | /// Create a new BufferedUarteTx without hardware flow control. | 385 | /// Create a new BufferedUarteTx without hardware flow control. |
| 378 | pub fn new( | 386 | pub fn new<U: UarteInstance>( |
| 379 | uarte: Peri<'d, U>, | 387 | uarte: Peri<'d, U>, |
| 380 | txd: Peri<'d, impl GpioPin>, | 388 | txd: Peri<'d, impl GpioPin>, |
| 381 | _irq: impl interrupt::typelevel::Binding<U::Interrupt, InterruptHandler<U>> + 'd, | 389 | _irq: impl interrupt::typelevel::Binding<U::Interrupt, InterruptHandler<U>> + 'd, |
| @@ -390,7 +398,7 @@ impl<'d, U: UarteInstance> BufferedUarteTx<'d, U> { | |||
| 390 | /// # Panics | 398 | /// # Panics |
| 391 | /// | 399 | /// |
| 392 | /// Panics if `rx_buffer.len()` is odd. | 400 | /// Panics if `rx_buffer.len()` is odd. |
| 393 | pub fn new_with_cts( | 401 | pub fn new_with_cts<U: UarteInstance>( |
| 394 | uarte: Peri<'d, U>, | 402 | uarte: Peri<'d, U>, |
| 395 | txd: Peri<'d, impl GpioPin>, | 403 | txd: Peri<'d, impl GpioPin>, |
| 396 | cts: Peri<'d, impl GpioPin>, | 404 | cts: Peri<'d, impl GpioPin>, |
| @@ -401,41 +409,48 @@ impl<'d, U: UarteInstance> BufferedUarteTx<'d, U> { | |||
| 401 | Self::new_inner(uarte, txd.into(), Some(cts.into()), config, tx_buffer) | 409 | Self::new_inner(uarte, txd.into(), Some(cts.into()), config, tx_buffer) |
| 402 | } | 410 | } |
| 403 | 411 | ||
| 404 | fn new_inner( | 412 | fn new_inner<U: UarteInstance>( |
| 405 | peri: Peri<'d, U>, | 413 | peri: Peri<'d, U>, |
| 406 | txd: Peri<'d, AnyPin>, | 414 | txd: Peri<'d, AnyPin>, |
| 407 | cts: Option<Peri<'d, AnyPin>>, | 415 | cts: Option<Peri<'d, AnyPin>>, |
| 408 | config: Config, | 416 | config: Config, |
| 409 | tx_buffer: &'d mut [u8], | 417 | tx_buffer: &'d mut [u8], |
| 410 | ) -> Self { | 418 | ) -> Self { |
| 411 | configure(U::regs(), config, cts.is_some()); | 419 | let r = U::regs(); |
| 420 | let irq = U::Interrupt::IRQ; | ||
| 421 | let state = U::state(); | ||
| 422 | let _buffered_state = U::buffered_state(); | ||
| 423 | |||
| 424 | configure(r, config, cts.is_some()); | ||
| 412 | 425 | ||
| 413 | let this = Self::new_innerer(peri, txd, cts, tx_buffer); | 426 | let this = Self::new_innerer(peri, txd, cts, tx_buffer); |
| 414 | 427 | ||
| 415 | U::regs().enable().write(|w| w.set_enable(vals::Enable::ENABLED)); | 428 | r.enable().write(|w| w.set_enable(vals::Enable::ENABLED)); |
| 416 | U::Interrupt::pend(); | 429 | irq.pend(); |
| 417 | unsafe { U::Interrupt::enable() }; | 430 | unsafe { irq.enable() }; |
| 418 | 431 | ||
| 419 | U::state().tx_rx_refcount.store(1, Ordering::Relaxed); | 432 | state.tx_rx_refcount.store(1, Ordering::Relaxed); |
| 420 | 433 | ||
| 421 | this | 434 | this |
| 422 | } | 435 | } |
| 423 | 436 | ||
| 424 | fn new_innerer( | 437 | fn new_innerer<U: UarteInstance>( |
| 425 | peri: Peri<'d, U>, | 438 | _peri: Peri<'d, U>, |
| 426 | txd: Peri<'d, AnyPin>, | 439 | txd: Peri<'d, AnyPin>, |
| 427 | cts: Option<Peri<'d, AnyPin>>, | 440 | cts: Option<Peri<'d, AnyPin>>, |
| 428 | tx_buffer: &'d mut [u8], | 441 | tx_buffer: &'d mut [u8], |
| 429 | ) -> Self { | 442 | ) -> Self { |
| 430 | let r = U::regs(); | 443 | let r = U::regs(); |
| 444 | let irq = U::Interrupt::IRQ; | ||
| 445 | let state = U::state(); | ||
| 446 | let buffered_state = U::buffered_state(); | ||
| 431 | 447 | ||
| 432 | configure_tx_pins(r, txd, cts); | 448 | configure_tx_pins(r, txd, cts); |
| 433 | 449 | ||
| 434 | // Initialize state | 450 | // Initialize state |
| 435 | let s = U::buffered_state(); | 451 | buffered_state.tx_count.store(0, Ordering::Relaxed); |
| 436 | s.tx_count.store(0, Ordering::Relaxed); | ||
| 437 | let len = tx_buffer.len(); | 452 | let len = tx_buffer.len(); |
| 438 | unsafe { s.tx_buf.init(tx_buffer.as_mut_ptr(), len) }; | 453 | unsafe { buffered_state.tx_buf.init(tx_buffer.as_mut_ptr(), len) }; |
| 439 | 454 | ||
| 440 | r.events_txstarted().write_value(0); | 455 | r.events_txstarted().write_value(0); |
| 441 | 456 | ||
| @@ -444,15 +459,21 @@ impl<'d, U: UarteInstance> BufferedUarteTx<'d, U> { | |||
| 444 | w.set_endtx(true); | 459 | w.set_endtx(true); |
| 445 | }); | 460 | }); |
| 446 | 461 | ||
| 447 | Self { _peri: peri } | 462 | Self { |
| 463 | r, | ||
| 464 | _irq: irq, | ||
| 465 | state, | ||
| 466 | buffered_state, | ||
| 467 | _p: PhantomData, | ||
| 468 | } | ||
| 448 | } | 469 | } |
| 449 | 470 | ||
| 450 | /// Write a buffer into this writer, returning how many bytes were written. | 471 | /// Write a buffer into this writer, returning how many bytes were written. |
| 451 | pub fn write<'a>(&'a mut self, buf: &'a [u8]) -> impl Future<Output = Result<usize, Error>> + 'a { | 472 | pub fn write<'a>(&'a mut self, buf: &'a [u8]) -> impl Future<Output = Result<usize, Error>> + 'a + use<'a, 'd> { |
| 452 | poll_fn(move |cx| { | 473 | poll_fn(move |cx| { |
| 453 | //trace!("poll_write: {:?}", buf.len()); | 474 | //trace!("poll_write: {:?}", buf.len()); |
| 454 | let ss = U::state(); | 475 | let ss = self.state; |
| 455 | let s = U::buffered_state(); | 476 | let s = self.buffered_state; |
| 456 | let mut tx = unsafe { s.tx_buf.writer() }; | 477 | let mut tx = unsafe { s.tx_buf.writer() }; |
| 457 | 478 | ||
| 458 | let tx_buf = tx.push_slice(); | 479 | let tx_buf = tx.push_slice(); |
| @@ -469,7 +490,7 @@ impl<'d, U: UarteInstance> BufferedUarteTx<'d, U> { | |||
| 469 | //trace!("poll_write: queued {:?}", n); | 490 | //trace!("poll_write: queued {:?}", n); |
| 470 | 491 | ||
| 471 | compiler_fence(Ordering::SeqCst); | 492 | compiler_fence(Ordering::SeqCst); |
| 472 | U::Interrupt::pend(); | 493 | self._irq.pend(); |
| 473 | 494 | ||
| 474 | Poll::Ready(Ok(n)) | 495 | Poll::Ready(Ok(n)) |
| 475 | }) | 496 | }) |
| @@ -478,7 +499,7 @@ impl<'d, U: UarteInstance> BufferedUarteTx<'d, U> { | |||
| 478 | /// Try writing a buffer without waiting, returning how many bytes were written. | 499 | /// Try writing a buffer without waiting, returning how many bytes were written. |
| 479 | pub fn try_write(&mut self, buf: &[u8]) -> Result<usize, Error> { | 500 | pub fn try_write(&mut self, buf: &[u8]) -> Result<usize, Error> { |
| 480 | //trace!("poll_write: {:?}", buf.len()); | 501 | //trace!("poll_write: {:?}", buf.len()); |
| 481 | let s = U::buffered_state(); | 502 | let s = self.buffered_state; |
| 482 | let mut tx = unsafe { s.tx_buf.writer() }; | 503 | let mut tx = unsafe { s.tx_buf.writer() }; |
| 483 | 504 | ||
| 484 | let tx_buf = tx.push_slice(); | 505 | let tx_buf = tx.push_slice(); |
| @@ -493,17 +514,17 @@ impl<'d, U: UarteInstance> BufferedUarteTx<'d, U> { | |||
| 493 | //trace!("poll_write: queued {:?}", n); | 514 | //trace!("poll_write: queued {:?}", n); |
| 494 | 515 | ||
| 495 | compiler_fence(Ordering::SeqCst); | 516 | compiler_fence(Ordering::SeqCst); |
| 496 | U::Interrupt::pend(); | 517 | self._irq.pend(); |
| 497 | 518 | ||
| 498 | Ok(n) | 519 | Ok(n) |
| 499 | } | 520 | } |
| 500 | 521 | ||
| 501 | /// Flush this output stream, ensuring that all intermediately buffered contents reach their destination. | 522 | /// Flush this output stream, ensuring that all intermediately buffered contents reach their destination. |
| 502 | pub fn flush(&mut self) -> impl Future<Output = Result<(), Error>> + '_ { | 523 | pub fn flush(&mut self) -> impl Future<Output = Result<(), Error>> + '_ { |
| 524 | let ss = self.state; | ||
| 525 | let s = self.buffered_state; | ||
| 503 | poll_fn(move |cx| { | 526 | poll_fn(move |cx| { |
| 504 | //trace!("poll_flush"); | 527 | //trace!("poll_flush"); |
| 505 | let ss = U::state(); | ||
| 506 | let s = U::buffered_state(); | ||
| 507 | if !s.tx_buf.is_empty() { | 528 | if !s.tx_buf.is_empty() { |
| 508 | //trace!("poll_flush: pending"); | 529 | //trace!("poll_flush: pending"); |
| 509 | ss.tx_waker.register(cx.waker()); | 530 | ss.tx_waker.register(cx.waker()); |
| @@ -513,11 +534,16 @@ impl<'d, U: UarteInstance> BufferedUarteTx<'d, U> { | |||
| 513 | Poll::Ready(Ok(())) | 534 | Poll::Ready(Ok(())) |
| 514 | }) | 535 | }) |
| 515 | } | 536 | } |
| 537 | |||
| 538 | /// Adjust the baud rate to the provided value. | ||
| 539 | pub fn set_baudrate(&mut self, baudrate: Baudrate) { | ||
| 540 | self.r.baudrate().write(|w| w.set_baudrate(baudrate)); | ||
| 541 | } | ||
| 516 | } | 542 | } |
| 517 | 543 | ||
| 518 | impl<'a, U: UarteInstance> Drop for BufferedUarteTx<'a, U> { | 544 | impl<'a> Drop for BufferedUarteTx<'a> { |
| 519 | fn drop(&mut self) { | 545 | fn drop(&mut self) { |
| 520 | let r = U::regs(); | 546 | let r = self.r; |
| 521 | 547 | ||
| 522 | r.intenclr().write(|w| { | 548 | r.intenclr().write(|w| { |
| 523 | w.set_txdrdy(true); | 549 | w.set_txdrdy(true); |
| @@ -528,31 +554,34 @@ impl<'a, U: UarteInstance> Drop for BufferedUarteTx<'a, U> { | |||
| 528 | r.tasks_stoptx().write_value(1); | 554 | r.tasks_stoptx().write_value(1); |
| 529 | while r.events_txstopped().read() == 0 {} | 555 | while r.events_txstopped().read() == 0 {} |
| 530 | 556 | ||
| 531 | let s = U::buffered_state(); | 557 | let s = self.buffered_state; |
| 532 | unsafe { s.tx_buf.deinit() } | 558 | unsafe { s.tx_buf.deinit() } |
| 533 | 559 | ||
| 534 | let s = U::state(); | 560 | let s = self.state; |
| 535 | drop_tx_rx(r, s); | 561 | drop_tx_rx(r, s); |
| 536 | } | 562 | } |
| 537 | } | 563 | } |
| 538 | 564 | ||
| 539 | /// Reader part of the buffered UARTE driver. | 565 | /// Reader part of the buffered UARTE driver. |
| 540 | pub struct BufferedUarteRx<'d, U: UarteInstance, T: TimerInstance> { | 566 | pub struct BufferedUarteRx<'d> { |
| 541 | _peri: Peri<'d, U>, | 567 | r: pac::uarte::Uarte, |
| 542 | timer: Timer<'d, T>, | 568 | state: &'static crate::uarte::State, |
| 569 | buffered_state: &'static State, | ||
| 570 | timer: Timer<'d>, | ||
| 543 | _ppi_ch1: Ppi<'d, AnyConfigurableChannel, 1, 1>, | 571 | _ppi_ch1: Ppi<'d, AnyConfigurableChannel, 1, 1>, |
| 544 | _ppi_ch2: Ppi<'d, AnyConfigurableChannel, 1, 2>, | 572 | _ppi_ch2: Ppi<'d, AnyConfigurableChannel, 1, 2>, |
| 545 | _ppi_group: PpiGroup<'d, AnyGroup>, | 573 | _ppi_group: PpiGroup<'d, AnyGroup>, |
| 574 | _p: PhantomData<&'d ()>, | ||
| 546 | } | 575 | } |
| 547 | 576 | ||
| 548 | impl<'d, U: UarteInstance, T: TimerInstance> BufferedUarteRx<'d, U, T> { | 577 | impl<'d> BufferedUarteRx<'d> { |
| 549 | /// Create a new BufferedUarte without hardware flow control. | 578 | /// Create a new BufferedUarte without hardware flow control. |
| 550 | /// | 579 | /// |
| 551 | /// # Panics | 580 | /// # Panics |
| 552 | /// | 581 | /// |
| 553 | /// Panics if `rx_buffer.len()` is odd. | 582 | /// Panics if `rx_buffer.len()` is odd. |
| 554 | #[allow(clippy::too_many_arguments)] | 583 | #[allow(clippy::too_many_arguments)] |
| 555 | pub fn new( | 584 | pub fn new<U: UarteInstance, T: TimerInstance>( |
| 556 | uarte: Peri<'d, U>, | 585 | uarte: Peri<'d, U>, |
| 557 | timer: Peri<'d, T>, | 586 | timer: Peri<'d, T>, |
| 558 | ppi_ch1: Peri<'d, impl ConfigurableChannel>, | 587 | ppi_ch1: Peri<'d, impl ConfigurableChannel>, |
| @@ -582,7 +611,7 @@ impl<'d, U: UarteInstance, T: TimerInstance> BufferedUarteRx<'d, U, T> { | |||
| 582 | /// | 611 | /// |
| 583 | /// Panics if `rx_buffer.len()` is odd. | 612 | /// Panics if `rx_buffer.len()` is odd. |
| 584 | #[allow(clippy::too_many_arguments)] | 613 | #[allow(clippy::too_many_arguments)] |
| 585 | pub fn new_with_rts( | 614 | pub fn new_with_rts<U: UarteInstance, T: TimerInstance>( |
| 586 | uarte: Peri<'d, U>, | 615 | uarte: Peri<'d, U>, |
| 587 | timer: Peri<'d, T>, | 616 | timer: Peri<'d, T>, |
| 588 | ppi_ch1: Peri<'d, impl ConfigurableChannel>, | 617 | ppi_ch1: Peri<'d, impl ConfigurableChannel>, |
| @@ -608,7 +637,7 @@ impl<'d, U: UarteInstance, T: TimerInstance> BufferedUarteRx<'d, U, T> { | |||
| 608 | } | 637 | } |
| 609 | 638 | ||
| 610 | #[allow(clippy::too_many_arguments)] | 639 | #[allow(clippy::too_many_arguments)] |
| 611 | fn new_inner( | 640 | fn new_inner<U: UarteInstance, T: TimerInstance>( |
| 612 | peri: Peri<'d, U>, | 641 | peri: Peri<'d, U>, |
| 613 | timer: Peri<'d, T>, | 642 | timer: Peri<'d, T>, |
| 614 | ppi_ch1: Peri<'d, AnyConfigurableChannel>, | 643 | ppi_ch1: Peri<'d, AnyConfigurableChannel>, |
| @@ -619,22 +648,27 @@ impl<'d, U: UarteInstance, T: TimerInstance> BufferedUarteRx<'d, U, T> { | |||
| 619 | config: Config, | 648 | config: Config, |
| 620 | rx_buffer: &'d mut [u8], | 649 | rx_buffer: &'d mut [u8], |
| 621 | ) -> Self { | 650 | ) -> Self { |
| 622 | configure(U::regs(), config, rts.is_some()); | 651 | let r = U::regs(); |
| 652 | let irq = U::Interrupt::IRQ; | ||
| 653 | let state = U::state(); | ||
| 654 | let _buffered_state = U::buffered_state(); | ||
| 655 | |||
| 656 | configure(r, config, rts.is_some()); | ||
| 623 | 657 | ||
| 624 | let this = Self::new_innerer(peri, timer, ppi_ch1, ppi_ch2, ppi_group, rxd, rts, rx_buffer); | 658 | let this = Self::new_innerer(peri, timer, ppi_ch1, ppi_ch2, ppi_group, rxd, rts, rx_buffer); |
| 625 | 659 | ||
| 626 | U::regs().enable().write(|w| w.set_enable(vals::Enable::ENABLED)); | 660 | r.enable().write(|w| w.set_enable(vals::Enable::ENABLED)); |
| 627 | U::Interrupt::pend(); | 661 | irq.pend(); |
| 628 | unsafe { U::Interrupt::enable() }; | 662 | unsafe { irq.enable() }; |
| 629 | 663 | ||
| 630 | U::state().tx_rx_refcount.store(1, Ordering::Relaxed); | 664 | state.tx_rx_refcount.store(1, Ordering::Relaxed); |
| 631 | 665 | ||
| 632 | this | 666 | this |
| 633 | } | 667 | } |
| 634 | 668 | ||
| 635 | #[allow(clippy::too_many_arguments)] | 669 | #[allow(clippy::too_many_arguments)] |
| 636 | fn new_innerer( | 670 | fn new_innerer<U: UarteInstance, T: TimerInstance>( |
| 637 | peri: Peri<'d, U>, | 671 | _peri: Peri<'d, U>, |
| 638 | timer: Peri<'d, T>, | 672 | timer: Peri<'d, T>, |
| 639 | ppi_ch1: Peri<'d, AnyConfigurableChannel>, | 673 | ppi_ch1: Peri<'d, AnyConfigurableChannel>, |
| 640 | ppi_ch2: Peri<'d, AnyConfigurableChannel>, | 674 | ppi_ch2: Peri<'d, AnyConfigurableChannel>, |
| @@ -646,16 +680,17 @@ impl<'d, U: UarteInstance, T: TimerInstance> BufferedUarteRx<'d, U, T> { | |||
| 646 | assert!(rx_buffer.len() % 2 == 0); | 680 | assert!(rx_buffer.len() % 2 == 0); |
| 647 | 681 | ||
| 648 | let r = U::regs(); | 682 | let r = U::regs(); |
| 683 | let state = U::state(); | ||
| 684 | let buffered_state = U::buffered_state(); | ||
| 649 | 685 | ||
| 650 | configure_rx_pins(r, rxd, rts); | 686 | configure_rx_pins(r, rxd, rts); |
| 651 | 687 | ||
| 652 | // Initialize state | 688 | // Initialize state |
| 653 | let s = U::buffered_state(); | 689 | buffered_state.rx_started_count.store(0, Ordering::Relaxed); |
| 654 | s.rx_started_count.store(0, Ordering::Relaxed); | 690 | buffered_state.rx_ended_count.store(0, Ordering::Relaxed); |
| 655 | s.rx_ended_count.store(0, Ordering::Relaxed); | 691 | buffered_state.rx_started.store(false, Ordering::Relaxed); |
| 656 | s.rx_started.store(false, Ordering::Relaxed); | ||
| 657 | let rx_len = rx_buffer.len().min(EASY_DMA_SIZE * 2); | 692 | let rx_len = rx_buffer.len().min(EASY_DMA_SIZE * 2); |
| 658 | unsafe { s.rx_buf.init(rx_buffer.as_mut_ptr(), rx_len) }; | 693 | unsafe { buffered_state.rx_buf.init(rx_buffer.as_mut_ptr(), rx_len) }; |
| 659 | 694 | ||
| 660 | // clear errors | 695 | // clear errors |
| 661 | let errors = r.errorsrc().read(); | 696 | let errors = r.errorsrc().read(); |
| @@ -683,7 +718,9 @@ impl<'d, U: UarteInstance, T: TimerInstance> BufferedUarteRx<'d, U, T> { | |||
| 683 | let mut ppi_ch1 = Ppi::new_one_to_one(ppi_ch1, Event::from_reg(r.events_rxdrdy()), timer.task_count()); | 718 | let mut ppi_ch1 = Ppi::new_one_to_one(ppi_ch1, Event::from_reg(r.events_rxdrdy()), timer.task_count()); |
| 684 | ppi_ch1.enable(); | 719 | ppi_ch1.enable(); |
| 685 | 720 | ||
| 686 | s.rx_ppi_ch.store(ppi_ch2.number() as u8, Ordering::Relaxed); | 721 | buffered_state |
| 722 | .rx_ppi_ch | ||
| 723 | .store(ppi_ch2.number() as u8, Ordering::Relaxed); | ||
| 687 | let mut ppi_group = PpiGroup::new(ppi_group); | 724 | let mut ppi_group = PpiGroup::new(ppi_group); |
| 688 | let mut ppi_ch2 = Ppi::new_one_to_two( | 725 | let mut ppi_ch2 = Ppi::new_one_to_two( |
| 689 | ppi_ch2, | 726 | ppi_ch2, |
| @@ -695,11 +732,14 @@ impl<'d, U: UarteInstance, T: TimerInstance> BufferedUarteRx<'d, U, T> { | |||
| 695 | ppi_group.add_channel(&ppi_ch2); | 732 | ppi_group.add_channel(&ppi_ch2); |
| 696 | 733 | ||
| 697 | Self { | 734 | Self { |
| 698 | _peri: peri, | 735 | r, |
| 736 | state, | ||
| 737 | buffered_state, | ||
| 699 | timer, | 738 | timer, |
| 700 | _ppi_ch1: ppi_ch1, | 739 | _ppi_ch1: ppi_ch1, |
| 701 | _ppi_ch2: ppi_ch2, | 740 | _ppi_ch2: ppi_ch2, |
| 702 | _ppi_group: ppi_group, | 741 | _ppi_group: ppi_group, |
| 742 | _p: PhantomData, | ||
| 703 | } | 743 | } |
| 704 | } | 744 | } |
| 705 | 745 | ||
| @@ -714,17 +754,17 @@ impl<'d, U: UarteInstance, T: TimerInstance> BufferedUarteRx<'d, U, T> { | |||
| 714 | 754 | ||
| 715 | /// Return the contents of the internal buffer, filling it with more data from the inner reader if it is empty. | 755 | /// Return the contents of the internal buffer, filling it with more data from the inner reader if it is empty. |
| 716 | pub fn fill_buf(&mut self) -> impl Future<Output = Result<&'_ [u8], Error>> { | 756 | pub fn fill_buf(&mut self) -> impl Future<Output = Result<&'_ [u8], Error>> { |
| 757 | let r = self.r; | ||
| 758 | let s = self.buffered_state; | ||
| 759 | let ss = self.state; | ||
| 760 | let timer = &self.timer; | ||
| 717 | poll_fn(move |cx| { | 761 | poll_fn(move |cx| { |
| 718 | compiler_fence(Ordering::SeqCst); | 762 | compiler_fence(Ordering::SeqCst); |
| 719 | //trace!("poll_read"); | 763 | //trace!("poll_read"); |
| 720 | 764 | ||
| 721 | let r = U::regs(); | ||
| 722 | let s = U::buffered_state(); | ||
| 723 | let ss = U::state(); | ||
| 724 | |||
| 725 | // Read the RXDRDY counter. | 765 | // Read the RXDRDY counter. |
| 726 | T::regs().tasks_capture(0).write_value(1); | 766 | timer.cc(0).capture(); |
| 727 | let mut end = T::regs().cc(0).read() as usize; | 767 | let mut end = timer.cc(0).read() as usize; |
| 728 | //trace!(" rxdrdy count = {:?}", end); | 768 | //trace!(" rxdrdy count = {:?}", end); |
| 729 | 769 | ||
| 730 | // We've set a compare channel that resets the counter to 0 when it reaches `len*2`. | 770 | // We've set a compare channel that resets the counter to 0 when it reaches `len*2`. |
| @@ -771,24 +811,24 @@ impl<'d, U: UarteInstance, T: TimerInstance> BufferedUarteRx<'d, U, T> { | |||
| 771 | return; | 811 | return; |
| 772 | } | 812 | } |
| 773 | 813 | ||
| 774 | let s = U::buffered_state(); | 814 | let s = self.buffered_state; |
| 775 | let mut rx = unsafe { s.rx_buf.reader() }; | 815 | let mut rx = unsafe { s.rx_buf.reader() }; |
| 776 | rx.pop_done(amt); | 816 | rx.pop_done(amt); |
| 777 | U::regs().intenset().write(|w| w.set_rxstarted(true)); | 817 | self.r.intenset().write(|w| w.set_rxstarted(true)); |
| 778 | } | 818 | } |
| 779 | 819 | ||
| 780 | /// we are ready to read if there is data in the buffer | 820 | /// we are ready to read if there is data in the buffer |
| 781 | fn read_ready() -> Result<bool, Error> { | 821 | fn read_ready(&self) -> Result<bool, Error> { |
| 782 | let state = U::buffered_state(); | 822 | let state = self.buffered_state; |
| 783 | Ok(!state.rx_buf.is_empty()) | 823 | Ok(!state.rx_buf.is_empty()) |
| 784 | } | 824 | } |
| 785 | } | 825 | } |
| 786 | 826 | ||
| 787 | impl<'a, U: UarteInstance, T: TimerInstance> Drop for BufferedUarteRx<'a, U, T> { | 827 | impl<'a> Drop for BufferedUarteRx<'a> { |
| 788 | fn drop(&mut self) { | 828 | fn drop(&mut self) { |
| 789 | self._ppi_group.disable_all(); | 829 | self._ppi_group.disable_all(); |
| 790 | 830 | ||
| 791 | let r = U::regs(); | 831 | let r = self.r; |
| 792 | 832 | ||
| 793 | self.timer.stop(); | 833 | self.timer.stop(); |
| 794 | 834 | ||
| @@ -801,10 +841,10 @@ impl<'a, U: UarteInstance, T: TimerInstance> Drop for BufferedUarteRx<'a, U, T> | |||
| 801 | r.tasks_stoprx().write_value(1); | 841 | r.tasks_stoprx().write_value(1); |
| 802 | while r.events_rxto().read() == 0 {} | 842 | while r.events_rxto().read() == 0 {} |
| 803 | 843 | ||
| 804 | let s = U::buffered_state(); | 844 | let s = self.buffered_state; |
| 805 | unsafe { s.rx_buf.deinit() } | 845 | unsafe { s.rx_buf.deinit() } |
| 806 | 846 | ||
| 807 | let s = U::state(); | 847 | let s = self.state; |
| 808 | drop_tx_rx(r, s); | 848 | drop_tx_rx(r, s); |
| 809 | } | 849 | } |
| 810 | } | 850 | } |
| @@ -818,43 +858,44 @@ mod _embedded_io { | |||
| 818 | } | 858 | } |
| 819 | } | 859 | } |
| 820 | 860 | ||
| 821 | impl<'d, U: UarteInstance, T: TimerInstance> embedded_io_async::ErrorType for BufferedUarte<'d, U, T> { | 861 | impl<'d> embedded_io_async::ErrorType for BufferedUarte<'d> { |
| 822 | type Error = Error; | 862 | type Error = Error; |
| 823 | } | 863 | } |
| 824 | 864 | ||
| 825 | impl<'d, U: UarteInstance, T: TimerInstance> embedded_io_async::ErrorType for BufferedUarteRx<'d, U, T> { | 865 | impl<'d> embedded_io_async::ErrorType for BufferedUarteRx<'d> { |
| 826 | type Error = Error; | 866 | type Error = Error; |
| 827 | } | 867 | } |
| 828 | 868 | ||
| 829 | impl<'d, U: UarteInstance> embedded_io_async::ErrorType for BufferedUarteTx<'d, U> { | 869 | impl<'d> embedded_io_async::ErrorType for BufferedUarteTx<'d> { |
| 830 | type Error = Error; | 870 | type Error = Error; |
| 831 | } | 871 | } |
| 832 | 872 | ||
| 833 | impl<'d, U: UarteInstance, T: TimerInstance> embedded_io_async::Read for BufferedUarte<'d, U, T> { | 873 | impl<'d> embedded_io_async::Read for BufferedUarte<'d> { |
| 834 | async fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error> { | 874 | async fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error> { |
| 835 | self.read(buf).await | 875 | self.read(buf).await |
| 836 | } | 876 | } |
| 837 | } | 877 | } |
| 838 | 878 | ||
| 839 | impl<'d: 'd, U: UarteInstance, T: TimerInstance> embedded_io_async::Read for BufferedUarteRx<'d, U, T> { | 879 | impl<'d> embedded_io_async::Read for BufferedUarteRx<'d> { |
| 840 | async fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error> { | 880 | async fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error> { |
| 841 | self.read(buf).await | 881 | self.read(buf).await |
| 842 | } | 882 | } |
| 843 | } | 883 | } |
| 844 | 884 | ||
| 845 | impl<'d, U: UarteInstance, T: TimerInstance + 'd> embedded_io_async::ReadReady for BufferedUarte<'d, U, T> { | 885 | impl<'d> embedded_io_async::ReadReady for BufferedUarte<'d> { |
| 846 | fn read_ready(&mut self) -> Result<bool, Self::Error> { | 886 | fn read_ready(&mut self) -> Result<bool, Self::Error> { |
| 847 | BufferedUarteRx::<'d, U, T>::read_ready() | 887 | self.rx.read_ready() |
| 848 | } | 888 | } |
| 849 | } | 889 | } |
| 850 | 890 | ||
| 851 | impl<'d, U: UarteInstance, T: TimerInstance + 'd> embedded_io_async::ReadReady for BufferedUarteRx<'d, U, T> { | 891 | impl<'d> embedded_io_async::ReadReady for BufferedUarteRx<'d> { |
| 852 | fn read_ready(&mut self) -> Result<bool, Self::Error> { | 892 | fn read_ready(&mut self) -> Result<bool, Self::Error> { |
| 853 | Self::read_ready() | 893 | let state = self.buffered_state; |
| 894 | Ok(!state.rx_buf.is_empty()) | ||
| 854 | } | 895 | } |
| 855 | } | 896 | } |
| 856 | 897 | ||
| 857 | impl<'d, U: UarteInstance, T: TimerInstance> embedded_io_async::BufRead for BufferedUarte<'d, U, T> { | 898 | impl<'d> embedded_io_async::BufRead for BufferedUarte<'d> { |
| 858 | async fn fill_buf(&mut self) -> Result<&[u8], Self::Error> { | 899 | async fn fill_buf(&mut self) -> Result<&[u8], Self::Error> { |
| 859 | self.fill_buf().await | 900 | self.fill_buf().await |
| 860 | } | 901 | } |
| @@ -864,7 +905,7 @@ mod _embedded_io { | |||
| 864 | } | 905 | } |
| 865 | } | 906 | } |
| 866 | 907 | ||
| 867 | impl<'d: 'd, U: UarteInstance, T: TimerInstance> embedded_io_async::BufRead for BufferedUarteRx<'d, U, T> { | 908 | impl<'d> embedded_io_async::BufRead for BufferedUarteRx<'d> { |
| 868 | async fn fill_buf(&mut self) -> Result<&[u8], Self::Error> { | 909 | async fn fill_buf(&mut self) -> Result<&[u8], Self::Error> { |
| 869 | self.fill_buf().await | 910 | self.fill_buf().await |
| 870 | } | 911 | } |
| @@ -874,7 +915,7 @@ mod _embedded_io { | |||
| 874 | } | 915 | } |
| 875 | } | 916 | } |
| 876 | 917 | ||
| 877 | impl<'d, U: UarteInstance, T: TimerInstance> embedded_io_async::Write for BufferedUarte<'d, U, T> { | 918 | impl<'d> embedded_io_async::Write for BufferedUarte<'d> { |
| 878 | async fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> { | 919 | async fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> { |
| 879 | self.write(buf).await | 920 | self.write(buf).await |
| 880 | } | 921 | } |
| @@ -884,7 +925,7 @@ mod _embedded_io { | |||
| 884 | } | 925 | } |
| 885 | } | 926 | } |
| 886 | 927 | ||
| 887 | impl<'d: 'd, U: UarteInstance> embedded_io_async::Write for BufferedUarteTx<'d, U> { | 928 | impl<'d> embedded_io_async::Write for BufferedUarteTx<'d> { |
| 888 | async fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> { | 929 | async fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> { |
| 889 | self.write(buf).await | 930 | self.write(buf).await |
| 890 | } | 931 | } |
diff --git a/embassy-nrf/src/timer.rs b/embassy-nrf/src/timer.rs index 1d1f77ea8..0b0bb9780 100644 --- a/embassy-nrf/src/timer.rs +++ b/embassy-nrf/src/timer.rs | |||
| @@ -6,6 +6,8 @@ | |||
| 6 | 6 | ||
| 7 | #![macro_use] | 7 | #![macro_use] |
| 8 | 8 | ||
| 9 | use core::marker::PhantomData; | ||
| 10 | |||
| 9 | use embassy_hal_internal::{Peri, PeripheralType}; | 11 | use embassy_hal_internal::{Peri, PeripheralType}; |
| 10 | 12 | ||
| 11 | use crate::pac; | 13 | use crate::pac; |
| @@ -81,16 +83,18 @@ pub enum Frequency { | |||
| 81 | /// | 83 | /// |
| 82 | /// It has either 4 or 6 Capture/Compare registers, which can be used to capture the current state of the counter | 84 | /// It has either 4 or 6 Capture/Compare registers, which can be used to capture the current state of the counter |
| 83 | /// or trigger an event when the counter reaches a certain value. | 85 | /// or trigger an event when the counter reaches a certain value. |
| 84 | pub struct Timer<'d, T: Instance> { | 86 | pub struct Timer<'d> { |
| 85 | _p: Peri<'d, T>, | 87 | r: pac::timer::Timer, |
| 88 | ccs: usize, | ||
| 89 | _p: PhantomData<&'d ()>, | ||
| 86 | } | 90 | } |
| 87 | 91 | ||
| 88 | impl<'d, T: Instance> Timer<'d, T> { | 92 | impl<'d> Timer<'d> { |
| 89 | /// Create a new `Timer` driver. | 93 | /// Create a new `Timer` driver. |
| 90 | /// | 94 | /// |
| 91 | /// This can be useful for triggering tasks via PPI. | 95 | /// This can be useful for triggering tasks via PPI. |
| 92 | /// `Uarte` uses this internally. | 96 | /// `Uarte` uses this internally. |
| 93 | pub fn new(timer: Peri<'d, T>) -> Self { | 97 | pub fn new<T: Instance>(timer: Peri<'d, T>) -> Self { |
| 94 | Self::new_inner(timer, false) | 98 | Self::new_inner(timer, false) |
| 95 | } | 99 | } |
| 96 | 100 | ||
| @@ -98,14 +102,18 @@ impl<'d, T: Instance> Timer<'d, T> { | |||
| 98 | /// | 102 | /// |
| 99 | /// This can be useful for triggering tasks via PPI. | 103 | /// This can be useful for triggering tasks via PPI. |
| 100 | /// `Uarte` uses this internally. | 104 | /// `Uarte` uses this internally. |
| 101 | pub fn new_counter(timer: Peri<'d, T>) -> Self { | 105 | pub fn new_counter<T: Instance>(timer: Peri<'d, T>) -> Self { |
| 102 | Self::new_inner(timer, true) | 106 | Self::new_inner(timer, true) |
| 103 | } | 107 | } |
| 104 | 108 | ||
| 105 | fn new_inner(timer: Peri<'d, T>, is_counter: bool) -> Self { | 109 | fn new_inner<T: Instance>(_timer: Peri<'d, T>, is_counter: bool) -> Self { |
| 106 | let regs = T::regs(); | 110 | let regs = T::regs(); |
| 107 | 111 | ||
| 108 | let this = Self { _p: timer }; | 112 | let this = Self { |
| 113 | r: regs, | ||
| 114 | ccs: T::CCS, | ||
| 115 | _p: PhantomData, | ||
| 116 | }; | ||
| 109 | 117 | ||
| 110 | // Stop the timer before doing anything else, | 118 | // Stop the timer before doing anything else, |
| 111 | // since changing BITMODE while running can cause 'unpredictable behaviour' according to the specification. | 119 | // since changing BITMODE while running can cause 'unpredictable behaviour' according to the specification. |
| @@ -131,7 +139,7 @@ impl<'d, T: Instance> Timer<'d, T> { | |||
| 131 | // Default to the max frequency of the lower power clock | 139 | // Default to the max frequency of the lower power clock |
| 132 | this.set_frequency(Frequency::F1MHz); | 140 | this.set_frequency(Frequency::F1MHz); |
| 133 | 141 | ||
| 134 | for n in 0..T::CCS { | 142 | for n in 0..this.ccs { |
| 135 | let cc = this.cc(n); | 143 | let cc = this.cc(n); |
| 136 | // Initialize all the shorts as disabled. | 144 | // Initialize all the shorts as disabled. |
| 137 | cc.unshort_compare_clear(); | 145 | cc.unshort_compare_clear(); |
| @@ -147,43 +155,43 @@ impl<'d, T: Instance> Timer<'d, T> { | |||
| 147 | #[cfg(feature = "unstable-pac")] | 155 | #[cfg(feature = "unstable-pac")] |
| 148 | #[inline] | 156 | #[inline] |
| 149 | pub fn regs(&mut self) -> pac::timer::Timer { | 157 | pub fn regs(&mut self) -> pac::timer::Timer { |
| 150 | T::regs() | 158 | self.r |
| 151 | } | 159 | } |
| 152 | 160 | ||
| 153 | /// Starts the timer. | 161 | /// Starts the timer. |
| 154 | pub fn start(&self) { | 162 | pub fn start(&self) { |
| 155 | T::regs().tasks_start().write_value(1) | 163 | self.r.tasks_start().write_value(1) |
| 156 | } | 164 | } |
| 157 | 165 | ||
| 158 | /// Stops the timer. | 166 | /// Stops the timer. |
| 159 | pub fn stop(&self) { | 167 | pub fn stop(&self) { |
| 160 | T::regs().tasks_stop().write_value(1) | 168 | self.r.tasks_stop().write_value(1) |
| 161 | } | 169 | } |
| 162 | 170 | ||
| 163 | /// Reset the timer's counter to 0. | 171 | /// Reset the timer's counter to 0. |
| 164 | pub fn clear(&self) { | 172 | pub fn clear(&self) { |
| 165 | T::regs().tasks_clear().write_value(1) | 173 | self.r.tasks_clear().write_value(1) |
| 166 | } | 174 | } |
| 167 | 175 | ||
| 168 | /// Returns the START task, for use with PPI. | 176 | /// Returns the START task, for use with PPI. |
| 169 | /// | 177 | /// |
| 170 | /// When triggered, this task starts the timer. | 178 | /// When triggered, this task starts the timer. |
| 171 | pub fn task_start(&self) -> Task<'d> { | 179 | pub fn task_start(&self) -> Task<'d> { |
| 172 | Task::from_reg(T::regs().tasks_start()) | 180 | Task::from_reg(self.r.tasks_start()) |
| 173 | } | 181 | } |
| 174 | 182 | ||
| 175 | /// Returns the STOP task, for use with PPI. | 183 | /// Returns the STOP task, for use with PPI. |
| 176 | /// | 184 | /// |
| 177 | /// When triggered, this task stops the timer. | 185 | /// When triggered, this task stops the timer. |
| 178 | pub fn task_stop(&self) -> Task<'d> { | 186 | pub fn task_stop(&self) -> Task<'d> { |
| 179 | Task::from_reg(T::regs().tasks_stop()) | 187 | Task::from_reg(self.r.tasks_stop()) |
| 180 | } | 188 | } |
| 181 | 189 | ||
| 182 | /// Returns the CLEAR task, for use with PPI. | 190 | /// Returns the CLEAR task, for use with PPI. |
| 183 | /// | 191 | /// |
| 184 | /// When triggered, this task resets the timer's counter to 0. | 192 | /// When triggered, this task resets the timer's counter to 0. |
| 185 | pub fn task_clear(&self) -> Task<'d> { | 193 | pub fn task_clear(&self) -> Task<'d> { |
| 186 | Task::from_reg(T::regs().tasks_clear()) | 194 | Task::from_reg(self.r.tasks_clear()) |
| 187 | } | 195 | } |
| 188 | 196 | ||
| 189 | /// Returns the COUNT task, for use with PPI. | 197 | /// Returns the COUNT task, for use with PPI. |
| @@ -191,7 +199,7 @@ impl<'d, T: Instance> Timer<'d, T> { | |||
| 191 | /// When triggered, this task increments the timer's counter by 1. | 199 | /// When triggered, this task increments the timer's counter by 1. |
| 192 | /// Only works in counter mode. | 200 | /// Only works in counter mode. |
| 193 | pub fn task_count(&self) -> Task<'d> { | 201 | pub fn task_count(&self) -> Task<'d> { |
| 194 | Task::from_reg(T::regs().tasks_count()) | 202 | Task::from_reg(self.r.tasks_count()) |
| 195 | } | 203 | } |
| 196 | 204 | ||
| 197 | /// Change the timer's frequency. | 205 | /// Change the timer's frequency. |
| @@ -201,7 +209,7 @@ impl<'d, T: Instance> Timer<'d, T> { | |||
| 201 | pub fn set_frequency(&self, frequency: Frequency) { | 209 | pub fn set_frequency(&self, frequency: Frequency) { |
| 202 | self.stop(); | 210 | self.stop(); |
| 203 | 211 | ||
| 204 | T::regs() | 212 | self.r |
| 205 | .prescaler() | 213 | .prescaler() |
| 206 | // SAFETY: `frequency` is a variant of `Frequency`, | 214 | // SAFETY: `frequency` is a variant of `Frequency`, |
| 207 | // whose values are all in the range of 0-9 (the valid range of `prescaler`). | 215 | // whose values are all in the range of 0-9 (the valid range of `prescaler`). |
| @@ -212,18 +220,19 @@ impl<'d, T: Instance> Timer<'d, T> { | |||
| 212 | /// | 220 | /// |
| 213 | /// # Panics | 221 | /// # Panics |
| 214 | /// Panics if `n` >= the number of CC registers this timer has (4 for a normal timer, 6 for an extended timer). | 222 | /// Panics if `n` >= the number of CC registers this timer has (4 for a normal timer, 6 for an extended timer). |
| 215 | pub fn cc(&self, n: usize) -> Cc<'d, T> { | 223 | pub fn cc(&self, n: usize) -> Cc<'d> { |
| 216 | if n >= T::CCS { | 224 | if n >= self.ccs { |
| 217 | panic!("Cannot get CC register {} of timer with {} CC registers.", n, T::CCS); | 225 | panic!("Cannot get CC register {} of timer with {} CC registers.", n, self.ccs); |
| 218 | } | 226 | } |
| 219 | Cc { | 227 | Cc { |
| 220 | n, | 228 | n, |
| 221 | _p: unsafe { self._p.clone_unchecked() }, | 229 | r: self.r, |
| 230 | _p: PhantomData, | ||
| 222 | } | 231 | } |
| 223 | } | 232 | } |
| 224 | } | 233 | } |
| 225 | 234 | ||
| 226 | impl<T: Instance> Timer<'static, T> { | 235 | impl Timer<'static> { |
| 227 | /// Persist the timer's configuration for the rest of the program's lifetime. This method | 236 | /// Persist the timer's configuration for the rest of the program's lifetime. This method |
| 228 | /// should be preferred over [`core::mem::forget()`] because the `'static` bound prevents | 237 | /// should be preferred over [`core::mem::forget()`] because the `'static` bound prevents |
| 229 | /// accidental reuse of the underlying peripheral. | 238 | /// accidental reuse of the underlying peripheral. |
| @@ -232,7 +241,7 @@ impl<T: Instance> Timer<'static, T> { | |||
| 232 | } | 241 | } |
| 233 | } | 242 | } |
| 234 | 243 | ||
| 235 | impl<'d, T: Instance> Drop for Timer<'d, T> { | 244 | impl<'d> Drop for Timer<'d> { |
| 236 | fn drop(&mut self) { | 245 | fn drop(&mut self) { |
| 237 | self.stop(); | 246 | self.stop(); |
| 238 | } | 247 | } |
| @@ -245,27 +254,28 @@ impl<'d, T: Instance> Drop for Timer<'d, T> { | |||
| 245 | /// | 254 | /// |
| 246 | /// The timer will fire the register's COMPARE event when its counter reaches the value stored in the register. | 255 | /// The timer will fire the register's COMPARE event when its counter reaches the value stored in the register. |
| 247 | /// When the register's CAPTURE task is triggered, the timer will store the current value of its counter in the register | 256 | /// When the register's CAPTURE task is triggered, the timer will store the current value of its counter in the register |
| 248 | pub struct Cc<'d, T: Instance> { | 257 | pub struct Cc<'d> { |
| 249 | n: usize, | 258 | n: usize, |
| 250 | _p: Peri<'d, T>, | 259 | r: pac::timer::Timer, |
| 260 | _p: PhantomData<&'d ()>, | ||
| 251 | } | 261 | } |
| 252 | 262 | ||
| 253 | impl<'d, T: Instance> Cc<'d, T> { | 263 | impl<'d> Cc<'d> { |
| 254 | /// Get the current value stored in the register. | 264 | /// Get the current value stored in the register. |
| 255 | pub fn read(&self) -> u32 { | 265 | pub fn read(&self) -> u32 { |
| 256 | T::regs().cc(self.n).read() | 266 | self.r.cc(self.n).read() |
| 257 | } | 267 | } |
| 258 | 268 | ||
| 259 | /// Set the value stored in the register. | 269 | /// Set the value stored in the register. |
| 260 | /// | 270 | /// |
| 261 | /// `event_compare` will fire when the timer's counter reaches this value. | 271 | /// `event_compare` will fire when the timer's counter reaches this value. |
| 262 | pub fn write(&self, value: u32) { | 272 | pub fn write(&self, value: u32) { |
| 263 | T::regs().cc(self.n).write_value(value); | 273 | self.r.cc(self.n).write_value(value); |
| 264 | } | 274 | } |
| 265 | 275 | ||
| 266 | /// Capture the current value of the timer's counter in this register, and return it. | 276 | /// Capture the current value of the timer's counter in this register, and return it. |
| 267 | pub fn capture(&self) -> u32 { | 277 | pub fn capture(&self) -> u32 { |
| 268 | T::regs().tasks_capture(self.n).write_value(1); | 278 | self.r.tasks_capture(self.n).write_value(1); |
| 269 | self.read() | 279 | self.read() |
| 270 | } | 280 | } |
| 271 | 281 | ||
| @@ -273,20 +283,20 @@ impl<'d, T: Instance> Cc<'d, T> { | |||
| 273 | /// | 283 | /// |
| 274 | /// When triggered, this task will capture the current value of the timer's counter in this register. | 284 | /// When triggered, this task will capture the current value of the timer's counter in this register. |
| 275 | pub fn task_capture(&self) -> Task<'d> { | 285 | pub fn task_capture(&self) -> Task<'d> { |
| 276 | Task::from_reg(T::regs().tasks_capture(self.n)) | 286 | Task::from_reg(self.r.tasks_capture(self.n)) |
| 277 | } | 287 | } |
| 278 | 288 | ||
| 279 | /// Returns this CC register's COMPARE event, for use with PPI. | 289 | /// Returns this CC register's COMPARE event, for use with PPI. |
| 280 | /// | 290 | /// |
| 281 | /// This event will fire when the timer's counter reaches the value in this CC register. | 291 | /// This event will fire when the timer's counter reaches the value in this CC register. |
| 282 | pub fn event_compare(&self) -> Event<'d> { | 292 | pub fn event_compare(&self) -> Event<'d> { |
| 283 | Event::from_reg(T::regs().events_compare(self.n)) | 293 | Event::from_reg(self.r.events_compare(self.n)) |
| 284 | } | 294 | } |
| 285 | 295 | ||
| 286 | /// Clear the COMPARE event for this CC register. | 296 | /// Clear the COMPARE event for this CC register. |
| 287 | #[inline] | 297 | #[inline] |
| 288 | pub fn clear_events(&self) { | 298 | pub fn clear_events(&self) { |
| 289 | T::regs().events_compare(self.n).write_value(0); | 299 | self.r.events_compare(self.n).write_value(0); |
| 290 | } | 300 | } |
| 291 | 301 | ||
| 292 | /// Enable the shortcut between this CC register's COMPARE event and the timer's CLEAR task. | 302 | /// Enable the shortcut between this CC register's COMPARE event and the timer's CLEAR task. |
| @@ -295,12 +305,12 @@ impl<'d, T: Instance> Cc<'d, T> { | |||
| 295 | /// | 305 | /// |
| 296 | /// So, when the timer's counter reaches the value stored in this register, the timer's counter will be reset to 0. | 306 | /// So, when the timer's counter reaches the value stored in this register, the timer's counter will be reset to 0. |
| 297 | pub fn short_compare_clear(&self) { | 307 | pub fn short_compare_clear(&self) { |
| 298 | T::regs().shorts().modify(|w| w.set_compare_clear(self.n, true)) | 308 | self.r.shorts().modify(|w| w.set_compare_clear(self.n, true)) |
| 299 | } | 309 | } |
| 300 | 310 | ||
| 301 | /// Disable the shortcut between this CC register's COMPARE event and the timer's CLEAR task. | 311 | /// Disable the shortcut between this CC register's COMPARE event and the timer's CLEAR task. |
| 302 | pub fn unshort_compare_clear(&self) { | 312 | pub fn unshort_compare_clear(&self) { |
| 303 | T::regs().shorts().modify(|w| w.set_compare_clear(self.n, false)) | 313 | self.r.shorts().modify(|w| w.set_compare_clear(self.n, false)) |
| 304 | } | 314 | } |
| 305 | 315 | ||
| 306 | /// Enable the shortcut between this CC register's COMPARE event and the timer's STOP task. | 316 | /// Enable the shortcut between this CC register's COMPARE event and the timer's STOP task. |
| @@ -309,11 +319,11 @@ impl<'d, T: Instance> Cc<'d, T> { | |||
| 309 | /// | 319 | /// |
| 310 | /// So, when the timer's counter reaches the value stored in this register, the timer will stop counting up. | 320 | /// So, when the timer's counter reaches the value stored in this register, the timer will stop counting up. |
| 311 | pub fn short_compare_stop(&self) { | 321 | pub fn short_compare_stop(&self) { |
| 312 | T::regs().shorts().modify(|w| w.set_compare_stop(self.n, true)) | 322 | self.r.shorts().modify(|w| w.set_compare_stop(self.n, true)) |
| 313 | } | 323 | } |
| 314 | 324 | ||
| 315 | /// Disable the shortcut between this CC register's COMPARE event and the timer's STOP task. | 325 | /// Disable the shortcut between this CC register's COMPARE event and the timer's STOP task. |
| 316 | pub fn unshort_compare_stop(&self) { | 326 | pub fn unshort_compare_stop(&self) { |
| 317 | T::regs().shorts().modify(|w| w.set_compare_stop(self.n, false)) | 327 | self.r.shorts().modify(|w| w.set_compare_stop(self.n, false)) |
| 318 | } | 328 | } |
| 319 | } | 329 | } |
diff --git a/embassy-nrf/src/twim.rs b/embassy-nrf/src/twim.rs index ffc9b39f6..943ea9d31 100644 --- a/embassy-nrf/src/twim.rs +++ b/embassy-nrf/src/twim.rs | |||
| @@ -114,7 +114,6 @@ impl<T: Instance> interrupt::typelevel::Handler<T::Interrupt> for InterruptHandl | |||
| 114 | /// TWI driver. | 114 | /// TWI driver. |
| 115 | pub struct Twim<'d> { | 115 | pub struct Twim<'d> { |
| 116 | r: pac::twim::Twim, | 116 | r: pac::twim::Twim, |
| 117 | irq: interrupt::Interrupt, | ||
| 118 | state: &'static State, | 117 | state: &'static State, |
| 119 | tx_ram_buffer: &'d mut [u8], | 118 | tx_ram_buffer: &'d mut [u8], |
| 120 | _p: PhantomData<&'d ()>, | 119 | _p: PhantomData<&'d ()>, |
| @@ -171,7 +170,6 @@ impl<'d> Twim<'d> { | |||
| 171 | 170 | ||
| 172 | let mut twim = Self { | 171 | let mut twim = Self { |
| 173 | r: T::regs(), | 172 | r: T::regs(), |
| 174 | irq: T::Interrupt::IRQ, | ||
| 175 | state: T::state(), | 173 | state: T::state(), |
| 176 | tx_ram_buffer, | 174 | tx_ram_buffer, |
| 177 | _p: PhantomData {}, | 175 | _p: PhantomData {}, |
diff --git a/embassy-nrf/src/uarte.rs b/embassy-nrf/src/uarte.rs index 927a0ac08..66fb3b3f2 100644 --- a/embassy-nrf/src/uarte.rs +++ b/embassy-nrf/src/uarte.rs | |||
| @@ -132,28 +132,32 @@ impl<T: Instance> interrupt::typelevel::Handler<T::Interrupt> for InterruptHandl | |||
| 132 | } | 132 | } |
| 133 | 133 | ||
| 134 | /// UARTE driver. | 134 | /// UARTE driver. |
| 135 | pub struct Uarte<'d, T: Instance> { | 135 | pub struct Uarte<'d> { |
| 136 | tx: UarteTx<'d, T>, | 136 | tx: UarteTx<'d>, |
| 137 | rx: UarteRx<'d, T>, | 137 | rx: UarteRx<'d>, |
| 138 | } | 138 | } |
| 139 | 139 | ||
| 140 | /// Transmitter part of the UARTE driver. | 140 | /// Transmitter part of the UARTE driver. |
| 141 | /// | 141 | /// |
| 142 | /// This can be obtained via [`Uarte::split`], or created directly. | 142 | /// This can be obtained via [`Uarte::split`], or created directly. |
| 143 | pub struct UarteTx<'d, T: Instance> { | 143 | pub struct UarteTx<'d> { |
| 144 | _p: Peri<'d, T>, | 144 | r: pac::uarte::Uarte, |
| 145 | state: &'static State, | ||
| 146 | _p: PhantomData<&'d ()>, | ||
| 145 | } | 147 | } |
| 146 | 148 | ||
| 147 | /// Receiver part of the UARTE driver. | 149 | /// Receiver part of the UARTE driver. |
| 148 | /// | 150 | /// |
| 149 | /// This can be obtained via [`Uarte::split`], or created directly. | 151 | /// This can be obtained via [`Uarte::split`], or created directly. |
| 150 | pub struct UarteRx<'d, T: Instance> { | 152 | pub struct UarteRx<'d> { |
| 151 | _p: Peri<'d, T>, | 153 | r: pac::uarte::Uarte, |
| 154 | state: &'static State, | ||
| 155 | _p: PhantomData<&'d ()>, | ||
| 152 | } | 156 | } |
| 153 | 157 | ||
| 154 | impl<'d, T: Instance> Uarte<'d, T> { | 158 | impl<'d> Uarte<'d> { |
| 155 | /// Create a new UARTE without hardware flow control | 159 | /// Create a new UARTE without hardware flow control |
| 156 | pub fn new( | 160 | pub fn new<T: Instance>( |
| 157 | uarte: Peri<'d, T>, | 161 | uarte: Peri<'d, T>, |
| 158 | rxd: Peri<'d, impl GpioPin>, | 162 | rxd: Peri<'d, impl GpioPin>, |
| 159 | txd: Peri<'d, impl GpioPin>, | 163 | txd: Peri<'d, impl GpioPin>, |
| @@ -164,7 +168,7 @@ impl<'d, T: Instance> Uarte<'d, T> { | |||
| 164 | } | 168 | } |
| 165 | 169 | ||
| 166 | /// Create a new UARTE with hardware flow control (RTS/CTS) | 170 | /// Create a new UARTE with hardware flow control (RTS/CTS) |
| 167 | pub fn new_with_rtscts( | 171 | pub fn new_with_rtscts<T: Instance>( |
| 168 | uarte: Peri<'d, T>, | 172 | uarte: Peri<'d, T>, |
| 169 | rxd: Peri<'d, impl GpioPin>, | 173 | rxd: Peri<'d, impl GpioPin>, |
| 170 | txd: Peri<'d, impl GpioPin>, | 174 | txd: Peri<'d, impl GpioPin>, |
| @@ -183,8 +187,8 @@ impl<'d, T: Instance> Uarte<'d, T> { | |||
| 183 | ) | 187 | ) |
| 184 | } | 188 | } |
| 185 | 189 | ||
| 186 | fn new_inner( | 190 | fn new_inner<T: Instance>( |
| 187 | uarte: Peri<'d, T>, | 191 | _uarte: Peri<'d, T>, |
| 188 | rxd: Peri<'d, AnyPin>, | 192 | rxd: Peri<'d, AnyPin>, |
| 189 | txd: Peri<'d, AnyPin>, | 193 | txd: Peri<'d, AnyPin>, |
| 190 | cts: Option<Peri<'d, AnyPin>>, | 194 | cts: Option<Peri<'d, AnyPin>>, |
| @@ -211,16 +215,22 @@ impl<'d, T: Instance> Uarte<'d, T> { | |||
| 211 | 215 | ||
| 212 | Self { | 216 | Self { |
| 213 | tx: UarteTx { | 217 | tx: UarteTx { |
| 214 | _p: unsafe { uarte.clone_unchecked() }, | 218 | r: T::regs(), |
| 219 | state: T::state(), | ||
| 220 | _p: PhantomData {}, | ||
| 221 | }, | ||
| 222 | rx: UarteRx { | ||
| 223 | r: T::regs(), | ||
| 224 | state: T::state(), | ||
| 225 | _p: PhantomData {}, | ||
| 215 | }, | 226 | }, |
| 216 | rx: UarteRx { _p: uarte }, | ||
| 217 | } | 227 | } |
| 218 | } | 228 | } |
| 219 | 229 | ||
| 220 | /// Split the Uarte into the transmitter and receiver parts. | 230 | /// Split the Uarte into the transmitter and receiver parts. |
| 221 | /// | 231 | /// |
| 222 | /// This is useful to concurrently transmit and receive from independent tasks. | 232 | /// This is useful to concurrently transmit and receive from independent tasks. |
| 223 | pub fn split(self) -> (UarteTx<'d, T>, UarteRx<'d, T>) { | 233 | pub fn split(self) -> (UarteTx<'d>, UarteRx<'d>) { |
| 224 | (self.tx, self.rx) | 234 | (self.tx, self.rx) |
| 225 | } | 235 | } |
| 226 | 236 | ||
| @@ -228,7 +238,7 @@ impl<'d, T: Instance> Uarte<'d, T> { | |||
| 228 | /// | 238 | /// |
| 229 | /// The returned halves borrow from `self`, so you can drop them and go back to using | 239 | /// The returned halves borrow from `self`, so you can drop them and go back to using |
| 230 | /// the "un-split" `self`. This allows temporarily splitting the UART. | 240 | /// the "un-split" `self`. This allows temporarily splitting the UART. |
| 231 | pub fn split_by_ref(&mut self) -> (&mut UarteTx<'d, T>, &mut UarteRx<'d, T>) { | 241 | pub fn split_by_ref(&mut self) -> (&mut UarteTx<'d>, &mut UarteRx<'d>) { |
| 232 | (&mut self.tx, &mut self.rx) | 242 | (&mut self.tx, &mut self.rx) |
| 233 | } | 243 | } |
| 234 | 244 | ||
| @@ -240,13 +250,13 @@ impl<'d, T: Instance> Uarte<'d, T> { | |||
| 240 | timer: Peri<'d, U>, | 250 | timer: Peri<'d, U>, |
| 241 | ppi_ch1: Peri<'d, impl ConfigurableChannel + 'd>, | 251 | ppi_ch1: Peri<'d, impl ConfigurableChannel + 'd>, |
| 242 | ppi_ch2: Peri<'d, impl ConfigurableChannel + 'd>, | 252 | ppi_ch2: Peri<'d, impl ConfigurableChannel + 'd>, |
| 243 | ) -> (UarteTx<'d, T>, UarteRxWithIdle<'d, T, U>) { | 253 | ) -> (UarteTx<'d>, UarteRxWithIdle<'d>) { |
| 244 | (self.tx, self.rx.with_idle(timer, ppi_ch1, ppi_ch2)) | 254 | (self.tx, self.rx.with_idle(timer, ppi_ch1, ppi_ch2)) |
| 245 | } | 255 | } |
| 246 | 256 | ||
| 247 | /// Return the endtx event for use with PPI | 257 | /// Return the endtx event for use with PPI |
| 248 | pub fn event_endtx(&self) -> Event<'_> { | 258 | pub fn event_endtx(&self) -> Event<'_> { |
| 249 | let r = T::regs(); | 259 | let r = self.tx.r; |
| 250 | Event::from_reg(r.events_endtx()) | 260 | Event::from_reg(r.events_endtx()) |
| 251 | } | 261 | } |
| 252 | 262 | ||
| @@ -343,9 +353,9 @@ pub(crate) fn configure(r: pac::uarte::Uarte, config: Config, hardware_flow_cont | |||
| 343 | apply_workaround_for_enable_anomaly(r); | 353 | apply_workaround_for_enable_anomaly(r); |
| 344 | } | 354 | } |
| 345 | 355 | ||
| 346 | impl<'d, T: Instance> UarteTx<'d, T> { | 356 | impl<'d> UarteTx<'d> { |
| 347 | /// Create a new tx-only UARTE without hardware flow control | 357 | /// Create a new tx-only UARTE without hardware flow control |
| 348 | pub fn new( | 358 | pub fn new<T: Instance>( |
| 349 | uarte: Peri<'d, T>, | 359 | uarte: Peri<'d, T>, |
| 350 | _irq: impl interrupt::typelevel::Binding<T::Interrupt, InterruptHandler<T>> + 'd, | 360 | _irq: impl interrupt::typelevel::Binding<T::Interrupt, InterruptHandler<T>> + 'd, |
| 351 | txd: Peri<'d, impl GpioPin>, | 361 | txd: Peri<'d, impl GpioPin>, |
| @@ -355,7 +365,7 @@ impl<'d, T: Instance> UarteTx<'d, T> { | |||
| 355 | } | 365 | } |
| 356 | 366 | ||
| 357 | /// Create a new tx-only UARTE with hardware flow control (RTS/CTS) | 367 | /// Create a new tx-only UARTE with hardware flow control (RTS/CTS) |
| 358 | pub fn new_with_rtscts( | 368 | pub fn new_with_rtscts<T: Instance>( |
| 359 | uarte: Peri<'d, T>, | 369 | uarte: Peri<'d, T>, |
| 360 | _irq: impl interrupt::typelevel::Binding<T::Interrupt, InterruptHandler<T>> + 'd, | 370 | _irq: impl interrupt::typelevel::Binding<T::Interrupt, InterruptHandler<T>> + 'd, |
| 361 | txd: Peri<'d, impl GpioPin>, | 371 | txd: Peri<'d, impl GpioPin>, |
| @@ -365,7 +375,12 @@ impl<'d, T: Instance> UarteTx<'d, T> { | |||
| 365 | Self::new_inner(uarte, txd.into(), Some(cts.into()), config) | 375 | Self::new_inner(uarte, txd.into(), Some(cts.into()), config) |
| 366 | } | 376 | } |
| 367 | 377 | ||
| 368 | fn new_inner(uarte: Peri<'d, T>, txd: Peri<'d, AnyPin>, cts: Option<Peri<'d, AnyPin>>, config: Config) -> Self { | 378 | fn new_inner<T: Instance>( |
| 379 | _uarte: Peri<'d, T>, | ||
| 380 | txd: Peri<'d, AnyPin>, | ||
| 381 | cts: Option<Peri<'d, AnyPin>>, | ||
| 382 | config: Config, | ||
| 383 | ) -> Self { | ||
| 369 | let r = T::regs(); | 384 | let r = T::regs(); |
| 370 | 385 | ||
| 371 | configure(r, config, cts.is_some()); | 386 | configure(r, config, cts.is_some()); |
| @@ -378,7 +393,11 @@ impl<'d, T: Instance> UarteTx<'d, T> { | |||
| 378 | let s = T::state(); | 393 | let s = T::state(); |
| 379 | s.tx_rx_refcount.store(1, Ordering::Relaxed); | 394 | s.tx_rx_refcount.store(1, Ordering::Relaxed); |
| 380 | 395 | ||
| 381 | Self { _p: uarte } | 396 | Self { |
| 397 | r: T::regs(), | ||
| 398 | state: T::state(), | ||
| 399 | _p: PhantomData {}, | ||
| 400 | } | ||
| 382 | } | 401 | } |
| 383 | 402 | ||
| 384 | /// Write all bytes in the buffer. | 403 | /// Write all bytes in the buffer. |
| @@ -409,8 +428,8 @@ impl<'d, T: Instance> UarteTx<'d, T> { | |||
| 409 | let ptr = buffer.as_ptr(); | 428 | let ptr = buffer.as_ptr(); |
| 410 | let len = buffer.len(); | 429 | let len = buffer.len(); |
| 411 | 430 | ||
| 412 | let r = T::regs(); | 431 | let r = self.r; |
| 413 | let s = T::state(); | 432 | let s = self.state; |
| 414 | 433 | ||
| 415 | let drop = OnDrop::new(move || { | 434 | let drop = OnDrop::new(move || { |
| 416 | trace!("write drop: stopping"); | 435 | trace!("write drop: stopping"); |
| @@ -479,7 +498,7 @@ impl<'d, T: Instance> UarteTx<'d, T> { | |||
| 479 | let ptr = buffer.as_ptr(); | 498 | let ptr = buffer.as_ptr(); |
| 480 | let len = buffer.len(); | 499 | let len = buffer.len(); |
| 481 | 500 | ||
| 482 | let r = T::regs(); | 501 | let r = self.r; |
| 483 | 502 | ||
| 484 | r.txd().ptr().write_value(ptr as u32); | 503 | r.txd().ptr().write_value(ptr as u32); |
| 485 | r.txd().maxcnt().write(|w| w.set_maxcnt(len as _)); | 504 | r.txd().maxcnt().write(|w| w.set_maxcnt(len as _)); |
| @@ -501,11 +520,11 @@ impl<'d, T: Instance> UarteTx<'d, T> { | |||
| 501 | } | 520 | } |
| 502 | } | 521 | } |
| 503 | 522 | ||
| 504 | impl<'a, T: Instance> Drop for UarteTx<'a, T> { | 523 | impl<'a> Drop for UarteTx<'a> { |
| 505 | fn drop(&mut self) { | 524 | fn drop(&mut self) { |
| 506 | trace!("uarte tx drop"); | 525 | trace!("uarte tx drop"); |
| 507 | 526 | ||
| 508 | let r = T::regs(); | 527 | let r = self.r; |
| 509 | 528 | ||
| 510 | let did_stoptx = r.events_txstarted().read() != 0; | 529 | let did_stoptx = r.events_txstarted().read() != 0; |
| 511 | trace!("did_stoptx {}", did_stoptx); | 530 | trace!("did_stoptx {}", did_stoptx); |
| @@ -513,15 +532,15 @@ impl<'a, T: Instance> Drop for UarteTx<'a, T> { | |||
| 513 | // Wait for txstopped, if needed. | 532 | // Wait for txstopped, if needed. |
| 514 | while did_stoptx && r.events_txstopped().read() == 0 {} | 533 | while did_stoptx && r.events_txstopped().read() == 0 {} |
| 515 | 534 | ||
| 516 | let s = T::state(); | 535 | let s = self.state; |
| 517 | 536 | ||
| 518 | drop_tx_rx(r, s); | 537 | drop_tx_rx(r, s); |
| 519 | } | 538 | } |
| 520 | } | 539 | } |
| 521 | 540 | ||
| 522 | impl<'d, T: Instance> UarteRx<'d, T> { | 541 | impl<'d> UarteRx<'d> { |
| 523 | /// Create a new rx-only UARTE without hardware flow control | 542 | /// Create a new rx-only UARTE without hardware flow control |
| 524 | pub fn new( | 543 | pub fn new<T: Instance>( |
| 525 | uarte: Peri<'d, T>, | 544 | uarte: Peri<'d, T>, |
| 526 | _irq: impl interrupt::typelevel::Binding<T::Interrupt, InterruptHandler<T>> + 'd, | 545 | _irq: impl interrupt::typelevel::Binding<T::Interrupt, InterruptHandler<T>> + 'd, |
| 527 | rxd: Peri<'d, impl GpioPin>, | 546 | rxd: Peri<'d, impl GpioPin>, |
| @@ -531,7 +550,7 @@ impl<'d, T: Instance> UarteRx<'d, T> { | |||
| 531 | } | 550 | } |
| 532 | 551 | ||
| 533 | /// Create a new rx-only UARTE with hardware flow control (RTS/CTS) | 552 | /// Create a new rx-only UARTE with hardware flow control (RTS/CTS) |
| 534 | pub fn new_with_rtscts( | 553 | pub fn new_with_rtscts<T: Instance>( |
| 535 | uarte: Peri<'d, T>, | 554 | uarte: Peri<'d, T>, |
| 536 | _irq: impl interrupt::typelevel::Binding<T::Interrupt, InterruptHandler<T>> + 'd, | 555 | _irq: impl interrupt::typelevel::Binding<T::Interrupt, InterruptHandler<T>> + 'd, |
| 537 | rxd: Peri<'d, impl GpioPin>, | 556 | rxd: Peri<'d, impl GpioPin>, |
| @@ -543,13 +562,18 @@ impl<'d, T: Instance> UarteRx<'d, T> { | |||
| 543 | 562 | ||
| 544 | /// Check for errors and clear the error register if an error occured. | 563 | /// Check for errors and clear the error register if an error occured. |
| 545 | fn check_and_clear_errors(&mut self) -> Result<(), Error> { | 564 | fn check_and_clear_errors(&mut self) -> Result<(), Error> { |
| 546 | let r = T::regs(); | 565 | let r = self.r; |
| 547 | let err_bits = r.errorsrc().read(); | 566 | let err_bits = r.errorsrc().read(); |
| 548 | r.errorsrc().write_value(err_bits); | 567 | r.errorsrc().write_value(err_bits); |
| 549 | ErrorSource::from_bits_truncate(err_bits.0).check() | 568 | ErrorSource::from_bits_truncate(err_bits.0).check() |
| 550 | } | 569 | } |
| 551 | 570 | ||
| 552 | fn new_inner(uarte: Peri<'d, T>, rxd: Peri<'d, AnyPin>, rts: Option<Peri<'d, AnyPin>>, config: Config) -> Self { | 571 | fn new_inner<T: Instance>( |
| 572 | _uarte: Peri<'d, T>, | ||
| 573 | rxd: Peri<'d, AnyPin>, | ||
| 574 | rts: Option<Peri<'d, AnyPin>>, | ||
| 575 | config: Config, | ||
| 576 | ) -> Self { | ||
| 553 | let r = T::regs(); | 577 | let r = T::regs(); |
| 554 | 578 | ||
| 555 | configure(r, config, rts.is_some()); | 579 | configure(r, config, rts.is_some()); |
| @@ -562,7 +586,11 @@ impl<'d, T: Instance> UarteRx<'d, T> { | |||
| 562 | let s = T::state(); | 586 | let s = T::state(); |
| 563 | s.tx_rx_refcount.store(1, Ordering::Relaxed); | 587 | s.tx_rx_refcount.store(1, Ordering::Relaxed); |
| 564 | 588 | ||
| 565 | Self { _p: uarte } | 589 | Self { |
| 590 | r: T::regs(), | ||
| 591 | state: T::state(), | ||
| 592 | _p: PhantomData {}, | ||
| 593 | } | ||
| 566 | } | 594 | } |
| 567 | 595 | ||
| 568 | /// Upgrade to an instance that supports idle line detection. | 596 | /// Upgrade to an instance that supports idle line detection. |
| @@ -571,10 +599,10 @@ impl<'d, T: Instance> UarteRx<'d, T> { | |||
| 571 | timer: Peri<'d, U>, | 599 | timer: Peri<'d, U>, |
| 572 | ppi_ch1: Peri<'d, impl ConfigurableChannel + 'd>, | 600 | ppi_ch1: Peri<'d, impl ConfigurableChannel + 'd>, |
| 573 | ppi_ch2: Peri<'d, impl ConfigurableChannel + 'd>, | 601 | ppi_ch2: Peri<'d, impl ConfigurableChannel + 'd>, |
| 574 | ) -> UarteRxWithIdle<'d, T, U> { | 602 | ) -> UarteRxWithIdle<'d> { |
| 575 | let timer = Timer::new(timer); | 603 | let timer = Timer::new(timer); |
| 576 | 604 | ||
| 577 | let r = T::regs(); | 605 | let r = self.r; |
| 578 | 606 | ||
| 579 | // BAUDRATE register values are `baudrate * 2^32 / 16000000` | 607 | // BAUDRATE register values are `baudrate * 2^32 / 16000000` |
| 580 | // source: https://devzone.nordicsemi.com/f/nordic-q-a/391/uart-baudrate-register-values | 608 | // source: https://devzone.nordicsemi.com/f/nordic-q-a/391/uart-baudrate-register-values |
| @@ -605,11 +633,15 @@ impl<'d, T: Instance> UarteRx<'d, T> { | |||
| 605 | ); | 633 | ); |
| 606 | ppi_ch2.enable(); | 634 | ppi_ch2.enable(); |
| 607 | 635 | ||
| 636 | let state = self.state; | ||
| 637 | |||
| 608 | UarteRxWithIdle { | 638 | UarteRxWithIdle { |
| 609 | rx: self, | 639 | rx: self, |
| 610 | timer, | 640 | timer, |
| 611 | ppi_ch1, | 641 | ppi_ch1: ppi_ch1, |
| 612 | _ppi_ch2: ppi_ch2, | 642 | _ppi_ch2: ppi_ch2, |
| 643 | r: r, | ||
| 644 | state: state, | ||
| 613 | } | 645 | } |
| 614 | } | 646 | } |
| 615 | 647 | ||
| @@ -625,8 +657,8 @@ impl<'d, T: Instance> UarteRx<'d, T> { | |||
| 625 | let ptr = buffer.as_ptr(); | 657 | let ptr = buffer.as_ptr(); |
| 626 | let len = buffer.len(); | 658 | let len = buffer.len(); |
| 627 | 659 | ||
| 628 | let r = T::regs(); | 660 | let r = self.r; |
| 629 | let s = T::state(); | 661 | let s = self.state; |
| 630 | 662 | ||
| 631 | let drop = OnDrop::new(move || { | 663 | let drop = OnDrop::new(move || { |
| 632 | trace!("read drop: stopping"); | 664 | trace!("read drop: stopping"); |
| @@ -692,7 +724,7 @@ impl<'d, T: Instance> UarteRx<'d, T> { | |||
| 692 | let ptr = buffer.as_ptr(); | 724 | let ptr = buffer.as_ptr(); |
| 693 | let len = buffer.len(); | 725 | let len = buffer.len(); |
| 694 | 726 | ||
| 695 | let r = T::regs(); | 727 | let r = self.r; |
| 696 | 728 | ||
| 697 | r.rxd().ptr().write_value(ptr as u32); | 729 | r.rxd().ptr().write_value(ptr as u32); |
| 698 | r.rxd().maxcnt().write(|w| w.set_maxcnt(len as _)); | 730 | r.rxd().maxcnt().write(|w| w.set_maxcnt(len as _)); |
| @@ -718,11 +750,11 @@ impl<'d, T: Instance> UarteRx<'d, T> { | |||
| 718 | } | 750 | } |
| 719 | } | 751 | } |
| 720 | 752 | ||
| 721 | impl<'a, T: Instance> Drop for UarteRx<'a, T> { | 753 | impl<'a> Drop for UarteRx<'a> { |
| 722 | fn drop(&mut self) { | 754 | fn drop(&mut self) { |
| 723 | trace!("uarte rx drop"); | 755 | trace!("uarte rx drop"); |
| 724 | 756 | ||
| 725 | let r = T::regs(); | 757 | let r = self.r; |
| 726 | 758 | ||
| 727 | let did_stoprx = r.events_rxstarted().read() != 0; | 759 | let did_stoprx = r.events_rxstarted().read() != 0; |
| 728 | trace!("did_stoprx {}", did_stoprx); | 760 | trace!("did_stoprx {}", did_stoprx); |
| @@ -730,7 +762,7 @@ impl<'a, T: Instance> Drop for UarteRx<'a, T> { | |||
| 730 | // Wait for rxto, if needed. | 762 | // Wait for rxto, if needed. |
| 731 | while did_stoprx && r.events_rxto().read() == 0 {} | 763 | while did_stoprx && r.events_rxto().read() == 0 {} |
| 732 | 764 | ||
| 733 | let s = T::state(); | 765 | let s = self.state; |
| 734 | 766 | ||
| 735 | drop_tx_rx(r, s); | 767 | drop_tx_rx(r, s); |
| 736 | } | 768 | } |
| @@ -739,14 +771,16 @@ impl<'a, T: Instance> Drop for UarteRx<'a, T> { | |||
| 739 | /// Receiver part of the UARTE driver, with `read_until_idle` support. | 771 | /// Receiver part of the UARTE driver, with `read_until_idle` support. |
| 740 | /// | 772 | /// |
| 741 | /// This can be obtained via [`Uarte::split_with_idle`]. | 773 | /// This can be obtained via [`Uarte::split_with_idle`]. |
| 742 | pub struct UarteRxWithIdle<'d, T: Instance, U: TimerInstance> { | 774 | pub struct UarteRxWithIdle<'d> { |
| 743 | rx: UarteRx<'d, T>, | 775 | rx: UarteRx<'d>, |
| 744 | timer: Timer<'d, U>, | 776 | timer: Timer<'d>, |
| 745 | ppi_ch1: Ppi<'d, AnyConfigurableChannel, 1, 2>, | 777 | ppi_ch1: Ppi<'d, AnyConfigurableChannel, 1, 2>, |
| 746 | _ppi_ch2: Ppi<'d, AnyConfigurableChannel, 1, 1>, | 778 | _ppi_ch2: Ppi<'d, AnyConfigurableChannel, 1, 1>, |
| 779 | r: pac::uarte::Uarte, | ||
| 780 | state: &'static State, | ||
| 747 | } | 781 | } |
| 748 | 782 | ||
| 749 | impl<'d, T: Instance, U: TimerInstance> UarteRxWithIdle<'d, T, U> { | 783 | impl<'d> UarteRxWithIdle<'d> { |
| 750 | /// Read bytes until the buffer is filled. | 784 | /// Read bytes until the buffer is filled. |
| 751 | pub async fn read(&mut self, buffer: &mut [u8]) -> Result<(), Error> { | 785 | pub async fn read(&mut self, buffer: &mut [u8]) -> Result<(), Error> { |
| 752 | self.ppi_ch1.disable(); | 786 | self.ppi_ch1.disable(); |
| @@ -773,8 +807,8 @@ impl<'d, T: Instance, U: TimerInstance> UarteRxWithIdle<'d, T, U> { | |||
| 773 | let ptr = buffer.as_ptr(); | 807 | let ptr = buffer.as_ptr(); |
| 774 | let len = buffer.len(); | 808 | let len = buffer.len(); |
| 775 | 809 | ||
| 776 | let r = T::regs(); | 810 | let r = self.r; |
| 777 | let s = T::state(); | 811 | let s = self.state; |
| 778 | 812 | ||
| 779 | self.ppi_ch1.enable(); | 813 | self.ppi_ch1.enable(); |
| 780 | 814 | ||
| @@ -846,7 +880,7 @@ impl<'d, T: Instance, U: TimerInstance> UarteRxWithIdle<'d, T, U> { | |||
| 846 | let ptr = buffer.as_ptr(); | 880 | let ptr = buffer.as_ptr(); |
| 847 | let len = buffer.len(); | 881 | let len = buffer.len(); |
| 848 | 882 | ||
| 849 | let r = T::regs(); | 883 | let r = self.r; |
| 850 | 884 | ||
| 851 | self.ppi_ch1.enable(); | 885 | self.ppi_ch1.enable(); |
| 852 | 886 | ||
| @@ -997,7 +1031,7 @@ macro_rules! impl_uarte { | |||
| 997 | mod eh02 { | 1031 | mod eh02 { |
| 998 | use super::*; | 1032 | use super::*; |
| 999 | 1033 | ||
| 1000 | impl<'d, T: Instance> embedded_hal_02::blocking::serial::Write<u8> for Uarte<'d, T> { | 1034 | impl<'d> embedded_hal_02::blocking::serial::Write<u8> for Uarte<'d> { |
| 1001 | type Error = Error; | 1035 | type Error = Error; |
| 1002 | 1036 | ||
| 1003 | fn bwrite_all(&mut self, buffer: &[u8]) -> Result<(), Self::Error> { | 1037 | fn bwrite_all(&mut self, buffer: &[u8]) -> Result<(), Self::Error> { |
| @@ -1009,7 +1043,7 @@ mod eh02 { | |||
| 1009 | } | 1043 | } |
| 1010 | } | 1044 | } |
| 1011 | 1045 | ||
| 1012 | impl<'d, T: Instance> embedded_hal_02::blocking::serial::Write<u8> for UarteTx<'d, T> { | 1046 | impl<'d> embedded_hal_02::blocking::serial::Write<u8> for UarteTx<'d> { |
| 1013 | type Error = Error; | 1047 | type Error = Error; |
| 1014 | 1048 | ||
| 1015 | fn bwrite_all(&mut self, buffer: &[u8]) -> Result<(), Self::Error> { | 1049 | fn bwrite_all(&mut self, buffer: &[u8]) -> Result<(), Self::Error> { |
| @@ -1038,22 +1072,22 @@ mod _embedded_io { | |||
| 1038 | } | 1072 | } |
| 1039 | } | 1073 | } |
| 1040 | 1074 | ||
| 1041 | impl<'d, U: Instance> embedded_io_async::ErrorType for Uarte<'d, U> { | 1075 | impl<'d> embedded_io_async::ErrorType for Uarte<'d> { |
| 1042 | type Error = Error; | 1076 | type Error = Error; |
| 1043 | } | 1077 | } |
| 1044 | 1078 | ||
| 1045 | impl<'d, U: Instance> embedded_io_async::ErrorType for UarteTx<'d, U> { | 1079 | impl<'d> embedded_io_async::ErrorType for UarteTx<'d> { |
| 1046 | type Error = Error; | 1080 | type Error = Error; |
| 1047 | } | 1081 | } |
| 1048 | 1082 | ||
| 1049 | impl<'d, U: Instance> embedded_io_async::Write for Uarte<'d, U> { | 1083 | impl<'d> embedded_io_async::Write for Uarte<'d> { |
| 1050 | async fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> { | 1084 | async fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> { |
| 1051 | self.write(buf).await?; | 1085 | self.write(buf).await?; |
| 1052 | Ok(buf.len()) | 1086 | Ok(buf.len()) |
| 1053 | } | 1087 | } |
| 1054 | } | 1088 | } |
| 1055 | 1089 | ||
| 1056 | impl<'d: 'd, U: Instance> embedded_io_async::Write for UarteTx<'d, U> { | 1090 | impl<'d> embedded_io_async::Write for UarteTx<'d> { |
| 1057 | async fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> { | 1091 | async fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> { |
| 1058 | self.write(buf).await?; | 1092 | self.write(buf).await?; |
| 1059 | Ok(buf.len()) | 1093 | Ok(buf.len()) |
