#no-std #usb-device

no-std dev usbd-serial

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

2 releases

0.1.1 Oct 3, 2020
0.1.0 Jul 24, 2019

#125 in Hardware support

Download history 2389/week @ 2022-12-01 2278/week @ 2022-12-08 1428/week @ 2022-12-15 2250/week @ 2022-12-22 2277/week @ 2022-12-29 1686/week @ 2023-01-05 2175/week @ 2023-01-12 1432/week @ 2023-01-19 977/week @ 2023-01-26 1385/week @ 2023-02-02 892/week @ 2023-02-09 2988/week @ 2023-02-16 2692/week @ 2023-02-23 2085/week @ 2023-03-02 1465/week @ 2023-03-09 1536/week @ 2023-03-16

8,192 downloads per month
Used in 48 crates (45 directly)

MIT license

29KB
558 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

~170KB