#![no_std] #![no_main] #![feature(type_alias_impl_trait)] mod modem; use esp_backtrace as _; use esp_println::println; use hal::{ clock::ClockControl, embassy::{self, executor::Executor}, gpio::IO, interrupt, peripherals::{Peripherals, Interrupt, UART1}, prelude::*, timer::TimerGroup, Uart, uart::{ config::{ Config, DataBits, Parity, StopBits, }, TxRxPins, }, }; use esp_hal_common::{UartRx, UartTx}; use atat::{asynch::AtatClient, AtatIngress, Buffers, DefaultDigester, Ingress}; use heapless::Vec; use static_cell::StaticCell; const READ_BUF_SIZE: usize = 64; const INGRESS_BUF_SIZE: usize = 1024; 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; } } } #[entry] fn main() -> ! { let peripherals = Peripherals::take(); let mut system = peripherals.DPORT.split(); let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); let timer_group0 = TimerGroup::new( peripherals.TIMG0, &clocks, &mut system.peripheral_clock_control, ); embassy::init(&clocks, timer_group0.timer0); let io = IO::new(peripherals.GPIO, peripherals.IO_MUX); // Power on the LilyGo A7670E module. let mut power = io.pins.gpio4.into_push_pull_output(); power.set_high().ok(); let modem_uart_pins = TxRxPins::new_tx_rx( io.pins.gpio26.into_push_pull_output(), io.pins.gpio27.into_floating_input(), ); let config = Config { baudrate: 115200, data_bits: DataBits::DataBits8, parity: Parity::ParityNone, stop_bits: StopBits::STOP1, }; // UART interface for the GSM modem let mut uart = Uart::new_with_config( peripherals.UART1, config, Some(modem_uart_pins), &clocks, &mut system.peripheral_clock_control, ); uart.set_rx_fifo_full_threshold(READ_BUF_SIZE as u16).unwrap(); let (tx, rx) = uart.split(); static BUFFERS: Buffers = Buffers::::new(); let (ingress, client) = BUFFERS.split( tx, DefaultDigester::::default(), 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()); executor.run(|spawner| { spawner.spawn(reader(rx)).unwrap(); }); }