#no-std #usb-device

no-std usbd-serial

USB CDC-ACM serial port class for use with usb-device

1 unstable release

✓ Uses Rust 2018 edition

0.1.0 Jul 24, 2019

#309 in Encoding

Download history 118/week @ 2019-12-02 41/week @ 2019-12-09 68/week @ 2019-12-16 42/week @ 2019-12-23 33/week @ 2019-12-30 91/week @ 2020-01-06 117/week @ 2020-01-13 70/week @ 2020-01-20 60/week @ 2020-01-27 91/week @ 2020-02-03 127/week @ 2020-02-10 77/week @ 2020-02-17 31/week @ 2020-02-24 178/week @ 2020-03-02 107/week @ 2020-03-09 100/week @ 2020-03-16

214 downloads per month
Used in 12 crates

MIT license

29KB
552 lines

usbd-serial

CDC-ACM USB serial port implementation for usb-device.

CDC-ACM is a USB class that's supported out of the box by most operating systems and used for implementing modems and generic serial ports. The SerialPort class implements a stream-like buffered serial port that can be used similarly to a normal UART.

The crate also contains CdcAcmClass which is a lower-level implementation that has less overhead, but requires more care to use correctly.

Example

A full example requires the use of a hardware-driver, but the hardware independent part is as follows:

let mut serial = SerialPort::new(&usb_bus);

let mut usb_dev = UsbDeviceBuilder::new(&usb_bus, UsbVidPid(0x16c0, 0x27dd))
    .product("Serial port")
    .device_class(USB_CLASS_CDC)
    .build();

loop {
    if !usb_dev.poll(&mut [&mut serial]) {
        continue;
    }

    let mut buf = [0u8; 64];

    match serial.read(&mut buf[..]) {
        Ok(count) => {
            // count bytes were read to &buf[..count]
        },
        Err(UsbError::WouldBlock) => // No data received
        Err(err) => // An error occurred
    };

    match serial.write(&[0x3a, 0x29]) {
        Ok(count) => {
            // count bytes were written
        },
        Err(UsbError::WouldBlock) => // No data could be written (buffers full)
        Err(err) => // An error occurred
    };
}

Dependencies

~190KB