Worked System: A Sensor Hub
A phone counts steps all day while its large processor sleeps. A small chip beside it, the sensor hub, does the work. This page designs one on the review board, column by column.
You Will Be Able To
- Turn four sensor streams into bytes a second, a batch size and an interrupt count.
- Size the batch storage two ways, and say why a ring needs half the SRAM of a double buffer.
- Choose the slowest clock that meets the load, and show why it forces DMA on the host link.
- Lesson
- S11.10
- Time
- 30 min
- Runs on
- Your browser
- Check
- 5 questions
Every 62.5 ms, one reading of how the phone turns goes missing. The reading of how it moves comes over the same wires, at the same rate, and never does.
Nothing is broken. Which number did nobody write down?
Before You Start
You need Design Reviews: "Design This SoC": the eight columns of the board.
You need Serial Peripherals: SPI, I2C and Their Pins and DMA.
You need the watermark of Serial Peripherals: The UART: a request raised when a queue holds a set number of items.
| Question | |
|---|---|
| 1 | How do four sensors become one rate in bytes a second? |
| 2 | Why does a ring need about half the SRAM of a double buffer? |
| 3 | Why does the second sensor on a shared bus need a deeper FIFO? |
| 4 | Why does the slowest good clock force DMA on the host link? |
| 5 | The sensor bus runs twice as fast. What does the lab print last? |
The Idea: A Small Chip That Stays Awake
A large processor uses a lot of power just by being awake. A sensor hub is a small chip that stays awake in its place. It collects sensor readings and stores them.
Every few seconds it hands a whole batch to the large processor, the host processor, or host for short. The host wakes, takes the batch and sleeps again.
The first idea is a table of streams. Each sensor gives a sample of a few bytes, many times a second. Add a tag to each record, and the table gives bytes a second.
The second idea is where the data waits. It waits first in a FIFO inside each sensor, then in a batch in the SRAM of the hub. Both must be sized.
The third idea is that the clock comes last. It should be as slow as the work allows, to save power. A slow clock then rules some jobs out for the core, and those jobs need hardware.
New Words
| Word | Meaning |
|---|---|
| Sensor hub | A small, always-on chip that collects sensor readings so that a larger processor can sleep. |
| Host processor | The processor that the hub serves, called the host for short. Here it is the application processor of a phone. |
| Accelerometer, gyroscope, magnetometer | Sensors of motion, of turning and of the magnetic field. Each gives three values, of 2 bytes each. |
| Pressure sensor | A sensor of air pressure, used for height. It gives one value of 3 bytes here. |
| Record tag | Two bytes stored with each sample: which sensor, and the time since the last record. |
| Batch | The records stored between two visits of the host. The host takes a whole batch at once. |
| Sensor FIFO | A small queue of samples inside a sensor. It can raise a request when it reaches a watermark. |
| Double buffer | Two batch spaces. The hub fills one while the host reads the other, then they swap. |
| Ring buffer | One space that the hub writes round and round. The reader follows behind, and the writer must never pass it. |
| Host SPI target | The SPI port of the hub that faces the host. It answers the host and uses the clock the host drives. |
One Picture: The Board, Filled for a Hub
- File
- Processor
- Memory
- Clock / reset
- Control / FSM
- Power
- Debug
- Report
- 1. Requirements
Four streams, each with a size and a rate. A 2-byte tag goes with every record. The host takes a batch every 2 seconds.
- 2. Block diagram
One block for each job: two sensor buses and a timer for the tags. The host link moves the batch without the core.
- 3. Memory map
The two new controllers take the first pages of the expansion reserve. The ring of 14,758 bytes needs 32 KiB of SRAM.
- 4. Clocks and resets
The slowest clock that keeps the load under 25 percent. The host sees the reset of the hub, so it knows a batch was lost.
- 5. Interrupts
The accelerometer and gyroscope ask once for every 25 samples. The other two ask for each sample.
- 6. Power modes
The core sleeps between requests. Deep sleep would lose the ring, so it is used only when no batch is kept.
- 7. Debug
A missing sample must leave a mark. Counters show which sensor lost data and whether the host came late.
- 8. Risks
Each thin number is listed with a test. A faster clock, a lower watermark or a bus-recovery routine would retire one.
Read the diagram as text
- Requirements: four sensors, 925 samples a second; 7325 B/s with tags; a batch every 2 s
- Block diagram: core, SPI and I2C to the sensors, a SPI target and DMA to the host, timer, SRAM
- Memory map: ap_soc map; I2C at 1000_8000, host SPI at 1000_9000; SRAM of 32 KiB
- Clocks and resets: RC oscillator 25 MHz ÷ 16 = 1.5625 MHz; one reset, plus a line to the host
- Interrupts: 157 requests a second with watermarks, not 925; eight source numbers
- Power modes: run 24 percent, sleep the rest; SRAM stays powered; about 398 µW
- Debug: JTAG from reset; a lost-sample counter for each sensor; a late-read flag
- Risks: load 24 of 25 percent; gyroscope FIFO 26 of 32; the I2C bus can hang
- Requirements → Block diagram: what to build
- Block diagram → Memory map: what to place
- Memory map → Clocks and resets: what to time
- Clocks and resets → Interrupts: what to count
- Interrupts → Power modes: when to wake
- Power modes → Debug: what to see
- Debug → Risks: what is thin
- Risks → Requirements: change and run again
The eight columns of S11.4, filled for the hub. The next two pages fill the same board for very different products.
The Smallest Example, Worked by Hand
No simulator yet. Start with column 1, the streams. Every record is the sample plus a tag of 2 bytes.
| Sensor | Bus | Sample | Rate | Record × rate |
|---|---|---|---|---|
| Accelerometer | SPI | 6 bytes | 400 a second | 8 × 400 = 3200 B/s |
| Gyroscope | SPI | 6 bytes | 400 a second | 8 × 400 = 3200 B/s |
| Magnetometer | I2C | 6 bytes | 100 a second | 8 × 100 = 800 B/s |
| Pressure sensor | I2C | 3 bytes | 25 a second | 5 × 25 = 125 B/s |
| All four | 925 samples | 7325 B/s |
The host comes every 2 seconds, so a batch holds 14,650 bytes. Now size the storage two ways.
| Plan | Sum | Bytes |
|---|---|---|
| Double buffer | 2 × 14,650 | 29,300 |
| Ring buffer: one batch | 14,650 | 14,650 |
| ... plus what arrives during the read | 14,650 × 8 bits ÷ 8 Mb/s = 14,650 µs; 7325 B/s × 14,650 µs = 107.3 | 108 |
| Ring buffer in all | 14,650 + 108 | 14,758 |
Last, the interrupts of column 5. One request for every sample is 925 a second. With a watermark of 25 on the two motion sensors, each asks 400 ÷ 25 = 16 times. The total is 16 + 16 + 100 + 25 = 157.
The Rules
Rule 1: Count the record, not the sample.
The tag travels with every sample. Leave it out and every later column is short.
Rule 2: A watermark trades requests for depth.
Fewer requests mean more samples waiting in the sensor. The FIFO must hold the watermark plus what arrives before it is read.
Rule 3: On a shared bus, the second reader waits.
Size each FIFO for the wait behind the readers served before it, not only for its own read.
Rule 4: A ring holds one batch plus the read.
The writer keeps going while the host reads. Only what arrives during the read needs extra room.
Rule 5: Choose the slowest clock that meets the load, then check every byte-rate job.
A job that needs a few cycles for each byte is moved to hardware, here to DMA.
Rule 6: Data that must survive a sleep stays powered.
The ring lives in SRAM, so the hub may sleep but not lose the SRAM supply while a batch is kept.
Sources for These Rules
A DMA moves data without the core: DMA. A request raised at a watermark: Serial Peripherals: The UART.
Every sensor size, rate, watermark and cycle count is an exercise value, not a part's data sheet. The I2C cost of 9 bits a byte, plus 3 for start and stop, is a budget rule of this page. It is not checked against the I2C specification.
Sensor hubs beside phone processors, batching, and the ring against the double buffer are common practice. No book in our library describes them.
Now Run It: The Sizing Sheet
The lab at the end of this page is the sizing sheet of the hub. It works out every number on the board. Its last row comes from a run of the two sensor FIFOs.
records rate=7325 B/s batch=14650 B samples=925/s
links sensor_spi=8% i2c=2% host_read=14650 us
clock work=379420 cycles/s need=1517680 Hz core=1562500 Hz (divide by 16) load=24%
irq one_per_sample=925/s with_watermark=157/s
buffer double=29300 B ring=14758 B (arrive during a read: 108 B)
memory ring: need=17830 sram=32768 spare=45% double: need=32372 sram=32768 spare=1%
power awake=24% average=398 uW
host core_cycles_per_byte=1 dma_needed=1
fifo read_per_sample=112 us max_fill: accelerometer=25 gyroscope=26Rule 1, in the first row: 7325 bytes a second and a batch of 14,650, as in the hand table.
The links row. The motion sensors use 8 percent of their SPI bus, and the I2C sensors 2 percent. The host needs 14,650 µs to read a batch.
Rule 5, in the clock row. The work is 379,420 cycles a second. Under a 25 percent cap, the core needs 1,517,680 Hz. The RC oscillator divided by 16 gives 1,562,500 Hz, just enough.
Rule 5 again, in the host row. At that clock the core has one cycle for each byte of the host link. No program moves a byte in one cycle. The batch goes by DMA.
Rule 4, in the buffer and memory rows. The ring fits 32 KiB with 45 percent spare. The double buffer fits too, with 1 percent spare. That is a risk, not a plan.
Rules 2 and 3, in the last row. Each sample takes 112 µs on the sensor bus, so 25 samples take 2800 µs. The gyroscope waits behind the accelerometer.
A gyroscope sample arrives every 2500 µs. One arrives during the wait, so its FIFO reaches 26. A FIFO of 25 places would drop that sample, once every 62.5 ms.
This lab leaves things out. Its sensors are counters, its buses are delays, and its power numbers are exercise values. No sensor or SPI controller is modelled.
The Rest of the Board
Columns 3 to 8 need decisions as well as numbers. Here they are, column by column.
| Block | Base | Size | Why there |
|---|---|---|---|
| Boot ROM, CLINT, PLIC, UART0 to DMA | as in ap_soc | as in ap_soc | Old firmware and tools keep working. |
| I2C controller | 1000_8000 | 4 KiB | The first page of the expansion reserve. |
| Host SPI target | 1000_9000 | 4 KiB | The next page; the DMA writes each batch into it. |
| SRAM | 8000_0000 | 32 KiB | The ring and stack need 17,830 bytes. 16 KiB is too small. |
| Entry | Choice | Reason |
|---|---|---|
| Core clock | RC oscillator ÷ 16 | The slowest that meets the load. The bus and peripherals share it. |
| Reset | one reset domain; a reset line out to the host | After a reset the host must know that the batch is gone. |
| Sources 1 to 6 | as in ap_soc; GPIO carries the four data-ready pins | Each sensor pin raises a GPIO request. |
| Source 7 | I2C controller | A transfer is done, or the bus is stuck. |
| Source 8 | Host SPI target | The host has started or ended a read. |
| Entry | Choice | Reason |
|---|---|---|
| Power modes | run; sleep with every supply on; deep sleep only with no batch kept | Deep sleep would cut the SRAM and lose the ring. |
| Debug | JTAG from reset; a lost-sample counter for each sensor; a late-read flag | A missing sample must leave a mark the host can read. |
| Risk: load | 24 percent against a cap of 25 | Measure the real handlers. Divide by 8 if they are slower. |
| Risk: FIFO | gyroscope 26 of 32 places | Retest whenever the watermark or the bus speed changes. |
| Risk: I2C | a sensor can hold the data wire low | A routine that clocks the bus free, and a counter of its use. |
Why It Is Built This Way
| Choice | The alternative | What it buys, and what it costs |
|---|---|---|
| A ring buffer | A double buffer | Half the SRAM. The cost is two pointers and a rule that the writer never passes the reader. |
| Watermarks on the motion sensors | A request for every sample | 157 requests a second in place of 925, so the core sleeps longer. The FIFOs must be deeper. |
| DMA to the host | The core copies the batch | The only way at 1.5625 MHz. The cost is a DMA channel and a source number. |
| A slow clock | A fast clock, then a long sleep | Less power while running. A fast clock might save energy if sleep is very cheap. This lab does not measure that. |
The Trap: A FIFO Sized to Its Own Read
This is the hook. The gyroscope FIFO was given 25 places, the watermark. That is enough for its own read. It is not enough for the wait behind the accelerometer.
So one gyroscope sample was dropped every 62.5 ms. The lost-sample counter of column 7 showed it, and rule 3 explains it.
The second trap is the double buffer. It fits 32 KiB, so it passed review. With 1 percent spare, the first larger stack broke it.
The third trap is the slow clock. It met the load, so the host link was left to the core. The core had one cycle for each byte.
At Scale
This section looks ahead, to the hubs inside phones and watches.
The hub thinks
Real hubs run step counting, orientation and gesture detection. They wake the host only for an event, not on a timer.
More streams, same sums
Each new sensor is one more row of the stream table. The columns to its right are run again.
A product of its own
The hub gets its own firmware, updated by the host. Its boot and its debug lock follow Secure Boot.
Try It
Run the lab. Find the rows that give a clock, a buffer depth, an interrupt count and a memory size.
Set BATCH_MS to 1000. Which rows change? Which memory plan now fits 16 KiB?
Set WATERMARK to 30. Watch both the interrupt count and the gyroscope fill. A sensor FIFO of 32 places is the limit.
Names Printed by the Lab
| Name | Meaning |
|---|---|
| rate, batch, samples | Bytes stored each second with their tags; bytes in one batch; samples taken each second. |
| sensor_spi, i2c | The share of each sensor bus that the readings use. |
| host_read | The time to send one batch to the host over its SPI link. |
| work, need, core, load | Core cycles each second; the clock that would use exactly 25 percent; the clock chosen; the share it uses. |
| one_per_sample, with_watermark | Interrupt requests each second, without and with sensor watermarks. |
| double, ring | The batch storage as two whole batches, or as one batch plus what arrives during a read. |
| need, sram, spare | Storage plus 2048 bytes of stack and 1024 of driver data; the power of two that holds it; the share left. |
| awake, average | The share of time the core runs, and the average power at 1500 µW running and 50 µW asleep. |
| core_cycles_per_byte, dma_needed | Core cycles for each byte of the host link; 1 when that is fewer than 20. |
| read_per_sample, max_fill | The time to read one sample over the sensor SPI bus, and the most samples each FIFO held during the run. |
records rate=7325 B/s batch=14650 B samples=925/s links sensor_spi=8% i2c=2% host_read=14650 us clock work=379420 cycles/s need=1517680 Hz core=1562500 Hz (divide by 16) load=24% irq one_per_sample=925/s with_watermark=157/s buffer double=29300 B ring=14758 B (arrive during a read: 108 B) memory ring: need=17830 sram=32768 spare=45% double: need=32372 sram=32768 spare=1% power awake=24% average=398 uW host core_cycles_per_byte=1 dma_needed=1 fifo read_per_sample=112 us max_fill: accelerometer=25 gyroscope=26Check Yourself
Common Mistakes
Wrong Count only the sample bytes.
The tag goes with every record.
Fix Add the tag before summing the streams.
Wrong Give each sensor FIFO as many places as its watermark.
A reader on a shared bus may wait behind another.
Fix Add the samples that arrive during the longest wait.
Wrong Leave a byte-rate job to the core after choosing a slow clock.
At 1.5625 MHz the core has one cycle for each host byte.
Fix Check every byte-rate job against the clock, and give it DMA.
Remember
- Streams times records give bytes a second; the host interval gives the batch.
- A ring holds one batch plus what arrives during the read: about half a double buffer.
- A watermark cuts requests, and the FIFO must hold the watermark plus the longest wait.
- The slowest good clock is chosen last, and byte-rate jobs then go to hardware.