diff options
| author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2023-01-18 04:17:08 +0000 |
|---|---|---|
| committer | GitHub <[email protected]> | 2023-01-18 04:17:08 +0000 |
| commit | db8e9efe731c8f37927f9d1debac4628c9894204 (patch) | |
| tree | 7317a2fc6c5e38e82d8c90e6a37ea8d2c0386582 | |
| parent | 15e3f42b7c9fa542844e1f8bc5b1e5c67faf7837 (diff) | |
| parent | 6ab4ecaf8379820984242a4bfb6ff4421c01b11e (diff) | |
Merge #1160
1160: Stop sampling when exiting the Saadc methods r=huntc a=huntc
Prior to this commit, the onDrop function was being dropped immediately and not on exiting the Saadc sampling methods.
A few other places have been corrected also.
Thanks to Peter Hansen for pointing out this issue.
Co-authored-by: huntc <[email protected]>
| -rw-r--r-- | embassy-nrf/src/saadc.rs | 12 | ||||
| -rw-r--r-- | embassy-stm32/src/i2c/v2.rs | 10 | ||||
| -rw-r--r-- | embassy-stm32/src/usart/mod.rs | 10 |
3 files changed, 24 insertions, 8 deletions
diff --git a/embassy-nrf/src/saadc.rs b/embassy-nrf/src/saadc.rs index d1c82423e..4592d4687 100644 --- a/embassy-nrf/src/saadc.rs +++ b/embassy-nrf/src/saadc.rs | |||
| @@ -225,7 +225,7 @@ impl<'d, const N: usize> Saadc<'d, N> { | |||
| 225 | /// also cause the sampling to be stopped. | 225 | /// also cause the sampling to be stopped. |
| 226 | pub async fn sample(&mut self, buf: &mut [i16; N]) { | 226 | pub async fn sample(&mut self, buf: &mut [i16; N]) { |
| 227 | // In case the future is dropped, stop the task and wait for it to end. | 227 | // In case the future is dropped, stop the task and wait for it to end. |
| 228 | OnDrop::new(Self::stop_sampling_immediately); | 228 | let on_drop = OnDrop::new(Self::stop_sampling_immediately); |
| 229 | 229 | ||
| 230 | let r = Self::regs(); | 230 | let r = Self::regs(); |
| 231 | 231 | ||
| @@ -258,6 +258,8 @@ impl<'d, const N: usize> Saadc<'d, N> { | |||
| 258 | Poll::Pending | 258 | Poll::Pending |
| 259 | }) | 259 | }) |
| 260 | .await; | 260 | .await; |
| 261 | |||
| 262 | drop(on_drop); | ||
| 261 | } | 263 | } |
| 262 | 264 | ||
| 263 | /// Continuous sampling with double buffers. | 265 | /// Continuous sampling with double buffers. |
| @@ -335,7 +337,7 @@ impl<'d, const N: usize> Saadc<'d, N> { | |||
| 335 | S: FnMut(&[[i16; N]]) -> SamplerState, | 337 | S: FnMut(&[[i16; N]]) -> SamplerState, |
| 336 | { | 338 | { |
| 337 | // In case the future is dropped, stop the task and wait for it to end. | 339 | // In case the future is dropped, stop the task and wait for it to end. |
| 338 | OnDrop::new(Self::stop_sampling_immediately); | 340 | let on_drop = OnDrop::new(Self::stop_sampling_immediately); |
| 339 | 341 | ||
| 340 | let r = Self::regs(); | 342 | let r = Self::regs(); |
| 341 | 343 | ||
| @@ -382,7 +384,7 @@ impl<'d, const N: usize> Saadc<'d, N> { | |||
| 382 | let mut current_buffer = 0; | 384 | let mut current_buffer = 0; |
| 383 | 385 | ||
| 384 | // Wait for events and complete when the sampler indicates it has had enough. | 386 | // Wait for events and complete when the sampler indicates it has had enough. |
| 385 | poll_fn(|cx| { | 387 | let r = poll_fn(|cx| { |
| 386 | let r = Self::regs(); | 388 | let r = Self::regs(); |
| 387 | 389 | ||
| 388 | WAKER.register(cx.waker()); | 390 | WAKER.register(cx.waker()); |
| @@ -419,6 +421,10 @@ impl<'d, const N: usize> Saadc<'d, N> { | |||
| 419 | Poll::Pending | 421 | Poll::Pending |
| 420 | }) | 422 | }) |
| 421 | .await; | 423 | .await; |
| 424 | |||
| 425 | drop(on_drop); | ||
| 426 | |||
| 427 | r | ||
| 422 | } | 428 | } |
| 423 | 429 | ||
| 424 | // Stop sampling and wait for it to stop in a blocking fashion | 430 | // Stop sampling and wait for it to stop in a blocking fashion |
diff --git a/embassy-stm32/src/i2c/v2.rs b/embassy-stm32/src/i2c/v2.rs index 4622635bf..06ff07b21 100644 --- a/embassy-stm32/src/i2c/v2.rs +++ b/embassy-stm32/src/i2c/v2.rs | |||
| @@ -483,7 +483,7 @@ impl<'d, T: Instance, TXDMA, RXDMA> I2c<'d, T, TXDMA, RXDMA> { | |||
| 483 | state.chunks_transferred.store(0, Ordering::Relaxed); | 483 | state.chunks_transferred.store(0, Ordering::Relaxed); |
| 484 | let mut remaining_len = total_len; | 484 | let mut remaining_len = total_len; |
| 485 | 485 | ||
| 486 | let _on_drop = OnDrop::new(|| { | 486 | let on_drop = OnDrop::new(|| { |
| 487 | let regs = T::regs(); | 487 | let regs = T::regs(); |
| 488 | unsafe { | 488 | unsafe { |
| 489 | regs.cr1().modify(|w| { | 489 | regs.cr1().modify(|w| { |
| @@ -542,6 +542,9 @@ impl<'d, T: Instance, TXDMA, RXDMA> I2c<'d, T, TXDMA, RXDMA> { | |||
| 542 | self.wait_tc(&check_timeout)?; | 542 | self.wait_tc(&check_timeout)?; |
| 543 | self.master_stop(); | 543 | self.master_stop(); |
| 544 | } | 544 | } |
| 545 | |||
| 546 | drop(on_drop); | ||
| 547 | |||
| 545 | Ok(()) | 548 | Ok(()) |
| 546 | } | 549 | } |
| 547 | 550 | ||
| @@ -580,7 +583,7 @@ impl<'d, T: Instance, TXDMA, RXDMA> I2c<'d, T, TXDMA, RXDMA> { | |||
| 580 | state.chunks_transferred.store(0, Ordering::Relaxed); | 583 | state.chunks_transferred.store(0, Ordering::Relaxed); |
| 581 | let mut remaining_len = total_len; | 584 | let mut remaining_len = total_len; |
| 582 | 585 | ||
| 583 | let _on_drop = OnDrop::new(|| { | 586 | let on_drop = OnDrop::new(|| { |
| 584 | let regs = T::regs(); | 587 | let regs = T::regs(); |
| 585 | unsafe { | 588 | unsafe { |
| 586 | regs.cr1().modify(|w| { | 589 | regs.cr1().modify(|w| { |
| @@ -629,6 +632,9 @@ impl<'d, T: Instance, TXDMA, RXDMA> I2c<'d, T, TXDMA, RXDMA> { | |||
| 629 | // This should be done already | 632 | // This should be done already |
| 630 | self.wait_tc(&check_timeout)?; | 633 | self.wait_tc(&check_timeout)?; |
| 631 | self.master_stop(); | 634 | self.master_stop(); |
| 635 | |||
| 636 | drop(on_drop); | ||
| 637 | |||
| 632 | Ok(()) | 638 | Ok(()) |
| 633 | } | 639 | } |
| 634 | 640 | ||
diff --git a/embassy-stm32/src/usart/mod.rs b/embassy-stm32/src/usart/mod.rs index d71aa61a4..20f4eedeb 100644 --- a/embassy-stm32/src/usart/mod.rs +++ b/embassy-stm32/src/usart/mod.rs | |||
| @@ -405,7 +405,7 @@ impl<'d, T: BasicInstance, RxDma> UartRx<'d, T, RxDma> { | |||
| 405 | let r = T::regs(); | 405 | let r = T::regs(); |
| 406 | 406 | ||
| 407 | // make sure USART state is restored to neutral state when this future is dropped | 407 | // make sure USART state is restored to neutral state when this future is dropped |
| 408 | let _drop = OnDrop::new(move || { | 408 | let on_drop = OnDrop::new(move || { |
| 409 | // defmt::trace!("Clear all USART interrupts and DMA Read Request"); | 409 | // defmt::trace!("Clear all USART interrupts and DMA Read Request"); |
| 410 | // clear all interrupts and DMA Rx Request | 410 | // clear all interrupts and DMA Rx Request |
| 411 | // SAFETY: only clears Rx related flags | 411 | // SAFETY: only clears Rx related flags |
| @@ -563,7 +563,7 @@ impl<'d, T: BasicInstance, RxDma> UartRx<'d, T, RxDma> { | |||
| 563 | // wait for the first of DMA request or idle line detected to completes | 563 | // wait for the first of DMA request or idle line detected to completes |
| 564 | // select consumes its arguments | 564 | // select consumes its arguments |
| 565 | // when transfer is dropped, it will stop the DMA request | 565 | // when transfer is dropped, it will stop the DMA request |
| 566 | match select(transfer, idle).await { | 566 | let r = match select(transfer, idle).await { |
| 567 | // DMA transfer completed first | 567 | // DMA transfer completed first |
| 568 | Either::First(()) => Ok(ReadCompletionEvent::DmaCompleted), | 568 | Either::First(()) => Ok(ReadCompletionEvent::DmaCompleted), |
| 569 | 569 | ||
| @@ -572,7 +572,11 @@ impl<'d, T: BasicInstance, RxDma> UartRx<'d, T, RxDma> { | |||
| 572 | 572 | ||
| 573 | // error occurred | 573 | // error occurred |
| 574 | Either::Second(Err(e)) => Err(e), | 574 | Either::Second(Err(e)) => Err(e), |
| 575 | } | 575 | }; |
| 576 | |||
| 577 | drop(on_drop); | ||
| 578 | |||
| 579 | r | ||
| 576 | } | 580 | } |
| 577 | 581 | ||
| 578 | async fn inner_read(&mut self, buffer: &mut [u8], enable_idle_line_detection: bool) -> Result<usize, Error> | 582 | async fn inner_read(&mut self, buffer: &mut [u8], enable_idle_line_detection: bool) -> Result<usize, Error> |
