32
32
#[ rtic:: app( device = crate :: hal:: pac, peripherals = true , monotonic = rtic:: cyccnt:: CYCCNT ) ]
33
33
const APP : ( ) = {
34
34
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 ] ,
40
37
#[ init( None ) ]
41
38
queue : Option < Queue < State , U256 > > ,
42
39
producer : Producer < ' static , State , U256 > ,
@@ -49,10 +46,21 @@ const APP: () = {
49
46
btn1 : Pin < Input < PullUp > > ,
50
47
btn2 : Pin < Input < PullUp > > ,
51
48
led : Pin < Output < PushPull > > ,
49
+ transfer : Option < Transfer < & ' static [ i16 ] > > ,
52
50
}
53
51
54
- #[ init( resources = [ queue, signal_buf , mute_buf ] , spawn = [ tick] ) ]
52
+ #[ init( resources = [ queue] , spawn = [ tick] ) ]
55
53
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
+
56
64
let _clocks = hal:: clocks:: Clocks :: new ( ctx. device . CLOCK ) . enable_ext_hfosc ( ) ;
57
65
// Enable the monotonic timer (CYCCNT)
58
66
ctx. core . DCB . enable_trace ( ) ;
@@ -75,16 +83,7 @@ const APP: () = {
75
83
None ,
76
84
Some ( & sdout_pin) ,
77
85
) ;
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 ( ) ;
88
87
89
88
// Configure buttons
90
89
let btn1 = p0. p0_11 . into_pullup_input ( ) . degrade ( ) ;
@@ -117,7 +116,6 @@ const APP: () = {
117
116
ctx. spawn . tick ( ) . ok ( ) ;
118
117
119
118
init:: LateResources {
120
- i2s,
121
119
producer,
122
120
consumer,
123
121
gpiote,
@@ -126,6 +124,9 @@ const APP: () = {
126
124
led : p0. p0_13 . into_push_pull_output ( Level :: High ) . degrade ( ) ,
127
125
uarte,
128
126
uarte_timer : Timer :: new ( ctx. device . TIMER0 ) ,
127
+ transfer : i2s. tx ( & MUTE_BUF [ ..] ) . ok ( ) ,
128
+ signal_buf : & SIGNAL_BUF [ ..] ,
129
+ mute_buf : & MUTE_BUF [ ..] ,
129
130
}
130
131
}
131
132
@@ -150,7 +151,7 @@ const APP: () = {
150
151
}
151
152
}
152
153
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] ) {
154
155
rprintln ! ( "{}" , msg) ;
155
156
for action in encode ( msg) {
156
157
for _ in 0 ..action. duration {
@@ -164,18 +165,18 @@ const APP: () = {
164
165
}
165
166
}
166
167
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] ) ]
168
169
fn tick ( ctx : tick:: Context ) {
169
- let i2s = ctx. resources . i2s ;
170
+ let ( _buf , i2s) = ctx. resources . transfer . take ( ) . unwrap ( ) . wait ( ) ;
170
171
match ctx. resources . consumer . dequeue ( ) {
171
172
Some ( State :: On ) => {
172
173
// 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 ( ) ;
174
175
ctx. resources . led . set_low ( ) . ok ( ) ;
175
176
}
176
177
_ => {
177
178
// 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 ( ) ;
179
180
ctx. resources . led . set_high ( ) . ok ( ) ;
180
181
}
181
182
}
@@ -190,7 +191,7 @@ const APP: () = {
190
191
ctx. schedule . debounce ( ctx. start + 3_000_000 . cycles ( ) ) . ok ( ) ;
191
192
}
192
193
193
- #[ task( resources = [ btn1, btn2, i2s , speed] ) ]
194
+ #[ task( resources = [ btn1, btn2, speed] ) ]
194
195
fn debounce ( ctx : debounce:: Context ) {
195
196
if ctx. resources . btn1 . is_low ( ) . unwrap ( ) {
196
197
rprintln ! ( "Go slower" ) ;
0 commit comments