ALeggeUp's USB DFU and HID "Blinky" Build Log

The pandemic got me all out of routine, I stopped listening to my podcasts because I wasn’t driving anywhere and with working from home and home schooling I just didn’t have the motivation to do anything else. Now that I’m driving more, I’m binge catching up on the podcasts and realizing how much I’ve missed and getting “the itch” again.

I don’t really have a specific need for a custom board, but listening to what the AND!XOR guys did in a year got me thinking that I could just make something fun for myself, so I decided to just start with some building blocks that I might need if I was going to make something like a badge.

First up, I just want to make a board that has a USB-C connector, a button and an LED so that I can plug it in to the computer, load the firmware on with DFU and have an HID device show up that I can see when the button is pressed and blink the LED.

1 Like

Hi @ALeggeUp, welcome back! I know that feeling of missing podcasts on the road.

What chip(s) do you have on the shortlist for your USB HID gadget?

1 Like

Thanks!

I haven’t narrowed it down too far, I want to give myself the best chance of success. I’m looking at something in the STM32 Cortex-M, Atmel SAMD or possibly Nordic families, actually now that I type that I haven’t narrowed it down at all, I guess that’s step 1!

I’m open to suggestions.

Well all good options. Parts availability aside (because for one-offs you can pretty much get most things), I’d offer two things to help you choose. Firstly, if you think you will want to go on to develop a full keyboard, it may be worth looking at what chips are supported by https://qmk.fm/. Also, TinyUSB has HID and because it works on several of the chips you mention, it may be worth investing in learning a bit about TinyUSB and targeting one of the chips it runs on. It should be fine for a macro keypad but there’s probably quite a lot of work to get it working as a full keyboard (although you never know!). TinyUSB is available as a USB stack option for many Adafruit boards, so you could try it without needing to make your custom hardware just yet.

1 Like

I’m not too interested in making a full keyboard, but I only just realized that being able to load firmware on a microcontroller without needing a USB to serial device was even possible and now looking back I feel silly for missing that.

Good point on the availability, looks like none of the Nordic chips that TinyUSB supports are available, so that might help narrow things down.

Also great point on the Adafruit option, but one of the reasons I stalled on my hardware progress was that the Adafruit stuff is so convenient that anything I wanted to do was too easy to just buy a few parts from them and then I’m just writing software and I already do that for a day job so it doesn’t really scratch the itch.

Maybe I need to re-think what I want to get out of it, I think I’m just chasing that “Getting to Blinky” rush.

No need to feel embarrassed; things clicking into place that had never previously occurred is a core part of enjoying electronics!

Do you have a budget for programming tools? The ATMEL ICE programmer (needed for SAMD series) can seem like a bit of an investment. Whereas ST chips can be programmed by ST Links, either available as a sub-system on a dev board or a standalone (and clones are very cheap).

