Practice
Hive Scale Sampler
A bee farm weighs every hive to catch swarms early. When a swarm leaves, half the bees go with it, and the hive gets lighter within the hour. The scale under each hive feeds a 12-bit ADC, and the ADC talks SPI.
The old readout board sampled the data line one clock too late, just after the ADC had already shifted it. Every bit landed one place too high, so the logged weight came out doubled. Hive 7 swarmed in June. The chart showed a fat, thriving colony, and nobody opened the lid until the honey was gone.
Your task: build the SPI master that reads one sample from the ADC. Wait in idle until start pulses. Then pull cs_n low, run sclk at 1/4 of the system clock rate, and capture miso on each rising sclk edge. The ADC sends 2 null bits, then the 12 data bits, highest bit first. Put the reading on sample, pulse valid for 1 cycle, and go back to idle.
Interface
| Port | Direction | Type | Description |
|---|---|---|---|
clk | in | 1 bit | System clock |
rst | in | 1 bit | Synchronous reset (active high) |
start | in | 1 bit | Request one reading (one-cycle pulse) |
miso | in | 1 bit | Serial data from the ADC |
cs_n | out | 1 bit | ADC chip select (active low) |
sclk | out | 1 bit | Serial clock to the ADC (idles low) |
sample | out | 12-bit vector | Last completed reading, held |
valid | out | 1 bit | Reading complete (one-cycle pulse) |
Behavior
- When idle,
cs_nis high,sclkis low, andvalidis low - While idle,
starthigh at a rising clock edge begins a read:cs_nfalls on that same edge - Cycle numbering: cycle 0 is the first full cycle with
cs_nlow sclkdivides the system clock by 4: in each group of 4 cycles from cycle 0, it is low for 2 cycles, then high for 2- The first
sclkrising edge lands at cycle 2; the rest follow every 4 cycles, 14 rising edges in total cs_nstays low for cycles 0 through 55: exactly 14sclkperiods- The ADC updates
misoafter eachsclkfalling edge; the master capturesmisoon eachsclkrising edge - The first 2 captured bits are null bits, driven 0 by the ADC; the reading does not include them
- The next 12 captured bits are the reading, bit 11 first, bit 0 last
- At cycle 56 the read ends:
cs_nreturns high,sclkis low,validis high, andsamplepresents the new reading validis high for exactly 1 cycle per readsampleholds between reads and while a read is in progress; reset clears it to 0startpulses whilecs_nis low are ignored- A
startpulse on the cycle right aftervalidbegins a new read rsthigh at a rising clock edge cancels any read:cs_nhigh,sclklow,validlow,samplecleared
What the bench checks
- The testbench models the ADC: it loads a known code when
cs_nfalls and shifts it out onsclkfalling edges - The testbench checks
cs_n,sclk, andvalidagainst the fixed timeline on every cycle of every read - The testbench compares
sampleto the loaded code after each read, using codes that expose shifted or reversed bits - The testbench drives
misohigh whenevercs_nis high - The testbench pulses
startin the middle of one read and checks that no second read starts after it - The testbench pulses
starton the last cycle withcs_nlow and checks that no new read follows - The testbench starts one read on the cycle right after
validand expects a normal read - The testbench asserts
rstin the middle of one read and checks the outputs return to idle on the next cycle - The testbench checks
sampleon every cycle of every read: it must hold the previous reading until the read completes - The testbench checks that
sampleholds its value between reads
Constraints
everything is synchronous to clk; all outputs are registered.
this models the SPI master side of one fixed read: 2 null bits, then 12 data bits at a quarter-rate clock. The scale, the ADC's analog side, and the logging are out of scope.
Do not add ports.
Click Run to execute your code. Output will appear here.