dmitrystu / libusb_stm32

Lightweight USB device Stack for STM32 microcontrollers
Apache License 2.0
713 stars 163 forks source link

Interrupt example? #8

Closed kisielk closed 6 years ago

kisielk commented 6 years ago

First of all, thanks for writing this library. I'm seriously considering using it over the official ST libs which are a pretty big mess when it comes to USB especially.

Do you have a code example of the library with interrupts? The demo seems to use polling, unless I'm missing something...

dmitrystu commented 6 years ago

It's easy. Enable USB IRQ and use usbd_poll(&udev) in the IRQ handler.

void USB_IRQHandler(void) {
    usbd_poll(&udev);
}

It worked at least for L052 and L100. I'll add a code example uses interrupts to CDC loop demo.

kisielk commented 6 years ago

Simple enough. Do you happen to have a DFU example somewhere? I'm doing a project with the L052 and discovered the embedded bootloader doesn't have USB DFU, unlike most other ST chips with USB, so I'm looking at rolling my own.

dmitrystu commented 6 years ago

About IRQ: Just checked on bluepill. It's works. About DFU: Is this project good for you ?

kisielk commented 6 years ago

Oh yeah, that's perfect! :) Seems you've done most of the work for me! I'll give it a try.

xcvista commented 6 years ago

https://github.com/SushiBits/DAP42-Firmware/blob/master/src/usb-device.c

Here is my example, with the USB started during C runtime initialization, and interrupt driven programming. My main file is simply a line of for (;;) __WFE();.

adadurov commented 4 years ago

Anyone tested the interrupt mode with bluepill? Recently, it didn't work for me.

dmitrystu commented 4 years ago

See no problems with bluepill (STM32F103C8T6) for both interrupt and non-interrupt builds. Since it uses fixed DP pullup, device reconnect may be required to force re-enumeration.

adadurov commented 4 years ago

Dmitrystu, many thanks for the quick response. I'm pretty sure I attempted reconnection a few times, but will give it yet another try. By the way, can you share some insights on how you do debugging of USB code? What are the tools or approaches that you use? I did some USB debugging a few years ago on ATMEGA by logging to a serial port and then analyzing the execution path. Do you know a better/more efficient way to do that?

xcvista commented 4 years ago

I would suggest USB packet sniffing with Wireshark as a beginner.

dmitrystu commented 4 years ago

@adadurov Is non-interrupt build working for you? Since I'm under Linux I'm using the following toolset:

adadurov commented 4 years ago

@dmitrystu , thanks for sharing the details of your debugging environment. Yes, the non-interrupt build for BluePill worked out of the box on my BP device.

xcvista commented 4 years ago

@adadurov I use macOS and Window 10, so my stack is: