Compare commits

..

11 Commits

Author SHA1 Message Date
Thomas Klaehn
26a6ef8d50 Cargo.toml: remove unnecessary dependencies
Signed-off-by: Thomas Klaehn <thomas.klaehn@perinet.io>
2025-02-26 08:11:47 +01:00
Thomas Klaehn
27294df811 hp-core: use mqtt lib
Signed-off-by: Thomas Klaehn <thomas.klaehn@perinet.io>
2025-02-26 08:10:10 +01:00
Thomas Klaehn
ec58e9e268 Add mqtt lib
Signed-off-by: Thomas Klaehn <thomas.klaehn@perinet.io>
2025-02-26 08:09:15 +01:00
Thomas Klaehn
feaf167e85 Add mqtt publish
Signed-off-by: Thomas Klaehn <thomas.klaehn@perinet.io>
2025-02-25 00:47:44 +01:00
Thomas Klaehn
b87e6c552f hp-core: add wifi connection
Signed-off-by: Thomas Klaehn <thomas.klaehn@perinet.io>
2025-02-18 09:36:45 +01:00
Thomas Klaehn
14fe370083 Devcontainer: Add second HW connection
Signed-off-by: Thomas Klaehn <thomas.klaehn@perinet.io>
2025-02-18 09:35:11 +01:00
Thomas Klaehn
38830d9fe4 Add low power print to lp-core
Signed-off-by: Thomas Klaehn <thomas.klaehn@perinet.io>
2025-02-14 07:33:55 +01:00
Thomas Klaehn
18610940f4 hp-core: Sleep after action
Signed-off-by: Thomas Klaehn <thomas.klaehn@perinet.io>
2025-02-12 09:05:57 +01:00
Thomas Klaehn
0d40a0e884 lp-core: Add waking hp-core from sleep
Signed-off-by: Thomas Klaehn <thomas.klaehn@perinet.io>
2025-02-12 09:02:25 +01:00
Thomas Klaehn
ab2f440b82 vscode: Fix task executions
Signed-off-by: Thomas Klaehn <thomas.klaehn@perinet.io>
2025-02-12 08:52:34 +01:00
Thomas Klaehn
15d240ef20 Fix: Move profile property to root Cargo.toml
Signed-off-by: Thomas Klaehn <thomas.klaehn@perinet.io>
2025-02-12 08:51:26 +01:00
11 changed files with 372 additions and 1041 deletions

View File

@ -7,5 +7,10 @@ target = "riscv32imac-unknown-none-elf"
[target.'cfg(target_arch = "riscv32")'] [target.'cfg(target_arch = "riscv32")']
runner = "espflash flash --monitor" runner = "espflash flash --monitor"
[env]
ESP_LOG = "info"
SSID = "tkl"
PASSWORD = "hosenmuchte"
[unstable] [unstable]
build-std = ["core"] build-std = ["alloc", "core"]

3
.devcontainer/devcontainer.json Executable file → Normal file
View File

@ -13,7 +13,8 @@
"source=/dev/bus/usb/,target=/dev/bus/usb/,type=bind" "source=/dev/bus/usb/,target=/dev/bus/usb/,type=bind"
], ],
"runArgs": [ "runArgs": [
"--device=/dev/ttyACM0" "--device=/dev/ttyACM0",
"--device=/dev/ttyACM1"
], ],
"customizations": { "customizations": {
"vscode": { "vscode": {

35
.vscode/tasks.json vendored
View File

@ -15,7 +15,26 @@
"group": { "group": {
"kind": "build" "kind": "build"
}, },
"label": "rust: cargo build lp-core" "label": "build lp-core"
},
{
"type": "cargo",
"command": "build",
"args": [
"-p",
"hp-core",
"--release"
],
"dependsOn": [
"build lp-core"
],
"problemMatcher": [
"$rustc"
],
"group": {
"kind": "build"
},
"label": "build hp-core"
}, },
{ {
"type": "cargo", "type": "cargo",
@ -24,7 +43,7 @@
"$rustc" "$rustc"
], ],
"group": "build", "group": "build",
"label": "rust: cargo check" "label": "check"
}, },
{ {
"type": "cargo", "type": "cargo",
@ -33,16 +52,24 @@
"$rustc" "$rustc"
], ],
"group": "build", "group": "build",
"label": "rust: cargo clean" "label": "clean"
}, },
{ {
"type": "cargo", "type": "cargo",
"command": "run", "command": "run",
"args": [
"-p",
"hp-core",
"--release"
],
"dependsOn": [
"build hp-core"
],
"problemMatcher": [ "problemMatcher": [
"$rustc" "$rustc"
], ],
"group": "build", "group": "build",
"label": "rust: cargo run" "label": "run"
} }
] ]
} }

