diff options
| author | Andres Vahter <[email protected]> | 2024-07-02 17:35:15 +0300 |
|---|---|---|
| committer | Andres Vahter <[email protected]> | 2024-07-02 17:35:15 +0300 |
| commit | d4ff7616f952bc801390e46bb3163b63cc01828d (patch) | |
| tree | 25c827a010fd3a763df9a75e22038dafd579195d | |
| parent | dd69efe70804f57c17adb45602798dad617cb41d (diff) | |
stm32 ringbuffered adc docs improvements
| -rw-r--r-- | embassy-stm32/src/adc/ringbuffered_v2.rs | 17 |
1 files changed, 7 insertions, 10 deletions
diff --git a/embassy-stm32/src/adc/ringbuffered_v2.rs b/embassy-stm32/src/adc/ringbuffered_v2.rs index 3f5277a9d..8df224783 100644 --- a/embassy-stm32/src/adc/ringbuffered_v2.rs +++ b/embassy-stm32/src/adc/ringbuffered_v2.rs | |||
| @@ -93,11 +93,12 @@ pub struct RingBufferedAdc<'d, T: Instance> { | |||
| 93 | impl<'d, T: Instance> Adc<'d, T> { | 93 | impl<'d, T: Instance> Adc<'d, T> { |
| 94 | /// Configures the ADC to use a DMA ring buffer for continuous data acquisition. | 94 | /// Configures the ADC to use a DMA ring buffer for continuous data acquisition. |
| 95 | /// | 95 | /// |
| 96 | /// The `dma_buf` should be large enough to prevent buffer overflow, allowing sufficient time to read out measurements. | 96 | /// The `dma_buf` should be large enough to prevent DMA buffer overrun. |
| 97 | /// The length of the `dma_buf` should be a multiple of the ADC channel count. | 97 | /// The length of the `dma_buf` should be a multiple of the ADC channel count. |
| 98 | /// For example, if 3 channels are measured, its length can be 3 * 40 = 120 measurements. | 98 | /// For example, if 3 channels are measured, its length can be 3 * 40 = 120 measurements. |
| 99 | /// | 99 | /// |
| 100 | /// `read_exact` method is used to read out measurements from the DMA ring buffer, and its buffer should be exactly half of the `dma_buf` length. | 100 | /// `read_exact` method is used to read out measurements from the DMA ring buffer, and its buffer should be exactly half of the `dma_buf` length. |
| 101 | /// It is critical to call `read_exact` frequently to prevent DMA buffer overrun. | ||
| 101 | /// | 102 | /// |
| 102 | /// [`read_exact`]: #method.read_exact | 103 | /// [`read_exact`]: #method.read_exact |
| 103 | pub fn into_ring_buffered( | 104 | pub fn into_ring_buffered( |
| @@ -358,18 +359,17 @@ impl<'d, T: Instance> RingBufferedAdc<'d, T> { | |||
| 358 | 359 | ||
| 359 | /// Reads measurements from the DMA ring buffer. | 360 | /// Reads measurements from the DMA ring buffer. |
| 360 | /// | 361 | /// |
| 361 | /// This method fills the provided `measurements` array with ADC readings. | 362 | /// This method fills the provided `measurements` array with ADC readings from the DMA buffer. |
| 362 | /// The length of the `measurements` array should be exactly half of the DMA buffer length. | 363 | /// The length of the `measurements` array should be exactly half of the DMA buffer length. Because interrupts are only generated if half or full DMA transfer completes. |
| 363 | /// Because interrupts are only generated if half or full DMA transfer completes. | ||
| 364 | /// | 364 | /// |
| 365 | /// Each call to `read_exact` will populate the `measurements` array in the same order as the channels defined with `set_sample_sequence`. | 365 | /// Each call to `read_exact` will populate the `measurements` array in the same order as the channels defined with `set_sample_sequence`. |
| 366 | /// There will be many sequences worth of measurements in this array because it only returns if at least half of the DMA buffer is filled. | 366 | /// There will be many sequences worth of measurements in this array because it only returns if at least half of the DMA buffer is filled. |
| 367 | /// For example if 3 channels are sampled `measurements` contain: `[sq0 sq1 sq3 sq0 sq1 sq3 sq0 sq1 sq3 sq0 sq1 sq3..]`. | 367 | /// For example if 3 channels are sampled `measurements` contain: `[sq0 sq1 sq3 sq0 sq1 sq3 sq0 sq1 sq3 sq0 sq1 sq3..]`. |
| 368 | /// | 368 | /// |
| 369 | /// If an error is returned, it indicates a DMA overrun, and the process must be restarted by calling `start` again. | 369 | /// If an error is returned, it indicates a DMA overrun, and the process must be restarted by calling `start` or `read_exact` again. |
| 370 | /// | 370 | /// |
| 371 | /// By default, the ADC fills the DMA buffer as quickly as possible. To control the sample rate, call `teardown_adc` after each readout, and then start the DMA again at the desired interval. | 371 | /// By default, the ADC fills the DMA buffer as quickly as possible. To control the sample rate, call `teardown_adc` after each readout, and then start the DMA again at the desired interval. |
| 372 | /// Note that even if using `teardown_adc` to control sample rate, with each call to `read_exact`, measurements equivalent to half the size of the DMA buffer are still collected. | 372 | /// Note that even if using `teardown_adc` to control the sample rate, with each call to `read_exact`, measurements equivalent to half the size of the DMA buffer are still collected. |
| 373 | /// | 373 | /// |
| 374 | /// Example: | 374 | /// Example: |
| 375 | /// ```rust,ignore | 375 | /// ```rust,ignore |
| @@ -381,7 +381,6 @@ impl<'d, T: Instance> RingBufferedAdc<'d, T> { | |||
| 381 | /// adc.set_sample_sequence(Sequence::Two, &mut p.PA1, SampleTime::CYCLES112); | 381 | /// adc.set_sample_sequence(Sequence::Two, &mut p.PA1, SampleTime::CYCLES112); |
| 382 | /// adc.set_sample_sequence(Sequence::Three, &mut p.PA2, SampleTime::CYCLES112); | 382 | /// adc.set_sample_sequence(Sequence::Three, &mut p.PA2, SampleTime::CYCLES112); |
| 383 | /// | 383 | /// |
| 384 | /// adc.start.unwrap(); | ||
| 385 | /// let mut measurements = [0u16; DMA_BUF_LEN / 2]; | 384 | /// let mut measurements = [0u16; DMA_BUF_LEN / 2]; |
| 386 | /// loop { | 385 | /// loop { |
| 387 | /// match adc.read_exact(&mut measurements).await { | 386 | /// match adc.read_exact(&mut measurements).await { |
| @@ -392,14 +391,12 @@ impl<'d, T: Instance> RingBufferedAdc<'d, T> { | |||
| 392 | /// } | 391 | /// } |
| 393 | /// Err(e) => { | 392 | /// Err(e) => { |
| 394 | /// defmt::warn!("Error: {:?}", e); | 393 | /// defmt::warn!("Error: {:?}", e); |
| 395 | /// // DMA overflow, restart ADC. | 394 | /// // DMA overrun, next call to `read_exact` restart ADC. |
| 396 | /// let _ = adc.start(); | ||
| 397 | /// } | 395 | /// } |
| 398 | /// } | 396 | /// } |
| 399 | /// | 397 | /// |
| 400 | /// // Manually control sample rate. | 398 | /// // Manually control sample rate. |
| 401 | /// Timer::after_millis(100).await; | 399 | /// Timer::after_millis(100).await; |
| 402 | /// let _ = adc.start(); | ||
| 403 | /// } | 400 | /// } |
| 404 | /// ``` | 401 | /// ``` |
| 405 | /// | 402 | /// |
