Skip to content

Commit 91bb9c6

Browse files
authored
Merge pull request #15 from lxuxx/i2c_feature
i2c master and slave
2 parents 8dae56b + 73be8d2 commit 91bb9c6

File tree

11 files changed

+2397
-6
lines changed

11 files changed

+2397
-6
lines changed

Cargo.lock

Lines changed: 32 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ edition = "2021"
1717
[features]
1818
default = []
1919
std = []
20+
i2c_target = []
2021
test-rsa = []
2122
test-ecdsa = []
2223
test-hmac = []
@@ -33,6 +34,7 @@ embedded-io = "0.6.1"
3334
fugit = "0.3.7"
3435
proposed-traits = { git = "https://github.com/rusty1968/proposed_traits.git", package = "proposed-traits", rev = "85641310df5a5276c67f81621b104322cff0286c" }
3536
hex-literal = "0.4"
37+
heapless = "0.8.0"
3638
nb = "1.1.0"
3739
paste = "1.0"
3840

src/common.rs

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
// Licensed under the Apache-2.0 license
22

3+
use crate::uart::UartController;
4+
use core::ops::{Index, IndexMut};
5+
use embedded_io::Write;
6+
37
pub struct DummyDelay;
48

59
impl embedded_hal::delay::DelayNs for DummyDelay {
@@ -38,27 +42,25 @@ impl<const N: usize> DmaBuffer<N> {
3842
}
3943

4044
#[must_use]
41-
pub fn len(&self) -> usize {
45+
pub const fn len(&self) -> usize {
4246
N
4347
}
4448

4549
#[must_use]
46-
pub fn is_empty(&self) -> bool {
50+
pub const fn is_empty(&self) -> bool {
4751
N == 0
4852
}
4953

5054
#[must_use]
51-
pub fn as_slice(&self) -> &[u8] {
52-
&self.buf
55+
pub fn as_slice(&self, start: usize, end: usize) -> &[u8] {
56+
&self.buf[start..end]
5357
}
5458

5559
pub fn as_mut_slice(&mut self, start: usize, end: usize) -> &mut [u8] {
5660
&mut self.buf[start..end]
5761
}
5862
}
5963

60-
use core::ops::{Index, IndexMut};
61-
6264
impl<const N: usize> Index<usize> for DmaBuffer<N> {
6365
type Output = u8;
6466
fn index(&self, idx: usize) -> &Self::Output {
@@ -71,3 +73,37 @@ impl<const N: usize> IndexMut<usize> for DmaBuffer<N> {
7173
&mut self.buf[idx]
7274
}
7375
}
76+
77+
pub trait Logger {
78+
fn debug(&mut self, msg: &str);
79+
fn error(&mut self, msg: &str);
80+
}
81+
82+
// No-op implementation for production builds
83+
pub struct NoOpLogger;
84+
impl Logger for NoOpLogger {
85+
fn debug(&mut self, _msg: &str) {}
86+
fn error(&mut self, _msg: &str) {}
87+
}
88+
89+
// UART logger adapter (separate concern)
90+
pub struct UartLogger<'a> {
91+
uart: &'a mut UartController<'a>,
92+
}
93+
94+
impl<'a> UartLogger<'a> {
95+
pub fn new(uart: &'a mut UartController<'a>) -> Self {
96+
UartLogger { uart }
97+
}
98+
}
99+
100+
impl<'a> Logger for UartLogger<'a> {
101+
fn debug(&mut self, msg: &str) {
102+
writeln!(self.uart, "{msg}").ok();
103+
write!(self.uart, "\r").ok();
104+
}
105+
fn error(&mut self, msg: &str) {
106+
writeln!(self.uart, "ERROR: {msg}").ok();
107+
write!(self.uart, "\r").ok();
108+
}
109+
}

0 commit comments

Comments
 (0)