This gets communication working with the device

The device is currently emmitting primes. pi-usb-serial and serial-comm
give me a template for being able to do communication with a USB device
through a serial port.
This commit is contained in:
Savanni D'Gerinel 2024-08-09 01:06:30 -04:00
parent 93b84ee72d
commit d76fd8cc39
6 changed files with 403 additions and 28 deletions

294
Cargo.lock generated
View File

@ -321,7 +321,7 @@ version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "edcdbedc2236483ab103a53415653d6b4442ea6141baf1ffa85df29635e88436"
dependencies = [
"nix",
"nix 0.27.1",
"rand 0.8.5",
]
@ -450,6 +450,12 @@ version = "0.13.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "46afbd2983a5d5a7bd740ccb198caf5b82f45c40c09c0eed36052d91cb92e719"
[[package]]
name = "bitfield"
version = "0.14.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2d7e60934ceec538daadb9d8432424ed043a904d8e0243f3c6446bce549a46ac"
[[package]]
name = "bitflags"
version = "1.3.2"
@ -771,7 +777,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ec610d8f49840a5b376c69663b6369e71f4b34484b9b2eb29fb918d92516cb9"
dependencies = [
"bare-metal",
"bitfield",
"bitfield 0.13.2",
"embedded-hal 0.2.7",
"volatile-register",
]
@ -864,7 +870,7 @@ dependencies = [
"autocfg 1.1.0",
"cfg-if",
"crossbeam-utils",
"memoffset",
"memoffset 0.9.0",
]
[[package]]
@ -1147,6 +1153,22 @@ version = "0.15.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1aaf95b3e5c8f23aa320147307562d361db0ae0d51242340f558153b4eb2439b"
[[package]]
name = "e-ink"
version = "0.1.0"
dependencies = [
"cortex-m-rt",
"defmt",
"defmt-rtt",
"embassy-embedded-hal",
"embassy-executor",
"embassy-rp",
"embassy-sync 0.6.0",
"embassy-time",
"embedded-hal 1.0.0",
"panic-probe",
]
[[package]]
name = "either"
version = "1.9.0"
@ -1219,6 +1241,23 @@ dependencies = [
"num-traits",
]
[[package]]
name = "embassy-net-driver"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "524eb3c489760508f71360112bca70f6e53173e6fe48fc5f0efd0f5ab217751d"
[[package]]
name = "embassy-net-driver-channel"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4818c32afec43e3cae234f324bad9a976c9aa7501022d26ff60a4017a1a006b7"
dependencies = [
"embassy-futures",
"embassy-net-driver",
"embassy-sync 0.6.0",
]
[[package]]
name = "embassy-rp"
version = "0.1.0"
@ -1318,6 +1357,21 @@ version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f1177859559ebf42cd24ae7ba8fe6ee707489b01d0bf471f8827b7b12dcb0bc0"
[[package]]
name = "embassy-usb"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8d0b882133fa684b9d4652351cd7aac5afe8a2c2bf4a7da59f442ff61087cda2"
dependencies = [
"embassy-futures",
"embassy-net-driver-channel",
"embassy-sync 0.6.0",
"embassy-usb-driver",
"heapless",
"ssmarshal",
"usbd-hid",
]
[[package]]
name = "embassy-usb-driver"
version = "0.1.0"
@ -1435,6 +1489,12 @@ dependencies = [
"log 0.4.22",
]
[[package]]
name = "encode_unicode"
version = "0.3.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a357d28ed41a50f9c765dbfe56cbc04a64e53e5fc58ba79fbc34c10ef3df831f"
[[package]]
name = "encoding_rs"
version = "0.8.33"
@ -1557,7 +1617,7 @@ version = "0.3.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "38e2275cc4e4fc009b0669731a1e5ab7ebf11f469eaede2bab9309a5b4d6057f"
dependencies = [
"memoffset",
"memoffset 0.9.0",
"rustc_version 0.4.0",
]
@ -2353,6 +2413,15 @@ dependencies = [
"byteorder",
]
[[package]]
name = "hashbrown"
version = "0.13.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "43a3c133739dddd0d2990f9a4bdf8eb4b21ef50e4851ca85ab661199821d510e"
dependencies = [
"ahash",
]
[[package]]
name = "hashbrown"
version = "0.14.3"
@ -2369,7 +2438,7 @@ version = "0.8.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e8094feaf31ff591f651a2664fb9cfd92bba7a60ce3197265e9482ebe753c8f7"
dependencies = [
"hashbrown",
"hashbrown 0.14.3",
]
[[package]]
@ -2417,9 +2486,9 @@ dependencies = [
[[package]]
name = "hermit-abi"
version = "0.3.3"
version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d77f7ec81a6d05a3abb01ab6eb7590f6083d08449fe5a1c8b1e620283546ccb7"
checksum = "d231dfb89cfffdbc30e7fc41579ed6066ad03abda9e567ccafae602b97ec5024"
[[package]]
name = "hex"
@ -2700,7 +2769,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d530e1a18b1cb4c484e6e34556a0d948706958449fca0cab753d649f2bce3d1f"
dependencies = [
"equivalent",
"hashbrown",
"hashbrown 0.14.3",
]
[[package]]
@ -2731,6 +2800,16 @@ dependencies = [
"unic-langid",
]
[[package]]
name = "io-kit-sys"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "617ee6cf8e3f66f3b4ea67a4058564628cde41901316e19f559e14c7c72c5e7b"
dependencies = [
"core-foundation-sys",
"mach2",
]
[[package]]
name = "io-lifetimes"
version = "1.0.11"
@ -3029,6 +3108,15 @@ dependencies = [
"time 0.1.45",
]
[[package]]
name = "mach2"
version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "19b955cdeb2a02b9117f121ce63aa52d08ade45de53e48fe6a38b39c10f6f709"
dependencies = [
"libc",
]
[[package]]
name = "matches"
version = "0.1.10"
@ -3051,6 +3139,15 @@ version = "2.6.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f665ee40bc4a3c5590afb1e9677db74a508659dfd71e126420da8274909a0167"
[[package]]
name = "memoffset"
version = "0.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5de893c32cde5f383baa4c04c5d6dbdd735cfd4a794b0debdb2bb1b421da5ff4"
dependencies = [
"autocfg 1.1.0",
]
[[package]]
name = "memoffset"
version = "0.9.0"
@ -3150,10 +3247,36 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8f3d0b296e374a4e6f3c7b0a1f5a51d748a0d34c85e7dc48fc3fa9a87657fe09"
dependencies = [
"libc",
"log 0.4.22",
"wasi 0.11.0+wasi-snapshot-preview1",
"windows-sys 0.48.0",
]
[[package]]
name = "mio"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4569e456d394deccd22ce1c1913e6ea0e54519f577285001215d33557431afe4"
dependencies = [
"hermit-abi",
"libc",
"wasi 0.11.0+wasi-snapshot-preview1",
"windows-sys 0.52.0",
]
[[package]]
name = "mio-serial"
version = "5.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "20a4c60ca5c9c0e114b3bd66ff4aa5f9b2b175442be51ca6c4365d687a97a2ac"
dependencies = [
"log 0.4.22",
"mio 0.8.10",
"nix 0.26.4",
"serialport",
"winapi",
]
[[package]]
name = "modifier"
version = "0.1.0"
@ -3227,6 +3350,19 @@ version = "1.0.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "650eef8c711430f1a879fdd01d4745a7deea475becfb90269c06775983bbf086"
[[package]]
name = "nix"
version = "0.26.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "598beaf3cc6fdd9a5dfb1630c2800c7acd31df7aaf0f565796fba2b53ca1af1b"
dependencies = [
"bitflags 1.3.2",
"cfg-if",
"libc",
"memoffset 0.7.1",
"pin-utils",
]
[[package]]
name = "nix"
version = "0.27.1"
@ -3673,7 +3809,10 @@ dependencies = [
"embassy-rp",
"embassy-sync 0.6.0",
"embassy-time",
"embassy-usb",
"panic-probe",
"portable-atomic",
"static_cell",
]
[[package]]
@ -3850,6 +3989,15 @@ dependencies = [
"windows-sys 0.52.0",
]
[[package]]
name = "portable-atomic"
version = "1.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "da544ee218f0d287a911e9c99a39a8c9bc8fcad3cb8db5959940044ecfc67265"
dependencies = [
"critical-section",
]
[[package]]
name = "powerfmt"
version = "0.2.0"
@ -4301,7 +4449,7 @@ dependencies = [
"fugit",
"rp2040-boot2",
"rp2040-hal",
"usb-device",
"usb-device 0.2.9",
]
[[package]]
@ -4332,7 +4480,7 @@ dependencies = [
"rand_core 0.6.4",
"rp2040-hal-macros",
"rp2040-pac",
"usb-device",
"usb-device 0.2.9",
"vcell",
"void",
]
@ -4629,6 +4777,31 @@ dependencies = [
"serde 1.0.204",
]
[[package]]
name = "serial-comm"
version = "0.1.0"
dependencies = [
"tokio",
"tokio-serial",
]
[[package]]
name = "serialport"
version = "4.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "241ebb629ed9bf598b2b392ba42aa429f9ef2a0099001246a36ac4c084ee183f"
dependencies = [
"bitflags 2.4.1",
"cfg-if",
"core-foundation-sys",
"io-kit-sys",
"mach2",
"nix 0.26.4",
"scopeguard",
"unescaper",
"winapi",
]
[[package]]
name = "sgf"
version = "0.1.0"
@ -4989,12 +5162,31 @@ dependencies = [
"urlencoding",
]
[[package]]
name = "ssmarshal"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f3e6ad23b128192ed337dfa4f1b8099ced0c2bf30d61e551b65fda5916dbb850"
dependencies = [
"encode_unicode",
"serde 1.0.204",
]
[[package]]
name = "stable_deref_trait"
version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a8f112729512f8e442d81f95a8a7ddf2b7c6b8a1a6f509a95864142b30cab2d3"
[[package]]
name = "static_cell"
version = "2.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d89b0684884a883431282db1e4343f34afc2ff6996fe1f4a1664519b66e14c1e"
dependencies = [
"portable-atomic",
]
[[package]]
name = "string_cache"
version = "0.8.7"
@ -5267,28 +5459,27 @@ checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20"
[[package]]
name = "tokio"
version = "1.35.1"
version = "1.39.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c89b4efa943be685f629b149f53829423f8f5531ea21249408e8e2f8671ec104"
checksum = "daa4fb1bc778bd6f04cbfc4bb2d06a7396a8f299dc33ea1900cedaa316f467b1"
dependencies = [
"backtrace",
"bytes",
"libc",
"mio",
"num_cpus",
"mio 1.0.1",
"parking_lot",
"pin-project-lite",
"signal-hook-registry",
"socket2 0.5.5",
"tokio-macros",
"windows-sys 0.48.0",
"windows-sys 0.52.0",
]
[[package]]
name = "tokio-macros"
version = "2.2.0"
version = "2.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5b8a1e28f2deaa14e508979454cb3a223b10b938b45af148bc0986de36f1923b"
checksum = "693d596312e88961bc67d7f1f97af8a70227d9f90c31bba5806eec004978d752"
dependencies = [
"proc-macro2",
"quote",
@ -5305,6 +5496,19 @@ dependencies = [
"tokio",
]
[[package]]
name = "tokio-serial"
version = "5.4.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "aa6e2e4cf0520a99c5f87d5abb24172b5bd220de57c3181baaaa5440540c64aa"
dependencies = [
"cfg-if",
"futures",
"log 0.4.22",
"mio-serial",
"tokio",
]
[[package]]
name = "tokio-stream"
version = "0.1.14"
@ -5518,6 +5722,15 @@ version = "0.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eaea85b334db583fe3274d12b4cd1880032beab409c0d774be044d4480ab9a94"
[[package]]
name = "unescaper"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c878a167baa8afd137494101a688ef8c67125089ff2249284bd2b5f9bfedb815"
dependencies = [
"thiserror",
]
[[package]]
name = "unic-langid"
version = "0.9.4"
@ -5642,6 +5855,53 @@ version = "0.2.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1f6cc3adc849b5292b4075fc0d5fdcf2f24866e88e336dd27a8943090a520508"
[[package]]
name = "usb-device"
version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "98816b1accafbb09085168b90f27e93d790b4bfa19d883466b5e53315b5f06a6"
dependencies = [
"heapless",
"portable-atomic",
]
[[package]]
name = "usbd-hid"
version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e6f291ab53d428685cc780f08a2eb9d5d6ff58622db2b36e239a4f715f1e184c"
dependencies = [
"serde 1.0.204",
"ssmarshal",
"usb-device 0.3.2",
"usbd-hid-macros",
]
[[package]]
name = "usbd-hid-descriptors"
version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0eee54712c5d778d2fb2da43b1ce5a7b5060886ef7b09891baeb4bf36910a3ed"
dependencies = [
"bitfield 0.14.0",
]
[[package]]
name = "usbd-hid-macros"
version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bb573c76e7884035ac5e1ab4a81234c187a82b6100140af0ab45757650ccda38"
dependencies = [
"byteorder",
"hashbrown 0.13.2",
"log 0.4.22",
"proc-macro2",
"quote",
"serde 1.0.204",
"syn 1.0.109",
"usbd-hid-descriptors",
]
[[package]]
name = "utf-8"
version = "0.7.6"

View File

@ -30,5 +30,5 @@ members = [
"sgf",
"timezone-testing",
"tree",
"visions/server", "pi-usb-serial",
]
"visions/server", "pi-usb-serial", "tft"
, "serial-comm"]

View File

@ -10,9 +10,12 @@ embassy-sync = { version = "0.6.0", features = ["defmt"] }
embassy-executor = { version = "0.5.0", features = ["task-arena-size-98304", "arch-cortex-m", "executor-thread", "executor-interrupt", "defmt", "integrated-timers"] }
embassy-time = { version = "0.3.1", features = ["defmt", "defmt-timestamp-uptime"] }
embassy-rp = { version = "0.1.0", features = ["defmt", "unstable-pac", "time-driver", "critical-section-impl"] }
embassy-usb = { version = "0.3.0" }
defmt = "0.3"
defmt-rtt = "0.4"
cortex-m-rt = "0.7.0"
panic-probe = { version = "0.3", features = ["print-defmt"] }
portable-atomic = { version = "1.5", features = ["critical-section"] }
static_cell = "2"

View File

@ -5,26 +5,108 @@
#![no_std]
#![no_main]
use embassy_usb::{Builder, Config, class::cdc_acm::{CdcAcmClass, State}, UsbDevice, driver::EndpointError};
use embassy_executor::Spawner;
use embassy_rp::gpio;
use embassy_rp::{bind_interrupts, gpio, peripherals::USB, usb::{Driver, Instance, InterruptHandler}};
use embassy_time::Timer;
use gpio::{Level, Output};
use defmt::*;
use defmt::{info, panic, unwrap};
use defmt_rtt as _;
use panic_probe as _;
use static_cell::StaticCell;
bind_interrupts!(struct Irqs {
USBCTRL_IRQ => InterruptHandler<USB>;
});
#[embassy_executor::task]
async fn usb_task(mut usb: UsbDevice<'static, Driver<'static, USB>>) {
usb.run().await
}
#[embassy_executor::main]
async fn main(_spawner: Spawner) {
async fn main(spawner: Spawner) {
let p = embassy_rp::init(Default::default());
let mut led = Output::new(p.PIN_25, Level::Low);
let mut led = Output::new(p.PIN_15, Level::Low);
let driver = embassy_rp::usb::Driver::new(p.USB, Irqs);
let mut config = Config::new(0x9988, 0x8899);
config.manufacturer = Some("Savanni");
config.product = Some("USB test device");
config.serial_number = Some("abcdefg");
config.max_power = 100;
config.max_packet_size_0 = 64;
let mut 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 mut builder = Builder::new(
driver,
config,
CONFIG_DESCRIPTOR.init([0; 256]),
BOS_DESCRIPTOR.init([0; 256]),
&mut [],
CONTROL_BUF.init([0; 64]),
);
builder
};
let mut class = {
static STATE: StaticCell<State> = StaticCell::new();
let state = STATE.init(State::new());
CdcAcmClass::new(&mut builder, state, 64)
};
let usb = builder.build();
unwrap!(spawner.spawn(usb_task(usb)));
loop {
info!("led on!");
class.wait_connection().await;
led.set_high();
Timer::after_secs(1).await;
info!("led off!");
info!("Connected");
// let _ = echo(&mut class).await;
let _ = primes(&mut class).await;
info!("Disconnected");
led.set_low();
Timer::after_secs(1).await;
}
}
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<'d, T: Instance + 'd>(class: &mut CdcAcmClass<'d, Driver<'d, T>>) -> Result<(), Disconnected> {
let mut buf = [0; 64];
loop {
let n = class.read_packet(&mut buf).await?;
buf[n] = b'\r';
buf[n+1] = b'\n';
let data = &buf[..n+2];
info!("data: {:x}", data);
class.write_packet(&data).await?;
}
}
async fn primes<'d, T: Instance + 'd>(class: &mut CdcAcmClass<'d, Driver<'d, T>>) -> Result<(), Disconnected> {
let PRIMES: [u8; 55] = [1, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251];
let mut buf = [0; 64];
loop {
for idx in 0..PRIMES.len() {
buf[0] = PRIMES[idx];
class.write_packet(&buf).await?;
Timer::after_secs(1).await;
};
}
}

8
serial-comm/Cargo.toml Normal file
View File

@ -0,0 +1,8 @@
[package]
name = "serial-comm"
version = "0.1.0"
edition = "2021"
[dependencies]
tokio = { version = "1.39.2", features = ["full"] }
tokio-serial = "5.4.4"

22
serial-comm/src/main.rs Normal file
View File

@ -0,0 +1,22 @@
use tokio_serial::{DataBits, StopBits, Parity, SerialStream};
#[tokio::main]
async fn main() {
let mut port = SerialStream::open(&tokio_serial::new("/dev/ttyACM0", 115200).parity(Parity::None).stop_bits(StopBits::One).data_bits(DataBits::Eight)).expect("serial port to open");
/*
port.writable().await.expect("writeable to succeed");
let bytes = port.try_write(b"abcdefg").expect("write to succeed");
println!("{} bytes written", bytes);
tokio::time::sleep(std::time::Duration::from_millis(1)).await;
*/
loop {
port.readable().await.expect("readable to succeed");
let mut buf: [u8; 64]= [0; 64];
if let Ok(_) = port.try_read(&mut buf) {
println!("{:?}", &buf);
}
}
}