diff options
| author | Ulf Lilleengen <[email protected]> | 2024-09-04 14:09:17 +0200 |
|---|---|---|
| committer | Ulf Lilleengen <[email protected]> | 2024-09-04 14:09:17 +0200 |
| commit | b4221d75b87664485977d37df28f7319143411fc (patch) | |
| tree | 7002cc6264f300c4d321e9bbece46098157d8194 /examples/nrf9160 | |
| parent | 5e27a3e64f46340e50bc6f61e6ef5a89e9f077ab (diff) | |
Make tracing optional and use dedicated task
Diffstat (limited to 'examples/nrf9160')
| -rw-r--r-- | examples/nrf9160/src/bin/modem_tcp_client.rs | 58 |
1 files changed, 27 insertions, 31 deletions
diff --git a/examples/nrf9160/src/bin/modem_tcp_client.rs b/examples/nrf9160/src/bin/modem_tcp_client.rs index 55ab2a707..c65e6e153 100644 --- a/examples/nrf9160/src/bin/modem_tcp_client.rs +++ b/examples/nrf9160/src/bin/modem_tcp_client.rs | |||
| @@ -4,20 +4,20 @@ | |||
| 4 | use core::mem::MaybeUninit; | 4 | use core::mem::MaybeUninit; |
| 5 | use core::net::IpAddr; | 5 | use core::net::IpAddr; |
| 6 | use core::ptr::addr_of_mut; | 6 | use core::ptr::addr_of_mut; |
| 7 | use core::str::FromStr; | ||
| 8 | use core::slice; | 7 | use core::slice; |
| 8 | use core::str::FromStr; | ||
| 9 | 9 | ||
| 10 | use defmt::{info, warn, unwrap}; | 10 | use defmt::{info, unwrap, warn}; |
| 11 | use heapless::Vec; | ||
| 12 | use embassy_executor::Spawner; | 11 | use embassy_executor::Spawner; |
| 13 | use embassy_net::{Ipv4Address, Ipv4Cidr, Stack, StackResources}; | 12 | use embassy_net::{Ipv4Address, Ipv4Cidr, Stack, StackResources}; |
| 14 | use embassy_net_nrf91::{Runner, State, context}; | 13 | use embassy_net_nrf91::{context, Runner, State, TraceBuffer, TraceReader}; |
| 15 | use embassy_nrf::buffered_uarte::{self, BufferedUarteTx}; | 14 | use embassy_nrf::buffered_uarte::{self, BufferedUarteTx}; |
| 16 | use embassy_nrf::gpio::{AnyPin, Level, Output, OutputDrive, Pin}; | 15 | use embassy_nrf::gpio::{AnyPin, Level, Output, OutputDrive, Pin}; |
| 17 | use embassy_nrf::uarte::Baudrate; | 16 | use embassy_nrf::uarte::Baudrate; |
| 18 | use embassy_nrf::{bind_interrupts, interrupt, peripherals, uarte}; | 17 | use embassy_nrf::{bind_interrupts, interrupt, peripherals, uarte}; |
| 19 | use embassy_time::{Duration, Timer}; | 18 | use embassy_time::{Duration, Timer}; |
| 20 | use embedded_io_async::Write; | 19 | use embedded_io_async::Write; |
| 20 | use heapless::Vec; | ||
| 21 | use static_cell::StaticCell; | 21 | use static_cell::StaticCell; |
| 22 | use {defmt_rtt as _, panic_probe as _}; | 22 | use {defmt_rtt as _, panic_probe as _}; |
| 23 | 23 | ||
| @@ -30,28 +30,17 @@ bind_interrupts!(struct Irqs { | |||
| 30 | UARTE0_SPIM0_SPIS0_TWIM0_TWIS0 => buffered_uarte::InterruptHandler<peripherals::SERIAL0>; | 30 | UARTE0_SPIM0_SPIS0_TWIM0_TWIS0 => buffered_uarte::InterruptHandler<peripherals::SERIAL0>; |
| 31 | }); | 31 | }); |
| 32 | 32 | ||
| 33 | // embassy-net-nrf91 only supports blocking trace write for now. | 33 | #[embassy_executor::task] |
| 34 | // We don't want to block packet processing with slow uart writes, so | 34 | async fn trace_task(mut uart: BufferedUarteTx<'static, peripherals::SERIAL0>, reader: TraceReader<'static>) -> ! { |
| 35 | // we make an adapter that writes whatever fits in the buffer and drops | 35 | let mut rx = [0u8; 1024]; |
| 36 | // data if it's full. | 36 | loop { |
| 37 | struct TraceWriter(BufferedUarteTx<'static, peripherals::SERIAL0>); | 37 | let n = reader.read(&mut rx[..]).await; |
| 38 | 38 | unwrap!(uart.write_all(&rx[..n]).await); | |
| 39 | impl embedded_io::ErrorType for TraceWriter { | ||
| 40 | type Error = core::convert::Infallible; | ||
| 41 | } | ||
| 42 | |||
| 43 | impl embedded_io::Write for TraceWriter { | ||
| 44 | fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> { | ||
| 45 | let _ = self.0.try_write(buf); | ||
| 46 | Ok(buf.len()) | ||
| 47 | } | ||
| 48 | fn flush(&mut self) -> Result<(), Self::Error> { | ||
| 49 | Ok(()) | ||
| 50 | } | 39 | } |
| 51 | } | 40 | } |
| 52 | 41 | ||
| 53 | #[embassy_executor::task] | 42 | #[embassy_executor::task] |
| 54 | async fn modem_task(runner: Runner<'static, TraceWriter>) -> ! { | 43 | async fn modem_task(runner: Runner<'static>) -> ! { |
| 55 | runner.run().await | 44 | runner.run().await |
| 56 | } | 45 | } |
| 57 | 46 | ||
| @@ -93,8 +82,8 @@ async fn main(spawner: Spawner) { | |||
| 93 | 82 | ||
| 94 | static mut TRACE_BUF: [u8; 4096] = [0u8; 4096]; | 83 | static mut TRACE_BUF: [u8; 4096] = [0u8; 4096]; |
| 95 | let mut config = uarte::Config::default(); | 84 | let mut config = uarte::Config::default(); |
| 96 | config.baudrate = Baudrate::BAUD115200; | 85 | config.baudrate = Baudrate::BAUD1M; |
| 97 | let trace_writer = TraceWriter(BufferedUarteTx::new( | 86 | let uart = BufferedUarteTx::new( |
| 98 | //let trace_uart = BufferedUarteTx::new( | 87 | //let trace_uart = BufferedUarteTx::new( |
| 99 | unsafe { peripherals::SERIAL0::steal() }, | 88 | unsafe { peripherals::SERIAL0::steal() }, |
| 100 | Irqs, | 89 | Irqs, |
| @@ -102,11 +91,14 @@ async fn main(spawner: Spawner) { | |||
| 102 | //unsafe { peripherals::P0_14::steal() }, | 91 | //unsafe { peripherals::P0_14::steal() }, |
| 103 | config, | 92 | config, |
| 104 | unsafe { &mut *addr_of_mut!(TRACE_BUF) }, | 93 | unsafe { &mut *addr_of_mut!(TRACE_BUF) }, |
| 105 | )); | 94 | ); |
| 106 | 95 | ||
| 107 | static STATE: StaticCell<State> = StaticCell::new(); | 96 | static STATE: StaticCell<State> = StaticCell::new(); |
| 108 | let (device, control, runner) = embassy_net_nrf91::new(STATE.init(State::new()), ipc_mem, trace_writer).await; | 97 | static TRACE: StaticCell<TraceBuffer> = StaticCell::new(); |
| 98 | let (device, control, runner, tracer) = | ||
| 99 | embassy_net_nrf91::new_with_trace(STATE.init(State::new()), ipc_mem, TRACE.init(TraceBuffer::new())).await; | ||
| 109 | unwrap!(spawner.spawn(modem_task(runner))); | 100 | unwrap!(spawner.spawn(modem_task(runner))); |
| 101 | unwrap!(spawner.spawn(trace_task(uart, tracer))); | ||
| 110 | 102 | ||
| 111 | let config = embassy_net::Config::default(); | 103 | let config = embassy_net::Config::default(); |
| 112 | 104 | ||
| @@ -127,11 +119,15 @@ async fn main(spawner: Spawner) { | |||
| 127 | 119 | ||
| 128 | let control = context::Control::new(control, 0).await; | 120 | let control = context::Control::new(control, 0).await; |
| 129 | 121 | ||
| 130 | unwrap!(control.configure(context::Config { | 122 | unwrap!( |
| 131 | apn: "iot.nat.es", | 123 | control |
| 132 | auth_prot: context::AuthProt::Pap, | 124 | .configure(context::Config { |
| 133 | auth: Some(("orange", "orange")), | 125 | apn: "iot.nat.es", |
| 134 | }).await); | 126 | auth_prot: context::AuthProt::Pap, |
| 127 | auth: Some(("orange", "orange")), | ||
| 128 | }) | ||
| 129 | .await | ||
| 130 | ); | ||
| 135 | 131 | ||
| 136 | info!("waiting for attach..."); | 132 | info!("waiting for attach..."); |
| 137 | 133 | ||
