Skip to content

Commit 4b5ad88

Browse files
9namesburrbull
authored andcommitted
Comment buffer usage in adc_dma_rtic example
1 parent d2b0266 commit 4b5ad88

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1717

1818
### Added
1919

20+
- Docs in `rtic-adc-dma` example [#532]
2021
- `OutPortX` (X = 2..8) and `OutPortArray` structures which can handle several pins at once [#426]
2122
- `restore` for `ErasedPin` and `PartiallyErasedPin`
2223

@@ -27,6 +28,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
2728
- Fix SDIO hardware flow control errata [#577]
2829

2930
[#426]: https://github.com/stm32-rs/stm32f4xx-hal/pull/426
31+
[#532]: https://github.com/stm32-rs/stm32f4xx-hal/pull/532
3032
[#571]: https://github.com/stm32-rs/stm32f4xx-hal/pull/571
3133
[#572]: https://github.com/stm32-rs/stm32f4xx-hal/pull/572
3234
[#577]: https://github.com/stm32-rs/stm32f4xx-hal/pull/577

examples/rtic-adc-dma.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,11 @@ mod app {
7777
adc.configure_channel(&voltage, Sequence::Two, SampleTime::Cycles_480);
7878
adc.enable_temperature_and_vref();
7979

80+
// These buffers need to be 'static to use safely with the DMA - we can't allow them to be dropped while the DMA is accessing them.
81+
// The easiest way to satisfy that is to make them static, and the safest way to do that is with `cortex_m::singleton!`
8082
let first_buffer = cortex_m::singleton!(: [u16; 2] = [0; 2]).unwrap();
8183
let second_buffer = Some(cortex_m::singleton!(: [u16; 2] = [0; 2]).unwrap());
84+
// Give the first buffer to the DMA. The second buffer is held in an Option in `local.buffer` until the transfer is complete
8285
let transfer = Transfer::init_peripheral_to_memory(dma.0, adc, first_buffer, None, config);
8386

8487
polling::spawn_after(1.secs()).ok();
@@ -107,6 +110,8 @@ mod app {
107110
fn dma(cx: dma::Context) {
108111
let dma::Context { mut shared, local } = cx;
109112
let (buffer, sample_to_millivolts) = shared.transfer.lock(|transfer| {
113+
// When the DMA completes it will return the buffer we gave it last time - we now store that as `buffer`
114+
// We still have our other buffer waiting in `local.buffer`, so `take` that and give it to the `transfer`
110115
let (buffer, _) = transfer
111116
.next_transfer(local.buffer.take().unwrap())
112117
.unwrap();
@@ -115,9 +120,12 @@ mod app {
115120
(buffer, sample_to_millivolts)
116121
});
117122

123+
// Pull the ADC data out of the buffer that the DMA transfer gave us
118124
let raw_temp = buffer[0];
119125
let raw_volt = buffer[1];
120126

127+
// Now that we're finished with this buffer, put it back in `local.buffer` so it's ready for the next transfer
128+
// If we don't do this before the next transfer, we'll get a panic
121129
*local.buffer = Some(buffer);
122130

123131
let cal30 = VtempCal30::get().read() as f32;

0 commit comments

Comments
 (0)