#![no_std] #![no_main] use defmt::*; use embassy_executor::Spawner; use embassy_net::StackResources; use embassy_net_ppp; use embassy_stm32::i2c::{self, I2c}; use embassy_stm32::rng::{self, Rng}; use embassy_stm32::time::Hertz; use embassy_stm32::usart::Config; use embassy_stm32::{bind_interrupts, peripherals, usart}; use embassy_stm32::{ gpio::{Level, Output, Speed}, usart::BufferedUart, }; use embassy_time::{Duration, Timer}; use net::net_init; use rand::RngCore; use static_cell::StaticCell; use {defmt_rtt as _, panic_probe as _}; mod gnss; mod mqtt; mod net; mod sensors; bind_interrupts!(struct Irqs { USART2 => usart::BufferedInterruptHandler; USART3 => usart::InterruptHandler; RNG => rng::InterruptHandler; I2C1_EV => i2c::EventInterruptHandler; I2C1_ER => i2c::ErrorInterruptHandler; }); #[embassy_executor::main] async fn main(spawner: Spawner) { let mut stm_config = embassy_stm32::Config::default(); { use embassy_stm32::rcc::*; stm_config.rcc.sys = Sysclk::PLL1_R; stm_config.rcc.hsi = false; stm_config.rcc.hse = Some(Hse { freq: embassy_stm32::time::Hertz(12_000_000), mode: HseMode::Oscillator, }); stm_config.rcc.pll = Some(Pll { source: PllSource::HSE, // 12 MHz prediv: PllPreDiv::DIV1, mul: PllMul::MUL8, // 12 * 8 = 96 MHz divp: None, divq: None, divr: Some(PllRDiv::DIV2), // 96 / 2 = 48 MHz }); stm_config.rcc.ls = LsConfig::default(); } let p = embassy_stm32::init(stm_config); let mut config = Config::default(); config.baudrate = 115_200; config.stop_bits = embassy_stm32::usart::StopBits::STOP1; defmt::info!("Moin"); let mut rng = Rng::new(p.RNG, Irqs); let mut net_key = Output::new(p.PA1, Level::Low, Speed::Low); let mut pwr_en = Output::new(p.PB2, Level::Low, Speed::Low); let mut pwr_tmp = Output::new(p.PB5, Level::Low, Speed::Low); // Power on modem pwr_en.set_high(); pwr_tmp.set_high(); static RX_BUF: StaticCell<[u8; 512]> = StaticCell::new(); static TX_BUF: StaticCell<[u8; 512]> = StaticCell::new(); let mut usart_modem = BufferedUart::new( p.USART2, Irqs, p.PA3, p.PA2, RX_BUF.init([0; 512]), TX_BUF.init([0; 512]), config, ) .unwrap(); // TODO untested let gnss_uart = usart::Uart::new( p.USART3, p.PB11, p.PB10, Irqs, p.DMA1_CH2, p.DMA1_CH3, Config::default(), ) .unwrap(); let i2c = I2c::new( p.I2C1, p.PB6, p.PB7, Irqs, p.DMA1_CH6, p.DMA1_CH7, Hertz(100_000), Default::default(), ); net_init(&mut net_key, &mut usart_modem).await; // spawner.spawn(gnss::gnss_task(gnss_uart)).unwrap(); static STATE: StaticCell> = StaticCell::new(); let state = STATE.init(embassy_net_ppp::State::<8, 8>::new()); let (device, runner) = embassy_net_ppp::new(state); // Generate random seed let seed = rng.next_u64(); // Init network stack static RESOURCES: StaticCell> = StaticCell::new(); let (stack, runner_net) = embassy_net::new( device, embassy_net::Config::default(), RESOURCES.init(StackResources::new()), seed, ); // Launch network task spawner.spawn(net::net_task(runner_net)).unwrap(); spawner .spawn(net::ppp_task(stack, runner, usart_modem)) .unwrap(); loop { Timer::after(Duration::from_secs(1)).await; if stack.is_link_up() { info!("Ready"); break; } } spawner.spawn(sensors::sensors_task(i2c)).unwrap(); spawner.spawn(mqtt::mqtt_task(stack)).unwrap(); loop { Timer::after(Duration::from_secs(1)).await; } }