mirror of
https://github.com/esp-rs/esp-hal.git
synced 2025-09-27 04:10:28 +00:00
150 lines
4.2 KiB
Rust
150 lines
4.2 KiB
Rust
//! CDC-ACM serial port example using embassy.
|
|
//!
|
|
//! This example should be built in release mode.
|
|
//!
|
|
//! The following wiring is assumed:
|
|
//! - DP => GPIO20
|
|
//! - DM => GPIO19
|
|
|
|
//% CHIPS: esp32s2 esp32s3
|
|
//% FEATURES: async embassy embassy-generic-timers
|
|
|
|
#![no_std]
|
|
#![no_main]
|
|
|
|
use embassy_executor::Spawner;
|
|
use embassy_futures::join::join;
|
|
use embassy_usb::{
|
|
class::cdc_acm::{CdcAcmClass, State},
|
|
driver::EndpointError,
|
|
Builder,
|
|
};
|
|
use esp_backtrace as _;
|
|
use esp_hal::{
|
|
clock::ClockControl,
|
|
gpio::Io,
|
|
otg_fs::{
|
|
asynch::{Config, Driver},
|
|
Usb,
|
|
},
|
|
peripherals::Peripherals,
|
|
system::SystemControl,
|
|
timer::{timg::TimerGroup, ErasedTimer, OneShotTimer},
|
|
};
|
|
|
|
// When you are okay with using a nightly compiler it's better to use https://docs.rs/static_cell/2.1.0/static_cell/macro.make_static.html
|
|
macro_rules! mk_static {
|
|
($t:ty,$val:expr) => {{
|
|
static STATIC_CELL: static_cell::StaticCell<$t> = static_cell::StaticCell::new();
|
|
#[deny(unused_attributes)]
|
|
let x = STATIC_CELL.uninit().write(($val));
|
|
x
|
|
}};
|
|
}
|
|
|
|
#[esp_hal_embassy::main]
|
|
async fn main(_spawner: Spawner) -> () {
|
|
esp_println::println!("Init!");
|
|
let peripherals = Peripherals::take();
|
|
let system = SystemControl::new(peripherals.SYSTEM);
|
|
let clocks = ClockControl::boot_defaults(system.clock_control).freeze();
|
|
|
|
let timg0 = TimerGroup::new(peripherals.TIMG0, &clocks);
|
|
esp_hal_embassy::init(
|
|
&clocks,
|
|
mk_static!(
|
|
[OneShotTimer<ErasedTimer>; 1],
|
|
[OneShotTimer::new(timg0.timer0.into())]
|
|
),
|
|
);
|
|
|
|
let io = Io::new(peripherals.GPIO, peripherals.IO_MUX);
|
|
|
|
let usb = Usb::new(peripherals.USB0, io.pins.gpio20, io.pins.gpio19);
|
|
|
|
// Create the driver, from the HAL.
|
|
let mut ep_out_buffer = [0u8; 1024];
|
|
let config = Config::default();
|
|
|
|
let driver = Driver::new(usb, &mut ep_out_buffer, config);
|
|
|
|
// Create embassy-usb Config
|
|
let mut config = embassy_usb::Config::new(0x303A, 0x3001);
|
|
config.manufacturer = Some("Espressif");
|
|
config.product = Some("USB-serial example");
|
|
config.serial_number = Some("12345678");
|
|
|
|
// Required for windows compatibility.
|
|
// https://developer.nordicsemi.com/nRF_Connect_SDK/doc/1.9.1/kconfig/CONFIG_CDC_ACM_IAD.html#help
|
|
config.device_class = 0xEF;
|
|
config.device_sub_class = 0x02;
|
|
config.device_protocol = 0x01;
|
|
config.composite_with_iads = true;
|
|
|
|
// Create embassy-usb DeviceBuilder using the driver and config.
|
|
// It needs some buffers for building the descriptors.
|
|
let mut config_descriptor = [0; 256];
|
|
let mut bos_descriptor = [0; 256];
|
|
let mut control_buf = [0; 64];
|
|
|
|
let mut state = State::new();
|
|
|
|
let mut builder = Builder::new(
|
|
driver,
|
|
config,
|
|
&mut config_descriptor,
|
|
&mut bos_descriptor,
|
|
&mut [], // no msos descriptors
|
|
&mut control_buf,
|
|
);
|
|
|
|
// Create classes on the builder.
|
|
let mut class = CdcAcmClass::new(&mut builder, &mut state, 64);
|
|
|
|
// Build the builder.
|
|
let mut usb = builder.build();
|
|
|
|
// Run the USB device.
|
|
let usb_fut = usb.run();
|
|
|
|
// Do stuff with the class!
|
|
let echo_fut = async {
|
|
loop {
|
|
class.wait_connection().await;
|
|
esp_println::println!("Connected");
|
|
let _ = echo(&mut class).await;
|
|
esp_println::println!("Disconnected");
|
|
}
|
|
};
|
|
|
|
// Run everything concurrently.
|
|
// If we had made everything `'static` above instead, we could do this using separate tasks instead.
|
|
join(usb_fut, echo_fut).await;
|
|
}
|
|
|
|
async fn echo<'d>(class: &mut CdcAcmClass<'d, Driver<'d>>) -> Result<(), Disconnected> {
|
|
let mut buf = [0; 64];
|
|
loop {
|
|
let n = class.read_packet(&mut buf).await?;
|
|
// Echo back in upper case
|
|
for c in buf[0..n].iter_mut() {
|
|
if 0x61 <= *c && *c <= 0x7a {
|
|
*c &= !0x20;
|
|
}
|
|
}
|
|
let data = &buf[..n];
|
|
class.write_packet(data).await?;
|
|
}
|
|
}
|
|
|
|
struct Disconnected {}
|
|
|
|
impl From<EndpointError> for Disconnected {
|
|
fn from(val: EndpointError) -> Self {
|
|
match val {
|
|
EndpointError::BufferOverflow => panic!("Buffer overflow"),
|
|
EndpointError::Disabled => Disconnected {},
|
|
}
|
|
}
|
|
}
|