4 ms·
Making USB devices – end to end guide to your first gadget
- bofew59301 2y ago[dead]
- foz 2y agoA very nice writeup and intro into USB, although - it's very focused on using an ST microcontroller, which is a lot more steps and toolchains compared the the recent ESP32 ecosystem that offers a number of easy plug-and-play ways to make USB devices work (for example, several projects from Adafruit's learning pages provide basic use cases). Another issue is differential pairs - having designed several working USB boards myself, this has not a concern for beginners, mainly relevant for doing high-speed work. USB controller ICs (as used typically with arduio/esp) can handle a lot of the details for you, so doing the calculations seems overkill, especially for someone making their first gadget.
- lemonlime0x3C33 2y agoI think it is nice to have a basic understanding of differential pairs and impedance, I will admit I never really had to do the calculations since the ecad software I use has several tools for routing differential pairs and even analyzing signals for impedance. But as long as you keep your traces really short it normally doesn't matter.
- vgalin 2y agoI recently built a small hand-wired macro pad using an Arduino Pro Micro equipped with ATmega32U4, it's apparently quite popular amongst hobbyists building custom keyboards. Quick and fun project for a beginner, the most tedious part of this project was to carve the wooden case.
- stavros 2y agoDo you have a link to a PCB/kit, preferably with LEDs? Does yours run QMK? It sounds like a nice project if it costs a few bucks to get the materials, though I don't know what I'd do with it.
- vgalin 2y agoI didn't used a kit, but bought the components separately (microcontroller, key caps, switches, wires). It runs custom code as it was sufficient to fit my use case, though QMK may be possible? The keys are mapped from F13 to F22 and can be use in some software that allow custom keybinds.
- stavros 2y agoThis is great, thanks! I had researched extra F keys when I was making my keyboard (exactly to use as macros) but I hadn't discovered F13 to F22, thank you! That's exactly what I needed.
- fulladder 2y ago> USB controller ICs (as used typically with arduio/esp) can handle a lot of the details for you Can anyone recommend any good USB controller ICs? I normally just use a microcontroller with USB built in.
- lemonlime0x3C33 2y agoI have had a good experience using microchips USB2514 and USB2512
- stragies 2y agoI just looked up the USB2514, and it would seem to not support PerPortPowerSwitching. (used by e.g. uhubctrl to toggle/switch power to individual USB ports). Am I not reading the specs correctly?
- lemonlime0x3C33 2y agoI was able to reset downstream USB devices using usbreset to the individual ports (or the entire hub) in my design, using usbreset. I did also have a hardware reset built in using the cfg_sel0 and cfg_sel1 pins 24 & 25 and RESET_N. I did not try to turn off power to those ports for any extended time so I am not sure if that works.
- arrw 2y agoVery cool guide. But if you are worried about soldering more fiddly stuff like those ARM processors: It doesn't have to be that big, STM32 is nice if you need the power, but for smaller stuff smaller controllers can be preferrable. E.g. one may also consider using VUSB, which is a library that bit-bangs USB on small Atmel microcontrollers: https://www.obdev.at/products/vusb/index.html https://www.obdev.at/products/vusb/index.html Example board schematic we've used to teach students Linux kernel module programming: https://gitlab.cs.fau.de/i4/passt/passtboard-v2 https://gitlab.cs.fau.de/i4/passt/passtboard-v2 with firmware http://www.poempelfox.de/ds1820tousb/ http://www.poempelfox.de/ds1820tousb/ and https://gitlab.cs.fau.de/i4/passt/ds1820tousb https://gitlab.cs.fau.de/i4/passt/ds1820tousb Also very easy, if you are inclined towards Arduino style programming there are tons of boards you can just use as USB devices with the included libraries in very few lines of code, for example https://www.az-delivery.de/en/products/digispark-board https://www.az-delivery.de/en/products/digispark-board
- unwind 2y agoThere's also things like the CH32V203 [1] which is a TSSOP-20 with hardware USB and costs around $0.81 in singles. The software side might be a little more ... challenging, though. https://www.wch-ic.com/products/CH32V203.html https://www.wch-ic.com/products/CH32V203.html?
- 15155 2y agoCH32F series is more or less the same and actually has CoreSight SWD and clone STM peripherals
- zoobab 2y agoSoftware USB can also be done on the 10 cents CH32V003: https://github.com/cnlohr/rv003usb https://github.com/cnlohr/rv003usb
- hakfoo 2y agoThey offer some fairly usable example reference code for a lot of the on-chip functionality-- including USB peripherals. I was able to turn their CH32v3xx examples into a pretty capable custom-keyboard firmware.
- lemonlime0x3C33 2y agoThis is a very well written and informative article for working with USB, wish I had it when I first started USB signals.
- dvh 2y agoI recently used CH340N (soic 8 virtual com port) and it was really easy. Easy soldering, no extra parts just directly connect to USB, works out of the box on Linux.
- anymouse123456 2y agoFriendly reminder that USB is not free. You must pay a one-time fee of $6,000 in exchange Vendor ID [0]. [0] https://www.usb.org/getting-vendor-id https://www.usb.org/getting-vendor-id
- rm445 2y agoYeah, I was going to ask about this. I think ST Micro will sublicense you a PID under their VID, but with restrictions, and companies attempting to buy a VID and sell PIDs as a service have been sued?
- zoobab 2y agoSee also https://pid.codes/ https://pid.codes/ For the DirtyJTAG project we use one of those: https://pid.codes/1209/C0CA/ https://pid.codes/1209/C0CA/
- pjc50 2y ago.. or use someone else's. I believe there's a hobbyist VID under which you can reserve PIDs, or in many cases you can just copy an existing product if your device is generic enough and you're not planning to sell a big production run.
- hananova 2y agoThere's pid.codes which uses a vid of a defunct organization, but before that there was also the F055 vid. I still use that vid because no company will accept that vid, since it's commonly used by foss projects.
- brezelgoring 2y agoWow, first time I hear about it, this is an eye opening experience. I will think twice now before saying “why couldn’t they just use USB for this?”
- kraskoss 2y agoEven crazier, it's just a 16 bit value. What are they gonna do when they run out?
- kevmo314 2y agoAre there any development boards with USB 3 support? I'm trying to prototype a USB C monitor sink but having trouble finding a board that has the power necessary to receive DisplayPort over USB.
- dfox 2y agoThere is this: https://octopart.com/cyusb3kit-003-cypress+semiconductor-49426799 https://octopart.com/cyusb3kit-003-cypress+semiconductor-494... and the pricing seems reasonable. When I looked for something like that two years ago the prices were order of magnitude higher.
- BertoldVdb 2y agoThis device is not really suited, it is a high speed USB3.0 peripheral. You might be able to use it with the right software, but then you are just using the CPU in it.
- dfox 2y agoThe way how the Cypress FX series works (and for that matter majority of non-trivial USB-to-something chips do) is that there is a bunch of DMA-capable peripherals inside and the in fact ridiculously underpowered CPU core inside only sets up the required configuration for the DMA transactions to happen.
- BertoldVdb 2y agoThere is not much power needed to receive DisplayPort over USB (assuming you already can receive displayport signals or just route them to an external monitor). You just need to implement an USB billboard device (optional to make it work, but required by the spec IIRC) and signal the correct alternate function. Then DisplayPort signalling will be present on the USB-C plug. Then just connect the right AUX wires to the DisplayPort connector.
- 15155 2y agoMost FPGAs with transceivers can do it.
- perbu 2y agofwiw; I've prototyped some USB gadgets using Raspberry Pi Zeroes and Composite USB in the Linux Kernel. At least storage and serial devices were pretty easy to get going. You'll need something like a shell script on the Raspberry to initialize the composite kernel module and you'll find the boildplate in the kernel docs.
- m463 2y agoI would love to play with pis and make virtual usb devices, like a webcam. pikvm is an interesting project. It will hook to a PC, and the USB connection can not only pretend to be a keyboard and mouse, but it can be a USB drive that you can boot the system with. Pretty interesting for installs.
- the__alchemist 2y agoA note on USB-C, to augment the article's note: Wire the CC pins to appropriate resistors, or it probably won't work! And a note on the differential routing and impedance: For USB 2.0, it's not a big deal. Keep the traces of similar length, and reasonably direct. Probably next to each other. You probably don't need to worry about fine-tuning the length and trace widths, impedance control, RF best practices etc. Just connect the nets.
- jmole 2y ago480MBit/s is fast enough that you do need to worry about best practices and impedance matching within 10% or so. Many MCUs will also need series resistors between the USB PHY pins and the connector. Will you need to respin a board because you needed to use a 20 mil trace instead of 24 mil? Probably not. All things considered, laying out a USB 2.0 differential pair is pretty low stakes. But you should still try and do it right - it's good practice.
- dnpls 2y agowhat about converting existing devices from USB-A to USB-C? I have a bunch of powerbanks lying around that I'd love to keep using but most of my devices now use a USB-C charging cable, would it be a simple case of replacing the ports and re-soldering or is there any extra component I need to add?
- the__alchemist 2y agoI would use an adapter for that use case. The wiring of the ports on a new design is straightforward; you are using the same pins if it's USB 2.0 (As the article says), plus two extra connections that go to a resistors. If you can find ports that use the same footprint, feasible, but probably not worth it. Desoldering a port is a pain because you have to get all the pins to melting temperature concurrently, and you'd have to figure out how to wire those CC pins.
- the__alchemist 2y agoOK, dumb question, but since we're talking USB on STM32: Does anyone know how to support receiving more than 64 bytes in a frame? I've been looping over 64 byte frames in software, but I know it's possible to go higher. (Up to 1Mbyte I think). The problem is, the Reference Manual lists settings for this that are not normal registers. They are a pseudo-register of some sort. Wondering if there is an easy workaround for this! (The non-OTG USB peripheral)
- genter 2y agoUSB full speed bulk transfer is limited to 64 bytes. (Dunno which stm32 you're talking about, but stm32g4 is only full speed.)
- jmole 2y agoA single BULK USB endpoint can only support 64 bytes per transfer. You could make use of multiple endpoints (8 are available in total) or switch to isochronous endpoints which can support up to 1023 bytes per frame. Most hardware support for isochronous transfers requires DMA on the MCU side, so it tends to be a pain unless your vendor has a library that handles it for you. You can in general send up to 19 bulk transfers in a single frame (even on a single endpoint), but again, vendor libraries differ wildly in their support for this.
- AdamH12113 2y agoNote that isochronous transfers require kernel-mode drivers on the host side, so you won't be able to use libusb in that case. Bulk transfers are the way to go if you want high throughput.
- the__alchemist 2y agoIsn't the restriction just on the reception side? So, if you have a MCU talking to a PC or another MCU, you can send however many bytes you want, but not receive? I say this because A: The Reference Manual only indicates this limit for reception, and B: I only experienced this on reception: PCs seem capable of sending and receiving messages larger than 64-bytes, and STM32s seem capable of sending messages larger than 64-bytes, but not receiving (without isosynchronous, or anything special)
- petabyt 2y agoI've been using https://github.com/xairy/raw-gadget https://github.com/xairy/raw-gadget to create a virtual USB device on my raspberry pi I've got plugged into my PC. I'm currently using It to emulate a MTP camera so I can spoof some proprietary software.
- worthless-trash 2y agoYou gotta give us more details than that! Sounds like fun!
- petabyt 2y agoMost of the details are here: https://github.com/petabyt/vcam https://github.com/petabyt/vcam With the raw gadget code in src/otg.c It hasn't only just been fun, it's also proven to be a big help with an Android app I've been working on :)
- worthless-trash 2y agoThat is so very cool, thank you!
- _spduchamp 2y agoI use ESP32 mostly but I have a cheap hack that fits a lot of projects... For super quick easy custom controllers, also consider pulling the control board from discarded USB keyboards. Use conductive glue instead of solder to attach wires to the contacts, and a helping of hot glue to keep them secured. I've made cheap but very robust 1-button game controllers with an arcade button that sends a space bar click. You get all the debounce etc, no code.
- mcshicks 2y agoI used to test devices for USB compliance a long time ago. One of the things I often saw problems with was the inrush current test. Basically too much bypass caps on the 5v. I didn't see it mentioned in the article. It's really easy to get focused on the high speed digital design but for compliance it's sometimes the less sexy stuff that gets you. No idea how this stuff works with newer versions of the standard but it seems like there still is a test. Nice article though. https://compliance.usb.org/index.asp?UpdateFile=Electrical#:~:text=Inrush%20current%20is%20measured%20for,of%20the%20inrush%20current%20event https://compliance.usb.org/index.asp?UpdateFile=Electrical#:....
- amelius 2y agoWhat devices do people use to limit inrush current? You can make a current limiter with a few transistors, but I'm guessing there must be better integrated solutions (e.g. with temperature protection, etc.)
- mcshicks 2y agoYou can use inductors. Some USB cables have a choke on them. But basically the problem is when a large amount of circuitry all powers up at one when you plug in. So everything connected directly to USB 5v with a lot of large capacitors to filter the power. If you really need that then you can self power and avoid the problem.
- amelius 2y agoI'm looking more for a solution that acts as a constant current source, but where the voltage never exceeds the input voltage.
- tomooot 2y agoAMC7135 might work: it's a linear LDO fixed current regulator designed for driving LEDs, which internally is basically an N-channel MOSFET which holds it's gate voltage in the linear region and actively adjusts it to provide constant current.
- AdamH12113 2y agoIve written bare-metal USB code on an MCU before, and I found it to be quite a shock compared to simple digital protocols like SPI or I2C. The physical and data link layers aren't much more complicated than, say, CAN, but beyond that you immediately run into a brick wall of descriptors, endpoints, and driver configuration. USB was designed from the ground up to be a plug-and-play ecosystem for PCs, and it really shows. Using the vendor-provided software as much as possible is definitely the way to go if you can. Some half-remembered hints: You want bulk transfers for high throughput (don't even look at isochronous). USB is a master/slave protocol so if you're not getting peak throughput it's usually due to something on the host (PC) side. If the license (LGPL) is compatible with your needs, libusb is pretty easy to use. If you're not using the vendor driver, a hardware USB protocol analyzer is really helpful. USB in a NutShell[1] is a decent web reference for understanding the protocol. [1] https://www.beyondlogic.org/usbnutshell/usb1.shtml https://www.beyondlogic.org/usbnutshell/usb1.shtml