aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDario Nieuwenhuis <[email protected]>2025-09-27 19:20:11 +0200
committerDario Nieuwenhuis <[email protected]>2025-09-28 21:05:37 +0200
commit00db482c2b701b6e8f7013ba63d6e1efe3b5c62d (patch)
tree91d7cdd77c2176722dfcfd3fce3da2fbd446035c
parentfd574ffd091b579f30655e349aef16fbfc2bbb75 (diff)
nrf/twis: erase instance generics
-rw-r--r--embassy-nrf/src/twis.rs65
1 files changed, 34 insertions, 31 deletions
diff --git a/embassy-nrf/src/twis.rs b/embassy-nrf/src/twis.rs
index c77d0f048..dd4978b3e 100644
--- a/embassy-nrf/src/twis.rs
+++ b/embassy-nrf/src/twis.rs
@@ -140,14 +140,16 @@ impl<T: Instance> interrupt::typelevel::Handler<T::Interrupt> for InterruptHandl
140} 140}
141 141
142/// TWIS driver. 142/// TWIS driver.
143pub struct Twis<'d, T: Instance> { 143pub struct Twis<'d> {
144 _p: Peri<'d, T>, 144 r: pac::twis::Twis,
145 state: &'static State,
146 _p: PhantomData<&'d ()>,
145} 147}
146 148
147impl<'d, T: Instance> Twis<'d, T> { 149impl<'d> Twis<'d> {
148 /// Create a new TWIS driver. 150 /// Create a new TWIS driver.
149 pub fn new( 151 pub fn new<T: Instance>(
150 twis: Peri<'d, T>, 152 _twis: Peri<'d, T>,
151 _irq: impl interrupt::typelevel::Binding<T::Interrupt, InterruptHandler<T>> + 'd, 153 _irq: impl interrupt::typelevel::Binding<T::Interrupt, InterruptHandler<T>> + 'd,
152 sda: Peri<'d, impl GpioPin>, 154 sda: Peri<'d, impl GpioPin>,
153 scl: Peri<'d, impl GpioPin>, 155 scl: Peri<'d, impl GpioPin>,
@@ -206,7 +208,11 @@ impl<'d, T: Instance> Twis<'d, T> {
206 T::Interrupt::unpend(); 208 T::Interrupt::unpend();
207 unsafe { T::Interrupt::enable() }; 209 unsafe { T::Interrupt::enable() };
208 210
209 Self { _p: twis } 211 Self {
212 r: T::regs(),
213 state: T::state(),
214 _p: PhantomData,
215 }
210 } 216 }
211 217
212 /// Set TX buffer, checking that it is in RAM and has suitable length. 218 /// Set TX buffer, checking that it is in RAM and has suitable length.
@@ -217,7 +223,7 @@ impl<'d, T: Instance> Twis<'d, T> {
217 return Err(Error::TxBufferTooLong); 223 return Err(Error::TxBufferTooLong);
218 } 224 }
219 225
220 let r = T::regs(); 226 let r = self.r;
221 227
222 // We're giving the register a pointer to the stack. Since we're 228 // We're giving the register a pointer to the stack. Since we're
223 // waiting for the I2C transaction to end before this stack pointer 229 // waiting for the I2C transaction to end before this stack pointer
@@ -244,7 +250,7 @@ impl<'d, T: Instance> Twis<'d, T> {
244 return Err(Error::RxBufferTooLong); 250 return Err(Error::RxBufferTooLong);
245 } 251 }
246 252
247 let r = T::regs(); 253 let r = self.r;
248 254
249 // We're giving the register a pointer to the stack. Since we're 255 // We're giving the register a pointer to the stack. Since we're
250 // waiting for the I2C transaction to end before this stack pointer 256 // waiting for the I2C transaction to end before this stack pointer
@@ -266,7 +272,7 @@ impl<'d, T: Instance> Twis<'d, T> {
266 } 272 }
267 273
268 fn clear_errorsrc(&mut self) { 274 fn clear_errorsrc(&mut self) {
269 let r = T::regs(); 275 let r = self.r;
270 r.errorsrc().write(|w| { 276 r.errorsrc().write(|w| {
271 w.set_overflow(true); 277 w.set_overflow(true);
272 w.set_overread(true); 278 w.set_overread(true);
@@ -276,18 +282,18 @@ impl<'d, T: Instance> Twis<'d, T> {
276 282
277 /// Returns matched address for latest command. 283 /// Returns matched address for latest command.
278 pub fn address_match(&self) -> u8 { 284 pub fn address_match(&self) -> u8 {
279 let r = T::regs(); 285 let r = self.r;
280 r.address(r.match_().read().0 as usize).read().address() 286 r.address(r.match_().read().0 as usize).read().address()
281 } 287 }
282 288
283 /// Returns the index of the address matched in the latest command. 289 /// Returns the index of the address matched in the latest command.
284 pub fn address_match_index(&self) -> usize { 290 pub fn address_match_index(&self) -> usize {
285 T::regs().match_().read().0 as _ 291 self.r.match_().read().0 as _
286 } 292 }
287 293
288 /// Wait for read, write, stop or error 294 /// Wait for read, write, stop or error
289 fn blocking_listen_wait(&mut self) -> Result<Status, Error> { 295 fn blocking_listen_wait(&mut self) -> Result<Status, Error> {
290 let r = T::regs(); 296 let r = self.r;
291 loop { 297 loop {
292 if r.events_error().read() != 0 { 298 if r.events_error().read() != 0 {
293 r.events_error().write_value(0); 299 r.events_error().write_value(0);
@@ -312,7 +318,7 @@ impl<'d, T: Instance> Twis<'d, T> {
312 318
313 /// Wait for stop, repeated start or error 319 /// Wait for stop, repeated start or error
314 fn blocking_listen_wait_end(&mut self, status: Status) -> Result<Command, Error> { 320 fn blocking_listen_wait_end(&mut self, status: Status) -> Result<Command, Error> {
315 let r = T::regs(); 321 let r = self.r;
316 loop { 322 loop {
317 // stop if an error occurred 323 // stop if an error occurred
318 if r.events_error().read() != 0 { 324 if r.events_error().read() != 0 {
@@ -338,7 +344,7 @@ impl<'d, T: Instance> Twis<'d, T> {
338 344
339 /// Wait for stop or error 345 /// Wait for stop or error
340 fn blocking_wait(&mut self) -> Result<usize, Error> { 346 fn blocking_wait(&mut self) -> Result<usize, Error> {
341 let r = T::regs(); 347 let r = self.r;
342 loop { 348 loop {
343 // stop if an error occurred 349 // stop if an error occurred
344 if r.events_error().read() != 0 { 350 if r.events_error().read() != 0 {
@@ -363,7 +369,7 @@ impl<'d, T: Instance> Twis<'d, T> {
363 /// Wait for stop or error with timeout 369 /// Wait for stop or error with timeout
364 #[cfg(feature = "time")] 370 #[cfg(feature = "time")]
365 fn blocking_wait_timeout(&mut self, timeout: Duration) -> Result<usize, Error> { 371 fn blocking_wait_timeout(&mut self, timeout: Duration) -> Result<usize, Error> {
366 let r = T::regs(); 372 let r = self.r;
367 let deadline = Instant::now() + timeout; 373 let deadline = Instant::now() + timeout;
368 loop { 374 loop {
369 // stop if an error occurred 375 // stop if an error occurred
@@ -392,7 +398,7 @@ impl<'d, T: Instance> Twis<'d, T> {
392 /// Wait for read, write, stop or error with timeout 398 /// Wait for read, write, stop or error with timeout
393 #[cfg(feature = "time")] 399 #[cfg(feature = "time")]
394 fn blocking_listen_wait_timeout(&mut self, timeout: Duration) -> Result<Status, Error> { 400 fn blocking_listen_wait_timeout(&mut self, timeout: Duration) -> Result<Status, Error> {
395 let r = T::regs(); 401 let r = self.r;
396 let deadline = Instant::now() + timeout; 402 let deadline = Instant::now() + timeout;
397 loop { 403 loop {
398 if r.events_error().read() != 0 { 404 if r.events_error().read() != 0 {
@@ -423,7 +429,7 @@ impl<'d, T: Instance> Twis<'d, T> {
423 /// Wait for stop, repeated start or error with timeout 429 /// Wait for stop, repeated start or error with timeout
424 #[cfg(feature = "time")] 430 #[cfg(feature = "time")]
425 fn blocking_listen_wait_end_timeout(&mut self, status: Status, timeout: Duration) -> Result<Command, Error> { 431 fn blocking_listen_wait_end_timeout(&mut self, status: Status, timeout: Duration) -> Result<Command, Error> {
426 let r = T::regs(); 432 let r = self.r;
427 let deadline = Instant::now() + timeout; 433 let deadline = Instant::now() + timeout;
428 loop { 434 loop {
429 // stop if an error occurred 435 // stop if an error occurred
@@ -453,10 +459,9 @@ impl<'d, T: Instance> Twis<'d, T> {
453 459
454 /// Wait for stop or error 460 /// Wait for stop or error
455 fn async_wait(&mut self) -> impl Future<Output = Result<usize, Error>> { 461 fn async_wait(&mut self) -> impl Future<Output = Result<usize, Error>> {
462 let r = self.r;
463 let s = self.state;
456 poll_fn(move |cx| { 464 poll_fn(move |cx| {
457 let r = T::regs();
458 let s = T::state();
459
460 s.waker.register(cx.waker()); 465 s.waker.register(cx.waker());
461 466
462 // stop if an error occurred 467 // stop if an error occurred
@@ -483,10 +488,9 @@ impl<'d, T: Instance> Twis<'d, T> {
483 488
484 /// Wait for read or write 489 /// Wait for read or write
485 fn async_listen_wait(&mut self) -> impl Future<Output = Result<Status, Error>> { 490 fn async_listen_wait(&mut self) -> impl Future<Output = Result<Status, Error>> {
491 let r = self.r;
492 let s = self.state;
486 poll_fn(move |cx| { 493 poll_fn(move |cx| {
487 let r = T::regs();
488 let s = T::state();
489
490 s.waker.register(cx.waker()); 494 s.waker.register(cx.waker());
491 495
492 // stop if an error occurred 496 // stop if an error occurred
@@ -510,10 +514,9 @@ impl<'d, T: Instance> Twis<'d, T> {
510 514
511 /// Wait for stop, repeated start or error 515 /// Wait for stop, repeated start or error
512 fn async_listen_wait_end(&mut self, status: Status) -> impl Future<Output = Result<Command, Error>> { 516 fn async_listen_wait_end(&mut self, status: Status) -> impl Future<Output = Result<Command, Error>> {
517 let r = self.r;
518 let s = self.state;
513 poll_fn(move |cx| { 519 poll_fn(move |cx| {
514 let r = T::regs();
515 let s = T::state();
516
517 s.waker.register(cx.waker()); 520 s.waker.register(cx.waker());
518 521
519 // stop if an error occurred 522 // stop if an error occurred
@@ -540,7 +543,7 @@ impl<'d, T: Instance> Twis<'d, T> {
540 } 543 }
541 544
542 fn setup_respond_from_ram(&mut self, buffer: &[u8], inten: bool) -> Result<(), Error> { 545 fn setup_respond_from_ram(&mut self, buffer: &[u8], inten: bool) -> Result<(), Error> {
543 let r = T::regs(); 546 let r = self.r;
544 547
545 compiler_fence(SeqCst); 548 compiler_fence(SeqCst);
546 549
@@ -584,7 +587,7 @@ impl<'d, T: Instance> Twis<'d, T> {
584 } 587 }
585 588
586 fn setup_listen(&mut self, buffer: &mut [u8], inten: bool) -> Result<(), Error> { 589 fn setup_listen(&mut self, buffer: &mut [u8], inten: bool) -> Result<(), Error> {
587 let r = T::regs(); 590 let r = self.r;
588 compiler_fence(SeqCst); 591 compiler_fence(SeqCst);
589 592
590 // Set up the DMA read. 593 // Set up the DMA read.
@@ -620,7 +623,7 @@ impl<'d, T: Instance> Twis<'d, T> {
620 } 623 }
621 624
622 fn setup_listen_end(&mut self, inten: bool) -> Result<(), Error> { 625 fn setup_listen_end(&mut self, inten: bool) -> Result<(), Error> {
623 let r = T::regs(); 626 let r = self.r;
624 compiler_fence(SeqCst); 627 compiler_fence(SeqCst);
625 628
626 // Clear events 629 // Clear events
@@ -753,14 +756,14 @@ impl<'d, T: Instance> Twis<'d, T> {
753 } 756 }
754} 757}
755 758
756impl<'a, T: Instance> Drop for Twis<'a, T> { 759impl<'a> Drop for Twis<'a> {
757 fn drop(&mut self) { 760 fn drop(&mut self) {
758 trace!("twis drop"); 761 trace!("twis drop");
759 762
760 // TODO: check for abort 763 // TODO: check for abort
761 764
762 // disable! 765 // disable!
763 let r = T::regs(); 766 let r = self.r;
764 r.enable().write(|w| w.set_enable(vals::Enable::DISABLED)); 767 r.enable().write(|w| w.set_enable(vals::Enable::DISABLED));
765 768
766 gpio::deconfigure_pin(r.psel().sda().read()); 769 gpio::deconfigure_pin(r.psel().sda().read());