Skip to content

Commit 3f2f7e6

Browse files
committed
Run rustfmt
1 parent dd58876 commit 3f2f7e6

File tree

17 files changed

+218
-140
lines changed

17 files changed

+218
-140
lines changed

examples/blinking_led_with_interrupts.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ use stm32f7::stm32f7x6::{CorePeripherals, Peripherals};
1717
use stm32f7_discovery::{
1818
gpio::{GpioPort, OutputPin},
1919
init,
20-
system_clock::{self, Hz},
2120
interrupts::{self, InterruptRequest, Priority},
21+
system_clock::{self, Hz},
2222
};
2323

2424
const HEAP_SIZE: usize = 50 * 1024; // in bytes

src/bin/async-await.rs

Lines changed: 123 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,20 @@ extern crate cortex_m_semihosting as sh;
1717
extern crate stm32f7;
1818
#[macro_use]
1919
extern crate stm32f7_discovery;
20-
extern crate smoltcp;
2120
extern crate futures;
21+
extern crate smoltcp;
2222
extern crate spin;
2323

24-
use alloc::vec::Vec;
2524
use alloc::boxed::Box;
25+
use alloc::sync::Arc;
26+
use alloc::vec::Vec;
2627
use alloc_cortex_m::CortexMHeap;
2728
use core::alloc::Layout as AllocLayout;
2829
use core::fmt::Write;
2930
use core::panic::PanicInfo;
3031
use cortex_m::{asm, interrupt};
32+
use futures::{Stream, StreamExt};
33+
use pin_utils::pin_mut;
3134
use rt::{entry, exception, ExceptionFrame};
3235
use sh::hio::{self, HStdout};
3336
use smoltcp::{
@@ -38,25 +41,22 @@ use smoltcp::{
3841
time::Instant,
3942
wire::{EthernetAddress, IpAddress, IpEndpoint, Ipv4Address},
4043
};
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+
};
4347
use stm32f7_discovery::{
4448
ethernet,
49+
future_mutex::FutureMutex,
4550
gpio::{GpioPort, InputPin, OutputPin},
51+
i2c::I2C,
4652
init,
47-
lcd::{self, Color, Layer, Framebuffer, AudioWriter},
53+
interrupts::{self, InterruptRequest, Priority},
54+
lcd::{self, AudioWriter, Color, Framebuffer, Layer},
4855
random::Rng,
4956
sd,
5057
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,
5659
};
57-
use futures::{Stream, StreamExt};
58-
use pin_utils::pin_mut;
59-
use alloc::sync::Arc;
6060

