aboutsummaryrefslogtreecommitdiff
path: root/examples/nrf9160/src
diff options
context:
space:
mode:
authorUlf Lilleengen <[email protected]>2024-09-04 14:09:17 +0200
committerUlf Lilleengen <[email protected]>2024-09-04 14:09:17 +0200
commitb4221d75b87664485977d37df28f7319143411fc (patch)
tree7002cc6264f300c4d321e9bbece46098157d8194 /examples/nrf9160/src
parent5e27a3e64f46340e50bc6f61e6ef5a89e9f077ab (diff)
Make tracing optional and use dedicated task
Diffstat (limited to 'examples/nrf9160/src')
-rw-r--r--examples/nrf9160/src/bin/modem_tcp_client.rs58
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 @@
4use core::mem::MaybeUninit; 4use core::mem::MaybeUninit;
5use core::net::IpAddr; 5use core::net::IpAddr;
6use core::ptr::addr_of_mut; 6use core::ptr::addr_of_mut;
7use core::str::FromStr;
8use core::slice; 7use core::slice;
8use core::str::FromStr;
9 9
10use defmt::{info, warn, unwrap}; 10use defmt::{info, unwrap, warn};
11use heapless::Vec;
12use embassy_executor::Spawner; 11use embassy_executor::Spawner;
13use embassy_net::{Ipv4Address, Ipv4Cidr, Stack, StackResources}; 12use embassy_net::{Ipv4Address, Ipv4Cidr, Stack, StackResources};
14use embassy_net_nrf91::{Runner, State, context}; 13use embassy_net_nrf91::{context, Runner, State, TraceBuffer, TraceReader};
15use embassy_nrf::buffered_uarte::{self, BufferedUarteTx}; 14use embassy_nrf::buffered_uarte::{self, BufferedUarteTx};
16use embassy_nrf::gpio::{AnyPin, Level, Output, OutputDrive, Pin}; 15use embassy_nrf::gpio::{AnyPin, Level, Output, OutputDrive, Pin};
17use embassy_nrf::uarte::Baudrate; 16use embassy_nrf::uarte::Baudrate;
18use embassy_nrf::{bind_interrupts, interrupt, peripherals, uarte}; 17use embassy_nrf::{bind_interrupts, interrupt, peripherals, uarte};
19use embassy_time::{Duration, Timer}; 18use embassy_time::{Duration, Timer};
20use embedded_io_async::Write; 19use embedded_io_async::Write;
20use heapless::Vec;
21use static_cell::StaticCell; 21use static_cell::StaticCell;
22use {defmt_rtt as _, panic_probe as _}; 22use {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 34async 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 {
37struct TraceWriter(BufferedUarteTx<'static, peripherals::SERIAL0>); 37 let n = reader.read(&mut rx[..]).await;
38 38 unwrap!(uart.write_all(&rx[..n]).await);
39impl embedded_io::ErrorType for TraceWriter {
40 type Error = core::convert::Infallible;
41}
42
43impl 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]
54async fn modem_task(runner: Runner<'static, TraceWriter>) -> ! { 43async 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