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

feat(examples): add usb-serial example #328

Merged
merged 1 commit into from
Jul 2, 2024
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
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions examples/laze.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ subdirs:
- random
- threading
- threading-channel
- usb-serial
15 changes: 15 additions & 0 deletions examples/usb-serial/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[package]
name = "usb-serial"
version = "0.1.0"
authors.workspace = true
edition.workspace = true
publish = false

[dependencies]
embassy-executor = { workspace = true, default-features = false }
embassy-time = { workspace = true, default-features = false }
riot-rs = { path = "../../src/riot-rs", features = [
"usb",
"override-usb-config",
] }
riot-rs-boards = { path = "../../src/riot-rs-boards" }
16 changes: 16 additions & 0 deletions examples/usb-serial/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# usb-serial

## About

This application is showing basic
[Embassy](https://github.com/embassy-rs/embassy) _USB serial_ usage with RIOT-rs.

## How to run

In this folder, run

laze build -b nrf52840dk run

With the device USB cable connected, a USB ACM serial port should show up on
your laptop / workstation.
It will simply echo every sent character.
5 changes: 5 additions & 0 deletions examples/usb-serial/laze.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
apps:
- name: usb-serial
selects:
- hw/usb-device-port
- ?release
75 changes: 75 additions & 0 deletions examples/usb-serial/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#![no_std]
#![no_main]
#![feature(type_alias_impl_trait)]
#![feature(used_with_arg)]

use riot_rs::{
debug::log::info,
embassy::{
embassy_usb, make_static,
usb::{UsbBuilderHook, UsbDriver},
},
};

use embassy_usb::{
class::cdc_acm::{CdcAcmClass, State},
driver::EndpointError,
};

#[riot_rs::config(usb)]
fn usb_config() -> embassy_usb::Config<'static> {
let mut config = embassy_usb::Config::new(0xc0de, 0xcafe);
kaspar030 marked this conversation as resolved.
Show resolved Hide resolved
config.manufacturer = Some("RIOT-rs");
config.product = Some("USB serial example");
config.serial_number = Some("12345678");
config.max_power = 100;
config.max_packet_size_0 = 64;

// Required for Windows support.
config.composite_with_iads = true;
config.device_class = 0xEF;
config.device_sub_class = 0x02;
config.device_protocol = 0x01;
kaspar030 marked this conversation as resolved.
Show resolved Hide resolved
config
}

#[riot_rs::task(autostart, usb_builder_hook)]
async fn main() {
info!("Hello World!");

let state = make_static!(State::new());

// Inject class on the system USB builder.
let mut class = USB_BUILDER_HOOK
.with(|builder| CdcAcmClass::new(builder, state, 64))
.await;

// Do stuff with the class!
loop {
class.wait_connection().await;
info!("Connected");
let _ = echo(&mut class).await;
info!("Disconnected");
}
}

struct Disconnected {}

impl From<EndpointError> for Disconnected {
fn from(val: EndpointError) -> Self {
match val {
EndpointError::BufferOverflow => panic!("Buffer overflow"),
EndpointError::Disabled => Disconnected {},
}
}
}

async fn echo(class: &mut CdcAcmClass<'static, UsbDriver>) -> Result<(), Disconnected> {
let mut buf = [0; 64];
loop {
let n = class.read_packet(&mut buf).await?;
let data = &buf[..n];
info!("data: {:x}", data);
class.write_packet(data).await?;
}
}
Loading