-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy path1-yield.rs
53 lines (44 loc) · 883 Bytes
/
1-yield.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
//! Yielding from a task
//!
//! Expected output:
//!
//! ```
//! B: yield
//! A: yield
//! B: yield
//! A: yield
//! DONE
//! ```
#![deny(unsafe_code)]
#![deny(warnings)]
#![no_main]
#![no_std]
use async_embedded::task;
use cortex_m::asm;
use cortex_m_rt::entry;
use cortex_m_semihosting::hprintln;
use nrf52 as _; // memory layout
use panic_udf as _; // panic handler
#[entry]
fn main() -> ! {
// task A
task::spawn(async {
loop {
hprintln!("A: yield").ok();
// context switch to B
task::r#yield().await;
}
});
// task B
task::block_on(async {
hprintln!("B: yield").ok();
// context switch to A
task::r#yield().await;
hprintln!("B: yield").ok();
task::r#yield().await;
hprintln!("DONE").ok();
loop {
asm::bkpt();
}
})
}