diff options
| author | Dion Dokter <[email protected]> | 2024-08-05 11:21:21 +0200 |
|---|---|---|
| committer | Dion Dokter <[email protected]> | 2024-08-05 11:21:21 +0200 |
| commit | ab4d378dda5a74834dcc1fc0c872824f4a616911 (patch) | |
| tree | d79824af61988c9adae883fa04e40b7b74e112a9 /examples/rp/src | |
| parent | 2a7fe16ceb53aca38f73ac01a923ea445654673c (diff) | |
| parent | 0f685761e09b1617e435de4e50986fe9a548502e (diff) | |
Merge branch 'master' into stm-dualcore
Diffstat (limited to 'examples/rp/src')
| -rw-r--r-- | examples/rp/src/bin/assign_resources.rs | 79 | ||||
| -rw-r--r-- | examples/rp/src/bin/shared_bus.rs | 115 | ||||
| -rw-r--r-- | examples/rp/src/bin/sharing.rs | 150 | ||||
| -rw-r--r-- | examples/rp/src/bin/uart_buffered_split.rs | 2 |
4 files changed, 345 insertions, 1 deletions
diff --git a/examples/rp/src/bin/assign_resources.rs b/examples/rp/src/bin/assign_resources.rs new file mode 100644 index 000000000..ff6eff4a2 --- /dev/null +++ b/examples/rp/src/bin/assign_resources.rs | |||
| @@ -0,0 +1,79 @@ | |||
| 1 | //! This example demonstrates how to assign resources to multiple tasks by splitting up the peripherals. | ||
| 2 | //! It is not about sharing the same resources between tasks, see sharing.rs for that or head to https://embassy.dev/book/#_sharing_peripherals_between_tasks) | ||
| 3 | //! Of course splitting up resources and sharing resources can be combined, yet this example is only about splitting up resources. | ||
| 4 | //! | ||
| 5 | //! There are basically two ways we demonstrate here: | ||
| 6 | //! 1) Assigning resources to a task by passing parts of the peripherals | ||
| 7 | //! 2) Assigning resources to a task by passing a struct with the split up peripherals, using the assign-resources macro | ||
| 8 | //! | ||
| 9 | //! using four LEDs on Pins 10, 11, 20 and 21 | ||
| 10 | |||
| 11 | #![no_std] | ||
| 12 | #![no_main] | ||
| 13 | |||
| 14 | use assign_resources::assign_resources; | ||
| 15 | use defmt::*; | ||
| 16 | use embassy_executor::Spawner; | ||
| 17 | use embassy_rp::gpio::{Level, Output}; | ||
| 18 | use embassy_rp::peripherals::{self, PIN_20, PIN_21}; | ||
| 19 | use embassy_time::Timer; | ||
| 20 | use {defmt_rtt as _, panic_probe as _}; | ||
| 21 | |||
| 22 | #[embassy_executor::main] | ||
| 23 | async fn main(spawner: Spawner) { | ||
| 24 | // initialize the peripherals | ||
| 25 | let p = embassy_rp::init(Default::default()); | ||
| 26 | |||
| 27 | // 1) Assigning a resource to a task by passing parts of the peripherals. | ||
| 28 | spawner | ||
| 29 | .spawn(double_blinky_manually_assigned(spawner, p.PIN_20, p.PIN_21)) | ||
| 30 | .unwrap(); | ||
| 31 | |||
| 32 | // 2) Using the assign-resources macro to assign resources to a task. | ||
| 33 | // we perform the split, see further below for the definition of the resources struct | ||
| 34 | let r = split_resources!(p); | ||
| 35 | // and then we can use them | ||
| 36 | spawner.spawn(double_blinky_macro_assigned(spawner, r.leds)).unwrap(); | ||
| 37 | } | ||
| 38 | |||
| 39 | // 1) Assigning a resource to a task by passing parts of the peripherals. | ||
| 40 | #[embassy_executor::task] | ||
| 41 | async fn double_blinky_manually_assigned(_spawner: Spawner, pin_20: PIN_20, pin_21: PIN_21) { | ||
| 42 | let mut led_20 = Output::new(pin_20, Level::Low); | ||
| 43 | let mut led_21 = Output::new(pin_21, Level::High); | ||
| 44 | |||
| 45 | loop { | ||
| 46 | info!("toggling leds"); | ||
| 47 | led_20.toggle(); | ||
| 48 | led_21.toggle(); | ||
| 49 | Timer::after_secs(1).await; | ||
| 50 | } | ||
| 51 | } | ||
| 52 | |||
| 53 | // 2) Using the assign-resources macro to assign resources to a task. | ||
| 54 | // first we define the resources we want to assign to the task using the assign_resources! macro | ||
| 55 | // basically this will split up the peripherals struct into smaller structs, that we define here | ||
| 56 | // naming is up to you, make sure your future self understands what you did here | ||
| 57 | assign_resources! { | ||
| 58 | leds: Leds{ | ||
| 59 | led_10: PIN_10, | ||
| 60 | led_11: PIN_11, | ||
| 61 | } | ||
| 62 | // add more resources to more structs if needed, for example defining one struct for each task | ||
| 63 | } | ||
| 64 | // this could be done in another file and imported here, but for the sake of simplicity we do it here | ||
| 65 | // see https://github.com/adamgreig/assign-resources for more information | ||
| 66 | |||
| 67 | // 2) Using the split resources in a task | ||
| 68 | #[embassy_executor::task] | ||
| 69 | async fn double_blinky_macro_assigned(_spawner: Spawner, r: Leds) { | ||
| 70 | let mut led_10 = Output::new(r.led_10, Level::Low); | ||
| 71 | let mut led_11 = Output::new(r.led_11, Level::High); | ||
| 72 | |||
| 73 | loop { | ||
| 74 | info!("toggling leds"); | ||
| 75 | led_10.toggle(); | ||
| 76 | led_11.toggle(); | ||
| 77 | Timer::after_secs(1).await; | ||
| 78 | } | ||
| 79 | } | ||
diff --git a/examples/rp/src/bin/shared_bus.rs b/examples/rp/src/bin/shared_bus.rs new file mode 100644 index 000000000..c6cb5d64c --- /dev/null +++ b/examples/rp/src/bin/shared_bus.rs | |||
| @@ -0,0 +1,115 @@ | |||
| 1 | //! This example shows how to share (async) I2C and SPI buses between multiple devices. | ||
| 2 | |||
| 3 | #![no_std] | ||
| 4 | #![no_main] | ||
| 5 | |||
| 6 | use defmt::*; | ||
| 7 | use embassy_embedded_hal::shared_bus::asynch::i2c::I2cDevice; | ||
| 8 | use embassy_embedded_hal::shared_bus::asynch::spi::SpiDevice; | ||
| 9 | use embassy_executor::Spawner; | ||
| 10 | use embassy_rp::bind_interrupts; | ||
| 11 | use embassy_rp::gpio::{AnyPin, Level, Output}; | ||
| 12 | use embassy_rp::i2c::{self, I2c, InterruptHandler}; | ||
| 13 | use embassy_rp::peripherals::{I2C1, SPI1}; | ||
| 14 | use embassy_rp::spi::{self, Spi}; | ||
| 15 | use embassy_sync::blocking_mutex::raw::NoopRawMutex; | ||
| 16 | use embassy_sync::mutex::Mutex; | ||
| 17 | use embassy_time::Timer; | ||
| 18 | use static_cell::StaticCell; | ||
| 19 | use {defmt_rtt as _, panic_probe as _}; | ||
| 20 | |||
| 21 | type Spi1Bus = Mutex<NoopRawMutex, Spi<'static, SPI1, spi::Async>>; | ||
| 22 | type I2c1Bus = Mutex<NoopRawMutex, I2c<'static, I2C1, i2c::Async>>; | ||
| 23 | |||
| 24 | bind_interrupts!(struct Irqs { | ||
| 25 | I2C1_IRQ => InterruptHandler<I2C1>; | ||
| 26 | }); | ||
| 27 | |||
| 28 | #[embassy_executor::main] | ||
| 29 | async fn main(spawner: Spawner) { | ||
| 30 | let p = embassy_rp::init(Default::default()); | ||
| 31 | info!("Here we go!"); | ||
| 32 | |||
| 33 | // Shared I2C bus | ||
| 34 | let i2c = I2c::new_async(p.I2C1, p.PIN_15, p.PIN_14, Irqs, i2c::Config::default()); | ||
| 35 | static I2C_BUS: StaticCell<I2c1Bus> = StaticCell::new(); | ||
| 36 | let i2c_bus = I2C_BUS.init(Mutex::new(i2c)); | ||
| 37 | |||
| 38 | spawner.must_spawn(i2c_task_a(i2c_bus)); | ||
| 39 | spawner.must_spawn(i2c_task_b(i2c_bus)); | ||
| 40 | |||
| 41 | // Shared SPI bus | ||
| 42 | let spi_cfg = spi::Config::default(); | ||
| 43 | let spi = Spi::new(p.SPI1, p.PIN_10, p.PIN_11, p.PIN_12, p.DMA_CH0, p.DMA_CH1, spi_cfg); | ||
| 44 | static SPI_BUS: StaticCell<Spi1Bus> = StaticCell::new(); | ||
| 45 | let spi_bus = SPI_BUS.init(Mutex::new(spi)); | ||
| 46 | |||
| 47 | // Chip select pins for the SPI devices | ||
| 48 | let cs_a = Output::new(AnyPin::from(p.PIN_0), Level::High); | ||
| 49 | let cs_b = Output::new(AnyPin::from(p.PIN_1), Level::High); | ||
| 50 | |||
| 51 | spawner.must_spawn(spi_task_a(spi_bus, cs_a)); | ||
| 52 | spawner.must_spawn(spi_task_b(spi_bus, cs_b)); | ||
| 53 | } | ||
| 54 | |||
| 55 | #[embassy_executor::task] | ||
| 56 | async fn i2c_task_a(i2c_bus: &'static I2c1Bus) { | ||
| 57 | let i2c_dev = I2cDevice::new(i2c_bus); | ||
| 58 | let _sensor = DummyI2cDeviceDriver::new(i2c_dev, 0xC0); | ||
| 59 | loop { | ||
| 60 | info!("i2c task A"); | ||
| 61 | Timer::after_secs(1).await; | ||
| 62 | } | ||
| 63 | } | ||
| 64 | |||
| 65 | #[embassy_executor::task] | ||
| 66 | async fn i2c_task_b(i2c_bus: &'static I2c1Bus) { | ||
| 67 | let i2c_dev = I2cDevice::new(i2c_bus); | ||
| 68 | let _sensor = DummyI2cDeviceDriver::new(i2c_dev, 0xDE); | ||
| 69 | loop { | ||
| 70 | info!("i2c task B"); | ||
| 71 | Timer::after_secs(1).await; | ||
| 72 | } | ||
| 73 | } | ||
| 74 | |||
| 75 | #[embassy_executor::task] | ||
| 76 | async fn spi_task_a(spi_bus: &'static Spi1Bus, cs: Output<'static>) { | ||
| 77 | let spi_dev = SpiDevice::new(spi_bus, cs); | ||
| 78 | let _sensor = DummySpiDeviceDriver::new(spi_dev); | ||
| 79 | loop { | ||
| 80 | info!("spi task A"); | ||
| 81 | Timer::after_secs(1).await; | ||
| 82 | } | ||
| 83 | } | ||
| 84 | |||
| 85 | #[embassy_executor::task] | ||
| 86 | async fn spi_task_b(spi_bus: &'static Spi1Bus, cs: Output<'static>) { | ||
| 87 | let spi_dev = SpiDevice::new(spi_bus, cs); | ||
| 88 | let _sensor = DummySpiDeviceDriver::new(spi_dev); | ||
| 89 | loop { | ||
| 90 | info!("spi task B"); | ||
| 91 | Timer::after_secs(1).await; | ||
| 92 | } | ||
| 93 | } | ||
| 94 | |||
| 95 | // Dummy I2C device driver, using `embedded-hal-async` | ||
| 96 | struct DummyI2cDeviceDriver<I2C: embedded_hal_async::i2c::I2c> { | ||
| 97 | _i2c: I2C, | ||
| 98 | } | ||
| 99 | |||
| 100 | impl<I2C: embedded_hal_async::i2c::I2c> DummyI2cDeviceDriver<I2C> { | ||
| 101 | fn new(i2c_dev: I2C, _address: u8) -> Self { | ||
| 102 | Self { _i2c: i2c_dev } | ||
| 103 | } | ||
| 104 | } | ||
| 105 | |||
| 106 | // Dummy SPI device driver, using `embedded-hal-async` | ||
| 107 | struct DummySpiDeviceDriver<SPI: embedded_hal_async::spi::SpiDevice> { | ||
| 108 | _spi: SPI, | ||
| 109 | } | ||
| 110 | |||
| 111 | impl<SPI: embedded_hal_async::spi::SpiDevice> DummySpiDeviceDriver<SPI> { | ||
| 112 | fn new(spi_dev: SPI) -> Self { | ||
| 113 | Self { _spi: spi_dev } | ||
| 114 | } | ||
| 115 | } | ||
diff --git a/examples/rp/src/bin/sharing.rs b/examples/rp/src/bin/sharing.rs new file mode 100644 index 000000000..5416e20ce --- /dev/null +++ b/examples/rp/src/bin/sharing.rs | |||
| @@ -0,0 +1,150 @@ | |||
| 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 | ||
| 12 | |||
| 13 | #![no_std] | ||
| 14 | #![no_main] | ||
| 15 | |||
| 16 | use core::cell::{Cell, RefCell}; | ||
| 17 | use core::sync::atomic::{AtomicU32, Ordering}; | ||
| 18 | |||
| 19 | use cortex_m_rt::entry; | ||
| 20 | use defmt::info; | ||
| 21 | use embassy_executor::{Executor, InterruptExecutor}; | ||
| 22 | use embassy_rp::clocks::RoscRng; | ||
| 23 | use embassy_rp::interrupt::{InterruptExt, Priority}; | ||
| 24 | use embassy_rp::peripherals::UART0; | ||
| 25 | use embassy_rp::uart::{self, InterruptHandler, UartTx}; | ||
| 26 | use embassy_rp::{bind_interrupts, interrupt}; | ||
| 27 | use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex; | ||
| 28 | use embassy_sync::{blocking_mutex, mutex}; | ||
| 29 | use embassy_time::{Duration, Ticker}; | ||
| 30 | use rand::RngCore; | ||
| 31 | use static_cell::{ConstStaticCell, StaticCell}; | ||
| 32 | use {defmt_rtt as _, panic_probe as _}; | ||
| 33 | |||
| 34 | type UartAsyncMutex = mutex::Mutex<CriticalSectionRawMutex, UartTx<'static, UART0, uart::Async>>; | ||
| 35 | |||
| 36 | struct MyType { | ||
| 37 | inner: u32, | ||
| 38 | } | ||
| 39 | |||
| 40 | static EXECUTOR_HI: InterruptExecutor = InterruptExecutor::new(); | ||
| 41 | static EXECUTOR_LOW: StaticCell<Executor> = StaticCell::new(); | ||
| 42 | |||
| 43 | // Use Atomics for simple values | ||
| 44 | static ATOMIC: AtomicU32 = AtomicU32::new(0); | ||
| 45 | |||
| 46 | // Use blocking Mutex with Cell/RefCell for sharing non-async things | ||
| 47 | static MUTEX_BLOCKING: blocking_mutex::Mutex<CriticalSectionRawMutex, RefCell<MyType>> = | ||
| 48 | blocking_mutex::Mutex::new(RefCell::new(MyType { inner: 0 })); | ||
| 49 | |||
| 50 | bind_interrupts!(struct Irqs { | ||
| 51 | UART0_IRQ => InterruptHandler<UART0>; | ||
| 52 | }); | ||
| 53 | |||
| 54 | #[interrupt] | ||
| 55 | unsafe fn SWI_IRQ_0() { | ||
| 56 | EXECUTOR_HI.on_interrupt() | ||
| 57 | } | ||
| 58 | |||
| 59 | #[entry] | ||
| 60 | fn main() -> ! { | ||
| 61 | let p = embassy_rp::init(Default::default()); | ||
| 62 | info!("Here we go!"); | ||
| 63 | |||
| 64 | let uart = UartTx::new(p.UART0, p.PIN_0, p.DMA_CH0, uart::Config::default()); | ||
| 65 | // Use the async Mutex for sharing async things (built-in interior mutability) | ||
| 66 | static UART: StaticCell<UartAsyncMutex> = StaticCell::new(); | ||
| 67 | let uart = UART.init(mutex::Mutex::new(uart)); | ||
| 68 | |||
| 69 | // High-priority executor: runs in interrupt mode | ||
| 70 | interrupt::SWI_IRQ_0.set_priority(Priority::P3); | ||
| 71 | let spawner = EXECUTOR_HI.start(interrupt::SWI_IRQ_0); | ||
| 72 | spawner.must_spawn(task_a(uart)); | ||
| 73 | |||
| 74 | // Low priority executor: runs in thread mode | ||
| 75 | let executor = EXECUTOR_LOW.init(Executor::new()); | ||
| 76 | executor.run(|spawner| { | ||
| 77 | // No Mutex needed when sharing between tasks running on the same executor | ||
| 78 | |||
| 79 | // Use Cell for Copy-types | ||
| 80 | static CELL: ConstStaticCell<Cell<[u8; 4]>> = ConstStaticCell::new(Cell::new([0; 4])); | ||
| 81 | let cell = CELL.take(); | ||
| 82 | |||
| 83 | // Use RefCell for &mut access | ||
| 84 | static REF_CELL: ConstStaticCell<RefCell<MyType>> = ConstStaticCell::new(RefCell::new(MyType { inner: 0 })); | ||
| 85 | let ref_cell = REF_CELL.take(); | ||
| 86 | |||
| 87 | spawner.must_spawn(task_b(uart, cell, ref_cell)); | ||
| 88 | spawner.must_spawn(task_c(cell, ref_cell)); | ||
| 89 | }); | ||
| 90 | } | ||
| 91 | |||
| 92 | #[embassy_executor::task] | ||
| 93 | async fn task_a(uart: &'static UartAsyncMutex) { | ||
| 94 | let mut ticker = Ticker::every(Duration::from_secs(1)); | ||
| 95 | loop { | ||
| 96 | let random = RoscRng.next_u32(); | ||
| 97 | |||
| 98 | { | ||
| 99 | let mut uart = uart.lock().await; | ||
| 100 | uart.write(b"task a").await.unwrap(); | ||
| 101 | // The uart lock is released when it goes out of scope | ||
| 102 | } | ||
| 103 | |||
| 104 | ATOMIC.store(random, Ordering::Relaxed); | ||
| 105 | |||
| 106 | MUTEX_BLOCKING.lock(|x| x.borrow_mut().inner = random); | ||
| 107 | |||
| 108 | ticker.next().await; | ||
| 109 | } | ||
| 110 | } | ||
| 111 | |||
| 112 | #[embassy_executor::task] | ||
| 113 | async fn task_b(uart: &'static UartAsyncMutex, cell: &'static Cell<[u8; 4]>, ref_cell: &'static RefCell<MyType>) { | ||
| 114 | let mut ticker = Ticker::every(Duration::from_secs(1)); | ||
| 115 | loop { | ||
| 116 | let random = RoscRng.next_u32(); | ||
| 117 | |||
| 118 | uart.lock().await.write(b"task b").await.unwrap(); | ||
| 119 | |||
| 120 | cell.set(random.to_be_bytes()); | ||
| 121 | |||
| 122 | ref_cell.borrow_mut().inner = random; | ||
| 123 | |||
| 124 | ticker.next().await; | ||
| 125 | } | ||
| 126 | } | ||
| 127 | |||
| 128 | #[embassy_executor::task] | ||
| 129 | async fn task_c(cell: &'static Cell<[u8; 4]>, ref_cell: &'static RefCell<MyType>) { | ||
| 130 | let mut ticker = Ticker::every(Duration::from_secs(1)); | ||
| 131 | loop { | ||
| 132 | info!("======================="); | ||
| 133 | |||
| 134 | let atomic_val = ATOMIC.load(Ordering::Relaxed); | ||
| 135 | info!("atomic: {}", atomic_val); | ||
| 136 | |||
| 137 | MUTEX_BLOCKING.lock(|x| { | ||
| 138 | let val = x.borrow().inner; | ||
| 139 | info!("blocking mutex: {}", val); | ||
| 140 | }); | ||
| 141 | |||
| 142 | let cell_val = cell.get(); | ||
| 143 | info!("cell: {:?}", cell_val); | ||
| 144 | |||
| 145 | let ref_cell_val = ref_cell.borrow().inner; | ||
| 146 | info!("ref_cell: {:?}", ref_cell_val); | ||
| 147 | |||
| 148 | ticker.next().await; | ||
| 149 | } | ||
| 150 | } | ||
diff --git a/examples/rp/src/bin/uart_buffered_split.rs b/examples/rp/src/bin/uart_buffered_split.rs index fac61aa04..468d2b61a 100644 --- a/examples/rp/src/bin/uart_buffered_split.rs +++ b/examples/rp/src/bin/uart_buffered_split.rs | |||
| @@ -31,7 +31,7 @@ async fn main(spawner: Spawner) { | |||
| 31 | static RX_BUF: StaticCell<[u8; 16]> = StaticCell::new(); | 31 | static RX_BUF: StaticCell<[u8; 16]> = StaticCell::new(); |
| 32 | let rx_buf = &mut RX_BUF.init([0; 16])[..]; | 32 | let rx_buf = &mut RX_BUF.init([0; 16])[..]; |
| 33 | let uart = BufferedUart::new(uart, Irqs, tx_pin, rx_pin, tx_buf, rx_buf, Config::default()); | 33 | let uart = BufferedUart::new(uart, Irqs, tx_pin, rx_pin, tx_buf, rx_buf, Config::default()); |
| 34 | let (rx, mut tx) = uart.split(); | 34 | let (mut tx, rx) = uart.split(); |
| 35 | 35 | ||
| 36 | unwrap!(spawner.spawn(reader(rx))); | 36 | unwrap!(spawner.spawn(reader(rx))); |
| 37 | 37 | ||