6161
#[global_allocator]
6262
static ALLOCATOR: CortexMHeap = CortexMHeap::empty();
@@ -186,11 +186,7 @@ fn run() -> ! {
186186
&mut nvic_stir,
187187
|_| {},
188188
|interrupt_table| {
189-
use futures::{
190-
channel::mpsc,
191-
task::LocalSpawnExt,
192-
StreamExt,
193-
};
189+
use futures::{channel::mpsc, task::LocalSpawnExt, StreamExt};
194190

195191
// Future channels for passing interrupts events. The interrupt handler pushes
196192
// to a channel and the interrupt handler awaits the next item of the channel. There
@@ -205,56 +201,73 @@ fn run() -> ! {
205201

206202
// Interrupt handler for the TIM6_DAC interrupt, which is the interrupt triggered by
207203
// 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");
214214

215215
// 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) });
217219
// trigger exti11 on rising
218220
exti.rtsr.modify(|_, w| w.tr11().set_bit());
219221
// unmask exti11 line
220222
exti.imr.modify(|_, w| w.mr11().set_bit());
221223

222224
// 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) });
224228
// trigger exti13 on rising
225229
exti.rtsr.modify(|_, w| w.tr13().set_bit());
226230
// unmask exti13 line
227231
exti.imr.modify(|_, w| w.mr13().set_bit());
228232

229233
// choose pin H-15 for exti15 line, which is the GPIO pin signalizing new audio data
230234
// 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) });
232238
// trigger exti15 on rising
233239
exti.rtsr.modify(|_, w| w.tr15().set_bit());
234240
// unmask exti15 line
235241
exti.imr.modify(|_, w| w.mr15().set_bit());
236242

237243
// Interrupt handler for the EXTI15_10 interrupt, which is triggered by different
238244
// 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");
253265

254266
let idle_stream = task_runtime::IdleStream::new(idle_waker_sink.clone());
255267

256268
// 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);
258271

259272
let i2c_3_mutex = Arc::new(FutureMutex::new(i2c_3));
260273
let layer_1_mutex = Arc::new(FutureMutex::new(layer_1));
@@ -271,7 +284,9 @@ fn run() -> ! {
271284
executor.spawn_local(button_task(button_stream)).unwrap();
272285
executor.spawn_local(tim6_task(tim6_stream)).unwrap();
273286
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();
275290
executor.spawn_local(audio_task.run()).unwrap();
276291

277292
//executor.spawn_local(print_x);
@@ -302,8 +317,7 @@ fn run() -> ! {
302317
)
303318
}
304319

305-
306-
async fn button_task(button_stream: impl Stream<Item=()>) {
320+
async fn button_task(button_stream: impl Stream<Item = ()>) {
307321
pin_mut!(button_stream);
308322
for i in 1usize.. {
309323
let next = await!(button_stream.next());
@@ -312,7 +326,7 @@ async fn button_task(button_stream: impl Stream<Item=()>) {
312326
}
313327
}
314328

315-
async fn tim6_task(tim6_stream: impl Stream<Item=()>) {
329+
async fn tim6_task(tim6_stream: impl Stream<Item = ()>) {
316330
pin_mut!(tim6_stream);
317331
loop {
318332
let next = await!(tim6_stream.next());
@@ -322,42 +336,58 @@ async fn tim6_task(tim6_stream: impl Stream<Item=()>) {
322336
}
323337

324338
struct TouchTask<S, F>
325-
where S: Stream<Item=()>, F: Framebuffer,
339+
where
340+
S: Stream<Item = ()>,
341+
F: Framebuffer,
326342
{
327343
touch_int_stream: S,
328344
i2c_3_mutex: Arc<FutureMutex<I2C<'static>>>,
329345
layer_mutex: Arc<FutureMutex<Layer<F>>>,
330346
}
331347

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+
{
333353
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;
335359
pin_mut!(touch_int_stream);
336360
await!(layer_mutex.with(|l| l.clear()));
337361
loop {
338362
await!(touch_int_stream.next()).expect("touch channel closed");
339363
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+
);
348370
}))
349371
}
350372
}
351373
}
352374

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+
{
354380
sai_2: SAI2,
355381
idle_stream: S,
356382
layer_mutex: Arc<FutureMutex<Layer<F>>>,
357383
audio_writer: AudioWriter,
358384
}
359385

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+
{
361391
fn new(layer_mutex: Arc<FutureMutex<Layer<F>>>, sai_2: SAI2, idle_stream: S) -> Self {
362392
Self {
363393
sai_2,
@@ -368,7 +398,12 @@ impl<F, S> AudioTask<F, S> where F: Framebuffer, S: Stream<Item=()> {
368398
}
369399

370400
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;
372407
pin_mut!(idle_stream);
373408

374409
let mut data0_buffer = None;
@@ -383,12 +418,10 @@ impl<F, S> AudioTask<F, S> where F: Framebuffer, S: Stream<Item=()> {
383418
match data0_buffer {
384419
None => {
385420
data0_buffer = Some(data);
386-
},
421+
}
387422
Some(data0) => {
388423
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)));
392425
data0_buffer = None;
393426
}
394427
}
@@ -397,7 +430,7 @@ impl<F, S> AudioTask<F, S> where F: Framebuffer, S: Stream<Item=()> {
397430
}
398431
}
399432

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 = ()>) {
401434
pin_mut!(idle_stream);
402435
let mut number = 0;
403436
loop {
@@ -409,7 +442,11 @@ async fn count_up_on_idle_task(idle_stream: impl Stream<Item=()>) {
409442
}
410443
}
411444

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+
{
413450
pin_mut!(idle_stream);
414451
// Initialize the SD Card on insert and deinitialize on extract.
415452
loop {
@@ -424,18 +461,28 @@ async fn sd_card_task<S, P>(mut sd: sd::Sd<'static, P>, idle_stream: S) where S:
424461
}
425462
}
426463

427-
struct EthernetTask<S> where S: Stream<Item=()> {
464+
struct EthernetTask<S>
465+
where
466+
S: Stream<Item = ()>,
467+
{
428468
idle_stream: S,
429469
rcc: RCC,
430470
syscfg: SYSCFG,
431471
ethernet_mac: ETHERNET_MAC,
432472
ethernet_dma: ETHERNET_DMA,
433473
}
434474

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 {
439486
Self {
440487
idle_stream,
441488
rcc,
@@ -467,8 +514,10 @@ impl<S> EthernetTask<S> where S: Stream<Item=()> {
467514

468515
if ethernet_interface.is_ok() {
469516
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]);
472521
let mut example_udp_socket = UdpSocket::new(udp_rx_buffer, udp_tx_buffer);
473522
example_udp_socket.bind(endpoint).unwrap();
474523
sockets.add(example_udp_socket);
@@ -489,7 +538,7 @@ impl<S> EthernetTask<S> where S: Stream<Item=()> {
489538
) {
490539
Err(::smoltcp::Error::Exhausted) => {
491540
await!(idle_stream.next()).expect("idle stream closed");
492-
},
541+
}
493542
Err(::smoltcp::Error::Unrecognized) => {}
494543
Err(e) => println!("Network error: {:?}", e),
495544
Ok(socket_changed) => {

0 commit comments

Comments
 (0)