Skip to content

Commit

Permalink
Merge pull request #3 from dlkj/ci_and_defmt
Browse files Browse the repository at this point in the history
add CI and defmt feature
  • Loading branch information
dlkj authored Nov 17, 2024
2 parents 4373837 + 673881c commit 350711c
Show file tree
Hide file tree
Showing 12 changed files with 258 additions and 113 deletions.
10 changes: 10 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
version: 2
updates:
- package-ecosystem: "cargo"
directory: "/"
schedule:
interval: "daily"
- package-ecosystem: "cargo"
directory: "/examples/rp2040/"
schedule:
interval: "daily"
18 changes: 18 additions & 0 deletions .github/workflows/audit.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: Security audit

on:
schedule:
- cron: "0 0 * * 0"
push:
paths:
- "**/Cargo.toml"
- "**/Cargo.lock"

jobs:
audit:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: rustsec/audit-check@v1
with:
token: ${{ secrets.GITHUB_TOKEN }}
37 changes: 37 additions & 0 deletions .github/workflows/examples_rp2040_build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
on: [push, pull_request]

name: Examples RP2040 build

env:
CARGO_TERM_COLOR: always

jobs:
check_format_build:
name: Examples - Check, Format, Build
runs-on: ubuntu-latest
steps:
#Checkout source
- name: Checkout sources
uses: actions/checkout@v4
#toolchain and tools
- name: Install stable toolchain
uses: actions-rs/toolchain@v1
with:
toolchain: stable
target: thumbv6m-none-eabi
components: rustfmt, clippy
- name: Install flip-link linker
run: cargo install flip-link
#build and lint
- name: Run cargo check - examples rp2040
working-directory: ./examples/rp2040/
run: cargo check
- name: Run cargo fmt - examples rp2040
working-directory: ./examples/rp2040/
run: cargo fmt --all -- --check
- name: Run cargo clippy - examples rp2040
working-directory: ./examples/rp2040/
run: cargo clippy -- -D warnings
- name: Run cargo build - examples rp2040
working-directory: ./examples/rp2040/
run: cargo build
38 changes: 38 additions & 0 deletions .github/workflows/lib_build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
on: [push, pull_request]

name: Lib build

env:
CARGO_TERM_COLOR: always

jobs:
check_format_build:
name: Lib - Check, Format, Build
runs-on: ubuntu-latest
steps:
#Checkout source
- name: Checkout sources
uses: actions/checkout@v4
#toolchain and tools
- name: Install stable toolchain
uses: actions-rs/toolchain@v1
with:
toolchain: stable
components: rustfmt, clippy
#build and lint
- name: Run cargo check
run: cargo check
- name: Run cargo fmt
run: cargo fmt --all -- --check
- name: Run cargo clippy
run: cargo clippy -- -D warnings
- name: Run cargo clippy with defmt
run: cargo clippy --features defmt -- -D warnings
- name: Run cargo clippy on tests
run: cargo clippy --tests -- -D warnings
- name: Run cargo test
run: cargo test
- name: Run cargo build
run: cargo build
- name: Run cargo build with defmt
run: cargo build --features defmt
5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ smoltcp = { version = "0.9", default-features = false, features = [
"medium-ethernet",
"socket-udp",
] }
defmt = "0.3"
defmt = { version = "0.3", optional = true }
heapless = { version = "0.7", features = ["defmt-impl"] }
usb-device = { version = "0.2", features = ["defmt"] }

[features]
defmt = ["dep:defmt", "usb-device/defmt"]
3 changes: 1 addition & 2 deletions examples/rp2040/.cargo/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ rustflags = [
# trap unreachable can save a lot of space, but requires nightly compiler.
# uncomment the next line if you wish to enable it
# "-Z", "trap-unreachable=no",
"-C",
"inline-threshold=5",
"-Cllvm-args=--inline-threshold=5",
"-C",
"no-vectorize-loops",
]
Expand Down
2 changes: 1 addition & 1 deletion examples/rp2040/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ smoltcp = { version = "0.9.1", default-features = false, features = [
"proto-ipv4",
] }
rp-pico = { version = "0.7" }
usbd-ethernet = { path = "../../" }
usbd-ethernet = { path = "../../", features = ["defmt"] }

# cargo build/run
[profile.dev]
Expand Down
4 changes: 2 additions & 2 deletions examples/rp2040/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,12 +157,12 @@ fn usb_poll<B: UsbBus>(usb_dev: &mut UsbDevice<B>, cdc_ethernet: &mut Ethernet<B
if cdc_ethernet.connection_speed().is_none() {
// 1000 Kps upload and download
match cdc_ethernet.set_connection_speed(1_000_000, 1_000_000) {
Ok(_) | Err(UsbError::WouldBlock) => {}
Ok(()) | Err(UsbError::WouldBlock) => {}
Err(e) => error!("Failed to set connection speed: {}", e),
}
} else if cdc_ethernet.state() == DeviceState::Disconnected {
match cdc_ethernet.connect() {
Ok(_) | Err(UsbError::WouldBlock) => {}
Ok(()) | Err(UsbError::WouldBlock) => {}
Err(e) => error!("Failed to connect: {}", e),
}
}
Expand Down
26 changes: 12 additions & 14 deletions src/buffer.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use defmt::error;
// TODO remove usb stuff from this mod
use usb_device::Result;
use usb_device::UsbError;
Expand Down Expand Up @@ -44,14 +43,15 @@ impl<'a, const LEN: usize> RWBuffer<'a, LEN> {
len: usize,
f: impl FnOnce(&mut [u8]) -> Result<(usize, R)>,
) -> Result<(usize, R)> {
let Some(buf) = self.store.get_mut(self.write_ptr .. self.write_ptr + len)
else{
error!("buffer: tried to write more data than capacity");
let Some(buf) = self.store.get_mut(self.write_ptr..self.write_ptr + len) else {
#[cfg(feature = "defmt")]
defmt::error!("buffer: tried to write more data than capacity");
return Err(UsbError::BufferOverflow);
};
let (written, r) = f(buf)?;
if written > len {
error!("buffer: claim to have written more data than allocated");
#[cfg(feature = "defmt")]
defmt::error!("buffer: claim to have written more data than allocated");
return Err(UsbError::BufferOverflow);
}
self.write_ptr += written;
Expand All @@ -68,18 +68,16 @@ impl<'a, const LEN: usize> RWBuffer<'a, LEN> {
len: usize,
f: impl FnOnce(&mut [u8]) -> Result<(usize, R)>,
) -> Result<(usize, R)> {
let Some(buf) = self
.store
.get_mut(self.read_ptr..self.read_ptr + len)
else
{
error!("buffer: tried to read more data than available");
return Err(UsbError::InvalidState);
};
let Some(buf) = self.store.get_mut(self.read_ptr..self.read_ptr + len) else {
#[cfg(feature = "defmt")]
defmt::error!("buffer: tried to read more data than available");
return Err(UsbError::InvalidState);
};

let (read, r) = f(buf)?;
if read > len {
error!("buffer: claim to have read more data than allocated");
#[cfg(feature = "defmt")]
defmt::error!("buffer: claim to have read more data than allocated");
return Err(UsbError::BufferOverflow);
}
self.read_ptr += read;
Expand Down
1 change: 1 addition & 0 deletions src/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ pub(crate) trait Buf {

fn get_slice(&mut self, size: usize) -> &[u8];

#[allow(dead_code)]
fn get_u8(&mut self) -> Option<u8> {
const SIZE: usize = core::mem::size_of::<u8>();
let value = self.chunk().first().copied()?;
Expand Down
Loading

0 comments on commit 350711c

Please sign in to comment.