Learn how to set up tinyUSB on Raspberry Pi Pico using an HID example, covering configuration, compilation, and testing.
Key Takeaways
- tinyUSB enables flexible USB device and host development on supported microcontrollers.
- Raspberry Pi Pico is a suitable platform for running tinyUSB with HID examples.
- Proper configuration and build setup are crucial for successful compilation and deployment.
- udev rules simplify device access permissions on Linux systems.
- tinyUSB examples provide practical templates for embedded USB development.
What the video covers
- Introduction to tinyUSB as an open-source USB host/device stack for embedded systems supporting multiple MCUs.
- Explanation of hardware requirements for tinyUSB, including built-in USB controller and device/host mode support.
- Overview of supported microcontrollers including ESP32, STM series, and Raspberry Pi Pico (RP2040/RP2035).
- Step-by-step guide to cloning the Pico SDK and locating tinyUSB as a submodule with example projects.
- Details on the HID generic in/out example used as a template, including file structure and configuration.
- Description of tinyUSB configuration file settings such as USB class, endpoint sizes, and FreeRTOS support.
- Instructions on creating a CMakeLists.txt file for building the project and linking necessary libraries.
- Compilation process using CMake and Make, including troubleshooting missing files.
- Deploying the compiled UF2 file to Raspberry Pi Pico and verifying device functionality with LED blinking and lsusb.
- Using udev rules and Python scripts to interact with the tinyUSB HID device on the host system.
Chapters
- 00:00Introduction to tinyUSB and embedded programming
- 00:48Hardware requirements for tinyUSB
- 01:45Cloning and exploring the Pico SDK
- 02:34Overview of tinyUSB examples
- 03:21Selecting and copying the HID generic in/out example
- 04:48Configuring tinyUSB (tusb_config and USB descriptors)
- 05:58Main program structure and initialization
- 06:19Compiling the program with CMake and Make
- 10:54Deploying and testing on Raspberry Pi Pico
- 11:38Verifying USB device with lsusb and HID test script
Full Transcript — Download SRT & Markdown
Speaker A
Hi, today let's dive a little bit into embedded programming. I want to show you how you can get tinyUSB up and running on a Raspberry Pi Pico by using an HID example. But what is tinyUSB? Well, tinyUSB is basically magic. So tinyUSB
Speaker A
is an open-source cross-platform USB host device stack for embedded systems. It supports over 50 plus MCU families and it's available on a lot of different systems.
Speaker A
It supports various USB protocols and interfaces like CDC, HID, MSC, audio, and also host support. So you can use tinyUSB to create USB devices, but you can also use it to access or to let your microcontroller act as an
Speaker A
USB host. But one thing tinyUSB needs for this, the microcontroller needs to have a built-in USB controller in the chip and it must support host and device um con mode to be able to act as both. If it only supports device mode for
Speaker A
example, tinyUSB can also be used in device mode for the system. So down here we can see some microcontrollers which are supported. So the ESP32 is supported for example. We also have a lot of STM microcontrollers which we can see here. So
Speaker A
basically the whole ST microcontroller line with USB controller is supported and also our Raspberry Pi Pico chips, the RP2040 and the RP2035 is also supported. And as you can see here, we will use our Raspberry Pi Pico today to get started with
Speaker A
tinyUSB. Okay, the first thing we need is we need to clone the Pico SDK which can be found here. And to save us a little bit of time, I already did this. So here I have the Pico SDK installed. And if we are
Speaker A
taking a look into the submodules, we can see we have tinyUSB as a submodule in here. So under lib tinyUSB we can see um yeah, we can see basically the sources of tinyUSB. And the cool thing is tinyUSB comes with a bunch of
Speaker A
examples. And I will use the HID generic in out example as a template for today's video. So the generic in out example is basically an echo device. Everything you write to the device can be read back.
Speaker A
So yeah, let's take a look. So in lib tinyUSB examples, we can see all the examples and you can see it's structured in examples for USB devices, examples for USB hosts, and examples for both USB devices and hosts.
Speaker A
And in devices, we have a bunch of examples for various USB interfaces and protocols. And I will go with the HID generic in out example for today.
Speaker A
So let's take a look here. And this here is also quite handy. We also get a UDEV rule for tinyUSB or for our tinyUSB device, so we don't need root access for our tinyUSB devices, but we'll take a look at this later.
Speaker A
So let's go with the generic in out example. Here you can see we have some files.
Speaker A
And the important one is all these source files here. So besides of the library, we only need three source files to implement our tinyUSB device.
Speaker A
We have our tinyUSB configuration file in which we configure tinyUSB. We have a file in which we have our USB descriptors and we have a main file.
Speaker A
So what I will do now is I will just copy all of these these files here.
Speaker A
Or maybe let's first create a new folder which I will call HID hello in which we will implement our hello world HID device. And then we can copy all the files here into this folder.
Speaker A
Okay. Good. Then let's navigate into this new folder. And let's take a brief look at the different files we have in here.
Speaker A
So tinyUSB or tusb_config is the configuration file for tinyUSB. In here we can see for example, we can set the Yep, here. So tinyUSB also comes with FreeRTOS support. And here we can set if we want to use it or if we want to
Speaker A
run bare metal. In this case, we're running bare metal. Here we can see set some debug flags.
Speaker A
Here we are enabling the device stack because we only want to be a USB device in here. Here we are setting the speed.
Speaker A
Down here we have the device configuration. So we have one endpoint zero with a size of 64 bytes. Here we are telling it the USB class we want to use. In this case, we want to be an HID device.
Speaker A
And the buffer, the endpoint buffer for our HID device should be 64 bytes. And that's basically the config here.
Speaker A
In the USB descriptor file, we can also set some more things. For example, the USB vendor and product ID. So as a vendor, we use this free available one which is coffee. And the device ID is constructed up here.
Speaker A
And for the rest, well, we're just setting some fields here in this device descriptor so we have a valid USB device descriptor here.
Speaker A
And here in the main function, we're starting the stack and everything. So here we have a void function in which we are initializing the board and tinyUSB.
Speaker A
And then we have an endless while loop in which we're calling the tinyUSB device task and then a LED blinking task. So we're basically just toggling an LED here to see everything is alive. And later when we have our own tasks, we can add it in
Speaker A
this while one loop here. Okay, so how can we compile this program now? Well, therefore we need to take a look back to the Pico SDK readme.
Speaker A
So the first thing we need to do is we have to set this path here, the Pico SDK path.
Speaker A
So we can find our Pico SDK. So let's set this path to programming tinyUSB Pico SDK. Here is our SDK.
Speaker A
The next thing we need to do is we need to create a CMakeLists.txt file with this content here.
Speaker A
So let's create our CMake txt file and let's pass this content in here. The only thing I will change here is the name of the project which will be hello HID.
Speaker A
Okay, and then the next thing we have to do is whoop. [laughter] We have to add our executable and tell it the sources it should use for this executable. We have to tell it the libraries it should use and we have to
Speaker A
tell it to create a UF2 file for us. So let's add this code here and let's do some changes. So the name of the project should be hello HID. The sources are main.c and USB descriptor.c.
Speaker A
The libraries I will have to change just in a second because we need some more libraries.
Speaker A
And yep, this I also have to change. Okay, so first let me check if I've written this Oh, it's descriptors.
Speaker A
Yep, maybe let's check if I've written this correctly. Yes. Okay, and now when we are going into the Pico example folder, we can also see there are also some examples available for the um tinyUSB on the Raspberry Pi Pico. So
Speaker A
here under device um device HID composite, we also have a CMake.txt file and from here I also need to copy some stuff.
Speaker A
So we need some more libraries. So let's copy the libraries here. I think the first one is already included.
Speaker A
Yep, the Pico SDK standard library is already included. And then we will include tinyUSB device and tinyUSB board here.
Speaker A
And one thing we also need to do is um tinyUSB needs the tinyUSB config file available.
Speaker A
And with this line of code here, we make this file available to the SDK. So let's also add this.
Speaker A
But of course here I have to change this to hello HID. Okay, and now let's try to compile it.
Speaker A
So first we have to run CMake to create everything for us. Okay, and I have an error. I haven't defined this file here or I have this file is not found.
Speaker A
So let's search for this file in the Pico SDK. And then we can copy it over.
Speaker A
And now if I run CMake again, it should work. Okay, this is looking good. And if I take a look here, it has created a make file for us. So now I can build my project.
Speaker A
So let's build tiny USB. Already tiny USB example here. In the meantime, I can show you. So I have already depressed this button here and I have so the Raspberry Pi is in programming mode right now. So if I'm taking a look to
Speaker A
run media my username, I have this Raspberry Pi Raspberry Pi 2 folder in here and in here I can just copy the hello HID you UF2 file it has created here.
Speaker A
So this file I have to copy into run media Johannes Raspberry Pi 2. And now you can see this LED is blinking now because tiny USB is up and running.
Speaker A
And if I run lsusb, we can also see we have a tiny USB device here.
Speaker A
Well, let's take a closer look at this tiny USB device here. So we can see this device implements one human interface device class as an interface descriptor and this human interface devic
Speaker A
endpoint one in. And these endpoints are 64 bytes in size as we defined it in our tiny USB config.
Speaker A
And here we can also see some more parameters we could have set in the USB descriptor.c file. Yep, so tiny USB sets everything up for us.
Speaker A
Okay, but now I would like to interact with this device. And therefore, tiny USB gave us an example Python script which I want to use.
Speaker A
So let's go back to our pico-sdk lib tinyusb examples device HID generic in out and here we have this HID test.py script which I will copy in here.
Speaker A
We have also seen we have uh um tiny USB um udev rule file and in theory, all we would have to do is we have to copy this file into etc/udev/rules and then we have to reload the udev rules and it will work
Speaker A
on most Linux distributions. Unfortunately or luckily, I'm using Arch Linux and here I had to do a little bit of tinkering with this file because the thing is maybe let's take a look at the vanilla file first.
Speaker A
So 99 So basically, what this does is it adds all tiny USB devices and applies the user mode 666 so everyone is allowed to read and write to it and it assigns the group dialout. But the thing is on Arch Linux, there is no
Speaker A
or on my system, there is no dialout group. So I changed this file a little bit so you can see my user is in the wheel group and I changed this file a little bit.
Speaker A
Um udev rules tiny USB. So now every user who So um every tiny USB file will be up or will also be accessible from everyone in the group wheel and then this tiny USB rule is up and running. And of course,
Speaker A
then we have to run sudo udev admin control reload and sudo udev admin trigger to apply these udev file or this new udev rules.
Speaker A
But I won't do this now. Okay, and now we need to set up Python.
Speaker A
So the first thing I need to do is I have to create a new environment.
Speaker A
And I have to install two Python packages. So bin activate and now with pip, I can install HID and HIDAPI.
Speaker A
So these are the two files I need to install. Okay, they're installed now here on my system and now I can run this test file we have here.
Speaker A
So this file asks us to send a text to the HID device, hello HID and what the device does is it sends the string back.
Speaker A
So received from device hello HID device. Let's send another string, this is a text and then the device echoes this back.
Speaker A
Okay, cool. So I think that's it for today. We have successfully created a HID device on our Raspberry Pi Pico by using tiny USB. I hope you've enjoyed the video and learned something. In case you want to support my work, you can buy me
Speaker A
a coffee on buymeacoffee.com/nerdy Linux. So thanks for watching and goodbye.
Topics:tinyUSBRaspberry Pi PicoUSB HIDembedded programmingPico SDKUSB device stackUSB hostCMakeFreeRTOSUSB descriptors











