2023-09-20 00:15:04 +02:00
|
|
|
#![no_std]
|
|
|
|
#![no_main]
|
2023-09-25 02:12:22 +02:00
|
|
|
#![feature(type_alias_impl_trait)]
|
|
|
|
|
|
|
|
mod modem;
|
2023-09-20 00:15:04 +02:00
|
|
|
|
|
|
|
use esp_backtrace as _;
|
|
|
|
use esp_println::println;
|
2023-09-20 00:15:43 +02:00
|
|
|
use hal::{
|
|
|
|
clock::ClockControl,
|
2023-09-25 02:12:22 +02:00
|
|
|
embassy::{self, executor::Executor},
|
|
|
|
gpio::IO,
|
|
|
|
interrupt,
|
|
|
|
peripherals::{Peripherals, Interrupt, UART1},
|
2023-09-20 00:15:43 +02:00
|
|
|
prelude::*,
|
2023-09-25 02:12:22 +02:00
|
|
|
timer::TimerGroup,
|
|
|
|
Uart,
|
2023-09-20 00:15:43 +02:00
|
|
|
uart::{
|
|
|
|
config::{
|
|
|
|
Config,
|
2023-09-25 02:12:22 +02:00
|
|
|
DataBits,
|
2023-09-20 00:15:43 +02:00
|
|
|
Parity,
|
|
|
|
StopBits,
|
|
|
|
},
|
2023-09-25 02:12:22 +02:00
|
|
|
TxRxPins,
|
|
|
|
},
|
2023-09-20 00:15:43 +02:00
|
|
|
};
|
2023-09-25 02:12:22 +02:00
|
|
|
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<u8, MAX_BUFFER_SIZE> = 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2023-09-20 00:15:04 +02:00
|
|
|
|
|
|
|
#[entry]
|
|
|
|
fn main() -> ! {
|
|
|
|
let peripherals = Peripherals::take();
|
2023-09-20 00:15:43 +02:00
|
|
|
let mut system = peripherals.DPORT.split();
|
2023-09-20 00:15:04 +02:00
|
|
|
let clocks = ClockControl::boot_defaults(system.clock_control).freeze();
|
2023-09-25 02:12:22 +02:00
|
|
|
|
|
|
|
let timer_group0 = TimerGroup::new(
|
|
|
|
peripherals.TIMG0,
|
|
|
|
&clocks,
|
|
|
|
&mut system.peripheral_clock_control,
|
|
|
|
);
|
|
|
|
|
|
|
|
embassy::init(&clocks, timer_group0.timer0);
|
2023-09-20 00:15:04 +02:00
|
|
|
|
2023-09-20 00:15:43 +02:00
|
|
|
let io = IO::new(peripherals.GPIO, peripherals.IO_MUX);
|
|
|
|
|
2023-09-25 02:12:22 +02:00
|
|
|
// Power on the LilyGo A7670E module.
|
|
|
|
let mut power = io.pins.gpio4.into_push_pull_output();
|
|
|
|
power.set_high().ok();
|
|
|
|
|
2023-09-20 00:15:43 +02:00
|
|
|
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,
|
|
|
|
};
|
2023-09-20 00:15:04 +02:00
|
|
|
|
2023-09-20 00:15:43 +02:00
|
|
|
// UART interface for the GSM modem
|
2023-09-25 02:12:22 +02:00
|
|
|
let mut uart = Uart::new_with_config(
|
2023-09-20 00:15:43 +02:00
|
|
|
peripherals.UART1,
|
2023-09-25 02:12:22 +02:00
|
|
|
config,
|
2023-09-20 00:15:43 +02:00
|
|
|
Some(modem_uart_pins),
|
|
|
|
&clocks,
|
|
|
|
&mut system.peripheral_clock_control,
|
|
|
|
);
|
2023-09-25 02:12:22 +02:00
|
|
|
uart.set_rx_fifo_full_threshold(READ_BUF_SIZE as u16).unwrap();
|
2023-09-20 00:15:43 +02:00
|
|
|
|
2023-09-25 02:12:22 +02:00
|
|
|
let (tx, rx) = uart.split();
|
2023-09-20 00:15:43 +02:00
|
|
|
|
2023-09-25 02:12:22 +02:00
|
|
|
static BUFFERS: Buffers<modem::Urc, INGRESS_BUF_SIZE, URC_CAPACITY, URC_SUBSCRIBERS> =
|
|
|
|
Buffers::<modem::Urc, INGRESS_BUF_SIZE, URC_CAPACITY, URC_SUBSCRIBERS>::new();
|
|
|
|
|
|
|
|
let (ingress, client) = BUFFERS.split(
|
|
|
|
tx,
|
|
|
|
DefaultDigester::<modem::Urc>::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();
|
|
|
|
});
|
2023-09-20 00:15:04 +02:00
|
|
|
}
|