@@ -77,8 +77,11 @@ mod app {
77
77
adc. configure_channel ( & voltage, Sequence :: Two , SampleTime :: Cycles_480 ) ;
78
78
adc. enable_temperature_and_vref ( ) ;
79
79
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!`
80
82
let first_buffer = cortex_m:: singleton!( : [ u16 ; 2 ] = [ 0 ; 2 ] ) . unwrap ( ) ;
81
83
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
82
85
let transfer = Transfer :: init_peripheral_to_memory ( dma. 0 , adc, first_buffer, None , config) ;
83
86
84
87
polling:: spawn_after ( 1 . secs ( ) ) . ok ( ) ;
@@ -107,6 +110,8 @@ mod app {
107
110
fn dma ( cx : dma:: Context ) {
108
111
let dma:: Context { mut shared, local } = cx;
109
112
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`
110
115
let ( buffer, _) = transfer
111
116
. next_transfer ( local. buffer . take ( ) . unwrap ( ) )
112
117
. unwrap ( ) ;
@@ -115,9 +120,12 @@ mod app {
115
120
( buffer, sample_to_millivolts)
116
121
} ) ;
117
122
123
+ // Pull the ADC data out of the buffer that the DMA transfer gave us
118
124
let raw_temp = buffer[ 0 ] ;
119
125
let raw_volt = buffer[ 1 ] ;
120
126
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
121
129
* local. buffer = Some ( buffer) ;
122
130
123
131
let cal30 = VtempCal30 :: get ( ) . read ( ) as f32 ;
0 commit comments