949
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -1,5 +1,14 @@
cargo-features = ["profile-rustflags"] cargo-features = ["profile-rustflags"]
[workspace] [workspace]
members = ["hp-core", "lp-core"] members = ["hp-core", "lib/mqtt", "lp-core"]
resolver = "2" resolver = "2"
[profile.release]
codegen-units = 1
debug = 2
debug-assertions = true
incremental = false
opt-level = 3
lto = 'fat'
overflow-checks = false

View File

@ -1,79 +1,22 @@
[package] [package]
name = "hp-core" name = "hp-core"
version = "0.1.0" version = "0.1.0"
edition = "2021" edition = "2024"
license = "MIT OR Apache-2.0" license = "MIT OR Apache-2.0"
publish = false publish = false
[dependencies] [dependencies]
aligned = { version = "0.4.2", optional = true }
bleps = { git = "https://github.com/bjoernQ/bleps", package = "bleps", rev = "a5148d8ae679e021b78f53fd33afb8bb35d0b62e", features = [ "macros", "async"] }
blocking-network-stack = { git = "https://github.com/bjoernQ/blocking-network-stack.git", rev = "b3ecefc222d8806edd221f266999ca339c52d34e" } blocking-network-stack = { git = "https://github.com/bjoernQ/blocking-network-stack.git", rev = "b3ecefc222d8806edd221f266999ca339c52d34e" }
bt-hci = "0.2.0"
cfg-if = "1.0.0"
critical-section = "1.1.3"
embassy-executor = { version = "0.7.0", features = ["task-arena-size-20480"] }
embassy-futures = "0.1.1"
embassy-net = { version = "0.6.0", features = [ "tcp", "udp", "dhcpv4", "medium-ethernet"] }
embassy-sync = "0.6.0"
embassy-time = "0.4.0"
embassy-usb = { version = "0.2.0", default-features = false }
embedded-can = "0.4.1"
embedded-hal-async = "1.0.0"
embedded-io = { version = "0.6.1", default-features = false }
embedded-io-async = "0.6.1"
embedded-storage = "0.3.1"
esp-alloc = "0.6.0" esp-alloc = "0.6.0"
esp-backtrace = { version = "0.15.0", features = ["esp32c6", "exception-handler", "panic-handler", "println"] } esp-backtrace = { version = "0.15.0", features = ["esp32c6", "exception-handler", "panic-handler", "println"] }
esp-hal = { version = "0.23.1", features = ["log"] } esp-hal = { version = "0.23.1", features = ["esp32c6", "log"] }
esp-hal-embassy = { version = "0.6.0", optional = true }
esp-ieee802154 = { version = "0.5.0", optional = true }
esp-println = { version = "0.13.0", features = ["log"] } esp-println = { version = "0.13.0", features = ["log"] }
esp-storage = { version = "0.4.0", optional = true } esp-wifi = { version = "0.12.0", features = ["log", "utils", "wifi"] }
esp-wifi = { version = "0.12.0", features = ["log"], optional = true }
heapless = "0.8.0" heapless = "0.8.0"
hmac = { version = "0.12.1", default-features = false } mqtt = { path = "../lib/mqtt"}
ieee80211 = { version = "0.4.0", default-features = false }
ieee802154 = "0.6.1"
log = "0.4.22"
nb = "1.1.0"
portable-atomic = { version = "1.9.0", default-features = false }
sha2 = { version = "0.10.8", default-features = false }
smoltcp = { version = "0.12.0", default-features = false, features = [ "medium-ethernet", "socket-raw"] } smoltcp = { version = "0.12.0", default-features = false, features = [ "medium-ethernet", "socket-raw"] }
embedded-time = "=0.12.1"
static_cell = { version = "2.1.0", features = ["nightly"] }
usb-device = "0.3.2"
usbd-serial = "0.2.2"
edge-dhcp = { version = "0.5.0" }
edge-raw = { version = "0.5.0" }
edge-nal = { version = "0.5.0" }
edge-nal-embassy = { version = "0.5.0" }
[features] [features]
default=["esp32c6"] default=["esp32c6"]
esp32 = ["esp-hal/esp32", "esp-backtrace/esp32", "esp-hal-embassy?/esp32", "esp-println/esp32", "esp-storage?/esp32", "esp-wifi?/esp32"] esp32c6 = ["esp-hal/esp32c6", "esp-backtrace/esp32c6", "esp-println/esp32c6", "esp-wifi/esp32c6"]
esp32c2 = ["esp-hal/esp32c2", "esp-backtrace/esp32c2", "esp-hal-embassy?/esp32c2", "esp-println/esp32c2", "esp-storage?/esp32c2", "esp-wifi?/esp32c2", ]
esp32c3 = ["esp-hal/esp32c3", "esp-backtrace/esp32c3", "esp-hal-embassy?/esp32c3", "esp-println/esp32c3", "esp-storage?/esp32c3", "esp-wifi?/esp32c3"]
esp32c6 = ["esp-hal/esp32c6", "esp-backtrace/esp32c6", "esp-hal-embassy?/esp32c6", "esp-println/esp32c6", "esp-storage?/esp32c6", "esp-wifi?/esp32c6", "esp-ieee802154?/esp32c6"]
esp32h2 = ["esp-hal/esp32h2", "esp-backtrace/esp32h2", "esp-hal-embassy?/esp32h2", "esp-println/esp32h2", "esp-storage?/esp32h2", "esp-wifi?/esp32h2", "esp-ieee802154?/esp32h2"]
esp32s2 = ["esp-hal/esp32s2", "esp-backtrace/esp32s2", "esp-hal-embassy?/esp32s2", "esp-println/esp32s2", "esp-storage?/esp32s2", "esp-wifi?/esp32s2"]
esp32s3 = ["esp-hal/esp32s3", "esp-backtrace/esp32s3", "esp-hal-embassy?/esp32s3", "esp-println/esp32s3", "esp-storage?/esp32s3", "esp-wifi?/esp32s3"]
esp-wifi = ["dep:esp-wifi"]
embassy = ["dep:esp-hal-embassy"]
[profile.release]
codegen-units = 1
debug = 2
debug-assertions = true
incremental = false
opt-level = 3
lto = 'fat'
overflow-checks = false
[build]
rustflags = [
"-C", "force-frame-pointers",
]

