traccer-no-std/src/main.rs

125 lines
3.3 KiB
Rust
Raw Normal View History

#![no_std]
#![no_main]
2023-09-25 02:12:22 +02:00
#![feature(type_alias_impl_trait)]
mod modem;
use esp_backtrace as _;
use esp_println::println;
use hal::{
clock::ClockControl,
2023-09-25 02:12:22 +02:00
embassy::{self, executor::Executor},
gpio::IO,
interrupt,
peripherals::{Peripherals, Interrupt, UART1},
prelude::*,
2023-09-25 02:12:22 +02:00
timer::TimerGroup,
Uart,
uart::{
config::{
Config,
2023-09-25 02:12:22 +02:00
DataBits,
Parity,
StopBits,
},
2023-09-25 02:12:22 +02:00
TxRxPins,
},
};
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;
}
}
}
#[entry]
fn main() -> ! {
let peripherals = Peripherals::take();
let mut system = peripherals.DPORT.split();
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);
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();
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
2023-09-25 02:12:22 +02:00
let mut uart = Uart::new_with_config(
peripherals.UART1,
2023-09-25 02:12:22 +02:00
config,
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-25 02:12:22 +02:00
let (tx, rx) = uart.split();
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();
});
}