diff options
| author | James Munns <[email protected]> | 2025-12-05 15:22:38 +0100 |
|---|---|---|
| committer | James Munns <[email protected]> | 2025-12-05 15:22:38 +0100 |
| commit | fa54dd5849a083b286b2a3f1928428c8704d3d70 (patch) | |
| tree | 56f6e4f4ad5233d357a7ef5fefb38083f32d0562 /examples/mcxa/src/bin | |
| parent | 1ce2083f9a0fcf1cfbb10de0fb3ed44b460a5cc7 (diff) | |
Create separate ring buffered RX receiver to encapsulate unsafe
Diffstat (limited to 'examples/mcxa/src/bin')
| -rw-r--r-- | examples/mcxa/src/bin/lpuart_dma.rs | 2 | ||||
| -rw-r--r-- | examples/mcxa/src/bin/lpuart_ring_buffer.rs | 15 |
2 files changed, 5 insertions, 12 deletions
diff --git a/examples/mcxa/src/bin/lpuart_dma.rs b/examples/mcxa/src/bin/lpuart_dma.rs index 1fc6595e6..34d343452 100644 --- a/examples/mcxa/src/bin/lpuart_dma.rs +++ b/examples/mcxa/src/bin/lpuart_dma.rs | |||
| @@ -14,7 +14,7 @@ use embassy_executor::Spawner; | |||
| 14 | use embassy_mcxa::clocks::config::Div8; | 14 | use embassy_mcxa::clocks::config::Div8; |
| 15 | use embassy_mcxa::dma::{DmaCh0InterruptHandler, DmaCh1InterruptHandler}; | 15 | use embassy_mcxa::dma::{DmaCh0InterruptHandler, DmaCh1InterruptHandler}; |
| 16 | use embassy_mcxa::lpuart::{Config, LpuartDma}; | 16 | use embassy_mcxa::lpuart::{Config, LpuartDma}; |
| 17 | use embassy_mcxa::{bind_interrupts, pac}; | 17 | use embassy_mcxa::bind_interrupts; |
| 18 | use {defmt_rtt as _, embassy_mcxa as hal, panic_probe as _}; | 18 | use {defmt_rtt as _, embassy_mcxa as hal, panic_probe as _}; |
| 19 | 19 | ||
| 20 | // Bind DMA channel interrupts using Embassy-style macro | 20 | // Bind DMA channel interrupts using Embassy-style macro |
diff --git a/examples/mcxa/src/bin/lpuart_ring_buffer.rs b/examples/mcxa/src/bin/lpuart_ring_buffer.rs index 1d1a51970..b707e20f8 100644 --- a/examples/mcxa/src/bin/lpuart_ring_buffer.rs +++ b/examples/mcxa/src/bin/lpuart_ring_buffer.rs | |||
| @@ -23,6 +23,7 @@ use embassy_mcxa::bind_interrupts; | |||
| 23 | use embassy_mcxa::clocks::config::Div8; | 23 | use embassy_mcxa::clocks::config::Div8; |
| 24 | use embassy_mcxa::dma::{DmaCh0InterruptHandler, DmaCh1InterruptHandler}; | 24 | use embassy_mcxa::dma::{DmaCh0InterruptHandler, DmaCh1InterruptHandler}; |
| 25 | use embassy_mcxa::lpuart::{Config, LpuartDma, LpuartTxDma}; | 25 | use embassy_mcxa::lpuart::{Config, LpuartDma, LpuartTxDma}; |
| 26 | use static_cell::ConstStaticCell; | ||
| 26 | use {defmt_rtt as _, embassy_mcxa as hal, panic_probe as _}; | 27 | use {defmt_rtt as _, embassy_mcxa as hal, panic_probe as _}; |
| 27 | 28 | ||
| 28 | // Bind DMA channel interrupts | 29 | // Bind DMA channel interrupts |
| @@ -32,7 +33,7 @@ bind_interrupts!(struct Irqs { | |||
| 32 | }); | 33 | }); |
| 33 | 34 | ||
| 34 | // Ring buffer for RX - power of 2 is ideal for modulo efficiency | 35 | // Ring buffer for RX - power of 2 is ideal for modulo efficiency |
| 35 | static mut RX_RING_BUFFER: [u8; 64] = [0; 64]; | 36 | static RX_RING_BUFFER: ConstStaticCell<[u8; 64]> = ConstStaticCell::new([0; 64]); |
| 36 | 37 | ||
| 37 | /// Helper to write a byte as hex to UART | 38 | /// Helper to write a byte as hex to UART |
| 38 | fn write_hex<T: embassy_mcxa::lpuart::Instance, C: embassy_mcxa::dma::Channel>( | 39 | fn write_hex<T: embassy_mcxa::lpuart::Instance, C: embassy_mcxa::dma::Channel>( |
| @@ -75,17 +76,9 @@ async fn main(_spawner: Spawner) { | |||
| 75 | tx.blocking_write(b"Setting up circular DMA for UART RX...\r\n") | 76 | tx.blocking_write(b"Setting up circular DMA for UART RX...\r\n") |
| 76 | .unwrap(); | 77 | .unwrap(); |
| 77 | 78 | ||
| 79 | let buf = RX_RING_BUFFER.take(); | ||
| 78 | // Set up the ring buffer with circular DMA | 80 | // Set up the ring buffer with circular DMA |
| 79 | // The HAL handles: DMA request source, RDMAE enable, circular transfer config, NVIC enable | 81 | let mut ring_buf = rx.into_ring_dma_rx(buf); |
| 80 | let ring_buf = unsafe { | ||
| 81 | let buf = &mut *core::ptr::addr_of_mut!(RX_RING_BUFFER); | ||
| 82 | rx.setup_ring_buffer(buf) | ||
| 83 | }; | ||
| 84 | |||
| 85 | // Enable DMA requests to start continuous reception | ||
| 86 | unsafe { | ||
| 87 | rx.enable_dma_request(); | ||
| 88 | } | ||
| 89 | 82 | ||
| 90 | tx.blocking_write(b"Ring buffer ready! Type characters to see them echoed.\r\n") | 83 | tx.blocking_write(b"Ring buffer ready! Type characters to see them echoed.\r\n") |
| 91 | .unwrap(); | 84 | .unwrap(); |