View File

@ -1,52 +1,205 @@
//! 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_std]
#![no_main] #![no_main]
extern crate alloc;
use blocking_network_stack::Stack;
use esp_backtrace as _; use esp_backtrace as _;
use esp_hal::{ use esp_hal::{
gpio::lp_io::LowPowerOutput, clock::CpuClock,
gpio::lp_io::{
LowPowerInput,
LowPowerOutput},
load_lp_code, load_lp_code,
lp_core::{LpCore, LpCoreWakeupSource}, lp_core::{
LpCore,
LpCoreWakeupSource
},
main, main,
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 mqtt::Mqtt;
use smoltcp::{
iface::{
SocketSet,
SocketStorage
},
wire::{
DhcpOption,
Ipv4Address
}
};
const SSID: &str = env!("SSID");
const PASSWORD: &str = env!("PASSWORD");
fn as_millis() -> u64 {
time::now().duration_since_epoch().to_millis()
}
#[main] #[main]
fn 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);
// configure GPIO 1 as LP output pin 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 mut rx_buffer = [0u8; 1536];
let mut tx_buffer = [0u8; 1536];
let socket = stack.get_socket(&mut rx_buffer, &mut tx_buffer);
let address = Ipv4Address::new(192, 168, 178, 143);
let port: u16 = 1883;
let mut mqtt = Mqtt::new(socket, address, port);
match mqtt.open() {
Ok(()) => {},
Err(err) => panic!("unable to open mqtt connection: {:?}", err),
}
match mqtt.connect("stm32-c6") {
Ok(_size) => {},
Err(err) => panic!("unable to connect to mqtt broker: {:?}", err),
}
match mqtt.publish("testtopic", b"testpayload") {
Ok(_size) => {},
Err(err) => panic!("unable to publish to mqtt broker: {:?}", err),
}
mqtt.disconnect();
let lp_pin = LowPowerOutput::new(peripherals.GPIO1); 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 lp_core = LpCore::new(peripherals.LP_CORE); let mut lp_core = LpCore::new(peripherals.LP_CORE);
lp_core.stop(); lp_core.stop();
println!("lp core stopped"); println!("lp-core -> stop");
// load code to LP core // load code to LP core
let lp_core_code = let lp_core_code =
load_lp_code!("./target/riscv32imac-unknown-none-elf/release/lp-core"); load_lp_code!("./target/riscv32imac-unknown-none-elf/release/lp-core");
// start LP core // start LP core
lp_core_code.run(&mut lp_core, LpCoreWakeupSource::HpCpu, lp_pin); lp_core_code.run(&mut lp_core, LpCoreWakeupSource::HpCpu, lp_uart, lp_pin);
println!("lpcore run"); println!("lpcore -> run");
let mut last: u32 = 0;
let data = (0x5000_2000) as *mut u32; let data = (0x5000_2000) as *mut u32;
loop { loop {
print!("Current {:x} \u{000d}", unsafe { let read = unsafe {
data.read_volatile() data.read_volatile()
}); };
if last != read {
print!("Current {:x} \u{000d}", read);
last = read;
}
// FIXME...
// println!("hp-core -> deep sleep");
// rtc.sleep_light(&[&reason]);
}
}
pub fn sleep_millis(delay: u32) {
let sleep_end = as_millis() + delay as u64;
while as_millis() < sleep_end {
// wait
} }
} }

