hp-core: add wifi connection
Signed-off-by: Thomas Klaehn <thomas.klaehn@perinet.io>
This commit is contained in:
@@ -1,45 +1,149 @@
|
||||
//! This shows a very basic example of running code on the LP core.
|
||||
//!
|
||||
//! Code on LP core increments a counter and continuously toggles LED. The
|
||||
//! current value is printed by the HP core.
|
||||
//!
|
||||
//! Make sure to first compile the `esp-lp-hal/examples/blinky.rs` example
|
||||
//!
|
||||
//! The following wiring is assumed:
|
||||
//! - LED => GPIO1
|
||||
|
||||
//% CHIPS: esp32c6
|
||||
//% FEATURES: esp-hal/unstable
|
||||
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
|
||||
extern crate alloc;
|
||||
|
||||
use blocking_network_stack::Stack;
|
||||
use esp_backtrace as _;
|
||||
use esp_hal::{
|
||||
gpio::lp_io::{LowPowerInput, LowPowerOutput},
|
||||
clock::CpuClock,
|
||||
gpio::lp_io::{
|
||||
LowPowerInput,
|
||||
LowPowerOutput},
|
||||
load_lp_code,
|
||||
lp_core::{LpCore, LpCoreWakeupSource},
|
||||
lp_core::{
|
||||
LpCore,
|
||||
LpCoreWakeupSource
|
||||
},
|
||||
main,
|
||||
rtc_cntl::{sleep::WakeFromLpCoreWakeupSource, Rtc},
|
||||
uart::{Config, lp_uart::LpUart}
|
||||
rng::Rng,
|
||||
// rtc_cntl::{
|
||||
// sleep::WakeFromLpCoreWakeupSource,
|
||||
// Rtc
|
||||
// },
|
||||
time::{
|
||||
self
|
||||
},
|
||||
timer::timg::TimerGroup,
|
||||
uart::{
|
||||
Config,
|
||||
lp_uart::LpUart
|
||||
}
|
||||
};
|
||||
use esp_println::{print, println};
|
||||
use esp_println::{
|
||||
print,
|
||||
println
|
||||
};
|
||||
use esp_wifi::{
|
||||
init,
|
||||
wifi::{
|
||||
utils::create_network_interface,
|
||||
AccessPointInfo,
|
||||
ClientConfiguration,
|
||||
Configuration,
|
||||
WifiError,
|
||||
WifiStaDevice,
|
||||
}
|
||||
};
|
||||
use smoltcp::{
|
||||
iface::{
|
||||
SocketSet,
|
||||
SocketStorage
|
||||
},
|
||||
wire::DhcpOption
|
||||
};
|
||||
|
||||
const SSID: &str = env!("SSID");
|
||||
const PASSWORD: &str = env!("PASSWORD");
|
||||
|
||||
fn as_millis() -> u64 {
|
||||
time::now().duration_since_epoch().to_millis()
|
||||
}
|
||||
|
||||
#[main]
|
||||
fn main() -> ! {
|
||||
let peripherals = esp_hal::init(esp_hal::Config::default());
|
||||
esp_println::logger::init_logger_from_env();
|
||||
let config = esp_hal::Config::default().with_cpu_clock(CpuClock::max());
|
||||
let peripherals = esp_hal::init(config);
|
||||
|
||||
esp_alloc::heap_allocator!(72 * 1024);
|
||||
let timg0 = TimerGroup::new(peripherals.TIMG0);
|
||||
let mut rng = Rng::new(peripherals.RNG);
|
||||
let init = init(timg0.timer0, rng, peripherals.RADIO_CLK).unwrap();
|
||||
let mut wifi = peripherals.WIFI;
|
||||
let (iface, device, mut controller) =
|
||||
create_network_interface(&init, &mut wifi, WifiStaDevice).unwrap();
|
||||
controller
|
||||
.set_power_saving(esp_wifi::config::PowerSaveMode::None)
|
||||
.unwrap();
|
||||
let mut socket_set_entries: [SocketStorage; 3] = Default::default();
|
||||
let mut socket_set = SocketSet::new(&mut socket_set_entries[..]);
|
||||
let mut dhcp_socket = smoltcp::socket::dhcpv4::Socket::new();
|
||||
// we can set a hostname here (or add other DHCP options)
|
||||
dhcp_socket.set_outgoing_options(&[DhcpOption {
|
||||
kind: 12,
|
||||
data: b"esp32-c6",
|
||||
}]);
|
||||
socket_set.add(dhcp_socket);
|
||||
|
||||
let stack = Stack::new(iface, device, socket_set, as_millis, rng.random());
|
||||
let client_config = Configuration::Client(ClientConfiguration {
|
||||
ssid: SSID.try_into().unwrap(),
|
||||
password: PASSWORD.try_into().unwrap(),
|
||||
..Default::default()
|
||||
});
|
||||
let res = controller.set_configuration(&client_config);
|
||||
println!("wifi_set_configuration returned {:?}", res);
|
||||
|
||||
controller.start().unwrap();
|
||||
println!("is wifi started: {:?}", controller.is_started());
|
||||
|
||||
println!("Start Wifi Scan");
|
||||
let res: Result<(heapless::Vec<AccessPointInfo, 10>, usize), WifiError> = controller.scan_n();
|
||||
if let Ok((res, _count)) = res {
|
||||
for ap in res {
|
||||
println!("{:?}", ap.ssid);
|
||||
}
|
||||
}
|
||||
|
||||
println!("{:?}", controller.capabilities());
|
||||
println!("wifi_connect {:?}", controller.connect());
|
||||
|
||||
// wait to get connected
|
||||
println!("Wait to get connected");
|
||||
loop {
|
||||
match controller.is_connected() {
|
||||
Ok(true) => break,
|
||||
Ok(false) => {}
|
||||
Err(err) => {
|
||||
println!("{:?}", err);
|
||||
// FIXME: Error handling
|
||||
loop {}
|
||||
}
|
||||
}
|
||||
}
|
||||
println!("{:?}", controller.is_connected());
|
||||
|
||||
// wait for getting an ip address
|
||||
println!("Wait to get an ip address");
|
||||
loop {
|
||||
stack.work();
|
||||
|
||||
if stack.is_iface_up() {
|
||||
println!("got ip {:?}", stack.get_ip_info());
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
let lp_pin = LowPowerOutput::new(peripherals.GPIO1);
|
||||
|
||||
let uart_cfg = Config::default();
|
||||
let tx = LowPowerOutput::new(peripherals.GPIO5);
|
||||
let rx = LowPowerInput::new(peripherals.GPIO4);
|
||||
let lp_uart = LpUart::new(peripherals.LP_UART, uart_cfg, tx, rx);
|
||||
|
||||
let mut rtc = Rtc::new(peripherals.LPWR);
|
||||
let reason = WakeFromLpCoreWakeupSource::default();
|
||||
|
||||
// let mut rtc = Rtc::new(peripherals.LPWR);
|
||||
// let reason = WakeFromLpCoreWakeupSource::default();
|
||||
let mut lp_core = LpCore::new(peripherals.LP_CORE);
|
||||
|
||||
lp_core.stop();
|
||||
println!("lp-core -> stop");
|
||||
|
||||
|
Reference in New Issue
Block a user