@@ -14,6 +14,7 @@ use small_morse::{encode, State};
14
14
use {
15
15
core:: {
16
16
panic:: PanicInfo ,
17
+ pin,
17
18
sync:: atomic:: { compiler_fence, Ordering } ,
18
19
} ,
19
20
hal:: {
@@ -32,11 +33,11 @@ use {
32
33
#[ rtic:: app( device = crate :: hal:: pac, peripherals = true , monotonic = rtic:: cyccnt:: CYCCNT ) ]
33
34
const APP : ( ) = {
34
35
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 ] ,
36
+ // i2s: hal::i2s::I2S,
37
+ // #[init([0; 32])]
38
+ signal_buf : pin :: Pin < & ' static [ i16 ] > ,
39
+ // #[init([0; 32])]
40
+ mute_buf : pin :: Pin < & ' static [ i16 ] > ,
40
41
#[ init( None ) ]
41
42
queue : Option < Queue < State , U256 > > ,
42
43
producer : Producer < ' static , State , U256 > ,
@@ -49,10 +50,20 @@ const APP: () = {
49
50
btn1 : Pin < Input < PullUp > > ,
50
51
btn2 : Pin < Input < PullUp > > ,
51
52
led : Pin < Output < PushPull > > ,
53
+ transfer : Option < hal:: i2s:: Transfer < & ' static [ i16 ] > > ,
52
54
}
53
55
54
- #[ init( resources = [ queue, signal_buf , mute_buf ] , spawn = [ tick] ) ]
56
+ #[ init( resources = [ queue] , spawn = [ tick] ) ]
55
57
fn init ( mut ctx : init:: Context ) -> init:: LateResources {
58
+ static mut MUTE_BUF : [ i16 ; 32 ] = [ 0i16 ; 32 ] ;
59
+ static mut SIGNAL_BUF : [ i16 ; 32 ] = [ 0i16 ; 32 ] ;
60
+ // Fill signal buffer with triangle waveform, 2 channels interleaved
61
+ let len = SIGNAL_BUF . len ( ) / 2 ;
62
+ for x in 0 ..len {
63
+ SIGNAL_BUF [ 2 * x] = triangle_wave ( x as i32 , len, 2048 , 0 , 1 ) as i16 ;
64
+ SIGNAL_BUF [ 2 * x + 1 ] = triangle_wave ( x as i32 , len, 2048 , 0 , 1 ) as i16 ;
65
+ }
66
+
56
67
let _clocks = hal:: clocks:: Clocks :: new ( ctx. device . CLOCK ) . enable_ext_hfosc ( ) ;
57
68
// Enable the monotonic timer (CYCCNT)
58
69
ctx. core . DCB . enable_trace ( ) ;
@@ -75,16 +86,7 @@ const APP: () = {
75
86
None ,
76
87
Some ( & sdout_pin) ,
77
88
) ;
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
- }
89
+ i2s. start ( ) ;
88
90
89
91
// Configure buttons
90
92
let btn1 = p0. p0_11 . into_pullup_input ( ) . degrade ( ) ;
@@ -117,7 +119,6 @@ const APP: () = {
117
119
ctx. spawn . tick ( ) . ok ( ) ;
118
120
119
121
init:: LateResources {
120
- i2s,
121
122
producer,
122
123
consumer,
123
124
gpiote,
@@ -126,6 +127,9 @@ const APP: () = {
126
127
led : p0. p0_13 . into_push_pull_output ( Level :: High ) . degrade ( ) ,
127
128
uarte,
128
129
uarte_timer : Timer :: new ( ctx. device . TIMER0 ) ,
130
+ transfer : i2s. tx ( pin:: Pin :: new ( & MUTE_BUF [ ..] ) ) . ok ( ) ,
131
+ signal_buf : pin:: Pin :: new ( & SIGNAL_BUF [ ..] ) ,
132
+ mute_buf : pin:: Pin :: new ( & MUTE_BUF [ ..] ) ,
129
133
}
130
134
}
131
135
@@ -150,7 +154,7 @@ const APP: () = {
150
154
}
151
155
}
152
156
Err ( hal:: uarte:: Error :: Timeout ( n) ) if n > 0 => {
153
- if let Ok ( msg) = core:: str:: from_utf8 ( & uarte_rx_buf[ 0 ..n] ) {
157
+ if let Ok ( msg) = core:: str:: from_utf8 ( & uarte_rx_buf[ ..n] ) {
154
158
rprintln ! ( "{}" , msg) ;
155
159
for action in encode ( msg) {
156
160
for _ in 0 ..action. duration {
@@ -164,18 +168,18 @@ const APP: () = {
164
168
}
165
169
}
166
170
167
- #[ task( resources = [ consumer, i2s , signal_buf, mute_buf, led, speed] , schedule = [ tick] ) ]
171
+ #[ task( resources = [ consumer, transfer , signal_buf, mute_buf, led, speed] , schedule = [ tick] ) ]
168
172
fn tick ( ctx : tick:: Context ) {
169
- let i2s = ctx. resources . i2s ;
173
+ let ( _buf , i2s) = ctx. resources . transfer . take ( ) . unwrap ( ) . wait ( ) ;
170
174
match ctx. resources . consumer . dequeue ( ) {
171
175
Some ( State :: On ) => {
172
176
// Move TX pointer to signal buffer (sound ON)
173
- i2s. tx_buffer ( & ctx. resources . signal_buf [ .. ] ) . ok ( ) ;
177
+ * ctx . resources . transfer = i2s. tx ( * ctx. resources . signal_buf ) . ok ( ) ;
174
178
ctx. resources . led . set_low ( ) . ok ( ) ;
175
179
}
176
180
_ => {
177
181
// Move TX pointer to silent buffer (sound OFF)
178
- i2s. tx_buffer ( & ctx. resources . mute_buf [ .. ] ) . ok ( ) ;
182
+ * ctx . resources . transfer = i2s. tx ( * ctx. resources . mute_buf ) . ok ( ) ;
179
183
ctx. resources . led . set_high ( ) . ok ( ) ;
180
184
}
181
185
}
@@ -190,7 +194,7 @@ const APP: () = {
190
194
ctx. schedule . debounce ( ctx. start + 3_000_000 . cycles ( ) ) . ok ( ) ;
191
195
}
192
196
193
- #[ task( resources = [ btn1, btn2, i2s , speed] ) ]
197
+ #[ task( resources = [ btn1, btn2, speed] ) ]
194
198
fn debounce ( ctx : debounce:: Context ) {
195
199
if ctx. resources . btn1 . is_low ( ) . unwrap ( ) {
196
200
rprintln ! ( "Go slower" ) ;
0 commit comments