3 Commits
Author SHA1 Message Date
melfely 1247caa3f4 Project attempts to send the current LED status on a USB file 2026-08-18 22:35:52 -05:00
melfely 985a329da4 Commit 2026-08-18 20:43:32 -05:00
melfely dc935d56ee USB test that works! It parrots.
I mostly just wanted to save this state of the project
2026-08-15 19:01:34 -05:00
4 changed files with 272 additions and 24 deletions
Generated
+101 -4
View File
@@ -26,6 +26,15 @@ dependencies = [
"term", "term",
] ]
[[package]]
name = "atomic-polyfill"
version = "1.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8cf2bce30dfe09ef0bfaef228b9d414faaf7e563035494d7fe092dba54b300f4"
dependencies = [
"critical-section",
]
[[package]] [[package]]
name = "autocfg" name = "autocfg"
version = "1.5.1" version = "1.5.1"
@@ -44,7 +53,7 @@ version = "0.2.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5deb64efa5bd81e31fcd1938615a6d98c82eafcbcd787162b6f63b91d6bac5b3" checksum = "5deb64efa5bd81e31fcd1938615a6d98c82eafcbcd787162b6f63b91d6bac5b3"
dependencies = [ dependencies = [
"rustc_version", "rustc_version 0.2.3",
] ]
[[package]] [[package]]
@@ -134,6 +143,15 @@ dependencies = [
"pure-rust-locales", "pure-rust-locales",
] ]
[[package]]
name = "cobs"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0fa961b519f0b462e3a3b4a34b64d119eeaca1d59af726fe450bbba07a9fc0a1"
dependencies = [
"thiserror",
]
[[package]] [[package]]
name = "codespan-reporting" name = "codespan-reporting"
version = "0.11.1" version = "0.11.1"
@@ -588,6 +606,10 @@ dependencies = [
"embedded-io-async 0.7.0", "embedded-io-async 0.7.0",
"heapless 0.9.3", "heapless 0.9.3",
"panic-probe", "panic-probe",
"postcard",
"serde",
"static_cell",
"usb_communication_shared",
] ]
[[package]] [[package]]
@@ -773,6 +795,15 @@ dependencies = [
"zerocopy", "zerocopy",
] ]
[[package]]
name = "hash32"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b0c35f58762feb77d74ebe43bdbc3210f09be9fe6742234d573bacc26ed92b67"
dependencies = [
"byteorder",
]
[[package]] [[package]]
name = "hash32" name = "hash32"
version = "0.3.1" version = "0.3.1"
@@ -788,13 +819,27 @@ version = "0.17.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ed5909b6e89a2db4456e54cd5f673791d7eca6732202bbf2a9cc504fe2f9b84a" checksum = "ed5909b6e89a2db4456e54cd5f673791d7eca6732202bbf2a9cc504fe2f9b84a"
[[package]]
name = "heapless"
version = "0.7.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cdc6457c0eb62c71aac4bc17216026d8410337c4126773b9c5daba343f17964f"
dependencies = [
"atomic-polyfill",
"hash32 0.2.1",
"rustc_version 0.4.1",
"serde",
"spin",
"stable_deref_trait",
]
[[package]] [[package]]
name = "heapless" name = "heapless"
version = "0.8.0" version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0bfb9eb618601c89945a70e254898da93b13be0388091d42117462b265bb3fad" checksum = "0bfb9eb618601c89945a70e254898da93b13be0388091d42117462b265bb3fad"
dependencies = [ dependencies = [
"hash32", "hash32 0.3.1",
"stable_deref_trait", "stable_deref_trait",
] ]
@@ -804,7 +849,7 @@ version = "0.9.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "25ba4bd83f9415b58b4ed8dc5714c76e626a105be4646c02630ad730ad3b5aa4" checksum = "25ba4bd83f9415b58b4ed8dc5714c76e626a105be4646c02630ad730ad3b5aa4"
dependencies = [ dependencies = [
"hash32", "hash32 0.3.1",
"stable_deref_trait", "stable_deref_trait",
] ]
@@ -1125,6 +1170,17 @@ version = "1.15.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "05c8b63e8d9609db387f0324918f81d68fe27748f084ef092fb35954d0539a85" checksum = "05c8b63e8d9609db387f0324918f81d68fe27748f084ef092fb35954d0539a85"
[[package]]
name = "postcard"
version = "1.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6764c3b5dd454e283a30e6dfe78e9b31096d9e32036b5d1eaac7a6119ccb9a24"
dependencies = [
"cobs",
"heapless 0.7.17",
"serde",
]
[[package]] [[package]]
name = "precomputed-hash" name = "precomputed-hash"
version = "0.1.1" version = "0.1.1"
@@ -1267,7 +1323,16 @@ version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "138e3e0acb6c9fb258b19b67cb8abd63c00679d2851805ea151465464fe9030a" checksum = "138e3e0acb6c9fb258b19b67cb8abd63c00679d2851805ea151465464fe9030a"
dependencies = [ dependencies = [
"semver", "semver 0.9.0",
]
[[package]]
name = "rustc_version"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cfcb3a22ef46e85b45de6ee7e79d063319ebb6594faafcf1c225ea92ab6e9b92"
dependencies = [
"semver 1.0.28",
] ]
[[package]] [[package]]
@@ -1306,6 +1371,12 @@ dependencies = [
"semver-parser", "semver-parser",
] ]
[[package]]
name = "semver"
version = "1.0.28"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8a7852d02fc848982e0c167ef163aaff9cd91dc640ba85e263cb1ce46fae51cd"
[[package]] [[package]]
name = "semver-parser" name = "semver-parser"
version = "0.7.0" version = "0.7.0"
@@ -1319,6 +1390,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4148590afebada386688f18773da617792bf2ef03ffc1e4cbd2b1d45b023e0ba" checksum = "4148590afebada386688f18773da617792bf2ef03ffc1e4cbd2b1d45b023e0ba"
dependencies = [ dependencies = [
"serde_core", "serde_core",
"serde_derive",
] ]
[[package]] [[package]]
@@ -1402,6 +1474,15 @@ dependencies = [
"rgb", "rgb",
] ]
[[package]]
name = "spin"
version = "0.9.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3763264f6b73151db08c50ff20d7d8a0b8796e021cdea7ceedad07b80155fa0e"
dependencies = [
"lock_api",
]
[[package]] [[package]]
name = "ssmarshal" name = "ssmarshal"
version = "1.0.0" version = "1.0.0"
@@ -1418,6 +1499,15 @@ version = "1.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6ce2be8dc25455e1f91df71bfa12ad37d7af1092ae736f3a6cd0e37bc7810596" checksum = "6ce2be8dc25455e1f91df71bfa12ad37d7af1092ae736f3a6cd0e37bc7810596"
[[package]]
name = "static_cell"
version = "2.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0530892bb4fa575ee0da4b86f86c667132a94b74bb72160f58ee5a4afec74c23"
dependencies = [
"portable-atomic",
]
[[package]] [[package]]
name = "string_cache" name = "string_cache"
version = "0.8.9" version = "0.8.9"
@@ -1600,6 +1690,13 @@ dependencies = [
"portable-atomic", "portable-atomic",
] ]
[[package]]
name = "usb_communication_shared"
version = "0.1.0"
dependencies = [
"serde",
]
[[package]] [[package]]
name = "usbd-hid" name = "usbd-hid"
version = "0.10.1" version = "0.10.1"
+4
View File
@@ -20,3 +20,7 @@ heapless = "0.9"
embedded-hal-async = "1.0" embedded-hal-async = "1.0"
embedded-io-async = { version = "0.7.0", features = ["defmt"] } embedded-io-async = { version = "0.7.0", features = ["defmt"] }
defmt-rtt = "1.3.0" defmt-rtt = "1.3.0"
static_cell = "2.1.1"
usb_communication_shared = { version = "0.1.0", path = "../usb_communication_shared" }
serde = { version = "1.0.229", default-features = false }
postcard = "1.1.3"
+54 -20
View File
@@ -4,34 +4,68 @@
use defmt::*; use defmt::*;
use defmt_rtt as _; use defmt_rtt as _;
use embassy_executor::Spawner; use embassy_executor::Spawner;
use embassy_rp::{
bind_interrupts,
peripherals::USB,
usb::{Driver as UsbDriver, InterruptHandler as UsbInterruptHandler},
};
use embassy_time::Timer; use embassy_time::Timer;
use panic_probe as _; use panic_probe as _;
use crate::usb::{USB_TX_CHANNEL, handle_usb};
use usb_communication_shared::response::*;
mod usb;
bind_interrupts!(struct Irqs{
USBCTRL_IRQ => UsbInterruptHandler<USB>;
});
#[embassy_executor::main(
executor = "embassy_rp::executor::Executor",
entry = "cortex_m_rt::entry"
)]
async fn main(spawner: Spawner) {
let p = embassy_rp::init(Default::default());
let usb_driver = UsbDriver::new(p.USB, Irqs);
let _pin = embassy_rp::gpio::Output::new(p.PIN_26, true.into());
let _pin = embassy_rp::gpio::Output::new(p.PIN_27, true.into());
spawner.spawn(handle_usb(usb_driver).unwrap());
let led_future = async {
loop {
info!("led on!");
let response = Packet::LedStatus(LedStatus {
id: 0,
status: LedState::On,
});
USB_TX_CHANNEL.send(response).await;
Timer::after_millis(500).await;
info!("led off!");
let response = Packet::LedStatus(LedStatus {
id: 0,
status: LedState::Off,
});
USB_TX_CHANNEL.send(response).await;
Timer::after_millis(500).await;
}
};
led_future.await;
}
// Program metadata for `picotool info`. // Program metadata for `picotool info`.
// This isn't needed, but it's recommended to have these minimal entries. // This isn't needed, but it's recommended to have these minimal entries.
#[unsafe(link_section = ".bi_entries")] #[unsafe(link_section = ".bi_entries")]
#[used] #[used]
pub static PICOTOOL_ENTRIES: [embassy_rp::binary_info::EntryAddr; 4] = [ pub static PICOTOOL_ENTRIES: [embassy_rp::binary_info::EntryAddr; 4] = [
embassy_rp::binary_info::rp_program_name!(c"Blinky Example"), embassy_rp::binary_info::rp_program_name!(c"USB-Communication"),
embassy_rp::binary_info::rp_program_description!( embassy_rp::binary_info::rp_program_description!(c"USB-Communication System"),
c"This example tests the RP Pico on board LED, connected to gpio 25"
),
embassy_rp::binary_info::rp_cargo_version!(), embassy_rp::binary_info::rp_cargo_version!(),
embassy_rp::binary_info::rp_program_build_attribute!(), embassy_rp::binary_info::rp_program_build_attribute!(),
]; ];
#[embassy_executor::main(
executor = "embassy_rp::executor::Executor",
entry = "cortex_m_rt::entry"
)]
async fn main(_spawner: Spawner) {
let p = embassy_rp::init(Default::default());
loop {
info!("led on!");
Timer::after_millis(500).await;
info!("led off!");
Timer::after_millis(500).await;
}
}
+113
View File
@@ -0,0 +1,113 @@
use defmt::*;
use static_cell::StaticCell;
use embassy_sync::{blocking_mutex::raw::CriticalSectionRawMutex, channel::Channel};
use embassy_rp::{peripherals::USB, usb::Driver as UsbDriver};
use embassy_usb::class::cdc_acm::{CdcAcmClass, State as UsbState};
use usb_communication_shared::request::Packet as RequestPacket;
use usb_communication_shared::response::Packet as ResponsePacket;
/// USB Channel to Send Packets to the Host
pub static USB_TX_CHANNEL: Channel<CriticalSectionRawMutex, ResponsePacket, 4> = Channel::new();
/// USB Recieving Channel to Get Packets from the host
pub static USB_RX_CHANNEL: Channel<CriticalSectionRawMutex, RequestPacket, 4> = Channel::new();
#[embassy_executor::task]
pub async fn handle_usb(usb_driver: UsbDriver<'static, USB>) {
let mut config = embassy_usb::Config::new(0xc0de, 0xcafe);
config.manufacturer = Some("Melfely");
config.product = Some("USB-Parrot");
config.serial_number = Some("1234");
config.max_power = 100;
config.max_packet_size_0 = 64;
let mut usb_builder = {
static CONFIG_DESCRIPTOR: StaticCell<[u8; 256]> = StaticCell::new();
static BOS_DESCRIPTOR: StaticCell<[u8; 256]> = StaticCell::new();
static CONTROL_BUF: StaticCell<[u8; 64]> = StaticCell::new();
let embassy = embassy_usb::Builder::new(
usb_driver,
config,
CONFIG_DESCRIPTOR.init([0; 256]),
BOS_DESCRIPTOR.init([0; 256]),
&mut [],
CONTROL_BUF.init([0; 64]),
);
embassy
};
let usb_class = {
static STATE: StaticCell<UsbState> = StaticCell::new();
let state = STATE.init(UsbState::new());
CdcAcmClass::new(&mut usb_builder, state, 64)
};
let (mut usb_tx, mut usb_rx, _usb_control) = usb_class.split_with_control();
let mut usb = usb_builder.build();
let usb_fut = usb.run();
let mut usb_buf = [0; 64];
let usb_rx_future = async {
let sender = USB_RX_CHANNEL.sender();
loop {
usb_rx.wait_connection().await;
info!("USB RX Connected");
loop {
let packet = match usb_rx.read_packet(&mut usb_buf).await {
Err(err) => {
error!(
"Usb read error: {:?}. Assume Discconection",
Debug2Format(&err)
);
break;
}
Ok(_) => postcard::from_bytes::<RequestPacket>(&usb_buf),
};
match packet {
Ok(packet) => {
sender.send(packet).await;
}
Err(err) => {
error!("Serde Deserailize Error: {:?}.", Debug2Format(&err));
}
}
}
}
};
let usb_tx_future = async {
let reciever = USB_TX_CHANNEL.receiver();
loop {
usb_tx.wait_connection().await;
info!("USB TX Connected");
loop {
let bytes = match postcard::to_vec::<ResponsePacket, 64>(&reciever.receive().await)
{
Err(err) => {
error!("Serde Serailize Error: {:?}.", Debug2Format(&err));
continue; // Run loop again if it fails
}
Ok(vec) => vec,
};
match usb_tx.write_packet(&bytes).await {
Ok(_) => {}
Err(err) => {
error!(
"Usb write error: {:?}, Assume Disconnection",
Debug2Format(&err)
);
break;
}
}
}
}
};
embassy_futures::join::join3(usb_fut, usb_rx_future, usb_tx_future).await;
}