aboutsummaryrefslogtreecommitdiff
path: root/examples/rp/src/bin/sharing.rs
diff options
context:
space:
mode:
Diffstat (limited to 'examples/rp/src/bin/sharing.rs')
-rw-r--r--examples/rp/src/bin/sharing.rs22
1 files changed, 16 insertions, 6 deletions
diff --git a/examples/rp/src/bin/sharing.rs b/examples/rp/src/bin/sharing.rs
index 0761500ef..5416e20ce 100644
--- a/examples/rp/src/bin/sharing.rs
+++ b/examples/rp/src/bin/sharing.rs
@@ -1,4 +1,14 @@
1//! This example shows some common strategies for sharing resources between tasks. 1//! This example shows some common strategies for sharing resources between tasks.
2//!
3//! We demonstrate five different ways of sharing, covering different use cases:
4//! - Atomics: This method is used for simple values, such as bool and u8..u32
5//! - Blocking Mutex: This is used for sharing non-async things, using Cell/RefCell for interior mutability.
6//! - Async Mutex: This is used for sharing async resources, where you need to hold the lock across await points.
7//! The async Mutex has interior mutability built-in, so no RefCell is needed.
8//! - Cell: For sharing Copy types between tasks running on the same executor.
9//! - RefCell: When you want &mut access to a value shared between tasks running on the same executor.
10//!
11//! More information: https://embassy.dev/book/#_sharing_peripherals_between_tasks
2 12
3#![no_std] 13#![no_std]
4#![no_main] 14#![no_main]
@@ -21,7 +31,7 @@ use rand::RngCore;
21use static_cell::{ConstStaticCell, StaticCell}; 31use static_cell::{ConstStaticCell, StaticCell};
22use {defmt_rtt as _, panic_probe as _}; 32use {defmt_rtt as _, panic_probe as _};
23 33
24type UartMutex = mutex::Mutex<CriticalSectionRawMutex, UartTx<'static, UART0, uart::Async>>; 34type UartAsyncMutex = mutex::Mutex<CriticalSectionRawMutex, UartTx<'static, UART0, uart::Async>>;
25 35
26struct MyType { 36struct MyType {
27 inner: u32, 37 inner: u32,
@@ -53,7 +63,7 @@ fn main() -> ! {
53 63
54 let uart = UartTx::new(p.UART0, p.PIN_0, p.DMA_CH0, uart::Config::default()); 64 let uart = UartTx::new(p.UART0, p.PIN_0, p.DMA_CH0, uart::Config::default());
55 // Use the async Mutex for sharing async things (built-in interior mutability) 65 // Use the async Mutex for sharing async things (built-in interior mutability)
56 static UART: StaticCell<UartMutex> = StaticCell::new(); 66 static UART: StaticCell<UartAsyncMutex> = StaticCell::new();
57 let uart = UART.init(mutex::Mutex::new(uart)); 67 let uart = UART.init(mutex::Mutex::new(uart));
58 68
59 // High-priority executor: runs in interrupt mode 69 // High-priority executor: runs in interrupt mode
@@ -80,7 +90,7 @@ fn main() -> ! {
80} 90}
81 91
82#[embassy_executor::task] 92#[embassy_executor::task]
83async fn task_a(uart: &'static UartMutex) { 93async fn task_a(uart: &'static UartAsyncMutex) {
84 let mut ticker = Ticker::every(Duration::from_secs(1)); 94 let mut ticker = Ticker::every(Duration::from_secs(1));
85 loop { 95 loop {
86 let random = RoscRng.next_u32(); 96 let random = RoscRng.next_u32();
@@ -100,7 +110,7 @@ async fn task_a(uart: &'static UartMutex) {
100} 110}
101 111
102#[embassy_executor::task] 112#[embassy_executor::task]
103async fn task_b(uart: &'static UartMutex, cell: &'static Cell<[u8; 4]>, ref_cell: &'static RefCell<MyType>) { 113async fn task_b(uart: &'static UartAsyncMutex, cell: &'static Cell<[u8; 4]>, ref_cell: &'static RefCell<MyType>) {
104 let mut ticker = Ticker::every(Duration::from_secs(1)); 114 let mut ticker = Ticker::every(Duration::from_secs(1));
105 loop { 115 loop {
106 let random = RoscRng.next_u32(); 116 let random = RoscRng.next_u32();
@@ -121,8 +131,8 @@ async fn task_c(cell: &'static Cell<[u8; 4]>, ref_cell: &'static RefCell<MyType>
121 loop { 131 loop {
122 info!("======================="); 132 info!("=======================");
123 133
124 let atomic = ATOMIC.load(Ordering::Relaxed); 134 let atomic_val = ATOMIC.load(Ordering::Relaxed);
125 info!("atomic: {}", atomic); 135 info!("atomic: {}", atomic_val);
126 136
127 MUTEX_BLOCKING.lock(|x| { 137 MUTEX_BLOCKING.lock(|x| {
128 let val = x.borrow().inner; 138 let val = x.borrow().inner;