diff options
| author | kalkyl <[email protected]> | 2024-07-08 13:58:36 +0200 |
|---|---|---|
| committer | kalkyl <[email protected]> | 2024-07-08 13:58:36 +0200 |
| commit | 87f66343493a5ae99f0f9b27602b96524111c94a (patch) | |
| tree | 471b22b4a2a9af9bb64f3bdc3fbcf4c39ca3f3c1 /examples | |
| parent | 462daeeb4927416ed817423e91adb71c0861a83d (diff) | |
Add example for sharing things between tasks
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/rp/src/bin/sharing.rs | 140 |
1 files changed, 140 insertions, 0 deletions
diff --git a/examples/rp/src/bin/sharing.rs b/examples/rp/src/bin/sharing.rs new file mode 100644 index 000000000..0761500ef --- /dev/null +++ b/examples/rp/src/bin/sharing.rs | |||
| @@ -0,0 +1,140 @@ | |||
| 1 | //! This example shows some common strategies for sharing resources between tasks. | ||
| 2 | |||
| 3 | #![no_std] | ||
| 4 | #![no_main] | ||
| 5 | |||
| 6 | use core::cell::{Cell, RefCell}; | ||
| 7 | use core::sync::atomic::{AtomicU32, Ordering}; | ||
| 8 | |||
| 9 | use cortex_m_rt::entry; | ||
| 10 | use defmt::info; | ||
| 11 | use embassy_executor::{Executor, InterruptExecutor}; | ||
| 12 | use embassy_rp::clocks::RoscRng; | ||
| 13 | use embassy_rp::interrupt::{InterruptExt, Priority}; | ||
| 14 | use embassy_rp::peripherals::UART0; | ||
| 15 | use embassy_rp::uart::{self, InterruptHandler, UartTx}; | ||
| 16 | use embassy_rp::{bind_interrupts, interrupt}; | ||
| 17 | use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex; | ||
| 18 | use embassy_sync::{blocking_mutex, mutex}; | ||
| 19 | use embassy_time::{Duration, Ticker}; | ||
| 20 | use rand::RngCore; | ||
| 21 | use static_cell::{ConstStaticCell, StaticCell}; | ||
| 22 | use {defmt_rtt as _, panic_probe as _}; | ||
| 23 | |||
| 24 | type UartMutex = mutex::Mutex<CriticalSectionRawMutex, UartTx<'static, UART0, uart::Async>>; | ||
| 25 | |||
| 26 | struct MyType { | ||
| 27 | inner: u32, | ||
| 28 | } | ||
| 29 | |||
| 30 | static EXECUTOR_HI: InterruptExecutor = InterruptExecutor::new(); | ||
| 31 | static EXECUTOR_LOW: StaticCell<Executor> = StaticCell::new(); | ||
| 32 | |||
| 33 | // Use Atomics for simple values | ||
| 34 | static ATOMIC: AtomicU32 = AtomicU32::new(0); | ||
| 35 | |||
| 36 | // Use blocking Mutex with Cell/RefCell for sharing non-async things | ||
| 37 | static MUTEX_BLOCKING: blocking_mutex::Mutex<CriticalSectionRawMutex, RefCell<MyType>> = | ||
| 38 | blocking_mutex::Mutex::new(RefCell::new(MyType { inner: 0 })); | ||
| 39 | |||
| 40 | bind_interrupts!(struct Irqs { | ||
| 41 | UART0_IRQ => InterruptHandler<UART0>; | ||
| 42 | }); | ||
| 43 | |||
| 44 | #[interrupt] | ||
| 45 | unsafe fn SWI_IRQ_0() { | ||
| 46 | EXECUTOR_HI.on_interrupt() | ||
| 47 | } | ||
| 48 | |||
| 49 | #[entry] | ||
| 50 | fn main() -> ! { | ||
| 51 | let p = embassy_rp::init(Default::default()); | ||
| 52 | info!("Here we go!"); | ||
| 53 | |||
| 54 | 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) | ||
| 56 | static UART: StaticCell<UartMutex> = StaticCell::new(); | ||
| 57 | let uart = UART.init(mutex::Mutex::new(uart)); | ||
| 58 | |||
| 59 | // High-priority executor: runs in interrupt mode | ||
| 60 | interrupt::SWI_IRQ_0.set_priority(Priority::P3); | ||
| 61 | let spawner = EXECUTOR_HI.start(interrupt::SWI_IRQ_0); | ||
| 62 | spawner.must_spawn(task_a(uart)); | ||
| 63 | |||
| 64 | // Low priority executor: runs in thread mode | ||
| 65 | let executor = EXECUTOR_LOW.init(Executor::new()); | ||
| 66 | executor.run(|spawner| { | ||
| 67 | // No Mutex needed when sharing between tasks running on the same executor | ||
| 68 | |||
| 69 | // Use Cell for Copy-types | ||
| 70 | static CELL: ConstStaticCell<Cell<[u8; 4]>> = ConstStaticCell::new(Cell::new([0; 4])); | ||
| 71 | let cell = CELL.take(); | ||
| 72 | |||
| 73 | // Use RefCell for &mut access | ||
| 74 | static REF_CELL: ConstStaticCell<RefCell<MyType>> = ConstStaticCell::new(RefCell::new(MyType { inner: 0 })); | ||
| 75 | let ref_cell = REF_CELL.take(); | ||
| 76 | |||
| 77 | spawner.must_spawn(task_b(uart, cell, ref_cell)); | ||
| 78 | spawner.must_spawn(task_c(cell, ref_cell)); | ||
| 79 | }); | ||
| 80 | } | ||
| 81 | |||
| 82 | #[embassy_executor::task] | ||
| 83 | async fn task_a(uart: &'static UartMutex) { | ||
| 84 | let mut ticker = Ticker::every(Duration::from_secs(1)); | ||
| 85 | loop { | ||
| 86 | let random = RoscRng.next_u32(); | ||
| 87 | |||
| 88 | { | ||
| 89 | let mut uart = uart.lock().await; | ||
| 90 | uart.write(b"task a").await.unwrap(); | ||
| 91 | // The uart lock is released when it goes out of scope | ||
| 92 | } | ||
| 93 | |||
| 94 | ATOMIC.store(random, Ordering::Relaxed); | ||
| 95 | |||
| 96 | MUTEX_BLOCKING.lock(|x| x.borrow_mut().inner = random); | ||
| 97 | |||
| 98 | ticker.next().await; | ||
| 99 | } | ||
| 100 | } | ||
| 101 | |||
| 102 | #[embassy_executor::task] | ||
| 103 | async fn task_b(uart: &'static UartMutex, cell: &'static Cell<[u8; 4]>, ref_cell: &'static RefCell<MyType>) { | ||
| 104 | let mut ticker = Ticker::every(Duration::from_secs(1)); | ||
| 105 | loop { | ||
| 106 | let random = RoscRng.next_u32(); | ||
| 107 | |||
| 108 | uart.lock().await.write(b"task b").await.unwrap(); | ||
| 109 | |||
| 110 | cell.set(random.to_be_bytes()); | ||
| 111 | |||
| 112 | ref_cell.borrow_mut().inner = random; | ||
| 113 | |||
| 114 | ticker.next().await; | ||
| 115 | } | ||
| 116 | } | ||
| 117 | |||
| 118 | #[embassy_executor::task] | ||
| 119 | async fn task_c(cell: &'static Cell<[u8; 4]>, ref_cell: &'static RefCell<MyType>) { | ||
| 120 | let mut ticker = Ticker::every(Duration::from_secs(1)); | ||
| 121 | loop { | ||
| 122 | info!("======================="); | ||
| 123 | |||
| 124 | let atomic = ATOMIC.load(Ordering::Relaxed); | ||
| 125 | info!("atomic: {}", atomic); | ||
| 126 | |||
| 127 | MUTEX_BLOCKING.lock(|x| { | ||
| 128 | let val = x.borrow().inner; | ||
| 129 | info!("blocking mutex: {}", val); | ||
| 130 | }); | ||
| 131 | |||
| 132 | let cell_val = cell.get(); | ||
| 133 | info!("cell: {:?}", cell_val); | ||
| 134 | |||
| 135 | let ref_cell_val = ref_cell.borrow().inner; | ||
| 136 | info!("ref_cell: {:?}", ref_cell_val); | ||
| 137 | |||
| 138 | ticker.next().await; | ||
| 139 | } | ||
| 140 | } | ||