17
lib/mqtt/Cargo.toml Normal file
View File

@ -0,0 +1,17 @@
[package]
name = "mqtt"
version = "0.1.0"
edition = "2024"
license = "MIT OR Apache-2.0"
publish = false
[dependencies]
blocking-network-stack = { git = "https://github.com/bjoernQ/blocking-network-stack.git", rev = "b3ecefc222d8806edd221f266999ca339c52d34e" }
embedded-io = { version = "0.6.1", default-features = false }
mqttrust = "0.6.0"
smoltcp = { version = "0.12.0", default-features = false, features = [ "medium-ethernet", "socket-raw"] }
[features]
ipv4 = ["smoltcp/proto-ipv4"]
tcp = ["ipv4", "smoltcp/socket-tcp"]

84
lib/mqtt/src/lib.rs Normal file
View File

@ -0,0 +1,84 @@
#![no_std]
use blocking_network_stack::IoError;
use embedded_io::{
Read,
Write
};
use mqttrust::encoding::v4::{
encode_slice,
Connect,
Packet,
Protocol,
Publish,
QoS
};
use smoltcp::wire::{
IpAddress,
Ipv4Address
};
pub struct Mqtt <'a, 'n, D: smoltcp::phy::Device> {
socket: blocking_network_stack::Socket<'a, 'n, D>,
address: Ipv4Address,
port: u16
}
impl <'n, 'a: 'n, D: smoltcp::phy::Device> Mqtt<'a, 'a, D> {
pub fn new(
socket: blocking_network_stack::Socket<'a, 'n, D>,
address: Ipv4Address,
port: u16
) -> Self {
Self {
socket,
address,
port
}
}
pub fn open(&mut self) -> Result<(), IoError> {
self.socket.open(IpAddress::Ipv4(self.address), self.port)
}
pub fn connect(&mut self, client_id: &str) -> Result<usize, IoError>{
let connect = Packet::Connect(Connect {
protocol: Protocol::MQTT311,
keep_alive: 10,
client_id,
clean_session: true,
last_will: None,
username: None,
password: None,
});
let mut buf = [0u8; 1024];
let len = encode_slice(&connect, &mut buf).unwrap();
self.socket.write(&buf[..len])
}
pub fn publish(&mut self, topic: &str, payload: &[u8]) -> Result<usize, IoError> {
let packet = Packet::Publish(Publish {
dup: false,
qos: QoS::AtMostOnce,
pid: None,
retain: false,
topic_name: topic,
payload,
});
let mut buf = [0u8; 1024];
let len = encode_slice(&packet, &mut buf).unwrap();
let written = match self.socket.write(&buf[..len]) {
Ok(value) => value,
Err(err) => return core::prelude::v1::Err(err),
};
let _ = match self.socket.read(&mut buf) {
Ok(value) => value,
Err(err) => return core::prelude::v1::Err(err),
};
Ok(written)
}
pub fn disconnect(&mut self) {
self.socket.disconnect();
}
}

