Skip to main content

HP 34970A compatible firmware for Eletechsup UD85C08

Project | Article by Maarten Tromp | Published , updated | 2227 words

I replaced the AT-command firmware of a cheap 8-channel USB relay board with SCPI firmware that emulates an HP 34970A data acquisition unit. It was generated with Claude Code in about a day.

Flashing the Eletechsup UD85C08
Flashing the Eletechsup UD85C08

In this article:

Background

For a testrig (article coming soon) I needed to switch a couple of relays. I came across the Eletechsup UD85C08 8-channel USB relay board, on AliExpress, and it looked just the thing. It's an inexpensive digital input/output board with 8 changeover relay outputs and 8 optocoupler inputs, and you control it with AT commands over USB-serial.

It does the job, but the protocol feels like it was designed by someone who has heard of AT commands, but never actually used one. What mostly caught my attention was the inconsistent use of line terminators. Every other piece of measurement equipment on my bench speaks SCPI, and this board was the odd one out. So I started wondering: could I make SCPI-compatible firmware for it?

Actually my first impulse was to build my own digital input/output board, but I quickly realised the more sensible approach was to start with better firmware for the board I already had.

And that is what I did. The new firmware emulates an HP 34970A/34972A Data Acquisition / Data Logger Switch Unit on the inexpensive I/O board. It has been written for use with RigKit, but can be used anywhere.

Hardware

I traced one of my boards, and used Claude to research the MCU. In about 20 minutes I had the hardware mapped out.

The firmware runs on a MindMotion MM32F0010A1T: an Arm Cortex-M0 that can run at up to 48 MHz (the firmware keeps it at its 8 MHz reset clock, which is plenty), in a TSSOP20 package, with 16 kB of flash, 2 kB of RAM, 2 UARTs, 1 SPI, 1 I2C and an ADC. That seems hugely overkill for a simple relay board, but fits this project nicely.

This is how the pins are connected:

PinNameFunction
1PA11not connected
2PA12UART1_TX → CH340C
3PA3UART1_RX → CH340C
4PA14SWDCLK (debug interface), /reset
5PB0input 1
6PB1input 2
7GND/VSS
8PA1input 3
9VCC/VDD
10PA0shift register SRCLK
11PA4shift register SER
12PA5shift register /OE
13PA6shift register RCLK
14PA7power LED, active low
15PA8input 8
16PA9input 7
17PA10input 6
18PA13SWDIO (debug interface)
19PA15input 4
20PA2input 5

The USB connection is handled by a CH340C (SO16) USB to serial converter, with only TXD and RXD connected, to UART 1 of the MCU.

The relays are driven by an SM74HC595D (SO16) shift register, connected to a ULN2803A (SOP18) driver. The relays are marked SPE S3F-S-112D, which seems to be a non-existing part number. Search engines kept “correcting” it to existing part numbers, mostly the Yuanze Y3F-SS-112D, and I ended up using that datasheet for the switching times. They are changeover relays, with the common, normally open and normally closed contacts all on screw terminals. They are connected in an order that probably made sense for routing: shift register outputs 1 to 8 drive relays 1, 8, 7, 6, 5, 4, 2 and 3. Inputs use EL817-C445 optocouplers, and are connected directly to the MCU, without a shift register. On images I have seen, the 16-channel version of this board uses shift registers for the inputs as well as for the outputs.

On the side of the board are four pads, connected to VCC, SWDCLK, SWDIO and GND. That is exactly what is needed to program and debug the MCU over SWD (Serial Wire Debug), Arm's two-wire debug interface. It makes sense to have these available, as Eletechsup offers custom firmware as one of their services.

At this point the project looked doable, so I decided to go ahead with it. I didn't want to experiment with the boards that were in the testrig, so I ordered two extra: one to develop on, and one to keep stock for comparison. Plus a debug probe, to connect the computer to those SWD pads. By then, Claude (the free online version) had already generated a blinky, a Makefile and a first version of the firmware, from my description of the project and the datasheets, without ever seeing the hardware.

Connecting

I had never worked with an Arm MCU before, so it took some research to find out which hardware and software to use.

A debug probe sits between the computer and the MCU's debug interface. It's the Arm equivalent of an AVR programmer. After reading up on the options, I picked up a cheap J-Link OB clone.

SEGGER and Keil support the MCU, so that's a promising start, but favouring open source I chose OpenOCD. As it turned out, OpenOCD didn't support this MCU out of the box, but I hadn't realised.

When the boards and the probe came in, I asked Claude Code for a really simple test, to see if the probe could reach the MCU. I meant a test that Claude Code would write and I would run. I never expected it could control the programmer, but that is what happened. That came as a bit of a shock, as I didn't realize I had allowed this in the phrazing of my prompt. Later in the process I was actually happy with it: with access to the debugger and the serial port, Claude Code could debug, test and fuzz on the real hardware without needing my help. Still, my settings now require confirmation before it touches any hardware or anything that is not a project file.

First, the probe didn't see the MCU at all: the clock and data labels on my cheap probe were reversed. Then there was the missing OpenOCD support: Claude Code generated an OpenOCD configuration that drives the MCU's flash controller directly, following the user manual.

