@@ -17,17 +17,20 @@ extern crate cortex_m_semihosting as sh;
17
17
extern crate stm32f7;
18
18
#[ macro_use]
19
19
extern crate stm32f7_discovery;
20
- extern crate smoltcp;
21
20
extern crate futures;
21
+ extern crate smoltcp;
22
22
extern crate spin;
23
23
24
- use alloc:: vec:: Vec ;
25
24
use alloc:: boxed:: Box ;
25
+ use alloc:: sync:: Arc ;
26
+ use alloc:: vec:: Vec ;
26
27
use alloc_cortex_m:: CortexMHeap ;
27
28
use core:: alloc:: Layout as AllocLayout ;
28
29
use core:: fmt:: Write ;
29
30
use core:: panic:: PanicInfo ;
30
31
use cortex_m:: { asm, interrupt} ;
32
+ use futures:: { Stream , StreamExt } ;
33
+ use pin_utils:: pin_mut;
31
34
use rt:: { entry, exception, ExceptionFrame } ;
32
35
use sh:: hio:: { self , HStdout } ;
33
36
use smoltcp:: {
@@ -38,25 +41,22 @@ use smoltcp::{
38
41
time:: Instant ,
39
42
wire:: { EthernetAddress , IpAddress , IpEndpoint , Ipv4Address } ,
40
43
} ;
41
- use stm32f7:: stm32f7x6:: { CorePeripherals , Interrupt , Peripherals , SAI2 , RCC , SYSCFG ,
42
- ETHERNET_DMA , ETHERNET_MAC } ;
44
+ use stm32f7:: stm32f7x6:: {
45
+ CorePeripherals , Interrupt , Peripherals , ETHERNET_DMA , ETHERNET_MAC , RCC , SAI2 , SYSCFG ,
46
+ } ;
43
47
use stm32f7_discovery:: {
44
48
ethernet,
49
+ future_mutex:: FutureMutex ,
45
50
gpio:: { GpioPort , InputPin , OutputPin } ,
51
+ i2c:: I2C ,
46
52
init,
47
- lcd:: { self , Color , Layer , Framebuffer , AudioWriter } ,
53
+ interrupts:: { self , InterruptRequest , Priority } ,
54
+ lcd:: { self , AudioWriter , Color , Framebuffer , Layer } ,
48
55
random:: Rng ,
49
56
sd,
50
57
system_clock:: { self , Hz } ,
51
- touch,
52
- task_runtime,
53
- interrupts:: { self , InterruptRequest , Priority } ,
54
- future_mutex:: FutureMutex ,
55
- i2c:: I2C ,
58
+ task_runtime, touch,
56
59
} ;
57
- use futures:: { Stream , StreamExt } ;
58
- use pin_utils:: pin_mut;
59
- use alloc:: sync:: Arc ;
60
60
61
61
#[ global_allocator]
62
62
static ALLOCATOR : CortexMHeap = CortexMHeap :: empty ( ) ;
@@ -186,11 +186,7 @@ fn run() -> ! {
186
186
& mut nvic_stir,
187
187
|_| { } ,
188
188
|interrupt_table| {
189
- use futures:: {
190
- channel:: mpsc,
191
- task:: LocalSpawnExt ,
192
- StreamExt ,
193
- } ;
189
+ use futures:: { channel:: mpsc, task:: LocalSpawnExt , StreamExt } ;
194
190
195
191
// Future channels for passing interrupts events. The interrupt handler pushes
196
192
// to a channel and the interrupt handler awaits the next item of the channel. There
@@ -205,56 +201,73 @@ fn run() -> ! {
205
201
206
202
// Interrupt handler for the TIM6_DAC interrupt, which is the interrupt triggered by
207
203
// the tim6 timer.
208
- interrupt_table. register ( InterruptRequest :: TIM6_DAC , Priority :: P1 , move || {
209
- tim6_sink. unbounded_send ( ( ) ) . expect ( "sending on tim6 channel failed" ) ;
210
- let tim = & mut tim6;
211
- // make sure the interrupt doesn't just restart again by clearing the flag
212
- tim. sr . modify ( |_, w| w. uif ( ) . clear_bit ( ) ) ;
213
- } ) . expect ( "registering tim6 interrupt failed" ) ;
204
+ interrupt_table
205
+ . register ( InterruptRequest :: TIM6_DAC , Priority :: P1 , move || {
206
+ tim6_sink
207
+ . unbounded_send ( ( ) )
208
+ . expect ( "sending on tim6 channel failed" ) ;
209
+ let tim = & mut tim6;
210
+ // make sure the interrupt doesn't just restart again by clearing the flag
211
+ tim. sr . modify ( |_, w| w. uif ( ) . clear_bit ( ) ) ;
212
+ } )
213
+ . expect ( "registering tim6 interrupt failed" ) ;
214
214
215
215
// choose pin I-11 for exti11 line, which is the GPIO pin for the hardware button
216
- syscfg. exticr3 . modify ( |_, w| unsafe { w. exti11 ( ) . bits ( 0b1000 ) } ) ;
216
+ syscfg
217
+ . exticr3
218
+ . modify ( |_, w| unsafe { w. exti11 ( ) . bits ( 0b1000 ) } ) ;
217
219
// trigger exti11 on rising
218
220
exti. rtsr . modify ( |_, w| w. tr11 ( ) . set_bit ( ) ) ;
219
221
// unmask exti11 line
220
222
exti. imr . modify ( |_, w| w. mr11 ( ) . set_bit ( ) ) ;
221
223
222
224
// choose pin I-13 for exti13 line, which is the GPIO pin signalizing a touch event
223
- syscfg. exticr4 . modify ( |_, w| unsafe { w. exti13 ( ) . bits ( 0b1000 ) } ) ;
225
+ syscfg
226
+ . exticr4
227
+ . modify ( |_, w| unsafe { w. exti13 ( ) . bits ( 0b1000 ) } ) ;
224
228
// trigger exti13 on rising
225
229
exti. rtsr . modify ( |_, w| w. tr13 ( ) . set_bit ( ) ) ;
226
230
// unmask exti13 line
227
231
exti. imr . modify ( |_, w| w. mr13 ( ) . set_bit ( ) ) ;
228
232
229
233
// choose pin H-15 for exti15 line, which is the GPIO pin signalizing new audio data
230
234
// TODO: the audio interrupt doesn't work yet
231
- syscfg. exticr4 . modify ( |_, w| unsafe { w. exti15 ( ) . bits ( 0b0111 ) } ) ;
235
+ syscfg
236
+ . exticr4
237
+ . modify ( |_, w| unsafe { w. exti15 ( ) . bits ( 0b0111 ) } ) ;
232
238
// trigger exti15 on rising
233
239
exti. rtsr . modify ( |_, w| w. tr15 ( ) . set_bit ( ) ) ;
234
240
// unmask exti15 line
235
241
exti. imr . modify ( |_, w| w. mr15 ( ) . set_bit ( ) ) ;
236
242
237
243
// Interrupt handler for the EXTI15_10 interrupt, which is triggered by different
238
244
// sources.
239
- interrupt_table. register ( InterruptRequest :: EXTI15_10 , Priority :: P1 , move || {
240
- exti. pr . modify ( |r, w| {
241
- if r. pr11 ( ) . bit_is_set ( ) {
242
- button_sink. unbounded_send ( ( ) ) . expect ( "sending on button channel failed" ) ;
243
- w. pr11 ( ) . set_bit ( ) ;
244
- } else if r. pr13 ( ) . bit_is_set ( ) {
245
- touch_int_sink. unbounded_send ( ( ) ) . expect ( "sending on touch_int channel failed" ) ;
246
- w. pr13 ( ) . set_bit ( ) ;
247
- } else {
248
- panic ! ( "unknown exti15_10 interrupt" ) ;
249
- }
250
- w
251
- } ) ;
252
- } ) . expect ( "registering exti15_10 interrupt failed" ) ;
245
+ interrupt_table
246
+ . register ( InterruptRequest :: EXTI15_10 , Priority :: P1 , move || {
247
+ exti. pr . modify ( |r, w| {
248
+ if r. pr11 ( ) . bit_is_set ( ) {
249
+ button_sink
250
+ . unbounded_send ( ( ) )
251
+ . expect ( "sending on button channel failed" ) ;
252
+ w. pr11 ( ) . set_bit ( ) ;
253
+ } else if r. pr13 ( ) . bit_is_set ( ) {
254
+ touch_int_sink
255
+ . unbounded_send ( ( ) )
256
+ . expect ( "sending on touch_int channel failed" ) ;
257
+ w. pr13 ( ) . set_bit ( ) ;
258
+ } else {
259
+ panic ! ( "unknown exti15_10 interrupt" ) ;
260
+ }
261
+ w
262
+ } ) ;
263
+ } )
264
+ . expect ( "registering exti15_10 interrupt failed" ) ;
253
265
254
266
let idle_stream = task_runtime:: IdleStream :: new ( idle_waker_sink. clone ( ) ) ;
255
267
256
268
// ethernet
257
- let ethernet_task = EthernetTask :: new ( idle_stream. clone ( ) , rcc, syscfg, ethernet_mac, ethernet_dma) ;
269
+ let ethernet_task =
270
+ EthernetTask :: new ( idle_stream. clone ( ) , rcc, syscfg, ethernet_mac, ethernet_dma) ;
258
271
259
272
let i2c_3_mutex = Arc :: new ( FutureMutex :: new ( i2c_3) ) ;
260
273
let layer_1_mutex = Arc :: new ( FutureMutex :: new ( layer_1) ) ;
@@ -271,7 +284,9 @@ fn run() -> ! {
271
284
executor. spawn_local ( button_task ( button_stream) ) . unwrap ( ) ;
272
285
executor. spawn_local ( tim6_task ( tim6_stream) ) . unwrap ( ) ;
273
286
executor. spawn_local ( touch_task. run ( ) ) . unwrap ( ) ;
274
- executor. spawn_local ( count_up_on_idle_task ( idle_stream. clone ( ) ) ) . unwrap ( ) ;
287
+ executor
288
+ . spawn_local ( count_up_on_idle_task ( idle_stream. clone ( ) ) )
289
+ . unwrap ( ) ;
275
290
executor. spawn_local ( audio_task. run ( ) ) . unwrap ( ) ;
276
291
277
292
//executor.spawn_local(print_x);
@@ -302,8 +317,7 @@ fn run() -> ! {
302
317
)
303
318
}
304
319
305
-
306
- async fn button_task ( button_stream : impl Stream < Item =( ) > ) {
320
+ async fn button_task ( button_stream : impl Stream < Item = ( ) > ) {
307
321
pin_mut ! ( button_stream) ;
308
322
for i in 1usize .. {
309
323
let next = await ! ( button_stream. next( ) ) ;
@@ -312,7 +326,7 @@ async fn button_task(button_stream: impl Stream<Item=()>) {
312
326
}
313
327
}
314
328
315
- async fn tim6_task ( tim6_stream : impl Stream < Item = ( ) > ) {
329
+ async fn tim6_task ( tim6_stream : impl Stream < Item = ( ) > ) {
316
330
pin_mut ! ( tim6_stream) ;
317
331
loop {
318
332
let next = await ! ( tim6_stream. next( ) ) ;
@@ -322,42 +336,58 @@ async fn tim6_task(tim6_stream: impl Stream<Item=()>) {
322
336
}
323
337
324
338
struct TouchTask < S , F >
325
- where S : Stream < Item =( ) > , F : Framebuffer ,
339
+ where
340
+ S : Stream < Item = ( ) > ,
341
+ F : Framebuffer ,
326
342
{
327
343
touch_int_stream : S ,
328
344
i2c_3_mutex : Arc < FutureMutex < I2C < ' static > > > ,
329
345
layer_mutex : Arc < FutureMutex < Layer < F > > > ,
330
346
}
331
347
332
- impl < S , F > TouchTask < S , F > where S : Stream < Item =( ) > , F : Framebuffer , {
348
+ impl < S , F > TouchTask < S , F >
349
+ where
350
+ S : Stream < Item = ( ) > ,
351
+ F : Framebuffer ,
352
+ {
333
353
async fn run ( self ) {
334
- let Self { touch_int_stream, i2c_3_mutex, layer_mutex} = self ;
354
+ let Self {
355
+ touch_int_stream,
356
+ i2c_3_mutex,
357
+ layer_mutex,
358
+ } = self ;
335
359
pin_mut ! ( touch_int_stream) ;
336
360
await ! ( layer_mutex. with( |l| l. clear( ) ) ) ;
337
361
loop {
338
362
await ! ( touch_int_stream. next( ) ) . expect ( "touch channel closed" ) ;
339
363
let touches = await ! ( i2c_3_mutex. with( |i2c_3| touch:: touches( i2c_3) ) ) . unwrap ( ) ;
340
- await ! ( layer_mutex. with( |layer| {
341
- for touch in touches {
342
- layer. print_point_color_at(
343
- touch. x as usize ,
344
- touch. y as usize ,
345
- Color :: from_hex( 0xffff00 ) ,
346
- ) ;
347
- }
364
+ await ! ( layer_mutex. with( |layer| for touch in touches {
365
+ layer. print_point_color_at(
366
+ touch. x as usize ,
367
+ touch. y as usize ,
368
+ Color :: from_hex( 0xffff00 ) ,
369
+ ) ;
348
370
} ) )
349
371
}
350
372
}
351
373
}
352
374
353
- struct AudioTask < F , S > where F : Framebuffer , S : Stream < Item =( ) > {
375
+ struct AudioTask < F , S >
376
+ where
377
+ F : Framebuffer ,
378
+ S : Stream < Item = ( ) > ,
379
+ {
354
380
sai_2 : SAI2 ,
355
381
idle_stream : S ,
356
382
layer_mutex : Arc < FutureMutex < Layer < F > > > ,
357
383
audio_writer : AudioWriter ,
358
384
}
359
385
360
- impl < F , S > AudioTask < F , S > where F : Framebuffer , S : Stream < Item =( ) > {
386
+ impl < F , S > AudioTask < F , S >
387
+ where
388
+ F : Framebuffer ,
389
+ S : Stream < Item = ( ) > ,
390
+ {
361
391
fn new ( layer_mutex : Arc < FutureMutex < Layer < F > > > , sai_2 : SAI2 , idle_stream : S ) -> Self {
362
392
Self {
363
393
sai_2,
@@ -368,7 +398,12 @@ impl<F, S> AudioTask<F, S> where F: Framebuffer, S: Stream<Item=()> {
368
398
}
369
399
370
400
async fn run ( self ) {
371
- let Self { idle_stream, layer_mutex, mut audio_writer, sai_2} = self ;
401
+ let Self {
402
+ idle_stream,
403
+ layer_mutex,
404
+ mut audio_writer,
405
+ sai_2,
406
+ } = self ;
372
407
pin_mut ! ( idle_stream) ;
373
408
374
409
let mut data0_buffer = None ;
@@ -383,12 +418,10 @@ impl<F, S> AudioTask<F, S> where F: Framebuffer, S: Stream<Item=()> {
383
418
match data0_buffer {
384
419
None => {
385
420
data0_buffer = Some ( data) ;
386
- } ,
421
+ }
387
422
Some ( data0) => {
388
423
let data1 = data;
389
- await ! ( layer_mutex. with( |l| {
390
- audio_writer. set_next_col( l, data0, data1)
391
- } ) ) ;
424
+ await ! ( layer_mutex. with( |l| audio_writer. set_next_col( l, data0, data1) ) ) ;
392
425
data0_buffer = None ;
393
426
}
394
427
}
@@ -397,7 +430,7 @@ impl<F, S> AudioTask<F, S> where F: Framebuffer, S: Stream<Item=()> {
397
430
}
398
431
}
399
432
400
- async fn count_up_on_idle_task ( idle_stream : impl Stream < Item = ( ) > ) {
433
+ async fn count_up_on_idle_task ( idle_stream : impl Stream < Item = ( ) > ) {
401
434
pin_mut ! ( idle_stream) ;
402
435
let mut number = 0 ;
403
436
loop {
@@ -409,7 +442,11 @@ async fn count_up_on_idle_task(idle_stream: impl Stream<Item=()>) {
409
442
}
410
443
}
411
444
412
- async fn sd_card_task < S , P > ( mut sd : sd:: Sd < ' static , P > , idle_stream : S ) where S : Stream < Item =( ) > , P : InputPin {
445
+ async fn sd_card_task < S , P > ( mut sd : sd:: Sd < ' static , P > , idle_stream : S )
446
+ where
447
+ S : Stream < Item = ( ) > ,
448
+ P : InputPin ,
449
+ {
413
450
pin_mut ! ( idle_stream) ;
414
451
// Initialize the SD Card on insert and deinitialize on extract.
415
452
loop {
@@ -424,18 +461,28 @@ async fn sd_card_task<S, P>(mut sd: sd::Sd<'static, P>, idle_stream: S) where S:
424
461
}
425
462
}
426
463
427
- struct EthernetTask < S > where S : Stream < Item =( ) > {
464
+ struct EthernetTask < S >
465
+ where
466
+ S : Stream < Item = ( ) > ,
467
+ {
428
468
idle_stream : S ,
429
469
rcc : RCC ,
430
470
syscfg : SYSCFG ,
431
471
ethernet_mac : ETHERNET_MAC ,
432
472
ethernet_dma : ETHERNET_DMA ,
433
473
}
434
474
435
- impl < S > EthernetTask < S > where S : Stream < Item =( ) > {
436
- fn new ( idle_stream : S , rcc : RCC , syscfg : SYSCFG , ethernet_mac : ETHERNET_MAC ,
437
- ethernet_dma : ETHERNET_DMA ) -> Self
438
- {
475
+ impl < S > EthernetTask < S >
476
+ where
477
+ S : Stream < Item = ( ) > ,
478
+ {
479
+ fn new (
480
+ idle_stream : S ,
481
+ rcc : RCC ,
482
+ syscfg : SYSCFG ,
483
+ ethernet_mac : ETHERNET_MAC ,
484
+ ethernet_dma : ETHERNET_DMA ,
485
+ ) -> Self {
439
486
Self {
440
487
idle_stream,
441
488
rcc,
@@ -467,8 +514,10 @@ impl<S> EthernetTask<S> where S: Stream<Item=()> {
467
514
468
515
if ethernet_interface. is_ok ( ) {
469
516
let endpoint = IpEndpoint :: new ( IpAddress :: Ipv4 ( IP_ADDR ) , 15 ) ;
470
- let udp_rx_buffer = UdpSocketBuffer :: new ( vec ! [ UdpPacketMetadata :: EMPTY ; 3 ] , vec ! [ 0u8 ; 256 ] ) ;
471
- let udp_tx_buffer = UdpSocketBuffer :: new ( vec ! [ UdpPacketMetadata :: EMPTY ; 1 ] , vec ! [ 0u8 ; 128 ] ) ;
517
+ let udp_rx_buffer =
518
+ UdpSocketBuffer :: new ( vec ! [ UdpPacketMetadata :: EMPTY ; 3 ] , vec ! [ 0u8 ; 256 ] ) ;
519
+ let udp_tx_buffer =
520
+ UdpSocketBuffer :: new ( vec ! [ UdpPacketMetadata :: EMPTY ; 1 ] , vec ! [ 0u8 ; 128 ] ) ;
472
521
let mut example_udp_socket = UdpSocket :: new ( udp_rx_buffer, udp_tx_buffer) ;
473
522
example_udp_socket. bind ( endpoint) . unwrap ( ) ;
474
523
sockets. add ( example_udp_socket) ;
@@ -489,7 +538,7 @@ impl<S> EthernetTask<S> where S: Stream<Item=()> {
489
538
) {
490
539
Err ( :: smoltcp:: Error :: Exhausted ) => {
491
540
await ! ( idle_stream. next( ) ) . expect ( "idle stream closed" ) ;
492
- } ,
541
+ }
493
542
Err ( :: smoltcp:: Error :: Unrecognized ) => { }
494
543
Err ( e) => println ! ( "Network error: {:?}" , e) ,
495
544
Ok ( socket_changed) => {
0 commit comments