If a USB peripheral is a must have, then choices are pretty much as you’ve covered (although Raspi RP2040 may be worth a look too). If you can get your dopamine hit from other aspects of blinking, there are tonnes of options. I really like the new Microchip ATtiny 0 and 1 series chips. They are supported in the Arduino IDE but with a load of “baremetal” selectable options (check out the support repo). You don’t need external crystals (a single decoupling cap will do) and they run 10MHz at 3.3V+ or 20MHz at 4.5V+ ('cos they’re 5V tolerant and run down to 1.8V, so perfect for a pair of AA or a single lithium cell - I often use them with supercaps and photovoltaics!). They have some great little peripherals like the Event System (think simple interrupts that don’t even need CPU to process an ISR), custom configurable logic (logic gates), ability to measure their own supply voltage without external components, fast IO read and writes, low power 32kHz timer. They also come in hand-solderable SOIC packages or VQFN for more of a challenge (and more IO). Also, if you write code for them that you like, much of it is easily ported to the newer ATMEGA chips like the ATMEGA4809 that is in one of the newer Arduinos, as well as the AVRnnDX series that run faster (32 MHz). You can program an ATtiny 0 or 1 series chip (as well as ATMEGA4809 etc) with an Arduino Nano (or clone, running jtag2updi sketch) and a resistor, so that’s a pretty low bar to entry.

Sorry if that sounded like a hard sell but for playing around, they are great fun. For more ideas about what you can do with them, check out David Johnson-Davies’ blog where he has several interesting posts about them.

1 Like

Yes, but I thought the whole point of DFU is that I wouldn’t need a sperate programmer, I thought the point was to be able to just use USB. Am I misunderstanding?

Only because I thought that would be a fun project and would make future projects easier. I’ll add RP2040 to the list to see if it will fit the bill, I’m striking out on finding chips with USB, I didn’t realize the shortage was this bad.

Not at all! I love all the info, but I’ve played around with lots of dev boards that I’ve had to program with JTAG or USART and that’s what I’m trying to avoid. I think being able to plug in a board I’ve made into the computer and be able to use it directly like that would be a huge win for me.

Gotcha - unless you find a source with a bootloader installed, I think you need at least a first-time SWD / SPI or similar programming interface before you can use the USB DFU mode. If you don’t plan on doing anything too memory-intensive you could try a SAMD11 (in stock at Digikey) but I don’t know how keen you are for an ICE programmer to get a USB DFU bootloader onto it.

There is a whisper that a new line of AVRs will come with native USB, but again I doubt that would be the main programming interface from the factory - it’ll still likely be UPDI. The other think to note about the new ATtiny and ATMEGA / AVR chips is that they do have the option of a UART bootloader, like the ATMEGA328Ps etc, so you could put a USB-serial adapter chip onboard. I realise things like the CH340G may also be hard to source in the simpler packages (that don’t need external crystal etc) but worth considering to widen you choice of chips if you don’t want to shell out for a programmer and just use UPDI to burn a UART bootloader.

I don’t know but you could perhaps use an ST Link to get an ST part up and running DFU for a cheaper entry, tooling-wise. From a quick look on Digikey, may of the available parts are STM32 L and G series. I think ST are trying to move people away from the F series, so learning L and G parts could be a good move. But I don’t know how much is out there on the bootloader/firmware front. I wouldn’t want to encourage you to go this route for you to get bogged down in drivers and registers, despite having fully assembled boards in your hands!

1 Like

Another suggestion fora USB-native microcontroller is the ESP32-S2. It might sound a little weird, since they are mostly known for cheap WiFi, but the price/value on them is favorable enough that we’ve been using them for projects that don’t need wireless, and just don’t add the wireless components. They are actually available:
https://mou.sr/3leiehI

They have a hardware USB bootloader, so no extra tools are needed.

A second suggestion if you are looking for low-cost development tools, is to use OpenOCD (https://openocd.org/) to convert a spare Raspberry Pi into a JTAG programmer/debugger- it can use the pins on the RPi to generate JTAG signals, so you only need some jumper wires to connect to your target project and get started.

2 Likes

It looks to me that some of the STM32 devices do come with a bootloader installed that allows reprogramming the flash through the USB DFU interface, I thought it was more common than it is. I have been looking through each datasheet at the Boot Modes section and some of them mention DFU but those are all the ones that are out of stock (like STM32F042x4 – https://www.st.com/resource/en/datasheet/stm32f042k6.pdf)

That’s good to know, I’ll look for those for sure

You’re right, I hadn’t thought of it because I wasn’t thinking WiFi, but that could work.

TIL that you can get ESP32’s from an authorized distributor in Canada and also that we have Mouser Canada too! These are amazing discoveries, thanks!

I’d be happy to look at some elements of this as I currently have two challenges:

  1. I need some dedicated buttons next to my keyboard to make debugging across the various IDEs I use simpler - so I just press the button in front of me rather than try to remember the keypress or wave the mouse around the toolbar.
  2. Eliminating the USB to serial from a PCB would be useful given that all the bits are there in something like the SAMD21. Inconjunction with that it would be super useful to be able to do some sort of firmware update with either a USB stick, SD card or, at worst, a small device that has an MCU + some sort of memory to plug in - at which point I could revert to SWD but if it’s done over serial a small OLED could also capture diagnostic data & or be used as a settings interface.

The Raspberry Pi PicoProbe config works well as an SWD programmer & OpenOCD programmer but I’ve not tried it on anything else. Alex Taradov’s Free-DAP looks excellent value (he sells them on Tindie for ~$15).

Generally I’m keen to keep life simple and I find the ATmega4808 as a SOIC does that nicely for me, I can wrap my head around the clock configuration, it’s got the useful extras like the CCL & Events and if the clients wants to co-develop the firmware, I can make it be an Arduino for which there are instructions on making a UPDI programmer from a Nano. Otherwise the SNAP debugger works nicely with Microchip(Atmel) Studio.

1 Like

Nice! That’s right in line with the direction I was heading.

More things to research, thanks!

ATMEGA4808 (same series as ATMEGA4809, 3208, 1609 etc etc - good code and peripheral compatibility between them all, as well as ATtiny 0 and 1 series). I’m sure it wouldn’t have lasted long but didn’t want you getting lost in searching for 4080 :slightly_smiling_face:

1 Like

Huh, thanks… I didn’t even notice since the search engines magically “fixed” it for me while searching, but it didn’t magically fix it in my hand written notes.

It doesn’t look like those parts can do what I want with USB, but they might come in to play for other side projects.

Like a game of Chinese Whispers! Seems it was a quote issue as I can’t see an edit on my post …

The ATmega4809 is its big brother, internals are the same but more pins. Generally I don’t find a use for all those pins and making a couple of boards on the bench with the human-pick-n-place is so much easier with SOIC.

1 Like

Right, ya… I see what I did there.

I think I’ve landed on the ESP32-S2 for my first run at this one, it seemed like the STM32 and ESP32 had the highest chance of success but availability of an ideal STM32 is low.

Since I’ll get WiFi and a temperature sensor basically for free I might as well feature creep this project into a data logger to log temperatures in different parts of the house. I’ll add a rechargeable battery and experiment with how long I can run it from a battery.

It has been a while since I have looked at the dev tools. What would you use? (I don’t use Arduino style, too little control)

1 Like

The official ESP-IDF is pretty well thought out: https://docs.espressif.com/projects/esp-idf/en/latest/esp32s2/index.html

I mostly use it with vim and command line tools, however for debugging I use Eclipse, and they also have a plugin for VS Code.

1 Like

I’ve played around with ESP32 dev boards through USART interfaces so I’m familiar with the ESP-IDF build environment and how to get the basics going, but the native USB is the new part I want to focus on for this experiment, so I’ll have to figure that out as I go.

Right, I’ve used vim and cmake so far, but I’ll want debugging too so I’ll add that to the list.