This commit is contained in:
2026-08-18 20:43:32 -05:00
parent dc935d56ee
commit 985a329da4
4 changed files with 128 additions and 20 deletions
Generated
+82 -4
View File
@@ -26,6 +26,15 @@ dependencies = [
"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]]
name = "autocfg"
version = "1.5.1"
@@ -44,7 +53,7 @@ version = "0.2.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5deb64efa5bd81e31fcd1938615a6d98c82eafcbcd787162b6f63b91d6bac5b3"
dependencies = [
"rustc_version",
"rustc_version 0.2.3",
]
[[package]]
@@ -134,6 +143,15 @@ dependencies = [
"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]]
name = "codespan-reporting"
version = "0.11.1"
@@ -588,6 +606,8 @@ dependencies = [
"embedded-io-async 0.7.0",
"heapless 0.9.3",
"panic-probe",
"postcard",
"serde",
"static_cell",
"usb_communication_shared",
]
@@ -775,6 +795,15 @@ dependencies = [
"zerocopy",
]
[[package]]
name = "hash32"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b0c35f58762feb77d74ebe43bdbc3210f09be9fe6742234d573bacc26ed92b67"
dependencies = [
"byteorder",
]
[[package]]
name = "hash32"
version = "0.3.1"
@@ -790,13 +819,27 @@ version = "0.17.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
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]]
name = "heapless"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0bfb9eb618601c89945a70e254898da93b13be0388091d42117462b265bb3fad"
dependencies = [
"hash32",
"hash32 0.3.1",
"stable_deref_trait",
]
@@ -806,7 +849,7 @@ version = "0.9.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "25ba4bd83f9415b58b4ed8dc5714c76e626a105be4646c02630ad730ad3b5aa4"
dependencies = [
"hash32",
"hash32 0.3.1",
"stable_deref_trait",
]
@@ -1127,6 +1170,17 @@ version = "1.15.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
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]]
name = "precomputed-hash"
version = "0.1.1"
@@ -1269,7 +1323,16 @@ version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "138e3e0acb6c9fb258b19b67cb8abd63c00679d2851805ea151465464fe9030a"
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]]
@@ -1308,6 +1371,12 @@ dependencies = [
"semver-parser",
]
[[package]]
name = "semver"
version = "1.0.28"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8a7852d02fc848982e0c167ef163aaff9cd91dc640ba85e263cb1ce46fae51cd"
[[package]]
name = "semver-parser"
version = "0.7.0"
@@ -1405,6 +1474,15 @@ dependencies = [
"rgb",
]
[[package]]
name = "spin"
version = "0.9.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3763264f6b73151db08c50ff20d7d8a0b8796e021cdea7ceedad07b80155fa0e"
dependencies = [
"lock_api",
]
[[package]]
name = "ssmarshal"
version = "1.0.0"
+2
View File
@@ -22,3 +22,5 @@ embedded-io-async = { version = "0.7.0", features = ["defmt"] }
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"
+15 -1
View File
@@ -12,7 +12,8 @@ use embassy_rp::{
use embassy_time::Timer;
use panic_probe as _;
use crate::usb::handle_usb;
use crate::usb::{USB_TX_CHANNEL, handle_usb};
use usb_communication_shared::response::*;
mod usb;
@@ -29,14 +30,27 @@ async fn main(spawner: Spawner) {
let usb_driver = UsbDriver::new(p.USB, Irqs);
let pin = embassy_rp::gpio::Output::new(p.PIN_26, true);
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;
}
};
+29 -15
View File
@@ -1,5 +1,4 @@
use defmt::*;
use heapless::Vec;
use static_cell::StaticCell;
use embassy_sync::{blocking_mutex::raw::CriticalSectionRawMutex, channel::Channel};
@@ -8,8 +7,12 @@ 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;
static USB_SEND_CHANNEL: Channel<CriticalSectionRawMutex, ResponsePacket, 4> = Channel::new();
static USB_RECIEVE_CHANNEL: Channel<CriticalSectionRawMutex, RequestPacket, 4> = Channel::new();
/// 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>) {
@@ -48,26 +51,29 @@ pub async fn handle_usb(usb_driver: UsbDriver<'static, USB>) {
let usb_fut = usb.run();
let channel = Channel::<CriticalSectionRawMutex, Vec<u8, 64>, 4>::new();
let mut usb_buf = [0; 64];
let usb_rx_future = async {
let sender = channel.sender();
let sender = USB_RX_CHANNEL.sender();
loop {
usb_rx.wait_connection().await;
info!("USB RX Connected");
loop {
match usb_rx.read_packet(&mut usb_buf).await {
Err(e) => {
let packet = match usb_rx.read_packet(&mut usb_buf).await {
Err(err) => {
error!(
"Usb read error: {:?}. Assume Discconection",
Debug2Format(&e)
Debug2Format(&err)
);
break;
}
Ok(_) => {
let vec: Vec<u8, 64> = usb_buf.into();
sender.send(vec).await;
Ok(_) => postcard::from_bytes::<RequestPacket>(&usb_buf),
};
match packet {
Ok(packet) => {
sender.send(packet).await;
}
Err(err) => {
error!("Serde Deserailize Error: {:?}.", Debug2Format(&err));
}
}
}
@@ -75,13 +81,21 @@ pub async fn handle_usb(usb_driver: UsbDriver<'static, USB>) {
};
let usb_tx_future = async {
let reciever = channel.receiver();
let reciever = USB_TX_CHANNEL.receiver();
loop {
usb_tx.wait_connection().await;
info!("USB TX Connected");
loop {
let packet = reciever.receive().await;
match usb_tx.write_packet(&packet).await {
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!(