From 9e2dee492661f6fe372a498a2b4fb974e94b7cd8 Mon Sep 17 00:00:00 2001 From: Vladan Popovic Date: Mon, 25 Sep 2023 23:51:30 +0200 Subject: [PATCH] tests with embassy --- src/main.rs | 78 +++++++++++++++++++++++++++++------------------- src/modem/mod.rs | 26 ---------------- 2 files changed, 48 insertions(+), 56 deletions(-) diff --git a/src/main.rs b/src/main.rs index def0aa2..bf651ee 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,11 +8,12 @@ use esp_backtrace as _; use esp_println::println; use hal::{ clock::ClockControl, + Delay, embassy::{self, executor::Executor}, gpio::IO, - interrupt, - peripherals::{Peripherals, Interrupt, UART1}, + peripherals::{Peripherals, UART1}, prelude::*, + prelude::entry, timer::TimerGroup, Uart, uart::{ @@ -27,8 +28,16 @@ use hal::{ }; use esp_hal_common::{UartRx, UartTx}; -use atat::{asynch::AtatClient, AtatIngress, Buffers, DefaultDigester, Ingress}; -use heapless::Vec; +use atat::{ + asynch::{ + AtatClient, + Client, + }, + AtatIngress, + Buffers, + DefaultDigester, + Ingress, +}; use static_cell::StaticCell; const READ_BUF_SIZE: usize = 64; @@ -37,27 +46,24 @@ const URC_CAPACITY: usize = 128; const URC_SUBSCRIBERS: usize = 3; #[embassy_executor::task] -async fn reader(mut rx: UartRx<'static, UART1>) { - esp_println::println!("reading..."); - // max message size to receive - // leave some extra space for AT-CMD characters - const MAX_BUFFER_SIZE: usize = 10 * READ_BUF_SIZE + 16; - - let mut rbuf: Vec = Vec::new(); - let mut offset = 0; - while let Ok(len) = embedded_io_async::Read::read(&mut rx, &mut rbuf[offset..]).await { - offset += len; - if offset == 0 { - rbuf.truncate(0); - break; - } - // if set_at_cmd is used than stop reading - if len < READ_BUF_SIZE { - rbuf.truncate(offset); - break; - } - } +async fn ingress_task( + mut ingress: Ingress< + 'static, + DefaultDigester, + modem::Urc, + INGRESS_BUF_SIZE, + URC_CAPACITY, + URC_SUBSCRIBERS, + >, + mut rx: UartRx<'static, UART1>) { + println!("reading in ingress_task..."); + ingress.read_from(&mut rx).await; +} +#[embassy_executor::task] +async fn test_send(mut client: Client<'static, UartTx<'static, UART1>, INGRESS_BUF_SIZE>) { + println!("sending a single message (GetManufacturerId)"); + client.send(&modem::GetManufacturerId).await.ok(); } #[entry] @@ -65,6 +71,7 @@ fn main() -> ! { let peripherals = Peripherals::take(); let mut system = peripherals.DPORT.split(); let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); + let mut delay = Delay::new(&clocks); let timer_group0 = TimerGroup::new( peripherals.TIMG0, @@ -80,6 +87,11 @@ fn main() -> ! { let mut power = io.pins.gpio4.into_push_pull_output(); power.set_high().ok(); + for i in 0..15 { + println!("waiting {} seconds ...", 15 - i); + delay.delay_ms(1000u32); + } + let modem_uart_pins = TxRxPins::new_tx_rx( io.pins.gpio26.into_push_pull_output(), io.pins.gpio27.into_floating_input(), @@ -107,18 +119,24 @@ fn main() -> ! { static BUFFERS: Buffers = Buffers::::new(); + let digester = DefaultDigester::::default() + .with_custom_success(|buf| { + println!("digested:\n{:?}", buf); + Ok((buf, buf.len())) + }); + let (ingress, client) = BUFFERS.split( tx, - DefaultDigester::::default(), + digester, atat::Config::default() ); - interrupt::enable(Interrupt::UART1, interrupt::Priority::Priority1).unwrap(); - println!("Starting embassy executor ..."); - let cell = StaticCell::new(); - let executor: &'static mut Executor = cell.init(Executor::new()); + + static CELL: StaticCell = StaticCell::new(); + let executor = CELL.init(Executor::new()); executor.run(|spawner| { - spawner.spawn(reader(rx)).unwrap(); + let _ = spawner.spawn(ingress_task(ingress, rx)); + let _ = spawner.spawn(test_send(client)); }); } diff --git a/src/modem/mod.rs b/src/modem/mod.rs index d56876e..840bace 100644 --- a/src/modem/mod.rs +++ b/src/modem/mod.rs @@ -11,23 +11,6 @@ use heapless::String; #[derive(Clone, AtatResp)] pub struct NoResponse; -#[derive(Clone, AtatCmd)] -#[at_cmd("+CSGT", NoResponse)] -pub struct SetGreetingText<'a> { - #[at_arg(position = 0, len = 32)] - pub text: &'a str, -} - -#[derive(Clone, AtatCmd)] -#[at_cmd("AT", GreetingText)] -pub struct GetGreetingText; - -#[derive(Clone, AtatResp)] -pub struct GreetingText { - #[at_arg(position = 0)] - pub text: String<64>, -} - #[derive(Clone, AtatCmd)] #[at_cmd("+CGMI", ManufacturerIdText)] pub struct GetManufacturerId; @@ -46,12 +29,3 @@ pub enum Urc { #[at_urc("+UMWI")] MessageWaitingIndication(MessageWaitingIndication), } - -type ModemClient<'a, W> = Client<'a, W, 256>; - -pub fn setup_modem(tx: W, res_channel: &'_ ResponseChannel<256>) -> ModemClient<'_, W> -where W: Write { - let config = Config::new(); - let client: ModemClient = Client::new(tx, res_channel, config); - client -}