3232#[ rtic:: app( device = crate :: hal:: pac, peripherals = true , monotonic = rtic:: cyccnt:: CYCCNT ) ]
3333const APP : ( ) = {
3434 struct Resources {
35- i2s : hal:: i2s:: I2S ,
36- #[ init( [ 0 ; 32 ] ) ]
37- signal_buf : [ i16 ; 32 ] ,
38- #[ init( [ 0 ; 32 ] ) ]
39- mute_buf : [ i16 ; 32 ] ,
35+ signal_buf : & ' static [ i16 ] ,
36+ mute_buf : & ' static [ i16 ] ,
4037 #[ init( None ) ]
4138 queue : Option < Queue < State , U256 > > ,
4239 producer : Producer < ' static , State , U256 > ,
@@ -49,10 +46,21 @@ const APP: () = {
4946 btn1 : Pin < Input < PullUp > > ,
5047 btn2 : Pin < Input < PullUp > > ,
5148 led : Pin < Output < PushPull > > ,
49+ transfer : Option < Transfer < & ' static [ i16 ] > > ,
5250 }
5351
54- #[ init( resources = [ queue, signal_buf , mute_buf ] , spawn = [ tick] ) ]
52+ #[ init( resources = [ queue] , spawn = [ tick] ) ]
5553 fn init ( mut ctx : init:: Context ) -> init:: LateResources {
54+ static mut MUTE_BUF : [ i16 ; 32 ] = [ 0i16 ; 32 ] ;
55+ static mut SIGNAL_BUF : [ i16 ; 32 ] = [ 0i16 ; 32 ] ;
56+
57+ // Fill signal buffer with triangle waveform, 2 channels interleaved
58+ let len = SIGNAL_BUF . len ( ) / 2 ;
59+ for x in 0 ..len {
60+ SIGNAL_BUF [ 2 * x] = triangle_wave ( x as i32 , len, 2048 , 0 , 1 ) as i16 ;
61+ SIGNAL_BUF [ 2 * x + 1 ] = triangle_wave ( x as i32 , len, 2048 , 0 , 1 ) as i16 ;
62+ }
63+
5664 let _clocks = hal:: clocks:: Clocks :: new ( ctx. device . CLOCK ) . enable_ext_hfosc ( ) ;
5765 // Enable the monotonic timer (CYCCNT)
5866 ctx. core . DCB . enable_trace ( ) ;
@@ -75,16 +83,7 @@ const APP: () = {
7583 None ,
7684 Some ( & sdout_pin) ,
7785 ) ;
78- i2s. tx_buffer ( & ctx. resources . mute_buf [ ..] ) . ok ( ) ;
79- i2s. enable ( ) . start ( ) ;
80-
81- // Fill signal buffer with triangle waveform, 2 channels interleaved
82- let signal_buf = ctx. resources . signal_buf ;
83- let len = signal_buf. len ( ) / 2 ;
84- for x in 0 ..len {
85- signal_buf[ 2 * x] = triangle_wave ( x as i32 , len, 2048 , 0 , 1 ) as i16 ;
86- signal_buf[ 2 * x + 1 ] = triangle_wave ( x as i32 , len, 2048 , 0 , 1 ) as i16 ;
87- }
86+ i2s. start ( ) ;
8887
8988 // Configure buttons
9089 let btn1 = p0. p0_11 . into_pullup_input ( ) . degrade ( ) ;
@@ -117,7 +116,6 @@ const APP: () = {
117116 ctx. spawn . tick ( ) . ok ( ) ;
118117
119118 init:: LateResources {
120- i2s,
121119 producer,
122120 consumer,
123121 gpiote,
@@ -126,6 +124,9 @@ const APP: () = {
126124 led : p0. p0_13 . into_push_pull_output ( Level :: High ) . degrade ( ) ,
127125 uarte,
128126 uarte_timer : Timer :: new ( ctx. device . TIMER0 ) ,
127+ transfer : i2s. tx ( & MUTE_BUF [ ..] ) . ok ( ) ,
128+ signal_buf : & SIGNAL_BUF [ ..] ,
129+ mute_buf : & MUTE_BUF [ ..] ,
129130 }
130131 }
131132
@@ -150,7 +151,7 @@ const APP: () = {
150151 }
151152 }
152153 Err ( hal:: uarte:: Error :: Timeout ( n) ) if n > 0 => {
153- if let Ok ( msg) = core:: str:: from_utf8 ( & uarte_rx_buf[ 0 ..n] ) {
154+ if let Ok ( msg) = core:: str:: from_utf8 ( & uarte_rx_buf[ ..n] ) {
154155 rprintln ! ( "{}" , msg) ;
155156 for action in encode ( msg) {
156157 for _ in 0 ..action. duration {
@@ -164,18 +165,18 @@ const APP: () = {
164165 }
165166 }
166167
167- #[ task( resources = [ consumer, i2s , signal_buf, mute_buf, led, speed] , schedule = [ tick] ) ]
168+ #[ task( resources = [ consumer, transfer , signal_buf, mute_buf, led, speed] , schedule = [ tick] ) ]
168169 fn tick ( ctx : tick:: Context ) {
169- let i2s = ctx. resources . i2s ;
170+ let ( _buf , i2s) = ctx. resources . transfer . take ( ) . unwrap ( ) . wait ( ) ;
170171 match ctx. resources . consumer . dequeue ( ) {
171172 Some ( State :: On ) => {
172173 // Move TX pointer to signal buffer (sound ON)
173- i2s. tx_buffer ( & ctx. resources . signal_buf [ .. ] ) . ok ( ) ;
174+ * ctx . resources . transfer = i2s. tx ( * ctx. resources . signal_buf ) . ok ( ) ;
174175 ctx. resources . led . set_low ( ) . ok ( ) ;
175176 }
176177 _ => {
177178 // Move TX pointer to silent buffer (sound OFF)
178- i2s. tx_buffer ( & ctx. resources . mute_buf [ .. ] ) . ok ( ) ;
179+ * ctx . resources . transfer = i2s. tx ( * ctx. resources . mute_buf ) . ok ( ) ;
179180 ctx. resources . led . set_high ( ) . ok ( ) ;
180181 }
181182 }
@@ -190,7 +191,7 @@ const APP: () = {
190191 ctx. schedule . debounce ( ctx. start + 3_000_000 . cycles ( ) ) . ok ( ) ;
191192 }
192193
193- #[ task( resources = [ btn1, btn2, i2s , speed] ) ]
194+ #[ task( resources = [ btn1, btn2, speed] ) ]
194195 fn debounce ( ctx : debounce:: Context ) {
195196 if ctx. resources . btn1 . is_low ( ) . unwrap ( ) {
196197 rprintln ! ( "Go slower" ) ;
0 commit comments