The next step was dumping the stock firmware. It's not that I'm really interested in it, but it would have been nice to have a backup. The dump came back as the same 32-bit word, 0x480e8808, repeated over all 16 kB: that's what a read-protected MCU looks like from the outside. I hadn't expected to be able to read out the firmware, but it was worth a try. Removing the protection is a bit of a ritual: you write a magic value to the option bytes, and the MCU erases itself. While it does, the debug connection drops, so it looks like it failed. Only after a power cycle can you see it worked. I have since done another board, and the sequence is: erase, power cycle, flash.

Then flashing the blinky failed, in an odd way. When the debugger wrote 16 bits to flash, a full 32-bit word was programmed, with the other half zeroes. Writing 32 bits instead hung the bus. The fix: a tiny 22-byte loader that OpenOCD copies to RAM, so the MCU's own CPU does the writes, which is how OpenOCD's proper flash drivers work. And one more: while the flash is busy, the MCU stalls its bus, and my cheap probe silently gave up whenever OpenOCD checked on the CPU during a stall. All of this was diagnosed and fixed in the Claude Code session, without my help. Only then could I finally start working with the MCU.

Despite all this, I have never had a new toolchain, probe, and board up and running this fast.

Firmware

With the blinky running, it was time for the actual firmware. The starting point was the firmware generated earlier with the online version of Claude, before the hardware arrived. Checking it against the MCU's user manual turned up two bugs: the UART clock was enabled with the wrong bit, so the serial port would never have worked, and the UART transmit pin was set to a mode the MCU calls reserved. Then I gave Claude Code the HP 34970A manual and the RigKit source, and let it rework the firmware to match. That took quite a while. I steered it in the right direction now and then, but that was about all I had to do.

The real hardware brought a few surprises. At 57600 baud a byte arrives every 174 µs, faster than a command is processed, and the UART only holds one byte, so commands sent back to back lost characters. Reception is now interrupt driven. When bytes are lost anyway, the whole line is discarded, rather than running what's left of it: a damaged (@101,105) could otherwise turn into a valid (@105).

The watchdog didn't behave as the MCU's manual says, twice. Starting the watchdog should switch on its clock, but it didn't. And the bit that should pause the watchdog only while a debugger halts the CPU froze it altogether, and even survived a reset. Both were found by a test that deliberately hangs the CPU over the debug interface, and checks that the board recovers.

The relay cycle counts are stored in flash, as the MCU has no EEPROM. To spare the flash, a page starts with a full record, followed by small incremental records. This makes the flash last almost four times as long.

The entire firmware was generated in a few hours. I have not written a single line of code for it, and that feels really weird. This must be what project managers feel like all the time.

For comparison, most of my firmware projects take at least one month.

Testing

To make sure the firmware behaved properly, especially since I didn't write it myself, I had Claude Code generate a full suite of unit tests, integration tests and fuzzing. (Yes, I am aware of the pitfalls of testing your own code.)

Besides analysis and tests on my laptop, tests running on the real board were added as well. Claude Code was given control over the USB serial port for this.

I pointed Claude Code to the RigKit source, and it used that to double check the firmware was compatible with RigKit. At some point I worried it would start changing the RigKit code as well, without asking for permission. It did, once: an option was added to RigKit's 34970A driver to work without hardware handshake, without asking first, though the change was reported right away. This also revealed that RigKit's driver used 2 stop bits, a leftover from another HP instrument, where the 34970A manual says 1. RigKit itself was also generated with Claude Code, in a separate session.

Finally, I tested it in RigKit, and it works exactly as intended.

All of this was within a day's work. Literally: about 2 hours with the free online version of Claude before the hardware arrived, for tracing the board, the blinky and the first version of the firmware, and about 5 hours on the day the boards were delivered. The day after I was mostly writing this article. Now and then something popped into my head, such as pulse counting or relay cycle counting, and Claude Code added it on the side.

Capabilities

The firmware emulates the HP 34970A closely enough to be recognised as one. It uses the default HP 34970A serial settings: 57600 baud, 8N1. There is no flow control, but it works just as well when the other side expects Xon/Xoff, the 34970A's default. It supports, among other things:

The result is a board that you can talk to like any other instrument on the bench, with the same kind of commands, and without having to special-case its line terminators.

The firmware source in the downloads directory comes with extensive documentation, if you want to know more.

Choices

Before settling on the HP 34970A, I wondered whether the HP 3499A/B/C switch/control system would be a closer match. It's a pure switch box, without the 34970A's multimeter, and its N2267A plug-in module has exactly 8 relays. Most of the commands are the same. But it has no totalizers, so pulse counting on the inputs would have become a non-HP extension. That settled it: the 34970A stays.

Many HP instruments, like my HP 6632B, check a firmware ROM checksum. I considered adding one, but the 34970A doesn't document one, and flashing already verifies every byte it writes. So I left it out, which keeps things simpler.

Open source

Everything in this project is done using free and open-source software and hardware wherever possible.

Accordingly, the software, article, and documentation created for this project are released into the public domain. You can find the relevant files in the downloads directory associated with this article.

The exception to this is anything I did not create myself, as it is not mine to give away. This includes the HP 34970A manual and command set, which remain subject to their original licences and copyright, and the Eletechsup board itself.

Afterword

It feels a bit like blasphemy, emulating a robust and expensive HP on an inexpensive DIO board. But it makes the little board a lot more useful, imho.

And if you notice something different about this article, it's also mostly written by Claude Code.

All in all, it was an interesting learning experience.