Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Log using defmt #103

Merged
merged 25 commits into from
Sep 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"rust-analyzer.check.allTargets": false
"rust-analyzer.check.allTargets": false,
"rust-analyzer.showUnlinkedFileNotification": false
}
64 changes: 52 additions & 12 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ embedded-hal-async = "1.0.0-rc.1"
embedded-hal-bus = { version = "0.1.0-rc.1", features = ["async"] }
embedded-io = { version = "0.4", features = ["async"] }
embedded-menu = "0.4.0"
embedded-svc = { version = "0.25", default-features = false, features = [] }
embedded-svc = { version = "0.25", default-features = false }
embassy-net = { version = "0.1.0", features = [
"nightly",
"tcp",
Expand All @@ -32,11 +32,14 @@ log = { version = "0.4.18", default-features = false, features = [
"max_level_info",
"release_max_level_info",
] }
logger = { path = "logger" }
signal-processing = { path = "signal-processing" }
norfs = { git = "https://github.com/card-io-ecg/norfs.git", features = [
"esp32s3",
] }
object-chain = "0.1.3"
bad-server = { path = "bad-server" }
defmt = { version = "=0.3.5" }

[dependencies]
embassy-futures = { version = "0.1.0" }
Expand All @@ -58,20 +61,19 @@ esp32s3-hal = { version = "0.12.0", optional = true, features = [
esp-backtrace = { version = "0.8.0", features = [
"panic-handler",
"exception-handler",
] } # add "halt-cores"
] }
esp-println = { version = "0.6.0", default-features = false, features = [
"critical-section",
"colors",
"log",
] }
esp-alloc = { version = "0.3.0" }
esp-wifi = { git = "https://github.com/esp-rs/esp-wifi.git", rev = "02239f9", features = [
esp-wifi = { git = "https://github.com/esp-rs/esp-wifi.git", rev = "15e5a55", default-features = false, features = [
"phy-enable-usb",
"ps-min-modem",
"wifi",
"embedded-svc",
"async",
"embassy-net",
"utils"
] }

display-interface = { git = "https://github.com/therealprof/display-interface.git", branch = "master" }
Expand All @@ -90,15 +92,16 @@ critical-section = "1.1"
device-descriptor = { path = "device-descriptor" }
register-access = { path = "register-access" }
gui = { path = "gui" }
ads129x = { path = "ads129x" }
macros = { path = "macros" }
embassy-alloc-taskpool = { path = "embassy-alloc-taskpool" }
ads129x = { path = "ads129x" }
max17055 = { path = "max17055", optional = true }
signal-processing = { path = "signal-processing", features = ["nostd"] }
signal-processing = { workspace = true, features = ["nostd"] }
replace_with = { version = "0.1", default-features = false, features = [
"nightly",
] }
static_cell = { version = "1.2.0", features = ["nightly"] }
bad-server = { path = "bad-server", features = ["embassy"] }

embedded-graphics = { workspace = true }
embedded-hal = { workspace = true }
Expand All @@ -112,14 +115,14 @@ embassy-sync = { workspace = true }

norfs = { workspace = true }
heapless = { workspace = true }
log = { workspace = true }
object-chain = { workspace = true }

[patch.'https://github.com/esp-rs/esp-wifi']
esp-wifi = { git = "https://github.com/bugadani/esp-wifi.git", branch = "dev" }
logger = { workspace = true }
log = { workspace = true, optional = true }
defmt = { workspace = true, optional = true }

[features]
default = ["esp32s3"]
default = ["esp32s3", "defmt"]

battery_adc = []
battery_max17055 = ["dep:max17055"]
Expand All @@ -133,14 +136,51 @@ esp32s3 = [
"dep:esp32s3-hal",
"esp-backtrace/esp32s3",
"esp-println/esp32s3",
"jtag_serial",
"uart", # print via uart, serial_jtag seems to lose bytes
"esp-wifi/esp32s3",
]

# Print options
uart = ["esp-backtrace/print-uart", "esp-println/uart"]
jtag_serial = ["esp-backtrace/print-jtag-serial", "esp-println/jtag_serial"]
rtt = ["esp-backtrace/print-rtt", "esp-println/rtt"]

log = [
"dep:log",
"norfs/log",
"esp-println/log",
"esp-println/colors",
"esp-wifi/log",
"esp32s3-hal/log",

"logger/log",
"config-site/log",
"ads129x/log",
"max17055?/log",
"bad-server/log",
"gui/log",
"signal-processing/log",
]

defmt = [
"dep:defmt",
"embassy-net/defmt",
"norfs/defmt",
"embedded-io/defmt",
"embedded-svc/defmt",
"esp-println/defmt",
"esp-wifi/defmt",
"esp32s3-hal/defmt",

"logger/defmt",
"config-site/defmt",
"ads129x/defmt",
"max17055?/defmt",
"bad-server/defmt",
"gui/defmt",
"signal-processing/defmt",
]

[package.metadata.espflash]
partition_table = "partitions.csv"
format = "esp-bootloader"
Expand Down
9 changes: 8 additions & 1 deletion ads129x/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,12 @@ byteorder = { version = "1.4.3", default-features = false }
device-descriptor = { path = "../device-descriptor" }
embedded-hal = { workspace = true }
embedded-hal-async = { workspace = true }
log = { workspace = true }
register-access = { path = "../register-access" }
logger = { workspace = true }
log = { workspace = true, optional = true }
defmt = { workspace = true, optional = true }

[features]
default = []
defmt = ["dep:defmt", "logger/defmt", "device-descriptor/defmt"]
log = ["dep:log", "logger/log"]
52 changes: 28 additions & 24 deletions ads129x/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
#![feature(async_fn_in_trait)]
#![allow(incomplete_features)]

#[macro_use]
extern crate logger;

use byteorder::{BigEndian, ByteOrder};
use device_descriptor::{ReadOnlyRegister, ReaderProxy, Register};
use embedded_hal::{
Expand All @@ -16,14 +19,16 @@ use crate::descriptors::*;
pub mod descriptors;

#[derive(Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum Error<SpiE> {
InvalidState,
UnexpectedDeviceId,
Verification,
Transfer(SpiE),
}

#[derive(Copy, Clone, Debug, Default)]
#[derive(Copy, Clone, Default, Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct ConfigRegisters {
pub config1: Config1,
pub config2: Config2,
Expand Down Expand Up @@ -99,10 +104,9 @@ impl ConfigRegisters {
if config_bytes == readback {
Ok(())
} else {
log::warn!(
warn!(
"Verification failed: received: {:?}, expected: {:?}",
readback,
config_bytes
readback, config_bytes
);
Err(Error::Verification)
}
Expand Down Expand Up @@ -263,11 +267,11 @@ where
where
RESET: OutputPin,
{
reset.set_high().unwrap();
unwrap!(reset.set_high().ok());
delay.delay_ms(Self::MIN_T_POR);
reset.set_low().unwrap();
unwrap!(reset.set_low().ok());
delay.delay_ms(Self::MIN_T_RST);
reset.set_high().unwrap();
unwrap!(reset.set_high().ok());
delay.delay_ms(Self::MIN_RST_WAIT);
}
}
Expand Down Expand Up @@ -295,9 +299,9 @@ where
.await
.map_err(Error::Transfer)?;

Ok(AdsData::new_single_channel(
buffer[bytes..bytes + 6].try_into().unwrap(),
))
Ok(AdsData::new_single_channel(unwrap!(buffer
[bytes..bytes + 6]
.try_into())))
}

pub async fn read_data_2ch_async_rdatac(&mut self) -> Result<AdsData, Error<SPI::Error>> {
Expand All @@ -319,7 +323,7 @@ where
.await
.map_err(Error::Transfer)?;

Ok(AdsData::new(buffer[bytes..bytes + 9].try_into().unwrap()))
Ok(AdsData::new(unwrap!(buffer[bytes..bytes + 9].try_into())))
}

pub async fn write_command_async(
Expand All @@ -343,10 +347,7 @@ where
match read_result.read() {
Some(id) => Ok(id),
None => {
log::warn!(
"Read unknown device id: {:?}",
read_result.read_field_bits()
);
warn!("Read unknown device id: {}", read_result.read_field_bits());
Err(Error::UnexpectedDeviceId)
}
}
Expand All @@ -367,18 +368,19 @@ where
where
RESET: OutputPin,
{
reset.set_high().unwrap();
unwrap!(reset.set_high().ok());
delay.delay_ms(Self::MIN_T_POR).await;
reset.set_low().unwrap();
unwrap!(reset.set_low().ok());
delay.delay_ms(Self::MIN_T_RST).await;
reset.set_high().unwrap();
unwrap!(reset.set_high().ok());
delay.delay_ms(Self::MIN_RST_WAIT).await;

self.write_command_async(Command::SDATAC, &mut []).await
}
}

#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct Sample {
sample: i32,
}
Expand All @@ -395,6 +397,8 @@ impl Sample {
}
}

#[derive(Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct AdsData {
status: LoffStat,
ch1: Sample,
Expand All @@ -414,16 +418,16 @@ impl AdsData {

pub fn new(buffer: [u8; 9]) -> Self {
Self {
status: Self::read_status(buffer[0..3].try_into().unwrap()),
ch1: Self::read_channel(buffer[3..6].try_into().unwrap()),
ch2: Self::read_channel(buffer[6..9].try_into().unwrap()),
status: Self::read_status(unwrap!(buffer[0..3].try_into())),
ch1: Self::read_channel(unwrap!(buffer[3..6].try_into())),
ch2: Self::read_channel(unwrap!(buffer[6..9].try_into())),
}
}

pub fn new_single_channel(buffer: [u8; 6]) -> Self {
Self {
status: Self::read_status(buffer[0..3].try_into().unwrap()),
ch1: Self::read_channel(buffer[3..6].try_into().unwrap()),
status: Self::read_status(unwrap!(buffer[0..3].try_into())),
ch1: Self::read_channel(unwrap!(buffer[3..6].try_into())),
ch2: Sample { sample: 0 },
}
}
Expand Down
8 changes: 6 additions & 2 deletions bad-server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ edition = "2021"

[dependencies]
async-io = { version = "1", optional = true }
defmt = { workspace = true, optional = true }
embassy-net = { workspace = true, optional = true }
embedded-io = { workspace = true }
embedded-svc = { workspace = true }
heapless = { workspace = true }
httparse = { version = "1.8", default-features = false }
log = { workspace = true }
logger = { workspace = true }
log = { workspace = true, optional = true }
smol = { version = "1", optional = true }
object-chain = { workspace = true }
const-fnv1a-hash = "1.1"
Expand All @@ -25,7 +27,9 @@ simple_logger = "4.1"
default = []
std = ["async-io", "smol"]
embassy = ["embassy-net"]
log = ["dep:log", "logger/log"]
defmt = ["dep:defmt", "logger/defmt"]

[[example]]
name = "simple"
required-features = ["std"]
required-features = ["std", "log"]
8 changes: 6 additions & 2 deletions bad-server/src/connector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ use core::fmt::Debug;
use embedded_io::asynch::{Read, Write};

pub trait Connection: Read + Write {
#[cfg(feature = "defmt")]
type AcceptError: Debug + defmt::Format;
#[cfg(not(feature = "defmt"))]
type AcceptError: Debug;

// TODO: separate listener and socket
Expand All @@ -13,6 +16,7 @@ pub trait Connection: Read + Write {

#[cfg(feature = "embassy")]
pub mod embassy_net_compat {

use super::*;
use embassy_net::{
tcp::{AcceptError, TcpSocket},
Expand All @@ -29,7 +33,7 @@ pub mod embassy_net_compat {
fn close(&mut self) {
TcpSocket::close(self);
TcpSocket::abort(self);
log::debug!("Socket closed");
debug!("Socket closed");
}
}
}
Expand Down Expand Up @@ -118,7 +122,7 @@ pub mod std_compat {
let socket = socket.into_inner().unwrap();

socket.shutdown(std::net::Shutdown::Both).unwrap();
log::debug!("Socket closed");
debug!("Socket closed");
}
}
}
2 changes: 1 addition & 1 deletion bad-server/src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ impl<'a> StaticHandler<'a> {
let hash = fnv1a_hash_32(body, None);
let hash = match const_base::encode(&hash.to_le_bytes(), const_base::Config::B64) {
Ok(hash) => hash,
Err(_err) => panic!("Failed to base64-encode hash"),
Err(_err) => ::core::panic!("Failed to base64-encode hash"),
};

Self {
Expand Down
Loading