Reading from the sensor

This commit is contained in:
Adam Macdonald 2025-02-22 01:05:38 +00:00
parent 0afda7192c
commit a93730a61d
5 changed files with 128 additions and 38 deletions

View File

@ -15,8 +15,6 @@ rustflags = [
"link-arg=--nmagic",
"-C",
"link-arg=-Tlink.x",
# "-C",
# "link-arg=-Tdefmt.x",
"-C",
"target-cpu=cortex-m33",
]
@ -24,13 +22,6 @@ rustflags = [
runner = "picotool load -u -v -x -t elf"
[target.riscv32imac-unknown-none-elf]
rustflags = [
"-C",
"link-arg=--nmagic",
"-C",
"link-arg=-Trp235x_riscv.x",
# "-C",
# "link-arg=-Tdefmt.x",
]
rustflags = ["-C", "link-arg=--nmagic", "-C", "link-arg=-Trp235x_riscv.x"]
runner = "picotool load -u -v -x -t elf"

3
.gitignore vendored
View File

@ -1,2 +1,5 @@
# Cargo
target/
# VSCode
.vscode

52
Cargo.lock generated
View File

@ -211,6 +211,12 @@ dependencies = [
"either",
]
[[package]]
name = "log"
version = "0.4.25"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "04cbf5b083de1c7e0222a7a51dbfdba1cbe1c6ab0b15e29fff3f6c077fd9cd9f"
[[package]]
name = "nb"
version = "0.1.3"
@ -246,12 +252,6 @@ dependencies = [
"syn 1.0.109",
]
[[package]]
name = "panic-halt"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a513e167849a384b7f9b746e517604398518590a9142f4846a32e3c2a4de7b11"
[[package]]
name = "paste"
version = "1.0.15"
@ -262,10 +262,12 @@ checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a"
name = "pico-enviro-sensor"
version = "0.1.0"
dependencies = [
"critical-section",
"embedded-hal 1.0.0",
"fugit",
"panic-halt",
"rp235x-hal",
"rtt-target",
"scd4x",
]
[[package]]
@ -414,6 +416,17 @@ dependencies = [
"vcell",
]
[[package]]
name = "rtt-target"
version = "0.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4235cd78091930e907d2a510adb0db1369e82668eafa338f109742fa0c83059d"
dependencies = [
"critical-section",
"portable-atomic",
"ufmt-write",
]
[[package]]
name = "rustc_version"
version = "0.2.3"
@ -423,6 +436,16 @@ dependencies = [
"semver",
]
[[package]]
name = "scd4x"
version = "0.4.0"
source = "git+https://github.com/twokilohertz/scd4x-rs.git?branch=conversion-fixes#07730523c51d6909530c000de271832125760385"
dependencies = [
"embedded-hal 1.0.0",
"log",
"sensirion-i2c",
]
[[package]]
name = "semver"
version = "0.9.0"
@ -438,6 +461,15 @@ version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3"
[[package]]
name = "sensirion-i2c"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "640a03c95c176226cf159ae2727f7fe3245546022639d61459af46d2878aec06"
dependencies = [
"embedded-hal 1.0.0",
]
[[package]]
name = "sha2-const-stable"
version = "0.1.0"
@ -472,6 +504,12 @@ dependencies = [
"unicode-ident",
]
[[package]]
name = "ufmt-write"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e87a2ed6b42ec5e28cc3b94c09982969e9227600b2e3dcbc1db927a84c06bd69"
[[package]]
name = "unicode-ident"
version = "1.0.17"

View File

@ -8,13 +8,17 @@ version = "0.1.0"
edition = "2021"
[dependencies]
embedded-hal = "1.0.0"
fugit = "0.3.7"
panic-halt = "1.0.0"
rp235x-hal = { version = "0.2.0", features = [
"critical-section-impl",
"binary-info",
] }
critical-section = "1.2.0"
embedded-hal = "1.0.0"
fugit = "0.3.7"
scd4x = { git = "https://github.com/twokilohertz/scd4x-rs.git", branch = "conversion-fixes", features = [
"scd41",
] }
rtt-target = "0.6.1"
# Super-optimised release build, maximum performance, minimal debuggability
# Build with cargo build --profile dist

View File

@ -3,17 +3,13 @@
use embedded_hal::delay::DelayNs;
use fugit::RateExtU32;
use panic_halt as _; // Needed so its built & linked correctly
// Alias for our HAL crate
use rp235x_hal as hal;
use rtt_target::{rprintln, rtt_init_print};
// RP235x HAL
use hal::uart::DataBits;
use hal::uart::StopBits;
use hal::uart::UartConfig;
use hal::uart::UartPeripheral;
use hal::Clock;
use rp235x_hal as hal;
// Sensor
use scd4x::Scd4x;
mod constants;
@ -23,6 +19,9 @@ pub static IMAGE_DEF: hal::block::ImageDef = hal::block::ImageDef::secure_exe();
#[hal::entry]
fn main() -> ! {
rtt_init_print!();
rprintln!("Logging over RTT initialised");
let mut peripherals = hal::pac::Peripherals::take().unwrap();
let mut watchdog = hal::Watchdog::new(peripherals.WATCHDOG);
@ -49,18 +48,73 @@ fn main() -> ! {
&mut peripherals.RESETS,
);
let uart0_pins = (pins.gpio0.into_function(), pins.gpio1.into_function());
rprintln!("Core RP2350 hardware initialisation successful");
let uart = UartPeripheral::new(peripherals.UART0, uart0_pins, &mut peripherals.RESETS)
.enable(
UartConfig::new(9600_u32.Hz(), DataBits::Eight, None, StopBits::One),
clocks.peripheral_clock.freq(),
)
.unwrap();
// Initialise SCD41 sensor
let i2c0 = hal::I2C::i2c0(
peripherals.I2C0,
pins.gpio4.reconfigure(), // Pin 6 on Pico 2 (SDA)
pins.gpio5.reconfigure(), // Pin 7 on Pico 2 (SCL)
400.kHz(),
&mut peripherals.RESETS,
&clocks.peripheral_clock,
);
timer.delay_ms(30); // Power-up delay
let mut scd41 = Scd4x::new(i2c0, timer);
scd41.wake_up();
match scd41.reinit() {
Ok(_) => rprintln!("Initialised SCD41"),
Err(error) => rprintln!("Failed to initialise SCD41: {:?}", error),
}
timer.delay_ms(30); // Soft reset delay
match scd41.serial_number() {
Ok(serial) => rprintln!("SCD41 serial number: {}", serial),
Err(error) => rprintln!("SCD41 did not respond to get_serial_number: {:?}", error),
}
match scd41.self_test_is_ok() {
Ok(ok) => {
if ok {
rprintln!("SCD41 reported successful self-test")
} else {
rprintln!("SCD41 reported unsuccessful self-test!")
}
}
Err(_) => rprintln!("SCD41 failed to perform self-test"),
}
match scd41.start_periodic_measurement() {
Ok(_) => rprintln!("Configured sensor to measure every 5 seconds"),
Err(error) => rprintln!("SCD41 start_periodic_measurement() failed: {:?}", error),
}
loop {
uart.write_full_blocking(b"hello, world!\r\n");
timer.delay_ms(500);
timer.delay_ms(5010);
match scd41.measurement() {
Ok(data) => rprintln!(
"CO2: {}, temperature: {}, humidity: {}",
data.co2,
data.temperature,
data.humidity
),
Err(error) => rprintln!("SCD41 get_measurement() failed: {:?}", error),
}
}
// loop {
// hal::arch::wfi();
// }
}
#[inline(never)]
#[panic_handler]
fn panic(info: &core::panic::PanicInfo) -> ! {
rprintln!("Panicked! {}", info);
loop {
hal::arch::nop()
}
}