Designing MCLP, a multi-channel load-cell weighing core for FPGAs
How the MCLP core reads up to 64 AD7190 converters at the same instant, corrects each load cell, filters the sum into a weight and checks itself, and what testing found.
A load cell is a metal block with strain gauges bonded to it. The gauges form a bridge. When you put weight on the block, the bridge puts out a few millivolts. A kitchen or shop scale has one load cell, and a weigh-scale ADC and a small microcontroller read it.
Bigger scales are harder. A truck scale or a platform scale stands on several load cells, and no two cells have exactly the same sensitivity. The weight splits between the cells according to where the load sits. Unless you trim every cell, the same load reads differently in each corner. A weigh-in-motion lane adds another problem. It must weigh a vehicle as it rolls across, in the short time the vehicle spends on the platform.
I wanted one design for all of these cases. It reads every load cell at the same instant, corrects each one and filters the sum into a weight. Linux then takes the result for calibration, logging and the network.
I drew it first as a concept for a Zynq-7000. Then I built it as MCLP, a weighing core for AMD FPGAs. The core comes with a reference design for the ZC702 board, an 8-channel ADC card and PetaLinux software. The source is on GitHub.
The design at a glance
The core. Green runs at 20 MHz, blue at 50 MHz, beige on the AXI clock. T0 to T3 are the raw-capture taps.
Each load cell has its own converter. An SPI master reads all the converters at once. The samples then pass through one processing chain that all channels share: a decimating filter, a per-cell correction, a sum and a final low-pass filter. The chain puts the weight into registers that software reads, together with a stability flag and a weigh-in-motion estimate. A second path streams raw samples into DDR for calibration and diagnosis.
The number of channels is a parameter, from 1 to 64. The reference design uses eight. Version 2.0 adds an inference engine, which corrects the weigh-in-motion estimate with a small neural network. Part 5 and part 6 describe it.
Reading the load cells
Each load cell has an Analog Devices AD7190. This is a 24-bit sigma-delta ADC with a gain-128 amplifier at its input, a good match for millivolt signals. It runs at 4800 samples per second with its sinc4 filter. The bridge excitation also feeds the reference input of the converter. Thus the converter measures the ratio of signal to excitation, and drift in the excitation voltage cancels out.
To correct each corner, the core needs samples from all cells at the same moment. All eight converters run from one 4.9152 MHz oscillator. One SYNC line restarts their filters together, so they convert in lockstep. They also share the SPI clock, data-in and chip-select lines. Each converter sends its data back on its own pin. The SPI master reads all the pins in parallel, with one receive lane per converter.
Before an AD7190 streams data, it needs a reset, a write of its configuration registers and an internal calibration. I put that sequence in a small command list in RAM instead of a fixed state machine. Six commands cover it: transfer bits, pulse SYNC, wait a set time, wait for data ready, stream, and stop. The self-tests are also command lists. Software can load new lists without a change to the RTL.
Decimating with a CIC filter
A weight doesn't change 4800 times a second, and an average of many samples has less noise. So the first stage divides the rate by 8, to 600 Hz.
A TDM mux turns the parallel lanes into one stream and tags each sample with its channel number. A FIFO carries the stream into the 50 MHz processing clock. Every later stage uses the channel number to select the state and coefficients of that channel. In this way, one chain serves all channels.
The decimator is a cascaded integrator-comb (CIC) filter with three stages. In effect, it is three moving averages of 8 samples in a row. It uses only adders and delays, with no multipliers. Its response has nulls at every multiple of 600 Hz. At those frequencies, noise would fold into the band when the rate drops.
The integrators can overflow. In two's complement arithmetic the combs undo the wrap, and the output is correct. At 12 Hz, the cutoff of the final filter, the droop of the CIC is 0.017 dB, so it needs no compensation.
My first CIC had one adder per stage and kept every stage register in flip-flops. It used 717 LUTs and 1670 registers. The current version processes the stages of a sample one after the other with one shared 33-bit adder. It keeps the stage registers in distributed RAM. Out of context, it uses 194 LUTs and 87 registers.
Correcting each cell, then summing
Each channel then passes through two multiplications in Q2.30 fixed point:
gain/offset: y = sat32(((x - OFFSET[ch]) * GAIN[ch]) >>> 30)
corner trim: y = sat32((x * TRIM[ch]) >>> 30)
The first multiplication removes the zero offset of the cell and scales it. The second is the corner trim, which makes the contribution of each cell match the others. Both happen before the sum. After the sum, the errors of the separate channels can't be told apart.
The coefficients come from a calibration routine in software. The routine takes the offsets from one second of data with nothing on the scale. It solves the corner trims by least squares, from a test weight that sits over each cell in turn. Then it sets the span from the mass of the test weight.
The sum is exact. Up to 64 channels add into 40 bits, so nothing clips or wraps inside the adder. If a channel saturates in its own scaling, the core sets an overload flag.
The final filter and the weight
One 64-tap FIR filters the sum. The default response is a 12 Hz low-pass, a Hamming-windowed sinc, with linear phase and a delay of 52.5 ms. The filter keeps full precision internally. It computes one multiply-accumulate per clock, which at 600 Hz leaves plenty of time for all 64 taps. The data is wider than one DSP48E1 port, so the multiplier uses two DSP slices.
Software can load new coefficients while the filter runs. The new coefficients go into a second bank, and the core switches banks between two samples.
The weight is the filter output, shifted down and limited to 32 bits. The shift grows with the channel count, so all channels at full scale still fit. At eight channels, one ADC code is worth 32 counts of weight.
Static weight and weight in motion
The 600 Hz weight feeds three outputs.
RESULT is the weight that software reads. It updates at 50 Hz by default and at up to 600 Hz. Each update raises an interrupt.
The stability flag tells a static scale that the load is steady. The flag goes up after 60 consecutive weights stay within ±4096 counts of a reference. Any weight outside that band clears the flag and becomes the new reference. Both numbers are registers.
The weigh-in-motion estimator handles a vehicle that rolls across. A load starts when the weight rises above an entry threshold. It ends when the weight drops below an exit threshold.
The core keeps the weights of the load in a 1024-deep buffer. At the end of the load, it adds up the weights at or above a set share of the peak, 93% by default. This drops the ramps on and off the platform. Software divides the sum by the count to get the estimate. With the inference engine on, a corrected estimate follows about 1 ms later.
Raw data and self-test
For calibration and diagnosis, a raw-capture block can tap the chain at four points: the ADC codes, the CIC output, after gain and offset, or after the trim. Each frame becomes a record with a timestamp, the samples and a fault word. Capture runs continuously, for a set number of frames, or from a threshold trigger. In the reference design, an AXI DMA writes the records into a 4 MB ring in DDR. If the DMA falls behind, the block drops whole records. The weight path is not affected.
On a scale, a broken wire or a missing excitation doesn't always look like a fault. It can look like a wrong weight. The core has two ways to check itself.
The loopback BIST replaces the converter inputs with an AD7190 emulator inside the FPGA. The emulator answers the SPI master with the timing and data format of the AD7190. It can inject an error bit or a parity error on one channel. With a constant test pattern, software can predict every result and every raw record exactly. Thus the BIST checks the whole chain, the DMA included.
The AD7190 self-test checks the parts outside the FPGA with features of the converter itself:
- register readback for the SPI link
- reference detection for missing excitation
- burnout currents for a broken wire
- an internal short for offset and noise
Each channel also keeps sticky fault flags for error bits, missing reference, parity errors, stale data and loopback mismatches. A new fault raises an interrupt.
What testing found
The first versions of the core used AMD's CIC Compiler and FIR Compiler. I wanted to ship MCLP as one IP under its own license, so I wrote my own filters. They had to give exactly the same output as AMD's filters.
The FIR was the easier one. Vivado includes a bit-accurate C model of the FIR Compiler. I ran the model and my RTL on the same random input. The input included coefficient reloads and the worst case, where every tap and every sample is at its most negative value. All 1393 outputs matched.
There is no C model for the CIC. So I ran AMD's core and mine side by side in the Vivado simulator, and every output differed. The first output of AMD's core was the response to the first sample alone. This showed that AMD's core decimates on frames 0, 8, 16 and so on. Mine kept frames 7, 15, 23: the same filter, sampled at a different phase.
My earlier tests missed this. The system test used constant inputs, and with constant inputs the phase makes no difference once the filter settles. The unit test compared my RTL with my own model, which made the same assumption. After the fix, both filters match.
The weight scaling had a headroom problem. My first version shifted the FIR output right by 20 bits. With eight channels, the 32-bit result clipped at about 4.9 mV per channel at gain 128, well below full scale. To avoid the clip, software had to set every gain to 1/8. The shift now grows with the channel count, to 23 bits at eight channels. All channels at full scale fit, and the software gains default to 1.
The third problem was in the clock-domain crossings. The core has three clocks: 20 MHz for the converter side, 50 MHz for processing, and the AXI clock for registers. Every crossing uses an AMD XPM macro.
For simulation in Icarus, I wrote behavioral models of those macros. The models start their synchronizer flip-flops at X, as real hardware would without an initial value. With those models, the handshakes that carry configuration words never left X.
Every crossing now sets an initial value, and report_cdc reports nothing critical.
Size and timing
I synthesized the core on its own with eight channels. Without the inference engine, it uses 3288 LUTs, 6053 registers, 11 block RAM tiles and 13 DSP48E1 slices. With the engine, it uses 5252 LUTs, 7319 registers, 18.5 block RAM tiles and 23 DSP48E1 slices.
Each filter and each AMD core, synthesized on its own at 50 MHz, uses these resources:
| Block | LUTs | Registers | DSP48E1 | BRAM tiles |
|---|---|---|---|---|
| AMD CIC Compiler | 189 | 359 | 2 | 0 |
mclp_cic |
194 | 87 | 0 | 0 |
| AMD FIR Compiler | 383 | 406 | 3 | 0 |
mclp_fir |
163 | 173 | 2 | 1 |
The full ZC702 reference design uses 9783 LUTs, 14190 registers, 20 block RAM tiles and 23 DSP48E1 slices on the xc7z020-1. This count includes the inference engine, the processor system, the DMA and the interconnect. All timing constraints are met. The worst setup slack is +0.737 ns, and the worst hold slack is +0.021 ns.
Where it stands
The system test passes at 1, 8 and 64 channels, bit for bit from ADC code to result. A separate test runs AMD's real AXI DMA and interconnect with the Zynq-7000 verification IP. It checks the records that land in DDR. The console and the network service ran against the RTL from a PC and from the PetaLinux image in QEMU. Part 3 describes that setup.
The converter model and the controller both come from my reading of the AD7190 data sheet. I checked registers, commands, reset and calibration against ADI's no-OS and Linux drivers. But neither driver uses continuous read, SYNC or the parity bit. Until the card is built, those features rely on the data sheet alone.
The resolution must also be measured on the card. This version has no temperature or creep compensation. Legal-for-trade approval (OIML R76, NTEP) is for a finished scale.
The hardware is under the CERN-OHL-S v2, and the software is under the GPL-3.0. A commercial license is also available. Part 2 describes the ADC card.