View File

@ -1,24 +1,15 @@
[package] [package]
name = "lp-core" name = "lp-core"
version = "0.1.0" version = "0.1.0"
edition = "2021" edition = "2024"
rust-version = "1.84.0"
description = "Firmware for esp32-c6's low power core"
keywords = ["embedded", "embedded-hal", "esp32", "espressif", "hal"]
categories = ["embedded", "hardware-support", "no-std"]
license = "MIT OR Apache-2.0" license = "MIT OR Apache-2.0"
publish = false
[dependencies] [dependencies]
cfg-if = "1.0.0"
document-features = "0.2.10"
esp-lp-hal = { version = "0.1.0", features = ["esp32c6"] } esp-lp-hal = { version = "0.1.0", features = ["esp32c6"] }
embedded-hal = { version = "1.0.0" } embedded-hal = { version = "1.0.0" }
embedded-hal-nb = { version = "1.0.0", optional = true }
embedded-io = { version = "0.6.1", optional = true }
esp32c6-lp = { version = "0.3.0", features = ["critical-section"], optional = true } esp32c6-lp = { version = "0.3.0", features = ["critical-section"], optional = true }
nb = { version = "1.1.0", optional = true }
procmacros = { version = "0.16.0", package = "esp-hal-procmacros" } procmacros = { version = "0.16.0", package = "esp-hal-procmacros" }
riscv = { version = "0.11.1", features = ["critical-section-single-hart"] }
panic-halt = "0.2.0" panic-halt = "0.2.0"
[build-dependencies] [build-dependencies]
@ -27,19 +18,4 @@ esp-build = { version = "0.2.0" }
[features] [features]
default = ["esp32c6"] default = ["esp32c6"]
## Enable debug features in the HAL (used for development). esp32c6 = ["dep:esp32c6-lp", "procmacros/is-lp-core"]
debug = [
"esp32c6-lp?/impl-register-debug",
]
# Chip Support Feature Flags
# Target the ESP32-C6.
esp32c6 = ["dep:esp32c6-lp", "procmacros/is-lp-core", "dep:nb"]
# embedded-hal = ["dep:embedded-hal"]
#! ### Trait Implementation Feature Flags
## Implement the traits defined in the `1.0.0` releases of `embedded-hal` and
## `embedded-hal-nb` for the relevant peripherals.
# embedded-hal = ["dep:embedded-hal", "dep:embedded-hal-nb"]
## Implement the traits defined in `embedded-io` for the relevant peripherals.
# embedded-io = ["dep:embedded-io"]

View File

@ -7,15 +7,17 @@
#![no_std] #![no_std]
#![no_main] #![no_main]
use core::fmt::Write;
use embedded_hal::{delay::DelayNs, digital::OutputPin}; use embedded_hal::{delay::DelayNs, digital::OutputPin};
use esp_lp_hal::{delay::Delay, gpio::Output, prelude::*}; use esp_lp_hal::{delay::Delay, uart::LpUart, gpio::Output, prelude::*, wake_hp_core};
use panic_halt as _; use panic_halt as _;
const ADDRESS: u32 = 0x5000_2000; const ADDRESS: u32 = 0x5000_2000;
#[entry] #[entry]
fn main(mut gpio1: Output<1>) -> ! { fn main(mut lp_uart :LpUart, mut gpio1: Output<1>) -> ! {
let mut i: u32 = 0; let mut i: u32 = 0;
let ptr = ADDRESS as *mut u32; let ptr = ADDRESS as *mut u32;
@ -26,9 +28,12 @@ fn main(mut gpio1: Output<1>) -> ! {
ptr.write_volatile(i); ptr.write_volatile(i);
} }
writeln!(lp_uart, "high\n").unwrap();
gpio1.set_high().unwrap(); gpio1.set_high().unwrap();
wake_hp_core();
Delay.delay_ms(500); Delay.delay_ms(500);
writeln!(lp_uart, "low\n").unwrap();
gpio1.set_low().unwrap(); gpio1.set_low().unwrap();
Delay.delay_ms(500); Delay.delay_ms(500);
} }