Radio from scratch · Chapter 13
USB from zero
- Chapter 12: the dongle's two chips (the RTL2832U talks USB; the tuner sits behind it)
- Chapter 3: what the bytes mean (I, Q, I, Q…, 127.5 = zero)
- Chapter 3: hexadecimal: numbers written in base 16, with a
0xin front. 0x10 is 16, 0xFF is 255. Each pair of hex digits is one byte.
Every chapter so far has worked on numbers that were already in the computer. Now: how do they get there? Between your web page and the dongle sit a USB cable, your operating system, and the browser. This chapter goes through each layer, using the real messages our driver sends, and ends with you talking to the dongle's chips yourself in four steps.
What a USB device says about itself
Before your code can ask the dongle for anything, it needs to know where to send settings and where the samples come out. The dongle tells it, in a fixed format, the moment it's plugged in. That's this section; everything after it uses the two numbers it finds.
Who speaks first on USB, the computer or the device?
- The computer (the host), always
- Whichever has data
- The device, to announce itself
USB has one boss: the computer, called the host. Devices never speak first; they only answer. When you plug something in, the host asks it a series of questions, and the device answers with descriptors: small, standardized blocks of facts about itself. Together they form a tree. Here's the tree of the dongle on this site's server, as it really answered (click each part):
The key word is endpoint: a numbered, one-way mailbox on the device. Endpoint 0 is the control channel every device has; endpoint 0x81 is where the radio samples come out. Everything a radio app does is either a message through endpoint 0 or a big read from endpoint 0x81.
On USB the host asks and the device answers: small control transfers through endpoint 0 change settings, bulk transfers from endpoint 0x81 carry the samples, and samples are lost only when nobody is asking.
Why are samples lost when the bus is ten times fast enough?
- On USB the host asks, the device answers: data moves only while a request is waiting.
- The dongle's own buffer holds only a moment's worth of samples.
- If no request is pending when its buffer fills, new samples have nowhere to go and are dropped.
- One 256 KiB request (about 262 000 bytes) takes 55 ms to fill at 2.4 MS/s, and the program needs time to send the next.
- So the driver keeps several requests queued at once: the bus's speed doesn't help if nobody is asking.
Where it breaks
Plug the dongle into an old USB 1.1 port, or a hub that runs at that "full speed", and the bus itself is too slow: 12 Mbit/s (megabits per second; 8 bits make a byte) is 1.5 MB/s (USB signaling rates), while 2.4 MS/s needs 4.8 MB/s. Streaming then drops about two thirds of the samples whatever the driver does. The fixes: a USB 2.0 port, or a lower sample rate (1.024 MS/s needs 2 MB/s, still too much; 0.25 MS/s would fit).
A control transfer, byte by byte
Why do we care about the exact bytes of a USB control transfer? Because the RTL2832U chip has no high-level "tune to 98.7 MHz" command: every action in chapter 12 and chapter 14 (powering on the chip, setting the sample rate, writing the tuner's PLL registers over I2C) is done by packing a register address into an 8-byte setup packet from the host. It always has five fields, in the same order:
- request type (1 byte): direction and kind. 0x40 means "host to device, vendor-specific"; 0xC0 means "device to host, vendor-specific". "Vendor-specific" means the meaning is defined by the chip's maker, not by the USB standard.
- request (1 byte): which command. The RTL2832U uses just one, 0.
- value and index (2 bytes each): the command's arguments. The RTL2832U packs which register into them (the table in the build guide).
- length (2 bytes): how many data bytes follow (to write) or are expected back (to read).
Two-byte numbers go on the wire lowest byte first ("little-endian"), so 0x2000 travels as 00 20. Pick an operation:
Look at the last one: reading the tuner's ID takes two USB transfers. The tuner isn't on the USB bus at all; it's on the RTL2832U's internal I2C wires. So the computer first asks the RTL2832U to send one byte over I2C ("start at register 0"), then asks it to read one byte back over I2C, and that byte comes back over USB. Talking to a chip through another chip is everyday life in electronics.
A two-byte value 0x1234 goes into a setup packet. In what order do its bytes travel on the wire?
- 12 34
- 34 12
Bulk transfers and bus time
At 2.4 million samples per second, each sample being one byte of I and one of Q, how many megabytes per second must cross the cable?
Control transfers are small and careful. The samples need the opposite: lots of data, fast. That's what bulk transfers are for. The host asks endpoint 0x81 for, say, 256 KiB (a KiB is 1024 bytes, so about 262 000 bytes); the dongle answers in 512-byte packets, as fast as the bus has room, until the request is filled.
USB 2.0 ("high speed", 480 Mbit/s: megabits per second; a byte is 8 bits, so that's 60 MB, megabytes, per second) divides time into slots of 125 µs, called microframes, each with room for up to 13 bulk packets of 512 bytes. How much of that does a radio stream need?
Less than a tenth of the bus. So when samples get lost, it's not because USB is full. It's because nobody was waiting for them: bulk data only moves while the host has a request pending, and the dongle's own buffer is tiny. That's why the driver always keeps several requests queued (build guide: the streaming timeline).
The math, step by step
\[ \text{rate} = f_s \times 2\,\tfrac{\text{bytes}}{\text{sample}} = 2.4\,\text{M} \times 2 = 4.8\,\text{MB/s} \]
\[ \text{bus} = 8000\,\tfrac{\text{microframes}}{\text{s}} \times 13 \times 512\,\text{B} \approx 53\,\text{MB/s}, \qquad \frac{4.8}{53} \approx 9\% \]
\[ \text{0x1234} \;\to\; \underbrace{\text{34}}_{\text{low byte first}}\;\text{12} \]
- A 256 KiB bulk request fills in 262 144 / 4.8 MB/s ≈ 55 ms, so the driver keeps several requests waiting at once and the bus never idles between them.
Who's allowed to talk to the dongle?
Between your page and the cable sit two gatekeepers:
- The operating system decides which driver owns each device. A keyboard or USB stick gets a standard driver. Our dongle's interface is vendor-specific (class 255), so it needs a generic driver that just passes messages through. macOS does that automatically. Windows needs one installed (WinUSB, via a tool called Zadig). Linux helpfully grabs the dongle with its TV tuner driver, which must be switched off first. And only one program at a time can own the device, so another radio program (an SDR app, for software-defined radio) that has it open blocks everyone else.
- The browser protects you from web pages. A page can't see your USB devices at all until you pick one in the browser's own pop-up list, which only appears after a click. Pages also only get access on secure (https://) addresses or on
localhost. After you've said yes once, the browser remembers the permission for that site.
Once both gatekeepers agree, the page gets a USBDevice object, and its methods map one-to-one onto everything in this chapter: open(), claimInterface(0), controlTransferOut/In() for endpoint 0, and transferIn(1, length) for endpoint 0x81.
Lab: talk to your dongle's chips
Four buttons, four steps, using only raw WebUSB calls: no driver. Plug in an RTL-SDR, open this page on an https:// or localhost address in Chrome, Edge or Opera, and press them in order. If the dongle is in use by something else (the server SDR, another program), step 2 will say so.
In code
Show the real code (optional: everything it does is explained above)
The same four steps as the lab above, written as a script that runs on a computer with the dongle plugged in. The heart of it is the setup-packet arithmetic:
const setup = (value, index) => ({ requestType: "vendor", recipient: "device", request: 0, value, index });
await dev.controlTransferOut(setup(0x2000, (1 << 8) | 0x10), new Uint8Array([0x09])); // USB block (1), write
await dev.controlTransferOut(setup(0x3000, (2 << 8) | 0x10), new Uint8Array([0xe8])); // system block (2): demod power on
await dev.controlTransferOut(setup((1 << 8) | 0x20, 0x10 | 1), new Uint8Array([0x18])); // demod page 1, reg 1: I2C repeater on
await dev.controlTransferOut(setup(0x34, (6 << 8) | 0x10), new Uint8Array([0x00])); // I2C (6) write to 0x34: "register 0"
const id = await dev.controlTransferIn(setup(0x34, 6 << 8), 1); // I2C read: 0x69
Its output on this site's dongle:
device: Nooelec NESDR SMArt v5, serial 28695281, USB 2.0, vendor 0xbda, product 0x2838
wrote 0x09 to USB register 0x2000 (value 0x2000, index 0x110)
read back USB register 0x2000: 0x09
demodulator powered on
I2C repeater on
tuner at I2C address 0x34, register 0: 0x69 → an R820T-family tuner
all checks passed
A lesson from writing it: the first version skipped "power on the demodulator", and the I2C step failed with "endpoint stalled". A stall is a device's way of saying "I can't do that right now". The demodulator part of the chip is powered down until switched on, and our driver powers it down when it closes. Real hardware is full of rules like that, which is what the next chapter's driver is made of.
Check yourself
What does endpoint 0x81 mean?
Endpoint number 1, in the "in" direction (device to computer): the top bit, 0x80, marks "in".
Why does reading the tuner's ID take two USB transfers?
The tuner isn't on USB; it's on the RTL2832U's I2C bus. The first transfer makes the RTL2832U write the register number over I2C; the second makes it read the answer over I2C and return it over USB.
Why can't a web page quietly read your USB devices?
The browser hides them until you choose one yourself in its pop-up list (opened only by a click), and only on secure pages.
At 2.4 MS/s, is USB 2.0 fast enough for the dongle? So why do samples sometimes get lost?
Easily: 4.8 MB/s is under a tenth of what the bus carries. Samples get lost when no request is waiting, because bulk data only flows while the host is asking for it.
Last updated