• 1.0.0 f8214f6672

    doryan released this 2024-12-30 22:32:48 +03:00 | 0 commits to main since this release

    1.0.0

    It's first release of my implementation UsbBus trait from usb_device.

    Features

    This library has main functionality and can works with last Rust release and with usb_d class crates.

    Code example:

    #![no_std]
    #![no_main]
    #![feature(abi_avr_interrupt)]
    #![deny(unsafe_op_in_unsafe_fn)]
    
    use avr_device::{asm::sleep, interrupt};
    
    use panic_halt as _;
    
    use usb_avr::UsbDevice;
    use usb_device::{
        bus::UsbBusAllocator,
        device::{StringDescriptors, UsbDevice as UsbD, UsbDeviceBuilder, UsbVidPid},
        LangID,
    };
    
    use usbd_hid::{
        descriptor::{KeyboardReport, SerializedDescriptor},
        hid_class::HIDClass,
    };
    
    const L: usize = 7; // max count of endpoints, maybe I will remove in next releases
    
    #[arduino_hal::entry]
    fn main() -> ! {
        let dp = arduino_hal::Peripherals::take().unwrap();
        let (pll, usb) = (dp.PLL, dp.USB_DEVICE);
    
        let usb_bus = unsafe {
            static mut USB_BUS: Option<UsbBusAllocator<UsbDevice<L>>> = None;
            &*USB_BUS.insert(UsbDevice::new(pll, usb))
        };
    
        let hid_class: HIDClass<'static, UsbDevice<L>> =
            HIDClass::new(usb_bus, KeyboardReport::desc(), 1);
    
        let usb_dev: UsbD<'static, UsbDevice<L>> =
            UsbDeviceBuilder::new(usb_bus, UsbVidPid(0x5824, 0x27dd))
                .strings(&[StringDescriptors::new(LangID::EN)
                    .product("keyboard")
                    .manufacturer("doryan")
                    .serial_number("TEST")])
                .expect("Failed to set string")
                .build();
    
        unsafe {
            CTX = Some(Ctx {
                ctx: usb_dev,
                hid: hid_class,
            });
        }
    
        unsafe {
            interrupt::enable();
        }
    
        loop {
            sleep();
        }
    }
    
    static mut CTX: Option<Ctx<L>> = None;
    
    #[interrupt(atmega32u4)]
    fn USB_GEN() {
        unsafe {
            usb_polling();
        }
    }
    
    #[interrupt(atmega32u4)]
    fn USB_COM() {
        unsafe {
            usb_polling();
        }
    }
    
    unsafe fn usb_polling() {
        let ctx = unsafe { CTX.as_mut().unwrap() };
        ctx.poll();
    }
    
    struct Ctx<const L: usize> {
        ctx: UsbD<'static, UsbDevice<L>>,
        hid: HIDClass<'static, UsbDevice<L>>,
    }
    
    impl<const L: usize> Ctx<L> {
        fn poll(&mut self) {
            self.ctx.poll(&mut [&mut self.hid]);
        }
    }
    
    Downloads